blob: abd0e6b05f7913a9c3ba3dbac66a57abb078f74d [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>
Sahitya Tummala856b3482014-09-25 15:32:34 +030041#include <linux/devfreq.h>
Yaniv Gardib573d482016-03-10 17:37:09 +020042#include <linux/nls.h>
Yaniv Gardi54b879b2016-03-10 17:37:05 +020043#include <linux/of.h>
Adrian Hunterad448372018-03-20 15:07:38 +020044#include <linux/bitfield.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053045#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020046#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053047#include "unipro.h"
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +020048#include "ufs-sysfs.h"
Avri Altmandf032bf2018-10-07 17:30:35 +030049#include "ufs_bsg.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053050
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -080051#define CREATE_TRACE_POINTS
52#include <trace/events/ufs.h>
53
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053054#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
55 UTP_TASK_REQ_COMPL |\
56 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053057/* UIC command timeout, unit: ms */
58#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053059
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053060/* NOP OUT retries waiting for NOP IN response */
61#define NOP_OUT_RETRIES 10
62/* Timeout after 30 msecs if NOP OUT hangs without response */
63#define NOP_OUT_TIMEOUT 30 /* msecs */
64
Dolev Raviv68078d52013-07-30 00:35:58 +053065/* Query request retries */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080066#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +053067/* Query request timeout */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080068#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +053069
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053070/* Task management command timeout */
71#define TM_CMD_TIMEOUT 100 /* msecs */
72
Yaniv Gardi64238fb2016-02-01 15:02:43 +020073/* maximum number of retries for a general UIC command */
74#define UFS_UIC_COMMAND_RETRIES 3
75
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030076/* maximum number of link-startup retries */
77#define DME_LINKSTARTUP_RETRIES 3
78
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020079/* Maximum retries for Hibern8 enter */
80#define UIC_HIBERN8_ENTER_RETRIES 3
81
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030082/* maximum number of reset retries before giving up */
83#define MAX_HOST_RESET_RETRIES 5
84
Dolev Raviv68078d52013-07-30 00:35:58 +053085/* Expose the flag value from utp_upiu_query.value */
86#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
87
Seungwon Jeon7d568652013-08-31 21:40:20 +053088/* Interrupt aggregation default timeout, unit: 40us */
89#define INT_AGGR_DEF_TO 0x02
90
Stanley Chu49615ba2019-09-16 23:56:50 +080091/* default delay of autosuspend: 2000 ms */
92#define RPM_AUTOSUSPEND_DELAY_MS 2000
93
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030094#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
95 ({ \
96 int _ret; \
97 if (_on) \
98 _ret = ufshcd_enable_vreg(_dev, _vreg); \
99 else \
100 _ret = ufshcd_disable_vreg(_dev, _vreg); \
101 _ret; \
102 })
103
Tomas Winklerba809172018-06-14 11:14:09 +0300104#define ufshcd_hex_dump(prefix_str, buf, len) do { \
105 size_t __len = (len); \
106 print_hex_dump(KERN_ERR, prefix_str, \
107 __len > 4 ? DUMP_PREFIX_OFFSET : DUMP_PREFIX_NONE,\
108 16, 4, buf, __len, false); \
109} while (0)
110
111int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,
112 const char *prefix)
113{
Marc Gonzalezd6724752019-01-22 18:29:22 +0100114 u32 *regs;
115 size_t pos;
116
117 if (offset % 4 != 0 || len % 4 != 0) /* keep readl happy */
118 return -EINVAL;
Tomas Winklerba809172018-06-14 11:14:09 +0300119
Can Guocddaeba2019-11-14 22:09:27 -0800120 regs = kzalloc(len, GFP_ATOMIC);
Tomas Winklerba809172018-06-14 11:14:09 +0300121 if (!regs)
122 return -ENOMEM;
123
Marc Gonzalezd6724752019-01-22 18:29:22 +0100124 for (pos = 0; pos < len; pos += 4)
125 regs[pos / 4] = ufshcd_readl(hba, offset + pos);
126
Tomas Winklerba809172018-06-14 11:14:09 +0300127 ufshcd_hex_dump(prefix, regs, len);
128 kfree(regs);
129
130 return 0;
131}
132EXPORT_SYMBOL_GPL(ufshcd_dump_regs);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800133
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530134enum {
135 UFSHCD_MAX_CHANNEL = 0,
136 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530137 UFSHCD_CMD_PER_LUN = 32,
138 UFSHCD_CAN_QUEUE = 32,
139};
140
141/* UFSHCD states */
142enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530143 UFSHCD_STATE_RESET,
144 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530145 UFSHCD_STATE_OPERATIONAL,
Zang Leigang141f8162016-11-16 11:29:37 +0800146 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530147};
148
149/* UFSHCD error handling flags */
150enum {
151 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530152};
153
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530154/* UFSHCD UIC layer error flags */
155enum {
156 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200157 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
158 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
159 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
160 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
161 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530162};
163
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530164#define ufshcd_set_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300165 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530166#define ufshcd_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300167 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530168#define ufshcd_clear_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300169 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530170
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300171#define ufshcd_set_ufs_dev_active(h) \
172 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
173#define ufshcd_set_ufs_dev_sleep(h) \
174 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
175#define ufshcd_set_ufs_dev_poweroff(h) \
176 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
177#define ufshcd_is_ufs_dev_active(h) \
178 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
179#define ufshcd_is_ufs_dev_sleep(h) \
180 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
181#define ufshcd_is_ufs_dev_poweroff(h) \
182 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
183
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +0200184struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300185 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
186 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
187 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
188 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
189 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
190 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
191};
192
193static inline enum ufs_dev_pwr_mode
194ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
195{
196 return ufs_pm_lvl_states[lvl].dev_state;
197}
198
199static inline enum uic_link_state
200ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
201{
202 return ufs_pm_lvl_states[lvl].link_state;
203}
204
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -0800205static inline enum ufs_pm_level
206ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
207 enum uic_link_state link_state)
208{
209 enum ufs_pm_level lvl;
210
211 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
212 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
213 (ufs_pm_lvl_states[lvl].link_state == link_state))
214 return lvl;
215 }
216
217 /* if no match found, return the level 0 */
218 return UFS_PM_LVL_0;
219}
220
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800221static struct ufs_dev_fix ufs_fixups[] = {
222 /* UFS cards deviations table */
223 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
224 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800225 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
226 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
227 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800228 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
229 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
230 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
231 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
232 UFS_DEVICE_QUIRK_PA_TACTIVATE),
233 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
234 UFS_DEVICE_QUIRK_PA_TACTIVATE),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800235 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
236 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
Wei Li8e4829c2018-11-08 09:08:29 -0800237 UFS_FIX(UFS_VENDOR_SKHYNIX, "hB8aL1" /*H28U62301AMR*/,
238 UFS_DEVICE_QUIRK_HOST_VS_DEBUGSAVECONFIGTIME),
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800239
240 END_FIX
241};
242
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -0800243static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530244static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530245static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800246static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530247static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300248static void ufshcd_hba_exit(struct ufs_hba *hba);
Bean Huo1b9e2142020-01-20 14:08:15 +0100249static int ufshcd_probe_hba(struct ufs_hba *hba, bool async);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300250static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
251 bool skip_ref_clk);
252static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300253static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300254static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300255static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800256static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
257static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800258static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800259static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300260static irqreturn_t ufshcd_intr(int irq, void *__hba);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300261static int ufshcd_change_power_mode(struct ufs_hba *hba,
262 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200263static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
264{
265 return tag >= 0 && tag < hba->nutrs;
266}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300267
Can Guo5231d382019-12-05 02:14:46 +0000268static inline void ufshcd_enable_irq(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300269{
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300270 if (!hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000271 enable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300272 hba->is_irq_enabled = true;
273 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300274}
275
276static inline void ufshcd_disable_irq(struct ufs_hba *hba)
277{
278 if (hba->is_irq_enabled) {
Can Guo5231d382019-12-05 02:14:46 +0000279 disable_irq(hba->irq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300280 hba->is_irq_enabled = false;
281 }
282}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530283
Subhash Jadavani38135532018-05-03 16:37:18 +0530284static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba)
285{
286 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt))
287 scsi_unblock_requests(hba->host);
288}
289
290static void ufshcd_scsi_block_requests(struct ufs_hba *hba)
291{
292 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1)
293 scsi_block_requests(hba->host);
294}
295
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300296static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag,
297 const char *str)
298{
299 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
300
301 trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->sc.cdb);
302}
303
304static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, unsigned int tag,
305 const char *str)
306{
307 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr;
308
309 trace_ufshcd_upiu(dev_name(hba->dev), str, &rq->header, &rq->qr);
310}
311
312static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag,
313 const char *str)
314{
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300315 int off = (int)tag - hba->nutrs;
Christoph Hellwig391e3882018-10-07 17:30:32 +0300316 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[off];
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300317
Christoph Hellwig391e3882018-10-07 17:30:32 +0300318 trace_ufshcd_upiu(dev_name(hba->dev), str, &descp->req_header,
319 &descp->input_param1);
Ohad Sharabi6667e6d2018-03-28 12:42:18 +0300320}
321
Lee Susman1a07f2d2016-12-22 18:42:03 -0800322static void ufshcd_add_command_trace(struct ufs_hba *hba,
323 unsigned int tag, const char *str)
324{
325 sector_t lba = -1;
326 u8 opcode = 0;
327 u32 intr, doorbell;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300328 struct ufshcd_lrb *lrbp = &hba->lrb[tag];
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800329 struct scsi_cmnd *cmd = lrbp->cmd;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800330 int transfer_len = -1;
331
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300332 if (!trace_ufshcd_command_enabled()) {
333 /* trace UPIU W/O tracing command */
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800334 if (cmd)
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300335 ufshcd_add_cmd_upiu_trace(hba, tag, str);
Lee Susman1a07f2d2016-12-22 18:42:03 -0800336 return;
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300337 }
Lee Susman1a07f2d2016-12-22 18:42:03 -0800338
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800339 if (cmd) { /* data phase exists */
Ohad Sharabie7c3b372018-08-05 16:26:23 +0300340 /* trace UPIU also */
341 ufshcd_add_cmd_upiu_trace(hba, tag, str);
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800342 opcode = cmd->cmnd[0];
Lee Susman1a07f2d2016-12-22 18:42:03 -0800343 if ((opcode == READ_10) || (opcode == WRITE_10)) {
344 /*
345 * Currently we only fully trace read(10) and write(10)
346 * commands
347 */
Bart Van Asschee4d2add2019-12-24 14:02:44 -0800348 if (cmd->request && cmd->request->bio)
349 lba = cmd->request->bio->bi_iter.bi_sector;
Lee Susman1a07f2d2016-12-22 18:42:03 -0800350 transfer_len = be32_to_cpu(
351 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
352 }
353 }
354
355 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
356 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
357 trace_ufshcd_command(dev_name(hba->dev), str, tag,
358 doorbell, transfer_len, intr, lba, opcode);
359}
360
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800361static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
362{
363 struct ufs_clk_info *clki;
364 struct list_head *head = &hba->clk_list_head;
365
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300366 if (list_empty(head))
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800367 return;
368
369 list_for_each_entry(clki, head, list) {
370 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
371 clki->max_freq)
372 dev_err(hba->dev, "clk: %s, rate: %u\n",
373 clki->name, clki->curr_freq);
374 }
375}
376
Stanley Chu48d5b972019-07-10 21:38:18 +0800377static void ufshcd_print_err_hist(struct ufs_hba *hba,
378 struct ufs_err_reg_hist *err_hist,
379 char *err_name)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800380{
381 int i;
Stanley Chu27752642019-01-28 22:04:26 +0800382 bool found = false;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800383
Stanley Chu48d5b972019-07-10 21:38:18 +0800384 for (i = 0; i < UFS_ERR_REG_HIST_LENGTH; i++) {
385 int p = (i + err_hist->pos) % UFS_ERR_REG_HIST_LENGTH;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800386
Stanley Chu645728a2020-01-04 22:26:06 +0800387 if (err_hist->tstamp[p] == 0)
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800388 continue;
Stanley Chuc5397f12019-07-10 21:38:20 +0800389 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p,
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800390 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
Stanley Chu27752642019-01-28 22:04:26 +0800391 found = true;
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800392 }
Stanley Chu27752642019-01-28 22:04:26 +0800393
394 if (!found)
Stanley Chufd1fb4d2020-01-04 22:26:08 +0800395 dev_err(hba->dev, "No record of %s\n", err_name);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800396}
397
Dolev Raviv66cc8202016-12-22 18:39:42 -0800398static void ufshcd_print_host_regs(struct ufs_hba *hba)
399{
Tomas Winklerba809172018-06-14 11:14:09 +0300400 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
Dolev Raviv66cc8202016-12-22 18:39:42 -0800401 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
402 hba->ufs_version, hba->capabilities);
403 dev_err(hba->dev,
404 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
405 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800406 dev_err(hba->dev,
407 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n",
408 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
409 hba->ufs_stats.hibern8_exit_cnt);
410
Stanley Chu48d5b972019-07-10 21:38:18 +0800411 ufshcd_print_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
412 ufshcd_print_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
413 ufshcd_print_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
414 ufshcd_print_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
415 ufshcd_print_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
Stanley Chud3c615b2019-07-10 21:38:19 +0800416 ufshcd_print_err_hist(hba, &hba->ufs_stats.auto_hibern8_err,
417 "auto_hibern8_err");
Stanley Chu8808b4e2019-07-10 21:38:21 +0800418 ufshcd_print_err_hist(hba, &hba->ufs_stats.fatal_err, "fatal_err");
419 ufshcd_print_err_hist(hba, &hba->ufs_stats.link_startup_err,
420 "link_startup_fail");
421 ufshcd_print_err_hist(hba, &hba->ufs_stats.resume_err, "resume_fail");
422 ufshcd_print_err_hist(hba, &hba->ufs_stats.suspend_err,
423 "suspend_fail");
424 ufshcd_print_err_hist(hba, &hba->ufs_stats.dev_reset, "dev_reset");
425 ufshcd_print_err_hist(hba, &hba->ufs_stats.host_reset, "host_reset");
426 ufshcd_print_err_hist(hba, &hba->ufs_stats.task_abort, "task_abort");
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800427
428 ufshcd_print_clk_freqs(hba);
429
Stanley Chu7c486d912019-12-24 21:01:06 +0800430 ufshcd_vops_dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800431}
432
433static
434void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
435{
436 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800437 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800438 int tag;
439
440 for_each_set_bit(tag, &bitmap, hba->nutrs) {
441 lrbp = &hba->lrb[tag];
442
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800443 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
444 tag, ktime_to_us(lrbp->issue_time_stamp));
Zang Leigang09017182017-09-27 10:06:06 +0800445 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
446 tag, ktime_to_us(lrbp->compl_time_stamp));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800447 dev_err(hba->dev,
448 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
449 tag, (u64)lrbp->utrd_dma_addr);
450
Dolev Raviv66cc8202016-12-22 18:39:42 -0800451 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
452 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800453 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
454 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800455 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
456 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800457 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
458 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800459 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
460 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800461
Gilad Broner7fabb772017-02-03 16:56:50 -0800462 prdt_length = le16_to_cpu(
463 lrbp->utr_descriptor_ptr->prd_table_length);
464 dev_err(hba->dev,
465 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
466 tag, prdt_length,
467 (u64)lrbp->ucd_prdt_dma_addr);
468
469 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800470 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800471 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800472 }
473}
474
475static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
476{
Dolev Raviv66cc8202016-12-22 18:39:42 -0800477 int tag;
478
479 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
Christoph Hellwig391e3882018-10-07 17:30:32 +0300480 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag];
481
Dolev Raviv66cc8202016-12-22 18:39:42 -0800482 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
Christoph Hellwig391e3882018-10-07 17:30:32 +0300483 ufshcd_hex_dump("", tmrdp, sizeof(*tmrdp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800484 }
485}
486
Gilad Broner6ba65582017-02-03 16:57:28 -0800487static void ufshcd_print_host_state(struct ufs_hba *hba)
488{
489 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
Bart Van Assche7252a362019-12-09 10:13:08 -0800490 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n",
491 hba->outstanding_reqs, hba->outstanding_tasks);
Gilad Broner6ba65582017-02-03 16:57:28 -0800492 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
493 hba->saved_err, hba->saved_uic_err);
494 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
495 hba->curr_dev_pwr_mode, hba->uic_link_state);
496 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
497 hba->pm_op_in_progress, hba->is_sys_suspended);
498 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
499 hba->auto_bkops_enabled, hba->host->host_self_blocked);
500 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
501 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
502 hba->eh_flags, hba->req_abort_count);
503 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
504 hba->capabilities, hba->caps);
505 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
506 hba->dev_quirks);
507}
508
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800509/**
510 * ufshcd_print_pwr_info - print power params as saved in hba
511 * power info
512 * @hba: per-adapter instance
513 */
514static void ufshcd_print_pwr_info(struct ufs_hba *hba)
515{
516 static const char * const names[] = {
517 "INVALID MODE",
518 "FAST MODE",
519 "SLOW_MODE",
520 "INVALID MODE",
521 "FASTAUTO_MODE",
522 "SLOWAUTO_MODE",
523 "INVALID MODE",
524 };
525
526 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
527 __func__,
528 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
529 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
530 names[hba->pwr_info.pwr_rx],
531 names[hba->pwr_info.pwr_tx],
532 hba->pwr_info.hs_rate);
533}
534
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530535/*
536 * ufshcd_wait_for_register - wait for register value to change
537 * @hba - per-adapter interface
538 * @reg - mmio register offset
539 * @mask - mask to apply to read register value
540 * @val - wait condition
541 * @interval_us - polling interval in microsecs
542 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200543 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530544 *
545 * Returns -ETIMEDOUT on error, zero on success
546 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200547int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
548 u32 val, unsigned long interval_us,
549 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530550{
551 int err = 0;
552 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
553
554 /* ignore bits that we don't intend to wait on */
555 val = val & mask;
556
557 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200558 if (can_sleep)
559 usleep_range(interval_us, interval_us + 50);
560 else
561 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530562 if (time_after(jiffies, timeout)) {
563 if ((ufshcd_readl(hba, reg) & mask) != val)
564 err = -ETIMEDOUT;
565 break;
566 }
567 }
568
569 return err;
570}
571
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530572/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530573 * ufshcd_get_intr_mask - Get the interrupt bit mask
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800574 * @hba: Pointer to adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530575 *
576 * Returns interrupt bit mask per version
577 */
578static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
579{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800580 u32 intr_mask = 0;
581
582 switch (hba->ufs_version) {
583 case UFSHCI_VERSION_10:
584 intr_mask = INTERRUPT_MASK_ALL_VER_10;
585 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800586 case UFSHCI_VERSION_11:
587 case UFSHCI_VERSION_20:
588 intr_mask = INTERRUPT_MASK_ALL_VER_11;
589 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800590 case UFSHCI_VERSION_21:
591 default:
592 intr_mask = INTERRUPT_MASK_ALL_VER_21;
Tomohiro Kusumi031d1e02017-03-23 12:49:04 +0200593 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800594 }
595
596 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530597}
598
599/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530600 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800601 * @hba: Pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530602 *
603 * Returns UFSHCI version supported by the controller
604 */
605static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
606{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200607 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
608 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300609
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530610 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530611}
612
613/**
614 * ufshcd_is_device_present - Check if any device connected to
615 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300616 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530617 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300618 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530619 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300620static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530621{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300622 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300623 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530624}
625
626/**
627 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
Bart Van Assche8aa29f12018-03-01 15:07:20 -0800628 * @lrbp: pointer to local command reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530629 *
630 * This function is used to get the OCS field from UTRD
631 * Returns the OCS field in the UTRD
632 */
633static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
634{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530635 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530636}
637
638/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530639 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
640 * @hba: per adapter instance
641 * @pos: position of the bit to be cleared
642 */
643static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
644{
Alim Akhtar1399c5b2018-05-06 15:44:15 +0530645 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
646 ufshcd_writel(hba, (1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
647 else
648 ufshcd_writel(hba, ~(1 << pos),
649 REG_UTP_TRANSFER_REQ_LIST_CLEAR);
650}
651
652/**
653 * ufshcd_utmrl_clear - Clear a bit in UTRMLCLR register
654 * @hba: per adapter instance
655 * @pos: position of the bit to be cleared
656 */
657static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos)
658{
659 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR)
660 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
661 else
662 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530663}
664
665/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200666 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
667 * @hba: per adapter instance
668 * @tag: position of the bit to be cleared
669 */
670static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
671{
672 __clear_bit(tag, &hba->outstanding_reqs);
673}
674
675/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530676 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
677 * @reg: Register value of host controller status
678 *
679 * Returns integer, 0 on Success and positive value if failed
680 */
681static inline int ufshcd_get_lists_status(u32 reg)
682{
Tomohiro Kusumi6cf16112017-04-26 20:28:58 +0300683 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530684}
685
686/**
687 * ufshcd_get_uic_cmd_result - Get the UIC command result
688 * @hba: Pointer to adapter instance
689 *
690 * This function gets the result of UIC command completion
691 * Returns 0 on success, non zero value on error
692 */
693static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
694{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530695 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530696 MASK_UIC_COMMAND_RESULT;
697}
698
699/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530700 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
701 * @hba: Pointer to adapter instance
702 *
703 * This function gets UIC command argument3
704 * Returns 0 on success, non zero value on error
705 */
706static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
707{
708 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
709}
710
711/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530712 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530713 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530714 */
715static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530716ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530717{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530718 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530719}
720
721/**
722 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
723 * @ucd_rsp_ptr: pointer to response UPIU
724 *
725 * This function gets the response status and scsi_status from response UPIU
726 * Returns the response result code.
727 */
728static inline int
729ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
730{
731 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
732}
733
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530734/*
735 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
736 * from response UPIU
737 * @ucd_rsp_ptr: pointer to response UPIU
738 *
739 * Return the data segment length.
740 */
741static inline unsigned int
742ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
743{
744 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
745 MASK_RSP_UPIU_DATA_SEG_LEN;
746}
747
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530748/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530749 * ufshcd_is_exception_event - Check if the device raised an exception event
750 * @ucd_rsp_ptr: pointer to response UPIU
751 *
752 * The function checks if the device raised an exception event indicated in
753 * the Device Information field of response UPIU.
754 *
755 * Returns true if exception is raised, false otherwise.
756 */
757static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
758{
759 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
760 MASK_RSP_EXCEPTION_EVENT ? true : false;
761}
762
763/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530764 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530765 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530766 */
767static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530768ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530769{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530770 ufshcd_writel(hba, INT_AGGR_ENABLE |
771 INT_AGGR_COUNTER_AND_TIMER_RESET,
772 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
773}
774
775/**
776 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
777 * @hba: per adapter instance
778 * @cnt: Interrupt aggregation counter threshold
779 * @tmout: Interrupt aggregation timeout value
780 */
781static inline void
782ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
783{
784 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
785 INT_AGGR_COUNTER_THLD_VAL(cnt) |
786 INT_AGGR_TIMEOUT_VAL(tmout),
787 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530788}
789
790/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300791 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
792 * @hba: per adapter instance
793 */
794static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
795{
796 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
797}
798
799/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530800 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
801 * When run-stop registers are set to 1, it indicates the
802 * host controller that it can process the requests
803 * @hba: per adapter instance
804 */
805static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
806{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530807 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
808 REG_UTP_TASK_REQ_LIST_RUN_STOP);
809 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
810 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530811}
812
813/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530814 * ufshcd_hba_start - Start controller initialization sequence
815 * @hba: per adapter instance
816 */
817static inline void ufshcd_hba_start(struct ufs_hba *hba)
818{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530819 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530820}
821
822/**
823 * ufshcd_is_hba_active - Get controller state
824 * @hba: per adapter instance
825 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300826 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530827 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300828static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530829{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300830 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
831 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530832}
833
Yaniv Gardi37113102016-03-10 17:37:16 +0200834u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
835{
836 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
837 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
838 (hba->ufs_version == UFSHCI_VERSION_11))
839 return UFS_UNIPRO_VER_1_41;
840 else
841 return UFS_UNIPRO_VER_1_6;
842}
843EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
844
845static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
846{
847 /*
848 * If both host and device support UniPro ver1.6 or later, PA layer
849 * parameters tuning happens during link startup itself.
850 *
851 * We can manually tune PA layer parameters if either host or device
852 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
853 * logic simple, we will only do manual tuning if local unipro version
854 * doesn't support ver1.6 or later.
855 */
856 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
857 return true;
858 else
859 return false;
860}
861
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800862static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
863{
864 int ret = 0;
865 struct ufs_clk_info *clki;
866 struct list_head *head = &hba->clk_list_head;
867 ktime_t start = ktime_get();
868 bool clk_state_changed = false;
869
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300870 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800871 goto out;
872
873 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
874 if (ret)
875 return ret;
876
877 list_for_each_entry(clki, head, list) {
878 if (!IS_ERR_OR_NULL(clki->clk)) {
879 if (scale_up && clki->max_freq) {
880 if (clki->curr_freq == clki->max_freq)
881 continue;
882
883 clk_state_changed = true;
884 ret = clk_set_rate(clki->clk, clki->max_freq);
885 if (ret) {
886 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
887 __func__, clki->name,
888 clki->max_freq, ret);
889 break;
890 }
891 trace_ufshcd_clk_scaling(dev_name(hba->dev),
892 "scaled up", clki->name,
893 clki->curr_freq,
894 clki->max_freq);
895
896 clki->curr_freq = clki->max_freq;
897
898 } else if (!scale_up && clki->min_freq) {
899 if (clki->curr_freq == clki->min_freq)
900 continue;
901
902 clk_state_changed = true;
903 ret = clk_set_rate(clki->clk, clki->min_freq);
904 if (ret) {
905 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
906 __func__, clki->name,
907 clki->min_freq, ret);
908 break;
909 }
910 trace_ufshcd_clk_scaling(dev_name(hba->dev),
911 "scaled down", clki->name,
912 clki->curr_freq,
913 clki->min_freq);
914 clki->curr_freq = clki->min_freq;
915 }
916 }
917 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
918 clki->name, clk_get_rate(clki->clk));
919 }
920
921 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
922
923out:
924 if (clk_state_changed)
925 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
926 (scale_up ? "up" : "down"),
927 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
928 return ret;
929}
930
931/**
932 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
933 * @hba: per adapter instance
934 * @scale_up: True if scaling up and false if scaling down
935 *
936 * Returns true if scaling is required, false otherwise.
937 */
938static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
939 bool scale_up)
940{
941 struct ufs_clk_info *clki;
942 struct list_head *head = &hba->clk_list_head;
943
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300944 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800945 return false;
946
947 list_for_each_entry(clki, head, list) {
948 if (!IS_ERR_OR_NULL(clki->clk)) {
949 if (scale_up && clki->max_freq) {
950 if (clki->curr_freq == clki->max_freq)
951 continue;
952 return true;
953 } else if (!scale_up && clki->min_freq) {
954 if (clki->curr_freq == clki->min_freq)
955 continue;
956 return true;
957 }
958 }
959 }
960
961 return false;
962}
963
964static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
965 u64 wait_timeout_us)
966{
967 unsigned long flags;
968 int ret = 0;
969 u32 tm_doorbell;
970 u32 tr_doorbell;
971 bool timeout = false, do_last_check = false;
972 ktime_t start;
973
974 ufshcd_hold(hba, false);
975 spin_lock_irqsave(hba->host->host_lock, flags);
976 /*
977 * Wait for all the outstanding tasks/transfer requests.
978 * Verify by checking the doorbell registers are clear.
979 */
980 start = ktime_get();
981 do {
982 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
983 ret = -EBUSY;
984 goto out;
985 }
986
987 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
988 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
989 if (!tm_doorbell && !tr_doorbell) {
990 timeout = false;
991 break;
992 } else if (do_last_check) {
993 break;
994 }
995
996 spin_unlock_irqrestore(hba->host->host_lock, flags);
997 schedule();
998 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
999 wait_timeout_us) {
1000 timeout = true;
1001 /*
1002 * We might have scheduled out for long time so make
1003 * sure to check if doorbells are cleared by this time
1004 * or not.
1005 */
1006 do_last_check = true;
1007 }
1008 spin_lock_irqsave(hba->host->host_lock, flags);
1009 } while (tm_doorbell || tr_doorbell);
1010
1011 if (timeout) {
1012 dev_err(hba->dev,
1013 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1014 __func__, tm_doorbell, tr_doorbell);
1015 ret = -EBUSY;
1016 }
1017out:
1018 spin_unlock_irqrestore(hba->host->host_lock, flags);
1019 ufshcd_release(hba);
1020 return ret;
1021}
1022
1023/**
1024 * ufshcd_scale_gear - scale up/down UFS gear
1025 * @hba: per adapter instance
1026 * @scale_up: True for scaling up gear and false for scaling down
1027 *
1028 * Returns 0 for success,
1029 * Returns -EBUSY if scaling can't happen at this time
1030 * Returns non-zero for any other errors
1031 */
1032static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1033{
1034 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1035 int ret = 0;
1036 struct ufs_pa_layer_attr new_pwr_info;
1037
1038 if (scale_up) {
1039 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1040 sizeof(struct ufs_pa_layer_attr));
1041 } else {
1042 memcpy(&new_pwr_info, &hba->pwr_info,
1043 sizeof(struct ufs_pa_layer_attr));
1044
1045 if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN
1046 || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) {
1047 /* save the current power mode */
1048 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1049 &hba->pwr_info,
1050 sizeof(struct ufs_pa_layer_attr));
1051
1052 /* scale down gear */
1053 new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1054 new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1055 }
1056 }
1057
1058 /* check if the power mode needs to be changed or not? */
1059 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
1060
1061 if (ret)
1062 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1063 __func__, ret,
1064 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1065 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1066
1067 return ret;
1068}
1069
1070static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1071{
1072 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1073 int ret = 0;
1074 /*
1075 * make sure that there are no outstanding requests when
1076 * clock scaling is in progress
1077 */
Subhash Jadavani38135532018-05-03 16:37:18 +05301078 ufshcd_scsi_block_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001079 down_write(&hba->clk_scaling_lock);
1080 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1081 ret = -EBUSY;
1082 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301083 ufshcd_scsi_unblock_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001084 }
1085
1086 return ret;
1087}
1088
1089static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1090{
1091 up_write(&hba->clk_scaling_lock);
Subhash Jadavani38135532018-05-03 16:37:18 +05301092 ufshcd_scsi_unblock_requests(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001093}
1094
1095/**
1096 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1097 * @hba: per adapter instance
1098 * @scale_up: True for scaling up and false for scalin down
1099 *
1100 * Returns 0 for success,
1101 * Returns -EBUSY if scaling can't happen at this time
1102 * Returns non-zero for any other errors
1103 */
1104static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1105{
1106 int ret = 0;
1107
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001108 /* let's not get into low power until clock scaling is completed */
1109 ufshcd_hold(hba, false);
1110
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001111 ret = ufshcd_clock_scaling_prepare(hba);
1112 if (ret)
1113 return ret;
1114
1115 /* scale down the gear before scaling down clocks */
1116 if (!scale_up) {
1117 ret = ufshcd_scale_gear(hba, false);
1118 if (ret)
1119 goto out;
1120 }
1121
1122 ret = ufshcd_scale_clks(hba, scale_up);
1123 if (ret) {
1124 if (!scale_up)
1125 ufshcd_scale_gear(hba, true);
1126 goto out;
1127 }
1128
1129 /* scale up the gear after scaling up clocks */
1130 if (scale_up) {
1131 ret = ufshcd_scale_gear(hba, true);
1132 if (ret) {
1133 ufshcd_scale_clks(hba, false);
1134 goto out;
1135 }
1136 }
1137
1138 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1139
1140out:
1141 ufshcd_clock_scaling_unprepare(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001142 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001143 return ret;
1144}
1145
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001146static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1147{
1148 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1149 clk_scaling.suspend_work);
1150 unsigned long irq_flags;
1151
1152 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1153 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1154 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1155 return;
1156 }
1157 hba->clk_scaling.is_suspended = true;
1158 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1159
1160 __ufshcd_suspend_clkscaling(hba);
1161}
1162
1163static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1164{
1165 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1166 clk_scaling.resume_work);
1167 unsigned long irq_flags;
1168
1169 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1170 if (!hba->clk_scaling.is_suspended) {
1171 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1172 return;
1173 }
1174 hba->clk_scaling.is_suspended = false;
1175 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1176
1177 devfreq_resume_device(hba->devfreq);
1178}
1179
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001180static int ufshcd_devfreq_target(struct device *dev,
1181 unsigned long *freq, u32 flags)
1182{
1183 int ret = 0;
1184 struct ufs_hba *hba = dev_get_drvdata(dev);
1185 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001186 bool scale_up, sched_clk_scaling_suspend_work = false;
Bjorn Andersson092b4552018-05-17 23:26:37 -07001187 struct list_head *clk_list = &hba->clk_list_head;
1188 struct ufs_clk_info *clki;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001189 unsigned long irq_flags;
1190
1191 if (!ufshcd_is_clkscaling_supported(hba))
1192 return -EINVAL;
1193
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001194 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1195 if (ufshcd_eh_in_progress(hba)) {
1196 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1197 return 0;
1198 }
1199
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001200 if (!hba->clk_scaling.active_reqs)
1201 sched_clk_scaling_suspend_work = true;
1202
Bjorn Andersson092b4552018-05-17 23:26:37 -07001203 if (list_empty(clk_list)) {
1204 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1205 goto out;
1206 }
1207
1208 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list);
1209 scale_up = (*freq == clki->max_freq) ? true : false;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001210 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1211 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1212 ret = 0;
1213 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001214 }
1215 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1216
1217 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001218 ret = ufshcd_devfreq_scale(hba, scale_up);
1219
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001220 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1221 (scale_up ? "up" : "down"),
1222 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1223
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001224out:
1225 if (sched_clk_scaling_suspend_work)
1226 queue_work(hba->clk_scaling.workq,
1227 &hba->clk_scaling.suspend_work);
1228
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001229 return ret;
1230}
1231
Bart Van Assche7252a362019-12-09 10:13:08 -08001232static bool ufshcd_is_busy(struct request *req, void *priv, bool reserved)
1233{
1234 int *busy = priv;
1235
1236 WARN_ON_ONCE(reserved);
1237 (*busy)++;
1238 return false;
1239}
1240
1241/* Whether or not any tag is in use by a request that is in progress. */
1242static bool ufshcd_any_tag_in_use(struct ufs_hba *hba)
1243{
1244 struct request_queue *q = hba->cmd_queue;
1245 int busy = 0;
1246
1247 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_is_busy, &busy);
1248 return busy;
1249}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001250
1251static int ufshcd_devfreq_get_dev_status(struct device *dev,
1252 struct devfreq_dev_status *stat)
1253{
1254 struct ufs_hba *hba = dev_get_drvdata(dev);
1255 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1256 unsigned long flags;
1257
1258 if (!ufshcd_is_clkscaling_supported(hba))
1259 return -EINVAL;
1260
1261 memset(stat, 0, sizeof(*stat));
1262
1263 spin_lock_irqsave(hba->host->host_lock, flags);
1264 if (!scaling->window_start_t)
1265 goto start_window;
1266
1267 if (scaling->is_busy_started)
1268 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1269 scaling->busy_start_t));
1270
1271 stat->total_time = jiffies_to_usecs((long)jiffies -
1272 (long)scaling->window_start_t);
1273 stat->busy_time = scaling->tot_busy_t;
1274start_window:
1275 scaling->window_start_t = jiffies;
1276 scaling->tot_busy_t = 0;
1277
1278 if (hba->outstanding_reqs) {
1279 scaling->busy_start_t = ktime_get();
1280 scaling->is_busy_started = true;
1281 } else {
1282 scaling->busy_start_t = 0;
1283 scaling->is_busy_started = false;
1284 }
1285 spin_unlock_irqrestore(hba->host->host_lock, flags);
1286 return 0;
1287}
1288
1289static struct devfreq_dev_profile ufs_devfreq_profile = {
1290 .polling_ms = 100,
1291 .target = ufshcd_devfreq_target,
1292 .get_dev_status = ufshcd_devfreq_get_dev_status,
1293};
1294
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001295static int ufshcd_devfreq_init(struct ufs_hba *hba)
1296{
Bjorn Andersson092b4552018-05-17 23:26:37 -07001297 struct list_head *clk_list = &hba->clk_list_head;
1298 struct ufs_clk_info *clki;
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001299 struct devfreq *devfreq;
1300 int ret;
1301
Bjorn Andersson092b4552018-05-17 23:26:37 -07001302 /* Skip devfreq if we don't have any clocks in the list */
1303 if (list_empty(clk_list))
1304 return 0;
1305
1306 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1307 dev_pm_opp_add(hba->dev, clki->min_freq, 0);
1308 dev_pm_opp_add(hba->dev, clki->max_freq, 0);
1309
1310 devfreq = devfreq_add_device(hba->dev,
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001311 &ufs_devfreq_profile,
1312 DEVFREQ_GOV_SIMPLE_ONDEMAND,
1313 NULL);
1314 if (IS_ERR(devfreq)) {
1315 ret = PTR_ERR(devfreq);
1316 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret);
Bjorn Andersson092b4552018-05-17 23:26:37 -07001317
1318 dev_pm_opp_remove(hba->dev, clki->min_freq);
1319 dev_pm_opp_remove(hba->dev, clki->max_freq);
Bjorn Anderssondeac4442018-05-17 23:26:36 -07001320 return ret;
1321 }
1322
1323 hba->devfreq = devfreq;
1324
1325 return 0;
1326}
1327
Bjorn Andersson092b4552018-05-17 23:26:37 -07001328static void ufshcd_devfreq_remove(struct ufs_hba *hba)
1329{
1330 struct list_head *clk_list = &hba->clk_list_head;
1331 struct ufs_clk_info *clki;
1332
1333 if (!hba->devfreq)
1334 return;
1335
1336 devfreq_remove_device(hba->devfreq);
1337 hba->devfreq = NULL;
1338
1339 clki = list_first_entry(clk_list, struct ufs_clk_info, list);
1340 dev_pm_opp_remove(hba->dev, clki->min_freq);
1341 dev_pm_opp_remove(hba->dev, clki->max_freq);
1342}
1343
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001344static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1345{
1346 unsigned long flags;
1347
1348 devfreq_suspend_device(hba->devfreq);
1349 spin_lock_irqsave(hba->host->host_lock, flags);
1350 hba->clk_scaling.window_start_t = 0;
1351 spin_unlock_irqrestore(hba->host->host_lock, flags);
1352}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001353
Gilad Bronera5082532016-10-17 17:10:00 -07001354static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1355{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001356 unsigned long flags;
1357 bool suspend = false;
1358
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001359 if (!ufshcd_is_clkscaling_supported(hba))
1360 return;
1361
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001362 spin_lock_irqsave(hba->host->host_lock, flags);
1363 if (!hba->clk_scaling.is_suspended) {
1364 suspend = true;
1365 hba->clk_scaling.is_suspended = true;
1366 }
1367 spin_unlock_irqrestore(hba->host->host_lock, flags);
1368
1369 if (suspend)
1370 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001371}
1372
1373static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1374{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001375 unsigned long flags;
1376 bool resume = false;
1377
1378 if (!ufshcd_is_clkscaling_supported(hba))
1379 return;
1380
1381 spin_lock_irqsave(hba->host->host_lock, flags);
1382 if (hba->clk_scaling.is_suspended) {
1383 resume = true;
1384 hba->clk_scaling.is_suspended = false;
1385 }
1386 spin_unlock_irqrestore(hba->host->host_lock, flags);
1387
1388 if (resume)
1389 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001390}
1391
1392static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1393 struct device_attribute *attr, char *buf)
1394{
1395 struct ufs_hba *hba = dev_get_drvdata(dev);
1396
1397 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1398}
1399
1400static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1401 struct device_attribute *attr, const char *buf, size_t count)
1402{
1403 struct ufs_hba *hba = dev_get_drvdata(dev);
1404 u32 value;
1405 int err;
1406
1407 if (kstrtou32(buf, 0, &value))
1408 return -EINVAL;
1409
1410 value = !!value;
1411 if (value == hba->clk_scaling.is_allowed)
1412 goto out;
1413
1414 pm_runtime_get_sync(hba->dev);
1415 ufshcd_hold(hba, false);
1416
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001417 cancel_work_sync(&hba->clk_scaling.suspend_work);
1418 cancel_work_sync(&hba->clk_scaling.resume_work);
1419
1420 hba->clk_scaling.is_allowed = value;
1421
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001422 if (value) {
1423 ufshcd_resume_clkscaling(hba);
1424 } else {
1425 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001426 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001427 if (err)
1428 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1429 __func__, err);
1430 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001431
1432 ufshcd_release(hba);
1433 pm_runtime_put_sync(hba->dev);
1434out:
1435 return count;
Gilad Bronera5082532016-10-17 17:10:00 -07001436}
1437
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001438static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1439{
1440 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1441 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1442 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1443 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1444 hba->clk_scaling.enable_attr.attr.mode = 0644;
1445 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1446 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1447}
1448
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001449static void ufshcd_ungate_work(struct work_struct *work)
1450{
1451 int ret;
1452 unsigned long flags;
1453 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1454 clk_gating.ungate_work);
1455
1456 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1457
1458 spin_lock_irqsave(hba->host->host_lock, flags);
1459 if (hba->clk_gating.state == CLKS_ON) {
1460 spin_unlock_irqrestore(hba->host->host_lock, flags);
1461 goto unblock_reqs;
1462 }
1463
1464 spin_unlock_irqrestore(hba->host->host_lock, flags);
1465 ufshcd_setup_clocks(hba, true);
1466
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001467 ufshcd_enable_irq(hba);
1468
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001469 /* Exit from hibern8 */
1470 if (ufshcd_can_hibern8_during_gating(hba)) {
1471 /* Prevent gating in this path */
1472 hba->clk_gating.is_suspended = true;
1473 if (ufshcd_is_link_hibern8(hba)) {
1474 ret = ufshcd_uic_hibern8_exit(hba);
1475 if (ret)
1476 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1477 __func__, ret);
1478 else
1479 ufshcd_set_link_active(hba);
1480 }
1481 hba->clk_gating.is_suspended = false;
1482 }
1483unblock_reqs:
Subhash Jadavani38135532018-05-03 16:37:18 +05301484 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001485}
1486
1487/**
1488 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1489 * Also, exit from hibern8 mode and set the link as active.
1490 * @hba: per adapter instance
1491 * @async: This indicates whether caller should ungate clocks asynchronously.
1492 */
1493int ufshcd_hold(struct ufs_hba *hba, bool async)
1494{
1495 int rc = 0;
1496 unsigned long flags;
1497
1498 if (!ufshcd_is_clkgating_allowed(hba))
1499 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001500 spin_lock_irqsave(hba->host->host_lock, flags);
1501 hba->clk_gating.active_reqs++;
1502
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001503 if (ufshcd_eh_in_progress(hba)) {
1504 spin_unlock_irqrestore(hba->host->host_lock, flags);
1505 return 0;
1506 }
1507
Sahitya Tummala856b3482014-09-25 15:32:34 +03001508start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001509 switch (hba->clk_gating.state) {
1510 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001511 /*
1512 * Wait for the ungate work to complete if in progress.
1513 * Though the clocks may be in ON state, the link could
1514 * still be in hibner8 state if hibern8 is allowed
1515 * during clock gating.
1516 * Make sure we exit hibern8 state also in addition to
1517 * clocks being ON.
1518 */
1519 if (ufshcd_can_hibern8_during_gating(hba) &&
1520 ufshcd_is_link_hibern8(hba)) {
1521 spin_unlock_irqrestore(hba->host->host_lock, flags);
1522 flush_work(&hba->clk_gating.ungate_work);
1523 spin_lock_irqsave(hba->host->host_lock, flags);
1524 goto start;
1525 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001526 break;
1527 case REQ_CLKS_OFF:
1528 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1529 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001530 trace_ufshcd_clk_gating(dev_name(hba->dev),
1531 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001532 break;
1533 }
1534 /*
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +03001535 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001536 * currently running. Hence, fall through to cancel gating
1537 * work and to enable clocks.
1538 */
Tomas Winkler30eb2e42018-11-26 10:10:34 +02001539 /* fallthrough */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001540 case CLKS_OFF:
Subhash Jadavani38135532018-05-03 16:37:18 +05301541 ufshcd_scsi_block_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001542 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001543 trace_ufshcd_clk_gating(dev_name(hba->dev),
1544 hba->clk_gating.state);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301545 queue_work(hba->clk_gating.clk_gating_workq,
1546 &hba->clk_gating.ungate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001547 /*
1548 * fall through to check if we should wait for this
1549 * work to be done or not.
1550 */
Tomas Winkler30eb2e42018-11-26 10:10:34 +02001551 /* fallthrough */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001552 case REQ_CLKS_ON:
1553 if (async) {
1554 rc = -EAGAIN;
1555 hba->clk_gating.active_reqs--;
1556 break;
1557 }
1558
1559 spin_unlock_irqrestore(hba->host->host_lock, flags);
1560 flush_work(&hba->clk_gating.ungate_work);
1561 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001562 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001563 goto start;
1564 default:
1565 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1566 __func__, hba->clk_gating.state);
1567 break;
1568 }
1569 spin_unlock_irqrestore(hba->host->host_lock, flags);
1570out:
1571 return rc;
1572}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001573EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001574
1575static void ufshcd_gate_work(struct work_struct *work)
1576{
1577 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1578 clk_gating.gate_work.work);
1579 unsigned long flags;
1580
1581 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001582 /*
1583 * In case you are here to cancel this work the gating state
1584 * would be marked as REQ_CLKS_ON. In this case save time by
1585 * skipping the gating work and exit after changing the clock
1586 * state to CLKS_ON.
1587 */
1588 if (hba->clk_gating.is_suspended ||
Asutosh Das18f013742019-11-14 22:09:29 -08001589 (hba->clk_gating.state != REQ_CLKS_OFF)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001590 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001591 trace_ufshcd_clk_gating(dev_name(hba->dev),
1592 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001593 goto rel_lock;
1594 }
1595
1596 if (hba->clk_gating.active_reqs
1597 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
Bart Van Assche7252a362019-12-09 10:13:08 -08001598 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001599 || hba->active_uic_cmd || hba->uic_async_done)
1600 goto rel_lock;
1601
1602 spin_unlock_irqrestore(hba->host->host_lock, flags);
1603
1604 /* put the link into hibern8 mode before turning off clocks */
1605 if (ufshcd_can_hibern8_during_gating(hba)) {
1606 if (ufshcd_uic_hibern8_enter(hba)) {
1607 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001608 trace_ufshcd_clk_gating(dev_name(hba->dev),
1609 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001610 goto out;
1611 }
1612 ufshcd_set_link_hibern8(hba);
1613 }
1614
Stanley Chu8b0bbf02019-12-07 20:22:01 +08001615 ufshcd_disable_irq(hba);
1616
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001617 if (!ufshcd_is_link_active(hba))
1618 ufshcd_setup_clocks(hba, false);
1619 else
1620 /* If link is active, device ref_clk can't be switched off */
1621 __ufshcd_setup_clocks(hba, false, true);
1622
1623 /*
1624 * In case you are here to cancel this work the gating state
1625 * would be marked as REQ_CLKS_ON. In this case keep the state
1626 * as REQ_CLKS_ON which would anyway imply that clocks are off
1627 * and a request to turn them on is pending. By doing this way,
1628 * we keep the state machine in tact and this would ultimately
1629 * prevent from doing cancel work multiple times when there are
1630 * new requests arriving before the current cancel work is done.
1631 */
1632 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001633 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001634 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001635 trace_ufshcd_clk_gating(dev_name(hba->dev),
1636 hba->clk_gating.state);
1637 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001638rel_lock:
1639 spin_unlock_irqrestore(hba->host->host_lock, flags);
1640out:
1641 return;
1642}
1643
1644/* host lock must be held before calling this variant */
1645static void __ufshcd_release(struct ufs_hba *hba)
1646{
1647 if (!ufshcd_is_clkgating_allowed(hba))
1648 return;
1649
1650 hba->clk_gating.active_reqs--;
1651
1652 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1653 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
Bart Van Assche7252a362019-12-09 10:13:08 -08001654 || ufshcd_any_tag_in_use(hba) || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001655 || hba->active_uic_cmd || hba->uic_async_done
1656 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001657 return;
1658
1659 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001660 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Evan Greenf4bb7702018-10-05 10:27:32 -07001661 queue_delayed_work(hba->clk_gating.clk_gating_workq,
1662 &hba->clk_gating.gate_work,
1663 msecs_to_jiffies(hba->clk_gating.delay_ms));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001664}
1665
1666void ufshcd_release(struct ufs_hba *hba)
1667{
1668 unsigned long flags;
1669
1670 spin_lock_irqsave(hba->host->host_lock, flags);
1671 __ufshcd_release(hba);
1672 spin_unlock_irqrestore(hba->host->host_lock, flags);
1673}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001674EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001675
1676static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1677 struct device_attribute *attr, char *buf)
1678{
1679 struct ufs_hba *hba = dev_get_drvdata(dev);
1680
1681 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1682}
1683
1684static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1685 struct device_attribute *attr, const char *buf, size_t count)
1686{
1687 struct ufs_hba *hba = dev_get_drvdata(dev);
1688 unsigned long flags, value;
1689
1690 if (kstrtoul(buf, 0, &value))
1691 return -EINVAL;
1692
1693 spin_lock_irqsave(hba->host->host_lock, flags);
1694 hba->clk_gating.delay_ms = value;
1695 spin_unlock_irqrestore(hba->host->host_lock, flags);
1696 return count;
1697}
1698
Sahitya Tummalab4274112016-12-22 18:40:39 -08001699static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1700 struct device_attribute *attr, char *buf)
1701{
1702 struct ufs_hba *hba = dev_get_drvdata(dev);
1703
1704 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1705}
1706
1707static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1708 struct device_attribute *attr, const char *buf, size_t count)
1709{
1710 struct ufs_hba *hba = dev_get_drvdata(dev);
1711 unsigned long flags;
1712 u32 value;
1713
1714 if (kstrtou32(buf, 0, &value))
1715 return -EINVAL;
1716
1717 value = !!value;
1718 if (value == hba->clk_gating.is_enabled)
1719 goto out;
1720
1721 if (value) {
1722 ufshcd_release(hba);
1723 } else {
1724 spin_lock_irqsave(hba->host->host_lock, flags);
1725 hba->clk_gating.active_reqs++;
1726 spin_unlock_irqrestore(hba->host->host_lock, flags);
1727 }
1728
1729 hba->clk_gating.is_enabled = value;
1730out:
1731 return count;
1732}
1733
Vivek Gautameebcc192018-08-07 23:17:39 +05301734static void ufshcd_init_clk_scaling(struct ufs_hba *hba)
1735{
1736 char wq_name[sizeof("ufs_clkscaling_00")];
1737
1738 if (!ufshcd_is_clkscaling_supported(hba))
1739 return;
1740
1741 INIT_WORK(&hba->clk_scaling.suspend_work,
1742 ufshcd_clk_scaling_suspend_work);
1743 INIT_WORK(&hba->clk_scaling.resume_work,
1744 ufshcd_clk_scaling_resume_work);
1745
1746 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
1747 hba->host->host_no);
1748 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
1749
1750 ufshcd_clkscaling_init_sysfs(hba);
1751}
1752
1753static void ufshcd_exit_clk_scaling(struct ufs_hba *hba)
1754{
1755 if (!ufshcd_is_clkscaling_supported(hba))
1756 return;
1757
1758 destroy_workqueue(hba->clk_scaling.workq);
1759 ufshcd_devfreq_remove(hba);
1760}
1761
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001762static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1763{
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301764 char wq_name[sizeof("ufs_clk_gating_00")];
1765
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001766 if (!ufshcd_is_clkgating_allowed(hba))
1767 return;
1768
1769 hba->clk_gating.delay_ms = 150;
1770 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1771 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1772
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301773 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clk_gating_%d",
1774 hba->host->host_no);
1775 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name,
1776 WQ_MEM_RECLAIM);
1777
Sahitya Tummalab4274112016-12-22 18:40:39 -08001778 hba->clk_gating.is_enabled = true;
1779
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001780 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1781 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1782 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1783 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001784 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001785 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1786 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001787
1788 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1789 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1790 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1791 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1792 hba->clk_gating.enable_attr.attr.mode = 0644;
1793 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1794 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001795}
1796
1797static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1798{
1799 if (!ufshcd_is_clkgating_allowed(hba))
1800 return;
1801 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001802 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001803 cancel_work_sync(&hba->clk_gating.ungate_work);
1804 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Vijay Viswanath10e5e372018-05-03 16:37:22 +05301805 destroy_workqueue(hba->clk_gating.clk_gating_workq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001806}
1807
Sahitya Tummala856b3482014-09-25 15:32:34 +03001808/* Must be called with host lock acquired */
1809static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1810{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001811 bool queue_resume_work = false;
1812
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001813 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001814 return;
1815
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001816 if (!hba->clk_scaling.active_reqs++)
1817 queue_resume_work = true;
1818
1819 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1820 return;
1821
1822 if (queue_resume_work)
1823 queue_work(hba->clk_scaling.workq,
1824 &hba->clk_scaling.resume_work);
1825
1826 if (!hba->clk_scaling.window_start_t) {
1827 hba->clk_scaling.window_start_t = jiffies;
1828 hba->clk_scaling.tot_busy_t = 0;
1829 hba->clk_scaling.is_busy_started = false;
1830 }
1831
Sahitya Tummala856b3482014-09-25 15:32:34 +03001832 if (!hba->clk_scaling.is_busy_started) {
1833 hba->clk_scaling.busy_start_t = ktime_get();
1834 hba->clk_scaling.is_busy_started = true;
1835 }
1836}
1837
1838static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1839{
1840 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1841
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001842 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001843 return;
1844
1845 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1846 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1847 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001848 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001849 scaling->is_busy_started = false;
1850 }
1851}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301852/**
1853 * ufshcd_send_command - Send SCSI or device management commands
1854 * @hba: per adapter instance
1855 * @task_tag: Task tag of the command
1856 */
1857static inline
1858void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1859{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08001860 hba->lrb[task_tag].issue_time_stamp = ktime_get();
Zang Leigang09017182017-09-27 10:06:06 +08001861 hba->lrb[task_tag].compl_time_stamp = ktime_set(0, 0);
Bart Van Asscheeacf36f2019-12-24 14:02:46 -08001862 ufshcd_add_command_trace(hba, task_tag, "send");
Sahitya Tummala856b3482014-09-25 15:32:34 +03001863 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301864 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301865 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001866 /* Make sure that doorbell is committed immediately */
1867 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301868}
1869
1870/**
1871 * ufshcd_copy_sense_data - Copy sense data in case of check condition
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001872 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301873 */
1874static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1875{
1876 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301877 if (lrbp->sense_buffer &&
1878 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001879 int len_to_copy;
1880
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301881 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Avri Altman09a5a242018-11-22 20:04:56 +02001882 len_to_copy = min_t(int, UFS_SENSE_SIZE, len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001883
Avri Altman09a5a242018-11-22 20:04:56 +02001884 memcpy(lrbp->sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data,
1885 len_to_copy);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301886 }
1887}
1888
1889/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301890 * ufshcd_copy_query_response() - Copy the Query Response and the data
1891 * descriptor
1892 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001893 * @lrbp: pointer to local reference block
Dolev Raviv68078d52013-07-30 00:35:58 +05301894 */
1895static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001896int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301897{
1898 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1899
Dolev Raviv68078d52013-07-30 00:35:58 +05301900 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301901
Dolev Raviv68078d52013-07-30 00:35:58 +05301902 /* Get the descriptor */
Avri Altman1c908362019-05-21 11:24:22 +03001903 if (hba->dev_cmd.query.descriptor &&
1904 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001905 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301906 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001907 u16 resp_len;
1908 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301909
1910 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001911 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301912 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001913 buf_len = be16_to_cpu(
1914 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001915 if (likely(buf_len >= resp_len)) {
1916 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1917 } else {
1918 dev_warn(hba->dev,
Bean Huo3d4881d2019-11-12 23:34:35 +01001919 "%s: rsp size %d is bigger than buffer size %d",
1920 __func__, resp_len, buf_len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001921 return -EINVAL;
1922 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301923 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001924
1925 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301926}
1927
1928/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301929 * ufshcd_hba_capabilities - Read controller capabilities
1930 * @hba: per adapter instance
1931 */
1932static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
1933{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301934 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301935
1936 /* nutrs and nutmrs are 0 based values */
1937 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
1938 hba->nutmrs =
1939 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
1940}
1941
1942/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301943 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1944 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301945 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301946 * Return true on success, else false
1947 */
1948static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
1949{
1950 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1951 return true;
1952 else
1953 return false;
1954}
1955
1956/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301957 * ufshcd_get_upmcrs - Get the power mode change request status
1958 * @hba: Pointer to adapter instance
1959 *
1960 * This function gets the UPMCRS field of HCS register
1961 * Returns value of UPMCRS field
1962 */
1963static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1964{
1965 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1966}
1967
1968/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301969 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1970 * @hba: per adapter instance
1971 * @uic_cmd: UIC command
1972 *
1973 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301974 */
1975static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301976ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301977{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301978 WARN_ON(hba->active_uic_cmd);
1979
1980 hba->active_uic_cmd = uic_cmd;
1981
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301982 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301983 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1984 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1985 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301986
1987 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301988 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301989 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301990}
1991
1992/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301993 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1994 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08001995 * @uic_cmd: UIC command
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301996 *
1997 * Must be called with mutex held.
1998 * Returns 0 only if success.
1999 */
2000static int
2001ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
2002{
2003 int ret;
2004 unsigned long flags;
2005
2006 if (wait_for_completion_timeout(&uic_cmd->done,
2007 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
2008 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
2009 else
2010 ret = -ETIMEDOUT;
2011
2012 spin_lock_irqsave(hba->host->host_lock, flags);
2013 hba->active_uic_cmd = NULL;
2014 spin_unlock_irqrestore(hba->host->host_lock, flags);
2015
2016 return ret;
2017}
2018
2019/**
2020 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2021 * @hba: per adapter instance
2022 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002023 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302024 *
2025 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002026 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302027 * Returns 0 only if success.
2028 */
2029static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002030__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
2031 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302032{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302033 if (!ufshcd_ready_for_uic_cmd(hba)) {
2034 dev_err(hba->dev,
2035 "Controller not ready to accept UIC commands\n");
2036 return -EIO;
2037 }
2038
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002039 if (completion)
2040 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302041
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302042 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302043
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002044 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302045}
2046
2047/**
2048 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2049 * @hba: per adapter instance
2050 * @uic_cmd: UIC command
2051 *
2052 * Returns 0 only if success.
2053 */
Avri Altmane77044c52018-10-07 17:30:39 +03002054int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302055{
2056 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002057 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302058
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002059 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302060 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002061 ufshcd_add_delay_before_dme_cmd(hba);
2062
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002063 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002064 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002065 spin_unlock_irqrestore(hba->host->host_lock, flags);
2066 if (!ret)
2067 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
2068
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302069 mutex_unlock(&hba->uic_cmd_mutex);
2070
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002071 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302072 return ret;
2073}
2074
2075/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302076 * ufshcd_map_sg - Map scatter-gather list to prdt
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002077 * @hba: per adapter instance
2078 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302079 *
2080 * Returns 0 in case of success, non-zero value in case of failure
2081 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002082static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302083{
2084 struct ufshcd_sg_entry *prd_table;
2085 struct scatterlist *sg;
2086 struct scsi_cmnd *cmd;
2087 int sg_segments;
2088 int i;
2089
2090 cmd = lrbp->cmd;
2091 sg_segments = scsi_dma_map(cmd);
2092 if (sg_segments < 0)
2093 return sg_segments;
2094
2095 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002096 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2097 lrbp->utr_descriptor_ptr->prd_table_length =
2098 cpu_to_le16((u16)(sg_segments *
2099 sizeof(struct ufshcd_sg_entry)));
2100 else
2101 lrbp->utr_descriptor_ptr->prd_table_length =
2102 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302103
2104 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2105
2106 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2107 prd_table[i].size =
2108 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2109 prd_table[i].base_addr =
2110 cpu_to_le32(lower_32_bits(sg->dma_address));
2111 prd_table[i].upper_addr =
2112 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002113 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302114 }
2115 } else {
2116 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2117 }
2118
2119 return 0;
2120}
2121
2122/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302123 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302124 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302125 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302126 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302127static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302128{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302129 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2130
2131 if (hba->ufs_version == UFSHCI_VERSION_10) {
2132 u32 rw;
2133 rw = set & INTERRUPT_MASK_RW_VER_10;
2134 set = rw | ((set ^ intrs) & intrs);
2135 } else {
2136 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302137 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302138
2139 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2140}
2141
2142/**
2143 * ufshcd_disable_intr - disable interrupts
2144 * @hba: per adapter instance
2145 * @intrs: interrupt bits
2146 */
2147static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2148{
2149 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2150
2151 if (hba->ufs_version == UFSHCI_VERSION_10) {
2152 u32 rw;
2153 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2154 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2155 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2156
2157 } else {
2158 set &= ~intrs;
2159 }
2160
2161 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302162}
2163
2164/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302165 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2166 * descriptor according to request
2167 * @lrbp: pointer to local reference block
2168 * @upiu_flags: flags required in the header
2169 * @cmd_dir: requests data direction
2170 */
2171static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01002172 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302173{
2174 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2175 u32 data_direction;
2176 u32 dword_0;
2177
2178 if (cmd_dir == DMA_FROM_DEVICE) {
2179 data_direction = UTP_DEVICE_TO_HOST;
2180 *upiu_flags = UPIU_CMD_FLAGS_READ;
2181 } else if (cmd_dir == DMA_TO_DEVICE) {
2182 data_direction = UTP_HOST_TO_DEVICE;
2183 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2184 } else {
2185 data_direction = UTP_NO_DATA_TRANSFER;
2186 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2187 }
2188
2189 dword_0 = data_direction | (lrbp->command_type
2190 << UPIU_COMMAND_TYPE_OFFSET);
2191 if (lrbp->intr_cmd)
2192 dword_0 |= UTP_REQ_DESC_INT_CMD;
2193
2194 /* Transfer request descriptor header fields */
2195 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002196 /* dword_1 is reserved, hence it is set to 0 */
2197 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302198 /*
2199 * assigning invalid value for command status. Controller
2200 * updates OCS on command completion, with the command
2201 * status
2202 */
2203 req_desc->header.dword_2 =
2204 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002205 /* dword_3 is reserved, hence it is set to 0 */
2206 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002207
2208 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302209}
2210
2211/**
2212 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2213 * for scsi commands
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002214 * @lrbp: local reference block pointer
2215 * @upiu_flags: flags
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302216 */
2217static
2218void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2219{
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002220 struct scsi_cmnd *cmd = lrbp->cmd;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302221 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002222 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302223
2224 /* command descriptor fields */
2225 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2226 UPIU_TRANSACTION_COMMAND, upiu_flags,
2227 lrbp->lun, lrbp->task_tag);
2228 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2229 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2230
2231 /* Total EHS length and Data segment length will be zero */
2232 ucd_req_ptr->header.dword_2 = 0;
2233
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002234 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302235
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002236 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE);
Avri Altmana851b2b2018-10-07 17:30:34 +03002237 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE);
Bart Van Assche1b21b8f2019-12-24 14:02:45 -08002238 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002239
2240 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302241}
2242
Dolev Raviv68078d52013-07-30 00:35:58 +05302243/**
2244 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2245 * for query requsts
2246 * @hba: UFS hba
2247 * @lrbp: local reference block pointer
2248 * @upiu_flags: flags
2249 */
2250static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2251 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2252{
2253 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2254 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302255 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302256
2257 /* Query request header */
2258 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2259 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2260 lrbp->lun, lrbp->task_tag);
2261 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2262 0, query->request.query_func, 0, 0);
2263
Zang Leigang68612852016-08-25 17:39:19 +08002264 /* Data segment length only need for WRITE_DESC */
2265 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2266 ucd_req_ptr->header.dword_2 =
2267 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2268 else
2269 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302270
2271 /* Copy the Query Request buffer as is */
2272 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2273 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302274
2275 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002276 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
Avri Altman220d17a62018-10-07 17:30:36 +03002277 memcpy(ucd_req_ptr + 1, query->descriptor, len);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002278
Yaniv Gardi51047262016-02-01 15:02:38 +02002279 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302280}
2281
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302282static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2283{
2284 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2285
2286 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2287
2288 /* command descriptor fields */
2289 ucd_req_ptr->header.dword_0 =
2290 UPIU_HEADER_DWORD(
2291 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002292 /* clear rest of the fields of basic header */
2293 ucd_req_ptr->header.dword_1 = 0;
2294 ucd_req_ptr->header.dword_2 = 0;
2295
2296 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302297}
2298
2299/**
Joao Pinto300bb132016-05-11 12:21:27 +01002300 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2301 * for Device Management Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002302 * @hba: per adapter instance
2303 * @lrbp: pointer to local reference block
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302304 */
Joao Pinto300bb132016-05-11 12:21:27 +01002305static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302306{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302307 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302308 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302309
kehuanlin83dc7e32017-09-06 17:58:39 +08002310 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2311 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002312 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin83dc7e32017-09-06 17:58:39 +08002313 else
2314 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002315
2316 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2317 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2318 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2319 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2320 ufshcd_prepare_utp_nop_upiu(lrbp);
2321 else
2322 ret = -EINVAL;
2323
2324 return ret;
2325}
2326
2327/**
2328 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2329 * for SCSI Purposes
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002330 * @hba: per adapter instance
2331 * @lrbp: pointer to local reference block
Joao Pinto300bb132016-05-11 12:21:27 +01002332 */
2333static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2334{
2335 u32 upiu_flags;
2336 int ret = 0;
2337
kehuanlin83dc7e32017-09-06 17:58:39 +08002338 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2339 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002340 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin83dc7e32017-09-06 17:58:39 +08002341 else
2342 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002343
2344 if (likely(lrbp->cmd)) {
2345 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2346 lrbp->cmd->sc_data_direction);
2347 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2348 } else {
2349 ret = -EINVAL;
2350 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302351
2352 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302353}
2354
2355/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002356 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002357 * @upiu_wlun_id: UPIU W-LUN id
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002358 *
2359 * Returns SCSI W-LUN id
2360 */
2361static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2362{
2363 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2364}
2365
2366/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302367 * ufshcd_queuecommand - main entry point for SCSI requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002368 * @host: SCSI host pointer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302369 * @cmd: command from SCSI Midlayer
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302370 *
2371 * Returns 0 for success, non-zero in case of failure
2372 */
2373static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2374{
2375 struct ufshcd_lrb *lrbp;
2376 struct ufs_hba *hba;
2377 unsigned long flags;
2378 int tag;
2379 int err = 0;
2380
2381 hba = shost_priv(host);
2382
2383 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002384 if (!ufshcd_valid_tag(hba, tag)) {
2385 dev_err(hba->dev,
2386 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2387 __func__, tag, cmd, cmd->request);
2388 BUG();
2389 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302390
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002391 if (!down_read_trylock(&hba->clk_scaling_lock))
2392 return SCSI_MLQUEUE_HOST_BUSY;
2393
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302394 spin_lock_irqsave(hba->host->host_lock, flags);
2395 switch (hba->ufshcd_state) {
2396 case UFSHCD_STATE_OPERATIONAL:
2397 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002398 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302399 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302400 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302401 goto out_unlock;
2402 case UFSHCD_STATE_ERROR:
2403 set_host_byte(cmd, DID_ERROR);
2404 cmd->scsi_done(cmd);
2405 goto out_unlock;
2406 default:
2407 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2408 __func__, hba->ufshcd_state);
2409 set_host_byte(cmd, DID_BAD_TARGET);
2410 cmd->scsi_done(cmd);
2411 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302412 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002413
2414 /* if error handling is in progress, don't issue commands */
2415 if (ufshcd_eh_in_progress(hba)) {
2416 set_host_byte(cmd, DID_ERROR);
2417 cmd->scsi_done(cmd);
2418 goto out_unlock;
2419 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302420 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302421
Gilad Broner7fabb772017-02-03 16:56:50 -08002422 hba->req_abort_count = 0;
2423
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002424 err = ufshcd_hold(hba, true);
2425 if (err) {
2426 err = SCSI_MLQUEUE_HOST_BUSY;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002427 goto out;
2428 }
2429 WARN_ON(hba->clk_gating.state != CLKS_ON);
2430
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302431 lrbp = &hba->lrb[tag];
2432
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302433 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302434 lrbp->cmd = cmd;
Avri Altman09a5a242018-11-22 20:04:56 +02002435 lrbp->sense_bufflen = UFS_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302436 lrbp->sense_buffer = cmd->sense_buffer;
2437 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002438 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002439 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Gilad Bronere0b299e2017-02-03 16:56:40 -08002440 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302441
Joao Pinto300bb132016-05-11 12:21:27 +01002442 ufshcd_comp_scsi_upiu(hba, lrbp);
2443
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002444 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302445 if (err) {
2446 lrbp->cmd = NULL;
Can Guo17c7d352019-12-05 02:14:33 +00002447 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302448 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302449 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002450 /* Make sure descriptors are ready before ringing the doorbell */
2451 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302452
2453 /* issue command to the controller */
2454 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002455 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302456 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302457out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302458 spin_unlock_irqrestore(hba->host->host_lock, flags);
2459out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002460 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302461 return err;
2462}
2463
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302464static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2465 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2466{
2467 lrbp->cmd = NULL;
2468 lrbp->sense_bufflen = 0;
2469 lrbp->sense_buffer = NULL;
2470 lrbp->task_tag = tag;
2471 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302472 lrbp->intr_cmd = true; /* No interrupt aggregation */
2473 hba->dev_cmd.type = cmd_type;
2474
Joao Pinto300bb132016-05-11 12:21:27 +01002475 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302476}
2477
2478static int
2479ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2480{
2481 int err = 0;
2482 unsigned long flags;
2483 u32 mask = 1 << tag;
2484
2485 /* clear outstanding transaction before retry */
2486 spin_lock_irqsave(hba->host->host_lock, flags);
2487 ufshcd_utrl_clear(hba, tag);
2488 spin_unlock_irqrestore(hba->host->host_lock, flags);
2489
2490 /*
2491 * wait for for h/w to clear corresponding bit in door-bell.
2492 * max. wait is 1 sec.
2493 */
2494 err = ufshcd_wait_for_register(hba,
2495 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002496 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302497
2498 return err;
2499}
2500
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002501static int
2502ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2503{
2504 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2505
2506 /* Get the UPIU response */
2507 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2508 UPIU_RSP_CODE_OFFSET;
2509 return query_res->response;
2510}
2511
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302512/**
2513 * ufshcd_dev_cmd_completion() - handles device management command responses
2514 * @hba: per adapter instance
2515 * @lrbp: pointer to local reference block
2516 */
2517static int
2518ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2519{
2520 int resp;
2521 int err = 0;
2522
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002523 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302524 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2525
2526 switch (resp) {
2527 case UPIU_TRANSACTION_NOP_IN:
2528 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2529 err = -EINVAL;
2530 dev_err(hba->dev, "%s: unexpected response %x\n",
2531 __func__, resp);
2532 }
2533 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302534 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002535 err = ufshcd_check_query_response(hba, lrbp);
2536 if (!err)
2537 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302538 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302539 case UPIU_TRANSACTION_REJECT_UPIU:
2540 /* TODO: handle Reject UPIU Response */
2541 err = -EPERM;
2542 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2543 __func__);
2544 break;
2545 default:
2546 err = -EINVAL;
2547 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2548 __func__, resp);
2549 break;
2550 }
2551
2552 return err;
2553}
2554
2555static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2556 struct ufshcd_lrb *lrbp, int max_timeout)
2557{
2558 int err = 0;
2559 unsigned long time_left;
2560 unsigned long flags;
2561
2562 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2563 msecs_to_jiffies(max_timeout));
2564
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002565 /* Make sure descriptors are ready before ringing the doorbell */
2566 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302567 spin_lock_irqsave(hba->host->host_lock, flags);
2568 hba->dev_cmd.complete = NULL;
2569 if (likely(time_left)) {
2570 err = ufshcd_get_tr_ocs(lrbp);
2571 if (!err)
2572 err = ufshcd_dev_cmd_completion(hba, lrbp);
2573 }
2574 spin_unlock_irqrestore(hba->host->host_lock, flags);
2575
2576 if (!time_left) {
2577 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002578 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2579 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302580 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002581 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302582 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002583 /*
2584 * in case of an error, after clearing the doorbell,
2585 * we also need to clear the outstanding_request
2586 * field in hba
2587 */
2588 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302589 }
2590
2591 return err;
2592}
2593
2594/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302595 * ufshcd_exec_dev_cmd - API for sending device management requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002596 * @hba: UFS hba
2597 * @cmd_type: specifies the type (NOP, Query...)
2598 * @timeout: time in seconds
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302599 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302600 * NOTE: Since there is only one available tag for device management commands,
2601 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302602 */
2603static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2604 enum dev_cmd_type cmd_type, int timeout)
2605{
Bart Van Assche7252a362019-12-09 10:13:08 -08002606 struct request_queue *q = hba->cmd_queue;
2607 struct request *req;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302608 struct ufshcd_lrb *lrbp;
2609 int err;
2610 int tag;
2611 struct completion wait;
2612 unsigned long flags;
2613
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002614 down_read(&hba->clk_scaling_lock);
2615
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302616 /*
2617 * Get free slot, sleep if slots are unavailable.
2618 * Even though we use wait_event() which sleeps indefinitely,
2619 * the maximum wait time is bounded by SCSI request timeout.
2620 */
Bart Van Assche7252a362019-12-09 10:13:08 -08002621 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002622 if (IS_ERR(req)) {
2623 err = PTR_ERR(req);
2624 goto out_unlock;
2625 }
Bart Van Assche7252a362019-12-09 10:13:08 -08002626 tag = req->tag;
2627 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302628
2629 init_completion(&wait);
2630 lrbp = &hba->lrb[tag];
2631 WARN_ON(lrbp->cmd);
2632 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2633 if (unlikely(err))
2634 goto out_put_tag;
2635
2636 hba->dev_cmd.complete = &wait;
2637
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002638 ufshcd_add_query_upiu_trace(hba, tag, "query_send");
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002639 /* Make sure descriptors are ready before ringing the doorbell */
2640 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302641 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002642 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302643 ufshcd_send_command(hba, tag);
2644 spin_unlock_irqrestore(hba->host->host_lock, flags);
2645
2646 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2647
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03002648 ufshcd_add_query_upiu_trace(hba, tag,
2649 err ? "query_complete_err" : "query_complete");
2650
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302651out_put_tag:
Bart Van Assche7252a362019-12-09 10:13:08 -08002652 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03002653out_unlock:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002654 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302655 return err;
2656}
2657
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302658/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002659 * ufshcd_init_query() - init the query response and request parameters
2660 * @hba: per-adapter instance
2661 * @request: address of the request pointer to be initialized
2662 * @response: address of the response pointer to be initialized
2663 * @opcode: operation to perform
2664 * @idn: flag idn to access
2665 * @index: LU number to access
2666 * @selector: query/flag/descriptor further identification
2667 */
2668static inline void ufshcd_init_query(struct ufs_hba *hba,
2669 struct ufs_query_req **request, struct ufs_query_res **response,
2670 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2671{
2672 *request = &hba->dev_cmd.query.request;
2673 *response = &hba->dev_cmd.query.response;
2674 memset(*request, 0, sizeof(struct ufs_query_req));
2675 memset(*response, 0, sizeof(struct ufs_query_res));
2676 (*request)->upiu_req.opcode = opcode;
2677 (*request)->upiu_req.idn = idn;
2678 (*request)->upiu_req.index = index;
2679 (*request)->upiu_req.selector = selector;
2680}
2681
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002682static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2683 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2684{
2685 int ret;
2686 int retries;
2687
2688 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2689 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
2690 if (ret)
2691 dev_dbg(hba->dev,
2692 "%s: failed with error %d, retries %d\n",
2693 __func__, ret, retries);
2694 else
2695 break;
2696 }
2697
2698 if (ret)
2699 dev_err(hba->dev,
2700 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2701 __func__, opcode, idn, ret, retries);
2702 return ret;
2703}
2704
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002705/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302706 * ufshcd_query_flag() - API function for sending flag query requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002707 * @hba: per-adapter instance
2708 * @opcode: flag query to perform
2709 * @idn: flag idn to access
2710 * @flag_res: the flag value after the query request completes
Dolev Raviv68078d52013-07-30 00:35:58 +05302711 *
2712 * Returns 0 for success, non-zero in case of failure
2713 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002714int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05302715 enum flag_idn idn, bool *flag_res)
2716{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002717 struct ufs_query_req *request = NULL;
2718 struct ufs_query_res *response = NULL;
2719 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002720 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302721
2722 BUG_ON(!hba);
2723
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002724 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302725 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002726 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2727 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302728
2729 switch (opcode) {
2730 case UPIU_QUERY_OPCODE_SET_FLAG:
2731 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2732 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2733 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2734 break;
2735 case UPIU_QUERY_OPCODE_READ_FLAG:
2736 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2737 if (!flag_res) {
2738 /* No dummy reads */
2739 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2740 __func__);
2741 err = -EINVAL;
2742 goto out_unlock;
2743 }
2744 break;
2745 default:
2746 dev_err(hba->dev,
2747 "%s: Expected query flag opcode but got = %d\n",
2748 __func__, opcode);
2749 err = -EINVAL;
2750 goto out_unlock;
2751 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302752
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002753 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302754
2755 if (err) {
2756 dev_err(hba->dev,
2757 "%s: Sending flag query for idn %d failed, err = %d\n",
2758 __func__, idn, err);
2759 goto out_unlock;
2760 }
2761
2762 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302763 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302764 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2765
2766out_unlock:
2767 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002768 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302769 return err;
2770}
2771
2772/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302773 * ufshcd_query_attr - API function for sending attribute requests
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002774 * @hba: per-adapter instance
2775 * @opcode: attribute opcode
2776 * @idn: attribute idn to access
2777 * @index: index field
2778 * @selector: selector field
2779 * @attr_val: the attribute value after the query request completes
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302780 *
2781 * Returns 0 for success, non-zero in case of failure
2782*/
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02002783int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
2784 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302785{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002786 struct ufs_query_req *request = NULL;
2787 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302788 int err;
2789
2790 BUG_ON(!hba);
2791
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002792 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302793 if (!attr_val) {
2794 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2795 __func__, opcode);
2796 err = -EINVAL;
2797 goto out;
2798 }
2799
2800 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002801 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2802 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302803
2804 switch (opcode) {
2805 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2806 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302807 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302808 break;
2809 case UPIU_QUERY_OPCODE_READ_ATTR:
2810 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2811 break;
2812 default:
2813 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2814 __func__, opcode);
2815 err = -EINVAL;
2816 goto out_unlock;
2817 }
2818
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002819 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302820
2821 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002822 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2823 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302824 goto out_unlock;
2825 }
2826
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302827 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302828
2829out_unlock:
2830 mutex_unlock(&hba->dev_cmd.lock);
2831out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002832 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302833 return err;
2834}
2835
2836/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002837 * ufshcd_query_attr_retry() - API function for sending query
2838 * attribute with retries
2839 * @hba: per-adapter instance
2840 * @opcode: attribute opcode
2841 * @idn: attribute idn to access
2842 * @index: index field
2843 * @selector: selector field
2844 * @attr_val: the attribute value after the query request
2845 * completes
2846 *
2847 * Returns 0 for success, non-zero in case of failure
2848*/
2849static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2850 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2851 u32 *attr_val)
2852{
2853 int ret = 0;
2854 u32 retries;
2855
Bart Van Assche68c9fcf2019-12-24 14:02:43 -08002856 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002857 ret = ufshcd_query_attr(hba, opcode, idn, index,
2858 selector, attr_val);
2859 if (ret)
2860 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2861 __func__, ret, retries);
2862 else
2863 break;
2864 }
2865
2866 if (ret)
2867 dev_err(hba->dev,
2868 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2869 __func__, idn, ret, QUERY_REQ_RETRIES);
2870 return ret;
2871}
2872
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002873static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002874 enum query_opcode opcode, enum desc_idn idn, u8 index,
2875 u8 selector, u8 *desc_buf, int *buf_len)
2876{
2877 struct ufs_query_req *request = NULL;
2878 struct ufs_query_res *response = NULL;
2879 int err;
2880
2881 BUG_ON(!hba);
2882
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002883 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002884 if (!desc_buf) {
2885 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2886 __func__, opcode);
2887 err = -EINVAL;
2888 goto out;
2889 }
2890
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002891 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002892 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2893 __func__, *buf_len);
2894 err = -EINVAL;
2895 goto out;
2896 }
2897
2898 mutex_lock(&hba->dev_cmd.lock);
2899 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2900 selector);
2901 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002902 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002903
2904 switch (opcode) {
2905 case UPIU_QUERY_OPCODE_WRITE_DESC:
2906 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2907 break;
2908 case UPIU_QUERY_OPCODE_READ_DESC:
2909 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2910 break;
2911 default:
2912 dev_err(hba->dev,
2913 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2914 __func__, opcode);
2915 err = -EINVAL;
2916 goto out_unlock;
2917 }
2918
2919 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2920
2921 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002922 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2923 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002924 goto out_unlock;
2925 }
2926
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002927 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002928
2929out_unlock:
Bean Huocfcbae32019-11-12 23:34:36 +01002930 hba->dev_cmd.query.descriptor = NULL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002931 mutex_unlock(&hba->dev_cmd.lock);
2932out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002933 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002934 return err;
2935}
2936
2937/**
Bart Van Assche8aa29f12018-03-01 15:07:20 -08002938 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
2939 * @hba: per-adapter instance
2940 * @opcode: attribute opcode
2941 * @idn: attribute idn to access
2942 * @index: index field
2943 * @selector: selector field
2944 * @desc_buf: the buffer that contains the descriptor
2945 * @buf_len: length parameter passed to the device
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002946 *
2947 * Returns 0 for success, non-zero in case of failure.
2948 * The buf_len parameter will contain, on return, the length parameter
2949 * received on the response.
2950 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02002951int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2952 enum query_opcode opcode,
2953 enum desc_idn idn, u8 index,
2954 u8 selector,
2955 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002956{
2957 int err;
2958 int retries;
2959
2960 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2961 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2962 selector, desc_buf, buf_len);
2963 if (!err || err == -EINVAL)
2964 break;
2965 }
2966
2967 return err;
2968}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002969
2970/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002971 * ufshcd_read_desc_length - read the specified descriptor length from header
2972 * @hba: Pointer to adapter instance
2973 * @desc_id: descriptor idn value
2974 * @desc_index: descriptor index
2975 * @desc_length: pointer to variable to read the length of descriptor
2976 *
2977 * Return 0 in case of success, non-zero otherwise
2978 */
2979static int ufshcd_read_desc_length(struct ufs_hba *hba,
2980 enum desc_idn desc_id,
2981 int desc_index,
2982 int *desc_length)
2983{
2984 int ret;
2985 u8 header[QUERY_DESC_HDR_SIZE];
2986 int header_len = QUERY_DESC_HDR_SIZE;
2987
2988 if (desc_id >= QUERY_DESC_IDN_MAX)
2989 return -EINVAL;
2990
2991 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2992 desc_id, desc_index, 0, header,
2993 &header_len);
2994
2995 if (ret) {
2996 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2997 __func__, desc_id);
2998 return ret;
2999 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
3000 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
3001 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
3002 desc_id);
3003 ret = -EINVAL;
3004 }
3005
3006 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
3007 return ret;
3008
3009}
3010
3011/**
3012 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
3013 * @hba: Pointer to adapter instance
3014 * @desc_id: descriptor idn value
3015 * @desc_len: mapped desc length (out)
3016 *
3017 * Return 0 in case of success, non-zero otherwise
3018 */
3019int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
3020 enum desc_idn desc_id, int *desc_len)
3021{
3022 switch (desc_id) {
3023 case QUERY_DESC_IDN_DEVICE:
3024 *desc_len = hba->desc_size.dev_desc;
3025 break;
3026 case QUERY_DESC_IDN_POWER:
3027 *desc_len = hba->desc_size.pwr_desc;
3028 break;
3029 case QUERY_DESC_IDN_GEOMETRY:
3030 *desc_len = hba->desc_size.geom_desc;
3031 break;
3032 case QUERY_DESC_IDN_CONFIGURATION:
3033 *desc_len = hba->desc_size.conf_desc;
3034 break;
3035 case QUERY_DESC_IDN_UNIT:
3036 *desc_len = hba->desc_size.unit_desc;
3037 break;
3038 case QUERY_DESC_IDN_INTERCONNECT:
3039 *desc_len = hba->desc_size.interc_desc;
3040 break;
3041 case QUERY_DESC_IDN_STRING:
3042 *desc_len = QUERY_DESC_MAX_SIZE;
3043 break;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02003044 case QUERY_DESC_IDN_HEALTH:
3045 *desc_len = hba->desc_size.hlth_desc;
3046 break;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003047 case QUERY_DESC_IDN_RFU_0:
3048 case QUERY_DESC_IDN_RFU_1:
3049 *desc_len = 0;
3050 break;
3051 default:
3052 *desc_len = 0;
3053 return -EINVAL;
3054 }
3055 return 0;
3056}
3057EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
3058
3059/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003060 * ufshcd_read_desc_param - read the specified descriptor parameter
3061 * @hba: Pointer to adapter instance
3062 * @desc_id: descriptor idn value
3063 * @desc_index: descriptor index
3064 * @param_offset: offset of the parameter to read
3065 * @param_read_buf: pointer to buffer where parameter would be read
3066 * @param_size: sizeof(param_read_buf)
3067 *
3068 * Return 0 in case of success, non-zero otherwise
3069 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02003070int ufshcd_read_desc_param(struct ufs_hba *hba,
3071 enum desc_idn desc_id,
3072 int desc_index,
3073 u8 param_offset,
3074 u8 *param_read_buf,
3075 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003076{
3077 int ret;
3078 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003079 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003080 bool is_kmalloc = true;
3081
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003082 /* Safety check */
3083 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003084 return -EINVAL;
3085
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003086 /* Get the max length of descriptor from structure filled up at probe
3087 * time.
3088 */
3089 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003090
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003091 /* Sanity checks */
3092 if (ret || !buff_len) {
3093 dev_err(hba->dev, "%s: Failed to get full descriptor length",
3094 __func__);
3095 return ret;
3096 }
3097
3098 /* Check whether we need temp memory */
3099 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003100 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3101 if (!desc_buf)
3102 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003103 } else {
3104 desc_buf = param_read_buf;
3105 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003106 }
3107
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003108 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003109 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003110 desc_id, desc_index, 0,
3111 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003112
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003113 if (ret) {
3114 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3115 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003116 goto out;
3117 }
3118
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003119 /* Sanity check */
3120 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3121 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3122 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3123 ret = -EINVAL;
3124 goto out;
3125 }
3126
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003127 /* Check wherher we will not copy more data, than available */
3128 if (is_kmalloc && param_size > buff_len)
3129 param_size = buff_len;
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003130
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003131 if (is_kmalloc)
3132 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3133out:
3134 if (is_kmalloc)
3135 kfree(desc_buf);
3136 return ret;
3137}
3138
3139static inline int ufshcd_read_desc(struct ufs_hba *hba,
3140 enum desc_idn desc_id,
3141 int desc_index,
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003142 void *buf,
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003143 u32 size)
3144{
3145 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3146}
3147
Yaniv Gardib573d482016-03-10 17:37:09 +02003148
3149/**
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003150 * struct uc_string_id - unicode string
3151 *
3152 * @len: size of this descriptor inclusive
3153 * @type: descriptor type
3154 * @uc: unicode string character
3155 */
3156struct uc_string_id {
3157 u8 len;
3158 u8 type;
3159 wchar_t uc[0];
3160} __packed;
3161
3162/* replace non-printable or non-ASCII characters with spaces */
3163static inline char ufshcd_remove_non_printable(u8 ch)
3164{
3165 return (ch >= 0x20 && ch <= 0x7e) ? ch : ' ';
3166}
3167
3168/**
Yaniv Gardib573d482016-03-10 17:37:09 +02003169 * ufshcd_read_string_desc - read string descriptor
3170 * @hba: pointer to adapter instance
3171 * @desc_index: descriptor index
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003172 * @buf: pointer to buffer where descriptor would be read,
3173 * the caller should free the memory.
Yaniv Gardib573d482016-03-10 17:37:09 +02003174 * @ascii: if true convert from unicode to ascii characters
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003175 * null terminated string.
Yaniv Gardib573d482016-03-10 17:37:09 +02003176 *
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003177 * Return:
3178 * * string size on success.
3179 * * -ENOMEM: on allocation failure
3180 * * -EINVAL: on a wrong parameter
Yaniv Gardib573d482016-03-10 17:37:09 +02003181 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003182int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,
3183 u8 **buf, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003184{
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003185 struct uc_string_id *uc_str;
3186 u8 *str;
3187 int ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003188
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003189 if (!buf)
3190 return -EINVAL;
Yaniv Gardib573d482016-03-10 17:37:09 +02003191
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003192 uc_str = kzalloc(QUERY_DESC_MAX_SIZE, GFP_KERNEL);
3193 if (!uc_str)
3194 return -ENOMEM;
3195
3196 ret = ufshcd_read_desc(hba, QUERY_DESC_IDN_STRING,
3197 desc_index, uc_str,
3198 QUERY_DESC_MAX_SIZE);
3199 if (ret < 0) {
3200 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n",
3201 QUERY_REQ_RETRIES, ret);
3202 str = NULL;
3203 goto out;
3204 }
3205
3206 if (uc_str->len <= QUERY_DESC_HDR_SIZE) {
3207 dev_dbg(hba->dev, "String Desc is of zero length\n");
3208 str = NULL;
3209 ret = 0;
Yaniv Gardib573d482016-03-10 17:37:09 +02003210 goto out;
3211 }
3212
3213 if (ascii) {
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003214 ssize_t ascii_len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003215 int i;
Yaniv Gardib573d482016-03-10 17:37:09 +02003216 /* remove header and divide by 2 to move from UTF16 to UTF8 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003217 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3218 str = kzalloc(ascii_len, GFP_KERNEL);
3219 if (!str) {
3220 ret = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003221 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003222 }
3223
3224 /*
3225 * the descriptor contains string in UTF16 format
3226 * we need to convert to utf-8 so it can be displayed
3227 */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003228 ret = utf16s_to_utf8s(uc_str->uc,
3229 uc_str->len - QUERY_DESC_HDR_SIZE,
3230 UTF16_BIG_ENDIAN, str, ascii_len);
Yaniv Gardib573d482016-03-10 17:37:09 +02003231
3232 /* replace non-printable or non-ASCII characters with spaces */
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003233 for (i = 0; i < ret; i++)
3234 str[i] = ufshcd_remove_non_printable(str[i]);
Yaniv Gardib573d482016-03-10 17:37:09 +02003235
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003236 str[ret++] = '\0';
3237
3238 } else {
YueHaibing5f577042019-08-31 12:44:24 +00003239 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003240 if (!str) {
3241 ret = -ENOMEM;
3242 goto out;
3243 }
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003244 ret = uc_str->len;
Yaniv Gardib573d482016-03-10 17:37:09 +02003245 }
3246out:
Tomas Winkler4b828fe2019-07-30 08:55:17 +03003247 *buf = str;
3248 kfree(uc_str);
3249 return ret;
Yaniv Gardib573d482016-03-10 17:37:09 +02003250}
Yaniv Gardib573d482016-03-10 17:37:09 +02003251
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003252/**
3253 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3254 * @hba: Pointer to adapter instance
3255 * @lun: lun id
3256 * @param_offset: offset of the parameter to read
3257 * @param_read_buf: pointer to buffer where parameter would be read
3258 * @param_size: sizeof(param_read_buf)
3259 *
3260 * Return 0 in case of success, non-zero otherwise
3261 */
3262static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3263 int lun,
3264 enum unit_desc_param param_offset,
3265 u8 *param_read_buf,
3266 u32 param_size)
3267{
3268 /*
3269 * Unit descriptors are only available for general purpose LUs (LUN id
3270 * from 0 to 7) and RPMB Well known LU.
3271 */
Bean Huo1baa8012020-01-20 14:08:20 +01003272 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003273 return -EOPNOTSUPP;
3274
3275 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3276 param_offset, param_read_buf, param_size);
3277}
3278
3279/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303280 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3281 * @hba: per adapter instance
3282 *
3283 * 1. Allocate DMA memory for Command Descriptor array
3284 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3285 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3286 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3287 * (UTMRDL)
3288 * 4. Allocate memory for local reference block(lrb).
3289 *
3290 * Returns 0 for success, non-zero in case of failure
3291 */
3292static int ufshcd_memory_alloc(struct ufs_hba *hba)
3293{
3294 size_t utmrdl_size, utrdl_size, ucdl_size;
3295
3296 /* Allocate memory for UTP command descriptors */
3297 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003298 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3299 ucdl_size,
3300 &hba->ucdl_dma_addr,
3301 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303302
3303 /*
3304 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3305 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3306 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3307 * be aligned to 128 bytes as well
3308 */
3309 if (!hba->ucdl_base_addr ||
3310 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303311 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303312 "Command Descriptor Memory allocation failed\n");
3313 goto out;
3314 }
3315
3316 /*
3317 * Allocate memory for UTP Transfer descriptors
3318 * UFSHCI requires 1024 byte alignment of UTRD
3319 */
3320 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003321 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3322 utrdl_size,
3323 &hba->utrdl_dma_addr,
3324 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303325 if (!hba->utrdl_base_addr ||
3326 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303327 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303328 "Transfer Descriptor Memory allocation failed\n");
3329 goto out;
3330 }
3331
3332 /*
3333 * Allocate memory for UTP Task Management descriptors
3334 * UFSHCI requires 1024 byte alignment of UTMRD
3335 */
3336 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003337 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3338 utmrdl_size,
3339 &hba->utmrdl_dma_addr,
3340 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303341 if (!hba->utmrdl_base_addr ||
3342 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303343 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303344 "Task Management Descriptor Memory allocation failed\n");
3345 goto out;
3346 }
3347
3348 /* Allocate memory for local reference block */
Kees Cooka86854d2018-06-12 14:07:58 -07003349 hba->lrb = devm_kcalloc(hba->dev,
3350 hba->nutrs, sizeof(struct ufshcd_lrb),
Seungwon Jeon2953f852013-06-27 13:31:54 +09003351 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303352 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303353 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303354 goto out;
3355 }
3356 return 0;
3357out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303358 return -ENOMEM;
3359}
3360
3361/**
3362 * ufshcd_host_memory_configure - configure local reference block with
3363 * memory offsets
3364 * @hba: per adapter instance
3365 *
3366 * Configure Host memory space
3367 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3368 * address.
3369 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3370 * and PRDT offset.
3371 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3372 * into local reference block.
3373 */
3374static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3375{
3376 struct utp_transfer_cmd_desc *cmd_descp;
3377 struct utp_transfer_req_desc *utrdlp;
3378 dma_addr_t cmd_desc_dma_addr;
3379 dma_addr_t cmd_desc_element_addr;
3380 u16 response_offset;
3381 u16 prdt_offset;
3382 int cmd_desc_size;
3383 int i;
3384
3385 utrdlp = hba->utrdl_base_addr;
3386 cmd_descp = hba->ucdl_base_addr;
3387
3388 response_offset =
3389 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3390 prdt_offset =
3391 offsetof(struct utp_transfer_cmd_desc, prd_table);
3392
3393 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3394 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3395
3396 for (i = 0; i < hba->nutrs; i++) {
3397 /* Configure UTRD with command descriptor base address */
3398 cmd_desc_element_addr =
3399 (cmd_desc_dma_addr + (cmd_desc_size * i));
3400 utrdlp[i].command_desc_base_addr_lo =
3401 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3402 utrdlp[i].command_desc_base_addr_hi =
3403 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3404
3405 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003406 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3407 utrdlp[i].response_upiu_offset =
3408 cpu_to_le16(response_offset);
3409 utrdlp[i].prd_table_offset =
3410 cpu_to_le16(prdt_offset);
3411 utrdlp[i].response_upiu_length =
3412 cpu_to_le16(ALIGNED_UPIU_SIZE);
3413 } else {
3414 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303415 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003416 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303417 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003418 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303419 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003420 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303421
3422 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003423 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3424 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303425 hba->lrb[i].ucd_req_ptr =
3426 (struct utp_upiu_req *)(cmd_descp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003427 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303428 hba->lrb[i].ucd_rsp_ptr =
3429 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003430 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3431 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303432 hba->lrb[i].ucd_prdt_ptr =
3433 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003434 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3435 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303436 }
3437}
3438
3439/**
3440 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3441 * @hba: per adapter instance
3442 *
3443 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3444 * in order to initialize the Unipro link startup procedure.
3445 * Once the Unipro links are up, the device connected to the controller
3446 * is detected.
3447 *
3448 * Returns 0 on success, non-zero value on failure
3449 */
3450static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3451{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303452 struct uic_command uic_cmd = {0};
3453 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303454
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303455 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3456
3457 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3458 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003459 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303460 "dme-link-startup: error code %d\n", ret);
3461 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303462}
Alim Akhtar4404c5d2018-05-06 15:44:17 +05303463/**
3464 * ufshcd_dme_reset - UIC command for DME_RESET
3465 * @hba: per adapter instance
3466 *
3467 * DME_RESET command is issued in order to reset UniPro stack.
3468 * This function now deal with cold reset.
3469 *
3470 * Returns 0 on success, non-zero value on failure
3471 */
3472static int ufshcd_dme_reset(struct ufs_hba *hba)
3473{
3474 struct uic_command uic_cmd = {0};
3475 int ret;
3476
3477 uic_cmd.command = UIC_CMD_DME_RESET;
3478
3479 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3480 if (ret)
3481 dev_err(hba->dev,
3482 "dme-reset: error code %d\n", ret);
3483
3484 return ret;
3485}
3486
3487/**
3488 * ufshcd_dme_enable - UIC command for DME_ENABLE
3489 * @hba: per adapter instance
3490 *
3491 * DME_ENABLE command is issued in order to enable UniPro stack.
3492 *
3493 * Returns 0 on success, non-zero value on failure
3494 */
3495static int ufshcd_dme_enable(struct ufs_hba *hba)
3496{
3497 struct uic_command uic_cmd = {0};
3498 int ret;
3499
3500 uic_cmd.command = UIC_CMD_DME_ENABLE;
3501
3502 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3503 if (ret)
3504 dev_err(hba->dev,
3505 "dme-reset: error code %d\n", ret);
3506
3507 return ret;
3508}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303509
Yaniv Gardicad2e032015-03-31 17:37:14 +03003510static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3511{
3512 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3513 unsigned long min_sleep_time_us;
3514
3515 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3516 return;
3517
3518 /*
3519 * last_dme_cmd_tstamp will be 0 only for 1st call to
3520 * this function
3521 */
3522 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3523 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3524 } else {
3525 unsigned long delta =
3526 (unsigned long) ktime_to_us(
3527 ktime_sub(ktime_get(),
3528 hba->last_dme_cmd_tstamp));
3529
3530 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3531 min_sleep_time_us =
3532 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3533 else
3534 return; /* no more delay required */
3535 }
3536
3537 /* allow sleep for extra 50us if needed */
3538 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3539}
3540
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303541/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303542 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3543 * @hba: per adapter instance
3544 * @attr_sel: uic command argument1
3545 * @attr_set: attribute set type as uic command argument2
3546 * @mib_val: setting value as uic command argument3
3547 * @peer: indicate whether peer or local
3548 *
3549 * Returns 0 on success, non-zero value on failure
3550 */
3551int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3552 u8 attr_set, u32 mib_val, u8 peer)
3553{
3554 struct uic_command uic_cmd = {0};
3555 static const char *const action[] = {
3556 "dme-set",
3557 "dme-peer-set"
3558 };
3559 const char *set = action[!!peer];
3560 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003561 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303562
3563 uic_cmd.command = peer ?
3564 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3565 uic_cmd.argument1 = attr_sel;
3566 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3567 uic_cmd.argument3 = mib_val;
3568
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003569 do {
3570 /* for peer attributes we retry upon failure */
3571 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3572 if (ret)
3573 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3574 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3575 } while (ret && peer && --retries);
3576
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003577 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003578 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003579 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3580 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303581
3582 return ret;
3583}
3584EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3585
3586/**
3587 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3588 * @hba: per adapter instance
3589 * @attr_sel: uic command argument1
3590 * @mib_val: the value of the attribute as returned by the UIC command
3591 * @peer: indicate whether peer or local
3592 *
3593 * Returns 0 on success, non-zero value on failure
3594 */
3595int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3596 u32 *mib_val, u8 peer)
3597{
3598 struct uic_command uic_cmd = {0};
3599 static const char *const action[] = {
3600 "dme-get",
3601 "dme-peer-get"
3602 };
3603 const char *get = action[!!peer];
3604 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003605 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003606 struct ufs_pa_layer_attr orig_pwr_info;
3607 struct ufs_pa_layer_attr temp_pwr_info;
3608 bool pwr_mode_change = false;
3609
3610 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3611 orig_pwr_info = hba->pwr_info;
3612 temp_pwr_info = orig_pwr_info;
3613
3614 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3615 orig_pwr_info.pwr_rx == FAST_MODE) {
3616 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3617 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3618 pwr_mode_change = true;
3619 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3620 orig_pwr_info.pwr_rx == SLOW_MODE) {
3621 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3622 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3623 pwr_mode_change = true;
3624 }
3625 if (pwr_mode_change) {
3626 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3627 if (ret)
3628 goto out;
3629 }
3630 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303631
3632 uic_cmd.command = peer ?
3633 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3634 uic_cmd.argument1 = attr_sel;
3635
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003636 do {
3637 /* for peer attributes we retry upon failure */
3638 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3639 if (ret)
3640 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3641 get, UIC_GET_ATTR_ID(attr_sel), ret);
3642 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303643
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003644 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003645 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003646 get, UIC_GET_ATTR_ID(attr_sel),
3647 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003648
3649 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303650 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003651
3652 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3653 && pwr_mode_change)
3654 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303655out:
3656 return ret;
3657}
3658EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3659
3660/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003661 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3662 * state) and waits for it to take effect.
3663 *
3664 * @hba: per adapter instance
3665 * @cmd: UIC command to execute
3666 *
3667 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3668 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3669 * and device UniPro link and hence it's final completion would be indicated by
3670 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3671 * addition to normal UIC command completion Status (UCCS). This function only
3672 * returns after the relevant status bits indicate the completion.
3673 *
3674 * Returns 0 on success, non-zero value on failure
3675 */
3676static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3677{
3678 struct completion uic_async_done;
3679 unsigned long flags;
3680 u8 status;
3681 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003682 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003683
3684 mutex_lock(&hba->uic_cmd_mutex);
3685 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003686 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003687
3688 spin_lock_irqsave(hba->host->host_lock, flags);
3689 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003690 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3691 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3692 /*
3693 * Make sure UIC command completion interrupt is disabled before
3694 * issuing UIC command.
3695 */
3696 wmb();
3697 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003698 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003699 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3700 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003701 if (ret) {
3702 dev_err(hba->dev,
3703 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3704 cmd->command, cmd->argument3, ret);
3705 goto out;
3706 }
3707
3708 if (!wait_for_completion_timeout(hba->uic_async_done,
3709 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3710 dev_err(hba->dev,
3711 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3712 cmd->command, cmd->argument3);
3713 ret = -ETIMEDOUT;
3714 goto out;
3715 }
3716
3717 status = ufshcd_get_upmcrs(hba);
3718 if (status != PWR_LOCAL) {
3719 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08003720 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003721 cmd->command, status);
3722 ret = (status != PWR_OK) ? status : -1;
3723 }
3724out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003725 if (ret) {
3726 ufshcd_print_host_state(hba);
3727 ufshcd_print_pwr_info(hba);
3728 ufshcd_print_host_regs(hba);
3729 }
3730
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003731 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003732 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003733 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003734 if (reenable_intr)
3735 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003736 spin_unlock_irqrestore(hba->host->host_lock, flags);
3737 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003738
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003739 return ret;
3740}
3741
3742/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303743 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3744 * using DME_SET primitives.
3745 * @hba: per adapter instance
3746 * @mode: powr mode value
3747 *
3748 * Returns 0 on success, non-zero value on failure
3749 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303750static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303751{
3752 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003753 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303754
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003755 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3756 ret = ufshcd_dme_set(hba,
3757 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3758 if (ret) {
3759 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3760 __func__, ret);
3761 goto out;
3762 }
3763 }
3764
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303765 uic_cmd.command = UIC_CMD_DME_SET;
3766 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3767 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003768 ufshcd_hold(hba, false);
3769 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3770 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303771
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003772out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003773 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003774}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303775
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003776static int ufshcd_link_recovery(struct ufs_hba *hba)
3777{
3778 int ret;
3779 unsigned long flags;
3780
3781 spin_lock_irqsave(hba->host->host_lock, flags);
3782 hba->ufshcd_state = UFSHCD_STATE_RESET;
3783 ufshcd_set_eh_in_progress(hba);
3784 spin_unlock_irqrestore(hba->host->host_lock, flags);
3785
Can Guoebdd1df2019-11-14 22:09:24 -08003786 /* Reset the attached device */
3787 ufshcd_vops_device_reset(hba);
3788
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003789 ret = ufshcd_host_reset_and_restore(hba);
3790
3791 spin_lock_irqsave(hba->host->host_lock, flags);
3792 if (ret)
3793 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3794 ufshcd_clear_eh_in_progress(hba);
3795 spin_unlock_irqrestore(hba->host->host_lock, flags);
3796
3797 if (ret)
3798 dev_err(hba->dev, "%s: link recovery failed, err %d",
3799 __func__, ret);
3800
3801 return ret;
3802}
3803
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003804static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003805{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003806 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003807 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003808 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003809
Kiwoong Kimee32c902016-11-10 21:17:43 +09003810 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3811
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003812 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003813 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003814 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3815 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003816
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003817 if (ret) {
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003818 int err;
3819
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003820 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3821 __func__, ret);
3822
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003823 /*
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003824 * If link recovery fails then return error code returned from
3825 * ufshcd_link_recovery().
3826 * If link recovery succeeds then return -EAGAIN to attempt
3827 * hibern8 enter retry again.
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003828 */
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003829 err = ufshcd_link_recovery(hba);
3830 if (err) {
3831 dev_err(hba->dev, "%s: link recovery failed", __func__);
3832 ret = err;
3833 } else {
3834 ret = -EAGAIN;
3835 }
Kiwoong Kimee32c902016-11-10 21:17:43 +09003836 } else
3837 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3838 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003839
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003840 return ret;
3841}
3842
3843static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3844{
3845 int ret = 0, retries;
3846
3847 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3848 ret = __ufshcd_uic_hibern8_enter(hba);
Subhash Jadavani6d303e42019-11-14 22:09:30 -08003849 if (!ret)
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003850 goto out;
3851 }
3852out:
3853 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003854}
3855
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08003856int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003857{
3858 struct uic_command uic_cmd = {0};
3859 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003860 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003861
Kiwoong Kimee32c902016-11-10 21:17:43 +09003862 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3863
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003864 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3865 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003866 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3867 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3868
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303869 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003870 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3871 __func__, ret);
3872 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003873 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003874 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3875 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003876 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3877 hba->ufs_stats.hibern8_exit_cnt++;
3878 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303879
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303880 return ret;
3881}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08003882EXPORT_SYMBOL_GPL(ufshcd_uic_hibern8_exit);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303883
Stanley Chuba7af5e2019-12-30 13:32:28 +08003884void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit)
3885{
3886 unsigned long flags;
3887
3888 if (!(hba->capabilities & MASK_AUTO_HIBERN8_SUPPORT))
3889 return;
3890
3891 spin_lock_irqsave(hba->host->host_lock, flags);
3892 if (hba->ahit == ahit)
3893 goto out_unlock;
3894 hba->ahit = ahit;
3895 if (!pm_runtime_suspended(hba->dev))
3896 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
3897out_unlock:
3898 spin_unlock_irqrestore(hba->host->host_lock, flags);
3899}
3900EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
3901
Can Guo71d848b2019-11-14 22:09:26 -08003902void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
Adrian Hunterad448372018-03-20 15:07:38 +02003903{
3904 unsigned long flags;
3905
Stanley Chuee5f1042019-05-21 14:44:52 +08003906 if (!ufshcd_is_auto_hibern8_supported(hba) || !hba->ahit)
Adrian Hunterad448372018-03-20 15:07:38 +02003907 return;
3908
3909 spin_lock_irqsave(hba->host->host_lock, flags);
3910 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
3911 spin_unlock_irqrestore(hba->host->host_lock, flags);
3912}
3913
Yaniv Gardi50646362014-10-23 13:25:13 +03003914 /**
3915 * ufshcd_init_pwr_info - setting the POR (power on reset)
3916 * values in hba power info
3917 * @hba: per-adapter instance
3918 */
3919static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3920{
3921 hba->pwr_info.gear_rx = UFS_PWM_G1;
3922 hba->pwr_info.gear_tx = UFS_PWM_G1;
3923 hba->pwr_info.lane_rx = 1;
3924 hba->pwr_info.lane_tx = 1;
3925 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3926 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3927 hba->pwr_info.hs_rate = 0;
3928}
3929
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303930/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003931 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3932 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303933 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003934static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303935{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003936 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3937
3938 if (hba->max_pwr_info.is_valid)
3939 return 0;
3940
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003941 pwr_info->pwr_tx = FAST_MODE;
3942 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003943 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303944
3945 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003946 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3947 &pwr_info->lane_rx);
3948 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3949 &pwr_info->lane_tx);
3950
3951 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3952 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3953 __func__,
3954 pwr_info->lane_rx,
3955 pwr_info->lane_tx);
3956 return -EINVAL;
3957 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303958
3959 /*
3960 * First, get the maximum gears of HS speed.
3961 * If a zero value, it means there is no HSGEAR capability.
3962 * Then, get the maximum gears of PWM speed.
3963 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003964 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3965 if (!pwr_info->gear_rx) {
3966 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3967 &pwr_info->gear_rx);
3968 if (!pwr_info->gear_rx) {
3969 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3970 __func__, pwr_info->gear_rx);
3971 return -EINVAL;
3972 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003973 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303974 }
3975
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003976 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3977 &pwr_info->gear_tx);
3978 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303979 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003980 &pwr_info->gear_tx);
3981 if (!pwr_info->gear_tx) {
3982 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3983 __func__, pwr_info->gear_tx);
3984 return -EINVAL;
3985 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003986 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003987 }
3988
3989 hba->max_pwr_info.is_valid = true;
3990 return 0;
3991}
3992
3993static int ufshcd_change_power_mode(struct ufs_hba *hba,
3994 struct ufs_pa_layer_attr *pwr_mode)
3995{
3996 int ret;
3997
3998 /* if already configured to the requested pwr_mode */
3999 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4000 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4001 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4002 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4003 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4004 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4005 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4006 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4007 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304008 }
4009
4010 /*
4011 * Configure attributes for power mode change with below.
4012 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4013 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4014 * - PA_HSSERIES
4015 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004016 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4017 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4018 pwr_mode->lane_rx);
4019 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4020 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304021 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004022 else
4023 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304024
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004025 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4026 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4027 pwr_mode->lane_tx);
4028 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4029 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304030 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004031 else
4032 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304033
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004034 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4035 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4036 pwr_mode->pwr_rx == FAST_MODE ||
4037 pwr_mode->pwr_tx == FAST_MODE)
4038 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4039 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304040
Can Guo08342532019-12-05 02:14:42 +00004041 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4042 DL_FC0ProtectionTimeOutVal_Default);
4043 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4044 DL_TC0ReplayTimeOutVal_Default);
4045 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4046 DL_AFC0ReqTimeOutVal_Default);
4047 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4048 DL_FC1ProtectionTimeOutVal_Default);
4049 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4050 DL_TC1ReplayTimeOutVal_Default);
4051 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4052 DL_AFC1ReqTimeOutVal_Default);
4053
4054 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4055 DL_FC0ProtectionTimeOutVal_Default);
4056 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4057 DL_TC0ReplayTimeOutVal_Default);
4058 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4059 DL_AFC0ReqTimeOutVal_Default);
4060
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004061 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4062 | pwr_mode->pwr_tx);
4063
4064 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304065 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004066 "%s: power mode change failed %d\n", __func__, ret);
4067 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004068 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4069 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004070
4071 memcpy(&hba->pwr_info, pwr_mode,
4072 sizeof(struct ufs_pa_layer_attr));
4073 }
4074
4075 return ret;
4076}
4077
4078/**
4079 * ufshcd_config_pwr_mode - configure a new power mode
4080 * @hba: per-adapter instance
4081 * @desired_pwr_mode: desired power configuration
4082 */
Alim Akhtar0d846e72018-05-06 15:44:18 +05304083int ufshcd_config_pwr_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004084 struct ufs_pa_layer_attr *desired_pwr_mode)
4085{
4086 struct ufs_pa_layer_attr final_params = { 0 };
4087 int ret;
4088
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004089 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4090 desired_pwr_mode, &final_params);
4091
4092 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004093 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4094
4095 ret = ufshcd_change_power_mode(hba, &final_params);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08004096 if (!ret)
4097 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304098
4099 return ret;
4100}
Alim Akhtar0d846e72018-05-06 15:44:18 +05304101EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304102
4103/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304104 * ufshcd_complete_dev_init() - checks device readiness
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004105 * @hba: per-adapter instance
Dolev Raviv68078d52013-07-30 00:35:58 +05304106 *
4107 * Set fDeviceInit flag and poll until device toggles it.
4108 */
4109static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4110{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004111 int i;
4112 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05304113 bool flag_res = 1;
4114
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004115 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4116 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304117 if (err) {
4118 dev_err(hba->dev,
4119 "%s setting fDeviceInit flag failed with error %d\n",
4120 __func__, err);
4121 goto out;
4122 }
4123
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004124 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4125 for (i = 0; i < 1000 && !err && flag_res; i++)
4126 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4127 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
4128
Dolev Raviv68078d52013-07-30 00:35:58 +05304129 if (err)
4130 dev_err(hba->dev,
4131 "%s reading fDeviceInit flag failed with error %d\n",
4132 __func__, err);
4133 else if (flag_res)
4134 dev_err(hba->dev,
4135 "%s fDeviceInit was not cleared by the device\n",
4136 __func__);
4137
4138out:
4139 return err;
4140}
4141
4142/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304143 * ufshcd_make_hba_operational - Make UFS controller operational
4144 * @hba: per adapter instance
4145 *
4146 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004147 * 1. Enable required interrupts
4148 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004149 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004150 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304151 *
4152 * Returns 0 on success, non-zero value on failure
4153 */
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004154int ufshcd_make_hba_operational(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304155{
4156 int err = 0;
4157 u32 reg;
4158
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304159 /* Enable required interrupts */
4160 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4161
4162 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004163 if (ufshcd_is_intr_aggr_allowed(hba))
4164 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4165 else
4166 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304167
4168 /* Configure UTRL and UTMRL base address registers */
4169 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4170 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4171 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4172 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4173 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4174 REG_UTP_TASK_REQ_LIST_BASE_L);
4175 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4176 REG_UTP_TASK_REQ_LIST_BASE_H);
4177
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304178 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004179 * Make sure base address and interrupt setup are updated before
4180 * enabling the run/stop registers below.
4181 */
4182 wmb();
4183
4184 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304185 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304186 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004187 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304188 if (!(ufshcd_get_lists_status(reg))) {
4189 ufshcd_enable_run_stop_reg(hba);
4190 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304191 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304192 "Host controller not ready to process requests");
4193 err = -EIO;
4194 goto out;
4195 }
4196
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304197out:
4198 return err;
4199}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004200EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304201
4202/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004203 * ufshcd_hba_stop - Send controller to reset state
4204 * @hba: per adapter instance
4205 * @can_sleep: perform sleep or just spin
4206 */
4207static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
4208{
4209 int err;
4210
4211 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4212 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4213 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4214 10, 1, can_sleep);
4215 if (err)
4216 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4217}
4218
4219/**
Alim Akhtar4404c5d2018-05-06 15:44:17 +05304220 * ufshcd_hba_execute_hce - initialize the controller
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304221 * @hba: per adapter instance
4222 *
4223 * The controller resets itself and controller firmware initialization
4224 * sequence kicks off. When controller is ready it will set
4225 * the Host Controller Enable bit to 1.
4226 *
4227 * Returns 0 on success, non-zero value on failure
4228 */
Alim Akhtar4404c5d2018-05-06 15:44:17 +05304229static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304230{
4231 int retry;
4232
Yaniv Gardi596585a2016-03-10 17:37:08 +02004233 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304234 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004235 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304236
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004237 /* UniPro link is disabled at this point */
4238 ufshcd_set_link_off(hba);
4239
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004240 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004241
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304242 /* start controller initialization sequence */
4243 ufshcd_hba_start(hba);
4244
4245 /*
4246 * To initialize a UFS host controller HCE bit must be set to 1.
4247 * During initialization the HCE bit value changes from 1->0->1.
4248 * When the host controller completes initialization sequence
4249 * it sets the value of HCE bit to 1. The same HCE bit is read back
4250 * to check if the controller has completed initialization sequence.
4251 * So without this delay the value HCE = 1, set in the previous
4252 * instruction might be read back.
4253 * This delay can be changed based on the controller.
4254 */
Bean Huo838c1ef2019-07-15 11:21:10 +00004255 usleep_range(1000, 1100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304256
4257 /* wait for the host controller to complete initialization */
4258 retry = 10;
4259 while (ufshcd_is_hba_active(hba)) {
4260 if (retry) {
4261 retry--;
4262 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304263 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304264 "Controller enable failed\n");
4265 return -EIO;
4266 }
Bean Huo838c1ef2019-07-15 11:21:10 +00004267 usleep_range(5000, 5100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304268 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004269
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004270 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004271 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004272
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004273 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004274
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304275 return 0;
4276}
4277
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004278int ufshcd_hba_enable(struct ufs_hba *hba)
Alim Akhtar4404c5d2018-05-06 15:44:17 +05304279{
4280 int ret;
4281
4282 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4283 ufshcd_set_link_off(hba);
4284 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4285
4286 /* enable UIC related interrupts */
4287 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4288 ret = ufshcd_dme_reset(hba);
4289 if (!ret) {
4290 ret = ufshcd_dme_enable(hba);
4291 if (!ret)
4292 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4293 if (ret)
4294 dev_err(hba->dev,
4295 "Host controller enable failed with non-hce\n");
4296 }
4297 } else {
4298 ret = ufshcd_hba_execute_hce(hba);
4299 }
4300
4301 return ret;
4302}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004303EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4304
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004305static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4306{
4307 int tx_lanes, i, err = 0;
4308
4309 if (!peer)
4310 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4311 &tx_lanes);
4312 else
4313 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4314 &tx_lanes);
4315 for (i = 0; i < tx_lanes; i++) {
4316 if (!peer)
4317 err = ufshcd_dme_set(hba,
4318 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4319 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4320 0);
4321 else
4322 err = ufshcd_dme_peer_set(hba,
4323 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4324 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4325 0);
4326 if (err) {
4327 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4328 __func__, peer, i, err);
4329 break;
4330 }
4331 }
4332
4333 return err;
4334}
4335
4336static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4337{
4338 return ufshcd_disable_tx_lcc(hba, true);
4339}
4340
Stanley Chua5fe372d2020-01-04 22:26:07 +08004341void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist,
4342 u32 reg)
Stanley Chu8808b4e2019-07-10 21:38:21 +08004343{
4344 reg_hist->reg[reg_hist->pos] = reg;
4345 reg_hist->tstamp[reg_hist->pos] = ktime_get();
4346 reg_hist->pos = (reg_hist->pos + 1) % UFS_ERR_REG_HIST_LENGTH;
4347}
Stanley Chua5fe372d2020-01-04 22:26:07 +08004348EXPORT_SYMBOL_GPL(ufshcd_update_reg_hist);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004349
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304350/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304351 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304352 * @hba: per adapter instance
4353 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304354 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304355 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304356static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304357{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304358 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004359 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004360 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304361
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004362 /*
4363 * If UFS device isn't active then we will have to issue link startup
4364 * 2 times to make sure the device state move to active.
4365 */
4366 if (!ufshcd_is_ufs_dev_active(hba))
4367 link_startup_again = true;
4368
4369link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004370 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004371 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304372
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004373 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004374
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004375 /* check if device is detected by inter-connect layer */
4376 if (!ret && !ufshcd_is_device_present(hba)) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08004377 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4378 0);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004379 dev_err(hba->dev, "%s: Device not present\n", __func__);
4380 ret = -ENXIO;
4381 goto out;
4382 }
4383
4384 /*
4385 * DME link lost indication is only received when link is up,
4386 * but we can't be sure if the link is up until link startup
4387 * succeeds. So reset the local Uni-Pro and try again.
4388 */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004389 if (ret && ufshcd_hba_enable(hba)) {
4390 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4391 (u32)ret);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004392 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004393 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004394 } while (ret && retries--);
4395
Stanley Chu8808b4e2019-07-10 21:38:21 +08004396 if (ret) {
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004397 /* failed to get the link up... retire */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004398 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4399 (u32)ret);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304400 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004401 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304402
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004403 if (link_startup_again) {
4404 link_startup_again = false;
4405 retries = DME_LINKSTARTUP_RETRIES;
4406 goto link_startup;
4407 }
4408
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004409 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4410 ufshcd_init_pwr_info(hba);
4411 ufshcd_print_pwr_info(hba);
4412
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004413 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4414 ret = ufshcd_disable_device_tx_lcc(hba);
4415 if (ret)
4416 goto out;
4417 }
4418
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004419 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004420 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4421 if (ret)
4422 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004423
4424 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304425out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004426 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304427 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004428 ufshcd_print_host_state(hba);
4429 ufshcd_print_pwr_info(hba);
4430 ufshcd_print_host_regs(hba);
4431 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304432 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304433}
4434
4435/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304436 * ufshcd_verify_dev_init() - Verify device initialization
4437 * @hba: per-adapter instance
4438 *
4439 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4440 * device Transport Protocol (UTP) layer is ready after a reset.
4441 * If the UTP layer at the device side is not initialized, it may
4442 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4443 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4444 */
4445static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4446{
4447 int err = 0;
4448 int retries;
4449
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004450 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304451 mutex_lock(&hba->dev_cmd.lock);
4452 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4453 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4454 NOP_OUT_TIMEOUT);
4455
4456 if (!err || err == -ETIMEDOUT)
4457 break;
4458
4459 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4460 }
4461 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004462 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304463
4464 if (err)
4465 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4466 return err;
4467}
4468
4469/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004470 * ufshcd_set_queue_depth - set lun queue depth
4471 * @sdev: pointer to SCSI device
4472 *
4473 * Read bLUQueueDepth value and activate scsi tagged command
4474 * queueing. For WLUN, queue depth is set to 1. For best-effort
4475 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4476 * value that host can queue.
4477 */
4478static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4479{
4480 int ret = 0;
4481 u8 lun_qdepth;
4482 struct ufs_hba *hba;
4483
4484 hba = shost_priv(sdev->host);
4485
4486 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004487 ret = ufshcd_read_unit_desc_param(hba,
4488 ufshcd_scsi_to_upiu_lun(sdev->lun),
4489 UNIT_DESC_PARAM_LU_Q_DEPTH,
4490 &lun_qdepth,
4491 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004492
4493 /* Some WLUN doesn't support unit descriptor */
4494 if (ret == -EOPNOTSUPP)
4495 lun_qdepth = 1;
4496 else if (!lun_qdepth)
4497 /* eventually, we can figure out the real queue depth */
4498 lun_qdepth = hba->nutrs;
4499 else
4500 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4501
4502 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4503 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004504 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004505}
4506
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004507/*
4508 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4509 * @hba: per-adapter instance
4510 * @lun: UFS device lun id
4511 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4512 *
4513 * Returns 0 in case of success and b_lu_write_protect status would be returned
4514 * @b_lu_write_protect parameter.
4515 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4516 * Returns -EINVAL in case of invalid parameters passed to this function.
4517 */
4518static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4519 u8 lun,
4520 u8 *b_lu_write_protect)
4521{
4522 int ret;
4523
4524 if (!b_lu_write_protect)
4525 ret = -EINVAL;
4526 /*
4527 * According to UFS device spec, RPMB LU can't be write
4528 * protected so skip reading bLUWriteProtect parameter for
4529 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4530 */
Bean Huo1baa8012020-01-20 14:08:20 +01004531 else if (lun >= hba->dev_info.max_lu_supported)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004532 ret = -ENOTSUPP;
4533 else
4534 ret = ufshcd_read_unit_desc_param(hba,
4535 lun,
4536 UNIT_DESC_PARAM_LU_WR_PROTECT,
4537 b_lu_write_protect,
4538 sizeof(*b_lu_write_protect));
4539 return ret;
4540}
4541
4542/**
4543 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4544 * status
4545 * @hba: per-adapter instance
4546 * @sdev: pointer to SCSI device
4547 *
4548 */
4549static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4550 struct scsi_device *sdev)
4551{
4552 if (hba->dev_info.f_power_on_wp_en &&
4553 !hba->dev_info.is_lu_power_on_wp) {
4554 u8 b_lu_write_protect;
4555
4556 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4557 &b_lu_write_protect) &&
4558 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4559 hba->dev_info.is_lu_power_on_wp = true;
4560 }
4561}
4562
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004563/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304564 * ufshcd_slave_alloc - handle initial SCSI device configurations
4565 * @sdev: pointer to SCSI device
4566 *
4567 * Returns success
4568 */
4569static int ufshcd_slave_alloc(struct scsi_device *sdev)
4570{
4571 struct ufs_hba *hba;
4572
4573 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304574
4575 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4576 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304577
Can Guoa3a76392019-12-05 02:14:30 +00004578 /* DBD field should be set to 1 in mode sense(10) */
4579 sdev->set_dbd_for_ms = 1;
4580
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304581 /* allow SCSI layer to restart the device in case of errors */
4582 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004583
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004584 /* REPORT SUPPORTED OPERATION CODES is not supported */
4585 sdev->no_report_opcodes = 1;
4586
Sujit Reddy Thumma84af7e82018-01-24 09:52:35 +05304587 /* WRITE_SAME command is not supported */
4588 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004589
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004590 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004591
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004592 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4593
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004594 return 0;
4595}
4596
4597/**
4598 * ufshcd_change_queue_depth - change queue depth
4599 * @sdev: pointer to SCSI device
4600 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004601 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004602 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004603 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004604static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004605{
4606 struct ufs_hba *hba = shost_priv(sdev->host);
4607
4608 if (depth > hba->nutrs)
4609 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004610 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304611}
4612
4613/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004614 * ufshcd_slave_configure - adjust SCSI device configurations
4615 * @sdev: pointer to SCSI device
4616 */
4617static int ufshcd_slave_configure(struct scsi_device *sdev)
4618{
Stanley Chu49615ba2019-09-16 23:56:50 +08004619 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004620 struct request_queue *q = sdev->request_queue;
4621
4622 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
Stanley Chu49615ba2019-09-16 23:56:50 +08004623
4624 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4625 sdev->rpm_autosuspend = 1;
4626
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004627 return 0;
4628}
4629
4630/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304631 * ufshcd_slave_destroy - remove SCSI device configurations
4632 * @sdev: pointer to SCSI device
4633 */
4634static void ufshcd_slave_destroy(struct scsi_device *sdev)
4635{
4636 struct ufs_hba *hba;
4637
4638 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004639 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004640 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4641 unsigned long flags;
4642
4643 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004644 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004645 spin_unlock_irqrestore(hba->host->host_lock, flags);
4646 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304647}
4648
4649/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304650 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004651 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304652 * @scsi_status: SCSI command status
4653 *
4654 * Returns value base on SCSI command status
4655 */
4656static inline int
4657ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4658{
4659 int result = 0;
4660
4661 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304662 case SAM_STAT_CHECK_CONDITION:
4663 ufshcd_copy_sense_data(lrbp);
Tomas Winkler30eb2e42018-11-26 10:10:34 +02004664 /* fallthrough */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304665 case SAM_STAT_GOOD:
4666 result |= DID_OK << 16 |
4667 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304668 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304669 break;
4670 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304671 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304672 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304673 ufshcd_copy_sense_data(lrbp);
4674 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304675 break;
4676 default:
4677 result |= DID_ERROR << 16;
4678 break;
4679 } /* end of switch */
4680
4681 return result;
4682}
4683
4684/**
4685 * ufshcd_transfer_rsp_status - Get overall status of the response
4686 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004687 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304688 *
4689 * Returns result of the command to notify SCSI midlayer
4690 */
4691static inline int
4692ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4693{
4694 int result = 0;
4695 int scsi_status;
4696 int ocs;
4697
4698 /* overall command status of utrd */
4699 ocs = ufshcd_get_tr_ocs(lrbp);
4700
4701 switch (ocs) {
4702 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304703 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004704 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304705 switch (result) {
4706 case UPIU_TRANSACTION_RESPONSE:
4707 /*
4708 * get the response UPIU result to extract
4709 * the SCSI command status
4710 */
4711 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4712
4713 /*
4714 * get the result based on SCSI status response
4715 * to notify the SCSI midlayer of the command status
4716 */
4717 scsi_status = result & MASK_SCSI_STATUS;
4718 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304719
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004720 /*
4721 * Currently we are only supporting BKOPs exception
4722 * events hence we can ignore BKOPs exception event
4723 * during power management callbacks. BKOPs exception
4724 * event is not expected to be raised in runtime suspend
4725 * callback as it allows the urgent bkops.
4726 * During system suspend, we are anyway forcefully
4727 * disabling the bkops and if urgent bkops is needed
4728 * it will be enabled on system resume. Long term
4729 * solution could be to abort the system suspend if
4730 * UFS device needs urgent BKOPs.
4731 */
4732 if (!hba->pm_op_in_progress &&
4733 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304734 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304735 break;
4736 case UPIU_TRANSACTION_REJECT_UPIU:
4737 /* TODO: handle Reject UPIU Response */
4738 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304739 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304740 "Reject UPIU not fully implemented\n");
4741 break;
4742 default:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304743 dev_err(hba->dev,
4744 "Unexpected request response code = %x\n",
4745 result);
Stanley Chue0347d82019-04-15 20:23:38 +08004746 result = DID_ERROR << 16;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304747 break;
4748 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304749 break;
4750 case OCS_ABORTED:
4751 result |= DID_ABORT << 16;
4752 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304753 case OCS_INVALID_COMMAND_STATUS:
4754 result |= DID_REQUEUE << 16;
4755 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304756 case OCS_INVALID_CMD_TABLE_ATTR:
4757 case OCS_INVALID_PRDT_ATTR:
4758 case OCS_MISMATCH_DATA_BUF_SIZE:
4759 case OCS_MISMATCH_RESP_UPIU_SIZE:
4760 case OCS_PEER_COMM_FAILURE:
4761 case OCS_FATAL_ERROR:
4762 default:
4763 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304764 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004765 "OCS error from controller = %x for tag %d\n",
4766 ocs, lrbp->task_tag);
4767 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004768 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304769 break;
4770 } /* end of switch */
4771
Can Guo2df74b62019-11-25 22:53:33 -08004772 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
Dolev Raviv66cc8202016-12-22 18:39:42 -08004773 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304774 return result;
4775}
4776
4777/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304778 * ufshcd_uic_cmd_compl - handle completion of uic command
4779 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304780 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004781 *
4782 * Returns
4783 * IRQ_HANDLED - If interrupt is valid
4784 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304785 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004786static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304787{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004788 irqreturn_t retval = IRQ_NONE;
4789
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304790 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304791 hba->active_uic_cmd->argument2 |=
4792 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304793 hba->active_uic_cmd->argument3 =
4794 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304795 complete(&hba->active_uic_cmd->done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004796 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304797 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304798
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004799 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004800 complete(hba->uic_async_done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004801 retval = IRQ_HANDLED;
4802 }
4803 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304804}
4805
4806/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004807 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304808 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004809 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304810 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004811static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4812 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304813{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304814 struct ufshcd_lrb *lrbp;
4815 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304816 int result;
4817 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004818
Dolev Ravive9d501b2014-07-01 12:22:37 +03004819 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4820 lrbp = &hba->lrb[index];
4821 cmd = lrbp->cmd;
4822 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004823 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004824 result = ufshcd_transfer_rsp_status(hba, lrbp);
4825 scsi_dma_unmap(cmd);
4826 cmd->result = result;
4827 /* Mark completed command as NULL in LRB */
4828 lrbp->cmd = NULL;
Can Guo74a527a2019-11-25 22:53:32 -08004829 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03004830 /* Do not touch lrbp after scsi done */
4831 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004832 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004833 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4834 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Can Guo74a527a2019-11-25 22:53:32 -08004835 lrbp->compl_time_stamp = ktime_get();
Lee Susman1a07f2d2016-12-22 18:42:03 -08004836 if (hba->dev_cmd.complete) {
4837 ufshcd_add_command_trace(hba, index,
4838 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004839 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004840 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004841 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004842 if (ufshcd_is_clkscaling_supported(hba))
4843 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004844 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304845
4846 /* clear corresponding bits of completed commands */
4847 hba->outstanding_reqs ^= completed_reqs;
4848
Sahitya Tummala856b3482014-09-25 15:32:34 +03004849 ufshcd_clk_scaling_update_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304850}
4851
4852/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004853 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4854 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004855 *
4856 * Returns
4857 * IRQ_HANDLED - If interrupt is valid
4858 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004859 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004860static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004861{
4862 unsigned long completed_reqs;
4863 u32 tr_doorbell;
4864
4865 /* Resetting interrupt aggregation counters first and reading the
4866 * DOOR_BELL afterward allows us to handle all the completed requests.
4867 * In order to prevent other interrupts starvation the DB is read once
4868 * after reset. The down side of this solution is the possibility of
4869 * false interrupt if device completes another request after resetting
4870 * aggregation and before reading the DB.
4871 */
Alim Akhtar5ac6abc2018-05-06 15:44:16 +05304872 if (ufshcd_is_intr_aggr_allowed(hba) &&
4873 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004874 ufshcd_reset_intr_aggr(hba);
4875
4876 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4877 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4878
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004879 if (completed_reqs) {
4880 __ufshcd_transfer_req_compl(hba, completed_reqs);
4881 return IRQ_HANDLED;
4882 } else {
4883 return IRQ_NONE;
4884 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004885}
4886
4887/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304888 * ufshcd_disable_ee - disable exception event
4889 * @hba: per-adapter instance
4890 * @mask: exception event to disable
4891 *
4892 * Disables exception event in the device so that the EVENT_ALERT
4893 * bit is not set.
4894 *
4895 * Returns zero on success, non-zero error value on failure.
4896 */
4897static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4898{
4899 int err = 0;
4900 u32 val;
4901
4902 if (!(hba->ee_ctrl_mask & mask))
4903 goto out;
4904
4905 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004906 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004907 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304908 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4909 if (!err)
4910 hba->ee_ctrl_mask &= ~mask;
4911out:
4912 return err;
4913}
4914
4915/**
4916 * ufshcd_enable_ee - enable exception event
4917 * @hba: per-adapter instance
4918 * @mask: exception event to enable
4919 *
4920 * Enable corresponding exception event in the device to allow
4921 * device to alert host in critical scenarios.
4922 *
4923 * Returns zero on success, non-zero error value on failure.
4924 */
4925static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4926{
4927 int err = 0;
4928 u32 val;
4929
4930 if (hba->ee_ctrl_mask & mask)
4931 goto out;
4932
4933 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004934 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004935 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304936 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4937 if (!err)
4938 hba->ee_ctrl_mask |= mask;
4939out:
4940 return err;
4941}
4942
4943/**
4944 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4945 * @hba: per-adapter instance
4946 *
4947 * Allow device to manage background operations on its own. Enabling
4948 * this might lead to inconsistent latencies during normal data transfers
4949 * as the device is allowed to manage its own way of handling background
4950 * operations.
4951 *
4952 * Returns zero on success, non-zero on failure.
4953 */
4954static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4955{
4956 int err = 0;
4957
4958 if (hba->auto_bkops_enabled)
4959 goto out;
4960
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004961 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304962 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4963 if (err) {
4964 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4965 __func__, err);
4966 goto out;
4967 }
4968
4969 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004970 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304971
4972 /* No need of URGENT_BKOPS exception from the device */
4973 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4974 if (err)
4975 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4976 __func__, err);
4977out:
4978 return err;
4979}
4980
4981/**
4982 * ufshcd_disable_auto_bkops - block device in doing background operations
4983 * @hba: per-adapter instance
4984 *
4985 * Disabling background operations improves command response latency but
4986 * has drawback of device moving into critical state where the device is
4987 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4988 * host is idle so that BKOPS are managed effectively without any negative
4989 * impacts.
4990 *
4991 * Returns zero on success, non-zero on failure.
4992 */
4993static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
4994{
4995 int err = 0;
4996
4997 if (!hba->auto_bkops_enabled)
4998 goto out;
4999
5000 /*
5001 * If host assisted BKOPs is to be enabled, make sure
5002 * urgent bkops exception is allowed.
5003 */
5004 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5005 if (err) {
5006 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5007 __func__, err);
5008 goto out;
5009 }
5010
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005011 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305012 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5013 if (err) {
5014 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5015 __func__, err);
5016 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5017 goto out;
5018 }
5019
5020 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005021 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Asutosh Das24366c2a2019-11-25 22:53:30 -08005022 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305023out:
5024 return err;
5025}
5026
5027/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005028 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305029 * @hba: per adapter instance
5030 *
5031 * After a device reset the device may toggle the BKOPS_EN flag
5032 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005033 * as well. This function would change the auto-bkops state based on
5034 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305035 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005036static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305037{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005038 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5039 hba->auto_bkops_enabled = false;
5040 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5041 ufshcd_enable_auto_bkops(hba);
5042 } else {
5043 hba->auto_bkops_enabled = true;
5044 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5045 ufshcd_disable_auto_bkops(hba);
5046 }
Asutosh Das24366c2a2019-11-25 22:53:30 -08005047 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305048}
5049
5050static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5051{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005052 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305053 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5054}
5055
5056/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005057 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5058 * @hba: per-adapter instance
5059 * @status: bkops_status value
5060 *
5061 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5062 * flag in the device to permit background operations if the device
5063 * bkops_status is greater than or equal to "status" argument passed to
5064 * this function, disable otherwise.
5065 *
5066 * Returns 0 for success, non-zero in case of failure.
5067 *
5068 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5069 * to know whether auto bkops is enabled or disabled after this function
5070 * returns control to it.
5071 */
5072static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5073 enum bkops_status status)
5074{
5075 int err;
5076 u32 curr_status = 0;
5077
5078 err = ufshcd_get_bkops_status(hba, &curr_status);
5079 if (err) {
5080 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5081 __func__, err);
5082 goto out;
5083 } else if (curr_status > BKOPS_STATUS_MAX) {
5084 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5085 __func__, curr_status);
5086 err = -EINVAL;
5087 goto out;
5088 }
5089
5090 if (curr_status >= status)
5091 err = ufshcd_enable_auto_bkops(hba);
5092 else
5093 err = ufshcd_disable_auto_bkops(hba);
Asutosh Das24366c2a2019-11-25 22:53:30 -08005094 hba->urgent_bkops_lvl = curr_status;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005095out:
5096 return err;
5097}
5098
5099/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305100 * ufshcd_urgent_bkops - handle urgent bkops exception event
5101 * @hba: per-adapter instance
5102 *
5103 * Enable fBackgroundOpsEn flag in the device to permit background
5104 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005105 *
5106 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5107 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305108 */
5109static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5110{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005111 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305112}
5113
5114static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5115{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005116 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305117 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5118}
5119
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005120static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5121{
5122 int err;
5123 u32 curr_status = 0;
5124
5125 if (hba->is_urgent_bkops_lvl_checked)
5126 goto enable_auto_bkops;
5127
5128 err = ufshcd_get_bkops_status(hba, &curr_status);
5129 if (err) {
5130 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5131 __func__, err);
5132 goto out;
5133 }
5134
5135 /*
5136 * We are seeing that some devices are raising the urgent bkops
5137 * exception events even when BKOPS status doesn't indicate performace
5138 * impacted or critical. Handle these device by determining their urgent
5139 * bkops status at runtime.
5140 */
5141 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5142 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5143 __func__, curr_status);
5144 /* update the current status as the urgent bkops level */
5145 hba->urgent_bkops_lvl = curr_status;
5146 hba->is_urgent_bkops_lvl_checked = true;
5147 }
5148
5149enable_auto_bkops:
5150 err = ufshcd_enable_auto_bkops(hba);
5151out:
5152 if (err < 0)
5153 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5154 __func__, err);
5155}
5156
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305157/**
5158 * ufshcd_exception_event_handler - handle exceptions raised by device
5159 * @work: pointer to work data
5160 *
5161 * Read bExceptionEventStatus attribute from the device and handle the
5162 * exception event accordingly.
5163 */
5164static void ufshcd_exception_event_handler(struct work_struct *work)
5165{
5166 struct ufs_hba *hba;
5167 int err;
5168 u32 status = 0;
5169 hba = container_of(work, struct ufs_hba, eeh_work);
5170
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305171 pm_runtime_get_sync(hba->dev);
Stanley Chu03e1d282019-12-24 21:01:05 +08005172 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305173 err = ufshcd_get_ee_status(hba, &status);
5174 if (err) {
5175 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5176 __func__, err);
5177 goto out;
5178 }
5179
5180 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005181
5182 if (status & MASK_EE_URGENT_BKOPS)
5183 ufshcd_bkops_exception_event_handler(hba);
5184
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305185out:
Stanley Chu03e1d282019-12-24 21:01:05 +08005186 ufshcd_scsi_unblock_requests(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305187 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305188 return;
5189}
5190
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005191/* Complete requests that have door-bell cleared */
5192static void ufshcd_complete_requests(struct ufs_hba *hba)
5193{
5194 ufshcd_transfer_req_compl(hba);
5195 ufshcd_tmc_handler(hba);
5196}
5197
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305198/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005199 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5200 * to recover from the DL NAC errors or not.
5201 * @hba: per-adapter instance
5202 *
5203 * Returns true if error handling is required, false otherwise
5204 */
5205static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5206{
5207 unsigned long flags;
5208 bool err_handling = true;
5209
5210 spin_lock_irqsave(hba->host->host_lock, flags);
5211 /*
5212 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5213 * device fatal error and/or DL NAC & REPLAY timeout errors.
5214 */
5215 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5216 goto out;
5217
5218 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5219 ((hba->saved_err & UIC_ERROR) &&
5220 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5221 goto out;
5222
5223 if ((hba->saved_err & UIC_ERROR) &&
5224 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5225 int err;
5226 /*
5227 * wait for 50ms to see if we can get any other errors or not.
5228 */
5229 spin_unlock_irqrestore(hba->host->host_lock, flags);
5230 msleep(50);
5231 spin_lock_irqsave(hba->host->host_lock, flags);
5232
5233 /*
5234 * now check if we have got any other severe errors other than
5235 * DL NAC error?
5236 */
5237 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5238 ((hba->saved_err & UIC_ERROR) &&
5239 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5240 goto out;
5241
5242 /*
5243 * As DL NAC is the only error received so far, send out NOP
5244 * command to confirm if link is still active or not.
5245 * - If we don't get any response then do error recovery.
5246 * - If we get response then clear the DL NAC error bit.
5247 */
5248
5249 spin_unlock_irqrestore(hba->host->host_lock, flags);
5250 err = ufshcd_verify_dev_init(hba);
5251 spin_lock_irqsave(hba->host->host_lock, flags);
5252
5253 if (err)
5254 goto out;
5255
5256 /* Link seems to be alive hence ignore the DL NAC errors */
5257 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5258 hba->saved_err &= ~UIC_ERROR;
5259 /* clear NAC error */
5260 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5261 if (!hba->saved_uic_err) {
5262 err_handling = false;
5263 goto out;
5264 }
5265 }
5266out:
5267 spin_unlock_irqrestore(hba->host->host_lock, flags);
5268 return err_handling;
5269}
5270
5271/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305272 * ufshcd_err_handler - handle UFS errors that require s/w attention
5273 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305274 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305275static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305276{
5277 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305278 unsigned long flags;
5279 u32 err_xfer = 0;
5280 u32 err_tm = 0;
5281 int err = 0;
5282 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005283 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305284
5285 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305286
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305287 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005288 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305289
5290 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005291 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305292 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305293
5294 hba->ufshcd_state = UFSHCD_STATE_RESET;
5295 ufshcd_set_eh_in_progress(hba);
5296
5297 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005298 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005299
5300 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5301 bool ret;
5302
5303 spin_unlock_irqrestore(hba->host->host_lock, flags);
5304 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5305 ret = ufshcd_quirk_dl_nac_errors(hba);
5306 spin_lock_irqsave(hba->host->host_lock, flags);
5307 if (!ret)
5308 goto skip_err_handling;
5309 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005310 if ((hba->saved_err & INT_FATAL_ERRORS) ||
Stanley Chu82174442019-05-21 14:44:54 +08005311 (hba->saved_err & UFSHCD_UIC_HIBERN8_MASK) ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005312 ((hba->saved_err & UIC_ERROR) &&
5313 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5314 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5315 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5316 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305317
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005318 /*
5319 * if host reset is required then skip clearing the pending
Can Guo2df74b62019-11-25 22:53:33 -08005320 * transfers forcefully because they will get cleared during
5321 * host reset and restore
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005322 */
5323 if (needs_reset)
5324 goto skip_pending_xfer_clear;
5325
5326 /* release lock as clear command might sleep */
5327 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305328 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005329 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5330 if (ufshcd_clear_cmd(hba, tag)) {
5331 err_xfer = true;
5332 goto lock_skip_pending_xfer_clear;
5333 }
5334 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305335
5336 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005337 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5338 if (ufshcd_clear_tm_cmd(hba, tag)) {
5339 err_tm = true;
5340 goto lock_skip_pending_xfer_clear;
5341 }
5342 }
5343
5344lock_skip_pending_xfer_clear:
5345 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305346
5347 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005348 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305349
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005350 if (err_xfer || err_tm)
5351 needs_reset = true;
5352
5353skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305354 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005355 if (needs_reset) {
5356 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5357
5358 /*
5359 * ufshcd_reset_and_restore() does the link reinitialization
5360 * which will need atleast one empty doorbell slot to send the
5361 * device management commands (NOP and query commands).
5362 * If there is no slot empty at this moment then free up last
5363 * slot forcefully.
5364 */
5365 if (hba->outstanding_reqs == max_doorbells)
5366 __ufshcd_transfer_req_compl(hba,
5367 (1UL << (hba->nutrs - 1)));
5368
5369 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305370 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005371 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305372 if (err) {
5373 dev_err(hba->dev, "%s: reset and restore failed\n",
5374 __func__);
5375 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5376 }
5377 /*
5378 * Inform scsi mid-layer that we did reset and allow to handle
5379 * Unit Attention properly.
5380 */
5381 scsi_report_bus_reset(hba->host, 0);
5382 hba->saved_err = 0;
5383 hba->saved_uic_err = 0;
5384 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005385
Yaniv Gardi583fa622016-03-10 17:37:13 +02005386skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005387 if (!needs_reset) {
5388 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5389 if (hba->saved_err || hba->saved_uic_err)
5390 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5391 __func__, hba->saved_err, hba->saved_uic_err);
5392 }
5393
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305394 ufshcd_clear_eh_in_progress(hba);
5395
5396out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005397 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani38135532018-05-03 16:37:18 +05305398 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005399 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305400 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305401}
5402
5403/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305404 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5405 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005406 *
5407 * Returns
5408 * IRQ_HANDLED - If interrupt is valid
5409 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305410 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005411static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305412{
5413 u32 reg;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005414 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305415
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005416 /* PHY layer lane error */
5417 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5418 /* Ignore LINERESET indication, as this is not an error */
5419 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005420 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005421 /*
5422 * To know whether this error is fatal or not, DB timeout
5423 * must be checked but this error is handled separately.
5424 */
5425 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Stanley Chu48d5b972019-07-10 21:38:18 +08005426 ufshcd_update_reg_hist(&hba->ufs_stats.pa_err, reg);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005427 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005428 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005429
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305430 /* PA_INIT_ERROR is fatal and needs UIC reset */
5431 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005432 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
5433 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005434 ufshcd_update_reg_hist(&hba->ufs_stats.dl_err, reg);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005435
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005436 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5437 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
5438 else if (hba->dev_quirks &
5439 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5440 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5441 hba->uic_error |=
5442 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5443 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5444 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5445 }
5446 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005447 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305448
5449 /* UIC NL/TL/DME errors needs software retry */
5450 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005451 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
5452 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005453 ufshcd_update_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305454 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005455 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005456 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305457
5458 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005459 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
5460 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005461 ufshcd_update_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305462 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005463 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005464 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305465
5466 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005467 if ((reg & UIC_DME_ERROR) &&
5468 (reg & UIC_DME_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005469 ufshcd_update_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305470 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005471 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005472 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305473
5474 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5475 __func__, hba->uic_error);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005476 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305477}
5478
Stanley Chu82174442019-05-21 14:44:54 +08005479static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
5480 u32 intr_mask)
5481{
5482 if (!ufshcd_is_auto_hibern8_supported(hba))
5483 return false;
5484
5485 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
5486 return false;
5487
5488 if (hba->active_uic_cmd &&
5489 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
5490 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
5491 return false;
5492
5493 return true;
5494}
5495
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305496/**
5497 * ufshcd_check_errors - Check for errors that need s/w attention
5498 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005499 *
5500 * Returns
5501 * IRQ_HANDLED - If interrupt is valid
5502 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305503 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005504static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305505{
5506 bool queue_eh_work = false;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005507 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305508
Stanley Chud3c615b2019-07-10 21:38:19 +08005509 if (hba->errors & INT_FATAL_ERRORS) {
5510 ufshcd_update_reg_hist(&hba->ufs_stats.fatal_err, hba->errors);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305511 queue_eh_work = true;
Stanley Chud3c615b2019-07-10 21:38:19 +08005512 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305513
5514 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305515 hba->uic_error = 0;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005516 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305517 if (hba->uic_error)
5518 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305519 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305520
Stanley Chu82174442019-05-21 14:44:54 +08005521 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
5522 dev_err(hba->dev,
5523 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
5524 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
5525 "Enter" : "Exit",
5526 hba->errors, ufshcd_get_upmcrs(hba));
Stanley Chud3c615b2019-07-10 21:38:19 +08005527 ufshcd_update_reg_hist(&hba->ufs_stats.auto_hibern8_err,
5528 hba->errors);
Stanley Chu82174442019-05-21 14:44:54 +08005529 queue_eh_work = true;
5530 }
5531
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305532 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005533 /*
5534 * update the transfer error masks to sticky bits, let's do this
5535 * irrespective of current ufshcd_state.
5536 */
5537 hba->saved_err |= hba->errors;
5538 hba->saved_uic_err |= hba->uic_error;
5539
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305540 /* handle fatal errors only when link is functional */
5541 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5542 /* block commands from scsi mid-layer */
Subhash Jadavani38135532018-05-03 16:37:18 +05305543 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305544
Zang Leigang141f8162016-11-16 11:29:37 +08005545 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005546
5547 /* dump controller state before resetting */
5548 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5549 bool pr_prdt = !!(hba->saved_err &
5550 SYSTEM_BUS_FATAL_ERROR);
5551
5552 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5553 __func__, hba->saved_err,
5554 hba->saved_uic_err);
5555
5556 ufshcd_print_host_regs(hba);
5557 ufshcd_print_pwr_info(hba);
5558 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5559 ufshcd_print_trs(hba, hba->outstanding_reqs,
5560 pr_prdt);
5561 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305562 schedule_work(&hba->eh_work);
5563 }
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005564 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305565 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305566 /*
5567 * if (!queue_eh_work) -
5568 * Other errors are either non-fatal where host recovers
5569 * itself without s/w intervention or errors that will be
5570 * handled by the SCSI core layer.
5571 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005572 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305573}
5574
Bart Van Assche69a6c262019-12-09 10:13:09 -08005575struct ctm_info {
5576 struct ufs_hba *hba;
5577 unsigned long pending;
5578 unsigned int ncpl;
5579};
5580
5581static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
5582{
5583 struct ctm_info *const ci = priv;
5584 struct completion *c;
5585
5586 WARN_ON_ONCE(reserved);
5587 if (test_bit(req->tag, &ci->pending))
5588 return true;
5589 ci->ncpl++;
5590 c = req->end_io_data;
5591 if (c)
5592 complete(c);
5593 return true;
5594}
5595
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305596/**
5597 * ufshcd_tmc_handler - handle task management function completion
5598 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005599 *
5600 * Returns
5601 * IRQ_HANDLED - If interrupt is valid
5602 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305603 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005604static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305605{
Bart Van Assche69a6c262019-12-09 10:13:09 -08005606 struct request_queue *q = hba->tmf_queue;
5607 struct ctm_info ci = {
5608 .hba = hba,
5609 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
5610 };
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305611
Bart Van Assche69a6c262019-12-09 10:13:09 -08005612 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
5613 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305614}
5615
5616/**
5617 * ufshcd_sl_intr - Interrupt service routine
5618 * @hba: per adapter instance
5619 * @intr_status: contains interrupts generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005620 *
5621 * Returns
5622 * IRQ_HANDLED - If interrupt is valid
5623 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305624 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005625static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305626{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005627 irqreturn_t retval = IRQ_NONE;
5628
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305629 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Stanley Chu82174442019-05-21 14:44:54 +08005630
5631 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
5632 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
5633
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305634 if (hba->errors)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005635 retval |= ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305636
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305637 if (intr_status & UFSHCD_UIC_MASK)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005638 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305639
5640 if (intr_status & UTP_TASK_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005641 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305642
5643 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005644 retval |= ufshcd_transfer_req_compl(hba);
5645
5646 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305647}
5648
5649/**
5650 * ufshcd_intr - Main interrupt service routine
5651 * @irq: irq number
5652 * @__hba: pointer to adapter instance
5653 *
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005654 * Returns
5655 * IRQ_HANDLED - If interrupt is valid
5656 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305657 */
5658static irqreturn_t ufshcd_intr(int irq, void *__hba)
5659{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005660 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305661 irqreturn_t retval = IRQ_NONE;
5662 struct ufs_hba *hba = __hba;
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305663 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305664
5665 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305666 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305667
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305668 /*
5669 * There could be max of hba->nutrs reqs in flight and in worst case
5670 * if the reqs get finished 1 by 1 after the interrupt status is
5671 * read, make sure we handle them by checking the interrupt status
5672 * again in a loop until we process all of the reqs before returning.
5673 */
5674 do {
5675 enabled_intr_status =
5676 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
5677 if (intr_status)
5678 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005679 if (enabled_intr_status)
5680 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005681
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305682 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
5683 } while (intr_status && --retries);
5684
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005685 if (retval == IRQ_NONE) {
5686 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
5687 __func__, intr_status);
5688 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
5689 }
5690
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305691 spin_unlock(hba->host->host_lock);
5692 return retval;
5693}
5694
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305695static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5696{
5697 int err = 0;
5698 u32 mask = 1 << tag;
5699 unsigned long flags;
5700
5701 if (!test_bit(tag, &hba->outstanding_tasks))
5702 goto out;
5703
5704 spin_lock_irqsave(hba->host->host_lock, flags);
Alim Akhtar1399c5b2018-05-06 15:44:15 +05305705 ufshcd_utmrl_clear(hba, tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305706 spin_unlock_irqrestore(hba->host->host_lock, flags);
5707
5708 /* poll for max. 1 sec to clear door bell register by h/w */
5709 err = ufshcd_wait_for_register(hba,
5710 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005711 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305712out:
5713 return err;
5714}
5715
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005716static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
5717 struct utp_task_req_desc *treq, u8 tm_function)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305718{
Bart Van Assche69a6c262019-12-09 10:13:09 -08005719 struct request_queue *q = hba->tmf_queue;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005720 struct Scsi_Host *host = hba->host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08005721 DECLARE_COMPLETION_ONSTACK(wait);
5722 struct request *req;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305723 unsigned long flags;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005724 int free_slot, task_tag, err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305725
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305726 /*
5727 * Get free slot, sleep if slots are unavailable.
5728 * Even though we use wait_event() which sleeps indefinitely,
5729 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5730 */
Bart Van Assche69a6c262019-12-09 10:13:09 -08005731 req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED);
5732 req->end_io_data = &wait;
5733 free_slot = req->tag;
5734 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005735 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305736
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305737 spin_lock_irqsave(host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305738 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305739
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005740 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
5741
5742 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
Kiwoong Kimd2877be2016-11-10 21:16:15 +09005743 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
5744
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305745 /* send command to the controller */
5746 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005747
5748 /* Make sure descriptors are ready before ringing the task doorbell */
5749 wmb();
5750
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305751 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07005752 /* Make sure that doorbell is committed immediately */
5753 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305754
5755 spin_unlock_irqrestore(host->host_lock, flags);
5756
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03005757 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send");
5758
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305759 /* wait until the task management command is completed */
Bart Van Assche69a6c262019-12-09 10:13:09 -08005760 err = wait_for_completion_io_timeout(&wait,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305761 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305762 if (!err) {
Bart Van Assche69a6c262019-12-09 10:13:09 -08005763 /*
5764 * Make sure that ufshcd_compl_tm() does not trigger a
5765 * use-after-free.
5766 */
5767 req->end_io_data = NULL;
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03005768 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err");
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305769 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
5770 __func__, tm_function);
5771 if (ufshcd_clear_tm_cmd(hba, free_slot))
5772 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
5773 __func__, free_slot);
5774 err = -ETIMEDOUT;
5775 } else {
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005776 err = 0;
5777 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
5778
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03005779 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305780 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305781
Stanley Chub5572172019-08-19 21:43:28 +08005782 spin_lock_irqsave(hba->host->host_lock, flags);
5783 __clear_bit(free_slot, &hba->outstanding_tasks);
5784 spin_unlock_irqrestore(hba->host->host_lock, flags);
5785
Bart Van Assche69a6c262019-12-09 10:13:09 -08005786 blk_put_request(req);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305787
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005788 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305789 return err;
5790}
5791
5792/**
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005793 * ufshcd_issue_tm_cmd - issues task management commands to controller
5794 * @hba: per adapter instance
5795 * @lun_id: LUN ID to which TM command is sent
5796 * @task_id: task ID to which the TM command is applicable
5797 * @tm_function: task management function opcode
5798 * @tm_response: task management service response return value
5799 *
5800 * Returns non-zero value on error, zero on success.
5801 */
5802static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5803 u8 tm_function, u8 *tm_response)
5804{
5805 struct utp_task_req_desc treq = { { 0 }, };
5806 int ocs_value, err;
5807
5808 /* Configure task request descriptor */
5809 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5810 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5811
5812 /* Configure task request UPIU */
5813 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
5814 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
5815 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
5816
5817 /*
5818 * The host shall provide the same value for LUN field in the basic
5819 * header and for Input Parameter.
5820 */
5821 treq.input_param1 = cpu_to_be32(lun_id);
5822 treq.input_param2 = cpu_to_be32(task_id);
5823
5824 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
5825 if (err == -ETIMEDOUT)
5826 return err;
5827
5828 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
5829 if (ocs_value != OCS_SUCCESS)
5830 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
5831 __func__, ocs_value);
5832 else if (tm_response)
5833 *tm_response = be32_to_cpu(treq.output_param1) &
5834 MASK_TM_SERVICE_RESP;
5835 return err;
5836}
5837
5838/**
Avri Altman5e0a86e2018-10-07 17:30:37 +03005839 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
5840 * @hba: per-adapter instance
5841 * @req_upiu: upiu request
5842 * @rsp_upiu: upiu reply
Avri Altman5e0a86e2018-10-07 17:30:37 +03005843 * @desc_buff: pointer to descriptor buffer, NULL if NA
5844 * @buff_len: descriptor size, 0 if NA
Bart Van Assched0e97602019-10-29 16:07:08 -07005845 * @cmd_type: specifies the type (NOP, Query...)
Avri Altman5e0a86e2018-10-07 17:30:37 +03005846 * @desc_op: descriptor operation
5847 *
5848 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
5849 * Therefore, it "rides" the device management infrastructure: uses its tag and
5850 * tasks work queues.
5851 *
5852 * Since there is only one available tag for device management commands,
5853 * the caller is expected to hold the hba->dev_cmd.lock mutex.
5854 */
5855static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
5856 struct utp_upiu_req *req_upiu,
5857 struct utp_upiu_req *rsp_upiu,
5858 u8 *desc_buff, int *buff_len,
Bart Van Assche7f674c32019-10-29 16:07:09 -07005859 enum dev_cmd_type cmd_type,
Avri Altman5e0a86e2018-10-07 17:30:37 +03005860 enum query_opcode desc_op)
5861{
Bart Van Assche7252a362019-12-09 10:13:08 -08005862 struct request_queue *q = hba->cmd_queue;
5863 struct request *req;
Avri Altman5e0a86e2018-10-07 17:30:37 +03005864 struct ufshcd_lrb *lrbp;
5865 int err = 0;
5866 int tag;
5867 struct completion wait;
5868 unsigned long flags;
5869 u32 upiu_flags;
5870
5871 down_read(&hba->clk_scaling_lock);
5872
Bart Van Assche7252a362019-12-09 10:13:08 -08005873 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03005874 if (IS_ERR(req)) {
5875 err = PTR_ERR(req);
5876 goto out_unlock;
5877 }
Bart Van Assche7252a362019-12-09 10:13:08 -08005878 tag = req->tag;
5879 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Avri Altman5e0a86e2018-10-07 17:30:37 +03005880
5881 init_completion(&wait);
5882 lrbp = &hba->lrb[tag];
5883 WARN_ON(lrbp->cmd);
5884
5885 lrbp->cmd = NULL;
5886 lrbp->sense_bufflen = 0;
5887 lrbp->sense_buffer = NULL;
5888 lrbp->task_tag = tag;
5889 lrbp->lun = 0;
5890 lrbp->intr_cmd = true;
5891 hba->dev_cmd.type = cmd_type;
5892
5893 switch (hba->ufs_version) {
5894 case UFSHCI_VERSION_10:
5895 case UFSHCI_VERSION_11:
5896 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
5897 break;
5898 default:
5899 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
5900 break;
5901 }
5902
5903 /* update the task tag in the request upiu */
5904 req_upiu->header.dword_0 |= cpu_to_be32(tag);
5905
5906 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
5907
5908 /* just copy the upiu request as it is */
5909 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
5910 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
5911 /* The Data Segment Area is optional depending upon the query
5912 * function value. for WRITE DESCRIPTOR, the data segment
5913 * follows right after the tsf.
5914 */
5915 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
5916 *buff_len = 0;
5917 }
5918
5919 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
5920
5921 hba->dev_cmd.complete = &wait;
5922
5923 /* Make sure descriptors are ready before ringing the doorbell */
5924 wmb();
5925 spin_lock_irqsave(hba->host->host_lock, flags);
5926 ufshcd_send_command(hba, tag);
5927 spin_unlock_irqrestore(hba->host->host_lock, flags);
5928
5929 /*
5930 * ignore the returning value here - ufshcd_check_query_response is
5931 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
5932 * read the response directly ignoring all errors.
5933 */
5934 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
5935
5936 /* just copy the upiu response as it is */
5937 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
Avri Altman4bbbe242019-02-20 09:11:13 +02005938 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
5939 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
5940 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
5941 MASK_QUERY_DATA_SEG_LEN;
5942
5943 if (*buff_len >= resp_len) {
5944 memcpy(desc_buff, descp, resp_len);
5945 *buff_len = resp_len;
5946 } else {
Bean Huo3d4881d2019-11-12 23:34:35 +01005947 dev_warn(hba->dev,
5948 "%s: rsp size %d is bigger than buffer size %d",
5949 __func__, resp_len, *buff_len);
Avri Altman4bbbe242019-02-20 09:11:13 +02005950 *buff_len = 0;
5951 err = -EINVAL;
5952 }
5953 }
Avri Altman5e0a86e2018-10-07 17:30:37 +03005954
Bart Van Assche7252a362019-12-09 10:13:08 -08005955 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03005956out_unlock:
Avri Altman5e0a86e2018-10-07 17:30:37 +03005957 up_read(&hba->clk_scaling_lock);
5958 return err;
5959}
5960
5961/**
5962 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
5963 * @hba: per-adapter instance
5964 * @req_upiu: upiu request
5965 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
5966 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
5967 * @desc_buff: pointer to descriptor buffer, NULL if NA
5968 * @buff_len: descriptor size, 0 if NA
5969 * @desc_op: descriptor operation
5970 *
5971 * Supports UTP Transfer requests (nop and query), and UTP Task
5972 * Management requests.
5973 * It is up to the caller to fill the upiu conent properly, as it will
5974 * be copied without any further input validations.
5975 */
5976int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
5977 struct utp_upiu_req *req_upiu,
5978 struct utp_upiu_req *rsp_upiu,
5979 int msgcode,
5980 u8 *desc_buff, int *buff_len,
5981 enum query_opcode desc_op)
5982{
5983 int err;
Bart Van Assche7f674c32019-10-29 16:07:09 -07005984 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
Avri Altman5e0a86e2018-10-07 17:30:37 +03005985 struct utp_task_req_desc treq = { { 0 }, };
5986 int ocs_value;
5987 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
5988
Avri Altman5e0a86e2018-10-07 17:30:37 +03005989 switch (msgcode) {
5990 case UPIU_TRANSACTION_NOP_OUT:
5991 cmd_type = DEV_CMD_TYPE_NOP;
5992 /* fall through */
5993 case UPIU_TRANSACTION_QUERY_REQ:
5994 ufshcd_hold(hba, false);
5995 mutex_lock(&hba->dev_cmd.lock);
5996 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
5997 desc_buff, buff_len,
5998 cmd_type, desc_op);
5999 mutex_unlock(&hba->dev_cmd.lock);
6000 ufshcd_release(hba);
6001
6002 break;
6003 case UPIU_TRANSACTION_TASK_REQ:
6004 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6005 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6006
6007 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6008
6009 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6010 if (err == -ETIMEDOUT)
6011 break;
6012
6013 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6014 if (ocs_value != OCS_SUCCESS) {
6015 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6016 ocs_value);
6017 break;
6018 }
6019
6020 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6021
6022 break;
6023 default:
6024 err = -EINVAL;
6025
6026 break;
6027 }
6028
Avri Altman5e0a86e2018-10-07 17:30:37 +03006029 return err;
6030}
6031
6032/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306033 * ufshcd_eh_device_reset_handler - device reset handler registered to
6034 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306035 * @cmd: SCSI command pointer
6036 *
6037 * Returns SUCCESS/FAILED
6038 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306039static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306040{
6041 struct Scsi_Host *host;
6042 struct ufs_hba *hba;
6043 unsigned int tag;
6044 u32 pos;
6045 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306046 u8 resp = 0xF;
6047 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306048 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306049
6050 host = cmd->device->host;
6051 hba = shost_priv(host);
6052 tag = cmd->request->tag;
6053
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306054 lrbp = &hba->lrb[tag];
6055 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6056 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306057 if (!err)
6058 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306059 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306060 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306061
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306062 /* clear the commands that were pending for corresponding LUN */
6063 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6064 if (hba->lrb[pos].lun == lrbp->lun) {
6065 err = ufshcd_clear_cmd(hba, pos);
6066 if (err)
6067 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306068 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306069 }
6070 spin_lock_irqsave(host->host_lock, flags);
6071 ufshcd_transfer_req_compl(hba);
6072 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08006073
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306074out:
Gilad Broner7fabb772017-02-03 16:56:50 -08006075 hba->req_abort_count = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08006076 ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306077 if (!err) {
6078 err = SUCCESS;
6079 } else {
6080 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6081 err = FAILED;
6082 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306083 return err;
6084}
6085
Gilad Bronere0b299e2017-02-03 16:56:40 -08006086static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6087{
6088 struct ufshcd_lrb *lrbp;
6089 int tag;
6090
6091 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6092 lrbp = &hba->lrb[tag];
6093 lrbp->req_abort_skip = true;
6094 }
6095}
6096
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306097/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306098 * ufshcd_abort - abort a specific command
6099 * @cmd: SCSI command pointer
6100 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306101 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6102 * command, and in host controller by clearing the door-bell register. There can
6103 * be race between controller sending the command to the device while abort is
6104 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6105 * really issued and then try to abort it.
6106 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306107 * Returns SUCCESS/FAILED
6108 */
6109static int ufshcd_abort(struct scsi_cmnd *cmd)
6110{
6111 struct Scsi_Host *host;
6112 struct ufs_hba *hba;
6113 unsigned long flags;
6114 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306115 int err = 0;
6116 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306117 u8 resp = 0xF;
6118 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006119 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306120
6121 host = cmd->device->host;
6122 hba = shost_priv(host);
6123 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08006124 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02006125 if (!ufshcd_valid_tag(hba, tag)) {
6126 dev_err(hba->dev,
6127 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6128 __func__, tag, cmd, cmd->request);
6129 BUG();
6130 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306131
Dolev Ravive7d38252016-12-22 18:40:07 -08006132 /*
6133 * Task abort to the device W-LUN is illegal. When this command
6134 * will fail, due to spec violation, scsi err handling next step
6135 * will be to send LU reset which, again, is a spec violation.
6136 * To avoid these unnecessary/illegal step we skip to the last error
6137 * handling stage: reset and restore.
6138 */
6139 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6140 return ufshcd_eh_host_reset_handler(cmd);
6141
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006142 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006143 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006144 /* If command is already aborted/completed, return SUCCESS */
6145 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6146 dev_err(hba->dev,
6147 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6148 __func__, tag, hba->outstanding_reqs, reg);
6149 goto out;
6150 }
6151
Dolev Ravive9d501b2014-07-01 12:22:37 +03006152 if (!(reg & (1 << tag))) {
6153 dev_err(hba->dev,
6154 "%s: cmd was completed, but without a notifying intr, tag = %d",
6155 __func__, tag);
6156 }
6157
Dolev Raviv66cc8202016-12-22 18:39:42 -08006158 /* Print Transfer Request of aborted task */
6159 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08006160
Gilad Broner7fabb772017-02-03 16:56:50 -08006161 /*
6162 * Print detailed info about aborted request.
6163 * As more than one request might get aborted at the same time,
6164 * print full information only for the first aborted request in order
6165 * to reduce repeated printouts. For other aborted requests only print
6166 * basic details.
6167 */
6168 scsi_print_command(hba->lrb[tag].cmd);
6169 if (!hba->req_abort_count) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08006170 ufshcd_update_reg_hist(&hba->ufs_stats.task_abort, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08006171 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08006172 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08006173 ufshcd_print_pwr_info(hba);
6174 ufshcd_print_trs(hba, 1 << tag, true);
6175 } else {
6176 ufshcd_print_trs(hba, 1 << tag, false);
6177 }
6178 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08006179
6180 /* Skip task abort in case previous aborts failed and report failure */
6181 if (lrbp->req_abort_skip) {
6182 err = -EIO;
6183 goto out;
6184 }
6185
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306186 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6187 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6188 UFS_QUERY_TASK, &resp);
6189 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6190 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006191 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6192 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306193 break;
6194 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306195 /*
6196 * cmd not pending in the device, check if it is
6197 * in transition.
6198 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006199 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6200 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306201 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6202 if (reg & (1 << tag)) {
6203 /* sleep for max. 200us to stabilize */
6204 usleep_range(100, 200);
6205 continue;
6206 }
6207 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006208 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6209 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306210 goto out;
6211 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006212 dev_err(hba->dev,
6213 "%s: no response from device. tag = %d, err %d\n",
6214 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306215 if (!err)
6216 err = resp; /* service response error */
6217 goto out;
6218 }
6219 }
6220
6221 if (!poll_cnt) {
6222 err = -EBUSY;
6223 goto out;
6224 }
6225
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306226 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6227 UFS_ABORT_TASK, &resp);
6228 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006229 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306230 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006231 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6232 __func__, tag, err);
6233 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306234 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306235 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306236
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306237 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006238 if (err) {
6239 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6240 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306241 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006242 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306243
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306244 scsi_dma_unmap(cmd);
6245
6246 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006247 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306248 hba->lrb[tag].cmd = NULL;
6249 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306250
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306251out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306252 if (!err) {
6253 err = SUCCESS;
6254 } else {
6255 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08006256 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306257 err = FAILED;
6258 }
6259
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006260 /*
6261 * This ufshcd_release() corresponds to the original scsi cmd that got
6262 * aborted here (as we won't get any IRQ for it).
6263 */
6264 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306265 return err;
6266}
6267
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306268/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306269 * ufshcd_host_reset_and_restore - reset and restore host controller
6270 * @hba: per-adapter instance
6271 *
6272 * Note that host controller reset may issue DME_RESET to
6273 * local and remote (device) Uni-Pro stack and the attributes
6274 * are reset to default state.
6275 *
6276 * Returns zero on success, non-zero on failure
6277 */
6278static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6279{
6280 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306281 unsigned long flags;
6282
Can Guo2df74b62019-11-25 22:53:33 -08006283 /*
6284 * Stop the host controller and complete the requests
6285 * cleared by h/w
6286 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306287 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006288 ufshcd_hba_stop(hba, false);
Can Guo2df74b62019-11-25 22:53:33 -08006289 hba->silence_err_logs = true;
6290 ufshcd_complete_requests(hba);
6291 hba->silence_err_logs = false;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306292 spin_unlock_irqrestore(hba->host->host_lock, flags);
6293
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006294 /* scale up clocks to max frequency before full reinitialization */
6295 ufshcd_scale_clks(hba, true);
6296
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306297 err = ufshcd_hba_enable(hba);
6298 if (err)
6299 goto out;
6300
6301 /* Establish the link again and restore the device */
Bean Huo1b9e2142020-01-20 14:08:15 +01006302 err = ufshcd_probe_hba(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006303
6304 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306305 err = -EIO;
6306out:
6307 if (err)
6308 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
Stanley Chu8808b4e2019-07-10 21:38:21 +08006309 ufshcd_update_reg_hist(&hba->ufs_stats.host_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306310 return err;
6311}
6312
6313/**
6314 * ufshcd_reset_and_restore - reset and re-initialize host/device
6315 * @hba: per-adapter instance
6316 *
6317 * Reset and recover device, host and re-establish link. This
6318 * is helpful to recover the communication in fatal error conditions.
6319 *
6320 * Returns zero on success, non-zero on failure
6321 */
6322static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6323{
6324 int err = 0;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006325 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306326
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006327 do {
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07006328 /* Reset the attached device */
6329 ufshcd_vops_device_reset(hba);
6330
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006331 err = ufshcd_host_reset_and_restore(hba);
6332 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306333
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306334 return err;
6335}
6336
6337/**
6338 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006339 * @cmd: SCSI command pointer
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306340 *
6341 * Returns SUCCESS/FAILED
6342 */
6343static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6344{
6345 int err;
6346 unsigned long flags;
6347 struct ufs_hba *hba;
6348
6349 hba = shost_priv(cmd->device->host);
6350
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006351 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306352 /*
6353 * Check if there is any race with fatal error handling.
6354 * If so, wait for it to complete. Even though fatal error
6355 * handling does reset and restore in some cases, don't assume
6356 * anything out of it. We are just avoiding race here.
6357 */
6358 do {
6359 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306360 if (!(work_pending(&hba->eh_work) ||
Zang Leigang8dc0da72017-06-24 19:14:32 +08006361 hba->ufshcd_state == UFSHCD_STATE_RESET ||
6362 hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306363 break;
6364 spin_unlock_irqrestore(hba->host->host_lock, flags);
6365 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306366 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306367 } while (1);
6368
6369 hba->ufshcd_state = UFSHCD_STATE_RESET;
6370 ufshcd_set_eh_in_progress(hba);
6371 spin_unlock_irqrestore(hba->host->host_lock, flags);
6372
6373 err = ufshcd_reset_and_restore(hba);
6374
6375 spin_lock_irqsave(hba->host->host_lock, flags);
6376 if (!err) {
6377 err = SUCCESS;
6378 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6379 } else {
6380 err = FAILED;
6381 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6382 }
6383 ufshcd_clear_eh_in_progress(hba);
6384 spin_unlock_irqrestore(hba->host->host_lock, flags);
6385
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006386 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306387 return err;
6388}
6389
6390/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006391 * ufshcd_get_max_icc_level - calculate the ICC level
6392 * @sup_curr_uA: max. current supported by the regulator
6393 * @start_scan: row at the desc table to start scan from
6394 * @buff: power descriptor buffer
6395 *
6396 * Returns calculated max ICC level for specific regulator
6397 */
6398static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6399{
6400 int i;
6401 int curr_uA;
6402 u16 data;
6403 u16 unit;
6404
6405 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02006406 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006407 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6408 ATTR_ICC_LVL_UNIT_OFFSET;
6409 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6410 switch (unit) {
6411 case UFSHCD_NANO_AMP:
6412 curr_uA = curr_uA / 1000;
6413 break;
6414 case UFSHCD_MILI_AMP:
6415 curr_uA = curr_uA * 1000;
6416 break;
6417 case UFSHCD_AMP:
6418 curr_uA = curr_uA * 1000 * 1000;
6419 break;
6420 case UFSHCD_MICRO_AMP:
6421 default:
6422 break;
6423 }
6424 if (sup_curr_uA >= curr_uA)
6425 break;
6426 }
6427 if (i < 0) {
6428 i = 0;
6429 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6430 }
6431
6432 return (u32)i;
6433}
6434
6435/**
6436 * ufshcd_calc_icc_level - calculate the max ICC level
6437 * In case regulators are not initialized we'll return 0
6438 * @hba: per-adapter instance
6439 * @desc_buf: power descriptor buffer to extract ICC levels from.
6440 * @len: length of desc_buff
6441 *
6442 * Returns calculated ICC level
6443 */
6444static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6445 u8 *desc_buf, int len)
6446{
6447 u32 icc_level = 0;
6448
6449 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
6450 !hba->vreg_info.vccq2) {
6451 dev_err(hba->dev,
6452 "%s: Regulator capability was not set, actvIccLevel=%d",
6453 __func__, icc_level);
6454 goto out;
6455 }
6456
Stanley Chu0487fff2019-03-28 17:16:25 +08006457 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006458 icc_level = ufshcd_get_max_icc_level(
6459 hba->vreg_info.vcc->max_uA,
6460 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6461 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6462
Stanley Chu0487fff2019-03-28 17:16:25 +08006463 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006464 icc_level = ufshcd_get_max_icc_level(
6465 hba->vreg_info.vccq->max_uA,
6466 icc_level,
6467 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6468
Stanley Chu0487fff2019-03-28 17:16:25 +08006469 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006470 icc_level = ufshcd_get_max_icc_level(
6471 hba->vreg_info.vccq2->max_uA,
6472 icc_level,
6473 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6474out:
6475 return icc_level;
6476}
6477
6478static void ufshcd_init_icc_levels(struct ufs_hba *hba)
6479{
6480 int ret;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006481 int buff_len = hba->desc_size.pwr_desc;
Kees Cookbbe21d72018-05-02 16:58:09 -07006482 u8 *desc_buf;
6483
6484 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6485 if (!desc_buf)
6486 return;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006487
Bean Huo8c9a51b2020-01-20 14:08:17 +01006488 ret = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0,
6489 desc_buf, buff_len);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006490 if (ret) {
6491 dev_err(hba->dev,
6492 "%s: Failed reading power descriptor.len = %d ret = %d",
6493 __func__, buff_len, ret);
Kees Cookbbe21d72018-05-02 16:58:09 -07006494 goto out;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006495 }
6496
6497 hba->init_prefetch_data.icc_level =
6498 ufshcd_find_max_sup_active_icc_level(hba,
6499 desc_buf, buff_len);
6500 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
6501 __func__, hba->init_prefetch_data.icc_level);
6502
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02006503 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
6504 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
6505 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006506
6507 if (ret)
6508 dev_err(hba->dev,
6509 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
6510 __func__, hba->init_prefetch_data.icc_level , ret);
6511
Kees Cookbbe21d72018-05-02 16:58:09 -07006512out:
6513 kfree(desc_buf);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006514}
6515
6516/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006517 * ufshcd_scsi_add_wlus - Adds required W-LUs
6518 * @hba: per-adapter instance
6519 *
6520 * UFS device specification requires the UFS devices to support 4 well known
6521 * logical units:
6522 * "REPORT_LUNS" (address: 01h)
6523 * "UFS Device" (address: 50h)
6524 * "RPMB" (address: 44h)
6525 * "BOOT" (address: 30h)
6526 * UFS device's power management needs to be controlled by "POWER CONDITION"
6527 * field of SSU (START STOP UNIT) command. But this "power condition" field
6528 * will take effect only when its sent to "UFS device" well known logical unit
6529 * hence we require the scsi_device instance to represent this logical unit in
6530 * order for the UFS host driver to send the SSU command for power management.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006531 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006532 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
6533 * Block) LU so user space process can control this LU. User space may also
6534 * want to have access to BOOT LU.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006535 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006536 * This function adds scsi device instances for each of all well known LUs
6537 * (except "REPORT LUNS" LU).
6538 *
6539 * Returns zero on success (all required W-LUs are added successfully),
6540 * non-zero error value on failure (if failed to add any of the required W-LU).
6541 */
6542static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
6543{
6544 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006545 struct scsi_device *sdev_rpmb;
6546 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006547
6548 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
6549 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
6550 if (IS_ERR(hba->sdev_ufs_device)) {
6551 ret = PTR_ERR(hba->sdev_ufs_device);
6552 hba->sdev_ufs_device = NULL;
6553 goto out;
6554 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006555 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006556
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006557 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006558 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006559 if (IS_ERR(sdev_rpmb)) {
6560 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006561 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006562 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006563 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006564
6565 sdev_boot = __scsi_add_device(hba->host, 0, 0,
6566 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
6567 if (IS_ERR(sdev_boot))
6568 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
6569 else
6570 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006571 goto out;
6572
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006573remove_sdev_ufs_device:
6574 scsi_remove_device(hba->sdev_ufs_device);
6575out:
6576 return ret;
6577}
6578
Bean Huo09750062020-01-20 14:08:14 +01006579static int ufs_get_device_desc(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006580{
6581 int err;
Kees Cookbbe21d72018-05-02 16:58:09 -07006582 size_t buff_len;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006583 u8 model_index;
Kees Cookbbe21d72018-05-02 16:58:09 -07006584 u8 *desc_buf;
Bean Huo09750062020-01-20 14:08:14 +01006585 struct ufs_dev_info *dev_info = &hba->dev_info;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006586
Kees Cookbbe21d72018-05-02 16:58:09 -07006587 buff_len = max_t(size_t, hba->desc_size.dev_desc,
6588 QUERY_DESC_MAX_SIZE + 1);
6589 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6590 if (!desc_buf) {
6591 err = -ENOMEM;
6592 goto out;
6593 }
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006594
Bean Huo8c9a51b2020-01-20 14:08:17 +01006595 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, desc_buf,
6596 hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006597 if (err) {
6598 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6599 __func__, err);
6600 goto out;
6601 }
6602
6603 /*
6604 * getting vendor (manufacturerID) and Bank Index in big endian
6605 * format
6606 */
Bean Huo09750062020-01-20 14:08:14 +01006607 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006608 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6609
6610 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006611 err = ufshcd_read_string_desc(hba, model_index,
Bean Huo09750062020-01-20 14:08:14 +01006612 &dev_info->model, SD_ASCII_STD);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006613 if (err < 0) {
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006614 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
6615 __func__, err);
6616 goto out;
6617 }
6618
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006619 /*
6620 * ufshcd_read_string_desc returns size of the string
6621 * reset the error value
6622 */
6623 err = 0;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006624
6625out:
Kees Cookbbe21d72018-05-02 16:58:09 -07006626 kfree(desc_buf);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006627 return err;
6628}
6629
Bean Huo09750062020-01-20 14:08:14 +01006630static void ufs_put_device_desc(struct ufs_hba *hba)
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006631{
Bean Huo09750062020-01-20 14:08:14 +01006632 struct ufs_dev_info *dev_info = &hba->dev_info;
6633
6634 kfree(dev_info->model);
6635 dev_info->model = NULL;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006636}
6637
Bean Huo09750062020-01-20 14:08:14 +01006638static void ufs_fixup_device_setup(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006639{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006640 struct ufs_dev_fix *f;
Bean Huo09750062020-01-20 14:08:14 +01006641 struct ufs_dev_info *dev_info = &hba->dev_info;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006642
6643 for (f = ufs_fixups; f->quirk; f++) {
Bean Huo09750062020-01-20 14:08:14 +01006644 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
6645 f->wmanufacturerid == UFS_ANY_VENDOR) &&
6646 ((dev_info->model &&
6647 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
6648 !strcmp(f->model, UFS_ANY_MODEL)))
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006649 hba->dev_quirks |= f->quirk;
6650 }
6651}
6652
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006653/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006654 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6655 * @hba: per-adapter instance
6656 *
6657 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6658 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6659 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6660 * the hibern8 exit latency.
6661 *
6662 * Returns zero on success, non-zero error value on failure.
6663 */
6664static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6665{
6666 int ret = 0;
6667 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6668
6669 ret = ufshcd_dme_peer_get(hba,
6670 UIC_ARG_MIB_SEL(
6671 RX_MIN_ACTIVATETIME_CAPABILITY,
6672 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6673 &peer_rx_min_activatetime);
6674 if (ret)
6675 goto out;
6676
6677 /* make sure proper unit conversion is applied */
6678 tuned_pa_tactivate =
6679 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6680 / PA_TACTIVATE_TIME_UNIT_US);
6681 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6682 tuned_pa_tactivate);
6683
6684out:
6685 return ret;
6686}
6687
6688/**
6689 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6690 * @hba: per-adapter instance
6691 *
6692 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6693 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6694 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6695 * This optimal value can help reduce the hibern8 exit latency.
6696 *
6697 * Returns zero on success, non-zero error value on failure.
6698 */
6699static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6700{
6701 int ret = 0;
6702 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6703 u32 max_hibern8_time, tuned_pa_hibern8time;
6704
6705 ret = ufshcd_dme_get(hba,
6706 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6707 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6708 &local_tx_hibern8_time_cap);
6709 if (ret)
6710 goto out;
6711
6712 ret = ufshcd_dme_peer_get(hba,
6713 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6714 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6715 &peer_rx_hibern8_time_cap);
6716 if (ret)
6717 goto out;
6718
6719 max_hibern8_time = max(local_tx_hibern8_time_cap,
6720 peer_rx_hibern8_time_cap);
6721 /* make sure proper unit conversion is applied */
6722 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6723 / PA_HIBERN8_TIME_UNIT_US);
6724 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6725 tuned_pa_hibern8time);
6726out:
6727 return ret;
6728}
6729
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006730/**
6731 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6732 * less than device PA_TACTIVATE time.
6733 * @hba: per-adapter instance
6734 *
6735 * Some UFS devices require host PA_TACTIVATE to be lower than device
6736 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6737 * for such devices.
6738 *
6739 * Returns zero on success, non-zero error value on failure.
6740 */
6741static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6742{
6743 int ret = 0;
6744 u32 granularity, peer_granularity;
6745 u32 pa_tactivate, peer_pa_tactivate;
6746 u32 pa_tactivate_us, peer_pa_tactivate_us;
6747 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6748
6749 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6750 &granularity);
6751 if (ret)
6752 goto out;
6753
6754 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6755 &peer_granularity);
6756 if (ret)
6757 goto out;
6758
6759 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6760 (granularity > PA_GRANULARITY_MAX_VAL)) {
6761 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6762 __func__, granularity);
6763 return -EINVAL;
6764 }
6765
6766 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6767 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6768 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6769 __func__, peer_granularity);
6770 return -EINVAL;
6771 }
6772
6773 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6774 if (ret)
6775 goto out;
6776
6777 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6778 &peer_pa_tactivate);
6779 if (ret)
6780 goto out;
6781
6782 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6783 peer_pa_tactivate_us = peer_pa_tactivate *
6784 gran_to_us_table[peer_granularity - 1];
6785
6786 if (pa_tactivate_us > peer_pa_tactivate_us) {
6787 u32 new_peer_pa_tactivate;
6788
6789 new_peer_pa_tactivate = pa_tactivate_us /
6790 gran_to_us_table[peer_granularity - 1];
6791 new_peer_pa_tactivate++;
6792 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6793 new_peer_pa_tactivate);
6794 }
6795
6796out:
6797 return ret;
6798}
6799
Bean Huo09750062020-01-20 14:08:14 +01006800static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
Yaniv Gardi37113102016-03-10 17:37:16 +02006801{
6802 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6803 ufshcd_tune_pa_tactivate(hba);
6804 ufshcd_tune_pa_hibern8time(hba);
6805 }
6806
6807 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6808 /* set 1ms timeout for PA_TACTIVATE */
6809 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006810
6811 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6812 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08006813
Bean Huo09750062020-01-20 14:08:14 +01006814 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006815}
6816
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006817static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6818{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006819 hba->ufs_stats.hibern8_exit_cnt = 0;
6820 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08006821 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006822}
6823
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006824static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
6825{
6826 int err;
6827
6828 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
6829 &hba->desc_size.dev_desc);
6830 if (err)
6831 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
6832
6833 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
6834 &hba->desc_size.pwr_desc);
6835 if (err)
6836 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
6837
6838 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
6839 &hba->desc_size.interc_desc);
6840 if (err)
6841 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
6842
6843 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
6844 &hba->desc_size.conf_desc);
6845 if (err)
6846 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
6847
6848 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
6849 &hba->desc_size.unit_desc);
6850 if (err)
6851 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
6852
6853 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
6854 &hba->desc_size.geom_desc);
6855 if (err)
6856 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
Bean Huo059efd82019-10-29 14:22:45 +00006857
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02006858 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
6859 &hba->desc_size.hlth_desc);
6860 if (err)
6861 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006862}
6863
Bean Huo731f0622020-01-20 14:08:19 +01006864static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
6865{
6866 int err;
6867 size_t buff_len;
6868 u8 *desc_buf;
6869
6870 buff_len = hba->desc_size.geom_desc;
6871 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6872 if (!desc_buf) {
6873 err = -ENOMEM;
6874 goto out;
6875 }
6876
6877 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_GEOMETRY, 0,
6878 desc_buf, buff_len);
6879 if (err) {
6880 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
6881 __func__, err);
6882 goto out;
6883 }
6884
6885 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
6886 hba->dev_info.max_lu_supported = 32;
6887 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
6888 hba->dev_info.max_lu_supported = 8;
6889
6890out:
6891 kfree(desc_buf);
6892 return err;
6893}
6894
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05306895static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
6896 {19200000, REF_CLK_FREQ_19_2_MHZ},
6897 {26000000, REF_CLK_FREQ_26_MHZ},
6898 {38400000, REF_CLK_FREQ_38_4_MHZ},
6899 {52000000, REF_CLK_FREQ_52_MHZ},
6900 {0, REF_CLK_FREQ_INVAL},
6901};
6902
6903static enum ufs_ref_clk_freq
6904ufs_get_bref_clk_from_hz(unsigned long freq)
6905{
6906 int i;
6907
6908 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
6909 if (ufs_ref_clk_freqs[i].freq_hz == freq)
6910 return ufs_ref_clk_freqs[i].val;
6911
6912 return REF_CLK_FREQ_INVAL;
6913}
6914
6915void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
6916{
6917 unsigned long freq;
6918
6919 freq = clk_get_rate(refclk);
6920
6921 hba->dev_ref_clk_freq =
6922 ufs_get_bref_clk_from_hz(freq);
6923
6924 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
6925 dev_err(hba->dev,
6926 "invalid ref_clk setting = %ld\n", freq);
6927}
6928
6929static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
6930{
6931 int err;
6932 u32 ref_clk;
6933 u32 freq = hba->dev_ref_clk_freq;
6934
6935 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
6936 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
6937
6938 if (err) {
6939 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
6940 err);
6941 goto out;
6942 }
6943
6944 if (ref_clk == freq)
6945 goto out; /* nothing to update */
6946
6947 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
6948 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
6949
6950 if (err) {
6951 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
6952 ufs_ref_clk_freqs[freq].freq_hz);
6953 goto out;
6954 }
6955
6956 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
6957 ufs_ref_clk_freqs[freq].freq_hz);
6958
6959out:
6960 return err;
6961}
6962
Bean Huo1b9e2142020-01-20 14:08:15 +01006963static int ufshcd_device_params_init(struct ufs_hba *hba)
6964{
6965 bool flag;
6966 int ret;
6967
Bean Huo731f0622020-01-20 14:08:19 +01006968 /* Clear any previous UFS device information */
6969 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
6970
Bean Huo1b9e2142020-01-20 14:08:15 +01006971 /* Init check for device descriptor sizes */
6972 ufshcd_init_desc_sizes(hba);
6973
Bean Huo731f0622020-01-20 14:08:19 +01006974 /* Init UFS geometry descriptor related parameters */
6975 ret = ufshcd_device_geo_params_init(hba);
6976 if (ret)
6977 goto out;
6978
Bean Huo1b9e2142020-01-20 14:08:15 +01006979 /* Check and apply UFS device quirks */
6980 ret = ufs_get_device_desc(hba);
6981 if (ret) {
6982 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
6983 __func__, ret);
6984 goto out;
6985 }
6986
6987 ufs_fixup_device_setup(hba);
6988
Bean Huo1b9e2142020-01-20 14:08:15 +01006989 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6990 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
6991 hba->dev_info.f_power_on_wp_en = flag;
6992
Bean Huo2b35b2a2020-01-20 14:08:16 +01006993 /* Probe maximum power mode co-supported by both UFS host and device */
6994 if (ufshcd_get_max_pwr_mode(hba))
6995 dev_err(hba->dev,
6996 "%s: Failed getting max supported power mode\n",
6997 __func__);
Bean Huo1b9e2142020-01-20 14:08:15 +01006998out:
6999 return ret;
7000}
7001
7002/**
7003 * ufshcd_add_lus - probe and add UFS logical units
7004 * @hba: per-adapter instance
7005 */
7006static int ufshcd_add_lus(struct ufs_hba *hba)
7007{
7008 int ret;
7009
Bean Huo046c1e62020-01-20 14:08:18 +01007010 ufshcd_init_icc_levels(hba);
Bean Huo1b9e2142020-01-20 14:08:15 +01007011
7012 /* Add required well known logical units to scsi mid layer */
7013 ret = ufshcd_scsi_add_wlus(hba);
7014 if (ret)
7015 goto out;
7016
7017 /* Initialize devfreq after UFS device is detected */
7018 if (ufshcd_is_clkscaling_supported(hba)) {
7019 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7020 &hba->pwr_info,
7021 sizeof(struct ufs_pa_layer_attr));
7022 hba->clk_scaling.saved_pwr_info.is_valid = true;
7023 if (!hba->devfreq) {
7024 ret = ufshcd_devfreq_init(hba);
7025 if (ret)
7026 goto out;
7027 }
7028
7029 hba->clk_scaling.is_allowed = true;
7030 }
7031
7032 ufs_bsg_probe(hba);
7033 scsi_scan_host(hba->host);
7034 pm_runtime_put_sync(hba->dev);
7035
Bean Huo1b9e2142020-01-20 14:08:15 +01007036out:
7037 return ret;
7038}
7039
Yaniv Gardi37113102016-03-10 17:37:16 +02007040/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007041 * ufshcd_probe_hba - probe hba to detect device and initialize
7042 * @hba: per-adapter instance
Bean Huo1b9e2142020-01-20 14:08:15 +01007043 * @async: asynchronous execution or not
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007044 *
7045 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307046 */
Bean Huo1b9e2142020-01-20 14:08:15 +01007047static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307048{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307049 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007050 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307051
7052 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307053 if (ret)
7054 goto out;
7055
Yaniv Gardiafdfff52016-03-10 17:37:15 +02007056 /* set the default level for urgent bkops */
7057 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
7058 hba->is_urgent_bkops_lvl_checked = false;
7059
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007060 /* Debug counters initialization */
7061 ufshcd_clear_dbg_ufs_stats(hba);
7062
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007063 /* UniPro link is active now */
7064 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05307065
Bean Huo1b9e2142020-01-20 14:08:15 +01007066 /* Verify device initialization by sending NOP OUT UPIU */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307067 ret = ufshcd_verify_dev_init(hba);
7068 if (ret)
7069 goto out;
7070
Bean Huo1b9e2142020-01-20 14:08:15 +01007071 /* Initiate UFS initialization, and waiting until completion */
Dolev Raviv68078d52013-07-30 00:35:58 +05307072 ret = ufshcd_complete_dev_init(hba);
7073 if (ret)
7074 goto out;
7075
Bean Huo1b9e2142020-01-20 14:08:15 +01007076 /*
7077 * Initialize UFS device parameters used by driver, these
7078 * parameters are associated with UFS descriptors.
7079 */
7080 if (async) {
7081 ret = ufshcd_device_params_init(hba);
7082 if (ret)
7083 goto out;
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02007084 }
7085
Bean Huo09750062020-01-20 14:08:14 +01007086 ufshcd_tune_unipro_params(hba);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007087
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007088 /* UFS device is also active now */
7089 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307090 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007091 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307092
Bean Huo2b35b2a2020-01-20 14:08:16 +01007093 /* Gear up to HS gear if supported */
7094 if (hba->max_pwr_info.is_valid) {
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307095 /*
7096 * Set the right value to bRefClkFreq before attempting to
7097 * switch to HS gears.
7098 */
7099 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7100 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007101 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007102 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007103 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7104 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007105 goto out;
7106 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007107 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007108
Yaniv Gardi53c12d02016-02-01 15:02:45 +02007109 /* set the state as operational after switching to desired gear */
7110 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00007111
Can Guo71d848b2019-11-14 22:09:26 -08007112 /* Enable Auto-Hibernate if configured */
7113 ufshcd_auto_hibern8_enable(hba);
7114
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307115out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007116
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007117 trace_ufshcd_init(dev_name(hba->dev), ret,
7118 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007119 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007120 return ret;
7121}
7122
7123/**
7124 * ufshcd_async_scan - asynchronous execution for probing hba
7125 * @data: data pointer to pass to this function
7126 * @cookie: cookie data
7127 */
7128static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7129{
7130 struct ufs_hba *hba = (struct ufs_hba *)data;
Bean Huo1b9e2142020-01-20 14:08:15 +01007131 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007132
Bean Huo1b9e2142020-01-20 14:08:15 +01007133 /* Initialize hba, detect and initialize UFS device */
7134 ret = ufshcd_probe_hba(hba, true);
7135 if (ret)
7136 goto out;
7137
7138 /* Probe and add UFS logical units */
7139 ret = ufshcd_add_lus(hba);
7140out:
7141 /*
7142 * If we failed to initialize the device or the device is not
7143 * present, turn off the power/clocks etc.
7144 */
7145 if (ret) {
7146 pm_runtime_put_sync(hba->dev);
7147 ufshcd_exit_clk_scaling(hba);
7148 ufshcd_hba_exit(hba);
7149 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307150}
7151
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007152static const struct attribute_group *ufshcd_driver_groups[] = {
7153 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02007154 &ufs_sysfs_lun_attributes_group,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007155 NULL,
7156};
7157
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307158static struct scsi_host_template ufshcd_driver_template = {
7159 .module = THIS_MODULE,
7160 .name = UFSHCD,
7161 .proc_name = UFSHCD,
7162 .queuecommand = ufshcd_queuecommand,
7163 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007164 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307165 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007166 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307167 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307168 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7169 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307170 .this_id = -1,
7171 .sg_tablesize = SG_ALL,
7172 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7173 .can_queue = UFSHCD_CAN_QUEUE,
Christoph Hellwig552a9902019-06-17 14:19:55 +02007174 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007175 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007176 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007177 .sdev_groups = ufshcd_driver_groups,
Christoph Hellwig4af14d12018-12-13 16:17:09 +01007178 .dma_boundary = PAGE_SIZE - 1,
Stanley Chu49615ba2019-09-16 23:56:50 +08007179 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307180};
7181
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007182static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7183 int ua)
7184{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007185 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007186
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007187 if (!vreg)
7188 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007189
Stanley Chu0487fff2019-03-28 17:16:25 +08007190 /*
7191 * "set_load" operation shall be required on those regulators
7192 * which specifically configured current limitation. Otherwise
7193 * zero max_uA may cause unexpected behavior when regulator is
7194 * enabled or set as high power mode.
7195 */
7196 if (!vreg->max_uA)
7197 return 0;
7198
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007199 ret = regulator_set_load(vreg->reg, ua);
7200 if (ret < 0) {
7201 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7202 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007203 }
7204
7205 return ret;
7206}
7207
7208static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7209 struct ufs_vreg *vreg)
7210{
Marc Gonzalez73067982019-02-27 11:41:45 +01007211 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007212}
7213
7214static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7215 struct ufs_vreg *vreg)
7216{
Adrian Hunter7c7cfdc2019-08-14 15:59:50 +03007217 if (!vreg)
7218 return 0;
7219
Marc Gonzalez73067982019-02-27 11:41:45 +01007220 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007221}
7222
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007223static int ufshcd_config_vreg(struct device *dev,
7224 struct ufs_vreg *vreg, bool on)
7225{
7226 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007227 struct regulator *reg;
7228 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007229 int min_uV, uA_load;
7230
7231 BUG_ON(!vreg);
7232
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007233 reg = vreg->reg;
7234 name = vreg->name;
7235
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007236 if (regulator_count_voltages(reg) > 0) {
Stanley Chu3b141e82019-03-28 17:16:24 +08007237 if (vreg->min_uV && vreg->max_uV) {
7238 min_uV = on ? vreg->min_uV : 0;
7239 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7240 if (ret) {
7241 dev_err(dev,
7242 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007243 __func__, name, ret);
Stanley Chu3b141e82019-03-28 17:16:24 +08007244 goto out;
7245 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007246 }
7247
7248 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007249 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7250 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007251 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007252 }
7253out:
7254 return ret;
7255}
7256
7257static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7258{
7259 int ret = 0;
7260
Marc Gonzalez73067982019-02-27 11:41:45 +01007261 if (!vreg || vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007262 goto out;
7263
7264 ret = ufshcd_config_vreg(dev, vreg, true);
7265 if (!ret)
7266 ret = regulator_enable(vreg->reg);
7267
7268 if (!ret)
7269 vreg->enabled = true;
7270 else
7271 dev_err(dev, "%s: %s enable failed, err=%d\n",
7272 __func__, vreg->name, ret);
7273out:
7274 return ret;
7275}
7276
7277static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7278{
7279 int ret = 0;
7280
Marc Gonzalez73067982019-02-27 11:41:45 +01007281 if (!vreg || !vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007282 goto out;
7283
7284 ret = regulator_disable(vreg->reg);
7285
7286 if (!ret) {
7287 /* ignore errors on applying disable config */
7288 ufshcd_config_vreg(dev, vreg, false);
7289 vreg->enabled = false;
7290 } else {
7291 dev_err(dev, "%s: %s disable failed, err=%d\n",
7292 __func__, vreg->name, ret);
7293 }
7294out:
7295 return ret;
7296}
7297
7298static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7299{
7300 int ret = 0;
7301 struct device *dev = hba->dev;
7302 struct ufs_vreg_info *info = &hba->vreg_info;
7303
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007304 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7305 if (ret)
7306 goto out;
7307
7308 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7309 if (ret)
7310 goto out;
7311
7312 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7313 if (ret)
7314 goto out;
7315
7316out:
7317 if (ret) {
7318 ufshcd_toggle_vreg(dev, info->vccq2, false);
7319 ufshcd_toggle_vreg(dev, info->vccq, false);
7320 ufshcd_toggle_vreg(dev, info->vcc, false);
7321 }
7322 return ret;
7323}
7324
Raviv Shvili6a771a62014-09-25 15:32:24 +03007325static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7326{
7327 struct ufs_vreg_info *info = &hba->vreg_info;
7328
Zeng Guangyue60b7b822019-03-30 17:03:13 +08007329 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007330}
7331
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007332static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7333{
7334 int ret = 0;
7335
7336 if (!vreg)
7337 goto out;
7338
7339 vreg->reg = devm_regulator_get(dev, vreg->name);
7340 if (IS_ERR(vreg->reg)) {
7341 ret = PTR_ERR(vreg->reg);
7342 dev_err(dev, "%s: %s get failed, err=%d\n",
7343 __func__, vreg->name, ret);
7344 }
7345out:
7346 return ret;
7347}
7348
7349static int ufshcd_init_vreg(struct ufs_hba *hba)
7350{
7351 int ret = 0;
7352 struct device *dev = hba->dev;
7353 struct ufs_vreg_info *info = &hba->vreg_info;
7354
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007355 ret = ufshcd_get_vreg(dev, info->vcc);
7356 if (ret)
7357 goto out;
7358
7359 ret = ufshcd_get_vreg(dev, info->vccq);
7360 if (ret)
7361 goto out;
7362
7363 ret = ufshcd_get_vreg(dev, info->vccq2);
7364out:
7365 return ret;
7366}
7367
Raviv Shvili6a771a62014-09-25 15:32:24 +03007368static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
7369{
7370 struct ufs_vreg_info *info = &hba->vreg_info;
7371
7372 if (info)
7373 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
7374
7375 return 0;
7376}
7377
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007378static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
7379 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007380{
7381 int ret = 0;
7382 struct ufs_clk_info *clki;
7383 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007384 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007385 ktime_t start = ktime_get();
7386 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007387
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007388 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007389 goto out;
7390
Subhash Jadavanib3344562018-05-03 16:37:17 +05307391 /*
7392 * vendor specific setup_clocks ops may depend on clocks managed by
7393 * this standard driver hence call the vendor specific setup_clocks
7394 * before disabling the clocks managed here.
7395 */
7396 if (!on) {
7397 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
7398 if (ret)
7399 return ret;
7400 }
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007401
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007402 list_for_each_entry(clki, head, list) {
7403 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007404 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
7405 continue;
7406
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007407 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007408 if (on && !clki->enabled) {
7409 ret = clk_prepare_enable(clki->clk);
7410 if (ret) {
7411 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
7412 __func__, clki->name, ret);
7413 goto out;
7414 }
7415 } else if (!on && clki->enabled) {
7416 clk_disable_unprepare(clki->clk);
7417 }
7418 clki->enabled = on;
7419 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
7420 clki->name, on ? "en" : "dis");
7421 }
7422 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007423
Subhash Jadavanib3344562018-05-03 16:37:17 +05307424 /*
7425 * vendor specific setup_clocks ops may depend on clocks managed by
7426 * this standard driver hence call the vendor specific setup_clocks
7427 * after enabling the clocks managed here.
7428 */
7429 if (on) {
7430 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
7431 if (ret)
7432 return ret;
7433 }
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007434
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007435out:
7436 if (ret) {
7437 list_for_each_entry(clki, head, list) {
7438 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
7439 clk_disable_unprepare(clki->clk);
7440 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007441 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007442 spin_lock_irqsave(hba->host->host_lock, flags);
7443 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007444 trace_ufshcd_clk_gating(dev_name(hba->dev),
7445 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007446 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007447 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007448
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007449 if (clk_state_changed)
7450 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
7451 (on ? "on" : "off"),
7452 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007453 return ret;
7454}
7455
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007456static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
7457{
7458 return __ufshcd_setup_clocks(hba, on, false);
7459}
7460
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007461static int ufshcd_init_clocks(struct ufs_hba *hba)
7462{
7463 int ret = 0;
7464 struct ufs_clk_info *clki;
7465 struct device *dev = hba->dev;
7466 struct list_head *head = &hba->clk_list_head;
7467
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007468 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007469 goto out;
7470
7471 list_for_each_entry(clki, head, list) {
7472 if (!clki->name)
7473 continue;
7474
7475 clki->clk = devm_clk_get(dev, clki->name);
7476 if (IS_ERR(clki->clk)) {
7477 ret = PTR_ERR(clki->clk);
7478 dev_err(dev, "%s: %s clk get failed, %d\n",
7479 __func__, clki->name, ret);
7480 goto out;
7481 }
7482
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307483 /*
7484 * Parse device ref clk freq as per device tree "ref_clk".
7485 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
7486 * in ufshcd_alloc_host().
7487 */
7488 if (!strcmp(clki->name, "ref_clk"))
7489 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
7490
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007491 if (clki->max_freq) {
7492 ret = clk_set_rate(clki->clk, clki->max_freq);
7493 if (ret) {
7494 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7495 __func__, clki->name,
7496 clki->max_freq, ret);
7497 goto out;
7498 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03007499 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007500 }
7501 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
7502 clki->name, clk_get_rate(clki->clk));
7503 }
7504out:
7505 return ret;
7506}
7507
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007508static int ufshcd_variant_hba_init(struct ufs_hba *hba)
7509{
7510 int err = 0;
7511
7512 if (!hba->vops)
7513 goto out;
7514
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007515 err = ufshcd_vops_init(hba);
7516 if (err)
7517 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007518
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007519 err = ufshcd_vops_setup_regulators(hba, true);
7520 if (err)
7521 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007522
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007523 goto out;
7524
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007525out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007526 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007527out:
7528 if (err)
7529 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007530 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007531 return err;
7532}
7533
7534static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
7535{
7536 if (!hba->vops)
7537 return;
7538
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007539 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007540
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007541 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007542}
7543
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007544static int ufshcd_hba_init(struct ufs_hba *hba)
7545{
7546 int err;
7547
Raviv Shvili6a771a62014-09-25 15:32:24 +03007548 /*
7549 * Handle host controller power separately from the UFS device power
7550 * rails as it will help controlling the UFS host controller power
7551 * collapse easily which is different than UFS device power collapse.
7552 * Also, enable the host controller power before we go ahead with rest
7553 * of the initialization here.
7554 */
7555 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007556 if (err)
7557 goto out;
7558
Raviv Shvili6a771a62014-09-25 15:32:24 +03007559 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007560 if (err)
7561 goto out;
7562
Raviv Shvili6a771a62014-09-25 15:32:24 +03007563 err = ufshcd_init_clocks(hba);
7564 if (err)
7565 goto out_disable_hba_vreg;
7566
7567 err = ufshcd_setup_clocks(hba, true);
7568 if (err)
7569 goto out_disable_hba_vreg;
7570
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007571 err = ufshcd_init_vreg(hba);
7572 if (err)
7573 goto out_disable_clks;
7574
7575 err = ufshcd_setup_vreg(hba, true);
7576 if (err)
7577 goto out_disable_clks;
7578
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007579 err = ufshcd_variant_hba_init(hba);
7580 if (err)
7581 goto out_disable_vreg;
7582
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007583 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007584 goto out;
7585
7586out_disable_vreg:
7587 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007588out_disable_clks:
7589 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007590out_disable_hba_vreg:
7591 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007592out:
7593 return err;
7594}
7595
7596static void ufshcd_hba_exit(struct ufs_hba *hba)
7597{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007598 if (hba->is_powered) {
7599 ufshcd_variant_hba_exit(hba);
7600 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07007601 ufshcd_suspend_clkscaling(hba);
Vivek Gautameebcc192018-08-07 23:17:39 +05307602 if (ufshcd_is_clkscaling_supported(hba))
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08007603 if (hba->devfreq)
7604 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007605 ufshcd_setup_clocks(hba, false);
7606 ufshcd_setup_hba_vreg(hba, false);
7607 hba->is_powered = false;
Bean Huo09750062020-01-20 14:08:14 +01007608 ufs_put_device_desc(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007609 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007610}
7611
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007612static int
7613ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307614{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007615 unsigned char cmd[6] = {REQUEST_SENSE,
7616 0,
7617 0,
7618 0,
Avri Altman09a5a242018-11-22 20:04:56 +02007619 UFS_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007620 0};
7621 char *buffer;
7622 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307623
Avri Altman09a5a242018-11-22 20:04:56 +02007624 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007625 if (!buffer) {
7626 ret = -ENOMEM;
7627 goto out;
7628 }
7629
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007630 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
Avri Altman09a5a242018-11-22 20:04:56 +02007631 UFS_SENSE_SIZE, NULL, NULL,
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007632 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007633 if (ret)
7634 pr_err("%s: failed with err %d\n", __func__, ret);
7635
7636 kfree(buffer);
7637out:
7638 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307639}
7640
7641/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007642 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
7643 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307644 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007645 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307646 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007647 * Returns 0 if requested power mode is set successfully
7648 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307649 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007650static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
7651 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307652{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007653 unsigned char cmd[6] = { START_STOP };
7654 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007655 struct scsi_device *sdp;
7656 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007657 int ret;
7658
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007659 spin_lock_irqsave(hba->host->host_lock, flags);
7660 sdp = hba->sdev_ufs_device;
7661 if (sdp) {
7662 ret = scsi_device_get(sdp);
7663 if (!ret && !scsi_device_online(sdp)) {
7664 ret = -ENODEV;
7665 scsi_device_put(sdp);
7666 }
7667 } else {
7668 ret = -ENODEV;
7669 }
7670 spin_unlock_irqrestore(hba->host->host_lock, flags);
7671
7672 if (ret)
7673 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007674
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307675 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007676 * If scsi commands fail, the scsi mid-layer schedules scsi error-
7677 * handling, which would wait for host to be resumed. Since we know
7678 * we are functional while we are here, skip host resume in error
7679 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307680 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007681 hba->host->eh_noresume = 1;
7682 if (hba->wlun_dev_clr_ua) {
7683 ret = ufshcd_send_request_sense(hba, sdp);
7684 if (ret)
7685 goto out;
7686 /* Unit attention condition is cleared now */
7687 hba->wlun_dev_clr_ua = false;
7688 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307689
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007690 cmd[4] = pwr_mode << 4;
7691
7692 /*
7693 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02007694 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007695 * already suspended childs.
7696 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007697 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
7698 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007699 if (ret) {
7700 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02007701 "START_STOP failed for power mode: %d, result %x\n",
7702 pwr_mode, ret);
Johannes Thumshirnc65be1a2018-06-25 13:20:58 +02007703 if (driver_byte(ret) == DRIVER_SENSE)
Hannes Reinecke21045512015-01-08 07:43:46 +01007704 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007705 }
7706
7707 if (!ret)
7708 hba->curr_dev_pwr_mode = pwr_mode;
7709out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007710 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007711 hba->host->eh_noresume = 0;
7712 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307713}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307714
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007715static int ufshcd_link_state_transition(struct ufs_hba *hba,
7716 enum uic_link_state req_link_state,
7717 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307718{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007719 int ret = 0;
7720
7721 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307722 return 0;
7723
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007724 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7725 ret = ufshcd_uic_hibern8_enter(hba);
7726 if (!ret)
7727 ufshcd_set_link_hibern8(hba);
7728 else
7729 goto out;
7730 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307731 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007732 * If autobkops is enabled, link can't be turned off because
7733 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307734 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007735 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
Dan Carpenterdc30c9e2019-12-13 13:49:35 +03007736 (!check_for_bkops || !hba->auto_bkops_enabled)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007737 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007738 * Let's make sure that link is in low power mode, we are doing
7739 * this currently by putting the link in Hibern8. Otherway to
7740 * put the link in low power mode is to send the DME end point
7741 * to device and then send the DME reset command to local
7742 * unipro. But putting the link in hibern8 is much faster.
7743 */
7744 ret = ufshcd_uic_hibern8_enter(hba);
7745 if (ret)
7746 goto out;
7747 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007748 * Change controller state to "reset state" which
7749 * should also put the link in off/reset state
7750 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007751 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007752 /*
7753 * TODO: Check if we need any delay to make sure that
7754 * controller is reset
7755 */
7756 ufshcd_set_link_off(hba);
7757 }
7758
7759out:
7760 return ret;
7761}
7762
7763static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7764{
7765 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007766 * It seems some UFS devices may keep drawing more than sleep current
7767 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7768 * To avoid this situation, add 2ms delay before putting these UFS
7769 * rails in LPM mode.
7770 */
7771 if (!ufshcd_is_link_active(hba) &&
7772 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
7773 usleep_range(2000, 2100);
7774
7775 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007776 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7777 * power.
7778 *
7779 * If UFS device and link is in OFF state, all power supplies (VCC,
7780 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7781 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7782 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7783 *
7784 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7785 * in low power state which would save some power.
7786 */
7787 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7788 !hba->dev_info.is_lu_power_on_wp) {
7789 ufshcd_setup_vreg(hba, false);
7790 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7791 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7792 if (!ufshcd_is_link_active(hba)) {
7793 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7794 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7795 }
7796 }
7797}
7798
7799static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7800{
7801 int ret = 0;
7802
7803 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7804 !hba->dev_info.is_lu_power_on_wp) {
7805 ret = ufshcd_setup_vreg(hba, true);
7806 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007807 if (!ret && !ufshcd_is_link_active(hba)) {
7808 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7809 if (ret)
7810 goto vcc_disable;
7811 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7812 if (ret)
7813 goto vccq_lpm;
7814 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07007815 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007816 }
7817 goto out;
7818
7819vccq_lpm:
7820 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7821vcc_disable:
7822 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7823out:
7824 return ret;
7825}
7826
7827static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7828{
7829 if (ufshcd_is_link_off(hba))
7830 ufshcd_setup_hba_vreg(hba, false);
7831}
7832
7833static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7834{
7835 if (ufshcd_is_link_off(hba))
7836 ufshcd_setup_hba_vreg(hba, true);
7837}
7838
7839/**
7840 * ufshcd_suspend - helper function for suspend operations
7841 * @hba: per adapter instance
7842 * @pm_op: desired low power operation type
7843 *
7844 * This function will try to put the UFS device and link into low power
7845 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7846 * (System PM level).
7847 *
7848 * If this function is called during shutdown, it will make sure that
7849 * both UFS device and UFS link is powered off.
7850 *
7851 * NOTE: UFS device & link must be active before we enter in this function.
7852 *
7853 * Returns 0 for success and non-zero for failure
7854 */
7855static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7856{
7857 int ret = 0;
7858 enum ufs_pm_level pm_lvl;
7859 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7860 enum uic_link_state req_link_state;
7861
7862 hba->pm_op_in_progress = 1;
7863 if (!ufshcd_is_shutdown_pm(pm_op)) {
7864 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7865 hba->rpm_lvl : hba->spm_lvl;
7866 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7867 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7868 } else {
7869 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7870 req_link_state = UIC_LINK_OFF_STATE;
7871 }
7872
7873 /*
7874 * If we can't transition into any of the low power modes
7875 * just gate the clocks.
7876 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007877 ufshcd_hold(hba, false);
7878 hba->clk_gating.is_suspended = true;
7879
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007880 if (hba->clk_scaling.is_allowed) {
7881 cancel_work_sync(&hba->clk_scaling.suspend_work);
7882 cancel_work_sync(&hba->clk_scaling.resume_work);
7883 ufshcd_suspend_clkscaling(hba);
7884 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007885
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007886 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
7887 req_link_state == UIC_LINK_ACTIVE_STATE) {
7888 goto disable_clks;
7889 }
7890
7891 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
7892 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007893 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007894
7895 /* UFS device & link must be active before we enter in this function */
7896 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
7897 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007898 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007899 }
7900
7901 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03007902 if (ufshcd_can_autobkops_during_suspend(hba)) {
7903 /*
7904 * The device is idle with no requests in the queue,
7905 * allow background operations if bkops status shows
7906 * that performance might be impacted.
7907 */
7908 ret = ufshcd_urgent_bkops(hba);
7909 if (ret)
7910 goto enable_gating;
7911 } else {
7912 /* make sure that auto bkops is disabled */
7913 ufshcd_disable_auto_bkops(hba);
7914 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007915 }
7916
7917 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
7918 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
7919 !ufshcd_is_runtime_pm(pm_op))) {
7920 /* ensure that bkops is disabled */
7921 ufshcd_disable_auto_bkops(hba);
7922 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
7923 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007924 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007925 }
7926
7927 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
7928 if (ret)
7929 goto set_dev_active;
7930
7931 ufshcd_vreg_set_lpm(hba);
7932
7933disable_clks:
7934 /*
7935 * Call vendor specific suspend callback. As these callbacks may access
7936 * vendor specific host controller register space call them before the
7937 * host clocks are ON.
7938 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007939 ret = ufshcd_vops_suspend(hba, pm_op);
7940 if (ret)
7941 goto set_link_active;
Stanley Chudcb6cec2019-12-07 20:22:00 +08007942 /*
7943 * Disable the host irq as host controller as there won't be any
7944 * host controller transaction expected till resume.
7945 */
7946 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007947
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007948 if (!ufshcd_is_link_active(hba))
7949 ufshcd_setup_clocks(hba, false);
7950 else
7951 /* If link is active, device ref_clk can't be switched off */
7952 __ufshcd_setup_clocks(hba, false, true);
7953
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007954 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007955 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Stanley Chudcb6cec2019-12-07 20:22:00 +08007956
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007957 /* Put the host controller in low power mode if possible */
7958 ufshcd_hba_vreg_set_lpm(hba);
7959 goto out;
7960
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007961set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007962 if (hba->clk_scaling.is_allowed)
7963 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007964 ufshcd_vreg_set_hpm(hba);
7965 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
7966 ufshcd_set_link_active(hba);
7967 else if (ufshcd_is_link_off(hba))
7968 ufshcd_host_reset_and_restore(hba);
7969set_dev_active:
7970 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
7971 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007972enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007973 if (hba->clk_scaling.is_allowed)
7974 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007975 hba->clk_gating.is_suspended = false;
7976 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007977out:
7978 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08007979 if (ret)
7980 ufshcd_update_reg_hist(&hba->ufs_stats.suspend_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007981 return ret;
7982}
7983
7984/**
7985 * ufshcd_resume - helper function for resume operations
7986 * @hba: per adapter instance
7987 * @pm_op: runtime PM or system PM
7988 *
7989 * This function basically brings the UFS device, UniPro link and controller
7990 * to active state.
7991 *
7992 * Returns 0 for success and non-zero for failure
7993 */
7994static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7995{
7996 int ret;
7997 enum uic_link_state old_link_state;
7998
7999 hba->pm_op_in_progress = 1;
8000 old_link_state = hba->uic_link_state;
8001
8002 ufshcd_hba_vreg_set_hpm(hba);
8003 /* Make sure clocks are enabled before accessing controller */
8004 ret = ufshcd_setup_clocks(hba, true);
8005 if (ret)
8006 goto out;
8007
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008008 /* enable the host irq as host controller would be active soon */
Can Guo5231d382019-12-05 02:14:46 +00008009 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008010
8011 ret = ufshcd_vreg_set_hpm(hba);
8012 if (ret)
8013 goto disable_irq_and_vops_clks;
8014
8015 /*
8016 * Call vendor specific resume callback. As these callbacks may access
8017 * vendor specific host controller register space call them when the
8018 * host clocks are ON.
8019 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008020 ret = ufshcd_vops_resume(hba, pm_op);
8021 if (ret)
8022 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008023
8024 if (ufshcd_is_link_hibern8(hba)) {
8025 ret = ufshcd_uic_hibern8_exit(hba);
8026 if (!ret)
8027 ufshcd_set_link_active(hba);
8028 else
8029 goto vendor_suspend;
8030 } else if (ufshcd_is_link_off(hba)) {
8031 ret = ufshcd_host_reset_and_restore(hba);
8032 /*
8033 * ufshcd_host_reset_and_restore() should have already
8034 * set the link state as active
8035 */
8036 if (ret || !ufshcd_is_link_active(hba))
8037 goto vendor_suspend;
8038 }
8039
8040 if (!ufshcd_is_ufs_dev_active(hba)) {
8041 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8042 if (ret)
8043 goto set_old_link_state;
8044 }
8045
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08008046 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8047 ufshcd_enable_auto_bkops(hba);
8048 else
8049 /*
8050 * If BKOPs operations are urgently needed at this moment then
8051 * keep auto-bkops enabled or else disable it.
8052 */
8053 ufshcd_urgent_bkops(hba);
8054
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008055 hba->clk_gating.is_suspended = false;
8056
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008057 if (hba->clk_scaling.is_allowed)
8058 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008059
Adrian Hunterad448372018-03-20 15:07:38 +02008060 /* Enable Auto-Hibernate if configured */
8061 ufshcd_auto_hibern8_enable(hba);
8062
Can Guo71d848b2019-11-14 22:09:26 -08008063 /* Schedule clock gating in case of no access to UFS device yet */
8064 ufshcd_release(hba);
8065
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008066 goto out;
8067
8068set_old_link_state:
8069 ufshcd_link_state_transition(hba, old_link_state, 0);
8070vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008071 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008072disable_vreg:
8073 ufshcd_vreg_set_lpm(hba);
8074disable_irq_and_vops_clks:
8075 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008076 if (hba->clk_scaling.is_allowed)
8077 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008078 ufshcd_setup_clocks(hba, false);
8079out:
8080 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08008081 if (ret)
8082 ufshcd_update_reg_hist(&hba->ufs_stats.resume_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008083 return ret;
8084}
8085
8086/**
8087 * ufshcd_system_suspend - system suspend routine
8088 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008089 *
8090 * Check the description of ufshcd_suspend() function for more details.
8091 *
8092 * Returns 0 for success and non-zero for failure
8093 */
8094int ufshcd_system_suspend(struct ufs_hba *hba)
8095{
8096 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008097 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008098
8099 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008100 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008101
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008102 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8103 hba->curr_dev_pwr_mode) &&
8104 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8105 hba->uic_link_state))
8106 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008107
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008108 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008109 /*
8110 * UFS device and/or UFS link low power states during runtime
8111 * suspend seems to be different than what is expected during
8112 * system suspend. Hence runtime resume the devic & link and
8113 * let the system suspend low power states to take effect.
8114 * TODO: If resume takes longer time, we might have optimize
8115 * it in future by not resuming everything if possible.
8116 */
8117 ret = ufshcd_runtime_resume(hba);
8118 if (ret)
8119 goto out;
8120 }
8121
8122 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8123out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008124 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8125 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008126 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008127 if (!ret)
8128 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008129 return ret;
8130}
8131EXPORT_SYMBOL(ufshcd_system_suspend);
8132
8133/**
8134 * ufshcd_system_resume - system resume routine
8135 * @hba: per adapter instance
8136 *
8137 * Returns 0 for success and non-zero for failure
8138 */
8139
8140int ufshcd_system_resume(struct ufs_hba *hba)
8141{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008142 int ret = 0;
8143 ktime_t start = ktime_get();
8144
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008145 if (!hba)
8146 return -EINVAL;
8147
8148 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008149 /*
8150 * Let the runtime resume take care of resuming
8151 * if runtime suspended.
8152 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008153 goto out;
8154 else
8155 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8156out:
8157 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8158 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008159 hba->curr_dev_pwr_mode, hba->uic_link_state);
Stanley Chuce9e7bc2019-01-07 22:19:34 +08008160 if (!ret)
8161 hba->is_sys_suspended = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008162 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008163}
8164EXPORT_SYMBOL(ufshcd_system_resume);
8165
8166/**
8167 * ufshcd_runtime_suspend - runtime suspend routine
8168 * @hba: per adapter instance
8169 *
8170 * Check the description of ufshcd_suspend() function for more details.
8171 *
8172 * Returns 0 for success and non-zero for failure
8173 */
8174int ufshcd_runtime_suspend(struct ufs_hba *hba)
8175{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008176 int ret = 0;
8177 ktime_t start = ktime_get();
8178
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008179 if (!hba)
8180 return -EINVAL;
8181
8182 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008183 goto out;
8184 else
8185 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8186out:
8187 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8188 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008189 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008190 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308191}
8192EXPORT_SYMBOL(ufshcd_runtime_suspend);
8193
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008194/**
8195 * ufshcd_runtime_resume - runtime resume routine
8196 * @hba: per adapter instance
8197 *
8198 * This function basically brings the UFS device, UniPro link and controller
8199 * to active state. Following operations are done in this function:
8200 *
8201 * 1. Turn on all the controller related clocks
8202 * 2. Bring the UniPro link out of Hibernate state
8203 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8204 * to active state.
8205 * 4. If auto-bkops is enabled on the device, disable it.
8206 *
8207 * So following would be the possible power state after this function return
8208 * successfully:
8209 * S1: UFS device in Active state with VCC rail ON
8210 * UniPro link in Active state
8211 * All the UFS/UniPro controller clocks are ON
8212 *
8213 * Returns 0 for success and non-zero for failure
8214 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308215int ufshcd_runtime_resume(struct ufs_hba *hba)
8216{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008217 int ret = 0;
8218 ktime_t start = ktime_get();
8219
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008220 if (!hba)
8221 return -EINVAL;
8222
8223 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008224 goto out;
8225 else
8226 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8227out:
8228 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8229 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008230 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008231 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308232}
8233EXPORT_SYMBOL(ufshcd_runtime_resume);
8234
8235int ufshcd_runtime_idle(struct ufs_hba *hba)
8236{
8237 return 0;
8238}
8239EXPORT_SYMBOL(ufshcd_runtime_idle);
8240
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308241/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008242 * ufshcd_shutdown - shutdown routine
8243 * @hba: per adapter instance
8244 *
8245 * This function would power off both UFS device and UFS link.
8246 *
8247 * Returns 0 always to allow force shutdown even in case of errors.
8248 */
8249int ufshcd_shutdown(struct ufs_hba *hba)
8250{
8251 int ret = 0;
8252
Stanley Chuf51913e2019-09-18 12:20:38 +08008253 if (!hba->is_powered)
8254 goto out;
8255
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008256 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
8257 goto out;
8258
8259 if (pm_runtime_suspended(hba->dev)) {
8260 ret = ufshcd_runtime_resume(hba);
8261 if (ret)
8262 goto out;
8263 }
8264
8265 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
8266out:
8267 if (ret)
8268 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
8269 /* allow force shutdown even in case of errors */
8270 return 0;
8271}
8272EXPORT_SYMBOL(ufshcd_shutdown);
8273
8274/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308275 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308276 * data structure memory
Bart Van Assche8aa29f12018-03-01 15:07:20 -08008277 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308278 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308279void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308280{
Avri Altmandf032bf2018-10-07 17:30:35 +03008281 ufs_bsg_remove(hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008282 ufs_sysfs_remove_nodes(hba->dev);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008283 blk_cleanup_queue(hba->tmf_queue);
8284 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008285 blk_cleanup_queue(hba->cmd_queue);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05308286 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308287 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308288 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02008289 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308290
Vivek Gautameebcc192018-08-07 23:17:39 +05308291 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008292 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008293 if (ufshcd_is_clkscaling_supported(hba))
8294 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008295 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308296}
8297EXPORT_SYMBOL_GPL(ufshcd_remove);
8298
8299/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02008300 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
8301 * @hba: pointer to Host Bus Adapter (HBA)
8302 */
8303void ufshcd_dealloc_host(struct ufs_hba *hba)
8304{
8305 scsi_host_put(hba->host);
8306}
8307EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
8308
8309/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008310 * ufshcd_set_dma_mask - Set dma mask based on the controller
8311 * addressing capability
8312 * @hba: per adapter instance
8313 *
8314 * Returns 0 for success, non-zero for failure
8315 */
8316static int ufshcd_set_dma_mask(struct ufs_hba *hba)
8317{
8318 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
8319 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
8320 return 0;
8321 }
8322 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
8323}
8324
8325/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008326 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308327 * @dev: pointer to device handle
8328 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308329 * Returns 0 on success, non-zero value on failure
8330 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008331int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308332{
8333 struct Scsi_Host *host;
8334 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008335 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308336
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308337 if (!dev) {
8338 dev_err(dev,
8339 "Invalid memory reference for dev is NULL\n");
8340 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308341 goto out_error;
8342 }
8343
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308344 host = scsi_host_alloc(&ufshcd_driver_template,
8345 sizeof(struct ufs_hba));
8346 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308347 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308348 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308349 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308350 }
8351 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308352 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308353 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008354 *hba_handle = hba;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308355 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008356
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008357 INIT_LIST_HEAD(&hba->clk_list_head);
8358
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008359out_error:
8360 return err;
8361}
8362EXPORT_SYMBOL(ufshcd_alloc_host);
8363
Bart Van Assche69a6c262019-12-09 10:13:09 -08008364/* This function exists because blk_mq_alloc_tag_set() requires this. */
8365static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
8366 const struct blk_mq_queue_data *qd)
8367{
8368 WARN_ON_ONCE(true);
8369 return BLK_STS_NOTSUPP;
8370}
8371
8372static const struct blk_mq_ops ufshcd_tmf_ops = {
8373 .queue_rq = ufshcd_queue_tmf,
8374};
8375
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008376/**
8377 * ufshcd_init - Driver initialization routine
8378 * @hba: per-adapter instance
8379 * @mmio_base: base register address
8380 * @irq: Interrupt line of device
8381 * Returns 0 on success, non-zero value on failure
8382 */
8383int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
8384{
8385 int err;
8386 struct Scsi_Host *host = hba->host;
8387 struct device *dev = hba->dev;
8388
8389 if (!mmio_base) {
8390 dev_err(hba->dev,
8391 "Invalid memory reference for mmio_base is NULL\n");
8392 err = -ENODEV;
8393 goto out_error;
8394 }
8395
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308396 hba->mmio_base = mmio_base;
8397 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308398
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008399 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008400 if (err)
8401 goto out_error;
8402
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308403 /* Read capabilities registers */
8404 ufshcd_hba_capabilities(hba);
8405
8406 /* Get UFS version supported by the controller */
8407 hba->ufs_version = ufshcd_get_ufs_version(hba);
8408
Yaniv Gardic01848c2016-12-05 19:25:02 -08008409 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
8410 (hba->ufs_version != UFSHCI_VERSION_11) &&
8411 (hba->ufs_version != UFSHCI_VERSION_20) &&
8412 (hba->ufs_version != UFSHCI_VERSION_21))
8413 dev_err(hba->dev, "invalid UFS version 0x%x\n",
8414 hba->ufs_version);
8415
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308416 /* Get Interrupt bit mask per version */
8417 hba->intr_mask = ufshcd_get_intr_mask(hba);
8418
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008419 err = ufshcd_set_dma_mask(hba);
8420 if (err) {
8421 dev_err(hba->dev, "set dma mask failed\n");
8422 goto out_disable;
8423 }
8424
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308425 /* Allocate memory for host memory space */
8426 err = ufshcd_memory_alloc(hba);
8427 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308428 dev_err(hba->dev, "Memory allocation failed\n");
8429 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308430 }
8431
8432 /* Configure LRB */
8433 ufshcd_host_memory_configure(hba);
8434
8435 host->can_queue = hba->nutrs;
8436 host->cmd_per_lun = hba->nutrs;
8437 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03008438 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308439 host->max_channel = UFSHCD_MAX_CHANNEL;
8440 host->unique_id = host->host_no;
Avri Altmana851b2b2018-10-07 17:30:34 +03008441 host->max_cmd_len = UFS_CDB_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308442
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008443 hba->max_pwr_info.is_valid = false;
8444
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308445 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05308446 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308447 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308448
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308449 /* Initialize UIC command mutex */
8450 mutex_init(&hba->uic_cmd_mutex);
8451
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308452 /* Initialize mutex for device management commands */
8453 mutex_init(&hba->dev_cmd.lock);
8454
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08008455 init_rwsem(&hba->clk_scaling_lock);
8456
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008457 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02008458
Vivek Gautameebcc192018-08-07 23:17:39 +05308459 ufshcd_init_clk_scaling(hba);
8460
Yaniv Gardi199ef132016-03-10 17:37:06 +02008461 /*
8462 * In order to avoid any spurious interrupt immediately after
8463 * registering UFS controller interrupt handler, clear any pending UFS
8464 * interrupt status and disable all the UFS interrupts.
8465 */
8466 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
8467 REG_INTERRUPT_STATUS);
8468 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
8469 /*
8470 * Make sure that UFS interrupts are disabled and any pending interrupt
8471 * status is cleared before registering UFS interrupt handler.
8472 */
8473 mb();
8474
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308475 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09008476 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308477 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308478 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008479 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008480 } else {
8481 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308482 }
8483
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308484 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308485 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308486 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008487 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308488 }
8489
Bart Van Assche7252a362019-12-09 10:13:08 -08008490 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
8491 if (IS_ERR(hba->cmd_queue)) {
8492 err = PTR_ERR(hba->cmd_queue);
8493 goto out_remove_scsi_host;
8494 }
8495
Bart Van Assche69a6c262019-12-09 10:13:09 -08008496 hba->tmf_tag_set = (struct blk_mq_tag_set) {
8497 .nr_hw_queues = 1,
8498 .queue_depth = hba->nutmrs,
8499 .ops = &ufshcd_tmf_ops,
8500 .flags = BLK_MQ_F_NO_SCHED,
8501 };
8502 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
8503 if (err < 0)
8504 goto free_cmd_queue;
8505 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
8506 if (IS_ERR(hba->tmf_queue)) {
8507 err = PTR_ERR(hba->tmf_queue);
8508 goto free_tmf_tag_set;
8509 }
8510
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07008511 /* Reset the attached device */
8512 ufshcd_vops_device_reset(hba);
8513
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308514 /* Host controller enable */
8515 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308516 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308517 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08008518 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08008519 ufshcd_print_host_state(hba);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008520 goto free_tmf_queue;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308521 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308522
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08008523 /*
8524 * Set the default power management level for runtime and system PM.
8525 * Default power saving mode is to keep UFS link in Hibern8 state
8526 * and UFS device in sleep state.
8527 */
8528 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8529 UFS_SLEEP_PWR_MODE,
8530 UIC_LINK_HIBERN8_STATE);
8531 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8532 UFS_SLEEP_PWR_MODE,
8533 UIC_LINK_HIBERN8_STATE);
8534
Adrian Hunterad448372018-03-20 15:07:38 +02008535 /* Set the default auto-hiberate idle timer value to 150 ms */
Stanley Chuf571b372019-05-21 14:44:53 +08008536 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
Adrian Hunterad448372018-03-20 15:07:38 +02008537 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
8538 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
8539 }
8540
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05308541 /* Hold auto suspend until async scan completes */
8542 pm_runtime_get_sync(dev);
Subhash Jadavani38135532018-05-03 16:37:18 +05308543 atomic_set(&hba->scsi_block_reqs_cnt, 0);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008544 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008545 * We are assuming that device wasn't put in sleep/power-down
8546 * state exclusively during the boot stage before kernel.
8547 * This assumption helps avoid doing link startup twice during
8548 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008549 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008550 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008551
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308552 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008553 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308554
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308555 return 0;
8556
Bart Van Assche69a6c262019-12-09 10:13:09 -08008557free_tmf_queue:
8558 blk_cleanup_queue(hba->tmf_queue);
8559free_tmf_tag_set:
8560 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008561free_cmd_queue:
8562 blk_cleanup_queue(hba->cmd_queue);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308563out_remove_scsi_host:
8564 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008565exit_gating:
Vivek Gautameebcc192018-08-07 23:17:39 +05308566 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008567 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308568out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008569 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008570 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308571out_error:
8572 return err;
8573}
8574EXPORT_SYMBOL_GPL(ufshcd_init);
8575
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308576MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
8577MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05308578MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308579MODULE_LICENSE("GPL");
8580MODULE_VERSION(UFSHCD_DRIVER_VERSION);