blob: 9a136bae5ab90b50811d1dcf5e9fb92207e6ce16 [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>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053045#include "ufshcd.h"
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070046#include "ufshci.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020047#include "ufs_quirks.h"
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -070048#include "ufs-debugfs.h"
49
50#define CREATE_TRACE_POINTS
51#include <trace/events/ufs.h>
52
53#ifdef CONFIG_DEBUG_FS
54
55static int ufshcd_tag_req_type(struct request *rq)
56{
57 int rq_type = TS_WRITE;
58
59 if (!rq || !(rq->cmd_type & REQ_TYPE_FS))
60 rq_type = TS_NOT_SUPPORTED;
61 else if (rq->cmd_flags & REQ_PREFLUSH)
62 rq_type = TS_FLUSH;
63 else if (rq_data_dir(rq) == READ)
64 rq_type = (rq->cmd_flags & REQ_URGENT) ?
65 TS_URGENT_READ : TS_READ;
66 else if (rq->cmd_flags & REQ_URGENT)
67 rq_type = TS_URGENT_WRITE;
68
69 return rq_type;
70}
71
72static void ufshcd_update_error_stats(struct ufs_hba *hba, int type)
73{
74 ufsdbg_set_err_state(hba);
75 if (type < UFS_ERR_MAX)
76 hba->ufs_stats.err_stats[type]++;
77}
78
79static void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag)
80{
81 struct request *rq =
82 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->request : NULL;
83 u64 **tag_stats = hba->ufs_stats.tag_stats;
84 int rq_type;
85
86 if (!hba->ufs_stats.enabled)
87 return;
88
89 tag_stats[tag][TS_TAG]++;
90 if (!rq || !(rq->cmd_type & REQ_TYPE_FS))
91 return;
92
93 WARN_ON(hba->ufs_stats.q_depth > hba->nutrs);
94 rq_type = ufshcd_tag_req_type(rq);
95 if (!(rq_type < 0 || rq_type > TS_NUM_STATS))
96 tag_stats[hba->ufs_stats.q_depth++][rq_type]++;
97}
98
99static void ufshcd_update_tag_stats_completion(struct ufs_hba *hba,
100 struct scsi_cmnd *cmd)
101{
102 struct request *rq = cmd ? cmd->request : NULL;
103
104 if (rq && rq->cmd_type & REQ_TYPE_FS)
105 hba->ufs_stats.q_depth--;
106}
107
108static void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
109{
110 int rq_type;
111 struct request *rq = lrbp->cmd ? lrbp->cmd->request : NULL;
112 s64 delta = ktime_us_delta(lrbp->complete_time_stamp,
113 lrbp->issue_time_stamp);
114
115 /* update general request statistics */
116 if (hba->ufs_stats.req_stats[TS_TAG].count == 0)
117 hba->ufs_stats.req_stats[TS_TAG].min = delta;
118 hba->ufs_stats.req_stats[TS_TAG].count++;
119 hba->ufs_stats.req_stats[TS_TAG].sum += delta;
120 if (delta > hba->ufs_stats.req_stats[TS_TAG].max)
121 hba->ufs_stats.req_stats[TS_TAG].max = delta;
122 if (delta < hba->ufs_stats.req_stats[TS_TAG].min)
123 hba->ufs_stats.req_stats[TS_TAG].min = delta;
124
125 rq_type = ufshcd_tag_req_type(rq);
126 if (rq_type == TS_NOT_SUPPORTED)
127 return;
128
129 /* update request type specific statistics */
130 if (hba->ufs_stats.req_stats[rq_type].count == 0)
131 hba->ufs_stats.req_stats[rq_type].min = delta;
132 hba->ufs_stats.req_stats[rq_type].count++;
133 hba->ufs_stats.req_stats[rq_type].sum += delta;
134 if (delta > hba->ufs_stats.req_stats[rq_type].max)
135 hba->ufs_stats.req_stats[rq_type].max = delta;
136 if (delta < hba->ufs_stats.req_stats[rq_type].min)
137 hba->ufs_stats.req_stats[rq_type].min = delta;
138}
139
140static void
141ufshcd_update_query_stats(struct ufs_hba *hba, enum query_opcode opcode, u8 idn)
142{
143 if (opcode < UPIU_QUERY_OPCODE_MAX && idn < MAX_QUERY_IDN)
144 hba->ufs_stats.query_stats_arr[opcode][idn]++;
145}
146
147#else
148static inline void ufshcd_update_tag_stats(struct ufs_hba *hba, int tag)
149{
150}
151
152static inline void ufshcd_update_tag_stats_completion(struct ufs_hba *hba,
153 struct scsi_cmnd *cmd)
154{
155}
156
157static inline void ufshcd_update_error_stats(struct ufs_hba *hba, int type)
158{
159}
160
161static inline
162void update_req_stats(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
163{
164}
165
166static inline
167void ufshcd_update_query_stats(struct ufs_hba *hba,
168 enum query_opcode opcode, u8 idn)
169{
170}
171#endif
172
173#define UFSHCD_REQ_SENSE_SIZE 18
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530174
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530175#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
176 UTP_TASK_REQ_COMPL |\
177 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530178/* UIC command timeout, unit: ms */
179#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530180
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530181/* NOP OUT retries waiting for NOP IN response */
182#define NOP_OUT_RETRIES 10
183/* Timeout after 30 msecs if NOP OUT hangs without response */
184#define NOP_OUT_TIMEOUT 30 /* msecs */
185
Dolev Raviv68078d52013-07-30 00:35:58 +0530186/* Query request retries */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700187#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +0530188/* Query request timeout */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700189#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +0530190
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530191/* Task management command timeout */
192#define TM_CMD_TIMEOUT 100 /* msecs */
193
Yaniv Gardi64238fb2016-02-01 15:02:43 +0200194/* maximum number of retries for a general UIC command */
195#define UFS_UIC_COMMAND_RETRIES 3
196
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300197/* maximum number of link-startup retries */
198#define DME_LINKSTARTUP_RETRIES 3
199
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +0200200/* Maximum retries for Hibern8 enter */
201#define UIC_HIBERN8_ENTER_RETRIES 3
202
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300203/* maximum number of reset retries before giving up */
204#define MAX_HOST_RESET_RETRIES 5
205
Dolev Raviv68078d52013-07-30 00:35:58 +0530206/* Expose the flag value from utp_upiu_query.value */
207#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
208
Seungwon Jeon7d568652013-08-31 21:40:20 +0530209/* Interrupt aggregation default timeout, unit: 40us */
210#define INT_AGGR_DEF_TO 0x02
211
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700212/* default value of auto suspend is 3 seconds */
213#define UFSHCD_AUTO_SUSPEND_DELAY_MS 3000 /* millisecs */
214
215#define UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE 10
216#define UFSHCD_CLK_GATING_DELAY_MS_PERF 50
217
218/* IOCTL opcode for command - ufs set device read only */
219#define UFS_IOCTL_BLKROSET BLKROSET
220
221#define UFSHCD_DEFAULT_LANES_PER_DIRECTION 2
222
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +0300223#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
224 ({ \
225 int _ret; \
226 if (_on) \
227 _ret = ufshcd_enable_vreg(_dev, _vreg); \
228 else \
229 _ret = ufshcd_disable_vreg(_dev, _vreg); \
230 _ret; \
231 })
232
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700233#define ufshcd_hex_dump(prefix_str, buf, len) \
234print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false)
235
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300236static u32 ufs_query_desc_max_size[] = {
237 QUERY_DESC_DEVICE_MAX_SIZE,
238 QUERY_DESC_CONFIGURAION_MAX_SIZE,
239 QUERY_DESC_UNIT_MAX_SIZE,
240 QUERY_DESC_RFU_MAX_SIZE,
241 QUERY_DESC_INTERCONNECT_MAX_SIZE,
242 QUERY_DESC_STRING_MAX_SIZE,
243 QUERY_DESC_RFU_MAX_SIZE,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700244 QUERY_DESC_GEOMETRY_MAZ_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300245 QUERY_DESC_POWER_MAX_SIZE,
246 QUERY_DESC_RFU_MAX_SIZE,
247};
248
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530249enum {
250 UFSHCD_MAX_CHANNEL = 0,
251 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530252 UFSHCD_CMD_PER_LUN = 32,
253 UFSHCD_CAN_QUEUE = 32,
254};
255
256/* UFSHCD states */
257enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530258 UFSHCD_STATE_RESET,
259 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530260 UFSHCD_STATE_OPERATIONAL,
261};
262
263/* UFSHCD error handling flags */
264enum {
265 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530266};
267
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530268/* UFSHCD UIC layer error flags */
269enum {
270 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200271 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
272 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
273 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
274 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
275 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530276};
277
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530278/* Interrupt configuration options */
279enum {
280 UFSHCD_INT_DISABLE,
281 UFSHCD_INT_ENABLE,
282 UFSHCD_INT_CLEAR,
283};
284
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700285#define DEFAULT_UFSHCD_DBG_PRINT_EN UFSHCD_DBG_PRINT_ALL
286
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530287#define ufshcd_set_eh_in_progress(h) \
288 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
289#define ufshcd_eh_in_progress(h) \
290 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
291#define ufshcd_clear_eh_in_progress(h) \
292 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
293
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300294#define ufshcd_set_ufs_dev_active(h) \
295 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
296#define ufshcd_set_ufs_dev_sleep(h) \
297 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
298#define ufshcd_set_ufs_dev_poweroff(h) \
299 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
300#define ufshcd_is_ufs_dev_active(h) \
301 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
302#define ufshcd_is_ufs_dev_sleep(h) \
303 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
304#define ufshcd_is_ufs_dev_poweroff(h) \
305 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
306
307static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
308 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
309 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
310 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
311 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
312 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
313 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
314};
315
316static inline enum ufs_dev_pwr_mode
317ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
318{
319 return ufs_pm_lvl_states[lvl].dev_state;
320}
321
322static inline enum uic_link_state
323ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
324{
325 return ufs_pm_lvl_states[lvl].link_state;
326}
327
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700328static inline enum ufs_pm_level
329ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
330 enum uic_link_state link_state)
331{
332 enum ufs_pm_level lvl;
333
334 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
335 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
336 (ufs_pm_lvl_states[lvl].link_state == link_state))
337 return lvl;
338 }
339
340 /* if no match found, return the level 0 */
341 return UFS_PM_LVL_0;
342}
343
344static inline bool ufshcd_is_valid_pm_lvl(int lvl)
345{
346 if (lvl >= 0 && lvl < ARRAY_SIZE(ufs_pm_lvl_states))
347 return true;
348 else
349 return false;
350}
351
352static irqreturn_t ufshcd_intr(int irq, void *__hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530353static void ufshcd_tmc_handler(struct ufs_hba *hba);
354static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530355static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700356static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530357static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300358static void ufshcd_hba_exit(struct ufs_hba *hba);
359static int ufshcd_probe_hba(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700360static int ufshcd_enable_clocks(struct ufs_hba *hba);
361static int ufshcd_disable_clocks(struct ufs_hba *hba,
362 bool is_gating_context);
363static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba,
364 bool is_gating_context);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200365static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300366static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700367static inline void ufshcd_save_tstamp_of_last_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300368static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700369static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
370static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
371static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
372static void ufshcd_release_all(struct ufs_hba *hba);
373static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
374static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
375
Yaniv Gardi14497322016-02-01 15:02:39 +0200376static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
377{
378 return tag >= 0 && tag < hba->nutrs;
379}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300380
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700381static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300382{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300383 if (!hba->is_irq_enabled) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700384 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300385 hba->is_irq_enabled = true;
386 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300387}
388
389static inline void ufshcd_disable_irq(struct ufs_hba *hba)
390{
391 if (hba->is_irq_enabled) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700392 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300393 hba->is_irq_enabled = false;
394 }
395}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530396
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700397void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
398{
399 unsigned long flags;
400 bool unblock = false;
401
402 spin_lock_irqsave(hba->host->host_lock, flags);
403 hba->scsi_block_reqs_cnt--;
404 unblock = !hba->scsi_block_reqs_cnt;
405 spin_unlock_irqrestore(hba->host->host_lock, flags);
406 if (unblock)
407 scsi_unblock_requests(hba->host);
408}
409EXPORT_SYMBOL(ufshcd_scsi_unblock_requests);
410
411static inline void __ufshcd_scsi_block_requests(struct ufs_hba *hba)
412{
413 if (!hba->scsi_block_reqs_cnt++)
414 scsi_block_requests(hba->host);
415}
416
417void ufshcd_scsi_block_requests(struct ufs_hba *hba)
418{
419 unsigned long flags;
420
421 spin_lock_irqsave(hba->host->host_lock, flags);
422 __ufshcd_scsi_block_requests(hba);
423 spin_unlock_irqrestore(hba->host->host_lock, flags);
424}
425EXPORT_SYMBOL(ufshcd_scsi_block_requests);
426
Yaniv Gardib573d482016-03-10 17:37:09 +0200427/* replace non-printable or non-ASCII characters with spaces */
428static inline void ufshcd_remove_non_printable(char *val)
429{
430 if (!val)
431 return;
432
433 if (*val < 0x20 || *val > 0x7e)
434 *val = ' ';
435}
436
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700437#ifdef CONFIG_TRACEPOINTS
438static void ufshcd_add_command_trace(struct ufs_hba *hba,
439 unsigned int tag, const char *str)
440{
441 sector_t lba = -1;
442 u8 opcode = 0;
443 u32 intr, doorbell;
444 struct ufshcd_lrb *lrbp;
445 int transfer_len = -1;
446
447 lrbp = &hba->lrb[tag];
448
449 if (lrbp->cmd) { /* data phase exists */
450 opcode = (u8)(*lrbp->cmd->cmnd);
451 if ((opcode == READ_10) || (opcode == WRITE_10)) {
452 /*
453 * Currently we only fully trace read(10) and write(10)
454 * commands
455 */
456 if (lrbp->cmd->request && lrbp->cmd->request->bio)
457 lba =
458 lrbp->cmd->request->bio->bi_iter.bi_sector;
459 transfer_len = be32_to_cpu(
460 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
461 }
462 }
463
464 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
465 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
466 trace_ufshcd_command(dev_name(hba->dev), str, tag,
467 doorbell, transfer_len, intr, lba, opcode);
468}
469
470static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba,
471 unsigned int tag, const char *str)
472{
473 if (trace_ufshcd_command_enabled())
474 ufshcd_add_command_trace(hba, tag, str);
475}
476#else
477static inline void ufshcd_cond_add_cmd_trace(struct ufs_hba *hba,
478 unsigned int tag, const char *str)
479{
480}
481#endif
482
483static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
484{
485 struct ufs_clk_info *clki;
486 struct list_head *head = &hba->clk_list_head;
487
488 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_CLK_FREQ_EN))
489 return;
490
491 if (!head || list_empty(head))
492 return;
493
494 list_for_each_entry(clki, head, list) {
495 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
496 clki->max_freq)
497 dev_err(hba->dev, "clk: %s, rate: %u\n",
498 clki->name, clki->curr_freq);
499 }
500}
501
502static void ufshcd_print_uic_err_hist(struct ufs_hba *hba,
503 struct ufs_uic_err_reg_hist *err_hist, char *err_name)
504{
505 int i;
506
507 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_UIC_ERR_HIST_EN))
508 return;
509
510 for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) {
511 int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH;
512
513 if (err_hist->reg[p] == 0)
514 continue;
515 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us", err_name, i,
516 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
517 }
518}
519
520static void ufshcd_print_host_regs(struct ufs_hba *hba)
521{
522 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_REGS_EN))
523 return;
524
525 /*
526 * hex_dump reads its data without the readl macro. This might
527 * cause inconsistency issues on some platform, as the printed
528 * values may be from cache and not the most recent value.
529 * To know whether you are looking at an un-cached version verify
530 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
531 * during platform/pci probe function.
532 */
533 ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE);
534 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x",
535 hba->ufs_version, hba->capabilities);
536 dev_err(hba->dev,
537 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x",
538 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
539 dev_err(hba->dev,
540 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d",
541 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
542 hba->ufs_stats.hibern8_exit_cnt);
543
544 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
545 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
546 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
547 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
548 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
549
550 ufshcd_print_clk_freqs(hba);
551
552 ufshcd_vops_dbg_register_dump(hba);
553}
554
555static
556void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
557{
558 struct ufshcd_lrb *lrbp;
559 int prdt_length;
560 int tag;
561
562 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TRS_EN))
563 return;
564
565 for_each_set_bit(tag, &bitmap, hba->nutrs) {
566 lrbp = &hba->lrb[tag];
567
568 dev_err(hba->dev, "UPIU[%d] - issue time %lld us",
569 tag, ktime_to_us(lrbp->issue_time_stamp));
570 dev_err(hba->dev,
571 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx",
572 tag, (u64)lrbp->utrd_dma_addr);
573 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
574 sizeof(struct utp_transfer_req_desc));
575 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx", tag,
576 (u64)lrbp->ucd_req_dma_addr);
577 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
578 sizeof(struct utp_upiu_req));
579 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx", tag,
580 (u64)lrbp->ucd_rsp_dma_addr);
581 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
582 sizeof(struct utp_upiu_rsp));
583 prdt_length =
584 le16_to_cpu(lrbp->utr_descriptor_ptr->prd_table_length);
585 dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries phys@0x%llx",
586 tag, prdt_length, (u64)lrbp->ucd_prdt_dma_addr);
587 if (pr_prdt)
588 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
589 sizeof(struct ufshcd_sg_entry) * prdt_length);
590 }
591}
592
593static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
594{
595 struct utp_task_req_desc *tmrdp;
596 int tag;
597
598 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_TMRS_EN))
599 return;
600
601 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
602 tmrdp = &hba->utmrdl_base_addr[tag];
603 dev_err(hba->dev, "TM[%d] - Task Management Header", tag);
604 ufshcd_hex_dump("TM TRD: ", &tmrdp->header,
605 sizeof(struct request_desc_header));
606 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU",
607 tag);
608 ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu,
609 sizeof(struct utp_upiu_req));
610 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU",
611 tag);
612 ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu,
613 sizeof(struct utp_task_req_desc));
614 }
615}
616
617static void ufshcd_print_host_state(struct ufs_hba *hba)
618{
619 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_HOST_STATE_EN))
620 return;
621
622 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
623 dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n",
624 hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs);
625 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x, saved_ce_err=0x%x\n",
626 hba->saved_err, hba->saved_uic_err, hba->saved_ce_err);
627 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
628 hba->curr_dev_pwr_mode, hba->uic_link_state);
629 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
630 hba->pm_op_in_progress, hba->is_sys_suspended);
631 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
632 hba->auto_bkops_enabled, hba->host->host_self_blocked);
633 dev_err(hba->dev, "Clk gate=%d, hibern8 on idle=%d\n",
634 hba->clk_gating.state, hba->hibern8_on_idle.state);
635 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
636 hba->eh_flags, hba->req_abort_count);
637 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
638 hba->capabilities, hba->caps);
639 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
640 hba->dev_quirks);
641}
642
643/**
644 * ufshcd_print_pwr_info - print power params as saved in hba
645 * power info
646 * @hba: per-adapter instance
647 */
648static void ufshcd_print_pwr_info(struct ufs_hba *hba)
649{
650 char *names[] = {
651 "INVALID MODE",
652 "FAST MODE",
653 "SLOW_MODE",
654 "INVALID MODE",
655 "FASTAUTO_MODE",
656 "SLOWAUTO_MODE",
657 "INVALID MODE",
658 };
659
660 if (!(hba->ufshcd_dbg_print & UFSHCD_DBG_PRINT_PWR_EN))
661 return;
662
663 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
664 __func__,
665 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
666 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
667 names[hba->pwr_info.pwr_rx],
668 names[hba->pwr_info.pwr_tx],
669 hba->pwr_info.hs_rate);
670}
671
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530672/*
673 * ufshcd_wait_for_register - wait for register value to change
674 * @hba - per-adapter interface
675 * @reg - mmio register offset
676 * @mask - mask to apply to read register value
677 * @val - wait condition
678 * @interval_us - polling interval in microsecs
679 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200680 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530681 * Returns -ETIMEDOUT on error, zero on success
682 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200683int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
684 u32 val, unsigned long interval_us,
685 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530686{
687 int err = 0;
688 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
689
690 /* ignore bits that we don't intend to wait on */
691 val = val & mask;
692
693 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200694 if (can_sleep)
695 usleep_range(interval_us, interval_us + 50);
696 else
697 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530698 if (time_after(jiffies, timeout)) {
699 if ((ufshcd_readl(hba, reg) & mask) != val)
700 err = -ETIMEDOUT;
701 break;
702 }
703 }
704
705 return err;
706}
707
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530708/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530709 * ufshcd_get_intr_mask - Get the interrupt bit mask
710 * @hba - Pointer to adapter instance
711 *
712 * Returns interrupt bit mask per version
713 */
714static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
715{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700716 u32 intr_mask = 0;
717
718 switch (hba->ufs_version) {
719 case UFSHCI_VERSION_10:
720 intr_mask = INTERRUPT_MASK_ALL_VER_10;
721 break;
722 /* allow fall through */
723 case UFSHCI_VERSION_11:
724 case UFSHCI_VERSION_20:
725 intr_mask = INTERRUPT_MASK_ALL_VER_11;
726 break;
727 /* allow fall through */
728 case UFSHCI_VERSION_21:
729 default:
730 intr_mask = INTERRUPT_MASK_ALL_VER_21;
731 }
732
733 if (!ufshcd_is_crypto_supported(hba))
734 intr_mask &= ~CRYPTO_ENGINE_FATAL_ERROR;
735
736 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530737}
738
739/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530740 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
741 * @hba - Pointer to adapter instance
742 *
743 * Returns UFSHCI version supported by the controller
744 */
745static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
746{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200747 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
748 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300749
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530750 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530751}
752
753/**
754 * ufshcd_is_device_present - Check if any device connected to
755 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300756 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530757 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530758 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530759 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300760static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530761{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300762 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
763 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530764}
765
766/**
767 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
768 * @lrb: pointer to local command reference block
769 *
770 * This function is used to get the OCS field from UTRD
771 * Returns the OCS field in the UTRD
772 */
773static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
774{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530775 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530776}
777
778/**
779 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
780 * @task_req_descp: pointer to utp_task_req_desc structure
781 *
782 * This function is used to get the OCS field from UTMRD
783 * Returns the OCS field in the UTMRD
784 */
785static inline int
786ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
787{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530788 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530789}
790
791/**
792 * ufshcd_get_tm_free_slot - get a free slot for task management request
793 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530794 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530795 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530796 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
797 * Returns 0 if free slot is not available, else return 1 with tag value
798 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530799 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530800static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530801{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530802 int tag;
803 bool ret = false;
804
805 if (!free_slot)
806 goto out;
807
808 do {
809 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
810 if (tag >= hba->nutmrs)
811 goto out;
812 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
813
814 *free_slot = tag;
815 ret = true;
816out:
817 return ret;
818}
819
820static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
821{
822 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530823}
824
825/**
826 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
827 * @hba: per adapter instance
828 * @pos: position of the bit to be cleared
829 */
830static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
831{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530832 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530833}
834
835/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200836 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
837 * @hba: per adapter instance
838 * @tag: position of the bit to be cleared
839 */
840static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
841{
842 __clear_bit(tag, &hba->outstanding_reqs);
843}
844
845/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530846 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
847 * @reg: Register value of host controller status
848 *
849 * Returns integer, 0 on Success and positive value if failed
850 */
851static inline int ufshcd_get_lists_status(u32 reg)
852{
853 /*
854 * The mask 0xFF is for the following HCS register bits
855 * Bit Description
856 * 0 Device Present
857 * 1 UTRLRDY
858 * 2 UTMRLRDY
859 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200860 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530861 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200862 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530863}
864
865/**
866 * ufshcd_get_uic_cmd_result - Get the UIC command result
867 * @hba: Pointer to adapter instance
868 *
869 * This function gets the result of UIC command completion
870 * Returns 0 on success, non zero value on error
871 */
872static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
873{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530874 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530875 MASK_UIC_COMMAND_RESULT;
876}
877
878/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530879 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
880 * @hba: Pointer to adapter instance
881 *
882 * This function gets UIC command argument3
883 * Returns 0 on success, non zero value on error
884 */
885static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
886{
887 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
888}
889
890/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530891 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530892 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530893 */
894static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530895ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530896{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530897 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530898}
899
900/**
901 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
902 * @ucd_rsp_ptr: pointer to response UPIU
903 *
904 * This function gets the response status and scsi_status from response UPIU
905 * Returns the response result code.
906 */
907static inline int
908ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
909{
910 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
911}
912
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530913/*
914 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
915 * from response UPIU
916 * @ucd_rsp_ptr: pointer to response UPIU
917 *
918 * Return the data segment length.
919 */
920static inline unsigned int
921ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
922{
923 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
924 MASK_RSP_UPIU_DATA_SEG_LEN;
925}
926
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530927/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530928 * ufshcd_is_exception_event - Check if the device raised an exception event
929 * @ucd_rsp_ptr: pointer to response UPIU
930 *
931 * The function checks if the device raised an exception event indicated in
932 * the Device Information field of response UPIU.
933 *
934 * Returns true if exception is raised, false otherwise.
935 */
936static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
937{
938 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
939 MASK_RSP_EXCEPTION_EVENT ? true : false;
940}
941
942/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530943 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530944 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530945 */
946static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530947ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530948{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530949 ufshcd_writel(hba, INT_AGGR_ENABLE |
950 INT_AGGR_COUNTER_AND_TIMER_RESET,
951 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
952}
953
954/**
955 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
956 * @hba: per adapter instance
957 * @cnt: Interrupt aggregation counter threshold
958 * @tmout: Interrupt aggregation timeout value
959 */
960static inline void
961ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
962{
963 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
964 INT_AGGR_COUNTER_THLD_VAL(cnt) |
965 INT_AGGR_TIMEOUT_VAL(tmout),
966 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530967}
968
969/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300970 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
971 * @hba: per adapter instance
972 */
973static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
974{
975 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
976}
977
978/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530979 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
980 * When run-stop registers are set to 1, it indicates the
981 * host controller that it can process the requests
982 * @hba: per adapter instance
983 */
984static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
985{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530986 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
987 REG_UTP_TASK_REQ_LIST_RUN_STOP);
988 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
989 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530990}
991
992/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530993 * ufshcd_hba_start - Start controller initialization sequence
994 * @hba: per adapter instance
995 */
996static inline void ufshcd_hba_start(struct ufs_hba *hba)
997{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -0700998 u32 val = CONTROLLER_ENABLE;
999
1000 if (ufshcd_is_crypto_supported(hba))
1001 val |= CRYPTO_GENERAL_ENABLE;
1002 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301003}
1004
1005/**
1006 * ufshcd_is_hba_active - Get controller state
1007 * @hba: per adapter instance
1008 *
1009 * Returns zero if controller is active, 1 otherwise
1010 */
1011static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
1012{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301013 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301014}
1015
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001016static const char *ufschd_uic_link_state_to_string(
1017 enum uic_link_state state)
1018{
1019 switch (state) {
1020 case UIC_LINK_OFF_STATE: return "OFF";
1021 case UIC_LINK_ACTIVE_STATE: return "ACTIVE";
1022 case UIC_LINK_HIBERN8_STATE: return "HIBERN8";
1023 default: return "UNKNOWN";
1024 }
1025}
1026
1027static const char *ufschd_ufs_dev_pwr_mode_to_string(
1028 enum ufs_dev_pwr_mode state)
1029{
1030 switch (state) {
1031 case UFS_ACTIVE_PWR_MODE: return "ACTIVE";
1032 case UFS_SLEEP_PWR_MODE: return "SLEEP";
1033 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN";
1034 default: return "UNKNOWN";
1035 }
1036}
1037
Yaniv Gardi37113102016-03-10 17:37:16 +02001038u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
1039{
1040 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
1041 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
1042 (hba->ufs_version == UFSHCI_VERSION_11))
1043 return UFS_UNIPRO_VER_1_41;
1044 else
1045 return UFS_UNIPRO_VER_1_6;
1046}
1047EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
1048
1049static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
1050{
1051 /*
1052 * If both host and device support UniPro ver1.6 or later, PA layer
1053 * parameters tuning happens during link startup itself.
1054 *
1055 * We can manually tune PA layer parameters if either host or device
1056 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
1057 * logic simple, we will only do manual tuning if local unipro version
1058 * doesn't support ver1.6 or later.
1059 */
1060 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
1061 return true;
1062 else
1063 return false;
1064}
1065
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001066/**
1067 * ufshcd_set_clk_freq - set UFS controller clock frequencies
1068 * @hba: per adapter instance
1069 * @scale_up: If True, set max possible frequency othewise set low frequency
1070 *
1071 * Returns 0 if successful
1072 * Returns < 0 for any other errors
1073 */
1074static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up)
1075{
1076 int ret = 0;
1077 struct ufs_clk_info *clki;
1078 struct list_head *head = &hba->clk_list_head;
1079
1080 if (!head || list_empty(head))
1081 goto out;
1082
1083 list_for_each_entry(clki, head, list) {
1084 if (!IS_ERR_OR_NULL(clki->clk)) {
1085 if (scale_up && clki->max_freq) {
1086 if (clki->curr_freq == clki->max_freq)
1087 continue;
1088
1089 ret = clk_set_rate(clki->clk, clki->max_freq);
1090 if (ret) {
1091 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1092 __func__, clki->name,
1093 clki->max_freq, ret);
1094 break;
1095 }
1096 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1097 "scaled up", clki->name,
1098 clki->curr_freq,
1099 clki->max_freq);
1100 clki->curr_freq = clki->max_freq;
1101
1102 } else if (!scale_up && clki->min_freq) {
1103 if (clki->curr_freq == clki->min_freq)
1104 continue;
1105
1106 ret = clk_set_rate(clki->clk, clki->min_freq);
1107 if (ret) {
1108 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
1109 __func__, clki->name,
1110 clki->min_freq, ret);
1111 break;
1112 }
1113 trace_ufshcd_clk_scaling(dev_name(hba->dev),
1114 "scaled down", clki->name,
1115 clki->curr_freq,
1116 clki->min_freq);
1117 clki->curr_freq = clki->min_freq;
1118 }
1119 }
1120 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
1121 clki->name, clk_get_rate(clki->clk));
1122 }
1123
1124out:
1125 return ret;
1126}
1127
1128/**
1129 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1130 * @hba: per adapter instance
1131 * @scale_up: True if scaling up and false if scaling down
1132 *
1133 * Returns 0 if successful
1134 * Returns < 0 for any other errors
1135 */
1136static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
1137{
1138 int ret = 0;
1139
1140 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
1141 if (ret)
1142 return ret;
1143
1144 ret = ufshcd_set_clk_freq(hba, scale_up);
1145 if (ret)
1146 return ret;
1147
1148 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1149 if (ret) {
1150 ufshcd_set_clk_freq(hba, !scale_up);
1151 return ret;
1152 }
1153
1154 return ret;
1155}
1156
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001157static void ufshcd_ungate_work(struct work_struct *work)
1158{
1159 int ret;
1160 unsigned long flags;
1161 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1162 clk_gating.ungate_work);
1163
1164 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1165
1166 spin_lock_irqsave(hba->host->host_lock, flags);
1167 if (hba->clk_gating.state == CLKS_ON) {
1168 spin_unlock_irqrestore(hba->host->host_lock, flags);
1169 goto unblock_reqs;
1170 }
1171
1172 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001173 ufshcd_hba_vreg_set_hpm(hba);
1174 ufshcd_enable_clocks(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001175
1176 /* Exit from hibern8 */
1177 if (ufshcd_can_hibern8_during_gating(hba)) {
1178 /* Prevent gating in this path */
1179 hba->clk_gating.is_suspended = true;
1180 if (ufshcd_is_link_hibern8(hba)) {
1181 ret = ufshcd_uic_hibern8_exit(hba);
1182 if (ret)
1183 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1184 __func__, ret);
1185 else
1186 ufshcd_set_link_active(hba);
1187 }
1188 hba->clk_gating.is_suspended = false;
1189 }
1190unblock_reqs:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001191 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001192}
1193
1194/**
1195 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1196 * Also, exit from hibern8 mode and set the link as active.
1197 * @hba: per adapter instance
1198 * @async: This indicates whether caller should ungate clocks asynchronously.
1199 */
1200int ufshcd_hold(struct ufs_hba *hba, bool async)
1201{
1202 int rc = 0;
1203 unsigned long flags;
1204
1205 if (!ufshcd_is_clkgating_allowed(hba))
1206 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001207 spin_lock_irqsave(hba->host->host_lock, flags);
1208 hba->clk_gating.active_reqs++;
1209
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001210 if (ufshcd_eh_in_progress(hba)) {
1211 spin_unlock_irqrestore(hba->host->host_lock, flags);
1212 return 0;
1213 }
1214
Sahitya Tummala856b3482014-09-25 15:32:34 +03001215start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001216 switch (hba->clk_gating.state) {
1217 case CLKS_ON:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001218 /*
1219 * Wait for the ungate work to complete if in progress.
1220 * Though the clocks may be in ON state, the link could
1221 * still be in hibner8 state if hibern8 is allowed
1222 * during clock gating.
1223 * Make sure we exit hibern8 state also in addition to
1224 * clocks being ON.
1225 */
1226 if (ufshcd_can_hibern8_during_gating(hba) &&
1227 ufshcd_is_link_hibern8(hba)) {
1228 spin_unlock_irqrestore(hba->host->host_lock, flags);
1229 flush_work(&hba->clk_gating.ungate_work);
1230 spin_lock_irqsave(hba->host->host_lock, flags);
1231 goto start;
1232 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001233 break;
1234 case REQ_CLKS_OFF:
1235 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1236 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001237 trace_ufshcd_clk_gating(dev_name(hba->dev),
1238 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001239 break;
1240 }
1241 /*
1242 * If we here, it means gating work is either done or
1243 * currently running. Hence, fall through to cancel gating
1244 * work and to enable clocks.
1245 */
1246 case CLKS_OFF:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001247 __ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001248 hba->clk_gating.state = REQ_CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001249 trace_ufshcd_clk_gating(dev_name(hba->dev),
1250 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001251 schedule_work(&hba->clk_gating.ungate_work);
1252 /*
1253 * fall through to check if we should wait for this
1254 * work to be done or not.
1255 */
1256 case REQ_CLKS_ON:
1257 if (async) {
1258 rc = -EAGAIN;
1259 hba->clk_gating.active_reqs--;
1260 break;
1261 }
1262
1263 spin_unlock_irqrestore(hba->host->host_lock, flags);
1264 flush_work(&hba->clk_gating.ungate_work);
1265 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001266 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001267 goto start;
1268 default:
1269 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1270 __func__, hba->clk_gating.state);
1271 break;
1272 }
1273 spin_unlock_irqrestore(hba->host->host_lock, flags);
1274out:
1275 return rc;
1276}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001277EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001278
1279static void ufshcd_gate_work(struct work_struct *work)
1280{
1281 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1282 clk_gating.gate_work.work);
1283 unsigned long flags;
1284
1285 spin_lock_irqsave(hba->host->host_lock, flags);
1286 if (hba->clk_gating.is_suspended) {
1287 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001288 trace_ufshcd_clk_gating(dev_name(hba->dev),
1289 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001290 goto rel_lock;
1291 }
1292
1293 if (hba->clk_gating.active_reqs
1294 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1295 || hba->lrb_in_use || hba->outstanding_tasks
1296 || hba->active_uic_cmd || hba->uic_async_done)
1297 goto rel_lock;
1298
1299 spin_unlock_irqrestore(hba->host->host_lock, flags);
1300
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001301 if (ufshcd_is_hibern8_on_idle_allowed(hba) &&
1302 hba->hibern8_on_idle.is_enabled)
1303 /*
1304 * Hibern8 enter work (on Idle) needs clocks to be ON hence
1305 * make sure that it is flushed before turning off the clocks.
1306 */
1307 flush_delayed_work(&hba->hibern8_on_idle.enter_work);
1308
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001309 /* put the link into hibern8 mode before turning off clocks */
1310 if (ufshcd_can_hibern8_during_gating(hba)) {
1311 if (ufshcd_uic_hibern8_enter(hba)) {
1312 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001313 trace_ufshcd_clk_gating(dev_name(hba->dev),
1314 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001315 goto out;
1316 }
1317 ufshcd_set_link_hibern8(hba);
1318 }
1319
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001320 if (!ufshcd_is_link_active(hba) && !hba->no_ref_clk_gating)
1321 ufshcd_disable_clocks(hba, true);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001322 else
1323 /* If link is active, device ref_clk can't be switched off */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001324 ufshcd_disable_clocks_skip_ref_clk(hba, true);
1325
1326 /* Put the host controller in low power mode if possible */
1327 ufshcd_hba_vreg_set_lpm(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001328
1329 /*
1330 * In case you are here to cancel this work the gating state
1331 * would be marked as REQ_CLKS_ON. In this case keep the state
1332 * as REQ_CLKS_ON which would anyway imply that clocks are off
1333 * and a request to turn them on is pending. By doing this way,
1334 * we keep the state machine in tact and this would ultimately
1335 * prevent from doing cancel work multiple times when there are
1336 * new requests arriving before the current cancel work is done.
1337 */
1338 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001339 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001340 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001341 trace_ufshcd_clk_gating(dev_name(hba->dev),
1342 hba->clk_gating.state);
1343 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001344rel_lock:
1345 spin_unlock_irqrestore(hba->host->host_lock, flags);
1346out:
1347 return;
1348}
1349
1350/* host lock must be held before calling this variant */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001351static void __ufshcd_release(struct ufs_hba *hba, bool no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001352{
1353 if (!ufshcd_is_clkgating_allowed(hba))
1354 return;
1355
1356 hba->clk_gating.active_reqs--;
1357
1358 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1359 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1360 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001361 || hba->active_uic_cmd || hba->uic_async_done
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001362 || ufshcd_eh_in_progress(hba) || no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001363 return;
1364
1365 hba->clk_gating.state = REQ_CLKS_OFF;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001366 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
1367
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001368 schedule_delayed_work(&hba->clk_gating.gate_work,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001369 msecs_to_jiffies(hba->clk_gating.delay_ms));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001370}
1371
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001372void ufshcd_release(struct ufs_hba *hba, bool no_sched)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001373{
1374 unsigned long flags;
1375
1376 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001377 __ufshcd_release(hba, no_sched);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001378 spin_unlock_irqrestore(hba->host->host_lock, flags);
1379}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001380EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001381
1382static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1383 struct device_attribute *attr, char *buf)
1384{
1385 struct ufs_hba *hba = dev_get_drvdata(dev);
1386
1387 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1388}
1389
1390static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1391 struct device_attribute *attr, const char *buf, size_t count)
1392{
1393 struct ufs_hba *hba = dev_get_drvdata(dev);
1394 unsigned long flags, value;
1395
1396 if (kstrtoul(buf, 0, &value))
1397 return -EINVAL;
1398
1399 spin_lock_irqsave(hba->host->host_lock, flags);
1400 hba->clk_gating.delay_ms = value;
1401 spin_unlock_irqrestore(hba->host->host_lock, flags);
1402 return count;
1403}
1404
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001405static ssize_t ufshcd_clkgate_delay_pwr_save_show(struct device *dev,
1406 struct device_attribute *attr, char *buf)
1407{
1408 struct ufs_hba *hba = dev_get_drvdata(dev);
1409
1410 return snprintf(buf, PAGE_SIZE, "%lu\n",
1411 hba->clk_gating.delay_ms_pwr_save);
1412}
1413
1414static ssize_t ufshcd_clkgate_delay_pwr_save_store(struct device *dev,
1415 struct device_attribute *attr, const char *buf, size_t count)
1416{
1417 struct ufs_hba *hba = dev_get_drvdata(dev);
1418 unsigned long flags, value;
1419
1420 if (kstrtoul(buf, 0, &value))
1421 return -EINVAL;
1422
1423 spin_lock_irqsave(hba->host->host_lock, flags);
1424
1425 hba->clk_gating.delay_ms_pwr_save = value;
1426 if (ufshcd_is_clkscaling_supported(hba) &&
1427 !hba->clk_scaling.is_scaled_up)
1428 hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_pwr_save;
1429
1430 spin_unlock_irqrestore(hba->host->host_lock, flags);
1431 return count;
1432}
1433
1434static ssize_t ufshcd_clkgate_delay_perf_show(struct device *dev,
1435 struct device_attribute *attr, char *buf)
1436{
1437 struct ufs_hba *hba = dev_get_drvdata(dev);
1438
1439 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms_perf);
1440}
1441
1442static ssize_t ufshcd_clkgate_delay_perf_store(struct device *dev,
1443 struct device_attribute *attr, const char *buf, size_t count)
1444{
1445 struct ufs_hba *hba = dev_get_drvdata(dev);
1446 unsigned long flags, value;
1447
1448 if (kstrtoul(buf, 0, &value))
1449 return -EINVAL;
1450
1451 spin_lock_irqsave(hba->host->host_lock, flags);
1452
1453 hba->clk_gating.delay_ms_perf = value;
1454 if (ufshcd_is_clkscaling_supported(hba) &&
1455 hba->clk_scaling.is_scaled_up)
1456 hba->clk_gating.delay_ms = hba->clk_gating.delay_ms_perf;
1457
1458 spin_unlock_irqrestore(hba->host->host_lock, flags);
1459 return count;
1460}
1461
1462static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1463 struct device_attribute *attr, char *buf)
1464{
1465 struct ufs_hba *hba = dev_get_drvdata(dev);
1466
1467 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1468}
1469
1470static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1471 struct device_attribute *attr, const char *buf, size_t count)
1472{
1473 struct ufs_hba *hba = dev_get_drvdata(dev);
1474 unsigned long flags;
1475 u32 value;
1476
1477 if (kstrtou32(buf, 0, &value))
1478 return -EINVAL;
1479
1480 value = !!value;
1481 if (value == hba->clk_gating.is_enabled)
1482 goto out;
1483
1484 if (value) {
1485 ufshcd_release(hba, false);
1486 } else {
1487 spin_lock_irqsave(hba->host->host_lock, flags);
1488 hba->clk_gating.active_reqs++;
1489 spin_unlock_irqrestore(hba->host->host_lock, flags);
1490 }
1491
1492 hba->clk_gating.is_enabled = value;
1493out:
1494 return count;
1495}
1496
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001497static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1498{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001499 struct ufs_clk_gating *gating = &hba->clk_gating;
1500
1501 hba->clk_gating.state = CLKS_ON;
1502
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001503 if (!ufshcd_is_clkgating_allowed(hba))
1504 return;
1505
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001506 INIT_DELAYED_WORK(&gating->gate_work, ufshcd_gate_work);
1507 INIT_WORK(&gating->ungate_work, ufshcd_ungate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001508
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001509 gating->is_enabled = true;
1510
1511 /*
1512 * Scheduling the delayed work after 1 jiffies will make the work to
1513 * get schedule any time from 0ms to 1000/HZ ms which is not desirable
1514 * for hibern8 enter work as it may impact the performance if it gets
1515 * scheduled almost immediately. Hence make sure that hibern8 enter
1516 * work gets scheduled atleast after 2 jiffies (any time between
1517 * 1000/HZ ms to 2000/HZ ms).
1518 */
1519 gating->delay_ms_pwr_save = jiffies_to_msecs(
1520 max_t(unsigned long,
1521 msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PWR_SAVE),
1522 2));
1523 gating->delay_ms_perf = jiffies_to_msecs(
1524 max_t(unsigned long,
1525 msecs_to_jiffies(UFSHCD_CLK_GATING_DELAY_MS_PERF),
1526 2));
1527
1528 /* start with performance mode */
1529 gating->delay_ms = gating->delay_ms_perf;
1530
1531 if (!ufshcd_is_clkscaling_supported(hba))
1532 goto scaling_not_supported;
1533
1534 gating->delay_pwr_save_attr.show = ufshcd_clkgate_delay_pwr_save_show;
1535 gating->delay_pwr_save_attr.store = ufshcd_clkgate_delay_pwr_save_store;
1536 sysfs_attr_init(&gating->delay_pwr_save_attr.attr);
1537 gating->delay_pwr_save_attr.attr.name = "clkgate_delay_ms_pwr_save";
1538 gating->delay_pwr_save_attr.attr.mode = S_IRUGO | S_IWUSR;
1539 if (device_create_file(hba->dev, &gating->delay_pwr_save_attr))
1540 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_pwr_save\n");
1541
1542 gating->delay_perf_attr.show = ufshcd_clkgate_delay_perf_show;
1543 gating->delay_perf_attr.store = ufshcd_clkgate_delay_perf_store;
1544 sysfs_attr_init(&gating->delay_perf_attr.attr);
1545 gating->delay_perf_attr.attr.name = "clkgate_delay_ms_perf";
1546 gating->delay_perf_attr.attr.mode = S_IRUGO | S_IWUSR;
1547 if (device_create_file(hba->dev, &gating->delay_perf_attr))
1548 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay_ms_perf\n");
1549
1550 goto add_clkgate_enable;
1551
1552scaling_not_supported:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001553 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1554 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1555 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1556 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
1557 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
1558 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1559 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001560
1561add_clkgate_enable:
1562 gating->enable_attr.show = ufshcd_clkgate_enable_show;
1563 gating->enable_attr.store = ufshcd_clkgate_enable_store;
1564 sysfs_attr_init(&gating->enable_attr.attr);
1565 gating->enable_attr.attr.name = "clkgate_enable";
1566 gating->enable_attr.attr.mode = S_IRUGO | S_IWUSR;
1567 if (device_create_file(hba->dev, &gating->enable_attr))
1568 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001569}
1570
1571static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1572{
1573 if (!ufshcd_is_clkgating_allowed(hba))
1574 return;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001575 if (ufshcd_is_clkscaling_supported(hba)) {
1576 device_remove_file(hba->dev,
1577 &hba->clk_gating.delay_pwr_save_attr);
1578 device_remove_file(hba->dev, &hba->clk_gating.delay_perf_attr);
1579 } else {
1580 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
1581 }
1582 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001583 cancel_work_sync(&hba->clk_gating.ungate_work);
1584 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001585}
1586
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001587static void ufshcd_set_auto_hibern8_timer(struct ufs_hba *hba, u32 delay)
1588{
1589 ufshcd_rmwl(hba, AUTO_HIBERN8_TIMER_SCALE_MASK |
1590 AUTO_HIBERN8_IDLE_TIMER_MASK,
1591 AUTO_HIBERN8_TIMER_SCALE_1_MS | delay,
1592 REG_AUTO_HIBERN8_IDLE_TIMER);
1593 /* Make sure the timer gets applied before further operations */
1594 mb();
1595}
1596
1597/**
1598 * ufshcd_hibern8_hold - Make sure that link is not in hibern8.
1599 *
1600 * @hba: per adapter instance
1601 * @async: This indicates whether caller wants to exit hibern8 asynchronously.
1602 *
1603 * Exit from hibern8 mode and set the link as active.
1604 *
1605 * Return 0 on success, non-zero on failure.
1606 */
1607static int ufshcd_hibern8_hold(struct ufs_hba *hba, bool async)
1608{
1609 int rc = 0;
1610 unsigned long flags;
1611
1612 if (!ufshcd_is_hibern8_on_idle_allowed(hba))
1613 goto out;
1614
1615 spin_lock_irqsave(hba->host->host_lock, flags);
1616 hba->hibern8_on_idle.active_reqs++;
1617
1618 if (ufshcd_eh_in_progress(hba)) {
1619 spin_unlock_irqrestore(hba->host->host_lock, flags);
1620 return 0;
1621 }
1622
1623start:
1624 switch (hba->hibern8_on_idle.state) {
1625 case HIBERN8_EXITED:
1626 break;
1627 case REQ_HIBERN8_ENTER:
1628 if (cancel_delayed_work(&hba->hibern8_on_idle.enter_work)) {
1629 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1630 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1631 hba->hibern8_on_idle.state);
1632 break;
1633 }
1634 /*
1635 * If we here, it means Hibern8 enter work is either done or
1636 * currently running. Hence, fall through to cancel hibern8
1637 * work and exit hibern8.
1638 */
1639 case HIBERN8_ENTERED:
1640 __ufshcd_scsi_block_requests(hba);
1641 hba->hibern8_on_idle.state = REQ_HIBERN8_EXIT;
1642 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1643 hba->hibern8_on_idle.state);
1644 schedule_work(&hba->hibern8_on_idle.exit_work);
1645 /*
1646 * fall through to check if we should wait for this
1647 * work to be done or not.
1648 */
1649 case REQ_HIBERN8_EXIT:
1650 if (async) {
1651 rc = -EAGAIN;
1652 hba->hibern8_on_idle.active_reqs--;
1653 break;
1654 } else {
1655 spin_unlock_irqrestore(hba->host->host_lock, flags);
1656 flush_work(&hba->hibern8_on_idle.exit_work);
1657 /* Make sure state is HIBERN8_EXITED before returning */
1658 spin_lock_irqsave(hba->host->host_lock, flags);
1659 goto start;
1660 }
1661 default:
1662 dev_err(hba->dev, "%s: H8 is in invalid state %d\n",
1663 __func__, hba->hibern8_on_idle.state);
1664 break;
1665 }
1666 spin_unlock_irqrestore(hba->host->host_lock, flags);
1667out:
1668 return rc;
1669}
1670
1671/* host lock must be held before calling this variant */
1672static void __ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched)
1673{
1674 unsigned long delay_in_jiffies;
1675
1676 if (!ufshcd_is_hibern8_on_idle_allowed(hba))
1677 return;
1678
1679 hba->hibern8_on_idle.active_reqs--;
1680 BUG_ON(hba->hibern8_on_idle.active_reqs < 0);
1681
1682 if (hba->hibern8_on_idle.active_reqs
1683 || hba->hibern8_on_idle.is_suspended
1684 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1685 || hba->lrb_in_use || hba->outstanding_tasks
1686 || hba->active_uic_cmd || hba->uic_async_done
1687 || ufshcd_eh_in_progress(hba) || no_sched)
1688 return;
1689
1690 hba->hibern8_on_idle.state = REQ_HIBERN8_ENTER;
1691 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1692 hba->hibern8_on_idle.state);
1693 /*
1694 * Scheduling the delayed work after 1 jiffies will make the work to
1695 * get schedule any time from 0ms to 1000/HZ ms which is not desirable
1696 * for hibern8 enter work as it may impact the performance if it gets
1697 * scheduled almost immediately. Hence make sure that hibern8 enter
1698 * work gets scheduled atleast after 2 jiffies (any time between
1699 * 1000/HZ ms to 2000/HZ ms).
1700 */
1701 delay_in_jiffies = msecs_to_jiffies(hba->hibern8_on_idle.delay_ms);
1702 if (delay_in_jiffies == 1)
1703 delay_in_jiffies++;
1704
1705 schedule_delayed_work(&hba->hibern8_on_idle.enter_work,
1706 delay_in_jiffies);
1707}
1708
1709static void ufshcd_hibern8_release(struct ufs_hba *hba, bool no_sched)
1710{
1711 unsigned long flags;
1712
1713 spin_lock_irqsave(hba->host->host_lock, flags);
1714 __ufshcd_hibern8_release(hba, no_sched);
1715 spin_unlock_irqrestore(hba->host->host_lock, flags);
1716}
1717
1718static void ufshcd_hibern8_enter_work(struct work_struct *work)
1719{
1720 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1721 hibern8_on_idle.enter_work.work);
1722 unsigned long flags;
1723
1724 spin_lock_irqsave(hba->host->host_lock, flags);
1725 if (hba->hibern8_on_idle.is_suspended) {
1726 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1727 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1728 hba->hibern8_on_idle.state);
1729 goto rel_lock;
1730 }
1731
1732 if (hba->hibern8_on_idle.active_reqs
1733 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1734 || hba->lrb_in_use || hba->outstanding_tasks
1735 || hba->active_uic_cmd || hba->uic_async_done)
1736 goto rel_lock;
1737
1738 spin_unlock_irqrestore(hba->host->host_lock, flags);
1739
1740 if (ufshcd_is_link_active(hba) && ufshcd_uic_hibern8_enter(hba)) {
1741 /* Enter failed */
1742 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1743 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1744 hba->hibern8_on_idle.state);
1745 goto out;
1746 }
1747 ufshcd_set_link_hibern8(hba);
1748
1749 /*
1750 * In case you are here to cancel this work the hibern8_on_idle.state
1751 * would be marked as REQ_HIBERN8_EXIT. In this case keep the state
1752 * as REQ_HIBERN8_EXIT which would anyway imply that we are in hibern8
1753 * and a request to exit from it is pending. By doing this way,
1754 * we keep the state machine in tact and this would ultimately
1755 * prevent from doing cancel work multiple times when there are
1756 * new requests arriving before the current cancel work is done.
1757 */
1758 spin_lock_irqsave(hba->host->host_lock, flags);
1759 if (hba->hibern8_on_idle.state == REQ_HIBERN8_ENTER) {
1760 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
1761 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1762 hba->hibern8_on_idle.state);
1763 }
1764rel_lock:
1765 spin_unlock_irqrestore(hba->host->host_lock, flags);
1766out:
1767 return;
1768}
1769
1770static void ufshcd_hibern8_exit_work(struct work_struct *work)
1771{
1772 int ret;
1773 unsigned long flags;
1774 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1775 hibern8_on_idle.exit_work);
1776
1777 cancel_delayed_work_sync(&hba->hibern8_on_idle.enter_work);
1778
1779 spin_lock_irqsave(hba->host->host_lock, flags);
1780 if ((hba->hibern8_on_idle.state == HIBERN8_EXITED)
1781 || ufshcd_is_link_active(hba)) {
1782 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1783 spin_unlock_irqrestore(hba->host->host_lock, flags);
1784 goto unblock_reqs;
1785 }
1786 spin_unlock_irqrestore(hba->host->host_lock, flags);
1787
1788 /* Exit from hibern8 */
1789 if (ufshcd_is_link_hibern8(hba)) {
1790 ufshcd_hold(hba, false);
1791 ret = ufshcd_uic_hibern8_exit(hba);
1792 ufshcd_release(hba, false);
1793 if (!ret) {
1794 spin_lock_irqsave(hba->host->host_lock, flags);
1795 ufshcd_set_link_active(hba);
1796 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1797 trace_ufshcd_hibern8_on_idle(dev_name(hba->dev),
1798 hba->hibern8_on_idle.state);
1799 spin_unlock_irqrestore(hba->host->host_lock, flags);
1800 }
1801 }
1802unblock_reqs:
1803 ufshcd_scsi_unblock_requests(hba);
1804}
1805
1806static ssize_t ufshcd_hibern8_on_idle_delay_show(struct device *dev,
1807 struct device_attribute *attr, char *buf)
1808{
1809 struct ufs_hba *hba = dev_get_drvdata(dev);
1810
1811 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->hibern8_on_idle.delay_ms);
1812}
1813
1814static ssize_t ufshcd_hibern8_on_idle_delay_store(struct device *dev,
1815 struct device_attribute *attr, const char *buf, size_t count)
1816{
1817 struct ufs_hba *hba = dev_get_drvdata(dev);
1818 unsigned long flags, value;
1819
1820 if (kstrtoul(buf, 0, &value))
1821 return -EINVAL;
1822
1823 spin_lock_irqsave(hba->host->host_lock, flags);
1824 hba->hibern8_on_idle.delay_ms = value;
1825 spin_unlock_irqrestore(hba->host->host_lock, flags);
1826
1827 /* Update auto hibern8 timer value if supported */
1828 if (ufshcd_is_auto_hibern8_supported(hba) &&
1829 hba->hibern8_on_idle.is_enabled)
1830 ufshcd_set_auto_hibern8_timer(hba,
1831 hba->hibern8_on_idle.delay_ms);
1832
1833 return count;
1834}
1835
1836static ssize_t ufshcd_hibern8_on_idle_enable_show(struct device *dev,
1837 struct device_attribute *attr, char *buf)
1838{
1839 struct ufs_hba *hba = dev_get_drvdata(dev);
1840
1841 return snprintf(buf, PAGE_SIZE, "%d\n",
1842 hba->hibern8_on_idle.is_enabled);
1843}
1844
1845static ssize_t ufshcd_hibern8_on_idle_enable_store(struct device *dev,
1846 struct device_attribute *attr, const char *buf, size_t count)
1847{
1848 struct ufs_hba *hba = dev_get_drvdata(dev);
1849 unsigned long flags;
1850 u32 value;
1851
1852 if (kstrtou32(buf, 0, &value))
1853 return -EINVAL;
1854
1855 value = !!value;
1856 if (value == hba->hibern8_on_idle.is_enabled)
1857 goto out;
1858
1859 /* Update auto hibern8 timer value if supported */
1860 if (ufshcd_is_auto_hibern8_supported(hba)) {
1861 ufshcd_set_auto_hibern8_timer(hba,
1862 value ? hba->hibern8_on_idle.delay_ms : value);
1863 goto update;
1864 }
1865
1866 if (value) {
1867 /*
1868 * As clock gating work would wait for the hibern8 enter work
1869 * to finish, clocks would remain on during hibern8 enter work.
1870 */
1871 ufshcd_hold(hba, false);
1872 ufshcd_release_all(hba);
1873 } else {
1874 spin_lock_irqsave(hba->host->host_lock, flags);
1875 hba->hibern8_on_idle.active_reqs++;
1876 spin_unlock_irqrestore(hba->host->host_lock, flags);
1877 }
1878
1879update:
1880 hba->hibern8_on_idle.is_enabled = value;
1881out:
1882 return count;
1883}
1884
1885static void ufshcd_init_hibern8_on_idle(struct ufs_hba *hba)
1886{
1887 /* initialize the state variable here */
1888 hba->hibern8_on_idle.state = HIBERN8_EXITED;
1889
1890 if (!ufshcd_is_hibern8_on_idle_allowed(hba) &&
1891 !ufshcd_is_auto_hibern8_supported(hba))
1892 return;
1893
1894 if (ufshcd_is_auto_hibern8_supported(hba)) {
1895 hba->hibern8_on_idle.state = AUTO_HIBERN8;
1896 /*
1897 * Disable SW hibern8 enter on idle in case
1898 * auto hibern8 is supported
1899 */
1900 hba->caps &= ~UFSHCD_CAP_HIBERN8_ENTER_ON_IDLE;
1901 } else {
1902 INIT_DELAYED_WORK(&hba->hibern8_on_idle.enter_work,
1903 ufshcd_hibern8_enter_work);
1904 INIT_WORK(&hba->hibern8_on_idle.exit_work,
1905 ufshcd_hibern8_exit_work);
1906 }
1907
1908 hba->hibern8_on_idle.delay_ms = 10;
1909 hba->hibern8_on_idle.is_enabled = true;
1910
1911 hba->hibern8_on_idle.delay_attr.show =
1912 ufshcd_hibern8_on_idle_delay_show;
1913 hba->hibern8_on_idle.delay_attr.store =
1914 ufshcd_hibern8_on_idle_delay_store;
1915 sysfs_attr_init(&hba->hibern8_on_idle.delay_attr.attr);
1916 hba->hibern8_on_idle.delay_attr.attr.name = "hibern8_on_idle_delay_ms";
1917 hba->hibern8_on_idle.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
1918 if (device_create_file(hba->dev, &hba->hibern8_on_idle.delay_attr))
1919 dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_delay\n");
1920
1921 hba->hibern8_on_idle.enable_attr.show =
1922 ufshcd_hibern8_on_idle_enable_show;
1923 hba->hibern8_on_idle.enable_attr.store =
1924 ufshcd_hibern8_on_idle_enable_store;
1925 sysfs_attr_init(&hba->hibern8_on_idle.enable_attr.attr);
1926 hba->hibern8_on_idle.enable_attr.attr.name = "hibern8_on_idle_enable";
1927 hba->hibern8_on_idle.enable_attr.attr.mode = S_IRUGO | S_IWUSR;
1928 if (device_create_file(hba->dev, &hba->hibern8_on_idle.enable_attr))
1929 dev_err(hba->dev, "Failed to create sysfs for hibern8_on_idle_enable\n");
1930}
1931
1932static void ufshcd_exit_hibern8_on_idle(struct ufs_hba *hba)
1933{
1934 if (!ufshcd_is_hibern8_on_idle_allowed(hba) &&
1935 !ufshcd_is_auto_hibern8_supported(hba))
1936 return;
1937 device_remove_file(hba->dev, &hba->hibern8_on_idle.delay_attr);
1938 device_remove_file(hba->dev, &hba->hibern8_on_idle.enable_attr);
1939}
1940
1941static void ufshcd_hold_all(struct ufs_hba *hba)
1942{
1943 ufshcd_hold(hba, false);
1944 ufshcd_hibern8_hold(hba, false);
1945}
1946
1947static void ufshcd_release_all(struct ufs_hba *hba)
1948{
1949 ufshcd_hibern8_release(hba, false);
1950 ufshcd_release(hba, false);
1951}
1952
Sahitya Tummala856b3482014-09-25 15:32:34 +03001953/* Must be called with host lock acquired */
1954static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1955{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001956 bool queue_resume_work = false;
1957
1958 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001959 return;
1960
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001961 if (!hba->clk_scaling.active_reqs++)
1962 queue_resume_work = true;
1963
1964 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1965 return;
1966
1967 if (queue_resume_work)
1968 queue_work(hba->clk_scaling.workq,
1969 &hba->clk_scaling.resume_work);
1970
1971 if (!hba->clk_scaling.window_start_t) {
1972 hba->clk_scaling.window_start_t = jiffies;
1973 hba->clk_scaling.tot_busy_t = 0;
1974 hba->clk_scaling.is_busy_started = false;
1975 }
1976
Sahitya Tummala856b3482014-09-25 15:32:34 +03001977 if (!hba->clk_scaling.is_busy_started) {
1978 hba->clk_scaling.busy_start_t = ktime_get();
1979 hba->clk_scaling.is_busy_started = true;
1980 }
1981}
1982
1983static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1984{
1985 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1986
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001987 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001988 return;
1989
1990 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1991 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1992 scaling->busy_start_t));
1993 scaling->busy_start_t = ktime_set(0, 0);
1994 scaling->is_busy_started = false;
1995 }
1996}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07001997
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301998/**
1999 * ufshcd_send_command - Send SCSI or device management commands
2000 * @hba: per adapter instance
2001 * @task_tag: Task tag of the command
2002 */
2003static inline
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002004int ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302005{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002006 int ret = 0;
2007
2008 hba->lrb[task_tag].issue_time_stamp = ktime_get();
2009 hba->lrb[task_tag].complete_time_stamp = ktime_set(0, 0);
Sahitya Tummala856b3482014-09-25 15:32:34 +03002010 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302011 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302012 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002013 /* Make sure that doorbell is committed immediately */
2014 wmb();
2015 ufshcd_cond_add_cmd_trace(hba, task_tag, "send");
2016 ufshcd_update_tag_stats(hba, task_tag);
2017 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302018}
2019
2020/**
2021 * ufshcd_copy_sense_data - Copy sense data in case of check condition
2022 * @lrb - pointer to local reference block
2023 */
2024static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
2025{
2026 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302027 if (lrbp->sense_buffer &&
2028 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002029 int len_to_copy;
2030
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302031 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002032 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
2033
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302034 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302035 lrbp->ucd_rsp_ptr->sr.sense_data,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002036 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302037 }
2038}
2039
2040/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302041 * ufshcd_copy_query_response() - Copy the Query Response and the data
2042 * descriptor
2043 * @hba: per adapter instance
2044 * @lrb - pointer to local reference block
2045 */
2046static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002047int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05302048{
2049 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2050
Dolev Raviv68078d52013-07-30 00:35:58 +05302051 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302052
Dolev Raviv68078d52013-07-30 00:35:58 +05302053 /* Get the descriptor */
2054 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002055 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05302056 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002057 u16 resp_len;
2058 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05302059
2060 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002061 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302062 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002063 buf_len = be16_to_cpu(
2064 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002065 if (likely(buf_len >= resp_len)) {
2066 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
2067 } else {
2068 dev_warn(hba->dev,
2069 "%s: Response size is bigger than buffer",
2070 __func__);
2071 return -EINVAL;
2072 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302073 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002074
2075 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302076}
2077
2078/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302079 * ufshcd_hba_capabilities - Read controller capabilities
2080 * @hba: per adapter instance
2081 */
2082static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
2083{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302084 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302085
2086 /* nutrs and nutmrs are 0 based values */
2087 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
2088 hba->nutmrs =
2089 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
2090}
2091
2092/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302093 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2094 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302095 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302096 * Return true on success, else false
2097 */
2098static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
2099{
2100 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
2101 return true;
2102 else
2103 return false;
2104}
2105
2106/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302107 * ufshcd_get_upmcrs - Get the power mode change request status
2108 * @hba: Pointer to adapter instance
2109 *
2110 * This function gets the UPMCRS field of HCS register
2111 * Returns value of UPMCRS field
2112 */
2113static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
2114{
2115 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
2116}
2117
2118/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302119 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
2120 * @hba: per adapter instance
2121 * @uic_cmd: UIC command
2122 *
2123 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302124 */
2125static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302126ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302127{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302128 WARN_ON(hba->active_uic_cmd);
2129
2130 hba->active_uic_cmd = uic_cmd;
2131
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302132 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302133 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
2134 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
2135 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302136
2137 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302138 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302139 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302140}
2141
2142/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302143 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
2144 * @hba: per adapter instance
2145 * @uic_command: UIC command
2146 *
2147 * Must be called with mutex held.
2148 * Returns 0 only if success.
2149 */
2150static int
2151ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2152{
2153 int ret;
2154 unsigned long flags;
2155
2156 if (wait_for_completion_timeout(&uic_cmd->done,
2157 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2158 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2159 else
2160 ret = -ETIMEDOUT;
2161
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002162 if (ret)
2163 ufsdbg_set_err_state(hba);
2164
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302165 spin_lock_irqsave(hba->host->host_lock, flags);
2166 hba->active_uic_cmd = NULL;
2167 spin_unlock_irqrestore(hba->host->host_lock, flags);
2168
2169 return ret;
2170}
2171
2172/**
2173 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2174 * @hba: per adapter instance
2175 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002176 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302177 *
2178 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002179 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302180 * Returns 0 only if success.
2181 */
2182static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002183__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2184 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302185{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302186 if (!ufshcd_ready_for_uic_cmd(hba)) {
2187 dev_err(hba->dev,
2188 "Controller not ready to accept UIC commands\n");
2189 return -EIO;
2190 }
2191
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002192 if (completion)
2193 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302194
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302195 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302196
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002197 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302198}
2199
2200/**
2201 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2202 * @hba: per adapter instance
2203 * @uic_cmd: UIC command
2204 *
2205 * Returns 0 only if success.
2206 */
2207static int
2208ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2209{
2210 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002211 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302212
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002213 ufshcd_hold_all(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302214 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002215 ufshcd_add_delay_before_dme_cmd(hba);
2216
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002217 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002218 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002219 spin_unlock_irqrestore(hba->host->host_lock, flags);
2220 if (!ret)
2221 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2222
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002223 ufshcd_save_tstamp_of_last_dme_cmd(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302224 mutex_unlock(&hba->uic_cmd_mutex);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002225 ufshcd_release_all(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302226
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002227 ufsdbg_error_inject_dispatcher(hba,
2228 ERR_INJECT_UIC, 0, &ret);
2229
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302230 return ret;
2231}
2232
2233/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302234 * ufshcd_map_sg - Map scatter-gather list to prdt
2235 * @lrbp - pointer to local reference block
2236 *
2237 * Returns 0 in case of success, non-zero value in case of failure
2238 */
2239static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
2240{
2241 struct ufshcd_sg_entry *prd_table;
2242 struct scatterlist *sg;
2243 struct scsi_cmnd *cmd;
2244 int sg_segments;
2245 int i;
2246
2247 cmd = lrbp->cmd;
2248 sg_segments = scsi_dma_map(cmd);
2249 if (sg_segments < 0)
2250 return sg_segments;
2251
2252 if (sg_segments) {
2253 lrbp->utr_descriptor_ptr->prd_table_length =
2254 cpu_to_le16((u16) (sg_segments));
2255
2256 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2257
2258 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2259 prd_table[i].size =
2260 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2261 prd_table[i].base_addr =
2262 cpu_to_le32(lower_32_bits(sg->dma_address));
2263 prd_table[i].upper_addr =
2264 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002265 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302266 }
2267 } else {
2268 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2269 }
2270
2271 return 0;
2272}
2273
2274/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302275 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302276 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302277 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302278 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302279static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302280{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302281 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2282
2283 if (hba->ufs_version == UFSHCI_VERSION_10) {
2284 u32 rw;
2285 rw = set & INTERRUPT_MASK_RW_VER_10;
2286 set = rw | ((set ^ intrs) & intrs);
2287 } else {
2288 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302289 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302290
2291 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2292}
2293
2294/**
2295 * ufshcd_disable_intr - disable interrupts
2296 * @hba: per adapter instance
2297 * @intrs: interrupt bits
2298 */
2299static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2300{
2301 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2302
2303 if (hba->ufs_version == UFSHCI_VERSION_10) {
2304 u32 rw;
2305 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2306 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2307 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2308
2309 } else {
2310 set &= ~intrs;
2311 }
2312
2313 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302314}
2315
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002316static int ufshcd_prepare_crypto_utrd(struct ufs_hba *hba,
2317 struct ufshcd_lrb *lrbp)
2318{
2319 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2320 u8 cc_index = 0;
2321 bool enable = false;
2322 u64 dun = 0;
2323 int ret;
2324
2325 /*
2326 * Call vendor specific code to get crypto info for this request:
2327 * enable, crypto config. index, DUN.
2328 * If bypass is set, don't bother setting the other fields.
2329 */
2330 ret = ufshcd_vops_crypto_req_setup(hba, lrbp, &cc_index, &enable, &dun);
2331 if (ret) {
2332 if (ret != -EAGAIN) {
2333 dev_err(hba->dev,
2334 "%s: failed to setup crypto request (%d)\n",
2335 __func__, ret);
2336 }
2337
2338 return ret;
2339 }
2340
2341 if (!enable)
2342 goto out;
2343
2344 req_desc->header.dword_0 |= cc_index | UTRD_CRYPTO_ENABLE;
2345 if (lrbp->cmd->request && lrbp->cmd->request->bio)
2346 dun = lrbp->cmd->request->bio->bi_iter.bi_sector;
2347
2348 req_desc->header.dword_1 = (u32)(dun & 0xFFFFFFFF);
2349 req_desc->header.dword_3 = (u32)((dun >> 32) & 0xFFFFFFFF);
2350out:
2351 return 0;
2352}
2353
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302354/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302355 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2356 * descriptor according to request
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002357 * @hba: per adapter instance
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302358 * @lrbp: pointer to local reference block
2359 * @upiu_flags: flags required in the header
2360 * @cmd_dir: requests data direction
2361 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002362static int ufshcd_prepare_req_desc_hdr(struct ufs_hba *hba,
2363 struct ufshcd_lrb *lrbp, u32 *upiu_flags,
2364 enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302365{
2366 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2367 u32 data_direction;
2368 u32 dword_0;
2369
2370 if (cmd_dir == DMA_FROM_DEVICE) {
2371 data_direction = UTP_DEVICE_TO_HOST;
2372 *upiu_flags = UPIU_CMD_FLAGS_READ;
2373 } else if (cmd_dir == DMA_TO_DEVICE) {
2374 data_direction = UTP_HOST_TO_DEVICE;
2375 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2376 } else {
2377 data_direction = UTP_NO_DATA_TRANSFER;
2378 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2379 }
2380
2381 dword_0 = data_direction | (lrbp->command_type
2382 << UPIU_COMMAND_TYPE_OFFSET);
2383 if (lrbp->intr_cmd)
2384 dword_0 |= UTP_REQ_DESC_INT_CMD;
2385
2386 /* Transfer request descriptor header fields */
2387 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002388 /* dword_1 is reserved, hence it is set to 0 */
2389 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302390 /*
2391 * assigning invalid value for command status. Controller
2392 * updates OCS on command completion, with the command
2393 * status
2394 */
2395 req_desc->header.dword_2 =
2396 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002397 /* dword_3 is reserved, hence it is set to 0 */
2398 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002399
2400 req_desc->prd_table_length = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002401
2402 if (ufshcd_is_crypto_supported(hba))
2403 return ufshcd_prepare_crypto_utrd(hba, lrbp);
2404
2405 return 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302406}
2407
2408/**
2409 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2410 * for scsi commands
2411 * @lrbp - local reference block pointer
2412 * @upiu_flags - flags
2413 */
2414static
2415void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2416{
2417 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002418 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302419
2420 /* command descriptor fields */
2421 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2422 UPIU_TRANSACTION_COMMAND, upiu_flags,
2423 lrbp->lun, lrbp->task_tag);
2424 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2425 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2426
2427 /* Total EHS length and Data segment length will be zero */
2428 ucd_req_ptr->header.dword_2 = 0;
2429
2430 ucd_req_ptr->sc.exp_data_transfer_len =
2431 cpu_to_be32(lrbp->cmd->sdb.length);
2432
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002433 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002434 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002435 if (cdb_len < MAX_CDB_SIZE)
2436 memset(ucd_req_ptr->sc.cdb + cdb_len, 0,
2437 (MAX_CDB_SIZE - cdb_len));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002438 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302439}
2440
Dolev Raviv68078d52013-07-30 00:35:58 +05302441/**
2442 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2443 * for query requsts
2444 * @hba: UFS hba
2445 * @lrbp: local reference block pointer
2446 * @upiu_flags: flags
2447 */
2448static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2449 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2450{
2451 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2452 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302453 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302454 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
2455
2456 /* Query request header */
2457 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2458 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2459 lrbp->lun, lrbp->task_tag);
2460 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2461 0, query->request.query_func, 0, 0);
2462
2463 /* Data segment length */
2464 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
2465 0, 0, len >> 8, (u8)len);
2466
2467 /* Copy the Query Request buffer as is */
2468 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2469 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302470
2471 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002472 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2473 memcpy(descp, query->descriptor, len);
2474
Yaniv Gardi51047262016-02-01 15:02:38 +02002475 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302476}
2477
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302478static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2479{
2480 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2481
2482 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2483
2484 /* command descriptor fields */
2485 ucd_req_ptr->header.dword_0 =
2486 UPIU_HEADER_DWORD(
2487 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002488 /* clear rest of the fields of basic header */
2489 ucd_req_ptr->header.dword_1 = 0;
2490 ucd_req_ptr->header.dword_2 = 0;
2491
2492 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302493}
2494
2495/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002496 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302497 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302498 * @lrb - pointer to local reference block
2499 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002500static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302501{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302502 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302503 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302504
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002505 switch (lrbp->command_type) {
2506 case UTP_CMD_TYPE_SCSI:
2507 if (likely(lrbp->cmd)) {
2508 ret = ufshcd_prepare_req_desc_hdr(hba, lrbp,
2509 &upiu_flags, lrbp->cmd->sc_data_direction);
2510 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2511 } else {
2512 ret = -EINVAL;
2513 }
2514 break;
2515 case UTP_CMD_TYPE_DEV_MANAGE:
2516 ret = ufshcd_prepare_req_desc_hdr(hba, lrbp, &upiu_flags,
2517 DMA_NONE);
2518 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2519 ufshcd_prepare_utp_query_req_upiu(
2520 hba, lrbp, upiu_flags);
2521 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2522 ufshcd_prepare_utp_nop_upiu(lrbp);
2523 else
2524 ret = -EINVAL;
2525 break;
2526 case UTP_CMD_TYPE_UFS:
2527 /* For UFS native command implementation */
2528 ret = -ENOTSUPP;
2529 dev_err(hba->dev, "%s: UFS native command are not supported\n",
2530 __func__);
2531 break;
2532 default:
2533 ret = -ENOTSUPP;
2534 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
2535 __func__, lrbp->command_type);
2536 break;
2537 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302538
2539 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302540}
2541
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002542/*
2543 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2544 * @scsi_lun: scsi LUN id
2545 *
2546 * Returns UPIU LUN id
2547 */
2548static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
2549{
2550 if (scsi_is_wlun(scsi_lun))
2551 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
2552 | UFS_UPIU_WLUN_ID;
2553 else
2554 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
2555}
2556
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302557/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002558 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2559 * @scsi_lun: UPIU W-LUN id
2560 *
2561 * Returns SCSI W-LUN id
2562 */
2563static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2564{
2565 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2566}
2567
2568/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302569 * ufshcd_queuecommand - main entry point for SCSI requests
2570 * @cmd: command from SCSI Midlayer
2571 * @done: call back function
2572 *
2573 * Returns 0 for success, non-zero in case of failure
2574 */
2575static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2576{
2577 struct ufshcd_lrb *lrbp;
2578 struct ufs_hba *hba;
2579 unsigned long flags;
2580 int tag;
2581 int err = 0;
2582
2583 hba = shost_priv(host);
2584
2585 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002586 if (!ufshcd_valid_tag(hba, tag)) {
2587 dev_err(hba->dev,
2588 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2589 __func__, tag, cmd, cmd->request);
2590 BUG();
2591 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302592
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002593 if (!down_read_trylock(&hba->clk_scaling_lock))
2594 return SCSI_MLQUEUE_HOST_BUSY;
2595
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302596 spin_lock_irqsave(hba->host->host_lock, flags);
2597 switch (hba->ufshcd_state) {
2598 case UFSHCD_STATE_OPERATIONAL:
2599 break;
2600 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302601 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302602 goto out_unlock;
2603 case UFSHCD_STATE_ERROR:
2604 set_host_byte(cmd, DID_ERROR);
2605 cmd->scsi_done(cmd);
2606 goto out_unlock;
2607 default:
2608 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2609 __func__, hba->ufshcd_state);
2610 set_host_byte(cmd, DID_BAD_TARGET);
2611 cmd->scsi_done(cmd);
2612 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302613 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002614
2615 /* if error handling is in progress, don't issue commands */
2616 if (ufshcd_eh_in_progress(hba)) {
2617 set_host_byte(cmd, DID_ERROR);
2618 cmd->scsi_done(cmd);
2619 goto out_unlock;
2620 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302621 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302622
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002623 hba->req_abort_count = 0;
2624
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302625 /* acquire the tag to make sure device cmds don't use it */
2626 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2627 /*
2628 * Dev manage command in progress, requeue the command.
2629 * Requeuing the command helps in cases where the request *may*
2630 * find different tag instead of waiting for dev manage command
2631 * completion.
2632 */
2633 err = SCSI_MLQUEUE_HOST_BUSY;
2634 goto out;
2635 }
2636
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002637 err = ufshcd_hold(hba, true);
2638 if (err) {
2639 err = SCSI_MLQUEUE_HOST_BUSY;
2640 clear_bit_unlock(tag, &hba->lrb_in_use);
2641 goto out;
2642 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002643 if (ufshcd_is_clkgating_allowed(hba))
2644 WARN_ON(hba->clk_gating.state != CLKS_ON);
2645
2646 err = ufshcd_hibern8_hold(hba, true);
2647 if (err) {
2648 clear_bit_unlock(tag, &hba->lrb_in_use);
2649 err = SCSI_MLQUEUE_HOST_BUSY;
2650 ufshcd_release(hba, true);
2651 goto out;
2652 }
2653 if (ufshcd_is_hibern8_on_idle_allowed(hba))
2654 WARN_ON(hba->hibern8_on_idle.state != HIBERN8_EXITED);
2655
2656 /* Vote PM QoS for the request */
2657 ufshcd_vops_pm_qos_req_start(hba, cmd->request);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002658
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302659 lrbp = &hba->lrb[tag];
2660
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302661 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302662 lrbp->cmd = cmd;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002663 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302664 lrbp->sense_buffer = cmd->sense_buffer;
2665 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002666 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002667 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002668 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2669 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302670
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002671 /* form UPIU before issuing the command */
2672 err = ufshcd_compose_upiu(hba, lrbp);
2673 if (err) {
2674 if (err != -EAGAIN)
2675 dev_err(hba->dev,
2676 "%s: failed to compose upiu %d\n",
2677 __func__, err);
2678
2679 lrbp->cmd = NULL;
2680 clear_bit_unlock(tag, &hba->lrb_in_use);
2681 ufshcd_release_all(hba);
2682 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2683 goto out;
2684 }
Joao Pinto300bb132016-05-11 12:21:27 +01002685
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302686 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302687 if (err) {
2688 lrbp->cmd = NULL;
2689 clear_bit_unlock(tag, &hba->lrb_in_use);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002690 ufshcd_release_all(hba);
2691 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302692 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302693 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302694
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002695 err = ufshcd_vops_crypto_engine_cfg_start(hba, tag);
2696 if (err) {
2697 if (err != -EAGAIN)
2698 dev_err(hba->dev,
2699 "%s: failed to configure crypto engine %d\n",
2700 __func__, err);
2701
2702 scsi_dma_unmap(lrbp->cmd);
2703 lrbp->cmd = NULL;
2704 clear_bit_unlock(tag, &hba->lrb_in_use);
2705 ufshcd_release_all(hba);
2706 ufshcd_vops_pm_qos_req_end(hba, cmd->request, true);
2707
2708 goto out;
2709 }
2710
2711 /* Make sure descriptors are ready before ringing the doorbell */
2712 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302713 /* issue command to the controller */
2714 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002715
2716 err = ufshcd_send_command(hba, tag);
2717 if (err) {
2718 spin_unlock_irqrestore(hba->host->host_lock, flags);
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 ufshcd_vops_crypto_engine_cfg_end(hba, lrbp, cmd->request);
2725 dev_err(hba->dev, "%s: failed sending command, %d\n",
2726 __func__, err);
2727 err = DID_ERROR;
2728 goto out;
2729 }
2730
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302731out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302732 spin_unlock_irqrestore(hba->host->host_lock, flags);
2733out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002734 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302735 return err;
2736}
2737
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302738static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2739 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2740{
2741 lrbp->cmd = NULL;
2742 lrbp->sense_bufflen = 0;
2743 lrbp->sense_buffer = NULL;
2744 lrbp->task_tag = tag;
2745 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002746 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302747 lrbp->intr_cmd = true; /* No interrupt aggregation */
2748 hba->dev_cmd.type = cmd_type;
2749
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002750 return ufshcd_compose_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302751}
2752
2753static int
2754ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2755{
2756 int err = 0;
2757 unsigned long flags;
2758 u32 mask = 1 << tag;
2759
2760 /* clear outstanding transaction before retry */
2761 spin_lock_irqsave(hba->host->host_lock, flags);
2762 ufshcd_utrl_clear(hba, tag);
2763 spin_unlock_irqrestore(hba->host->host_lock, flags);
2764
2765 /*
2766 * wait for for h/w to clear corresponding bit in door-bell.
2767 * max. wait is 1 sec.
2768 */
2769 err = ufshcd_wait_for_register(hba,
2770 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002771 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302772
2773 return err;
2774}
2775
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002776static int
2777ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2778{
2779 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2780
2781 /* Get the UPIU response */
2782 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2783 UPIU_RSP_CODE_OFFSET;
2784 return query_res->response;
2785}
2786
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302787/**
2788 * ufshcd_dev_cmd_completion() - handles device management command responses
2789 * @hba: per adapter instance
2790 * @lrbp: pointer to local reference block
2791 */
2792static int
2793ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2794{
2795 int resp;
2796 int err = 0;
2797
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002798 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302799 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2800
2801 switch (resp) {
2802 case UPIU_TRANSACTION_NOP_IN:
2803 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2804 err = -EINVAL;
2805 dev_err(hba->dev, "%s: unexpected response %x\n",
2806 __func__, resp);
2807 }
2808 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302809 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002810 err = ufshcd_check_query_response(hba, lrbp);
2811 if (!err)
2812 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302813 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302814 case UPIU_TRANSACTION_REJECT_UPIU:
2815 /* TODO: handle Reject UPIU Response */
2816 err = -EPERM;
2817 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2818 __func__);
2819 break;
2820 default:
2821 err = -EINVAL;
2822 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2823 __func__, resp);
2824 break;
2825 }
2826
2827 return err;
2828}
2829
2830static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2831 struct ufshcd_lrb *lrbp, int max_timeout)
2832{
2833 int err = 0;
2834 unsigned long time_left;
2835 unsigned long flags;
2836
2837 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2838 msecs_to_jiffies(max_timeout));
2839
2840 spin_lock_irqsave(hba->host->host_lock, flags);
2841 hba->dev_cmd.complete = NULL;
2842 if (likely(time_left)) {
2843 err = ufshcd_get_tr_ocs(lrbp);
2844 if (!err)
2845 err = ufshcd_dev_cmd_completion(hba, lrbp);
2846 }
2847 spin_unlock_irqrestore(hba->host->host_lock, flags);
2848
2849 if (!time_left) {
2850 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002851 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2852 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302853 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002854 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302855 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002856 /*
2857 * in case of an error, after clearing the doorbell,
2858 * we also need to clear the outstanding_request
2859 * field in hba
2860 */
2861 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302862 }
2863
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002864 if (err)
2865 ufsdbg_set_err_state(hba);
2866
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302867 return err;
2868}
2869
2870/**
2871 * ufshcd_get_dev_cmd_tag - Get device management command tag
2872 * @hba: per-adapter instance
2873 * @tag: pointer to variable with available slot value
2874 *
2875 * Get a free slot and lock it until device management command
2876 * completes.
2877 *
2878 * Returns false if free slot is unavailable for locking, else
2879 * return true with tag value in @tag.
2880 */
2881static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
2882{
2883 int tag;
2884 bool ret = false;
2885 unsigned long tmp;
2886
2887 if (!tag_out)
2888 goto out;
2889
2890 do {
2891 tmp = ~hba->lrb_in_use;
2892 tag = find_last_bit(&tmp, hba->nutrs);
2893 if (tag >= hba->nutrs)
2894 goto out;
2895 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
2896
2897 *tag_out = tag;
2898 ret = true;
2899out:
2900 return ret;
2901}
2902
2903static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
2904{
2905 clear_bit_unlock(tag, &hba->lrb_in_use);
2906}
2907
2908/**
2909 * ufshcd_exec_dev_cmd - API for sending device management requests
2910 * @hba - UFS hba
2911 * @cmd_type - specifies the type (NOP, Query...)
2912 * @timeout - time in seconds
2913 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302914 * NOTE: Since there is only one available tag for device management commands,
2915 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302916 */
2917static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2918 enum dev_cmd_type cmd_type, int timeout)
2919{
2920 struct ufshcd_lrb *lrbp;
2921 int err;
2922 int tag;
2923 struct completion wait;
2924 unsigned long flags;
2925
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002926 down_read(&hba->clk_scaling_lock);
2927
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302928 /*
2929 * Get free slot, sleep if slots are unavailable.
2930 * Even though we use wait_event() which sleeps indefinitely,
2931 * the maximum wait time is bounded by SCSI request timeout.
2932 */
2933 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
2934
2935 init_completion(&wait);
2936 lrbp = &hba->lrb[tag];
2937 WARN_ON(lrbp->cmd);
2938 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2939 if (unlikely(err))
2940 goto out_put_tag;
2941
2942 hba->dev_cmd.complete = &wait;
2943
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002944 /* Make sure descriptors are ready before ringing the doorbell */
2945 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302946 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002947 err = ufshcd_send_command(hba, tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302948 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002949 if (err) {
2950 dev_err(hba->dev, "%s: failed sending command, %d\n",
2951 __func__, err);
2952 goto out_put_tag;
2953 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302954 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2955
2956out_put_tag:
2957 ufshcd_put_dev_cmd_tag(hba, tag);
2958 wake_up(&hba->dev_cmd.tag_wq);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002959 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302960 return err;
2961}
2962
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302963/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002964 * ufshcd_init_query() - init the query response and request parameters
2965 * @hba: per-adapter instance
2966 * @request: address of the request pointer to be initialized
2967 * @response: address of the response pointer to be initialized
2968 * @opcode: operation to perform
2969 * @idn: flag idn to access
2970 * @index: LU number to access
2971 * @selector: query/flag/descriptor further identification
2972 */
2973static inline void ufshcd_init_query(struct ufs_hba *hba,
2974 struct ufs_query_req **request, struct ufs_query_res **response,
2975 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2976{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002977 int idn_t = (int)idn;
2978
2979 ufsdbg_error_inject_dispatcher(hba,
2980 ERR_INJECT_QUERY, idn_t, (int *)&idn_t);
2981 idn = idn_t;
2982
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002983 *request = &hba->dev_cmd.query.request;
2984 *response = &hba->dev_cmd.query.response;
2985 memset(*request, 0, sizeof(struct ufs_query_req));
2986 memset(*response, 0, sizeof(struct ufs_query_res));
2987 (*request)->upiu_req.opcode = opcode;
2988 (*request)->upiu_req.idn = idn;
2989 (*request)->upiu_req.index = index;
2990 (*request)->upiu_req.selector = selector;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07002991
2992 ufshcd_update_query_stats(hba, opcode, idn);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002993}
2994
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002995static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2996 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2997{
2998 int ret;
2999 int retries;
3000
3001 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
3002 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
3003 if (ret)
3004 dev_dbg(hba->dev,
3005 "%s: failed with error %d, retries %d\n",
3006 __func__, ret, retries);
3007 else
3008 break;
3009 }
3010
3011 if (ret)
3012 dev_err(hba->dev,
3013 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
3014 __func__, opcode, idn, ret, retries);
3015 return ret;
3016}
3017
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003018/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303019 * ufshcd_query_flag() - API function for sending flag query requests
3020 * hba: per-adapter instance
3021 * query_opcode: flag query to perform
3022 * idn: flag idn to access
3023 * flag_res: the flag value after the query request completes
3024 *
3025 * Returns 0 for success, non-zero in case of failure
3026 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003027int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05303028 enum flag_idn idn, bool *flag_res)
3029{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003030 struct ufs_query_req *request = NULL;
3031 struct ufs_query_res *response = NULL;
3032 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003033 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05303034
3035 BUG_ON(!hba);
3036
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003037 ufshcd_hold_all(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303038 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003039 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3040 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05303041
3042 switch (opcode) {
3043 case UPIU_QUERY_OPCODE_SET_FLAG:
3044 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
3045 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
3046 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3047 break;
3048 case UPIU_QUERY_OPCODE_READ_FLAG:
3049 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3050 if (!flag_res) {
3051 /* No dummy reads */
3052 dev_err(hba->dev, "%s: Invalid argument for read request\n",
3053 __func__);
3054 err = -EINVAL;
3055 goto out_unlock;
3056 }
3057 break;
3058 default:
3059 dev_err(hba->dev,
3060 "%s: Expected query flag opcode but got = %d\n",
3061 __func__, opcode);
3062 err = -EINVAL;
3063 goto out_unlock;
3064 }
Dolev Raviv68078d52013-07-30 00:35:58 +05303065
Yaniv Gardie5ad4062016-02-01 15:02:41 +02003066 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05303067
3068 if (err) {
3069 dev_err(hba->dev,
3070 "%s: Sending flag query for idn %d failed, err = %d\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003071 __func__, request->upiu_req.idn, err);
Dolev Raviv68078d52013-07-30 00:35:58 +05303072 goto out_unlock;
3073 }
3074
3075 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303076 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05303077 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
3078
3079out_unlock:
3080 mutex_unlock(&hba->dev_cmd.lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003081 ufshcd_release_all(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05303082 return err;
3083}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003084EXPORT_SYMBOL(ufshcd_query_flag);
Dolev Raviv68078d52013-07-30 00:35:58 +05303085
3086/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303087 * ufshcd_query_attr - API function for sending attribute requests
3088 * hba: per-adapter instance
3089 * opcode: attribute opcode
3090 * idn: attribute idn to access
3091 * index: index field
3092 * selector: selector field
3093 * attr_val: the attribute value after the query request completes
3094 *
3095 * Returns 0 for success, non-zero in case of failure
3096*/
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003097int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303098 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
3099{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003100 struct ufs_query_req *request = NULL;
3101 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303102 int err;
3103
3104 BUG_ON(!hba);
3105
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003106 ufshcd_hold_all(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303107 if (!attr_val) {
3108 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
3109 __func__, opcode);
3110 err = -EINVAL;
3111 goto out;
3112 }
3113
3114 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003115 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3116 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303117
3118 switch (opcode) {
3119 case UPIU_QUERY_OPCODE_WRITE_ATTR:
3120 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303121 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303122 break;
3123 case UPIU_QUERY_OPCODE_READ_ATTR:
3124 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3125 break;
3126 default:
3127 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
3128 __func__, opcode);
3129 err = -EINVAL;
3130 goto out_unlock;
3131 }
3132
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003133 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303134
3135 if (err) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003136 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3137 __func__, opcode,
3138 request->upiu_req.idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303139 goto out_unlock;
3140 }
3141
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05303142 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303143
3144out_unlock:
3145 mutex_unlock(&hba->dev_cmd.lock);
3146out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003147 ufshcd_release_all(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303148 return err;
3149}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003150EXPORT_SYMBOL(ufshcd_query_attr);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303151
3152/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003153 * ufshcd_query_attr_retry() - API function for sending query
3154 * attribute with retries
3155 * @hba: per-adapter instance
3156 * @opcode: attribute opcode
3157 * @idn: attribute idn to access
3158 * @index: index field
3159 * @selector: selector field
3160 * @attr_val: the attribute value after the query request
3161 * completes
3162 *
3163 * Returns 0 for success, non-zero in case of failure
3164*/
3165static int ufshcd_query_attr_retry(struct ufs_hba *hba,
3166 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
3167 u32 *attr_val)
3168{
3169 int ret = 0;
3170 u32 retries;
3171
3172 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3173 ret = ufshcd_query_attr(hba, opcode, idn, index,
3174 selector, attr_val);
3175 if (ret)
3176 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
3177 __func__, ret, retries);
3178 else
3179 break;
3180 }
3181
3182 if (ret)
3183 dev_err(hba->dev,
3184 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003185 __func__, idn, ret, retries);
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003186 return ret;
3187}
3188
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003189static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003190 enum query_opcode opcode, enum desc_idn idn, u8 index,
3191 u8 selector, u8 *desc_buf, int *buf_len)
3192{
3193 struct ufs_query_req *request = NULL;
3194 struct ufs_query_res *response = NULL;
3195 int err;
3196
3197 BUG_ON(!hba);
3198
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003199 ufshcd_hold_all(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003200 if (!desc_buf) {
3201 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
3202 __func__, opcode);
3203 err = -EINVAL;
3204 goto out;
3205 }
3206
3207 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
3208 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
3209 __func__, *buf_len);
3210 err = -EINVAL;
3211 goto out;
3212 }
3213
3214 mutex_lock(&hba->dev_cmd.lock);
3215 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
3216 selector);
3217 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003218 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003219
3220 switch (opcode) {
3221 case UPIU_QUERY_OPCODE_WRITE_DESC:
3222 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
3223 break;
3224 case UPIU_QUERY_OPCODE_READ_DESC:
3225 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
3226 break;
3227 default:
3228 dev_err(hba->dev,
3229 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
3230 __func__, opcode);
3231 err = -EINVAL;
3232 goto out_unlock;
3233 }
3234
3235 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
3236
3237 if (err) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003238 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
3239 __func__, opcode,
3240 request->upiu_req.idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003241 goto out_unlock;
3242 }
3243
3244 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03003245 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003246
3247out_unlock:
3248 mutex_unlock(&hba->dev_cmd.lock);
3249out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003250 ufshcd_release_all(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03003251 return err;
3252}
3253
3254/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003255 * ufshcd_query_descriptor - API function for sending descriptor requests
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003256 * hba: per-adapter instance
3257 * opcode: attribute opcode
3258 * idn: attribute idn to access
3259 * index: index field
3260 * selector: selector field
3261 * desc_buf: the buffer that contains the descriptor
3262 * buf_len: length parameter passed to the device
3263 *
3264 * Returns 0 for success, non-zero in case of failure.
3265 * The buf_len parameter will contain, on return, the length parameter
3266 * received on the response.
3267 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003268int ufshcd_query_descriptor(struct ufs_hba *hba,
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003269 enum query_opcode opcode, enum desc_idn idn, u8 index,
3270 u8 selector, u8 *desc_buf, int *buf_len)
3271{
3272 int err;
3273 int retries;
3274
3275 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3276 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
3277 selector, desc_buf, buf_len);
3278 if (!err || err == -EINVAL)
3279 break;
3280 }
3281
3282 return err;
3283}
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003284EXPORT_SYMBOL(ufshcd_query_descriptor);
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003285
3286/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003287 * ufshcd_read_desc_param - read the specified descriptor parameter
3288 * @hba: Pointer to adapter instance
3289 * @desc_id: descriptor idn value
3290 * @desc_index: descriptor index
3291 * @param_offset: offset of the parameter to read
3292 * @param_read_buf: pointer to buffer where parameter would be read
3293 * @param_size: sizeof(param_read_buf)
3294 *
3295 * Return 0 in case of success, non-zero otherwise
3296 */
3297static int ufshcd_read_desc_param(struct ufs_hba *hba,
3298 enum desc_idn desc_id,
3299 int desc_index,
3300 u32 param_offset,
3301 u8 *param_read_buf,
3302 u32 param_size)
3303{
3304 int ret;
3305 u8 *desc_buf;
3306 u32 buff_len;
3307 bool is_kmalloc = true;
3308
3309 /* safety checks */
3310 if (desc_id >= QUERY_DESC_IDN_MAX)
3311 return -EINVAL;
3312
3313 buff_len = ufs_query_desc_max_size[desc_id];
3314 if ((param_offset + param_size) > buff_len)
3315 return -EINVAL;
3316
3317 if (!param_offset && (param_size == buff_len)) {
3318 /* memory space already available to hold full descriptor */
3319 desc_buf = param_read_buf;
3320 is_kmalloc = false;
3321 } else {
3322 /* allocate memory to hold full descriptor */
3323 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3324 if (!desc_buf)
3325 return -ENOMEM;
3326 }
3327
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003328 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
3329 desc_id, desc_index, 0, desc_buf,
3330 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003331
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003332 if (ret) {
3333 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3334 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003335
3336 goto out;
3337 }
3338
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003339 /* Sanity check */
3340 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3341 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3342 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3343 ret = -EINVAL;
3344 goto out;
3345 }
3346
3347 /*
3348 * While reading variable size descriptors (like string descriptor),
3349 * some UFS devices may report the "LENGTH" (field in "Transaction
3350 * Specific fields" of Query Response UPIU) same as what was requested
3351 * in Query Request UPIU instead of reporting the actual size of the
3352 * variable size descriptor.
3353 * Although it's safe to ignore the "LENGTH" field for variable size
3354 * descriptors as we can always derive the length of the descriptor from
3355 * the descriptor header fields. Hence this change impose the length
3356 * match check only for fixed size descriptors (for which we always
3357 * request the correct size as part of Query Request UPIU).
3358 */
3359 if ((desc_id != QUERY_DESC_IDN_STRING) &&
3360 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
3361 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
3362 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
3363 ret = -EINVAL;
3364 goto out;
3365 }
3366
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003367 if (is_kmalloc)
3368 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3369out:
3370 if (is_kmalloc)
3371 kfree(desc_buf);
3372 return ret;
3373}
3374
3375static inline int ufshcd_read_desc(struct ufs_hba *hba,
3376 enum desc_idn desc_id,
3377 int desc_index,
3378 u8 *buf,
3379 u32 size)
3380{
3381 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3382}
3383
3384static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3385 u8 *buf,
3386 u32 size)
3387{
3388 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
3389}
3390
Yaniv Gardib573d482016-03-10 17:37:09 +02003391int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
3392{
3393 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3394}
Yaniv Gardib573d482016-03-10 17:37:09 +02003395
3396/**
3397 * ufshcd_read_string_desc - read string descriptor
3398 * @hba: pointer to adapter instance
3399 * @desc_index: descriptor index
3400 * @buf: pointer to buffer where descriptor would be read
3401 * @size: size of buf
3402 * @ascii: if true convert from unicode to ascii characters
3403 *
3404 * Return 0 in case of success, non-zero otherwise
3405 */
3406int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
3407 u32 size, bool ascii)
3408{
3409 int err = 0;
3410
3411 err = ufshcd_read_desc(hba,
3412 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3413
3414 if (err) {
3415 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3416 __func__, QUERY_REQ_RETRIES, err);
3417 goto out;
3418 }
3419
3420 if (ascii) {
3421 int desc_len;
3422 int ascii_len;
3423 int i;
3424 char *buff_ascii;
3425
3426 desc_len = buf[0];
3427 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3428 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3429 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3430 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3431 __func__);
3432 err = -ENOMEM;
3433 goto out;
3434 }
3435
3436 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3437 if (!buff_ascii) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003438 dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
3439 __func__, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003440 err = -ENOMEM;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003441 goto out_free_buff;
Yaniv Gardib573d482016-03-10 17:37:09 +02003442 }
3443
3444 /*
3445 * the descriptor contains string in UTF16 format
3446 * we need to convert to utf-8 so it can be displayed
3447 */
3448 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3449 desc_len - QUERY_DESC_HDR_SIZE,
3450 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3451
3452 /* replace non-printable or non-ASCII characters with spaces */
3453 for (i = 0; i < ascii_len; i++)
3454 ufshcd_remove_non_printable(&buff_ascii[i]);
3455
3456 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3457 size - QUERY_DESC_HDR_SIZE);
3458 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3459 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003460out_free_buff:
Yaniv Gardib573d482016-03-10 17:37:09 +02003461 kfree(buff_ascii);
3462 }
3463out:
3464 return err;
3465}
Yaniv Gardib573d482016-03-10 17:37:09 +02003466
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003467/**
3468 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3469 * @hba: Pointer to adapter instance
3470 * @lun: lun id
3471 * @param_offset: offset of the parameter to read
3472 * @param_read_buf: pointer to buffer where parameter would be read
3473 * @param_size: sizeof(param_read_buf)
3474 *
3475 * Return 0 in case of success, non-zero otherwise
3476 */
3477static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3478 int lun,
3479 enum unit_desc_param param_offset,
3480 u8 *param_read_buf,
3481 u32 param_size)
3482{
3483 /*
3484 * Unit descriptors are only available for general purpose LUs (LUN id
3485 * from 0 to 7) and RPMB Well known LU.
3486 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003487 if (!ufs_is_valid_unit_desc_lun(lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003488 return -EOPNOTSUPP;
3489
3490 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3491 param_offset, param_read_buf, param_size);
3492}
3493
3494/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303495 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3496 * @hba: per adapter instance
3497 *
3498 * 1. Allocate DMA memory for Command Descriptor array
3499 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3500 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3501 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3502 * (UTMRDL)
3503 * 4. Allocate memory for local reference block(lrb).
3504 *
3505 * Returns 0 for success, non-zero in case of failure
3506 */
3507static int ufshcd_memory_alloc(struct ufs_hba *hba)
3508{
3509 size_t utmrdl_size, utrdl_size, ucdl_size;
3510
3511 /* Allocate memory for UTP command descriptors */
3512 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003513 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3514 ucdl_size,
3515 &hba->ucdl_dma_addr,
3516 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303517
3518 /*
3519 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3520 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3521 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3522 * be aligned to 128 bytes as well
3523 */
3524 if (!hba->ucdl_base_addr ||
3525 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303526 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303527 "Command Descriptor Memory allocation failed\n");
3528 goto out;
3529 }
3530
3531 /*
3532 * Allocate memory for UTP Transfer descriptors
3533 * UFSHCI requires 1024 byte alignment of UTRD
3534 */
3535 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003536 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3537 utrdl_size,
3538 &hba->utrdl_dma_addr,
3539 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303540 if (!hba->utrdl_base_addr ||
3541 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303542 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303543 "Transfer Descriptor Memory allocation failed\n");
3544 goto out;
3545 }
3546
3547 /*
3548 * Allocate memory for UTP Task Management descriptors
3549 * UFSHCI requires 1024 byte alignment of UTMRD
3550 */
3551 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003552 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3553 utmrdl_size,
3554 &hba->utmrdl_dma_addr,
3555 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303556 if (!hba->utmrdl_base_addr ||
3557 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303558 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303559 "Task Management Descriptor Memory allocation failed\n");
3560 goto out;
3561 }
3562
3563 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003564 hba->lrb = devm_kzalloc(hba->dev,
3565 hba->nutrs * sizeof(struct ufshcd_lrb),
3566 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303567 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303568 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303569 goto out;
3570 }
3571 return 0;
3572out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303573 return -ENOMEM;
3574}
3575
3576/**
3577 * ufshcd_host_memory_configure - configure local reference block with
3578 * memory offsets
3579 * @hba: per adapter instance
3580 *
3581 * Configure Host memory space
3582 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3583 * address.
3584 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3585 * and PRDT offset.
3586 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3587 * into local reference block.
3588 */
3589static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3590{
3591 struct utp_transfer_cmd_desc *cmd_descp;
3592 struct utp_transfer_req_desc *utrdlp;
3593 dma_addr_t cmd_desc_dma_addr;
3594 dma_addr_t cmd_desc_element_addr;
3595 u16 response_offset;
3596 u16 prdt_offset;
3597 int cmd_desc_size;
3598 int i;
3599
3600 utrdlp = hba->utrdl_base_addr;
3601 cmd_descp = hba->ucdl_base_addr;
3602
3603 response_offset =
3604 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3605 prdt_offset =
3606 offsetof(struct utp_transfer_cmd_desc, prd_table);
3607
3608 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3609 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3610
3611 for (i = 0; i < hba->nutrs; i++) {
3612 /* Configure UTRD with command descriptor base address */
3613 cmd_desc_element_addr =
3614 (cmd_desc_dma_addr + (cmd_desc_size * i));
3615 utrdlp[i].command_desc_base_addr_lo =
3616 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3617 utrdlp[i].command_desc_base_addr_hi =
3618 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3619
3620 /* Response upiu and prdt offset should be in double words */
3621 utrdlp[i].response_upiu_offset =
3622 cpu_to_le16((response_offset >> 2));
3623 utrdlp[i].prd_table_offset =
3624 cpu_to_le16((prdt_offset >> 2));
3625 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303626 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303627
3628 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003629 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3630 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303631 hba->lrb[i].ucd_req_ptr =
3632 (struct utp_upiu_req *)(cmd_descp + i);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003633 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303634 hba->lrb[i].ucd_rsp_ptr =
3635 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003636 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3637 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303638 hba->lrb[i].ucd_prdt_ptr =
3639 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003640 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3641 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303642 }
3643}
3644
3645/**
3646 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3647 * @hba: per adapter instance
3648 *
3649 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3650 * in order to initialize the Unipro link startup procedure.
3651 * Once the Unipro links are up, the device connected to the controller
3652 * is detected.
3653 *
3654 * Returns 0 on success, non-zero value on failure
3655 */
3656static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3657{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303658 struct uic_command uic_cmd = {0};
3659 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303660
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303661 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3662
3663 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3664 if (ret)
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003665 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303666 "dme-link-startup: error code %d\n", ret);
3667 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303668}
3669
Yaniv Gardicad2e032015-03-31 17:37:14 +03003670static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3671{
3672 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3673 unsigned long min_sleep_time_us;
3674
3675 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3676 return;
3677
3678 /*
3679 * last_dme_cmd_tstamp will be 0 only for 1st call to
3680 * this function
3681 */
3682 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3683 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3684 } else {
3685 unsigned long delta =
3686 (unsigned long) ktime_to_us(
3687 ktime_sub(ktime_get(),
3688 hba->last_dme_cmd_tstamp));
3689
3690 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3691 min_sleep_time_us =
3692 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3693 else
3694 return; /* no more delay required */
3695 }
3696
3697 /* allow sleep for extra 50us if needed */
3698 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3699}
3700
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003701static inline void ufshcd_save_tstamp_of_last_dme_cmd(
3702 struct ufs_hba *hba)
3703{
3704 if (hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)
3705 hba->last_dme_cmd_tstamp = ktime_get();
3706}
3707
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303708/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303709 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3710 * @hba: per adapter instance
3711 * @attr_sel: uic command argument1
3712 * @attr_set: attribute set type as uic command argument2
3713 * @mib_val: setting value as uic command argument3
3714 * @peer: indicate whether peer or local
3715 *
3716 * Returns 0 on success, non-zero value on failure
3717 */
3718int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3719 u8 attr_set, u32 mib_val, u8 peer)
3720{
3721 struct uic_command uic_cmd = {0};
3722 static const char *const action[] = {
3723 "dme-set",
3724 "dme-peer-set"
3725 };
3726 const char *set = action[!!peer];
3727 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003728 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303729
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003730 ufsdbg_error_inject_dispatcher(hba,
3731 ERR_INJECT_DME_ATTR, attr_sel, &attr_sel);
3732
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303733 uic_cmd.command = peer ?
3734 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3735 uic_cmd.argument1 = attr_sel;
3736 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3737 uic_cmd.argument3 = mib_val;
3738
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003739 do {
3740 /* for peer attributes we retry upon failure */
3741 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3742 if (ret)
3743 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3744 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3745 } while (ret && peer && --retries);
3746
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003747 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003748 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003749 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3750 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303751
3752 return ret;
3753}
3754EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3755
3756/**
3757 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3758 * @hba: per adapter instance
3759 * @attr_sel: uic command argument1
3760 * @mib_val: the value of the attribute as returned by the UIC command
3761 * @peer: indicate whether peer or local
3762 *
3763 * Returns 0 on success, non-zero value on failure
3764 */
3765int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3766 u32 *mib_val, u8 peer)
3767{
3768 struct uic_command uic_cmd = {0};
3769 static const char *const action[] = {
3770 "dme-get",
3771 "dme-peer-get"
3772 };
3773 const char *get = action[!!peer];
3774 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003775 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003776 struct ufs_pa_layer_attr orig_pwr_info;
3777 struct ufs_pa_layer_attr temp_pwr_info;
3778 bool pwr_mode_change = false;
3779
3780 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3781 orig_pwr_info = hba->pwr_info;
3782 temp_pwr_info = orig_pwr_info;
3783
3784 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3785 orig_pwr_info.pwr_rx == FAST_MODE) {
3786 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3787 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3788 pwr_mode_change = true;
3789 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3790 orig_pwr_info.pwr_rx == SLOW_MODE) {
3791 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3792 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3793 pwr_mode_change = true;
3794 }
3795 if (pwr_mode_change) {
3796 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3797 if (ret)
3798 goto out;
3799 }
3800 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303801
3802 uic_cmd.command = peer ?
3803 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003804
3805 ufsdbg_error_inject_dispatcher(hba,
3806 ERR_INJECT_DME_ATTR, attr_sel, &attr_sel);
3807
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303808 uic_cmd.argument1 = attr_sel;
3809
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003810 do {
3811 /* for peer attributes we retry upon failure */
3812 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3813 if (ret)
3814 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3815 get, UIC_GET_ATTR_ID(attr_sel), ret);
3816 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303817
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003818 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003819 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003820 get, UIC_GET_ATTR_ID(attr_sel),
3821 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003822
3823 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303824 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003825
3826 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3827 && pwr_mode_change)
3828 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303829out:
3830 return ret;
3831}
3832EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3833
3834/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003835 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3836 * state) and waits for it to take effect.
3837 *
3838 * @hba: per adapter instance
3839 * @cmd: UIC command to execute
3840 *
3841 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3842 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3843 * and device UniPro link and hence it's final completion would be indicated by
3844 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3845 * addition to normal UIC command completion Status (UCCS). This function only
3846 * returns after the relevant status bits indicate the completion.
3847 *
3848 * Returns 0 on success, non-zero value on failure
3849 */
3850static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3851{
3852 struct completion uic_async_done;
3853 unsigned long flags;
3854 u8 status;
3855 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003856 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003857
3858 mutex_lock(&hba->uic_cmd_mutex);
3859 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003860 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003861
3862 spin_lock_irqsave(hba->host->host_lock, flags);
3863 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003864 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3865 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3866 /*
3867 * Make sure UIC command completion interrupt is disabled before
3868 * issuing UIC command.
3869 */
3870 wmb();
3871 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003872 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003873 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3874 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003875 if (ret) {
3876 dev_err(hba->dev,
3877 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3878 cmd->command, cmd->argument3, ret);
3879 goto out;
3880 }
3881
3882 if (!wait_for_completion_timeout(hba->uic_async_done,
3883 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3884 dev_err(hba->dev,
3885 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3886 cmd->command, cmd->argument3);
3887 ret = -ETIMEDOUT;
3888 goto out;
3889 }
3890
3891 status = ufshcd_get_upmcrs(hba);
3892 if (status != PWR_LOCAL) {
3893 dev_err(hba->dev,
3894 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
3895 cmd->command, status);
3896 ret = (status != PWR_OK) ? status : -1;
3897 }
3898out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003899 if (ret)
3900 ufsdbg_set_err_state(hba);
3901
3902 ufshcd_save_tstamp_of_last_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003903 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003904 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003905 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003906 if (reenable_intr)
3907 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003908 spin_unlock_irqrestore(hba->host->host_lock, flags);
3909 mutex_unlock(&hba->uic_cmd_mutex);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003910 return ret;
3911}
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003912
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003913int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, u64 wait_timeout_us)
3914{
3915 unsigned long flags;
3916 int ret = 0;
3917 u32 tm_doorbell;
3918 u32 tr_doorbell;
3919 bool timeout = false, do_last_check = false;
3920 ktime_t start;
3921
3922 ufshcd_hold_all(hba);
3923 spin_lock_irqsave(hba->host->host_lock, flags);
3924 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
3925 ret = -EBUSY;
3926 goto out;
3927 }
3928
3929 /*
3930 * Wait for all the outstanding tasks/transfer requests.
3931 * Verify by checking the doorbell registers are clear.
3932 */
3933 start = ktime_get();
3934 do {
3935 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
3936 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3937 if (!tm_doorbell && !tr_doorbell) {
3938 timeout = false;
3939 break;
3940 } else if (do_last_check) {
3941 break;
3942 }
3943
3944 spin_unlock_irqrestore(hba->host->host_lock, flags);
3945 schedule();
3946 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
3947 wait_timeout_us) {
3948 timeout = true;
3949 /*
3950 * We might have scheduled out for long time so make
3951 * sure to check if doorbells are cleared by this time
3952 * or not.
3953 */
3954 do_last_check = true;
3955 }
3956 spin_lock_irqsave(hba->host->host_lock, flags);
3957 } while (tm_doorbell || tr_doorbell);
3958
3959 if (timeout) {
3960 dev_err(hba->dev,
3961 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
3962 __func__, tm_doorbell, tr_doorbell);
3963 ret = -EBUSY;
3964 }
3965out:
3966 spin_unlock_irqrestore(hba->host->host_lock, flags);
3967 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003968 return ret;
3969}
3970
3971/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303972 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3973 * using DME_SET primitives.
3974 * @hba: per adapter instance
3975 * @mode: powr mode value
3976 *
3977 * Returns 0 on success, non-zero value on failure
3978 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303979static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303980{
3981 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003982 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303983
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003984 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3985 ret = ufshcd_dme_set(hba,
3986 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3987 if (ret) {
3988 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3989 __func__, ret);
3990 goto out;
3991 }
3992 }
3993
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303994 uic_cmd.command = UIC_CMD_DME_SET;
3995 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3996 uic_cmd.argument3 = mode;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003997 ufshcd_hold_all(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003998 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07003999 ufshcd_release_all(hba);
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03004000out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004001 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004002}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304003
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004004static int ufshcd_link_recovery(struct ufs_hba *hba)
4005{
4006 int ret;
4007 unsigned long flags;
4008
4009 spin_lock_irqsave(hba->host->host_lock, flags);
4010 hba->ufshcd_state = UFSHCD_STATE_RESET;
4011 ufshcd_set_eh_in_progress(hba);
4012 spin_unlock_irqrestore(hba->host->host_lock, flags);
4013
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004014 ret = ufshcd_vops_full_reset(hba);
4015 if (ret)
4016 dev_warn(hba->dev,
4017 "full reset returned %d, trying to recover the link\n",
4018 ret);
4019
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004020 ret = ufshcd_host_reset_and_restore(hba);
4021
4022 spin_lock_irqsave(hba->host->host_lock, flags);
4023 if (ret)
4024 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4025 ufshcd_clear_eh_in_progress(hba);
4026 spin_unlock_irqrestore(hba->host->host_lock, flags);
4027
4028 if (ret)
4029 dev_err(hba->dev, "%s: link recovery failed, err %d",
4030 __func__, ret);
4031
4032 return ret;
4033}
4034
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004035static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004036{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004037 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004038 struct uic_command uic_cmd = {0};
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004039 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004040
4041 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004042 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004043 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
4044 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004045
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004046 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004047 ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_ENTER);
4048 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d",
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004049 __func__, ret);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004050 /*
4051 * If link recovery fails then return error so that caller
4052 * don't retry the hibern8 enter again.
4053 */
4054 if (ufshcd_link_recovery(hba))
4055 ret = -ENOLINK;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004056 } else {
4057 dev_dbg(hba->dev, "%s: Hibern8 Enter at %lld us", __func__,
4058 ktime_to_us(ktime_get()));
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004059 }
4060
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004061 return ret;
4062}
4063
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004064int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004065{
4066 int ret = 0, retries;
4067
4068 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
4069 ret = __ufshcd_uic_hibern8_enter(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004070 if (!ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004071 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004072 /* Unable to recover the link, so no point proceeding */
4073 if (ret == -ENOLINK)
4074 BUG();
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02004075 }
4076out:
4077 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004078}
4079
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004080int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004081{
4082 struct uic_command uic_cmd = {0};
4083 int ret;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004084 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004085
4086 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
4087 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004088 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
4089 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
4090
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304091 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004092 ufshcd_update_error_stats(hba, UFS_ERR_HIBERN8_EXIT);
4093 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d",
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004094 __func__, ret);
4095 ret = ufshcd_link_recovery(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004096 /* Unable to recover the link, so no point proceeding */
4097 if (ret)
4098 BUG();
4099 } else {
4100 dev_dbg(hba->dev, "%s: Hibern8 Exit at %lld us", __func__,
4101 ktime_to_us(ktime_get()));
4102 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
4103 hba->ufs_stats.hibern8_exit_cnt++;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304104 }
4105
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304106 return ret;
4107}
4108
Yaniv Gardi50646362014-10-23 13:25:13 +03004109 /**
4110 * ufshcd_init_pwr_info - setting the POR (power on reset)
4111 * values in hba power info
4112 * @hba: per-adapter instance
4113 */
4114static void ufshcd_init_pwr_info(struct ufs_hba *hba)
4115{
4116 hba->pwr_info.gear_rx = UFS_PWM_G1;
4117 hba->pwr_info.gear_tx = UFS_PWM_G1;
4118 hba->pwr_info.lane_rx = 1;
4119 hba->pwr_info.lane_tx = 1;
4120 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
4121 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
4122 hba->pwr_info.hs_rate = 0;
4123}
4124
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304125/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004126 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4127 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304128 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004129static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304130{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004131 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
4132
4133 if (hba->max_pwr_info.is_valid)
4134 return 0;
4135
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004136 pwr_info->pwr_tx = FAST_MODE;
4137 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004138 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304139
4140 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004141 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
4142 &pwr_info->lane_rx);
4143 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4144 &pwr_info->lane_tx);
4145
4146 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
4147 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
4148 __func__,
4149 pwr_info->lane_rx,
4150 pwr_info->lane_tx);
4151 return -EINVAL;
4152 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304153
4154 /*
4155 * First, get the maximum gears of HS speed.
4156 * If a zero value, it means there is no HSGEAR capability.
4157 * Then, get the maximum gears of PWM speed.
4158 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004159 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
4160 if (!pwr_info->gear_rx) {
4161 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
4162 &pwr_info->gear_rx);
4163 if (!pwr_info->gear_rx) {
4164 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
4165 __func__, pwr_info->gear_rx);
4166 return -EINVAL;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004167 } else {
4168 if (hba->limit_rx_pwm_gear > 0 &&
4169 (hba->limit_rx_pwm_gear < pwr_info->gear_rx))
4170 pwr_info->gear_rx = hba->limit_rx_pwm_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004171 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004172 pwr_info->pwr_rx = SLOW_MODE;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004173 } else {
4174 if (hba->limit_rx_hs_gear > 0 &&
4175 (hba->limit_rx_hs_gear < pwr_info->gear_rx))
4176 pwr_info->gear_rx = hba->limit_rx_hs_gear;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304177 }
4178
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004179 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
4180 &pwr_info->gear_tx);
4181 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304182 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004183 &pwr_info->gear_tx);
4184 if (!pwr_info->gear_tx) {
4185 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
4186 __func__, pwr_info->gear_tx);
4187 return -EINVAL;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004188 } else {
4189 if (hba->limit_tx_pwm_gear > 0 &&
4190 (hba->limit_tx_pwm_gear < pwr_info->gear_tx))
4191 pwr_info->gear_tx = hba->limit_tx_pwm_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004192 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004193 pwr_info->pwr_tx = SLOW_MODE;
Subhash Jadavani5e45e702016-08-09 18:43:10 -07004194 } else {
4195 if (hba->limit_tx_hs_gear > 0 &&
4196 (hba->limit_tx_hs_gear < pwr_info->gear_tx))
4197 pwr_info->gear_tx = hba->limit_tx_hs_gear;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004198 }
4199
4200 hba->max_pwr_info.is_valid = true;
4201 return 0;
4202}
4203
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004204int ufshcd_change_power_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004205 struct ufs_pa_layer_attr *pwr_mode)
4206{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004207 int ret = 0;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004208
4209 /* if already configured to the requested pwr_mode */
4210 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4211 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4212 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4213 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4214 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4215 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4216 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4217 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4218 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304219 }
4220
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004221 ufsdbg_error_inject_dispatcher(hba, ERR_INJECT_PWR_CHANGE, 0, &ret);
4222 if (ret)
4223 return ret;
4224
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304225 /*
4226 * Configure attributes for power mode change with below.
4227 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4228 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4229 * - PA_HSSERIES
4230 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004231 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4232 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4233 pwr_mode->lane_rx);
4234 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4235 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304236 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004237 else
4238 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304239
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004240 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4241 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4242 pwr_mode->lane_tx);
4243 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4244 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304245 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004246 else
4247 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304248
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004249 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4250 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4251 pwr_mode->pwr_rx == FAST_MODE ||
4252 pwr_mode->pwr_tx == FAST_MODE)
4253 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4254 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304255
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004256 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4257 DL_FC0ProtectionTimeOutVal_Default);
4258 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4259 DL_TC0ReplayTimeOutVal_Default);
4260 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4261 DL_AFC0ReqTimeOutVal_Default);
4262
4263 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4264 DL_FC0ProtectionTimeOutVal_Default);
4265 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4266 DL_TC0ReplayTimeOutVal_Default);
4267 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4268 DL_AFC0ReqTimeOutVal_Default);
4269
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004270 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4271 | pwr_mode->pwr_tx);
4272
4273 if (ret) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004274 ufshcd_update_error_stats(hba, UFS_ERR_POWER_MODE_CHANGE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304275 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004276 "%s: power mode change failed %d\n", __func__, ret);
4277 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004278 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4279 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004280
4281 memcpy(&hba->pwr_info, pwr_mode,
4282 sizeof(struct ufs_pa_layer_attr));
4283 }
4284
4285 return ret;
4286}
4287
4288/**
4289 * ufshcd_config_pwr_mode - configure a new power mode
4290 * @hba: per-adapter instance
4291 * @desired_pwr_mode: desired power configuration
4292 */
4293static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
4294 struct ufs_pa_layer_attr *desired_pwr_mode)
4295{
4296 struct ufs_pa_layer_attr final_params = { 0 };
4297 int ret;
4298
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004299 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4300 desired_pwr_mode, &final_params);
4301
4302 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004303 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4304
4305 ret = ufshcd_change_power_mode(hba, &final_params);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004306 if (!ret)
4307 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304308
4309 return ret;
4310}
4311
4312/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304313 * ufshcd_complete_dev_init() - checks device readiness
4314 * hba: per-adapter instance
4315 *
4316 * Set fDeviceInit flag and poll until device toggles it.
4317 */
4318static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4319{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004320 int i;
4321 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05304322 bool flag_res = 1;
4323
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004324 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4325 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304326 if (err) {
4327 dev_err(hba->dev,
4328 "%s setting fDeviceInit flag failed with error %d\n",
4329 __func__, err);
4330 goto out;
4331 }
4332
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004333 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4334 for (i = 0; i < 1000 && !err && flag_res; i++)
4335 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4336 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
4337
Dolev Raviv68078d52013-07-30 00:35:58 +05304338 if (err)
4339 dev_err(hba->dev,
4340 "%s reading fDeviceInit flag failed with error %d\n",
4341 __func__, err);
4342 else if (flag_res)
4343 dev_err(hba->dev,
4344 "%s fDeviceInit was not cleared by the device\n",
4345 __func__);
4346
4347out:
4348 return err;
4349}
4350
4351/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304352 * ufshcd_make_hba_operational - Make UFS controller operational
4353 * @hba: per adapter instance
4354 *
4355 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004356 * 1. Enable required interrupts
4357 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004358 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004359 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304360 *
4361 * Returns 0 on success, non-zero value on failure
4362 */
4363static int ufshcd_make_hba_operational(struct ufs_hba *hba)
4364{
4365 int err = 0;
4366 u32 reg;
4367
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304368 /* Enable required interrupts */
4369 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4370
4371 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004372 if (ufshcd_is_intr_aggr_allowed(hba))
4373 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4374 else
4375 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304376
4377 /* Configure UTRL and UTMRL base address registers */
4378 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4379 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4380 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4381 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4382 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4383 REG_UTP_TASK_REQ_LIST_BASE_L);
4384 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4385 REG_UTP_TASK_REQ_LIST_BASE_H);
4386
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304387 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004388 * Make sure base address and interrupt setup are updated before
4389 * enabling the run/stop registers below.
4390 */
4391 wmb();
4392
4393 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304394 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304395 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004396 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304397 if (!(ufshcd_get_lists_status(reg))) {
4398 ufshcd_enable_run_stop_reg(hba);
4399 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304400 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304401 "Host controller not ready to process requests");
4402 err = -EIO;
4403 goto out;
4404 }
4405
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304406out:
4407 return err;
4408}
4409
4410/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004411 * ufshcd_hba_stop - Send controller to reset state
4412 * @hba: per adapter instance
4413 * @can_sleep: perform sleep or just spin
4414 */
4415static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
4416{
4417 int err;
4418
4419 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4420 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4421 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4422 10, 1, can_sleep);
4423 if (err)
4424 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4425}
4426
4427/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304428 * ufshcd_hba_enable - initialize the controller
4429 * @hba: per adapter instance
4430 *
4431 * The controller resets itself and controller firmware initialization
4432 * sequence kicks off. When controller is ready it will set
4433 * the Host Controller Enable bit to 1.
4434 *
4435 * Returns 0 on success, non-zero value on failure
4436 */
4437static int ufshcd_hba_enable(struct ufs_hba *hba)
4438{
4439 int retry;
4440
4441 /*
4442 * msleep of 1 and 5 used in this function might result in msleep(20),
4443 * but it was necessary to send the UFS FPGA to reset mode during
4444 * development and testing of this driver. msleep can be changed to
4445 * mdelay and retry count can be reduced based on the controller.
4446 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004447 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304448 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004449 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304450
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004451 /* UniPro link is disabled at this point */
4452 ufshcd_set_link_off(hba);
4453
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004454 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004455
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304456 /* start controller initialization sequence */
4457 ufshcd_hba_start(hba);
4458
4459 /*
4460 * To initialize a UFS host controller HCE bit must be set to 1.
4461 * During initialization the HCE bit value changes from 1->0->1.
4462 * When the host controller completes initialization sequence
4463 * it sets the value of HCE bit to 1. The same HCE bit is read back
4464 * to check if the controller has completed initialization sequence.
4465 * So without this delay the value HCE = 1, set in the previous
4466 * instruction might be read back.
4467 * This delay can be changed based on the controller.
4468 */
4469 msleep(1);
4470
4471 /* wait for the host controller to complete initialization */
4472 retry = 10;
4473 while (ufshcd_is_hba_active(hba)) {
4474 if (retry) {
4475 retry--;
4476 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304477 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304478 "Controller enable failed\n");
4479 return -EIO;
4480 }
4481 msleep(5);
4482 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004483
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004484 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004485 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004486
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004487 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004488
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304489 return 0;
4490}
4491
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004492static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4493{
4494 int tx_lanes, i, err = 0;
4495
4496 if (!peer)
4497 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4498 &tx_lanes);
4499 else
4500 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4501 &tx_lanes);
4502 for (i = 0; i < tx_lanes; i++) {
4503 if (!peer)
4504 err = ufshcd_dme_set(hba,
4505 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4506 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4507 0);
4508 else
4509 err = ufshcd_dme_peer_set(hba,
4510 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4511 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4512 0);
4513 if (err) {
4514 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4515 __func__, peer, i, err);
4516 break;
4517 }
4518 }
4519
4520 return err;
4521}
4522
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004523static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)
4524{
4525 return ufshcd_disable_tx_lcc(hba, false);
4526}
4527
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004528static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4529{
4530 return ufshcd_disable_tx_lcc(hba, true);
4531}
4532
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304533/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304534 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304535 * @hba: per adapter instance
4536 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304537 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304538 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304539static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304540{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304541 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004542 int retries = DME_LINKSTARTUP_RETRIES;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004543 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304544
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004545 /*
4546 * If UFS device isn't active then we will have to issue link startup
4547 * 2 times to make sure the device state move to active.
4548 */
4549 if (!ufshcd_is_ufs_dev_active(hba))
4550 link_startup_again = true;
4551
4552link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004553 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004554 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304555
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004556 ret = ufshcd_dme_link_startup(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004557 if (ret)
4558 ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004559
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004560 /* check if device is detected by inter-connect layer */
4561 if (!ret && !ufshcd_is_device_present(hba)) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004562 ufshcd_update_error_stats(hba, UFS_ERR_LINKSTARTUP);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004563 dev_err(hba->dev, "%s: Device not present\n", __func__);
4564 ret = -ENXIO;
4565 goto out;
4566 }
4567
4568 /*
4569 * DME link lost indication is only received when link is up,
4570 * but we can't be sure if the link is up until link startup
4571 * succeeds. So reset the local Uni-Pro and try again.
4572 */
4573 if (ret && ufshcd_hba_enable(hba))
4574 goto out;
4575 } while (ret && retries--);
4576
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304577 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004578 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304579 goto out;
4580
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004581 if (link_startup_again) {
4582 link_startup_again = false;
4583 retries = DME_LINKSTARTUP_RETRIES;
4584 goto link_startup;
4585 }
4586
4587 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4588 ufshcd_init_pwr_info(hba);
4589 ufshcd_print_pwr_info(hba);
4590
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004591 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4592 ret = ufshcd_disable_device_tx_lcc(hba);
4593 if (ret)
4594 goto out;
4595 }
4596
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004597 if (hba->dev_quirks & UFS_DEVICE_QUIRK_BROKEN_LCC) {
4598 ret = ufshcd_disable_host_tx_lcc(hba);
4599 if (ret)
4600 goto out;
4601 }
4602
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004603 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004604 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4605 if (ret)
4606 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004607
4608 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304609out:
4610 if (ret)
4611 dev_err(hba->dev, "link startup failed %d\n", ret);
4612 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304613}
4614
4615/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304616 * ufshcd_verify_dev_init() - Verify device initialization
4617 * @hba: per-adapter instance
4618 *
4619 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4620 * device Transport Protocol (UTP) layer is ready after a reset.
4621 * If the UTP layer at the device side is not initialized, it may
4622 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4623 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4624 */
4625static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4626{
4627 int err = 0;
4628 int retries;
4629
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004630 ufshcd_hold_all(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304631 mutex_lock(&hba->dev_cmd.lock);
4632 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4633 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4634 NOP_OUT_TIMEOUT);
4635
4636 if (!err || err == -ETIMEDOUT)
4637 break;
4638
4639 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4640 }
4641 mutex_unlock(&hba->dev_cmd.lock);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004642 ufshcd_release_all(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304643
4644 if (err)
4645 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4646 return err;
4647}
4648
4649/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004650 * ufshcd_set_queue_depth - set lun queue depth
4651 * @sdev: pointer to SCSI device
4652 *
4653 * Read bLUQueueDepth value and activate scsi tagged command
4654 * queueing. For WLUN, queue depth is set to 1. For best-effort
4655 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4656 * value that host can queue.
4657 */
4658static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4659{
4660 int ret = 0;
4661 u8 lun_qdepth;
4662 struct ufs_hba *hba;
4663
4664 hba = shost_priv(sdev->host);
4665
4666 lun_qdepth = hba->nutrs;
4667 ret = ufshcd_read_unit_desc_param(hba,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004668 ufshcd_scsi_to_upiu_lun(sdev->lun),
4669 UNIT_DESC_PARAM_LU_Q_DEPTH,
4670 &lun_qdepth,
4671 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004672
4673 /* Some WLUN doesn't support unit descriptor */
4674 if (ret == -EOPNOTSUPP)
4675 lun_qdepth = 1;
4676 else if (!lun_qdepth)
4677 /* eventually, we can figure out the real queue depth */
4678 lun_qdepth = hba->nutrs;
4679 else
4680 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4681
4682 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4683 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004684 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004685}
4686
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004687/*
4688 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4689 * @hba: per-adapter instance
4690 * @lun: UFS device lun id
4691 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4692 *
4693 * Returns 0 in case of success and b_lu_write_protect status would be returned
4694 * @b_lu_write_protect parameter.
4695 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4696 * Returns -EINVAL in case of invalid parameters passed to this function.
4697 */
4698static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4699 u8 lun,
4700 u8 *b_lu_write_protect)
4701{
4702 int ret;
4703
4704 if (!b_lu_write_protect)
4705 ret = -EINVAL;
4706 /*
4707 * According to UFS device spec, RPMB LU can't be write
4708 * protected so skip reading bLUWriteProtect parameter for
4709 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4710 */
4711 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4712 ret = -ENOTSUPP;
4713 else
4714 ret = ufshcd_read_unit_desc_param(hba,
4715 lun,
4716 UNIT_DESC_PARAM_LU_WR_PROTECT,
4717 b_lu_write_protect,
4718 sizeof(*b_lu_write_protect));
4719 return ret;
4720}
4721
4722/**
4723 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4724 * status
4725 * @hba: per-adapter instance
4726 * @sdev: pointer to SCSI device
4727 *
4728 */
4729static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4730 struct scsi_device *sdev)
4731{
4732 if (hba->dev_info.f_power_on_wp_en &&
4733 !hba->dev_info.is_lu_power_on_wp) {
4734 u8 b_lu_write_protect;
4735
4736 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4737 &b_lu_write_protect) &&
4738 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4739 hba->dev_info.is_lu_power_on_wp = true;
4740 }
4741}
4742
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004743/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304744 * ufshcd_slave_alloc - handle initial SCSI device configurations
4745 * @sdev: pointer to SCSI device
4746 *
4747 * Returns success
4748 */
4749static int ufshcd_slave_alloc(struct scsi_device *sdev)
4750{
4751 struct ufs_hba *hba;
4752
4753 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304754
4755 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4756 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304757
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304758 /* allow SCSI layer to restart the device in case of errors */
4759 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004760
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004761 /* REPORT SUPPORTED OPERATION CODES is not supported */
4762 sdev->no_report_opcodes = 1;
4763
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004764 /* WRITE_SAME command is not supported*/
4765 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004766
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004767 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004768
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004769 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4770
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004771 return 0;
4772}
4773
4774/**
4775 * ufshcd_change_queue_depth - change queue depth
4776 * @sdev: pointer to SCSI device
4777 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004778 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004779 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004780 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004781static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004782{
4783 struct ufs_hba *hba = shost_priv(sdev->host);
4784
4785 if (depth > hba->nutrs)
4786 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004787 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304788}
4789
4790/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004791 * ufshcd_slave_configure - adjust SCSI device configurations
4792 * @sdev: pointer to SCSI device
4793 */
4794static int ufshcd_slave_configure(struct scsi_device *sdev)
4795{
4796 struct request_queue *q = sdev->request_queue;
4797
4798 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4799 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
4800
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004801 sdev->autosuspend_delay = UFSHCD_AUTO_SUSPEND_DELAY_MS;
4802 sdev->use_rpm_auto = 1;
4803
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004804 return 0;
4805}
4806
4807/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304808 * ufshcd_slave_destroy - remove SCSI device configurations
4809 * @sdev: pointer to SCSI device
4810 */
4811static void ufshcd_slave_destroy(struct scsi_device *sdev)
4812{
4813 struct ufs_hba *hba;
4814
4815 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004816 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004817 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4818 unsigned long flags;
4819
4820 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004821 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004822 spin_unlock_irqrestore(hba->host->host_lock, flags);
4823 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304824}
4825
4826/**
4827 * ufshcd_task_req_compl - handle task management request completion
4828 * @hba: per adapter instance
4829 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304830 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304831 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304832 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304833 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304834static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304835{
4836 struct utp_task_req_desc *task_req_descp;
4837 struct utp_upiu_task_rsp *task_rsp_upiup;
4838 unsigned long flags;
4839 int ocs_value;
4840 int task_result;
4841
4842 spin_lock_irqsave(hba->host->host_lock, flags);
4843
4844 /* Clear completed tasks from outstanding_tasks */
4845 __clear_bit(index, &hba->outstanding_tasks);
4846
4847 task_req_descp = hba->utmrdl_base_addr;
4848 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
4849
4850 if (ocs_value == OCS_SUCCESS) {
4851 task_rsp_upiup = (struct utp_upiu_task_rsp *)
4852 task_req_descp[index].task_rsp_upiu;
4853 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
4854 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304855 if (resp)
4856 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304857 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304858 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
4859 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304860 }
4861 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304862
4863 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304864}
4865
4866/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304867 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4868 * @lrb: pointer to local reference block of completed command
4869 * @scsi_status: SCSI command status
4870 *
4871 * Returns value base on SCSI command status
4872 */
4873static inline int
4874ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4875{
4876 int result = 0;
4877
4878 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304879 case SAM_STAT_CHECK_CONDITION:
4880 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304881 case SAM_STAT_GOOD:
4882 result |= DID_OK << 16 |
4883 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304884 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304885 break;
4886 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304887 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304888 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304889 ufshcd_copy_sense_data(lrbp);
4890 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304891 break;
4892 default:
4893 result |= DID_ERROR << 16;
4894 break;
4895 } /* end of switch */
4896
4897 return result;
4898}
4899
4900/**
4901 * ufshcd_transfer_rsp_status - Get overall status of the response
4902 * @hba: per adapter instance
4903 * @lrb: pointer to local reference block of completed command
4904 *
4905 * Returns result of the command to notify SCSI midlayer
4906 */
4907static inline int
4908ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4909{
4910 int result = 0;
4911 int scsi_status;
4912 int ocs;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004913 bool print_prdt;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304914
4915 /* overall command status of utrd */
4916 ocs = ufshcd_get_tr_ocs(lrbp);
4917
4918 switch (ocs) {
4919 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304920 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004921 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304922 switch (result) {
4923 case UPIU_TRANSACTION_RESPONSE:
4924 /*
4925 * get the response UPIU result to extract
4926 * the SCSI command status
4927 */
4928 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4929
4930 /*
4931 * get the result based on SCSI status response
4932 * to notify the SCSI midlayer of the command status
4933 */
4934 scsi_status = result & MASK_SCSI_STATUS;
4935 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304936
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004937 /*
4938 * Currently we are only supporting BKOPs exception
4939 * events hence we can ignore BKOPs exception event
4940 * during power management callbacks. BKOPs exception
4941 * event is not expected to be raised in runtime suspend
4942 * callback as it allows the urgent bkops.
4943 * During system suspend, we are anyway forcefully
4944 * disabling the bkops and if urgent bkops is needed
4945 * it will be enabled on system resume. Long term
4946 * solution could be to abort the system suspend if
4947 * UFS device needs urgent BKOPs.
4948 */
4949 if (!hba->pm_op_in_progress &&
4950 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304951 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304952 break;
4953 case UPIU_TRANSACTION_REJECT_UPIU:
4954 /* TODO: handle Reject UPIU Response */
4955 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304956 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304957 "Reject UPIU not fully implemented\n");
4958 break;
4959 default:
4960 result = DID_ERROR << 16;
4961 dev_err(hba->dev,
4962 "Unexpected request response code = %x\n",
4963 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304964 break;
4965 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304966 break;
4967 case OCS_ABORTED:
4968 result |= DID_ABORT << 16;
4969 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304970 case OCS_INVALID_COMMAND_STATUS:
4971 result |= DID_REQUEUE << 16;
4972 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304973 case OCS_INVALID_CMD_TABLE_ATTR:
4974 case OCS_INVALID_PRDT_ATTR:
4975 case OCS_MISMATCH_DATA_BUF_SIZE:
4976 case OCS_MISMATCH_RESP_UPIU_SIZE:
4977 case OCS_PEER_COMM_FAILURE:
4978 case OCS_FATAL_ERROR:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004979 case OCS_DEVICE_FATAL_ERROR:
4980 case OCS_INVALID_CRYPTO_CONFIG:
4981 case OCS_GENERAL_CRYPTO_ERROR:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304982 default:
4983 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304984 dev_err(hba->dev,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004985 "OCS error from controller = %x for tag %d\n",
4986 ocs, lrbp->task_tag);
4987 ufshcd_print_host_regs(hba);
4988 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304989 break;
4990 } /* end of switch */
4991
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07004992 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs) {
4993 print_prdt = (ocs == OCS_INVALID_PRDT_ATTR ||
4994 ocs == OCS_MISMATCH_DATA_BUF_SIZE);
4995 ufshcd_print_trs(hba, 1 << lrbp->task_tag, print_prdt);
4996 }
4997
4998 if ((host_byte(result) == DID_ERROR) ||
4999 (host_byte(result) == DID_ABORT))
5000 ufsdbg_set_err_state(hba);
5001
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305002 return result;
5003}
5004
5005/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305006 * ufshcd_uic_cmd_compl - handle completion of uic command
5007 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305008 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305009 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305010static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305011{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305012 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305013 hba->active_uic_cmd->argument2 |=
5014 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05305015 hba->active_uic_cmd->argument3 =
5016 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305017 complete(&hba->active_uic_cmd->done);
5018 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305019
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005020 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
5021 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305022}
5023
5024/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005025 * ufshcd_abort_outstanding_requests - abort all outstanding transfer requests.
5026 * @hba: per adapter instance
5027 * @result: error result to inform scsi layer about
5028 */
5029void ufshcd_abort_outstanding_transfer_requests(struct ufs_hba *hba, int result)
5030{
5031 u8 index;
5032 struct ufshcd_lrb *lrbp;
5033 struct scsi_cmnd *cmd;
5034
5035 if (!hba->outstanding_reqs)
5036 return;
5037
5038 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5039 lrbp = &hba->lrb[index];
5040 cmd = lrbp->cmd;
5041 if (cmd) {
5042 ufshcd_cond_add_cmd_trace(hba, index, "failed");
5043 ufshcd_update_error_stats(hba,
5044 UFS_ERR_INT_FATAL_ERRORS);
5045 scsi_dma_unmap(cmd);
5046 cmd->result = result;
5047 /* Clear pending transfer requests */
5048 ufshcd_clear_cmd(hba, index);
5049 ufshcd_outstanding_req_clear(hba, index);
5050 clear_bit_unlock(index, &hba->lrb_in_use);
5051 lrbp->complete_time_stamp = ktime_get();
5052 update_req_stats(hba, lrbp);
5053 /* Mark completed command as NULL in LRB */
5054 lrbp->cmd = NULL;
5055 ufshcd_release_all(hba);
5056 if (cmd->request) {
5057 /*
5058 * As we are accessing the "request" structure,
5059 * this must be called before calling
5060 * ->scsi_done() callback.
5061 */
5062 ufshcd_vops_pm_qos_req_end(hba, cmd->request,
5063 true);
5064 ufshcd_vops_crypto_engine_cfg_end(hba,
5065 lrbp, cmd->request);
5066 }
5067 /* Do not touch lrbp after scsi done */
5068 cmd->scsi_done(cmd);
5069 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
5070 if (hba->dev_cmd.complete) {
5071 ufshcd_cond_add_cmd_trace(hba, index,
5072 "dev_failed");
5073 ufshcd_outstanding_req_clear(hba, index);
5074 complete(hba->dev_cmd.complete);
5075 }
5076 }
5077 if (ufshcd_is_clkscaling_supported(hba))
5078 hba->clk_scaling.active_reqs--;
5079 }
5080}
5081
5082/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005083 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305084 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005085 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305086 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005087static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
5088 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305089{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305090 struct ufshcd_lrb *lrbp;
5091 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305092 int result;
5093 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005094
Dolev Ravive9d501b2014-07-01 12:22:37 +03005095 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
5096 lrbp = &hba->lrb[index];
5097 cmd = lrbp->cmd;
5098 if (cmd) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005099 ufshcd_cond_add_cmd_trace(hba, index, "complete");
5100 ufshcd_update_tag_stats_completion(hba, cmd);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005101 result = ufshcd_transfer_rsp_status(hba, lrbp);
5102 scsi_dma_unmap(cmd);
5103 cmd->result = result;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005104 clear_bit_unlock(index, &hba->lrb_in_use);
5105 lrbp->complete_time_stamp = ktime_get();
5106 update_req_stats(hba, lrbp);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005107 /* Mark completed command as NULL in LRB */
5108 lrbp->cmd = NULL;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005109 __ufshcd_release(hba, false);
5110 __ufshcd_hibern8_release(hba, false);
5111 if (cmd->request) {
5112 /*
5113 * As we are accessing the "request" structure,
5114 * this must be called before calling
5115 * ->scsi_done() callback.
5116 */
5117 ufshcd_vops_pm_qos_req_end(hba, cmd->request,
5118 false);
5119 ufshcd_vops_crypto_engine_cfg_end(hba,
5120 lrbp, cmd->request);
5121 }
5122
Dolev Ravive9d501b2014-07-01 12:22:37 +03005123 /* Do not touch lrbp after scsi done */
5124 cmd->scsi_done(cmd);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005125 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
5126 if (hba->dev_cmd.complete) {
5127 ufshcd_cond_add_cmd_trace(hba, index,
5128 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03005129 complete(hba->dev_cmd.complete);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005130 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03005131 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005132 if (ufshcd_is_clkscaling_supported(hba))
5133 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005134 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305135
5136 /* clear corresponding bits of completed commands */
5137 hba->outstanding_reqs ^= completed_reqs;
5138
Sahitya Tummala856b3482014-09-25 15:32:34 +03005139 ufshcd_clk_scaling_update_busy(hba);
5140
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305141 /* we might have free'd some tags above */
5142 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305143}
5144
5145/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005146 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5147 * @hba: per adapter instance
5148 */
5149static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
5150{
5151 unsigned long completed_reqs;
5152 u32 tr_doorbell;
5153
5154 /* Resetting interrupt aggregation counters first and reading the
5155 * DOOR_BELL afterward allows us to handle all the completed requests.
5156 * In order to prevent other interrupts starvation the DB is read once
5157 * after reset. The down side of this solution is the possibility of
5158 * false interrupt if device completes another request after resetting
5159 * aggregation and before reading the DB.
5160 */
5161 if (ufshcd_is_intr_aggr_allowed(hba))
5162 ufshcd_reset_intr_aggr(hba);
5163
5164 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5165 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
5166
5167 __ufshcd_transfer_req_compl(hba, completed_reqs);
5168}
5169
5170/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305171 * ufshcd_disable_ee - disable exception event
5172 * @hba: per-adapter instance
5173 * @mask: exception event to disable
5174 *
5175 * Disables exception event in the device so that the EVENT_ALERT
5176 * bit is not set.
5177 *
5178 * Returns zero on success, non-zero error value on failure.
5179 */
5180static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
5181{
5182 int err = 0;
5183 u32 val;
5184
5185 if (!(hba->ee_ctrl_mask & mask))
5186 goto out;
5187
5188 val = hba->ee_ctrl_mask & ~mask;
5189 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005190 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305191 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5192 if (!err)
5193 hba->ee_ctrl_mask &= ~mask;
5194out:
5195 return err;
5196}
5197
5198/**
5199 * ufshcd_enable_ee - enable exception event
5200 * @hba: per-adapter instance
5201 * @mask: exception event to enable
5202 *
5203 * Enable corresponding exception event in the device to allow
5204 * device to alert host in critical scenarios.
5205 *
5206 * Returns zero on success, non-zero error value on failure.
5207 */
5208static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
5209{
5210 int err = 0;
5211 u32 val;
5212
5213 if (hba->ee_ctrl_mask & mask)
5214 goto out;
5215
5216 val = hba->ee_ctrl_mask | mask;
5217 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005218 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305219 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
5220 if (!err)
5221 hba->ee_ctrl_mask |= mask;
5222out:
5223 return err;
5224}
5225
5226/**
5227 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5228 * @hba: per-adapter instance
5229 *
5230 * Allow device to manage background operations on its own. Enabling
5231 * this might lead to inconsistent latencies during normal data transfers
5232 * as the device is allowed to manage its own way of handling background
5233 * operations.
5234 *
5235 * Returns zero on success, non-zero on failure.
5236 */
5237static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
5238{
5239 int err = 0;
5240
5241 if (hba->auto_bkops_enabled)
5242 goto out;
5243
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005244 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305245 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5246 if (err) {
5247 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
5248 __func__, err);
5249 goto out;
5250 }
5251
5252 hba->auto_bkops_enabled = true;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005253 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 1);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305254
5255 /* No need of URGENT_BKOPS exception from the device */
5256 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5257 if (err)
5258 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
5259 __func__, err);
5260out:
5261 return err;
5262}
5263
5264/**
5265 * ufshcd_disable_auto_bkops - block device in doing background operations
5266 * @hba: per-adapter instance
5267 *
5268 * Disabling background operations improves command response latency but
5269 * has drawback of device moving into critical state where the device is
5270 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5271 * host is idle so that BKOPS are managed effectively without any negative
5272 * impacts.
5273 *
5274 * Returns zero on success, non-zero on failure.
5275 */
5276static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5277{
5278 int err = 0;
5279
5280 if (!hba->auto_bkops_enabled)
5281 goto out;
5282
5283 /*
5284 * If host assisted BKOPs is to be enabled, make sure
5285 * urgent bkops exception is allowed.
5286 */
5287 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5288 if (err) {
5289 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5290 __func__, err);
5291 goto out;
5292 }
5293
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005294 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305295 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5296 if (err) {
5297 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5298 __func__, err);
5299 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5300 goto out;
5301 }
5302
5303 hba->auto_bkops_enabled = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005304 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), 0);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305305out:
5306 return err;
5307}
5308
5309/**
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005310 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305311 * @hba: per adapter instance
5312 *
5313 * After a device reset the device may toggle the BKOPS_EN flag
5314 * to default value. The s/w tracking variables should be updated
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005315 * as well. This function would change the auto-bkops state based on
5316 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305317 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005318static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305319{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005320 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5321 hba->auto_bkops_enabled = false;
5322 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5323 ufshcd_enable_auto_bkops(hba);
5324 } else {
5325 hba->auto_bkops_enabled = true;
5326 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5327 ufshcd_disable_auto_bkops(hba);
5328 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305329}
5330
5331static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5332{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005333 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305334 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5335}
5336
5337/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005338 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5339 * @hba: per-adapter instance
5340 * @status: bkops_status value
5341 *
5342 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5343 * flag in the device to permit background operations if the device
5344 * bkops_status is greater than or equal to "status" argument passed to
5345 * this function, disable otherwise.
5346 *
5347 * Returns 0 for success, non-zero in case of failure.
5348 *
5349 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5350 * to know whether auto bkops is enabled or disabled after this function
5351 * returns control to it.
5352 */
5353static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5354 enum bkops_status status)
5355{
5356 int err;
5357 u32 curr_status = 0;
5358
5359 err = ufshcd_get_bkops_status(hba, &curr_status);
5360 if (err) {
5361 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5362 __func__, err);
5363 goto out;
5364 } else if (curr_status > BKOPS_STATUS_MAX) {
5365 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5366 __func__, curr_status);
5367 err = -EINVAL;
5368 goto out;
5369 }
5370
5371 if (curr_status >= status)
5372 err = ufshcd_enable_auto_bkops(hba);
5373 else
5374 err = ufshcd_disable_auto_bkops(hba);
5375out:
5376 return err;
5377}
5378
5379/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305380 * ufshcd_urgent_bkops - handle urgent bkops exception event
5381 * @hba: per-adapter instance
5382 *
5383 * Enable fBackgroundOpsEn flag in the device to permit background
5384 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005385 *
5386 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5387 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305388 */
5389static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5390{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005391 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305392}
5393
5394static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5395{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005396 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305397 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5398}
5399
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005400static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5401{
5402 int err;
5403 u32 curr_status = 0;
5404
5405 if (hba->is_urgent_bkops_lvl_checked)
5406 goto enable_auto_bkops;
5407
5408 err = ufshcd_get_bkops_status(hba, &curr_status);
5409 if (err) {
5410 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5411 __func__, err);
5412 goto out;
5413 }
5414
5415 /*
5416 * We are seeing that some devices are raising the urgent bkops
5417 * exception events even when BKOPS status doesn't indicate performace
5418 * impacted or critical. Handle these device by determining their urgent
5419 * bkops status at runtime.
5420 */
5421 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5422 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5423 __func__, curr_status);
5424 /* update the current status as the urgent bkops level */
5425 hba->urgent_bkops_lvl = curr_status;
5426 hba->is_urgent_bkops_lvl_checked = true;
5427 }
5428
5429enable_auto_bkops:
5430 err = ufshcd_enable_auto_bkops(hba);
5431out:
5432 if (err < 0)
5433 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5434 __func__, err);
5435}
5436
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305437/**
5438 * ufshcd_exception_event_handler - handle exceptions raised by device
5439 * @work: pointer to work data
5440 *
5441 * Read bExceptionEventStatus attribute from the device and handle the
5442 * exception event accordingly.
5443 */
5444static void ufshcd_exception_event_handler(struct work_struct *work)
5445{
5446 struct ufs_hba *hba;
5447 int err;
5448 u32 status = 0;
5449 hba = container_of(work, struct ufs_hba, eeh_work);
5450
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305451 pm_runtime_get_sync(hba->dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005452 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305453 err = ufshcd_get_ee_status(hba, &status);
5454 if (err) {
5455 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5456 __func__, err);
5457 goto out;
5458 }
5459
5460 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005461
5462 if (status & MASK_EE_URGENT_BKOPS)
5463 ufshcd_bkops_exception_event_handler(hba);
5464
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305465out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005466 ufshcd_scsi_unblock_requests(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305467 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305468 return;
5469}
5470
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005471/* Complete requests that have door-bell cleared */
5472static void ufshcd_complete_requests(struct ufs_hba *hba)
5473{
5474 ufshcd_transfer_req_compl(hba);
5475 ufshcd_tmc_handler(hba);
5476}
5477
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305478/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005479 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5480 * to recover from the DL NAC errors or not.
5481 * @hba: per-adapter instance
5482 *
5483 * Returns true if error handling is required, false otherwise
5484 */
5485static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5486{
5487 unsigned long flags;
5488 bool err_handling = true;
5489
5490 spin_lock_irqsave(hba->host->host_lock, flags);
5491 /*
5492 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5493 * device fatal error and/or DL NAC & REPLAY timeout errors.
5494 */
5495 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5496 goto out;
5497
5498 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5499 ((hba->saved_err & UIC_ERROR) &&
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005500 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) {
5501 /*
5502 * we have to do error recovery but atleast silence the error
5503 * logs.
5504 */
5505 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005506 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005507 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005508
5509 if ((hba->saved_err & UIC_ERROR) &&
5510 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5511 int err;
5512 /*
5513 * wait for 50ms to see if we can get any other errors or not.
5514 */
5515 spin_unlock_irqrestore(hba->host->host_lock, flags);
5516 msleep(50);
5517 spin_lock_irqsave(hba->host->host_lock, flags);
5518
5519 /*
5520 * now check if we have got any other severe errors other than
5521 * DL NAC error?
5522 */
5523 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5524 ((hba->saved_err & UIC_ERROR) &&
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005525 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) {
5526 if (((hba->saved_err & INT_FATAL_ERRORS) ==
5527 DEVICE_FATAL_ERROR) || (hba->saved_uic_err &
5528 ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))
5529 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005530 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005531 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005532
5533 /*
5534 * As DL NAC is the only error received so far, send out NOP
5535 * command to confirm if link is still active or not.
5536 * - If we don't get any response then do error recovery.
5537 * - If we get response then clear the DL NAC error bit.
5538 */
5539
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005540 /* silence the error logs from NOP command */
5541 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005542 spin_unlock_irqrestore(hba->host->host_lock, flags);
5543 err = ufshcd_verify_dev_init(hba);
5544 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005545 hba->silence_err_logs = false;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005546
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005547 if (err) {
5548 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005549 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005550 }
Yaniv Gardi583fa622016-03-10 17:37:13 +02005551
5552 /* Link seems to be alive hence ignore the DL NAC errors */
5553 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5554 hba->saved_err &= ~UIC_ERROR;
5555 /* clear NAC error */
5556 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5557 if (!hba->saved_uic_err) {
5558 err_handling = false;
5559 goto out;
5560 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005561 /*
5562 * there seems to be some errors other than NAC, so do error
5563 * recovery
5564 */
5565 hba->silence_err_logs = true;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005566 }
5567out:
5568 spin_unlock_irqrestore(hba->host->host_lock, flags);
5569 return err_handling;
5570}
5571
5572/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305573 * ufshcd_err_handler - handle UFS errors that require s/w attention
5574 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305575 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305576static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305577{
5578 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305579 unsigned long flags;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005580 bool err_xfer = false, err_tm = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305581 int err = 0;
5582 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005583 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305584
5585 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305586
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005587 ufsdbg_set_err_state(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305588 pm_runtime_get_sync(hba->dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005589 ufshcd_hold_all(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305590
5591 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005592 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305593 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305594
5595 hba->ufshcd_state = UFSHCD_STATE_RESET;
5596 ufshcd_set_eh_in_progress(hba);
5597
5598 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005599 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005600
5601 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5602 bool ret;
5603
5604 spin_unlock_irqrestore(hba->host->host_lock, flags);
5605 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5606 ret = ufshcd_quirk_dl_nac_errors(hba);
5607 spin_lock_irqsave(hba->host->host_lock, flags);
5608 if (!ret)
5609 goto skip_err_handling;
5610 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005611
5612 /*
5613 * Dump controller state before resetting. Transfer requests state
5614 * will be dump as part of the request completion.
5615 */
5616 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5617 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x",
5618 __func__, hba->saved_err, hba->saved_uic_err);
5619 if (!hba->silence_err_logs) {
5620 ufshcd_print_host_regs(hba);
5621 ufshcd_print_host_state(hba);
5622 ufshcd_print_pwr_info(hba);
5623 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5624 }
5625 }
5626
5627 if ((hba->saved_err & INT_FATAL_ERRORS) || hba->saved_ce_err ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005628 ((hba->saved_err & UIC_ERROR) &&
5629 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5630 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5631 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5632 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305633
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005634 /*
5635 * if host reset is required then skip clearing the pending
5636 * transfers forcefully because they will automatically get
5637 * cleared after link startup.
5638 */
5639 if (needs_reset)
5640 goto skip_pending_xfer_clear;
5641
5642 /* release lock as clear command might sleep */
5643 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305644 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005645 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5646 if (ufshcd_clear_cmd(hba, tag)) {
5647 err_xfer = true;
5648 goto lock_skip_pending_xfer_clear;
5649 }
5650 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305651
5652 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005653 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5654 if (ufshcd_clear_tm_cmd(hba, tag)) {
5655 err_tm = true;
5656 goto lock_skip_pending_xfer_clear;
5657 }
5658 }
5659
5660lock_skip_pending_xfer_clear:
5661 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305662
5663 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005664 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305665
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005666 if (err_xfer || err_tm)
5667 needs_reset = true;
5668
5669skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305670 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005671 if (needs_reset) {
5672 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5673
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005674 if (hba->saved_err & INT_FATAL_ERRORS)
5675 ufshcd_update_error_stats(hba,
5676 UFS_ERR_INT_FATAL_ERRORS);
5677 if (hba->saved_ce_err)
5678 ufshcd_update_error_stats(hba, UFS_ERR_CRYPTO_ENGINE);
5679
5680 if (hba->saved_err & UIC_ERROR)
5681 ufshcd_update_error_stats(hba,
5682 UFS_ERR_INT_UIC_ERROR);
5683
5684 if (err_xfer || err_tm)
5685 ufshcd_update_error_stats(hba,
5686 UFS_ERR_CLEAR_PEND_XFER_TM);
5687
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005688 /*
5689 * ufshcd_reset_and_restore() does the link reinitialization
5690 * which will need atleast one empty doorbell slot to send the
5691 * device management commands (NOP and query commands).
5692 * If there is no slot empty at this moment then free up last
5693 * slot forcefully.
5694 */
5695 if (hba->outstanding_reqs == max_doorbells)
5696 __ufshcd_transfer_req_compl(hba,
5697 (1UL << (hba->nutrs - 1)));
5698
5699 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305700 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005701 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305702 if (err) {
5703 dev_err(hba->dev, "%s: reset and restore failed\n",
5704 __func__);
5705 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5706 }
5707 /*
5708 * Inform scsi mid-layer that we did reset and allow to handle
5709 * Unit Attention properly.
5710 */
5711 scsi_report_bus_reset(hba->host, 0);
5712 hba->saved_err = 0;
5713 hba->saved_uic_err = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005714 hba->saved_ce_err = 0;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305715 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005716
Yaniv Gardi583fa622016-03-10 17:37:13 +02005717skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005718 if (!needs_reset) {
5719 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5720 if (hba->saved_err || hba->saved_uic_err)
5721 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5722 __func__, hba->saved_err, hba->saved_uic_err);
5723 }
5724
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005725 hba->silence_err_logs = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305726 ufshcd_clear_eh_in_progress(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305727out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005728 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005729 ufshcd_scsi_unblock_requests(hba);
5730 ufshcd_release_all(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305731 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305732}
5733
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005734static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
5735 u32 reg)
5736{
5737 reg_hist->reg[reg_hist->pos] = reg;
5738 reg_hist->tstamp[reg_hist->pos] = ktime_get();
5739 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
5740}
5741
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305742/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305743 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5744 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305745 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305746static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305747{
5748 u32 reg;
5749
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005750 /* PHY layer lane error */
5751 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5752 /* Ignore LINERESET indication, as this is not an error */
5753 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
5754 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
5755 /*
5756 * To know whether this error is fatal or not, DB timeout
5757 * must be checked but this error is handled separately.
5758 */
5759 dev_dbg(hba->dev, "%s: UIC Lane error reported, reg 0x%x\n",
5760 __func__, reg);
5761 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
5762 }
5763
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305764 /* PA_INIT_ERROR is fatal and needs UIC reset */
5765 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005766 if (reg)
5767 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
5768
5769 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305770 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005771 } else if (hba->dev_quirks &
Yaniv Gardi583fa622016-03-10 17:37:13 +02005772 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5773 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5774 hba->uic_error |=
5775 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5776 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5777 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5778 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305779
5780 /* UIC NL/TL/DME errors needs software retry */
5781 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005782 if (reg) {
5783 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305784 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005785 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305786
5787 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005788 if (reg) {
5789 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305790 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005791 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305792
5793 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005794 if (reg) {
5795 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305796 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005797 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305798
5799 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5800 __func__, hba->uic_error);
5801}
5802
5803/**
5804 * ufshcd_check_errors - Check for errors that need s/w attention
5805 * @hba: per-adapter instance
5806 */
5807static void ufshcd_check_errors(struct ufs_hba *hba)
5808{
5809 bool queue_eh_work = false;
5810
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005811 if (hba->errors & INT_FATAL_ERRORS || hba->ce_error)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305812 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305813
5814 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305815 hba->uic_error = 0;
5816 ufshcd_update_uic_error(hba);
5817 if (hba->uic_error)
5818 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305819 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305820
5821 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005822 /*
5823 * update the transfer error masks to sticky bits, let's do this
5824 * irrespective of current ufshcd_state.
5825 */
5826 hba->saved_err |= hba->errors;
5827 hba->saved_uic_err |= hba->uic_error;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005828 hba->saved_ce_err |= hba->ce_error;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005829
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305830 /* handle fatal errors only when link is functional */
5831 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5832 /* block commands from scsi mid-layer */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005833 __ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305834
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305835 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5836 schedule_work(&hba->eh_work);
5837 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305838 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305839 /*
5840 * if (!queue_eh_work) -
5841 * Other errors are either non-fatal where host recovers
5842 * itself without s/w intervention or errors that will be
5843 * handled by the SCSI core layer.
5844 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305845}
5846
5847/**
5848 * ufshcd_tmc_handler - handle task management function completion
5849 * @hba: per adapter instance
5850 */
5851static void ufshcd_tmc_handler(struct ufs_hba *hba)
5852{
5853 u32 tm_doorbell;
5854
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305855 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305856 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305857 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305858}
5859
5860/**
5861 * ufshcd_sl_intr - Interrupt service routine
5862 * @hba: per adapter instance
5863 * @intr_status: contains interrupts generated by the controller
5864 */
5865static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
5866{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005867 ufsdbg_error_inject_dispatcher(hba,
5868 ERR_INJECT_INTR, intr_status, &intr_status);
5869
5870 ufshcd_vops_crypto_engine_get_status(hba, &hba->ce_error);
5871
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305872 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005873 if (hba->errors || hba->ce_error)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305874 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305875
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305876 if (intr_status & UFSHCD_UIC_MASK)
5877 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305878
5879 if (intr_status & UTP_TASK_REQ_COMPL)
5880 ufshcd_tmc_handler(hba);
5881
5882 if (intr_status & UTP_TRANSFER_REQ_COMPL)
5883 ufshcd_transfer_req_compl(hba);
5884}
5885
5886/**
5887 * ufshcd_intr - Main interrupt service routine
5888 * @irq: irq number
5889 * @__hba: pointer to adapter instance
5890 *
5891 * Returns IRQ_HANDLED - If interrupt is valid
5892 * IRQ_NONE - If invalid interrupt
5893 */
5894static irqreturn_t ufshcd_intr(int irq, void *__hba)
5895{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005896 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305897 irqreturn_t retval = IRQ_NONE;
5898 struct ufs_hba *hba = __hba;
5899
5900 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305901 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005902 enabled_intr_status =
5903 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305904
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005905 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05305906 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005907
5908 if (enabled_intr_status) {
5909 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305910 retval = IRQ_HANDLED;
5911 }
5912 spin_unlock(hba->host->host_lock);
5913 return retval;
5914}
5915
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305916static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5917{
5918 int err = 0;
5919 u32 mask = 1 << tag;
5920 unsigned long flags;
5921
5922 if (!test_bit(tag, &hba->outstanding_tasks))
5923 goto out;
5924
5925 spin_lock_irqsave(hba->host->host_lock, flags);
5926 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
5927 spin_unlock_irqrestore(hba->host->host_lock, flags);
5928
5929 /* poll for max. 1 sec to clear door bell register by h/w */
5930 err = ufshcd_wait_for_register(hba,
5931 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005932 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305933out:
5934 return err;
5935}
5936
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305937/**
5938 * ufshcd_issue_tm_cmd - issues task management commands to controller
5939 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305940 * @lun_id: LUN ID to which TM command is sent
5941 * @task_id: task ID to which the TM command is applicable
5942 * @tm_function: task management function opcode
5943 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305944 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305945 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305946 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305947static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5948 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305949{
5950 struct utp_task_req_desc *task_req_descp;
5951 struct utp_upiu_task_req *task_req_upiup;
5952 struct Scsi_Host *host;
5953 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305954 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305955 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305956 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305957
5958 host = hba->host;
5959
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305960 /*
5961 * Get free slot, sleep if slots are unavailable.
5962 * Even though we use wait_event() which sleeps indefinitely,
5963 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5964 */
5965 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07005966 ufshcd_hold_all(hba);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305967
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305968 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305969 task_req_descp = hba->utmrdl_base_addr;
5970 task_req_descp += free_slot;
5971
5972 /* Configure task request descriptor */
5973 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5974 task_req_descp->header.dword_2 =
5975 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5976
5977 /* Configure task request UPIU */
5978 task_req_upiup =
5979 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305980 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305981 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305982 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305983 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305984 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305985 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005986 /*
5987 * The host shall provide the same value for LUN field in the basic
5988 * header and for Input Parameter.
5989 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305990 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
5991 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305992
5993 /* send command to the controller */
5994 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005995
5996 /* Make sure descriptors are ready before ringing the task doorbell */
5997 wmb();
5998
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305999 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006000 /* Make sure that doorbell is committed immediately */
6001 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306002
6003 spin_unlock_irqrestore(host->host_lock, flags);
6004
6005 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306006 err = wait_event_timeout(hba->tm_wq,
6007 test_bit(free_slot, &hba->tm_condition),
6008 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306009 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306010 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
6011 __func__, tm_function);
6012 if (ufshcd_clear_tm_cmd(hba, free_slot))
6013 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
6014 __func__, free_slot);
6015 err = -ETIMEDOUT;
6016 } else {
6017 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306018 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306019
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306020 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306021 ufshcd_put_tm_slot(hba, free_slot);
6022 wake_up(&hba->tm_tag_wq);
6023
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006024 ufshcd_release_all(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306025 return err;
6026}
6027
6028/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306029 * ufshcd_eh_device_reset_handler - device reset handler registered to
6030 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306031 * @cmd: SCSI command pointer
6032 *
6033 * Returns SUCCESS/FAILED
6034 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306035static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306036{
6037 struct Scsi_Host *host;
6038 struct ufs_hba *hba;
6039 unsigned int tag;
6040 u32 pos;
6041 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306042 u8 resp = 0xF;
6043 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306044 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306045
6046 host = cmd->device->host;
6047 hba = shost_priv(host);
6048 tag = cmd->request->tag;
6049
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306050 lrbp = &hba->lrb[tag];
6051 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6052 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306053 if (!err)
6054 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306055 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306056 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306057
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306058 /* clear the commands that were pending for corresponding LUN */
6059 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6060 if (hba->lrb[pos].lun == lrbp->lun) {
6061 err = ufshcd_clear_cmd(hba, pos);
6062 if (err)
6063 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306064 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306065 }
6066 spin_lock_irqsave(host->host_lock, flags);
6067 ufshcd_transfer_req_compl(hba);
6068 spin_unlock_irqrestore(host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006069
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306070out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006071 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306072 if (!err) {
6073 err = SUCCESS;
6074 } else {
6075 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6076 err = FAILED;
6077 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306078 return err;
6079}
6080
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006081static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6082{
6083 struct ufshcd_lrb *lrbp;
6084 int tag;
6085
6086 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6087 lrbp = &hba->lrb[tag];
6088 lrbp->req_abort_skip = true;
6089 }
6090}
6091
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306092/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306093 * ufshcd_abort - abort a specific command
6094 * @cmd: SCSI command pointer
6095 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306096 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6097 * command, and in host controller by clearing the door-bell register. There can
6098 * be race between controller sending the command to the device while abort is
6099 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6100 * really issued and then try to abort it.
6101 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306102 * Returns SUCCESS/FAILED
6103 */
6104static int ufshcd_abort(struct scsi_cmnd *cmd)
6105{
6106 struct Scsi_Host *host;
6107 struct ufs_hba *hba;
6108 unsigned long flags;
6109 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306110 int err = 0;
6111 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306112 u8 resp = 0xF;
6113 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006114 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306115
6116 host = cmd->device->host;
6117 hba = shost_priv(host);
6118 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02006119 if (!ufshcd_valid_tag(hba, tag)) {
6120 dev_err(hba->dev,
6121 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6122 __func__, tag, cmd, cmd->request);
6123 BUG();
6124 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306125
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006126 lrbp = &hba->lrb[tag];
6127
6128 ufshcd_update_error_stats(hba, UFS_ERR_TASK_ABORT);
6129
6130 /*
6131 * Task abort to the device W-LUN is illegal. When this command
6132 * will fail, due to spec violation, scsi err handling next step
6133 * will be to send LU reset which, again, is a spec violation.
6134 * To avoid these unnecessary/illegal step we skip to the last error
6135 * handling stage: reset and restore.
6136 */
6137 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6138 return ufshcd_eh_host_reset_handler(cmd);
6139
6140 ufshcd_hold_all(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006141 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006142 /* If command is already aborted/completed, return SUCCESS */
6143 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6144 dev_err(hba->dev,
6145 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6146 __func__, tag, hba->outstanding_reqs, reg);
6147 goto out;
6148 }
6149
Dolev Ravive9d501b2014-07-01 12:22:37 +03006150 if (!(reg & (1 << tag))) {
6151 dev_err(hba->dev,
6152 "%s: cmd was completed, but without a notifying intr, tag = %d",
6153 __func__, tag);
6154 }
6155
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006156 /* Print Transfer Request of aborted task */
6157 dev_err(hba->dev, "%s: Device abort task at tag %d", __func__, tag);
6158
6159 /*
6160 * Print detailed info about aborted request.
6161 * As more than one request might get aborted at the same time,
6162 * print full information only for the first aborted request in order
6163 * to reduce repeated printouts. For other aborted requests only print
6164 * basic details.
6165 */
6166 scsi_print_command(cmd);
6167 if (!hba->req_abort_count) {
6168 ufshcd_print_host_regs(hba);
6169 ufshcd_print_host_state(hba);
6170 ufshcd_print_pwr_info(hba);
6171 ufshcd_print_trs(hba, 1 << tag, true);
6172 } else {
6173 ufshcd_print_trs(hba, 1 << tag, false);
6174 }
6175 hba->req_abort_count++;
6176
6177
6178 /* Skip task abort in case previous aborts failed and report failure */
6179 if (lrbp->req_abort_skip) {
6180 err = -EIO;
6181 goto out;
6182 }
6183
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306184 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6185 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6186 UFS_QUERY_TASK, &resp);
6187 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6188 /* cmd pending in the device */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006189 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d",
6190 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306191 break;
6192 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306193 /*
6194 * cmd not pending in the device, check if it is
6195 * in transition.
6196 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006197 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.",
6198 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306199 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6200 if (reg & (1 << tag)) {
6201 /* sleep for max. 200us to stabilize */
6202 usleep_range(100, 200);
6203 continue;
6204 }
6205 /* command completed already */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006206 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.",
6207 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306208 goto out;
6209 } else {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006210 dev_err(hba->dev,
6211 "%s: no response from device. tag = %d, err %d",
6212 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306213 if (!err)
6214 err = resp; /* service response error */
6215 goto out;
6216 }
6217 }
6218
6219 if (!poll_cnt) {
6220 err = -EBUSY;
6221 goto out;
6222 }
6223
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306224 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6225 UFS_ABORT_TASK, &resp);
6226 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006227 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306228 err = resp; /* service response error */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006229 dev_err(hba->dev, "%s: issued. tag = %d, err %d",
6230 __func__, tag, err);
6231 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306232 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306233 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306234
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306235 err = ufshcd_clear_cmd(hba, tag);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006236 if (err) {
6237 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d",
6238 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306239 goto out;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006240 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306241
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306242 scsi_dma_unmap(cmd);
6243
6244 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006245 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306246 hba->lrb[tag].cmd = NULL;
6247 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306248
6249 clear_bit_unlock(tag, &hba->lrb_in_use);
6250 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006251
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306252out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306253 if (!err) {
6254 err = SUCCESS;
6255 } else {
6256 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006257 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306258 err = FAILED;
6259 }
6260
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006261 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006262 * This ufshcd_release_all() corresponds to the original scsi cmd that
6263 * got aborted here (as we won't get any IRQ for it).
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006264 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006265 ufshcd_release_all(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306266 return err;
6267}
6268
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306269/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306270 * ufshcd_host_reset_and_restore - reset and restore host controller
6271 * @hba: per-adapter instance
6272 *
6273 * Note that host controller reset may issue DME_RESET to
6274 * local and remote (device) Uni-Pro stack and the attributes
6275 * are reset to default state.
6276 *
6277 * Returns zero on success, non-zero on failure
6278 */
6279static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6280{
6281 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306282 unsigned long flags;
6283
6284 /* Reset the host controller */
6285 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006286 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306287 spin_unlock_irqrestore(hba->host->host_lock, flags);
6288
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006289 /* scale up clocks to max frequency before full reinitialization */
6290 ufshcd_set_clk_freq(hba, true);
6291
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306292 err = ufshcd_hba_enable(hba);
6293 if (err)
6294 goto out;
6295
6296 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006297 err = ufshcd_probe_hba(hba);
6298
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006299 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306300 err = -EIO;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006301 goto out;
6302 }
6303
6304 if (!err) {
6305 err = ufshcd_vops_crypto_engine_reset(hba);
6306 if (err) {
6307 dev_err(hba->dev,
6308 "%s: failed to reset crypto engine %d\n",
6309 __func__, err);
6310 goto out;
6311 }
6312 }
6313
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306314out:
6315 if (err)
6316 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
6317
6318 return err;
6319}
6320
6321/**
6322 * ufshcd_reset_and_restore - reset and re-initialize host/device
6323 * @hba: per-adapter instance
6324 *
6325 * Reset and recover device, host and re-establish link. This
6326 * is helpful to recover the communication in fatal error conditions.
6327 *
6328 * Returns zero on success, non-zero on failure
6329 */
6330static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6331{
6332 int err = 0;
6333 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006334 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306335
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006336 do {
6337 err = ufshcd_host_reset_and_restore(hba);
6338 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306339
6340 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006341 * There is no point proceeding even after failing
6342 * to recover after multiple retries.
6343 */
6344 if (err)
6345 BUG();
6346 /*
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306347 * After reset the door-bell might be cleared, complete
6348 * outstanding requests in s/w here.
6349 */
6350 spin_lock_irqsave(hba->host->host_lock, flags);
6351 ufshcd_transfer_req_compl(hba);
6352 ufshcd_tmc_handler(hba);
6353 spin_unlock_irqrestore(hba->host->host_lock, flags);
6354
6355 return err;
6356}
6357
6358/**
6359 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
6360 * @cmd - SCSI command pointer
6361 *
6362 * Returns SUCCESS/FAILED
6363 */
6364static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6365{
6366 int err;
6367 unsigned long flags;
6368 struct ufs_hba *hba;
6369
6370 hba = shost_priv(cmd->device->host);
6371
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006372 ufshcd_hold_all(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306373 /*
6374 * Check if there is any race with fatal error handling.
6375 * If so, wait for it to complete. Even though fatal error
6376 * handling does reset and restore in some cases, don't assume
6377 * anything out of it. We are just avoiding race here.
6378 */
6379 do {
6380 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306381 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306382 hba->ufshcd_state == UFSHCD_STATE_RESET))
6383 break;
6384 spin_unlock_irqrestore(hba->host->host_lock, flags);
6385 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306386 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306387 } while (1);
6388
6389 hba->ufshcd_state = UFSHCD_STATE_RESET;
6390 ufshcd_set_eh_in_progress(hba);
6391 spin_unlock_irqrestore(hba->host->host_lock, flags);
6392
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006393 ufshcd_update_error_stats(hba, UFS_ERR_EH);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306394 err = ufshcd_reset_and_restore(hba);
6395
6396 spin_lock_irqsave(hba->host->host_lock, flags);
6397 if (!err) {
6398 err = SUCCESS;
6399 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6400 } else {
6401 err = FAILED;
6402 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6403 }
6404 ufshcd_clear_eh_in_progress(hba);
6405 spin_unlock_irqrestore(hba->host->host_lock, flags);
6406
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006407 ufshcd_release_all(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306408 return err;
6409}
6410
6411/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006412 * ufshcd_get_max_icc_level - calculate the ICC level
6413 * @sup_curr_uA: max. current supported by the regulator
6414 * @start_scan: row at the desc table to start scan from
6415 * @buff: power descriptor buffer
6416 *
6417 * Returns calculated max ICC level for specific regulator
6418 */
6419static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6420{
6421 int i;
6422 int curr_uA;
6423 u16 data;
6424 u16 unit;
6425
6426 for (i = start_scan; i >= 0; i--) {
6427 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
6428 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6429 ATTR_ICC_LVL_UNIT_OFFSET;
6430 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6431 switch (unit) {
6432 case UFSHCD_NANO_AMP:
6433 curr_uA = curr_uA / 1000;
6434 break;
6435 case UFSHCD_MILI_AMP:
6436 curr_uA = curr_uA * 1000;
6437 break;
6438 case UFSHCD_AMP:
6439 curr_uA = curr_uA * 1000 * 1000;
6440 break;
6441 case UFSHCD_MICRO_AMP:
6442 default:
6443 break;
6444 }
6445 if (sup_curr_uA >= curr_uA)
6446 break;
6447 }
6448 if (i < 0) {
6449 i = 0;
6450 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6451 }
6452
6453 return (u32)i;
6454}
6455
6456/**
6457 * ufshcd_calc_icc_level - calculate the max ICC level
6458 * In case regulators are not initialized we'll return 0
6459 * @hba: per-adapter instance
6460 * @desc_buf: power descriptor buffer to extract ICC levels from.
6461 * @len: length of desc_buff
6462 *
6463 * Returns calculated ICC level
6464 */
6465static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6466 u8 *desc_buf, int len)
6467{
6468 u32 icc_level = 0;
6469
6470 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
6471 !hba->vreg_info.vccq2) {
6472 dev_err(hba->dev,
6473 "%s: Regulator capability was not set, actvIccLevel=%d",
6474 __func__, icc_level);
6475 goto out;
6476 }
6477
6478 if (hba->vreg_info.vcc)
6479 icc_level = ufshcd_get_max_icc_level(
6480 hba->vreg_info.vcc->max_uA,
6481 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6482 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6483
6484 if (hba->vreg_info.vccq)
6485 icc_level = ufshcd_get_max_icc_level(
6486 hba->vreg_info.vccq->max_uA,
6487 icc_level,
6488 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6489
6490 if (hba->vreg_info.vccq2)
6491 icc_level = ufshcd_get_max_icc_level(
6492 hba->vreg_info.vccq2->max_uA,
6493 icc_level,
6494 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6495out:
6496 return icc_level;
6497}
6498
6499static void ufshcd_init_icc_levels(struct ufs_hba *hba)
6500{
6501 int ret;
6502 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
6503 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
6504
6505 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
6506 if (ret) {
6507 dev_err(hba->dev,
6508 "%s: Failed reading power descriptor.len = %d ret = %d",
6509 __func__, buff_len, ret);
6510 return;
6511 }
6512
6513 hba->init_prefetch_data.icc_level =
6514 ufshcd_find_max_sup_active_icc_level(hba,
6515 desc_buf, buff_len);
6516 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
6517 __func__, hba->init_prefetch_data.icc_level);
6518
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02006519 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
6520 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
6521 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006522
6523 if (ret)
6524 dev_err(hba->dev,
6525 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
6526 __func__, hba->init_prefetch_data.icc_level , ret);
6527
6528}
6529
6530/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006531 * ufshcd_scsi_add_wlus - Adds required W-LUs
6532 * @hba: per-adapter instance
6533 *
6534 * UFS device specification requires the UFS devices to support 4 well known
6535 * logical units:
6536 * "REPORT_LUNS" (address: 01h)
6537 * "UFS Device" (address: 50h)
6538 * "RPMB" (address: 44h)
6539 * "BOOT" (address: 30h)
6540 * UFS device's power management needs to be controlled by "POWER CONDITION"
6541 * field of SSU (START STOP UNIT) command. But this "power condition" field
6542 * will take effect only when its sent to "UFS device" well known logical unit
6543 * hence we require the scsi_device instance to represent this logical unit in
6544 * order for the UFS host driver to send the SSU command for power management.
6545
6546 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
6547 * Block) LU so user space process can control this LU. User space may also
6548 * want to have access to BOOT LU.
6549
6550 * This function adds scsi device instances for each of all well known LUs
6551 * (except "REPORT LUNS" LU).
6552 *
6553 * Returns zero on success (all required W-LUs are added successfully),
6554 * non-zero error value on failure (if failed to add any of the required W-LU).
6555 */
6556static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
6557{
6558 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006559 struct scsi_device *sdev_rpmb;
6560 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006561
6562 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
6563 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
6564 if (IS_ERR(hba->sdev_ufs_device)) {
6565 ret = PTR_ERR(hba->sdev_ufs_device);
6566 hba->sdev_ufs_device = NULL;
6567 goto out;
6568 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006569 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006570
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006571 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006572 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006573 if (IS_ERR(sdev_boot)) {
6574 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006575 goto remove_sdev_ufs_device;
6576 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006577 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006578
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006579 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006580 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006581 if (IS_ERR(sdev_rpmb)) {
6582 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006583 goto remove_sdev_boot;
6584 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006585 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006586 goto out;
6587
6588remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006589 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006590remove_sdev_ufs_device:
6591 scsi_remove_device(hba->sdev_ufs_device);
6592out:
6593 return ret;
6594}
6595
6596/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006597 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6598 * @hba: per-adapter instance
6599 *
6600 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6601 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6602 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6603 * the hibern8 exit latency.
6604 *
6605 * Returns zero on success, non-zero error value on failure.
6606 */
6607static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6608{
6609 int ret = 0;
6610 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6611
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006612 if (!ufshcd_is_unipro_pa_params_tuning_req(hba))
6613 return 0;
6614
Yaniv Gardi37113102016-03-10 17:37:16 +02006615 ret = ufshcd_dme_peer_get(hba,
6616 UIC_ARG_MIB_SEL(
6617 RX_MIN_ACTIVATETIME_CAPABILITY,
6618 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6619 &peer_rx_min_activatetime);
6620 if (ret)
6621 goto out;
6622
6623 /* make sure proper unit conversion is applied */
6624 tuned_pa_tactivate =
6625 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6626 / PA_TACTIVATE_TIME_UNIT_US);
6627 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6628 tuned_pa_tactivate);
6629
6630out:
6631 return ret;
6632}
6633
6634/**
6635 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6636 * @hba: per-adapter instance
6637 *
6638 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6639 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6640 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6641 * This optimal value can help reduce the hibern8 exit latency.
6642 *
6643 * Returns zero on success, non-zero error value on failure.
6644 */
6645static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6646{
6647 int ret = 0;
6648 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6649 u32 max_hibern8_time, tuned_pa_hibern8time;
6650
6651 ret = ufshcd_dme_get(hba,
6652 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6653 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6654 &local_tx_hibern8_time_cap);
6655 if (ret)
6656 goto out;
6657
6658 ret = ufshcd_dme_peer_get(hba,
6659 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6660 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6661 &peer_rx_hibern8_time_cap);
6662 if (ret)
6663 goto out;
6664
6665 max_hibern8_time = max(local_tx_hibern8_time_cap,
6666 peer_rx_hibern8_time_cap);
6667 /* make sure proper unit conversion is applied */
6668 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6669 / PA_HIBERN8_TIME_UNIT_US);
6670 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6671 tuned_pa_hibern8time);
6672out:
6673 return ret;
6674}
6675
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006676/**
6677 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6678 * less than device PA_TACTIVATE time.
6679 * @hba: per-adapter instance
6680 *
6681 * Some UFS devices require host PA_TACTIVATE to be lower than device
6682 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6683 * for such devices.
6684 *
6685 * Returns zero on success, non-zero error value on failure.
6686 */
6687static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6688{
6689 int ret = 0;
6690 u32 granularity, peer_granularity;
6691 u32 pa_tactivate, peer_pa_tactivate;
6692 u32 pa_tactivate_us, peer_pa_tactivate_us;
6693 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6694
6695 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6696 &granularity);
6697 if (ret)
6698 goto out;
6699
6700 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6701 &peer_granularity);
6702 if (ret)
6703 goto out;
6704
6705 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6706 (granularity > PA_GRANULARITY_MAX_VAL)) {
6707 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6708 __func__, granularity);
6709 return -EINVAL;
6710 }
6711
6712 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6713 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6714 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6715 __func__, peer_granularity);
6716 return -EINVAL;
6717 }
6718
6719 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6720 if (ret)
6721 goto out;
6722
6723 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6724 &peer_pa_tactivate);
6725 if (ret)
6726 goto out;
6727
6728 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6729 peer_pa_tactivate_us = peer_pa_tactivate *
6730 gran_to_us_table[peer_granularity - 1];
6731
6732 if (pa_tactivate_us > peer_pa_tactivate_us) {
6733 u32 new_peer_pa_tactivate;
6734
6735 new_peer_pa_tactivate = pa_tactivate_us /
6736 gran_to_us_table[peer_granularity - 1];
6737 new_peer_pa_tactivate++;
6738 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6739 new_peer_pa_tactivate);
6740 }
6741
6742out:
6743 return ret;
6744}
6745
Yaniv Gardi37113102016-03-10 17:37:16 +02006746static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
6747{
6748 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6749 ufshcd_tune_pa_tactivate(hba);
6750 ufshcd_tune_pa_hibern8time(hba);
6751 }
6752
6753 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6754 /* set 1ms timeout for PA_TACTIVATE */
6755 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006756
6757 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6758 ufshcd_quirk_tune_host_pa_tactivate(hba);
6759
6760 ufshcd_vops_apply_dev_quirks(hba);
6761}
6762
6763static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6764{
6765 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
6766
6767 hba->ufs_stats.hibern8_exit_cnt = 0;
6768 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
6769
6770 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
6771 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
6772 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
6773 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
6774 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
6775
6776 hba->req_abort_count = 0;
6777}
6778
6779static void ufshcd_apply_pm_quirks(struct ufs_hba *hba)
6780{
6781 if (hba->dev_quirks & UFS_DEVICE_QUIRK_NO_LINK_OFF) {
6782 if (ufs_get_pm_lvl_to_link_pwr_state(hba->rpm_lvl) ==
6783 UIC_LINK_OFF_STATE) {
6784 hba->rpm_lvl =
6785 ufs_get_desired_pm_lvl_for_dev_link_state(
6786 UFS_SLEEP_PWR_MODE,
6787 UIC_LINK_HIBERN8_STATE);
6788 dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed rpm_lvl to %d\n",
6789 hba->rpm_lvl);
6790 }
6791 if (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
6792 UIC_LINK_OFF_STATE) {
6793 hba->spm_lvl =
6794 ufs_get_desired_pm_lvl_for_dev_link_state(
6795 UFS_SLEEP_PWR_MODE,
6796 UIC_LINK_HIBERN8_STATE);
6797 dev_info(hba->dev, "UFS_DEVICE_QUIRK_NO_LINK_OFF enabled, changed spm_lvl to %d\n",
6798 hba->spm_lvl);
6799 }
6800 }
Yaniv Gardi37113102016-03-10 17:37:16 +02006801}
6802
6803/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006804 * ufshcd_probe_hba - probe hba to detect device and initialize
6805 * @hba: per-adapter instance
6806 *
6807 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306808 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006809static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306810{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306811 int ret;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006812 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306813
6814 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306815 if (ret)
6816 goto out;
6817
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006818 /* Enable auto hibern8 if supported */
6819 if (ufshcd_is_auto_hibern8_supported(hba))
6820 ufshcd_set_auto_hibern8_timer(hba,
6821 hba->hibern8_on_idle.delay_ms);
Yaniv Gardi50646362014-10-23 13:25:13 +03006822
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006823 /* Debug counters initialization */
6824 ufshcd_clear_dbg_ufs_stats(hba);
Yaniv Gardiafdfff52016-03-10 17:37:15 +02006825 /* set the default level for urgent bkops */
6826 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
6827 hba->is_urgent_bkops_lvl_checked = false;
6828
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006829 /* UniPro link is active now */
6830 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05306831
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306832 ret = ufshcd_verify_dev_init(hba);
6833 if (ret)
6834 goto out;
6835
Dolev Raviv68078d52013-07-30 00:35:58 +05306836 ret = ufshcd_complete_dev_init(hba);
6837 if (ret)
6838 goto out;
6839
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006840 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006841 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006842
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006843 ufshcd_apply_pm_quirks(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006844 ret = ufshcd_set_vccq_rail_unused(hba,
6845 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
6846 if (ret)
6847 goto out;
6848
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006849 /* UFS device is also active now */
6850 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306851 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006852 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306853
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006854 if (ufshcd_get_max_pwr_mode(hba)) {
6855 dev_err(hba->dev,
6856 "%s: Failed getting max supported power mode\n",
6857 __func__);
6858 } else {
6859 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006860 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006861 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
6862 __func__, ret);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006863 goto out;
6864 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006865 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006866
Yaniv Gardi53c12d02016-02-01 15:02:45 +02006867 /* set the state as operational after switching to desired gear */
6868 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006869 /*
6870 * If we are in error handling context or in power management callbacks
6871 * context, no need to scan the host
6872 */
6873 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6874 bool flag;
6875
6876 /* clear any previous UFS device information */
6877 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02006878 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6879 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006880 hba->dev_info.f_power_on_wp_en = flag;
6881
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006882 if (!hba->is_init_prefetch)
6883 ufshcd_init_icc_levels(hba);
6884
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006885 /* Add required well known logical units to scsi mid layer */
6886 if (ufshcd_scsi_add_wlus(hba))
6887 goto out;
6888
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306889 scsi_scan_host(hba->host);
6890 pm_runtime_put_sync(hba->dev);
6891 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006892
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006893 /* Resume devfreq after UFS device is detected */
6894 if (ufshcd_is_clkscaling_supported(hba)) {
6895 memcpy(&hba->clk_scaling.saved_pwr_info.info, &hba->pwr_info,
6896 sizeof(struct ufs_pa_layer_attr));
6897 hba->clk_scaling.saved_pwr_info.is_valid = true;
6898 hba->clk_scaling.is_scaled_up = true;
6899 ufshcd_resume_clkscaling(hba);
6900 hba->clk_scaling.is_allowed = true;
6901 }
6902
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006903 if (!hba->is_init_prefetch)
6904 hba->is_init_prefetch = true;
6905
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306906out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006907 /*
6908 * If we failed to initialize the device or the device is not
6909 * present, turn off the power/clocks etc.
6910 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006911 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6912 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006913 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006914 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006915
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006916 trace_ufshcd_init(dev_name(hba->dev), ret,
6917 ktime_to_us(ktime_sub(ktime_get(), start)),
6918 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006919 return ret;
6920}
6921
6922/**
6923 * ufshcd_async_scan - asynchronous execution for probing hba
6924 * @data: data pointer to pass to this function
6925 * @cookie: cookie data
6926 */
6927static void ufshcd_async_scan(void *data, async_cookie_t cookie)
6928{
6929 struct ufs_hba *hba = (struct ufs_hba *)data;
6930
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006931 /*
6932 * Don't allow clock gating and hibern8 enter for faster device
6933 * detection.
6934 */
6935 ufshcd_hold_all(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006936 ufshcd_probe_hba(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07006937 ufshcd_release_all(hba);
6938}
6939
6940/**
6941 * ufshcd_query_ioctl - perform user read queries
6942 * @hba: per-adapter instance
6943 * @lun: used for lun specific queries
6944 * @buffer: user space buffer for reading and submitting query data and params
6945 * @return: 0 for success negative error code otherwise
6946 *
6947 * Expected/Submitted buffer structure is struct ufs_ioctl_query_data.
6948 * It will read the opcode, idn and buf_length parameters, and, put the
6949 * response in the buffer field while updating the used size in buf_length.
6950 */
6951static int ufshcd_query_ioctl(struct ufs_hba *hba, u8 lun, void __user *buffer)
6952{
6953 struct ufs_ioctl_query_data *ioctl_data;
6954 int err = 0;
6955 int length = 0;
6956 void *data_ptr;
6957 bool flag;
6958 u32 att;
6959 u8 index;
6960 u8 *desc = NULL;
6961
6962 ioctl_data = kzalloc(sizeof(struct ufs_ioctl_query_data), GFP_KERNEL);
6963 if (!ioctl_data) {
6964 dev_err(hba->dev, "%s: Failed allocating %zu bytes\n", __func__,
6965 sizeof(struct ufs_ioctl_query_data));
6966 err = -ENOMEM;
6967 goto out;
6968 }
6969
6970 /* extract params from user buffer */
6971 err = copy_from_user(ioctl_data, buffer,
6972 sizeof(struct ufs_ioctl_query_data));
6973 if (err) {
6974 dev_err(hba->dev,
6975 "%s: Failed copying buffer from user, err %d\n",
6976 __func__, err);
6977 goto out_release_mem;
6978 }
6979
6980 /* verify legal parameters & send query */
6981 switch (ioctl_data->opcode) {
6982 case UPIU_QUERY_OPCODE_READ_DESC:
6983 switch (ioctl_data->idn) {
6984 case QUERY_DESC_IDN_DEVICE:
6985 case QUERY_DESC_IDN_CONFIGURAION:
6986 case QUERY_DESC_IDN_INTERCONNECT:
6987 case QUERY_DESC_IDN_GEOMETRY:
6988 case QUERY_DESC_IDN_POWER:
6989 index = 0;
6990 break;
6991 case QUERY_DESC_IDN_UNIT:
6992 if (!ufs_is_valid_unit_desc_lun(lun)) {
6993 dev_err(hba->dev,
6994 "%s: No unit descriptor for lun 0x%x\n",
6995 __func__, lun);
6996 err = -EINVAL;
6997 goto out_release_mem;
6998 }
6999 index = lun;
7000 break;
7001 default:
7002 goto out_einval;
7003 }
7004 length = min_t(int, QUERY_DESC_MAX_SIZE,
7005 ioctl_data->buf_size);
7006 desc = kzalloc(length, GFP_KERNEL);
7007 if (!desc) {
7008 dev_err(hba->dev, "%s: Failed allocating %d bytes\n",
7009 __func__, length);
7010 err = -ENOMEM;
7011 goto out_release_mem;
7012 }
7013 err = ufshcd_query_descriptor(hba, ioctl_data->opcode,
7014 ioctl_data->idn, index, 0, desc, &length);
7015 break;
7016 case UPIU_QUERY_OPCODE_READ_ATTR:
7017 switch (ioctl_data->idn) {
7018 case QUERY_ATTR_IDN_BOOT_LU_EN:
7019 case QUERY_ATTR_IDN_POWER_MODE:
7020 case QUERY_ATTR_IDN_ACTIVE_ICC_LVL:
7021 case QUERY_ATTR_IDN_OOO_DATA_EN:
7022 case QUERY_ATTR_IDN_BKOPS_STATUS:
7023 case QUERY_ATTR_IDN_PURGE_STATUS:
7024 case QUERY_ATTR_IDN_MAX_DATA_IN:
7025 case QUERY_ATTR_IDN_MAX_DATA_OUT:
7026 case QUERY_ATTR_IDN_REF_CLK_FREQ:
7027 case QUERY_ATTR_IDN_CONF_DESC_LOCK:
7028 case QUERY_ATTR_IDN_MAX_NUM_OF_RTT:
7029 case QUERY_ATTR_IDN_EE_CONTROL:
7030 case QUERY_ATTR_IDN_EE_STATUS:
7031 case QUERY_ATTR_IDN_SECONDS_PASSED:
7032 index = 0;
7033 break;
7034 case QUERY_ATTR_IDN_DYN_CAP_NEEDED:
7035 case QUERY_ATTR_IDN_CORR_PRG_BLK_NUM:
7036 index = lun;
7037 break;
7038 default:
7039 goto out_einval;
7040 }
7041 err = ufshcd_query_attr(hba, ioctl_data->opcode, ioctl_data->idn,
7042 index, 0, &att);
7043 break;
7044
7045 case UPIU_QUERY_OPCODE_WRITE_ATTR:
7046 err = copy_from_user(&att,
7047 buffer + sizeof(struct ufs_ioctl_query_data),
7048 sizeof(u32));
7049 if (err) {
7050 dev_err(hba->dev,
7051 "%s: Failed copying buffer from user, err %d\n",
7052 __func__, err);
7053 goto out_release_mem;
7054 }
7055
7056 switch (ioctl_data->idn) {
7057 case QUERY_ATTR_IDN_BOOT_LU_EN:
7058 index = 0;
7059 if (att > QUERY_ATTR_IDN_BOOT_LU_EN_MAX) {
7060 dev_err(hba->dev,
7061 "%s: Illegal ufs query ioctl data, opcode 0x%x, idn 0x%x, att 0x%x\n",
7062 __func__, ioctl_data->opcode,
7063 (unsigned int)ioctl_data->idn, att);
7064 err = -EINVAL;
7065 goto out_release_mem;
7066 }
7067 break;
7068 default:
7069 goto out_einval;
7070 }
7071 err = ufshcd_query_attr(hba, ioctl_data->opcode,
7072 ioctl_data->idn, index, 0, &att);
7073 break;
7074
7075 case UPIU_QUERY_OPCODE_READ_FLAG:
7076 switch (ioctl_data->idn) {
7077 case QUERY_FLAG_IDN_FDEVICEINIT:
7078 case QUERY_FLAG_IDN_PERMANENT_WPE:
7079 case QUERY_FLAG_IDN_PWR_ON_WPE:
7080 case QUERY_FLAG_IDN_BKOPS_EN:
7081 case QUERY_FLAG_IDN_PURGE_ENABLE:
7082 case QUERY_FLAG_IDN_FPHYRESOURCEREMOVAL:
7083 case QUERY_FLAG_IDN_BUSY_RTC:
7084 break;
7085 default:
7086 goto out_einval;
7087 }
7088 err = ufshcd_query_flag_retry(hba, ioctl_data->opcode,
7089 ioctl_data->idn, &flag);
7090 break;
7091 default:
7092 goto out_einval;
7093 }
7094
7095 if (err) {
7096 dev_err(hba->dev, "%s: Query for idn %d failed\n", __func__,
7097 ioctl_data->idn);
7098 goto out_release_mem;
7099 }
7100
7101 /*
7102 * copy response data
7103 * As we might end up reading less data then what is specified in
7104 * "ioctl_data->buf_size". So we are updating "ioctl_data->
7105 * buf_size" to what exactly we have read.
7106 */
7107 switch (ioctl_data->opcode) {
7108 case UPIU_QUERY_OPCODE_READ_DESC:
7109 ioctl_data->buf_size = min_t(int, ioctl_data->buf_size, length);
7110 data_ptr = desc;
7111 break;
7112 case UPIU_QUERY_OPCODE_READ_ATTR:
7113 ioctl_data->buf_size = sizeof(u32);
7114 data_ptr = &att;
7115 break;
7116 case UPIU_QUERY_OPCODE_READ_FLAG:
7117 ioctl_data->buf_size = 1;
7118 data_ptr = &flag;
7119 break;
7120 case UPIU_QUERY_OPCODE_WRITE_ATTR:
7121 goto out_release_mem;
7122 default:
7123 goto out_einval;
7124 }
7125
7126 /* copy to user */
7127 err = copy_to_user(buffer, ioctl_data,
7128 sizeof(struct ufs_ioctl_query_data));
7129 if (err)
7130 dev_err(hba->dev, "%s: Failed copying back to user.\n",
7131 __func__);
7132 err = copy_to_user(buffer + sizeof(struct ufs_ioctl_query_data),
7133 data_ptr, ioctl_data->buf_size);
7134 if (err)
7135 dev_err(hba->dev, "%s: err %d copying back to user.\n",
7136 __func__, err);
7137 goto out_release_mem;
7138
7139out_einval:
7140 dev_err(hba->dev,
7141 "%s: illegal ufs query ioctl data, opcode 0x%x, idn 0x%x\n",
7142 __func__, ioctl_data->opcode, (unsigned int)ioctl_data->idn);
7143 err = -EINVAL;
7144out_release_mem:
7145 kfree(ioctl_data);
7146 kfree(desc);
7147out:
7148 return err;
7149}
7150
7151/**
7152 * ufshcd_ioctl - ufs ioctl callback registered in scsi_host
7153 * @dev: scsi device required for per LUN queries
7154 * @cmd: command opcode
7155 * @buffer: user space buffer for transferring data
7156 *
7157 * Supported commands:
7158 * UFS_IOCTL_QUERY
7159 */
7160static int ufshcd_ioctl(struct scsi_device *dev, int cmd, void __user *buffer)
7161{
7162 struct ufs_hba *hba = shost_priv(dev->host);
7163 int err = 0;
7164
7165 BUG_ON(!hba);
7166 if (!buffer) {
7167 dev_err(hba->dev, "%s: User buffer is NULL!\n", __func__);
7168 return -EINVAL;
7169 }
7170
7171 switch (cmd) {
7172 case UFS_IOCTL_QUERY:
7173 pm_runtime_get_sync(hba->dev);
7174 err = ufshcd_query_ioctl(hba, ufshcd_scsi_to_upiu_lun(dev->lun),
7175 buffer);
7176 pm_runtime_put_sync(hba->dev);
7177 break;
7178 default:
7179 err = -ENOIOCTLCMD;
7180 dev_dbg(hba->dev, "%s: Unsupported ioctl cmd %d\n", __func__,
7181 cmd);
7182 break;
7183 }
7184
7185 return err;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307186}
7187
Yaniv Gardif550c652016-03-10 17:37:07 +02007188static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
7189{
7190 unsigned long flags;
7191 struct Scsi_Host *host;
7192 struct ufs_hba *hba;
7193 int index;
7194 bool found = false;
7195
7196 if (!scmd || !scmd->device || !scmd->device->host)
7197 return BLK_EH_NOT_HANDLED;
7198
7199 host = scmd->device->host;
7200 hba = shost_priv(host);
7201 if (!hba)
7202 return BLK_EH_NOT_HANDLED;
7203
7204 spin_lock_irqsave(host->host_lock, flags);
7205
7206 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
7207 if (hba->lrb[index].cmd == scmd) {
7208 found = true;
7209 break;
7210 }
7211 }
7212
7213 spin_unlock_irqrestore(host->host_lock, flags);
7214
7215 /*
7216 * Bypass SCSI error handling and reset the block layer timer if this
7217 * SCSI command was not actually dispatched to UFS driver, otherwise
7218 * let SCSI layer handle the error as usual.
7219 */
7220 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
7221}
7222
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307223static struct scsi_host_template ufshcd_driver_template = {
7224 .module = THIS_MODULE,
7225 .name = UFSHCD,
7226 .proc_name = UFSHCD,
7227 .queuecommand = ufshcd_queuecommand,
7228 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007229 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307230 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007231 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307232 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307233 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7234 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02007235 .eh_timed_out = ufshcd_eh_timed_out,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007236 .ioctl = ufshcd_ioctl,
7237#ifdef CONFIG_COMPAT
7238 .compat_ioctl = ufshcd_ioctl,
7239#endif
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307240 .this_id = -1,
7241 .sg_tablesize = SG_ALL,
7242 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7243 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007244 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007245 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307246};
7247
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007248static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7249 int ua)
7250{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007251 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007252
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007253 if (!vreg)
7254 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007255
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007256 ret = regulator_set_load(vreg->reg, ua);
7257 if (ret < 0) {
7258 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7259 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007260 }
7261
7262 return ret;
7263}
7264
7265static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7266 struct ufs_vreg *vreg)
7267{
Yaniv Gardi60f01872016-03-10 17:37:11 +02007268 if (!vreg)
7269 return 0;
7270 else if (vreg->unused)
7271 return 0;
7272 else
7273 return ufshcd_config_vreg_load(hba->dev, vreg,
7274 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007275}
7276
7277static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7278 struct ufs_vreg *vreg)
7279{
Yaniv Gardi60f01872016-03-10 17:37:11 +02007280 if (!vreg)
7281 return 0;
7282 else if (vreg->unused)
7283 return 0;
7284 else
7285 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007286}
7287
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007288static int ufshcd_config_vreg(struct device *dev,
7289 struct ufs_vreg *vreg, bool on)
7290{
7291 int ret = 0;
7292 struct regulator *reg = vreg->reg;
7293 const char *name = vreg->name;
7294 int min_uV, uA_load;
7295
7296 BUG_ON(!vreg);
7297
7298 if (regulator_count_voltages(reg) > 0) {
7299 min_uV = on ? vreg->min_uV : 0;
7300 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7301 if (ret) {
7302 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
7303 __func__, name, ret);
7304 goto out;
7305 }
7306
7307 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007308 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7309 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007310 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007311 }
7312out:
7313 return ret;
7314}
7315
7316static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7317{
7318 int ret = 0;
7319
Yaniv Gardi60f01872016-03-10 17:37:11 +02007320 if (!vreg)
7321 goto out;
7322 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007323 goto out;
7324
7325 ret = ufshcd_config_vreg(dev, vreg, true);
7326 if (!ret)
7327 ret = regulator_enable(vreg->reg);
7328
7329 if (!ret)
7330 vreg->enabled = true;
7331 else
7332 dev_err(dev, "%s: %s enable failed, err=%d\n",
7333 __func__, vreg->name, ret);
7334out:
7335 return ret;
7336}
7337
7338static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7339{
7340 int ret = 0;
7341
Yaniv Gardi60f01872016-03-10 17:37:11 +02007342 if (!vreg)
7343 goto out;
7344 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007345 goto out;
7346
7347 ret = regulator_disable(vreg->reg);
7348
7349 if (!ret) {
7350 /* ignore errors on applying disable config */
7351 ufshcd_config_vreg(dev, vreg, false);
7352 vreg->enabled = false;
7353 } else {
7354 dev_err(dev, "%s: %s disable failed, err=%d\n",
7355 __func__, vreg->name, ret);
7356 }
7357out:
7358 return ret;
7359}
7360
7361static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7362{
7363 int ret = 0;
7364 struct device *dev = hba->dev;
7365 struct ufs_vreg_info *info = &hba->vreg_info;
7366
7367 if (!info)
7368 goto out;
7369
7370 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7371 if (ret)
7372 goto out;
7373
7374 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7375 if (ret)
7376 goto out;
7377
7378 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7379 if (ret)
7380 goto out;
7381
7382out:
7383 if (ret) {
7384 ufshcd_toggle_vreg(dev, info->vccq2, false);
7385 ufshcd_toggle_vreg(dev, info->vccq, false);
7386 ufshcd_toggle_vreg(dev, info->vcc, false);
7387 }
7388 return ret;
7389}
7390
Raviv Shvili6a771a62014-09-25 15:32:24 +03007391static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7392{
7393 struct ufs_vreg_info *info = &hba->vreg_info;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007394 int ret = 0;
Raviv Shvili6a771a62014-09-25 15:32:24 +03007395
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007396 if (info->vdd_hba) {
7397 ret = ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007398
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007399 if (!ret)
7400 ufshcd_vops_update_sec_cfg(hba, on);
7401 }
7402
7403 return ret;
Raviv Shvili6a771a62014-09-25 15:32:24 +03007404}
7405
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007406static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7407{
7408 int ret = 0;
7409
7410 if (!vreg)
7411 goto out;
7412
7413 vreg->reg = devm_regulator_get(dev, vreg->name);
7414 if (IS_ERR(vreg->reg)) {
7415 ret = PTR_ERR(vreg->reg);
7416 dev_err(dev, "%s: %s get failed, err=%d\n",
7417 __func__, vreg->name, ret);
7418 }
7419out:
7420 return ret;
7421}
7422
7423static int ufshcd_init_vreg(struct ufs_hba *hba)
7424{
7425 int ret = 0;
7426 struct device *dev = hba->dev;
7427 struct ufs_vreg_info *info = &hba->vreg_info;
7428
7429 if (!info)
7430 goto out;
7431
7432 ret = ufshcd_get_vreg(dev, info->vcc);
7433 if (ret)
7434 goto out;
7435
7436 ret = ufshcd_get_vreg(dev, info->vccq);
7437 if (ret)
7438 goto out;
7439
7440 ret = ufshcd_get_vreg(dev, info->vccq2);
7441out:
7442 return ret;
7443}
7444
Raviv Shvili6a771a62014-09-25 15:32:24 +03007445static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
7446{
7447 struct ufs_vreg_info *info = &hba->vreg_info;
7448
7449 if (info)
7450 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
7451
7452 return 0;
7453}
7454
Yaniv Gardi60f01872016-03-10 17:37:11 +02007455static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
7456{
7457 int ret = 0;
7458 struct ufs_vreg_info *info = &hba->vreg_info;
7459
7460 if (!info)
7461 goto out;
7462 else if (!info->vccq)
7463 goto out;
7464
7465 if (unused) {
7466 /* shut off the rail here */
7467 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
7468 /*
7469 * Mark this rail as no longer used, so it doesn't get enabled
7470 * later by mistake
7471 */
7472 if (!ret)
7473 info->vccq->unused = true;
7474 } else {
7475 /*
7476 * rail should have been already enabled hence just make sure
7477 * that unused flag is cleared.
7478 */
7479 info->vccq->unused = false;
7480 }
7481out:
7482 return ret;
7483}
7484
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007485static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
7486 bool skip_ref_clk, bool is_gating_context)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007487{
7488 int ret = 0;
7489 struct ufs_clk_info *clki;
7490 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007491 unsigned long flags;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007492 ktime_t start = ktime_get();
7493 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007494
7495 if (!head || list_empty(head))
7496 goto out;
7497
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007498 /*
7499 * vendor specific setup_clocks ops may depend on clocks managed by
7500 * this standard driver hence call the vendor specific setup_clocks
7501 * before disabling the clocks managed here.
7502 */
7503 if (!on) {
7504 ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context);
7505 if (ret)
7506 return ret;
7507 }
7508
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007509 list_for_each_entry(clki, head, list) {
7510 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007511 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
7512 continue;
7513
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007514 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007515 if (on && !clki->enabled) {
7516 ret = clk_prepare_enable(clki->clk);
7517 if (ret) {
7518 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
7519 __func__, clki->name, ret);
7520 goto out;
7521 }
7522 } else if (!on && clki->enabled) {
7523 clk_disable_unprepare(clki->clk);
7524 }
7525 clki->enabled = on;
7526 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
7527 clki->name, on ? "en" : "dis");
7528 }
7529 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007530
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007531 /*
7532 * vendor specific setup_clocks ops may depend on clocks managed by
7533 * this standard driver hence call the vendor specific setup_clocks
7534 * after enabling the clocks managed here.
7535 */
7536 if (on)
7537 ret = ufshcd_vops_setup_clocks(hba, on, is_gating_context);
7538
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007539out:
7540 if (ret) {
7541 list_for_each_entry(clki, head, list) {
7542 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
7543 clk_disable_unprepare(clki->clk);
7544 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007545 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007546 spin_lock_irqsave(hba->host->host_lock, flags);
7547 hba->clk_gating.state = CLKS_ON;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007548 trace_ufshcd_clk_gating(dev_name(hba->dev),
7549 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007550 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007551 /* restore the secure configuration as clocks are enabled */
7552 ufshcd_vops_update_sec_cfg(hba, true);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007553 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007554
7555 if (clk_state_changed)
7556 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
7557 (on ? "on" : "off"),
7558 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007559 return ret;
7560}
7561
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007562static int ufshcd_enable_clocks(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007563{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007564 return ufshcd_setup_clocks(hba, true, false, false);
7565}
7566
7567static int ufshcd_disable_clocks(struct ufs_hba *hba,
7568 bool is_gating_context)
7569{
7570 return ufshcd_setup_clocks(hba, false, false, is_gating_context);
7571}
7572
7573static int ufshcd_disable_clocks_skip_ref_clk(struct ufs_hba *hba,
7574 bool is_gating_context)
7575{
7576 return ufshcd_setup_clocks(hba, false, true, is_gating_context);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007577}
7578
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007579static int ufshcd_init_clocks(struct ufs_hba *hba)
7580{
7581 int ret = 0;
7582 struct ufs_clk_info *clki;
7583 struct device *dev = hba->dev;
7584 struct list_head *head = &hba->clk_list_head;
7585
7586 if (!head || list_empty(head))
7587 goto out;
7588
7589 list_for_each_entry(clki, head, list) {
7590 if (!clki->name)
7591 continue;
7592
7593 clki->clk = devm_clk_get(dev, clki->name);
7594 if (IS_ERR(clki->clk)) {
7595 ret = PTR_ERR(clki->clk);
7596 dev_err(dev, "%s: %s clk get failed, %d\n",
7597 __func__, clki->name, ret);
7598 goto out;
7599 }
7600
7601 if (clki->max_freq) {
7602 ret = clk_set_rate(clki->clk, clki->max_freq);
7603 if (ret) {
7604 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7605 __func__, clki->name,
7606 clki->max_freq, ret);
7607 goto out;
7608 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03007609 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007610 }
7611 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
7612 clki->name, clk_get_rate(clki->clk));
7613 }
7614out:
7615 return ret;
7616}
7617
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007618static int ufshcd_variant_hba_init(struct ufs_hba *hba)
7619{
7620 int err = 0;
7621
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007622 if (!hba->var || !hba->var->vops)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007623 goto out;
7624
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007625 err = ufshcd_vops_init(hba);
7626 if (err)
7627 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007628
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007629 err = ufshcd_vops_setup_regulators(hba, true);
7630 if (err)
7631 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007632
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007633 goto out;
7634
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007635out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007636 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007637out:
7638 if (err)
7639 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007640 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007641 return err;
7642}
7643
7644static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
7645{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007646 if (!hba->var || !hba->var->vops)
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007647 return;
7648
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007649 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007650
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007651 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007652}
7653
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007654static int ufshcd_hba_init(struct ufs_hba *hba)
7655{
7656 int err;
7657
Raviv Shvili6a771a62014-09-25 15:32:24 +03007658 /*
7659 * Handle host controller power separately from the UFS device power
7660 * rails as it will help controlling the UFS host controller power
7661 * collapse easily which is different than UFS device power collapse.
7662 * Also, enable the host controller power before we go ahead with rest
7663 * of the initialization here.
7664 */
7665 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007666 if (err)
7667 goto out;
7668
Raviv Shvili6a771a62014-09-25 15:32:24 +03007669 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007670 if (err)
7671 goto out;
7672
Raviv Shvili6a771a62014-09-25 15:32:24 +03007673 err = ufshcd_init_clocks(hba);
7674 if (err)
7675 goto out_disable_hba_vreg;
7676
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007677 err = ufshcd_enable_clocks(hba);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007678 if (err)
7679 goto out_disable_hba_vreg;
7680
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007681 err = ufshcd_init_vreg(hba);
7682 if (err)
7683 goto out_disable_clks;
7684
7685 err = ufshcd_setup_vreg(hba, true);
7686 if (err)
7687 goto out_disable_clks;
7688
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007689 err = ufshcd_variant_hba_init(hba);
7690 if (err)
7691 goto out_disable_vreg;
7692
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007693 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007694 goto out;
7695
7696out_disable_vreg:
7697 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007698out_disable_clks:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007699 ufshcd_disable_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007700out_disable_hba_vreg:
7701 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007702out:
7703 return err;
7704}
7705
7706static void ufshcd_hba_exit(struct ufs_hba *hba)
7707{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007708 if (hba->is_powered) {
7709 ufshcd_variant_hba_exit(hba);
7710 ufshcd_setup_vreg(hba, false);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007711 if (ufshcd_is_clkscaling_supported(hba)) {
7712 ufshcd_suspend_clkscaling(hba);
7713 destroy_workqueue(hba->clk_scaling.workq);
7714 }
7715 ufshcd_disable_clocks(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007716 ufshcd_setup_hba_vreg(hba, false);
7717 hba->is_powered = false;
7718 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007719}
7720
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007721static int
7722ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307723{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007724 unsigned char cmd[6] = {REQUEST_SENSE,
7725 0,
7726 0,
7727 0,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007728 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007729 0};
7730 char *buffer;
7731 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307732
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007733 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007734 if (!buffer) {
7735 ret = -ENOMEM;
7736 goto out;
7737 }
7738
7739 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007740 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007741 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
7742 if (ret)
7743 pr_err("%s: failed with err %d\n", __func__, ret);
7744
7745 kfree(buffer);
7746out:
7747 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307748}
7749
7750/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007751 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
7752 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307753 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007754 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307755 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007756 * Returns 0 if requested power mode is set successfully
7757 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307758 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007759static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
7760 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307761{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007762 unsigned char cmd[6] = { START_STOP };
7763 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007764 struct scsi_device *sdp;
7765 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007766 int ret;
7767
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007768 spin_lock_irqsave(hba->host->host_lock, flags);
7769 sdp = hba->sdev_ufs_device;
7770 if (sdp) {
7771 ret = scsi_device_get(sdp);
7772 if (!ret && !scsi_device_online(sdp)) {
7773 ret = -ENODEV;
7774 scsi_device_put(sdp);
7775 }
7776 } else {
7777 ret = -ENODEV;
7778 }
7779 spin_unlock_irqrestore(hba->host->host_lock, flags);
7780
7781 if (ret)
7782 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007783
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307784 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007785 * If scsi commands fail, the scsi mid-layer schedules scsi error-
7786 * handling, which would wait for host to be resumed. Since we know
7787 * we are functional while we are here, skip host resume in error
7788 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307789 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007790 hba->host->eh_noresume = 1;
7791 if (hba->wlun_dev_clr_ua) {
7792 ret = ufshcd_send_request_sense(hba, sdp);
7793 if (ret)
7794 goto out;
7795 /* Unit attention condition is cleared now */
7796 hba->wlun_dev_clr_ua = false;
7797 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307798
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007799 cmd[4] = pwr_mode << 4;
7800
7801 /*
7802 * Current function would be generally called from the power management
7803 * callbacks hence set the REQ_PM flag so that it doesn't resume the
7804 * already suspended childs.
7805 */
7806 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
7807 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
7808 if (ret) {
7809 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02007810 "START_STOP failed for power mode: %d, result %x\n",
7811 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01007812 if (driver_byte(ret) & DRIVER_SENSE)
7813 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007814 }
7815
7816 if (!ret)
7817 hba->curr_dev_pwr_mode = pwr_mode;
7818out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007819 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007820 hba->host->eh_noresume = 0;
7821 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307822}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307823
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007824static int ufshcd_link_state_transition(struct ufs_hba *hba,
7825 enum uic_link_state req_link_state,
7826 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307827{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007828 int ret = 0;
7829
7830 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307831 return 0;
7832
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007833 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7834 ret = ufshcd_uic_hibern8_enter(hba);
7835 if (!ret)
7836 ufshcd_set_link_hibern8(hba);
7837 else
7838 goto out;
7839 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307840 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007841 * If autobkops is enabled, link can't be turned off because
7842 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307843 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007844 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
7845 (!check_for_bkops || (check_for_bkops &&
7846 !hba->auto_bkops_enabled))) {
7847 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007848 * Let's make sure that link is in low power mode, we are doing
7849 * this currently by putting the link in Hibern8. Otherway to
7850 * put the link in low power mode is to send the DME end point
7851 * to device and then send the DME reset command to local
7852 * unipro. But putting the link in hibern8 is much faster.
7853 */
7854 ret = ufshcd_uic_hibern8_enter(hba);
7855 if (ret)
7856 goto out;
7857 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007858 * Change controller state to "reset state" which
7859 * should also put the link in off/reset state
7860 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007861 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007862 /*
7863 * TODO: Check if we need any delay to make sure that
7864 * controller is reset
7865 */
7866 ufshcd_set_link_off(hba);
7867 }
7868
7869out:
7870 return ret;
7871}
7872
7873static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7874{
7875 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007876 * It seems some UFS devices may keep drawing more than sleep current
7877 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7878 * To avoid this situation, add 2ms delay before putting these UFS
7879 * rails in LPM mode.
7880 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007881 if (!ufshcd_is_link_active(hba))
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007882 usleep_range(2000, 2100);
7883
7884 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007885 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7886 * power.
7887 *
7888 * If UFS device and link is in OFF state, all power supplies (VCC,
7889 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7890 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7891 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7892 *
7893 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7894 * in low power state which would save some power.
7895 */
7896 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7897 !hba->dev_info.is_lu_power_on_wp) {
7898 ufshcd_setup_vreg(hba, false);
7899 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7900 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7901 if (!ufshcd_is_link_active(hba)) {
7902 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7903 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7904 }
7905 }
7906}
7907
7908static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7909{
7910 int ret = 0;
7911
7912 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7913 !hba->dev_info.is_lu_power_on_wp) {
7914 ret = ufshcd_setup_vreg(hba, true);
7915 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007916 if (!ret && !ufshcd_is_link_active(hba)) {
7917 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7918 if (ret)
7919 goto vcc_disable;
7920 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7921 if (ret)
7922 goto vccq_lpm;
7923 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007924 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007925 }
7926 goto out;
7927
7928vccq_lpm:
7929 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7930vcc_disable:
7931 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7932out:
7933 return ret;
7934}
7935
7936static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7937{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007938 if (ufshcd_is_link_off(hba) ||
7939 (ufshcd_is_link_hibern8(hba)
7940 && ufshcd_is_power_collapse_during_hibern8_allowed(hba)))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007941 ufshcd_setup_hba_vreg(hba, false);
7942}
7943
7944static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7945{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007946 if (ufshcd_is_link_off(hba) ||
7947 (ufshcd_is_link_hibern8(hba)
7948 && ufshcd_is_power_collapse_during_hibern8_allowed(hba)))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007949 ufshcd_setup_hba_vreg(hba, true);
7950}
7951
7952/**
7953 * ufshcd_suspend - helper function for suspend operations
7954 * @hba: per adapter instance
7955 * @pm_op: desired low power operation type
7956 *
7957 * This function will try to put the UFS device and link into low power
7958 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7959 * (System PM level).
7960 *
7961 * If this function is called during shutdown, it will make sure that
7962 * both UFS device and UFS link is powered off.
7963 *
7964 * NOTE: UFS device & link must be active before we enter in this function.
7965 *
7966 * Returns 0 for success and non-zero for failure
7967 */
7968static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7969{
7970 int ret = 0;
7971 enum ufs_pm_level pm_lvl;
7972 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7973 enum uic_link_state req_link_state;
7974
7975 hba->pm_op_in_progress = 1;
7976 if (!ufshcd_is_shutdown_pm(pm_op)) {
7977 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7978 hba->rpm_lvl : hba->spm_lvl;
7979 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7980 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7981 } else {
7982 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7983 req_link_state = UIC_LINK_OFF_STATE;
7984 }
7985
7986 /*
7987 * If we can't transition into any of the low power modes
7988 * just gate the clocks.
7989 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007990 WARN_ON(hba->hibern8_on_idle.is_enabled &&
7991 hba->hibern8_on_idle.active_reqs);
7992 ufshcd_hold_all(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007993 hba->clk_gating.is_suspended = true;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07007994 hba->hibern8_on_idle.is_suspended = true;
7995
7996 if (hba->clk_scaling.is_allowed) {
7997 cancel_work_sync(&hba->clk_scaling.suspend_work);
7998 cancel_work_sync(&hba->clk_scaling.resume_work);
7999 ufshcd_suspend_clkscaling(hba);
8000 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008001
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008002 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
8003 req_link_state == UIC_LINK_ACTIVE_STATE) {
8004 goto disable_clks;
8005 }
8006
8007 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
8008 (req_link_state == hba->uic_link_state))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008009 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008010
8011 /* UFS device & link must be active before we enter in this function */
8012 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
8013 ret = -EINVAL;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008014 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008015 }
8016
8017 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03008018 if (ufshcd_can_autobkops_during_suspend(hba)) {
8019 /*
8020 * The device is idle with no requests in the queue,
8021 * allow background operations if bkops status shows
8022 * that performance might be impacted.
8023 */
8024 ret = ufshcd_urgent_bkops(hba);
8025 if (ret)
8026 goto enable_gating;
8027 } else {
8028 /* make sure that auto bkops is disabled */
8029 ufshcd_disable_auto_bkops(hba);
8030 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008031 }
8032
8033 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
8034 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
8035 !ufshcd_is_runtime_pm(pm_op))) {
8036 /* ensure that bkops is disabled */
8037 ufshcd_disable_auto_bkops(hba);
8038 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
8039 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008040 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008041 }
8042
8043 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
8044 if (ret)
8045 goto set_dev_active;
8046
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008047 if (ufshcd_is_link_hibern8(hba) &&
8048 ufshcd_is_hibern8_on_idle_allowed(hba))
8049 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
8050
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008051 ufshcd_vreg_set_lpm(hba);
8052
8053disable_clks:
8054 /*
8055 * Call vendor specific suspend callback. As these callbacks may access
8056 * vendor specific host controller register space call them before the
8057 * host clocks are ON.
8058 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008059 ret = ufshcd_vops_suspend(hba, pm_op);
8060 if (ret)
8061 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008062
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008063 if (!ufshcd_is_link_active(hba))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008064 ret = ufshcd_disable_clocks(hba, false);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008065 else
8066 /* If link is active, device ref_clk can't be switched off */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008067 ret = ufshcd_disable_clocks_skip_ref_clk(hba, false);
8068 if (ret)
8069 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008070
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008071 if (ufshcd_is_clkgating_allowed(hba)) {
8072 hba->clk_gating.state = CLKS_OFF;
8073 trace_ufshcd_clk_gating(dev_name(hba->dev),
8074 hba->clk_gating.state);
8075 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008076 /*
8077 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008078 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008079 */
8080 ufshcd_disable_irq(hba);
8081 /* Put the host controller in low power mode if possible */
8082 ufshcd_hba_vreg_set_lpm(hba);
8083 goto out;
8084
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008085set_link_active:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008086 if (hba->clk_scaling.is_allowed)
8087 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008088 ufshcd_vreg_set_hpm(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008089 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008090 ufshcd_set_link_active(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008091 } else if (ufshcd_is_link_off(hba)) {
8092 ufshcd_update_error_stats(hba, UFS_ERR_VOPS_SUSPEND);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008093 ufshcd_host_reset_and_restore(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008094 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008095set_dev_active:
8096 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
8097 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008098enable_gating:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008099 if (hba->clk_scaling.is_allowed)
8100 ufshcd_resume_clkscaling(hba);
8101 hba->hibern8_on_idle.is_suspended = false;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008102 hba->clk_gating.is_suspended = false;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008103 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008104out:
8105 hba->pm_op_in_progress = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008106
8107 if (ret)
8108 ufshcd_update_error_stats(hba, UFS_ERR_SUSPEND);
8109
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008110 return ret;
8111}
8112
8113/**
8114 * ufshcd_resume - helper function for resume operations
8115 * @hba: per adapter instance
8116 * @pm_op: runtime PM or system PM
8117 *
8118 * This function basically brings the UFS device, UniPro link and controller
8119 * to active state.
8120 *
8121 * Returns 0 for success and non-zero for failure
8122 */
8123static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8124{
8125 int ret;
8126 enum uic_link_state old_link_state;
8127
8128 hba->pm_op_in_progress = 1;
8129 old_link_state = hba->uic_link_state;
8130
8131 ufshcd_hba_vreg_set_hpm(hba);
8132 /* Make sure clocks are enabled before accessing controller */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008133 ret = ufshcd_enable_clocks(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008134 if (ret)
8135 goto out;
8136
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008137 /* enable the host irq as host controller would be active soon */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008138 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008139
8140 ret = ufshcd_vreg_set_hpm(hba);
8141 if (ret)
8142 goto disable_irq_and_vops_clks;
8143
8144 /*
8145 * Call vendor specific resume callback. As these callbacks may access
8146 * vendor specific host controller register space call them when the
8147 * host clocks are ON.
8148 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008149 ret = ufshcd_vops_resume(hba, pm_op);
8150 if (ret)
8151 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008152
8153 if (ufshcd_is_link_hibern8(hba)) {
8154 ret = ufshcd_uic_hibern8_exit(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008155 if (!ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008156 ufshcd_set_link_active(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008157 if (ufshcd_is_hibern8_on_idle_allowed(hba))
8158 hba->hibern8_on_idle.state = HIBERN8_EXITED;
8159 } else {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008160 goto vendor_suspend;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008161 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008162 } else if (ufshcd_is_link_off(hba)) {
8163 ret = ufshcd_host_reset_and_restore(hba);
8164 /*
8165 * ufshcd_host_reset_and_restore() should have already
8166 * set the link state as active
8167 */
8168 if (ret || !ufshcd_is_link_active(hba))
8169 goto vendor_suspend;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008170 /* mark link state as hibern8 exited */
8171 if (ufshcd_is_hibern8_on_idle_allowed(hba))
8172 hba->hibern8_on_idle.state = HIBERN8_EXITED;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008173 }
8174
8175 if (!ufshcd_is_ufs_dev_active(hba)) {
8176 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8177 if (ret)
8178 goto set_old_link_state;
8179 }
8180
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008181 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8182 ufshcd_enable_auto_bkops(hba);
8183 else
8184 /*
8185 * If BKOPs operations are urgently needed at this moment then
8186 * keep auto-bkops enabled or else disable it.
8187 */
8188 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008189
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008190 hba->clk_gating.is_suspended = false;
8191 hba->hibern8_on_idle.is_suspended = false;
8192
8193 if (hba->clk_scaling.is_allowed)
8194 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008195
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008196 /* Schedule clock gating in case of no access to UFS device yet */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008197 ufshcd_release_all(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008198 goto out;
8199
8200set_old_link_state:
8201 ufshcd_link_state_transition(hba, old_link_state, 0);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008202 if (ufshcd_is_link_hibern8(hba) &&
8203 ufshcd_is_hibern8_on_idle_allowed(hba))
8204 hba->hibern8_on_idle.state = HIBERN8_ENTERED;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008205vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008206 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008207disable_vreg:
8208 ufshcd_vreg_set_lpm(hba);
8209disable_irq_and_vops_clks:
8210 ufshcd_disable_irq(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008211 if (hba->clk_scaling.is_allowed)
8212 ufshcd_suspend_clkscaling(hba);
8213 ufshcd_disable_clocks(hba, false);
8214 if (ufshcd_is_clkgating_allowed(hba))
8215 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008216out:
8217 hba->pm_op_in_progress = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008218
8219 if (ret)
8220 ufshcd_update_error_stats(hba, UFS_ERR_RESUME);
8221
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008222 return ret;
8223}
8224
8225/**
8226 * ufshcd_system_suspend - system suspend routine
8227 * @hba: per adapter instance
8228 * @pm_op: runtime PM or system PM
8229 *
8230 * Check the description of ufshcd_suspend() function for more details.
8231 *
8232 * Returns 0 for success and non-zero for failure
8233 */
8234int ufshcd_system_suspend(struct ufs_hba *hba)
8235{
8236 int ret = 0;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008237 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008238
8239 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008240 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008241
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008242 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8243 hba->curr_dev_pwr_mode) &&
8244 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8245 hba->uic_link_state))
8246 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008247
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008248 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008249 /*
8250 * UFS device and/or UFS link low power states during runtime
8251 * suspend seems to be different than what is expected during
8252 * system suspend. Hence runtime resume the devic & link and
8253 * let the system suspend low power states to take effect.
8254 * TODO: If resume takes longer time, we might have optimize
8255 * it in future by not resuming everything if possible.
8256 */
8257 ret = ufshcd_runtime_resume(hba);
8258 if (ret)
8259 goto out;
8260 }
8261
8262 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8263out:
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008264 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8265 ktime_to_us(ktime_sub(ktime_get(), start)),
8266 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008267 if (!ret)
8268 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008269 return ret;
8270}
8271EXPORT_SYMBOL(ufshcd_system_suspend);
8272
8273/**
8274 * ufshcd_system_resume - system resume routine
8275 * @hba: per adapter instance
8276 *
8277 * Returns 0 for success and non-zero for failure
8278 */
8279
8280int ufshcd_system_resume(struct ufs_hba *hba)
8281{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008282 int ret = 0;
8283 ktime_t start = ktime_get();
8284
8285 if (!hba)
8286 return -EINVAL;
8287
8288 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008289 /*
8290 * Let the runtime resume take care of resuming
8291 * if runtime suspended.
8292 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008293 goto out;
8294 else
8295 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8296out:
8297 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8298 ktime_to_us(ktime_sub(ktime_get(), start)),
8299 hba->curr_dev_pwr_mode, hba->uic_link_state);
8300 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008301}
8302EXPORT_SYMBOL(ufshcd_system_resume);
8303
8304/**
8305 * ufshcd_runtime_suspend - runtime suspend routine
8306 * @hba: per adapter instance
8307 *
8308 * Check the description of ufshcd_suspend() function for more details.
8309 *
8310 * Returns 0 for success and non-zero for failure
8311 */
8312int ufshcd_runtime_suspend(struct ufs_hba *hba)
8313{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008314 int ret = 0;
8315 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008316
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008317 if (!hba)
8318 return -EINVAL;
8319
8320 if (!hba->is_powered)
8321 goto out;
8322 else
8323 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8324out:
8325 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8326 ktime_to_us(ktime_sub(ktime_get(), start)),
8327 hba->curr_dev_pwr_mode,
8328 hba->uic_link_state);
8329 return ret;
8330
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308331}
8332EXPORT_SYMBOL(ufshcd_runtime_suspend);
8333
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008334/**
8335 * ufshcd_runtime_resume - runtime resume routine
8336 * @hba: per adapter instance
8337 *
8338 * This function basically brings the UFS device, UniPro link and controller
8339 * to active state. Following operations are done in this function:
8340 *
8341 * 1. Turn on all the controller related clocks
8342 * 2. Bring the UniPro link out of Hibernate state
8343 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8344 * to active state.
8345 * 4. If auto-bkops is enabled on the device, disable it.
8346 *
8347 * So following would be the possible power state after this function return
8348 * successfully:
8349 * S1: UFS device in Active state with VCC rail ON
8350 * UniPro link in Active state
8351 * All the UFS/UniPro controller clocks are ON
8352 *
8353 * Returns 0 for success and non-zero for failure
8354 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308355int ufshcd_runtime_resume(struct ufs_hba *hba)
8356{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008357 int ret = 0;
8358 ktime_t start = ktime_get();
8359
8360 if (!hba)
8361 return -EINVAL;
8362
8363 if (!hba->is_powered)
8364 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008365 else
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008366 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8367out:
8368 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8369 ktime_to_us(ktime_sub(ktime_get(), start)),
8370 hba->curr_dev_pwr_mode,
8371 hba->uic_link_state);
8372 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308373}
8374EXPORT_SYMBOL(ufshcd_runtime_resume);
8375
8376int ufshcd_runtime_idle(struct ufs_hba *hba)
8377{
8378 return 0;
8379}
8380EXPORT_SYMBOL(ufshcd_runtime_idle);
8381
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008382static inline ssize_t ufshcd_pm_lvl_store(struct device *dev,
8383 struct device_attribute *attr,
8384 const char *buf, size_t count,
8385 bool rpm)
8386{
8387 struct ufs_hba *hba = dev_get_drvdata(dev);
8388 unsigned long flags, value;
8389
8390 if (kstrtoul(buf, 0, &value))
8391 return -EINVAL;
8392
8393 if (value >= UFS_PM_LVL_MAX)
8394 return -EINVAL;
8395
8396 spin_lock_irqsave(hba->host->host_lock, flags);
8397 if (rpm)
8398 hba->rpm_lvl = value;
8399 else
8400 hba->spm_lvl = value;
8401 ufshcd_apply_pm_quirks(hba);
8402 spin_unlock_irqrestore(hba->host->host_lock, flags);
8403 return count;
8404}
8405
8406static ssize_t ufshcd_rpm_lvl_show(struct device *dev,
8407 struct device_attribute *attr, char *buf)
8408{
8409 struct ufs_hba *hba = dev_get_drvdata(dev);
8410 int curr_len;
8411 u8 lvl;
8412
8413 curr_len = snprintf(buf, PAGE_SIZE,
8414 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
8415 hba->rpm_lvl,
8416 ufschd_ufs_dev_pwr_mode_to_string(
8417 ufs_pm_lvl_states[hba->rpm_lvl].dev_state),
8418 ufschd_uic_link_state_to_string(
8419 ufs_pm_lvl_states[hba->rpm_lvl].link_state));
8420
8421 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8422 "\nAll available Runtime PM levels info:\n");
8423 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
8424 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8425 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
8426 lvl,
8427 ufschd_ufs_dev_pwr_mode_to_string(
8428 ufs_pm_lvl_states[lvl].dev_state),
8429 ufschd_uic_link_state_to_string(
8430 ufs_pm_lvl_states[lvl].link_state));
8431
8432 return curr_len;
8433}
8434
8435static ssize_t ufshcd_rpm_lvl_store(struct device *dev,
8436 struct device_attribute *attr, const char *buf, size_t count)
8437{
8438 return ufshcd_pm_lvl_store(dev, attr, buf, count, true);
8439}
8440
8441static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba)
8442{
8443 hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show;
8444 hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store;
8445 sysfs_attr_init(&hba->rpm_lvl_attr.attr);
8446 hba->rpm_lvl_attr.attr.name = "rpm_lvl";
8447 hba->rpm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR;
8448 if (device_create_file(hba->dev, &hba->rpm_lvl_attr))
8449 dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n");
8450}
8451
8452static ssize_t ufshcd_spm_lvl_show(struct device *dev,
8453 struct device_attribute *attr, char *buf)
8454{
8455 struct ufs_hba *hba = dev_get_drvdata(dev);
8456 int curr_len;
8457 u8 lvl;
8458
8459 curr_len = snprintf(buf, PAGE_SIZE,
8460 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
8461 hba->spm_lvl,
8462 ufschd_ufs_dev_pwr_mode_to_string(
8463 ufs_pm_lvl_states[hba->spm_lvl].dev_state),
8464 ufschd_uic_link_state_to_string(
8465 ufs_pm_lvl_states[hba->spm_lvl].link_state));
8466
8467 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8468 "\nAll available System PM levels info:\n");
8469 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
8470 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
8471 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
8472 lvl,
8473 ufschd_ufs_dev_pwr_mode_to_string(
8474 ufs_pm_lvl_states[lvl].dev_state),
8475 ufschd_uic_link_state_to_string(
8476 ufs_pm_lvl_states[lvl].link_state));
8477
8478 return curr_len;
8479}
8480
8481static ssize_t ufshcd_spm_lvl_store(struct device *dev,
8482 struct device_attribute *attr, const char *buf, size_t count)
8483{
8484 return ufshcd_pm_lvl_store(dev, attr, buf, count, false);
8485}
8486
8487static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba)
8488{
8489 hba->spm_lvl_attr.show = ufshcd_spm_lvl_show;
8490 hba->spm_lvl_attr.store = ufshcd_spm_lvl_store;
8491 sysfs_attr_init(&hba->spm_lvl_attr.attr);
8492 hba->spm_lvl_attr.attr.name = "spm_lvl";
8493 hba->spm_lvl_attr.attr.mode = S_IRUGO | S_IWUSR;
8494 if (device_create_file(hba->dev, &hba->spm_lvl_attr))
8495 dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n");
8496}
8497
8498static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba)
8499{
8500 ufshcd_add_rpm_lvl_sysfs_nodes(hba);
8501 ufshcd_add_spm_lvl_sysfs_nodes(hba);
8502}
8503
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308504/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008505 * ufshcd_shutdown - shutdown routine
8506 * @hba: per adapter instance
8507 *
8508 * This function would power off both UFS device and UFS link.
8509 *
8510 * Returns 0 always to allow force shutdown even in case of errors.
8511 */
8512int ufshcd_shutdown(struct ufs_hba *hba)
8513{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008514 /*
8515 * TODO: This function should send the power down notification to
8516 * UFS device and then power off the UFS link. But we need to be sure
8517 * that there will not be any new UFS requests issued after this.
8518 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008519 return 0;
8520}
8521EXPORT_SYMBOL(ufshcd_shutdown);
8522
8523/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308524 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308525 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308526 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308527 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308528void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308529{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05308530 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308531 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308532 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02008533 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308534
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008535 ufshcd_exit_clk_gating(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008536 ufshcd_exit_hibern8_on_idle(hba);
8537 if (ufshcd_is_clkscaling_supported(hba)) {
8538 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008539 devfreq_remove_device(hba->devfreq);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008540 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008541 ufshcd_hba_exit(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008542 ufsdbg_remove_debugfs(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308543}
8544EXPORT_SYMBOL_GPL(ufshcd_remove);
8545
8546/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02008547 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
8548 * @hba: pointer to Host Bus Adapter (HBA)
8549 */
8550void ufshcd_dealloc_host(struct ufs_hba *hba)
8551{
8552 scsi_host_put(hba->host);
8553}
8554EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
8555
8556/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008557 * ufshcd_set_dma_mask - Set dma mask based on the controller
8558 * addressing capability
8559 * @hba: per adapter instance
8560 *
8561 * Returns 0 for success, non-zero for failure
8562 */
8563static int ufshcd_set_dma_mask(struct ufs_hba *hba)
8564{
8565 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
8566 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
8567 return 0;
8568 }
8569 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
8570}
8571
8572/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008573 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308574 * @dev: pointer to device handle
8575 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308576 * Returns 0 on success, non-zero value on failure
8577 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008578int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308579{
8580 struct Scsi_Host *host;
8581 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008582 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308583
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308584 if (!dev) {
8585 dev_err(dev,
8586 "Invalid memory reference for dev is NULL\n");
8587 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308588 goto out_error;
8589 }
8590
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308591 host = scsi_host_alloc(&ufshcd_driver_template,
8592 sizeof(struct ufs_hba));
8593 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308594 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308595 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308596 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308597 }
8598 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308599 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308600 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008601 *hba_handle = hba;
8602
8603out_error:
8604 return err;
8605}
8606EXPORT_SYMBOL(ufshcd_alloc_host);
8607
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008608/**
8609 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
8610 * @hba: per adapter instance
8611 * @scale_up: True if scaling up and false if scaling down
8612 *
8613 * Returns true if scaling is required, false otherwise.
8614 */
8615static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
8616 bool scale_up)
Sahitya Tummala856b3482014-09-25 15:32:34 +03008617{
Sahitya Tummala856b3482014-09-25 15:32:34 +03008618 struct ufs_clk_info *clki;
8619 struct list_head *head = &hba->clk_list_head;
8620
8621 if (!head || list_empty(head))
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008622 return false;
Yaniv Gardif06fcc72015-10-28 13:15:51 +02008623
Sahitya Tummala856b3482014-09-25 15:32:34 +03008624 list_for_each_entry(clki, head, list) {
8625 if (!IS_ERR_OR_NULL(clki->clk)) {
8626 if (scale_up && clki->max_freq) {
8627 if (clki->curr_freq == clki->max_freq)
8628 continue;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008629 return true;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008630 } else if (!scale_up && clki->min_freq) {
8631 if (clki->curr_freq == clki->min_freq)
8632 continue;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008633 return true;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008634 }
8635 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03008636 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02008637
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008638 return false;
8639}
Yaniv Gardif06fcc72015-10-28 13:15:51 +02008640
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008641/**
8642 * ufshcd_scale_gear - scale up/down UFS gear
8643 * @hba: per adapter instance
8644 * @scale_up: True for scaling up gear and false for scaling down
8645 *
8646 * Returns 0 for success,
8647 * Returns -EBUSY if scaling can't happen at this time
8648 * Returns non-zero for any other errors
8649 */
8650static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
8651{
8652 int ret = 0;
8653 struct ufs_pa_layer_attr new_pwr_info;
8654 u32 scale_down_gear = ufshcd_vops_get_scale_down_gear(hba);
8655
8656 BUG_ON(!hba->clk_scaling.saved_pwr_info.is_valid);
8657
8658 if (scale_up) {
8659 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
8660 sizeof(struct ufs_pa_layer_attr));
8661 } else {
8662 memcpy(&new_pwr_info, &hba->pwr_info,
8663 sizeof(struct ufs_pa_layer_attr));
8664
8665 if (hba->pwr_info.gear_tx > scale_down_gear
8666 || hba->pwr_info.gear_rx > scale_down_gear) {
8667 /* save the current power mode */
8668 memcpy(&hba->clk_scaling.saved_pwr_info.info,
8669 &hba->pwr_info,
8670 sizeof(struct ufs_pa_layer_attr));
8671
8672 /* scale down gear */
8673 new_pwr_info.gear_tx = scale_down_gear;
8674 new_pwr_info.gear_rx = scale_down_gear;
8675 if (!(hba->dev_quirks & UFS_DEVICE_NO_FASTAUTO)) {
8676 new_pwr_info.pwr_tx = FASTAUTO_MODE;
8677 new_pwr_info.pwr_rx = FASTAUTO_MODE;
8678 }
8679 }
8680 }
8681
8682 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
8683
8684 if (ret)
8685 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d), scale_up = %d",
8686 __func__, ret,
8687 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
8688 new_pwr_info.gear_tx, new_pwr_info.gear_rx,
8689 scale_up);
8690
Sahitya Tummala856b3482014-09-25 15:32:34 +03008691 return ret;
8692}
8693
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008694static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
8695{
8696 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
8697 int ret = 0;
8698 /*
8699 * make sure that there are no outstanding requests when
8700 * clock scaling is in progress
8701 */
8702 ufshcd_scsi_block_requests(hba);
8703 down_write(&hba->clk_scaling_lock);
8704 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
8705 ret = -EBUSY;
8706 up_write(&hba->clk_scaling_lock);
8707 ufshcd_scsi_unblock_requests(hba);
8708 }
8709
8710 return ret;
8711}
8712
8713static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
8714{
8715 up_write(&hba->clk_scaling_lock);
8716 ufshcd_scsi_unblock_requests(hba);
8717}
8718
8719/**
8720 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
8721 * @hba: per adapter instance
8722 * @scale_up: True for scaling up and false for scalin down
8723 *
8724 * Returns 0 for success,
8725 * Returns -EBUSY if scaling can't happen at this time
8726 * Returns non-zero for any other errors
8727 */
8728static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
8729{
8730 int ret = 0;
8731
8732 /* let's not get into low power until clock scaling is completed */
8733 ufshcd_hold_all(hba);
8734
8735 ret = ufshcd_clock_scaling_prepare(hba);
8736 if (ret)
8737 goto out;
8738
8739 /* scale down the gear before scaling down clocks */
8740 if (!scale_up) {
8741 ret = ufshcd_scale_gear(hba, false);
8742 if (ret)
8743 goto clk_scaling_unprepare;
8744 }
8745
8746 ret = ufshcd_scale_clks(hba, scale_up);
8747 if (ret)
8748 goto scale_up_gear;
8749
8750 /* scale up the gear after scaling up clocks */
8751 if (scale_up) {
8752 ret = ufshcd_scale_gear(hba, true);
8753 if (ret) {
8754 ufshcd_scale_clks(hba, false);
8755 goto clk_scaling_unprepare;
8756 }
8757 }
8758
8759 if (!ret) {
8760 hba->clk_scaling.is_scaled_up = scale_up;
8761 if (scale_up)
8762 hba->clk_gating.delay_ms =
8763 hba->clk_gating.delay_ms_perf;
8764 else
8765 hba->clk_gating.delay_ms =
8766 hba->clk_gating.delay_ms_pwr_save;
8767 }
8768
8769 goto clk_scaling_unprepare;
8770
8771scale_up_gear:
8772 if (!scale_up)
8773 ufshcd_scale_gear(hba, true);
8774clk_scaling_unprepare:
8775 ufshcd_clock_scaling_unprepare(hba);
8776out:
8777 ufshcd_release_all(hba);
8778 return ret;
8779}
8780
8781static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
8782{
8783 unsigned long flags;
8784
8785 devfreq_suspend_device(hba->devfreq);
8786 spin_lock_irqsave(hba->host->host_lock, flags);
8787 hba->clk_scaling.window_start_t = 0;
8788 spin_unlock_irqrestore(hba->host->host_lock, flags);
8789}
8790
8791static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
8792{
8793 unsigned long flags;
8794 bool suspend = false;
8795
8796 if (!ufshcd_is_clkscaling_supported(hba))
8797 return;
8798
8799 spin_lock_irqsave(hba->host->host_lock, flags);
8800 if (!hba->clk_scaling.is_suspended) {
8801 suspend = true;
8802 hba->clk_scaling.is_suspended = true;
8803 }
8804 spin_unlock_irqrestore(hba->host->host_lock, flags);
8805
8806 if (suspend)
8807 __ufshcd_suspend_clkscaling(hba);
8808}
8809
8810static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
8811{
8812 unsigned long flags;
8813 bool resume = false;
8814
8815 if (!ufshcd_is_clkscaling_supported(hba))
8816 return;
8817
8818 spin_lock_irqsave(hba->host->host_lock, flags);
8819 if (hba->clk_scaling.is_suspended) {
8820 resume = true;
8821 hba->clk_scaling.is_suspended = false;
8822 }
8823 spin_unlock_irqrestore(hba->host->host_lock, flags);
8824
8825 if (resume)
8826 devfreq_resume_device(hba->devfreq);
8827}
8828
8829static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
8830 struct device_attribute *attr, char *buf)
8831{
8832 struct ufs_hba *hba = dev_get_drvdata(dev);
8833
8834 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
8835}
8836
8837static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
8838 struct device_attribute *attr, const char *buf, size_t count)
8839{
8840 struct ufs_hba *hba = dev_get_drvdata(dev);
8841 u32 value;
8842 int err;
8843
8844 if (kstrtou32(buf, 0, &value))
8845 return -EINVAL;
8846
8847 value = !!value;
8848 if (value == hba->clk_scaling.is_allowed)
8849 goto out;
8850
8851 pm_runtime_get_sync(hba->dev);
8852 ufshcd_hold(hba, false);
8853
8854 cancel_work_sync(&hba->clk_scaling.suspend_work);
8855 cancel_work_sync(&hba->clk_scaling.resume_work);
8856
8857 hba->clk_scaling.is_allowed = value;
8858
8859 if (value) {
8860 ufshcd_resume_clkscaling(hba);
8861 } else {
8862 ufshcd_suspend_clkscaling(hba);
8863 err = ufshcd_devfreq_scale(hba, true);
8864 if (err)
8865 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
8866 __func__, err);
8867 }
8868
8869 ufshcd_release(hba, false);
8870 pm_runtime_put_sync(hba->dev);
8871out:
8872 return count;
8873}
8874
8875static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
8876{
8877 struct ufs_hba *hba = container_of(work, struct ufs_hba,
8878 clk_scaling.suspend_work);
8879 unsigned long irq_flags;
8880
8881 spin_lock_irqsave(hba->host->host_lock, irq_flags);
8882 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
8883 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8884 return;
8885 }
8886 hba->clk_scaling.is_suspended = true;
8887 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8888
8889 __ufshcd_suspend_clkscaling(hba);
8890}
8891
8892static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
8893{
8894 struct ufs_hba *hba = container_of(work, struct ufs_hba,
8895 clk_scaling.resume_work);
8896 unsigned long irq_flags;
8897
8898 spin_lock_irqsave(hba->host->host_lock, irq_flags);
8899 if (!hba->clk_scaling.is_suspended) {
8900 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8901 return;
8902 }
8903 hba->clk_scaling.is_suspended = false;
8904 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8905
8906 devfreq_resume_device(hba->devfreq);
8907}
8908
Sahitya Tummala856b3482014-09-25 15:32:34 +03008909static int ufshcd_devfreq_target(struct device *dev,
8910 unsigned long *freq, u32 flags)
8911{
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008912 int ret = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008913 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008914 unsigned long irq_flags;
8915 ktime_t start;
8916 bool scale_up, sched_clk_scaling_suspend_work = false;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008917
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008918 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03008919 return -EINVAL;
8920
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008921 if ((*freq > 0) && (*freq < UINT_MAX)) {
8922 dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq);
8923 return -EINVAL;
8924 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03008925
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008926 spin_lock_irqsave(hba->host->host_lock, irq_flags);
8927 if (ufshcd_eh_in_progress(hba)) {
8928 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8929 return 0;
8930 }
8931
8932 if (!hba->clk_scaling.active_reqs)
8933 sched_clk_scaling_suspend_work = true;
8934
8935 scale_up = (*freq == UINT_MAX) ? true : false;
8936 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
8937 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8938 ret = 0;
8939 goto out; /* no state change required */
8940 }
8941 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
8942
8943 start = ktime_get();
8944 ret = ufshcd_devfreq_scale(hba, scale_up);
8945 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
8946 (scale_up ? "up" : "down"),
8947 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
8948
8949out:
8950 if (sched_clk_scaling_suspend_work)
8951 queue_work(hba->clk_scaling.workq,
8952 &hba->clk_scaling.suspend_work);
8953
8954 return ret;
Sahitya Tummala856b3482014-09-25 15:32:34 +03008955}
8956
8957static int ufshcd_devfreq_get_dev_status(struct device *dev,
8958 struct devfreq_dev_status *stat)
8959{
8960 struct ufs_hba *hba = dev_get_drvdata(dev);
8961 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
8962 unsigned long flags;
8963
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008964 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03008965 return -EINVAL;
8966
8967 memset(stat, 0, sizeof(*stat));
8968
8969 spin_lock_irqsave(hba->host->host_lock, flags);
8970 if (!scaling->window_start_t)
8971 goto start_window;
8972
8973 if (scaling->is_busy_started)
8974 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
8975 scaling->busy_start_t));
8976
8977 stat->total_time = jiffies_to_usecs((long)jiffies -
8978 (long)scaling->window_start_t);
8979 stat->busy_time = scaling->tot_busy_t;
8980start_window:
8981 scaling->window_start_t = jiffies;
8982 scaling->tot_busy_t = 0;
8983
8984 if (hba->outstanding_reqs) {
8985 scaling->busy_start_t = ktime_get();
8986 scaling->is_busy_started = true;
8987 } else {
8988 scaling->busy_start_t = ktime_set(0, 0);
8989 scaling->is_busy_started = false;
8990 }
8991 spin_unlock_irqrestore(hba->host->host_lock, flags);
8992 return 0;
8993}
8994
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07008995#if IS_ENABLED(CONFIG_DEVFREQ_GOV_SIMPLE_ONDEMAND)
8996static struct devfreq_simple_ondemand_data ufshcd_ondemand_data = {
8997 .upthreshold = 35,
8998 .downdifferential = 30,
8999 .simple_scaling = 1,
9000};
9001
9002static void *gov_data = &ufshcd_ondemand_data;
9003#else
9004static void *gov_data;
9005#endif
9006
Sahitya Tummala856b3482014-09-25 15:32:34 +03009007static struct devfreq_dev_profile ufs_devfreq_profile = {
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009008 .polling_ms = 40,
Sahitya Tummala856b3482014-09-25 15:32:34 +03009009 .target = ufshcd_devfreq_target,
9010 .get_dev_status = ufshcd_devfreq_get_dev_status,
9011};
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009012static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
9013{
9014 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
9015 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
9016 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
9017 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
9018 hba->clk_scaling.enable_attr.attr.mode = S_IRUGO | S_IWUSR;
9019 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
9020 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
9021}
Sahitya Tummala856b3482014-09-25 15:32:34 +03009022
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009023static void ufshcd_init_lanes_per_dir(struct ufs_hba *hba)
9024{
9025 struct device *dev = hba->dev;
9026 int ret;
9027
9028 ret = of_property_read_u32(dev->of_node, "lanes-per-direction",
9029 &hba->lanes_per_direction);
9030 if (ret) {
9031 dev_dbg(hba->dev,
9032 "%s: failed to read lanes-per-direction, ret=%d\n",
9033 __func__, ret);
9034 hba->lanes_per_direction = UFSHCD_DEFAULT_LANES_PER_DIRECTION;
9035 }
9036}
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009037/**
9038 * ufshcd_init - Driver initialization routine
9039 * @hba: per-adapter instance
9040 * @mmio_base: base register address
9041 * @irq: Interrupt line of device
9042 * Returns 0 on success, non-zero value on failure
9043 */
9044int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
9045{
9046 int err;
9047 struct Scsi_Host *host = hba->host;
9048 struct device *dev = hba->dev;
9049
9050 if (!mmio_base) {
9051 dev_err(hba->dev,
9052 "Invalid memory reference for mmio_base is NULL\n");
9053 err = -ENODEV;
9054 goto out_error;
9055 }
9056
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309057 hba->mmio_base = mmio_base;
9058 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309059
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009060 ufshcd_init_lanes_per_dir(hba);
9061
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009062 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03009063 if (err)
9064 goto out_error;
9065
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309066 /* Read capabilities registers */
9067 ufshcd_hba_capabilities(hba);
9068
9069 /* Get UFS version supported by the controller */
9070 hba->ufs_version = ufshcd_get_ufs_version(hba);
9071
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009072 /* print error message if ufs_version is not valid */
9073 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
9074 (hba->ufs_version != UFSHCI_VERSION_11) &&
9075 (hba->ufs_version != UFSHCI_VERSION_20) &&
9076 (hba->ufs_version != UFSHCI_VERSION_21))
9077 dev_err(hba->dev, "invalid UFS version 0x%x\n",
9078 hba->ufs_version);
9079
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05309080 /* Get Interrupt bit mask per version */
9081 hba->intr_mask = ufshcd_get_intr_mask(hba);
9082
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009083 /* Enable debug prints */
9084 hba->ufshcd_dbg_print = DEFAULT_UFSHCD_DBG_PRINT_EN;
9085
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09009086 err = ufshcd_set_dma_mask(hba);
9087 if (err) {
9088 dev_err(hba->dev, "set dma mask failed\n");
9089 goto out_disable;
9090 }
9091
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309092 /* Allocate memory for host memory space */
9093 err = ufshcd_memory_alloc(hba);
9094 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309095 dev_err(hba->dev, "Memory allocation failed\n");
9096 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309097 }
9098
9099 /* Configure LRB */
9100 ufshcd_host_memory_configure(hba);
9101
9102 host->can_queue = hba->nutrs;
9103 host->cmd_per_lun = hba->nutrs;
9104 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03009105 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309106 host->max_channel = UFSHCD_MAX_CHANNEL;
9107 host->unique_id = host->host_no;
9108 host->max_cmd_len = MAX_CDB_SIZE;
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009109 host->set_dbd_for_caching = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309110
Dolev Raviv7eb584d2014-09-25 15:32:31 +03009111 hba->max_pwr_info.is_valid = false;
9112
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309113 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05309114 init_waitqueue_head(&hba->tm_wq);
9115 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309116
9117 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05309118 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05309119 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309120
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309121 /* Initialize UIC command mutex */
9122 mutex_init(&hba->uic_cmd_mutex);
9123
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309124 /* Initialize mutex for device management commands */
9125 mutex_init(&hba->dev_cmd.lock);
9126
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009127 init_rwsem(&hba->clk_scaling_lock);
9128
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05309129 /* Initialize device management tag acquire wait queue */
9130 init_waitqueue_head(&hba->dev_cmd.tag_wq);
9131
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009132 ufshcd_init_clk_gating(hba);
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009133 ufshcd_init_hibern8_on_idle(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02009134
9135 /*
9136 * In order to avoid any spurious interrupt immediately after
9137 * registering UFS controller interrupt handler, clear any pending UFS
9138 * interrupt status and disable all the UFS interrupts.
9139 */
9140 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
9141 REG_INTERRUPT_STATUS);
9142 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
9143 /*
9144 * Make sure that UFS interrupts are disabled and any pending interrupt
9145 * status is cleared before registering UFS interrupt handler.
9146 */
9147 mb();
9148
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309149 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09009150 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309151 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309152 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009153 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009154 } else {
9155 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309156 }
9157
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309158 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309159 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309160 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009161 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309162 }
9163
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309164 /* Host controller enable */
9165 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309166 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309167 dev_err(hba->dev, "Host controller enable failed\n");
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009168 ufshcd_print_host_regs(hba);
9169 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309170 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309171 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309172
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009173 if (ufshcd_is_clkscaling_supported(hba)) {
9174 char wq_name[sizeof("ufs_clkscaling_00")];
9175
Sahitya Tummala856b3482014-09-25 15:32:34 +03009176 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009177 "simple_ondemand", gov_data);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009178 if (IS_ERR(hba->devfreq)) {
9179 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
9180 PTR_ERR(hba->devfreq));
9181 goto out_remove_scsi_host;
9182 }
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009183 hba->clk_scaling.is_suspended = false;
9184
9185 INIT_WORK(&hba->clk_scaling.suspend_work,
9186 ufshcd_clk_scaling_suspend_work);
9187 INIT_WORK(&hba->clk_scaling.resume_work,
9188 ufshcd_clk_scaling_resume_work);
9189
9190 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d",
9191 host->host_no);
9192 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
9193
Sahitya Tummala856b3482014-09-25 15:32:34 +03009194 /* Suspend devfreq until the UFS device is detected */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009195 ufshcd_suspend_clkscaling(hba);
9196 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03009197 }
9198
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009199 /*
9200 * If rpm_lvl and and spm_lvl are not already set to valid levels,
9201 * set the default power management level for UFS runtime and system
9202 * suspend. Default power saving mode selected is keeping UFS link in
9203 * Hibern8 state and UFS device in sleep.
9204 */
9205 if (!ufshcd_is_valid_pm_lvl(hba->rpm_lvl))
9206 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9207 UFS_SLEEP_PWR_MODE,
9208 UIC_LINK_HIBERN8_STATE);
9209 if (!ufshcd_is_valid_pm_lvl(hba->spm_lvl))
9210 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
9211 UFS_SLEEP_PWR_MODE,
9212 UIC_LINK_HIBERN8_STATE);
9213
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05309214 /* Hold auto suspend until async scan completes */
9215 pm_runtime_get_sync(dev);
9216
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009217 /*
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009218 * We are assuming that device wasn't put in sleep/power-down
9219 * state exclusively during the boot stage before kernel.
9220 * This assumption helps avoid doing link startup twice during
9221 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009222 */
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009223 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009224
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05309225 async_schedule(ufshcd_async_scan, hba);
9226
Subhash Jadavanicce6fbc2016-08-11 11:35:26 -07009227 ufsdbg_add_debugfs(hba);
9228
9229 ufshcd_add_sysfs_nodes(hba);
9230
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309231 return 0;
9232
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309233out_remove_scsi_host:
9234 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03009235exit_gating:
9236 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309237out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03009238 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03009239 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309240out_error:
9241 return err;
9242}
9243EXPORT_SYMBOL_GPL(ufshcd_init);
9244
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05309245MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
9246MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05309247MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05309248MODULE_LICENSE("GPL");
9249MODULE_VERSION(UFSHCD_DRIVER_VERSION);