blob: 9cfdd8ed198afc65e08bd17b355da26beb7a6feb [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>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053044#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020045#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053046#include "unipro.h"
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +020047#include "ufs-sysfs.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053048
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -080049#define CREATE_TRACE_POINTS
50#include <trace/events/ufs.h>
51
Gilad Bronerdcea0bf2016-10-17 17:09:48 -070052#define UFSHCD_REQ_SENSE_SIZE 18
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
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030091#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
92 ({ \
93 int _ret; \
94 if (_on) \
95 _ret = ufshcd_enable_vreg(_dev, _vreg); \
96 else \
97 _ret = ufshcd_disable_vreg(_dev, _vreg); \
98 _ret; \
99 })
100
Dolev Raviv66cc8202016-12-22 18:39:42 -0800101#define ufshcd_hex_dump(prefix_str, buf, len) \
102print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false)
103
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530104enum {
105 UFSHCD_MAX_CHANNEL = 0,
106 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530107 UFSHCD_CMD_PER_LUN = 32,
108 UFSHCD_CAN_QUEUE = 32,
109};
110
111/* UFSHCD states */
112enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530113 UFSHCD_STATE_RESET,
114 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530115 UFSHCD_STATE_OPERATIONAL,
Zang Leigang141f8162016-11-16 11:29:37 +0800116 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530117};
118
119/* UFSHCD error handling flags */
120enum {
121 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530122};
123
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530124/* UFSHCD UIC layer error flags */
125enum {
126 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200127 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
128 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
129 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
130 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
131 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530132};
133
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530134#define ufshcd_set_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300135 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530136#define ufshcd_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300137 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530138#define ufshcd_clear_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300139 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530140
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300141#define ufshcd_set_ufs_dev_active(h) \
142 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
143#define ufshcd_set_ufs_dev_sleep(h) \
144 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
145#define ufshcd_set_ufs_dev_poweroff(h) \
146 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
147#define ufshcd_is_ufs_dev_active(h) \
148 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
149#define ufshcd_is_ufs_dev_sleep(h) \
150 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
151#define ufshcd_is_ufs_dev_poweroff(h) \
152 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
153
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +0200154struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300155 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
156 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
157 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
158 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
159 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
160 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
161};
162
163static inline enum ufs_dev_pwr_mode
164ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
165{
166 return ufs_pm_lvl_states[lvl].dev_state;
167}
168
169static inline enum uic_link_state
170ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
171{
172 return ufs_pm_lvl_states[lvl].link_state;
173}
174
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -0800175static inline enum ufs_pm_level
176ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
177 enum uic_link_state link_state)
178{
179 enum ufs_pm_level lvl;
180
181 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
182 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
183 (ufs_pm_lvl_states[lvl].link_state == link_state))
184 return lvl;
185 }
186
187 /* if no match found, return the level 0 */
188 return UFS_PM_LVL_0;
189}
190
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800191static struct ufs_dev_fix ufs_fixups[] = {
192 /* UFS cards deviations table */
193 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
194 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
195 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
196 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
197 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
198 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
199 UFS_DEVICE_NO_FASTAUTO),
200 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
201 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
202 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
203 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
204 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
205 UFS_DEVICE_QUIRK_PA_TACTIVATE),
206 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
207 UFS_DEVICE_QUIRK_PA_TACTIVATE),
208 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
209 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
210 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
211
212 END_FIX
213};
214
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530215static void ufshcd_tmc_handler(struct ufs_hba *hba);
216static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530217static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800218static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530219static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300220static void ufshcd_hba_exit(struct ufs_hba *hba);
221static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300222static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
223 bool skip_ref_clk);
224static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200225static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300226static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
227static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300228static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300229static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800230static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
231static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800232static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800233static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300234static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300235static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
236 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300237static int ufshcd_change_power_mode(struct ufs_hba *hba,
238 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200239static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
240{
241 return tag >= 0 && tag < hba->nutrs;
242}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300243
244static inline int ufshcd_enable_irq(struct ufs_hba *hba)
245{
246 int ret = 0;
247
248 if (!hba->is_irq_enabled) {
249 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
250 hba);
251 if (ret)
252 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
253 __func__, ret);
254 hba->is_irq_enabled = true;
255 }
256
257 return ret;
258}
259
260static inline void ufshcd_disable_irq(struct ufs_hba *hba)
261{
262 if (hba->is_irq_enabled) {
263 free_irq(hba->irq, hba);
264 hba->is_irq_enabled = false;
265 }
266}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530267
Yaniv Gardib573d482016-03-10 17:37:09 +0200268/* replace non-printable or non-ASCII characters with spaces */
269static inline void ufshcd_remove_non_printable(char *val)
270{
271 if (!val)
272 return;
273
274 if (*val < 0x20 || *val > 0x7e)
275 *val = ' ';
276}
277
Lee Susman1a07f2d2016-12-22 18:42:03 -0800278static void ufshcd_add_command_trace(struct ufs_hba *hba,
279 unsigned int tag, const char *str)
280{
281 sector_t lba = -1;
282 u8 opcode = 0;
283 u32 intr, doorbell;
284 struct ufshcd_lrb *lrbp;
285 int transfer_len = -1;
286
287 if (!trace_ufshcd_command_enabled())
288 return;
289
290 lrbp = &hba->lrb[tag];
291
292 if (lrbp->cmd) { /* data phase exists */
293 opcode = (u8)(*lrbp->cmd->cmnd);
294 if ((opcode == READ_10) || (opcode == WRITE_10)) {
295 /*
296 * Currently we only fully trace read(10) and write(10)
297 * commands
298 */
299 if (lrbp->cmd->request && lrbp->cmd->request->bio)
300 lba =
301 lrbp->cmd->request->bio->bi_iter.bi_sector;
302 transfer_len = be32_to_cpu(
303 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
304 }
305 }
306
307 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
308 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
309 trace_ufshcd_command(dev_name(hba->dev), str, tag,
310 doorbell, transfer_len, intr, lba, opcode);
311}
312
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800313static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
314{
315 struct ufs_clk_info *clki;
316 struct list_head *head = &hba->clk_list_head;
317
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300318 if (list_empty(head))
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800319 return;
320
321 list_for_each_entry(clki, head, list) {
322 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
323 clki->max_freq)
324 dev_err(hba->dev, "clk: %s, rate: %u\n",
325 clki->name, clki->curr_freq);
326 }
327}
328
329static void ufshcd_print_uic_err_hist(struct ufs_hba *hba,
330 struct ufs_uic_err_reg_hist *err_hist, char *err_name)
331{
332 int i;
333
334 for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) {
335 int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH;
336
337 if (err_hist->reg[p] == 0)
338 continue;
339 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, i,
340 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
341 }
342}
343
Dolev Raviv66cc8202016-12-22 18:39:42 -0800344static void ufshcd_print_host_regs(struct ufs_hba *hba)
345{
346 /*
347 * hex_dump reads its data without the readl macro. This might
348 * cause inconsistency issues on some platform, as the printed
349 * values may be from cache and not the most recent value.
350 * To know whether you are looking at an un-cached version verify
351 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
352 * during platform/pci probe function.
353 */
354 ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE);
355 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
356 hba->ufs_version, hba->capabilities);
357 dev_err(hba->dev,
358 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
359 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800360 dev_err(hba->dev,
361 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n",
362 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
363 hba->ufs_stats.hibern8_exit_cnt);
364
365 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
366 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
367 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
368 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
369 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
370
371 ufshcd_print_clk_freqs(hba);
372
373 if (hba->vops && hba->vops->dbg_register_dump)
374 hba->vops->dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800375}
376
377static
378void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
379{
380 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800381 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800382 int tag;
383
384 for_each_set_bit(tag, &bitmap, hba->nutrs) {
385 lrbp = &hba->lrb[tag];
386
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800387 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
388 tag, ktime_to_us(lrbp->issue_time_stamp));
Zang Leigang09017182017-09-27 10:06:06 +0800389 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n",
390 tag, ktime_to_us(lrbp->compl_time_stamp));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800391 dev_err(hba->dev,
392 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
393 tag, (u64)lrbp->utrd_dma_addr);
394
Dolev Raviv66cc8202016-12-22 18:39:42 -0800395 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
396 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800397 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
398 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800399 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
400 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800401 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
402 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800403 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
404 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800405
Gilad Broner7fabb772017-02-03 16:56:50 -0800406 prdt_length = le16_to_cpu(
407 lrbp->utr_descriptor_ptr->prd_table_length);
408 dev_err(hba->dev,
409 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
410 tag, prdt_length,
411 (u64)lrbp->ucd_prdt_dma_addr);
412
413 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800414 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800415 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800416 }
417}
418
419static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
420{
421 struct utp_task_req_desc *tmrdp;
422 int tag;
423
424 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
425 tmrdp = &hba->utmrdl_base_addr[tag];
426 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
427 ufshcd_hex_dump("TM TRD: ", &tmrdp->header,
428 sizeof(struct request_desc_header));
429 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU\n",
430 tag);
431 ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu,
432 sizeof(struct utp_upiu_req));
433 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU\n",
434 tag);
435 ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu,
436 sizeof(struct utp_task_req_desc));
437 }
438}
439
Gilad Broner6ba65582017-02-03 16:57:28 -0800440static void ufshcd_print_host_state(struct ufs_hba *hba)
441{
442 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
443 dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n",
Zang Leigange002e652017-08-24 10:57:15 +0800444 hba->lrb_in_use, hba->outstanding_reqs, hba->outstanding_tasks);
Gilad Broner6ba65582017-02-03 16:57:28 -0800445 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
446 hba->saved_err, hba->saved_uic_err);
447 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
448 hba->curr_dev_pwr_mode, hba->uic_link_state);
449 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
450 hba->pm_op_in_progress, hba->is_sys_suspended);
451 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
452 hba->auto_bkops_enabled, hba->host->host_self_blocked);
453 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
454 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
455 hba->eh_flags, hba->req_abort_count);
456 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
457 hba->capabilities, hba->caps);
458 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
459 hba->dev_quirks);
460}
461
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800462/**
463 * ufshcd_print_pwr_info - print power params as saved in hba
464 * power info
465 * @hba: per-adapter instance
466 */
467static void ufshcd_print_pwr_info(struct ufs_hba *hba)
468{
469 static const char * const names[] = {
470 "INVALID MODE",
471 "FAST MODE",
472 "SLOW_MODE",
473 "INVALID MODE",
474 "FASTAUTO_MODE",
475 "SLOWAUTO_MODE",
476 "INVALID MODE",
477 };
478
479 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
480 __func__,
481 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
482 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
483 names[hba->pwr_info.pwr_rx],
484 names[hba->pwr_info.pwr_tx],
485 hba->pwr_info.hs_rate);
486}
487
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530488/*
489 * ufshcd_wait_for_register - wait for register value to change
490 * @hba - per-adapter interface
491 * @reg - mmio register offset
492 * @mask - mask to apply to read register value
493 * @val - wait condition
494 * @interval_us - polling interval in microsecs
495 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200496 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530497 *
498 * Returns -ETIMEDOUT on error, zero on success
499 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200500int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
501 u32 val, unsigned long interval_us,
502 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530503{
504 int err = 0;
505 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
506
507 /* ignore bits that we don't intend to wait on */
508 val = val & mask;
509
510 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200511 if (can_sleep)
512 usleep_range(interval_us, interval_us + 50);
513 else
514 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530515 if (time_after(jiffies, timeout)) {
516 if ((ufshcd_readl(hba, reg) & mask) != val)
517 err = -ETIMEDOUT;
518 break;
519 }
520 }
521
522 return err;
523}
524
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530525/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530526 * ufshcd_get_intr_mask - Get the interrupt bit mask
527 * @hba - Pointer to adapter instance
528 *
529 * Returns interrupt bit mask per version
530 */
531static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
532{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800533 u32 intr_mask = 0;
534
535 switch (hba->ufs_version) {
536 case UFSHCI_VERSION_10:
537 intr_mask = INTERRUPT_MASK_ALL_VER_10;
538 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800539 case UFSHCI_VERSION_11:
540 case UFSHCI_VERSION_20:
541 intr_mask = INTERRUPT_MASK_ALL_VER_11;
542 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800543 case UFSHCI_VERSION_21:
544 default:
545 intr_mask = INTERRUPT_MASK_ALL_VER_21;
Tomohiro Kusumi031d1e02017-03-23 12:49:04 +0200546 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800547 }
548
549 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530550}
551
552/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530553 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
554 * @hba - Pointer to adapter instance
555 *
556 * Returns UFSHCI version supported by the controller
557 */
558static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
559{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200560 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
561 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300562
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530563 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530564}
565
566/**
567 * ufshcd_is_device_present - Check if any device connected to
568 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300569 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530570 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300571 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530572 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300573static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530574{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300575 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300576 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530577}
578
579/**
580 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
581 * @lrb: pointer to local command reference block
582 *
583 * This function is used to get the OCS field from UTRD
584 * Returns the OCS field in the UTRD
585 */
586static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
587{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530588 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530589}
590
591/**
592 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
593 * @task_req_descp: pointer to utp_task_req_desc structure
594 *
595 * This function is used to get the OCS field from UTMRD
596 * Returns the OCS field in the UTMRD
597 */
598static inline int
599ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
600{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530601 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530602}
603
604/**
605 * ufshcd_get_tm_free_slot - get a free slot for task management request
606 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530607 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530608 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530609 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
610 * Returns 0 if free slot is not available, else return 1 with tag value
611 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530612 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530613static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530614{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530615 int tag;
616 bool ret = false;
617
618 if (!free_slot)
619 goto out;
620
621 do {
622 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
623 if (tag >= hba->nutmrs)
624 goto out;
625 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
626
627 *free_slot = tag;
628 ret = true;
629out:
630 return ret;
631}
632
633static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
634{
635 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530636}
637
638/**
639 * 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{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530645 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530646}
647
648/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200649 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
650 * @hba: per adapter instance
651 * @tag: position of the bit to be cleared
652 */
653static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
654{
655 __clear_bit(tag, &hba->outstanding_reqs);
656}
657
658/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530659 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
660 * @reg: Register value of host controller status
661 *
662 * Returns integer, 0 on Success and positive value if failed
663 */
664static inline int ufshcd_get_lists_status(u32 reg)
665{
Tomohiro Kusumi6cf16112017-04-26 20:28:58 +0300666 return !((reg & UFSHCD_STATUS_READY) == UFSHCD_STATUS_READY);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530667}
668
669/**
670 * ufshcd_get_uic_cmd_result - Get the UIC command result
671 * @hba: Pointer to adapter instance
672 *
673 * This function gets the result of UIC command completion
674 * Returns 0 on success, non zero value on error
675 */
676static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
677{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530678 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530679 MASK_UIC_COMMAND_RESULT;
680}
681
682/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530683 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
684 * @hba: Pointer to adapter instance
685 *
686 * This function gets UIC command argument3
687 * Returns 0 on success, non zero value on error
688 */
689static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
690{
691 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
692}
693
694/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530695 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530696 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530697 */
698static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530699ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530700{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530701 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530702}
703
704/**
705 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
706 * @ucd_rsp_ptr: pointer to response UPIU
707 *
708 * This function gets the response status and scsi_status from response UPIU
709 * Returns the response result code.
710 */
711static inline int
712ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
713{
714 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
715}
716
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530717/*
718 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
719 * from response UPIU
720 * @ucd_rsp_ptr: pointer to response UPIU
721 *
722 * Return the data segment length.
723 */
724static inline unsigned int
725ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
726{
727 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
728 MASK_RSP_UPIU_DATA_SEG_LEN;
729}
730
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530731/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530732 * ufshcd_is_exception_event - Check if the device raised an exception event
733 * @ucd_rsp_ptr: pointer to response UPIU
734 *
735 * The function checks if the device raised an exception event indicated in
736 * the Device Information field of response UPIU.
737 *
738 * Returns true if exception is raised, false otherwise.
739 */
740static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
741{
742 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
743 MASK_RSP_EXCEPTION_EVENT ? true : false;
744}
745
746/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530747 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530748 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530749 */
750static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530751ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530752{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530753 ufshcd_writel(hba, INT_AGGR_ENABLE |
754 INT_AGGR_COUNTER_AND_TIMER_RESET,
755 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
756}
757
758/**
759 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
760 * @hba: per adapter instance
761 * @cnt: Interrupt aggregation counter threshold
762 * @tmout: Interrupt aggregation timeout value
763 */
764static inline void
765ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
766{
767 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
768 INT_AGGR_COUNTER_THLD_VAL(cnt) |
769 INT_AGGR_TIMEOUT_VAL(tmout),
770 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530771}
772
773/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300774 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
775 * @hba: per adapter instance
776 */
777static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
778{
779 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
780}
781
782/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530783 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
784 * When run-stop registers are set to 1, it indicates the
785 * host controller that it can process the requests
786 * @hba: per adapter instance
787 */
788static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
789{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530790 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
791 REG_UTP_TASK_REQ_LIST_RUN_STOP);
792 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
793 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530794}
795
796/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530797 * ufshcd_hba_start - Start controller initialization sequence
798 * @hba: per adapter instance
799 */
800static inline void ufshcd_hba_start(struct ufs_hba *hba)
801{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530802 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530803}
804
805/**
806 * ufshcd_is_hba_active - Get controller state
807 * @hba: per adapter instance
808 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300809 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530810 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300811static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530812{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300813 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
814 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530815}
816
Yaniv Gardi37113102016-03-10 17:37:16 +0200817u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
818{
819 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
820 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
821 (hba->ufs_version == UFSHCI_VERSION_11))
822 return UFS_UNIPRO_VER_1_41;
823 else
824 return UFS_UNIPRO_VER_1_6;
825}
826EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
827
828static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
829{
830 /*
831 * If both host and device support UniPro ver1.6 or later, PA layer
832 * parameters tuning happens during link startup itself.
833 *
834 * We can manually tune PA layer parameters if either host or device
835 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
836 * logic simple, we will only do manual tuning if local unipro version
837 * doesn't support ver1.6 or later.
838 */
839 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
840 return true;
841 else
842 return false;
843}
844
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800845static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
846{
847 int ret = 0;
848 struct ufs_clk_info *clki;
849 struct list_head *head = &hba->clk_list_head;
850 ktime_t start = ktime_get();
851 bool clk_state_changed = false;
852
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300853 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800854 goto out;
855
856 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
857 if (ret)
858 return ret;
859
860 list_for_each_entry(clki, head, list) {
861 if (!IS_ERR_OR_NULL(clki->clk)) {
862 if (scale_up && clki->max_freq) {
863 if (clki->curr_freq == clki->max_freq)
864 continue;
865
866 clk_state_changed = true;
867 ret = clk_set_rate(clki->clk, clki->max_freq);
868 if (ret) {
869 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
870 __func__, clki->name,
871 clki->max_freq, ret);
872 break;
873 }
874 trace_ufshcd_clk_scaling(dev_name(hba->dev),
875 "scaled up", clki->name,
876 clki->curr_freq,
877 clki->max_freq);
878
879 clki->curr_freq = clki->max_freq;
880
881 } else if (!scale_up && clki->min_freq) {
882 if (clki->curr_freq == clki->min_freq)
883 continue;
884
885 clk_state_changed = true;
886 ret = clk_set_rate(clki->clk, clki->min_freq);
887 if (ret) {
888 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
889 __func__, clki->name,
890 clki->min_freq, ret);
891 break;
892 }
893 trace_ufshcd_clk_scaling(dev_name(hba->dev),
894 "scaled down", clki->name,
895 clki->curr_freq,
896 clki->min_freq);
897 clki->curr_freq = clki->min_freq;
898 }
899 }
900 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
901 clki->name, clk_get_rate(clki->clk));
902 }
903
904 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
905
906out:
907 if (clk_state_changed)
908 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
909 (scale_up ? "up" : "down"),
910 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
911 return ret;
912}
913
914/**
915 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
916 * @hba: per adapter instance
917 * @scale_up: True if scaling up and false if scaling down
918 *
919 * Returns true if scaling is required, false otherwise.
920 */
921static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
922 bool scale_up)
923{
924 struct ufs_clk_info *clki;
925 struct list_head *head = &hba->clk_list_head;
926
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +0300927 if (list_empty(head))
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800928 return false;
929
930 list_for_each_entry(clki, head, list) {
931 if (!IS_ERR_OR_NULL(clki->clk)) {
932 if (scale_up && clki->max_freq) {
933 if (clki->curr_freq == clki->max_freq)
934 continue;
935 return true;
936 } else if (!scale_up && clki->min_freq) {
937 if (clki->curr_freq == clki->min_freq)
938 continue;
939 return true;
940 }
941 }
942 }
943
944 return false;
945}
946
947static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
948 u64 wait_timeout_us)
949{
950 unsigned long flags;
951 int ret = 0;
952 u32 tm_doorbell;
953 u32 tr_doorbell;
954 bool timeout = false, do_last_check = false;
955 ktime_t start;
956
957 ufshcd_hold(hba, false);
958 spin_lock_irqsave(hba->host->host_lock, flags);
959 /*
960 * Wait for all the outstanding tasks/transfer requests.
961 * Verify by checking the doorbell registers are clear.
962 */
963 start = ktime_get();
964 do {
965 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
966 ret = -EBUSY;
967 goto out;
968 }
969
970 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
971 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
972 if (!tm_doorbell && !tr_doorbell) {
973 timeout = false;
974 break;
975 } else if (do_last_check) {
976 break;
977 }
978
979 spin_unlock_irqrestore(hba->host->host_lock, flags);
980 schedule();
981 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
982 wait_timeout_us) {
983 timeout = true;
984 /*
985 * We might have scheduled out for long time so make
986 * sure to check if doorbells are cleared by this time
987 * or not.
988 */
989 do_last_check = true;
990 }
991 spin_lock_irqsave(hba->host->host_lock, flags);
992 } while (tm_doorbell || tr_doorbell);
993
994 if (timeout) {
995 dev_err(hba->dev,
996 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
997 __func__, tm_doorbell, tr_doorbell);
998 ret = -EBUSY;
999 }
1000out:
1001 spin_unlock_irqrestore(hba->host->host_lock, flags);
1002 ufshcd_release(hba);
1003 return ret;
1004}
1005
1006/**
1007 * ufshcd_scale_gear - scale up/down UFS gear
1008 * @hba: per adapter instance
1009 * @scale_up: True for scaling up gear and false for scaling down
1010 *
1011 * Returns 0 for success,
1012 * Returns -EBUSY if scaling can't happen at this time
1013 * Returns non-zero for any other errors
1014 */
1015static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1016{
1017 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1018 int ret = 0;
1019 struct ufs_pa_layer_attr new_pwr_info;
1020
1021 if (scale_up) {
1022 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1023 sizeof(struct ufs_pa_layer_attr));
1024 } else {
1025 memcpy(&new_pwr_info, &hba->pwr_info,
1026 sizeof(struct ufs_pa_layer_attr));
1027
1028 if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN
1029 || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) {
1030 /* save the current power mode */
1031 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1032 &hba->pwr_info,
1033 sizeof(struct ufs_pa_layer_attr));
1034
1035 /* scale down gear */
1036 new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1037 new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1038 }
1039 }
1040
1041 /* check if the power mode needs to be changed or not? */
1042 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
1043
1044 if (ret)
1045 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1046 __func__, ret,
1047 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1048 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1049
1050 return ret;
1051}
1052
1053static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1054{
1055 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1056 int ret = 0;
1057 /*
1058 * make sure that there are no outstanding requests when
1059 * clock scaling is in progress
1060 */
1061 scsi_block_requests(hba->host);
1062 down_write(&hba->clk_scaling_lock);
1063 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1064 ret = -EBUSY;
1065 up_write(&hba->clk_scaling_lock);
1066 scsi_unblock_requests(hba->host);
1067 }
1068
1069 return ret;
1070}
1071
1072static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1073{
1074 up_write(&hba->clk_scaling_lock);
1075 scsi_unblock_requests(hba->host);
1076}
1077
1078/**
1079 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1080 * @hba: per adapter instance
1081 * @scale_up: True for scaling up and false for scalin down
1082 *
1083 * Returns 0 for success,
1084 * Returns -EBUSY if scaling can't happen at this time
1085 * Returns non-zero for any other errors
1086 */
1087static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1088{
1089 int ret = 0;
1090
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001091 /* let's not get into low power until clock scaling is completed */
1092 ufshcd_hold(hba, false);
1093
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001094 ret = ufshcd_clock_scaling_prepare(hba);
1095 if (ret)
1096 return ret;
1097
1098 /* scale down the gear before scaling down clocks */
1099 if (!scale_up) {
1100 ret = ufshcd_scale_gear(hba, false);
1101 if (ret)
1102 goto out;
1103 }
1104
1105 ret = ufshcd_scale_clks(hba, scale_up);
1106 if (ret) {
1107 if (!scale_up)
1108 ufshcd_scale_gear(hba, true);
1109 goto out;
1110 }
1111
1112 /* scale up the gear after scaling up clocks */
1113 if (scale_up) {
1114 ret = ufshcd_scale_gear(hba, true);
1115 if (ret) {
1116 ufshcd_scale_clks(hba, false);
1117 goto out;
1118 }
1119 }
1120
1121 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1122
1123out:
1124 ufshcd_clock_scaling_unprepare(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001125 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001126 return ret;
1127}
1128
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001129static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1130{
1131 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1132 clk_scaling.suspend_work);
1133 unsigned long irq_flags;
1134
1135 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1136 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1137 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1138 return;
1139 }
1140 hba->clk_scaling.is_suspended = true;
1141 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1142
1143 __ufshcd_suspend_clkscaling(hba);
1144}
1145
1146static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1147{
1148 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1149 clk_scaling.resume_work);
1150 unsigned long irq_flags;
1151
1152 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1153 if (!hba->clk_scaling.is_suspended) {
1154 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1155 return;
1156 }
1157 hba->clk_scaling.is_suspended = false;
1158 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1159
1160 devfreq_resume_device(hba->devfreq);
1161}
1162
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001163static int ufshcd_devfreq_target(struct device *dev,
1164 unsigned long *freq, u32 flags)
1165{
1166 int ret = 0;
1167 struct ufs_hba *hba = dev_get_drvdata(dev);
1168 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001169 bool scale_up, sched_clk_scaling_suspend_work = false;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001170 unsigned long irq_flags;
1171
1172 if (!ufshcd_is_clkscaling_supported(hba))
1173 return -EINVAL;
1174
1175 if ((*freq > 0) && (*freq < UINT_MAX)) {
1176 dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq);
1177 return -EINVAL;
1178 }
1179
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001180 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1181 if (ufshcd_eh_in_progress(hba)) {
1182 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1183 return 0;
1184 }
1185
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001186 if (!hba->clk_scaling.active_reqs)
1187 sched_clk_scaling_suspend_work = true;
1188
1189 scale_up = (*freq == UINT_MAX) ? true : false;
1190 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1191 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1192 ret = 0;
1193 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001194 }
1195 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1196
1197 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001198 ret = ufshcd_devfreq_scale(hba, scale_up);
1199
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001200 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1201 (scale_up ? "up" : "down"),
1202 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1203
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001204out:
1205 if (sched_clk_scaling_suspend_work)
1206 queue_work(hba->clk_scaling.workq,
1207 &hba->clk_scaling.suspend_work);
1208
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001209 return ret;
1210}
1211
1212
1213static int ufshcd_devfreq_get_dev_status(struct device *dev,
1214 struct devfreq_dev_status *stat)
1215{
1216 struct ufs_hba *hba = dev_get_drvdata(dev);
1217 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1218 unsigned long flags;
1219
1220 if (!ufshcd_is_clkscaling_supported(hba))
1221 return -EINVAL;
1222
1223 memset(stat, 0, sizeof(*stat));
1224
1225 spin_lock_irqsave(hba->host->host_lock, flags);
1226 if (!scaling->window_start_t)
1227 goto start_window;
1228
1229 if (scaling->is_busy_started)
1230 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1231 scaling->busy_start_t));
1232
1233 stat->total_time = jiffies_to_usecs((long)jiffies -
1234 (long)scaling->window_start_t);
1235 stat->busy_time = scaling->tot_busy_t;
1236start_window:
1237 scaling->window_start_t = jiffies;
1238 scaling->tot_busy_t = 0;
1239
1240 if (hba->outstanding_reqs) {
1241 scaling->busy_start_t = ktime_get();
1242 scaling->is_busy_started = true;
1243 } else {
1244 scaling->busy_start_t = 0;
1245 scaling->is_busy_started = false;
1246 }
1247 spin_unlock_irqrestore(hba->host->host_lock, flags);
1248 return 0;
1249}
1250
1251static struct devfreq_dev_profile ufs_devfreq_profile = {
1252 .polling_ms = 100,
1253 .target = ufshcd_devfreq_target,
1254 .get_dev_status = ufshcd_devfreq_get_dev_status,
1255};
1256
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001257static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1258{
1259 unsigned long flags;
1260
1261 devfreq_suspend_device(hba->devfreq);
1262 spin_lock_irqsave(hba->host->host_lock, flags);
1263 hba->clk_scaling.window_start_t = 0;
1264 spin_unlock_irqrestore(hba->host->host_lock, flags);
1265}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001266
Gilad Bronera5082532016-10-17 17:10:00 -07001267static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1268{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001269 unsigned long flags;
1270 bool suspend = false;
1271
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001272 if (!ufshcd_is_clkscaling_supported(hba))
1273 return;
1274
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001275 spin_lock_irqsave(hba->host->host_lock, flags);
1276 if (!hba->clk_scaling.is_suspended) {
1277 suspend = true;
1278 hba->clk_scaling.is_suspended = true;
1279 }
1280 spin_unlock_irqrestore(hba->host->host_lock, flags);
1281
1282 if (suspend)
1283 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001284}
1285
1286static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1287{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001288 unsigned long flags;
1289 bool resume = false;
1290
1291 if (!ufshcd_is_clkscaling_supported(hba))
1292 return;
1293
1294 spin_lock_irqsave(hba->host->host_lock, flags);
1295 if (hba->clk_scaling.is_suspended) {
1296 resume = true;
1297 hba->clk_scaling.is_suspended = false;
1298 }
1299 spin_unlock_irqrestore(hba->host->host_lock, flags);
1300
1301 if (resume)
1302 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001303}
1304
1305static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1306 struct device_attribute *attr, char *buf)
1307{
1308 struct ufs_hba *hba = dev_get_drvdata(dev);
1309
1310 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1311}
1312
1313static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1314 struct device_attribute *attr, const char *buf, size_t count)
1315{
1316 struct ufs_hba *hba = dev_get_drvdata(dev);
1317 u32 value;
1318 int err;
1319
1320 if (kstrtou32(buf, 0, &value))
1321 return -EINVAL;
1322
1323 value = !!value;
1324 if (value == hba->clk_scaling.is_allowed)
1325 goto out;
1326
1327 pm_runtime_get_sync(hba->dev);
1328 ufshcd_hold(hba, false);
1329
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001330 cancel_work_sync(&hba->clk_scaling.suspend_work);
1331 cancel_work_sync(&hba->clk_scaling.resume_work);
1332
1333 hba->clk_scaling.is_allowed = value;
1334
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001335 if (value) {
1336 ufshcd_resume_clkscaling(hba);
1337 } else {
1338 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001339 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001340 if (err)
1341 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1342 __func__, err);
1343 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001344
1345 ufshcd_release(hba);
1346 pm_runtime_put_sync(hba->dev);
1347out:
1348 return count;
Gilad Bronera5082532016-10-17 17:10:00 -07001349}
1350
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001351static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1352{
1353 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1354 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1355 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1356 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1357 hba->clk_scaling.enable_attr.attr.mode = 0644;
1358 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1359 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1360}
1361
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001362static void ufshcd_ungate_work(struct work_struct *work)
1363{
1364 int ret;
1365 unsigned long flags;
1366 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1367 clk_gating.ungate_work);
1368
1369 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1370
1371 spin_lock_irqsave(hba->host->host_lock, flags);
1372 if (hba->clk_gating.state == CLKS_ON) {
1373 spin_unlock_irqrestore(hba->host->host_lock, flags);
1374 goto unblock_reqs;
1375 }
1376
1377 spin_unlock_irqrestore(hba->host->host_lock, flags);
1378 ufshcd_setup_clocks(hba, true);
1379
1380 /* Exit from hibern8 */
1381 if (ufshcd_can_hibern8_during_gating(hba)) {
1382 /* Prevent gating in this path */
1383 hba->clk_gating.is_suspended = true;
1384 if (ufshcd_is_link_hibern8(hba)) {
1385 ret = ufshcd_uic_hibern8_exit(hba);
1386 if (ret)
1387 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1388 __func__, ret);
1389 else
1390 ufshcd_set_link_active(hba);
1391 }
1392 hba->clk_gating.is_suspended = false;
1393 }
1394unblock_reqs:
1395 scsi_unblock_requests(hba->host);
1396}
1397
1398/**
1399 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1400 * Also, exit from hibern8 mode and set the link as active.
1401 * @hba: per adapter instance
1402 * @async: This indicates whether caller should ungate clocks asynchronously.
1403 */
1404int ufshcd_hold(struct ufs_hba *hba, bool async)
1405{
1406 int rc = 0;
1407 unsigned long flags;
1408
1409 if (!ufshcd_is_clkgating_allowed(hba))
1410 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001411 spin_lock_irqsave(hba->host->host_lock, flags);
1412 hba->clk_gating.active_reqs++;
1413
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001414 if (ufshcd_eh_in_progress(hba)) {
1415 spin_unlock_irqrestore(hba->host->host_lock, flags);
1416 return 0;
1417 }
1418
Sahitya Tummala856b3482014-09-25 15:32:34 +03001419start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001420 switch (hba->clk_gating.state) {
1421 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001422 /*
1423 * Wait for the ungate work to complete if in progress.
1424 * Though the clocks may be in ON state, the link could
1425 * still be in hibner8 state if hibern8 is allowed
1426 * during clock gating.
1427 * Make sure we exit hibern8 state also in addition to
1428 * clocks being ON.
1429 */
1430 if (ufshcd_can_hibern8_during_gating(hba) &&
1431 ufshcd_is_link_hibern8(hba)) {
1432 spin_unlock_irqrestore(hba->host->host_lock, flags);
1433 flush_work(&hba->clk_gating.ungate_work);
1434 spin_lock_irqsave(hba->host->host_lock, flags);
1435 goto start;
1436 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001437 break;
1438 case REQ_CLKS_OFF:
1439 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1440 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001441 trace_ufshcd_clk_gating(dev_name(hba->dev),
1442 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001443 break;
1444 }
1445 /*
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +03001446 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001447 * currently running. Hence, fall through to cancel gating
1448 * work and to enable clocks.
1449 */
1450 case CLKS_OFF:
1451 scsi_block_requests(hba->host);
1452 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001453 trace_ufshcd_clk_gating(dev_name(hba->dev),
1454 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001455 schedule_work(&hba->clk_gating.ungate_work);
1456 /*
1457 * fall through to check if we should wait for this
1458 * work to be done or not.
1459 */
1460 case REQ_CLKS_ON:
1461 if (async) {
1462 rc = -EAGAIN;
1463 hba->clk_gating.active_reqs--;
1464 break;
1465 }
1466
1467 spin_unlock_irqrestore(hba->host->host_lock, flags);
1468 flush_work(&hba->clk_gating.ungate_work);
1469 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001470 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001471 goto start;
1472 default:
1473 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1474 __func__, hba->clk_gating.state);
1475 break;
1476 }
1477 spin_unlock_irqrestore(hba->host->host_lock, flags);
1478out:
1479 return rc;
1480}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001481EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001482
1483static void ufshcd_gate_work(struct work_struct *work)
1484{
1485 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1486 clk_gating.gate_work.work);
1487 unsigned long flags;
1488
1489 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001490 /*
1491 * In case you are here to cancel this work the gating state
1492 * would be marked as REQ_CLKS_ON. In this case save time by
1493 * skipping the gating work and exit after changing the clock
1494 * state to CLKS_ON.
1495 */
1496 if (hba->clk_gating.is_suspended ||
1497 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001498 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001499 trace_ufshcd_clk_gating(dev_name(hba->dev),
1500 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001501 goto rel_lock;
1502 }
1503
1504 if (hba->clk_gating.active_reqs
1505 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1506 || hba->lrb_in_use || hba->outstanding_tasks
1507 || hba->active_uic_cmd || hba->uic_async_done)
1508 goto rel_lock;
1509
1510 spin_unlock_irqrestore(hba->host->host_lock, flags);
1511
1512 /* put the link into hibern8 mode before turning off clocks */
1513 if (ufshcd_can_hibern8_during_gating(hba)) {
1514 if (ufshcd_uic_hibern8_enter(hba)) {
1515 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001516 trace_ufshcd_clk_gating(dev_name(hba->dev),
1517 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001518 goto out;
1519 }
1520 ufshcd_set_link_hibern8(hba);
1521 }
1522
1523 if (!ufshcd_is_link_active(hba))
1524 ufshcd_setup_clocks(hba, false);
1525 else
1526 /* If link is active, device ref_clk can't be switched off */
1527 __ufshcd_setup_clocks(hba, false, true);
1528
1529 /*
1530 * In case you are here to cancel this work the gating state
1531 * would be marked as REQ_CLKS_ON. In this case keep the state
1532 * as REQ_CLKS_ON which would anyway imply that clocks are off
1533 * and a request to turn them on is pending. By doing this way,
1534 * we keep the state machine in tact and this would ultimately
1535 * prevent from doing cancel work multiple times when there are
1536 * new requests arriving before the current cancel work is done.
1537 */
1538 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001539 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001540 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001541 trace_ufshcd_clk_gating(dev_name(hba->dev),
1542 hba->clk_gating.state);
1543 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001544rel_lock:
1545 spin_unlock_irqrestore(hba->host->host_lock, flags);
1546out:
1547 return;
1548}
1549
1550/* host lock must be held before calling this variant */
1551static void __ufshcd_release(struct ufs_hba *hba)
1552{
1553 if (!ufshcd_is_clkgating_allowed(hba))
1554 return;
1555
1556 hba->clk_gating.active_reqs--;
1557
1558 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1559 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1560 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001561 || hba->active_uic_cmd || hba->uic_async_done
1562 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001563 return;
1564
1565 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001566 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001567 schedule_delayed_work(&hba->clk_gating.gate_work,
1568 msecs_to_jiffies(hba->clk_gating.delay_ms));
1569}
1570
1571void ufshcd_release(struct ufs_hba *hba)
1572{
1573 unsigned long flags;
1574
1575 spin_lock_irqsave(hba->host->host_lock, flags);
1576 __ufshcd_release(hba);
1577 spin_unlock_irqrestore(hba->host->host_lock, flags);
1578}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001579EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001580
1581static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1582 struct device_attribute *attr, char *buf)
1583{
1584 struct ufs_hba *hba = dev_get_drvdata(dev);
1585
1586 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1587}
1588
1589static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1590 struct device_attribute *attr, const char *buf, size_t count)
1591{
1592 struct ufs_hba *hba = dev_get_drvdata(dev);
1593 unsigned long flags, value;
1594
1595 if (kstrtoul(buf, 0, &value))
1596 return -EINVAL;
1597
1598 spin_lock_irqsave(hba->host->host_lock, flags);
1599 hba->clk_gating.delay_ms = value;
1600 spin_unlock_irqrestore(hba->host->host_lock, flags);
1601 return count;
1602}
1603
Sahitya Tummalab4274112016-12-22 18:40:39 -08001604static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1605 struct device_attribute *attr, char *buf)
1606{
1607 struct ufs_hba *hba = dev_get_drvdata(dev);
1608
1609 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1610}
1611
1612static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1613 struct device_attribute *attr, const char *buf, size_t count)
1614{
1615 struct ufs_hba *hba = dev_get_drvdata(dev);
1616 unsigned long flags;
1617 u32 value;
1618
1619 if (kstrtou32(buf, 0, &value))
1620 return -EINVAL;
1621
1622 value = !!value;
1623 if (value == hba->clk_gating.is_enabled)
1624 goto out;
1625
1626 if (value) {
1627 ufshcd_release(hba);
1628 } else {
1629 spin_lock_irqsave(hba->host->host_lock, flags);
1630 hba->clk_gating.active_reqs++;
1631 spin_unlock_irqrestore(hba->host->host_lock, flags);
1632 }
1633
1634 hba->clk_gating.is_enabled = value;
1635out:
1636 return count;
1637}
1638
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001639static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1640{
1641 if (!ufshcd_is_clkgating_allowed(hba))
1642 return;
1643
1644 hba->clk_gating.delay_ms = 150;
1645 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1646 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1647
Sahitya Tummalab4274112016-12-22 18:40:39 -08001648 hba->clk_gating.is_enabled = true;
1649
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001650 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1651 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1652 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1653 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001654 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001655 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1656 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001657
1658 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1659 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1660 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1661 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1662 hba->clk_gating.enable_attr.attr.mode = 0644;
1663 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1664 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001665}
1666
1667static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1668{
1669 if (!ufshcd_is_clkgating_allowed(hba))
1670 return;
1671 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001672 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001673 cancel_work_sync(&hba->clk_gating.ungate_work);
1674 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001675}
1676
Sahitya Tummala856b3482014-09-25 15:32:34 +03001677/* Must be called with host lock acquired */
1678static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1679{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001680 bool queue_resume_work = false;
1681
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001682 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001683 return;
1684
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001685 if (!hba->clk_scaling.active_reqs++)
1686 queue_resume_work = true;
1687
1688 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1689 return;
1690
1691 if (queue_resume_work)
1692 queue_work(hba->clk_scaling.workq,
1693 &hba->clk_scaling.resume_work);
1694
1695 if (!hba->clk_scaling.window_start_t) {
1696 hba->clk_scaling.window_start_t = jiffies;
1697 hba->clk_scaling.tot_busy_t = 0;
1698 hba->clk_scaling.is_busy_started = false;
1699 }
1700
Sahitya Tummala856b3482014-09-25 15:32:34 +03001701 if (!hba->clk_scaling.is_busy_started) {
1702 hba->clk_scaling.busy_start_t = ktime_get();
1703 hba->clk_scaling.is_busy_started = true;
1704 }
1705}
1706
1707static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1708{
1709 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1710
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001711 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001712 return;
1713
1714 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1715 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1716 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001717 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001718 scaling->is_busy_started = false;
1719 }
1720}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301721/**
1722 * ufshcd_send_command - Send SCSI or device management commands
1723 * @hba: per adapter instance
1724 * @task_tag: Task tag of the command
1725 */
1726static inline
1727void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1728{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08001729 hba->lrb[task_tag].issue_time_stamp = ktime_get();
Zang Leigang09017182017-09-27 10:06:06 +08001730 hba->lrb[task_tag].compl_time_stamp = ktime_set(0, 0);
Sahitya Tummala856b3482014-09-25 15:32:34 +03001731 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301732 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301733 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001734 /* Make sure that doorbell is committed immediately */
1735 wmb();
Lee Susman1a07f2d2016-12-22 18:42:03 -08001736 ufshcd_add_command_trace(hba, task_tag, "send");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301737}
1738
1739/**
1740 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1741 * @lrb - pointer to local reference block
1742 */
1743static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1744{
1745 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301746 if (lrbp->sense_buffer &&
1747 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001748 int len_to_copy;
1749
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301750 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001751 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
1752
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301753 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301754 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001755 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301756 }
1757}
1758
1759/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301760 * ufshcd_copy_query_response() - Copy the Query Response and the data
1761 * descriptor
1762 * @hba: per adapter instance
1763 * @lrb - pointer to local reference block
1764 */
1765static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001766int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301767{
1768 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1769
Dolev Raviv68078d52013-07-30 00:35:58 +05301770 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301771
Dolev Raviv68078d52013-07-30 00:35:58 +05301772 /* Get the descriptor */
1773 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001774 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301775 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001776 u16 resp_len;
1777 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301778
1779 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001780 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301781 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001782 buf_len = be16_to_cpu(
1783 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001784 if (likely(buf_len >= resp_len)) {
1785 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1786 } else {
1787 dev_warn(hba->dev,
1788 "%s: Response size is bigger than buffer",
1789 __func__);
1790 return -EINVAL;
1791 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301792 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001793
1794 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301795}
1796
1797/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301798 * ufshcd_hba_capabilities - Read controller capabilities
1799 * @hba: per adapter instance
1800 */
1801static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
1802{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301803 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301804
1805 /* nutrs and nutmrs are 0 based values */
1806 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
1807 hba->nutmrs =
1808 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
1809}
1810
1811/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301812 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1813 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301814 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301815 * Return true on success, else false
1816 */
1817static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
1818{
1819 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1820 return true;
1821 else
1822 return false;
1823}
1824
1825/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301826 * ufshcd_get_upmcrs - Get the power mode change request status
1827 * @hba: Pointer to adapter instance
1828 *
1829 * This function gets the UPMCRS field of HCS register
1830 * Returns value of UPMCRS field
1831 */
1832static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1833{
1834 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1835}
1836
1837/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301838 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1839 * @hba: per adapter instance
1840 * @uic_cmd: UIC command
1841 *
1842 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301843 */
1844static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301845ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301846{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301847 WARN_ON(hba->active_uic_cmd);
1848
1849 hba->active_uic_cmd = uic_cmd;
1850
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301851 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301852 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1853 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1854 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301855
1856 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301857 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301858 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301859}
1860
1861/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301862 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1863 * @hba: per adapter instance
1864 * @uic_command: UIC command
1865 *
1866 * Must be called with mutex held.
1867 * Returns 0 only if success.
1868 */
1869static int
1870ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1871{
1872 int ret;
1873 unsigned long flags;
1874
1875 if (wait_for_completion_timeout(&uic_cmd->done,
1876 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1877 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1878 else
1879 ret = -ETIMEDOUT;
1880
1881 spin_lock_irqsave(hba->host->host_lock, flags);
1882 hba->active_uic_cmd = NULL;
1883 spin_unlock_irqrestore(hba->host->host_lock, flags);
1884
1885 return ret;
1886}
1887
1888/**
1889 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1890 * @hba: per adapter instance
1891 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001892 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301893 *
1894 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001895 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301896 * Returns 0 only if success.
1897 */
1898static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001899__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1900 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301901{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301902 if (!ufshcd_ready_for_uic_cmd(hba)) {
1903 dev_err(hba->dev,
1904 "Controller not ready to accept UIC commands\n");
1905 return -EIO;
1906 }
1907
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001908 if (completion)
1909 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301910
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301911 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301912
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001913 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301914}
1915
1916/**
1917 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1918 * @hba: per adapter instance
1919 * @uic_cmd: UIC command
1920 *
1921 * Returns 0 only if success.
1922 */
1923static int
1924ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1925{
1926 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001927 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301928
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001929 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301930 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001931 ufshcd_add_delay_before_dme_cmd(hba);
1932
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001933 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001934 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001935 spin_unlock_irqrestore(hba->host->host_lock, flags);
1936 if (!ret)
1937 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1938
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301939 mutex_unlock(&hba->uic_cmd_mutex);
1940
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001941 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301942 return ret;
1943}
1944
1945/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301946 * ufshcd_map_sg - Map scatter-gather list to prdt
1947 * @lrbp - pointer to local reference block
1948 *
1949 * Returns 0 in case of success, non-zero value in case of failure
1950 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001951static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301952{
1953 struct ufshcd_sg_entry *prd_table;
1954 struct scatterlist *sg;
1955 struct scsi_cmnd *cmd;
1956 int sg_segments;
1957 int i;
1958
1959 cmd = lrbp->cmd;
1960 sg_segments = scsi_dma_map(cmd);
1961 if (sg_segments < 0)
1962 return sg_segments;
1963
1964 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001965 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1966 lrbp->utr_descriptor_ptr->prd_table_length =
1967 cpu_to_le16((u16)(sg_segments *
1968 sizeof(struct ufshcd_sg_entry)));
1969 else
1970 lrbp->utr_descriptor_ptr->prd_table_length =
1971 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301972
1973 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1974
1975 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1976 prd_table[i].size =
1977 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1978 prd_table[i].base_addr =
1979 cpu_to_le32(lower_32_bits(sg->dma_address));
1980 prd_table[i].upper_addr =
1981 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001982 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301983 }
1984 } else {
1985 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1986 }
1987
1988 return 0;
1989}
1990
1991/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301992 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301993 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301994 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301995 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301996static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301997{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301998 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1999
2000 if (hba->ufs_version == UFSHCI_VERSION_10) {
2001 u32 rw;
2002 rw = set & INTERRUPT_MASK_RW_VER_10;
2003 set = rw | ((set ^ intrs) & intrs);
2004 } else {
2005 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302006 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302007
2008 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2009}
2010
2011/**
2012 * ufshcd_disable_intr - disable interrupts
2013 * @hba: per adapter instance
2014 * @intrs: interrupt bits
2015 */
2016static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2017{
2018 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2019
2020 if (hba->ufs_version == UFSHCI_VERSION_10) {
2021 u32 rw;
2022 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2023 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2024 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2025
2026 } else {
2027 set &= ~intrs;
2028 }
2029
2030 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302031}
2032
2033/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302034 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2035 * descriptor according to request
2036 * @lrbp: pointer to local reference block
2037 * @upiu_flags: flags required in the header
2038 * @cmd_dir: requests data direction
2039 */
2040static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01002041 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302042{
2043 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2044 u32 data_direction;
2045 u32 dword_0;
2046
2047 if (cmd_dir == DMA_FROM_DEVICE) {
2048 data_direction = UTP_DEVICE_TO_HOST;
2049 *upiu_flags = UPIU_CMD_FLAGS_READ;
2050 } else if (cmd_dir == DMA_TO_DEVICE) {
2051 data_direction = UTP_HOST_TO_DEVICE;
2052 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2053 } else {
2054 data_direction = UTP_NO_DATA_TRANSFER;
2055 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2056 }
2057
2058 dword_0 = data_direction | (lrbp->command_type
2059 << UPIU_COMMAND_TYPE_OFFSET);
2060 if (lrbp->intr_cmd)
2061 dword_0 |= UTP_REQ_DESC_INT_CMD;
2062
2063 /* Transfer request descriptor header fields */
2064 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002065 /* dword_1 is reserved, hence it is set to 0 */
2066 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302067 /*
2068 * assigning invalid value for command status. Controller
2069 * updates OCS on command completion, with the command
2070 * status
2071 */
2072 req_desc->header.dword_2 =
2073 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002074 /* dword_3 is reserved, hence it is set to 0 */
2075 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002076
2077 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302078}
2079
2080/**
2081 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2082 * for scsi commands
2083 * @lrbp - local reference block pointer
2084 * @upiu_flags - flags
2085 */
2086static
2087void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2088{
2089 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002090 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302091
2092 /* command descriptor fields */
2093 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2094 UPIU_TRANSACTION_COMMAND, upiu_flags,
2095 lrbp->lun, lrbp->task_tag);
2096 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2097 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2098
2099 /* Total EHS length and Data segment length will be zero */
2100 ucd_req_ptr->header.dword_2 = 0;
2101
2102 ucd_req_ptr->sc.exp_data_transfer_len =
2103 cpu_to_be32(lrbp->cmd->sdb.length);
2104
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002105 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
2106 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
2107 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
2108
2109 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302110}
2111
Dolev Raviv68078d52013-07-30 00:35:58 +05302112/**
2113 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2114 * for query requsts
2115 * @hba: UFS hba
2116 * @lrbp: local reference block pointer
2117 * @upiu_flags: flags
2118 */
2119static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2120 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2121{
2122 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2123 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302124 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302125 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
2126
2127 /* Query request header */
2128 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2129 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2130 lrbp->lun, lrbp->task_tag);
2131 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2132 0, query->request.query_func, 0, 0);
2133
Zang Leigang68612852016-08-25 17:39:19 +08002134 /* Data segment length only need for WRITE_DESC */
2135 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2136 ucd_req_ptr->header.dword_2 =
2137 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2138 else
2139 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302140
2141 /* Copy the Query Request buffer as is */
2142 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2143 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302144
2145 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002146 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2147 memcpy(descp, query->descriptor, len);
2148
Yaniv Gardi51047262016-02-01 15:02:38 +02002149 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302150}
2151
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302152static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2153{
2154 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2155
2156 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2157
2158 /* command descriptor fields */
2159 ucd_req_ptr->header.dword_0 =
2160 UPIU_HEADER_DWORD(
2161 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002162 /* clear rest of the fields of basic header */
2163 ucd_req_ptr->header.dword_1 = 0;
2164 ucd_req_ptr->header.dword_2 = 0;
2165
2166 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302167}
2168
2169/**
Joao Pinto300bb132016-05-11 12:21:27 +01002170 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2171 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302172 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302173 * @lrb - pointer to local reference block
2174 */
Joao Pinto300bb132016-05-11 12:21:27 +01002175static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302176{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302177 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302178 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302179
kehuanlin83dc7e32017-09-06 17:58:39 +08002180 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2181 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002182 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin83dc7e32017-09-06 17:58:39 +08002183 else
2184 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002185
2186 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2187 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2188 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2189 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2190 ufshcd_prepare_utp_nop_upiu(lrbp);
2191 else
2192 ret = -EINVAL;
2193
2194 return ret;
2195}
2196
2197/**
2198 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2199 * for SCSI Purposes
2200 * @hba - per adapter instance
2201 * @lrb - pointer to local reference block
2202 */
2203static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2204{
2205 u32 upiu_flags;
2206 int ret = 0;
2207
kehuanlin83dc7e32017-09-06 17:58:39 +08002208 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
2209 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01002210 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin83dc7e32017-09-06 17:58:39 +08002211 else
2212 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01002213
2214 if (likely(lrbp->cmd)) {
2215 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2216 lrbp->cmd->sc_data_direction);
2217 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2218 } else {
2219 ret = -EINVAL;
2220 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302221
2222 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302223}
2224
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002225/*
2226 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2227 * @scsi_lun: scsi LUN id
2228 *
2229 * Returns UPIU LUN id
2230 */
2231static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
2232{
2233 if (scsi_is_wlun(scsi_lun))
2234 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
2235 | UFS_UPIU_WLUN_ID;
2236 else
2237 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
2238}
2239
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302240/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002241 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2242 * @scsi_lun: UPIU W-LUN id
2243 *
2244 * Returns SCSI W-LUN id
2245 */
2246static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2247{
2248 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2249}
2250
2251/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302252 * ufshcd_queuecommand - main entry point for SCSI requests
2253 * @cmd: command from SCSI Midlayer
2254 * @done: call back function
2255 *
2256 * Returns 0 for success, non-zero in case of failure
2257 */
2258static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2259{
2260 struct ufshcd_lrb *lrbp;
2261 struct ufs_hba *hba;
2262 unsigned long flags;
2263 int tag;
2264 int err = 0;
2265
2266 hba = shost_priv(host);
2267
2268 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002269 if (!ufshcd_valid_tag(hba, tag)) {
2270 dev_err(hba->dev,
2271 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2272 __func__, tag, cmd, cmd->request);
2273 BUG();
2274 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302275
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002276 if (!down_read_trylock(&hba->clk_scaling_lock))
2277 return SCSI_MLQUEUE_HOST_BUSY;
2278
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302279 spin_lock_irqsave(hba->host->host_lock, flags);
2280 switch (hba->ufshcd_state) {
2281 case UFSHCD_STATE_OPERATIONAL:
2282 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002283 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302284 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302285 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302286 goto out_unlock;
2287 case UFSHCD_STATE_ERROR:
2288 set_host_byte(cmd, DID_ERROR);
2289 cmd->scsi_done(cmd);
2290 goto out_unlock;
2291 default:
2292 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2293 __func__, hba->ufshcd_state);
2294 set_host_byte(cmd, DID_BAD_TARGET);
2295 cmd->scsi_done(cmd);
2296 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302297 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002298
2299 /* if error handling is in progress, don't issue commands */
2300 if (ufshcd_eh_in_progress(hba)) {
2301 set_host_byte(cmd, DID_ERROR);
2302 cmd->scsi_done(cmd);
2303 goto out_unlock;
2304 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302305 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302306
Gilad Broner7fabb772017-02-03 16:56:50 -08002307 hba->req_abort_count = 0;
2308
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302309 /* acquire the tag to make sure device cmds don't use it */
2310 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2311 /*
2312 * Dev manage command in progress, requeue the command.
2313 * Requeuing the command helps in cases where the request *may*
2314 * find different tag instead of waiting for dev manage command
2315 * completion.
2316 */
2317 err = SCSI_MLQUEUE_HOST_BUSY;
2318 goto out;
2319 }
2320
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002321 err = ufshcd_hold(hba, true);
2322 if (err) {
2323 err = SCSI_MLQUEUE_HOST_BUSY;
2324 clear_bit_unlock(tag, &hba->lrb_in_use);
2325 goto out;
2326 }
2327 WARN_ON(hba->clk_gating.state != CLKS_ON);
2328
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302329 lrbp = &hba->lrb[tag];
2330
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302331 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302332 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07002333 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302334 lrbp->sense_buffer = cmd->sense_buffer;
2335 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002336 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002337 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Gilad Bronere0b299e2017-02-03 16:56:40 -08002338 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302339
Joao Pinto300bb132016-05-11 12:21:27 +01002340 ufshcd_comp_scsi_upiu(hba, lrbp);
2341
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002342 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302343 if (err) {
2344 lrbp->cmd = NULL;
2345 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302346 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302347 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002348 /* Make sure descriptors are ready before ringing the doorbell */
2349 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302350
2351 /* issue command to the controller */
2352 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002353 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302354 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302355out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302356 spin_unlock_irqrestore(hba->host->host_lock, flags);
2357out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002358 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302359 return err;
2360}
2361
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302362static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2363 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2364{
2365 lrbp->cmd = NULL;
2366 lrbp->sense_bufflen = 0;
2367 lrbp->sense_buffer = NULL;
2368 lrbp->task_tag = tag;
2369 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302370 lrbp->intr_cmd = true; /* No interrupt aggregation */
2371 hba->dev_cmd.type = cmd_type;
2372
Joao Pinto300bb132016-05-11 12:21:27 +01002373 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302374}
2375
2376static int
2377ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2378{
2379 int err = 0;
2380 unsigned long flags;
2381 u32 mask = 1 << tag;
2382
2383 /* clear outstanding transaction before retry */
2384 spin_lock_irqsave(hba->host->host_lock, flags);
2385 ufshcd_utrl_clear(hba, tag);
2386 spin_unlock_irqrestore(hba->host->host_lock, flags);
2387
2388 /*
2389 * wait for for h/w to clear corresponding bit in door-bell.
2390 * max. wait is 1 sec.
2391 */
2392 err = ufshcd_wait_for_register(hba,
2393 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002394 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302395
2396 return err;
2397}
2398
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002399static int
2400ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2401{
2402 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2403
2404 /* Get the UPIU response */
2405 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2406 UPIU_RSP_CODE_OFFSET;
2407 return query_res->response;
2408}
2409
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302410/**
2411 * ufshcd_dev_cmd_completion() - handles device management command responses
2412 * @hba: per adapter instance
2413 * @lrbp: pointer to local reference block
2414 */
2415static int
2416ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2417{
2418 int resp;
2419 int err = 0;
2420
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002421 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302422 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2423
2424 switch (resp) {
2425 case UPIU_TRANSACTION_NOP_IN:
2426 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2427 err = -EINVAL;
2428 dev_err(hba->dev, "%s: unexpected response %x\n",
2429 __func__, resp);
2430 }
2431 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302432 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002433 err = ufshcd_check_query_response(hba, lrbp);
2434 if (!err)
2435 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302436 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302437 case UPIU_TRANSACTION_REJECT_UPIU:
2438 /* TODO: handle Reject UPIU Response */
2439 err = -EPERM;
2440 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2441 __func__);
2442 break;
2443 default:
2444 err = -EINVAL;
2445 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2446 __func__, resp);
2447 break;
2448 }
2449
2450 return err;
2451}
2452
2453static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2454 struct ufshcd_lrb *lrbp, int max_timeout)
2455{
2456 int err = 0;
2457 unsigned long time_left;
2458 unsigned long flags;
2459
2460 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2461 msecs_to_jiffies(max_timeout));
2462
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002463 /* Make sure descriptors are ready before ringing the doorbell */
2464 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302465 spin_lock_irqsave(hba->host->host_lock, flags);
2466 hba->dev_cmd.complete = NULL;
2467 if (likely(time_left)) {
2468 err = ufshcd_get_tr_ocs(lrbp);
2469 if (!err)
2470 err = ufshcd_dev_cmd_completion(hba, lrbp);
2471 }
2472 spin_unlock_irqrestore(hba->host->host_lock, flags);
2473
2474 if (!time_left) {
2475 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002476 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2477 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302478 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002479 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302480 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002481 /*
2482 * in case of an error, after clearing the doorbell,
2483 * we also need to clear the outstanding_request
2484 * field in hba
2485 */
2486 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302487 }
2488
2489 return err;
2490}
2491
2492/**
2493 * ufshcd_get_dev_cmd_tag - Get device management command tag
2494 * @hba: per-adapter instance
2495 * @tag: pointer to variable with available slot value
2496 *
2497 * Get a free slot and lock it until device management command
2498 * completes.
2499 *
2500 * Returns false if free slot is unavailable for locking, else
2501 * return true with tag value in @tag.
2502 */
2503static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
2504{
2505 int tag;
2506 bool ret = false;
2507 unsigned long tmp;
2508
2509 if (!tag_out)
2510 goto out;
2511
2512 do {
2513 tmp = ~hba->lrb_in_use;
2514 tag = find_last_bit(&tmp, hba->nutrs);
2515 if (tag >= hba->nutrs)
2516 goto out;
2517 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
2518
2519 *tag_out = tag;
2520 ret = true;
2521out:
2522 return ret;
2523}
2524
2525static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
2526{
2527 clear_bit_unlock(tag, &hba->lrb_in_use);
2528}
2529
2530/**
2531 * ufshcd_exec_dev_cmd - API for sending device management requests
2532 * @hba - UFS hba
2533 * @cmd_type - specifies the type (NOP, Query...)
2534 * @timeout - time in seconds
2535 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302536 * NOTE: Since there is only one available tag for device management commands,
2537 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302538 */
2539static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2540 enum dev_cmd_type cmd_type, int timeout)
2541{
2542 struct ufshcd_lrb *lrbp;
2543 int err;
2544 int tag;
2545 struct completion wait;
2546 unsigned long flags;
2547
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002548 down_read(&hba->clk_scaling_lock);
2549
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302550 /*
2551 * Get free slot, sleep if slots are unavailable.
2552 * Even though we use wait_event() which sleeps indefinitely,
2553 * the maximum wait time is bounded by SCSI request timeout.
2554 */
2555 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
2556
2557 init_completion(&wait);
2558 lrbp = &hba->lrb[tag];
2559 WARN_ON(lrbp->cmd);
2560 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2561 if (unlikely(err))
2562 goto out_put_tag;
2563
2564 hba->dev_cmd.complete = &wait;
2565
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002566 /* Make sure descriptors are ready before ringing the doorbell */
2567 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302568 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002569 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302570 ufshcd_send_command(hba, tag);
2571 spin_unlock_irqrestore(hba->host->host_lock, flags);
2572
2573 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2574
2575out_put_tag:
2576 ufshcd_put_dev_cmd_tag(hba, tag);
2577 wake_up(&hba->dev_cmd.tag_wq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002578 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302579 return err;
2580}
2581
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302582/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002583 * ufshcd_init_query() - init the query response and request parameters
2584 * @hba: per-adapter instance
2585 * @request: address of the request pointer to be initialized
2586 * @response: address of the response pointer to be initialized
2587 * @opcode: operation to perform
2588 * @idn: flag idn to access
2589 * @index: LU number to access
2590 * @selector: query/flag/descriptor further identification
2591 */
2592static inline void ufshcd_init_query(struct ufs_hba *hba,
2593 struct ufs_query_req **request, struct ufs_query_res **response,
2594 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2595{
2596 *request = &hba->dev_cmd.query.request;
2597 *response = &hba->dev_cmd.query.response;
2598 memset(*request, 0, sizeof(struct ufs_query_req));
2599 memset(*response, 0, sizeof(struct ufs_query_res));
2600 (*request)->upiu_req.opcode = opcode;
2601 (*request)->upiu_req.idn = idn;
2602 (*request)->upiu_req.index = index;
2603 (*request)->upiu_req.selector = selector;
2604}
2605
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002606static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2607 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2608{
2609 int ret;
2610 int retries;
2611
2612 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2613 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
2614 if (ret)
2615 dev_dbg(hba->dev,
2616 "%s: failed with error %d, retries %d\n",
2617 __func__, ret, retries);
2618 else
2619 break;
2620 }
2621
2622 if (ret)
2623 dev_err(hba->dev,
2624 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2625 __func__, opcode, idn, ret, retries);
2626 return ret;
2627}
2628
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002629/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302630 * ufshcd_query_flag() - API function for sending flag query requests
2631 * hba: per-adapter instance
2632 * query_opcode: flag query to perform
2633 * idn: flag idn to access
2634 * flag_res: the flag value after the query request completes
2635 *
2636 * Returns 0 for success, non-zero in case of failure
2637 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002638int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05302639 enum flag_idn idn, bool *flag_res)
2640{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002641 struct ufs_query_req *request = NULL;
2642 struct ufs_query_res *response = NULL;
2643 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002644 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302645
2646 BUG_ON(!hba);
2647
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002648 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302649 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002650 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2651 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302652
2653 switch (opcode) {
2654 case UPIU_QUERY_OPCODE_SET_FLAG:
2655 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2656 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2657 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2658 break;
2659 case UPIU_QUERY_OPCODE_READ_FLAG:
2660 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2661 if (!flag_res) {
2662 /* No dummy reads */
2663 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2664 __func__);
2665 err = -EINVAL;
2666 goto out_unlock;
2667 }
2668 break;
2669 default:
2670 dev_err(hba->dev,
2671 "%s: Expected query flag opcode but got = %d\n",
2672 __func__, opcode);
2673 err = -EINVAL;
2674 goto out_unlock;
2675 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302676
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002677 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302678
2679 if (err) {
2680 dev_err(hba->dev,
2681 "%s: Sending flag query for idn %d failed, err = %d\n",
2682 __func__, idn, err);
2683 goto out_unlock;
2684 }
2685
2686 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302687 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302688 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2689
2690out_unlock:
2691 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002692 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302693 return err;
2694}
2695
2696/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302697 * ufshcd_query_attr - API function for sending attribute requests
2698 * hba: per-adapter instance
2699 * opcode: attribute opcode
2700 * idn: attribute idn to access
2701 * index: index field
2702 * selector: selector field
2703 * attr_val: the attribute value after the query request completes
2704 *
2705 * Returns 0 for success, non-zero in case of failure
2706*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302707static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302708 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
2709{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002710 struct ufs_query_req *request = NULL;
2711 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302712 int err;
2713
2714 BUG_ON(!hba);
2715
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002716 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302717 if (!attr_val) {
2718 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2719 __func__, opcode);
2720 err = -EINVAL;
2721 goto out;
2722 }
2723
2724 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002725 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2726 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302727
2728 switch (opcode) {
2729 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2730 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302731 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302732 break;
2733 case UPIU_QUERY_OPCODE_READ_ATTR:
2734 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2735 break;
2736 default:
2737 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2738 __func__, opcode);
2739 err = -EINVAL;
2740 goto out_unlock;
2741 }
2742
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002743 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302744
2745 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002746 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2747 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302748 goto out_unlock;
2749 }
2750
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302751 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302752
2753out_unlock:
2754 mutex_unlock(&hba->dev_cmd.lock);
2755out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002756 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302757 return err;
2758}
2759
2760/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002761 * ufshcd_query_attr_retry() - API function for sending query
2762 * attribute with retries
2763 * @hba: per-adapter instance
2764 * @opcode: attribute opcode
2765 * @idn: attribute idn to access
2766 * @index: index field
2767 * @selector: selector field
2768 * @attr_val: the attribute value after the query request
2769 * completes
2770 *
2771 * Returns 0 for success, non-zero in case of failure
2772*/
2773static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2774 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2775 u32 *attr_val)
2776{
2777 int ret = 0;
2778 u32 retries;
2779
2780 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2781 ret = ufshcd_query_attr(hba, opcode, idn, index,
2782 selector, attr_val);
2783 if (ret)
2784 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2785 __func__, ret, retries);
2786 else
2787 break;
2788 }
2789
2790 if (ret)
2791 dev_err(hba->dev,
2792 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2793 __func__, idn, ret, QUERY_REQ_RETRIES);
2794 return ret;
2795}
2796
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002797static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002798 enum query_opcode opcode, enum desc_idn idn, u8 index,
2799 u8 selector, u8 *desc_buf, int *buf_len)
2800{
2801 struct ufs_query_req *request = NULL;
2802 struct ufs_query_res *response = NULL;
2803 int err;
2804
2805 BUG_ON(!hba);
2806
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002807 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002808 if (!desc_buf) {
2809 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2810 __func__, opcode);
2811 err = -EINVAL;
2812 goto out;
2813 }
2814
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002815 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002816 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2817 __func__, *buf_len);
2818 err = -EINVAL;
2819 goto out;
2820 }
2821
2822 mutex_lock(&hba->dev_cmd.lock);
2823 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2824 selector);
2825 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002826 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002827
2828 switch (opcode) {
2829 case UPIU_QUERY_OPCODE_WRITE_DESC:
2830 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2831 break;
2832 case UPIU_QUERY_OPCODE_READ_DESC:
2833 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2834 break;
2835 default:
2836 dev_err(hba->dev,
2837 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2838 __func__, opcode);
2839 err = -EINVAL;
2840 goto out_unlock;
2841 }
2842
2843 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2844
2845 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002846 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2847 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002848 goto out_unlock;
2849 }
2850
2851 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002852 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002853
2854out_unlock:
2855 mutex_unlock(&hba->dev_cmd.lock);
2856out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002857 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002858 return err;
2859}
2860
2861/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002862 * ufshcd_query_descriptor_retry - API function for sending descriptor
2863 * requests
2864 * hba: per-adapter instance
2865 * opcode: attribute opcode
2866 * idn: attribute idn to access
2867 * index: index field
2868 * selector: selector field
2869 * desc_buf: the buffer that contains the descriptor
2870 * buf_len: length parameter passed to the device
2871 *
2872 * Returns 0 for success, non-zero in case of failure.
2873 * The buf_len parameter will contain, on return, the length parameter
2874 * received on the response.
2875 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02002876int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2877 enum query_opcode opcode,
2878 enum desc_idn idn, u8 index,
2879 u8 selector,
2880 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002881{
2882 int err;
2883 int retries;
2884
2885 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2886 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2887 selector, desc_buf, buf_len);
2888 if (!err || err == -EINVAL)
2889 break;
2890 }
2891
2892 return err;
2893}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002894
2895/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002896 * ufshcd_read_desc_length - read the specified descriptor length from header
2897 * @hba: Pointer to adapter instance
2898 * @desc_id: descriptor idn value
2899 * @desc_index: descriptor index
2900 * @desc_length: pointer to variable to read the length of descriptor
2901 *
2902 * Return 0 in case of success, non-zero otherwise
2903 */
2904static int ufshcd_read_desc_length(struct ufs_hba *hba,
2905 enum desc_idn desc_id,
2906 int desc_index,
2907 int *desc_length)
2908{
2909 int ret;
2910 u8 header[QUERY_DESC_HDR_SIZE];
2911 int header_len = QUERY_DESC_HDR_SIZE;
2912
2913 if (desc_id >= QUERY_DESC_IDN_MAX)
2914 return -EINVAL;
2915
2916 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2917 desc_id, desc_index, 0, header,
2918 &header_len);
2919
2920 if (ret) {
2921 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2922 __func__, desc_id);
2923 return ret;
2924 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
2925 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
2926 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
2927 desc_id);
2928 ret = -EINVAL;
2929 }
2930
2931 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
2932 return ret;
2933
2934}
2935
2936/**
2937 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
2938 * @hba: Pointer to adapter instance
2939 * @desc_id: descriptor idn value
2940 * @desc_len: mapped desc length (out)
2941 *
2942 * Return 0 in case of success, non-zero otherwise
2943 */
2944int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
2945 enum desc_idn desc_id, int *desc_len)
2946{
2947 switch (desc_id) {
2948 case QUERY_DESC_IDN_DEVICE:
2949 *desc_len = hba->desc_size.dev_desc;
2950 break;
2951 case QUERY_DESC_IDN_POWER:
2952 *desc_len = hba->desc_size.pwr_desc;
2953 break;
2954 case QUERY_DESC_IDN_GEOMETRY:
2955 *desc_len = hba->desc_size.geom_desc;
2956 break;
2957 case QUERY_DESC_IDN_CONFIGURATION:
2958 *desc_len = hba->desc_size.conf_desc;
2959 break;
2960 case QUERY_DESC_IDN_UNIT:
2961 *desc_len = hba->desc_size.unit_desc;
2962 break;
2963 case QUERY_DESC_IDN_INTERCONNECT:
2964 *desc_len = hba->desc_size.interc_desc;
2965 break;
2966 case QUERY_DESC_IDN_STRING:
2967 *desc_len = QUERY_DESC_MAX_SIZE;
2968 break;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02002969 case QUERY_DESC_IDN_HEALTH:
2970 *desc_len = hba->desc_size.hlth_desc;
2971 break;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002972 case QUERY_DESC_IDN_RFU_0:
2973 case QUERY_DESC_IDN_RFU_1:
2974 *desc_len = 0;
2975 break;
2976 default:
2977 *desc_len = 0;
2978 return -EINVAL;
2979 }
2980 return 0;
2981}
2982EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
2983
2984/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002985 * ufshcd_read_desc_param - read the specified descriptor parameter
2986 * @hba: Pointer to adapter instance
2987 * @desc_id: descriptor idn value
2988 * @desc_index: descriptor index
2989 * @param_offset: offset of the parameter to read
2990 * @param_read_buf: pointer to buffer where parameter would be read
2991 * @param_size: sizeof(param_read_buf)
2992 *
2993 * Return 0 in case of success, non-zero otherwise
2994 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02002995int ufshcd_read_desc_param(struct ufs_hba *hba,
2996 enum desc_idn desc_id,
2997 int desc_index,
2998 u8 param_offset,
2999 u8 *param_read_buf,
3000 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003001{
3002 int ret;
3003 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003004 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003005 bool is_kmalloc = true;
3006
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003007 /* Safety check */
3008 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003009 return -EINVAL;
3010
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003011 /* Get the max length of descriptor from structure filled up at probe
3012 * time.
3013 */
3014 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003015
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003016 /* Sanity checks */
3017 if (ret || !buff_len) {
3018 dev_err(hba->dev, "%s: Failed to get full descriptor length",
3019 __func__);
3020 return ret;
3021 }
3022
3023 /* Check whether we need temp memory */
3024 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003025 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3026 if (!desc_buf)
3027 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003028 } else {
3029 desc_buf = param_read_buf;
3030 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003031 }
3032
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003033 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003034 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003035 desc_id, desc_index, 0,
3036 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003037
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003038 if (ret) {
3039 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3040 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003041 goto out;
3042 }
3043
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003044 /* Sanity check */
3045 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3046 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3047 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3048 ret = -EINVAL;
3049 goto out;
3050 }
3051
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003052 /* Check wherher we will not copy more data, than available */
3053 if (is_kmalloc && param_size > buff_len)
3054 param_size = buff_len;
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003055
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003056 if (is_kmalloc)
3057 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3058out:
3059 if (is_kmalloc)
3060 kfree(desc_buf);
3061 return ret;
3062}
3063
3064static inline int ufshcd_read_desc(struct ufs_hba *hba,
3065 enum desc_idn desc_id,
3066 int desc_index,
3067 u8 *buf,
3068 u32 size)
3069{
3070 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3071}
3072
3073static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3074 u8 *buf,
3075 u32 size)
3076{
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02003077 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003078}
3079
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003080static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
Yaniv Gardib573d482016-03-10 17:37:09 +02003081{
3082 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3083}
Yaniv Gardib573d482016-03-10 17:37:09 +02003084
3085/**
3086 * ufshcd_read_string_desc - read string descriptor
3087 * @hba: pointer to adapter instance
3088 * @desc_index: descriptor index
3089 * @buf: pointer to buffer where descriptor would be read
3090 * @size: size of buf
3091 * @ascii: if true convert from unicode to ascii characters
3092 *
3093 * Return 0 in case of success, non-zero otherwise
3094 */
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003095#define ASCII_STD true
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02003096int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
3097 u8 *buf, u32 size, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003098{
3099 int err = 0;
3100
3101 err = ufshcd_read_desc(hba,
3102 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3103
3104 if (err) {
3105 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3106 __func__, QUERY_REQ_RETRIES, err);
3107 goto out;
3108 }
3109
3110 if (ascii) {
3111 int desc_len;
3112 int ascii_len;
3113 int i;
3114 char *buff_ascii;
3115
3116 desc_len = buf[0];
3117 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3118 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3119 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3120 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3121 __func__);
3122 err = -ENOMEM;
3123 goto out;
3124 }
3125
3126 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3127 if (!buff_ascii) {
3128 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003129 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003130 }
3131
3132 /*
3133 * the descriptor contains string in UTF16 format
3134 * we need to convert to utf-8 so it can be displayed
3135 */
3136 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3137 desc_len - QUERY_DESC_HDR_SIZE,
3138 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3139
3140 /* replace non-printable or non-ASCII characters with spaces */
3141 for (i = 0; i < ascii_len; i++)
3142 ufshcd_remove_non_printable(&buff_ascii[i]);
3143
3144 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3145 size - QUERY_DESC_HDR_SIZE);
3146 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3147 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02003148 kfree(buff_ascii);
3149 }
3150out:
3151 return err;
3152}
Yaniv Gardib573d482016-03-10 17:37:09 +02003153
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003154/**
3155 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3156 * @hba: Pointer to adapter instance
3157 * @lun: lun id
3158 * @param_offset: offset of the parameter to read
3159 * @param_read_buf: pointer to buffer where parameter would be read
3160 * @param_size: sizeof(param_read_buf)
3161 *
3162 * Return 0 in case of success, non-zero otherwise
3163 */
3164static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3165 int lun,
3166 enum unit_desc_param param_offset,
3167 u8 *param_read_buf,
3168 u32 param_size)
3169{
3170 /*
3171 * Unit descriptors are only available for general purpose LUs (LUN id
3172 * from 0 to 7) and RPMB Well known LU.
3173 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003174 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003175 return -EOPNOTSUPP;
3176
3177 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3178 param_offset, param_read_buf, param_size);
3179}
3180
3181/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303182 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3183 * @hba: per adapter instance
3184 *
3185 * 1. Allocate DMA memory for Command Descriptor array
3186 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3187 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3188 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3189 * (UTMRDL)
3190 * 4. Allocate memory for local reference block(lrb).
3191 *
3192 * Returns 0 for success, non-zero in case of failure
3193 */
3194static int ufshcd_memory_alloc(struct ufs_hba *hba)
3195{
3196 size_t utmrdl_size, utrdl_size, ucdl_size;
3197
3198 /* Allocate memory for UTP command descriptors */
3199 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003200 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3201 ucdl_size,
3202 &hba->ucdl_dma_addr,
3203 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303204
3205 /*
3206 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3207 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3208 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3209 * be aligned to 128 bytes as well
3210 */
3211 if (!hba->ucdl_base_addr ||
3212 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303213 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303214 "Command Descriptor Memory allocation failed\n");
3215 goto out;
3216 }
3217
3218 /*
3219 * Allocate memory for UTP Transfer descriptors
3220 * UFSHCI requires 1024 byte alignment of UTRD
3221 */
3222 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003223 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3224 utrdl_size,
3225 &hba->utrdl_dma_addr,
3226 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303227 if (!hba->utrdl_base_addr ||
3228 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303229 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303230 "Transfer Descriptor Memory allocation failed\n");
3231 goto out;
3232 }
3233
3234 /*
3235 * Allocate memory for UTP Task Management descriptors
3236 * UFSHCI requires 1024 byte alignment of UTMRD
3237 */
3238 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003239 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3240 utmrdl_size,
3241 &hba->utmrdl_dma_addr,
3242 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303243 if (!hba->utmrdl_base_addr ||
3244 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303245 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303246 "Task Management Descriptor Memory allocation failed\n");
3247 goto out;
3248 }
3249
3250 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003251 hba->lrb = devm_kzalloc(hba->dev,
3252 hba->nutrs * sizeof(struct ufshcd_lrb),
3253 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303254 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303255 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303256 goto out;
3257 }
3258 return 0;
3259out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303260 return -ENOMEM;
3261}
3262
3263/**
3264 * ufshcd_host_memory_configure - configure local reference block with
3265 * memory offsets
3266 * @hba: per adapter instance
3267 *
3268 * Configure Host memory space
3269 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3270 * address.
3271 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3272 * and PRDT offset.
3273 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3274 * into local reference block.
3275 */
3276static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3277{
3278 struct utp_transfer_cmd_desc *cmd_descp;
3279 struct utp_transfer_req_desc *utrdlp;
3280 dma_addr_t cmd_desc_dma_addr;
3281 dma_addr_t cmd_desc_element_addr;
3282 u16 response_offset;
3283 u16 prdt_offset;
3284 int cmd_desc_size;
3285 int i;
3286
3287 utrdlp = hba->utrdl_base_addr;
3288 cmd_descp = hba->ucdl_base_addr;
3289
3290 response_offset =
3291 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3292 prdt_offset =
3293 offsetof(struct utp_transfer_cmd_desc, prd_table);
3294
3295 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3296 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3297
3298 for (i = 0; i < hba->nutrs; i++) {
3299 /* Configure UTRD with command descriptor base address */
3300 cmd_desc_element_addr =
3301 (cmd_desc_dma_addr + (cmd_desc_size * i));
3302 utrdlp[i].command_desc_base_addr_lo =
3303 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3304 utrdlp[i].command_desc_base_addr_hi =
3305 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3306
3307 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003308 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3309 utrdlp[i].response_upiu_offset =
3310 cpu_to_le16(response_offset);
3311 utrdlp[i].prd_table_offset =
3312 cpu_to_le16(prdt_offset);
3313 utrdlp[i].response_upiu_length =
3314 cpu_to_le16(ALIGNED_UPIU_SIZE);
3315 } else {
3316 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303317 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003318 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303319 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003320 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303321 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003322 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303323
3324 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003325 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3326 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303327 hba->lrb[i].ucd_req_ptr =
3328 (struct utp_upiu_req *)(cmd_descp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003329 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303330 hba->lrb[i].ucd_rsp_ptr =
3331 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003332 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3333 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303334 hba->lrb[i].ucd_prdt_ptr =
3335 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003336 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3337 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303338 }
3339}
3340
3341/**
3342 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3343 * @hba: per adapter instance
3344 *
3345 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3346 * in order to initialize the Unipro link startup procedure.
3347 * Once the Unipro links are up, the device connected to the controller
3348 * is detected.
3349 *
3350 * Returns 0 on success, non-zero value on failure
3351 */
3352static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3353{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303354 struct uic_command uic_cmd = {0};
3355 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303356
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303357 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3358
3359 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3360 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003361 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303362 "dme-link-startup: error code %d\n", ret);
3363 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303364}
3365
Yaniv Gardicad2e032015-03-31 17:37:14 +03003366static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3367{
3368 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3369 unsigned long min_sleep_time_us;
3370
3371 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3372 return;
3373
3374 /*
3375 * last_dme_cmd_tstamp will be 0 only for 1st call to
3376 * this function
3377 */
3378 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3379 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3380 } else {
3381 unsigned long delta =
3382 (unsigned long) ktime_to_us(
3383 ktime_sub(ktime_get(),
3384 hba->last_dme_cmd_tstamp));
3385
3386 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3387 min_sleep_time_us =
3388 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3389 else
3390 return; /* no more delay required */
3391 }
3392
3393 /* allow sleep for extra 50us if needed */
3394 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3395}
3396
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303397/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303398 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3399 * @hba: per adapter instance
3400 * @attr_sel: uic command argument1
3401 * @attr_set: attribute set type as uic command argument2
3402 * @mib_val: setting value as uic command argument3
3403 * @peer: indicate whether peer or local
3404 *
3405 * Returns 0 on success, non-zero value on failure
3406 */
3407int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3408 u8 attr_set, u32 mib_val, u8 peer)
3409{
3410 struct uic_command uic_cmd = {0};
3411 static const char *const action[] = {
3412 "dme-set",
3413 "dme-peer-set"
3414 };
3415 const char *set = action[!!peer];
3416 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003417 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303418
3419 uic_cmd.command = peer ?
3420 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3421 uic_cmd.argument1 = attr_sel;
3422 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3423 uic_cmd.argument3 = mib_val;
3424
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003425 do {
3426 /* for peer attributes we retry upon failure */
3427 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3428 if (ret)
3429 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3430 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3431 } while (ret && peer && --retries);
3432
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003433 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003434 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003435 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3436 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303437
3438 return ret;
3439}
3440EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3441
3442/**
3443 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3444 * @hba: per adapter instance
3445 * @attr_sel: uic command argument1
3446 * @mib_val: the value of the attribute as returned by the UIC command
3447 * @peer: indicate whether peer or local
3448 *
3449 * Returns 0 on success, non-zero value on failure
3450 */
3451int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3452 u32 *mib_val, u8 peer)
3453{
3454 struct uic_command uic_cmd = {0};
3455 static const char *const action[] = {
3456 "dme-get",
3457 "dme-peer-get"
3458 };
3459 const char *get = action[!!peer];
3460 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003461 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003462 struct ufs_pa_layer_attr orig_pwr_info;
3463 struct ufs_pa_layer_attr temp_pwr_info;
3464 bool pwr_mode_change = false;
3465
3466 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3467 orig_pwr_info = hba->pwr_info;
3468 temp_pwr_info = orig_pwr_info;
3469
3470 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3471 orig_pwr_info.pwr_rx == FAST_MODE) {
3472 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3473 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3474 pwr_mode_change = true;
3475 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3476 orig_pwr_info.pwr_rx == SLOW_MODE) {
3477 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3478 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3479 pwr_mode_change = true;
3480 }
3481 if (pwr_mode_change) {
3482 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3483 if (ret)
3484 goto out;
3485 }
3486 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303487
3488 uic_cmd.command = peer ?
3489 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3490 uic_cmd.argument1 = attr_sel;
3491
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003492 do {
3493 /* for peer attributes we retry upon failure */
3494 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3495 if (ret)
3496 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3497 get, UIC_GET_ATTR_ID(attr_sel), ret);
3498 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303499
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003500 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003501 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003502 get, UIC_GET_ATTR_ID(attr_sel),
3503 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003504
3505 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303506 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003507
3508 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3509 && pwr_mode_change)
3510 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303511out:
3512 return ret;
3513}
3514EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3515
3516/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003517 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3518 * state) and waits for it to take effect.
3519 *
3520 * @hba: per adapter instance
3521 * @cmd: UIC command to execute
3522 *
3523 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3524 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3525 * and device UniPro link and hence it's final completion would be indicated by
3526 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3527 * addition to normal UIC command completion Status (UCCS). This function only
3528 * returns after the relevant status bits indicate the completion.
3529 *
3530 * Returns 0 on success, non-zero value on failure
3531 */
3532static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3533{
3534 struct completion uic_async_done;
3535 unsigned long flags;
3536 u8 status;
3537 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003538 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003539
3540 mutex_lock(&hba->uic_cmd_mutex);
3541 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003542 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003543
3544 spin_lock_irqsave(hba->host->host_lock, flags);
3545 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003546 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3547 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3548 /*
3549 * Make sure UIC command completion interrupt is disabled before
3550 * issuing UIC command.
3551 */
3552 wmb();
3553 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003554 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003555 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3556 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003557 if (ret) {
3558 dev_err(hba->dev,
3559 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3560 cmd->command, cmd->argument3, ret);
3561 goto out;
3562 }
3563
3564 if (!wait_for_completion_timeout(hba->uic_async_done,
3565 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3566 dev_err(hba->dev,
3567 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3568 cmd->command, cmd->argument3);
3569 ret = -ETIMEDOUT;
3570 goto out;
3571 }
3572
3573 status = ufshcd_get_upmcrs(hba);
3574 if (status != PWR_LOCAL) {
3575 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08003576 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003577 cmd->command, status);
3578 ret = (status != PWR_OK) ? status : -1;
3579 }
3580out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003581 if (ret) {
3582 ufshcd_print_host_state(hba);
3583 ufshcd_print_pwr_info(hba);
3584 ufshcd_print_host_regs(hba);
3585 }
3586
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003587 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003588 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003589 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003590 if (reenable_intr)
3591 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003592 spin_unlock_irqrestore(hba->host->host_lock, flags);
3593 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003594
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003595 return ret;
3596}
3597
3598/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303599 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3600 * using DME_SET primitives.
3601 * @hba: per adapter instance
3602 * @mode: powr mode value
3603 *
3604 * Returns 0 on success, non-zero value on failure
3605 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303606static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303607{
3608 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003609 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303610
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003611 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3612 ret = ufshcd_dme_set(hba,
3613 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3614 if (ret) {
3615 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3616 __func__, ret);
3617 goto out;
3618 }
3619 }
3620
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303621 uic_cmd.command = UIC_CMD_DME_SET;
3622 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3623 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003624 ufshcd_hold(hba, false);
3625 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3626 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303627
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003628out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003629 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003630}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303631
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003632static int ufshcd_link_recovery(struct ufs_hba *hba)
3633{
3634 int ret;
3635 unsigned long flags;
3636
3637 spin_lock_irqsave(hba->host->host_lock, flags);
3638 hba->ufshcd_state = UFSHCD_STATE_RESET;
3639 ufshcd_set_eh_in_progress(hba);
3640 spin_unlock_irqrestore(hba->host->host_lock, flags);
3641
3642 ret = ufshcd_host_reset_and_restore(hba);
3643
3644 spin_lock_irqsave(hba->host->host_lock, flags);
3645 if (ret)
3646 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3647 ufshcd_clear_eh_in_progress(hba);
3648 spin_unlock_irqrestore(hba->host->host_lock, flags);
3649
3650 if (ret)
3651 dev_err(hba->dev, "%s: link recovery failed, err %d",
3652 __func__, ret);
3653
3654 return ret;
3655}
3656
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003657static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003658{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003659 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003660 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003661 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003662
Kiwoong Kimee32c902016-11-10 21:17:43 +09003663 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3664
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003665 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003666 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003667 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3668 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003669
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003670 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003671 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3672 __func__, ret);
3673
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003674 /*
3675 * If link recovery fails then return error so that caller
3676 * don't retry the hibern8 enter again.
3677 */
3678 if (ufshcd_link_recovery(hba))
3679 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09003680 } else
3681 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3682 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003683
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003684 return ret;
3685}
3686
3687static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3688{
3689 int ret = 0, retries;
3690
3691 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3692 ret = __ufshcd_uic_hibern8_enter(hba);
3693 if (!ret || ret == -ENOLINK)
3694 goto out;
3695 }
3696out:
3697 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003698}
3699
3700static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
3701{
3702 struct uic_command uic_cmd = {0};
3703 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003704 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003705
Kiwoong Kimee32c902016-11-10 21:17:43 +09003706 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3707
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003708 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3709 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003710 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3711 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3712
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303713 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003714 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3715 __func__, ret);
3716 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003717 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003718 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3719 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003720 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3721 hba->ufs_stats.hibern8_exit_cnt++;
3722 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303723
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303724 return ret;
3725}
3726
Yaniv Gardi50646362014-10-23 13:25:13 +03003727 /**
3728 * ufshcd_init_pwr_info - setting the POR (power on reset)
3729 * values in hba power info
3730 * @hba: per-adapter instance
3731 */
3732static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3733{
3734 hba->pwr_info.gear_rx = UFS_PWM_G1;
3735 hba->pwr_info.gear_tx = UFS_PWM_G1;
3736 hba->pwr_info.lane_rx = 1;
3737 hba->pwr_info.lane_tx = 1;
3738 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3739 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3740 hba->pwr_info.hs_rate = 0;
3741}
3742
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303743/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003744 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3745 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303746 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003747static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303748{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003749 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3750
3751 if (hba->max_pwr_info.is_valid)
3752 return 0;
3753
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003754 pwr_info->pwr_tx = FAST_MODE;
3755 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003756 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303757
3758 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003759 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3760 &pwr_info->lane_rx);
3761 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3762 &pwr_info->lane_tx);
3763
3764 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3765 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3766 __func__,
3767 pwr_info->lane_rx,
3768 pwr_info->lane_tx);
3769 return -EINVAL;
3770 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303771
3772 /*
3773 * First, get the maximum gears of HS speed.
3774 * If a zero value, it means there is no HSGEAR capability.
3775 * Then, get the maximum gears of PWM speed.
3776 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003777 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3778 if (!pwr_info->gear_rx) {
3779 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3780 &pwr_info->gear_rx);
3781 if (!pwr_info->gear_rx) {
3782 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3783 __func__, pwr_info->gear_rx);
3784 return -EINVAL;
3785 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003786 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303787 }
3788
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003789 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3790 &pwr_info->gear_tx);
3791 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303792 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003793 &pwr_info->gear_tx);
3794 if (!pwr_info->gear_tx) {
3795 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3796 __func__, pwr_info->gear_tx);
3797 return -EINVAL;
3798 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003799 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003800 }
3801
3802 hba->max_pwr_info.is_valid = true;
3803 return 0;
3804}
3805
3806static int ufshcd_change_power_mode(struct ufs_hba *hba,
3807 struct ufs_pa_layer_attr *pwr_mode)
3808{
3809 int ret;
3810
3811 /* if already configured to the requested pwr_mode */
3812 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
3813 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
3814 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
3815 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
3816 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
3817 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
3818 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
3819 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
3820 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303821 }
3822
3823 /*
3824 * Configure attributes for power mode change with below.
3825 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3826 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3827 * - PA_HSSERIES
3828 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003829 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
3830 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
3831 pwr_mode->lane_rx);
3832 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3833 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303834 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003835 else
3836 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303837
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003838 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
3839 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
3840 pwr_mode->lane_tx);
3841 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
3842 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303843 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003844 else
3845 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303846
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003847 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3848 pwr_mode->pwr_tx == FASTAUTO_MODE ||
3849 pwr_mode->pwr_rx == FAST_MODE ||
3850 pwr_mode->pwr_tx == FAST_MODE)
3851 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
3852 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303853
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003854 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
3855 | pwr_mode->pwr_tx);
3856
3857 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303858 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003859 "%s: power mode change failed %d\n", __func__, ret);
3860 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003861 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
3862 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003863
3864 memcpy(&hba->pwr_info, pwr_mode,
3865 sizeof(struct ufs_pa_layer_attr));
3866 }
3867
3868 return ret;
3869}
3870
3871/**
3872 * ufshcd_config_pwr_mode - configure a new power mode
3873 * @hba: per-adapter instance
3874 * @desired_pwr_mode: desired power configuration
3875 */
3876static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3877 struct ufs_pa_layer_attr *desired_pwr_mode)
3878{
3879 struct ufs_pa_layer_attr final_params = { 0 };
3880 int ret;
3881
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003882 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3883 desired_pwr_mode, &final_params);
3884
3885 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003886 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3887
3888 ret = ufshcd_change_power_mode(hba, &final_params);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08003889 if (!ret)
3890 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303891
3892 return ret;
3893}
3894
3895/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303896 * ufshcd_complete_dev_init() - checks device readiness
3897 * hba: per-adapter instance
3898 *
3899 * Set fDeviceInit flag and poll until device toggles it.
3900 */
3901static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3902{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003903 int i;
3904 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303905 bool flag_res = 1;
3906
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003907 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3908 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303909 if (err) {
3910 dev_err(hba->dev,
3911 "%s setting fDeviceInit flag failed with error %d\n",
3912 __func__, err);
3913 goto out;
3914 }
3915
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003916 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3917 for (i = 0; i < 1000 && !err && flag_res; i++)
3918 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3919 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3920
Dolev Raviv68078d52013-07-30 00:35:58 +05303921 if (err)
3922 dev_err(hba->dev,
3923 "%s reading fDeviceInit flag failed with error %d\n",
3924 __func__, err);
3925 else if (flag_res)
3926 dev_err(hba->dev,
3927 "%s fDeviceInit was not cleared by the device\n",
3928 __func__);
3929
3930out:
3931 return err;
3932}
3933
3934/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303935 * ufshcd_make_hba_operational - Make UFS controller operational
3936 * @hba: per adapter instance
3937 *
3938 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003939 * 1. Enable required interrupts
3940 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003941 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003942 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303943 *
3944 * Returns 0 on success, non-zero value on failure
3945 */
3946static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3947{
3948 int err = 0;
3949 u32 reg;
3950
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303951 /* Enable required interrupts */
3952 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3953
3954 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003955 if (ufshcd_is_intr_aggr_allowed(hba))
3956 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3957 else
3958 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303959
3960 /* Configure UTRL and UTMRL base address registers */
3961 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3962 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3963 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3964 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3965 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3966 REG_UTP_TASK_REQ_LIST_BASE_L);
3967 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3968 REG_UTP_TASK_REQ_LIST_BASE_H);
3969
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303970 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003971 * Make sure base address and interrupt setup are updated before
3972 * enabling the run/stop registers below.
3973 */
3974 wmb();
3975
3976 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303977 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303978 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003979 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303980 if (!(ufshcd_get_lists_status(reg))) {
3981 ufshcd_enable_run_stop_reg(hba);
3982 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303983 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303984 "Host controller not ready to process requests");
3985 err = -EIO;
3986 goto out;
3987 }
3988
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303989out:
3990 return err;
3991}
3992
3993/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003994 * ufshcd_hba_stop - Send controller to reset state
3995 * @hba: per adapter instance
3996 * @can_sleep: perform sleep or just spin
3997 */
3998static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3999{
4000 int err;
4001
4002 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
4003 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
4004 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
4005 10, 1, can_sleep);
4006 if (err)
4007 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
4008}
4009
4010/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304011 * ufshcd_hba_enable - initialize the controller
4012 * @hba: per adapter instance
4013 *
4014 * The controller resets itself and controller firmware initialization
4015 * sequence kicks off. When controller is ready it will set
4016 * the Host Controller Enable bit to 1.
4017 *
4018 * Returns 0 on success, non-zero value on failure
4019 */
4020static int ufshcd_hba_enable(struct ufs_hba *hba)
4021{
4022 int retry;
4023
4024 /*
4025 * msleep of 1 and 5 used in this function might result in msleep(20),
4026 * but it was necessary to send the UFS FPGA to reset mode during
4027 * development and testing of this driver. msleep can be changed to
4028 * mdelay and retry count can be reduced based on the controller.
4029 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004030 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304031 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004032 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304033
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004034 /* UniPro link is disabled at this point */
4035 ufshcd_set_link_off(hba);
4036
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004037 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004038
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304039 /* start controller initialization sequence */
4040 ufshcd_hba_start(hba);
4041
4042 /*
4043 * To initialize a UFS host controller HCE bit must be set to 1.
4044 * During initialization the HCE bit value changes from 1->0->1.
4045 * When the host controller completes initialization sequence
4046 * it sets the value of HCE bit to 1. The same HCE bit is read back
4047 * to check if the controller has completed initialization sequence.
4048 * So without this delay the value HCE = 1, set in the previous
4049 * instruction might be read back.
4050 * This delay can be changed based on the controller.
4051 */
4052 msleep(1);
4053
4054 /* wait for the host controller to complete initialization */
4055 retry = 10;
4056 while (ufshcd_is_hba_active(hba)) {
4057 if (retry) {
4058 retry--;
4059 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304060 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304061 "Controller enable failed\n");
4062 return -EIO;
4063 }
4064 msleep(5);
4065 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004066
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004067 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004068 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004069
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004070 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004071
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304072 return 0;
4073}
4074
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004075static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4076{
4077 int tx_lanes, i, err = 0;
4078
4079 if (!peer)
4080 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4081 &tx_lanes);
4082 else
4083 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4084 &tx_lanes);
4085 for (i = 0; i < tx_lanes; i++) {
4086 if (!peer)
4087 err = ufshcd_dme_set(hba,
4088 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4089 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4090 0);
4091 else
4092 err = ufshcd_dme_peer_set(hba,
4093 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4094 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4095 0);
4096 if (err) {
4097 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4098 __func__, peer, i, err);
4099 break;
4100 }
4101 }
4102
4103 return err;
4104}
4105
4106static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4107{
4108 return ufshcd_disable_tx_lcc(hba, true);
4109}
4110
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304111/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304112 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304113 * @hba: per adapter instance
4114 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304115 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304116 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304117static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304118{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304119 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004120 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004121 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304122
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004123 /*
4124 * If UFS device isn't active then we will have to issue link startup
4125 * 2 times to make sure the device state move to active.
4126 */
4127 if (!ufshcd_is_ufs_dev_active(hba))
4128 link_startup_again = true;
4129
4130link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004131 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004132 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304133
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004134 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004135
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004136 /* check if device is detected by inter-connect layer */
4137 if (!ret && !ufshcd_is_device_present(hba)) {
4138 dev_err(hba->dev, "%s: Device not present\n", __func__);
4139 ret = -ENXIO;
4140 goto out;
4141 }
4142
4143 /*
4144 * DME link lost indication is only received when link is up,
4145 * but we can't be sure if the link is up until link startup
4146 * succeeds. So reset the local Uni-Pro and try again.
4147 */
4148 if (ret && ufshcd_hba_enable(hba))
4149 goto out;
4150 } while (ret && retries--);
4151
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304152 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004153 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304154 goto out;
4155
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004156 if (link_startup_again) {
4157 link_startup_again = false;
4158 retries = DME_LINKSTARTUP_RETRIES;
4159 goto link_startup;
4160 }
4161
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004162 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4163 ufshcd_init_pwr_info(hba);
4164 ufshcd_print_pwr_info(hba);
4165
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004166 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4167 ret = ufshcd_disable_device_tx_lcc(hba);
4168 if (ret)
4169 goto out;
4170 }
4171
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004172 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004173 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4174 if (ret)
4175 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004176
4177 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304178out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004179 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304180 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004181 ufshcd_print_host_state(hba);
4182 ufshcd_print_pwr_info(hba);
4183 ufshcd_print_host_regs(hba);
4184 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304185 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304186}
4187
4188/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304189 * ufshcd_verify_dev_init() - Verify device initialization
4190 * @hba: per-adapter instance
4191 *
4192 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4193 * device Transport Protocol (UTP) layer is ready after a reset.
4194 * If the UTP layer at the device side is not initialized, it may
4195 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4196 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4197 */
4198static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4199{
4200 int err = 0;
4201 int retries;
4202
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004203 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304204 mutex_lock(&hba->dev_cmd.lock);
4205 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4206 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4207 NOP_OUT_TIMEOUT);
4208
4209 if (!err || err == -ETIMEDOUT)
4210 break;
4211
4212 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4213 }
4214 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004215 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304216
4217 if (err)
4218 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4219 return err;
4220}
4221
4222/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004223 * ufshcd_set_queue_depth - set lun queue depth
4224 * @sdev: pointer to SCSI device
4225 *
4226 * Read bLUQueueDepth value and activate scsi tagged command
4227 * queueing. For WLUN, queue depth is set to 1. For best-effort
4228 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4229 * value that host can queue.
4230 */
4231static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4232{
4233 int ret = 0;
4234 u8 lun_qdepth;
4235 struct ufs_hba *hba;
4236
4237 hba = shost_priv(sdev->host);
4238
4239 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004240 ret = ufshcd_read_unit_desc_param(hba,
4241 ufshcd_scsi_to_upiu_lun(sdev->lun),
4242 UNIT_DESC_PARAM_LU_Q_DEPTH,
4243 &lun_qdepth,
4244 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004245
4246 /* Some WLUN doesn't support unit descriptor */
4247 if (ret == -EOPNOTSUPP)
4248 lun_qdepth = 1;
4249 else if (!lun_qdepth)
4250 /* eventually, we can figure out the real queue depth */
4251 lun_qdepth = hba->nutrs;
4252 else
4253 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4254
4255 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4256 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004257 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004258}
4259
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004260/*
4261 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4262 * @hba: per-adapter instance
4263 * @lun: UFS device lun id
4264 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4265 *
4266 * Returns 0 in case of success and b_lu_write_protect status would be returned
4267 * @b_lu_write_protect parameter.
4268 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4269 * Returns -EINVAL in case of invalid parameters passed to this function.
4270 */
4271static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4272 u8 lun,
4273 u8 *b_lu_write_protect)
4274{
4275 int ret;
4276
4277 if (!b_lu_write_protect)
4278 ret = -EINVAL;
4279 /*
4280 * According to UFS device spec, RPMB LU can't be write
4281 * protected so skip reading bLUWriteProtect parameter for
4282 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4283 */
4284 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4285 ret = -ENOTSUPP;
4286 else
4287 ret = ufshcd_read_unit_desc_param(hba,
4288 lun,
4289 UNIT_DESC_PARAM_LU_WR_PROTECT,
4290 b_lu_write_protect,
4291 sizeof(*b_lu_write_protect));
4292 return ret;
4293}
4294
4295/**
4296 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4297 * status
4298 * @hba: per-adapter instance
4299 * @sdev: pointer to SCSI device
4300 *
4301 */
4302static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4303 struct scsi_device *sdev)
4304{
4305 if (hba->dev_info.f_power_on_wp_en &&
4306 !hba->dev_info.is_lu_power_on_wp) {
4307 u8 b_lu_write_protect;
4308
4309 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4310 &b_lu_write_protect) &&
4311 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4312 hba->dev_info.is_lu_power_on_wp = true;
4313 }
4314}
4315
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004316/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304317 * ufshcd_slave_alloc - handle initial SCSI device configurations
4318 * @sdev: pointer to SCSI device
4319 *
4320 * Returns success
4321 */
4322static int ufshcd_slave_alloc(struct scsi_device *sdev)
4323{
4324 struct ufs_hba *hba;
4325
4326 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304327
4328 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4329 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304330
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304331 /* allow SCSI layer to restart the device in case of errors */
4332 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004333
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004334 /* REPORT SUPPORTED OPERATION CODES is not supported */
4335 sdev->no_report_opcodes = 1;
4336
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004337
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004338 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004339
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004340 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4341
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004342 return 0;
4343}
4344
4345/**
4346 * ufshcd_change_queue_depth - change queue depth
4347 * @sdev: pointer to SCSI device
4348 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004349 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004350 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004351 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004352static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004353{
4354 struct ufs_hba *hba = shost_priv(sdev->host);
4355
4356 if (depth > hba->nutrs)
4357 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004358 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304359}
4360
4361/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004362 * ufshcd_slave_configure - adjust SCSI device configurations
4363 * @sdev: pointer to SCSI device
4364 */
4365static int ufshcd_slave_configure(struct scsi_device *sdev)
4366{
4367 struct request_queue *q = sdev->request_queue;
4368
4369 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4370 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
4371
4372 return 0;
4373}
4374
4375/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304376 * ufshcd_slave_destroy - remove SCSI device configurations
4377 * @sdev: pointer to SCSI device
4378 */
4379static void ufshcd_slave_destroy(struct scsi_device *sdev)
4380{
4381 struct ufs_hba *hba;
4382
4383 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004384 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004385 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4386 unsigned long flags;
4387
4388 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004389 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004390 spin_unlock_irqrestore(hba->host->host_lock, flags);
4391 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304392}
4393
4394/**
4395 * ufshcd_task_req_compl - handle task management request completion
4396 * @hba: per adapter instance
4397 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304398 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304399 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304400 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304401 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304402static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304403{
4404 struct utp_task_req_desc *task_req_descp;
4405 struct utp_upiu_task_rsp *task_rsp_upiup;
4406 unsigned long flags;
4407 int ocs_value;
4408 int task_result;
4409
4410 spin_lock_irqsave(hba->host->host_lock, flags);
4411
4412 /* Clear completed tasks from outstanding_tasks */
4413 __clear_bit(index, &hba->outstanding_tasks);
4414
4415 task_req_descp = hba->utmrdl_base_addr;
4416 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
4417
4418 if (ocs_value == OCS_SUCCESS) {
4419 task_rsp_upiup = (struct utp_upiu_task_rsp *)
4420 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09004421 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
4422 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304423 if (resp)
4424 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304425 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304426 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
4427 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304428 }
4429 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304430
4431 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432}
4433
4434/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304435 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4436 * @lrb: pointer to local reference block of completed command
4437 * @scsi_status: SCSI command status
4438 *
4439 * Returns value base on SCSI command status
4440 */
4441static inline int
4442ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4443{
4444 int result = 0;
4445
4446 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304447 case SAM_STAT_CHECK_CONDITION:
4448 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304449 case SAM_STAT_GOOD:
4450 result |= DID_OK << 16 |
4451 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304452 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304453 break;
4454 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304455 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304456 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304457 ufshcd_copy_sense_data(lrbp);
4458 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304459 break;
4460 default:
4461 result |= DID_ERROR << 16;
4462 break;
4463 } /* end of switch */
4464
4465 return result;
4466}
4467
4468/**
4469 * ufshcd_transfer_rsp_status - Get overall status of the response
4470 * @hba: per adapter instance
4471 * @lrb: pointer to local reference block of completed command
4472 *
4473 * Returns result of the command to notify SCSI midlayer
4474 */
4475static inline int
4476ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4477{
4478 int result = 0;
4479 int scsi_status;
4480 int ocs;
4481
4482 /* overall command status of utrd */
4483 ocs = ufshcd_get_tr_ocs(lrbp);
4484
4485 switch (ocs) {
4486 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304487 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004488 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304489 switch (result) {
4490 case UPIU_TRANSACTION_RESPONSE:
4491 /*
4492 * get the response UPIU result to extract
4493 * the SCSI command status
4494 */
4495 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4496
4497 /*
4498 * get the result based on SCSI status response
4499 * to notify the SCSI midlayer of the command status
4500 */
4501 scsi_status = result & MASK_SCSI_STATUS;
4502 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304503
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004504 /*
4505 * Currently we are only supporting BKOPs exception
4506 * events hence we can ignore BKOPs exception event
4507 * during power management callbacks. BKOPs exception
4508 * event is not expected to be raised in runtime suspend
4509 * callback as it allows the urgent bkops.
4510 * During system suspend, we are anyway forcefully
4511 * disabling the bkops and if urgent bkops is needed
4512 * it will be enabled on system resume. Long term
4513 * solution could be to abort the system suspend if
4514 * UFS device needs urgent BKOPs.
4515 */
4516 if (!hba->pm_op_in_progress &&
4517 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304518 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304519 break;
4520 case UPIU_TRANSACTION_REJECT_UPIU:
4521 /* TODO: handle Reject UPIU Response */
4522 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304523 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304524 "Reject UPIU not fully implemented\n");
4525 break;
4526 default:
4527 result = DID_ERROR << 16;
4528 dev_err(hba->dev,
4529 "Unexpected request response code = %x\n",
4530 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304531 break;
4532 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304533 break;
4534 case OCS_ABORTED:
4535 result |= DID_ABORT << 16;
4536 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304537 case OCS_INVALID_COMMAND_STATUS:
4538 result |= DID_REQUEUE << 16;
4539 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304540 case OCS_INVALID_CMD_TABLE_ATTR:
4541 case OCS_INVALID_PRDT_ATTR:
4542 case OCS_MISMATCH_DATA_BUF_SIZE:
4543 case OCS_MISMATCH_RESP_UPIU_SIZE:
4544 case OCS_PEER_COMM_FAILURE:
4545 case OCS_FATAL_ERROR:
4546 default:
4547 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304548 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004549 "OCS error from controller = %x for tag %d\n",
4550 ocs, lrbp->task_tag);
4551 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004552 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304553 break;
4554 } /* end of switch */
4555
Dolev Raviv66cc8202016-12-22 18:39:42 -08004556 if (host_byte(result) != DID_OK)
4557 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304558 return result;
4559}
4560
4561/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304562 * ufshcd_uic_cmd_compl - handle completion of uic command
4563 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304564 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304565 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304566static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304567{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304568 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304569 hba->active_uic_cmd->argument2 |=
4570 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304571 hba->active_uic_cmd->argument3 =
4572 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304573 complete(&hba->active_uic_cmd->done);
4574 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304575
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004576 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
4577 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304578}
4579
4580/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004581 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304582 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004583 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304584 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004585static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4586 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304587{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304588 struct ufshcd_lrb *lrbp;
4589 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304590 int result;
4591 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004592
Dolev Ravive9d501b2014-07-01 12:22:37 +03004593 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4594 lrbp = &hba->lrb[index];
4595 cmd = lrbp->cmd;
4596 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004597 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004598 result = ufshcd_transfer_rsp_status(hba, lrbp);
4599 scsi_dma_unmap(cmd);
4600 cmd->result = result;
4601 /* Mark completed command as NULL in LRB */
4602 lrbp->cmd = NULL;
4603 clear_bit_unlock(index, &hba->lrb_in_use);
4604 /* Do not touch lrbp after scsi done */
4605 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004606 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004607 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4608 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004609 if (hba->dev_cmd.complete) {
4610 ufshcd_add_command_trace(hba, index,
4611 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004612 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004613 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004614 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004615 if (ufshcd_is_clkscaling_supported(hba))
4616 hba->clk_scaling.active_reqs--;
Zang Leigang09017182017-09-27 10:06:06 +08004617
4618 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03004619 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304620
4621 /* clear corresponding bits of completed commands */
4622 hba->outstanding_reqs ^= completed_reqs;
4623
Sahitya Tummala856b3482014-09-25 15:32:34 +03004624 ufshcd_clk_scaling_update_busy(hba);
4625
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304626 /* we might have free'd some tags above */
4627 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304628}
4629
4630/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004631 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4632 * @hba: per adapter instance
4633 */
4634static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
4635{
4636 unsigned long completed_reqs;
4637 u32 tr_doorbell;
4638
4639 /* Resetting interrupt aggregation counters first and reading the
4640 * DOOR_BELL afterward allows us to handle all the completed requests.
4641 * In order to prevent other interrupts starvation the DB is read once
4642 * after reset. The down side of this solution is the possibility of
4643 * false interrupt if device completes another request after resetting
4644 * aggregation and before reading the DB.
4645 */
4646 if (ufshcd_is_intr_aggr_allowed(hba))
4647 ufshcd_reset_intr_aggr(hba);
4648
4649 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4650 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4651
4652 __ufshcd_transfer_req_compl(hba, completed_reqs);
4653}
4654
4655/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304656 * ufshcd_disable_ee - disable exception event
4657 * @hba: per-adapter instance
4658 * @mask: exception event to disable
4659 *
4660 * Disables exception event in the device so that the EVENT_ALERT
4661 * bit is not set.
4662 *
4663 * Returns zero on success, non-zero error value on failure.
4664 */
4665static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4666{
4667 int err = 0;
4668 u32 val;
4669
4670 if (!(hba->ee_ctrl_mask & mask))
4671 goto out;
4672
4673 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004674 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004675 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304676 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4677 if (!err)
4678 hba->ee_ctrl_mask &= ~mask;
4679out:
4680 return err;
4681}
4682
4683/**
4684 * ufshcd_enable_ee - enable exception event
4685 * @hba: per-adapter instance
4686 * @mask: exception event to enable
4687 *
4688 * Enable corresponding exception event in the device to allow
4689 * device to alert host in critical scenarios.
4690 *
4691 * Returns zero on success, non-zero error value on failure.
4692 */
4693static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4694{
4695 int err = 0;
4696 u32 val;
4697
4698 if (hba->ee_ctrl_mask & mask)
4699 goto out;
4700
4701 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004702 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004703 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304704 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4705 if (!err)
4706 hba->ee_ctrl_mask |= mask;
4707out:
4708 return err;
4709}
4710
4711/**
4712 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4713 * @hba: per-adapter instance
4714 *
4715 * Allow device to manage background operations on its own. Enabling
4716 * this might lead to inconsistent latencies during normal data transfers
4717 * as the device is allowed to manage its own way of handling background
4718 * operations.
4719 *
4720 * Returns zero on success, non-zero on failure.
4721 */
4722static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4723{
4724 int err = 0;
4725
4726 if (hba->auto_bkops_enabled)
4727 goto out;
4728
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004729 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304730 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4731 if (err) {
4732 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4733 __func__, err);
4734 goto out;
4735 }
4736
4737 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004738 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304739
4740 /* No need of URGENT_BKOPS exception from the device */
4741 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4742 if (err)
4743 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4744 __func__, err);
4745out:
4746 return err;
4747}
4748
4749/**
4750 * ufshcd_disable_auto_bkops - block device in doing background operations
4751 * @hba: per-adapter instance
4752 *
4753 * Disabling background operations improves command response latency but
4754 * has drawback of device moving into critical state where the device is
4755 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4756 * host is idle so that BKOPS are managed effectively without any negative
4757 * impacts.
4758 *
4759 * Returns zero on success, non-zero on failure.
4760 */
4761static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
4762{
4763 int err = 0;
4764
4765 if (!hba->auto_bkops_enabled)
4766 goto out;
4767
4768 /*
4769 * If host assisted BKOPs is to be enabled, make sure
4770 * urgent bkops exception is allowed.
4771 */
4772 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
4773 if (err) {
4774 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
4775 __func__, err);
4776 goto out;
4777 }
4778
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004779 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304780 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4781 if (err) {
4782 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
4783 __func__, err);
4784 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4785 goto out;
4786 }
4787
4788 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004789 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304790out:
4791 return err;
4792}
4793
4794/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004795 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304796 * @hba: per adapter instance
4797 *
4798 * After a device reset the device may toggle the BKOPS_EN flag
4799 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004800 * as well. This function would change the auto-bkops state based on
4801 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304802 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004803static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304804{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004805 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
4806 hba->auto_bkops_enabled = false;
4807 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
4808 ufshcd_enable_auto_bkops(hba);
4809 } else {
4810 hba->auto_bkops_enabled = true;
4811 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
4812 ufshcd_disable_auto_bkops(hba);
4813 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304814}
4815
4816static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
4817{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004818 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304819 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
4820}
4821
4822/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004823 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4824 * @hba: per-adapter instance
4825 * @status: bkops_status value
4826 *
4827 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4828 * flag in the device to permit background operations if the device
4829 * bkops_status is greater than or equal to "status" argument passed to
4830 * this function, disable otherwise.
4831 *
4832 * Returns 0 for success, non-zero in case of failure.
4833 *
4834 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4835 * to know whether auto bkops is enabled or disabled after this function
4836 * returns control to it.
4837 */
4838static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
4839 enum bkops_status status)
4840{
4841 int err;
4842 u32 curr_status = 0;
4843
4844 err = ufshcd_get_bkops_status(hba, &curr_status);
4845 if (err) {
4846 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4847 __func__, err);
4848 goto out;
4849 } else if (curr_status > BKOPS_STATUS_MAX) {
4850 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
4851 __func__, curr_status);
4852 err = -EINVAL;
4853 goto out;
4854 }
4855
4856 if (curr_status >= status)
4857 err = ufshcd_enable_auto_bkops(hba);
4858 else
4859 err = ufshcd_disable_auto_bkops(hba);
4860out:
4861 return err;
4862}
4863
4864/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304865 * ufshcd_urgent_bkops - handle urgent bkops exception event
4866 * @hba: per-adapter instance
4867 *
4868 * Enable fBackgroundOpsEn flag in the device to permit background
4869 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004870 *
4871 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4872 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304873 */
4874static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4875{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004876 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304877}
4878
4879static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4880{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004881 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304882 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4883}
4884
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004885static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4886{
4887 int err;
4888 u32 curr_status = 0;
4889
4890 if (hba->is_urgent_bkops_lvl_checked)
4891 goto enable_auto_bkops;
4892
4893 err = ufshcd_get_bkops_status(hba, &curr_status);
4894 if (err) {
4895 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4896 __func__, err);
4897 goto out;
4898 }
4899
4900 /*
4901 * We are seeing that some devices are raising the urgent bkops
4902 * exception events even when BKOPS status doesn't indicate performace
4903 * impacted or critical. Handle these device by determining their urgent
4904 * bkops status at runtime.
4905 */
4906 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4907 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4908 __func__, curr_status);
4909 /* update the current status as the urgent bkops level */
4910 hba->urgent_bkops_lvl = curr_status;
4911 hba->is_urgent_bkops_lvl_checked = true;
4912 }
4913
4914enable_auto_bkops:
4915 err = ufshcd_enable_auto_bkops(hba);
4916out:
4917 if (err < 0)
4918 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4919 __func__, err);
4920}
4921
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304922/**
4923 * ufshcd_exception_event_handler - handle exceptions raised by device
4924 * @work: pointer to work data
4925 *
4926 * Read bExceptionEventStatus attribute from the device and handle the
4927 * exception event accordingly.
4928 */
4929static void ufshcd_exception_event_handler(struct work_struct *work)
4930{
4931 struct ufs_hba *hba;
4932 int err;
4933 u32 status = 0;
4934 hba = container_of(work, struct ufs_hba, eeh_work);
4935
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304936 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304937 err = ufshcd_get_ee_status(hba, &status);
4938 if (err) {
4939 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4940 __func__, err);
4941 goto out;
4942 }
4943
4944 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004945
4946 if (status & MASK_EE_URGENT_BKOPS)
4947 ufshcd_bkops_exception_event_handler(hba);
4948
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304949out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304950 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304951 return;
4952}
4953
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004954/* Complete requests that have door-bell cleared */
4955static void ufshcd_complete_requests(struct ufs_hba *hba)
4956{
4957 ufshcd_transfer_req_compl(hba);
4958 ufshcd_tmc_handler(hba);
4959}
4960
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304961/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004962 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4963 * to recover from the DL NAC errors or not.
4964 * @hba: per-adapter instance
4965 *
4966 * Returns true if error handling is required, false otherwise
4967 */
4968static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4969{
4970 unsigned long flags;
4971 bool err_handling = true;
4972
4973 spin_lock_irqsave(hba->host->host_lock, flags);
4974 /*
4975 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4976 * device fatal error and/or DL NAC & REPLAY timeout errors.
4977 */
4978 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4979 goto out;
4980
4981 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4982 ((hba->saved_err & UIC_ERROR) &&
4983 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4984 goto out;
4985
4986 if ((hba->saved_err & UIC_ERROR) &&
4987 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4988 int err;
4989 /*
4990 * wait for 50ms to see if we can get any other errors or not.
4991 */
4992 spin_unlock_irqrestore(hba->host->host_lock, flags);
4993 msleep(50);
4994 spin_lock_irqsave(hba->host->host_lock, flags);
4995
4996 /*
4997 * now check if we have got any other severe errors other than
4998 * DL NAC error?
4999 */
5000 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5001 ((hba->saved_err & UIC_ERROR) &&
5002 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
5003 goto out;
5004
5005 /*
5006 * As DL NAC is the only error received so far, send out NOP
5007 * command to confirm if link is still active or not.
5008 * - If we don't get any response then do error recovery.
5009 * - If we get response then clear the DL NAC error bit.
5010 */
5011
5012 spin_unlock_irqrestore(hba->host->host_lock, flags);
5013 err = ufshcd_verify_dev_init(hba);
5014 spin_lock_irqsave(hba->host->host_lock, flags);
5015
5016 if (err)
5017 goto out;
5018
5019 /* Link seems to be alive hence ignore the DL NAC errors */
5020 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5021 hba->saved_err &= ~UIC_ERROR;
5022 /* clear NAC error */
5023 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5024 if (!hba->saved_uic_err) {
5025 err_handling = false;
5026 goto out;
5027 }
5028 }
5029out:
5030 spin_unlock_irqrestore(hba->host->host_lock, flags);
5031 return err_handling;
5032}
5033
5034/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305035 * ufshcd_err_handler - handle UFS errors that require s/w attention
5036 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305037 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305038static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305039{
5040 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305041 unsigned long flags;
5042 u32 err_xfer = 0;
5043 u32 err_tm = 0;
5044 int err = 0;
5045 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005046 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305047
5048 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305049
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305050 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005051 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305052
5053 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005054 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305055 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305056
5057 hba->ufshcd_state = UFSHCD_STATE_RESET;
5058 ufshcd_set_eh_in_progress(hba);
5059
5060 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005061 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005062
5063 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5064 bool ret;
5065
5066 spin_unlock_irqrestore(hba->host->host_lock, flags);
5067 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5068 ret = ufshcd_quirk_dl_nac_errors(hba);
5069 spin_lock_irqsave(hba->host->host_lock, flags);
5070 if (!ret)
5071 goto skip_err_handling;
5072 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005073 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5074 ((hba->saved_err & UIC_ERROR) &&
5075 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5076 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5077 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5078 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305079
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005080 /*
5081 * if host reset is required then skip clearing the pending
5082 * transfers forcefully because they will automatically get
5083 * cleared after link startup.
5084 */
5085 if (needs_reset)
5086 goto skip_pending_xfer_clear;
5087
5088 /* release lock as clear command might sleep */
5089 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305090 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005091 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5092 if (ufshcd_clear_cmd(hba, tag)) {
5093 err_xfer = true;
5094 goto lock_skip_pending_xfer_clear;
5095 }
5096 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305097
5098 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005099 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5100 if (ufshcd_clear_tm_cmd(hba, tag)) {
5101 err_tm = true;
5102 goto lock_skip_pending_xfer_clear;
5103 }
5104 }
5105
5106lock_skip_pending_xfer_clear:
5107 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305108
5109 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005110 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305111
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005112 if (err_xfer || err_tm)
5113 needs_reset = true;
5114
5115skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305116 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005117 if (needs_reset) {
5118 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5119
5120 /*
5121 * ufshcd_reset_and_restore() does the link reinitialization
5122 * which will need atleast one empty doorbell slot to send the
5123 * device management commands (NOP and query commands).
5124 * If there is no slot empty at this moment then free up last
5125 * slot forcefully.
5126 */
5127 if (hba->outstanding_reqs == max_doorbells)
5128 __ufshcd_transfer_req_compl(hba,
5129 (1UL << (hba->nutrs - 1)));
5130
5131 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305132 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005133 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305134 if (err) {
5135 dev_err(hba->dev, "%s: reset and restore failed\n",
5136 __func__);
5137 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5138 }
5139 /*
5140 * Inform scsi mid-layer that we did reset and allow to handle
5141 * Unit Attention properly.
5142 */
5143 scsi_report_bus_reset(hba->host, 0);
5144 hba->saved_err = 0;
5145 hba->saved_uic_err = 0;
5146 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005147
Yaniv Gardi583fa622016-03-10 17:37:13 +02005148skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005149 if (!needs_reset) {
5150 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5151 if (hba->saved_err || hba->saved_uic_err)
5152 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5153 __func__, hba->saved_err, hba->saved_uic_err);
5154 }
5155
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305156 ufshcd_clear_eh_in_progress(hba);
5157
5158out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005159 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305160 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005161 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305162 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305163}
5164
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005165static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
5166 u32 reg)
5167{
5168 reg_hist->reg[reg_hist->pos] = reg;
5169 reg_hist->tstamp[reg_hist->pos] = ktime_get();
5170 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
5171}
5172
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305173/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305174 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5175 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305176 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305177static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305178{
5179 u32 reg;
5180
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005181 /* PHY layer lane error */
5182 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5183 /* Ignore LINERESET indication, as this is not an error */
5184 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005185 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005186 /*
5187 * To know whether this error is fatal or not, DB timeout
5188 * must be checked but this error is handled separately.
5189 */
5190 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005191 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
5192 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005193
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305194 /* PA_INIT_ERROR is fatal and needs UIC reset */
5195 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005196 if (reg)
5197 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
5198
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305199 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5200 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005201 else if (hba->dev_quirks &
5202 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5203 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5204 hba->uic_error |=
5205 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5206 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5207 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5208 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305209
5210 /* UIC NL/TL/DME errors needs software retry */
5211 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005212 if (reg) {
5213 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305214 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005215 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305216
5217 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005218 if (reg) {
5219 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305220 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005221 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305222
5223 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005224 if (reg) {
5225 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305226 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005227 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305228
5229 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5230 __func__, hba->uic_error);
5231}
5232
5233/**
5234 * ufshcd_check_errors - Check for errors that need s/w attention
5235 * @hba: per-adapter instance
5236 */
5237static void ufshcd_check_errors(struct ufs_hba *hba)
5238{
5239 bool queue_eh_work = false;
5240
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305241 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305242 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305243
5244 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305245 hba->uic_error = 0;
5246 ufshcd_update_uic_error(hba);
5247 if (hba->uic_error)
5248 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305249 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305250
5251 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005252 /*
5253 * update the transfer error masks to sticky bits, let's do this
5254 * irrespective of current ufshcd_state.
5255 */
5256 hba->saved_err |= hba->errors;
5257 hba->saved_uic_err |= hba->uic_error;
5258
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305259 /* handle fatal errors only when link is functional */
5260 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5261 /* block commands from scsi mid-layer */
5262 scsi_block_requests(hba->host);
5263
Zang Leigang141f8162016-11-16 11:29:37 +08005264 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005265
5266 /* dump controller state before resetting */
5267 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5268 bool pr_prdt = !!(hba->saved_err &
5269 SYSTEM_BUS_FATAL_ERROR);
5270
5271 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5272 __func__, hba->saved_err,
5273 hba->saved_uic_err);
5274
5275 ufshcd_print_host_regs(hba);
5276 ufshcd_print_pwr_info(hba);
5277 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5278 ufshcd_print_trs(hba, hba->outstanding_reqs,
5279 pr_prdt);
5280 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305281 schedule_work(&hba->eh_work);
5282 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305283 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305284 /*
5285 * if (!queue_eh_work) -
5286 * Other errors are either non-fatal where host recovers
5287 * itself without s/w intervention or errors that will be
5288 * handled by the SCSI core layer.
5289 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305290}
5291
5292/**
5293 * ufshcd_tmc_handler - handle task management function completion
5294 * @hba: per adapter instance
5295 */
5296static void ufshcd_tmc_handler(struct ufs_hba *hba)
5297{
5298 u32 tm_doorbell;
5299
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305300 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305301 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305302 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305303}
5304
5305/**
5306 * ufshcd_sl_intr - Interrupt service routine
5307 * @hba: per adapter instance
5308 * @intr_status: contains interrupts generated by the controller
5309 */
5310static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
5311{
5312 hba->errors = UFSHCD_ERROR_MASK & intr_status;
5313 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305314 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305315
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305316 if (intr_status & UFSHCD_UIC_MASK)
5317 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305318
5319 if (intr_status & UTP_TASK_REQ_COMPL)
5320 ufshcd_tmc_handler(hba);
5321
5322 if (intr_status & UTP_TRANSFER_REQ_COMPL)
5323 ufshcd_transfer_req_compl(hba);
5324}
5325
5326/**
5327 * ufshcd_intr - Main interrupt service routine
5328 * @irq: irq number
5329 * @__hba: pointer to adapter instance
5330 *
5331 * Returns IRQ_HANDLED - If interrupt is valid
5332 * IRQ_NONE - If invalid interrupt
5333 */
5334static irqreturn_t ufshcd_intr(int irq, void *__hba)
5335{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005336 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305337 irqreturn_t retval = IRQ_NONE;
5338 struct ufs_hba *hba = __hba;
5339
5340 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305341 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005342 enabled_intr_status =
5343 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305344
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005345 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05305346 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005347
5348 if (enabled_intr_status) {
5349 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305350 retval = IRQ_HANDLED;
5351 }
5352 spin_unlock(hba->host->host_lock);
5353 return retval;
5354}
5355
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305356static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5357{
5358 int err = 0;
5359 u32 mask = 1 << tag;
5360 unsigned long flags;
5361
5362 if (!test_bit(tag, &hba->outstanding_tasks))
5363 goto out;
5364
5365 spin_lock_irqsave(hba->host->host_lock, flags);
5366 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
5367 spin_unlock_irqrestore(hba->host->host_lock, flags);
5368
5369 /* poll for max. 1 sec to clear door bell register by h/w */
5370 err = ufshcd_wait_for_register(hba,
5371 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005372 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305373out:
5374 return err;
5375}
5376
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305377/**
5378 * ufshcd_issue_tm_cmd - issues task management commands to controller
5379 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305380 * @lun_id: LUN ID to which TM command is sent
5381 * @task_id: task ID to which the TM command is applicable
5382 * @tm_function: task management function opcode
5383 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305384 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305385 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305386 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305387static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5388 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305389{
5390 struct utp_task_req_desc *task_req_descp;
5391 struct utp_upiu_task_req *task_req_upiup;
5392 struct Scsi_Host *host;
5393 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305394 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305395 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305396 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305397
5398 host = hba->host;
5399
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305400 /*
5401 * Get free slot, sleep if slots are unavailable.
5402 * Even though we use wait_event() which sleeps indefinitely,
5403 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5404 */
5405 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005406 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305407
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305408 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305409 task_req_descp = hba->utmrdl_base_addr;
5410 task_req_descp += free_slot;
5411
5412 /* Configure task request descriptor */
5413 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5414 task_req_descp->header.dword_2 =
5415 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5416
5417 /* Configure task request UPIU */
5418 task_req_upiup =
5419 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305420 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305421 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305422 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305423 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305424 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305425 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005426 /*
5427 * The host shall provide the same value for LUN field in the basic
5428 * header and for Input Parameter.
5429 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305430 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
5431 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305432
Kiwoong Kimd2877be2016-11-10 21:16:15 +09005433 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
5434
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305435 /* send command to the controller */
5436 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005437
5438 /* Make sure descriptors are ready before ringing the task doorbell */
5439 wmb();
5440
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305441 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07005442 /* Make sure that doorbell is committed immediately */
5443 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305444
5445 spin_unlock_irqrestore(host->host_lock, flags);
5446
5447 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305448 err = wait_event_timeout(hba->tm_wq,
5449 test_bit(free_slot, &hba->tm_condition),
5450 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305451 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305452 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
5453 __func__, tm_function);
5454 if (ufshcd_clear_tm_cmd(hba, free_slot))
5455 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
5456 __func__, free_slot);
5457 err = -ETIMEDOUT;
5458 } else {
5459 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305460 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305461
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305462 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305463 ufshcd_put_tm_slot(hba, free_slot);
5464 wake_up(&hba->tm_tag_wq);
5465
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005466 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305467 return err;
5468}
5469
5470/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305471 * ufshcd_eh_device_reset_handler - device reset handler registered to
5472 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305473 * @cmd: SCSI command pointer
5474 *
5475 * Returns SUCCESS/FAILED
5476 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305477static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305478{
5479 struct Scsi_Host *host;
5480 struct ufs_hba *hba;
5481 unsigned int tag;
5482 u32 pos;
5483 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305484 u8 resp = 0xF;
5485 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305486 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305487
5488 host = cmd->device->host;
5489 hba = shost_priv(host);
5490 tag = cmd->request->tag;
5491
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305492 lrbp = &hba->lrb[tag];
5493 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
5494 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305495 if (!err)
5496 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305497 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305498 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305499
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305500 /* clear the commands that were pending for corresponding LUN */
5501 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
5502 if (hba->lrb[pos].lun == lrbp->lun) {
5503 err = ufshcd_clear_cmd(hba, pos);
5504 if (err)
5505 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305506 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305507 }
5508 spin_lock_irqsave(host->host_lock, flags);
5509 ufshcd_transfer_req_compl(hba);
5510 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08005511
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305512out:
Gilad Broner7fabb772017-02-03 16:56:50 -08005513 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305514 if (!err) {
5515 err = SUCCESS;
5516 } else {
5517 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
5518 err = FAILED;
5519 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305520 return err;
5521}
5522
Gilad Bronere0b299e2017-02-03 16:56:40 -08005523static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
5524{
5525 struct ufshcd_lrb *lrbp;
5526 int tag;
5527
5528 for_each_set_bit(tag, &bitmap, hba->nutrs) {
5529 lrbp = &hba->lrb[tag];
5530 lrbp->req_abort_skip = true;
5531 }
5532}
5533
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305534/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305535 * ufshcd_abort - abort a specific command
5536 * @cmd: SCSI command pointer
5537 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305538 * Abort the pending command in device by sending UFS_ABORT_TASK task management
5539 * command, and in host controller by clearing the door-bell register. There can
5540 * be race between controller sending the command to the device while abort is
5541 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
5542 * really issued and then try to abort it.
5543 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305544 * Returns SUCCESS/FAILED
5545 */
5546static int ufshcd_abort(struct scsi_cmnd *cmd)
5547{
5548 struct Scsi_Host *host;
5549 struct ufs_hba *hba;
5550 unsigned long flags;
5551 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305552 int err = 0;
5553 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305554 u8 resp = 0xF;
5555 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005556 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305557
5558 host = cmd->device->host;
5559 hba = shost_priv(host);
5560 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08005561 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02005562 if (!ufshcd_valid_tag(hba, tag)) {
5563 dev_err(hba->dev,
5564 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
5565 __func__, tag, cmd, cmd->request);
5566 BUG();
5567 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305568
Dolev Ravive7d38252016-12-22 18:40:07 -08005569 /*
5570 * Task abort to the device W-LUN is illegal. When this command
5571 * will fail, due to spec violation, scsi err handling next step
5572 * will be to send LU reset which, again, is a spec violation.
5573 * To avoid these unnecessary/illegal step we skip to the last error
5574 * handling stage: reset and restore.
5575 */
5576 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
5577 return ufshcd_eh_host_reset_handler(cmd);
5578
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005579 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005580 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02005581 /* If command is already aborted/completed, return SUCCESS */
5582 if (!(test_bit(tag, &hba->outstanding_reqs))) {
5583 dev_err(hba->dev,
5584 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
5585 __func__, tag, hba->outstanding_reqs, reg);
5586 goto out;
5587 }
5588
Dolev Ravive9d501b2014-07-01 12:22:37 +03005589 if (!(reg & (1 << tag))) {
5590 dev_err(hba->dev,
5591 "%s: cmd was completed, but without a notifying intr, tag = %d",
5592 __func__, tag);
5593 }
5594
Dolev Raviv66cc8202016-12-22 18:39:42 -08005595 /* Print Transfer Request of aborted task */
5596 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08005597
Gilad Broner7fabb772017-02-03 16:56:50 -08005598 /*
5599 * Print detailed info about aborted request.
5600 * As more than one request might get aborted at the same time,
5601 * print full information only for the first aborted request in order
5602 * to reduce repeated printouts. For other aborted requests only print
5603 * basic details.
5604 */
5605 scsi_print_command(hba->lrb[tag].cmd);
5606 if (!hba->req_abort_count) {
5607 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08005608 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08005609 ufshcd_print_pwr_info(hba);
5610 ufshcd_print_trs(hba, 1 << tag, true);
5611 } else {
5612 ufshcd_print_trs(hba, 1 << tag, false);
5613 }
5614 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08005615
5616 /* Skip task abort in case previous aborts failed and report failure */
5617 if (lrbp->req_abort_skip) {
5618 err = -EIO;
5619 goto out;
5620 }
5621
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305622 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
5623 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5624 UFS_QUERY_TASK, &resp);
5625 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
5626 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005627 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
5628 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305629 break;
5630 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305631 /*
5632 * cmd not pending in the device, check if it is
5633 * in transition.
5634 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005635 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
5636 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305637 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5638 if (reg & (1 << tag)) {
5639 /* sleep for max. 200us to stabilize */
5640 usleep_range(100, 200);
5641 continue;
5642 }
5643 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005644 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
5645 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305646 goto out;
5647 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005648 dev_err(hba->dev,
5649 "%s: no response from device. tag = %d, err %d\n",
5650 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305651 if (!err)
5652 err = resp; /* service response error */
5653 goto out;
5654 }
5655 }
5656
5657 if (!poll_cnt) {
5658 err = -EBUSY;
5659 goto out;
5660 }
5661
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305662 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5663 UFS_ABORT_TASK, &resp);
5664 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005665 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305666 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005667 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
5668 __func__, tag, err);
5669 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305670 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305671 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305672
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305673 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005674 if (err) {
5675 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
5676 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305677 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005678 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305679
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305680 scsi_dma_unmap(cmd);
5681
5682 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02005683 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305684 hba->lrb[tag].cmd = NULL;
5685 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305686
5687 clear_bit_unlock(tag, &hba->lrb_in_use);
5688 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005689
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305690out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305691 if (!err) {
5692 err = SUCCESS;
5693 } else {
5694 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08005695 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305696 err = FAILED;
5697 }
5698
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005699 /*
5700 * This ufshcd_release() corresponds to the original scsi cmd that got
5701 * aborted here (as we won't get any IRQ for it).
5702 */
5703 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305704 return err;
5705}
5706
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305707/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305708 * ufshcd_host_reset_and_restore - reset and restore host controller
5709 * @hba: per-adapter instance
5710 *
5711 * Note that host controller reset may issue DME_RESET to
5712 * local and remote (device) Uni-Pro stack and the attributes
5713 * are reset to default state.
5714 *
5715 * Returns zero on success, non-zero on failure
5716 */
5717static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
5718{
5719 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305720 unsigned long flags;
5721
5722 /* Reset the host controller */
5723 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02005724 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305725 spin_unlock_irqrestore(hba->host->host_lock, flags);
5726
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08005727 /* scale up clocks to max frequency before full reinitialization */
5728 ufshcd_scale_clks(hba, true);
5729
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305730 err = ufshcd_hba_enable(hba);
5731 if (err)
5732 goto out;
5733
5734 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005735 err = ufshcd_probe_hba(hba);
5736
5737 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305738 err = -EIO;
5739out:
5740 if (err)
5741 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
5742
5743 return err;
5744}
5745
5746/**
5747 * ufshcd_reset_and_restore - reset and re-initialize host/device
5748 * @hba: per-adapter instance
5749 *
5750 * Reset and recover device, host and re-establish link. This
5751 * is helpful to recover the communication in fatal error conditions.
5752 *
5753 * Returns zero on success, non-zero on failure
5754 */
5755static int ufshcd_reset_and_restore(struct ufs_hba *hba)
5756{
5757 int err = 0;
5758 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005759 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305760
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005761 do {
5762 err = ufshcd_host_reset_and_restore(hba);
5763 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305764
5765 /*
5766 * After reset the door-bell might be cleared, complete
5767 * outstanding requests in s/w here.
5768 */
5769 spin_lock_irqsave(hba->host->host_lock, flags);
5770 ufshcd_transfer_req_compl(hba);
5771 ufshcd_tmc_handler(hba);
5772 spin_unlock_irqrestore(hba->host->host_lock, flags);
5773
5774 return err;
5775}
5776
5777/**
5778 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
5779 * @cmd - SCSI command pointer
5780 *
5781 * Returns SUCCESS/FAILED
5782 */
5783static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
5784{
5785 int err;
5786 unsigned long flags;
5787 struct ufs_hba *hba;
5788
5789 hba = shost_priv(cmd->device->host);
5790
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005791 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305792 /*
5793 * Check if there is any race with fatal error handling.
5794 * If so, wait for it to complete. Even though fatal error
5795 * handling does reset and restore in some cases, don't assume
5796 * anything out of it. We are just avoiding race here.
5797 */
5798 do {
5799 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305800 if (!(work_pending(&hba->eh_work) ||
Zang Leigang8dc0da72017-06-24 19:14:32 +08005801 hba->ufshcd_state == UFSHCD_STATE_RESET ||
5802 hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305803 break;
5804 spin_unlock_irqrestore(hba->host->host_lock, flags);
5805 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305806 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305807 } while (1);
5808
5809 hba->ufshcd_state = UFSHCD_STATE_RESET;
5810 ufshcd_set_eh_in_progress(hba);
5811 spin_unlock_irqrestore(hba->host->host_lock, flags);
5812
5813 err = ufshcd_reset_and_restore(hba);
5814
5815 spin_lock_irqsave(hba->host->host_lock, flags);
5816 if (!err) {
5817 err = SUCCESS;
5818 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5819 } else {
5820 err = FAILED;
5821 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5822 }
5823 ufshcd_clear_eh_in_progress(hba);
5824 spin_unlock_irqrestore(hba->host->host_lock, flags);
5825
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005826 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305827 return err;
5828}
5829
5830/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005831 * ufshcd_get_max_icc_level - calculate the ICC level
5832 * @sup_curr_uA: max. current supported by the regulator
5833 * @start_scan: row at the desc table to start scan from
5834 * @buff: power descriptor buffer
5835 *
5836 * Returns calculated max ICC level for specific regulator
5837 */
5838static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
5839{
5840 int i;
5841 int curr_uA;
5842 u16 data;
5843 u16 unit;
5844
5845 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02005846 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005847 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
5848 ATTR_ICC_LVL_UNIT_OFFSET;
5849 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
5850 switch (unit) {
5851 case UFSHCD_NANO_AMP:
5852 curr_uA = curr_uA / 1000;
5853 break;
5854 case UFSHCD_MILI_AMP:
5855 curr_uA = curr_uA * 1000;
5856 break;
5857 case UFSHCD_AMP:
5858 curr_uA = curr_uA * 1000 * 1000;
5859 break;
5860 case UFSHCD_MICRO_AMP:
5861 default:
5862 break;
5863 }
5864 if (sup_curr_uA >= curr_uA)
5865 break;
5866 }
5867 if (i < 0) {
5868 i = 0;
5869 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
5870 }
5871
5872 return (u32)i;
5873}
5874
5875/**
5876 * ufshcd_calc_icc_level - calculate the max ICC level
5877 * In case regulators are not initialized we'll return 0
5878 * @hba: per-adapter instance
5879 * @desc_buf: power descriptor buffer to extract ICC levels from.
5880 * @len: length of desc_buff
5881 *
5882 * Returns calculated ICC level
5883 */
5884static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
5885 u8 *desc_buf, int len)
5886{
5887 u32 icc_level = 0;
5888
5889 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
5890 !hba->vreg_info.vccq2) {
5891 dev_err(hba->dev,
5892 "%s: Regulator capability was not set, actvIccLevel=%d",
5893 __func__, icc_level);
5894 goto out;
5895 }
5896
5897 if (hba->vreg_info.vcc)
5898 icc_level = ufshcd_get_max_icc_level(
5899 hba->vreg_info.vcc->max_uA,
5900 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
5901 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
5902
5903 if (hba->vreg_info.vccq)
5904 icc_level = ufshcd_get_max_icc_level(
5905 hba->vreg_info.vccq->max_uA,
5906 icc_level,
5907 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
5908
5909 if (hba->vreg_info.vccq2)
5910 icc_level = ufshcd_get_max_icc_level(
5911 hba->vreg_info.vccq2->max_uA,
5912 icc_level,
5913 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
5914out:
5915 return icc_level;
5916}
5917
5918static void ufshcd_init_icc_levels(struct ufs_hba *hba)
5919{
5920 int ret;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00005921 int buff_len = hba->desc_size.pwr_desc;
5922 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005923
5924 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
5925 if (ret) {
5926 dev_err(hba->dev,
5927 "%s: Failed reading power descriptor.len = %d ret = %d",
5928 __func__, buff_len, ret);
5929 return;
5930 }
5931
5932 hba->init_prefetch_data.icc_level =
5933 ufshcd_find_max_sup_active_icc_level(hba,
5934 desc_buf, buff_len);
5935 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
5936 __func__, hba->init_prefetch_data.icc_level);
5937
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02005938 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5939 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
5940 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005941
5942 if (ret)
5943 dev_err(hba->dev,
5944 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5945 __func__, hba->init_prefetch_data.icc_level , ret);
5946
5947}
5948
5949/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005950 * ufshcd_scsi_add_wlus - Adds required W-LUs
5951 * @hba: per-adapter instance
5952 *
5953 * UFS device specification requires the UFS devices to support 4 well known
5954 * logical units:
5955 * "REPORT_LUNS" (address: 01h)
5956 * "UFS Device" (address: 50h)
5957 * "RPMB" (address: 44h)
5958 * "BOOT" (address: 30h)
5959 * UFS device's power management needs to be controlled by "POWER CONDITION"
5960 * field of SSU (START STOP UNIT) command. But this "power condition" field
5961 * will take effect only when its sent to "UFS device" well known logical unit
5962 * hence we require the scsi_device instance to represent this logical unit in
5963 * order for the UFS host driver to send the SSU command for power management.
5964
5965 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5966 * Block) LU so user space process can control this LU. User space may also
5967 * want to have access to BOOT LU.
5968
5969 * This function adds scsi device instances for each of all well known LUs
5970 * (except "REPORT LUNS" LU).
5971 *
5972 * Returns zero on success (all required W-LUs are added successfully),
5973 * non-zero error value on failure (if failed to add any of the required W-LU).
5974 */
5975static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
5976{
5977 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005978 struct scsi_device *sdev_rpmb;
5979 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005980
5981 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
5982 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
5983 if (IS_ERR(hba->sdev_ufs_device)) {
5984 ret = PTR_ERR(hba->sdev_ufs_device);
5985 hba->sdev_ufs_device = NULL;
5986 goto out;
5987 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005988 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005989
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005990 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005991 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005992 if (IS_ERR(sdev_rpmb)) {
5993 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08005994 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005995 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005996 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08005997
5998 sdev_boot = __scsi_add_device(hba->host, 0, 0,
5999 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
6000 if (IS_ERR(sdev_boot))
6001 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
6002 else
6003 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006004 goto out;
6005
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006006remove_sdev_ufs_device:
6007 scsi_remove_device(hba->sdev_ufs_device);
6008out:
6009 return ret;
6010}
6011
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006012static int ufs_get_device_desc(struct ufs_hba *hba,
6013 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006014{
6015 int err;
6016 u8 model_index;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006017 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
6018 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006019
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006020 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006021 if (err) {
6022 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6023 __func__, err);
6024 goto out;
6025 }
6026
6027 /*
6028 * getting vendor (manufacturerID) and Bank Index in big endian
6029 * format
6030 */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006031 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006032 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6033
6034 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
6035
6036 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006037 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006038 if (err) {
6039 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
6040 __func__, err);
6041 goto out;
6042 }
6043
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006044 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006045 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006046 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
6047 MAX_MODEL_LEN));
6048
6049 /* Null terminate the model string */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006050 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006051
6052out:
6053 return err;
6054}
6055
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006056static void ufs_fixup_device_setup(struct ufs_hba *hba,
6057 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006058{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006059 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006060
6061 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006062 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
6063 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
6064 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006065 !strcmp(f->card.model, UFS_ANY_MODEL)))
6066 hba->dev_quirks |= f->quirk;
6067 }
6068}
6069
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006070/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006071 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6072 * @hba: per-adapter instance
6073 *
6074 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6075 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6076 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6077 * the hibern8 exit latency.
6078 *
6079 * Returns zero on success, non-zero error value on failure.
6080 */
6081static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6082{
6083 int ret = 0;
6084 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6085
6086 ret = ufshcd_dme_peer_get(hba,
6087 UIC_ARG_MIB_SEL(
6088 RX_MIN_ACTIVATETIME_CAPABILITY,
6089 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6090 &peer_rx_min_activatetime);
6091 if (ret)
6092 goto out;
6093
6094 /* make sure proper unit conversion is applied */
6095 tuned_pa_tactivate =
6096 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6097 / PA_TACTIVATE_TIME_UNIT_US);
6098 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6099 tuned_pa_tactivate);
6100
6101out:
6102 return ret;
6103}
6104
6105/**
6106 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6107 * @hba: per-adapter instance
6108 *
6109 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6110 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6111 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6112 * This optimal value can help reduce the hibern8 exit latency.
6113 *
6114 * Returns zero on success, non-zero error value on failure.
6115 */
6116static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6117{
6118 int ret = 0;
6119 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6120 u32 max_hibern8_time, tuned_pa_hibern8time;
6121
6122 ret = ufshcd_dme_get(hba,
6123 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6124 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6125 &local_tx_hibern8_time_cap);
6126 if (ret)
6127 goto out;
6128
6129 ret = ufshcd_dme_peer_get(hba,
6130 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6131 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6132 &peer_rx_hibern8_time_cap);
6133 if (ret)
6134 goto out;
6135
6136 max_hibern8_time = max(local_tx_hibern8_time_cap,
6137 peer_rx_hibern8_time_cap);
6138 /* make sure proper unit conversion is applied */
6139 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6140 / PA_HIBERN8_TIME_UNIT_US);
6141 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6142 tuned_pa_hibern8time);
6143out:
6144 return ret;
6145}
6146
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006147/**
6148 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6149 * less than device PA_TACTIVATE time.
6150 * @hba: per-adapter instance
6151 *
6152 * Some UFS devices require host PA_TACTIVATE to be lower than device
6153 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6154 * for such devices.
6155 *
6156 * Returns zero on success, non-zero error value on failure.
6157 */
6158static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6159{
6160 int ret = 0;
6161 u32 granularity, peer_granularity;
6162 u32 pa_tactivate, peer_pa_tactivate;
6163 u32 pa_tactivate_us, peer_pa_tactivate_us;
6164 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6165
6166 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6167 &granularity);
6168 if (ret)
6169 goto out;
6170
6171 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6172 &peer_granularity);
6173 if (ret)
6174 goto out;
6175
6176 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6177 (granularity > PA_GRANULARITY_MAX_VAL)) {
6178 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6179 __func__, granularity);
6180 return -EINVAL;
6181 }
6182
6183 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6184 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6185 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6186 __func__, peer_granularity);
6187 return -EINVAL;
6188 }
6189
6190 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6191 if (ret)
6192 goto out;
6193
6194 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6195 &peer_pa_tactivate);
6196 if (ret)
6197 goto out;
6198
6199 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6200 peer_pa_tactivate_us = peer_pa_tactivate *
6201 gran_to_us_table[peer_granularity - 1];
6202
6203 if (pa_tactivate_us > peer_pa_tactivate_us) {
6204 u32 new_peer_pa_tactivate;
6205
6206 new_peer_pa_tactivate = pa_tactivate_us /
6207 gran_to_us_table[peer_granularity - 1];
6208 new_peer_pa_tactivate++;
6209 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6210 new_peer_pa_tactivate);
6211 }
6212
6213out:
6214 return ret;
6215}
6216
Yaniv Gardi37113102016-03-10 17:37:16 +02006217static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
6218{
6219 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6220 ufshcd_tune_pa_tactivate(hba);
6221 ufshcd_tune_pa_hibern8time(hba);
6222 }
6223
6224 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6225 /* set 1ms timeout for PA_TACTIVATE */
6226 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006227
6228 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6229 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08006230
6231 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006232}
6233
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006234static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6235{
6236 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
6237
6238 hba->ufs_stats.hibern8_exit_cnt = 0;
6239 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
6240
6241 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
6242 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
6243 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
6244 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
6245 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
Gilad Broner7fabb772017-02-03 16:56:50 -08006246
6247 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006248}
6249
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006250static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
6251{
6252 int err;
6253
6254 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
6255 &hba->desc_size.dev_desc);
6256 if (err)
6257 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
6258
6259 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
6260 &hba->desc_size.pwr_desc);
6261 if (err)
6262 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
6263
6264 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
6265 &hba->desc_size.interc_desc);
6266 if (err)
6267 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
6268
6269 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
6270 &hba->desc_size.conf_desc);
6271 if (err)
6272 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
6273
6274 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
6275 &hba->desc_size.unit_desc);
6276 if (err)
6277 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
6278
6279 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
6280 &hba->desc_size.geom_desc);
6281 if (err)
6282 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02006283 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
6284 &hba->desc_size.hlth_desc);
6285 if (err)
6286 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006287}
6288
6289static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
6290{
6291 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
6292 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
6293 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
6294 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
6295 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
6296 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02006297 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006298}
6299
Yaniv Gardi37113102016-03-10 17:37:16 +02006300/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006301 * ufshcd_probe_hba - probe hba to detect device and initialize
6302 * @hba: per-adapter instance
6303 *
6304 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306305 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006306static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306307{
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006308 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306309 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006310 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306311
6312 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306313 if (ret)
6314 goto out;
6315
Yaniv Gardiafdfff52016-03-10 17:37:15 +02006316 /* set the default level for urgent bkops */
6317 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
6318 hba->is_urgent_bkops_lvl_checked = false;
6319
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006320 /* Debug counters initialization */
6321 ufshcd_clear_dbg_ufs_stats(hba);
6322
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006323 /* UniPro link is active now */
6324 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05306325
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306326 ret = ufshcd_verify_dev_init(hba);
6327 if (ret)
6328 goto out;
6329
Dolev Raviv68078d52013-07-30 00:35:58 +05306330 ret = ufshcd_complete_dev_init(hba);
6331 if (ret)
6332 goto out;
6333
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006334 /* Init check for device descriptor sizes */
6335 ufshcd_init_desc_sizes(hba);
6336
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006337 ret = ufs_get_device_desc(hba, &card);
6338 if (ret) {
6339 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
6340 __func__, ret);
6341 goto out;
6342 }
6343
6344 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02006345 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006346
6347 ret = ufshcd_set_vccq_rail_unused(hba,
6348 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
6349 if (ret)
6350 goto out;
6351
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006352 /* UFS device is also active now */
6353 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306354 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006355 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306356
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006357 if (ufshcd_get_max_pwr_mode(hba)) {
6358 dev_err(hba->dev,
6359 "%s: Failed getting max supported power mode\n",
6360 __func__);
6361 } else {
6362 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006363 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006364 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
6365 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006366 goto out;
6367 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006368 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006369
Yaniv Gardi53c12d02016-02-01 15:02:45 +02006370 /* set the state as operational after switching to desired gear */
6371 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006372
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006373 /*
6374 * If we are in error handling context or in power management callbacks
6375 * context, no need to scan the host
6376 */
6377 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6378 bool flag;
6379
6380 /* clear any previous UFS device information */
6381 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02006382 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6383 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006384 hba->dev_info.f_power_on_wp_en = flag;
6385
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006386 if (!hba->is_init_prefetch)
6387 ufshcd_init_icc_levels(hba);
6388
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006389 /* Add required well known logical units to scsi mid layer */
6390 if (ufshcd_scsi_add_wlus(hba))
6391 goto out;
6392
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006393 /* Initialize devfreq after UFS device is detected */
6394 if (ufshcd_is_clkscaling_supported(hba)) {
6395 memcpy(&hba->clk_scaling.saved_pwr_info.info,
6396 &hba->pwr_info,
6397 sizeof(struct ufs_pa_layer_attr));
6398 hba->clk_scaling.saved_pwr_info.is_valid = true;
6399 if (!hba->devfreq) {
6400 hba->devfreq = devm_devfreq_add_device(hba->dev,
6401 &ufs_devfreq_profile,
6402 "simple_ondemand",
6403 NULL);
6404 if (IS_ERR(hba->devfreq)) {
6405 ret = PTR_ERR(hba->devfreq);
6406 dev_err(hba->dev, "Unable to register with devfreq %d\n",
6407 ret);
6408 goto out;
6409 }
6410 }
6411 hba->clk_scaling.is_allowed = true;
6412 }
6413
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306414 scsi_scan_host(hba->host);
6415 pm_runtime_put_sync(hba->dev);
6416 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006417
6418 if (!hba->is_init_prefetch)
6419 hba->is_init_prefetch = true;
6420
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306421out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006422 /*
6423 * If we failed to initialize the device or the device is not
6424 * present, turn off the power/clocks etc.
6425 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006426 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6427 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006428 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006429 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006430
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006431 trace_ufshcd_init(dev_name(hba->dev), ret,
6432 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08006433 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006434 return ret;
6435}
6436
6437/**
6438 * ufshcd_async_scan - asynchronous execution for probing hba
6439 * @data: data pointer to pass to this function
6440 * @cookie: cookie data
6441 */
6442static void ufshcd_async_scan(void *data, async_cookie_t cookie)
6443{
6444 struct ufs_hba *hba = (struct ufs_hba *)data;
6445
6446 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306447}
6448
Yaniv Gardif550c652016-03-10 17:37:07 +02006449static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
6450{
6451 unsigned long flags;
6452 struct Scsi_Host *host;
6453 struct ufs_hba *hba;
6454 int index;
6455 bool found = false;
6456
6457 if (!scmd || !scmd->device || !scmd->device->host)
6458 return BLK_EH_NOT_HANDLED;
6459
6460 host = scmd->device->host;
6461 hba = shost_priv(host);
6462 if (!hba)
6463 return BLK_EH_NOT_HANDLED;
6464
6465 spin_lock_irqsave(host->host_lock, flags);
6466
6467 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
6468 if (hba->lrb[index].cmd == scmd) {
6469 found = true;
6470 break;
6471 }
6472 }
6473
6474 spin_unlock_irqrestore(host->host_lock, flags);
6475
6476 /*
6477 * Bypass SCSI error handling and reset the block layer timer if this
6478 * SCSI command was not actually dispatched to UFS driver, otherwise
6479 * let SCSI layer handle the error as usual.
6480 */
6481 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
6482}
6483
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306484static struct scsi_host_template ufshcd_driver_template = {
6485 .module = THIS_MODULE,
6486 .name = UFSHCD,
6487 .proc_name = UFSHCD,
6488 .queuecommand = ufshcd_queuecommand,
6489 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09006490 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306491 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03006492 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306493 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306494 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
6495 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02006496 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306497 .this_id = -1,
6498 .sg_tablesize = SG_ALL,
6499 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
6500 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006501 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006502 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306503};
6504
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006505static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
6506 int ua)
6507{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006508 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006509
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006510 if (!vreg)
6511 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006512
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006513 ret = regulator_set_load(vreg->reg, ua);
6514 if (ret < 0) {
6515 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
6516 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006517 }
6518
6519 return ret;
6520}
6521
6522static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
6523 struct ufs_vreg *vreg)
6524{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006525 if (!vreg)
6526 return 0;
6527 else if (vreg->unused)
6528 return 0;
6529 else
6530 return ufshcd_config_vreg_load(hba->dev, vreg,
6531 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006532}
6533
6534static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
6535 struct ufs_vreg *vreg)
6536{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006537 if (!vreg)
6538 return 0;
6539 else if (vreg->unused)
6540 return 0;
6541 else
6542 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006543}
6544
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006545static int ufshcd_config_vreg(struct device *dev,
6546 struct ufs_vreg *vreg, bool on)
6547{
6548 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06006549 struct regulator *reg;
6550 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006551 int min_uV, uA_load;
6552
6553 BUG_ON(!vreg);
6554
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06006555 reg = vreg->reg;
6556 name = vreg->name;
6557
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006558 if (regulator_count_voltages(reg) > 0) {
6559 min_uV = on ? vreg->min_uV : 0;
6560 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
6561 if (ret) {
6562 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
6563 __func__, name, ret);
6564 goto out;
6565 }
6566
6567 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006568 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
6569 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006570 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006571 }
6572out:
6573 return ret;
6574}
6575
6576static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
6577{
6578 int ret = 0;
6579
Yaniv Gardi60f01872016-03-10 17:37:11 +02006580 if (!vreg)
6581 goto out;
6582 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006583 goto out;
6584
6585 ret = ufshcd_config_vreg(dev, vreg, true);
6586 if (!ret)
6587 ret = regulator_enable(vreg->reg);
6588
6589 if (!ret)
6590 vreg->enabled = true;
6591 else
6592 dev_err(dev, "%s: %s enable failed, err=%d\n",
6593 __func__, vreg->name, ret);
6594out:
6595 return ret;
6596}
6597
6598static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
6599{
6600 int ret = 0;
6601
Yaniv Gardi60f01872016-03-10 17:37:11 +02006602 if (!vreg)
6603 goto out;
6604 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006605 goto out;
6606
6607 ret = regulator_disable(vreg->reg);
6608
6609 if (!ret) {
6610 /* ignore errors on applying disable config */
6611 ufshcd_config_vreg(dev, vreg, false);
6612 vreg->enabled = false;
6613 } else {
6614 dev_err(dev, "%s: %s disable failed, err=%d\n",
6615 __func__, vreg->name, ret);
6616 }
6617out:
6618 return ret;
6619}
6620
6621static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
6622{
6623 int ret = 0;
6624 struct device *dev = hba->dev;
6625 struct ufs_vreg_info *info = &hba->vreg_info;
6626
6627 if (!info)
6628 goto out;
6629
6630 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
6631 if (ret)
6632 goto out;
6633
6634 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
6635 if (ret)
6636 goto out;
6637
6638 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
6639 if (ret)
6640 goto out;
6641
6642out:
6643 if (ret) {
6644 ufshcd_toggle_vreg(dev, info->vccq2, false);
6645 ufshcd_toggle_vreg(dev, info->vccq, false);
6646 ufshcd_toggle_vreg(dev, info->vcc, false);
6647 }
6648 return ret;
6649}
6650
Raviv Shvili6a771a62014-09-25 15:32:24 +03006651static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
6652{
6653 struct ufs_vreg_info *info = &hba->vreg_info;
6654
6655 if (info)
6656 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
6657
6658 return 0;
6659}
6660
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006661static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
6662{
6663 int ret = 0;
6664
6665 if (!vreg)
6666 goto out;
6667
6668 vreg->reg = devm_regulator_get(dev, vreg->name);
6669 if (IS_ERR(vreg->reg)) {
6670 ret = PTR_ERR(vreg->reg);
6671 dev_err(dev, "%s: %s get failed, err=%d\n",
6672 __func__, vreg->name, ret);
6673 }
6674out:
6675 return ret;
6676}
6677
6678static int ufshcd_init_vreg(struct ufs_hba *hba)
6679{
6680 int ret = 0;
6681 struct device *dev = hba->dev;
6682 struct ufs_vreg_info *info = &hba->vreg_info;
6683
6684 if (!info)
6685 goto out;
6686
6687 ret = ufshcd_get_vreg(dev, info->vcc);
6688 if (ret)
6689 goto out;
6690
6691 ret = ufshcd_get_vreg(dev, info->vccq);
6692 if (ret)
6693 goto out;
6694
6695 ret = ufshcd_get_vreg(dev, info->vccq2);
6696out:
6697 return ret;
6698}
6699
Raviv Shvili6a771a62014-09-25 15:32:24 +03006700static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
6701{
6702 struct ufs_vreg_info *info = &hba->vreg_info;
6703
6704 if (info)
6705 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
6706
6707 return 0;
6708}
6709
Yaniv Gardi60f01872016-03-10 17:37:11 +02006710static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
6711{
6712 int ret = 0;
6713 struct ufs_vreg_info *info = &hba->vreg_info;
6714
6715 if (!info)
6716 goto out;
6717 else if (!info->vccq)
6718 goto out;
6719
6720 if (unused) {
6721 /* shut off the rail here */
6722 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
6723 /*
6724 * Mark this rail as no longer used, so it doesn't get enabled
6725 * later by mistake
6726 */
6727 if (!ret)
6728 info->vccq->unused = true;
6729 } else {
6730 /*
6731 * rail should have been already enabled hence just make sure
6732 * that unused flag is cleared.
6733 */
6734 info->vccq->unused = false;
6735 }
6736out:
6737 return ret;
6738}
6739
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006740static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
6741 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006742{
6743 int ret = 0;
6744 struct ufs_clk_info *clki;
6745 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006746 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006747 ktime_t start = ktime_get();
6748 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006749
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03006750 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006751 goto out;
6752
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006753 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
6754 if (ret)
6755 return ret;
6756
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006757 list_for_each_entry(clki, head, list) {
6758 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006759 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
6760 continue;
6761
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006762 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006763 if (on && !clki->enabled) {
6764 ret = clk_prepare_enable(clki->clk);
6765 if (ret) {
6766 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
6767 __func__, clki->name, ret);
6768 goto out;
6769 }
6770 } else if (!on && clki->enabled) {
6771 clk_disable_unprepare(clki->clk);
6772 }
6773 clki->enabled = on;
6774 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
6775 clki->name, on ? "en" : "dis");
6776 }
6777 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006778
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006779 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
6780 if (ret)
6781 return ret;
6782
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006783out:
6784 if (ret) {
6785 list_for_each_entry(clki, head, list) {
6786 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
6787 clk_disable_unprepare(clki->clk);
6788 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006789 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006790 spin_lock_irqsave(hba->host->host_lock, flags);
6791 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006792 trace_ufshcd_clk_gating(dev_name(hba->dev),
6793 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006794 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006795 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006796
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006797 if (clk_state_changed)
6798 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
6799 (on ? "on" : "off"),
6800 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006801 return ret;
6802}
6803
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006804static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
6805{
6806 return __ufshcd_setup_clocks(hba, on, false);
6807}
6808
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006809static int ufshcd_init_clocks(struct ufs_hba *hba)
6810{
6811 int ret = 0;
6812 struct ufs_clk_info *clki;
6813 struct device *dev = hba->dev;
6814 struct list_head *head = &hba->clk_list_head;
6815
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03006816 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006817 goto out;
6818
6819 list_for_each_entry(clki, head, list) {
6820 if (!clki->name)
6821 continue;
6822
6823 clki->clk = devm_clk_get(dev, clki->name);
6824 if (IS_ERR(clki->clk)) {
6825 ret = PTR_ERR(clki->clk);
6826 dev_err(dev, "%s: %s clk get failed, %d\n",
6827 __func__, clki->name, ret);
6828 goto out;
6829 }
6830
6831 if (clki->max_freq) {
6832 ret = clk_set_rate(clki->clk, clki->max_freq);
6833 if (ret) {
6834 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6835 __func__, clki->name,
6836 clki->max_freq, ret);
6837 goto out;
6838 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03006839 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006840 }
6841 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
6842 clki->name, clk_get_rate(clki->clk));
6843 }
6844out:
6845 return ret;
6846}
6847
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006848static int ufshcd_variant_hba_init(struct ufs_hba *hba)
6849{
6850 int err = 0;
6851
6852 if (!hba->vops)
6853 goto out;
6854
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006855 err = ufshcd_vops_init(hba);
6856 if (err)
6857 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006858
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006859 err = ufshcd_vops_setup_regulators(hba, true);
6860 if (err)
6861 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006862
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006863 goto out;
6864
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006865out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006866 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006867out:
6868 if (err)
6869 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006870 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006871 return err;
6872}
6873
6874static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
6875{
6876 if (!hba->vops)
6877 return;
6878
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006879 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006880
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006881 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006882}
6883
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006884static int ufshcd_hba_init(struct ufs_hba *hba)
6885{
6886 int err;
6887
Raviv Shvili6a771a62014-09-25 15:32:24 +03006888 /*
6889 * Handle host controller power separately from the UFS device power
6890 * rails as it will help controlling the UFS host controller power
6891 * collapse easily which is different than UFS device power collapse.
6892 * Also, enable the host controller power before we go ahead with rest
6893 * of the initialization here.
6894 */
6895 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006896 if (err)
6897 goto out;
6898
Raviv Shvili6a771a62014-09-25 15:32:24 +03006899 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006900 if (err)
6901 goto out;
6902
Raviv Shvili6a771a62014-09-25 15:32:24 +03006903 err = ufshcd_init_clocks(hba);
6904 if (err)
6905 goto out_disable_hba_vreg;
6906
6907 err = ufshcd_setup_clocks(hba, true);
6908 if (err)
6909 goto out_disable_hba_vreg;
6910
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006911 err = ufshcd_init_vreg(hba);
6912 if (err)
6913 goto out_disable_clks;
6914
6915 err = ufshcd_setup_vreg(hba, true);
6916 if (err)
6917 goto out_disable_clks;
6918
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006919 err = ufshcd_variant_hba_init(hba);
6920 if (err)
6921 goto out_disable_vreg;
6922
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006923 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006924 goto out;
6925
6926out_disable_vreg:
6927 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006928out_disable_clks:
6929 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03006930out_disable_hba_vreg:
6931 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006932out:
6933 return err;
6934}
6935
6936static void ufshcd_hba_exit(struct ufs_hba *hba)
6937{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006938 if (hba->is_powered) {
6939 ufshcd_variant_hba_exit(hba);
6940 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07006941 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006942 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006943 if (hba->devfreq)
6944 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006945 destroy_workqueue(hba->clk_scaling.workq);
6946 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006947 ufshcd_setup_clocks(hba, false);
6948 ufshcd_setup_hba_vreg(hba, false);
6949 hba->is_powered = false;
6950 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006951}
6952
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006953static int
6954ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306955{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006956 unsigned char cmd[6] = {REQUEST_SENSE,
6957 0,
6958 0,
6959 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006960 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006961 0};
6962 char *buffer;
6963 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306964
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006965 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006966 if (!buffer) {
6967 ret = -ENOMEM;
6968 goto out;
6969 }
6970
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01006971 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
6972 UFSHCD_REQ_SENSE_SIZE, NULL, NULL,
6973 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006974 if (ret)
6975 pr_err("%s: failed with err %d\n", __func__, ret);
6976
6977 kfree(buffer);
6978out:
6979 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306980}
6981
6982/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006983 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6984 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306985 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006986 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306987 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006988 * Returns 0 if requested power mode is set successfully
6989 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306990 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006991static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
6992 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306993{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006994 unsigned char cmd[6] = { START_STOP };
6995 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006996 struct scsi_device *sdp;
6997 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006998 int ret;
6999
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007000 spin_lock_irqsave(hba->host->host_lock, flags);
7001 sdp = hba->sdev_ufs_device;
7002 if (sdp) {
7003 ret = scsi_device_get(sdp);
7004 if (!ret && !scsi_device_online(sdp)) {
7005 ret = -ENODEV;
7006 scsi_device_put(sdp);
7007 }
7008 } else {
7009 ret = -ENODEV;
7010 }
7011 spin_unlock_irqrestore(hba->host->host_lock, flags);
7012
7013 if (ret)
7014 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007015
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307016 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007017 * If scsi commands fail, the scsi mid-layer schedules scsi error-
7018 * handling, which would wait for host to be resumed. Since we know
7019 * we are functional while we are here, skip host resume in error
7020 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307021 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007022 hba->host->eh_noresume = 1;
7023 if (hba->wlun_dev_clr_ua) {
7024 ret = ufshcd_send_request_sense(hba, sdp);
7025 if (ret)
7026 goto out;
7027 /* Unit attention condition is cleared now */
7028 hba->wlun_dev_clr_ua = false;
7029 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307030
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007031 cmd[4] = pwr_mode << 4;
7032
7033 /*
7034 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02007035 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007036 * already suspended childs.
7037 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007038 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
7039 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007040 if (ret) {
7041 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02007042 "START_STOP failed for power mode: %d, result %x\n",
7043 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01007044 if (driver_byte(ret) & DRIVER_SENSE)
7045 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007046 }
7047
7048 if (!ret)
7049 hba->curr_dev_pwr_mode = pwr_mode;
7050out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007051 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007052 hba->host->eh_noresume = 0;
7053 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307054}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307055
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007056static int ufshcd_link_state_transition(struct ufs_hba *hba,
7057 enum uic_link_state req_link_state,
7058 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307059{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007060 int ret = 0;
7061
7062 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307063 return 0;
7064
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007065 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7066 ret = ufshcd_uic_hibern8_enter(hba);
7067 if (!ret)
7068 ufshcd_set_link_hibern8(hba);
7069 else
7070 goto out;
7071 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307072 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007073 * If autobkops is enabled, link can't be turned off because
7074 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307075 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007076 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
7077 (!check_for_bkops || (check_for_bkops &&
7078 !hba->auto_bkops_enabled))) {
7079 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007080 * Let's make sure that link is in low power mode, we are doing
7081 * this currently by putting the link in Hibern8. Otherway to
7082 * put the link in low power mode is to send the DME end point
7083 * to device and then send the DME reset command to local
7084 * unipro. But putting the link in hibern8 is much faster.
7085 */
7086 ret = ufshcd_uic_hibern8_enter(hba);
7087 if (ret)
7088 goto out;
7089 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007090 * Change controller state to "reset state" which
7091 * should also put the link in off/reset state
7092 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007093 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007094 /*
7095 * TODO: Check if we need any delay to make sure that
7096 * controller is reset
7097 */
7098 ufshcd_set_link_off(hba);
7099 }
7100
7101out:
7102 return ret;
7103}
7104
7105static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7106{
7107 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007108 * It seems some UFS devices may keep drawing more than sleep current
7109 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7110 * To avoid this situation, add 2ms delay before putting these UFS
7111 * rails in LPM mode.
7112 */
7113 if (!ufshcd_is_link_active(hba) &&
7114 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
7115 usleep_range(2000, 2100);
7116
7117 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007118 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7119 * power.
7120 *
7121 * If UFS device and link is in OFF state, all power supplies (VCC,
7122 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7123 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7124 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7125 *
7126 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7127 * in low power state which would save some power.
7128 */
7129 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7130 !hba->dev_info.is_lu_power_on_wp) {
7131 ufshcd_setup_vreg(hba, false);
7132 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7133 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7134 if (!ufshcd_is_link_active(hba)) {
7135 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7136 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7137 }
7138 }
7139}
7140
7141static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7142{
7143 int ret = 0;
7144
7145 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7146 !hba->dev_info.is_lu_power_on_wp) {
7147 ret = ufshcd_setup_vreg(hba, true);
7148 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007149 if (!ret && !ufshcd_is_link_active(hba)) {
7150 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7151 if (ret)
7152 goto vcc_disable;
7153 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7154 if (ret)
7155 goto vccq_lpm;
7156 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07007157 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007158 }
7159 goto out;
7160
7161vccq_lpm:
7162 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7163vcc_disable:
7164 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7165out:
7166 return ret;
7167}
7168
7169static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7170{
7171 if (ufshcd_is_link_off(hba))
7172 ufshcd_setup_hba_vreg(hba, false);
7173}
7174
7175static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7176{
7177 if (ufshcd_is_link_off(hba))
7178 ufshcd_setup_hba_vreg(hba, true);
7179}
7180
7181/**
7182 * ufshcd_suspend - helper function for suspend operations
7183 * @hba: per adapter instance
7184 * @pm_op: desired low power operation type
7185 *
7186 * This function will try to put the UFS device and link into low power
7187 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7188 * (System PM level).
7189 *
7190 * If this function is called during shutdown, it will make sure that
7191 * both UFS device and UFS link is powered off.
7192 *
7193 * NOTE: UFS device & link must be active before we enter in this function.
7194 *
7195 * Returns 0 for success and non-zero for failure
7196 */
7197static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7198{
7199 int ret = 0;
7200 enum ufs_pm_level pm_lvl;
7201 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7202 enum uic_link_state req_link_state;
7203
7204 hba->pm_op_in_progress = 1;
7205 if (!ufshcd_is_shutdown_pm(pm_op)) {
7206 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7207 hba->rpm_lvl : hba->spm_lvl;
7208 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7209 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7210 } else {
7211 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7212 req_link_state = UIC_LINK_OFF_STATE;
7213 }
7214
7215 /*
7216 * If we can't transition into any of the low power modes
7217 * just gate the clocks.
7218 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007219 ufshcd_hold(hba, false);
7220 hba->clk_gating.is_suspended = true;
7221
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007222 if (hba->clk_scaling.is_allowed) {
7223 cancel_work_sync(&hba->clk_scaling.suspend_work);
7224 cancel_work_sync(&hba->clk_scaling.resume_work);
7225 ufshcd_suspend_clkscaling(hba);
7226 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007227
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007228 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
7229 req_link_state == UIC_LINK_ACTIVE_STATE) {
7230 goto disable_clks;
7231 }
7232
7233 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
7234 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007235 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007236
7237 /* UFS device & link must be active before we enter in this function */
7238 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
7239 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007240 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007241 }
7242
7243 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03007244 if (ufshcd_can_autobkops_during_suspend(hba)) {
7245 /*
7246 * The device is idle with no requests in the queue,
7247 * allow background operations if bkops status shows
7248 * that performance might be impacted.
7249 */
7250 ret = ufshcd_urgent_bkops(hba);
7251 if (ret)
7252 goto enable_gating;
7253 } else {
7254 /* make sure that auto bkops is disabled */
7255 ufshcd_disable_auto_bkops(hba);
7256 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007257 }
7258
7259 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
7260 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
7261 !ufshcd_is_runtime_pm(pm_op))) {
7262 /* ensure that bkops is disabled */
7263 ufshcd_disable_auto_bkops(hba);
7264 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
7265 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007266 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007267 }
7268
7269 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
7270 if (ret)
7271 goto set_dev_active;
7272
7273 ufshcd_vreg_set_lpm(hba);
7274
7275disable_clks:
7276 /*
7277 * Call vendor specific suspend callback. As these callbacks may access
7278 * vendor specific host controller register space call them before the
7279 * host clocks are ON.
7280 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007281 ret = ufshcd_vops_suspend(hba, pm_op);
7282 if (ret)
7283 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007284
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007285 if (!ufshcd_is_link_active(hba))
7286 ufshcd_setup_clocks(hba, false);
7287 else
7288 /* If link is active, device ref_clk can't be switched off */
7289 __ufshcd_setup_clocks(hba, false, true);
7290
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007291 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007292 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007293 /*
7294 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007295 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007296 */
7297 ufshcd_disable_irq(hba);
7298 /* Put the host controller in low power mode if possible */
7299 ufshcd_hba_vreg_set_lpm(hba);
7300 goto out;
7301
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007302set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007303 if (hba->clk_scaling.is_allowed)
7304 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007305 ufshcd_vreg_set_hpm(hba);
7306 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
7307 ufshcd_set_link_active(hba);
7308 else if (ufshcd_is_link_off(hba))
7309 ufshcd_host_reset_and_restore(hba);
7310set_dev_active:
7311 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
7312 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007313enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007314 if (hba->clk_scaling.is_allowed)
7315 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007316 hba->clk_gating.is_suspended = false;
7317 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007318out:
7319 hba->pm_op_in_progress = 0;
7320 return ret;
7321}
7322
7323/**
7324 * ufshcd_resume - helper function for resume operations
7325 * @hba: per adapter instance
7326 * @pm_op: runtime PM or system PM
7327 *
7328 * This function basically brings the UFS device, UniPro link and controller
7329 * to active state.
7330 *
7331 * Returns 0 for success and non-zero for failure
7332 */
7333static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7334{
7335 int ret;
7336 enum uic_link_state old_link_state;
7337
7338 hba->pm_op_in_progress = 1;
7339 old_link_state = hba->uic_link_state;
7340
7341 ufshcd_hba_vreg_set_hpm(hba);
7342 /* Make sure clocks are enabled before accessing controller */
7343 ret = ufshcd_setup_clocks(hba, true);
7344 if (ret)
7345 goto out;
7346
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007347 /* enable the host irq as host controller would be active soon */
7348 ret = ufshcd_enable_irq(hba);
7349 if (ret)
7350 goto disable_irq_and_vops_clks;
7351
7352 ret = ufshcd_vreg_set_hpm(hba);
7353 if (ret)
7354 goto disable_irq_and_vops_clks;
7355
7356 /*
7357 * Call vendor specific resume callback. As these callbacks may access
7358 * vendor specific host controller register space call them when the
7359 * host clocks are ON.
7360 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007361 ret = ufshcd_vops_resume(hba, pm_op);
7362 if (ret)
7363 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007364
7365 if (ufshcd_is_link_hibern8(hba)) {
7366 ret = ufshcd_uic_hibern8_exit(hba);
7367 if (!ret)
7368 ufshcd_set_link_active(hba);
7369 else
7370 goto vendor_suspend;
7371 } else if (ufshcd_is_link_off(hba)) {
7372 ret = ufshcd_host_reset_and_restore(hba);
7373 /*
7374 * ufshcd_host_reset_and_restore() should have already
7375 * set the link state as active
7376 */
7377 if (ret || !ufshcd_is_link_active(hba))
7378 goto vendor_suspend;
7379 }
7380
7381 if (!ufshcd_is_ufs_dev_active(hba)) {
7382 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
7383 if (ret)
7384 goto set_old_link_state;
7385 }
7386
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08007387 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
7388 ufshcd_enable_auto_bkops(hba);
7389 else
7390 /*
7391 * If BKOPs operations are urgently needed at this moment then
7392 * keep auto-bkops enabled or else disable it.
7393 */
7394 ufshcd_urgent_bkops(hba);
7395
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007396 hba->clk_gating.is_suspended = false;
7397
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007398 if (hba->clk_scaling.is_allowed)
7399 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007400
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007401 /* Schedule clock gating in case of no access to UFS device yet */
7402 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007403 goto out;
7404
7405set_old_link_state:
7406 ufshcd_link_state_transition(hba, old_link_state, 0);
7407vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007408 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007409disable_vreg:
7410 ufshcd_vreg_set_lpm(hba);
7411disable_irq_and_vops_clks:
7412 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007413 if (hba->clk_scaling.is_allowed)
7414 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007415 ufshcd_setup_clocks(hba, false);
7416out:
7417 hba->pm_op_in_progress = 0;
7418 return ret;
7419}
7420
7421/**
7422 * ufshcd_system_suspend - system suspend routine
7423 * @hba: per adapter instance
7424 * @pm_op: runtime PM or system PM
7425 *
7426 * Check the description of ufshcd_suspend() function for more details.
7427 *
7428 * Returns 0 for success and non-zero for failure
7429 */
7430int ufshcd_system_suspend(struct ufs_hba *hba)
7431{
7432 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007433 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007434
7435 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03007436 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007437
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007438 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
7439 hba->curr_dev_pwr_mode) &&
7440 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
7441 hba->uic_link_state))
7442 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007443
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007444 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007445 /*
7446 * UFS device and/or UFS link low power states during runtime
7447 * suspend seems to be different than what is expected during
7448 * system suspend. Hence runtime resume the devic & link and
7449 * let the system suspend low power states to take effect.
7450 * TODO: If resume takes longer time, we might have optimize
7451 * it in future by not resuming everything if possible.
7452 */
7453 ret = ufshcd_runtime_resume(hba);
7454 if (ret)
7455 goto out;
7456 }
7457
7458 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
7459out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007460 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
7461 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007462 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03007463 if (!ret)
7464 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007465 return ret;
7466}
7467EXPORT_SYMBOL(ufshcd_system_suspend);
7468
7469/**
7470 * ufshcd_system_resume - system resume routine
7471 * @hba: per adapter instance
7472 *
7473 * Returns 0 for success and non-zero for failure
7474 */
7475
7476int ufshcd_system_resume(struct ufs_hba *hba)
7477{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007478 int ret = 0;
7479 ktime_t start = ktime_get();
7480
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007481 if (!hba)
7482 return -EINVAL;
7483
7484 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007485 /*
7486 * Let the runtime resume take care of resuming
7487 * if runtime suspended.
7488 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007489 goto out;
7490 else
7491 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
7492out:
7493 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
7494 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007495 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007496 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007497}
7498EXPORT_SYMBOL(ufshcd_system_resume);
7499
7500/**
7501 * ufshcd_runtime_suspend - runtime suspend routine
7502 * @hba: per adapter instance
7503 *
7504 * Check the description of ufshcd_suspend() function for more details.
7505 *
7506 * Returns 0 for success and non-zero for failure
7507 */
7508int ufshcd_runtime_suspend(struct ufs_hba *hba)
7509{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007510 int ret = 0;
7511 ktime_t start = ktime_get();
7512
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007513 if (!hba)
7514 return -EINVAL;
7515
7516 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007517 goto out;
7518 else
7519 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
7520out:
7521 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
7522 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007523 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007524 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307525}
7526EXPORT_SYMBOL(ufshcd_runtime_suspend);
7527
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007528/**
7529 * ufshcd_runtime_resume - runtime resume routine
7530 * @hba: per adapter instance
7531 *
7532 * This function basically brings the UFS device, UniPro link and controller
7533 * to active state. Following operations are done in this function:
7534 *
7535 * 1. Turn on all the controller related clocks
7536 * 2. Bring the UniPro link out of Hibernate state
7537 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
7538 * to active state.
7539 * 4. If auto-bkops is enabled on the device, disable it.
7540 *
7541 * So following would be the possible power state after this function return
7542 * successfully:
7543 * S1: UFS device in Active state with VCC rail ON
7544 * UniPro link in Active state
7545 * All the UFS/UniPro controller clocks are ON
7546 *
7547 * Returns 0 for success and non-zero for failure
7548 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307549int ufshcd_runtime_resume(struct ufs_hba *hba)
7550{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007551 int ret = 0;
7552 ktime_t start = ktime_get();
7553
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007554 if (!hba)
7555 return -EINVAL;
7556
7557 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007558 goto out;
7559 else
7560 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
7561out:
7562 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
7563 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007564 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007565 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307566}
7567EXPORT_SYMBOL(ufshcd_runtime_resume);
7568
7569int ufshcd_runtime_idle(struct ufs_hba *hba)
7570{
7571 return 0;
7572}
7573EXPORT_SYMBOL(ufshcd_runtime_idle);
7574
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307575/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007576 * ufshcd_shutdown - shutdown routine
7577 * @hba: per adapter instance
7578 *
7579 * This function would power off both UFS device and UFS link.
7580 *
7581 * Returns 0 always to allow force shutdown even in case of errors.
7582 */
7583int ufshcd_shutdown(struct ufs_hba *hba)
7584{
7585 int ret = 0;
7586
7587 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
7588 goto out;
7589
7590 if (pm_runtime_suspended(hba->dev)) {
7591 ret = ufshcd_runtime_resume(hba);
7592 if (ret)
7593 goto out;
7594 }
7595
7596 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
7597out:
7598 if (ret)
7599 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
7600 /* allow force shutdown even in case of errors */
7601 return 0;
7602}
7603EXPORT_SYMBOL(ufshcd_shutdown);
7604
7605/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307606 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307607 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307608 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307609 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307610void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307611{
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02007612 ufs_sysfs_remove_nodes(hba->dev);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05307613 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307614 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307615 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02007616 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307617
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007618 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007619 if (ufshcd_is_clkscaling_supported(hba))
7620 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007621 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307622}
7623EXPORT_SYMBOL_GPL(ufshcd_remove);
7624
7625/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02007626 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
7627 * @hba: pointer to Host Bus Adapter (HBA)
7628 */
7629void ufshcd_dealloc_host(struct ufs_hba *hba)
7630{
7631 scsi_host_put(hba->host);
7632}
7633EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
7634
7635/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007636 * ufshcd_set_dma_mask - Set dma mask based on the controller
7637 * addressing capability
7638 * @hba: per adapter instance
7639 *
7640 * Returns 0 for success, non-zero for failure
7641 */
7642static int ufshcd_set_dma_mask(struct ufs_hba *hba)
7643{
7644 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
7645 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
7646 return 0;
7647 }
7648 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
7649}
7650
7651/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007652 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307653 * @dev: pointer to device handle
7654 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307655 * Returns 0 on success, non-zero value on failure
7656 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007657int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307658{
7659 struct Scsi_Host *host;
7660 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007661 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307662
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307663 if (!dev) {
7664 dev_err(dev,
7665 "Invalid memory reference for dev is NULL\n");
7666 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307667 goto out_error;
7668 }
7669
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307670 host = scsi_host_alloc(&ufshcd_driver_template,
7671 sizeof(struct ufs_hba));
7672 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307673 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307674 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307675 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307676 }
7677 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307678 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307679 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007680 *hba_handle = hba;
7681
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007682 INIT_LIST_HEAD(&hba->clk_list_head);
7683
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007684out_error:
7685 return err;
7686}
7687EXPORT_SYMBOL(ufshcd_alloc_host);
7688
7689/**
7690 * ufshcd_init - Driver initialization routine
7691 * @hba: per-adapter instance
7692 * @mmio_base: base register address
7693 * @irq: Interrupt line of device
7694 * Returns 0 on success, non-zero value on failure
7695 */
7696int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
7697{
7698 int err;
7699 struct Scsi_Host *host = hba->host;
7700 struct device *dev = hba->dev;
7701
7702 if (!mmio_base) {
7703 dev_err(hba->dev,
7704 "Invalid memory reference for mmio_base is NULL\n");
7705 err = -ENODEV;
7706 goto out_error;
7707 }
7708
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307709 hba->mmio_base = mmio_base;
7710 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307711
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00007712 /* Set descriptor lengths to specification defaults */
7713 ufshcd_def_desc_sizes(hba);
7714
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007715 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007716 if (err)
7717 goto out_error;
7718
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307719 /* Read capabilities registers */
7720 ufshcd_hba_capabilities(hba);
7721
7722 /* Get UFS version supported by the controller */
7723 hba->ufs_version = ufshcd_get_ufs_version(hba);
7724
Yaniv Gardic01848c2016-12-05 19:25:02 -08007725 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
7726 (hba->ufs_version != UFSHCI_VERSION_11) &&
7727 (hba->ufs_version != UFSHCI_VERSION_20) &&
7728 (hba->ufs_version != UFSHCI_VERSION_21))
7729 dev_err(hba->dev, "invalid UFS version 0x%x\n",
7730 hba->ufs_version);
7731
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307732 /* Get Interrupt bit mask per version */
7733 hba->intr_mask = ufshcd_get_intr_mask(hba);
7734
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007735 err = ufshcd_set_dma_mask(hba);
7736 if (err) {
7737 dev_err(hba->dev, "set dma mask failed\n");
7738 goto out_disable;
7739 }
7740
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307741 /* Allocate memory for host memory space */
7742 err = ufshcd_memory_alloc(hba);
7743 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307744 dev_err(hba->dev, "Memory allocation failed\n");
7745 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307746 }
7747
7748 /* Configure LRB */
7749 ufshcd_host_memory_configure(hba);
7750
7751 host->can_queue = hba->nutrs;
7752 host->cmd_per_lun = hba->nutrs;
7753 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03007754 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307755 host->max_channel = UFSHCD_MAX_CHANNEL;
7756 host->unique_id = host->host_no;
7757 host->max_cmd_len = MAX_CDB_SIZE;
7758
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007759 hba->max_pwr_info.is_valid = false;
7760
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307761 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05307762 init_waitqueue_head(&hba->tm_wq);
7763 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307764
7765 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05307766 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307767 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307768
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307769 /* Initialize UIC command mutex */
7770 mutex_init(&hba->uic_cmd_mutex);
7771
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307772 /* Initialize mutex for device management commands */
7773 mutex_init(&hba->dev_cmd.lock);
7774
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08007775 init_rwsem(&hba->clk_scaling_lock);
7776
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307777 /* Initialize device management tag acquire wait queue */
7778 init_waitqueue_head(&hba->dev_cmd.tag_wq);
7779
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007780 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02007781
7782 /*
7783 * In order to avoid any spurious interrupt immediately after
7784 * registering UFS controller interrupt handler, clear any pending UFS
7785 * interrupt status and disable all the UFS interrupts.
7786 */
7787 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
7788 REG_INTERRUPT_STATUS);
7789 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
7790 /*
7791 * Make sure that UFS interrupts are disabled and any pending interrupt
7792 * status is cleared before registering UFS interrupt handler.
7793 */
7794 mb();
7795
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307796 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09007797 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307798 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307799 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007800 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007801 } else {
7802 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307803 }
7804
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307805 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307806 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307807 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007808 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307809 }
7810
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307811 /* Host controller enable */
7812 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307813 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307814 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08007815 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08007816 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307817 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307818 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307819
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007820 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007821 char wq_name[sizeof("ufs_clkscaling_00")];
7822
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007823 INIT_WORK(&hba->clk_scaling.suspend_work,
7824 ufshcd_clk_scaling_suspend_work);
7825 INIT_WORK(&hba->clk_scaling.resume_work,
7826 ufshcd_clk_scaling_resume_work);
7827
Tomohiro Kusumid985c6ea2017-03-28 16:49:29 +03007828 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007829 host->host_no);
7830 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
7831
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007832 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007833 }
7834
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08007835 /*
7836 * Set the default power management level for runtime and system PM.
7837 * Default power saving mode is to keep UFS link in Hibern8 state
7838 * and UFS device in sleep state.
7839 */
7840 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7841 UFS_SLEEP_PWR_MODE,
7842 UIC_LINK_HIBERN8_STATE);
7843 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7844 UFS_SLEEP_PWR_MODE,
7845 UIC_LINK_HIBERN8_STATE);
7846
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05307847 /* Hold auto suspend until async scan completes */
7848 pm_runtime_get_sync(dev);
7849
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007850 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007851 * We are assuming that device wasn't put in sleep/power-down
7852 * state exclusively during the boot stage before kernel.
7853 * This assumption helps avoid doing link startup twice during
7854 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007855 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007856 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007857
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307858 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02007859 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307860
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307861 return 0;
7862
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307863out_remove_scsi_host:
7864 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007865exit_gating:
7866 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307867out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007868 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007869 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307870out_error:
7871 return err;
7872}
7873EXPORT_SYMBOL_GPL(ufshcd_init);
7874
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307875MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7876MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05307877MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307878MODULE_LICENSE("GPL");
7879MODULE_VERSION(UFSHCD_DRIVER_VERSION);