blob: 2d705694636c235c71534cdae3b13eb17bd4a6a8 [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;
Can Guobe7594a2020-03-05 00:53:07 -08003887 bool update = false;
Stanley Chuba7af5e2019-12-30 13:32:28 +08003888
Can Guobe7594a2020-03-05 00:53:07 -08003889 if (!ufshcd_is_auto_hibern8_supported(hba))
Stanley Chuba7af5e2019-12-30 13:32:28 +08003890 return;
3891
3892 spin_lock_irqsave(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08003893 if (hba->ahit != ahit) {
3894 hba->ahit = ahit;
3895 update = true;
3896 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08003897 spin_unlock_irqrestore(hba->host->host_lock, flags);
Can Guobe7594a2020-03-05 00:53:07 -08003898
3899 if (update && !pm_runtime_suspended(hba->dev)) {
3900 pm_runtime_get_sync(hba->dev);
3901 ufshcd_hold(hba, false);
3902 ufshcd_auto_hibern8_enable(hba);
3903 ufshcd_release(hba);
3904 pm_runtime_put(hba->dev);
3905 }
Stanley Chuba7af5e2019-12-30 13:32:28 +08003906}
3907EXPORT_SYMBOL_GPL(ufshcd_auto_hibern8_update);
3908
Can Guo71d848b2019-11-14 22:09:26 -08003909void ufshcd_auto_hibern8_enable(struct ufs_hba *hba)
Adrian Hunterad448372018-03-20 15:07:38 +02003910{
3911 unsigned long flags;
3912
Stanley Chuee5f1042019-05-21 14:44:52 +08003913 if (!ufshcd_is_auto_hibern8_supported(hba) || !hba->ahit)
Adrian Hunterad448372018-03-20 15:07:38 +02003914 return;
3915
3916 spin_lock_irqsave(hba->host->host_lock, flags);
3917 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER);
3918 spin_unlock_irqrestore(hba->host->host_lock, flags);
3919}
3920
Yaniv Gardi50646362014-10-23 13:25:13 +03003921 /**
3922 * ufshcd_init_pwr_info - setting the POR (power on reset)
3923 * values in hba power info
3924 * @hba: per-adapter instance
3925 */
3926static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3927{
3928 hba->pwr_info.gear_rx = UFS_PWM_G1;
3929 hba->pwr_info.gear_tx = UFS_PWM_G1;
3930 hba->pwr_info.lane_rx = 1;
3931 hba->pwr_info.lane_tx = 1;
3932 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3933 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3934 hba->pwr_info.hs_rate = 0;
3935}
3936
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303937/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003938 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3939 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303940 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003941static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303942{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003943 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3944
3945 if (hba->max_pwr_info.is_valid)
3946 return 0;
3947
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003948 pwr_info->pwr_tx = FAST_MODE;
3949 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003950 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303951
3952 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003953 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3954 &pwr_info->lane_rx);
3955 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3956 &pwr_info->lane_tx);
3957
3958 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3959 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3960 __func__,
3961 pwr_info->lane_rx,
3962 pwr_info->lane_tx);
3963 return -EINVAL;
3964 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303965
3966 /*
3967 * First, get the maximum gears of HS speed.
3968 * If a zero value, it means there is no HSGEAR capability.
3969 * Then, get the maximum gears of PWM speed.
3970 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003971 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3972 if (!pwr_info->gear_rx) {
3973 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3974 &pwr_info->gear_rx);
3975 if (!pwr_info->gear_rx) {
3976 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3977 __func__, pwr_info->gear_rx);
3978 return -EINVAL;
3979 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003980 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303981 }
3982
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003983 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3984 &pwr_info->gear_tx);
3985 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303986 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003987 &pwr_info->gear_tx);
3988 if (!pwr_info->gear_tx) {
3989 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3990 __func__, pwr_info->gear_tx);
3991 return -EINVAL;
3992 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003993 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003994 }
3995
3996 hba->max_pwr_info.is_valid = true;
3997 return 0;
3998}
3999
4000static int ufshcd_change_power_mode(struct ufs_hba *hba,
4001 struct ufs_pa_layer_attr *pwr_mode)
4002{
4003 int ret;
4004
4005 /* if already configured to the requested pwr_mode */
4006 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
4007 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
4008 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
4009 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
4010 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
4011 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
4012 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
4013 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
4014 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304015 }
4016
4017 /*
4018 * Configure attributes for power mode change with below.
4019 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
4020 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
4021 * - PA_HSSERIES
4022 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004023 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
4024 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
4025 pwr_mode->lane_rx);
4026 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4027 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304028 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004029 else
4030 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304031
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004032 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
4033 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
4034 pwr_mode->lane_tx);
4035 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
4036 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304037 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004038 else
4039 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304040
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004041 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
4042 pwr_mode->pwr_tx == FASTAUTO_MODE ||
4043 pwr_mode->pwr_rx == FAST_MODE ||
4044 pwr_mode->pwr_tx == FAST_MODE)
4045 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
4046 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304047
Can Guo08342532019-12-05 02:14:42 +00004048 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0),
4049 DL_FC0ProtectionTimeOutVal_Default);
4050 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1),
4051 DL_TC0ReplayTimeOutVal_Default);
4052 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2),
4053 DL_AFC0ReqTimeOutVal_Default);
4054 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3),
4055 DL_FC1ProtectionTimeOutVal_Default);
4056 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4),
4057 DL_TC1ReplayTimeOutVal_Default);
4058 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5),
4059 DL_AFC1ReqTimeOutVal_Default);
4060
4061 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal),
4062 DL_FC0ProtectionTimeOutVal_Default);
4063 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal),
4064 DL_TC0ReplayTimeOutVal_Default);
4065 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal),
4066 DL_AFC0ReqTimeOutVal_Default);
4067
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004068 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
4069 | pwr_mode->pwr_tx);
4070
4071 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304072 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004073 "%s: power mode change failed %d\n", __func__, ret);
4074 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004075 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
4076 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004077
4078 memcpy(&hba->pwr_info, pwr_mode,
4079 sizeof(struct ufs_pa_layer_attr));
4080 }
4081
4082 return ret;
4083}
4084
4085/**
4086 * ufshcd_config_pwr_mode - configure a new power mode
4087 * @hba: per-adapter instance
4088 * @desired_pwr_mode: desired power configuration
4089 */
Alim Akhtar0d846e72018-05-06 15:44:18 +05304090int ufshcd_config_pwr_mode(struct ufs_hba *hba,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004091 struct ufs_pa_layer_attr *desired_pwr_mode)
4092{
4093 struct ufs_pa_layer_attr final_params = { 0 };
4094 int ret;
4095
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004096 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
4097 desired_pwr_mode, &final_params);
4098
4099 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004100 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
4101
4102 ret = ufshcd_change_power_mode(hba, &final_params);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08004103 if (!ret)
4104 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304105
4106 return ret;
4107}
Alim Akhtar0d846e72018-05-06 15:44:18 +05304108EXPORT_SYMBOL_GPL(ufshcd_config_pwr_mode);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304109
4110/**
Dolev Raviv68078d52013-07-30 00:35:58 +05304111 * ufshcd_complete_dev_init() - checks device readiness
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004112 * @hba: per-adapter instance
Dolev Raviv68078d52013-07-30 00:35:58 +05304113 *
4114 * Set fDeviceInit flag and poll until device toggles it.
4115 */
4116static int ufshcd_complete_dev_init(struct ufs_hba *hba)
4117{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004118 int i;
4119 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05304120 bool flag_res = 1;
4121
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004122 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
4123 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05304124 if (err) {
4125 dev_err(hba->dev,
4126 "%s setting fDeviceInit flag failed with error %d\n",
4127 __func__, err);
4128 goto out;
4129 }
4130
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004131 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
4132 for (i = 0; i < 1000 && !err && flag_res; i++)
4133 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4134 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
4135
Dolev Raviv68078d52013-07-30 00:35:58 +05304136 if (err)
4137 dev_err(hba->dev,
4138 "%s reading fDeviceInit flag failed with error %d\n",
4139 __func__, err);
4140 else if (flag_res)
4141 dev_err(hba->dev,
4142 "%s fDeviceInit was not cleared by the device\n",
4143 __func__);
4144
4145out:
4146 return err;
4147}
4148
4149/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304150 * ufshcd_make_hba_operational - Make UFS controller operational
4151 * @hba: per adapter instance
4152 *
4153 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004154 * 1. Enable required interrupts
4155 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02004156 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004157 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304158 *
4159 * Returns 0 on success, non-zero value on failure
4160 */
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004161int ufshcd_make_hba_operational(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304162{
4163 int err = 0;
4164 u32 reg;
4165
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304166 /* Enable required interrupts */
4167 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
4168
4169 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03004170 if (ufshcd_is_intr_aggr_allowed(hba))
4171 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
4172 else
4173 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304174
4175 /* Configure UTRL and UTMRL base address registers */
4176 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
4177 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
4178 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
4179 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
4180 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
4181 REG_UTP_TASK_REQ_LIST_BASE_L);
4182 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
4183 REG_UTP_TASK_REQ_LIST_BASE_H);
4184
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304185 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02004186 * Make sure base address and interrupt setup are updated before
4187 * enabling the run/stop registers below.
4188 */
4189 wmb();
4190
4191 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304192 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304193 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004194 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304195 if (!(ufshcd_get_lists_status(reg))) {
4196 ufshcd_enable_run_stop_reg(hba);
4197 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304198 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304199 "Host controller not ready to process requests");
4200 err = -EIO;
4201 goto out;
4202 }
4203
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304204out:
4205 return err;
4206}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004207EXPORT_SYMBOL_GPL(ufshcd_make_hba_operational);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304208
4209/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02004210 * ufshcd_hba_stop - Send controller to reset state
4211 * @hba: per adapter instance
4212 * @can_sleep: perform sleep or just spin
4213 */
4214static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
4215{
4216 int err;
4217
4218 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4219 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4220 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4221 10, 1, can_sleep);
4222 if (err)
4223 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4224}
4225
4226/**
Alim Akhtar4404c5d2018-05-06 15:44:17 +05304227 * ufshcd_hba_execute_hce - initialize the controller
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304228 * @hba: per adapter instance
4229 *
4230 * The controller resets itself and controller firmware initialization
4231 * sequence kicks off. When controller is ready it will set
4232 * the Host Controller Enable bit to 1.
4233 *
4234 * Returns 0 on success, non-zero value on failure
4235 */
Alim Akhtar4404c5d2018-05-06 15:44:17 +05304236static int ufshcd_hba_execute_hce(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304237{
4238 int retry;
4239
Yaniv Gardi596585a2016-03-10 17:37:08 +02004240 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304241 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004242 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304243
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004244 /* UniPro link is disabled at this point */
4245 ufshcd_set_link_off(hba);
4246
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004247 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004248
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304249 /* start controller initialization sequence */
4250 ufshcd_hba_start(hba);
4251
4252 /*
4253 * To initialize a UFS host controller HCE bit must be set to 1.
4254 * During initialization the HCE bit value changes from 1->0->1.
4255 * When the host controller completes initialization sequence
4256 * it sets the value of HCE bit to 1. The same HCE bit is read back
4257 * to check if the controller has completed initialization sequence.
4258 * So without this delay the value HCE = 1, set in the previous
4259 * instruction might be read back.
4260 * This delay can be changed based on the controller.
4261 */
Bean Huo838c1ef2019-07-15 11:21:10 +00004262 usleep_range(1000, 1100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304263
4264 /* wait for the host controller to complete initialization */
4265 retry = 10;
4266 while (ufshcd_is_hba_active(hba)) {
4267 if (retry) {
4268 retry--;
4269 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304270 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304271 "Controller enable failed\n");
4272 return -EIO;
4273 }
Bean Huo838c1ef2019-07-15 11:21:10 +00004274 usleep_range(5000, 5100);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304275 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004276
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004277 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004278 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004279
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004280 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004281
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304282 return 0;
4283}
4284
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004285int ufshcd_hba_enable(struct ufs_hba *hba)
Alim Akhtar4404c5d2018-05-06 15:44:17 +05304286{
4287 int ret;
4288
4289 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) {
4290 ufshcd_set_link_off(hba);
4291 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
4292
4293 /* enable UIC related interrupts */
4294 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
4295 ret = ufshcd_dme_reset(hba);
4296 if (!ret) {
4297 ret = ufshcd_dme_enable(hba);
4298 if (!ret)
4299 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
4300 if (ret)
4301 dev_err(hba->dev,
4302 "Host controller enable failed with non-hce\n");
4303 }
4304 } else {
4305 ret = ufshcd_hba_execute_hce(hba);
4306 }
4307
4308 return ret;
4309}
Stanley Chu9d19bf7a2020-01-17 11:51:07 +08004310EXPORT_SYMBOL_GPL(ufshcd_hba_enable);
4311
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004312static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4313{
4314 int tx_lanes, i, err = 0;
4315
4316 if (!peer)
4317 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4318 &tx_lanes);
4319 else
4320 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4321 &tx_lanes);
4322 for (i = 0; i < tx_lanes; i++) {
4323 if (!peer)
4324 err = ufshcd_dme_set(hba,
4325 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4326 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4327 0);
4328 else
4329 err = ufshcd_dme_peer_set(hba,
4330 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4331 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4332 0);
4333 if (err) {
4334 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4335 __func__, peer, i, err);
4336 break;
4337 }
4338 }
4339
4340 return err;
4341}
4342
4343static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4344{
4345 return ufshcd_disable_tx_lcc(hba, true);
4346}
4347
Stanley Chua5fe372d2020-01-04 22:26:07 +08004348void ufshcd_update_reg_hist(struct ufs_err_reg_hist *reg_hist,
4349 u32 reg)
Stanley Chu8808b4e2019-07-10 21:38:21 +08004350{
4351 reg_hist->reg[reg_hist->pos] = reg;
4352 reg_hist->tstamp[reg_hist->pos] = ktime_get();
4353 reg_hist->pos = (reg_hist->pos + 1) % UFS_ERR_REG_HIST_LENGTH;
4354}
Stanley Chua5fe372d2020-01-04 22:26:07 +08004355EXPORT_SYMBOL_GPL(ufshcd_update_reg_hist);
Stanley Chu8808b4e2019-07-10 21:38:21 +08004356
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304357/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304358 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304359 * @hba: per adapter instance
4360 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304361 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304362 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304363static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304364{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304365 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004366 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004367 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304368
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004369 /*
4370 * If UFS device isn't active then we will have to issue link startup
4371 * 2 times to make sure the device state move to active.
4372 */
4373 if (!ufshcd_is_ufs_dev_active(hba))
4374 link_startup_again = true;
4375
4376link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004377 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004378 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304379
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004380 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004381
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004382 /* check if device is detected by inter-connect layer */
4383 if (!ret && !ufshcd_is_device_present(hba)) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08004384 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4385 0);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004386 dev_err(hba->dev, "%s: Device not present\n", __func__);
4387 ret = -ENXIO;
4388 goto out;
4389 }
4390
4391 /*
4392 * DME link lost indication is only received when link is up,
4393 * but we can't be sure if the link is up until link startup
4394 * succeeds. So reset the local Uni-Pro and try again.
4395 */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004396 if (ret && ufshcd_hba_enable(hba)) {
4397 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4398 (u32)ret);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004399 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004400 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004401 } while (ret && retries--);
4402
Stanley Chu8808b4e2019-07-10 21:38:21 +08004403 if (ret) {
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004404 /* failed to get the link up... retire */
Stanley Chu8808b4e2019-07-10 21:38:21 +08004405 ufshcd_update_reg_hist(&hba->ufs_stats.link_startup_err,
4406 (u32)ret);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304407 goto out;
Stanley Chu8808b4e2019-07-10 21:38:21 +08004408 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304409
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004410 if (link_startup_again) {
4411 link_startup_again = false;
4412 retries = DME_LINKSTARTUP_RETRIES;
4413 goto link_startup;
4414 }
4415
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004416 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4417 ufshcd_init_pwr_info(hba);
4418 ufshcd_print_pwr_info(hba);
4419
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004420 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4421 ret = ufshcd_disable_device_tx_lcc(hba);
4422 if (ret)
4423 goto out;
4424 }
4425
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004426 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004427 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4428 if (ret)
4429 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004430
4431 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304432out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004433 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304434 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004435 ufshcd_print_host_state(hba);
4436 ufshcd_print_pwr_info(hba);
4437 ufshcd_print_host_regs(hba);
4438 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304439 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304440}
4441
4442/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304443 * ufshcd_verify_dev_init() - Verify device initialization
4444 * @hba: per-adapter instance
4445 *
4446 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4447 * device Transport Protocol (UTP) layer is ready after a reset.
4448 * If the UTP layer at the device side is not initialized, it may
4449 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4450 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4451 */
4452static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4453{
4454 int err = 0;
4455 int retries;
4456
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004457 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304458 mutex_lock(&hba->dev_cmd.lock);
4459 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4460 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4461 NOP_OUT_TIMEOUT);
4462
4463 if (!err || err == -ETIMEDOUT)
4464 break;
4465
4466 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4467 }
4468 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004469 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304470
4471 if (err)
4472 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4473 return err;
4474}
4475
4476/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004477 * ufshcd_set_queue_depth - set lun queue depth
4478 * @sdev: pointer to SCSI device
4479 *
4480 * Read bLUQueueDepth value and activate scsi tagged command
4481 * queueing. For WLUN, queue depth is set to 1. For best-effort
4482 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4483 * value that host can queue.
4484 */
4485static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4486{
4487 int ret = 0;
4488 u8 lun_qdepth;
4489 struct ufs_hba *hba;
4490
4491 hba = shost_priv(sdev->host);
4492
4493 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004494 ret = ufshcd_read_unit_desc_param(hba,
4495 ufshcd_scsi_to_upiu_lun(sdev->lun),
4496 UNIT_DESC_PARAM_LU_Q_DEPTH,
4497 &lun_qdepth,
4498 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004499
4500 /* Some WLUN doesn't support unit descriptor */
4501 if (ret == -EOPNOTSUPP)
4502 lun_qdepth = 1;
4503 else if (!lun_qdepth)
4504 /* eventually, we can figure out the real queue depth */
4505 lun_qdepth = hba->nutrs;
4506 else
4507 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4508
4509 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4510 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004511 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004512}
4513
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004514/*
4515 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4516 * @hba: per-adapter instance
4517 * @lun: UFS device lun id
4518 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4519 *
4520 * Returns 0 in case of success and b_lu_write_protect status would be returned
4521 * @b_lu_write_protect parameter.
4522 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4523 * Returns -EINVAL in case of invalid parameters passed to this function.
4524 */
4525static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4526 u8 lun,
4527 u8 *b_lu_write_protect)
4528{
4529 int ret;
4530
4531 if (!b_lu_write_protect)
4532 ret = -EINVAL;
4533 /*
4534 * According to UFS device spec, RPMB LU can't be write
4535 * protected so skip reading bLUWriteProtect parameter for
4536 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4537 */
Bean Huo1baa8012020-01-20 14:08:20 +01004538 else if (lun >= hba->dev_info.max_lu_supported)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004539 ret = -ENOTSUPP;
4540 else
4541 ret = ufshcd_read_unit_desc_param(hba,
4542 lun,
4543 UNIT_DESC_PARAM_LU_WR_PROTECT,
4544 b_lu_write_protect,
4545 sizeof(*b_lu_write_protect));
4546 return ret;
4547}
4548
4549/**
4550 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4551 * status
4552 * @hba: per-adapter instance
4553 * @sdev: pointer to SCSI device
4554 *
4555 */
4556static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4557 struct scsi_device *sdev)
4558{
4559 if (hba->dev_info.f_power_on_wp_en &&
4560 !hba->dev_info.is_lu_power_on_wp) {
4561 u8 b_lu_write_protect;
4562
4563 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4564 &b_lu_write_protect) &&
4565 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4566 hba->dev_info.is_lu_power_on_wp = true;
4567 }
4568}
4569
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004570/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304571 * ufshcd_slave_alloc - handle initial SCSI device configurations
4572 * @sdev: pointer to SCSI device
4573 *
4574 * Returns success
4575 */
4576static int ufshcd_slave_alloc(struct scsi_device *sdev)
4577{
4578 struct ufs_hba *hba;
4579
4580 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304581
4582 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4583 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304584
Can Guoa3a76392019-12-05 02:14:30 +00004585 /* DBD field should be set to 1 in mode sense(10) */
4586 sdev->set_dbd_for_ms = 1;
4587
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304588 /* allow SCSI layer to restart the device in case of errors */
4589 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004590
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004591 /* REPORT SUPPORTED OPERATION CODES is not supported */
4592 sdev->no_report_opcodes = 1;
4593
Sujit Reddy Thumma84af7e82018-01-24 09:52:35 +05304594 /* WRITE_SAME command is not supported */
4595 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004596
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004597 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004598
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004599 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4600
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004601 return 0;
4602}
4603
4604/**
4605 * ufshcd_change_queue_depth - change queue depth
4606 * @sdev: pointer to SCSI device
4607 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004608 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004609 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004610 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004611static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004612{
4613 struct ufs_hba *hba = shost_priv(sdev->host);
4614
4615 if (depth > hba->nutrs)
4616 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004617 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304618}
4619
4620/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004621 * ufshcd_slave_configure - adjust SCSI device configurations
4622 * @sdev: pointer to SCSI device
4623 */
4624static int ufshcd_slave_configure(struct scsi_device *sdev)
4625{
Stanley Chu49615ba2019-09-16 23:56:50 +08004626 struct ufs_hba *hba = shost_priv(sdev->host);
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004627 struct request_queue *q = sdev->request_queue;
4628
4629 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
Stanley Chu49615ba2019-09-16 23:56:50 +08004630
4631 if (ufshcd_is_rpm_autosuspend_allowed(hba))
4632 sdev->rpm_autosuspend = 1;
4633
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004634 return 0;
4635}
4636
4637/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304638 * ufshcd_slave_destroy - remove SCSI device configurations
4639 * @sdev: pointer to SCSI device
4640 */
4641static void ufshcd_slave_destroy(struct scsi_device *sdev)
4642{
4643 struct ufs_hba *hba;
4644
4645 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004646 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004647 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4648 unsigned long flags;
4649
4650 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004651 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004652 spin_unlock_irqrestore(hba->host->host_lock, flags);
4653 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304654}
4655
4656/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304657 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004658 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304659 * @scsi_status: SCSI command status
4660 *
4661 * Returns value base on SCSI command status
4662 */
4663static inline int
4664ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4665{
4666 int result = 0;
4667
4668 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304669 case SAM_STAT_CHECK_CONDITION:
4670 ufshcd_copy_sense_data(lrbp);
Tomas Winkler30eb2e42018-11-26 10:10:34 +02004671 /* fallthrough */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304672 case SAM_STAT_GOOD:
4673 result |= DID_OK << 16 |
4674 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304675 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304676 break;
4677 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304678 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304679 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304680 ufshcd_copy_sense_data(lrbp);
4681 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304682 break;
4683 default:
4684 result |= DID_ERROR << 16;
4685 break;
4686 } /* end of switch */
4687
4688 return result;
4689}
4690
4691/**
4692 * ufshcd_transfer_rsp_status - Get overall status of the response
4693 * @hba: per adapter instance
Bart Van Assche8aa29f12018-03-01 15:07:20 -08004694 * @lrbp: pointer to local reference block of completed command
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304695 *
4696 * Returns result of the command to notify SCSI midlayer
4697 */
4698static inline int
4699ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4700{
4701 int result = 0;
4702 int scsi_status;
4703 int ocs;
4704
4705 /* overall command status of utrd */
4706 ocs = ufshcd_get_tr_ocs(lrbp);
4707
4708 switch (ocs) {
4709 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304710 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004711 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304712 switch (result) {
4713 case UPIU_TRANSACTION_RESPONSE:
4714 /*
4715 * get the response UPIU result to extract
4716 * the SCSI command status
4717 */
4718 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4719
4720 /*
4721 * get the result based on SCSI status response
4722 * to notify the SCSI midlayer of the command status
4723 */
4724 scsi_status = result & MASK_SCSI_STATUS;
4725 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304726
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004727 /*
4728 * Currently we are only supporting BKOPs exception
4729 * events hence we can ignore BKOPs exception event
4730 * during power management callbacks. BKOPs exception
4731 * event is not expected to be raised in runtime suspend
4732 * callback as it allows the urgent bkops.
4733 * During system suspend, we are anyway forcefully
4734 * disabling the bkops and if urgent bkops is needed
4735 * it will be enabled on system resume. Long term
4736 * solution could be to abort the system suspend if
4737 * UFS device needs urgent BKOPs.
4738 */
4739 if (!hba->pm_op_in_progress &&
4740 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304741 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304742 break;
4743 case UPIU_TRANSACTION_REJECT_UPIU:
4744 /* TODO: handle Reject UPIU Response */
4745 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304746 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304747 "Reject UPIU not fully implemented\n");
4748 break;
4749 default:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304750 dev_err(hba->dev,
4751 "Unexpected request response code = %x\n",
4752 result);
Stanley Chue0347d82019-04-15 20:23:38 +08004753 result = DID_ERROR << 16;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304754 break;
4755 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304756 break;
4757 case OCS_ABORTED:
4758 result |= DID_ABORT << 16;
4759 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304760 case OCS_INVALID_COMMAND_STATUS:
4761 result |= DID_REQUEUE << 16;
4762 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304763 case OCS_INVALID_CMD_TABLE_ATTR:
4764 case OCS_INVALID_PRDT_ATTR:
4765 case OCS_MISMATCH_DATA_BUF_SIZE:
4766 case OCS_MISMATCH_RESP_UPIU_SIZE:
4767 case OCS_PEER_COMM_FAILURE:
4768 case OCS_FATAL_ERROR:
4769 default:
4770 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304771 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004772 "OCS error from controller = %x for tag %d\n",
4773 ocs, lrbp->task_tag);
4774 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004775 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304776 break;
4777 } /* end of switch */
4778
Can Guo2df74b62019-11-25 22:53:33 -08004779 if ((host_byte(result) != DID_OK) && !hba->silence_err_logs)
Dolev Raviv66cc8202016-12-22 18:39:42 -08004780 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304781 return result;
4782}
4783
4784/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304785 * ufshcd_uic_cmd_compl - handle completion of uic command
4786 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304787 * @intr_status: interrupt status generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004788 *
4789 * Returns
4790 * IRQ_HANDLED - If interrupt is valid
4791 * IRQ_NONE - If invalid interrupt
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304792 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004793static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304794{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004795 irqreturn_t retval = IRQ_NONE;
4796
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304797 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304798 hba->active_uic_cmd->argument2 |=
4799 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304800 hba->active_uic_cmd->argument3 =
4801 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304802 complete(&hba->active_uic_cmd->done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004803 retval = IRQ_HANDLED;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304804 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304805
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004806 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004807 complete(hba->uic_async_done);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004808 retval = IRQ_HANDLED;
4809 }
4810 return retval;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304811}
4812
4813/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004814 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304815 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004816 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304817 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004818static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4819 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304820{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304821 struct ufshcd_lrb *lrbp;
4822 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304823 int result;
4824 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004825
Dolev Ravive9d501b2014-07-01 12:22:37 +03004826 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4827 lrbp = &hba->lrb[index];
4828 cmd = lrbp->cmd;
4829 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004830 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004831 result = ufshcd_transfer_rsp_status(hba, lrbp);
4832 scsi_dma_unmap(cmd);
4833 cmd->result = result;
4834 /* Mark completed command as NULL in LRB */
4835 lrbp->cmd = NULL;
Can Guo74a527a2019-11-25 22:53:32 -08004836 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03004837 /* Do not touch lrbp after scsi done */
4838 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004839 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004840 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4841 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Can Guo74a527a2019-11-25 22:53:32 -08004842 lrbp->compl_time_stamp = ktime_get();
Lee Susman1a07f2d2016-12-22 18:42:03 -08004843 if (hba->dev_cmd.complete) {
4844 ufshcd_add_command_trace(hba, index,
4845 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004846 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004847 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004848 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004849 if (ufshcd_is_clkscaling_supported(hba))
4850 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004851 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304852
4853 /* clear corresponding bits of completed commands */
4854 hba->outstanding_reqs ^= completed_reqs;
4855
Sahitya Tummala856b3482014-09-25 15:32:34 +03004856 ufshcd_clk_scaling_update_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304857}
4858
4859/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004860 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4861 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004862 *
4863 * Returns
4864 * IRQ_HANDLED - If interrupt is valid
4865 * IRQ_NONE - If invalid interrupt
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004866 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004867static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba)
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004868{
4869 unsigned long completed_reqs;
4870 u32 tr_doorbell;
4871
4872 /* Resetting interrupt aggregation counters first and reading the
4873 * DOOR_BELL afterward allows us to handle all the completed requests.
4874 * In order to prevent other interrupts starvation the DB is read once
4875 * after reset. The down side of this solution is the possibility of
4876 * false interrupt if device completes another request after resetting
4877 * aggregation and before reading the DB.
4878 */
Alim Akhtar5ac6abc2018-05-06 15:44:16 +05304879 if (ufshcd_is_intr_aggr_allowed(hba) &&
4880 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR))
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004881 ufshcd_reset_intr_aggr(hba);
4882
4883 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4884 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4885
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08004886 if (completed_reqs) {
4887 __ufshcd_transfer_req_compl(hba, completed_reqs);
4888 return IRQ_HANDLED;
4889 } else {
4890 return IRQ_NONE;
4891 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004892}
4893
4894/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304895 * ufshcd_disable_ee - disable exception event
4896 * @hba: per-adapter instance
4897 * @mask: exception event to disable
4898 *
4899 * Disables exception event in the device so that the EVENT_ALERT
4900 * bit is not set.
4901 *
4902 * Returns zero on success, non-zero error value on failure.
4903 */
4904static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4905{
4906 int err = 0;
4907 u32 val;
4908
4909 if (!(hba->ee_ctrl_mask & mask))
4910 goto out;
4911
4912 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004913 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004914 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304915 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4916 if (!err)
4917 hba->ee_ctrl_mask &= ~mask;
4918out:
4919 return err;
4920}
4921
4922/**
4923 * ufshcd_enable_ee - enable exception event
4924 * @hba: per-adapter instance
4925 * @mask: exception event to enable
4926 *
4927 * Enable corresponding exception event in the device to allow
4928 * device to alert host in critical scenarios.
4929 *
4930 * Returns zero on success, non-zero error value on failure.
4931 */
4932static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4933{
4934 int err = 0;
4935 u32 val;
4936
4937 if (hba->ee_ctrl_mask & mask)
4938 goto out;
4939
4940 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004941 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004942 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304943 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4944 if (!err)
4945 hba->ee_ctrl_mask |= mask;
4946out:
4947 return err;
4948}
4949
4950/**
4951 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4952 * @hba: per-adapter instance
4953 *
4954 * Allow device to manage background operations on its own. Enabling
4955 * this might lead to inconsistent latencies during normal data transfers
4956 * as the device is allowed to manage its own way of handling background
4957 * operations.
4958 *
4959 * Returns zero on success, non-zero on failure.
4960 */
4961static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4962{
4963 int err = 0;
4964
4965 if (hba->auto_bkops_enabled)
4966 goto out;
4967
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004968 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304969 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4970 if (err) {
4971 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4972 __func__, err);
4973 goto out;
4974 }
4975
4976 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004977 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304978
4979 /* No need of URGENT_BKOPS exception from the device */
4980 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4981 if (err)
4982 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4983 __func__, err);
4984out:
4985 return err;
4986}
4987
4988/**
4989 * ufshcd_disable_auto_bkops - block device in doing background operations
4990 * @hba: per-adapter instance
4991 *
4992 * Disabling background operations improves command response latency but
4993 * has drawback of device moving into critical state where the device is
4994 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4995 * host is idle so that BKOPS are managed effectively without any negative
4996 * impacts.
4997 *
4998 * Returns zero on success, non-zero on failure.
4999 */
5000static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
5001{
5002 int err = 0;
5003
5004 if (!hba->auto_bkops_enabled)
5005 goto out;
5006
5007 /*
5008 * If host assisted BKOPs is to be enabled, make sure
5009 * urgent bkops exception is allowed.
5010 */
5011 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
5012 if (err) {
5013 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
5014 __func__, err);
5015 goto out;
5016 }
5017
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005018 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305019 QUERY_FLAG_IDN_BKOPS_EN, NULL);
5020 if (err) {
5021 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
5022 __func__, err);
5023 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
5024 goto out;
5025 }
5026
5027 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005028 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Asutosh Das24366c2a2019-11-25 22:53:30 -08005029 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305030out:
5031 return err;
5032}
5033
5034/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005035 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305036 * @hba: per adapter instance
5037 *
5038 * After a device reset the device may toggle the BKOPS_EN flag
5039 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005040 * as well. This function would change the auto-bkops state based on
5041 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305042 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005043static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305044{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08005045 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
5046 hba->auto_bkops_enabled = false;
5047 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
5048 ufshcd_enable_auto_bkops(hba);
5049 } else {
5050 hba->auto_bkops_enabled = true;
5051 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
5052 ufshcd_disable_auto_bkops(hba);
5053 }
Asutosh Das24366c2a2019-11-25 22:53:30 -08005054 hba->is_urgent_bkops_lvl_checked = false;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305055}
5056
5057static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
5058{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005059 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305060 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
5061}
5062
5063/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005064 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5065 * @hba: per-adapter instance
5066 * @status: bkops_status value
5067 *
5068 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
5069 * flag in the device to permit background operations if the device
5070 * bkops_status is greater than or equal to "status" argument passed to
5071 * this function, disable otherwise.
5072 *
5073 * Returns 0 for success, non-zero in case of failure.
5074 *
5075 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5076 * to know whether auto bkops is enabled or disabled after this function
5077 * returns control to it.
5078 */
5079static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
5080 enum bkops_status status)
5081{
5082 int err;
5083 u32 curr_status = 0;
5084
5085 err = ufshcd_get_bkops_status(hba, &curr_status);
5086 if (err) {
5087 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5088 __func__, err);
5089 goto out;
5090 } else if (curr_status > BKOPS_STATUS_MAX) {
5091 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
5092 __func__, curr_status);
5093 err = -EINVAL;
5094 goto out;
5095 }
5096
5097 if (curr_status >= status)
5098 err = ufshcd_enable_auto_bkops(hba);
5099 else
5100 err = ufshcd_disable_auto_bkops(hba);
Asutosh Das24366c2a2019-11-25 22:53:30 -08005101 hba->urgent_bkops_lvl = curr_status;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005102out:
5103 return err;
5104}
5105
5106/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305107 * ufshcd_urgent_bkops - handle urgent bkops exception event
5108 * @hba: per-adapter instance
5109 *
5110 * Enable fBackgroundOpsEn flag in the device to permit background
5111 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005112 *
5113 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
5114 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305115 */
5116static int ufshcd_urgent_bkops(struct ufs_hba *hba)
5117{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005118 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305119}
5120
5121static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
5122{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02005123 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305124 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
5125}
5126
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005127static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
5128{
5129 int err;
5130 u32 curr_status = 0;
5131
5132 if (hba->is_urgent_bkops_lvl_checked)
5133 goto enable_auto_bkops;
5134
5135 err = ufshcd_get_bkops_status(hba, &curr_status);
5136 if (err) {
5137 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
5138 __func__, err);
5139 goto out;
5140 }
5141
5142 /*
5143 * We are seeing that some devices are raising the urgent bkops
5144 * exception events even when BKOPS status doesn't indicate performace
5145 * impacted or critical. Handle these device by determining their urgent
5146 * bkops status at runtime.
5147 */
5148 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
5149 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
5150 __func__, curr_status);
5151 /* update the current status as the urgent bkops level */
5152 hba->urgent_bkops_lvl = curr_status;
5153 hba->is_urgent_bkops_lvl_checked = true;
5154 }
5155
5156enable_auto_bkops:
5157 err = ufshcd_enable_auto_bkops(hba);
5158out:
5159 if (err < 0)
5160 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
5161 __func__, err);
5162}
5163
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305164/**
5165 * ufshcd_exception_event_handler - handle exceptions raised by device
5166 * @work: pointer to work data
5167 *
5168 * Read bExceptionEventStatus attribute from the device and handle the
5169 * exception event accordingly.
5170 */
5171static void ufshcd_exception_event_handler(struct work_struct *work)
5172{
5173 struct ufs_hba *hba;
5174 int err;
5175 u32 status = 0;
5176 hba = container_of(work, struct ufs_hba, eeh_work);
5177
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305178 pm_runtime_get_sync(hba->dev);
Stanley Chu03e1d282019-12-24 21:01:05 +08005179 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305180 err = ufshcd_get_ee_status(hba, &status);
5181 if (err) {
5182 dev_err(hba->dev, "%s: failed to get exception status %d\n",
5183 __func__, err);
5184 goto out;
5185 }
5186
5187 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005188
5189 if (status & MASK_EE_URGENT_BKOPS)
5190 ufshcd_bkops_exception_event_handler(hba);
5191
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305192out:
Stanley Chu03e1d282019-12-24 21:01:05 +08005193 ufshcd_scsi_unblock_requests(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305194 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305195 return;
5196}
5197
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005198/* Complete requests that have door-bell cleared */
5199static void ufshcd_complete_requests(struct ufs_hba *hba)
5200{
5201 ufshcd_transfer_req_compl(hba);
5202 ufshcd_tmc_handler(hba);
5203}
5204
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305205/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02005206 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
5207 * to recover from the DL NAC errors or not.
5208 * @hba: per-adapter instance
5209 *
5210 * Returns true if error handling is required, false otherwise
5211 */
5212static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
5213{
5214 unsigned long flags;
5215 bool err_handling = true;
5216
5217 spin_lock_irqsave(hba->host->host_lock, flags);
5218 /*
5219 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
5220 * device fatal error and/or DL NAC & REPLAY timeout errors.
5221 */
5222 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
5223 goto out;
5224
5225 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
5226 ((hba->saved_err & UIC_ERROR) &&
5227 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
5228 goto out;
5229
5230 if ((hba->saved_err & UIC_ERROR) &&
5231 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
5232 int err;
5233 /*
5234 * wait for 50ms to see if we can get any other errors or not.
5235 */
5236 spin_unlock_irqrestore(hba->host->host_lock, flags);
5237 msleep(50);
5238 spin_lock_irqsave(hba->host->host_lock, flags);
5239
5240 /*
5241 * now check if we have got any other severe errors other than
5242 * DL NAC error?
5243 */
5244 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5245 ((hba->saved_err & UIC_ERROR) &&
5246 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5247 goto out;
5248
5249 /*
5250 * As DL NAC is the only error received so far, send out NOP
5251 * command to confirm if link is still active or not.
5252 * - If we don't get any response then do error recovery.
5253 * - If we get response then clear the DL NAC error bit.
5254 */
5255
5256 spin_unlock_irqrestore(hba->host->host_lock, flags);
5257 err = ufshcd_verify_dev_init(hba);
5258 spin_lock_irqsave(hba->host->host_lock, flags);
5259
5260 if (err)
5261 goto out;
5262
5263 /* Link seems to be alive hence ignore the DL NAC errors */
5264 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5265 hba->saved_err &= ~UIC_ERROR;
5266 /* clear NAC error */
5267 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5268 if (!hba->saved_uic_err) {
5269 err_handling = false;
5270 goto out;
5271 }
5272 }
5273out:
5274 spin_unlock_irqrestore(hba->host->host_lock, flags);
5275 return err_handling;
5276}
5277
5278/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305279 * ufshcd_err_handler - handle UFS errors that require s/w attention
5280 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305281 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305282static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305283{
5284 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305285 unsigned long flags;
5286 u32 err_xfer = 0;
5287 u32 err_tm = 0;
5288 int err = 0;
5289 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005290 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305291
5292 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305293
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305294 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005295 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305296
5297 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005298 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305299 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305300
5301 hba->ufshcd_state = UFSHCD_STATE_RESET;
5302 ufshcd_set_eh_in_progress(hba);
5303
5304 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005305 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005306
5307 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5308 bool ret;
5309
5310 spin_unlock_irqrestore(hba->host->host_lock, flags);
5311 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5312 ret = ufshcd_quirk_dl_nac_errors(hba);
5313 spin_lock_irqsave(hba->host->host_lock, flags);
5314 if (!ret)
5315 goto skip_err_handling;
5316 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005317 if ((hba->saved_err & INT_FATAL_ERRORS) ||
Stanley Chu82174442019-05-21 14:44:54 +08005318 (hba->saved_err & UFSHCD_UIC_HIBERN8_MASK) ||
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005319 ((hba->saved_err & UIC_ERROR) &&
5320 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5321 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5322 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5323 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305324
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005325 /*
5326 * if host reset is required then skip clearing the pending
Can Guo2df74b62019-11-25 22:53:33 -08005327 * transfers forcefully because they will get cleared during
5328 * host reset and restore
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005329 */
5330 if (needs_reset)
5331 goto skip_pending_xfer_clear;
5332
5333 /* release lock as clear command might sleep */
5334 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305335 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005336 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5337 if (ufshcd_clear_cmd(hba, tag)) {
5338 err_xfer = true;
5339 goto lock_skip_pending_xfer_clear;
5340 }
5341 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305342
5343 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005344 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5345 if (ufshcd_clear_tm_cmd(hba, tag)) {
5346 err_tm = true;
5347 goto lock_skip_pending_xfer_clear;
5348 }
5349 }
5350
5351lock_skip_pending_xfer_clear:
5352 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305353
5354 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005355 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305356
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005357 if (err_xfer || err_tm)
5358 needs_reset = true;
5359
5360skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305361 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005362 if (needs_reset) {
5363 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5364
5365 /*
5366 * ufshcd_reset_and_restore() does the link reinitialization
5367 * which will need atleast one empty doorbell slot to send the
5368 * device management commands (NOP and query commands).
5369 * If there is no slot empty at this moment then free up last
5370 * slot forcefully.
5371 */
5372 if (hba->outstanding_reqs == max_doorbells)
5373 __ufshcd_transfer_req_compl(hba,
5374 (1UL << (hba->nutrs - 1)));
5375
5376 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305377 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005378 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305379 if (err) {
5380 dev_err(hba->dev, "%s: reset and restore failed\n",
5381 __func__);
5382 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5383 }
5384 /*
5385 * Inform scsi mid-layer that we did reset and allow to handle
5386 * Unit Attention properly.
5387 */
5388 scsi_report_bus_reset(hba->host, 0);
5389 hba->saved_err = 0;
5390 hba->saved_uic_err = 0;
5391 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005392
Yaniv Gardi583fa622016-03-10 17:37:13 +02005393skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005394 if (!needs_reset) {
5395 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5396 if (hba->saved_err || hba->saved_uic_err)
5397 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5398 __func__, hba->saved_err, hba->saved_uic_err);
5399 }
5400
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305401 ufshcd_clear_eh_in_progress(hba);
5402
5403out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005404 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani38135532018-05-03 16:37:18 +05305405 ufshcd_scsi_unblock_requests(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005406 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305407 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305408}
5409
5410/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305411 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5412 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005413 *
5414 * Returns
5415 * IRQ_HANDLED - If interrupt is valid
5416 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305417 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005418static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305419{
5420 u32 reg;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005421 irqreturn_t retval = IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305422
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005423 /* PHY layer lane error */
5424 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5425 /* Ignore LINERESET indication, as this is not an error */
5426 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005427 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005428 /*
5429 * To know whether this error is fatal or not, DB timeout
5430 * must be checked but this error is handled separately.
5431 */
5432 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Stanley Chu48d5b972019-07-10 21:38:18 +08005433 ufshcd_update_reg_hist(&hba->ufs_stats.pa_err, reg);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005434 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005435 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005436
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305437 /* PA_INIT_ERROR is fatal and needs UIC reset */
5438 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005439 if ((reg & UIC_DATA_LINK_LAYER_ERROR) &&
5440 (reg & UIC_DATA_LINK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005441 ufshcd_update_reg_hist(&hba->ufs_stats.dl_err, reg);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005442
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005443 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5444 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
5445 else if (hba->dev_quirks &
5446 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5447 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5448 hba->uic_error |=
5449 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5450 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5451 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5452 }
5453 retval |= IRQ_HANDLED;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005454 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305455
5456 /* UIC NL/TL/DME errors needs software retry */
5457 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005458 if ((reg & UIC_NETWORK_LAYER_ERROR) &&
5459 (reg & UIC_NETWORK_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005460 ufshcd_update_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305461 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005462 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005463 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305464
5465 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005466 if ((reg & UIC_TRANSPORT_LAYER_ERROR) &&
5467 (reg & UIC_TRANSPORT_LAYER_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005468 ufshcd_update_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305469 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005470 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005471 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305472
5473 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005474 if ((reg & UIC_DME_ERROR) &&
5475 (reg & UIC_DME_ERROR_CODE_MASK)) {
Stanley Chu48d5b972019-07-10 21:38:18 +08005476 ufshcd_update_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305477 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005478 retval |= IRQ_HANDLED;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005479 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305480
5481 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5482 __func__, hba->uic_error);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005483 return retval;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305484}
5485
Stanley Chu82174442019-05-21 14:44:54 +08005486static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba,
5487 u32 intr_mask)
5488{
5489 if (!ufshcd_is_auto_hibern8_supported(hba))
5490 return false;
5491
5492 if (!(intr_mask & UFSHCD_UIC_HIBERN8_MASK))
5493 return false;
5494
5495 if (hba->active_uic_cmd &&
5496 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER ||
5497 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT))
5498 return false;
5499
5500 return true;
5501}
5502
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305503/**
5504 * ufshcd_check_errors - Check for errors that need s/w attention
5505 * @hba: per-adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005506 *
5507 * Returns
5508 * IRQ_HANDLED - If interrupt is valid
5509 * IRQ_NONE - If invalid interrupt
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305510 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005511static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305512{
5513 bool queue_eh_work = false;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005514 irqreturn_t retval = IRQ_NONE;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305515
Stanley Chud3c615b2019-07-10 21:38:19 +08005516 if (hba->errors & INT_FATAL_ERRORS) {
5517 ufshcd_update_reg_hist(&hba->ufs_stats.fatal_err, hba->errors);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305518 queue_eh_work = true;
Stanley Chud3c615b2019-07-10 21:38:19 +08005519 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305520
5521 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305522 hba->uic_error = 0;
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005523 retval = ufshcd_update_uic_error(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305524 if (hba->uic_error)
5525 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305526 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305527
Stanley Chu82174442019-05-21 14:44:54 +08005528 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) {
5529 dev_err(hba->dev,
5530 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n",
5531 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ?
5532 "Enter" : "Exit",
5533 hba->errors, ufshcd_get_upmcrs(hba));
Stanley Chud3c615b2019-07-10 21:38:19 +08005534 ufshcd_update_reg_hist(&hba->ufs_stats.auto_hibern8_err,
5535 hba->errors);
Stanley Chu82174442019-05-21 14:44:54 +08005536 queue_eh_work = true;
5537 }
5538
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305539 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005540 /*
5541 * update the transfer error masks to sticky bits, let's do this
5542 * irrespective of current ufshcd_state.
5543 */
5544 hba->saved_err |= hba->errors;
5545 hba->saved_uic_err |= hba->uic_error;
5546
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305547 /* handle fatal errors only when link is functional */
5548 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5549 /* block commands from scsi mid-layer */
Subhash Jadavani38135532018-05-03 16:37:18 +05305550 ufshcd_scsi_block_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305551
Zang Leigang141f8162016-11-16 11:29:37 +08005552 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005553
5554 /* dump controller state before resetting */
5555 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5556 bool pr_prdt = !!(hba->saved_err &
5557 SYSTEM_BUS_FATAL_ERROR);
5558
5559 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5560 __func__, hba->saved_err,
5561 hba->saved_uic_err);
5562
5563 ufshcd_print_host_regs(hba);
5564 ufshcd_print_pwr_info(hba);
5565 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5566 ufshcd_print_trs(hba, hba->outstanding_reqs,
5567 pr_prdt);
5568 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305569 schedule_work(&hba->eh_work);
5570 }
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005571 retval |= IRQ_HANDLED;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305572 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305573 /*
5574 * if (!queue_eh_work) -
5575 * Other errors are either non-fatal where host recovers
5576 * itself without s/w intervention or errors that will be
5577 * handled by the SCSI core layer.
5578 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005579 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305580}
5581
Bart Van Assche69a6c262019-12-09 10:13:09 -08005582struct ctm_info {
5583 struct ufs_hba *hba;
5584 unsigned long pending;
5585 unsigned int ncpl;
5586};
5587
5588static bool ufshcd_compl_tm(struct request *req, void *priv, bool reserved)
5589{
5590 struct ctm_info *const ci = priv;
5591 struct completion *c;
5592
5593 WARN_ON_ONCE(reserved);
5594 if (test_bit(req->tag, &ci->pending))
5595 return true;
5596 ci->ncpl++;
5597 c = req->end_io_data;
5598 if (c)
5599 complete(c);
5600 return true;
5601}
5602
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305603/**
5604 * ufshcd_tmc_handler - handle task management function completion
5605 * @hba: per adapter instance
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005606 *
5607 * Returns
5608 * IRQ_HANDLED - If interrupt is valid
5609 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305610 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005611static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305612{
Bart Van Assche69a6c262019-12-09 10:13:09 -08005613 struct request_queue *q = hba->tmf_queue;
5614 struct ctm_info ci = {
5615 .hba = hba,
5616 .pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL),
5617 };
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305618
Bart Van Assche69a6c262019-12-09 10:13:09 -08005619 blk_mq_tagset_busy_iter(q->tag_set, ufshcd_compl_tm, &ci);
5620 return ci.ncpl ? IRQ_HANDLED : IRQ_NONE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305621}
5622
5623/**
5624 * ufshcd_sl_intr - Interrupt service routine
5625 * @hba: per adapter instance
5626 * @intr_status: contains interrupts generated by the controller
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005627 *
5628 * Returns
5629 * IRQ_HANDLED - If interrupt is valid
5630 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305631 */
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005632static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305633{
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005634 irqreturn_t retval = IRQ_NONE;
5635
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305636 hba->errors = UFSHCD_ERROR_MASK & intr_status;
Stanley Chu82174442019-05-21 14:44:54 +08005637
5638 if (ufshcd_is_auto_hibern8_error(hba, intr_status))
5639 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status);
5640
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305641 if (hba->errors)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005642 retval |= ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305643
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305644 if (intr_status & UFSHCD_UIC_MASK)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005645 retval |= ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305646
5647 if (intr_status & UTP_TASK_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005648 retval |= ufshcd_tmc_handler(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305649
5650 if (intr_status & UTP_TRANSFER_REQ_COMPL)
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005651 retval |= ufshcd_transfer_req_compl(hba);
5652
5653 return retval;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305654}
5655
5656/**
5657 * ufshcd_intr - Main interrupt service routine
5658 * @irq: irq number
5659 * @__hba: pointer to adapter instance
5660 *
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005661 * Returns
5662 * IRQ_HANDLED - If interrupt is valid
5663 * IRQ_NONE - If invalid interrupt
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305664 */
5665static irqreturn_t ufshcd_intr(int irq, void *__hba)
5666{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005667 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305668 irqreturn_t retval = IRQ_NONE;
5669 struct ufs_hba *hba = __hba;
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305670 int retries = hba->nutrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305671
5672 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305673 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305674
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305675 /*
5676 * There could be max of hba->nutrs reqs in flight and in worst case
5677 * if the reqs get finished 1 by 1 after the interrupt status is
5678 * read, make sure we handle them by checking the interrupt status
5679 * again in a loop until we process all of the reqs before returning.
5680 */
5681 do {
5682 enabled_intr_status =
5683 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
5684 if (intr_status)
5685 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005686 if (enabled_intr_status)
5687 retval |= ufshcd_sl_intr(hba, enabled_intr_status);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005688
Venkat Gopalakrishnan7f6ba4f2018-05-03 16:37:20 +05305689 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
5690 } while (intr_status && --retries);
5691
Venkat Gopalakrishnan9333d772019-11-14 22:09:28 -08005692 if (retval == IRQ_NONE) {
5693 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x\n",
5694 __func__, intr_status);
5695 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: ");
5696 }
5697
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305698 spin_unlock(hba->host->host_lock);
5699 return retval;
5700}
5701
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305702static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5703{
5704 int err = 0;
5705 u32 mask = 1 << tag;
5706 unsigned long flags;
5707
5708 if (!test_bit(tag, &hba->outstanding_tasks))
5709 goto out;
5710
5711 spin_lock_irqsave(hba->host->host_lock, flags);
Alim Akhtar1399c5b2018-05-06 15:44:15 +05305712 ufshcd_utmrl_clear(hba, tag);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305713 spin_unlock_irqrestore(hba->host->host_lock, flags);
5714
5715 /* poll for max. 1 sec to clear door bell register by h/w */
5716 err = ufshcd_wait_for_register(hba,
5717 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005718 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305719out:
5720 return err;
5721}
5722
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005723static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba,
5724 struct utp_task_req_desc *treq, u8 tm_function)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305725{
Bart Van Assche69a6c262019-12-09 10:13:09 -08005726 struct request_queue *q = hba->tmf_queue;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005727 struct Scsi_Host *host = hba->host;
Bart Van Assche69a6c262019-12-09 10:13:09 -08005728 DECLARE_COMPLETION_ONSTACK(wait);
5729 struct request *req;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305730 unsigned long flags;
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005731 int free_slot, task_tag, err;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305732
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305733 /*
5734 * Get free slot, sleep if slots are unavailable.
5735 * Even though we use wait_event() which sleeps indefinitely,
5736 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5737 */
Bart Van Assche69a6c262019-12-09 10:13:09 -08005738 req = blk_get_request(q, REQ_OP_DRV_OUT, BLK_MQ_REQ_RESERVED);
5739 req->end_io_data = &wait;
5740 free_slot = req->tag;
5741 WARN_ON_ONCE(free_slot < 0 || free_slot >= hba->nutmrs);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005742 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305743
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305744 spin_lock_irqsave(host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305745 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305746
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005747 treq->req_header.dword_0 |= cpu_to_be32(task_tag);
5748
5749 memcpy(hba->utmrdl_base_addr + free_slot, treq, sizeof(*treq));
Kiwoong Kimd2877be2016-11-10 21:16:15 +09005750 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
5751
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305752 /* send command to the controller */
5753 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005754
5755 /* Make sure descriptors are ready before ringing the task doorbell */
5756 wmb();
5757
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305758 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07005759 /* Make sure that doorbell is committed immediately */
5760 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305761
5762 spin_unlock_irqrestore(host->host_lock, flags);
5763
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03005764 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_send");
5765
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305766 /* wait until the task management command is completed */
Bart Van Assche69a6c262019-12-09 10:13:09 -08005767 err = wait_for_completion_io_timeout(&wait,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305768 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305769 if (!err) {
Bart Van Assche69a6c262019-12-09 10:13:09 -08005770 /*
5771 * Make sure that ufshcd_compl_tm() does not trigger a
5772 * use-after-free.
5773 */
5774 req->end_io_data = NULL;
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03005775 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete_err");
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305776 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
5777 __func__, tm_function);
5778 if (ufshcd_clear_tm_cmd(hba, free_slot))
5779 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
5780 __func__, free_slot);
5781 err = -ETIMEDOUT;
5782 } else {
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005783 err = 0;
5784 memcpy(treq, hba->utmrdl_base_addr + free_slot, sizeof(*treq));
5785
Ohad Sharabi6667e6d2018-03-28 12:42:18 +03005786 ufshcd_add_tm_upiu_trace(hba, task_tag, "tm_complete");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305787 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305788
Stanley Chub5572172019-08-19 21:43:28 +08005789 spin_lock_irqsave(hba->host->host_lock, flags);
5790 __clear_bit(free_slot, &hba->outstanding_tasks);
5791 spin_unlock_irqrestore(hba->host->host_lock, flags);
5792
Bart Van Assche69a6c262019-12-09 10:13:09 -08005793 blk_put_request(req);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305794
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005795 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305796 return err;
5797}
5798
5799/**
Christoph Hellwigc6049cd2018-10-07 17:30:33 +03005800 * ufshcd_issue_tm_cmd - issues task management commands to controller
5801 * @hba: per adapter instance
5802 * @lun_id: LUN ID to which TM command is sent
5803 * @task_id: task ID to which the TM command is applicable
5804 * @tm_function: task management function opcode
5805 * @tm_response: task management service response return value
5806 *
5807 * Returns non-zero value on error, zero on success.
5808 */
5809static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5810 u8 tm_function, u8 *tm_response)
5811{
5812 struct utp_task_req_desc treq = { { 0 }, };
5813 int ocs_value, err;
5814
5815 /* Configure task request descriptor */
5816 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5817 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5818
5819 /* Configure task request UPIU */
5820 treq.req_header.dword_0 = cpu_to_be32(lun_id << 8) |
5821 cpu_to_be32(UPIU_TRANSACTION_TASK_REQ << 24);
5822 treq.req_header.dword_1 = cpu_to_be32(tm_function << 16);
5823
5824 /*
5825 * The host shall provide the same value for LUN field in the basic
5826 * header and for Input Parameter.
5827 */
5828 treq.input_param1 = cpu_to_be32(lun_id);
5829 treq.input_param2 = cpu_to_be32(task_id);
5830
5831 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function);
5832 if (err == -ETIMEDOUT)
5833 return err;
5834
5835 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
5836 if (ocs_value != OCS_SUCCESS)
5837 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
5838 __func__, ocs_value);
5839 else if (tm_response)
5840 *tm_response = be32_to_cpu(treq.output_param1) &
5841 MASK_TM_SERVICE_RESP;
5842 return err;
5843}
5844
5845/**
Avri Altman5e0a86e2018-10-07 17:30:37 +03005846 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
5847 * @hba: per-adapter instance
5848 * @req_upiu: upiu request
5849 * @rsp_upiu: upiu reply
Avri Altman5e0a86e2018-10-07 17:30:37 +03005850 * @desc_buff: pointer to descriptor buffer, NULL if NA
5851 * @buff_len: descriptor size, 0 if NA
Bart Van Assched0e97602019-10-29 16:07:08 -07005852 * @cmd_type: specifies the type (NOP, Query...)
Avri Altman5e0a86e2018-10-07 17:30:37 +03005853 * @desc_op: descriptor operation
5854 *
5855 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
5856 * Therefore, it "rides" the device management infrastructure: uses its tag and
5857 * tasks work queues.
5858 *
5859 * Since there is only one available tag for device management commands,
5860 * the caller is expected to hold the hba->dev_cmd.lock mutex.
5861 */
5862static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba,
5863 struct utp_upiu_req *req_upiu,
5864 struct utp_upiu_req *rsp_upiu,
5865 u8 *desc_buff, int *buff_len,
Bart Van Assche7f674c32019-10-29 16:07:09 -07005866 enum dev_cmd_type cmd_type,
Avri Altman5e0a86e2018-10-07 17:30:37 +03005867 enum query_opcode desc_op)
5868{
Bart Van Assche7252a362019-12-09 10:13:08 -08005869 struct request_queue *q = hba->cmd_queue;
5870 struct request *req;
Avri Altman5e0a86e2018-10-07 17:30:37 +03005871 struct ufshcd_lrb *lrbp;
5872 int err = 0;
5873 int tag;
5874 struct completion wait;
5875 unsigned long flags;
5876 u32 upiu_flags;
5877
5878 down_read(&hba->clk_scaling_lock);
5879
Bart Van Assche7252a362019-12-09 10:13:08 -08005880 req = blk_get_request(q, REQ_OP_DRV_OUT, 0);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03005881 if (IS_ERR(req)) {
5882 err = PTR_ERR(req);
5883 goto out_unlock;
5884 }
Bart Van Assche7252a362019-12-09 10:13:08 -08005885 tag = req->tag;
5886 WARN_ON_ONCE(!ufshcd_valid_tag(hba, tag));
Avri Altman5e0a86e2018-10-07 17:30:37 +03005887
5888 init_completion(&wait);
5889 lrbp = &hba->lrb[tag];
5890 WARN_ON(lrbp->cmd);
5891
5892 lrbp->cmd = NULL;
5893 lrbp->sense_bufflen = 0;
5894 lrbp->sense_buffer = NULL;
5895 lrbp->task_tag = tag;
5896 lrbp->lun = 0;
5897 lrbp->intr_cmd = true;
5898 hba->dev_cmd.type = cmd_type;
5899
5900 switch (hba->ufs_version) {
5901 case UFSHCI_VERSION_10:
5902 case UFSHCI_VERSION_11:
5903 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
5904 break;
5905 default:
5906 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
5907 break;
5908 }
5909
5910 /* update the task tag in the request upiu */
5911 req_upiu->header.dword_0 |= cpu_to_be32(tag);
5912
5913 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
5914
5915 /* just copy the upiu request as it is */
5916 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr));
5917 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_WRITE_DESC) {
5918 /* The Data Segment Area is optional depending upon the query
5919 * function value. for WRITE DESCRIPTOR, the data segment
5920 * follows right after the tsf.
5921 */
5922 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len);
5923 *buff_len = 0;
5924 }
5925
5926 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
5927
5928 hba->dev_cmd.complete = &wait;
5929
5930 /* Make sure descriptors are ready before ringing the doorbell */
5931 wmb();
5932 spin_lock_irqsave(hba->host->host_lock, flags);
5933 ufshcd_send_command(hba, tag);
5934 spin_unlock_irqrestore(hba->host->host_lock, flags);
5935
5936 /*
5937 * ignore the returning value here - ufshcd_check_query_response is
5938 * bound to fail since dev_cmd.query and dev_cmd.type were left empty.
5939 * read the response directly ignoring all errors.
5940 */
5941 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT);
5942
5943 /* just copy the upiu response as it is */
5944 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu));
Avri Altman4bbbe242019-02-20 09:11:13 +02005945 if (desc_buff && desc_op == UPIU_QUERY_OPCODE_READ_DESC) {
5946 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu);
5947 u16 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
5948 MASK_QUERY_DATA_SEG_LEN;
5949
5950 if (*buff_len >= resp_len) {
5951 memcpy(desc_buff, descp, resp_len);
5952 *buff_len = resp_len;
5953 } else {
Bean Huo3d4881d2019-11-12 23:34:35 +01005954 dev_warn(hba->dev,
5955 "%s: rsp size %d is bigger than buffer size %d",
5956 __func__, resp_len, *buff_len);
Avri Altman4bbbe242019-02-20 09:11:13 +02005957 *buff_len = 0;
5958 err = -EINVAL;
5959 }
5960 }
Avri Altman5e0a86e2018-10-07 17:30:37 +03005961
Bart Van Assche7252a362019-12-09 10:13:08 -08005962 blk_put_request(req);
Dan Carpenterbb14dd12019-12-13 13:48:28 +03005963out_unlock:
Avri Altman5e0a86e2018-10-07 17:30:37 +03005964 up_read(&hba->clk_scaling_lock);
5965 return err;
5966}
5967
5968/**
5969 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
5970 * @hba: per-adapter instance
5971 * @req_upiu: upiu request
5972 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
5973 * @msgcode: message code, one of UPIU Transaction Codes Initiator to Target
5974 * @desc_buff: pointer to descriptor buffer, NULL if NA
5975 * @buff_len: descriptor size, 0 if NA
5976 * @desc_op: descriptor operation
5977 *
5978 * Supports UTP Transfer requests (nop and query), and UTP Task
5979 * Management requests.
5980 * It is up to the caller to fill the upiu conent properly, as it will
5981 * be copied without any further input validations.
5982 */
5983int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba,
5984 struct utp_upiu_req *req_upiu,
5985 struct utp_upiu_req *rsp_upiu,
5986 int msgcode,
5987 u8 *desc_buff, int *buff_len,
5988 enum query_opcode desc_op)
5989{
5990 int err;
Bart Van Assche7f674c32019-10-29 16:07:09 -07005991 enum dev_cmd_type cmd_type = DEV_CMD_TYPE_QUERY;
Avri Altman5e0a86e2018-10-07 17:30:37 +03005992 struct utp_task_req_desc treq = { { 0 }, };
5993 int ocs_value;
5994 u8 tm_f = be32_to_cpu(req_upiu->header.dword_1) >> 16 & MASK_TM_FUNC;
5995
Avri Altman5e0a86e2018-10-07 17:30:37 +03005996 switch (msgcode) {
5997 case UPIU_TRANSACTION_NOP_OUT:
5998 cmd_type = DEV_CMD_TYPE_NOP;
5999 /* fall through */
6000 case UPIU_TRANSACTION_QUERY_REQ:
6001 ufshcd_hold(hba, false);
6002 mutex_lock(&hba->dev_cmd.lock);
6003 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu,
6004 desc_buff, buff_len,
6005 cmd_type, desc_op);
6006 mutex_unlock(&hba->dev_cmd.lock);
6007 ufshcd_release(hba);
6008
6009 break;
6010 case UPIU_TRANSACTION_TASK_REQ:
6011 treq.header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
6012 treq.header.dword_2 = cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
6013
6014 memcpy(&treq.req_header, req_upiu, sizeof(*req_upiu));
6015
6016 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f);
6017 if (err == -ETIMEDOUT)
6018 break;
6019
6020 ocs_value = le32_to_cpu(treq.header.dword_2) & MASK_OCS;
6021 if (ocs_value != OCS_SUCCESS) {
6022 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__,
6023 ocs_value);
6024 break;
6025 }
6026
6027 memcpy(rsp_upiu, &treq.rsp_header, sizeof(*rsp_upiu));
6028
6029 break;
6030 default:
6031 err = -EINVAL;
6032
6033 break;
6034 }
6035
Avri Altman5e0a86e2018-10-07 17:30:37 +03006036 return err;
6037}
6038
6039/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306040 * ufshcd_eh_device_reset_handler - device reset handler registered to
6041 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306042 * @cmd: SCSI command pointer
6043 *
6044 * Returns SUCCESS/FAILED
6045 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306046static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306047{
6048 struct Scsi_Host *host;
6049 struct ufs_hba *hba;
6050 unsigned int tag;
6051 u32 pos;
6052 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306053 u8 resp = 0xF;
6054 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306055 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306056
6057 host = cmd->device->host;
6058 hba = shost_priv(host);
6059 tag = cmd->request->tag;
6060
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306061 lrbp = &hba->lrb[tag];
6062 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
6063 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306064 if (!err)
6065 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306066 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306067 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306068
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306069 /* clear the commands that were pending for corresponding LUN */
6070 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
6071 if (hba->lrb[pos].lun == lrbp->lun) {
6072 err = ufshcd_clear_cmd(hba, pos);
6073 if (err)
6074 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306075 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306076 }
6077 spin_lock_irqsave(host->host_lock, flags);
6078 ufshcd_transfer_req_compl(hba);
6079 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08006080
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306081out:
Gilad Broner7fabb772017-02-03 16:56:50 -08006082 hba->req_abort_count = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08006083 ufshcd_update_reg_hist(&hba->ufs_stats.dev_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306084 if (!err) {
6085 err = SUCCESS;
6086 } else {
6087 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
6088 err = FAILED;
6089 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306090 return err;
6091}
6092
Gilad Bronere0b299e2017-02-03 16:56:40 -08006093static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
6094{
6095 struct ufshcd_lrb *lrbp;
6096 int tag;
6097
6098 for_each_set_bit(tag, &bitmap, hba->nutrs) {
6099 lrbp = &hba->lrb[tag];
6100 lrbp->req_abort_skip = true;
6101 }
6102}
6103
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306104/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306105 * ufshcd_abort - abort a specific command
6106 * @cmd: SCSI command pointer
6107 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306108 * Abort the pending command in device by sending UFS_ABORT_TASK task management
6109 * command, and in host controller by clearing the door-bell register. There can
6110 * be race between controller sending the command to the device while abort is
6111 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
6112 * really issued and then try to abort it.
6113 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306114 * Returns SUCCESS/FAILED
6115 */
6116static int ufshcd_abort(struct scsi_cmnd *cmd)
6117{
6118 struct Scsi_Host *host;
6119 struct ufs_hba *hba;
6120 unsigned long flags;
6121 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306122 int err = 0;
6123 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306124 u8 resp = 0xF;
6125 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03006126 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306127
6128 host = cmd->device->host;
6129 hba = shost_priv(host);
6130 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08006131 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02006132 if (!ufshcd_valid_tag(hba, tag)) {
6133 dev_err(hba->dev,
6134 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
6135 __func__, tag, cmd, cmd->request);
6136 BUG();
6137 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306138
Dolev Ravive7d38252016-12-22 18:40:07 -08006139 /*
6140 * Task abort to the device W-LUN is illegal. When this command
6141 * will fail, due to spec violation, scsi err handling next step
6142 * will be to send LU reset which, again, is a spec violation.
6143 * To avoid these unnecessary/illegal step we skip to the last error
6144 * handling stage: reset and restore.
6145 */
6146 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
6147 return ufshcd_eh_host_reset_handler(cmd);
6148
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006149 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03006150 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02006151 /* If command is already aborted/completed, return SUCCESS */
6152 if (!(test_bit(tag, &hba->outstanding_reqs))) {
6153 dev_err(hba->dev,
6154 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
6155 __func__, tag, hba->outstanding_reqs, reg);
6156 goto out;
6157 }
6158
Dolev Ravive9d501b2014-07-01 12:22:37 +03006159 if (!(reg & (1 << tag))) {
6160 dev_err(hba->dev,
6161 "%s: cmd was completed, but without a notifying intr, tag = %d",
6162 __func__, tag);
6163 }
6164
Dolev Raviv66cc8202016-12-22 18:39:42 -08006165 /* Print Transfer Request of aborted task */
6166 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08006167
Gilad Broner7fabb772017-02-03 16:56:50 -08006168 /*
6169 * Print detailed info about aborted request.
6170 * As more than one request might get aborted at the same time,
6171 * print full information only for the first aborted request in order
6172 * to reduce repeated printouts. For other aborted requests only print
6173 * basic details.
6174 */
6175 scsi_print_command(hba->lrb[tag].cmd);
6176 if (!hba->req_abort_count) {
Stanley Chu8808b4e2019-07-10 21:38:21 +08006177 ufshcd_update_reg_hist(&hba->ufs_stats.task_abort, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08006178 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08006179 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08006180 ufshcd_print_pwr_info(hba);
6181 ufshcd_print_trs(hba, 1 << tag, true);
6182 } else {
6183 ufshcd_print_trs(hba, 1 << tag, false);
6184 }
6185 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08006186
6187 /* Skip task abort in case previous aborts failed and report failure */
6188 if (lrbp->req_abort_skip) {
6189 err = -EIO;
6190 goto out;
6191 }
6192
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306193 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
6194 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6195 UFS_QUERY_TASK, &resp);
6196 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
6197 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006198 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
6199 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306200 break;
6201 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306202 /*
6203 * cmd not pending in the device, check if it is
6204 * in transition.
6205 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006206 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
6207 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306208 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
6209 if (reg & (1 << tag)) {
6210 /* sleep for max. 200us to stabilize */
6211 usleep_range(100, 200);
6212 continue;
6213 }
6214 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006215 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
6216 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306217 goto out;
6218 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006219 dev_err(hba->dev,
6220 "%s: no response from device. tag = %d, err %d\n",
6221 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306222 if (!err)
6223 err = resp; /* service response error */
6224 goto out;
6225 }
6226 }
6227
6228 if (!poll_cnt) {
6229 err = -EBUSY;
6230 goto out;
6231 }
6232
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306233 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
6234 UFS_ABORT_TASK, &resp);
6235 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006236 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306237 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006238 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
6239 __func__, tag, err);
6240 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306241 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306242 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306243
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306244 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006245 if (err) {
6246 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
6247 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306248 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006249 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306250
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306251 scsi_dma_unmap(cmd);
6252
6253 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02006254 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306255 hba->lrb[tag].cmd = NULL;
6256 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306257
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306258out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306259 if (!err) {
6260 err = SUCCESS;
6261 } else {
6262 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08006263 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05306264 err = FAILED;
6265 }
6266
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006267 /*
6268 * This ufshcd_release() corresponds to the original scsi cmd that got
6269 * aborted here (as we won't get any IRQ for it).
6270 */
6271 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306272 return err;
6273}
6274
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306275/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306276 * ufshcd_host_reset_and_restore - reset and restore host controller
6277 * @hba: per-adapter instance
6278 *
6279 * Note that host controller reset may issue DME_RESET to
6280 * local and remote (device) Uni-Pro stack and the attributes
6281 * are reset to default state.
6282 *
6283 * Returns zero on success, non-zero on failure
6284 */
6285static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
6286{
6287 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306288 unsigned long flags;
6289
Can Guo2df74b62019-11-25 22:53:33 -08006290 /*
6291 * Stop the host controller and complete the requests
6292 * cleared by h/w
6293 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306294 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006295 ufshcd_hba_stop(hba, false);
Can Guo2df74b62019-11-25 22:53:33 -08006296 hba->silence_err_logs = true;
6297 ufshcd_complete_requests(hba);
6298 hba->silence_err_logs = false;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306299 spin_unlock_irqrestore(hba->host->host_lock, flags);
6300
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08006301 /* scale up clocks to max frequency before full reinitialization */
6302 ufshcd_scale_clks(hba, true);
6303
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306304 err = ufshcd_hba_enable(hba);
6305 if (err)
6306 goto out;
6307
6308 /* Establish the link again and restore the device */
Bean Huo1b9e2142020-01-20 14:08:15 +01006309 err = ufshcd_probe_hba(hba, false);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006310
6311 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306312 err = -EIO;
6313out:
6314 if (err)
6315 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
Stanley Chu8808b4e2019-07-10 21:38:21 +08006316 ufshcd_update_reg_hist(&hba->ufs_stats.host_reset, (u32)err);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306317 return err;
6318}
6319
6320/**
6321 * ufshcd_reset_and_restore - reset and re-initialize host/device
6322 * @hba: per-adapter instance
6323 *
6324 * Reset and recover device, host and re-establish link. This
6325 * is helpful to recover the communication in fatal error conditions.
6326 *
6327 * Returns zero on success, non-zero on failure
6328 */
6329static int ufshcd_reset_and_restore(struct ufs_hba *hba)
6330{
6331 int err = 0;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006332 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306333
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006334 do {
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07006335 /* Reset the attached device */
6336 ufshcd_vops_device_reset(hba);
6337
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006338 err = ufshcd_host_reset_and_restore(hba);
6339 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306340
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306341 return err;
6342}
6343
6344/**
6345 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006346 * @cmd: SCSI command pointer
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306347 *
6348 * Returns SUCCESS/FAILED
6349 */
6350static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
6351{
6352 int err;
6353 unsigned long flags;
6354 struct ufs_hba *hba;
6355
6356 hba = shost_priv(cmd->device->host);
6357
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006358 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306359 /*
6360 * Check if there is any race with fatal error handling.
6361 * If so, wait for it to complete. Even though fatal error
6362 * handling does reset and restore in some cases, don't assume
6363 * anything out of it. We are just avoiding race here.
6364 */
6365 do {
6366 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306367 if (!(work_pending(&hba->eh_work) ||
Zang Leigang8dc0da72017-06-24 19:14:32 +08006368 hba->ufshcd_state == UFSHCD_STATE_RESET ||
6369 hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306370 break;
6371 spin_unlock_irqrestore(hba->host->host_lock, flags);
6372 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306373 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306374 } while (1);
6375
6376 hba->ufshcd_state = UFSHCD_STATE_RESET;
6377 ufshcd_set_eh_in_progress(hba);
6378 spin_unlock_irqrestore(hba->host->host_lock, flags);
6379
6380 err = ufshcd_reset_and_restore(hba);
6381
6382 spin_lock_irqsave(hba->host->host_lock, flags);
6383 if (!err) {
6384 err = SUCCESS;
6385 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
6386 } else {
6387 err = FAILED;
6388 hba->ufshcd_state = UFSHCD_STATE_ERROR;
6389 }
6390 ufshcd_clear_eh_in_progress(hba);
6391 spin_unlock_irqrestore(hba->host->host_lock, flags);
6392
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006393 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306394 return err;
6395}
6396
6397/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006398 * ufshcd_get_max_icc_level - calculate the ICC level
6399 * @sup_curr_uA: max. current supported by the regulator
6400 * @start_scan: row at the desc table to start scan from
6401 * @buff: power descriptor buffer
6402 *
6403 * Returns calculated max ICC level for specific regulator
6404 */
6405static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
6406{
6407 int i;
6408 int curr_uA;
6409 u16 data;
6410 u16 unit;
6411
6412 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02006413 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006414 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
6415 ATTR_ICC_LVL_UNIT_OFFSET;
6416 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
6417 switch (unit) {
6418 case UFSHCD_NANO_AMP:
6419 curr_uA = curr_uA / 1000;
6420 break;
6421 case UFSHCD_MILI_AMP:
6422 curr_uA = curr_uA * 1000;
6423 break;
6424 case UFSHCD_AMP:
6425 curr_uA = curr_uA * 1000 * 1000;
6426 break;
6427 case UFSHCD_MICRO_AMP:
6428 default:
6429 break;
6430 }
6431 if (sup_curr_uA >= curr_uA)
6432 break;
6433 }
6434 if (i < 0) {
6435 i = 0;
6436 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
6437 }
6438
6439 return (u32)i;
6440}
6441
6442/**
6443 * ufshcd_calc_icc_level - calculate the max ICC level
6444 * In case regulators are not initialized we'll return 0
6445 * @hba: per-adapter instance
6446 * @desc_buf: power descriptor buffer to extract ICC levels from.
6447 * @len: length of desc_buff
6448 *
6449 * Returns calculated ICC level
6450 */
6451static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
6452 u8 *desc_buf, int len)
6453{
6454 u32 icc_level = 0;
6455
6456 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
6457 !hba->vreg_info.vccq2) {
6458 dev_err(hba->dev,
6459 "%s: Regulator capability was not set, actvIccLevel=%d",
6460 __func__, icc_level);
6461 goto out;
6462 }
6463
Stanley Chu0487fff2019-03-28 17:16:25 +08006464 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006465 icc_level = ufshcd_get_max_icc_level(
6466 hba->vreg_info.vcc->max_uA,
6467 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
6468 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
6469
Stanley Chu0487fff2019-03-28 17:16:25 +08006470 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006471 icc_level = ufshcd_get_max_icc_level(
6472 hba->vreg_info.vccq->max_uA,
6473 icc_level,
6474 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
6475
Stanley Chu0487fff2019-03-28 17:16:25 +08006476 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006477 icc_level = ufshcd_get_max_icc_level(
6478 hba->vreg_info.vccq2->max_uA,
6479 icc_level,
6480 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
6481out:
6482 return icc_level;
6483}
6484
6485static void ufshcd_init_icc_levels(struct ufs_hba *hba)
6486{
6487 int ret;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006488 int buff_len = hba->desc_size.pwr_desc;
Kees Cookbbe21d72018-05-02 16:58:09 -07006489 u8 *desc_buf;
6490
6491 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6492 if (!desc_buf)
6493 return;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006494
Bean Huo8c9a51b2020-01-20 14:08:17 +01006495 ret = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0,
6496 desc_buf, buff_len);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006497 if (ret) {
6498 dev_err(hba->dev,
6499 "%s: Failed reading power descriptor.len = %d ret = %d",
6500 __func__, buff_len, ret);
Kees Cookbbe21d72018-05-02 16:58:09 -07006501 goto out;
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006502 }
6503
6504 hba->init_prefetch_data.icc_level =
6505 ufshcd_find_max_sup_active_icc_level(hba,
6506 desc_buf, buff_len);
6507 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
6508 __func__, hba->init_prefetch_data.icc_level);
6509
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02006510 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
6511 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
6512 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006513
6514 if (ret)
6515 dev_err(hba->dev,
6516 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
6517 __func__, hba->init_prefetch_data.icc_level , ret);
6518
Kees Cookbbe21d72018-05-02 16:58:09 -07006519out:
6520 kfree(desc_buf);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006521}
6522
6523/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006524 * ufshcd_scsi_add_wlus - Adds required W-LUs
6525 * @hba: per-adapter instance
6526 *
6527 * UFS device specification requires the UFS devices to support 4 well known
6528 * logical units:
6529 * "REPORT_LUNS" (address: 01h)
6530 * "UFS Device" (address: 50h)
6531 * "RPMB" (address: 44h)
6532 * "BOOT" (address: 30h)
6533 * UFS device's power management needs to be controlled by "POWER CONDITION"
6534 * field of SSU (START STOP UNIT) command. But this "power condition" field
6535 * will take effect only when its sent to "UFS device" well known logical unit
6536 * hence we require the scsi_device instance to represent this logical unit in
6537 * order for the UFS host driver to send the SSU command for power management.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006538 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006539 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
6540 * Block) LU so user space process can control this LU. User space may also
6541 * want to have access to BOOT LU.
Bart Van Assche8aa29f12018-03-01 15:07:20 -08006542 *
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006543 * This function adds scsi device instances for each of all well known LUs
6544 * (except "REPORT LUNS" LU).
6545 *
6546 * Returns zero on success (all required W-LUs are added successfully),
6547 * non-zero error value on failure (if failed to add any of the required W-LU).
6548 */
6549static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
6550{
6551 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006552 struct scsi_device *sdev_rpmb;
6553 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006554
6555 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
6556 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
6557 if (IS_ERR(hba->sdev_ufs_device)) {
6558 ret = PTR_ERR(hba->sdev_ufs_device);
6559 hba->sdev_ufs_device = NULL;
6560 goto out;
6561 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006562 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006563
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006564 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006565 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006566 if (IS_ERR(sdev_rpmb)) {
6567 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006568 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006569 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006570 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08006571
6572 sdev_boot = __scsi_add_device(hba->host, 0, 0,
6573 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
6574 if (IS_ERR(sdev_boot))
6575 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
6576 else
6577 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006578 goto out;
6579
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006580remove_sdev_ufs_device:
6581 scsi_remove_device(hba->sdev_ufs_device);
6582out:
6583 return ret;
6584}
6585
Bean Huo09750062020-01-20 14:08:14 +01006586static int ufs_get_device_desc(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006587{
6588 int err;
Kees Cookbbe21d72018-05-02 16:58:09 -07006589 size_t buff_len;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006590 u8 model_index;
Kees Cookbbe21d72018-05-02 16:58:09 -07006591 u8 *desc_buf;
Bean Huo09750062020-01-20 14:08:14 +01006592 struct ufs_dev_info *dev_info = &hba->dev_info;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006593
Kees Cookbbe21d72018-05-02 16:58:09 -07006594 buff_len = max_t(size_t, hba->desc_size.dev_desc,
6595 QUERY_DESC_MAX_SIZE + 1);
6596 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6597 if (!desc_buf) {
6598 err = -ENOMEM;
6599 goto out;
6600 }
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006601
Bean Huo8c9a51b2020-01-20 14:08:17 +01006602 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, desc_buf,
6603 hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006604 if (err) {
6605 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6606 __func__, err);
6607 goto out;
6608 }
6609
6610 /*
6611 * getting vendor (manufacturerID) and Bank Index in big endian
6612 * format
6613 */
Bean Huo09750062020-01-20 14:08:14 +01006614 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006615 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6616
6617 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006618 err = ufshcd_read_string_desc(hba, model_index,
Bean Huo09750062020-01-20 14:08:14 +01006619 &dev_info->model, SD_ASCII_STD);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006620 if (err < 0) {
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006621 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
6622 __func__, err);
6623 goto out;
6624 }
6625
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006626 /*
6627 * ufshcd_read_string_desc returns size of the string
6628 * reset the error value
6629 */
6630 err = 0;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006631
6632out:
Kees Cookbbe21d72018-05-02 16:58:09 -07006633 kfree(desc_buf);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006634 return err;
6635}
6636
Bean Huo09750062020-01-20 14:08:14 +01006637static void ufs_put_device_desc(struct ufs_hba *hba)
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006638{
Bean Huo09750062020-01-20 14:08:14 +01006639 struct ufs_dev_info *dev_info = &hba->dev_info;
6640
6641 kfree(dev_info->model);
6642 dev_info->model = NULL;
Tomas Winkler4b828fe2019-07-30 08:55:17 +03006643}
6644
Bean Huo09750062020-01-20 14:08:14 +01006645static void ufs_fixup_device_setup(struct ufs_hba *hba)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006646{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006647 struct ufs_dev_fix *f;
Bean Huo09750062020-01-20 14:08:14 +01006648 struct ufs_dev_info *dev_info = &hba->dev_info;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006649
6650 for (f = ufs_fixups; f->quirk; f++) {
Bean Huo09750062020-01-20 14:08:14 +01006651 if ((f->wmanufacturerid == dev_info->wmanufacturerid ||
6652 f->wmanufacturerid == UFS_ANY_VENDOR) &&
6653 ((dev_info->model &&
6654 STR_PRFX_EQUAL(f->model, dev_info->model)) ||
6655 !strcmp(f->model, UFS_ANY_MODEL)))
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006656 hba->dev_quirks |= f->quirk;
6657 }
6658}
6659
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006660/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006661 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6662 * @hba: per-adapter instance
6663 *
6664 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6665 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6666 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6667 * the hibern8 exit latency.
6668 *
6669 * Returns zero on success, non-zero error value on failure.
6670 */
6671static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6672{
6673 int ret = 0;
6674 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6675
6676 ret = ufshcd_dme_peer_get(hba,
6677 UIC_ARG_MIB_SEL(
6678 RX_MIN_ACTIVATETIME_CAPABILITY,
6679 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6680 &peer_rx_min_activatetime);
6681 if (ret)
6682 goto out;
6683
6684 /* make sure proper unit conversion is applied */
6685 tuned_pa_tactivate =
6686 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6687 / PA_TACTIVATE_TIME_UNIT_US);
6688 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6689 tuned_pa_tactivate);
6690
6691out:
6692 return ret;
6693}
6694
6695/**
6696 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6697 * @hba: per-adapter instance
6698 *
6699 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6700 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6701 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6702 * This optimal value can help reduce the hibern8 exit latency.
6703 *
6704 * Returns zero on success, non-zero error value on failure.
6705 */
6706static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6707{
6708 int ret = 0;
6709 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6710 u32 max_hibern8_time, tuned_pa_hibern8time;
6711
6712 ret = ufshcd_dme_get(hba,
6713 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6714 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6715 &local_tx_hibern8_time_cap);
6716 if (ret)
6717 goto out;
6718
6719 ret = ufshcd_dme_peer_get(hba,
6720 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6721 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6722 &peer_rx_hibern8_time_cap);
6723 if (ret)
6724 goto out;
6725
6726 max_hibern8_time = max(local_tx_hibern8_time_cap,
6727 peer_rx_hibern8_time_cap);
6728 /* make sure proper unit conversion is applied */
6729 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6730 / PA_HIBERN8_TIME_UNIT_US);
6731 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6732 tuned_pa_hibern8time);
6733out:
6734 return ret;
6735}
6736
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006737/**
6738 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6739 * less than device PA_TACTIVATE time.
6740 * @hba: per-adapter instance
6741 *
6742 * Some UFS devices require host PA_TACTIVATE to be lower than device
6743 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6744 * for such devices.
6745 *
6746 * Returns zero on success, non-zero error value on failure.
6747 */
6748static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6749{
6750 int ret = 0;
6751 u32 granularity, peer_granularity;
6752 u32 pa_tactivate, peer_pa_tactivate;
6753 u32 pa_tactivate_us, peer_pa_tactivate_us;
6754 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6755
6756 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6757 &granularity);
6758 if (ret)
6759 goto out;
6760
6761 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6762 &peer_granularity);
6763 if (ret)
6764 goto out;
6765
6766 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6767 (granularity > PA_GRANULARITY_MAX_VAL)) {
6768 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6769 __func__, granularity);
6770 return -EINVAL;
6771 }
6772
6773 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6774 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6775 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6776 __func__, peer_granularity);
6777 return -EINVAL;
6778 }
6779
6780 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6781 if (ret)
6782 goto out;
6783
6784 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6785 &peer_pa_tactivate);
6786 if (ret)
6787 goto out;
6788
6789 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6790 peer_pa_tactivate_us = peer_pa_tactivate *
6791 gran_to_us_table[peer_granularity - 1];
6792
6793 if (pa_tactivate_us > peer_pa_tactivate_us) {
6794 u32 new_peer_pa_tactivate;
6795
6796 new_peer_pa_tactivate = pa_tactivate_us /
6797 gran_to_us_table[peer_granularity - 1];
6798 new_peer_pa_tactivate++;
6799 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6800 new_peer_pa_tactivate);
6801 }
6802
6803out:
6804 return ret;
6805}
6806
Bean Huo09750062020-01-20 14:08:14 +01006807static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
Yaniv Gardi37113102016-03-10 17:37:16 +02006808{
6809 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6810 ufshcd_tune_pa_tactivate(hba);
6811 ufshcd_tune_pa_hibern8time(hba);
6812 }
6813
6814 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6815 /* set 1ms timeout for PA_TACTIVATE */
6816 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006817
6818 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6819 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08006820
Bean Huo09750062020-01-20 14:08:14 +01006821 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006822}
6823
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006824static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6825{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006826 hba->ufs_stats.hibern8_exit_cnt = 0;
6827 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Gilad Broner7fabb772017-02-03 16:56:50 -08006828 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006829}
6830
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006831static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
6832{
6833 int err;
6834
6835 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
6836 &hba->desc_size.dev_desc);
6837 if (err)
6838 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
6839
6840 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
6841 &hba->desc_size.pwr_desc);
6842 if (err)
6843 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
6844
6845 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
6846 &hba->desc_size.interc_desc);
6847 if (err)
6848 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
6849
6850 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
6851 &hba->desc_size.conf_desc);
6852 if (err)
6853 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
6854
6855 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
6856 &hba->desc_size.unit_desc);
6857 if (err)
6858 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
6859
6860 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
6861 &hba->desc_size.geom_desc);
6862 if (err)
6863 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
Bean Huo059efd82019-10-29 14:22:45 +00006864
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02006865 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
6866 &hba->desc_size.hlth_desc);
6867 if (err)
6868 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006869}
6870
Bean Huo731f0622020-01-20 14:08:19 +01006871static int ufshcd_device_geo_params_init(struct ufs_hba *hba)
6872{
6873 int err;
6874 size_t buff_len;
6875 u8 *desc_buf;
6876
6877 buff_len = hba->desc_size.geom_desc;
6878 desc_buf = kmalloc(buff_len, GFP_KERNEL);
6879 if (!desc_buf) {
6880 err = -ENOMEM;
6881 goto out;
6882 }
6883
6884 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_GEOMETRY, 0,
6885 desc_buf, buff_len);
6886 if (err) {
6887 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n",
6888 __func__, err);
6889 goto out;
6890 }
6891
6892 if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 1)
6893 hba->dev_info.max_lu_supported = 32;
6894 else if (desc_buf[GEOMETRY_DESC_PARAM_MAX_NUM_LUN] == 0)
6895 hba->dev_info.max_lu_supported = 8;
6896
6897out:
6898 kfree(desc_buf);
6899 return err;
6900}
6901
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05306902static struct ufs_ref_clk ufs_ref_clk_freqs[] = {
6903 {19200000, REF_CLK_FREQ_19_2_MHZ},
6904 {26000000, REF_CLK_FREQ_26_MHZ},
6905 {38400000, REF_CLK_FREQ_38_4_MHZ},
6906 {52000000, REF_CLK_FREQ_52_MHZ},
6907 {0, REF_CLK_FREQ_INVAL},
6908};
6909
6910static enum ufs_ref_clk_freq
6911ufs_get_bref_clk_from_hz(unsigned long freq)
6912{
6913 int i;
6914
6915 for (i = 0; ufs_ref_clk_freqs[i].freq_hz; i++)
6916 if (ufs_ref_clk_freqs[i].freq_hz == freq)
6917 return ufs_ref_clk_freqs[i].val;
6918
6919 return REF_CLK_FREQ_INVAL;
6920}
6921
6922void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk)
6923{
6924 unsigned long freq;
6925
6926 freq = clk_get_rate(refclk);
6927
6928 hba->dev_ref_clk_freq =
6929 ufs_get_bref_clk_from_hz(freq);
6930
6931 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL)
6932 dev_err(hba->dev,
6933 "invalid ref_clk setting = %ld\n", freq);
6934}
6935
6936static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba)
6937{
6938 int err;
6939 u32 ref_clk;
6940 u32 freq = hba->dev_ref_clk_freq;
6941
6942 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
6943 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &ref_clk);
6944
6945 if (err) {
6946 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n",
6947 err);
6948 goto out;
6949 }
6950
6951 if (ref_clk == freq)
6952 goto out; /* nothing to update */
6953
6954 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
6955 QUERY_ATTR_IDN_REF_CLK_FREQ, 0, 0, &freq);
6956
6957 if (err) {
6958 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n",
6959 ufs_ref_clk_freqs[freq].freq_hz);
6960 goto out;
6961 }
6962
6963 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n",
6964 ufs_ref_clk_freqs[freq].freq_hz);
6965
6966out:
6967 return err;
6968}
6969
Bean Huo1b9e2142020-01-20 14:08:15 +01006970static int ufshcd_device_params_init(struct ufs_hba *hba)
6971{
6972 bool flag;
6973 int ret;
6974
Bean Huo731f0622020-01-20 14:08:19 +01006975 /* Clear any previous UFS device information */
6976 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
6977
Bean Huo1b9e2142020-01-20 14:08:15 +01006978 /* Init check for device descriptor sizes */
6979 ufshcd_init_desc_sizes(hba);
6980
Bean Huo731f0622020-01-20 14:08:19 +01006981 /* Init UFS geometry descriptor related parameters */
6982 ret = ufshcd_device_geo_params_init(hba);
6983 if (ret)
6984 goto out;
6985
Bean Huo1b9e2142020-01-20 14:08:15 +01006986 /* Check and apply UFS device quirks */
6987 ret = ufs_get_device_desc(hba);
6988 if (ret) {
6989 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
6990 __func__, ret);
6991 goto out;
6992 }
6993
6994 ufs_fixup_device_setup(hba);
6995
Bean Huo1b9e2142020-01-20 14:08:15 +01006996 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6997 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
6998 hba->dev_info.f_power_on_wp_en = flag;
6999
Bean Huo2b35b2a2020-01-20 14:08:16 +01007000 /* Probe maximum power mode co-supported by both UFS host and device */
7001 if (ufshcd_get_max_pwr_mode(hba))
7002 dev_err(hba->dev,
7003 "%s: Failed getting max supported power mode\n",
7004 __func__);
Bean Huo1b9e2142020-01-20 14:08:15 +01007005out:
7006 return ret;
7007}
7008
7009/**
7010 * ufshcd_add_lus - probe and add UFS logical units
7011 * @hba: per-adapter instance
7012 */
7013static int ufshcd_add_lus(struct ufs_hba *hba)
7014{
7015 int ret;
7016
Bean Huo046c1e62020-01-20 14:08:18 +01007017 ufshcd_init_icc_levels(hba);
Bean Huo1b9e2142020-01-20 14:08:15 +01007018
7019 /* Add required well known logical units to scsi mid layer */
7020 ret = ufshcd_scsi_add_wlus(hba);
7021 if (ret)
7022 goto out;
7023
7024 /* Initialize devfreq after UFS device is detected */
7025 if (ufshcd_is_clkscaling_supported(hba)) {
7026 memcpy(&hba->clk_scaling.saved_pwr_info.info,
7027 &hba->pwr_info,
7028 sizeof(struct ufs_pa_layer_attr));
7029 hba->clk_scaling.saved_pwr_info.is_valid = true;
7030 if (!hba->devfreq) {
7031 ret = ufshcd_devfreq_init(hba);
7032 if (ret)
7033 goto out;
7034 }
7035
7036 hba->clk_scaling.is_allowed = true;
7037 }
7038
7039 ufs_bsg_probe(hba);
7040 scsi_scan_host(hba->host);
7041 pm_runtime_put_sync(hba->dev);
7042
Bean Huo1b9e2142020-01-20 14:08:15 +01007043out:
7044 return ret;
7045}
7046
Yaniv Gardi37113102016-03-10 17:37:16 +02007047/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007048 * ufshcd_probe_hba - probe hba to detect device and initialize
7049 * @hba: per-adapter instance
Bean Huo1b9e2142020-01-20 14:08:15 +01007050 * @async: asynchronous execution or not
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007051 *
7052 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307053 */
Bean Huo1b9e2142020-01-20 14:08:15 +01007054static int ufshcd_probe_hba(struct ufs_hba *hba, bool async)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307055{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307056 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007057 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307058
7059 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307060 if (ret)
7061 goto out;
7062
Yaniv Gardiafdfff52016-03-10 17:37:15 +02007063 /* set the default level for urgent bkops */
7064 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
7065 hba->is_urgent_bkops_lvl_checked = false;
7066
Dolev Ravivff8e20c2016-12-22 18:42:18 -08007067 /* Debug counters initialization */
7068 ufshcd_clear_dbg_ufs_stats(hba);
7069
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007070 /* UniPro link is active now */
7071 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05307072
Bean Huo1b9e2142020-01-20 14:08:15 +01007073 /* Verify device initialization by sending NOP OUT UPIU */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307074 ret = ufshcd_verify_dev_init(hba);
7075 if (ret)
7076 goto out;
7077
Bean Huo1b9e2142020-01-20 14:08:15 +01007078 /* Initiate UFS initialization, and waiting until completion */
Dolev Raviv68078d52013-07-30 00:35:58 +05307079 ret = ufshcd_complete_dev_init(hba);
7080 if (ret)
7081 goto out;
7082
Bean Huo1b9e2142020-01-20 14:08:15 +01007083 /*
7084 * Initialize UFS device parameters used by driver, these
7085 * parameters are associated with UFS descriptors.
7086 */
7087 if (async) {
7088 ret = ufshcd_device_params_init(hba);
7089 if (ret)
7090 goto out;
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02007091 }
7092
Bean Huo09750062020-01-20 14:08:14 +01007093 ufshcd_tune_unipro_params(hba);
Tomas Winkler4b828fe2019-07-30 08:55:17 +03007094
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007095 /* UFS device is also active now */
7096 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307097 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007098 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307099
Bean Huo2b35b2a2020-01-20 14:08:16 +01007100 /* Gear up to HS gear if supported */
7101 if (hba->max_pwr_info.is_valid) {
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307102 /*
7103 * Set the right value to bRefClkFreq before attempting to
7104 * switch to HS gears.
7105 */
7106 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL)
7107 ufshcd_set_dev_ref_clk(hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007108 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007109 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007110 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
7111 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07007112 goto out;
7113 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007114 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007115
Yaniv Gardi53c12d02016-02-01 15:02:45 +02007116 /* set the state as operational after switching to desired gear */
7117 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00007118
Can Guo71d848b2019-11-14 22:09:26 -08007119 /* Enable Auto-Hibernate if configured */
7120 ufshcd_auto_hibern8_enable(hba);
7121
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307122out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007123
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007124 trace_ufshcd_init(dev_name(hba->dev), ret,
7125 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007126 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007127 return ret;
7128}
7129
7130/**
7131 * ufshcd_async_scan - asynchronous execution for probing hba
7132 * @data: data pointer to pass to this function
7133 * @cookie: cookie data
7134 */
7135static void ufshcd_async_scan(void *data, async_cookie_t cookie)
7136{
7137 struct ufs_hba *hba = (struct ufs_hba *)data;
Bean Huo1b9e2142020-01-20 14:08:15 +01007138 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007139
Bean Huo1b9e2142020-01-20 14:08:15 +01007140 /* Initialize hba, detect and initialize UFS device */
7141 ret = ufshcd_probe_hba(hba, true);
7142 if (ret)
7143 goto out;
7144
7145 /* Probe and add UFS logical units */
7146 ret = ufshcd_add_lus(hba);
7147out:
7148 /*
7149 * If we failed to initialize the device or the device is not
7150 * present, turn off the power/clocks etc.
7151 */
7152 if (ret) {
7153 pm_runtime_put_sync(hba->dev);
7154 ufshcd_exit_clk_scaling(hba);
7155 ufshcd_hba_exit(hba);
7156 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307157}
7158
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007159static const struct attribute_group *ufshcd_driver_groups[] = {
7160 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02007161 &ufs_sysfs_lun_attributes_group,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007162 NULL,
7163};
7164
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307165static struct scsi_host_template ufshcd_driver_template = {
7166 .module = THIS_MODULE,
7167 .name = UFSHCD,
7168 .proc_name = UFSHCD,
7169 .queuecommand = ufshcd_queuecommand,
7170 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09007171 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307172 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03007173 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307174 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05307175 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
7176 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307177 .this_id = -1,
7178 .sg_tablesize = SG_ALL,
7179 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
7180 .can_queue = UFSHCD_CAN_QUEUE,
Christoph Hellwig552a9902019-06-17 14:19:55 +02007181 .max_segment_size = PRDT_DATA_BYTE_COUNT_MAX,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007182 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01007183 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02007184 .sdev_groups = ufshcd_driver_groups,
Christoph Hellwig4af14d12018-12-13 16:17:09 +01007185 .dma_boundary = PAGE_SIZE - 1,
Stanley Chu49615ba2019-09-16 23:56:50 +08007186 .rpm_autosuspend_delay = RPM_AUTOSUSPEND_DELAY_MS,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307187};
7188
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007189static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
7190 int ua)
7191{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007192 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007193
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007194 if (!vreg)
7195 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007196
Stanley Chu0487fff2019-03-28 17:16:25 +08007197 /*
7198 * "set_load" operation shall be required on those regulators
7199 * which specifically configured current limitation. Otherwise
7200 * zero max_uA may cause unexpected behavior when regulator is
7201 * enabled or set as high power mode.
7202 */
7203 if (!vreg->max_uA)
7204 return 0;
7205
Bjorn Andersson7b16a072015-02-11 19:35:28 -08007206 ret = regulator_set_load(vreg->reg, ua);
7207 if (ret < 0) {
7208 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
7209 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007210 }
7211
7212 return ret;
7213}
7214
7215static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
7216 struct ufs_vreg *vreg)
7217{
Marc Gonzalez73067982019-02-27 11:41:45 +01007218 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007219}
7220
7221static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
7222 struct ufs_vreg *vreg)
7223{
Adrian Hunter7c7cfdc2019-08-14 15:59:50 +03007224 if (!vreg)
7225 return 0;
7226
Marc Gonzalez73067982019-02-27 11:41:45 +01007227 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007228}
7229
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007230static int ufshcd_config_vreg(struct device *dev,
7231 struct ufs_vreg *vreg, bool on)
7232{
7233 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007234 struct regulator *reg;
7235 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007236 int min_uV, uA_load;
7237
7238 BUG_ON(!vreg);
7239
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06007240 reg = vreg->reg;
7241 name = vreg->name;
7242
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007243 if (regulator_count_voltages(reg) > 0) {
Stanley Chu3b141e82019-03-28 17:16:24 +08007244 if (vreg->min_uV && vreg->max_uV) {
7245 min_uV = on ? vreg->min_uV : 0;
7246 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
7247 if (ret) {
7248 dev_err(dev,
7249 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007250 __func__, name, ret);
Stanley Chu3b141e82019-03-28 17:16:24 +08007251 goto out;
7252 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007253 }
7254
7255 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007256 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
7257 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007258 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007259 }
7260out:
7261 return ret;
7262}
7263
7264static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
7265{
7266 int ret = 0;
7267
Marc Gonzalez73067982019-02-27 11:41:45 +01007268 if (!vreg || vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007269 goto out;
7270
7271 ret = ufshcd_config_vreg(dev, vreg, true);
7272 if (!ret)
7273 ret = regulator_enable(vreg->reg);
7274
7275 if (!ret)
7276 vreg->enabled = true;
7277 else
7278 dev_err(dev, "%s: %s enable failed, err=%d\n",
7279 __func__, vreg->name, ret);
7280out:
7281 return ret;
7282}
7283
7284static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
7285{
7286 int ret = 0;
7287
Marc Gonzalez73067982019-02-27 11:41:45 +01007288 if (!vreg || !vreg->enabled)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007289 goto out;
7290
7291 ret = regulator_disable(vreg->reg);
7292
7293 if (!ret) {
7294 /* ignore errors on applying disable config */
7295 ufshcd_config_vreg(dev, vreg, false);
7296 vreg->enabled = false;
7297 } else {
7298 dev_err(dev, "%s: %s disable failed, err=%d\n",
7299 __func__, vreg->name, ret);
7300 }
7301out:
7302 return ret;
7303}
7304
7305static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
7306{
7307 int ret = 0;
7308 struct device *dev = hba->dev;
7309 struct ufs_vreg_info *info = &hba->vreg_info;
7310
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007311 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
7312 if (ret)
7313 goto out;
7314
7315 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
7316 if (ret)
7317 goto out;
7318
7319 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
7320 if (ret)
7321 goto out;
7322
7323out:
7324 if (ret) {
7325 ufshcd_toggle_vreg(dev, info->vccq2, false);
7326 ufshcd_toggle_vreg(dev, info->vccq, false);
7327 ufshcd_toggle_vreg(dev, info->vcc, false);
7328 }
7329 return ret;
7330}
7331
Raviv Shvili6a771a62014-09-25 15:32:24 +03007332static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
7333{
7334 struct ufs_vreg_info *info = &hba->vreg_info;
7335
Zeng Guangyue60b7b822019-03-30 17:03:13 +08007336 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007337}
7338
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007339static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
7340{
7341 int ret = 0;
7342
7343 if (!vreg)
7344 goto out;
7345
7346 vreg->reg = devm_regulator_get(dev, vreg->name);
7347 if (IS_ERR(vreg->reg)) {
7348 ret = PTR_ERR(vreg->reg);
7349 dev_err(dev, "%s: %s get failed, err=%d\n",
7350 __func__, vreg->name, ret);
7351 }
7352out:
7353 return ret;
7354}
7355
7356static int ufshcd_init_vreg(struct ufs_hba *hba)
7357{
7358 int ret = 0;
7359 struct device *dev = hba->dev;
7360 struct ufs_vreg_info *info = &hba->vreg_info;
7361
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007362 ret = ufshcd_get_vreg(dev, info->vcc);
7363 if (ret)
7364 goto out;
7365
7366 ret = ufshcd_get_vreg(dev, info->vccq);
7367 if (ret)
7368 goto out;
7369
7370 ret = ufshcd_get_vreg(dev, info->vccq2);
7371out:
7372 return ret;
7373}
7374
Raviv Shvili6a771a62014-09-25 15:32:24 +03007375static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
7376{
7377 struct ufs_vreg_info *info = &hba->vreg_info;
7378
7379 if (info)
7380 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
7381
7382 return 0;
7383}
7384
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007385static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
7386 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007387{
7388 int ret = 0;
7389 struct ufs_clk_info *clki;
7390 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007391 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007392 ktime_t start = ktime_get();
7393 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007394
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007395 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007396 goto out;
7397
Subhash Jadavanib3344562018-05-03 16:37:17 +05307398 /*
7399 * vendor specific setup_clocks ops may depend on clocks managed by
7400 * this standard driver hence call the vendor specific setup_clocks
7401 * before disabling the clocks managed here.
7402 */
7403 if (!on) {
7404 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
7405 if (ret)
7406 return ret;
7407 }
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007408
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007409 list_for_each_entry(clki, head, list) {
7410 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007411 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
7412 continue;
7413
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007414 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007415 if (on && !clki->enabled) {
7416 ret = clk_prepare_enable(clki->clk);
7417 if (ret) {
7418 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
7419 __func__, clki->name, ret);
7420 goto out;
7421 }
7422 } else if (!on && clki->enabled) {
7423 clk_disable_unprepare(clki->clk);
7424 }
7425 clki->enabled = on;
7426 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
7427 clki->name, on ? "en" : "dis");
7428 }
7429 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007430
Subhash Jadavanib3344562018-05-03 16:37:17 +05307431 /*
7432 * vendor specific setup_clocks ops may depend on clocks managed by
7433 * this standard driver hence call the vendor specific setup_clocks
7434 * after enabling the clocks managed here.
7435 */
7436 if (on) {
7437 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
7438 if (ret)
7439 return ret;
7440 }
Subhash Jadavani1e879e82016-10-06 21:48:22 -07007441
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007442out:
7443 if (ret) {
7444 list_for_each_entry(clki, head, list) {
7445 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
7446 clk_disable_unprepare(clki->clk);
7447 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007448 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007449 spin_lock_irqsave(hba->host->host_lock, flags);
7450 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007451 trace_ufshcd_clk_gating(dev_name(hba->dev),
7452 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007453 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007454 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007455
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08007456 if (clk_state_changed)
7457 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
7458 (on ? "on" : "off"),
7459 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007460 return ret;
7461}
7462
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007463static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
7464{
7465 return __ufshcd_setup_clocks(hba, on, false);
7466}
7467
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007468static int ufshcd_init_clocks(struct ufs_hba *hba)
7469{
7470 int ret = 0;
7471 struct ufs_clk_info *clki;
7472 struct device *dev = hba->dev;
7473 struct list_head *head = &hba->clk_list_head;
7474
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007475 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007476 goto out;
7477
7478 list_for_each_entry(clki, head, list) {
7479 if (!clki->name)
7480 continue;
7481
7482 clki->clk = devm_clk_get(dev, clki->name);
7483 if (IS_ERR(clki->clk)) {
7484 ret = PTR_ERR(clki->clk);
7485 dev_err(dev, "%s: %s clk get failed, %d\n",
7486 __func__, clki->name, ret);
7487 goto out;
7488 }
7489
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05307490 /*
7491 * Parse device ref clk freq as per device tree "ref_clk".
7492 * Default dev_ref_clk_freq is set to REF_CLK_FREQ_INVAL
7493 * in ufshcd_alloc_host().
7494 */
7495 if (!strcmp(clki->name, "ref_clk"))
7496 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk);
7497
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007498 if (clki->max_freq) {
7499 ret = clk_set_rate(clki->clk, clki->max_freq);
7500 if (ret) {
7501 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7502 __func__, clki->name,
7503 clki->max_freq, ret);
7504 goto out;
7505 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03007506 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007507 }
7508 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
7509 clki->name, clk_get_rate(clki->clk));
7510 }
7511out:
7512 return ret;
7513}
7514
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007515static int ufshcd_variant_hba_init(struct ufs_hba *hba)
7516{
7517 int err = 0;
7518
7519 if (!hba->vops)
7520 goto out;
7521
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007522 err = ufshcd_vops_init(hba);
7523 if (err)
7524 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007525
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007526 err = ufshcd_vops_setup_regulators(hba, true);
7527 if (err)
7528 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007529
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007530 goto out;
7531
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007532out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007533 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007534out:
7535 if (err)
7536 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007537 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007538 return err;
7539}
7540
7541static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
7542{
7543 if (!hba->vops)
7544 return;
7545
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007546 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007547
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007548 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007549}
7550
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007551static int ufshcd_hba_init(struct ufs_hba *hba)
7552{
7553 int err;
7554
Raviv Shvili6a771a62014-09-25 15:32:24 +03007555 /*
7556 * Handle host controller power separately from the UFS device power
7557 * rails as it will help controlling the UFS host controller power
7558 * collapse easily which is different than UFS device power collapse.
7559 * Also, enable the host controller power before we go ahead with rest
7560 * of the initialization here.
7561 */
7562 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007563 if (err)
7564 goto out;
7565
Raviv Shvili6a771a62014-09-25 15:32:24 +03007566 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007567 if (err)
7568 goto out;
7569
Raviv Shvili6a771a62014-09-25 15:32:24 +03007570 err = ufshcd_init_clocks(hba);
7571 if (err)
7572 goto out_disable_hba_vreg;
7573
7574 err = ufshcd_setup_clocks(hba, true);
7575 if (err)
7576 goto out_disable_hba_vreg;
7577
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007578 err = ufshcd_init_vreg(hba);
7579 if (err)
7580 goto out_disable_clks;
7581
7582 err = ufshcd_setup_vreg(hba, true);
7583 if (err)
7584 goto out_disable_clks;
7585
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007586 err = ufshcd_variant_hba_init(hba);
7587 if (err)
7588 goto out_disable_vreg;
7589
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007590 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007591 goto out;
7592
7593out_disable_vreg:
7594 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03007595out_disable_clks:
7596 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03007597out_disable_hba_vreg:
7598 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007599out:
7600 return err;
7601}
7602
7603static void ufshcd_hba_exit(struct ufs_hba *hba)
7604{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007605 if (hba->is_powered) {
7606 ufshcd_variant_hba_exit(hba);
7607 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07007608 ufshcd_suspend_clkscaling(hba);
Vivek Gautameebcc192018-08-07 23:17:39 +05307609 if (ufshcd_is_clkscaling_supported(hba))
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08007610 if (hba->devfreq)
7611 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007612 ufshcd_setup_clocks(hba, false);
7613 ufshcd_setup_hba_vreg(hba, false);
7614 hba->is_powered = false;
Bean Huo09750062020-01-20 14:08:14 +01007615 ufs_put_device_desc(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03007616 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007617}
7618
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007619static int
7620ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307621{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007622 unsigned char cmd[6] = {REQUEST_SENSE,
7623 0,
7624 0,
7625 0,
Avri Altman09a5a242018-11-22 20:04:56 +02007626 UFS_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007627 0};
7628 char *buffer;
7629 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307630
Avri Altman09a5a242018-11-22 20:04:56 +02007631 buffer = kzalloc(UFS_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007632 if (!buffer) {
7633 ret = -ENOMEM;
7634 goto out;
7635 }
7636
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007637 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
Avri Altman09a5a242018-11-22 20:04:56 +02007638 UFS_SENSE_SIZE, NULL, NULL,
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007639 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007640 if (ret)
7641 pr_err("%s: failed with err %d\n", __func__, ret);
7642
7643 kfree(buffer);
7644out:
7645 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307646}
7647
7648/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007649 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
7650 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307651 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007652 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307653 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007654 * Returns 0 if requested power mode is set successfully
7655 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307656 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007657static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
7658 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307659{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007660 unsigned char cmd[6] = { START_STOP };
7661 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007662 struct scsi_device *sdp;
7663 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007664 int ret;
7665
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007666 spin_lock_irqsave(hba->host->host_lock, flags);
7667 sdp = hba->sdev_ufs_device;
7668 if (sdp) {
7669 ret = scsi_device_get(sdp);
7670 if (!ret && !scsi_device_online(sdp)) {
7671 ret = -ENODEV;
7672 scsi_device_put(sdp);
7673 }
7674 } else {
7675 ret = -ENODEV;
7676 }
7677 spin_unlock_irqrestore(hba->host->host_lock, flags);
7678
7679 if (ret)
7680 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007681
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307682 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007683 * If scsi commands fail, the scsi mid-layer schedules scsi error-
7684 * handling, which would wait for host to be resumed. Since we know
7685 * we are functional while we are here, skip host resume in error
7686 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307687 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007688 hba->host->eh_noresume = 1;
7689 if (hba->wlun_dev_clr_ua) {
7690 ret = ufshcd_send_request_sense(hba, sdp);
7691 if (ret)
7692 goto out;
7693 /* Unit attention condition is cleared now */
7694 hba->wlun_dev_clr_ua = false;
7695 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307696
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007697 cmd[4] = pwr_mode << 4;
7698
7699 /*
7700 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02007701 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007702 * already suspended childs.
7703 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007704 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
7705 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007706 if (ret) {
7707 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02007708 "START_STOP failed for power mode: %d, result %x\n",
7709 pwr_mode, ret);
Johannes Thumshirnc65be1a2018-06-25 13:20:58 +02007710 if (driver_byte(ret) == DRIVER_SENSE)
Hannes Reinecke21045512015-01-08 07:43:46 +01007711 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007712 }
7713
7714 if (!ret)
7715 hba->curr_dev_pwr_mode = pwr_mode;
7716out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007717 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007718 hba->host->eh_noresume = 0;
7719 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307720}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307721
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007722static int ufshcd_link_state_transition(struct ufs_hba *hba,
7723 enum uic_link_state req_link_state,
7724 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307725{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007726 int ret = 0;
7727
7728 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307729 return 0;
7730
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007731 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7732 ret = ufshcd_uic_hibern8_enter(hba);
7733 if (!ret)
7734 ufshcd_set_link_hibern8(hba);
7735 else
7736 goto out;
7737 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307738 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007739 * If autobkops is enabled, link can't be turned off because
7740 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307741 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007742 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
Dan Carpenterdc30c9e2019-12-13 13:49:35 +03007743 (!check_for_bkops || !hba->auto_bkops_enabled)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007744 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007745 * Let's make sure that link is in low power mode, we are doing
7746 * this currently by putting the link in Hibern8. Otherway to
7747 * put the link in low power mode is to send the DME end point
7748 * to device and then send the DME reset command to local
7749 * unipro. But putting the link in hibern8 is much faster.
7750 */
7751 ret = ufshcd_uic_hibern8_enter(hba);
7752 if (ret)
7753 goto out;
7754 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007755 * Change controller state to "reset state" which
7756 * should also put the link in off/reset state
7757 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007758 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007759 /*
7760 * TODO: Check if we need any delay to make sure that
7761 * controller is reset
7762 */
7763 ufshcd_set_link_off(hba);
7764 }
7765
7766out:
7767 return ret;
7768}
7769
7770static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7771{
7772 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007773 * It seems some UFS devices may keep drawing more than sleep current
7774 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7775 * To avoid this situation, add 2ms delay before putting these UFS
7776 * rails in LPM mode.
7777 */
7778 if (!ufshcd_is_link_active(hba) &&
7779 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
7780 usleep_range(2000, 2100);
7781
7782 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007783 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7784 * power.
7785 *
7786 * If UFS device and link is in OFF state, all power supplies (VCC,
7787 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7788 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7789 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7790 *
7791 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7792 * in low power state which would save some power.
7793 */
7794 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7795 !hba->dev_info.is_lu_power_on_wp) {
7796 ufshcd_setup_vreg(hba, false);
7797 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7798 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7799 if (!ufshcd_is_link_active(hba)) {
7800 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7801 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7802 }
7803 }
7804}
7805
7806static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7807{
7808 int ret = 0;
7809
7810 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7811 !hba->dev_info.is_lu_power_on_wp) {
7812 ret = ufshcd_setup_vreg(hba, true);
7813 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007814 if (!ret && !ufshcd_is_link_active(hba)) {
7815 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7816 if (ret)
7817 goto vcc_disable;
7818 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7819 if (ret)
7820 goto vccq_lpm;
7821 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07007822 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007823 }
7824 goto out;
7825
7826vccq_lpm:
7827 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7828vcc_disable:
7829 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7830out:
7831 return ret;
7832}
7833
7834static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7835{
7836 if (ufshcd_is_link_off(hba))
7837 ufshcd_setup_hba_vreg(hba, false);
7838}
7839
7840static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7841{
7842 if (ufshcd_is_link_off(hba))
7843 ufshcd_setup_hba_vreg(hba, true);
7844}
7845
7846/**
7847 * ufshcd_suspend - helper function for suspend operations
7848 * @hba: per adapter instance
7849 * @pm_op: desired low power operation type
7850 *
7851 * This function will try to put the UFS device and link into low power
7852 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7853 * (System PM level).
7854 *
7855 * If this function is called during shutdown, it will make sure that
7856 * both UFS device and UFS link is powered off.
7857 *
7858 * NOTE: UFS device & link must be active before we enter in this function.
7859 *
7860 * Returns 0 for success and non-zero for failure
7861 */
7862static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7863{
7864 int ret = 0;
7865 enum ufs_pm_level pm_lvl;
7866 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7867 enum uic_link_state req_link_state;
7868
7869 hba->pm_op_in_progress = 1;
7870 if (!ufshcd_is_shutdown_pm(pm_op)) {
7871 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7872 hba->rpm_lvl : hba->spm_lvl;
7873 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7874 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7875 } else {
7876 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7877 req_link_state = UIC_LINK_OFF_STATE;
7878 }
7879
7880 /*
7881 * If we can't transition into any of the low power modes
7882 * just gate the clocks.
7883 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007884 ufshcd_hold(hba, false);
7885 hba->clk_gating.is_suspended = true;
7886
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007887 if (hba->clk_scaling.is_allowed) {
7888 cancel_work_sync(&hba->clk_scaling.suspend_work);
7889 cancel_work_sync(&hba->clk_scaling.resume_work);
7890 ufshcd_suspend_clkscaling(hba);
7891 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007892
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007893 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
7894 req_link_state == UIC_LINK_ACTIVE_STATE) {
7895 goto disable_clks;
7896 }
7897
7898 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
7899 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007900 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007901
7902 /* UFS device & link must be active before we enter in this function */
7903 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
7904 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007905 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007906 }
7907
7908 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03007909 if (ufshcd_can_autobkops_during_suspend(hba)) {
7910 /*
7911 * The device is idle with no requests in the queue,
7912 * allow background operations if bkops status shows
7913 * that performance might be impacted.
7914 */
7915 ret = ufshcd_urgent_bkops(hba);
7916 if (ret)
7917 goto enable_gating;
7918 } else {
7919 /* make sure that auto bkops is disabled */
7920 ufshcd_disable_auto_bkops(hba);
7921 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007922 }
7923
7924 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
7925 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
7926 !ufshcd_is_runtime_pm(pm_op))) {
7927 /* ensure that bkops is disabled */
7928 ufshcd_disable_auto_bkops(hba);
7929 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
7930 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007931 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007932 }
7933
7934 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
7935 if (ret)
7936 goto set_dev_active;
7937
7938 ufshcd_vreg_set_lpm(hba);
7939
7940disable_clks:
7941 /*
7942 * Call vendor specific suspend callback. As these callbacks may access
7943 * vendor specific host controller register space call them before the
7944 * host clocks are ON.
7945 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007946 ret = ufshcd_vops_suspend(hba, pm_op);
7947 if (ret)
7948 goto set_link_active;
Stanley Chudcb6cec2019-12-07 20:22:00 +08007949 /*
7950 * Disable the host irq as host controller as there won't be any
7951 * host controller transaction expected till resume.
7952 */
7953 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007954
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007955 if (!ufshcd_is_link_active(hba))
7956 ufshcd_setup_clocks(hba, false);
7957 else
7958 /* If link is active, device ref_clk can't be switched off */
7959 __ufshcd_setup_clocks(hba, false, true);
7960
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007961 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007962 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Stanley Chudcb6cec2019-12-07 20:22:00 +08007963
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007964 /* Put the host controller in low power mode if possible */
7965 ufshcd_hba_vreg_set_lpm(hba);
7966 goto out;
7967
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007968set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007969 if (hba->clk_scaling.is_allowed)
7970 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007971 ufshcd_vreg_set_hpm(hba);
7972 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
7973 ufshcd_set_link_active(hba);
7974 else if (ufshcd_is_link_off(hba))
7975 ufshcd_host_reset_and_restore(hba);
7976set_dev_active:
7977 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
7978 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007979enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007980 if (hba->clk_scaling.is_allowed)
7981 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007982 hba->clk_gating.is_suspended = false;
7983 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007984out:
7985 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08007986 if (ret)
7987 ufshcd_update_reg_hist(&hba->ufs_stats.suspend_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007988 return ret;
7989}
7990
7991/**
7992 * ufshcd_resume - helper function for resume operations
7993 * @hba: per adapter instance
7994 * @pm_op: runtime PM or system PM
7995 *
7996 * This function basically brings the UFS device, UniPro link and controller
7997 * to active state.
7998 *
7999 * Returns 0 for success and non-zero for failure
8000 */
8001static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
8002{
8003 int ret;
8004 enum uic_link_state old_link_state;
8005
8006 hba->pm_op_in_progress = 1;
8007 old_link_state = hba->uic_link_state;
8008
8009 ufshcd_hba_vreg_set_hpm(hba);
8010 /* Make sure clocks are enabled before accessing controller */
8011 ret = ufshcd_setup_clocks(hba, true);
8012 if (ret)
8013 goto out;
8014
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008015 /* enable the host irq as host controller would be active soon */
Can Guo5231d382019-12-05 02:14:46 +00008016 ufshcd_enable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008017
8018 ret = ufshcd_vreg_set_hpm(hba);
8019 if (ret)
8020 goto disable_irq_and_vops_clks;
8021
8022 /*
8023 * Call vendor specific resume callback. As these callbacks may access
8024 * vendor specific host controller register space call them when the
8025 * host clocks are ON.
8026 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008027 ret = ufshcd_vops_resume(hba, pm_op);
8028 if (ret)
8029 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008030
8031 if (ufshcd_is_link_hibern8(hba)) {
8032 ret = ufshcd_uic_hibern8_exit(hba);
8033 if (!ret)
8034 ufshcd_set_link_active(hba);
8035 else
8036 goto vendor_suspend;
8037 } else if (ufshcd_is_link_off(hba)) {
8038 ret = ufshcd_host_reset_and_restore(hba);
8039 /*
8040 * ufshcd_host_reset_and_restore() should have already
8041 * set the link state as active
8042 */
8043 if (ret || !ufshcd_is_link_active(hba))
8044 goto vendor_suspend;
8045 }
8046
8047 if (!ufshcd_is_ufs_dev_active(hba)) {
8048 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
8049 if (ret)
8050 goto set_old_link_state;
8051 }
8052
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08008053 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
8054 ufshcd_enable_auto_bkops(hba);
8055 else
8056 /*
8057 * If BKOPs operations are urgently needed at this moment then
8058 * keep auto-bkops enabled or else disable it.
8059 */
8060 ufshcd_urgent_bkops(hba);
8061
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008062 hba->clk_gating.is_suspended = false;
8063
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008064 if (hba->clk_scaling.is_allowed)
8065 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03008066
Adrian Hunterad448372018-03-20 15:07:38 +02008067 /* Enable Auto-Hibernate if configured */
8068 ufshcd_auto_hibern8_enable(hba);
8069
Can Guo71d848b2019-11-14 22:09:26 -08008070 /* Schedule clock gating in case of no access to UFS device yet */
8071 ufshcd_release(hba);
8072
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008073 goto out;
8074
8075set_old_link_state:
8076 ufshcd_link_state_transition(hba, old_link_state, 0);
8077vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02008078 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008079disable_vreg:
8080 ufshcd_vreg_set_lpm(hba);
8081disable_irq_and_vops_clks:
8082 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08008083 if (hba->clk_scaling.is_allowed)
8084 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008085 ufshcd_setup_clocks(hba, false);
8086out:
8087 hba->pm_op_in_progress = 0;
Stanley Chu8808b4e2019-07-10 21:38:21 +08008088 if (ret)
8089 ufshcd_update_reg_hist(&hba->ufs_stats.resume_err, (u32)ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008090 return ret;
8091}
8092
8093/**
8094 * ufshcd_system_suspend - system suspend routine
8095 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008096 *
8097 * Check the description of ufshcd_suspend() function for more details.
8098 *
8099 * Returns 0 for success and non-zero for failure
8100 */
8101int ufshcd_system_suspend(struct ufs_hba *hba)
8102{
8103 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008104 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008105
8106 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03008107 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008108
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008109 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
8110 hba->curr_dev_pwr_mode) &&
8111 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
8112 hba->uic_link_state))
8113 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008114
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08008115 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008116 /*
8117 * UFS device and/or UFS link low power states during runtime
8118 * suspend seems to be different than what is expected during
8119 * system suspend. Hence runtime resume the devic & link and
8120 * let the system suspend low power states to take effect.
8121 * TODO: If resume takes longer time, we might have optimize
8122 * it in future by not resuming everything if possible.
8123 */
8124 ret = ufshcd_runtime_resume(hba);
8125 if (ret)
8126 goto out;
8127 }
8128
8129 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
8130out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008131 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
8132 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008133 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03008134 if (!ret)
8135 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008136 return ret;
8137}
8138EXPORT_SYMBOL(ufshcd_system_suspend);
8139
8140/**
8141 * ufshcd_system_resume - system resume routine
8142 * @hba: per adapter instance
8143 *
8144 * Returns 0 for success and non-zero for failure
8145 */
8146
8147int ufshcd_system_resume(struct ufs_hba *hba)
8148{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008149 int ret = 0;
8150 ktime_t start = ktime_get();
8151
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008152 if (!hba)
8153 return -EINVAL;
8154
8155 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008156 /*
8157 * Let the runtime resume take care of resuming
8158 * if runtime suspended.
8159 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008160 goto out;
8161 else
8162 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
8163out:
8164 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
8165 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008166 hba->curr_dev_pwr_mode, hba->uic_link_state);
Stanley Chuce9e7bc2019-01-07 22:19:34 +08008167 if (!ret)
8168 hba->is_sys_suspended = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008169 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008170}
8171EXPORT_SYMBOL(ufshcd_system_resume);
8172
8173/**
8174 * ufshcd_runtime_suspend - runtime suspend routine
8175 * @hba: per adapter instance
8176 *
8177 * Check the description of ufshcd_suspend() function for more details.
8178 *
8179 * Returns 0 for success and non-zero for failure
8180 */
8181int ufshcd_runtime_suspend(struct ufs_hba *hba)
8182{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008183 int ret = 0;
8184 ktime_t start = ktime_get();
8185
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008186 if (!hba)
8187 return -EINVAL;
8188
8189 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008190 goto out;
8191 else
8192 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
8193out:
8194 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
8195 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008196 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008197 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308198}
8199EXPORT_SYMBOL(ufshcd_runtime_suspend);
8200
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008201/**
8202 * ufshcd_runtime_resume - runtime resume routine
8203 * @hba: per adapter instance
8204 *
8205 * This function basically brings the UFS device, UniPro link and controller
8206 * to active state. Following operations are done in this function:
8207 *
8208 * 1. Turn on all the controller related clocks
8209 * 2. Bring the UniPro link out of Hibernate state
8210 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
8211 * to active state.
8212 * 4. If auto-bkops is enabled on the device, disable it.
8213 *
8214 * So following would be the possible power state after this function return
8215 * successfully:
8216 * S1: UFS device in Active state with VCC rail ON
8217 * UniPro link in Active state
8218 * All the UFS/UniPro controller clocks are ON
8219 *
8220 * Returns 0 for success and non-zero for failure
8221 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308222int ufshcd_runtime_resume(struct ufs_hba *hba)
8223{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008224 int ret = 0;
8225 ktime_t start = ktime_get();
8226
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07008227 if (!hba)
8228 return -EINVAL;
8229
8230 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008231 goto out;
8232 else
8233 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
8234out:
8235 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
8236 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08008237 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08008238 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308239}
8240EXPORT_SYMBOL(ufshcd_runtime_resume);
8241
8242int ufshcd_runtime_idle(struct ufs_hba *hba)
8243{
8244 return 0;
8245}
8246EXPORT_SYMBOL(ufshcd_runtime_idle);
8247
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308248/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008249 * ufshcd_shutdown - shutdown routine
8250 * @hba: per adapter instance
8251 *
8252 * This function would power off both UFS device and UFS link.
8253 *
8254 * Returns 0 always to allow force shutdown even in case of errors.
8255 */
8256int ufshcd_shutdown(struct ufs_hba *hba)
8257{
8258 int ret = 0;
8259
Stanley Chuf51913e2019-09-18 12:20:38 +08008260 if (!hba->is_powered)
8261 goto out;
8262
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008263 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
8264 goto out;
8265
8266 if (pm_runtime_suspended(hba->dev)) {
8267 ret = ufshcd_runtime_resume(hba);
8268 if (ret)
8269 goto out;
8270 }
8271
8272 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
8273out:
8274 if (ret)
8275 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
8276 /* allow force shutdown even in case of errors */
8277 return 0;
8278}
8279EXPORT_SYMBOL(ufshcd_shutdown);
8280
8281/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308282 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308283 * data structure memory
Bart Van Assche8aa29f12018-03-01 15:07:20 -08008284 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308285 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308286void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308287{
Avri Altmandf032bf2018-10-07 17:30:35 +03008288 ufs_bsg_remove(hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008289 ufs_sysfs_remove_nodes(hba->dev);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008290 blk_cleanup_queue(hba->tmf_queue);
8291 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008292 blk_cleanup_queue(hba->cmd_queue);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05308293 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308294 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308295 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02008296 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308297
Vivek Gautameebcc192018-08-07 23:17:39 +05308298 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008299 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08008300 if (ufshcd_is_clkscaling_supported(hba))
8301 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008302 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308303}
8304EXPORT_SYMBOL_GPL(ufshcd_remove);
8305
8306/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02008307 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
8308 * @hba: pointer to Host Bus Adapter (HBA)
8309 */
8310void ufshcd_dealloc_host(struct ufs_hba *hba)
8311{
8312 scsi_host_put(hba->host);
8313}
8314EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
8315
8316/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008317 * ufshcd_set_dma_mask - Set dma mask based on the controller
8318 * addressing capability
8319 * @hba: per adapter instance
8320 *
8321 * Returns 0 for success, non-zero for failure
8322 */
8323static int ufshcd_set_dma_mask(struct ufs_hba *hba)
8324{
8325 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
8326 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
8327 return 0;
8328 }
8329 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
8330}
8331
8332/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008333 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308334 * @dev: pointer to device handle
8335 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308336 * Returns 0 on success, non-zero value on failure
8337 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008338int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308339{
8340 struct Scsi_Host *host;
8341 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008342 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308343
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308344 if (!dev) {
8345 dev_err(dev,
8346 "Invalid memory reference for dev is NULL\n");
8347 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308348 goto out_error;
8349 }
8350
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308351 host = scsi_host_alloc(&ufshcd_driver_template,
8352 sizeof(struct ufs_hba));
8353 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308354 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308355 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308356 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308357 }
8358 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308359 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308360 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008361 *hba_handle = hba;
Subhash Jadavani9e1e8a72018-10-16 14:29:41 +05308362 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008363
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03008364 INIT_LIST_HEAD(&hba->clk_list_head);
8365
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008366out_error:
8367 return err;
8368}
8369EXPORT_SYMBOL(ufshcd_alloc_host);
8370
Bart Van Assche69a6c262019-12-09 10:13:09 -08008371/* This function exists because blk_mq_alloc_tag_set() requires this. */
8372static blk_status_t ufshcd_queue_tmf(struct blk_mq_hw_ctx *hctx,
8373 const struct blk_mq_queue_data *qd)
8374{
8375 WARN_ON_ONCE(true);
8376 return BLK_STS_NOTSUPP;
8377}
8378
8379static const struct blk_mq_ops ufshcd_tmf_ops = {
8380 .queue_rq = ufshcd_queue_tmf,
8381};
8382
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008383/**
8384 * ufshcd_init - Driver initialization routine
8385 * @hba: per-adapter instance
8386 * @mmio_base: base register address
8387 * @irq: Interrupt line of device
8388 * Returns 0 on success, non-zero value on failure
8389 */
8390int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
8391{
8392 int err;
8393 struct Scsi_Host *host = hba->host;
8394 struct device *dev = hba->dev;
8395
8396 if (!mmio_base) {
8397 dev_err(hba->dev,
8398 "Invalid memory reference for mmio_base is NULL\n");
8399 err = -ENODEV;
8400 goto out_error;
8401 }
8402
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308403 hba->mmio_base = mmio_base;
8404 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308405
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008406 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03008407 if (err)
8408 goto out_error;
8409
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308410 /* Read capabilities registers */
8411 ufshcd_hba_capabilities(hba);
8412
8413 /* Get UFS version supported by the controller */
8414 hba->ufs_version = ufshcd_get_ufs_version(hba);
8415
Yaniv Gardic01848c2016-12-05 19:25:02 -08008416 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
8417 (hba->ufs_version != UFSHCI_VERSION_11) &&
8418 (hba->ufs_version != UFSHCI_VERSION_20) &&
8419 (hba->ufs_version != UFSHCI_VERSION_21))
8420 dev_err(hba->dev, "invalid UFS version 0x%x\n",
8421 hba->ufs_version);
8422
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05308423 /* Get Interrupt bit mask per version */
8424 hba->intr_mask = ufshcd_get_intr_mask(hba);
8425
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09008426 err = ufshcd_set_dma_mask(hba);
8427 if (err) {
8428 dev_err(hba->dev, "set dma mask failed\n");
8429 goto out_disable;
8430 }
8431
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308432 /* Allocate memory for host memory space */
8433 err = ufshcd_memory_alloc(hba);
8434 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308435 dev_err(hba->dev, "Memory allocation failed\n");
8436 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308437 }
8438
8439 /* Configure LRB */
8440 ufshcd_host_memory_configure(hba);
8441
8442 host->can_queue = hba->nutrs;
8443 host->cmd_per_lun = hba->nutrs;
8444 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03008445 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308446 host->max_channel = UFSHCD_MAX_CHANNEL;
8447 host->unique_id = host->host_no;
Avri Altmana851b2b2018-10-07 17:30:34 +03008448 host->max_cmd_len = UFS_CDB_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308449
Dolev Raviv7eb584d2014-09-25 15:32:31 +03008450 hba->max_pwr_info.is_valid = false;
8451
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308452 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05308453 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05308454 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308455
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308456 /* Initialize UIC command mutex */
8457 mutex_init(&hba->uic_cmd_mutex);
8458
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05308459 /* Initialize mutex for device management commands */
8460 mutex_init(&hba->dev_cmd.lock);
8461
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08008462 init_rwsem(&hba->clk_scaling_lock);
8463
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008464 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02008465
Vivek Gautameebcc192018-08-07 23:17:39 +05308466 ufshcd_init_clk_scaling(hba);
8467
Yaniv Gardi199ef132016-03-10 17:37:06 +02008468 /*
8469 * In order to avoid any spurious interrupt immediately after
8470 * registering UFS controller interrupt handler, clear any pending UFS
8471 * interrupt status and disable all the UFS interrupts.
8472 */
8473 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
8474 REG_INTERRUPT_STATUS);
8475 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
8476 /*
8477 * Make sure that UFS interrupts are disabled and any pending interrupt
8478 * status is cleared before registering UFS interrupt handler.
8479 */
8480 mb();
8481
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308482 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09008483 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308484 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308485 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008486 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008487 } else {
8488 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308489 }
8490
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308491 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308492 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308493 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008494 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308495 }
8496
Bart Van Assche7252a362019-12-09 10:13:08 -08008497 hba->cmd_queue = blk_mq_init_queue(&hba->host->tag_set);
8498 if (IS_ERR(hba->cmd_queue)) {
8499 err = PTR_ERR(hba->cmd_queue);
8500 goto out_remove_scsi_host;
8501 }
8502
Bart Van Assche69a6c262019-12-09 10:13:09 -08008503 hba->tmf_tag_set = (struct blk_mq_tag_set) {
8504 .nr_hw_queues = 1,
8505 .queue_depth = hba->nutmrs,
8506 .ops = &ufshcd_tmf_ops,
8507 .flags = BLK_MQ_F_NO_SCHED,
8508 };
8509 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set);
8510 if (err < 0)
8511 goto free_cmd_queue;
8512 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set);
8513 if (IS_ERR(hba->tmf_queue)) {
8514 err = PTR_ERR(hba->tmf_queue);
8515 goto free_tmf_tag_set;
8516 }
8517
Bjorn Anderssond8d9f792019-08-28 12:17:54 -07008518 /* Reset the attached device */
8519 ufshcd_vops_device_reset(hba);
8520
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308521 /* Host controller enable */
8522 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308523 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308524 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08008525 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08008526 ufshcd_print_host_state(hba);
Bart Van Assche69a6c262019-12-09 10:13:09 -08008527 goto free_tmf_queue;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308528 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308529
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08008530 /*
8531 * Set the default power management level for runtime and system PM.
8532 * Default power saving mode is to keep UFS link in Hibern8 state
8533 * and UFS device in sleep state.
8534 */
8535 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8536 UFS_SLEEP_PWR_MODE,
8537 UIC_LINK_HIBERN8_STATE);
8538 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
8539 UFS_SLEEP_PWR_MODE,
8540 UIC_LINK_HIBERN8_STATE);
8541
Adrian Hunterad448372018-03-20 15:07:38 +02008542 /* Set the default auto-hiberate idle timer value to 150 ms */
Stanley Chuf571b372019-05-21 14:44:53 +08008543 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) {
Adrian Hunterad448372018-03-20 15:07:38 +02008544 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) |
8545 FIELD_PREP(UFSHCI_AHIBERN8_SCALE_MASK, 3);
8546 }
8547
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05308548 /* Hold auto suspend until async scan completes */
8549 pm_runtime_get_sync(dev);
Subhash Jadavani38135532018-05-03 16:37:18 +05308550 atomic_set(&hba->scsi_block_reqs_cnt, 0);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008551 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008552 * We are assuming that device wasn't put in sleep/power-down
8553 * state exclusively during the boot stage before kernel.
8554 * This assumption helps avoid doing link startup twice during
8555 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008556 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08008557 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008558
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308559 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02008560 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05308561
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308562 return 0;
8563
Bart Van Assche69a6c262019-12-09 10:13:09 -08008564free_tmf_queue:
8565 blk_cleanup_queue(hba->tmf_queue);
8566free_tmf_tag_set:
8567 blk_mq_free_tag_set(&hba->tmf_tag_set);
Bart Van Assche7252a362019-12-09 10:13:08 -08008568free_cmd_queue:
8569 blk_cleanup_queue(hba->cmd_queue);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308570out_remove_scsi_host:
8571 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008572exit_gating:
Vivek Gautameebcc192018-08-07 23:17:39 +05308573 ufshcd_exit_clk_scaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03008574 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308575out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03008576 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03008577 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308578out_error:
8579 return err;
8580}
8581EXPORT_SYMBOL_GPL(ufshcd_init);
8582
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308583MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
8584MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05308585MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05308586MODULE_LICENSE("GPL");
8587MODULE_VERSION(UFSHCD_DRIVER_VERSION);