blob: 7ffa3a77825f488f60cd32a75bcef79bbd3475f6 [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
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02006 * Copyright (c) 2013-2016, 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"
50
51#define CREATE_TRACE_POINTS
52#include <trace/events/ufs.h>
53
54#ifdef CONFIG_DEBUG_FS
55
56static int ufshcd_tag_req_type(struct request *rq)
57{
58 int rq_type = TS_WRITE;
59
60 if (!rq || !(rq->cmd_type & REQ_TYPE_FS))
61 rq_type = TS_NOT_SUPPORTED;
62 else if (rq->cmd_flags & REQ_PREFLUSH)
63 rq_type = TS_FLUSH;
64 else if (rq_data_dir(rq) == READ)
65 rq_type = (rq->cmd_flags & REQ_URGENT) ?
66 TS_URGENT_READ : TS_READ;
67 else if (rq->cmd_flags & REQ_URGENT)
68 rq_type = TS_URGENT_WRITE;
69
70 return rq_type;
71}
72
73static void ufshcd_update_error_stats(struct ufs_hba *hba, int type)
74{
75 ufsdbg_set_err_state(hba);
76 if (type < UFS_ERR_MAX)
77 hba->ufs_stats.err_stats[type]++;
78}
79
80static void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag)
81{
82 struct request *rq =
83 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->request : NULL;
84 u64 **tag_stats = hba->ufs_stats.tag_stats;
85 int rq_type;
86
87 if (!hba->ufs_stats.enabled)
88 return;
89
90 tag_stats[tag][TS_TAG]++;
91 if (!rq || !(rq->cmd_type & REQ_TYPE_FS))
92 return;
93
94 WARN_ON(hba->ufs_stats.q_depth > hba->nutrs);
95 rq_type = ufshcd_tag_req_type(rq);
96 if (!(rq_type < 0 || rq_type > TS_NUM_STATS))
97 tag_stats[hba->ufs_stats.q_depth++][rq_type]++;
98}
99
100static void ufshcd_update_tag_stats_completion(struct ufs_hba *hba,
101 struct scsi_cmnd *cmd)
102{
103 struct request *rq = cmd ? cmd->request : NULL;
104
105 if (rq && rq->cmd_type & REQ_TYPE_FS)
106 hba->ufs_stats.q_depth--;
107}
108
109static void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
110{
111 int rq_type;
112 struct request *rq = lrbp->cmd ? lrbp->cmd->request : NULL;
113 s64 delta = ktime_us_delta(lrbp->complete_time_stamp,
114 lrbp->issue_time_stamp);
115
116 /* update general request statistics */
117 if (hba->ufs_stats.req_stats[TS_TAG].count == 0)
118 hba->ufs_stats.req_stats[TS_TAG].min = delta;
119 hba->ufs_stats.req_stats[TS_TAG].count++;
120 hba->ufs_stats.req_stats[TS_TAG].sum += delta;
121 if (delta > hba->ufs_stats.req_stats[TS_TAG].max)
122 hba->ufs_stats.req_stats[TS_TAG].max = delta;
123 if (delta < hba->ufs_stats.req_stats[TS_TAG].min)
124 hba->ufs_stats.req_stats[TS_TAG].min = delta;
125
126 rq_type = ufshcd_tag_req_type(rq);
127 if (rq_type == TS_NOT_SUPPORTED)
128 return;
129
130 /* update request type specific statistics */
131 if (hba->ufs_stats.req_stats[rq_type].count == 0)
132 hba->ufs_stats.req_stats[rq_type].min = delta;
133 hba->ufs_stats.req_stats[rq_type].count++;
134 hba->ufs_stats.req_stats[rq_type].sum += delta;
135 if (delta > hba->ufs_stats.req_stats[rq_type].max)
136 hba->ufs_stats.req_stats[rq_type].max = delta;
137 if (delta < hba->ufs_stats.req_stats[rq_type].min)
138 hba->ufs_stats.req_stats[rq_type].min = delta;
139}
140
141static void
142ufshcd_update_query_stats(struct ufs_hba *hba, enum query_opcode opcode, u8 idn)
143{
144 if (opcode < UPIU_QUERY_OPCODE_MAX && idn < MAX_QUERY_IDN)
145 hba->ufs_stats.query_stats_arr[opcode][idn]++;
146}
147
148#else
149static inline void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag)
150{
151}
152
153static inline void ufshcd_update_tag_stats_completion(struct ufs_hba *hba,
154 struct scsi_cmnd *cmd)
155{
156}
157
158static inline void ufshcd_update_error_stats(struct ufs_hba *hba, int type)
159{
160}
161
162static inline
163void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
164{
165}
166
167static inline
168void ufshcd_update_query_stats(struct ufs_hba *hba,
169 enum query_opcode opcode, u8 idn)
170{
171}
172#endif
173
174#define UFSHCD_REQ_SENSE_SIZE 18
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530175
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530176#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
177 UTP_TASK_REQ_COMPL |\
178 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530179/* UIC command timeout, unit: ms */
180#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530181
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530182/* NOP OUT retries waiting for NOP IN response */
183#define NOP_OUT_RETRIES 10
184/* Timeout after 30 msecs if NOP OUT hangs without response */
185#define NOP_OUT_TIMEOUT 30 /* msecs */
186
Dolev Raviv68078d52013-07-30 00:35:58 +0530187/* Query request retries */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700188#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +0530189/* Query request timeout */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700190#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +0530191
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530192/* Task management command timeout */
193#define TM_CMD_TIMEOUT 100 /* msecs */
194
Yaniv Gardi64238fb2016-02-01 15:02:43 +0200195/* maximum number of retries for a general UIC command */
196#define UFS_UIC_COMMAND_RETRIES 3
197
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300198/* maximum number of link-startup retries */
199#define DME_LINKSTARTUP_RETRIES 3
200
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +0200201/* Maximum retries for Hibern8 enter */
202#define UIC_HIBERN8_ENTER_RETRIES 3
203
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300204/* maximum number of reset retries before giving up */
205#define MAX_HOST_RESET_RETRIES 5
206
Dolev Raviv68078d52013-07-30 00:35:58 +0530207/* Expose the flag value from utp_upiu_query.value */
208#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
209
Seungwon Jeon7d568652013-08-31 21:40:20 +0530210/* Interrupt aggregation default timeout, unit: 40us */
211#define INT_AGGR_DEF_TO 0x02
212
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700213/* default value of auto suspend is 3 seconds */
214#define UFSHCD_AUTO_SUSPEND_DELAY_MS 3000 /* millisecs */
215
216#define UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE 10
217#define UFSHCD_CLK_GATING_DELAY_MS_PERF 50
218
219/* IOCTL opcode for command - ufs set device read only */
220#define UFS_IOCTL_BLKROSET BLKROSET
221
222#define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
223
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300224#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
225 ({ \
226 int _ret; \
227 if (_on) \
228 _ret = ufshcd_enable_vreg(_dev, _vreg); \
229 else \
230 _ret = ufshcd_disable_vreg(_dev, _vreg); \
231 _ret; \
232 })
233
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700234#define ufshcd_hex_dump(prefix_str, buf, len) \
235print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false)
236
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300237static u32 ufs_query_desc_max_size[] = {
238 QUERY_DESC_DEVICE_MAX_SIZE,
239 QUERY_DESC_CONFIGURAION_MAX_SIZE,
240 QUERY_DESC_UNIT_MAX_SIZE,
241 QUERY_DESC_RFU_MAX_SIZE,
242 QUERY_DESC_INTERCONNECT_MAX_SIZE,
243 QUERY_DESC_STRING_MAX_SIZE,
244 QUERY_DESC_RFU_MAX_SIZE,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700245 QUERY_DESC_GEOMETRY_MAZ_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300246 QUERY_DESC_POWER_MAX_SIZE,
247 QUERY_DESC_RFU_MAX_SIZE,
248};
249
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530250enum {
251 UFSHCD_MAX_CHANNEL = 0,
252 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530253 UFSHCD_CMD_PER_LUN = 32,
254 UFSHCD_CAN_QUEUE = 32,
255};
256
257/* UFSHCD states */
258enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530259 UFSHCD_STATE_RESET,
260 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530261 UFSHCD_STATE_OPERATIONAL,
262};
263
264/* UFSHCD error handling flags */
265enum {
266 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530267};
268
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530269/* UFSHCD UIC layer error flags */
270enum {
271 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200272 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
273 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
274 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
275 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
276 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530277};
278
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530279/* Interrupt configuration options */
280enum {
281 UFSHCD_INT_DISABLE,
282 UFSHCD_INT_ENABLE,
283 UFSHCD_INT_CLEAR,
284};
285
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700286#define DEFAULT_UFSHCD_DBG_PRINT_EN UFSHCD_DBG_PRINT_ALL
287
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530288#define ufshcd_set_eh_in_progress(h) \
289 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
290#define ufshcd_eh_in_progress(h) \
291 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
292#define ufshcd_clear_eh_in_progress(h) \
293 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
294
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300295#define ufshcd_set_ufs_dev_active(h) \
296 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
297#define ufshcd_set_ufs_dev_sleep(h) \
298 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
299#define ufshcd_set_ufs_dev_poweroff(h) \
300 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
301#define ufshcd_is_ufs_dev_active(h) \
302 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
303#define ufshcd_is_ufs_dev_sleep(h) \
304 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
305#define ufshcd_is_ufs_dev_poweroff(h) \
306 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
307
308static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
309 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
310 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
311 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
312 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
313 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
314 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
315};
316
317static inline enum ufs_dev_pwr_mode
318ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
319{
320 return ufs_pm_lvl_states[lvl].dev_state;
321}
322
323static inline enum uic_link_state
324ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
325{
326 return ufs_pm_lvl_states[lvl].link_state;
327}
328
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700329static inline enum ufs_pm_level
330ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
331 enum uic_link_state link_state)
332{
333 enum ufs_pm_level lvl;
334
335 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
336 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
337 (ufs_pm_lvl_states[lvl].link_state == link_state))
338 return lvl;
339 }
340
341 /* if no match found, return the level 0 */
342 return UFS_PM_LVL_0;
343}
344
345static inline bool ufshcd_is_valid_pm_lvl(int lvl)
346{
347 if (lvl >= 0 && lvl < ARRAY_SIZE(ufs_pm_lvl_states))
348 return true;
349 else
350 return false;
351}
352
353static irqreturn_t ufshcd_intr(int irq, void *__hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530354static void ufshcd_tmc_handler(struct ufs_hba *hba);
355static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530356static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700357static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530358static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300359static void ufshcd_hba_exit(struct ufs_hba *hba);
360static int ufshcd_probe_hba(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700361static int ufshcd_enable_clocks(struct ufs_hba *hba);
362static int ufshcd_disable_clocks(struct ufs_hba *hba,
363 bool is_gating_context);
364static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba,
365 bool is_gating_context);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200366static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300367static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700368static inline void ufshcd_save_tstamp_of_last_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300369static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700370static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
371static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
372static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
373static void ufshcd_release_all(struct ufs_hba *hba);
374static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
375static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
376
Yaniv Gardi14497322016-02-01 15:02:39 +0200377static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
378{
379 return tag >= 0 && tag < hba->nutrs;
380}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300381
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700382static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300383{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300384 if (!hba->is_irq_enabled) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700385 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300386 hba->is_irq_enabled = true;
387 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300388}
389
390static inline void ufshcd_disable_irq(struct ufs_hba *hba)
391{
392 if (hba->is_irq_enabled) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700393 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300394 hba->is_irq_enabled = false;
395 }
396}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530397
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700398void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
399{
400 unsigned long flags;
401 bool unblock = false;
402
403 spin_lock_irqsave(hba->host->host_lock, flags);
404 hba->scsi_block_reqs_cnt--;
405 unblock = !hba->scsi_block_reqs_cnt;
406 spin_unlock_irqrestore(hba->host->host_lock, flags);
407 if (unblock)
408 scsi_unblock_requests(hba->host);
409}
410EXPORT_SYMBOL(ufshcd_scsi_unblock_requests);
411
412static inline void __ufshcd_scsi_block_requests(struct ufs_hba *hba)
413{
414 if (!hba->scsi_block_reqs_cnt++)
415 scsi_block_requests(hba->host);
416}
417
418void ufshcd_scsi_block_requests(struct ufs_hba *hba)
419{
420 unsigned long flags;
421
422 spin_lock_irqsave(hba->host->host_lock, flags);
423 __ufshcd_scsi_block_requests(hba);
424 spin_unlock_irqrestore(hba->host->host_lock, flags);
425}
426EXPORT_SYMBOL(ufshcd_scsi_block_requests);
427
Yaniv Gardib573d482016-03-10 17:37:09 +0200428/* replace non-printable or non-ASCII characters with spaces */
429static inline void ufshcd_remove_non_printable(char *val)
430{
431 if (!val)
432 return;
433
434 if (*val < 0x20 || *val > 0x7e)
435 *val = ' ';
436}
437
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700438#ifdef CONFIG_TRACEPOINTS
439static void ufshcd_add_command_trace(struct ufs_hba *hba,
440 unsigned int tag, const char *str)
441{
442 sector_t lba = -1;
443 u8 opcode = 0;
444 u32 intr, doorbell;
445 struct ufshcd_lrb *lrbp;
446 int transfer_len = -1;
447
448 lrbp = &hba->lrb[tag];
449
450 if (lrbp->cmd) { /* data phase exists */
451 opcode = (u8)(*lrbp->cmd->cmnd);
452 if ((opcode == READ_10) || (opcode == WRITE_10)) {
453 /*
454 * Currently we only fully trace read(10) and write(10)
455 * commands
456 */
457 if (lrbp->cmd->request && lrbp->cmd->request->bio)
458 lba =
459 lrbp->cmd->request->bio->bi_iter.bi_sector;
460 transfer_len = be32_to_cpu(
461 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
462 }
463 }
464
465 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
466 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
467 trace_ufshcd_command(dev_name(hba->dev), str, tag,
468 doorbell, transfer_len, intr, lba, opcode);
469}
470
471static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba,
472 unsigned int tag, const char *str)
473{
474 if (trace_ufshcd_command_enabled())
475 ufshcd_add_command_trace(hba, tag, str);
476}
477#else
478static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba,
479 unsigned int tag, const char *str)
480{
481}
482#endif
483
484static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
485{
486 struct ufs_clk_info *clki;
487 struct list_head *head = &hba->clk_list_head;
488
489 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_CLK_FREQ_EN))
490 return;
491
492 if (!head || list_empty(head))
493 return;
494
495 list_for_each_entry(clki, head, list) {
496 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
497 clki->max_freq)
498 dev_err(hba->dev, "clk: %s, rate: %u\n",
499 clki->name, clki->curr_freq);
500 }
501}
502
503static void ufshcd_print_uic_err_hist(struct ufs_hba *hba,
504 struct ufs_uic_err_reg_hist *err_hist, char *err_name)
505{
506 int i;
507
508 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_UIC_ERR_HIST_EN))
509 return;
510
511 for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) {
512 int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH;
513
514 if (err_hist->reg[p] == 0)
515 continue;
516 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us", err_name, i,
517 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
518 }
519}
520
521static void ufshcd_print_host_regs(struct ufs_hba *hba)
522{
523 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_REGS_EN))
524 return;
525
526 /*
527 * hex_dump reads its data without the readl macro. This might
528 * cause inconsistency issues on some platform, as the printed
529 * values may be from cache and not the most recent value.
530 * To know whether you are looking at an un-cached version verify
531 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
532 * during platform/pci probe function.
533 */
534 ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE);
535 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x",
536 hba->ufs_version, hba->capabilities);
537 dev_err(hba->dev,
538 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x",
539 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
540 dev_err(hba->dev,
541 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d",
542 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
543 hba->ufs_stats.hibern8_exit_cnt);
544
545 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
546 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
547 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
548 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
549 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
550
551 ufshcd_print_clk_freqs(hba);
552
553 ufshcd_vops_dbg_register_dump(hba);
554}
555
556static
557void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
558{
559 struct ufshcd_lrb *lrbp;
560 int prdt_length;
561 int tag;
562
563 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TRS_EN))
564 return;
565
566 for_each_set_bit(tag, &bitmap, hba->nutrs) {
567 lrbp = &hba->lrb[tag];
568
569 dev_err(hba->dev, "UPIU[%d] - issue time %lld us",
570 tag, ktime_to_us(lrbp->issue_time_stamp));
571 dev_err(hba->dev,
572 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx",
573 tag, (u64)lrbp->utrd_dma_addr);
574 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
575 sizeof(struct utp_transfer_req_desc));
576 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx", tag,
577 (u64)lrbp->ucd_req_dma_addr);
578 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
579 sizeof(struct utp_upiu_req));
580 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx", tag,
581 (u64)lrbp->ucd_rsp_dma_addr);
582 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
583 sizeof(struct utp_upiu_rsp));
584 prdt_length =
585 le16_to_cpu(lrbp->utr_descriptor_ptr->prd_table_length);
586 dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries phys@0x%llx",
587 tag, prdt_length, (u64)lrbp->ucd_prdt_dma_addr);
588 if (pr_prdt)
589 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
590 sizeof(struct ufshcd_sg_entry) * prdt_length);
591 }
592}
593
594static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
595{
596 struct utp_task_req_desc *tmrdp;
597 int tag;
598
599 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TMRS_EN))
600 return;
601
602 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
603 tmrdp = &hba->utmrdl_base_addr[tag];
604 dev_err(hba->dev, "TM[%d] - Task Management Header", tag);
605 ufshcd_hex_dump("TM TRD: ", &tmrdp->header,
606 sizeof(struct request_desc_header));
607 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU",
608 tag);
609 ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu,
610 sizeof(struct utp_upiu_req));
611 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU",
612 tag);
613 ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu,
614 sizeof(struct utp_task_req_desc));
615 }
616}
617
618static void ufshcd_print_host_state(struct ufs_hba *hba)
619{
620 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_STATE_EN))
621 return;
622
623 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
624 dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n",
625 hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs);
626 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x, saved_ce_err=0x%x\n",
627 hba->saved_err, hba->saved_uic_err, hba->saved_ce_err);
628 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
629 hba->curr_dev_pwr_mode, hba->uic_link_state);
630 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
631 hba->pm_op_in_progress, hba->is_sys_suspended);
632 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
633 hba->auto_bkops_enabled, hba->host->host_self_blocked);
634 dev_err(hba->dev, "Clk gate=%d, hibern8 on idle=%d\n",
635 hba->clk_gating.state, hba->hibern8_on_idle.state);
636 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
637 hba->eh_flags, hba->req_abort_count);
638 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
639 hba->capabilities, hba->caps);
640 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -0800641 hba->dev_info.quirks);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700642}
643
644/**
645 * ufshcd_print_pwr_info - print power params as saved in hba
646 * power info
647 * @hba: per-adapter instance
648 */
649static void ufshcd_print_pwr_info(struct ufs_hba *hba)
650{
651 char *names[] = {
652 "INVALID MODE",
653 "FAST MODE",
654 "SLOW_MODE",
655 "INVALID MODE",
656 "FASTAUTO_MODE",
657 "SLOWAUTO_MODE",
658 "INVALID MODE",
659 };
660
661 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_PWR_EN))
662 return;
663
664 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
665 __func__,
666 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
667 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
668 names[hba->pwr_info.pwr_rx],
669 names[hba->pwr_info.pwr_tx],
670 hba->pwr_info.hs_rate);
671}
672
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530673/*
674 * ufshcd_wait_for_register - wait for register value to change
675 * @hba - per-adapter interface
676 * @reg - mmio register offset
677 * @mask - mask to apply to read register value
678 * @val - wait condition
679 * @interval_us - polling interval in microsecs
680 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200681 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530682 * Returns -ETIMEDOUT on error, zero on success
683 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200684int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
685 u32 val, unsigned long interval_us,
686 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530687{
688 int err = 0;
689 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
690
691 /* ignore bits that we don't intend to wait on */
692 val = val & mask;
693
694 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200695 if (can_sleep)
696 usleep_range(interval_us, interval_us + 50);
697 else
698 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530699 if (time_after(jiffies, timeout)) {
700 if ((ufshcd_readl(hba, reg) & mask) != val)
701 err = -ETIMEDOUT;
702 break;
703 }
704 }
705
706 return err;
707}
708
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530709/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530710 * ufshcd_get_intr_mask - Get the interrupt bit mask
711 * @hba - Pointer to adapter instance
712 *
713 * Returns interrupt bit mask per version
714 */
715static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
716{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700717 u32 intr_mask = 0;
718
719 switch (hba->ufs_version) {
720 case UFSHCI_VERSION_10:
721 intr_mask = INTERRUPT_MASK_ALL_VER_10;
722 break;
723 /* allow fall through */
724 case UFSHCI_VERSION_11:
725 case UFSHCI_VERSION_20:
726 intr_mask = INTERRUPT_MASK_ALL_VER_11;
727 break;
728 /* allow fall through */
729 case UFSHCI_VERSION_21:
730 default:
731 intr_mask = INTERRUPT_MASK_ALL_VER_21;
732 }
733
734 if (!ufshcd_is_crypto_supported(hba))
735 intr_mask &= ~CRYPTO_ENGINE_FATAL_ERROR;
736
737 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530738}
739
740/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530741 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
742 * @hba - Pointer to adapter instance
743 *
744 * Returns UFSHCI version supported by the controller
745 */
746static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
747{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200748 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
749 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300750
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530751 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530752}
753
754/**
755 * ufshcd_is_device_present - Check if any device connected to
756 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300757 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530758 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530759 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530760 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300761static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530762{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300763 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
764 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530765}
766
767/**
768 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
769 * @lrb: pointer to local command reference block
770 *
771 * This function is used to get the OCS field from UTRD
772 * Returns the OCS field in the UTRD
773 */
774static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
775{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530776 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530777}
778
779/**
780 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
781 * @task_req_descp: pointer to utp_task_req_desc structure
782 *
783 * This function is used to get the OCS field from UTMRD
784 * Returns the OCS field in the UTMRD
785 */
786static inline int
787ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
788{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530789 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530790}
791
792/**
793 * ufshcd_get_tm_free_slot - get a free slot for task management request
794 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530795 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530796 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530797 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
798 * Returns 0 if free slot is not available, else return 1 with tag value
799 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530800 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530801static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530802{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530803 int tag;
804 bool ret = false;
805
806 if (!free_slot)
807 goto out;
808
809 do {
810 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
811 if (tag >= hba->nutmrs)
812 goto out;
813 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
814
815 *free_slot = tag;
816 ret = true;
817out:
818 return ret;
819}
820
821static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
822{
823 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530824}
825
826/**
827 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
828 * @hba: per adapter instance
829 * @pos: position of the bit to be cleared
830 */
831static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
832{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530833 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530834}
835
836/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200837 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
838 * @hba: per adapter instance
839 * @tag: position of the bit to be cleared
840 */
841static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
842{
843 __clear_bit(tag, &hba->outstanding_reqs);
844}
845
846/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530847 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
848 * @reg: Register value of host controller status
849 *
850 * Returns integer, 0 on Success and positive value if failed
851 */
852static inline int ufshcd_get_lists_status(u32 reg)
853{
854 /*
855 * The mask 0xFF is for the following HCS register bits
856 * Bit Description
857 * 0 Device Present
858 * 1 UTRLRDY
859 * 2 UTMRLRDY
860 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200861 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530862 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200863 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530864}
865
866/**
867 * ufshcd_get_uic_cmd_result - Get the UIC command result
868 * @hba: Pointer to adapter instance
869 *
870 * This function gets the result of UIC command completion
871 * Returns 0 on success, non zero value on error
872 */
873static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
874{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530875 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530876 MASK_UIC_COMMAND_RESULT;
877}
878
879/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530880 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
881 * @hba: Pointer to adapter instance
882 *
883 * This function gets UIC command argument3
884 * Returns 0 on success, non zero value on error
885 */
886static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
887{
888 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
889}
890
891/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530892 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530893 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530894 */
895static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530896ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530897{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530898 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530899}
900
901/**
902 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
903 * @ucd_rsp_ptr: pointer to response UPIU
904 *
905 * This function gets the response status and scsi_status from response UPIU
906 * Returns the response result code.
907 */
908static inline int
909ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
910{
911 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
912}
913
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530914/*
915 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
916 * from response UPIU
917 * @ucd_rsp_ptr: pointer to response UPIU
918 *
919 * Return the data segment length.
920 */
921static inline unsigned int
922ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
923{
924 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
925 MASK_RSP_UPIU_DATA_SEG_LEN;
926}
927
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530928/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530929 * ufshcd_is_exception_event - Check if the device raised an exception event
930 * @ucd_rsp_ptr: pointer to response UPIU
931 *
932 * The function checks if the device raised an exception event indicated in
933 * the Device Information field of response UPIU.
934 *
935 * Returns true if exception is raised, false otherwise.
936 */
937static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
938{
939 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
940 MASK_RSP_EXCEPTION_EVENT ? true : false;
941}
942
943/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530944 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530945 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530946 */
947static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530948ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530949{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530950 ufshcd_writel(hba, INT_AGGR_ENABLE |
951 INT_AGGR_COUNTER_AND_TIMER_RESET,
952 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
953}
954
955/**
956 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
957 * @hba: per adapter instance
958 * @cnt: Interrupt aggregation counter threshold
959 * @tmout: Interrupt aggregation timeout value
960 */
961static inline void
962ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
963{
964 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
965 INT_AGGR_COUNTER_THLD_VAL(cnt) |
966 INT_AGGR_TIMEOUT_VAL(tmout),
967 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530968}
969
970/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300971 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
972 * @hba: per adapter instance
973 */
974static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
975{
976 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
977}
978
979/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530980 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
981 * When run-stop registers are set to 1, it indicates the
982 * host controller that it can process the requests
983 * @hba: per adapter instance
984 */
985static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
986{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530987 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
988 REG_UTP_TASK_REQ_LIST_RUN_STOP);
989 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
990 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530991}
992
993/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530994 * ufshcd_hba_start - Start controller initialization sequence
995 * @hba: per adapter instance
996 */
997static inline void ufshcd_hba_start(struct ufs_hba *hba)
998{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700999 u32 val = CONTROLLER_ENABLE;
1000
1001 if (ufshcd_is_crypto_supported(hba))
1002 val |= CRYPTO_GENERAL_ENABLE;
1003 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301004}
1005
1006/**
1007 * ufshcd_is_hba_active - Get controller state
1008 * @hba: per adapter instance
1009 *
1010 * Returns zero if controller is active, 1 otherwise
1011 */
1012static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
1013{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301014 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301015}
1016
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001017static const char *ufschd_uic_link_state_to_string(
1018 enum uic_link_state state)
1019{
1020 switch (state) {
1021 case UIC_LINK_OFF_STATE: return "OFF";
1022 case UIC_LINK_ACTIVE_STATE: return "ACTIVE";
1023 case UIC_LINK_HIBERN8_STATE: return "HIBERN8";
1024 default: return "UNKNOWN";
1025 }
1026}
1027
1028static const char *ufschd_ufs_dev_pwr_mode_to_string(
1029 enum ufs_dev_pwr_mode state)
1030{
1031 switch (state) {
1032 case UFS_ACTIVE_PWR_MODE: return "ACTIVE";
1033 case UFS_SLEEP_PWR_MODE: return "SLEEP";
1034 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN";
1035 default: return "UNKNOWN";
1036 }
1037}
1038
Yaniv Gardi37113102016-03-10 17:37:16 +02001039u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
1040{
1041 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
1042 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
1043 (hba->ufs_version == UFSHCI_VERSION_11))
1044 return UFS_UNIPRO_VER_1_41;
1045 else
1046 return UFS_UNIPRO_VER_1_6;
1047}
1048EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
1049
1050static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
1051{
1052 /*
1053 * If both host and device support UniPro ver1.6 or later, PA layer
1054 * parameters tuning happens during link startup itself.
1055 *
1056 * We can manually tune PA layer parameters if either host or device
1057 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
1058 * logic simple, we will only do manual tuning if local unipro version
1059 * doesn't support ver1.6 or later.
1060 */
1061 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
1062 return true;
1063 else
1064 return false;
1065}
1066
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001067/**
1068 * ufshcd_set_clk_freq - set UFS controller clock frequencies
1069 * @hba: per adapter instance
1070 * @scale_up: If True, set max possible frequency othewise set low frequency
1071 *
1072 * Returns 0 if successful
1073 * Returns < 0 for any other errors
1074 */
1075static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
1076{
1077 int ret = 0;
1078 struct ufs_clk_info *clki;
1079 struct list_head *head = &hba->clk_list_head;
1080
1081 if (!head || list_empty(head))
1082 goto out;
1083
1084 list_for_each_entry(clki, head, list) {
1085 if (!IS_ERR_OR_NULL(clki->clk)) {
1086 if (scale_up && clki->max_freq) {
1087 if (clki->curr_freq == clki->max_freq)
1088 continue;
1089
1090 ret = clk_set_rate(clki->clk, clki->max_freq);
1091 if (ret) {
1092 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1093 __func__, clki->name,
1094 clki->max_freq, ret);
1095 break;
1096 }
1097 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1098 "scaled up", clki->name,
1099 clki->curr_freq,
1100 clki->max_freq);
1101 clki->curr_freq = clki->max_freq;
1102
1103 } else if (!scale_up && clki->min_freq) {
1104 if (clki->curr_freq == clki->min_freq)
1105 continue;
1106
1107 ret = clk_set_rate(clki->clk, clki->min_freq);
1108 if (ret) {
1109 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1110 __func__, clki->name,
1111 clki->min_freq, ret);
1112 break;
1113 }
1114 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1115 "scaled down", clki->name,
1116 clki->curr_freq,
1117 clki->min_freq);
1118 clki->curr_freq = clki->min_freq;
1119 }
1120 }
1121 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
1122 clki->name, clk_get_rate(clki->clk));
1123 }
1124
1125out:
1126 return ret;
1127}
1128
1129/**
1130 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1131 * @hba: per adapter instance
1132 * @scale_up: True if scaling up and false if scaling down
1133 *
1134 * Returns 0 if successful
1135 * Returns < 0 for any other errors
1136 */
1137static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1138{
1139 int ret = 0;
1140
1141 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1142 if (ret)
1143 return ret;
1144
1145 ret = ufshcd_set_clk_freq(hba, scale_up);
1146 if (ret)
1147 return ret;
1148
1149 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1150 if (ret) {
1151 ufshcd_set_clk_freq(hba, !scale_up);
1152 return ret;
1153 }
1154
1155 return ret;
1156}
1157
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001158static void ufshcd_ungate_work(struct work_struct *work)
1159{
1160 int ret;
1161 unsigned long flags;
1162 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1163 clk_gating.ungate_work);
1164
1165 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1166
1167 spin_lock_irqsave(hba->host->host_lock, flags);
1168 if (hba->clk_gating.state == CLKS_ON) {
1169 spin_unlock_irqrestore(hba->host->host_lock, flags);
1170 goto unblock_reqs;
1171 }
1172
1173 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001174 ufshcd_hba_vreg_set_hpm(hba);
1175 ufshcd_enable_clocks(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001176
1177 /* Exit from hibern8 */
1178 if (ufshcd_can_hibern8_during_gating(hba)) {
1179 /* Prevent gating in this path */
1180 hba->clk_gating.is_suspended = true;
1181 if (ufshcd_is_link_hibern8(hba)) {
1182 ret = ufshcd_uic_hibern8_exit(hba);
1183 if (ret)
1184 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1185 __func__, ret);
1186 else
1187 ufshcd_set_link_active(hba);
1188 }
1189 hba->clk_gating.is_suspended = false;
1190 }
1191unblock_reqs:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001192 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001193}
1194
1195/**
1196 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1197 * Also, exit from hibern8 mode and set the link as active.
1198 * @hba: per adapter instance
1199 * @async: This indicates whether caller should ungate clocks asynchronously.
1200 */
1201int ufshcd_hold(struct ufs_hba *hba, bool async)
1202{
1203 int rc = 0;
1204 unsigned long flags;
1205
1206 if (!ufshcd_is_clkgating_allowed(hba))
1207 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001208 spin_lock_irqsave(hba->host->host_lock, flags);
1209 hba->clk_gating.active_reqs++;
1210
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001211 if (ufshcd_eh_in_progress(hba)) {
1212 spin_unlock_irqrestore(hba->host->host_lock, flags);
1213 return 0;
1214 }
1215
Sahitya Tummala856b3482014-09-25 15:32:34 +03001216start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001217 switch (hba->clk_gating.state) {
1218 case CLKS_ON:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001219 /*
1220 * Wait for the ungate work to complete if in progress.
1221 * Though the clocks may be in ON state, the link could
1222 * still be in hibner8 state if hibern8 is allowed
1223 * during clock gating.
1224 * Make sure we exit hibern8 state also in addition to
1225 * clocks being ON.
1226 */
1227 if (ufshcd_can_hibern8_during_gating(hba) &&
1228 ufshcd_is_link_hibern8(hba)) {
1229 spin_unlock_irqrestore(hba->host->host_lock, flags);
1230 flush_work(&hba->clk_gating.ungate_work);
1231 spin_lock_irqsave(hba->host->host_lock, flags);
1232 goto start;
1233 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001234 break;
1235 case REQ_CLKS_OFF:
1236 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1237 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001238 trace_ufshcd_clk_gating(dev_name(hba->dev),
1239 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001240 break;
1241 }
1242 /*
1243 * If we here, it means gating work is either done or
1244 * currently running. Hence, fall through to cancel gating
1245 * work and to enable clocks.
1246 */
1247 case CLKS_OFF:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001248 __ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001249 hba->clk_gating.state = REQ_CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001250 trace_ufshcd_clk_gating(dev_name(hba->dev),
1251 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001252 schedule_work(&hba->clk_gating.ungate_work);
1253 /*
1254 * fall through to check if we should wait for this
1255 * work to be done or not.
1256 */
1257 case REQ_CLKS_ON:
1258 if (async) {
1259 rc = -EAGAIN;
1260 hba->clk_gating.active_reqs--;
1261 break;
1262 }
1263
1264 spin_unlock_irqrestore(hba->host->host_lock, flags);
1265 flush_work(&hba->clk_gating.ungate_work);
1266 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001267 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001268 goto start;
1269 default:
1270 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1271 __func__, hba->clk_gating.state);
1272 break;
1273 }
1274 spin_unlock_irqrestore(hba->host->host_lock, flags);
1275out:
1276 return rc;
1277}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001278EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001279
1280static void ufshcd_gate_work(struct work_struct *work)
1281{
1282 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1283 clk_gating.gate_work.work);
1284 unsigned long flags;
1285
1286 spin_lock_irqsave(hba->host->host_lock, flags);
1287 if (hba->clk_gating.is_suspended) {
1288 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001289 trace_ufshcd_clk_gating(dev_name(hba->dev),
1290 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001291 goto rel_lock;
1292 }
1293
1294 if (hba->clk_gating.active_reqs
1295 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1296 || hba->lrb_in_use || hba->outstanding_tasks
1297 || hba->active_uic_cmd || hba->uic_async_done)
1298 goto rel_lock;
1299
1300 spin_unlock_irqrestore(hba->host->host_lock, flags);
1301
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001302 if (ufshcd_is_hibern8_on_idle_allowed(hba) &&
1303 hba->hibern8_on_idle.is_enabled)
1304 /*
1305 * Hibern8 enter work (on Idle) needs clocks to be ON hence
1306 * make sure that it is flushed before turning off the clocks.
1307 */
1308 flush_delayed_work(&hba->hibern8_on_idle.enter_work);
1309
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001310 /* put the link into hibern8 mode before turning off clocks */
1311 if (ufshcd_can_hibern8_during_gating(hba)) {
1312 if (ufshcd_uic_hibern8_enter(hba)) {
1313 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001314 trace_ufshcd_clk_gating(dev_name(hba->dev),
1315 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001316 goto out;
1317 }
1318 ufshcd_set_link_hibern8(hba);
1319 }
1320
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001321 if (!ufshcd_is_link_active(hba) && !hba->no_ref_clk_gating)
1322 ufshcd_disable_clocks(hba, true);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001323 else
1324 /* If link is active, device ref_clk can't be switched off */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001325 ufshcd_disable_clocks_skip_ref_clk(hba, true);
1326
1327 /* Put the host controller in low power mode if possible */
1328 ufshcd_hba_vreg_set_lpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001329
1330 /*
1331 * In case you are here to cancel this work the gating state
1332 * would be marked as REQ_CLKS_ON. In this case keep the state
1333 * as REQ_CLKS_ON which would anyway imply that clocks are off
1334 * and a request to turn them on is pending. By doing this way,
1335 * we keep the state machine in tact and this would ultimately
1336 * prevent from doing cancel work multiple times when there are
1337 * new requests arriving before the current cancel work is done.
1338 */
1339 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001340 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001341 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001342 trace_ufshcd_clk_gating(dev_name(hba->dev),
1343 hba->clk_gating.state);
1344 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001345rel_lock:
1346 spin_unlock_irqrestore(hba->host->host_lock, flags);
1347out:
1348 return;
1349}
1350
1351/* host lock must be held before calling this variant */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001352static void __ufshcd_release(struct ufs_hba *hba, bool no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001353{
1354 if (!ufshcd_is_clkgating_allowed(hba))
1355 return;
1356
1357 hba->clk_gating.active_reqs--;
1358
1359 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1360 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1361 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001362 || hba->active_uic_cmd || hba->uic_async_done
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001363 || ufshcd_eh_in_progress(hba) || no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001364 return;
1365
1366 hba->clk_gating.state = REQ_CLKS_OFF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001367 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
1368
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001369 schedule_delayed_work(&hba->clk_gating.gate_work,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001370 msecs_to_jiffies(hba->clk_gating.delay_ms));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001371}
1372
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001373void ufshcd_release(struct ufs_hba *hba, bool no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001374{
1375 unsigned long flags;
1376
1377 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001378 __ufshcd_release(hba, no_sched);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001379 spin_unlock_irqrestore(hba->host->host_lock, flags);
1380}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001381EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001382
1383static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1384 struct device_attribute *attr, char *buf)
1385{
1386 struct ufs_hba *hba = dev_get_drvdata(dev);
1387
1388 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1389}
1390
1391static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1392 struct device_attribute *attr, const char *buf, size_t count)
1393{
1394 struct ufs_hba *hba = dev_get_drvdata(dev);
1395 unsigned long flags, value;
1396
1397 if (kstrtoul(buf, 0, &value))
1398 return -EINVAL;
1399
1400 spin_lock_irqsave(hba->host->host_lock, flags);
1401 hba->clk_gating.delay_ms = value;
1402 spin_unlock_irqrestore(hba->host->host_lock, flags);
1403 return count;
1404}
1405
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001406static ssize_t ufshcd_clkgate_delay_pwr_save_show(struct device *dev,
1407 struct device_attribute *attr, char *buf)
1408{
1409 struct ufs_hba *hba = dev_get_drvdata(dev);
1410
1411 return snprintf(buf, PAGE_SIZE, "%lu\n",
1412 hba->clk_gating.delay_ms_pwr_save);
1413}
1414
1415static ssize_t ufshcd_clkgate_delay_pwr_save_store(struct device *dev,
1416 struct device_attribute *attr, const char *buf, size_t count)
1417{
1418 struct ufs_hba *hba = dev_get_drvdata(dev);
1419 unsigned long flags, value;
1420
1421 if (kstrtoul(buf, 0, &value))
1422 return -EINVAL;
1423
1424 spin_lock_irqsave(hba->host->host_lock, flags);
1425
1426 hba->clk_gating.delay_ms_pwr_save = value;
1427 if (ufshcd_is_clkscaling_supported(hba) &&
1428 !hba->clk_scaling.is_scaled_up)
1429 hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_pwr_save;
1430
1431 spin_unlock_irqrestore(hba->host->host_lock, flags);
1432 return count;
1433}
1434
1435static ssize_t ufshcd_clkgate_delay_perf_show(struct device *dev,
1436 struct device_attribute *attr, char *buf)
1437{
1438 struct ufs_hba *hba = dev_get_drvdata(dev);
1439
1440 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms_perf);
1441}
1442
1443static ssize_t ufshcd_clkgate_delay_perf_store(struct device *dev,
1444 struct device_attribute *attr, const char *buf, size_t count)
1445{
1446 struct ufs_hba *hba = dev_get_drvdata(dev);
1447 unsigned long flags, value;
1448
1449 if (kstrtoul(buf, 0, &value))
1450 return -EINVAL;
1451
1452 spin_lock_irqsave(hba->host->host_lock, flags);
1453
1454 hba->clk_gating.delay_ms_perf = value;
1455 if (ufshcd_is_clkscaling_supported(hba) &&
1456 hba->clk_scaling.is_scaled_up)
1457 hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_perf;
1458
1459 spin_unlock_irqrestore(hba->host->host_lock, flags);
1460 return count;
1461}
1462
1463static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1464 struct device_attribute *attr, char *buf)
1465{
1466 struct ufs_hba *hba = dev_get_drvdata(dev);
1467
1468 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1469}
1470
1471static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1472 struct device_attribute *attr, const char *buf, size_t count)
1473{
1474 struct ufs_hba *hba = dev_get_drvdata(dev);
1475 unsigned long flags;
1476 u32 value;
1477
1478 if (kstrtou32(buf, 0, &value))
1479 return -EINVAL;
1480
1481 value = !!value;
1482 if (value == hba->clk_gating.is_enabled)
1483 goto out;
1484
1485 if (value) {
1486 ufshcd_release(hba, false);
1487 } else {
1488 spin_lock_irqsave(hba->host->host_lock, flags);
1489 hba->clk_gating.active_reqs++;
1490 spin_unlock_irqrestore(hba->host->host_lock, flags);
1491 }
1492
1493 hba->clk_gating.is_enabled = value;
1494out:
1495 return count;
1496}
1497
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001498static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1499{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001500 struct ufs_clk_gating *gating = &hba->clk_gating;
1501
1502 hba->clk_gating.state = CLKS_ON;
1503
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001504 if (!ufshcd_is_clkgating_allowed(hba))
1505 return;
1506
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001507 INIT_DELAYED_WORK(&gating->gate_work, ufshcd_gate_work);
1508 INIT_WORK(&gating->ungate_work, ufshcd_ungate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001509
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001510 gating->is_enabled = true;
1511
1512 /*
1513 * Scheduling the delayed work after 1 jiffies will make the work to
1514 * get schedule any time from 0ms to 1000/HZ ms which is not desirable
1515 * for hibern8 enter work as it may impact the performance if it gets
1516 * scheduled almost immediately. Hence make sure that hibern8 enter
1517 * work gets scheduled atleast after 2 jiffies (any time between
1518 * 1000/HZ ms to 2000/HZ ms).
1519 */
1520 gating->delay_ms_pwr_save = jiffies_to_msecs(
1521 max_t(unsigned long,
1522 msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE),
1523 2));
1524 gating->delay_ms_perf = jiffies_to_msecs(
1525 max_t(unsigned long,
1526 msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PERF),
1527 2));
1528
1529 /* start with performance mode */
1530 gating->delay_ms = gating->delay_ms_perf;
1531
1532 if (!ufshcd_is_clkscaling_supported(hba))
1533 goto scaling_not_supported;
1534
1535 gating->delay_pwr_save_attr.show = ufshcd_clkgate_delay_pwr_save_show;
1536 gating->delay_pwr_save_attr.store = ufshcd_clkgate_delay_pwr_save_store;
1537 sysfs_attr_init(&gating->delay_pwr_save_attr.attr);
1538 gating->delay_pwr_save_attr.attr.name = "clkgate_delay_ms_pwr_save";
1539 gating->delay_pwr_save_attr.attr.mode = S_IRUGO | S_IWUSR;
1540 if (device_create_file(hba->dev, &gating->delay_pwr_save_attr))
1541 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_pwr_save\n");
1542
1543 gating->delay_perf_attr.show = ufshcd_clkgate_delay_perf_show;
1544 gating->delay_perf_attr.store = ufshcd_clkgate_delay_perf_store;
1545 sysfs_attr_init(&gating->delay_perf_attr.attr);
1546 gating->delay_perf_attr.attr.name = "clkgate_delay_ms_perf";
1547 gating->delay_perf_attr.attr.mode = S_IRUGO | S_IWUSR;
1548 if (device_create_file(hba->dev, &gating->delay_perf_attr))
1549 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_perf\n");
1550
1551 goto add_clkgate_enable;
1552
1553scaling_not_supported:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001554 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1555 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1556 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1557 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1558 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
1559 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1560 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001561
1562add_clkgate_enable:
1563 gating->enable_attr.show = ufshcd_clkgate_enable_show;
1564 gating->enable_attr.store = ufshcd_clkgate_enable_store;
1565 sysfs_attr_init(&gating->enable_attr.attr);
1566 gating->enable_attr.attr.name = "clkgate_enable";
1567 gating->enable_attr.attr.mode = S_IRUGO | S_IWUSR;
1568 if (device_create_file(hba->dev, &gating->enable_attr))
1569 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001570}
1571
1572static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1573{
1574 if (!ufshcd_is_clkgating_allowed(hba))
1575 return;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001576 if (ufshcd_is_clkscaling_supported(hba)) {
1577 device_remove_file(hba->dev,
1578 &hba->clk_gating.delay_pwr_save_attr);
1579 device_remove_file(hba->dev, &hba->clk_gating.delay_perf_attr);
1580 } else {
1581 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1582 }
1583 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001584 cancel_work_sync(&hba->clk_gating.ungate_work);
1585 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001586}
1587
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001588static void ufshcd_set_auto_hibern8_timer(struct ufs_hba *hba, u32 delay)
1589{
1590 ufshcd_rmwl(hba, AUTO_HIBERN8_TIMER_SCALE_MASK |
1591 AUTO_HIBERN8_IDLE_TIMER_MASK,
1592 AUTO_HIBERN8_TIMER_SCALE_1_MS | delay,
1593 REG_AUTO_HIBERN8_IDLE_TIMER);
1594 /* Make sure the timer gets applied before further operations */
1595 mb();
1596}
1597
1598/**
1599 * ufshcd_hibern8_hold - Make sure that link is not in hibern8.
1600 *
1601 * @hba: per adapter instance
1602 * @async: This indicates whether caller wants to exit hibern8 asynchronously.
1603 *
1604 * Exit from hibern8 mode and set the link as active.
1605 *
1606 * Return 0 on success, non-zero on failure.
1607 */
1608static int ufshcd_hibern8_hold(struct ufs_hba *hba, bool async)
1609{
1610 int rc = 0;
1611 unsigned long flags;
1612
1613 if (!ufshcd_is_hibern8_on_idle_allowed(hba))
1614 goto out;
1615
1616 spin_lock_irqsave(hba->host->host_lock, flags);
1617 hba->hibern8_on_idle.active_reqs++;
1618
1619 if (ufshcd_eh_in_progress(hba)) {
1620 spin_unlock_irqrestore(hba->host->host_lock, flags);
1621 return 0;
1622 }
1623
1624start:
1625 switch (hba->hibern8_on_idle.state) {
1626 case HIBERN8_EXITED:
1627 break;
1628 case REQ_HIBERN8_ENTER:
1629 if (cancel_delayed_work(&hba->hibern8_on_idle.enter_work)) {
1630 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1631 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1632 hba->hibern8_on_idle.state);
1633 break;
1634 }
1635 /*
1636 * If we here, it means Hibern8 enter work is either done or
1637 * currently running. Hence, fall through to cancel hibern8
1638 * work and exit hibern8.
1639 */
1640 case HIBERN8_ENTERED:
1641 __ufshcd_scsi_block_requests(hba);
1642 hba->hibern8_on_idle.state = REQ_HIBERN8_EXIT;
1643 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1644 hba->hibern8_on_idle.state);
1645 schedule_work(&hba->hibern8_on_idle.exit_work);
1646 /*
1647 * fall through to check if we should wait for this
1648 * work to be done or not.
1649 */
1650 case REQ_HIBERN8_EXIT:
1651 if (async) {
1652 rc = -EAGAIN;
1653 hba->hibern8_on_idle.active_reqs--;
1654 break;
1655 } else {
1656 spin_unlock_irqrestore(hba->host->host_lock, flags);
1657 flush_work(&hba->hibern8_on_idle.exit_work);
1658 /* Make sure state is HIBERN8_EXITED before returning */
1659 spin_lock_irqsave(hba->host->host_lock, flags);
1660 goto start;
1661 }
1662 default:
1663 dev_err(hba->dev, "%s: H8 is in invalid state %d\n",
1664 __func__, hba->hibern8_on_idle.state);
1665 break;
1666 }
1667 spin_unlock_irqrestore(hba->host->host_lock, flags);
1668out:
1669 return rc;
1670}
1671
1672/* host lock must be held before calling this variant */
1673static void __ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched)
1674{
1675 unsigned long delay_in_jiffies;
1676
1677 if (!ufshcd_is_hibern8_on_idle_allowed(hba))
1678 return;
1679
1680 hba->hibern8_on_idle.active_reqs--;
1681 BUG_ON(hba->hibern8_on_idle.active_reqs < 0);
1682
1683 if (hba->hibern8_on_idle.active_reqs
1684 || hba->hibern8_on_idle.is_suspended
1685 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1686 || hba->lrb_in_use || hba->outstanding_tasks
1687 || hba->active_uic_cmd || hba->uic_async_done
1688 || ufshcd_eh_in_progress(hba) || no_sched)
1689 return;
1690
1691 hba->hibern8_on_idle.state = REQ_HIBERN8_ENTER;
1692 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1693 hba->hibern8_on_idle.state);
1694 /*
1695 * Scheduling the delayed work after 1 jiffies will make the work to
1696 * get schedule any time from 0ms to 1000/HZ ms which is not desirable
1697 * for hibern8 enter work as it may impact the performance if it gets
1698 * scheduled almost immediately. Hence make sure that hibern8 enter
1699 * work gets scheduled atleast after 2 jiffies (any time between
1700 * 1000/HZ ms to 2000/HZ ms).
1701 */
1702 delay_in_jiffies = msecs_to_jiffies(hba->hibern8_on_idle.delay_ms);
1703 if (delay_in_jiffies == 1)
1704 delay_in_jiffies++;
1705
1706 schedule_delayed_work(&hba->hibern8_on_idle.enter_work,
1707 delay_in_jiffies);
1708}
1709
1710static void ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched)
1711{
1712 unsigned long flags;
1713
1714 spin_lock_irqsave(hba->host->host_lock, flags);
1715 __ufshcd_hibern8_release(hba, no_sched);
1716 spin_unlock_irqrestore(hba->host->host_lock, flags);
1717}
1718
1719static void ufshcd_hibern8_enter_work(struct work_struct *work)
1720{
1721 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1722 hibern8_on_idle.enter_work.work);
1723 unsigned long flags;
1724
1725 spin_lock_irqsave(hba->host->host_lock, flags);
1726 if (hba->hibern8_on_idle.is_suspended) {
1727 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1728 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1729 hba->hibern8_on_idle.state);
1730 goto rel_lock;
1731 }
1732
1733 if (hba->hibern8_on_idle.active_reqs
1734 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1735 || hba->lrb_in_use || hba->outstanding_tasks
1736 || hba->active_uic_cmd || hba->uic_async_done)
1737 goto rel_lock;
1738
1739 spin_unlock_irqrestore(hba->host->host_lock, flags);
1740
1741 if (ufshcd_is_link_active(hba) && ufshcd_uic_hibern8_enter(hba)) {
1742 /* Enter failed */
1743 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1744 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1745 hba->hibern8_on_idle.state);
1746 goto out;
1747 }
1748 ufshcd_set_link_hibern8(hba);
1749
1750 /*
1751 * In case you are here to cancel this work the hibern8_on_idle.state
1752 * would be marked as REQ_HIBERN8_EXIT. In this case keep the state
1753 * as REQ_HIBERN8_EXIT which would anyway imply that we are in hibern8
1754 * and a request to exit from it is pending. By doing this way,
1755 * we keep the state machine in tact and this would ultimately
1756 * prevent from doing cancel work multiple times when there are
1757 * new requests arriving before the current cancel work is done.
1758 */
1759 spin_lock_irqsave(hba->host->host_lock, flags);
1760 if (hba->hibern8_on_idle.state == REQ_HIBERN8_ENTER) {
1761 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
1762 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1763 hba->hibern8_on_idle.state);
1764 }
1765rel_lock:
1766 spin_unlock_irqrestore(hba->host->host_lock, flags);
1767out:
1768 return;
1769}
1770
1771static void ufshcd_hibern8_exit_work(struct work_struct *work)
1772{
1773 int ret;
1774 unsigned long flags;
1775 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1776 hibern8_on_idle.exit_work);
1777
1778 cancel_delayed_work_sync(&hba->hibern8_on_idle.enter_work);
1779
1780 spin_lock_irqsave(hba->host->host_lock, flags);
1781 if ((hba->hibern8_on_idle.state == HIBERN8_EXITED)
1782 || ufshcd_is_link_active(hba)) {
1783 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1784 spin_unlock_irqrestore(hba->host->host_lock, flags);
1785 goto unblock_reqs;
1786 }
1787 spin_unlock_irqrestore(hba->host->host_lock, flags);
1788
1789 /* Exit from hibern8 */
1790 if (ufshcd_is_link_hibern8(hba)) {
1791 ufshcd_hold(hba, false);
1792 ret = ufshcd_uic_hibern8_exit(hba);
1793 ufshcd_release(hba, false);
1794 if (!ret) {
1795 spin_lock_irqsave(hba->host->host_lock, flags);
1796 ufshcd_set_link_active(hba);
1797 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1798 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1799 hba->hibern8_on_idle.state);
1800 spin_unlock_irqrestore(hba->host->host_lock, flags);
1801 }
1802 }
1803unblock_reqs:
1804 ufshcd_scsi_unblock_requests(hba);
1805}
1806
1807static ssize_t ufshcd_hibern8_on_idle_delay_show(struct device *dev,
1808 struct device_attribute *attr, char *buf)
1809{
1810 struct ufs_hba *hba = dev_get_drvdata(dev);
1811
1812 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->hibern8_on_idle.delay_ms);
1813}
1814
1815static ssize_t ufshcd_hibern8_on_idle_delay_store(struct device *dev,
1816 struct device_attribute *attr, const char *buf, size_t count)
1817{
1818 struct ufs_hba *hba = dev_get_drvdata(dev);
1819 unsigned long flags, value;
1820
1821 if (kstrtoul(buf, 0, &value))
1822 return -EINVAL;
1823
1824 spin_lock_irqsave(hba->host->host_lock, flags);
1825 hba->hibern8_on_idle.delay_ms = value;
1826 spin_unlock_irqrestore(hba->host->host_lock, flags);
1827
1828 /* Update auto hibern8 timer value if supported */
1829 if (ufshcd_is_auto_hibern8_supported(hba) &&
1830 hba->hibern8_on_idle.is_enabled)
1831 ufshcd_set_auto_hibern8_timer(hba,
1832 hba->hibern8_on_idle.delay_ms);
1833
1834 return count;
1835}
1836
1837static ssize_t ufshcd_hibern8_on_idle_enable_show(struct device *dev,
1838 struct device_attribute *attr, char *buf)
1839{
1840 struct ufs_hba *hba = dev_get_drvdata(dev);
1841
1842 return snprintf(buf, PAGE_SIZE, "%d\n",
1843 hba->hibern8_on_idle.is_enabled);
1844}
1845
1846static ssize_t ufshcd_hibern8_on_idle_enable_store(struct device *dev,
1847 struct device_attribute *attr, const char *buf, size_t count)
1848{
1849 struct ufs_hba *hba = dev_get_drvdata(dev);
1850 unsigned long flags;
1851 u32 value;
1852
1853 if (kstrtou32(buf, 0, &value))
1854 return -EINVAL;
1855
1856 value = !!value;
1857 if (value == hba->hibern8_on_idle.is_enabled)
1858 goto out;
1859
1860 /* Update auto hibern8 timer value if supported */
1861 if (ufshcd_is_auto_hibern8_supported(hba)) {
1862 ufshcd_set_auto_hibern8_timer(hba,
1863 value ? hba->hibern8_on_idle.delay_ms : value);
1864 goto update;
1865 }
1866
1867 if (value) {
1868 /*
1869 * As clock gating work would wait for the hibern8 enter work
1870 * to finish, clocks would remain on during hibern8 enter work.
1871 */
1872 ufshcd_hold(hba, false);
1873 ufshcd_release_all(hba);
1874 } else {
1875 spin_lock_irqsave(hba->host->host_lock, flags);
1876 hba->hibern8_on_idle.active_reqs++;
1877 spin_unlock_irqrestore(hba->host->host_lock, flags);
1878 }
1879
1880update:
1881 hba->hibern8_on_idle.is_enabled = value;
1882out:
1883 return count;
1884}
1885
1886static void ufshcd_init_hibern8_on_idle(struct ufs_hba *hba)
1887{
1888 /* initialize the state variable here */
1889 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1890
1891 if (!ufshcd_is_hibern8_on_idle_allowed(hba) &&
1892 !ufshcd_is_auto_hibern8_supported(hba))
1893 return;
1894
1895 if (ufshcd_is_auto_hibern8_supported(hba)) {
1896 hba->hibern8_on_idle.state = AUTO_HIBERN8;
1897 /*
1898 * Disable SW hibern8 enter on idle in case
1899 * auto hibern8 is supported
1900 */
1901 hba->caps &= ~UFSHCD_CAP_HIBERN8_ENTER_ON_IDLE;
1902 } else {
1903 INIT_DELAYED_WORK(&hba->hibern8_on_idle.enter_work,
1904 ufshcd_hibern8_enter_work);
1905 INIT_WORK(&hba->hibern8_on_idle.exit_work,
1906 ufshcd_hibern8_exit_work);
1907 }
1908
1909 hba->hibern8_on_idle.delay_ms = 10;
1910 hba->hibern8_on_idle.is_enabled = true;
1911
1912 hba->hibern8_on_idle.delay_attr.show =
1913 ufshcd_hibern8_on_idle_delay_show;
1914 hba->hibern8_on_idle.delay_attr.store =
1915 ufshcd_hibern8_on_idle_delay_store;
1916 sysfs_attr_init(&hba->hibern8_on_idle.delay_attr.attr);
1917 hba->hibern8_on_idle.delay_attr.attr.name = "hibern8_on_idle_delay_ms";
1918 hba->hibern8_on_idle.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
1919 if (device_create_file(hba->dev, &hba->hibern8_on_idle.delay_attr))
1920 dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_delay\n");
1921
1922 hba->hibern8_on_idle.enable_attr.show =
1923 ufshcd_hibern8_on_idle_enable_show;
1924 hba->hibern8_on_idle.enable_attr.store =
1925 ufshcd_hibern8_on_idle_enable_store;
1926 sysfs_attr_init(&hba->hibern8_on_idle.enable_attr.attr);
1927 hba->hibern8_on_idle.enable_attr.attr.name = "hibern8_on_idle_enable";
1928 hba->hibern8_on_idle.enable_attr.attr.mode = S_IRUGO | S_IWUSR;
1929 if (device_create_file(hba->dev, &hba->hibern8_on_idle.enable_attr))
1930 dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_enable\n");
1931}
1932
1933static void ufshcd_exit_hibern8_on_idle(struct ufs_hba *hba)
1934{
1935 if (!ufshcd_is_hibern8_on_idle_allowed(hba) &&
1936 !ufshcd_is_auto_hibern8_supported(hba))
1937 return;
1938 device_remove_file(hba->dev, &hba->hibern8_on_idle.delay_attr);
1939 device_remove_file(hba->dev, &hba->hibern8_on_idle.enable_attr);
1940}
1941
1942static void ufshcd_hold_all(struct ufs_hba *hba)
1943{
1944 ufshcd_hold(hba, false);
1945 ufshcd_hibern8_hold(hba, false);
1946}
1947
1948static void ufshcd_release_all(struct ufs_hba *hba)
1949{
1950 ufshcd_hibern8_release(hba, false);
1951 ufshcd_release(hba, false);
1952}
1953
Sahitya Tummala856b3482014-09-25 15:32:34 +03001954/* Must be called with host lock acquired */
1955static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1956{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001957 bool queue_resume_work = false;
1958
1959 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001960 return;
1961
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001962 if (!hba->clk_scaling.active_reqs++)
1963 queue_resume_work = true;
1964
1965 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1966 return;
1967
1968 if (queue_resume_work)
1969 queue_work(hba->clk_scaling.workq,
1970 &hba->clk_scaling.resume_work);
1971
1972 if (!hba->clk_scaling.window_start_t) {
1973 hba->clk_scaling.window_start_t = jiffies;
1974 hba->clk_scaling.tot_busy_t = 0;
1975 hba->clk_scaling.is_busy_started = false;
1976 }
1977
Sahitya Tummala856b3482014-09-25 15:32:34 +03001978 if (!hba->clk_scaling.is_busy_started) {
1979 hba->clk_scaling.busy_start_t = ktime_get();
1980 hba->clk_scaling.is_busy_started = true;
1981 }
1982}
1983
1984static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1985{
1986 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1987
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001988 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001989 return;
1990
1991 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1992 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1993 scaling->busy_start_t));
1994 scaling->busy_start_t = ktime_set(0, 0);
1995 scaling->is_busy_started = false;
1996 }
1997}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001998
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301999/**
2000 * ufshcd_send_command - Send SCSI or device management commands
2001 * @hba: per adapter instance
2002 * @task_tag: Task tag of the command
2003 */
2004static inline
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002005int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302006{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002007 int ret = 0;
2008
2009 hba->lrb[task_tag].issue_time_stamp = ktime_get();
2010 hba->lrb[task_tag].complete_time_stamp = ktime_set(0, 0);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002011 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302012 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302013 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002014 /* Make sure that doorbell is committed immediately */
2015 wmb();
2016 ufshcd_cond_add_cmd_trace(hba, task_tag, "send");
2017 ufshcd_update_tag_stats(hba, task_tag);
2018 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302019}
2020
2021/**
2022 * ufshcd_copy_sense_data - Copy sense data in case of check condition
2023 * @lrb - pointer to local reference block
2024 */
2025static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
2026{
2027 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302028 if (lrbp->sense_buffer &&
2029 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002030 int len_to_copy;
2031
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302032 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002033 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
2034
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302035 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302036 lrbp->ucd_rsp_ptr->sr.sense_data,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002037 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302038 }
2039}
2040
2041/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302042 * ufshcd_copy_query_response() - Copy the Query Response and the data
2043 * descriptor
2044 * @hba: per adapter instance
2045 * @lrb - pointer to local reference block
2046 */
2047static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002048int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05302049{
2050 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2051
Dolev Raviv68078d52013-07-30 00:35:58 +05302052 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302053
Dolev Raviv68078d52013-07-30 00:35:58 +05302054 /* Get the descriptor */
2055 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002056 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05302057 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002058 u16 resp_len;
2059 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05302060
2061 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002062 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302063 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002064 buf_len = be16_to_cpu(
2065 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002066 if (likely(buf_len >= resp_len)) {
2067 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2068 } else {
2069 dev_warn(hba->dev,
2070 "%s: Response size is bigger than buffer",
2071 __func__);
2072 return -EINVAL;
2073 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302074 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002075
2076 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302077}
2078
2079/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302080 * ufshcd_hba_capabilities - Read controller capabilities
2081 * @hba: per adapter instance
2082 */
2083static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
2084{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302085 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302086
2087 /* nutrs and nutmrs are 0 based values */
2088 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2089 hba->nutmrs =
2090 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
2091}
2092
2093/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302094 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2095 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302096 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302097 * Return true on success, else false
2098 */
2099static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2100{
2101 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2102 return true;
2103 else
2104 return false;
2105}
2106
2107/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302108 * ufshcd_get_upmcrs - Get the power mode change request status
2109 * @hba: Pointer to adapter instance
2110 *
2111 * This function gets the UPMCRS field of HCS register
2112 * Returns value of UPMCRS field
2113 */
2114static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2115{
2116 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2117}
2118
2119/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302120 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2121 * @hba: per adapter instance
2122 * @uic_cmd: UIC command
2123 *
2124 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302125 */
2126static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302127ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302128{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302129 WARN_ON(hba->active_uic_cmd);
2130
2131 hba->active_uic_cmd = uic_cmd;
2132
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302133 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302134 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2135 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2136 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302137
2138 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302139 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302140 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302141}
2142
2143/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302144 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2145 * @hba: per adapter instance
2146 * @uic_command: UIC command
2147 *
2148 * Must be called with mutex held.
2149 * Returns 0 only if success.
2150 */
2151static int
2152ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2153{
2154 int ret;
2155 unsigned long flags;
2156
2157 if (wait_for_completion_timeout(&uic_cmd->done,
2158 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2159 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2160 else
2161 ret = -ETIMEDOUT;
2162
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002163 if (ret)
2164 ufsdbg_set_err_state(hba);
2165
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302166 spin_lock_irqsave(hba->host->host_lock, flags);
2167 hba->active_uic_cmd = NULL;
2168 spin_unlock_irqrestore(hba->host->host_lock, flags);
2169
2170 return ret;
2171}
2172
2173/**
2174 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2175 * @hba: per adapter instance
2176 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002177 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302178 *
2179 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002180 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302181 * Returns 0 only if success.
2182 */
2183static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002184__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2185 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302186{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302187 if (!ufshcd_ready_for_uic_cmd(hba)) {
2188 dev_err(hba->dev,
2189 "Controller not ready to accept UIC commands\n");
2190 return -EIO;
2191 }
2192
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002193 if (completion)
2194 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302195
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302196 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302197
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002198 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302199}
2200
2201/**
2202 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2203 * @hba: per adapter instance
2204 * @uic_cmd: UIC command
2205 *
2206 * Returns 0 only if success.
2207 */
2208static int
2209ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2210{
2211 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002212 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302213
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002214 ufshcd_hold_all(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302215 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002216 ufshcd_add_delay_before_dme_cmd(hba);
2217
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002218 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002219 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002220 spin_unlock_irqrestore(hba->host->host_lock, flags);
2221 if (!ret)
2222 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2223
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002224 ufshcd_save_tstamp_of_last_dme_cmd(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302225 mutex_unlock(&hba->uic_cmd_mutex);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002226 ufshcd_release_all(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302227
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002228 ufsdbg_error_inject_dispatcher(hba,
2229 ERR_INJECT_UIC, 0, &ret);
2230
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302231 return ret;
2232}
2233
2234/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302235 * ufshcd_map_sg - Map scatter-gather list to prdt
2236 * @lrbp - pointer to local reference block
2237 *
2238 * Returns 0 in case of success, non-zero value in case of failure
2239 */
2240static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
2241{
2242 struct ufshcd_sg_entry *prd_table;
2243 struct scatterlist *sg;
2244 struct scsi_cmnd *cmd;
2245 int sg_segments;
2246 int i;
2247
2248 cmd = lrbp->cmd;
2249 sg_segments = scsi_dma_map(cmd);
2250 if (sg_segments < 0)
2251 return sg_segments;
2252
2253 if (sg_segments) {
2254 lrbp->utr_descriptor_ptr->prd_table_length =
2255 cpu_to_le16((u16) (sg_segments));
2256
2257 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2258
2259 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2260 prd_table[i].size =
2261 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2262 prd_table[i].base_addr =
2263 cpu_to_le32(lower_32_bits(sg->dma_address));
2264 prd_table[i].upper_addr =
2265 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002266 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302267 }
2268 } else {
2269 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2270 }
2271
2272 return 0;
2273}
2274
2275/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302276 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302277 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302278 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302279 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302280static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302281{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302282 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2283
2284 if (hba->ufs_version == UFSHCI_VERSION_10) {
2285 u32 rw;
2286 rw = set & INTERRUPT_MASK_RW_VER_10;
2287 set = rw | ((set ^ intrs) & intrs);
2288 } else {
2289 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302290 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302291
2292 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2293}
2294
2295/**
2296 * ufshcd_disable_intr - disable interrupts
2297 * @hba: per adapter instance
2298 * @intrs: interrupt bits
2299 */
2300static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2301{
2302 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2303
2304 if (hba->ufs_version == UFSHCI_VERSION_10) {
2305 u32 rw;
2306 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2307 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2308 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2309
2310 } else {
2311 set &= ~intrs;
2312 }
2313
2314 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302315}
2316
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002317static int ufshcd_prepare_crypto_utrd(struct ufs_hba *hba,
2318 struct ufshcd_lrb *lrbp)
2319{
2320 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2321 u8 cc_index = 0;
2322 bool enable = false;
2323 u64 dun = 0;
2324 int ret;
2325
2326 /*
2327 * Call vendor specific code to get crypto info for this request:
2328 * enable, crypto config. index, DUN.
2329 * If bypass is set, don't bother setting the other fields.
2330 */
2331 ret = ufshcd_vops_crypto_req_setup(hba, lrbp, &cc_index, &enable, &dun);
2332 if (ret) {
2333 if (ret != -EAGAIN) {
2334 dev_err(hba->dev,
2335 "%s: failed to setup crypto request (%d)\n",
2336 __func__, ret);
2337 }
2338
2339 return ret;
2340 }
2341
2342 if (!enable)
2343 goto out;
2344
2345 req_desc->header.dword_0 |= cc_index | UTRD_CRYPTO_ENABLE;
2346 if (lrbp->cmd->request && lrbp->cmd->request->bio)
2347 dun = lrbp->cmd->request->bio->bi_iter.bi_sector;
2348
2349 req_desc->header.dword_1 = (u32)(dun & 0xFFFFFFFF);
2350 req_desc->header.dword_3 = (u32)((dun >> 32) & 0xFFFFFFFF);
2351out:
2352 return 0;
2353}
2354
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302355/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302356 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2357 * descriptor according to request
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002358 * @hba: per adapter instance
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302359 * @lrbp: pointer to local reference block
2360 * @upiu_flags: flags required in the header
2361 * @cmd_dir: requests data direction
2362 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002363static int ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
2364 struct ufshcd_lrb *lrbp, u32 *upiu_flags,
2365 enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302366{
2367 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2368 u32 data_direction;
2369 u32 dword_0;
2370
2371 if (cmd_dir == DMA_FROM_DEVICE) {
2372 data_direction = UTP_DEVICE_TO_HOST;
2373 *upiu_flags = UPIU_CMD_FLAGS_READ;
2374 } else if (cmd_dir == DMA_TO_DEVICE) {
2375 data_direction = UTP_HOST_TO_DEVICE;
2376 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2377 } else {
2378 data_direction = UTP_NO_DATA_TRANSFER;
2379 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2380 }
2381
2382 dword_0 = data_direction | (lrbp->command_type
2383 << UPIU_COMMAND_TYPE_OFFSET);
2384 if (lrbp->intr_cmd)
2385 dword_0 |= UTP_REQ_DESC_INT_CMD;
2386
2387 /* Transfer request descriptor header fields */
2388 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002389 /* dword_1 is reserved, hence it is set to 0 */
2390 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302391 /*
2392 * assigning invalid value for command status. Controller
2393 * updates OCS on command completion, with the command
2394 * status
2395 */
2396 req_desc->header.dword_2 =
2397 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002398 /* dword_3 is reserved, hence it is set to 0 */
2399 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002400
2401 req_desc->prd_table_length = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002402
2403 if (ufshcd_is_crypto_supported(hba))
2404 return ufshcd_prepare_crypto_utrd(hba, lrbp);
2405
2406 return 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302407}
2408
2409/**
2410 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2411 * for scsi commands
2412 * @lrbp - local reference block pointer
2413 * @upiu_flags - flags
2414 */
2415static
2416void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2417{
2418 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002419 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302420
2421 /* command descriptor fields */
2422 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2423 UPIU_TRANSACTION_COMMAND, upiu_flags,
2424 lrbp->lun, lrbp->task_tag);
2425 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2426 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2427
2428 /* Total EHS length and Data segment length will be zero */
2429 ucd_req_ptr->header.dword_2 = 0;
2430
2431 ucd_req_ptr->sc.exp_data_transfer_len =
2432 cpu_to_be32(lrbp->cmd->sdb.length);
2433
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002434 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002435 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002436 if (cdb_len < MAX_CDB_SIZE)
2437 memset(ucd_req_ptr->sc.cdb + cdb_len, 0,
2438 (MAX_CDB_SIZE - cdb_len));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002439 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302440}
2441
Dolev Raviv68078d52013-07-30 00:35:58 +05302442/**
2443 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2444 * for query requsts
2445 * @hba: UFS hba
2446 * @lrbp: local reference block pointer
2447 * @upiu_flags: flags
2448 */
2449static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2450 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2451{
2452 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2453 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302454 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302455 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
2456
2457 /* Query request header */
2458 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2459 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2460 lrbp->lun, lrbp->task_tag);
2461 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2462 0, query->request.query_func, 0, 0);
2463
Zang Leigang68612852016-08-25 17:39:19 +08002464 /* Data segment length only need for WRITE_DESC */
2465 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2466 ucd_req_ptr->header.dword_2 =
2467 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2468 else
2469 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302470
2471 /* Copy the Query Request buffer as is */
2472 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2473 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302474
2475 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002476 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2477 memcpy(descp, query->descriptor, len);
2478
Yaniv Gardi51047262016-02-01 15:02:38 +02002479 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302480}
2481
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302482static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2483{
2484 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2485
2486 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2487
2488 /* command descriptor fields */
2489 ucd_req_ptr->header.dword_0 =
2490 UPIU_HEADER_DWORD(
2491 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002492 /* clear rest of the fields of basic header */
2493 ucd_req_ptr->header.dword_1 = 0;
2494 ucd_req_ptr->header.dword_2 = 0;
2495
2496 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302497}
2498
2499/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002500 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302501 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302502 * @lrb - pointer to local reference block
2503 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002504static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302505{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302506 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302507 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302508
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002509 switch (lrbp->command_type) {
2510 case UTP_CMD_TYPE_SCSI:
2511 if (likely(lrbp->cmd)) {
2512 ret = ufshcd_prepare_req_desc_hdr(hba, lrbp,
2513 &upiu_flags, lrbp->cmd->sc_data_direction);
2514 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2515 } else {
2516 ret = -EINVAL;
2517 }
2518 break;
2519 case UTP_CMD_TYPE_DEV_MANAGE:
2520 ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags,
2521 DMA_NONE);
2522 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2523 ufshcd_prepare_utp_query_req_upiu(
2524 hba, lrbp, upiu_flags);
2525 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2526 ufshcd_prepare_utp_nop_upiu(lrbp);
2527 else
2528 ret = -EINVAL;
2529 break;
2530 case UTP_CMD_TYPE_UFS:
2531 /* For UFS native command implementation */
2532 ret = -ENOTSUPP;
2533 dev_err(hba->dev, "%s: UFS native command are not supported\n",
2534 __func__);
2535 break;
2536 default:
2537 ret = -ENOTSUPP;
2538 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
2539 __func__, lrbp->command_type);
2540 break;
2541 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302542
2543 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302544}
2545
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002546/*
2547 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2548 * @scsi_lun: scsi LUN id
2549 *
2550 * Returns UPIU LUN id
2551 */
2552static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
2553{
2554 if (scsi_is_wlun(scsi_lun))
2555 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
2556 | UFS_UPIU_WLUN_ID;
2557 else
2558 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
2559}
2560
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302561/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002562 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2563 * @scsi_lun: UPIU W-LUN id
2564 *
2565 * Returns SCSI W-LUN id
2566 */
2567static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2568{
2569 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2570}
2571
2572/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302573 * ufshcd_queuecommand - main entry point for SCSI requests
2574 * @cmd: command from SCSI Midlayer
2575 * @done: call back function
2576 *
2577 * Returns 0 for success, non-zero in case of failure
2578 */
2579static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2580{
2581 struct ufshcd_lrb *lrbp;
2582 struct ufs_hba *hba;
2583 unsigned long flags;
2584 int tag;
2585 int err = 0;
2586
2587 hba = shost_priv(host);
2588
2589 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002590 if (!ufshcd_valid_tag(hba, tag)) {
2591 dev_err(hba->dev,
2592 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2593 __func__, tag, cmd, cmd->request);
2594 BUG();
2595 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302596
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002597 if (!down_read_trylock(&hba->clk_scaling_lock))
2598 return SCSI_MLQUEUE_HOST_BUSY;
2599
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302600 spin_lock_irqsave(hba->host->host_lock, flags);
2601 switch (hba->ufshcd_state) {
2602 case UFSHCD_STATE_OPERATIONAL:
2603 break;
2604 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302605 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302606 goto out_unlock;
2607 case UFSHCD_STATE_ERROR:
2608 set_host_byte(cmd, DID_ERROR);
2609 cmd->scsi_done(cmd);
2610 goto out_unlock;
2611 default:
2612 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2613 __func__, hba->ufshcd_state);
2614 set_host_byte(cmd, DID_BAD_TARGET);
2615 cmd->scsi_done(cmd);
2616 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302617 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002618
2619 /* if error handling is in progress, don't issue commands */
2620 if (ufshcd_eh_in_progress(hba)) {
2621 set_host_byte(cmd, DID_ERROR);
2622 cmd->scsi_done(cmd);
2623 goto out_unlock;
2624 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302625 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302626
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002627 hba->req_abort_count = 0;
2628
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302629 /* acquire the tag to make sure device cmds don't use it */
2630 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2631 /*
2632 * Dev manage command in progress, requeue the command.
2633 * Requeuing the command helps in cases where the request *may*
2634 * find different tag instead of waiting for dev manage command
2635 * completion.
2636 */
2637 err = SCSI_MLQUEUE_HOST_BUSY;
2638 goto out;
2639 }
2640
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002641 err = ufshcd_hold(hba, true);
2642 if (err) {
2643 err = SCSI_MLQUEUE_HOST_BUSY;
2644 clear_bit_unlock(tag, &hba->lrb_in_use);
2645 goto out;
2646 }
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07002647
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002648 if (ufshcd_is_clkgating_allowed(hba))
2649 WARN_ON(hba->clk_gating.state != CLKS_ON);
2650
2651 err = ufshcd_hibern8_hold(hba, true);
2652 if (err) {
2653 clear_bit_unlock(tag, &hba->lrb_in_use);
2654 err = SCSI_MLQUEUE_HOST_BUSY;
2655 ufshcd_release(hba, true);
2656 goto out;
2657 }
2658 if (ufshcd_is_hibern8_on_idle_allowed(hba))
2659 WARN_ON(hba->hibern8_on_idle.state != HIBERN8_EXITED);
2660
2661 /* Vote PM QoS for the request */
2662 ufshcd_vops_pm_qos_req_start(hba, cmd->request);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002663
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07002664 /* IO svc time latency histogram */
2665 if (hba != NULL && cmd->request != NULL) {
2666 if (hba->latency_hist_enabled &&
2667 (cmd->request->cmd_type == REQ_TYPE_FS)) {
2668 cmd->request->lat_hist_io_start = ktime_get();
2669 cmd->request->lat_hist_enabled = 1;
2670 } else
2671 cmd->request->lat_hist_enabled = 0;
2672 }
2673
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302674 WARN_ON(hba->clk_gating.state != CLKS_ON);
2675
2676 lrbp = &hba->lrb[tag];
2677
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302678 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302679 lrbp->cmd = cmd;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002680 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302681 lrbp->sense_buffer = cmd->sense_buffer;
2682 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002683 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002684 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002685 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2686 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302687
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002688 /* form UPIU before issuing the command */
2689 err = ufshcd_compose_upiu(hba, lrbp);
2690 if (err) {
2691 if (err != -EAGAIN)
2692 dev_err(hba->dev,
2693 "%s: failed to compose upiu %d\n",
2694 __func__, err);
2695
2696 lrbp->cmd = NULL;
2697 clear_bit_unlock(tag, &hba->lrb_in_use);
2698 ufshcd_release_all(hba);
2699 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2700 goto out;
2701 }
Joao Pinto300bb132016-05-11 12:21:27 +01002702
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302703 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302704 if (err) {
2705 lrbp->cmd = NULL;
2706 clear_bit_unlock(tag, &hba->lrb_in_use);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002707 ufshcd_release_all(hba);
2708 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302709 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302710 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302711
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002712 err = ufshcd_vops_crypto_engine_cfg_start(hba, tag);
2713 if (err) {
2714 if (err != -EAGAIN)
2715 dev_err(hba->dev,
2716 "%s: failed to configure crypto engine %d\n",
2717 __func__, err);
2718
2719 scsi_dma_unmap(lrbp->cmd);
2720 lrbp->cmd = NULL;
2721 clear_bit_unlock(tag, &hba->lrb_in_use);
2722 ufshcd_release_all(hba);
2723 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2724
2725 goto out;
2726 }
2727
2728 /* Make sure descriptors are ready before ringing the doorbell */
2729 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302730 /* issue command to the controller */
2731 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002732
2733 err = ufshcd_send_command(hba, tag);
2734 if (err) {
2735 spin_unlock_irqrestore(hba->host->host_lock, flags);
2736 scsi_dma_unmap(lrbp->cmd);
2737 lrbp->cmd = NULL;
2738 clear_bit_unlock(tag, &hba->lrb_in_use);
2739 ufshcd_release_all(hba);
2740 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2741 ufshcd_vops_crypto_engine_cfg_end(hba, lrbp, cmd->request);
2742 dev_err(hba->dev, "%s: failed sending command, %d\n",
2743 __func__, err);
2744 err = DID_ERROR;
2745 goto out;
2746 }
2747
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302748out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302749 spin_unlock_irqrestore(hba->host->host_lock, flags);
2750out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002751 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302752 return err;
2753}
2754
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302755static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2756 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2757{
2758 lrbp->cmd = NULL;
2759 lrbp->sense_bufflen = 0;
2760 lrbp->sense_buffer = NULL;
2761 lrbp->task_tag = tag;
2762 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002763 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302764 lrbp->intr_cmd = true; /* No interrupt aggregation */
2765 hba->dev_cmd.type = cmd_type;
2766
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002767 return ufshcd_compose_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302768}
2769
2770static int
2771ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2772{
2773 int err = 0;
2774 unsigned long flags;
2775 u32 mask = 1 << tag;
2776
2777 /* clear outstanding transaction before retry */
2778 spin_lock_irqsave(hba->host->host_lock, flags);
2779 ufshcd_utrl_clear(hba, tag);
2780 spin_unlock_irqrestore(hba->host->host_lock, flags);
2781
2782 /*
2783 * wait for for h/w to clear corresponding bit in door-bell.
2784 * max. wait is 1 sec.
2785 */
2786 err = ufshcd_wait_for_register(hba,
2787 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002788 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302789
2790 return err;
2791}
2792
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002793static int
2794ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2795{
2796 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2797
2798 /* Get the UPIU response */
2799 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2800 UPIU_RSP_CODE_OFFSET;
2801 return query_res->response;
2802}
2803
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302804/**
2805 * ufshcd_dev_cmd_completion() - handles device management command responses
2806 * @hba: per adapter instance
2807 * @lrbp: pointer to local reference block
2808 */
2809static int
2810ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2811{
2812 int resp;
2813 int err = 0;
2814
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002815 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302816 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2817
2818 switch (resp) {
2819 case UPIU_TRANSACTION_NOP_IN:
2820 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2821 err = -EINVAL;
2822 dev_err(hba->dev, "%s: unexpected response %x\n",
2823 __func__, resp);
2824 }
2825 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302826 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002827 err = ufshcd_check_query_response(hba, lrbp);
2828 if (!err)
2829 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302830 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302831 case UPIU_TRANSACTION_REJECT_UPIU:
2832 /* TODO: handle Reject UPIU Response */
2833 err = -EPERM;
2834 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2835 __func__);
2836 break;
2837 default:
2838 err = -EINVAL;
2839 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2840 __func__, resp);
2841 break;
2842 }
2843
2844 return err;
2845}
2846
2847static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2848 struct ufshcd_lrb *lrbp, int max_timeout)
2849{
2850 int err = 0;
2851 unsigned long time_left;
2852 unsigned long flags;
2853
2854 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2855 msecs_to_jiffies(max_timeout));
2856
2857 spin_lock_irqsave(hba->host->host_lock, flags);
2858 hba->dev_cmd.complete = NULL;
2859 if (likely(time_left)) {
2860 err = ufshcd_get_tr_ocs(lrbp);
2861 if (!err)
2862 err = ufshcd_dev_cmd_completion(hba, lrbp);
2863 }
2864 spin_unlock_irqrestore(hba->host->host_lock, flags);
2865
2866 if (!time_left) {
2867 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002868 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2869 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302870 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002871 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302872 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002873 /*
2874 * in case of an error, after clearing the doorbell,
2875 * we also need to clear the outstanding_request
2876 * field in hba
2877 */
2878 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302879 }
2880
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002881 if (err)
2882 ufsdbg_set_err_state(hba);
2883
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302884 return err;
2885}
2886
2887/**
2888 * ufshcd_get_dev_cmd_tag - Get device management command tag
2889 * @hba: per-adapter instance
2890 * @tag: pointer to variable with available slot value
2891 *
2892 * Get a free slot and lock it until device management command
2893 * completes.
2894 *
2895 * Returns false if free slot is unavailable for locking, else
2896 * return true with tag value in @tag.
2897 */
2898static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
2899{
2900 int tag;
2901 bool ret = false;
2902 unsigned long tmp;
2903
2904 if (!tag_out)
2905 goto out;
2906
2907 do {
2908 tmp = ~hba->lrb_in_use;
2909 tag = find_last_bit(&tmp, hba->nutrs);
2910 if (tag >= hba->nutrs)
2911 goto out;
2912 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
2913
2914 *tag_out = tag;
2915 ret = true;
2916out:
2917 return ret;
2918}
2919
2920static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
2921{
2922 clear_bit_unlock(tag, &hba->lrb_in_use);
2923}
2924
2925/**
2926 * ufshcd_exec_dev_cmd - API for sending device management requests
2927 * @hba - UFS hba
2928 * @cmd_type - specifies the type (NOP, Query...)
2929 * @timeout - time in seconds
2930 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302931 * NOTE: Since there is only one available tag for device management commands,
2932 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302933 */
2934static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2935 enum dev_cmd_type cmd_type, int timeout)
2936{
2937 struct ufshcd_lrb *lrbp;
2938 int err;
2939 int tag;
2940 struct completion wait;
2941 unsigned long flags;
2942
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002943 down_read(&hba->clk_scaling_lock);
2944
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302945 /*
2946 * Get free slot, sleep if slots are unavailable.
2947 * Even though we use wait_event() which sleeps indefinitely,
2948 * the maximum wait time is bounded by SCSI request timeout.
2949 */
2950 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
2951
2952 init_completion(&wait);
2953 lrbp = &hba->lrb[tag];
2954 WARN_ON(lrbp->cmd);
2955 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2956 if (unlikely(err))
2957 goto out_put_tag;
2958
2959 hba->dev_cmd.complete = &wait;
2960
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002961 /* Make sure descriptors are ready before ringing the doorbell */
2962 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302963 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002964 err = ufshcd_send_command(hba, tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302965 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002966 if (err) {
2967 dev_err(hba->dev, "%s: failed sending command, %d\n",
2968 __func__, err);
2969 goto out_put_tag;
2970 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302971 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2972
2973out_put_tag:
2974 ufshcd_put_dev_cmd_tag(hba, tag);
2975 wake_up(&hba->dev_cmd.tag_wq);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002976 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302977 return err;
2978}
2979
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302980/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002981 * ufshcd_init_query() - init the query response and request parameters
2982 * @hba: per-adapter instance
2983 * @request: address of the request pointer to be initialized
2984 * @response: address of the response pointer to be initialized
2985 * @opcode: operation to perform
2986 * @idn: flag idn to access
2987 * @index: LU number to access
2988 * @selector: query/flag/descriptor further identification
2989 */
2990static inline void ufshcd_init_query(struct ufs_hba *hba,
2991 struct ufs_query_req **request, struct ufs_query_res **response,
2992 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2993{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002994 int idn_t = (int)idn;
2995
2996 ufsdbg_error_inject_dispatcher(hba,
2997 ERR_INJECT_QUERY, idn_t, (int *)&idn_t);
2998 idn = idn_t;
2999
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003000 *request = &hba->dev_cmd.query.request;
3001 *response = &hba->dev_cmd.query.response;
3002 memset(*request, 0, sizeof(struct ufs_query_req));
3003 memset(*response, 0, sizeof(struct ufs_query_res));
3004 (*request)->upiu_req.opcode = opcode;
3005 (*request)->upiu_req.idn = idn;
3006 (*request)->upiu_req.index = index;
3007 (*request)->upiu_req.selector = selector;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003008
3009 ufshcd_update_query_stats(hba, opcode, idn);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003010}
3011
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003012static int ufshcd_query_flag_retry(struct ufs_hba *hba,
3013 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
3014{
3015 int ret;
3016 int retries;
3017
3018 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
3019 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
3020 if (ret)
3021 dev_dbg(hba->dev,
3022 "%s: failed with error %d, retries %d\n",
3023 __func__, ret, retries);
3024 else
3025 break;
3026 }
3027
3028 if (ret)
3029 dev_err(hba->dev,
3030 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
3031 __func__, opcode, idn, ret, retries);
3032 return ret;
3033}
3034
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003035/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303036 * ufshcd_query_flag() - API function for sending flag query requests
3037 * hba: per-adapter instance
3038 * query_opcode: flag query to perform
3039 * idn: flag idn to access
3040 * flag_res: the flag value after the query request completes
3041 *
3042 * Returns 0 for success, non-zero in case of failure
3043 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003044int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05303045 enum flag_idn idn, bool *flag_res)
3046{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003047 struct ufs_query_req *request = NULL;
3048 struct ufs_query_res *response = NULL;
3049 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003050 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05303051
3052 BUG_ON(!hba);
3053
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003054 ufshcd_hold_all(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303055 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003056 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3057 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05303058
3059 switch (opcode) {
3060 case UPIU_QUERY_OPCODE_SET_FLAG:
3061 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
3062 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
3063 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3064 break;
3065 case UPIU_QUERY_OPCODE_READ_FLAG:
3066 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3067 if (!flag_res) {
3068 /* No dummy reads */
3069 dev_err(hba->dev, "%s: Invalid argument for read request\n",
3070 __func__);
3071 err = -EINVAL;
3072 goto out_unlock;
3073 }
3074 break;
3075 default:
3076 dev_err(hba->dev,
3077 "%s: Expected query flag opcode but got = %d\n",
3078 __func__, opcode);
3079 err = -EINVAL;
3080 goto out_unlock;
3081 }
Dolev Raviv68078d52013-07-30 00:35:58 +05303082
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003083 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05303084
3085 if (err) {
3086 dev_err(hba->dev,
3087 "%s: Sending flag query for idn %d failed, err = %d\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003088 __func__, request->upiu_req.idn, err);
Dolev Raviv68078d52013-07-30 00:35:58 +05303089 goto out_unlock;
3090 }
3091
3092 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303093 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05303094 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
3095
3096out_unlock:
3097 mutex_unlock(&hba->dev_cmd.lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003098 ufshcd_release_all(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303099 return err;
3100}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003101EXPORT_SYMBOL(ufshcd_query_flag);
Dolev Raviv68078d52013-07-30 00:35:58 +05303102
3103/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303104 * ufshcd_query_attr - API function for sending attribute requests
3105 * hba: per-adapter instance
3106 * opcode: attribute opcode
3107 * idn: attribute idn to access
3108 * index: index field
3109 * selector: selector field
3110 * attr_val: the attribute value after the query request completes
3111 *
3112 * Returns 0 for success, non-zero in case of failure
3113*/
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003114int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303115 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
3116{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003117 struct ufs_query_req *request = NULL;
3118 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303119 int err;
3120
3121 BUG_ON(!hba);
3122
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003123 ufshcd_hold_all(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303124 if (!attr_val) {
3125 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
3126 __func__, opcode);
3127 err = -EINVAL;
3128 goto out;
3129 }
3130
3131 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003132 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3133 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303134
3135 switch (opcode) {
3136 case UPIU_QUERY_OPCODE_WRITE_ATTR:
3137 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303138 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303139 break;
3140 case UPIU_QUERY_OPCODE_READ_ATTR:
3141 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3142 break;
3143 default:
3144 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3145 __func__, opcode);
3146 err = -EINVAL;
3147 goto out_unlock;
3148 }
3149
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003150 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303151
3152 if (err) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003153 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3154 __func__, opcode,
3155 request->upiu_req.idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303156 goto out_unlock;
3157 }
3158
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303159 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303160
3161out_unlock:
3162 mutex_unlock(&hba->dev_cmd.lock);
3163out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003164 ufshcd_release_all(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303165 return err;
3166}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003167EXPORT_SYMBOL(ufshcd_query_attr);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303168
3169/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003170 * ufshcd_query_attr_retry() - API function for sending query
3171 * attribute with retries
3172 * @hba: per-adapter instance
3173 * @opcode: attribute opcode
3174 * @idn: attribute idn to access
3175 * @index: index field
3176 * @selector: selector field
3177 * @attr_val: the attribute value after the query request
3178 * completes
3179 *
3180 * Returns 0 for success, non-zero in case of failure
3181*/
3182static int ufshcd_query_attr_retry(struct ufs_hba *hba,
3183 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3184 u32 *attr_val)
3185{
3186 int ret = 0;
3187 u32 retries;
3188
3189 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3190 ret = ufshcd_query_attr(hba, opcode, idn, index,
3191 selector, attr_val);
3192 if (ret)
3193 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3194 __func__, ret, retries);
3195 else
3196 break;
3197 }
3198
3199 if (ret)
3200 dev_err(hba->dev,
3201 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003202 __func__, idn, ret, retries);
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003203 return ret;
3204}
3205
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003206static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003207 enum query_opcode opcode, enum desc_idn idn, u8 index,
3208 u8 selector, u8 *desc_buf, int *buf_len)
3209{
3210 struct ufs_query_req *request = NULL;
3211 struct ufs_query_res *response = NULL;
3212 int err;
3213
3214 BUG_ON(!hba);
3215
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003216 ufshcd_hold_all(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003217 if (!desc_buf) {
3218 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3219 __func__, opcode);
3220 err = -EINVAL;
3221 goto out;
3222 }
3223
3224 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
3225 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3226 __func__, *buf_len);
3227 err = -EINVAL;
3228 goto out;
3229 }
3230
3231 mutex_lock(&hba->dev_cmd.lock);
3232 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3233 selector);
3234 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003235 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003236
3237 switch (opcode) {
3238 case UPIU_QUERY_OPCODE_WRITE_DESC:
3239 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3240 break;
3241 case UPIU_QUERY_OPCODE_READ_DESC:
3242 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3243 break;
3244 default:
3245 dev_err(hba->dev,
3246 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3247 __func__, opcode);
3248 err = -EINVAL;
3249 goto out_unlock;
3250 }
3251
3252 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3253
3254 if (err) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003255 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3256 __func__, opcode,
3257 request->upiu_req.idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003258 goto out_unlock;
3259 }
3260
3261 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003262 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003263
3264out_unlock:
3265 mutex_unlock(&hba->dev_cmd.lock);
3266out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003267 ufshcd_release_all(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003268 return err;
3269}
3270
3271/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003272 * ufshcd_query_descriptor - API function for sending descriptor requests
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003273 * hba: per-adapter instance
3274 * opcode: attribute opcode
3275 * idn: attribute idn to access
3276 * index: index field
3277 * selector: selector field
3278 * desc_buf: the buffer that contains the descriptor
3279 * buf_len: length parameter passed to the device
3280 *
3281 * Returns 0 for success, non-zero in case of failure.
3282 * The buf_len parameter will contain, on return, the length parameter
3283 * received on the response.
3284 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003285int ufshcd_query_descriptor(struct ufs_hba *hba,
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003286 enum query_opcode opcode, enum desc_idn idn, u8 index,
3287 u8 selector, u8 *desc_buf, int *buf_len)
3288{
3289 int err;
3290 int retries;
3291
3292 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3293 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3294 selector, desc_buf, buf_len);
3295 if (!err || err == -EINVAL)
3296 break;
3297 }
3298
3299 return err;
3300}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003301EXPORT_SYMBOL(ufshcd_query_descriptor);
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003302
3303/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003304 * ufshcd_read_desc_param - read the specified descriptor parameter
3305 * @hba: Pointer to adapter instance
3306 * @desc_id: descriptor idn value
3307 * @desc_index: descriptor index
3308 * @param_offset: offset of the parameter to read
3309 * @param_read_buf: pointer to buffer where parameter would be read
3310 * @param_size: sizeof(param_read_buf)
3311 *
3312 * Return 0 in case of success, non-zero otherwise
3313 */
3314static int ufshcd_read_desc_param(struct ufs_hba *hba,
3315 enum desc_idn desc_id,
3316 int desc_index,
3317 u32 param_offset,
3318 u8 *param_read_buf,
3319 u32 param_size)
3320{
3321 int ret;
3322 u8 *desc_buf;
3323 u32 buff_len;
3324 bool is_kmalloc = true;
3325
3326 /* safety checks */
3327 if (desc_id >= QUERY_DESC_IDN_MAX)
3328 return -EINVAL;
3329
3330 buff_len = ufs_query_desc_max_size[desc_id];
3331 if ((param_offset + param_size) > buff_len)
3332 return -EINVAL;
3333
3334 if (!param_offset && (param_size == buff_len)) {
3335 /* memory space already available to hold full descriptor */
3336 desc_buf = param_read_buf;
3337 is_kmalloc = false;
3338 } else {
3339 /* allocate memory to hold full descriptor */
3340 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3341 if (!desc_buf)
3342 return -ENOMEM;
3343 }
3344
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003345 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
3346 desc_id, desc_index, 0, desc_buf,
3347 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003348
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003349 if (ret) {
3350 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3351 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003352
3353 goto out;
3354 }
3355
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003356 /* Sanity check */
3357 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3358 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3359 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3360 ret = -EINVAL;
3361 goto out;
3362 }
3363
3364 /*
3365 * While reading variable size descriptors (like string descriptor),
3366 * some UFS devices may report the "LENGTH" (field in "Transaction
3367 * Specific fields" of Query Response UPIU) same as what was requested
3368 * in Query Request UPIU instead of reporting the actual size of the
3369 * variable size descriptor.
3370 * Although it's safe to ignore the "LENGTH" field for variable size
3371 * descriptors as we can always derive the length of the descriptor from
3372 * the descriptor header fields. Hence this change impose the length
3373 * match check only for fixed size descriptors (for which we always
3374 * request the correct size as part of Query Request UPIU).
3375 */
3376 if ((desc_id != QUERY_DESC_IDN_STRING) &&
3377 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
3378 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
3379 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
3380 ret = -EINVAL;
3381 goto out;
3382 }
3383
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003384 if (is_kmalloc)
3385 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3386out:
3387 if (is_kmalloc)
3388 kfree(desc_buf);
3389 return ret;
3390}
3391
3392static inline int ufshcd_read_desc(struct ufs_hba *hba,
3393 enum desc_idn desc_id,
3394 int desc_index,
3395 u8 *buf,
3396 u32 size)
3397{
3398 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3399}
3400
3401static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3402 u8 *buf,
3403 u32 size)
3404{
3405 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
3406}
3407
Yaniv Gardib573d482016-03-10 17:37:09 +02003408int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
3409{
3410 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3411}
Yaniv Gardib573d482016-03-10 17:37:09 +02003412
3413/**
3414 * ufshcd_read_string_desc - read string descriptor
3415 * @hba: pointer to adapter instance
3416 * @desc_index: descriptor index
3417 * @buf: pointer to buffer where descriptor would be read
3418 * @size: size of buf
3419 * @ascii: if true convert from unicode to ascii characters
3420 *
3421 * Return 0 in case of success, non-zero otherwise
3422 */
3423int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
3424 u32 size, bool ascii)
3425{
3426 int err = 0;
3427
3428 err = ufshcd_read_desc(hba,
3429 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3430
3431 if (err) {
3432 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3433 __func__, QUERY_REQ_RETRIES, err);
3434 goto out;
3435 }
3436
3437 if (ascii) {
3438 int desc_len;
3439 int ascii_len;
3440 int i;
3441 char *buff_ascii;
3442
3443 desc_len = buf[0];
3444 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3445 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3446 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3447 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3448 __func__);
3449 err = -ENOMEM;
3450 goto out;
3451 }
3452
3453 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3454 if (!buff_ascii) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003455 dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
3456 __func__, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003457 err = -ENOMEM;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003458 goto out_free_buff;
Yaniv Gardib573d482016-03-10 17:37:09 +02003459 }
3460
3461 /*
3462 * the descriptor contains string in UTF16 format
3463 * we need to convert to utf-8 so it can be displayed
3464 */
3465 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3466 desc_len - QUERY_DESC_HDR_SIZE,
3467 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3468
3469 /* replace non-printable or non-ASCII characters with spaces */
3470 for (i = 0; i < ascii_len; i++)
3471 ufshcd_remove_non_printable(&buff_ascii[i]);
3472
3473 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3474 size - QUERY_DESC_HDR_SIZE);
3475 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3476 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003477out_free_buff:
Yaniv Gardib573d482016-03-10 17:37:09 +02003478 kfree(buff_ascii);
3479 }
3480out:
3481 return err;
3482}
Yaniv Gardib573d482016-03-10 17:37:09 +02003483
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003484/**
3485 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3486 * @hba: Pointer to adapter instance
3487 * @lun: lun id
3488 * @param_offset: offset of the parameter to read
3489 * @param_read_buf: pointer to buffer where parameter would be read
3490 * @param_size: sizeof(param_read_buf)
3491 *
3492 * Return 0 in case of success, non-zero otherwise
3493 */
3494static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3495 int lun,
3496 enum unit_desc_param param_offset,
3497 u8 *param_read_buf,
3498 u32 param_size)
3499{
3500 /*
3501 * Unit descriptors are only available for general purpose LUs (LUN id
3502 * from 0 to 7) and RPMB Well known LU.
3503 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003504 if (!ufs_is_valid_unit_desc_lun(lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003505 return -EOPNOTSUPP;
3506
3507 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3508 param_offset, param_read_buf, param_size);
3509}
3510
3511/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303512 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3513 * @hba: per adapter instance
3514 *
3515 * 1. Allocate DMA memory for Command Descriptor array
3516 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3517 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3518 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3519 * (UTMRDL)
3520 * 4. Allocate memory for local reference block(lrb).
3521 *
3522 * Returns 0 for success, non-zero in case of failure
3523 */
3524static int ufshcd_memory_alloc(struct ufs_hba *hba)
3525{
3526 size_t utmrdl_size, utrdl_size, ucdl_size;
3527
3528 /* Allocate memory for UTP command descriptors */
3529 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003530 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3531 ucdl_size,
3532 &hba->ucdl_dma_addr,
3533 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303534
3535 /*
3536 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3537 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3538 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3539 * be aligned to 128 bytes as well
3540 */
3541 if (!hba->ucdl_base_addr ||
3542 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303543 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303544 "Command Descriptor Memory allocation failed\n");
3545 goto out;
3546 }
3547
3548 /*
3549 * Allocate memory for UTP Transfer descriptors
3550 * UFSHCI requires 1024 byte alignment of UTRD
3551 */
3552 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003553 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3554 utrdl_size,
3555 &hba->utrdl_dma_addr,
3556 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303557 if (!hba->utrdl_base_addr ||
3558 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303559 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303560 "Transfer Descriptor Memory allocation failed\n");
3561 goto out;
3562 }
3563
3564 /*
3565 * Allocate memory for UTP Task Management descriptors
3566 * UFSHCI requires 1024 byte alignment of UTMRD
3567 */
3568 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003569 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3570 utmrdl_size,
3571 &hba->utmrdl_dma_addr,
3572 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303573 if (!hba->utmrdl_base_addr ||
3574 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303575 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303576 "Task Management Descriptor Memory allocation failed\n");
3577 goto out;
3578 }
3579
3580 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003581 hba->lrb = devm_kzalloc(hba->dev,
3582 hba->nutrs * sizeof(struct ufshcd_lrb),
3583 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303584 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303585 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303586 goto out;
3587 }
3588 return 0;
3589out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303590 return -ENOMEM;
3591}
3592
3593/**
3594 * ufshcd_host_memory_configure - configure local reference block with
3595 * memory offsets
3596 * @hba: per adapter instance
3597 *
3598 * Configure Host memory space
3599 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3600 * address.
3601 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3602 * and PRDT offset.
3603 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3604 * into local reference block.
3605 */
3606static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3607{
3608 struct utp_transfer_cmd_desc *cmd_descp;
3609 struct utp_transfer_req_desc *utrdlp;
3610 dma_addr_t cmd_desc_dma_addr;
3611 dma_addr_t cmd_desc_element_addr;
3612 u16 response_offset;
3613 u16 prdt_offset;
3614 int cmd_desc_size;
3615 int i;
3616
3617 utrdlp = hba->utrdl_base_addr;
3618 cmd_descp = hba->ucdl_base_addr;
3619
3620 response_offset =
3621 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3622 prdt_offset =
3623 offsetof(struct utp_transfer_cmd_desc, prd_table);
3624
3625 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3626 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3627
3628 for (i = 0; i < hba->nutrs; i++) {
3629 /* Configure UTRD with command descriptor base address */
3630 cmd_desc_element_addr =
3631 (cmd_desc_dma_addr + (cmd_desc_size * i));
3632 utrdlp[i].command_desc_base_addr_lo =
3633 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3634 utrdlp[i].command_desc_base_addr_hi =
3635 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3636
3637 /* Response upiu and prdt offset should be in double words */
3638 utrdlp[i].response_upiu_offset =
3639 cpu_to_le16((response_offset >> 2));
3640 utrdlp[i].prd_table_offset =
3641 cpu_to_le16((prdt_offset >> 2));
3642 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303643 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303644
3645 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003646 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3647 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303648 hba->lrb[i].ucd_req_ptr =
3649 (struct utp_upiu_req *)(cmd_descp + i);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003650 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303651 hba->lrb[i].ucd_rsp_ptr =
3652 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003653 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3654 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303655 hba->lrb[i].ucd_prdt_ptr =
3656 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003657 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3658 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303659 }
3660}
3661
3662/**
3663 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3664 * @hba: per adapter instance
3665 *
3666 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3667 * in order to initialize the Unipro link startup procedure.
3668 * Once the Unipro links are up, the device connected to the controller
3669 * is detected.
3670 *
3671 * Returns 0 on success, non-zero value on failure
3672 */
3673static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3674{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303675 struct uic_command uic_cmd = {0};
3676 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303677
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303678 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3679
3680 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3681 if (ret)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003682 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303683 "dme-link-startup: error code %d\n", ret);
3684 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303685}
3686
Yaniv Gardicad2e032015-03-31 17:37:14 +03003687static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3688{
3689 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3690 unsigned long min_sleep_time_us;
3691
3692 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3693 return;
3694
3695 /*
3696 * last_dme_cmd_tstamp will be 0 only for 1st call to
3697 * this function
3698 */
3699 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3700 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3701 } else {
3702 unsigned long delta =
3703 (unsigned long) ktime_to_us(
3704 ktime_sub(ktime_get(),
3705 hba->last_dme_cmd_tstamp));
3706
3707 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3708 min_sleep_time_us =
3709 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3710 else
3711 return; /* no more delay required */
3712 }
3713
3714 /* allow sleep for extra 50us if needed */
3715 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3716}
3717
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003718static inline void ufshcd_save_tstamp_of_last_dme_cmd(
3719 struct ufs_hba *hba)
3720{
3721 if (hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)
3722 hba->last_dme_cmd_tstamp = ktime_get();
3723}
3724
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303725/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303726 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3727 * @hba: per adapter instance
3728 * @attr_sel: uic command argument1
3729 * @attr_set: attribute set type as uic command argument2
3730 * @mib_val: setting value as uic command argument3
3731 * @peer: indicate whether peer or local
3732 *
3733 * Returns 0 on success, non-zero value on failure
3734 */
3735int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3736 u8 attr_set, u32 mib_val, u8 peer)
3737{
3738 struct uic_command uic_cmd = {0};
3739 static const char *const action[] = {
3740 "dme-set",
3741 "dme-peer-set"
3742 };
3743 const char *set = action[!!peer];
3744 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003745 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303746
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003747 ufsdbg_error_inject_dispatcher(hba,
3748 ERR_INJECT_DME_ATTR, attr_sel, &attr_sel);
3749
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303750 uic_cmd.command = peer ?
3751 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3752 uic_cmd.argument1 = attr_sel;
3753 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3754 uic_cmd.argument3 = mib_val;
3755
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003756 do {
3757 /* for peer attributes we retry upon failure */
3758 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3759 if (ret)
3760 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3761 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3762 } while (ret && peer && --retries);
3763
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003764 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003765 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003766 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3767 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303768
3769 return ret;
3770}
3771EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3772
3773/**
3774 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3775 * @hba: per adapter instance
3776 * @attr_sel: uic command argument1
3777 * @mib_val: the value of the attribute as returned by the UIC command
3778 * @peer: indicate whether peer or local
3779 *
3780 * Returns 0 on success, non-zero value on failure
3781 */
3782int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3783 u32 *mib_val, u8 peer)
3784{
3785 struct uic_command uic_cmd = {0};
3786 static const char *const action[] = {
3787 "dme-get",
3788 "dme-peer-get"
3789 };
3790 const char *get = action[!!peer];
3791 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003792 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003793 struct ufs_pa_layer_attr orig_pwr_info;
3794 struct ufs_pa_layer_attr temp_pwr_info;
3795 bool pwr_mode_change = false;
3796
3797 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3798 orig_pwr_info = hba->pwr_info;
3799 temp_pwr_info = orig_pwr_info;
3800
3801 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3802 orig_pwr_info.pwr_rx == FAST_MODE) {
3803 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3804 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3805 pwr_mode_change = true;
3806 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3807 orig_pwr_info.pwr_rx == SLOW_MODE) {
3808 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3809 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3810 pwr_mode_change = true;
3811 }
3812 if (pwr_mode_change) {
3813 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3814 if (ret)
3815 goto out;
3816 }
3817 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303818
3819 uic_cmd.command = peer ?
3820 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003821
3822 ufsdbg_error_inject_dispatcher(hba,
3823 ERR_INJECT_DME_ATTR, attr_sel, &attr_sel);
3824
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303825 uic_cmd.argument1 = attr_sel;
3826
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003827 do {
3828 /* for peer attributes we retry upon failure */
3829 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3830 if (ret)
3831 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3832 get, UIC_GET_ATTR_ID(attr_sel), ret);
3833 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303834
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003835 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003836 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003837 get, UIC_GET_ATTR_ID(attr_sel),
3838 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003839
3840 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303841 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003842
3843 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3844 && pwr_mode_change)
3845 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303846out:
3847 return ret;
3848}
3849EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3850
3851/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003852 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3853 * state) and waits for it to take effect.
3854 *
3855 * @hba: per adapter instance
3856 * @cmd: UIC command to execute
3857 *
3858 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3859 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3860 * and device UniPro link and hence it's final completion would be indicated by
3861 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3862 * addition to normal UIC command completion Status (UCCS). This function only
3863 * returns after the relevant status bits indicate the completion.
3864 *
3865 * Returns 0 on success, non-zero value on failure
3866 */
3867static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3868{
3869 struct completion uic_async_done;
3870 unsigned long flags;
3871 u8 status;
3872 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003873 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003874
3875 mutex_lock(&hba->uic_cmd_mutex);
3876 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003877 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003878
3879 spin_lock_irqsave(hba->host->host_lock, flags);
3880 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003881 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3882 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3883 /*
3884 * Make sure UIC command completion interrupt is disabled before
3885 * issuing UIC command.
3886 */
3887 wmb();
3888 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003889 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003890 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3891 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003892 if (ret) {
3893 dev_err(hba->dev,
3894 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3895 cmd->command, cmd->argument3, ret);
3896 goto out;
3897 }
3898
3899 if (!wait_for_completion_timeout(hba->uic_async_done,
3900 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3901 dev_err(hba->dev,
3902 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3903 cmd->command, cmd->argument3);
3904 ret = -ETIMEDOUT;
3905 goto out;
3906 }
3907
3908 status = ufshcd_get_upmcrs(hba);
3909 if (status != PWR_LOCAL) {
3910 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09003911 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003912 cmd->command, status);
3913 ret = (status != PWR_OK) ? status : -1;
3914 }
3915out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003916 if (ret)
3917 ufsdbg_set_err_state(hba);
3918
3919 ufshcd_save_tstamp_of_last_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003920 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003921 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003922 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003923 if (reenable_intr)
3924 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003925 spin_unlock_irqrestore(hba->host->host_lock, flags);
3926 mutex_unlock(&hba->uic_cmd_mutex);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003927 return ret;
3928}
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003929
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003930int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, u64 wait_timeout_us)
3931{
3932 unsigned long flags;
3933 int ret = 0;
3934 u32 tm_doorbell;
3935 u32 tr_doorbell;
3936 bool timeout = false, do_last_check = false;
3937 ktime_t start;
3938
3939 ufshcd_hold_all(hba);
3940 spin_lock_irqsave(hba->host->host_lock, flags);
3941 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
3942 ret = -EBUSY;
3943 goto out;
3944 }
3945
3946 /*
3947 * Wait for all the outstanding tasks/transfer requests.
3948 * Verify by checking the doorbell registers are clear.
3949 */
3950 start = ktime_get();
3951 do {
3952 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
3953 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3954 if (!tm_doorbell && !tr_doorbell) {
3955 timeout = false;
3956 break;
3957 } else if (do_last_check) {
3958 break;
3959 }
3960
3961 spin_unlock_irqrestore(hba->host->host_lock, flags);
3962 schedule();
3963 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
3964 wait_timeout_us) {
3965 timeout = true;
3966 /*
3967 * We might have scheduled out for long time so make
3968 * sure to check if doorbells are cleared by this time
3969 * or not.
3970 */
3971 do_last_check = true;
3972 }
3973 spin_lock_irqsave(hba->host->host_lock, flags);
3974 } while (tm_doorbell || tr_doorbell);
3975
3976 if (timeout) {
3977 dev_err(hba->dev,
3978 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
3979 __func__, tm_doorbell, tr_doorbell);
3980 ret = -EBUSY;
3981 }
3982out:
3983 spin_unlock_irqrestore(hba->host->host_lock, flags);
3984 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003985 return ret;
3986}
3987
3988/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303989 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3990 * using DME_SET primitives.
3991 * @hba: per adapter instance
3992 * @mode: powr mode value
3993 *
3994 * Returns 0 on success, non-zero value on failure
3995 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303996static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303997{
3998 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003999 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304000
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004001 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
4002 ret = ufshcd_dme_set(hba,
4003 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
4004 if (ret) {
4005 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
4006 __func__, ret);
4007 goto out;
4008 }
4009 }
4010
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304011 uic_cmd.command = UIC_CMD_DME_SET;
4012 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
4013 uic_cmd.argument3 = mode;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004014 ufshcd_hold_all(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004015 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004016 ufshcd_release_all(hba);
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004017out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004018 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004019}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304020
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004021static int ufshcd_link_recovery(struct ufs_hba *hba)
4022{
4023 int ret;
4024 unsigned long flags;
4025
4026 spin_lock_irqsave(hba->host->host_lock, flags);
4027 hba->ufshcd_state = UFSHCD_STATE_RESET;
4028 ufshcd_set_eh_in_progress(hba);
4029 spin_unlock_irqrestore(hba->host->host_lock, flags);
4030
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004031 ret = ufshcd_vops_full_reset(hba);
4032 if (ret)
4033 dev_warn(hba->dev,
4034 "full reset returned %d, trying to recover the link\n",
4035 ret);
4036
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004037 ret = ufshcd_host_reset_and_restore(hba);
4038
4039 spin_lock_irqsave(hba->host->host_lock, flags);
4040 if (ret)
4041 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4042 ufshcd_clear_eh_in_progress(hba);
4043 spin_unlock_irqrestore(hba->host->host_lock, flags);
4044
4045 if (ret)
4046 dev_err(hba->dev, "%s: link recovery failed, err %d",
4047 __func__, ret);
4048
4049 return ret;
4050}
4051
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004052static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004053{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004054 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004055 struct uic_command uic_cmd = {0};
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004056 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004057
4058 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004059 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004060 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
4061 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004062
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004063 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004064 ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_ENTER);
4065 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d",
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004066 __func__, ret);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004067 /*
4068 * If link recovery fails then return error so that caller
4069 * don't retry the hibern8 enter again.
4070 */
4071 if (ufshcd_link_recovery(hba))
4072 ret = -ENOLINK;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004073 } else {
4074 dev_dbg(hba->dev, "%s: Hibern8 Enter at %lld us", __func__,
4075 ktime_to_us(ktime_get()));
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004076 }
4077
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004078 return ret;
4079}
4080
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004081int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004082{
4083 int ret = 0, retries;
4084
4085 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
4086 ret = __ufshcd_uic_hibern8_enter(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004087 if (!ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004088 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004089 /* Unable to recover the link, so no point proceeding */
4090 if (ret == -ENOLINK)
4091 BUG();
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004092 }
4093out:
4094 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004095}
4096
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004097int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004098{
4099 struct uic_command uic_cmd = {0};
4100 int ret;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004101 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004102
4103 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
4104 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004105 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
4106 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4107
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304108 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004109 ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_EXIT);
4110 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d",
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004111 __func__, ret);
4112 ret = ufshcd_link_recovery(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004113 /* Unable to recover the link, so no point proceeding */
4114 if (ret)
4115 BUG();
4116 } else {
4117 dev_dbg(hba->dev, "%s: Hibern8 Exit at %lld us", __func__,
4118 ktime_to_us(ktime_get()));
4119 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
4120 hba->ufs_stats.hibern8_exit_cnt++;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304121 }
4122
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304123 return ret;
4124}
4125
Yaniv Gardi50646362014-10-23 13:25:13 +03004126 /**
4127 * ufshcd_init_pwr_info - setting the POR (power on reset)
4128 * values in hba power info
4129 * @hba: per-adapter instance
4130 */
4131static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4132{
4133 hba->pwr_info.gear_rx = UFS_PWM_G1;
4134 hba->pwr_info.gear_tx = UFS_PWM_G1;
4135 hba->pwr_info.lane_rx = 1;
4136 hba->pwr_info.lane_tx = 1;
4137 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4138 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4139 hba->pwr_info.hs_rate = 0;
4140}
4141
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304142/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004143 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4144 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304145 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004146static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304147{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004148 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4149
4150 if (hba->max_pwr_info.is_valid)
4151 return 0;
4152
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004153 pwr_info->pwr_tx = FAST_MODE;
4154 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004155 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304156
4157 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004158 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4159 &pwr_info->lane_rx);
4160 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4161 &pwr_info->lane_tx);
4162
4163 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4164 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4165 __func__,
4166 pwr_info->lane_rx,
4167 pwr_info->lane_tx);
4168 return -EINVAL;
4169 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304170
4171 /*
4172 * First, get the maximum gears of HS speed.
4173 * If a zero value, it means there is no HSGEAR capability.
4174 * Then, get the maximum gears of PWM speed.
4175 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004176 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4177 if (!pwr_info->gear_rx) {
4178 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4179 &pwr_info->gear_rx);
4180 if (!pwr_info->gear_rx) {
4181 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4182 __func__, pwr_info->gear_rx);
4183 return -EINVAL;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004184 } else {
4185 if (hba->limit_rx_pwm_gear > 0 &&
4186 (hba->limit_rx_pwm_gear < pwr_info->gear_rx))
4187 pwr_info->gear_rx = hba->limit_rx_pwm_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004188 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004189 pwr_info->pwr_rx = SLOW_MODE;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004190 } else {
4191 if (hba->limit_rx_hs_gear > 0 &&
4192 (hba->limit_rx_hs_gear < pwr_info->gear_rx))
4193 pwr_info->gear_rx = hba->limit_rx_hs_gear;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304194 }
4195
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004196 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4197 &pwr_info->gear_tx);
4198 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304199 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004200 &pwr_info->gear_tx);
4201 if (!pwr_info->gear_tx) {
4202 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4203 __func__, pwr_info->gear_tx);
4204 return -EINVAL;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004205 } else {
4206 if (hba->limit_tx_pwm_gear > 0 &&
4207 (hba->limit_tx_pwm_gear < pwr_info->gear_tx))
4208 pwr_info->gear_tx = hba->limit_tx_pwm_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004209 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004210 pwr_info->pwr_tx = SLOW_MODE;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004211 } else {
4212 if (hba->limit_tx_hs_gear > 0 &&
4213 (hba->limit_tx_hs_gear < pwr_info->gear_tx))
4214 pwr_info->gear_tx = hba->limit_tx_hs_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004215 }
4216
4217 hba->max_pwr_info.is_valid = true;
4218 return 0;
4219}
4220
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004221int ufshcd_change_power_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004222 struct ufs_pa_layer_attr *pwr_mode)
4223{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004224 int ret = 0;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004225
4226 /* if already configured to the requested pwr_mode */
4227 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4228 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4229 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4230 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4231 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4232 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4233 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4234 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4235 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304236 }
4237
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004238 ufsdbg_error_inject_dispatcher(hba, ERR_INJECT_PWR_CHANGE, 0, &ret);
4239 if (ret)
4240 return ret;
4241
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304242 /*
4243 * Configure attributes for power mode change with below.
4244 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4245 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4246 * - PA_HSSERIES
4247 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004248 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4249 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4250 pwr_mode->lane_rx);
4251 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4252 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304253 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004254 else
4255 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304256
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004257 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4258 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4259 pwr_mode->lane_tx);
4260 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4261 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304262 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004263 else
4264 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304265
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004266 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4267 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4268 pwr_mode->pwr_rx == FAST_MODE ||
4269 pwr_mode->pwr_tx == FAST_MODE)
4270 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4271 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304272
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004273 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4274 DL_FC0ProtectionTimeOutVal_Default);
4275 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4276 DL_TC0ReplayTimeOutVal_Default);
4277 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4278 DL_AFC0ReqTimeOutVal_Default);
4279
4280 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4281 DL_FC0ProtectionTimeOutVal_Default);
4282 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4283 DL_TC0ReplayTimeOutVal_Default);
4284 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4285 DL_AFC0ReqTimeOutVal_Default);
4286
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004287 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4288 | pwr_mode->pwr_tx);
4289
4290 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004291 ufshcd_update_error_stats(hba, UFS_ERR_POWER_MODE_CHANGE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304292 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004293 "%s: power mode change failed %d\n", __func__, ret);
4294 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004295 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4296 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004297
4298 memcpy(&hba->pwr_info, pwr_mode,
4299 sizeof(struct ufs_pa_layer_attr));
4300 }
4301
4302 return ret;
4303}
4304
4305/**
4306 * ufshcd_config_pwr_mode - configure a new power mode
4307 * @hba: per-adapter instance
4308 * @desired_pwr_mode: desired power configuration
4309 */
4310static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
4311 struct ufs_pa_layer_attr *desired_pwr_mode)
4312{
4313 struct ufs_pa_layer_attr final_params = { 0 };
4314 int ret;
4315
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004316 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4317 desired_pwr_mode, &final_params);
4318
4319 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004320 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4321
4322 ret = ufshcd_change_power_mode(hba, &final_params);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004323 if (!ret)
4324 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304325
4326 return ret;
4327}
4328
4329/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304330 * ufshcd_complete_dev_init() - checks device readiness
4331 * hba: per-adapter instance
4332 *
4333 * Set fDeviceInit flag and poll until device toggles it.
4334 */
4335static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4336{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004337 int i;
4338 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05304339 bool flag_res = 1;
4340
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004341 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4342 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304343 if (err) {
4344 dev_err(hba->dev,
4345 "%s setting fDeviceInit flag failed with error %d\n",
4346 __func__, err);
4347 goto out;
4348 }
4349
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004350 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4351 for (i = 0; i < 1000 && !err && flag_res; i++)
4352 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4353 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
4354
Dolev Raviv68078d52013-07-30 00:35:58 +05304355 if (err)
4356 dev_err(hba->dev,
4357 "%s reading fDeviceInit flag failed with error %d\n",
4358 __func__, err);
4359 else if (flag_res)
4360 dev_err(hba->dev,
4361 "%s fDeviceInit was not cleared by the device\n",
4362 __func__);
4363
4364out:
4365 return err;
4366}
4367
4368/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304369 * ufshcd_make_hba_operational - Make UFS controller operational
4370 * @hba: per adapter instance
4371 *
4372 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004373 * 1. Enable required interrupts
4374 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004375 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004376 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304377 *
4378 * Returns 0 on success, non-zero value on failure
4379 */
4380static int ufshcd_make_hba_operational(struct ufs_hba *hba)
4381{
4382 int err = 0;
4383 u32 reg;
4384
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304385 /* Enable required interrupts */
4386 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4387
4388 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004389 if (ufshcd_is_intr_aggr_allowed(hba))
4390 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4391 else
4392 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304393
4394 /* Configure UTRL and UTMRL base address registers */
4395 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4396 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4397 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4398 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4399 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4400 REG_UTP_TASK_REQ_LIST_BASE_L);
4401 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4402 REG_UTP_TASK_REQ_LIST_BASE_H);
4403
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304404 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004405 * Make sure base address and interrupt setup are updated before
4406 * enabling the run/stop registers below.
4407 */
4408 wmb();
4409
4410 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304411 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304412 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004413 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304414 if (!(ufshcd_get_lists_status(reg))) {
4415 ufshcd_enable_run_stop_reg(hba);
4416 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304417 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304418 "Host controller not ready to process requests");
4419 err = -EIO;
4420 goto out;
4421 }
4422
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304423out:
4424 return err;
4425}
4426
4427/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004428 * ufshcd_hba_stop - Send controller to reset state
4429 * @hba: per adapter instance
4430 * @can_sleep: perform sleep or just spin
4431 */
4432static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
4433{
4434 int err;
4435
4436 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4437 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4438 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4439 10, 1, can_sleep);
4440 if (err)
4441 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4442}
4443
4444/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304445 * ufshcd_hba_enable - initialize the controller
4446 * @hba: per adapter instance
4447 *
4448 * The controller resets itself and controller firmware initialization
4449 * sequence kicks off. When controller is ready it will set
4450 * the Host Controller Enable bit to 1.
4451 *
4452 * Returns 0 on success, non-zero value on failure
4453 */
4454static int ufshcd_hba_enable(struct ufs_hba *hba)
4455{
4456 int retry;
4457
4458 /*
4459 * msleep of 1 and 5 used in this function might result in msleep(20),
4460 * but it was necessary to send the UFS FPGA to reset mode during
4461 * development and testing of this driver. msleep can be changed to
4462 * mdelay and retry count can be reduced based on the controller.
4463 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004464 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304465 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004466 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304467
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004468 /* UniPro link is disabled at this point */
4469 ufshcd_set_link_off(hba);
4470
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004471 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004472
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304473 /* start controller initialization sequence */
4474 ufshcd_hba_start(hba);
4475
4476 /*
4477 * To initialize a UFS host controller HCE bit must be set to 1.
4478 * During initialization the HCE bit value changes from 1->0->1.
4479 * When the host controller completes initialization sequence
4480 * it sets the value of HCE bit to 1. The same HCE bit is read back
4481 * to check if the controller has completed initialization sequence.
4482 * So without this delay the value HCE = 1, set in the previous
4483 * instruction might be read back.
4484 * This delay can be changed based on the controller.
4485 */
4486 msleep(1);
4487
4488 /* wait for the host controller to complete initialization */
4489 retry = 10;
4490 while (ufshcd_is_hba_active(hba)) {
4491 if (retry) {
4492 retry--;
4493 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304494 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304495 "Controller enable failed\n");
4496 return -EIO;
4497 }
4498 msleep(5);
4499 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004500
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004501 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004502 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004503
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004504 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004505
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304506 return 0;
4507}
4508
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004509static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4510{
4511 int tx_lanes, i, err = 0;
4512
4513 if (!peer)
4514 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4515 &tx_lanes);
4516 else
4517 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4518 &tx_lanes);
4519 for (i = 0; i < tx_lanes; i++) {
4520 if (!peer)
4521 err = ufshcd_dme_set(hba,
4522 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4523 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4524 0);
4525 else
4526 err = ufshcd_dme_peer_set(hba,
4527 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4528 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4529 0);
4530 if (err) {
4531 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4532 __func__, peer, i, err);
4533 break;
4534 }
4535 }
4536
4537 return err;
4538}
4539
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004540static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)
4541{
4542 return ufshcd_disable_tx_lcc(hba, false);
4543}
4544
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004545static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4546{
4547 return ufshcd_disable_tx_lcc(hba, true);
4548}
4549
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304550/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304551 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304552 * @hba: per adapter instance
4553 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304554 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304555 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304556static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304557{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304558 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004559 int retries = DME_LINKSTARTUP_RETRIES;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004560 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304561
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004562 /*
4563 * If UFS device isn't active then we will have to issue link startup
4564 * 2 times to make sure the device state move to active.
4565 */
4566 if (!ufshcd_is_ufs_dev_active(hba))
4567 link_startup_again = true;
4568
4569link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004570 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004571 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304572
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004573 ret = ufshcd_dme_link_startup(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004574 if (ret)
4575 ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004576
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004577 /* check if device is detected by inter-connect layer */
4578 if (!ret && !ufshcd_is_device_present(hba)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004579 ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004580 dev_err(hba->dev, "%s: Device not present\n", __func__);
4581 ret = -ENXIO;
4582 goto out;
4583 }
4584
4585 /*
4586 * DME link lost indication is only received when link is up,
4587 * but we can't be sure if the link is up until link startup
4588 * succeeds. So reset the local Uni-Pro and try again.
4589 */
4590 if (ret && ufshcd_hba_enable(hba))
4591 goto out;
4592 } while (ret && retries--);
4593
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304594 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004595 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304596 goto out;
4597
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004598 if (link_startup_again) {
4599 link_startup_again = false;
4600 retries = DME_LINKSTARTUP_RETRIES;
4601 goto link_startup;
4602 }
4603
4604 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4605 ufshcd_init_pwr_info(hba);
4606 ufshcd_print_pwr_info(hba);
4607
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004608 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4609 ret = ufshcd_disable_device_tx_lcc(hba);
4610 if (ret)
4611 goto out;
4612 }
4613
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08004614 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_BROKEN_LCC) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004615 ret = ufshcd_disable_host_tx_lcc(hba);
4616 if (ret)
4617 goto out;
4618 }
4619
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004620 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004621 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4622 if (ret)
4623 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004624
4625 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304626out:
4627 if (ret)
4628 dev_err(hba->dev, "link startup failed %d\n", ret);
4629 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304630}
4631
4632/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304633 * ufshcd_verify_dev_init() - Verify device initialization
4634 * @hba: per-adapter instance
4635 *
4636 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4637 * device Transport Protocol (UTP) layer is ready after a reset.
4638 * If the UTP layer at the device side is not initialized, it may
4639 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4640 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4641 */
4642static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4643{
4644 int err = 0;
4645 int retries;
4646
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004647 ufshcd_hold_all(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304648 mutex_lock(&hba->dev_cmd.lock);
4649 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4650 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4651 NOP_OUT_TIMEOUT);
4652
4653 if (!err || err == -ETIMEDOUT)
4654 break;
4655
4656 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4657 }
4658 mutex_unlock(&hba->dev_cmd.lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004659 ufshcd_release_all(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304660
4661 if (err)
4662 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4663 return err;
4664}
4665
4666/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004667 * ufshcd_set_queue_depth - set lun queue depth
4668 * @sdev: pointer to SCSI device
4669 *
4670 * Read bLUQueueDepth value and activate scsi tagged command
4671 * queueing. For WLUN, queue depth is set to 1. For best-effort
4672 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4673 * value that host can queue.
4674 */
4675static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4676{
4677 int ret = 0;
4678 u8 lun_qdepth;
4679 struct ufs_hba *hba;
4680
4681 hba = shost_priv(sdev->host);
4682
4683 lun_qdepth = hba->nutrs;
4684 ret = ufshcd_read_unit_desc_param(hba,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004685 ufshcd_scsi_to_upiu_lun(sdev->lun),
4686 UNIT_DESC_PARAM_LU_Q_DEPTH,
4687 &lun_qdepth,
4688 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004689
4690 /* Some WLUN doesn't support unit descriptor */
4691 if (ret == -EOPNOTSUPP)
4692 lun_qdepth = 1;
4693 else if (!lun_qdepth)
4694 /* eventually, we can figure out the real queue depth */
4695 lun_qdepth = hba->nutrs;
4696 else
4697 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4698
4699 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4700 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004701 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004702}
4703
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004704/*
4705 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4706 * @hba: per-adapter instance
4707 * @lun: UFS device lun id
4708 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4709 *
4710 * Returns 0 in case of success and b_lu_write_protect status would be returned
4711 * @b_lu_write_protect parameter.
4712 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4713 * Returns -EINVAL in case of invalid parameters passed to this function.
4714 */
4715static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4716 u8 lun,
4717 u8 *b_lu_write_protect)
4718{
4719 int ret;
4720
4721 if (!b_lu_write_protect)
4722 ret = -EINVAL;
4723 /*
4724 * According to UFS device spec, RPMB LU can't be write
4725 * protected so skip reading bLUWriteProtect parameter for
4726 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4727 */
4728 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4729 ret = -ENOTSUPP;
4730 else
4731 ret = ufshcd_read_unit_desc_param(hba,
4732 lun,
4733 UNIT_DESC_PARAM_LU_WR_PROTECT,
4734 b_lu_write_protect,
4735 sizeof(*b_lu_write_protect));
4736 return ret;
4737}
4738
4739/**
4740 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4741 * status
4742 * @hba: per-adapter instance
4743 * @sdev: pointer to SCSI device
4744 *
4745 */
4746static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4747 struct scsi_device *sdev)
4748{
4749 if (hba->dev_info.f_power_on_wp_en &&
4750 !hba->dev_info.is_lu_power_on_wp) {
4751 u8 b_lu_write_protect;
4752
4753 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4754 &b_lu_write_protect) &&
4755 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4756 hba->dev_info.is_lu_power_on_wp = true;
4757 }
4758}
4759
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004760/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304761 * ufshcd_slave_alloc - handle initial SCSI device configurations
4762 * @sdev: pointer to SCSI device
4763 *
4764 * Returns success
4765 */
4766static int ufshcd_slave_alloc(struct scsi_device *sdev)
4767{
4768 struct ufs_hba *hba;
4769
4770 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304771
4772 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4773 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304774
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304775 /* allow SCSI layer to restart the device in case of errors */
4776 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004777
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004778 /* REPORT SUPPORTED OPERATION CODES is not supported */
4779 sdev->no_report_opcodes = 1;
4780
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004781 /* WRITE_SAME command is not supported*/
4782 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004783
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004784 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004785
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004786 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4787
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004788 return 0;
4789}
4790
4791/**
4792 * ufshcd_change_queue_depth - change queue depth
4793 * @sdev: pointer to SCSI device
4794 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004795 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004796 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004797 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004798static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004799{
4800 struct ufs_hba *hba = shost_priv(sdev->host);
4801
4802 if (depth > hba->nutrs)
4803 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004804 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304805}
4806
4807/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004808 * ufshcd_slave_configure - adjust SCSI device configurations
4809 * @sdev: pointer to SCSI device
4810 */
4811static int ufshcd_slave_configure(struct scsi_device *sdev)
4812{
4813 struct request_queue *q = sdev->request_queue;
Subhash Jadavani5ea586f2016-08-17 19:08:09 -07004814 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004815
4816 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4817 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
4818
Subhash Jadavani5ea586f2016-08-17 19:08:09 -07004819 if (hba->scsi_cmd_timeout) {
4820 blk_queue_rq_timeout(q, hba->scsi_cmd_timeout * HZ);
4821 scsi_set_cmd_timeout_override(sdev, hba->scsi_cmd_timeout * HZ);
4822 }
4823
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004824 sdev->autosuspend_delay = UFSHCD_AUTO_SUSPEND_DELAY_MS;
4825 sdev->use_rpm_auto = 1;
4826
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004827 return 0;
4828}
4829
4830/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304831 * ufshcd_slave_destroy - remove SCSI device configurations
4832 * @sdev: pointer to SCSI device
4833 */
4834static void ufshcd_slave_destroy(struct scsi_device *sdev)
4835{
4836 struct ufs_hba *hba;
4837
4838 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004839 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004840 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4841 unsigned long flags;
4842
4843 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004844 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004845 spin_unlock_irqrestore(hba->host->host_lock, flags);
4846 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304847}
4848
4849/**
4850 * ufshcd_task_req_compl - handle task management request completion
4851 * @hba: per adapter instance
4852 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304853 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304854 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304855 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304856 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304857static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304858{
4859 struct utp_task_req_desc *task_req_descp;
4860 struct utp_upiu_task_rsp *task_rsp_upiup;
4861 unsigned long flags;
4862 int ocs_value;
4863 int task_result;
4864
4865 spin_lock_irqsave(hba->host->host_lock, flags);
4866
4867 /* Clear completed tasks from outstanding_tasks */
4868 __clear_bit(index, &hba->outstanding_tasks);
4869
4870 task_req_descp = hba->utmrdl_base_addr;
4871 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
4872
4873 if (ocs_value == OCS_SUCCESS) {
4874 task_rsp_upiup = (struct utp_upiu_task_rsp *)
4875 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09004876 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
4877 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304878 if (resp)
4879 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304880 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304881 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
4882 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304883 }
4884 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304885
4886 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304887}
4888
4889/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304890 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4891 * @lrb: pointer to local reference block of completed command
4892 * @scsi_status: SCSI command status
4893 *
4894 * Returns value base on SCSI command status
4895 */
4896static inline int
4897ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4898{
4899 int result = 0;
4900
4901 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304902 case SAM_STAT_CHECK_CONDITION:
4903 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304904 case SAM_STAT_GOOD:
4905 result |= DID_OK << 16 |
4906 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304907 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304908 break;
4909 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304910 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304911 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304912 ufshcd_copy_sense_data(lrbp);
4913 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304914 break;
4915 default:
4916 result |= DID_ERROR << 16;
4917 break;
4918 } /* end of switch */
4919
4920 return result;
4921}
4922
4923/**
4924 * ufshcd_transfer_rsp_status - Get overall status of the response
4925 * @hba: per adapter instance
4926 * @lrb: pointer to local reference block of completed command
4927 *
4928 * Returns result of the command to notify SCSI midlayer
4929 */
4930static inline int
4931ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4932{
4933 int result = 0;
4934 int scsi_status;
4935 int ocs;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004936 bool print_prdt;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304937
4938 /* overall command status of utrd */
4939 ocs = ufshcd_get_tr_ocs(lrbp);
4940
4941 switch (ocs) {
4942 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304943 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004944 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304945 switch (result) {
4946 case UPIU_TRANSACTION_RESPONSE:
4947 /*
4948 * get the response UPIU result to extract
4949 * the SCSI command status
4950 */
4951 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4952
4953 /*
4954 * get the result based on SCSI status response
4955 * to notify the SCSI midlayer of the command status
4956 */
4957 scsi_status = result & MASK_SCSI_STATUS;
4958 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304959
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004960 /*
4961 * Currently we are only supporting BKOPs exception
4962 * events hence we can ignore BKOPs exception event
4963 * during power management callbacks. BKOPs exception
4964 * event is not expected to be raised in runtime suspend
4965 * callback as it allows the urgent bkops.
4966 * During system suspend, we are anyway forcefully
4967 * disabling the bkops and if urgent bkops is needed
4968 * it will be enabled on system resume. Long term
4969 * solution could be to abort the system suspend if
4970 * UFS device needs urgent BKOPs.
4971 */
4972 if (!hba->pm_op_in_progress &&
4973 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304974 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304975 break;
4976 case UPIU_TRANSACTION_REJECT_UPIU:
4977 /* TODO: handle Reject UPIU Response */
4978 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304979 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304980 "Reject UPIU not fully implemented\n");
4981 break;
4982 default:
4983 result = DID_ERROR << 16;
4984 dev_err(hba->dev,
4985 "Unexpected request response code = %x\n",
4986 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304987 break;
4988 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304989 break;
4990 case OCS_ABORTED:
4991 result |= DID_ABORT << 16;
4992 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304993 case OCS_INVALID_COMMAND_STATUS:
4994 result |= DID_REQUEUE << 16;
4995 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304996 case OCS_INVALID_CMD_TABLE_ATTR:
4997 case OCS_INVALID_PRDT_ATTR:
4998 case OCS_MISMATCH_DATA_BUF_SIZE:
4999 case OCS_MISMATCH_RESP_UPIU_SIZE:
5000 case OCS_PEER_COMM_FAILURE:
5001 case OCS_FATAL_ERROR:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005002 case OCS_DEVICE_FATAL_ERROR:
5003 case OCS_INVALID_CRYPTO_CONFIG:
5004 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305005 default:
5006 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305007 dev_err(hba->dev,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005008 "OCS error from controller = %x for tag %d\n",
5009 ocs, lrbp->task_tag);
5010 ufshcd_print_host_regs(hba);
5011 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305012 break;
5013 } /* end of switch */
5014
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005015 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) {
5016 print_prdt = (ocs == OCS_INVALID_PRDT_ATTR ||
5017 ocs == OCS_MISMATCH_DATA_BUF_SIZE);
5018 ufshcd_print_trs(hba, 1 << lrbp->task_tag, print_prdt);
5019 }
5020
5021 if ((host_byte(result) == DID_ERROR) ||
5022 (host_byte(result) == DID_ABORT))
5023 ufsdbg_set_err_state(hba);
5024
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305025 return result;
5026}
5027
5028/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305029 * ufshcd_uic_cmd_compl - handle completion of uic command
5030 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305031 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305032 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305033static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305034{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305035 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305036 hba->active_uic_cmd->argument2 |=
5037 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05305038 hba->active_uic_cmd->argument3 =
5039 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305040 complete(&hba->active_uic_cmd->done);
5041 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305042
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005043 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
5044 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305045}
5046
5047/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005048 * ufshcd_abort_outstanding_requests - abort all outstanding transfer requests.
5049 * @hba: per adapter instance
5050 * @result: error result to inform scsi layer about
5051 */
5052void ufshcd_abort_outstanding_transfer_requests(struct ufs_hba *hba, int result)
5053{
5054 u8 index;
5055 struct ufshcd_lrb *lrbp;
5056 struct scsi_cmnd *cmd;
5057
5058 if (!hba->outstanding_reqs)
5059 return;
5060
5061 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5062 lrbp = &hba->lrb[index];
5063 cmd = lrbp->cmd;
5064 if (cmd) {
5065 ufshcd_cond_add_cmd_trace(hba, index, "failed");
5066 ufshcd_update_error_stats(hba,
5067 UFS_ERR_INT_FATAL_ERRORS);
5068 scsi_dma_unmap(cmd);
5069 cmd->result = result;
5070 /* Clear pending transfer requests */
5071 ufshcd_clear_cmd(hba, index);
5072 ufshcd_outstanding_req_clear(hba, index);
5073 clear_bit_unlock(index, &hba->lrb_in_use);
5074 lrbp->complete_time_stamp = ktime_get();
5075 update_req_stats(hba, lrbp);
5076 /* Mark completed command as NULL in LRB */
5077 lrbp->cmd = NULL;
5078 ufshcd_release_all(hba);
5079 if (cmd->request) {
5080 /*
5081 * As we are accessing the "request" structure,
5082 * this must be called before calling
5083 * ->scsi_done() callback.
5084 */
5085 ufshcd_vops_pm_qos_req_end(hba, cmd->request,
5086 true);
5087 ufshcd_vops_crypto_engine_cfg_end(hba,
5088 lrbp, cmd->request);
5089 }
5090 /* Do not touch lrbp after scsi done */
5091 cmd->scsi_done(cmd);
5092 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
5093 if (hba->dev_cmd.complete) {
5094 ufshcd_cond_add_cmd_trace(hba, index,
5095 "dev_failed");
5096 ufshcd_outstanding_req_clear(hba, index);
5097 complete(hba->dev_cmd.complete);
5098 }
5099 }
5100 if (ufshcd_is_clkscaling_supported(hba))
5101 hba->clk_scaling.active_reqs--;
5102 }
5103}
5104
5105/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005106 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305107 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005108 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305109 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005110static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
5111 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305112{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305113 struct ufshcd_lrb *lrbp;
5114 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305115 int result;
5116 int index;
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07005117 struct request *req;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005118
Dolev Ravive9d501b2014-07-01 12:22:37 +03005119 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
5120 lrbp = &hba->lrb[index];
5121 cmd = lrbp->cmd;
5122 if (cmd) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005123 ufshcd_cond_add_cmd_trace(hba, index, "complete");
5124 ufshcd_update_tag_stats_completion(hba, cmd);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005125 result = ufshcd_transfer_rsp_status(hba, lrbp);
5126 scsi_dma_unmap(cmd);
5127 cmd->result = result;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005128 clear_bit_unlock(index, &hba->lrb_in_use);
5129 lrbp->complete_time_stamp = ktime_get();
5130 update_req_stats(hba, lrbp);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005131 /* Mark completed command as NULL in LRB */
5132 lrbp->cmd = NULL;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005133 __ufshcd_release(hba, false);
5134 __ufshcd_hibern8_release(hba, false);
5135 if (cmd->request) {
5136 /*
5137 * As we are accessing the "request" structure,
5138 * this must be called before calling
5139 * ->scsi_done() callback.
5140 */
5141 ufshcd_vops_pm_qos_req_end(hba, cmd->request,
5142 false);
5143 ufshcd_vops_crypto_engine_cfg_end(hba,
5144 lrbp, cmd->request);
5145 }
5146
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07005147 req = cmd->request;
5148 if (req) {
5149 /* Update IO svc time latency histogram */
5150 if (req->lat_hist_enabled) {
5151 ktime_t completion;
5152 u_int64_t delta_us;
5153
5154 completion = ktime_get();
5155 delta_us = ktime_us_delta(completion,
5156 req->lat_hist_io_start);
5157 /* rq_data_dir() => true if WRITE */
5158 blk_update_latency_hist(&hba->io_lat_s,
5159 (rq_data_dir(req) == READ),
5160 delta_us);
5161 }
5162 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005163 /* Do not touch lrbp after scsi done */
5164 cmd->scsi_done(cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005165 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
5166 if (hba->dev_cmd.complete) {
5167 ufshcd_cond_add_cmd_trace(hba, index,
5168 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03005169 complete(hba->dev_cmd.complete);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005170 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005171 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005172 if (ufshcd_is_clkscaling_supported(hba))
5173 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005174 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305175
5176 /* clear corresponding bits of completed commands */
5177 hba->outstanding_reqs ^= completed_reqs;
5178
Sahitya Tummala856b3482014-09-25 15:32:34 +03005179 ufshcd_clk_scaling_update_busy(hba);
5180
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305181 /* we might have free'd some tags above */
5182 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305183}
5184
5185/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005186 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5187 * @hba: per adapter instance
5188 */
5189static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
5190{
5191 unsigned long completed_reqs;
5192 u32 tr_doorbell;
5193
5194 /* Resetting interrupt aggregation counters first and reading the
5195 * DOOR_BELL afterward allows us to handle all the completed requests.
5196 * In order to prevent other interrupts starvation the DB is read once
5197 * after reset. The down side of this solution is the possibility of
5198 * false interrupt if device completes another request after resetting
5199 * aggregation and before reading the DB.
5200 */
5201 if (ufshcd_is_intr_aggr_allowed(hba))
5202 ufshcd_reset_intr_aggr(hba);
5203
5204 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5205 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
5206
5207 __ufshcd_transfer_req_compl(hba, completed_reqs);
5208}
5209
5210/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305211 * ufshcd_disable_ee - disable exception event
5212 * @hba: per-adapter instance
5213 * @mask: exception event to disable
5214 *
5215 * Disables exception event in the device so that the EVENT_ALERT
5216 * bit is not set.
5217 *
5218 * Returns zero on success, non-zero error value on failure.
5219 */
5220static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
5221{
5222 int err = 0;
5223 u32 val;
5224
5225 if (!(hba->ee_ctrl_mask & mask))
5226 goto out;
5227
5228 val = hba->ee_ctrl_mask & ~mask;
5229 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005230 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305231 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5232 if (!err)
5233 hba->ee_ctrl_mask &= ~mask;
5234out:
5235 return err;
5236}
5237
5238/**
5239 * ufshcd_enable_ee - enable exception event
5240 * @hba: per-adapter instance
5241 * @mask: exception event to enable
5242 *
5243 * Enable corresponding exception event in the device to allow
5244 * device to alert host in critical scenarios.
5245 *
5246 * Returns zero on success, non-zero error value on failure.
5247 */
5248static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5249{
5250 int err = 0;
5251 u32 val;
5252
5253 if (hba->ee_ctrl_mask & mask)
5254 goto out;
5255
5256 val = hba->ee_ctrl_mask | mask;
5257 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005258 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305259 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5260 if (!err)
5261 hba->ee_ctrl_mask |= mask;
5262out:
5263 return err;
5264}
5265
5266/**
5267 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5268 * @hba: per-adapter instance
5269 *
5270 * Allow device to manage background operations on its own. Enabling
5271 * this might lead to inconsistent latencies during normal data transfers
5272 * as the device is allowed to manage its own way of handling background
5273 * operations.
5274 *
5275 * Returns zero on success, non-zero on failure.
5276 */
5277static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5278{
5279 int err = 0;
5280
5281 if (hba->auto_bkops_enabled)
5282 goto out;
5283
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005284 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305285 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5286 if (err) {
5287 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5288 __func__, err);
5289 goto out;
5290 }
5291
5292 hba->auto_bkops_enabled = true;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005293 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 1);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305294
5295 /* No need of URGENT_BKOPS exception from the device */
5296 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5297 if (err)
5298 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5299 __func__, err);
5300out:
5301 return err;
5302}
5303
5304/**
5305 * ufshcd_disable_auto_bkops - block device in doing background operations
5306 * @hba: per-adapter instance
5307 *
5308 * Disabling background operations improves command response latency but
5309 * has drawback of device moving into critical state where the device is
5310 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5311 * host is idle so that BKOPS are managed effectively without any negative
5312 * impacts.
5313 *
5314 * Returns zero on success, non-zero on failure.
5315 */
5316static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5317{
5318 int err = 0;
5319
5320 if (!hba->auto_bkops_enabled)
5321 goto out;
5322
5323 /*
5324 * If host assisted BKOPs is to be enabled, make sure
5325 * urgent bkops exception is allowed.
5326 */
5327 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5328 if (err) {
5329 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5330 __func__, err);
5331 goto out;
5332 }
5333
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005334 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305335 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5336 if (err) {
5337 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5338 __func__, err);
5339 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5340 goto out;
5341 }
5342
5343 hba->auto_bkops_enabled = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005344 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 0);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305345out:
5346 return err;
5347}
5348
5349/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005350 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305351 * @hba: per adapter instance
5352 *
5353 * After a device reset the device may toggle the BKOPS_EN flag
5354 * to default value. The s/w tracking variables should be updated
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005355 * as well. This function would change the auto-bkops state based on
5356 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305357 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005358static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305359{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005360 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5361 hba->auto_bkops_enabled = false;
5362 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5363 ufshcd_enable_auto_bkops(hba);
5364 } else {
5365 hba->auto_bkops_enabled = true;
5366 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5367 ufshcd_disable_auto_bkops(hba);
5368 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305369}
5370
5371static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5372{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005373 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305374 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5375}
5376
5377/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005378 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5379 * @hba: per-adapter instance
5380 * @status: bkops_status value
5381 *
5382 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5383 * flag in the device to permit background operations if the device
5384 * bkops_status is greater than or equal to "status" argument passed to
5385 * this function, disable otherwise.
5386 *
5387 * Returns 0 for success, non-zero in case of failure.
5388 *
5389 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5390 * to know whether auto bkops is enabled or disabled after this function
5391 * returns control to it.
5392 */
5393static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5394 enum bkops_status status)
5395{
5396 int err;
5397 u32 curr_status = 0;
5398
5399 err = ufshcd_get_bkops_status(hba, &curr_status);
5400 if (err) {
5401 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5402 __func__, err);
5403 goto out;
5404 } else if (curr_status > BKOPS_STATUS_MAX) {
5405 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5406 __func__, curr_status);
5407 err = -EINVAL;
5408 goto out;
5409 }
5410
5411 if (curr_status >= status)
5412 err = ufshcd_enable_auto_bkops(hba);
5413 else
5414 err = ufshcd_disable_auto_bkops(hba);
5415out:
5416 return err;
5417}
5418
5419/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305420 * ufshcd_urgent_bkops - handle urgent bkops exception event
5421 * @hba: per-adapter instance
5422 *
5423 * Enable fBackgroundOpsEn flag in the device to permit background
5424 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005425 *
5426 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5427 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305428 */
5429static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5430{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005431 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305432}
5433
5434static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5435{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005436 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305437 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5438}
5439
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005440static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5441{
5442 int err;
5443 u32 curr_status = 0;
5444
5445 if (hba->is_urgent_bkops_lvl_checked)
5446 goto enable_auto_bkops;
5447
5448 err = ufshcd_get_bkops_status(hba, &curr_status);
5449 if (err) {
5450 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5451 __func__, err);
5452 goto out;
5453 }
5454
5455 /*
5456 * We are seeing that some devices are raising the urgent bkops
5457 * exception events even when BKOPS status doesn't indicate performace
5458 * impacted or critical. Handle these device by determining their urgent
5459 * bkops status at runtime.
5460 */
5461 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5462 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5463 __func__, curr_status);
5464 /* update the current status as the urgent bkops level */
5465 hba->urgent_bkops_lvl = curr_status;
5466 hba->is_urgent_bkops_lvl_checked = true;
5467 }
5468
5469enable_auto_bkops:
5470 err = ufshcd_enable_auto_bkops(hba);
5471out:
5472 if (err < 0)
5473 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5474 __func__, err);
5475}
5476
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305477/**
5478 * ufshcd_exception_event_handler - handle exceptions raised by device
5479 * @work: pointer to work data
5480 *
5481 * Read bExceptionEventStatus attribute from the device and handle the
5482 * exception event accordingly.
5483 */
5484static void ufshcd_exception_event_handler(struct work_struct *work)
5485{
5486 struct ufs_hba *hba;
5487 int err;
5488 u32 status = 0;
5489 hba = container_of(work, struct ufs_hba, eeh_work);
5490
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305491 pm_runtime_get_sync(hba->dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005492 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305493 err = ufshcd_get_ee_status(hba, &status);
5494 if (err) {
5495 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5496 __func__, err);
5497 goto out;
5498 }
5499
5500 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005501
5502 if (status & MASK_EE_URGENT_BKOPS)
5503 ufshcd_bkops_exception_event_handler(hba);
5504
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305505out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005506 ufshcd_scsi_unblock_requests(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305507 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305508 return;
5509}
5510
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005511/* Complete requests that have door-bell cleared */
5512static void ufshcd_complete_requests(struct ufs_hba *hba)
5513{
5514 ufshcd_transfer_req_compl(hba);
5515 ufshcd_tmc_handler(hba);
5516}
5517
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305518/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005519 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5520 * to recover from the DL NAC errors or not.
5521 * @hba: per-adapter instance
5522 *
5523 * Returns true if error handling is required, false otherwise
5524 */
5525static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5526{
5527 unsigned long flags;
5528 bool err_handling = true;
5529
5530 spin_lock_irqsave(hba->host->host_lock, flags);
5531 /*
5532 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5533 * device fatal error and/or DL NAC & REPLAY timeout errors.
5534 */
5535 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5536 goto out;
5537
5538 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5539 ((hba->saved_err & UIC_ERROR) &&
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005540 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) {
5541 /*
5542 * we have to do error recovery but atleast silence the error
5543 * logs.
5544 */
5545 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005546 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005547 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005548
5549 if ((hba->saved_err & UIC_ERROR) &&
5550 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5551 int err;
5552 /*
5553 * wait for 50ms to see if we can get any other errors or not.
5554 */
5555 spin_unlock_irqrestore(hba->host->host_lock, flags);
5556 msleep(50);
5557 spin_lock_irqsave(hba->host->host_lock, flags);
5558
5559 /*
5560 * now check if we have got any other severe errors other than
5561 * DL NAC error?
5562 */
5563 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5564 ((hba->saved_err & UIC_ERROR) &&
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005565 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) {
5566 if (((hba->saved_err & INT_FATAL_ERRORS) ==
5567 DEVICE_FATAL_ERROR) || (hba->saved_uic_err &
5568 ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))
5569 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005570 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005571 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005572
5573 /*
5574 * As DL NAC is the only error received so far, send out NOP
5575 * command to confirm if link is still active or not.
5576 * - If we don't get any response then do error recovery.
5577 * - If we get response then clear the DL NAC error bit.
5578 */
5579
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005580 /* silence the error logs from NOP command */
5581 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005582 spin_unlock_irqrestore(hba->host->host_lock, flags);
5583 err = ufshcd_verify_dev_init(hba);
5584 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005585 hba->silence_err_logs = false;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005586
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005587 if (err) {
5588 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005589 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005590 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005591
5592 /* Link seems to be alive hence ignore the DL NAC errors */
5593 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5594 hba->saved_err &= ~UIC_ERROR;
5595 /* clear NAC error */
5596 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5597 if (!hba->saved_uic_err) {
5598 err_handling = false;
5599 goto out;
5600 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005601 /*
5602 * there seems to be some errors other than NAC, so do error
5603 * recovery
5604 */
5605 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005606 }
5607out:
5608 spin_unlock_irqrestore(hba->host->host_lock, flags);
5609 return err_handling;
5610}
5611
5612/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305613 * ufshcd_err_handler - handle UFS errors that require s/w attention
5614 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305615 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305616static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305617{
5618 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305619 unsigned long flags;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005620 bool err_xfer = false, err_tm = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305621 int err = 0;
5622 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005623 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305624
5625 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305626
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005627 ufsdbg_set_err_state(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305628 pm_runtime_get_sync(hba->dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005629 ufshcd_hold_all(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305630
5631 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005632 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305633 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305634
5635 hba->ufshcd_state = UFSHCD_STATE_RESET;
5636 ufshcd_set_eh_in_progress(hba);
5637
5638 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005639 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005640
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08005641 if (hba->dev_info.quirks &
5642 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
Yaniv Gardi583fa622016-03-10 17:37:13 +02005643 bool ret;
5644
5645 spin_unlock_irqrestore(hba->host->host_lock, flags);
5646 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5647 ret = ufshcd_quirk_dl_nac_errors(hba);
5648 spin_lock_irqsave(hba->host->host_lock, flags);
5649 if (!ret)
5650 goto skip_err_handling;
5651 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005652
5653 /*
5654 * Dump controller state before resetting. Transfer requests state
5655 * will be dump as part of the request completion.
5656 */
5657 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5658 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x",
5659 __func__, hba->saved_err, hba->saved_uic_err);
5660 if (!hba->silence_err_logs) {
5661 ufshcd_print_host_regs(hba);
5662 ufshcd_print_host_state(hba);
5663 ufshcd_print_pwr_info(hba);
5664 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5665 }
5666 }
5667
5668 if ((hba->saved_err & INT_FATAL_ERRORS) || hba->saved_ce_err ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005669 ((hba->saved_err & UIC_ERROR) &&
5670 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5671 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5672 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5673 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305674
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005675 /*
5676 * if host reset is required then skip clearing the pending
5677 * transfers forcefully because they will automatically get
5678 * cleared after link startup.
5679 */
5680 if (needs_reset)
5681 goto skip_pending_xfer_clear;
5682
5683 /* release lock as clear command might sleep */
5684 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305685 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005686 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5687 if (ufshcd_clear_cmd(hba, tag)) {
5688 err_xfer = true;
5689 goto lock_skip_pending_xfer_clear;
5690 }
5691 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305692
5693 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005694 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5695 if (ufshcd_clear_tm_cmd(hba, tag)) {
5696 err_tm = true;
5697 goto lock_skip_pending_xfer_clear;
5698 }
5699 }
5700
5701lock_skip_pending_xfer_clear:
5702 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305703
5704 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005705 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305706
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005707 if (err_xfer || err_tm)
5708 needs_reset = true;
5709
5710skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305711 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005712 if (needs_reset) {
5713 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5714
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005715 if (hba->saved_err & INT_FATAL_ERRORS)
5716 ufshcd_update_error_stats(hba,
5717 UFS_ERR_INT_FATAL_ERRORS);
5718 if (hba->saved_ce_err)
5719 ufshcd_update_error_stats(hba, UFS_ERR_CRYPTO_ENGINE);
5720
5721 if (hba->saved_err & UIC_ERROR)
5722 ufshcd_update_error_stats(hba,
5723 UFS_ERR_INT_UIC_ERROR);
5724
5725 if (err_xfer || err_tm)
5726 ufshcd_update_error_stats(hba,
5727 UFS_ERR_CLEAR_PEND_XFER_TM);
5728
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005729 /*
5730 * ufshcd_reset_and_restore() does the link reinitialization
5731 * which will need atleast one empty doorbell slot to send the
5732 * device management commands (NOP and query commands).
5733 * If there is no slot empty at this moment then free up last
5734 * slot forcefully.
5735 */
5736 if (hba->outstanding_reqs == max_doorbells)
5737 __ufshcd_transfer_req_compl(hba,
5738 (1UL << (hba->nutrs - 1)));
5739
5740 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305741 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005742 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305743 if (err) {
5744 dev_err(hba->dev, "%s: reset and restore failed\n",
5745 __func__);
5746 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5747 }
5748 /*
5749 * Inform scsi mid-layer that we did reset and allow to handle
5750 * Unit Attention properly.
5751 */
5752 scsi_report_bus_reset(hba->host, 0);
5753 hba->saved_err = 0;
5754 hba->saved_uic_err = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005755 hba->saved_ce_err = 0;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305756 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005757
Yaniv Gardi583fa622016-03-10 17:37:13 +02005758skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005759 if (!needs_reset) {
5760 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5761 if (hba->saved_err || hba->saved_uic_err)
5762 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5763 __func__, hba->saved_err, hba->saved_uic_err);
5764 }
5765
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005766 hba->silence_err_logs = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305767 ufshcd_clear_eh_in_progress(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305768out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005769 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005770 ufshcd_scsi_unblock_requests(hba);
5771 ufshcd_release_all(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305772 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305773}
5774
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005775static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
5776 u32 reg)
5777{
5778 reg_hist->reg[reg_hist->pos] = reg;
5779 reg_hist->tstamp[reg_hist->pos] = ktime_get();
5780 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
5781}
5782
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305783/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305784 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5785 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305786 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305787static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305788{
5789 u32 reg;
5790
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005791 /* PHY layer lane error */
5792 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5793 /* Ignore LINERESET indication, as this is not an error */
5794 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
5795 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
5796 /*
5797 * To know whether this error is fatal or not, DB timeout
5798 * must be checked but this error is handled separately.
5799 */
5800 dev_dbg(hba->dev, "%s: UIC Lane error reported, reg 0x%x\n",
5801 __func__, reg);
5802 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
5803 }
5804
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305805 /* PA_INIT_ERROR is fatal and needs UIC reset */
5806 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005807 if (reg)
5808 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
5809
5810 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305811 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08005812 } else if (hba->dev_info.quirks &
Yaniv Gardi583fa622016-03-10 17:37:13 +02005813 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5814 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5815 hba->uic_error |=
5816 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5817 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5818 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5819 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305820
5821 /* UIC NL/TL/DME errors needs software retry */
5822 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005823 if (reg) {
5824 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305825 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005826 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305827
5828 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005829 if (reg) {
5830 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305831 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005832 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305833
5834 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005835 if (reg) {
5836 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305837 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005838 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305839
5840 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5841 __func__, hba->uic_error);
5842}
5843
5844/**
5845 * ufshcd_check_errors - Check for errors that need s/w attention
5846 * @hba: per-adapter instance
5847 */
5848static void ufshcd_check_errors(struct ufs_hba *hba)
5849{
5850 bool queue_eh_work = false;
5851
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005852 if (hba->errors & INT_FATAL_ERRORS || hba->ce_error)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305853 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305854
5855 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305856 hba->uic_error = 0;
5857 ufshcd_update_uic_error(hba);
5858 if (hba->uic_error)
5859 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305860 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305861
5862 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005863 /*
5864 * update the transfer error masks to sticky bits, let's do this
5865 * irrespective of current ufshcd_state.
5866 */
5867 hba->saved_err |= hba->errors;
5868 hba->saved_uic_err |= hba->uic_error;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005869 hba->saved_ce_err |= hba->ce_error;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005870
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305871 /* handle fatal errors only when link is functional */
5872 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5873 /* block commands from scsi mid-layer */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005874 __ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305875
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305876 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5877 schedule_work(&hba->eh_work);
5878 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305879 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305880 /*
5881 * if (!queue_eh_work) -
5882 * Other errors are either non-fatal where host recovers
5883 * itself without s/w intervention or errors that will be
5884 * handled by the SCSI core layer.
5885 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305886}
5887
5888/**
5889 * ufshcd_tmc_handler - handle task management function completion
5890 * @hba: per adapter instance
5891 */
5892static void ufshcd_tmc_handler(struct ufs_hba *hba)
5893{
5894 u32 tm_doorbell;
5895
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305896 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305897 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305898 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305899}
5900
5901/**
5902 * ufshcd_sl_intr - Interrupt service routine
5903 * @hba: per adapter instance
5904 * @intr_status: contains interrupts generated by the controller
5905 */
5906static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
5907{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005908 ufsdbg_error_inject_dispatcher(hba,
5909 ERR_INJECT_INTR, intr_status, &intr_status);
5910
5911 ufshcd_vops_crypto_engine_get_status(hba, &hba->ce_error);
5912
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305913 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005914 if (hba->errors || hba->ce_error)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305915 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305916
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305917 if (intr_status & UFSHCD_UIC_MASK)
5918 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305919
5920 if (intr_status & UTP_TASK_REQ_COMPL)
5921 ufshcd_tmc_handler(hba);
5922
5923 if (intr_status & UTP_TRANSFER_REQ_COMPL)
5924 ufshcd_transfer_req_compl(hba);
5925}
5926
5927/**
5928 * ufshcd_intr - Main interrupt service routine
5929 * @irq: irq number
5930 * @__hba: pointer to adapter instance
5931 *
5932 * Returns IRQ_HANDLED - If interrupt is valid
5933 * IRQ_NONE - If invalid interrupt
5934 */
5935static irqreturn_t ufshcd_intr(int irq, void *__hba)
5936{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005937 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305938 irqreturn_t retval = IRQ_NONE;
5939 struct ufs_hba *hba = __hba;
5940
5941 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305942 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005943 enabled_intr_status =
5944 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305945
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005946 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05305947 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005948
5949 if (enabled_intr_status) {
5950 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305951 retval = IRQ_HANDLED;
5952 }
5953 spin_unlock(hba->host->host_lock);
5954 return retval;
5955}
5956
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305957static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5958{
5959 int err = 0;
5960 u32 mask = 1 << tag;
5961 unsigned long flags;
5962
5963 if (!test_bit(tag, &hba->outstanding_tasks))
5964 goto out;
5965
5966 spin_lock_irqsave(hba->host->host_lock, flags);
5967 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
5968 spin_unlock_irqrestore(hba->host->host_lock, flags);
5969
5970 /* poll for max. 1 sec to clear door bell register by h/w */
5971 err = ufshcd_wait_for_register(hba,
5972 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005973 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305974out:
5975 return err;
5976}
5977
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305978/**
5979 * ufshcd_issue_tm_cmd - issues task management commands to controller
5980 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305981 * @lun_id: LUN ID to which TM command is sent
5982 * @task_id: task ID to which the TM command is applicable
5983 * @tm_function: task management function opcode
5984 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305985 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305986 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305987 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305988static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5989 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305990{
5991 struct utp_task_req_desc *task_req_descp;
5992 struct utp_upiu_task_req *task_req_upiup;
5993 struct Scsi_Host *host;
5994 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305995 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305996 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305997 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305998
5999 host = hba->host;
6000
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306001 /*
6002 * Get free slot, sleep if slots are unavailable.
6003 * Even though we use wait_event() which sleeps indefinitely,
6004 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
6005 */
6006 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006007 ufshcd_hold_all(hba);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306008
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306009 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306010 task_req_descp = hba->utmrdl_base_addr;
6011 task_req_descp += free_slot;
6012
6013 /* Configure task request descriptor */
6014 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6015 task_req_descp->header.dword_2 =
6016 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6017
6018 /* Configure task request UPIU */
6019 task_req_upiup =
6020 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306021 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306022 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306023 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306024 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306025 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306026 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006027 /*
6028 * The host shall provide the same value for LUN field in the basic
6029 * header and for Input Parameter.
6030 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306031 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
6032 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306033
6034 /* send command to the controller */
6035 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02006036
6037 /* Make sure descriptors are ready before ringing the task doorbell */
6038 wmb();
6039
Seungwon Jeonb873a2752013-06-26 22:39:26 +05306040 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006041 /* Make sure that doorbell is committed immediately */
6042 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306043
6044 spin_unlock_irqrestore(host->host_lock, flags);
6045
6046 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306047 err = wait_event_timeout(hba->tm_wq,
6048 test_bit(free_slot, &hba->tm_condition),
6049 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306050 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306051 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6052 __func__, tm_function);
6053 if (ufshcd_clear_tm_cmd(hba, free_slot))
6054 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6055 __func__, free_slot);
6056 err = -ETIMEDOUT;
6057 } else {
6058 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306059 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306060
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306061 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306062 ufshcd_put_tm_slot(hba, free_slot);
6063 wake_up(&hba->tm_tag_wq);
6064
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006065 ufshcd_release_all(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306066 return err;
6067}
6068
6069/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306070 * ufshcd_eh_device_reset_handler - device reset handler registered to
6071 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306072 * @cmd: SCSI command pointer
6073 *
6074 * Returns SUCCESS/FAILED
6075 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306076static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306077{
6078 struct Scsi_Host *host;
6079 struct ufs_hba *hba;
6080 unsigned int tag;
6081 u32 pos;
6082 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306083 u8 resp = 0xF;
6084 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306085 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306086
6087 host = cmd->device->host;
6088 hba = shost_priv(host);
6089 tag = cmd->request->tag;
6090
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306091 lrbp = &hba->lrb[tag];
6092 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6093 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306094 if (!err)
6095 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306096 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306097 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306098
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306099 /* clear the commands that were pending for corresponding LUN */
6100 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6101 if (hba->lrb[pos].lun == lrbp->lun) {
6102 err = ufshcd_clear_cmd(hba, pos);
6103 if (err)
6104 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306105 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306106 }
6107 spin_lock_irqsave(host->host_lock, flags);
6108 ufshcd_transfer_req_compl(hba);
6109 spin_unlock_irqrestore(host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006110
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306111out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006112 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306113 if (!err) {
6114 err = SUCCESS;
6115 } else {
6116 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6117 err = FAILED;
6118 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306119 return err;
6120}
6121
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006122static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6123{
6124 struct ufshcd_lrb *lrbp;
6125 int tag;
6126
6127 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6128 lrbp = &hba->lrb[tag];
6129 lrbp->req_abort_skip = true;
6130 }
6131}
6132
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306133/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306134 * ufshcd_abort - abort a specific command
6135 * @cmd: SCSI command pointer
6136 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306137 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6138 * command, and in host controller by clearing the door-bell register. There can
6139 * be race between controller sending the command to the device while abort is
6140 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6141 * really issued and then try to abort it.
6142 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306143 * Returns SUCCESS/FAILED
6144 */
6145static int ufshcd_abort(struct scsi_cmnd *cmd)
6146{
6147 struct Scsi_Host *host;
6148 struct ufs_hba *hba;
6149 unsigned long flags;
6150 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306151 int err = 0;
6152 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306153 u8 resp = 0xF;
6154 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006155 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306156
6157 host = cmd->device->host;
6158 hba = shost_priv(host);
6159 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02006160 if (!ufshcd_valid_tag(hba, tag)) {
6161 dev_err(hba->dev,
6162 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6163 __func__, tag, cmd, cmd->request);
6164 BUG();
6165 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306166
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006167 lrbp = &hba->lrb[tag];
6168
6169 ufshcd_update_error_stats(hba, UFS_ERR_TASK_ABORT);
6170
6171 /*
6172 * Task abort to the device W-LUN is illegal. When this command
6173 * will fail, due to spec violation, scsi err handling next step
6174 * will be to send LU reset which, again, is a spec violation.
6175 * To avoid these unnecessary/illegal step we skip to the last error
6176 * handling stage: reset and restore.
6177 */
6178 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6179 return ufshcd_eh_host_reset_handler(cmd);
6180
6181 ufshcd_hold_all(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006182 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006183 /* If command is already aborted/completed, return SUCCESS */
6184 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6185 dev_err(hba->dev,
6186 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6187 __func__, tag, hba->outstanding_reqs, reg);
6188 goto out;
6189 }
6190
Dolev Ravive9d501b2014-07-01 12:22:37 +03006191 if (!(reg & (1 << tag))) {
6192 dev_err(hba->dev,
6193 "%s: cmd was completed, but without a notifying intr, tag = %d",
6194 __func__, tag);
6195 }
6196
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006197 /* Print Transfer Request of aborted task */
6198 dev_err(hba->dev, "%s: Device abort task at tag %d", __func__, tag);
6199
6200 /*
6201 * Print detailed info about aborted request.
6202 * As more than one request might get aborted at the same time,
6203 * print full information only for the first aborted request in order
6204 * to reduce repeated printouts. For other aborted requests only print
6205 * basic details.
6206 */
6207 scsi_print_command(cmd);
6208 if (!hba->req_abort_count) {
6209 ufshcd_print_host_regs(hba);
6210 ufshcd_print_host_state(hba);
6211 ufshcd_print_pwr_info(hba);
6212 ufshcd_print_trs(hba, 1 << tag, true);
6213 } else {
6214 ufshcd_print_trs(hba, 1 << tag, false);
6215 }
6216 hba->req_abort_count++;
6217
6218
6219 /* Skip task abort in case previous aborts failed and report failure */
6220 if (lrbp->req_abort_skip) {
6221 err = -EIO;
6222 goto out;
6223 }
6224
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306225 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6226 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6227 UFS_QUERY_TASK, &resp);
6228 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6229 /* cmd pending in the device */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006230 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d",
6231 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306232 break;
6233 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306234 /*
6235 * cmd not pending in the device, check if it is
6236 * in transition.
6237 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006238 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.",
6239 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306240 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6241 if (reg & (1 << tag)) {
6242 /* sleep for max. 200us to stabilize */
6243 usleep_range(100, 200);
6244 continue;
6245 }
6246 /* command completed already */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006247 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.",
6248 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306249 goto out;
6250 } else {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006251 dev_err(hba->dev,
6252 "%s: no response from device. tag = %d, err %d",
6253 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306254 if (!err)
6255 err = resp; /* service response error */
6256 goto out;
6257 }
6258 }
6259
6260 if (!poll_cnt) {
6261 err = -EBUSY;
6262 goto out;
6263 }
6264
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306265 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6266 UFS_ABORT_TASK, &resp);
6267 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006268 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306269 err = resp; /* service response error */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006270 dev_err(hba->dev, "%s: issued. tag = %d, err %d",
6271 __func__, tag, err);
6272 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306273 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306274 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306275
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306276 err = ufshcd_clear_cmd(hba, tag);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006277 if (err) {
6278 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d",
6279 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306280 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006281 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306282
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306283 scsi_dma_unmap(cmd);
6284
6285 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006286 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306287 hba->lrb[tag].cmd = NULL;
6288 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306289
6290 clear_bit_unlock(tag, &hba->lrb_in_use);
6291 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006292
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306293out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306294 if (!err) {
6295 err = SUCCESS;
6296 } else {
6297 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006298 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306299 err = FAILED;
6300 }
6301
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006302 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006303 * This ufshcd_release_all() corresponds to the original scsi cmd that
6304 * got aborted here (as we won't get any IRQ for it).
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006305 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006306 ufshcd_release_all(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306307 return err;
6308}
6309
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306310/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306311 * ufshcd_host_reset_and_restore - reset and restore host controller
6312 * @hba: per-adapter instance
6313 *
6314 * Note that host controller reset may issue DME_RESET to
6315 * local and remote (device) Uni-Pro stack and the attributes
6316 * are reset to default state.
6317 *
6318 * Returns zero on success, non-zero on failure
6319 */
6320static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6321{
6322 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306323 unsigned long flags;
6324
6325 /* Reset the host controller */
6326 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006327 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306328 spin_unlock_irqrestore(hba->host->host_lock, flags);
6329
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006330 /* scale up clocks to max frequency before full reinitialization */
6331 ufshcd_set_clk_freq(hba, true);
6332
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306333 err = ufshcd_hba_enable(hba);
6334 if (err)
6335 goto out;
6336
6337 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006338 err = ufshcd_probe_hba(hba);
6339
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006340 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306341 err = -EIO;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006342 goto out;
6343 }
6344
6345 if (!err) {
6346 err = ufshcd_vops_crypto_engine_reset(hba);
6347 if (err) {
6348 dev_err(hba->dev,
6349 "%s: failed to reset crypto engine %d\n",
6350 __func__, err);
6351 goto out;
6352 }
6353 }
6354
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306355out:
6356 if (err)
6357 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
6358
6359 return err;
6360}
6361
6362/**
6363 * ufshcd_reset_and_restore - reset and re-initialize host/device
6364 * @hba: per-adapter instance
6365 *
6366 * Reset and recover device, host and re-establish link. This
6367 * is helpful to recover the communication in fatal error conditions.
6368 *
6369 * Returns zero on success, non-zero on failure
6370 */
6371static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6372{
6373 int err = 0;
6374 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006375 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306376
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006377 do {
6378 err = ufshcd_host_reset_and_restore(hba);
6379 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306380
6381 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006382 * There is no point proceeding even after failing
6383 * to recover after multiple retries.
6384 */
6385 if (err)
6386 BUG();
6387 /*
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306388 * After reset the door-bell might be cleared, complete
6389 * outstanding requests in s/w here.
6390 */
6391 spin_lock_irqsave(hba->host->host_lock, flags);
6392 ufshcd_transfer_req_compl(hba);
6393 ufshcd_tmc_handler(hba);
6394 spin_unlock_irqrestore(hba->host->host_lock, flags);
6395
6396 return err;
6397}
6398
6399/**
6400 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
6401 * @cmd - SCSI command pointer
6402 *
6403 * Returns SUCCESS/FAILED
6404 */
6405static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6406{
6407 int err;
6408 unsigned long flags;
6409 struct ufs_hba *hba;
6410
6411 hba = shost_priv(cmd->device->host);
6412
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006413 ufshcd_hold_all(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306414 /*
6415 * Check if there is any race with fatal error handling.
6416 * If so, wait for it to complete. Even though fatal error
6417 * handling does reset and restore in some cases, don't assume
6418 * anything out of it. We are just avoiding race here.
6419 */
6420 do {
6421 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306422 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306423 hba->ufshcd_state == UFSHCD_STATE_RESET))
6424 break;
6425 spin_unlock_irqrestore(hba->host->host_lock, flags);
6426 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306427 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306428 } while (1);
6429
6430 hba->ufshcd_state = UFSHCD_STATE_RESET;
6431 ufshcd_set_eh_in_progress(hba);
6432 spin_unlock_irqrestore(hba->host->host_lock, flags);
6433
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006434 ufshcd_update_error_stats(hba, UFS_ERR_EH);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306435 err = ufshcd_reset_and_restore(hba);
6436
6437 spin_lock_irqsave(hba->host->host_lock, flags);
6438 if (!err) {
6439 err = SUCCESS;
6440 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6441 } else {
6442 err = FAILED;
6443 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6444 }
6445 ufshcd_clear_eh_in_progress(hba);
6446 spin_unlock_irqrestore(hba->host->host_lock, flags);
6447
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006448 ufshcd_release_all(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306449 return err;
6450}
6451
6452/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006453 * ufshcd_get_max_icc_level - calculate the ICC level
6454 * @sup_curr_uA: max. current supported by the regulator
6455 * @start_scan: row at the desc table to start scan from
6456 * @buff: power descriptor buffer
6457 *
6458 * Returns calculated max ICC level for specific regulator
6459 */
6460static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6461{
6462 int i;
6463 int curr_uA;
6464 u16 data;
6465 u16 unit;
6466
6467 for (i = start_scan; i >= 0; i--) {
6468 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
6469 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6470 ATTR_ICC_LVL_UNIT_OFFSET;
6471 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6472 switch (unit) {
6473 case UFSHCD_NANO_AMP:
6474 curr_uA = curr_uA / 1000;
6475 break;
6476 case UFSHCD_MILI_AMP:
6477 curr_uA = curr_uA * 1000;
6478 break;
6479 case UFSHCD_AMP:
6480 curr_uA = curr_uA * 1000 * 1000;
6481 break;
6482 case UFSHCD_MICRO_AMP:
6483 default:
6484 break;
6485 }
6486 if (sup_curr_uA >= curr_uA)
6487 break;
6488 }
6489 if (i < 0) {
6490 i = 0;
6491 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6492 }
6493
6494 return (u32)i;
6495}
6496
6497/**
Subhash Jadavania8d1ba32016-12-12 18:19:21 -08006498 * ufshcd_find_max_sup_active_icc_level - find the max ICC level
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006499 * In case regulators are not initialized we'll return 0
6500 * @hba: per-adapter instance
6501 * @desc_buf: power descriptor buffer to extract ICC levels from.
6502 * @len: length of desc_buff
6503 *
Subhash Jadavania8d1ba32016-12-12 18:19:21 -08006504 * Returns calculated max ICC level
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006505 */
6506static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6507 u8 *desc_buf, int len)
6508{
6509 u32 icc_level = 0;
6510
Subhash Jadavania8d1ba32016-12-12 18:19:21 -08006511 /*
6512 * VCCQ rail is optional for removable UFS card and also most of the
6513 * vendors don't use this rail for embedded UFS devices as well. So
6514 * it is normal that VCCQ rail may not be provided for given platform.
6515 */
6516 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq2) {
6517 dev_err(hba->dev, "%s: Regulator capability was not set, bActiveICCLevel=%d\n",
6518 __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006519 goto out;
6520 }
6521
6522 if (hba->vreg_info.vcc)
6523 icc_level = ufshcd_get_max_icc_level(
6524 hba->vreg_info.vcc->max_uA,
6525 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6526 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6527
6528 if (hba->vreg_info.vccq)
6529 icc_level = ufshcd_get_max_icc_level(
6530 hba->vreg_info.vccq->max_uA,
6531 icc_level,
6532 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6533
6534 if (hba->vreg_info.vccq2)
6535 icc_level = ufshcd_get_max_icc_level(
6536 hba->vreg_info.vccq2->max_uA,
6537 icc_level,
6538 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6539out:
6540 return icc_level;
6541}
6542
Subhash Jadavani8a93dbd2016-12-12 17:59:44 -08006543static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006544{
6545 int ret;
6546 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
6547 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
Subhash Jadavani35732e52016-12-09 16:09:42 -08006548 u32 icc_level;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006549
6550 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
6551 if (ret) {
6552 dev_err(hba->dev,
6553 "%s: Failed reading power descriptor.len = %d ret = %d",
6554 __func__, buff_len, ret);
6555 return;
6556 }
6557
Subhash Jadavani35732e52016-12-09 16:09:42 -08006558 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf,
6559 buff_len);
6560 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006561
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02006562 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Subhash Jadavani35732e52016-12-09 16:09:42 -08006563 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006564
6565 if (ret)
6566 dev_err(hba->dev,
6567 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
Subhash Jadavani35732e52016-12-09 16:09:42 -08006568 __func__, icc_level, ret);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006569}
6570
6571/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006572 * ufshcd_scsi_add_wlus - Adds required W-LUs
6573 * @hba: per-adapter instance
6574 *
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006575 * UFS devices can support upto 4 well known logical units:
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006576 * "REPORT_LUNS" (address: 01h)
6577 * "UFS Device" (address: 50h)
6578 * "RPMB" (address: 44h)
6579 * "BOOT" (address: 30h)
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006580 *
6581 * "REPORT_LUNS" & "UFS Device" are mandatory for all device classes (see
6582 * "bDeviceSubClass" parameter of device descriptor) while "BOOT" is supported
6583 * only for bootable devices. "RPMB" is only supported with embedded devices.
6584 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006585 * UFS device's power management needs to be controlled by "POWER CONDITION"
6586 * field of SSU (START STOP UNIT) command. But this "power condition" field
6587 * will take effect only when its sent to "UFS device" well known logical unit
6588 * hence we require the scsi_device instance to represent this logical unit in
6589 * order for the UFS host driver to send the SSU command for power management.
6590
6591 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
6592 * Block) LU so user space process can control this LU. User space may also
6593 * want to have access to BOOT LU.
6594
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006595 * This function tries to add scsi device instances for each of all well known
6596 * LUs (except "REPORT LUNS" LU) depending on device class.
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006597 *
6598 * Returns zero on success (all required W-LUs are added successfully),
6599 * non-zero error value on failure (if failed to add any of the required W-LU).
6600 */
6601static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
6602{
6603 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006604 struct scsi_device *sdev_rpmb;
6605 struct scsi_device *sdev_boot;
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006606 bool is_bootable_dev = false;
6607 bool is_embedded_dev = false;
6608
6609 if ((hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_BOOTABLE) ||
6610 (hba->dev_info.b_device_sub_class == UFS_DEV_REMOVABLE_BOOTABLE))
6611 is_bootable_dev = true;
6612
6613 if ((hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_BOOTABLE) ||
6614 (hba->dev_info.b_device_sub_class == UFS_DEV_EMBEDDED_NON_BOOTABLE))
6615 is_embedded_dev = true;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006616
6617 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
6618 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
6619 if (IS_ERR(hba->sdev_ufs_device)) {
6620 ret = PTR_ERR(hba->sdev_ufs_device);
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006621 dev_err(hba->dev, "%s: failed adding DEVICE_WLUN. ret %d\n",
6622 __func__, ret);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006623 hba->sdev_ufs_device = NULL;
6624 goto out;
6625 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006626 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006627
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006628 if (is_bootable_dev) {
6629 sdev_boot = __scsi_add_device(hba->host, 0, 0,
6630 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN),
6631 NULL);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006632
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006633 if (IS_ERR(sdev_boot)) {
6634 dev_err(hba->dev, "%s: failed adding BOOT_WLUN. ret %d\n",
6635 __func__, ret);
6636 ret = PTR_ERR(sdev_boot);
6637 goto remove_sdev_ufs_device;
6638 }
6639 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006640 }
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006641
6642 if (is_embedded_dev) {
6643 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
6644 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN),
6645 NULL);
6646 if (IS_ERR(sdev_rpmb)) {
6647 dev_err(hba->dev, "%s: failed adding RPMB_WLUN. ret %d\n",
6648 __func__, ret);
6649 ret = PTR_ERR(sdev_rpmb);
6650 goto remove_sdev_boot;
6651 }
6652 scsi_device_put(sdev_rpmb);
6653 }
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006654 goto out;
6655
6656remove_sdev_boot:
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006657 if (is_bootable_dev)
6658 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006659remove_sdev_ufs_device:
6660 scsi_remove_device(hba->sdev_ufs_device);
6661out:
6662 return ret;
6663}
6664
6665/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006666 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6667 * @hba: per-adapter instance
6668 *
6669 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6670 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6671 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6672 * the hibern8 exit latency.
6673 *
6674 * Returns zero on success, non-zero error value on failure.
6675 */
6676static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6677{
6678 int ret = 0;
6679 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6680
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006681 if (!ufshcd_is_unipro_pa_params_tuning_req(hba))
6682 return 0;
6683
Yaniv Gardi37113102016-03-10 17:37:16 +02006684 ret = ufshcd_dme_peer_get(hba,
6685 UIC_ARG_MIB_SEL(
6686 RX_MIN_ACTIVATETIME_CAPABILITY,
6687 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6688 &peer_rx_min_activatetime);
6689 if (ret)
6690 goto out;
6691
6692 /* make sure proper unit conversion is applied */
6693 tuned_pa_tactivate =
6694 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6695 / PA_TACTIVATE_TIME_UNIT_US);
6696 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6697 tuned_pa_tactivate);
6698
6699out:
6700 return ret;
6701}
6702
6703/**
6704 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6705 * @hba: per-adapter instance
6706 *
6707 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6708 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6709 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6710 * This optimal value can help reduce the hibern8 exit latency.
6711 *
6712 * Returns zero on success, non-zero error value on failure.
6713 */
6714static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6715{
6716 int ret = 0;
6717 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6718 u32 max_hibern8_time, tuned_pa_hibern8time;
6719
6720 ret = ufshcd_dme_get(hba,
6721 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6722 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6723 &local_tx_hibern8_time_cap);
6724 if (ret)
6725 goto out;
6726
6727 ret = ufshcd_dme_peer_get(hba,
6728 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6729 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6730 &peer_rx_hibern8_time_cap);
6731 if (ret)
6732 goto out;
6733
6734 max_hibern8_time = max(local_tx_hibern8_time_cap,
6735 peer_rx_hibern8_time_cap);
6736 /* make sure proper unit conversion is applied */
6737 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6738 / PA_HIBERN8_TIME_UNIT_US);
6739 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6740 tuned_pa_hibern8time);
6741out:
6742 return ret;
6743}
6744
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006745/**
6746 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6747 * less than device PA_TACTIVATE time.
6748 * @hba: per-adapter instance
6749 *
6750 * Some UFS devices require host PA_TACTIVATE to be lower than device
6751 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6752 * for such devices.
6753 *
6754 * Returns zero on success, non-zero error value on failure.
6755 */
6756static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6757{
6758 int ret = 0;
6759 u32 granularity, peer_granularity;
6760 u32 pa_tactivate, peer_pa_tactivate;
6761 u32 pa_tactivate_us, peer_pa_tactivate_us;
6762 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6763
6764 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6765 &granularity);
6766 if (ret)
6767 goto out;
6768
6769 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6770 &peer_granularity);
6771 if (ret)
6772 goto out;
6773
6774 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6775 (granularity > PA_GRANULARITY_MAX_VAL)) {
6776 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6777 __func__, granularity);
6778 return -EINVAL;
6779 }
6780
6781 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6782 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6783 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6784 __func__, peer_granularity);
6785 return -EINVAL;
6786 }
6787
6788 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6789 if (ret)
6790 goto out;
6791
6792 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6793 &peer_pa_tactivate);
6794 if (ret)
6795 goto out;
6796
6797 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6798 peer_pa_tactivate_us = peer_pa_tactivate *
6799 gran_to_us_table[peer_granularity - 1];
6800
6801 if (pa_tactivate_us > peer_pa_tactivate_us) {
6802 u32 new_peer_pa_tactivate;
6803
6804 new_peer_pa_tactivate = pa_tactivate_us /
6805 gran_to_us_table[peer_granularity - 1];
6806 new_peer_pa_tactivate++;
6807 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6808 new_peer_pa_tactivate);
6809 }
6810
6811out:
6812 return ret;
6813}
6814
Yaniv Gardi37113102016-03-10 17:37:16 +02006815static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
6816{
6817 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6818 ufshcd_tune_pa_tactivate(hba);
6819 ufshcd_tune_pa_hibern8time(hba);
6820 }
6821
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08006822 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
Yaniv Gardi37113102016-03-10 17:37:16 +02006823 /* set 1ms timeout for PA_TACTIVATE */
6824 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006825
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08006826 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006827 ufshcd_quirk_tune_host_pa_tactivate(hba);
6828
6829 ufshcd_vops_apply_dev_quirks(hba);
6830}
6831
6832static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6833{
6834 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
6835
6836 hba->ufs_stats.hibern8_exit_cnt = 0;
6837 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
6838
6839 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
6840 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
6841 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
6842 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
6843 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
6844
6845 hba->req_abort_count = 0;
6846}
6847
6848static void ufshcd_apply_pm_quirks(struct ufs_hba *hba)
6849{
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08006850 if (hba->dev_info.quirks & UFS_DEVICE_QUIRK_NO_LINK_OFF) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006851 if (ufs_get_pm_lvl_to_link_pwr_state(hba->rpm_lvl) ==
6852 UIC_LINK_OFF_STATE) {
6853 hba->rpm_lvl =
6854 ufs_get_desired_pm_lvl_for_dev_link_state(
6855 UFS_SLEEP_PWR_MODE,
6856 UIC_LINK_HIBERN8_STATE);
6857 dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed rpm_lvl to %d\n",
6858 hba->rpm_lvl);
6859 }
6860 if (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
6861 UIC_LINK_OFF_STATE) {
6862 hba->spm_lvl =
6863 ufs_get_desired_pm_lvl_for_dev_link_state(
6864 UFS_SLEEP_PWR_MODE,
6865 UIC_LINK_HIBERN8_STATE);
6866 dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed spm_lvl to %d\n",
6867 hba->spm_lvl);
6868 }
6869 }
Yaniv Gardi37113102016-03-10 17:37:16 +02006870}
6871
6872/**
Subhash Jadavani88f99992016-12-13 15:52:21 -08006873 * ufshcd_set_dev_ref_clk - set the device bRefClkFreq
6874 * @hba: per-adapter instance
6875 *
6876 * Read the current value of the bRefClkFreq attribute from device and update it
6877 * if host is supplying different reference clock frequency than one mentioned
6878 * in bRefClkFreq attribute.
6879 *
6880 * Returns zero on success, non-zero error value on failure.
6881 */
6882static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
6883{
6884 int err = 0;
6885 int ref_clk = -1;
6886 static const char * const ref_clk_freqs[] = {"19.2 MHz", "26 MHz",
6887 "38.4 MHz", "52 MHz"};
6888
6889 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
6890 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
6891
6892 if (err) {
6893 dev_err(hba->dev, "%s: failed reading bRefClkFreq. err = %d\n",
6894 __func__, err);
6895 goto out;
6896 }
6897
6898 if ((ref_clk < 0) || (ref_clk > REF_CLK_FREQ_52_MHZ)) {
6899 dev_err(hba->dev, "%s: invalid ref_clk setting = %d\n",
6900 __func__, ref_clk);
6901 err = -EINVAL;
6902 goto out;
6903 }
6904
6905 if (ref_clk == hba->dev_ref_clk_freq)
6906 goto out; /* nothing to update */
6907
6908 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
6909 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0,
6910 &hba->dev_ref_clk_freq);
6911
6912 if (err)
6913 dev_err(hba->dev, "%s: bRefClkFreq setting to %s failed\n",
6914 __func__, ref_clk_freqs[hba->dev_ref_clk_freq]);
6915 else
6916 /*
6917 * It is good to print this out here to debug any later failures
6918 * related to gear switch.
6919 */
6920 dev_info(hba->dev, "%s: bRefClkFreq setting to %s succeeded\n",
6921 __func__, ref_clk_freqs[hba->dev_ref_clk_freq]);
6922
6923out:
6924 return err;
6925}
6926
Subhash Jadavani344c16c2016-12-15 17:09:35 -08006927static int ufs_read_device_desc_data(struct ufs_hba *hba)
6928{
6929 int err;
6930 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
6931
6932 err = ufshcd_read_device_desc(hba, desc_buf, sizeof(desc_buf));
6933 if (err)
6934 return err;
6935
6936 /*
6937 * getting vendor (manufacturerID) and Bank Index in big endian
6938 * format
6939 */
6940 hba->dev_info.w_manufacturer_id =
6941 desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
6942 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6943 hba->dev_info.b_device_sub_class =
6944 desc_buf[DEVICE_DESC_PARAM_DEVICE_SUB_CLASS];
6945 hba->dev_info.i_product_name = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
6946
6947 return 0;
6948}
6949
Subhash Jadavani88f99992016-12-13 15:52:21 -08006950/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006951 * ufshcd_probe_hba - probe hba to detect device and initialize
6952 * @hba: per-adapter instance
6953 *
6954 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306955 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006956static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306957{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306958 int ret;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006959 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306960
6961 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306962 if (ret)
6963 goto out;
6964
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006965 /* Enable auto hibern8 if supported */
6966 if (ufshcd_is_auto_hibern8_supported(hba))
6967 ufshcd_set_auto_hibern8_timer(hba,
6968 hba->hibern8_on_idle.delay_ms);
Yaniv Gardi50646362014-10-23 13:25:13 +03006969
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006970 /* Debug counters initialization */
6971 ufshcd_clear_dbg_ufs_stats(hba);
Yaniv Gardiafdfff52016-03-10 17:37:15 +02006972 /* set the default level for urgent bkops */
6973 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
6974 hba->is_urgent_bkops_lvl_checked = false;
6975
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006976 /* UniPro link is active now */
6977 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05306978
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306979 ret = ufshcd_verify_dev_init(hba);
6980 if (ret)
6981 goto out;
6982
Dolev Raviv68078d52013-07-30 00:35:58 +05306983 ret = ufshcd_complete_dev_init(hba);
6984 if (ret)
6985 goto out;
6986
Subhash Jadavani2df121a2016-12-15 18:27:31 -08006987 /* clear any previous UFS device information */
6988 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
6989
Subhash Jadavani344c16c2016-12-15 17:09:35 -08006990 /* cache important parameters from device descriptor for later use */
6991 ret = ufs_read_device_desc_data(hba);
6992 if (ret)
6993 goto out;
6994
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006995 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006996 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006997
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006998 ufshcd_apply_pm_quirks(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006999 ret = ufshcd_set_vccq_rail_unused(hba,
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08007000 (hba->dev_info.quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
Yaniv Gardi60f01872016-03-10 17:37:11 +02007001 if (ret)
7002 goto out;
7003
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007004 /* UFS device is also active now */
7005 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307006 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307007
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007008 if (ufshcd_get_max_pwr_mode(hba)) {
7009 dev_err(hba->dev,
7010 "%s: Failed getting max supported power mode\n",
7011 __func__);
7012 } else {
Subhash Jadavani88f99992016-12-13 15:52:21 -08007013 /*
7014 * Set the right value to bRefClkFreq before attempting to
7015 * switch to HS gears.
7016 */
7017 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007018 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007019 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007020 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7021 __func__, ret);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007022 goto out;
7023 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007024 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007025
Subhash Jadavani8a93dbd2016-12-12 17:59:44 -08007026 /*
7027 * bActiveICCLevel is volatile for UFS device (as per latest v2.1 spec)
7028 * and for removable UFS card as well, hence always set the parameter.
7029 * Note: Error handler may issue the device reset hence resetting
7030 * bActiveICCLevel as well so it is always safe to set this here.
7031 */
7032 ufshcd_set_active_icc_lvl(hba);
7033
Yaniv Gardi53c12d02016-02-01 15:02:45 +02007034 /* set the state as operational after switching to desired gear */
7035 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007036 /*
7037 * If we are in error handling context or in power management callbacks
7038 * context, no need to scan the host
7039 */
7040 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
7041 bool flag;
7042
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02007043 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
7044 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007045 hba->dev_info.f_power_on_wp_en = flag;
7046
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03007047 /* Add required well known logical units to scsi mid layer */
7048 if (ufshcd_scsi_add_wlus(hba))
7049 goto out;
7050
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307051 scsi_scan_host(hba->host);
7052 pm_runtime_put_sync(hba->dev);
7053 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03007054
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007055 /* Resume devfreq after UFS device is detected */
7056 if (ufshcd_is_clkscaling_supported(hba)) {
7057 memcpy(&hba->clk_scaling.saved_pwr_info.info, &hba->pwr_info,
7058 sizeof(struct ufs_pa_layer_attr));
7059 hba->clk_scaling.saved_pwr_info.is_valid = true;
7060 hba->clk_scaling.is_scaled_up = true;
7061 ufshcd_resume_clkscaling(hba);
7062 hba->clk_scaling.is_allowed = true;
7063 }
7064
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307065out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007066 /*
7067 * If we failed to initialize the device or the device is not
7068 * present, turn off the power/clocks etc.
7069 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007070 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
7071 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007072 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007073 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007074
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007075 trace_ufshcd_init(dev_name(hba->dev), ret,
7076 ktime_to_us(ktime_sub(ktime_get(), start)),
7077 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007078 return ret;
7079}
7080
7081/**
7082 * ufshcd_async_scan - asynchronous execution for probing hba
7083 * @data: data pointer to pass to this function
7084 * @cookie: cookie data
7085 */
7086static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7087{
7088 struct ufs_hba *hba = (struct ufs_hba *)data;
7089
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007090 /*
7091 * Don't allow clock gating and hibern8 enter for faster device
7092 * detection.
7093 */
7094 ufshcd_hold_all(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007095 ufshcd_probe_hba(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007096 ufshcd_release_all(hba);
7097}
7098
7099/**
7100 * ufshcd_query_ioctl - perform user read queries
7101 * @hba: per-adapter instance
7102 * @lun: used for lun specific queries
7103 * @buffer: user space buffer for reading and submitting query data and params
7104 * @return: 0 for success negative error code otherwise
7105 *
7106 * Expected/Submitted buffer structure is struct ufs_ioctl_query_data.
7107 * It will read the opcode, idn and buf_length parameters, and, put the
7108 * response in the buffer field while updating the used size in buf_length.
7109 */
7110static int ufshcd_query_ioctl(struct ufs_hba *hba, u8 lun, void __user *buffer)
7111{
7112 struct ufs_ioctl_query_data *ioctl_data;
7113 int err = 0;
7114 int length = 0;
7115 void *data_ptr;
7116 bool flag;
7117 u32 att;
7118 u8 index;
7119 u8 *desc = NULL;
7120
7121 ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL);
7122 if (!ioctl_data) {
7123 dev_err(hba->dev, "%s: Failed allocating %zu bytes\n", __func__,
7124 sizeof(struct ufs_ioctl_query_data));
7125 err = -ENOMEM;
7126 goto out;
7127 }
7128
7129 /* extract params from user buffer */
7130 err = copy_from_user(ioctl_data, buffer,
7131 sizeof(struct ufs_ioctl_query_data));
7132 if (err) {
7133 dev_err(hba->dev,
7134 "%s: Failed copying buffer from user, err %d\n",
7135 __func__, err);
7136 goto out_release_mem;
7137 }
7138
7139 /* verify legal parameters & send query */
7140 switch (ioctl_data->opcode) {
7141 case UPIU_QUERY_OPCODE_READ_DESC:
7142 switch (ioctl_data->idn) {
7143 case QUERY_DESC_IDN_DEVICE:
7144 case QUERY_DESC_IDN_CONFIGURAION:
7145 case QUERY_DESC_IDN_INTERCONNECT:
7146 case QUERY_DESC_IDN_GEOMETRY:
7147 case QUERY_DESC_IDN_POWER:
7148 index = 0;
7149 break;
7150 case QUERY_DESC_IDN_UNIT:
7151 if (!ufs_is_valid_unit_desc_lun(lun)) {
7152 dev_err(hba->dev,
7153 "%s: No unit descriptor for lun 0x%x\n",
7154 __func__, lun);
7155 err = -EINVAL;
7156 goto out_release_mem;
7157 }
7158 index = lun;
7159 break;
7160 default:
7161 goto out_einval;
7162 }
7163 length = min_t(int, QUERY_DESC_MAX_SIZE,
7164 ioctl_data->buf_size);
7165 desc = kzalloc(length, GFP_KERNEL);
7166 if (!desc) {
7167 dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
7168 __func__, length);
7169 err = -ENOMEM;
7170 goto out_release_mem;
7171 }
7172 err = ufshcd_query_descriptor(hba, ioctl_data->opcode,
7173 ioctl_data->idn, index, 0, desc, &length);
7174 break;
7175 case UPIU_QUERY_OPCODE_READ_ATTR:
7176 switch (ioctl_data->idn) {
7177 case QUERY_ATTR_IDN_BOOT_LU_EN:
7178 case QUERY_ATTR_IDN_POWER_MODE:
7179 case QUERY_ATTR_IDN_ACTIVE_ICC_LVL:
7180 case QUERY_ATTR_IDN_OOO_DATA_EN:
7181 case QUERY_ATTR_IDN_BKOPS_STATUS:
7182 case QUERY_ATTR_IDN_PURGE_STATUS:
7183 case QUERY_ATTR_IDN_MAX_DATA_IN:
7184 case QUERY_ATTR_IDN_MAX_DATA_OUT:
7185 case QUERY_ATTR_IDN_REF_CLK_FREQ:
7186 case QUERY_ATTR_IDN_CONF_DESC_LOCK:
7187 case QUERY_ATTR_IDN_MAX_NUM_OF_RTT:
7188 case QUERY_ATTR_IDN_EE_CONTROL:
7189 case QUERY_ATTR_IDN_EE_STATUS:
7190 case QUERY_ATTR_IDN_SECONDS_PASSED:
7191 index = 0;
7192 break;
7193 case QUERY_ATTR_IDN_DYN_CAP_NEEDED:
7194 case QUERY_ATTR_IDN_CORR_PRG_BLK_NUM:
7195 index = lun;
7196 break;
7197 default:
7198 goto out_einval;
7199 }
7200 err = ufshcd_query_attr(hba, ioctl_data->opcode, ioctl_data->idn,
7201 index, 0, &att);
7202 break;
7203
7204 case UPIU_QUERY_OPCODE_WRITE_ATTR:
7205 err = copy_from_user(&att,
7206 buffer + sizeof(struct ufs_ioctl_query_data),
7207 sizeof(u32));
7208 if (err) {
7209 dev_err(hba->dev,
7210 "%s: Failed copying buffer from user, err %d\n",
7211 __func__, err);
7212 goto out_release_mem;
7213 }
7214
7215 switch (ioctl_data->idn) {
7216 case QUERY_ATTR_IDN_BOOT_LU_EN:
7217 index = 0;
7218 if (att > QUERY_ATTR_IDN_BOOT_LU_EN_MAX) {
7219 dev_err(hba->dev,
7220 "%s: Illegal ufs query ioctl data, opcode 0x%x, idn 0x%x, att 0x%x\n",
7221 __func__, ioctl_data->opcode,
7222 (unsigned int)ioctl_data->idn, att);
7223 err = -EINVAL;
7224 goto out_release_mem;
7225 }
7226 break;
7227 default:
7228 goto out_einval;
7229 }
7230 err = ufshcd_query_attr(hba, ioctl_data->opcode,
7231 ioctl_data->idn, index, 0, &att);
7232 break;
7233
7234 case UPIU_QUERY_OPCODE_READ_FLAG:
7235 switch (ioctl_data->idn) {
7236 case QUERY_FLAG_IDN_FDEVICEINIT:
7237 case QUERY_FLAG_IDN_PERMANENT_WPE:
7238 case QUERY_FLAG_IDN_PWR_ON_WPE:
7239 case QUERY_FLAG_IDN_BKOPS_EN:
7240 case QUERY_FLAG_IDN_PURGE_ENABLE:
7241 case QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL:
7242 case QUERY_FLAG_IDN_BUSY_RTC:
7243 break;
7244 default:
7245 goto out_einval;
7246 }
7247 err = ufshcd_query_flag_retry(hba, ioctl_data->opcode,
7248 ioctl_data->idn, &flag);
7249 break;
7250 default:
7251 goto out_einval;
7252 }
7253
7254 if (err) {
7255 dev_err(hba->dev, "%s: Query for idn %d failed\n", __func__,
7256 ioctl_data->idn);
7257 goto out_release_mem;
7258 }
7259
7260 /*
7261 * copy response data
7262 * As we might end up reading less data then what is specified in
7263 * "ioctl_data->buf_size". So we are updating "ioctl_data->
7264 * buf_size" to what exactly we have read.
7265 */
7266 switch (ioctl_data->opcode) {
7267 case UPIU_QUERY_OPCODE_READ_DESC:
7268 ioctl_data->buf_size = min_t(int, ioctl_data->buf_size, length);
7269 data_ptr = desc;
7270 break;
7271 case UPIU_QUERY_OPCODE_READ_ATTR:
7272 ioctl_data->buf_size = sizeof(u32);
7273 data_ptr = &att;
7274 break;
7275 case UPIU_QUERY_OPCODE_READ_FLAG:
7276 ioctl_data->buf_size = 1;
7277 data_ptr = &flag;
7278 break;
7279 case UPIU_QUERY_OPCODE_WRITE_ATTR:
7280 goto out_release_mem;
7281 default:
7282 goto out_einval;
7283 }
7284
7285 /* copy to user */
7286 err = copy_to_user(buffer, ioctl_data,
7287 sizeof(struct ufs_ioctl_query_data));
7288 if (err)
7289 dev_err(hba->dev, "%s: Failed copying back to user.\n",
7290 __func__);
7291 err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data),
7292 data_ptr, ioctl_data->buf_size);
7293 if (err)
7294 dev_err(hba->dev, "%s: err %d copying back to user.\n",
7295 __func__, err);
7296 goto out_release_mem;
7297
7298out_einval:
7299 dev_err(hba->dev,
7300 "%s: illegal ufs query ioctl data, opcode 0x%x, idn 0x%x\n",
7301 __func__, ioctl_data->opcode, (unsigned int)ioctl_data->idn);
7302 err = -EINVAL;
7303out_release_mem:
7304 kfree(ioctl_data);
7305 kfree(desc);
7306out:
7307 return err;
7308}
7309
7310/**
7311 * ufshcd_ioctl - ufs ioctl callback registered in scsi_host
7312 * @dev: scsi device required for per LUN queries
7313 * @cmd: command opcode
7314 * @buffer: user space buffer for transferring data
7315 *
7316 * Supported commands:
7317 * UFS_IOCTL_QUERY
7318 */
7319static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer)
7320{
7321 struct ufs_hba *hba = shost_priv(dev->host);
7322 int err = 0;
7323
7324 BUG_ON(!hba);
7325 if (!buffer) {
7326 dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__);
7327 return -EINVAL;
7328 }
7329
7330 switch (cmd) {
7331 case UFS_IOCTL_QUERY:
7332 pm_runtime_get_sync(hba->dev);
7333 err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun),
7334 buffer);
7335 pm_runtime_put_sync(hba->dev);
7336 break;
7337 default:
7338 err = -ENOIOCTLCMD;
7339 dev_dbg(hba->dev, "%s: Unsupported ioctl cmd %d\n", __func__,
7340 cmd);
7341 break;
7342 }
7343
7344 return err;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307345}
7346
Yaniv Gardif550c652016-03-10 17:37:07 +02007347static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
7348{
7349 unsigned long flags;
7350 struct Scsi_Host *host;
7351 struct ufs_hba *hba;
7352 int index;
7353 bool found = false;
7354
7355 if (!scmd || !scmd->device || !scmd->device->host)
7356 return BLK_EH_NOT_HANDLED;
7357
7358 host = scmd->device->host;
7359 hba = shost_priv(host);
7360 if (!hba)
7361 return BLK_EH_NOT_HANDLED;
7362
7363 spin_lock_irqsave(host->host_lock, flags);
7364
7365 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
7366 if (hba->lrb[index].cmd == scmd) {
7367 found = true;
7368 break;
7369 }
7370 }
7371
7372 spin_unlock_irqrestore(host->host_lock, flags);
7373
7374 /*
7375 * Bypass SCSI error handling and reset the block layer timer if this
7376 * SCSI command was not actually dispatched to UFS driver, otherwise
7377 * let SCSI layer handle the error as usual.
7378 */
7379 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
7380}
7381
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307382static struct scsi_host_template ufshcd_driver_template = {
7383 .module = THIS_MODULE,
7384 .name = UFSHCD,
7385 .proc_name = UFSHCD,
7386 .queuecommand = ufshcd_queuecommand,
7387 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007388 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307389 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007390 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307391 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307392 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7393 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02007394 .eh_timed_out = ufshcd_eh_timed_out,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007395 .ioctl = ufshcd_ioctl,
7396#ifdef CONFIG_COMPAT
7397 .compat_ioctl = ufshcd_ioctl,
7398#endif
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307399 .this_id = -1,
7400 .sg_tablesize = SG_ALL,
7401 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7402 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007403 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007404 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307405};
7406
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007407static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7408 int ua)
7409{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007410 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007411
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007412 if (!vreg)
7413 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007414
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007415 ret = regulator_set_load(vreg->reg, ua);
7416 if (ret < 0) {
7417 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7418 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007419 }
7420
7421 return ret;
7422}
7423
7424static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7425 struct ufs_vreg *vreg)
7426{
Yaniv Gardi60f01872016-03-10 17:37:11 +02007427 if (!vreg)
7428 return 0;
7429 else if (vreg->unused)
7430 return 0;
7431 else
7432 return ufshcd_config_vreg_load(hba->dev, vreg,
7433 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007434}
7435
7436static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7437 struct ufs_vreg *vreg)
7438{
Yaniv Gardi60f01872016-03-10 17:37:11 +02007439 if (!vreg)
7440 return 0;
7441 else if (vreg->unused)
7442 return 0;
7443 else
7444 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007445}
7446
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007447static int ufshcd_config_vreg(struct device *dev,
7448 struct ufs_vreg *vreg, bool on)
7449{
7450 int ret = 0;
7451 struct regulator *reg = vreg->reg;
7452 const char *name = vreg->name;
7453 int min_uV, uA_load;
7454
7455 BUG_ON(!vreg);
7456
7457 if (regulator_count_voltages(reg) > 0) {
7458 min_uV = on ? vreg->min_uV : 0;
7459 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7460 if (ret) {
7461 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
7462 __func__, name, ret);
7463 goto out;
7464 }
7465
7466 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007467 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7468 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007469 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007470 }
7471out:
7472 return ret;
7473}
7474
7475static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7476{
7477 int ret = 0;
7478
Yaniv Gardi60f01872016-03-10 17:37:11 +02007479 if (!vreg)
7480 goto out;
7481 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007482 goto out;
7483
7484 ret = ufshcd_config_vreg(dev, vreg, true);
7485 if (!ret)
7486 ret = regulator_enable(vreg->reg);
7487
7488 if (!ret)
7489 vreg->enabled = true;
7490 else
7491 dev_err(dev, "%s: %s enable failed, err=%d\n",
7492 __func__, vreg->name, ret);
7493out:
7494 return ret;
7495}
7496
7497static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7498{
7499 int ret = 0;
7500
Yaniv Gardi60f01872016-03-10 17:37:11 +02007501 if (!vreg)
7502 goto out;
7503 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007504 goto out;
7505
7506 ret = regulator_disable(vreg->reg);
7507
7508 if (!ret) {
7509 /* ignore errors on applying disable config */
7510 ufshcd_config_vreg(dev, vreg, false);
7511 vreg->enabled = false;
7512 } else {
7513 dev_err(dev, "%s: %s disable failed, err=%d\n",
7514 __func__, vreg->name, ret);
7515 }
7516out:
7517 return ret;
7518}
7519
7520static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7521{
7522 int ret = 0;
7523 struct device *dev = hba->dev;
7524 struct ufs_vreg_info *info = &hba->vreg_info;
7525
7526 if (!info)
7527 goto out;
7528
7529 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7530 if (ret)
7531 goto out;
7532
7533 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7534 if (ret)
7535 goto out;
7536
7537 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7538 if (ret)
7539 goto out;
7540
7541out:
7542 if (ret) {
7543 ufshcd_toggle_vreg(dev, info->vccq2, false);
7544 ufshcd_toggle_vreg(dev, info->vccq, false);
7545 ufshcd_toggle_vreg(dev, info->vcc, false);
7546 }
7547 return ret;
7548}
7549
Raviv Shvili6a771a62014-09-25 15:32:24 +03007550static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7551{
7552 struct ufs_vreg_info *info = &hba->vreg_info;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007553 int ret = 0;
Raviv Shvili6a771a62014-09-25 15:32:24 +03007554
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007555 if (info->vdd_hba) {
7556 ret = ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007557
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007558 if (!ret)
7559 ufshcd_vops_update_sec_cfg(hba, on);
7560 }
7561
7562 return ret;
Raviv Shvili6a771a62014-09-25 15:32:24 +03007563}
7564
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007565static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7566{
7567 int ret = 0;
7568
7569 if (!vreg)
7570 goto out;
7571
7572 vreg->reg = devm_regulator_get(dev, vreg->name);
7573 if (IS_ERR(vreg->reg)) {
7574 ret = PTR_ERR(vreg->reg);
7575 dev_err(dev, "%s: %s get failed, err=%d\n",
7576 __func__, vreg->name, ret);
7577 }
7578out:
7579 return ret;
7580}
7581
7582static int ufshcd_init_vreg(struct ufs_hba *hba)
7583{
7584 int ret = 0;
7585 struct device *dev = hba->dev;
7586 struct ufs_vreg_info *info = &hba->vreg_info;
7587
7588 if (!info)
7589 goto out;
7590
7591 ret = ufshcd_get_vreg(dev, info->vcc);
7592 if (ret)
7593 goto out;
7594
7595 ret = ufshcd_get_vreg(dev, info->vccq);
7596 if (ret)
7597 goto out;
7598
7599 ret = ufshcd_get_vreg(dev, info->vccq2);
7600out:
7601 return ret;
7602}
7603
Raviv Shvili6a771a62014-09-25 15:32:24 +03007604static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
7605{
7606 struct ufs_vreg_info *info = &hba->vreg_info;
7607
7608 if (info)
7609 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
7610
7611 return 0;
7612}
7613
Yaniv Gardi60f01872016-03-10 17:37:11 +02007614static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
7615{
7616 int ret = 0;
7617 struct ufs_vreg_info *info = &hba->vreg_info;
7618
7619 if (!info)
7620 goto out;
7621 else if (!info->vccq)
7622 goto out;
7623
7624 if (unused) {
7625 /* shut off the rail here */
7626 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
7627 /*
7628 * Mark this rail as no longer used, so it doesn't get enabled
7629 * later by mistake
7630 */
7631 if (!ret)
7632 info->vccq->unused = true;
7633 } else {
7634 /*
7635 * rail should have been already enabled hence just make sure
7636 * that unused flag is cleared.
7637 */
7638 info->vccq->unused = false;
7639 }
7640out:
7641 return ret;
7642}
7643
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007644static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
7645 bool skip_ref_clk, bool is_gating_context)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007646{
7647 int ret = 0;
7648 struct ufs_clk_info *clki;
7649 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007650 unsigned long flags;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007651 ktime_t start = ktime_get();
7652 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007653
7654 if (!head || list_empty(head))
7655 goto out;
7656
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007657 /*
7658 * vendor specific setup_clocks ops may depend on clocks managed by
7659 * this standard driver hence call the vendor specific setup_clocks
7660 * before disabling the clocks managed here.
7661 */
7662 if (!on) {
7663 ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context);
7664 if (ret)
7665 return ret;
7666 }
7667
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007668 list_for_each_entry(clki, head, list) {
7669 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007670 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
7671 continue;
7672
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007673 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007674 if (on && !clki->enabled) {
7675 ret = clk_prepare_enable(clki->clk);
7676 if (ret) {
7677 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
7678 __func__, clki->name, ret);
7679 goto out;
7680 }
7681 } else if (!on && clki->enabled) {
7682 clk_disable_unprepare(clki->clk);
7683 }
7684 clki->enabled = on;
7685 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
7686 clki->name, on ? "en" : "dis");
7687 }
7688 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007689
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007690 /*
7691 * vendor specific setup_clocks ops may depend on clocks managed by
7692 * this standard driver hence call the vendor specific setup_clocks
7693 * after enabling the clocks managed here.
7694 */
7695 if (on)
7696 ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context);
7697
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007698out:
7699 if (ret) {
7700 list_for_each_entry(clki, head, list) {
7701 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
7702 clk_disable_unprepare(clki->clk);
7703 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007704 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007705 spin_lock_irqsave(hba->host->host_lock, flags);
7706 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007707 trace_ufshcd_clk_gating(dev_name(hba->dev),
7708 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007709 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007710 /* restore the secure configuration as clocks are enabled */
7711 ufshcd_vops_update_sec_cfg(hba, true);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007712 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007713
7714 if (clk_state_changed)
7715 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
7716 (on ? "on" : "off"),
7717 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007718 return ret;
7719}
7720
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007721static int ufshcd_enable_clocks(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007722{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007723 return ufshcd_setup_clocks(hba, true, false, false);
7724}
7725
7726static int ufshcd_disable_clocks(struct ufs_hba *hba,
7727 bool is_gating_context)
7728{
7729 return ufshcd_setup_clocks(hba, false, false, is_gating_context);
7730}
7731
7732static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba,
7733 bool is_gating_context)
7734{
7735 return ufshcd_setup_clocks(hba, false, true, is_gating_context);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007736}
7737
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007738static int ufshcd_init_clocks(struct ufs_hba *hba)
7739{
7740 int ret = 0;
7741 struct ufs_clk_info *clki;
7742 struct device *dev = hba->dev;
7743 struct list_head *head = &hba->clk_list_head;
7744
7745 if (!head || list_empty(head))
7746 goto out;
7747
7748 list_for_each_entry(clki, head, list) {
7749 if (!clki->name)
7750 continue;
7751
7752 clki->clk = devm_clk_get(dev, clki->name);
7753 if (IS_ERR(clki->clk)) {
7754 ret = PTR_ERR(clki->clk);
7755 dev_err(dev, "%s: %s clk get failed, %d\n",
7756 __func__, clki->name, ret);
7757 goto out;
7758 }
7759
7760 if (clki->max_freq) {
7761 ret = clk_set_rate(clki->clk, clki->max_freq);
7762 if (ret) {
7763 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7764 __func__, clki->name,
7765 clki->max_freq, ret);
7766 goto out;
7767 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03007768 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007769 }
7770 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
7771 clki->name, clk_get_rate(clki->clk));
7772 }
7773out:
7774 return ret;
7775}
7776
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007777static int ufshcd_variant_hba_init(struct ufs_hba *hba)
7778{
7779 int err = 0;
7780
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007781 if (!hba->var || !hba->var->vops)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007782 goto out;
7783
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007784 err = ufshcd_vops_init(hba);
7785 if (err)
7786 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007787
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007788 err = ufshcd_vops_setup_regulators(hba, true);
7789 if (err)
7790 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007791
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007792 goto out;
7793
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007794out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007795 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007796out:
7797 if (err)
7798 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007799 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007800 return err;
7801}
7802
7803static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
7804{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007805 if (!hba->var || !hba->var->vops)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007806 return;
7807
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007808 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007809
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007810 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007811}
7812
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007813static int ufshcd_hba_init(struct ufs_hba *hba)
7814{
7815 int err;
7816
Raviv Shvili6a771a62014-09-25 15:32:24 +03007817 /*
7818 * Handle host controller power separately from the UFS device power
7819 * rails as it will help controlling the UFS host controller power
7820 * collapse easily which is different than UFS device power collapse.
7821 * Also, enable the host controller power before we go ahead with rest
7822 * of the initialization here.
7823 */
7824 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007825 if (err)
7826 goto out;
7827
Raviv Shvili6a771a62014-09-25 15:32:24 +03007828 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007829 if (err)
7830 goto out;
7831
Raviv Shvili6a771a62014-09-25 15:32:24 +03007832 err = ufshcd_init_clocks(hba);
7833 if (err)
7834 goto out_disable_hba_vreg;
7835
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007836 err = ufshcd_enable_clocks(hba);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007837 if (err)
7838 goto out_disable_hba_vreg;
7839
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007840 err = ufshcd_init_vreg(hba);
7841 if (err)
7842 goto out_disable_clks;
7843
7844 err = ufshcd_setup_vreg(hba, true);
7845 if (err)
7846 goto out_disable_clks;
7847
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007848 err = ufshcd_variant_hba_init(hba);
7849 if (err)
7850 goto out_disable_vreg;
7851
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007852 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007853 goto out;
7854
7855out_disable_vreg:
7856 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007857out_disable_clks:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007858 ufshcd_disable_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007859out_disable_hba_vreg:
7860 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007861out:
7862 return err;
7863}
7864
7865static void ufshcd_hba_exit(struct ufs_hba *hba)
7866{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007867 if (hba->is_powered) {
7868 ufshcd_variant_hba_exit(hba);
7869 ufshcd_setup_vreg(hba, false);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007870 if (ufshcd_is_clkscaling_supported(hba)) {
7871 ufshcd_suspend_clkscaling(hba);
7872 destroy_workqueue(hba->clk_scaling.workq);
7873 }
7874 ufshcd_disable_clocks(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007875 ufshcd_setup_hba_vreg(hba, false);
7876 hba->is_powered = false;
7877 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007878}
7879
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007880static int
7881ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307882{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007883 unsigned char cmd[6] = {REQUEST_SENSE,
7884 0,
7885 0,
7886 0,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007887 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007888 0};
7889 char *buffer;
7890 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307891
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007892 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007893 if (!buffer) {
7894 ret = -ENOMEM;
7895 goto out;
7896 }
7897
7898 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007899 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007900 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
7901 if (ret)
7902 pr_err("%s: failed with err %d\n", __func__, ret);
7903
7904 kfree(buffer);
7905out:
7906 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307907}
7908
7909/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007910 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
7911 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307912 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007913 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307914 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007915 * Returns 0 if requested power mode is set successfully
7916 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307917 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007918static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
7919 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307920{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007921 unsigned char cmd[6] = { START_STOP };
7922 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007923 struct scsi_device *sdp;
7924 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007925 int ret;
7926
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007927 spin_lock_irqsave(hba->host->host_lock, flags);
7928 sdp = hba->sdev_ufs_device;
7929 if (sdp) {
7930 ret = scsi_device_get(sdp);
7931 if (!ret && !scsi_device_online(sdp)) {
7932 ret = -ENODEV;
7933 scsi_device_put(sdp);
7934 }
7935 } else {
7936 ret = -ENODEV;
7937 }
7938 spin_unlock_irqrestore(hba->host->host_lock, flags);
7939
7940 if (ret)
7941 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007942
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307943 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007944 * If scsi commands fail, the scsi mid-layer schedules scsi error-
7945 * handling, which would wait for host to be resumed. Since we know
7946 * we are functional while we are here, skip host resume in error
7947 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307948 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007949 hba->host->eh_noresume = 1;
Subhash Jadavani23df2312016-12-16 12:54:30 -08007950 if (!hba->dev_info.is_ufs_dev_wlun_ua_cleared) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007951 ret = ufshcd_send_request_sense(hba, sdp);
7952 if (ret)
7953 goto out;
7954 /* Unit attention condition is cleared now */
Subhash Jadavani23df2312016-12-16 12:54:30 -08007955 hba->dev_info.is_ufs_dev_wlun_ua_cleared = 1;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007956 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307957
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007958 cmd[4] = pwr_mode << 4;
7959
7960 /*
7961 * Current function would be generally called from the power management
7962 * callbacks hence set the REQ_PM flag so that it doesn't resume the
7963 * already suspended childs.
7964 */
7965 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
7966 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
7967 if (ret) {
7968 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02007969 "START_STOP failed for power mode: %d, result %x\n",
7970 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01007971 if (driver_byte(ret) & DRIVER_SENSE)
7972 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007973 }
7974
7975 if (!ret)
7976 hba->curr_dev_pwr_mode = pwr_mode;
7977out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007978 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007979 hba->host->eh_noresume = 0;
7980 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307981}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307982
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007983static int ufshcd_link_state_transition(struct ufs_hba *hba,
7984 enum uic_link_state req_link_state,
7985 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307986{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007987 int ret = 0;
7988
7989 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307990 return 0;
7991
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007992 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7993 ret = ufshcd_uic_hibern8_enter(hba);
7994 if (!ret)
7995 ufshcd_set_link_hibern8(hba);
7996 else
7997 goto out;
7998 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307999 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008000 * If autobkops is enabled, link can't be turned off because
8001 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308002 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008003 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
8004 (!check_for_bkops || (check_for_bkops &&
8005 !hba->auto_bkops_enabled))) {
8006 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02008007 * Let's make sure that link is in low power mode, we are doing
8008 * this currently by putting the link in Hibern8. Otherway to
8009 * put the link in low power mode is to send the DME end point
8010 * to device and then send the DME reset command to local
8011 * unipro. But putting the link in hibern8 is much faster.
8012 */
8013 ret = ufshcd_uic_hibern8_enter(hba);
8014 if (ret)
8015 goto out;
8016 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008017 * Change controller state to "reset state" which
8018 * should also put the link in off/reset state
8019 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02008020 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008021 /*
8022 * TODO: Check if we need any delay to make sure that
8023 * controller is reset
8024 */
8025 ufshcd_set_link_off(hba);
8026 }
8027
8028out:
8029 return ret;
8030}
8031
8032static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
8033{
8034 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008035 * It seems some UFS devices may keep drawing more than sleep current
8036 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
8037 * To avoid this situation, add 2ms delay before putting these UFS
8038 * rails in LPM mode.
8039 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008040 if (!ufshcd_is_link_active(hba))
Yaniv Gardib799fdf2016-03-10 17:37:18 +02008041 usleep_range(2000, 2100);
8042
8043 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008044 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
8045 * power.
8046 *
8047 * If UFS device and link is in OFF state, all power supplies (VCC,
8048 * VCCQ, VCCQ2) can be turned off if power on write protect is not
8049 * required. If UFS link is inactive (Hibern8 or OFF state) and device
8050 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
8051 *
8052 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
8053 * in low power state which would save some power.
8054 */
8055 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8056 !hba->dev_info.is_lu_power_on_wp) {
8057 ufshcd_setup_vreg(hba, false);
8058 } else if (!ufshcd_is_ufs_dev_active(hba)) {
8059 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8060 if (!ufshcd_is_link_active(hba)) {
8061 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8062 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
8063 }
8064 }
8065}
8066
8067static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
8068{
8069 int ret = 0;
8070
8071 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
8072 !hba->dev_info.is_lu_power_on_wp) {
8073 ret = ufshcd_setup_vreg(hba, true);
8074 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008075 if (!ret && !ufshcd_is_link_active(hba)) {
8076 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
8077 if (ret)
8078 goto vcc_disable;
8079 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
8080 if (ret)
8081 goto vccq_lpm;
8082 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008083 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008084 }
8085 goto out;
8086
8087vccq_lpm:
8088 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
8089vcc_disable:
8090 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
8091out:
8092 return ret;
8093}
8094
8095static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
8096{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008097 if (ufshcd_is_link_off(hba) ||
8098 (ufshcd_is_link_hibern8(hba)
8099 && ufshcd_is_power_collapse_during_hibern8_allowed(hba)))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008100 ufshcd_setup_hba_vreg(hba, false);
8101}
8102
8103static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
8104{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008105 if (ufshcd_is_link_off(hba) ||
8106 (ufshcd_is_link_hibern8(hba)
8107 && ufshcd_is_power_collapse_during_hibern8_allowed(hba)))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008108 ufshcd_setup_hba_vreg(hba, true);
8109}
8110
8111/**
8112 * ufshcd_suspend - helper function for suspend operations
8113 * @hba: per adapter instance
8114 * @pm_op: desired low power operation type
8115 *
8116 * This function will try to put the UFS device and link into low power
8117 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
8118 * (System PM level).
8119 *
8120 * If this function is called during shutdown, it will make sure that
8121 * both UFS device and UFS link is powered off.
8122 *
8123 * NOTE: UFS device & link must be active before we enter in this function.
8124 *
8125 * Returns 0 for success and non-zero for failure
8126 */
8127static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8128{
8129 int ret = 0;
8130 enum ufs_pm_level pm_lvl;
8131 enum ufs_dev_pwr_mode req_dev_pwr_mode;
8132 enum uic_link_state req_link_state;
8133
8134 hba->pm_op_in_progress = 1;
8135 if (!ufshcd_is_shutdown_pm(pm_op)) {
8136 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
8137 hba->rpm_lvl : hba->spm_lvl;
8138 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
8139 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
8140 } else {
8141 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
8142 req_link_state = UIC_LINK_OFF_STATE;
8143 }
8144
8145 /*
8146 * If we can't transition into any of the low power modes
8147 * just gate the clocks.
8148 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008149 WARN_ON(hba->hibern8_on_idle.is_enabled &&
8150 hba->hibern8_on_idle.active_reqs);
8151 ufshcd_hold_all(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008152 hba->clk_gating.is_suspended = true;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008153 hba->hibern8_on_idle.is_suspended = true;
8154
8155 if (hba->clk_scaling.is_allowed) {
8156 cancel_work_sync(&hba->clk_scaling.suspend_work);
8157 cancel_work_sync(&hba->clk_scaling.resume_work);
8158 ufshcd_suspend_clkscaling(hba);
8159 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008160
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008161 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8162 req_link_state == UIC_LINK_ACTIVE_STATE) {
8163 goto disable_clks;
8164 }
8165
8166 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8167 (req_link_state == hba->uic_link_state))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008168 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008169
8170 /* UFS device & link must be active before we enter in this function */
8171 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8172 ret = -EINVAL;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008173 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008174 }
8175
8176 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008177 if (ufshcd_can_autobkops_during_suspend(hba)) {
8178 /*
8179 * The device is idle with no requests in the queue,
8180 * allow background operations if bkops status shows
8181 * that performance might be impacted.
8182 */
8183 ret = ufshcd_urgent_bkops(hba);
8184 if (ret)
8185 goto enable_gating;
8186 } else {
8187 /* make sure that auto bkops is disabled */
8188 ufshcd_disable_auto_bkops(hba);
8189 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008190 }
8191
8192 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
8193 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
8194 !ufshcd_is_runtime_pm(pm_op))) {
8195 /* ensure that bkops is disabled */
8196 ufshcd_disable_auto_bkops(hba);
8197 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8198 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008199 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008200 }
8201
8202 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
8203 if (ret)
8204 goto set_dev_active;
8205
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008206 if (ufshcd_is_link_hibern8(hba) &&
8207 ufshcd_is_hibern8_on_idle_allowed(hba))
8208 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
8209
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008210 ufshcd_vreg_set_lpm(hba);
8211
8212disable_clks:
8213 /*
8214 * Call vendor specific suspend callback. As these callbacks may access
8215 * vendor specific host controller register space call them before the
8216 * host clocks are ON.
8217 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008218 ret = ufshcd_vops_suspend(hba, pm_op);
8219 if (ret)
8220 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008221
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008222 if (!ufshcd_is_link_active(hba))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008223 ret = ufshcd_disable_clocks(hba, false);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008224 else
8225 /* If link is active, device ref_clk can't be switched off */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008226 ret = ufshcd_disable_clocks_skip_ref_clk(hba, false);
8227 if (ret)
8228 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008229
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008230 if (ufshcd_is_clkgating_allowed(hba)) {
8231 hba->clk_gating.state = CLKS_OFF;
8232 trace_ufshcd_clk_gating(dev_name(hba->dev),
8233 hba->clk_gating.state);
8234 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008235 /*
8236 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008237 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008238 */
8239 ufshcd_disable_irq(hba);
8240 /* Put the host controller in low power mode if possible */
8241 ufshcd_hba_vreg_set_lpm(hba);
8242 goto out;
8243
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008244set_link_active:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008245 if (hba->clk_scaling.is_allowed)
8246 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008247 ufshcd_vreg_set_hpm(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008248 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008249 ufshcd_set_link_active(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008250 } else if (ufshcd_is_link_off(hba)) {
8251 ufshcd_update_error_stats(hba, UFS_ERR_VOPS_SUSPEND);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008252 ufshcd_host_reset_and_restore(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008253 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008254set_dev_active:
8255 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8256 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008257enable_gating:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008258 if (hba->clk_scaling.is_allowed)
8259 ufshcd_resume_clkscaling(hba);
8260 hba->hibern8_on_idle.is_suspended = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008261 hba->clk_gating.is_suspended = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008262 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008263out:
8264 hba->pm_op_in_progress = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008265
8266 if (ret)
8267 ufshcd_update_error_stats(hba, UFS_ERR_SUSPEND);
8268
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008269 return ret;
8270}
8271
8272/**
8273 * ufshcd_resume - helper function for resume operations
8274 * @hba: per adapter instance
8275 * @pm_op: runtime PM or system PM
8276 *
8277 * This function basically brings the UFS device, UniPro link and controller
8278 * to active state.
8279 *
8280 * Returns 0 for success and non-zero for failure
8281 */
8282static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8283{
8284 int ret;
8285 enum uic_link_state old_link_state;
8286
8287 hba->pm_op_in_progress = 1;
8288 old_link_state = hba->uic_link_state;
8289
8290 ufshcd_hba_vreg_set_hpm(hba);
8291 /* Make sure clocks are enabled before accessing controller */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008292 ret = ufshcd_enable_clocks(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008293 if (ret)
8294 goto out;
8295
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008296 /* enable the host irq as host controller would be active soon */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008297 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008298
8299 ret = ufshcd_vreg_set_hpm(hba);
8300 if (ret)
8301 goto disable_irq_and_vops_clks;
8302
8303 /*
8304 * Call vendor specific resume callback. As these callbacks may access
8305 * vendor specific host controller register space call them when the
8306 * host clocks are ON.
8307 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008308 ret = ufshcd_vops_resume(hba, pm_op);
8309 if (ret)
8310 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008311
8312 if (ufshcd_is_link_hibern8(hba)) {
8313 ret = ufshcd_uic_hibern8_exit(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008314 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008315 ufshcd_set_link_active(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008316 if (ufshcd_is_hibern8_on_idle_allowed(hba))
8317 hba->hibern8_on_idle.state = HIBERN8_EXITED;
8318 } else {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008319 goto vendor_suspend;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008320 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008321 } else if (ufshcd_is_link_off(hba)) {
8322 ret = ufshcd_host_reset_and_restore(hba);
8323 /*
8324 * ufshcd_host_reset_and_restore() should have already
8325 * set the link state as active
8326 */
8327 if (ret || !ufshcd_is_link_active(hba))
8328 goto vendor_suspend;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008329 /* mark link state as hibern8 exited */
8330 if (ufshcd_is_hibern8_on_idle_allowed(hba))
8331 hba->hibern8_on_idle.state = HIBERN8_EXITED;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008332 }
8333
8334 if (!ufshcd_is_ufs_dev_active(hba)) {
8335 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8336 if (ret)
8337 goto set_old_link_state;
8338 }
8339
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008340 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8341 ufshcd_enable_auto_bkops(hba);
8342 else
8343 /*
8344 * If BKOPs operations are urgently needed at this moment then
8345 * keep auto-bkops enabled or else disable it.
8346 */
8347 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008348
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008349 hba->clk_gating.is_suspended = false;
8350 hba->hibern8_on_idle.is_suspended = false;
8351
8352 if (hba->clk_scaling.is_allowed)
8353 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008354
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008355 /* Schedule clock gating in case of no access to UFS device yet */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008356 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008357 goto out;
8358
8359set_old_link_state:
8360 ufshcd_link_state_transition(hba, old_link_state, 0);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008361 if (ufshcd_is_link_hibern8(hba) &&
8362 ufshcd_is_hibern8_on_idle_allowed(hba))
8363 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008364vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008365 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008366disable_vreg:
8367 ufshcd_vreg_set_lpm(hba);
8368disable_irq_and_vops_clks:
8369 ufshcd_disable_irq(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008370 if (hba->clk_scaling.is_allowed)
8371 ufshcd_suspend_clkscaling(hba);
8372 ufshcd_disable_clocks(hba, false);
8373 if (ufshcd_is_clkgating_allowed(hba))
8374 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008375out:
8376 hba->pm_op_in_progress = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008377
8378 if (ret)
8379 ufshcd_update_error_stats(hba, UFS_ERR_RESUME);
8380
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008381 return ret;
8382}
8383
8384/**
8385 * ufshcd_system_suspend - system suspend routine
8386 * @hba: per adapter instance
8387 * @pm_op: runtime PM or system PM
8388 *
8389 * Check the description of ufshcd_suspend() function for more details.
8390 *
8391 * Returns 0 for success and non-zero for failure
8392 */
8393int ufshcd_system_suspend(struct ufs_hba *hba)
8394{
8395 int ret = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008396 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008397
8398 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008399 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008400
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008401 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8402 hba->curr_dev_pwr_mode) &&
8403 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8404 hba->uic_link_state))
8405 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008406
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008407 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008408 /*
8409 * UFS device and/or UFS link low power states during runtime
8410 * suspend seems to be different than what is expected during
8411 * system suspend. Hence runtime resume the devic & link and
8412 * let the system suspend low power states to take effect.
8413 * TODO: If resume takes longer time, we might have optimize
8414 * it in future by not resuming everything if possible.
8415 */
8416 ret = ufshcd_runtime_resume(hba);
8417 if (ret)
8418 goto out;
8419 }
8420
8421 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8422out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008423 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8424 ktime_to_us(ktime_sub(ktime_get(), start)),
8425 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008426 if (!ret)
8427 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008428 return ret;
8429}
8430EXPORT_SYMBOL(ufshcd_system_suspend);
8431
8432/**
8433 * ufshcd_system_resume - system resume routine
8434 * @hba: per adapter instance
8435 *
8436 * Returns 0 for success and non-zero for failure
8437 */
8438
8439int ufshcd_system_resume(struct ufs_hba *hba)
8440{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008441 int ret = 0;
8442 ktime_t start = ktime_get();
8443
8444 if (!hba)
8445 return -EINVAL;
8446
8447 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008448 /*
8449 * Let the runtime resume take care of resuming
8450 * if runtime suspended.
8451 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008452 goto out;
8453 else
8454 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8455out:
8456 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8457 ktime_to_us(ktime_sub(ktime_get(), start)),
8458 hba->curr_dev_pwr_mode, hba->uic_link_state);
8459 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008460}
8461EXPORT_SYMBOL(ufshcd_system_resume);
8462
8463/**
8464 * ufshcd_runtime_suspend - runtime suspend routine
8465 * @hba: per adapter instance
8466 *
8467 * Check the description of ufshcd_suspend() function for more details.
8468 *
8469 * Returns 0 for success and non-zero for failure
8470 */
8471int ufshcd_runtime_suspend(struct ufs_hba *hba)
8472{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008473 int ret = 0;
8474 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008475
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008476 if (!hba)
8477 return -EINVAL;
8478
8479 if (!hba->is_powered)
8480 goto out;
8481 else
8482 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8483out:
8484 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8485 ktime_to_us(ktime_sub(ktime_get(), start)),
8486 hba->curr_dev_pwr_mode,
8487 hba->uic_link_state);
8488 return ret;
8489
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308490}
8491EXPORT_SYMBOL(ufshcd_runtime_suspend);
8492
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008493/**
8494 * ufshcd_runtime_resume - runtime resume routine
8495 * @hba: per adapter instance
8496 *
8497 * This function basically brings the UFS device, UniPro link and controller
8498 * to active state. Following operations are done in this function:
8499 *
8500 * 1. Turn on all the controller related clocks
8501 * 2. Bring the UniPro link out of Hibernate state
8502 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8503 * to active state.
8504 * 4. If auto-bkops is enabled on the device, disable it.
8505 *
8506 * So following would be the possible power state after this function return
8507 * successfully:
8508 * S1: UFS device in Active state with VCC rail ON
8509 * UniPro link in Active state
8510 * All the UFS/UniPro controller clocks are ON
8511 *
8512 * Returns 0 for success and non-zero for failure
8513 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308514int ufshcd_runtime_resume(struct ufs_hba *hba)
8515{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008516 int ret = 0;
8517 ktime_t start = ktime_get();
8518
8519 if (!hba)
8520 return -EINVAL;
8521
8522 if (!hba->is_powered)
8523 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008524 else
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008525 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8526out:
8527 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8528 ktime_to_us(ktime_sub(ktime_get(), start)),
8529 hba->curr_dev_pwr_mode,
8530 hba->uic_link_state);
8531 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308532}
8533EXPORT_SYMBOL(ufshcd_runtime_resume);
8534
8535int ufshcd_runtime_idle(struct ufs_hba *hba)
8536{
8537 return 0;
8538}
8539EXPORT_SYMBOL(ufshcd_runtime_idle);
8540
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008541static inline ssize_t ufshcd_pm_lvl_store(struct device *dev,
8542 struct device_attribute *attr,
8543 const char *buf, size_t count,
8544 bool rpm)
8545{
8546 struct ufs_hba *hba = dev_get_drvdata(dev);
8547 unsigned long flags, value;
8548
8549 if (kstrtoul(buf, 0, &value))
8550 return -EINVAL;
8551
8552 if (value >= UFS_PM_LVL_MAX)
8553 return -EINVAL;
8554
8555 spin_lock_irqsave(hba->host->host_lock, flags);
8556 if (rpm)
8557 hba->rpm_lvl = value;
8558 else
8559 hba->spm_lvl = value;
8560 ufshcd_apply_pm_quirks(hba);
8561 spin_unlock_irqrestore(hba->host->host_lock, flags);
8562 return count;
8563}
8564
8565static ssize_t ufshcd_rpm_lvl_show(struct device *dev,
8566 struct device_attribute *attr, char *buf)
8567{
8568 struct ufs_hba *hba = dev_get_drvdata(dev);
8569 int curr_len;
8570 u8 lvl;
8571
8572 curr_len = snprintf(buf, PAGE_SIZE,
8573 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
8574 hba->rpm_lvl,
8575 ufschd_ufs_dev_pwr_mode_to_string(
8576 ufs_pm_lvl_states[hba->rpm_lvl].dev_state),
8577 ufschd_uic_link_state_to_string(
8578 ufs_pm_lvl_states[hba->rpm_lvl].link_state));
8579
8580 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8581 "\nAll available Runtime PM levels info:\n");
8582 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
8583 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8584 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
8585 lvl,
8586 ufschd_ufs_dev_pwr_mode_to_string(
8587 ufs_pm_lvl_states[lvl].dev_state),
8588 ufschd_uic_link_state_to_string(
8589 ufs_pm_lvl_states[lvl].link_state));
8590
8591 return curr_len;
8592}
8593
8594static ssize_t ufshcd_rpm_lvl_store(struct device *dev,
8595 struct device_attribute *attr, const char *buf, size_t count)
8596{
8597 return ufshcd_pm_lvl_store(dev, attr, buf, count, true);
8598}
8599
8600static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba)
8601{
8602 hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show;
8603 hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store;
8604 sysfs_attr_init(&hba->rpm_lvl_attr.attr);
8605 hba->rpm_lvl_attr.attr.name = "rpm_lvl";
8606 hba->rpm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR;
8607 if (device_create_file(hba->dev, &hba->rpm_lvl_attr))
8608 dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n");
8609}
8610
8611static ssize_t ufshcd_spm_lvl_show(struct device *dev,
8612 struct device_attribute *attr, char *buf)
8613{
8614 struct ufs_hba *hba = dev_get_drvdata(dev);
8615 int curr_len;
8616 u8 lvl;
8617
8618 curr_len = snprintf(buf, PAGE_SIZE,
8619 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
8620 hba->spm_lvl,
8621 ufschd_ufs_dev_pwr_mode_to_string(
8622 ufs_pm_lvl_states[hba->spm_lvl].dev_state),
8623 ufschd_uic_link_state_to_string(
8624 ufs_pm_lvl_states[hba->spm_lvl].link_state));
8625
8626 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8627 "\nAll available System PM levels info:\n");
8628 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
8629 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8630 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
8631 lvl,
8632 ufschd_ufs_dev_pwr_mode_to_string(
8633 ufs_pm_lvl_states[lvl].dev_state),
8634 ufschd_uic_link_state_to_string(
8635 ufs_pm_lvl_states[lvl].link_state));
8636
8637 return curr_len;
8638}
8639
8640static ssize_t ufshcd_spm_lvl_store(struct device *dev,
8641 struct device_attribute *attr, const char *buf, size_t count)
8642{
8643 return ufshcd_pm_lvl_store(dev, attr, buf, count, false);
8644}
8645
8646static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba)
8647{
8648 hba->spm_lvl_attr.show = ufshcd_spm_lvl_show;
8649 hba->spm_lvl_attr.store = ufshcd_spm_lvl_store;
8650 sysfs_attr_init(&hba->spm_lvl_attr.attr);
8651 hba->spm_lvl_attr.attr.name = "spm_lvl";
8652 hba->spm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR;
8653 if (device_create_file(hba->dev, &hba->spm_lvl_attr))
8654 dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n");
8655}
8656
8657static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba)
8658{
8659 ufshcd_add_rpm_lvl_sysfs_nodes(hba);
8660 ufshcd_add_spm_lvl_sysfs_nodes(hba);
8661}
8662
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308663/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008664 * ufshcd_shutdown - shutdown routine
8665 * @hba: per adapter instance
8666 *
8667 * This function would power off both UFS device and UFS link.
8668 *
8669 * Returns 0 always to allow force shutdown even in case of errors.
8670 */
8671int ufshcd_shutdown(struct ufs_hba *hba)
8672{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008673 /*
8674 * TODO: This function should send the power down notification to
8675 * UFS device and then power off the UFS link. But we need to be sure
8676 * that there will not be any new UFS requests issued after this.
8677 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008678 return 0;
8679}
8680EXPORT_SYMBOL(ufshcd_shutdown);
8681
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07008682/*
8683 * Values permitted 0, 1, 2.
8684 * 0 -> Disable IO latency histograms (default)
8685 * 1 -> Enable IO latency histograms
8686 * 2 -> Zero out IO latency histograms
8687 */
8688static ssize_t
8689latency_hist_store(struct device *dev, struct device_attribute *attr,
8690 const char *buf, size_t count)
8691{
8692 struct ufs_hba *hba = dev_get_drvdata(dev);
8693 long value;
8694
8695 if (kstrtol(buf, 0, &value))
8696 return -EINVAL;
8697 if (value == BLK_IO_LAT_HIST_ZERO)
8698 blk_zero_latency_hist(&hba->io_lat_s);
8699 else if (value == BLK_IO_LAT_HIST_ENABLE ||
8700 value == BLK_IO_LAT_HIST_DISABLE)
8701 hba->latency_hist_enabled = value;
8702 return count;
8703}
8704
8705ssize_t
8706latency_hist_show(struct device *dev, struct device_attribute *attr,
8707 char *buf)
8708{
8709 struct ufs_hba *hba = dev_get_drvdata(dev);
8710
8711 return blk_latency_hist_show(&hba->io_lat_s, buf);
8712}
8713
8714static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR,
8715 latency_hist_show, latency_hist_store);
8716
8717static void
8718ufshcd_init_latency_hist(struct ufs_hba *hba)
8719{
8720 if (device_create_file(hba->dev, &dev_attr_latency_hist))
8721 dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n");
8722}
8723
8724static void
8725ufshcd_exit_latency_hist(struct ufs_hba *hba)
8726{
8727 device_create_file(hba->dev, &dev_attr_latency_hist);
8728}
8729
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008730/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308731 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308732 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308733 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308734 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308735void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308736{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05308737 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308738 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308739 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02008740 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308741
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008742 ufshcd_exit_clk_gating(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008743 ufshcd_exit_hibern8_on_idle(hba);
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07008744 ufshcd_exit_latency_hist(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008745 if (ufshcd_is_clkscaling_supported(hba)) {
8746 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Kyle Yan65be4a52016-10-31 15:05:00 -07008747 if (hba->devfreq)
8748 devfreq_remove_device(hba->devfreq);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008749 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008750 ufshcd_hba_exit(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008751 ufsdbg_remove_debugfs(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308752}
8753EXPORT_SYMBOL_GPL(ufshcd_remove);
8754
8755/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02008756 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
8757 * @hba: pointer to Host Bus Adapter (HBA)
8758 */
8759void ufshcd_dealloc_host(struct ufs_hba *hba)
8760{
8761 scsi_host_put(hba->host);
8762}
8763EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
8764
8765/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008766 * ufshcd_set_dma_mask - Set dma mask based on the controller
8767 * addressing capability
8768 * @hba: per adapter instance
8769 *
8770 * Returns 0 for success, non-zero for failure
8771 */
8772static int ufshcd_set_dma_mask(struct ufs_hba *hba)
8773{
8774 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
8775 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
8776 return 0;
8777 }
8778 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
8779}
8780
8781/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008782 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308783 * @dev: pointer to device handle
8784 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308785 * Returns 0 on success, non-zero value on failure
8786 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008787int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308788{
8789 struct Scsi_Host *host;
8790 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008791 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308792
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308793 if (!dev) {
8794 dev_err(dev,
8795 "Invalid memory reference for dev is NULL\n");
8796 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308797 goto out_error;
8798 }
8799
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308800 host = scsi_host_alloc(&ufshcd_driver_template,
8801 sizeof(struct ufs_hba));
8802 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308803 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308804 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308805 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308806 }
8807 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308808 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308809 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008810 *hba_handle = hba;
8811
8812out_error:
8813 return err;
8814}
8815EXPORT_SYMBOL(ufshcd_alloc_host);
8816
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008817/**
8818 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
8819 * @hba: per adapter instance
8820 * @scale_up: True if scaling up and false if scaling down
8821 *
8822 * Returns true if scaling is required, false otherwise.
8823 */
8824static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
8825 bool scale_up)
Sahitya Tummala856b3482014-09-25 15:32:34 +03008826{
Sahitya Tummala856b3482014-09-25 15:32:34 +03008827 struct ufs_clk_info *clki;
8828 struct list_head *head = &hba->clk_list_head;
8829
8830 if (!head || list_empty(head))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008831 return false;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02008832
Sahitya Tummala856b3482014-09-25 15:32:34 +03008833 list_for_each_entry(clki, head, list) {
8834 if (!IS_ERR_OR_NULL(clki->clk)) {
8835 if (scale_up && clki->max_freq) {
8836 if (clki->curr_freq == clki->max_freq)
8837 continue;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008838 return true;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008839 } else if (!scale_up && clki->min_freq) {
8840 if (clki->curr_freq == clki->min_freq)
8841 continue;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008842 return true;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008843 }
8844 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03008845 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02008846
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008847 return false;
8848}
Yaniv Gardif06fcc72015-10-28 13:15:51 +02008849
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008850/**
8851 * ufshcd_scale_gear - scale up/down UFS gear
8852 * @hba: per adapter instance
8853 * @scale_up: True for scaling up gear and false for scaling down
8854 *
8855 * Returns 0 for success,
8856 * Returns -EBUSY if scaling can't happen at this time
8857 * Returns non-zero for any other errors
8858 */
8859static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
8860{
8861 int ret = 0;
8862 struct ufs_pa_layer_attr new_pwr_info;
8863 u32 scale_down_gear = ufshcd_vops_get_scale_down_gear(hba);
8864
8865 BUG_ON(!hba->clk_scaling.saved_pwr_info.is_valid);
8866
8867 if (scale_up) {
8868 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
8869 sizeof(struct ufs_pa_layer_attr));
8870 } else {
8871 memcpy(&new_pwr_info, &hba->pwr_info,
8872 sizeof(struct ufs_pa_layer_attr));
8873
8874 if (hba->pwr_info.gear_tx > scale_down_gear
8875 || hba->pwr_info.gear_rx > scale_down_gear) {
8876 /* save the current power mode */
8877 memcpy(&hba->clk_scaling.saved_pwr_info.info,
8878 &hba->pwr_info,
8879 sizeof(struct ufs_pa_layer_attr));
8880
8881 /* scale down gear */
8882 new_pwr_info.gear_tx = scale_down_gear;
8883 new_pwr_info.gear_rx = scale_down_gear;
Subhash Jadavani4f0df17b2016-12-16 13:19:27 -08008884 if (!(hba->dev_info.quirks & UFS_DEVICE_NO_FASTAUTO)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008885 new_pwr_info.pwr_tx = FASTAUTO_MODE;
8886 new_pwr_info.pwr_rx = FASTAUTO_MODE;
8887 }
8888 }
8889 }
8890
8891 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
8892
8893 if (ret)
8894 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d), scale_up = %d",
8895 __func__, ret,
8896 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
8897 new_pwr_info.gear_tx, new_pwr_info.gear_rx,
8898 scale_up);
8899
Sahitya Tummala856b3482014-09-25 15:32:34 +03008900 return ret;
8901}
8902
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008903static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
8904{
8905 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
8906 int ret = 0;
8907 /*
8908 * make sure that there are no outstanding requests when
8909 * clock scaling is in progress
8910 */
8911 ufshcd_scsi_block_requests(hba);
8912 down_write(&hba->clk_scaling_lock);
8913 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
8914 ret = -EBUSY;
8915 up_write(&hba->clk_scaling_lock);
8916 ufshcd_scsi_unblock_requests(hba);
8917 }
8918
8919 return ret;
8920}
8921
8922static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
8923{
8924 up_write(&hba->clk_scaling_lock);
8925 ufshcd_scsi_unblock_requests(hba);
8926}
8927
8928/**
8929 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
8930 * @hba: per adapter instance
8931 * @scale_up: True for scaling up and false for scalin down
8932 *
8933 * Returns 0 for success,
8934 * Returns -EBUSY if scaling can't happen at this time
8935 * Returns non-zero for any other errors
8936 */
8937static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
8938{
8939 int ret = 0;
8940
8941 /* let's not get into low power until clock scaling is completed */
8942 ufshcd_hold_all(hba);
8943
8944 ret = ufshcd_clock_scaling_prepare(hba);
8945 if (ret)
8946 goto out;
8947
8948 /* scale down the gear before scaling down clocks */
8949 if (!scale_up) {
8950 ret = ufshcd_scale_gear(hba, false);
8951 if (ret)
8952 goto clk_scaling_unprepare;
8953 }
8954
8955 ret = ufshcd_scale_clks(hba, scale_up);
8956 if (ret)
8957 goto scale_up_gear;
8958
8959 /* scale up the gear after scaling up clocks */
8960 if (scale_up) {
8961 ret = ufshcd_scale_gear(hba, true);
8962 if (ret) {
8963 ufshcd_scale_clks(hba, false);
8964 goto clk_scaling_unprepare;
8965 }
8966 }
8967
8968 if (!ret) {
8969 hba->clk_scaling.is_scaled_up = scale_up;
8970 if (scale_up)
8971 hba->clk_gating.delay_ms =
8972 hba->clk_gating.delay_ms_perf;
8973 else
8974 hba->clk_gating.delay_ms =
8975 hba->clk_gating.delay_ms_pwr_save;
8976 }
8977
8978 goto clk_scaling_unprepare;
8979
8980scale_up_gear:
8981 if (!scale_up)
8982 ufshcd_scale_gear(hba, true);
8983clk_scaling_unprepare:
8984 ufshcd_clock_scaling_unprepare(hba);
8985out:
8986 ufshcd_release_all(hba);
8987 return ret;
8988}
8989
8990static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
8991{
8992 unsigned long flags;
8993
8994 devfreq_suspend_device(hba->devfreq);
8995 spin_lock_irqsave(hba->host->host_lock, flags);
8996 hba->clk_scaling.window_start_t = 0;
8997 spin_unlock_irqrestore(hba->host->host_lock, flags);
8998}
8999
9000static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
9001{
9002 unsigned long flags;
9003 bool suspend = false;
9004
9005 if (!ufshcd_is_clkscaling_supported(hba))
9006 return;
9007
9008 spin_lock_irqsave(hba->host->host_lock, flags);
9009 if (!hba->clk_scaling.is_suspended) {
9010 suspend = true;
9011 hba->clk_scaling.is_suspended = true;
9012 }
9013 spin_unlock_irqrestore(hba->host->host_lock, flags);
9014
9015 if (suspend)
9016 __ufshcd_suspend_clkscaling(hba);
9017}
9018
9019static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
9020{
9021 unsigned long flags;
9022 bool resume = false;
9023
9024 if (!ufshcd_is_clkscaling_supported(hba))
9025 return;
9026
9027 spin_lock_irqsave(hba->host->host_lock, flags);
9028 if (hba->clk_scaling.is_suspended) {
9029 resume = true;
9030 hba->clk_scaling.is_suspended = false;
9031 }
9032 spin_unlock_irqrestore(hba->host->host_lock, flags);
9033
9034 if (resume)
9035 devfreq_resume_device(hba->devfreq);
9036}
9037
9038static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
9039 struct device_attribute *attr, char *buf)
9040{
9041 struct ufs_hba *hba = dev_get_drvdata(dev);
9042
9043 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
9044}
9045
9046static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
9047 struct device_attribute *attr, const char *buf, size_t count)
9048{
9049 struct ufs_hba *hba = dev_get_drvdata(dev);
9050 u32 value;
9051 int err;
9052
9053 if (kstrtou32(buf, 0, &value))
9054 return -EINVAL;
9055
9056 value = !!value;
9057 if (value == hba->clk_scaling.is_allowed)
9058 goto out;
9059
9060 pm_runtime_get_sync(hba->dev);
9061 ufshcd_hold(hba, false);
9062
9063 cancel_work_sync(&hba->clk_scaling.suspend_work);
9064 cancel_work_sync(&hba->clk_scaling.resume_work);
9065
9066 hba->clk_scaling.is_allowed = value;
9067
9068 if (value) {
9069 ufshcd_resume_clkscaling(hba);
9070 } else {
9071 ufshcd_suspend_clkscaling(hba);
9072 err = ufshcd_devfreq_scale(hba, true);
9073 if (err)
9074 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
9075 __func__, err);
9076 }
9077
9078 ufshcd_release(hba, false);
9079 pm_runtime_put_sync(hba->dev);
9080out:
9081 return count;
9082}
9083
9084static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
9085{
9086 struct ufs_hba *hba = container_of(work, struct ufs_hba,
9087 clk_scaling.suspend_work);
9088 unsigned long irq_flags;
9089
9090 spin_lock_irqsave(hba->host->host_lock, irq_flags);
9091 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
9092 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9093 return;
9094 }
9095 hba->clk_scaling.is_suspended = true;
9096 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9097
9098 __ufshcd_suspend_clkscaling(hba);
9099}
9100
9101static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
9102{
9103 struct ufs_hba *hba = container_of(work, struct ufs_hba,
9104 clk_scaling.resume_work);
9105 unsigned long irq_flags;
9106
9107 spin_lock_irqsave(hba->host->host_lock, irq_flags);
9108 if (!hba->clk_scaling.is_suspended) {
9109 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9110 return;
9111 }
9112 hba->clk_scaling.is_suspended = false;
9113 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9114
9115 devfreq_resume_device(hba->devfreq);
9116}
9117
Sahitya Tummala856b3482014-09-25 15:32:34 +03009118static int ufshcd_devfreq_target(struct device *dev,
9119 unsigned long *freq, u32 flags)
9120{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009121 int ret = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009122 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009123 unsigned long irq_flags;
9124 ktime_t start;
9125 bool scale_up, sched_clk_scaling_suspend_work = false;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009126
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009127 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03009128 return -EINVAL;
9129
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009130 if ((*freq > 0) && (*freq < UINT_MAX)) {
9131 dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq);
9132 return -EINVAL;
9133 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03009134
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009135 spin_lock_irqsave(hba->host->host_lock, irq_flags);
9136 if (ufshcd_eh_in_progress(hba)) {
9137 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9138 return 0;
9139 }
9140
9141 if (!hba->clk_scaling.active_reqs)
9142 sched_clk_scaling_suspend_work = true;
9143
9144 scale_up = (*freq == UINT_MAX) ? true : false;
9145 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
9146 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9147 ret = 0;
9148 goto out; /* no state change required */
9149 }
9150 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
9151
9152 start = ktime_get();
9153 ret = ufshcd_devfreq_scale(hba, scale_up);
9154 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
9155 (scale_up ? "up" : "down"),
9156 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
9157
9158out:
9159 if (sched_clk_scaling_suspend_work)
9160 queue_work(hba->clk_scaling.workq,
9161 &hba->clk_scaling.suspend_work);
9162
9163 return ret;
Sahitya Tummala856b3482014-09-25 15:32:34 +03009164}
9165
9166static int ufshcd_devfreq_get_dev_status(struct device *dev,
9167 struct devfreq_dev_status *stat)
9168{
9169 struct ufs_hba *hba = dev_get_drvdata(dev);
9170 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
9171 unsigned long flags;
9172
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009173 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03009174 return -EINVAL;
9175
9176 memset(stat, 0, sizeof(*stat));
9177
9178 spin_lock_irqsave(hba->host->host_lock, flags);
9179 if (!scaling->window_start_t)
9180 goto start_window;
9181
9182 if (scaling->is_busy_started)
9183 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
9184 scaling->busy_start_t));
9185
9186 stat->total_time = jiffies_to_usecs((long)jiffies -
9187 (long)scaling->window_start_t);
9188 stat->busy_time = scaling->tot_busy_t;
9189start_window:
9190 scaling->window_start_t = jiffies;
9191 scaling->tot_busy_t = 0;
9192
9193 if (hba->outstanding_reqs) {
9194 scaling->busy_start_t = ktime_get();
9195 scaling->is_busy_started = true;
9196 } else {
9197 scaling->busy_start_t = ktime_set(0, 0);
9198 scaling->is_busy_started = false;
9199 }
9200 spin_unlock_irqrestore(hba->host->host_lock, flags);
9201 return 0;
9202}
9203
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009204#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
9205static struct devfreq_simple_ondemand_data ufshcd_ondemand_data = {
9206 .upthreshold = 35,
9207 .downdifferential = 30,
9208 .simple_scaling = 1,
9209};
9210
9211static void *gov_data = &ufshcd_ondemand_data;
9212#else
9213static void *gov_data;
9214#endif
9215
Sahitya Tummala856b3482014-09-25 15:32:34 +03009216static struct devfreq_dev_profile ufs_devfreq_profile = {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009217 .polling_ms = 40,
Sahitya Tummala856b3482014-09-25 15:32:34 +03009218 .target = ufshcd_devfreq_target,
9219 .get_dev_status = ufshcd_devfreq_get_dev_status,
9220};
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009221static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
9222{
9223 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
9224 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
9225 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
9226 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
9227 hba->clk_scaling.enable_attr.attr.mode = S_IRUGO | S_IWUSR;
9228 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
9229 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
9230}
Sahitya Tummala856b3482014-09-25 15:32:34 +03009231
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009232static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
9233{
9234 struct device *dev = hba->dev;
9235 int ret;
9236
9237 ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
9238 &hba->lanes_per_direction);
9239 if (ret) {
9240 dev_dbg(hba->dev,
9241 "%s: failed to read lanes-per-direction, ret=%d\n",
9242 __func__, ret);
9243 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
9244 }
9245}
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009246/**
9247 * ufshcd_init - Driver initialization routine
9248 * @hba: per-adapter instance
9249 * @mmio_base: base register address
9250 * @irq: Interrupt line of device
9251 * Returns 0 on success, non-zero value on failure
9252 */
9253int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9254{
9255 int err;
9256 struct Scsi_Host *host = hba->host;
9257 struct device *dev = hba->dev;
9258
9259 if (!mmio_base) {
9260 dev_err(hba->dev,
9261 "Invalid memory reference for mmio_base is NULL\n");
9262 err = -ENODEV;
9263 goto out_error;
9264 }
9265
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309266 hba->mmio_base = mmio_base;
9267 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309268
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009269 ufshcd_init_lanes_per_dir(hba);
9270
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009271 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009272 if (err)
9273 goto out_error;
9274
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309275 /* Read capabilities registers */
9276 ufshcd_hba_capabilities(hba);
9277
9278 /* Get UFS version supported by the controller */
9279 hba->ufs_version = ufshcd_get_ufs_version(hba);
9280
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009281 /* print error message if ufs_version is not valid */
9282 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
9283 (hba->ufs_version != UFSHCI_VERSION_11) &&
9284 (hba->ufs_version != UFSHCI_VERSION_20) &&
9285 (hba->ufs_version != UFSHCI_VERSION_21))
9286 dev_err(hba->dev, "invalid UFS version 0x%x\n",
9287 hba->ufs_version);
9288
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309289 /* Get Interrupt bit mask per version */
9290 hba->intr_mask = ufshcd_get_intr_mask(hba);
9291
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009292 /* Enable debug prints */
9293 hba->ufshcd_dbg_print = DEFAULT_UFSHCD_DBG_PRINT_EN;
9294
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009295 err = ufshcd_set_dma_mask(hba);
9296 if (err) {
9297 dev_err(hba->dev, "set dma mask failed\n");
9298 goto out_disable;
9299 }
9300
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309301 /* Allocate memory for host memory space */
9302 err = ufshcd_memory_alloc(hba);
9303 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309304 dev_err(hba->dev, "Memory allocation failed\n");
9305 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309306 }
9307
9308 /* Configure LRB */
9309 ufshcd_host_memory_configure(hba);
9310
9311 host->can_queue = hba->nutrs;
9312 host->cmd_per_lun = hba->nutrs;
9313 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03009314 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309315 host->max_channel = UFSHCD_MAX_CHANNEL;
9316 host->unique_id = host->host_no;
9317 host->max_cmd_len = MAX_CDB_SIZE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009318 host->set_dbd_for_caching = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309319
Dolev Raviv7eb584d2014-09-25 15:32:31 +03009320 hba->max_pwr_info.is_valid = false;
9321
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309322 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05309323 init_waitqueue_head(&hba->tm_wq);
9324 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309325
9326 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05309327 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309328 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309329
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309330 /* Initialize UIC command mutex */
9331 mutex_init(&hba->uic_cmd_mutex);
9332
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309333 /* Initialize mutex for device management commands */
9334 mutex_init(&hba->dev_cmd.lock);
9335
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009336 init_rwsem(&hba->clk_scaling_lock);
9337
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309338 /* Initialize device management tag acquire wait queue */
9339 init_waitqueue_head(&hba->dev_cmd.tag_wq);
9340
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009341 ufshcd_init_clk_gating(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009342 ufshcd_init_hibern8_on_idle(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02009343
9344 /*
9345 * In order to avoid any spurious interrupt immediately after
9346 * registering UFS controller interrupt handler, clear any pending UFS
9347 * interrupt status and disable all the UFS interrupts.
9348 */
9349 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9350 REG_INTERRUPT_STATUS);
9351 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9352 /*
9353 * Make sure that UFS interrupts are disabled and any pending interrupt
9354 * status is cleared before registering UFS interrupt handler.
9355 */
9356 mb();
9357
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309358 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09009359 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309360 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309361 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009362 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009363 } else {
9364 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309365 }
9366
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309367 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309368 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309369 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009370 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309371 }
9372
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309373 /* Host controller enable */
9374 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309375 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309376 dev_err(hba->dev, "Host controller enable failed\n");
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009377 ufshcd_print_host_regs(hba);
9378 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309379 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309380 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309381
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009382 if (ufshcd_is_clkscaling_supported(hba)) {
9383 char wq_name[sizeof("ufs_clkscaling_00")];
9384
Sahitya Tummala856b3482014-09-25 15:32:34 +03009385 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009386 "simple_ondemand", gov_data);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009387 if (IS_ERR(hba->devfreq)) {
9388 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
9389 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00009390 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009391 goto out_remove_scsi_host;
9392 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009393 hba->clk_scaling.is_suspended = false;
9394
9395 INIT_WORK(&hba->clk_scaling.suspend_work,
9396 ufshcd_clk_scaling_suspend_work);
9397 INIT_WORK(&hba->clk_scaling.resume_work,
9398 ufshcd_clk_scaling_resume_work);
9399
9400 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d",
9401 host->host_no);
9402 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
9403
Sahitya Tummala856b3482014-09-25 15:32:34 +03009404 /* Suspend devfreq until the UFS device is detected */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009405 ufshcd_suspend_clkscaling(hba);
9406 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009407 }
9408
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009409 /*
9410 * If rpm_lvl and and spm_lvl are not already set to valid levels,
9411 * set the default power management level for UFS runtime and system
9412 * suspend. Default power saving mode selected is keeping UFS link in
9413 * Hibern8 state and UFS device in sleep.
9414 */
9415 if (!ufshcd_is_valid_pm_lvl(hba->rpm_lvl))
9416 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9417 UFS_SLEEP_PWR_MODE,
9418 UIC_LINK_HIBERN8_STATE);
9419 if (!ufshcd_is_valid_pm_lvl(hba->spm_lvl))
9420 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9421 UFS_SLEEP_PWR_MODE,
9422 UIC_LINK_HIBERN8_STATE);
9423
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05309424 /* Hold auto suspend until async scan completes */
9425 pm_runtime_get_sync(dev);
9426
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07009427 ufshcd_init_latency_hist(hba);
9428
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009429 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009430 * We are assuming that device wasn't put in sleep/power-down
9431 * state exclusively during the boot stage before kernel.
9432 * This assumption helps avoid doing link startup twice during
9433 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009434 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009435 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009436
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309437 async_schedule(ufshcd_async_scan, hba);
9438
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009439 ufsdbg_add_debugfs(hba);
9440
9441 ufshcd_add_sysfs_nodes(hba);
9442
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309443 return 0;
9444
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309445out_remove_scsi_host:
9446 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009447exit_gating:
9448 ufshcd_exit_clk_gating(hba);
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07009449 ufshcd_exit_latency_hist(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309450out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009451 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009452 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309453out_error:
9454 return err;
9455}
9456EXPORT_SYMBOL_GPL(ufshcd_init);
9457
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309458MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9459MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05309460MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309461MODULE_LICENSE("GPL");
9462MODULE_VERSION(UFSHCD_DRIVER_VERSION);