blob: 1fbb17bb175b1e442755d68e5df920df66881ad9 [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
2225/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002226 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2227 * @scsi_lun: UPIU W-LUN id
2228 *
2229 * Returns SCSI W-LUN id
2230 */
2231static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2232{
2233 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2234}
2235
2236/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302237 * ufshcd_queuecommand - main entry point for SCSI requests
2238 * @cmd: command from SCSI Midlayer
2239 * @done: call back function
2240 *
2241 * Returns 0 for success, non-zero in case of failure
2242 */
2243static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2244{
2245 struct ufshcd_lrb *lrbp;
2246 struct ufs_hba *hba;
2247 unsigned long flags;
2248 int tag;
2249 int err = 0;
2250
2251 hba = shost_priv(host);
2252
2253 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002254 if (!ufshcd_valid_tag(hba, tag)) {
2255 dev_err(hba->dev,
2256 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2257 __func__, tag, cmd, cmd->request);
2258 BUG();
2259 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302260
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002261 if (!down_read_trylock(&hba->clk_scaling_lock))
2262 return SCSI_MLQUEUE_HOST_BUSY;
2263
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302264 spin_lock_irqsave(hba->host->host_lock, flags);
2265 switch (hba->ufshcd_state) {
2266 case UFSHCD_STATE_OPERATIONAL:
2267 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002268 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302269 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302270 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302271 goto out_unlock;
2272 case UFSHCD_STATE_ERROR:
2273 set_host_byte(cmd, DID_ERROR);
2274 cmd->scsi_done(cmd);
2275 goto out_unlock;
2276 default:
2277 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2278 __func__, hba->ufshcd_state);
2279 set_host_byte(cmd, DID_BAD_TARGET);
2280 cmd->scsi_done(cmd);
2281 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302282 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002283
2284 /* if error handling is in progress, don't issue commands */
2285 if (ufshcd_eh_in_progress(hba)) {
2286 set_host_byte(cmd, DID_ERROR);
2287 cmd->scsi_done(cmd);
2288 goto out_unlock;
2289 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302290 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302291
Gilad Broner7fabb772017-02-03 16:56:50 -08002292 hba->req_abort_count = 0;
2293
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302294 /* acquire the tag to make sure device cmds don't use it */
2295 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2296 /*
2297 * Dev manage command in progress, requeue the command.
2298 * Requeuing the command helps in cases where the request *may*
2299 * find different tag instead of waiting for dev manage command
2300 * completion.
2301 */
2302 err = SCSI_MLQUEUE_HOST_BUSY;
2303 goto out;
2304 }
2305
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002306 err = ufshcd_hold(hba, true);
2307 if (err) {
2308 err = SCSI_MLQUEUE_HOST_BUSY;
2309 clear_bit_unlock(tag, &hba->lrb_in_use);
2310 goto out;
2311 }
2312 WARN_ON(hba->clk_gating.state != CLKS_ON);
2313
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302314 lrbp = &hba->lrb[tag];
2315
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302316 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302317 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07002318 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302319 lrbp->sense_buffer = cmd->sense_buffer;
2320 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002321 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002322 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Gilad Bronere0b299e2017-02-03 16:56:40 -08002323 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302324
Joao Pinto300bb132016-05-11 12:21:27 +01002325 ufshcd_comp_scsi_upiu(hba, lrbp);
2326
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002327 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302328 if (err) {
2329 lrbp->cmd = NULL;
2330 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302331 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302332 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002333 /* Make sure descriptors are ready before ringing the doorbell */
2334 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302335
2336 /* issue command to the controller */
2337 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002338 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302339 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302340out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302341 spin_unlock_irqrestore(hba->host->host_lock, flags);
2342out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002343 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302344 return err;
2345}
2346
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302347static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2348 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2349{
2350 lrbp->cmd = NULL;
2351 lrbp->sense_bufflen = 0;
2352 lrbp->sense_buffer = NULL;
2353 lrbp->task_tag = tag;
2354 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302355 lrbp->intr_cmd = true; /* No interrupt aggregation */
2356 hba->dev_cmd.type = cmd_type;
2357
Joao Pinto300bb132016-05-11 12:21:27 +01002358 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302359}
2360
2361static int
2362ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2363{
2364 int err = 0;
2365 unsigned long flags;
2366 u32 mask = 1 << tag;
2367
2368 /* clear outstanding transaction before retry */
2369 spin_lock_irqsave(hba->host->host_lock, flags);
2370 ufshcd_utrl_clear(hba, tag);
2371 spin_unlock_irqrestore(hba->host->host_lock, flags);
2372
2373 /*
2374 * wait for for h/w to clear corresponding bit in door-bell.
2375 * max. wait is 1 sec.
2376 */
2377 err = ufshcd_wait_for_register(hba,
2378 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002379 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302380
2381 return err;
2382}
2383
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002384static int
2385ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2386{
2387 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2388
2389 /* Get the UPIU response */
2390 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2391 UPIU_RSP_CODE_OFFSET;
2392 return query_res->response;
2393}
2394
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302395/**
2396 * ufshcd_dev_cmd_completion() - handles device management command responses
2397 * @hba: per adapter instance
2398 * @lrbp: pointer to local reference block
2399 */
2400static int
2401ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2402{
2403 int resp;
2404 int err = 0;
2405
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002406 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302407 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2408
2409 switch (resp) {
2410 case UPIU_TRANSACTION_NOP_IN:
2411 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2412 err = -EINVAL;
2413 dev_err(hba->dev, "%s: unexpected response %x\n",
2414 __func__, resp);
2415 }
2416 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302417 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002418 err = ufshcd_check_query_response(hba, lrbp);
2419 if (!err)
2420 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302421 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302422 case UPIU_TRANSACTION_REJECT_UPIU:
2423 /* TODO: handle Reject UPIU Response */
2424 err = -EPERM;
2425 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2426 __func__);
2427 break;
2428 default:
2429 err = -EINVAL;
2430 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2431 __func__, resp);
2432 break;
2433 }
2434
2435 return err;
2436}
2437
2438static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2439 struct ufshcd_lrb *lrbp, int max_timeout)
2440{
2441 int err = 0;
2442 unsigned long time_left;
2443 unsigned long flags;
2444
2445 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2446 msecs_to_jiffies(max_timeout));
2447
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002448 /* Make sure descriptors are ready before ringing the doorbell */
2449 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302450 spin_lock_irqsave(hba->host->host_lock, flags);
2451 hba->dev_cmd.complete = NULL;
2452 if (likely(time_left)) {
2453 err = ufshcd_get_tr_ocs(lrbp);
2454 if (!err)
2455 err = ufshcd_dev_cmd_completion(hba, lrbp);
2456 }
2457 spin_unlock_irqrestore(hba->host->host_lock, flags);
2458
2459 if (!time_left) {
2460 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002461 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2462 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302463 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002464 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302465 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002466 /*
2467 * in case of an error, after clearing the doorbell,
2468 * we also need to clear the outstanding_request
2469 * field in hba
2470 */
2471 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302472 }
2473
2474 return err;
2475}
2476
2477/**
2478 * ufshcd_get_dev_cmd_tag - Get device management command tag
2479 * @hba: per-adapter instance
2480 * @tag: pointer to variable with available slot value
2481 *
2482 * Get a free slot and lock it until device management command
2483 * completes.
2484 *
2485 * Returns false if free slot is unavailable for locking, else
2486 * return true with tag value in @tag.
2487 */
2488static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
2489{
2490 int tag;
2491 bool ret = false;
2492 unsigned long tmp;
2493
2494 if (!tag_out)
2495 goto out;
2496
2497 do {
2498 tmp = ~hba->lrb_in_use;
2499 tag = find_last_bit(&tmp, hba->nutrs);
2500 if (tag >= hba->nutrs)
2501 goto out;
2502 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
2503
2504 *tag_out = tag;
2505 ret = true;
2506out:
2507 return ret;
2508}
2509
2510static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
2511{
2512 clear_bit_unlock(tag, &hba->lrb_in_use);
2513}
2514
2515/**
2516 * ufshcd_exec_dev_cmd - API for sending device management requests
2517 * @hba - UFS hba
2518 * @cmd_type - specifies the type (NOP, Query...)
2519 * @timeout - time in seconds
2520 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302521 * NOTE: Since there is only one available tag for device management commands,
2522 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302523 */
2524static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2525 enum dev_cmd_type cmd_type, int timeout)
2526{
2527 struct ufshcd_lrb *lrbp;
2528 int err;
2529 int tag;
2530 struct completion wait;
2531 unsigned long flags;
2532
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002533 down_read(&hba->clk_scaling_lock);
2534
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302535 /*
2536 * Get free slot, sleep if slots are unavailable.
2537 * Even though we use wait_event() which sleeps indefinitely,
2538 * the maximum wait time is bounded by SCSI request timeout.
2539 */
2540 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
2541
2542 init_completion(&wait);
2543 lrbp = &hba->lrb[tag];
2544 WARN_ON(lrbp->cmd);
2545 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2546 if (unlikely(err))
2547 goto out_put_tag;
2548
2549 hba->dev_cmd.complete = &wait;
2550
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002551 /* Make sure descriptors are ready before ringing the doorbell */
2552 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302553 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002554 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302555 ufshcd_send_command(hba, tag);
2556 spin_unlock_irqrestore(hba->host->host_lock, flags);
2557
2558 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2559
2560out_put_tag:
2561 ufshcd_put_dev_cmd_tag(hba, tag);
2562 wake_up(&hba->dev_cmd.tag_wq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002563 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302564 return err;
2565}
2566
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302567/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002568 * ufshcd_init_query() - init the query response and request parameters
2569 * @hba: per-adapter instance
2570 * @request: address of the request pointer to be initialized
2571 * @response: address of the response pointer to be initialized
2572 * @opcode: operation to perform
2573 * @idn: flag idn to access
2574 * @index: LU number to access
2575 * @selector: query/flag/descriptor further identification
2576 */
2577static inline void ufshcd_init_query(struct ufs_hba *hba,
2578 struct ufs_query_req **request, struct ufs_query_res **response,
2579 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2580{
2581 *request = &hba->dev_cmd.query.request;
2582 *response = &hba->dev_cmd.query.response;
2583 memset(*request, 0, sizeof(struct ufs_query_req));
2584 memset(*response, 0, sizeof(struct ufs_query_res));
2585 (*request)->upiu_req.opcode = opcode;
2586 (*request)->upiu_req.idn = idn;
2587 (*request)->upiu_req.index = index;
2588 (*request)->upiu_req.selector = selector;
2589}
2590
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002591static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2592 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2593{
2594 int ret;
2595 int retries;
2596
2597 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2598 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
2599 if (ret)
2600 dev_dbg(hba->dev,
2601 "%s: failed with error %d, retries %d\n",
2602 __func__, ret, retries);
2603 else
2604 break;
2605 }
2606
2607 if (ret)
2608 dev_err(hba->dev,
2609 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2610 __func__, opcode, idn, ret, retries);
2611 return ret;
2612}
2613
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002614/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302615 * ufshcd_query_flag() - API function for sending flag query requests
2616 * hba: per-adapter instance
2617 * query_opcode: flag query to perform
2618 * idn: flag idn to access
2619 * flag_res: the flag value after the query request completes
2620 *
2621 * Returns 0 for success, non-zero in case of failure
2622 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002623int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05302624 enum flag_idn idn, bool *flag_res)
2625{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002626 struct ufs_query_req *request = NULL;
2627 struct ufs_query_res *response = NULL;
2628 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002629 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302630
2631 BUG_ON(!hba);
2632
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002633 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302634 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002635 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2636 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302637
2638 switch (opcode) {
2639 case UPIU_QUERY_OPCODE_SET_FLAG:
2640 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2641 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2642 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2643 break;
2644 case UPIU_QUERY_OPCODE_READ_FLAG:
2645 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2646 if (!flag_res) {
2647 /* No dummy reads */
2648 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2649 __func__);
2650 err = -EINVAL;
2651 goto out_unlock;
2652 }
2653 break;
2654 default:
2655 dev_err(hba->dev,
2656 "%s: Expected query flag opcode but got = %d\n",
2657 __func__, opcode);
2658 err = -EINVAL;
2659 goto out_unlock;
2660 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302661
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002662 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302663
2664 if (err) {
2665 dev_err(hba->dev,
2666 "%s: Sending flag query for idn %d failed, err = %d\n",
2667 __func__, idn, err);
2668 goto out_unlock;
2669 }
2670
2671 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302672 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302673 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2674
2675out_unlock:
2676 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002677 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302678 return err;
2679}
2680
2681/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302682 * ufshcd_query_attr - API function for sending attribute requests
2683 * hba: per-adapter instance
2684 * opcode: attribute opcode
2685 * idn: attribute idn to access
2686 * index: index field
2687 * selector: selector field
2688 * attr_val: the attribute value after the query request completes
2689 *
2690 * Returns 0 for success, non-zero in case of failure
2691*/
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02002692int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
2693 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302694{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002695 struct ufs_query_req *request = NULL;
2696 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302697 int err;
2698
2699 BUG_ON(!hba);
2700
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002701 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302702 if (!attr_val) {
2703 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2704 __func__, opcode);
2705 err = -EINVAL;
2706 goto out;
2707 }
2708
2709 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002710 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2711 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302712
2713 switch (opcode) {
2714 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2715 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302716 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302717 break;
2718 case UPIU_QUERY_OPCODE_READ_ATTR:
2719 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2720 break;
2721 default:
2722 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2723 __func__, opcode);
2724 err = -EINVAL;
2725 goto out_unlock;
2726 }
2727
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002728 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302729
2730 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002731 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2732 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302733 goto out_unlock;
2734 }
2735
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302736 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302737
2738out_unlock:
2739 mutex_unlock(&hba->dev_cmd.lock);
2740out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002741 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302742 return err;
2743}
2744
2745/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002746 * ufshcd_query_attr_retry() - API function for sending query
2747 * attribute with retries
2748 * @hba: per-adapter instance
2749 * @opcode: attribute opcode
2750 * @idn: attribute idn to access
2751 * @index: index field
2752 * @selector: selector field
2753 * @attr_val: the attribute value after the query request
2754 * completes
2755 *
2756 * Returns 0 for success, non-zero in case of failure
2757*/
2758static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2759 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2760 u32 *attr_val)
2761{
2762 int ret = 0;
2763 u32 retries;
2764
2765 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2766 ret = ufshcd_query_attr(hba, opcode, idn, index,
2767 selector, attr_val);
2768 if (ret)
2769 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2770 __func__, ret, retries);
2771 else
2772 break;
2773 }
2774
2775 if (ret)
2776 dev_err(hba->dev,
2777 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2778 __func__, idn, ret, QUERY_REQ_RETRIES);
2779 return ret;
2780}
2781
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002782static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002783 enum query_opcode opcode, enum desc_idn idn, u8 index,
2784 u8 selector, u8 *desc_buf, int *buf_len)
2785{
2786 struct ufs_query_req *request = NULL;
2787 struct ufs_query_res *response = NULL;
2788 int err;
2789
2790 BUG_ON(!hba);
2791
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002792 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002793 if (!desc_buf) {
2794 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2795 __func__, opcode);
2796 err = -EINVAL;
2797 goto out;
2798 }
2799
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002800 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002801 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2802 __func__, *buf_len);
2803 err = -EINVAL;
2804 goto out;
2805 }
2806
2807 mutex_lock(&hba->dev_cmd.lock);
2808 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2809 selector);
2810 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002811 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002812
2813 switch (opcode) {
2814 case UPIU_QUERY_OPCODE_WRITE_DESC:
2815 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2816 break;
2817 case UPIU_QUERY_OPCODE_READ_DESC:
2818 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2819 break;
2820 default:
2821 dev_err(hba->dev,
2822 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2823 __func__, opcode);
2824 err = -EINVAL;
2825 goto out_unlock;
2826 }
2827
2828 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2829
2830 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002831 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2832 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002833 goto out_unlock;
2834 }
2835
2836 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002837 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002838
2839out_unlock:
2840 mutex_unlock(&hba->dev_cmd.lock);
2841out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002842 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002843 return err;
2844}
2845
2846/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002847 * ufshcd_query_descriptor_retry - API function for sending descriptor
2848 * requests
2849 * hba: per-adapter instance
2850 * opcode: attribute opcode
2851 * idn: attribute idn to access
2852 * index: index field
2853 * selector: selector field
2854 * desc_buf: the buffer that contains the descriptor
2855 * buf_len: length parameter passed to the device
2856 *
2857 * Returns 0 for success, non-zero in case of failure.
2858 * The buf_len parameter will contain, on return, the length parameter
2859 * received on the response.
2860 */
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02002861int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2862 enum query_opcode opcode,
2863 enum desc_idn idn, u8 index,
2864 u8 selector,
2865 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002866{
2867 int err;
2868 int retries;
2869
2870 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2871 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2872 selector, desc_buf, buf_len);
2873 if (!err || err == -EINVAL)
2874 break;
2875 }
2876
2877 return err;
2878}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002879
2880/**
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002881 * ufshcd_read_desc_length - read the specified descriptor length from header
2882 * @hba: Pointer to adapter instance
2883 * @desc_id: descriptor idn value
2884 * @desc_index: descriptor index
2885 * @desc_length: pointer to variable to read the length of descriptor
2886 *
2887 * Return 0 in case of success, non-zero otherwise
2888 */
2889static int ufshcd_read_desc_length(struct ufs_hba *hba,
2890 enum desc_idn desc_id,
2891 int desc_index,
2892 int *desc_length)
2893{
2894 int ret;
2895 u8 header[QUERY_DESC_HDR_SIZE];
2896 int header_len = QUERY_DESC_HDR_SIZE;
2897
2898 if (desc_id >= QUERY_DESC_IDN_MAX)
2899 return -EINVAL;
2900
2901 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2902 desc_id, desc_index, 0, header,
2903 &header_len);
2904
2905 if (ret) {
2906 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2907 __func__, desc_id);
2908 return ret;
2909 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
2910 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
2911 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
2912 desc_id);
2913 ret = -EINVAL;
2914 }
2915
2916 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
2917 return ret;
2918
2919}
2920
2921/**
2922 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
2923 * @hba: Pointer to adapter instance
2924 * @desc_id: descriptor idn value
2925 * @desc_len: mapped desc length (out)
2926 *
2927 * Return 0 in case of success, non-zero otherwise
2928 */
2929int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
2930 enum desc_idn desc_id, int *desc_len)
2931{
2932 switch (desc_id) {
2933 case QUERY_DESC_IDN_DEVICE:
2934 *desc_len = hba->desc_size.dev_desc;
2935 break;
2936 case QUERY_DESC_IDN_POWER:
2937 *desc_len = hba->desc_size.pwr_desc;
2938 break;
2939 case QUERY_DESC_IDN_GEOMETRY:
2940 *desc_len = hba->desc_size.geom_desc;
2941 break;
2942 case QUERY_DESC_IDN_CONFIGURATION:
2943 *desc_len = hba->desc_size.conf_desc;
2944 break;
2945 case QUERY_DESC_IDN_UNIT:
2946 *desc_len = hba->desc_size.unit_desc;
2947 break;
2948 case QUERY_DESC_IDN_INTERCONNECT:
2949 *desc_len = hba->desc_size.interc_desc;
2950 break;
2951 case QUERY_DESC_IDN_STRING:
2952 *desc_len = QUERY_DESC_MAX_SIZE;
2953 break;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02002954 case QUERY_DESC_IDN_HEALTH:
2955 *desc_len = hba->desc_size.hlth_desc;
2956 break;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002957 case QUERY_DESC_IDN_RFU_0:
2958 case QUERY_DESC_IDN_RFU_1:
2959 *desc_len = 0;
2960 break;
2961 default:
2962 *desc_len = 0;
2963 return -EINVAL;
2964 }
2965 return 0;
2966}
2967EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
2968
2969/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002970 * ufshcd_read_desc_param - read the specified descriptor parameter
2971 * @hba: Pointer to adapter instance
2972 * @desc_id: descriptor idn value
2973 * @desc_index: descriptor index
2974 * @param_offset: offset of the parameter to read
2975 * @param_read_buf: pointer to buffer where parameter would be read
2976 * @param_size: sizeof(param_read_buf)
2977 *
2978 * Return 0 in case of success, non-zero otherwise
2979 */
Stanislav Nijnikov45bced82018-02-15 14:14:02 +02002980int ufshcd_read_desc_param(struct ufs_hba *hba,
2981 enum desc_idn desc_id,
2982 int desc_index,
2983 u8 param_offset,
2984 u8 *param_read_buf,
2985 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002986{
2987 int ret;
2988 u8 *desc_buf;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002989 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002990 bool is_kmalloc = true;
2991
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002992 /* Safety check */
2993 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002994 return -EINVAL;
2995
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00002996 /* Get the max length of descriptor from structure filled up at probe
2997 * time.
2998 */
2999 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003000
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003001 /* Sanity checks */
3002 if (ret || !buff_len) {
3003 dev_err(hba->dev, "%s: Failed to get full descriptor length",
3004 __func__);
3005 return ret;
3006 }
3007
3008 /* Check whether we need temp memory */
3009 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003010 desc_buf = kmalloc(buff_len, GFP_KERNEL);
3011 if (!desc_buf)
3012 return -ENOMEM;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003013 } else {
3014 desc_buf = param_read_buf;
3015 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003016 }
3017
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003018 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02003019 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003020 desc_id, desc_index, 0,
3021 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003022
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003023 if (ret) {
3024 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
3025 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003026 goto out;
3027 }
3028
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003029 /* Sanity check */
3030 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
3031 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
3032 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
3033 ret = -EINVAL;
3034 goto out;
3035 }
3036
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00003037 /* Check wherher we will not copy more data, than available */
3038 if (is_kmalloc && param_size > buff_len)
3039 param_size = buff_len;
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08003040
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003041 if (is_kmalloc)
3042 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3043out:
3044 if (is_kmalloc)
3045 kfree(desc_buf);
3046 return ret;
3047}
3048
3049static inline int ufshcd_read_desc(struct ufs_hba *hba,
3050 enum desc_idn desc_id,
3051 int desc_index,
3052 u8 *buf,
3053 u32 size)
3054{
3055 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3056}
3057
3058static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3059 u8 *buf,
3060 u32 size)
3061{
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02003062 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003063}
3064
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003065static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
Yaniv Gardib573d482016-03-10 17:37:09 +02003066{
3067 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3068}
Yaniv Gardib573d482016-03-10 17:37:09 +02003069
3070/**
3071 * ufshcd_read_string_desc - read string descriptor
3072 * @hba: pointer to adapter instance
3073 * @desc_index: descriptor index
3074 * @buf: pointer to buffer where descriptor would be read
3075 * @size: size of buf
3076 * @ascii: if true convert from unicode to ascii characters
3077 *
3078 * Return 0 in case of success, non-zero otherwise
3079 */
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003080#define ASCII_STD true
Stanislav Nijnikov2238d312018-02-15 14:14:07 +02003081int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
3082 u8 *buf, u32 size, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003083{
3084 int err = 0;
3085
3086 err = ufshcd_read_desc(hba,
3087 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3088
3089 if (err) {
3090 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3091 __func__, QUERY_REQ_RETRIES, err);
3092 goto out;
3093 }
3094
3095 if (ascii) {
3096 int desc_len;
3097 int ascii_len;
3098 int i;
3099 char *buff_ascii;
3100
3101 desc_len = buf[0];
3102 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3103 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3104 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3105 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3106 __func__);
3107 err = -ENOMEM;
3108 goto out;
3109 }
3110
3111 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3112 if (!buff_ascii) {
3113 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003114 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003115 }
3116
3117 /*
3118 * the descriptor contains string in UTF16 format
3119 * we need to convert to utf-8 so it can be displayed
3120 */
3121 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3122 desc_len - QUERY_DESC_HDR_SIZE,
3123 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3124
3125 /* replace non-printable or non-ASCII characters with spaces */
3126 for (i = 0; i < ascii_len; i++)
3127 ufshcd_remove_non_printable(&buff_ascii[i]);
3128
3129 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3130 size - QUERY_DESC_HDR_SIZE);
3131 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3132 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02003133 kfree(buff_ascii);
3134 }
3135out:
3136 return err;
3137}
Yaniv Gardib573d482016-03-10 17:37:09 +02003138
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003139/**
3140 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3141 * @hba: Pointer to adapter instance
3142 * @lun: lun id
3143 * @param_offset: offset of the parameter to read
3144 * @param_read_buf: pointer to buffer where parameter would be read
3145 * @param_size: sizeof(param_read_buf)
3146 *
3147 * Return 0 in case of success, non-zero otherwise
3148 */
3149static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3150 int lun,
3151 enum unit_desc_param param_offset,
3152 u8 *param_read_buf,
3153 u32 param_size)
3154{
3155 /*
3156 * Unit descriptors are only available for general purpose LUs (LUN id
3157 * from 0 to 7) and RPMB Well known LU.
3158 */
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02003159 if (!ufs_is_valid_unit_desc_lun(lun))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003160 return -EOPNOTSUPP;
3161
3162 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3163 param_offset, param_read_buf, param_size);
3164}
3165
3166/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303167 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3168 * @hba: per adapter instance
3169 *
3170 * 1. Allocate DMA memory for Command Descriptor array
3171 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3172 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3173 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3174 * (UTMRDL)
3175 * 4. Allocate memory for local reference block(lrb).
3176 *
3177 * Returns 0 for success, non-zero in case of failure
3178 */
3179static int ufshcd_memory_alloc(struct ufs_hba *hba)
3180{
3181 size_t utmrdl_size, utrdl_size, ucdl_size;
3182
3183 /* Allocate memory for UTP command descriptors */
3184 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003185 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3186 ucdl_size,
3187 &hba->ucdl_dma_addr,
3188 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303189
3190 /*
3191 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3192 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3193 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3194 * be aligned to 128 bytes as well
3195 */
3196 if (!hba->ucdl_base_addr ||
3197 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303198 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303199 "Command Descriptor Memory allocation failed\n");
3200 goto out;
3201 }
3202
3203 /*
3204 * Allocate memory for UTP Transfer descriptors
3205 * UFSHCI requires 1024 byte alignment of UTRD
3206 */
3207 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003208 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3209 utrdl_size,
3210 &hba->utrdl_dma_addr,
3211 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303212 if (!hba->utrdl_base_addr ||
3213 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303214 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303215 "Transfer Descriptor Memory allocation failed\n");
3216 goto out;
3217 }
3218
3219 /*
3220 * Allocate memory for UTP Task Management descriptors
3221 * UFSHCI requires 1024 byte alignment of UTMRD
3222 */
3223 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003224 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3225 utmrdl_size,
3226 &hba->utmrdl_dma_addr,
3227 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303228 if (!hba->utmrdl_base_addr ||
3229 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303230 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303231 "Task Management Descriptor Memory allocation failed\n");
3232 goto out;
3233 }
3234
3235 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003236 hba->lrb = devm_kzalloc(hba->dev,
3237 hba->nutrs * sizeof(struct ufshcd_lrb),
3238 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303239 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303240 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303241 goto out;
3242 }
3243 return 0;
3244out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303245 return -ENOMEM;
3246}
3247
3248/**
3249 * ufshcd_host_memory_configure - configure local reference block with
3250 * memory offsets
3251 * @hba: per adapter instance
3252 *
3253 * Configure Host memory space
3254 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3255 * address.
3256 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3257 * and PRDT offset.
3258 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3259 * into local reference block.
3260 */
3261static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3262{
3263 struct utp_transfer_cmd_desc *cmd_descp;
3264 struct utp_transfer_req_desc *utrdlp;
3265 dma_addr_t cmd_desc_dma_addr;
3266 dma_addr_t cmd_desc_element_addr;
3267 u16 response_offset;
3268 u16 prdt_offset;
3269 int cmd_desc_size;
3270 int i;
3271
3272 utrdlp = hba->utrdl_base_addr;
3273 cmd_descp = hba->ucdl_base_addr;
3274
3275 response_offset =
3276 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3277 prdt_offset =
3278 offsetof(struct utp_transfer_cmd_desc, prd_table);
3279
3280 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3281 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3282
3283 for (i = 0; i < hba->nutrs; i++) {
3284 /* Configure UTRD with command descriptor base address */
3285 cmd_desc_element_addr =
3286 (cmd_desc_dma_addr + (cmd_desc_size * i));
3287 utrdlp[i].command_desc_base_addr_lo =
3288 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3289 utrdlp[i].command_desc_base_addr_hi =
3290 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3291
3292 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003293 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3294 utrdlp[i].response_upiu_offset =
3295 cpu_to_le16(response_offset);
3296 utrdlp[i].prd_table_offset =
3297 cpu_to_le16(prdt_offset);
3298 utrdlp[i].response_upiu_length =
3299 cpu_to_le16(ALIGNED_UPIU_SIZE);
3300 } else {
3301 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303302 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003303 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303304 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003305 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303306 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003307 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303308
3309 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003310 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3311 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303312 hba->lrb[i].ucd_req_ptr =
3313 (struct utp_upiu_req *)(cmd_descp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003314 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303315 hba->lrb[i].ucd_rsp_ptr =
3316 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003317 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3318 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303319 hba->lrb[i].ucd_prdt_ptr =
3320 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003321 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3322 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303323 }
3324}
3325
3326/**
3327 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3328 * @hba: per adapter instance
3329 *
3330 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3331 * in order to initialize the Unipro link startup procedure.
3332 * Once the Unipro links are up, the device connected to the controller
3333 * is detected.
3334 *
3335 * Returns 0 on success, non-zero value on failure
3336 */
3337static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3338{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303339 struct uic_command uic_cmd = {0};
3340 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303341
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303342 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3343
3344 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3345 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003346 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303347 "dme-link-startup: error code %d\n", ret);
3348 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303349}
3350
Yaniv Gardicad2e032015-03-31 17:37:14 +03003351static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3352{
3353 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3354 unsigned long min_sleep_time_us;
3355
3356 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3357 return;
3358
3359 /*
3360 * last_dme_cmd_tstamp will be 0 only for 1st call to
3361 * this function
3362 */
3363 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3364 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3365 } else {
3366 unsigned long delta =
3367 (unsigned long) ktime_to_us(
3368 ktime_sub(ktime_get(),
3369 hba->last_dme_cmd_tstamp));
3370
3371 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3372 min_sleep_time_us =
3373 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3374 else
3375 return; /* no more delay required */
3376 }
3377
3378 /* allow sleep for extra 50us if needed */
3379 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3380}
3381
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303382/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303383 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3384 * @hba: per adapter instance
3385 * @attr_sel: uic command argument1
3386 * @attr_set: attribute set type as uic command argument2
3387 * @mib_val: setting value as uic command argument3
3388 * @peer: indicate whether peer or local
3389 *
3390 * Returns 0 on success, non-zero value on failure
3391 */
3392int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3393 u8 attr_set, u32 mib_val, u8 peer)
3394{
3395 struct uic_command uic_cmd = {0};
3396 static const char *const action[] = {
3397 "dme-set",
3398 "dme-peer-set"
3399 };
3400 const char *set = action[!!peer];
3401 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003402 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303403
3404 uic_cmd.command = peer ?
3405 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3406 uic_cmd.argument1 = attr_sel;
3407 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3408 uic_cmd.argument3 = mib_val;
3409
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003410 do {
3411 /* for peer attributes we retry upon failure */
3412 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3413 if (ret)
3414 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3415 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3416 } while (ret && peer && --retries);
3417
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003418 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003419 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003420 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3421 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303422
3423 return ret;
3424}
3425EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3426
3427/**
3428 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3429 * @hba: per adapter instance
3430 * @attr_sel: uic command argument1
3431 * @mib_val: the value of the attribute as returned by the UIC command
3432 * @peer: indicate whether peer or local
3433 *
3434 * Returns 0 on success, non-zero value on failure
3435 */
3436int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3437 u32 *mib_val, u8 peer)
3438{
3439 struct uic_command uic_cmd = {0};
3440 static const char *const action[] = {
3441 "dme-get",
3442 "dme-peer-get"
3443 };
3444 const char *get = action[!!peer];
3445 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003446 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003447 struct ufs_pa_layer_attr orig_pwr_info;
3448 struct ufs_pa_layer_attr temp_pwr_info;
3449 bool pwr_mode_change = false;
3450
3451 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3452 orig_pwr_info = hba->pwr_info;
3453 temp_pwr_info = orig_pwr_info;
3454
3455 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3456 orig_pwr_info.pwr_rx == FAST_MODE) {
3457 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3458 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3459 pwr_mode_change = true;
3460 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3461 orig_pwr_info.pwr_rx == SLOW_MODE) {
3462 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3463 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3464 pwr_mode_change = true;
3465 }
3466 if (pwr_mode_change) {
3467 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3468 if (ret)
3469 goto out;
3470 }
3471 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303472
3473 uic_cmd.command = peer ?
3474 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3475 uic_cmd.argument1 = attr_sel;
3476
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003477 do {
3478 /* for peer attributes we retry upon failure */
3479 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3480 if (ret)
3481 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3482 get, UIC_GET_ATTR_ID(attr_sel), ret);
3483 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303484
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003485 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003486 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003487 get, UIC_GET_ATTR_ID(attr_sel),
3488 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003489
3490 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303491 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003492
3493 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3494 && pwr_mode_change)
3495 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303496out:
3497 return ret;
3498}
3499EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3500
3501/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003502 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3503 * state) and waits for it to take effect.
3504 *
3505 * @hba: per adapter instance
3506 * @cmd: UIC command to execute
3507 *
3508 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3509 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3510 * and device UniPro link and hence it's final completion would be indicated by
3511 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3512 * addition to normal UIC command completion Status (UCCS). This function only
3513 * returns after the relevant status bits indicate the completion.
3514 *
3515 * Returns 0 on success, non-zero value on failure
3516 */
3517static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3518{
3519 struct completion uic_async_done;
3520 unsigned long flags;
3521 u8 status;
3522 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003523 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003524
3525 mutex_lock(&hba->uic_cmd_mutex);
3526 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003527 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003528
3529 spin_lock_irqsave(hba->host->host_lock, flags);
3530 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003531 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3532 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3533 /*
3534 * Make sure UIC command completion interrupt is disabled before
3535 * issuing UIC command.
3536 */
3537 wmb();
3538 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003539 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003540 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3541 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003542 if (ret) {
3543 dev_err(hba->dev,
3544 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3545 cmd->command, cmd->argument3, ret);
3546 goto out;
3547 }
3548
3549 if (!wait_for_completion_timeout(hba->uic_async_done,
3550 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3551 dev_err(hba->dev,
3552 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3553 cmd->command, cmd->argument3);
3554 ret = -ETIMEDOUT;
3555 goto out;
3556 }
3557
3558 status = ufshcd_get_upmcrs(hba);
3559 if (status != PWR_LOCAL) {
3560 dev_err(hba->dev,
Zang Leigang479da362017-09-19 16:50:30 +08003561 "pwr ctrl cmd 0x%x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003562 cmd->command, status);
3563 ret = (status != PWR_OK) ? status : -1;
3564 }
3565out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003566 if (ret) {
3567 ufshcd_print_host_state(hba);
3568 ufshcd_print_pwr_info(hba);
3569 ufshcd_print_host_regs(hba);
3570 }
3571
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003572 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003573 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003574 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003575 if (reenable_intr)
3576 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003577 spin_unlock_irqrestore(hba->host->host_lock, flags);
3578 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003579
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003580 return ret;
3581}
3582
3583/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303584 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3585 * using DME_SET primitives.
3586 * @hba: per adapter instance
3587 * @mode: powr mode value
3588 *
3589 * Returns 0 on success, non-zero value on failure
3590 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303591static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303592{
3593 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003594 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303595
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003596 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3597 ret = ufshcd_dme_set(hba,
3598 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3599 if (ret) {
3600 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3601 __func__, ret);
3602 goto out;
3603 }
3604 }
3605
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303606 uic_cmd.command = UIC_CMD_DME_SET;
3607 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3608 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003609 ufshcd_hold(hba, false);
3610 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3611 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303612
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003613out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003614 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003615}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303616
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003617static int ufshcd_link_recovery(struct ufs_hba *hba)
3618{
3619 int ret;
3620 unsigned long flags;
3621
3622 spin_lock_irqsave(hba->host->host_lock, flags);
3623 hba->ufshcd_state = UFSHCD_STATE_RESET;
3624 ufshcd_set_eh_in_progress(hba);
3625 spin_unlock_irqrestore(hba->host->host_lock, flags);
3626
3627 ret = ufshcd_host_reset_and_restore(hba);
3628
3629 spin_lock_irqsave(hba->host->host_lock, flags);
3630 if (ret)
3631 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3632 ufshcd_clear_eh_in_progress(hba);
3633 spin_unlock_irqrestore(hba->host->host_lock, flags);
3634
3635 if (ret)
3636 dev_err(hba->dev, "%s: link recovery failed, err %d",
3637 __func__, ret);
3638
3639 return ret;
3640}
3641
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003642static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003643{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003644 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003645 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003646 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003647
Kiwoong Kimee32c902016-11-10 21:17:43 +09003648 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3649
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003650 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003651 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003652 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3653 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003654
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003655 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003656 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3657 __func__, ret);
3658
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003659 /*
3660 * If link recovery fails then return error so that caller
3661 * don't retry the hibern8 enter again.
3662 */
3663 if (ufshcd_link_recovery(hba))
3664 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09003665 } else
3666 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3667 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003668
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003669 return ret;
3670}
3671
3672static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3673{
3674 int ret = 0, retries;
3675
3676 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3677 ret = __ufshcd_uic_hibern8_enter(hba);
3678 if (!ret || ret == -ENOLINK)
3679 goto out;
3680 }
3681out:
3682 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003683}
3684
3685static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
3686{
3687 struct uic_command uic_cmd = {0};
3688 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003689 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003690
Kiwoong Kimee32c902016-11-10 21:17:43 +09003691 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3692
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003693 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3694 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003695 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3696 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3697
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303698 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003699 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3700 __func__, ret);
3701 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003702 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003703 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3704 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003705 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3706 hba->ufs_stats.hibern8_exit_cnt++;
3707 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303708
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303709 return ret;
3710}
3711
Yaniv Gardi50646362014-10-23 13:25:13 +03003712 /**
3713 * ufshcd_init_pwr_info - setting the POR (power on reset)
3714 * values in hba power info
3715 * @hba: per-adapter instance
3716 */
3717static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3718{
3719 hba->pwr_info.gear_rx = UFS_PWM_G1;
3720 hba->pwr_info.gear_tx = UFS_PWM_G1;
3721 hba->pwr_info.lane_rx = 1;
3722 hba->pwr_info.lane_tx = 1;
3723 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3724 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3725 hba->pwr_info.hs_rate = 0;
3726}
3727
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303728/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003729 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3730 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303731 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003732static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303733{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003734 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3735
3736 if (hba->max_pwr_info.is_valid)
3737 return 0;
3738
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003739 pwr_info->pwr_tx = FAST_MODE;
3740 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003741 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303742
3743 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003744 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3745 &pwr_info->lane_rx);
3746 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3747 &pwr_info->lane_tx);
3748
3749 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3750 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3751 __func__,
3752 pwr_info->lane_rx,
3753 pwr_info->lane_tx);
3754 return -EINVAL;
3755 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303756
3757 /*
3758 * First, get the maximum gears of HS speed.
3759 * If a zero value, it means there is no HSGEAR capability.
3760 * Then, get the maximum gears of PWM speed.
3761 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003762 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3763 if (!pwr_info->gear_rx) {
3764 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3765 &pwr_info->gear_rx);
3766 if (!pwr_info->gear_rx) {
3767 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3768 __func__, pwr_info->gear_rx);
3769 return -EINVAL;
3770 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003771 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303772 }
3773
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003774 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3775 &pwr_info->gear_tx);
3776 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303777 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003778 &pwr_info->gear_tx);
3779 if (!pwr_info->gear_tx) {
3780 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3781 __func__, pwr_info->gear_tx);
3782 return -EINVAL;
3783 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003784 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003785 }
3786
3787 hba->max_pwr_info.is_valid = true;
3788 return 0;
3789}
3790
3791static int ufshcd_change_power_mode(struct ufs_hba *hba,
3792 struct ufs_pa_layer_attr *pwr_mode)
3793{
3794 int ret;
3795
3796 /* if already configured to the requested pwr_mode */
3797 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
3798 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
3799 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
3800 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
3801 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
3802 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
3803 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
3804 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
3805 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303806 }
3807
3808 /*
3809 * Configure attributes for power mode change with below.
3810 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3811 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3812 * - PA_HSSERIES
3813 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003814 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
3815 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
3816 pwr_mode->lane_rx);
3817 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3818 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303819 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003820 else
3821 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303822
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003823 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
3824 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
3825 pwr_mode->lane_tx);
3826 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
3827 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303828 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003829 else
3830 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303831
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003832 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3833 pwr_mode->pwr_tx == FASTAUTO_MODE ||
3834 pwr_mode->pwr_rx == FAST_MODE ||
3835 pwr_mode->pwr_tx == FAST_MODE)
3836 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
3837 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303838
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003839 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
3840 | pwr_mode->pwr_tx);
3841
3842 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303843 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003844 "%s: power mode change failed %d\n", __func__, ret);
3845 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003846 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
3847 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003848
3849 memcpy(&hba->pwr_info, pwr_mode,
3850 sizeof(struct ufs_pa_layer_attr));
3851 }
3852
3853 return ret;
3854}
3855
3856/**
3857 * ufshcd_config_pwr_mode - configure a new power mode
3858 * @hba: per-adapter instance
3859 * @desired_pwr_mode: desired power configuration
3860 */
3861static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3862 struct ufs_pa_layer_attr *desired_pwr_mode)
3863{
3864 struct ufs_pa_layer_attr final_params = { 0 };
3865 int ret;
3866
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003867 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3868 desired_pwr_mode, &final_params);
3869
3870 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003871 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3872
3873 ret = ufshcd_change_power_mode(hba, &final_params);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08003874 if (!ret)
3875 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303876
3877 return ret;
3878}
3879
3880/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303881 * ufshcd_complete_dev_init() - checks device readiness
3882 * hba: per-adapter instance
3883 *
3884 * Set fDeviceInit flag and poll until device toggles it.
3885 */
3886static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3887{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003888 int i;
3889 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303890 bool flag_res = 1;
3891
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003892 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3893 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303894 if (err) {
3895 dev_err(hba->dev,
3896 "%s setting fDeviceInit flag failed with error %d\n",
3897 __func__, err);
3898 goto out;
3899 }
3900
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003901 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3902 for (i = 0; i < 1000 && !err && flag_res; i++)
3903 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3904 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3905
Dolev Raviv68078d52013-07-30 00:35:58 +05303906 if (err)
3907 dev_err(hba->dev,
3908 "%s reading fDeviceInit flag failed with error %d\n",
3909 __func__, err);
3910 else if (flag_res)
3911 dev_err(hba->dev,
3912 "%s fDeviceInit was not cleared by the device\n",
3913 __func__);
3914
3915out:
3916 return err;
3917}
3918
3919/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303920 * ufshcd_make_hba_operational - Make UFS controller operational
3921 * @hba: per adapter instance
3922 *
3923 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003924 * 1. Enable required interrupts
3925 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003926 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003927 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303928 *
3929 * Returns 0 on success, non-zero value on failure
3930 */
3931static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3932{
3933 int err = 0;
3934 u32 reg;
3935
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303936 /* Enable required interrupts */
3937 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3938
3939 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003940 if (ufshcd_is_intr_aggr_allowed(hba))
3941 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3942 else
3943 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303944
3945 /* Configure UTRL and UTMRL base address registers */
3946 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3947 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3948 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3949 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3950 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3951 REG_UTP_TASK_REQ_LIST_BASE_L);
3952 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3953 REG_UTP_TASK_REQ_LIST_BASE_H);
3954
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303955 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003956 * Make sure base address and interrupt setup are updated before
3957 * enabling the run/stop registers below.
3958 */
3959 wmb();
3960
3961 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303962 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303963 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003964 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303965 if (!(ufshcd_get_lists_status(reg))) {
3966 ufshcd_enable_run_stop_reg(hba);
3967 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303968 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303969 "Host controller not ready to process requests");
3970 err = -EIO;
3971 goto out;
3972 }
3973
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303974out:
3975 return err;
3976}
3977
3978/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003979 * ufshcd_hba_stop - Send controller to reset state
3980 * @hba: per adapter instance
3981 * @can_sleep: perform sleep or just spin
3982 */
3983static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3984{
3985 int err;
3986
3987 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3988 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3989 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3990 10, 1, can_sleep);
3991 if (err)
3992 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3993}
3994
3995/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303996 * ufshcd_hba_enable - initialize the controller
3997 * @hba: per adapter instance
3998 *
3999 * The controller resets itself and controller firmware initialization
4000 * sequence kicks off. When controller is ready it will set
4001 * the Host Controller Enable bit to 1.
4002 *
4003 * Returns 0 on success, non-zero value on failure
4004 */
4005static int ufshcd_hba_enable(struct ufs_hba *hba)
4006{
4007 int retry;
4008
4009 /*
4010 * msleep of 1 and 5 used in this function might result in msleep(20),
4011 * but it was necessary to send the UFS FPGA to reset mode during
4012 * development and testing of this driver. msleep can be changed to
4013 * mdelay and retry count can be reduced based on the controller.
4014 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004015 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304016 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004017 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304018
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004019 /* UniPro link is disabled at this point */
4020 ufshcd_set_link_off(hba);
4021
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004022 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004023
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304024 /* start controller initialization sequence */
4025 ufshcd_hba_start(hba);
4026
4027 /*
4028 * To initialize a UFS host controller HCE bit must be set to 1.
4029 * During initialization the HCE bit value changes from 1->0->1.
4030 * When the host controller completes initialization sequence
4031 * it sets the value of HCE bit to 1. The same HCE bit is read back
4032 * to check if the controller has completed initialization sequence.
4033 * So without this delay the value HCE = 1, set in the previous
4034 * instruction might be read back.
4035 * This delay can be changed based on the controller.
4036 */
4037 msleep(1);
4038
4039 /* wait for the host controller to complete initialization */
4040 retry = 10;
4041 while (ufshcd_is_hba_active(hba)) {
4042 if (retry) {
4043 retry--;
4044 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304045 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304046 "Controller enable failed\n");
4047 return -EIO;
4048 }
4049 msleep(5);
4050 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004051
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004052 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004053 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004054
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004055 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004056
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304057 return 0;
4058}
4059
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004060static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4061{
4062 int tx_lanes, i, err = 0;
4063
4064 if (!peer)
4065 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4066 &tx_lanes);
4067 else
4068 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4069 &tx_lanes);
4070 for (i = 0; i < tx_lanes; i++) {
4071 if (!peer)
4072 err = ufshcd_dme_set(hba,
4073 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4074 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4075 0);
4076 else
4077 err = ufshcd_dme_peer_set(hba,
4078 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4079 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4080 0);
4081 if (err) {
4082 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4083 __func__, peer, i, err);
4084 break;
4085 }
4086 }
4087
4088 return err;
4089}
4090
4091static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4092{
4093 return ufshcd_disable_tx_lcc(hba, true);
4094}
4095
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304096/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304097 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304098 * @hba: per adapter instance
4099 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304100 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304101 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304102static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304103{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304104 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004105 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004106 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304107
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004108 /*
4109 * If UFS device isn't active then we will have to issue link startup
4110 * 2 times to make sure the device state move to active.
4111 */
4112 if (!ufshcd_is_ufs_dev_active(hba))
4113 link_startup_again = true;
4114
4115link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004116 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004117 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304118
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004119 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004120
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004121 /* check if device is detected by inter-connect layer */
4122 if (!ret && !ufshcd_is_device_present(hba)) {
4123 dev_err(hba->dev, "%s: Device not present\n", __func__);
4124 ret = -ENXIO;
4125 goto out;
4126 }
4127
4128 /*
4129 * DME link lost indication is only received when link is up,
4130 * but we can't be sure if the link is up until link startup
4131 * succeeds. So reset the local Uni-Pro and try again.
4132 */
4133 if (ret && ufshcd_hba_enable(hba))
4134 goto out;
4135 } while (ret && retries--);
4136
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304137 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004138 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304139 goto out;
4140
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004141 if (link_startup_again) {
4142 link_startup_again = false;
4143 retries = DME_LINKSTARTUP_RETRIES;
4144 goto link_startup;
4145 }
4146
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004147 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4148 ufshcd_init_pwr_info(hba);
4149 ufshcd_print_pwr_info(hba);
4150
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004151 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4152 ret = ufshcd_disable_device_tx_lcc(hba);
4153 if (ret)
4154 goto out;
4155 }
4156
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004157 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004158 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4159 if (ret)
4160 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004161
4162 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304163out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004164 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304165 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004166 ufshcd_print_host_state(hba);
4167 ufshcd_print_pwr_info(hba);
4168 ufshcd_print_host_regs(hba);
4169 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304170 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304171}
4172
4173/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304174 * ufshcd_verify_dev_init() - Verify device initialization
4175 * @hba: per-adapter instance
4176 *
4177 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4178 * device Transport Protocol (UTP) layer is ready after a reset.
4179 * If the UTP layer at the device side is not initialized, it may
4180 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4181 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4182 */
4183static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4184{
4185 int err = 0;
4186 int retries;
4187
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004188 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304189 mutex_lock(&hba->dev_cmd.lock);
4190 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4191 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4192 NOP_OUT_TIMEOUT);
4193
4194 if (!err || err == -ETIMEDOUT)
4195 break;
4196
4197 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4198 }
4199 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004200 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304201
4202 if (err)
4203 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4204 return err;
4205}
4206
4207/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004208 * ufshcd_set_queue_depth - set lun queue depth
4209 * @sdev: pointer to SCSI device
4210 *
4211 * Read bLUQueueDepth value and activate scsi tagged command
4212 * queueing. For WLUN, queue depth is set to 1. For best-effort
4213 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4214 * value that host can queue.
4215 */
4216static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4217{
4218 int ret = 0;
4219 u8 lun_qdepth;
4220 struct ufs_hba *hba;
4221
4222 hba = shost_priv(sdev->host);
4223
4224 lun_qdepth = hba->nutrs;
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02004225 ret = ufshcd_read_unit_desc_param(hba,
4226 ufshcd_scsi_to_upiu_lun(sdev->lun),
4227 UNIT_DESC_PARAM_LU_Q_DEPTH,
4228 &lun_qdepth,
4229 sizeof(lun_qdepth));
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004230
4231 /* Some WLUN doesn't support unit descriptor */
4232 if (ret == -EOPNOTSUPP)
4233 lun_qdepth = 1;
4234 else if (!lun_qdepth)
4235 /* eventually, we can figure out the real queue depth */
4236 lun_qdepth = hba->nutrs;
4237 else
4238 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4239
4240 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4241 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004242 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004243}
4244
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004245/*
4246 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4247 * @hba: per-adapter instance
4248 * @lun: UFS device lun id
4249 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4250 *
4251 * Returns 0 in case of success and b_lu_write_protect status would be returned
4252 * @b_lu_write_protect parameter.
4253 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4254 * Returns -EINVAL in case of invalid parameters passed to this function.
4255 */
4256static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4257 u8 lun,
4258 u8 *b_lu_write_protect)
4259{
4260 int ret;
4261
4262 if (!b_lu_write_protect)
4263 ret = -EINVAL;
4264 /*
4265 * According to UFS device spec, RPMB LU can't be write
4266 * protected so skip reading bLUWriteProtect parameter for
4267 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4268 */
4269 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4270 ret = -ENOTSUPP;
4271 else
4272 ret = ufshcd_read_unit_desc_param(hba,
4273 lun,
4274 UNIT_DESC_PARAM_LU_WR_PROTECT,
4275 b_lu_write_protect,
4276 sizeof(*b_lu_write_protect));
4277 return ret;
4278}
4279
4280/**
4281 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4282 * status
4283 * @hba: per-adapter instance
4284 * @sdev: pointer to SCSI device
4285 *
4286 */
4287static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4288 struct scsi_device *sdev)
4289{
4290 if (hba->dev_info.f_power_on_wp_en &&
4291 !hba->dev_info.is_lu_power_on_wp) {
4292 u8 b_lu_write_protect;
4293
4294 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4295 &b_lu_write_protect) &&
4296 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4297 hba->dev_info.is_lu_power_on_wp = true;
4298 }
4299}
4300
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004301/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304302 * ufshcd_slave_alloc - handle initial SCSI device configurations
4303 * @sdev: pointer to SCSI device
4304 *
4305 * Returns success
4306 */
4307static int ufshcd_slave_alloc(struct scsi_device *sdev)
4308{
4309 struct ufs_hba *hba;
4310
4311 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304312
4313 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4314 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304315
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304316 /* allow SCSI layer to restart the device in case of errors */
4317 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004318
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004319 /* REPORT SUPPORTED OPERATION CODES is not supported */
4320 sdev->no_report_opcodes = 1;
4321
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004322
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004323 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004324
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004325 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4326
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004327 return 0;
4328}
4329
4330/**
4331 * ufshcd_change_queue_depth - change queue depth
4332 * @sdev: pointer to SCSI device
4333 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004334 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004335 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004336 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004337static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004338{
4339 struct ufs_hba *hba = shost_priv(sdev->host);
4340
4341 if (depth > hba->nutrs)
4342 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004343 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304344}
4345
4346/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004347 * ufshcd_slave_configure - adjust SCSI device configurations
4348 * @sdev: pointer to SCSI device
4349 */
4350static int ufshcd_slave_configure(struct scsi_device *sdev)
4351{
4352 struct request_queue *q = sdev->request_queue;
4353
4354 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4355 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
4356
4357 return 0;
4358}
4359
4360/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304361 * ufshcd_slave_destroy - remove SCSI device configurations
4362 * @sdev: pointer to SCSI device
4363 */
4364static void ufshcd_slave_destroy(struct scsi_device *sdev)
4365{
4366 struct ufs_hba *hba;
4367
4368 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004369 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004370 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4371 unsigned long flags;
4372
4373 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004374 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004375 spin_unlock_irqrestore(hba->host->host_lock, flags);
4376 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304377}
4378
4379/**
4380 * ufshcd_task_req_compl - handle task management request completion
4381 * @hba: per adapter instance
4382 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304383 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304384 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304385 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304386 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304387static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304388{
4389 struct utp_task_req_desc *task_req_descp;
4390 struct utp_upiu_task_rsp *task_rsp_upiup;
4391 unsigned long flags;
4392 int ocs_value;
4393 int task_result;
4394
4395 spin_lock_irqsave(hba->host->host_lock, flags);
4396
4397 /* Clear completed tasks from outstanding_tasks */
4398 __clear_bit(index, &hba->outstanding_tasks);
4399
4400 task_req_descp = hba->utmrdl_base_addr;
4401 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
4402
4403 if (ocs_value == OCS_SUCCESS) {
4404 task_rsp_upiup = (struct utp_upiu_task_rsp *)
4405 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09004406 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
4407 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304408 if (resp)
4409 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304410 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304411 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
4412 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304413 }
4414 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304415
4416 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304417}
4418
4419/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304420 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4421 * @lrb: pointer to local reference block of completed command
4422 * @scsi_status: SCSI command status
4423 *
4424 * Returns value base on SCSI command status
4425 */
4426static inline int
4427ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4428{
4429 int result = 0;
4430
4431 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304432 case SAM_STAT_CHECK_CONDITION:
4433 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304434 case SAM_STAT_GOOD:
4435 result |= DID_OK << 16 |
4436 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304437 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304438 break;
4439 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304440 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304441 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304442 ufshcd_copy_sense_data(lrbp);
4443 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304444 break;
4445 default:
4446 result |= DID_ERROR << 16;
4447 break;
4448 } /* end of switch */
4449
4450 return result;
4451}
4452
4453/**
4454 * ufshcd_transfer_rsp_status - Get overall status of the response
4455 * @hba: per adapter instance
4456 * @lrb: pointer to local reference block of completed command
4457 *
4458 * Returns result of the command to notify SCSI midlayer
4459 */
4460static inline int
4461ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4462{
4463 int result = 0;
4464 int scsi_status;
4465 int ocs;
4466
4467 /* overall command status of utrd */
4468 ocs = ufshcd_get_tr_ocs(lrbp);
4469
4470 switch (ocs) {
4471 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304472 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004473 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304474 switch (result) {
4475 case UPIU_TRANSACTION_RESPONSE:
4476 /*
4477 * get the response UPIU result to extract
4478 * the SCSI command status
4479 */
4480 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4481
4482 /*
4483 * get the result based on SCSI status response
4484 * to notify the SCSI midlayer of the command status
4485 */
4486 scsi_status = result & MASK_SCSI_STATUS;
4487 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304488
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004489 /*
4490 * Currently we are only supporting BKOPs exception
4491 * events hence we can ignore BKOPs exception event
4492 * during power management callbacks. BKOPs exception
4493 * event is not expected to be raised in runtime suspend
4494 * callback as it allows the urgent bkops.
4495 * During system suspend, we are anyway forcefully
4496 * disabling the bkops and if urgent bkops is needed
4497 * it will be enabled on system resume. Long term
4498 * solution could be to abort the system suspend if
4499 * UFS device needs urgent BKOPs.
4500 */
4501 if (!hba->pm_op_in_progress &&
4502 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304503 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304504 break;
4505 case UPIU_TRANSACTION_REJECT_UPIU:
4506 /* TODO: handle Reject UPIU Response */
4507 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304508 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304509 "Reject UPIU not fully implemented\n");
4510 break;
4511 default:
4512 result = DID_ERROR << 16;
4513 dev_err(hba->dev,
4514 "Unexpected request response code = %x\n",
4515 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304516 break;
4517 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304518 break;
4519 case OCS_ABORTED:
4520 result |= DID_ABORT << 16;
4521 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304522 case OCS_INVALID_COMMAND_STATUS:
4523 result |= DID_REQUEUE << 16;
4524 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304525 case OCS_INVALID_CMD_TABLE_ATTR:
4526 case OCS_INVALID_PRDT_ATTR:
4527 case OCS_MISMATCH_DATA_BUF_SIZE:
4528 case OCS_MISMATCH_RESP_UPIU_SIZE:
4529 case OCS_PEER_COMM_FAILURE:
4530 case OCS_FATAL_ERROR:
4531 default:
4532 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304533 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004534 "OCS error from controller = %x for tag %d\n",
4535 ocs, lrbp->task_tag);
4536 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004537 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304538 break;
4539 } /* end of switch */
4540
Dolev Raviv66cc8202016-12-22 18:39:42 -08004541 if (host_byte(result) != DID_OK)
4542 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304543 return result;
4544}
4545
4546/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304547 * ufshcd_uic_cmd_compl - handle completion of uic command
4548 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304549 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304550 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304551static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304552{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304553 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304554 hba->active_uic_cmd->argument2 |=
4555 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304556 hba->active_uic_cmd->argument3 =
4557 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304558 complete(&hba->active_uic_cmd->done);
4559 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304560
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004561 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
4562 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304563}
4564
4565/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004566 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304567 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004568 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304569 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004570static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4571 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304572{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304573 struct ufshcd_lrb *lrbp;
4574 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304575 int result;
4576 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004577
Dolev Ravive9d501b2014-07-01 12:22:37 +03004578 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4579 lrbp = &hba->lrb[index];
4580 cmd = lrbp->cmd;
4581 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004582 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004583 result = ufshcd_transfer_rsp_status(hba, lrbp);
4584 scsi_dma_unmap(cmd);
4585 cmd->result = result;
4586 /* Mark completed command as NULL in LRB */
4587 lrbp->cmd = NULL;
4588 clear_bit_unlock(index, &hba->lrb_in_use);
4589 /* Do not touch lrbp after scsi done */
4590 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004591 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004592 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4593 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004594 if (hba->dev_cmd.complete) {
4595 ufshcd_add_command_trace(hba, index,
4596 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004597 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004598 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004599 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004600 if (ufshcd_is_clkscaling_supported(hba))
4601 hba->clk_scaling.active_reqs--;
Zang Leigang09017182017-09-27 10:06:06 +08004602
4603 lrbp->compl_time_stamp = ktime_get();
Dolev Ravive9d501b2014-07-01 12:22:37 +03004604 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304605
4606 /* clear corresponding bits of completed commands */
4607 hba->outstanding_reqs ^= completed_reqs;
4608
Sahitya Tummala856b3482014-09-25 15:32:34 +03004609 ufshcd_clk_scaling_update_busy(hba);
4610
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304611 /* we might have free'd some tags above */
4612 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304613}
4614
4615/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004616 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4617 * @hba: per adapter instance
4618 */
4619static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
4620{
4621 unsigned long completed_reqs;
4622 u32 tr_doorbell;
4623
4624 /* Resetting interrupt aggregation counters first and reading the
4625 * DOOR_BELL afterward allows us to handle all the completed requests.
4626 * In order to prevent other interrupts starvation the DB is read once
4627 * after reset. The down side of this solution is the possibility of
4628 * false interrupt if device completes another request after resetting
4629 * aggregation and before reading the DB.
4630 */
4631 if (ufshcd_is_intr_aggr_allowed(hba))
4632 ufshcd_reset_intr_aggr(hba);
4633
4634 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4635 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4636
4637 __ufshcd_transfer_req_compl(hba, completed_reqs);
4638}
4639
4640/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304641 * ufshcd_disable_ee - disable exception event
4642 * @hba: per-adapter instance
4643 * @mask: exception event to disable
4644 *
4645 * Disables exception event in the device so that the EVENT_ALERT
4646 * bit is not set.
4647 *
4648 * Returns zero on success, non-zero error value on failure.
4649 */
4650static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4651{
4652 int err = 0;
4653 u32 val;
4654
4655 if (!(hba->ee_ctrl_mask & mask))
4656 goto out;
4657
4658 val = hba->ee_ctrl_mask & ~mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004659 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004660 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304661 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4662 if (!err)
4663 hba->ee_ctrl_mask &= ~mask;
4664out:
4665 return err;
4666}
4667
4668/**
4669 * ufshcd_enable_ee - enable exception event
4670 * @hba: per-adapter instance
4671 * @mask: exception event to enable
4672 *
4673 * Enable corresponding exception event in the device to allow
4674 * device to alert host in critical scenarios.
4675 *
4676 * Returns zero on success, non-zero error value on failure.
4677 */
4678static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4679{
4680 int err = 0;
4681 u32 val;
4682
4683 if (hba->ee_ctrl_mask & mask)
4684 goto out;
4685
4686 val = hba->ee_ctrl_mask | mask;
Tomohiro Kusumid7e2ddd2017-04-20 15:01:44 +03004687 val &= MASK_EE_STATUS;
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004688 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304689 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4690 if (!err)
4691 hba->ee_ctrl_mask |= mask;
4692out:
4693 return err;
4694}
4695
4696/**
4697 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4698 * @hba: per-adapter instance
4699 *
4700 * Allow device to manage background operations on its own. Enabling
4701 * this might lead to inconsistent latencies during normal data transfers
4702 * as the device is allowed to manage its own way of handling background
4703 * operations.
4704 *
4705 * Returns zero on success, non-zero on failure.
4706 */
4707static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4708{
4709 int err = 0;
4710
4711 if (hba->auto_bkops_enabled)
4712 goto out;
4713
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004714 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304715 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4716 if (err) {
4717 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4718 __func__, err);
4719 goto out;
4720 }
4721
4722 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004723 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304724
4725 /* No need of URGENT_BKOPS exception from the device */
4726 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4727 if (err)
4728 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4729 __func__, err);
4730out:
4731 return err;
4732}
4733
4734/**
4735 * ufshcd_disable_auto_bkops - block device in doing background operations
4736 * @hba: per-adapter instance
4737 *
4738 * Disabling background operations improves command response latency but
4739 * has drawback of device moving into critical state where the device is
4740 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4741 * host is idle so that BKOPS are managed effectively without any negative
4742 * impacts.
4743 *
4744 * Returns zero on success, non-zero on failure.
4745 */
4746static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
4747{
4748 int err = 0;
4749
4750 if (!hba->auto_bkops_enabled)
4751 goto out;
4752
4753 /*
4754 * If host assisted BKOPs is to be enabled, make sure
4755 * urgent bkops exception is allowed.
4756 */
4757 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
4758 if (err) {
4759 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
4760 __func__, err);
4761 goto out;
4762 }
4763
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004764 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304765 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4766 if (err) {
4767 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
4768 __func__, err);
4769 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4770 goto out;
4771 }
4772
4773 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004774 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304775out:
4776 return err;
4777}
4778
4779/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004780 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304781 * @hba: per adapter instance
4782 *
4783 * After a device reset the device may toggle the BKOPS_EN flag
4784 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004785 * as well. This function would change the auto-bkops state based on
4786 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304787 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004788static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304789{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004790 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
4791 hba->auto_bkops_enabled = false;
4792 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
4793 ufshcd_enable_auto_bkops(hba);
4794 } else {
4795 hba->auto_bkops_enabled = true;
4796 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
4797 ufshcd_disable_auto_bkops(hba);
4798 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304799}
4800
4801static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
4802{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004803 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304804 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
4805}
4806
4807/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004808 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4809 * @hba: per-adapter instance
4810 * @status: bkops_status value
4811 *
4812 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4813 * flag in the device to permit background operations if the device
4814 * bkops_status is greater than or equal to "status" argument passed to
4815 * this function, disable otherwise.
4816 *
4817 * Returns 0 for success, non-zero in case of failure.
4818 *
4819 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4820 * to know whether auto bkops is enabled or disabled after this function
4821 * returns control to it.
4822 */
4823static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
4824 enum bkops_status status)
4825{
4826 int err;
4827 u32 curr_status = 0;
4828
4829 err = ufshcd_get_bkops_status(hba, &curr_status);
4830 if (err) {
4831 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4832 __func__, err);
4833 goto out;
4834 } else if (curr_status > BKOPS_STATUS_MAX) {
4835 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
4836 __func__, curr_status);
4837 err = -EINVAL;
4838 goto out;
4839 }
4840
4841 if (curr_status >= status)
4842 err = ufshcd_enable_auto_bkops(hba);
4843 else
4844 err = ufshcd_disable_auto_bkops(hba);
4845out:
4846 return err;
4847}
4848
4849/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304850 * ufshcd_urgent_bkops - handle urgent bkops exception event
4851 * @hba: per-adapter instance
4852 *
4853 * Enable fBackgroundOpsEn flag in the device to permit background
4854 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004855 *
4856 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4857 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304858 */
4859static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4860{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004861 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304862}
4863
4864static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4865{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004866 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304867 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4868}
4869
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004870static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4871{
4872 int err;
4873 u32 curr_status = 0;
4874
4875 if (hba->is_urgent_bkops_lvl_checked)
4876 goto enable_auto_bkops;
4877
4878 err = ufshcd_get_bkops_status(hba, &curr_status);
4879 if (err) {
4880 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4881 __func__, err);
4882 goto out;
4883 }
4884
4885 /*
4886 * We are seeing that some devices are raising the urgent bkops
4887 * exception events even when BKOPS status doesn't indicate performace
4888 * impacted or critical. Handle these device by determining their urgent
4889 * bkops status at runtime.
4890 */
4891 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4892 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4893 __func__, curr_status);
4894 /* update the current status as the urgent bkops level */
4895 hba->urgent_bkops_lvl = curr_status;
4896 hba->is_urgent_bkops_lvl_checked = true;
4897 }
4898
4899enable_auto_bkops:
4900 err = ufshcd_enable_auto_bkops(hba);
4901out:
4902 if (err < 0)
4903 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4904 __func__, err);
4905}
4906
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304907/**
4908 * ufshcd_exception_event_handler - handle exceptions raised by device
4909 * @work: pointer to work data
4910 *
4911 * Read bExceptionEventStatus attribute from the device and handle the
4912 * exception event accordingly.
4913 */
4914static void ufshcd_exception_event_handler(struct work_struct *work)
4915{
4916 struct ufs_hba *hba;
4917 int err;
4918 u32 status = 0;
4919 hba = container_of(work, struct ufs_hba, eeh_work);
4920
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304921 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304922 err = ufshcd_get_ee_status(hba, &status);
4923 if (err) {
4924 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4925 __func__, err);
4926 goto out;
4927 }
4928
4929 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004930
4931 if (status & MASK_EE_URGENT_BKOPS)
4932 ufshcd_bkops_exception_event_handler(hba);
4933
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304934out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304935 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304936 return;
4937}
4938
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004939/* Complete requests that have door-bell cleared */
4940static void ufshcd_complete_requests(struct ufs_hba *hba)
4941{
4942 ufshcd_transfer_req_compl(hba);
4943 ufshcd_tmc_handler(hba);
4944}
4945
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304946/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004947 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4948 * to recover from the DL NAC errors or not.
4949 * @hba: per-adapter instance
4950 *
4951 * Returns true if error handling is required, false otherwise
4952 */
4953static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4954{
4955 unsigned long flags;
4956 bool err_handling = true;
4957
4958 spin_lock_irqsave(hba->host->host_lock, flags);
4959 /*
4960 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4961 * device fatal error and/or DL NAC & REPLAY timeout errors.
4962 */
4963 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4964 goto out;
4965
4966 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4967 ((hba->saved_err & UIC_ERROR) &&
4968 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4969 goto out;
4970
4971 if ((hba->saved_err & UIC_ERROR) &&
4972 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4973 int err;
4974 /*
4975 * wait for 50ms to see if we can get any other errors or not.
4976 */
4977 spin_unlock_irqrestore(hba->host->host_lock, flags);
4978 msleep(50);
4979 spin_lock_irqsave(hba->host->host_lock, flags);
4980
4981 /*
4982 * now check if we have got any other severe errors other than
4983 * DL NAC error?
4984 */
4985 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4986 ((hba->saved_err & UIC_ERROR) &&
4987 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4988 goto out;
4989
4990 /*
4991 * As DL NAC is the only error received so far, send out NOP
4992 * command to confirm if link is still active or not.
4993 * - If we don't get any response then do error recovery.
4994 * - If we get response then clear the DL NAC error bit.
4995 */
4996
4997 spin_unlock_irqrestore(hba->host->host_lock, flags);
4998 err = ufshcd_verify_dev_init(hba);
4999 spin_lock_irqsave(hba->host->host_lock, flags);
5000
5001 if (err)
5002 goto out;
5003
5004 /* Link seems to be alive hence ignore the DL NAC errors */
5005 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5006 hba->saved_err &= ~UIC_ERROR;
5007 /* clear NAC error */
5008 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5009 if (!hba->saved_uic_err) {
5010 err_handling = false;
5011 goto out;
5012 }
5013 }
5014out:
5015 spin_unlock_irqrestore(hba->host->host_lock, flags);
5016 return err_handling;
5017}
5018
5019/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305020 * ufshcd_err_handler - handle UFS errors that require s/w attention
5021 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305022 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305023static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305024{
5025 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305026 unsigned long flags;
5027 u32 err_xfer = 0;
5028 u32 err_tm = 0;
5029 int err = 0;
5030 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005031 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305032
5033 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305034
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305035 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005036 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305037
5038 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005039 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305040 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305041
5042 hba->ufshcd_state = UFSHCD_STATE_RESET;
5043 ufshcd_set_eh_in_progress(hba);
5044
5045 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005046 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005047
5048 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5049 bool ret;
5050
5051 spin_unlock_irqrestore(hba->host->host_lock, flags);
5052 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5053 ret = ufshcd_quirk_dl_nac_errors(hba);
5054 spin_lock_irqsave(hba->host->host_lock, flags);
5055 if (!ret)
5056 goto skip_err_handling;
5057 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005058 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5059 ((hba->saved_err & UIC_ERROR) &&
5060 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5061 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5062 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5063 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305064
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005065 /*
5066 * if host reset is required then skip clearing the pending
5067 * transfers forcefully because they will automatically get
5068 * cleared after link startup.
5069 */
5070 if (needs_reset)
5071 goto skip_pending_xfer_clear;
5072
5073 /* release lock as clear command might sleep */
5074 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305075 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005076 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5077 if (ufshcd_clear_cmd(hba, tag)) {
5078 err_xfer = true;
5079 goto lock_skip_pending_xfer_clear;
5080 }
5081 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305082
5083 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005084 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5085 if (ufshcd_clear_tm_cmd(hba, tag)) {
5086 err_tm = true;
5087 goto lock_skip_pending_xfer_clear;
5088 }
5089 }
5090
5091lock_skip_pending_xfer_clear:
5092 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305093
5094 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005095 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305096
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005097 if (err_xfer || err_tm)
5098 needs_reset = true;
5099
5100skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305101 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005102 if (needs_reset) {
5103 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5104
5105 /*
5106 * ufshcd_reset_and_restore() does the link reinitialization
5107 * which will need atleast one empty doorbell slot to send the
5108 * device management commands (NOP and query commands).
5109 * If there is no slot empty at this moment then free up last
5110 * slot forcefully.
5111 */
5112 if (hba->outstanding_reqs == max_doorbells)
5113 __ufshcd_transfer_req_compl(hba,
5114 (1UL << (hba->nutrs - 1)));
5115
5116 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305117 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005118 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305119 if (err) {
5120 dev_err(hba->dev, "%s: reset and restore failed\n",
5121 __func__);
5122 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5123 }
5124 /*
5125 * Inform scsi mid-layer that we did reset and allow to handle
5126 * Unit Attention properly.
5127 */
5128 scsi_report_bus_reset(hba->host, 0);
5129 hba->saved_err = 0;
5130 hba->saved_uic_err = 0;
5131 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005132
Yaniv Gardi583fa622016-03-10 17:37:13 +02005133skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005134 if (!needs_reset) {
5135 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5136 if (hba->saved_err || hba->saved_uic_err)
5137 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5138 __func__, hba->saved_err, hba->saved_uic_err);
5139 }
5140
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305141 ufshcd_clear_eh_in_progress(hba);
5142
5143out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005144 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305145 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005146 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305147 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305148}
5149
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005150static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
5151 u32 reg)
5152{
5153 reg_hist->reg[reg_hist->pos] = reg;
5154 reg_hist->tstamp[reg_hist->pos] = ktime_get();
5155 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
5156}
5157
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305158/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305159 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5160 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305161 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305162static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305163{
5164 u32 reg;
5165
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005166 /* PHY layer lane error */
5167 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5168 /* Ignore LINERESET indication, as this is not an error */
5169 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005170 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005171 /*
5172 * To know whether this error is fatal or not, DB timeout
5173 * must be checked but this error is handled separately.
5174 */
5175 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005176 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
5177 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005178
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305179 /* PA_INIT_ERROR is fatal and needs UIC reset */
5180 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005181 if (reg)
5182 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
5183
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305184 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5185 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005186 else if (hba->dev_quirks &
5187 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5188 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5189 hba->uic_error |=
5190 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5191 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5192 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5193 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305194
5195 /* UIC NL/TL/DME errors needs software retry */
5196 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005197 if (reg) {
5198 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305199 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005200 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305201
5202 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005203 if (reg) {
5204 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305205 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005206 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305207
5208 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005209 if (reg) {
5210 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305211 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005212 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305213
5214 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5215 __func__, hba->uic_error);
5216}
5217
5218/**
5219 * ufshcd_check_errors - Check for errors that need s/w attention
5220 * @hba: per-adapter instance
5221 */
5222static void ufshcd_check_errors(struct ufs_hba *hba)
5223{
5224 bool queue_eh_work = false;
5225
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305226 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305227 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305228
5229 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305230 hba->uic_error = 0;
5231 ufshcd_update_uic_error(hba);
5232 if (hba->uic_error)
5233 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305234 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305235
5236 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005237 /*
5238 * update the transfer error masks to sticky bits, let's do this
5239 * irrespective of current ufshcd_state.
5240 */
5241 hba->saved_err |= hba->errors;
5242 hba->saved_uic_err |= hba->uic_error;
5243
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305244 /* handle fatal errors only when link is functional */
5245 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5246 /* block commands from scsi mid-layer */
5247 scsi_block_requests(hba->host);
5248
Zang Leigang141f8162016-11-16 11:29:37 +08005249 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005250
5251 /* dump controller state before resetting */
5252 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5253 bool pr_prdt = !!(hba->saved_err &
5254 SYSTEM_BUS_FATAL_ERROR);
5255
5256 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5257 __func__, hba->saved_err,
5258 hba->saved_uic_err);
5259
5260 ufshcd_print_host_regs(hba);
5261 ufshcd_print_pwr_info(hba);
5262 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5263 ufshcd_print_trs(hba, hba->outstanding_reqs,
5264 pr_prdt);
5265 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305266 schedule_work(&hba->eh_work);
5267 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305268 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305269 /*
5270 * if (!queue_eh_work) -
5271 * Other errors are either non-fatal where host recovers
5272 * itself without s/w intervention or errors that will be
5273 * handled by the SCSI core layer.
5274 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305275}
5276
5277/**
5278 * ufshcd_tmc_handler - handle task management function completion
5279 * @hba: per adapter instance
5280 */
5281static void ufshcd_tmc_handler(struct ufs_hba *hba)
5282{
5283 u32 tm_doorbell;
5284
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305285 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305286 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305287 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305288}
5289
5290/**
5291 * ufshcd_sl_intr - Interrupt service routine
5292 * @hba: per adapter instance
5293 * @intr_status: contains interrupts generated by the controller
5294 */
5295static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
5296{
5297 hba->errors = UFSHCD_ERROR_MASK & intr_status;
5298 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305299 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305300
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305301 if (intr_status & UFSHCD_UIC_MASK)
5302 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305303
5304 if (intr_status & UTP_TASK_REQ_COMPL)
5305 ufshcd_tmc_handler(hba);
5306
5307 if (intr_status & UTP_TRANSFER_REQ_COMPL)
5308 ufshcd_transfer_req_compl(hba);
5309}
5310
5311/**
5312 * ufshcd_intr - Main interrupt service routine
5313 * @irq: irq number
5314 * @__hba: pointer to adapter instance
5315 *
5316 * Returns IRQ_HANDLED - If interrupt is valid
5317 * IRQ_NONE - If invalid interrupt
5318 */
5319static irqreturn_t ufshcd_intr(int irq, void *__hba)
5320{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005321 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305322 irqreturn_t retval = IRQ_NONE;
5323 struct ufs_hba *hba = __hba;
5324
5325 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305326 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005327 enabled_intr_status =
5328 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305329
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005330 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05305331 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005332
5333 if (enabled_intr_status) {
5334 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305335 retval = IRQ_HANDLED;
5336 }
5337 spin_unlock(hba->host->host_lock);
5338 return retval;
5339}
5340
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305341static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5342{
5343 int err = 0;
5344 u32 mask = 1 << tag;
5345 unsigned long flags;
5346
5347 if (!test_bit(tag, &hba->outstanding_tasks))
5348 goto out;
5349
5350 spin_lock_irqsave(hba->host->host_lock, flags);
5351 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
5352 spin_unlock_irqrestore(hba->host->host_lock, flags);
5353
5354 /* poll for max. 1 sec to clear door bell register by h/w */
5355 err = ufshcd_wait_for_register(hba,
5356 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005357 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305358out:
5359 return err;
5360}
5361
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305362/**
5363 * ufshcd_issue_tm_cmd - issues task management commands to controller
5364 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305365 * @lun_id: LUN ID to which TM command is sent
5366 * @task_id: task ID to which the TM command is applicable
5367 * @tm_function: task management function opcode
5368 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305369 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305370 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305371 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305372static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5373 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305374{
5375 struct utp_task_req_desc *task_req_descp;
5376 struct utp_upiu_task_req *task_req_upiup;
5377 struct Scsi_Host *host;
5378 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305379 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305380 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305381 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305382
5383 host = hba->host;
5384
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305385 /*
5386 * Get free slot, sleep if slots are unavailable.
5387 * Even though we use wait_event() which sleeps indefinitely,
5388 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5389 */
5390 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005391 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305392
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305393 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305394 task_req_descp = hba->utmrdl_base_addr;
5395 task_req_descp += free_slot;
5396
5397 /* Configure task request descriptor */
5398 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5399 task_req_descp->header.dword_2 =
5400 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5401
5402 /* Configure task request UPIU */
5403 task_req_upiup =
5404 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305405 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305406 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305407 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305408 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305409 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305410 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005411 /*
5412 * The host shall provide the same value for LUN field in the basic
5413 * header and for Input Parameter.
5414 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305415 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
5416 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305417
Kiwoong Kimd2877be2016-11-10 21:16:15 +09005418 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
5419
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305420 /* send command to the controller */
5421 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005422
5423 /* Make sure descriptors are ready before ringing the task doorbell */
5424 wmb();
5425
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305426 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07005427 /* Make sure that doorbell is committed immediately */
5428 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305429
5430 spin_unlock_irqrestore(host->host_lock, flags);
5431
5432 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305433 err = wait_event_timeout(hba->tm_wq,
5434 test_bit(free_slot, &hba->tm_condition),
5435 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305436 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305437 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
5438 __func__, tm_function);
5439 if (ufshcd_clear_tm_cmd(hba, free_slot))
5440 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
5441 __func__, free_slot);
5442 err = -ETIMEDOUT;
5443 } else {
5444 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305445 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305446
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305447 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305448 ufshcd_put_tm_slot(hba, free_slot);
5449 wake_up(&hba->tm_tag_wq);
5450
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005451 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305452 return err;
5453}
5454
5455/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305456 * ufshcd_eh_device_reset_handler - device reset handler registered to
5457 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305458 * @cmd: SCSI command pointer
5459 *
5460 * Returns SUCCESS/FAILED
5461 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305462static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305463{
5464 struct Scsi_Host *host;
5465 struct ufs_hba *hba;
5466 unsigned int tag;
5467 u32 pos;
5468 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305469 u8 resp = 0xF;
5470 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305471 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305472
5473 host = cmd->device->host;
5474 hba = shost_priv(host);
5475 tag = cmd->request->tag;
5476
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305477 lrbp = &hba->lrb[tag];
5478 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
5479 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305480 if (!err)
5481 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305482 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305483 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305484
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305485 /* clear the commands that were pending for corresponding LUN */
5486 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
5487 if (hba->lrb[pos].lun == lrbp->lun) {
5488 err = ufshcd_clear_cmd(hba, pos);
5489 if (err)
5490 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305491 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305492 }
5493 spin_lock_irqsave(host->host_lock, flags);
5494 ufshcd_transfer_req_compl(hba);
5495 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08005496
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305497out:
Gilad Broner7fabb772017-02-03 16:56:50 -08005498 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305499 if (!err) {
5500 err = SUCCESS;
5501 } else {
5502 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
5503 err = FAILED;
5504 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305505 return err;
5506}
5507
Gilad Bronere0b299e2017-02-03 16:56:40 -08005508static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
5509{
5510 struct ufshcd_lrb *lrbp;
5511 int tag;
5512
5513 for_each_set_bit(tag, &bitmap, hba->nutrs) {
5514 lrbp = &hba->lrb[tag];
5515 lrbp->req_abort_skip = true;
5516 }
5517}
5518
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305519/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305520 * ufshcd_abort - abort a specific command
5521 * @cmd: SCSI command pointer
5522 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305523 * Abort the pending command in device by sending UFS_ABORT_TASK task management
5524 * command, and in host controller by clearing the door-bell register. There can
5525 * be race between controller sending the command to the device while abort is
5526 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
5527 * really issued and then try to abort it.
5528 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305529 * Returns SUCCESS/FAILED
5530 */
5531static int ufshcd_abort(struct scsi_cmnd *cmd)
5532{
5533 struct Scsi_Host *host;
5534 struct ufs_hba *hba;
5535 unsigned long flags;
5536 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305537 int err = 0;
5538 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305539 u8 resp = 0xF;
5540 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005541 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305542
5543 host = cmd->device->host;
5544 hba = shost_priv(host);
5545 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08005546 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02005547 if (!ufshcd_valid_tag(hba, tag)) {
5548 dev_err(hba->dev,
5549 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
5550 __func__, tag, cmd, cmd->request);
5551 BUG();
5552 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305553
Dolev Ravive7d38252016-12-22 18:40:07 -08005554 /*
5555 * Task abort to the device W-LUN is illegal. When this command
5556 * will fail, due to spec violation, scsi err handling next step
5557 * will be to send LU reset which, again, is a spec violation.
5558 * To avoid these unnecessary/illegal step we skip to the last error
5559 * handling stage: reset and restore.
5560 */
5561 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
5562 return ufshcd_eh_host_reset_handler(cmd);
5563
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005564 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005565 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02005566 /* If command is already aborted/completed, return SUCCESS */
5567 if (!(test_bit(tag, &hba->outstanding_reqs))) {
5568 dev_err(hba->dev,
5569 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
5570 __func__, tag, hba->outstanding_reqs, reg);
5571 goto out;
5572 }
5573
Dolev Ravive9d501b2014-07-01 12:22:37 +03005574 if (!(reg & (1 << tag))) {
5575 dev_err(hba->dev,
5576 "%s: cmd was completed, but without a notifying intr, tag = %d",
5577 __func__, tag);
5578 }
5579
Dolev Raviv66cc8202016-12-22 18:39:42 -08005580 /* Print Transfer Request of aborted task */
5581 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08005582
Gilad Broner7fabb772017-02-03 16:56:50 -08005583 /*
5584 * Print detailed info about aborted request.
5585 * As more than one request might get aborted at the same time,
5586 * print full information only for the first aborted request in order
5587 * to reduce repeated printouts. For other aborted requests only print
5588 * basic details.
5589 */
5590 scsi_print_command(hba->lrb[tag].cmd);
5591 if (!hba->req_abort_count) {
5592 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08005593 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08005594 ufshcd_print_pwr_info(hba);
5595 ufshcd_print_trs(hba, 1 << tag, true);
5596 } else {
5597 ufshcd_print_trs(hba, 1 << tag, false);
5598 }
5599 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08005600
5601 /* Skip task abort in case previous aborts failed and report failure */
5602 if (lrbp->req_abort_skip) {
5603 err = -EIO;
5604 goto out;
5605 }
5606
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305607 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
5608 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5609 UFS_QUERY_TASK, &resp);
5610 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
5611 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005612 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
5613 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305614 break;
5615 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305616 /*
5617 * cmd not pending in the device, check if it is
5618 * in transition.
5619 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005620 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
5621 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305622 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5623 if (reg & (1 << tag)) {
5624 /* sleep for max. 200us to stabilize */
5625 usleep_range(100, 200);
5626 continue;
5627 }
5628 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005629 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
5630 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305631 goto out;
5632 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005633 dev_err(hba->dev,
5634 "%s: no response from device. tag = %d, err %d\n",
5635 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305636 if (!err)
5637 err = resp; /* service response error */
5638 goto out;
5639 }
5640 }
5641
5642 if (!poll_cnt) {
5643 err = -EBUSY;
5644 goto out;
5645 }
5646
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305647 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5648 UFS_ABORT_TASK, &resp);
5649 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005650 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305651 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005652 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
5653 __func__, tag, err);
5654 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305655 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305656 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305657
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305658 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005659 if (err) {
5660 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
5661 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305662 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005663 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305664
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305665 scsi_dma_unmap(cmd);
5666
5667 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02005668 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305669 hba->lrb[tag].cmd = NULL;
5670 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305671
5672 clear_bit_unlock(tag, &hba->lrb_in_use);
5673 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005674
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305675out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305676 if (!err) {
5677 err = SUCCESS;
5678 } else {
5679 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08005680 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305681 err = FAILED;
5682 }
5683
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005684 /*
5685 * This ufshcd_release() corresponds to the original scsi cmd that got
5686 * aborted here (as we won't get any IRQ for it).
5687 */
5688 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305689 return err;
5690}
5691
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305692/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305693 * ufshcd_host_reset_and_restore - reset and restore host controller
5694 * @hba: per-adapter instance
5695 *
5696 * Note that host controller reset may issue DME_RESET to
5697 * local and remote (device) Uni-Pro stack and the attributes
5698 * are reset to default state.
5699 *
5700 * Returns zero on success, non-zero on failure
5701 */
5702static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
5703{
5704 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305705 unsigned long flags;
5706
5707 /* Reset the host controller */
5708 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02005709 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305710 spin_unlock_irqrestore(hba->host->host_lock, flags);
5711
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08005712 /* scale up clocks to max frequency before full reinitialization */
5713 ufshcd_scale_clks(hba, true);
5714
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305715 err = ufshcd_hba_enable(hba);
5716 if (err)
5717 goto out;
5718
5719 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005720 err = ufshcd_probe_hba(hba);
5721
5722 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305723 err = -EIO;
5724out:
5725 if (err)
5726 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
5727
5728 return err;
5729}
5730
5731/**
5732 * ufshcd_reset_and_restore - reset and re-initialize host/device
5733 * @hba: per-adapter instance
5734 *
5735 * Reset and recover device, host and re-establish link. This
5736 * is helpful to recover the communication in fatal error conditions.
5737 *
5738 * Returns zero on success, non-zero on failure
5739 */
5740static int ufshcd_reset_and_restore(struct ufs_hba *hba)
5741{
5742 int err = 0;
5743 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005744 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305745
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005746 do {
5747 err = ufshcd_host_reset_and_restore(hba);
5748 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305749
5750 /*
5751 * After reset the door-bell might be cleared, complete
5752 * outstanding requests in s/w here.
5753 */
5754 spin_lock_irqsave(hba->host->host_lock, flags);
5755 ufshcd_transfer_req_compl(hba);
5756 ufshcd_tmc_handler(hba);
5757 spin_unlock_irqrestore(hba->host->host_lock, flags);
5758
5759 return err;
5760}
5761
5762/**
5763 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
5764 * @cmd - SCSI command pointer
5765 *
5766 * Returns SUCCESS/FAILED
5767 */
5768static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
5769{
5770 int err;
5771 unsigned long flags;
5772 struct ufs_hba *hba;
5773
5774 hba = shost_priv(cmd->device->host);
5775
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005776 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305777 /*
5778 * Check if there is any race with fatal error handling.
5779 * If so, wait for it to complete. Even though fatal error
5780 * handling does reset and restore in some cases, don't assume
5781 * anything out of it. We are just avoiding race here.
5782 */
5783 do {
5784 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305785 if (!(work_pending(&hba->eh_work) ||
Zang Leigang8dc0da72017-06-24 19:14:32 +08005786 hba->ufshcd_state == UFSHCD_STATE_RESET ||
5787 hba->ufshcd_state == UFSHCD_STATE_EH_SCHEDULED))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305788 break;
5789 spin_unlock_irqrestore(hba->host->host_lock, flags);
5790 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305791 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305792 } while (1);
5793
5794 hba->ufshcd_state = UFSHCD_STATE_RESET;
5795 ufshcd_set_eh_in_progress(hba);
5796 spin_unlock_irqrestore(hba->host->host_lock, flags);
5797
5798 err = ufshcd_reset_and_restore(hba);
5799
5800 spin_lock_irqsave(hba->host->host_lock, flags);
5801 if (!err) {
5802 err = SUCCESS;
5803 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5804 } else {
5805 err = FAILED;
5806 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5807 }
5808 ufshcd_clear_eh_in_progress(hba);
5809 spin_unlock_irqrestore(hba->host->host_lock, flags);
5810
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005811 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305812 return err;
5813}
5814
5815/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005816 * ufshcd_get_max_icc_level - calculate the ICC level
5817 * @sup_curr_uA: max. current supported by the regulator
5818 * @start_scan: row at the desc table to start scan from
5819 * @buff: power descriptor buffer
5820 *
5821 * Returns calculated max ICC level for specific regulator
5822 */
5823static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
5824{
5825 int i;
5826 int curr_uA;
5827 u16 data;
5828 u16 unit;
5829
5830 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02005831 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005832 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
5833 ATTR_ICC_LVL_UNIT_OFFSET;
5834 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
5835 switch (unit) {
5836 case UFSHCD_NANO_AMP:
5837 curr_uA = curr_uA / 1000;
5838 break;
5839 case UFSHCD_MILI_AMP:
5840 curr_uA = curr_uA * 1000;
5841 break;
5842 case UFSHCD_AMP:
5843 curr_uA = curr_uA * 1000 * 1000;
5844 break;
5845 case UFSHCD_MICRO_AMP:
5846 default:
5847 break;
5848 }
5849 if (sup_curr_uA >= curr_uA)
5850 break;
5851 }
5852 if (i < 0) {
5853 i = 0;
5854 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
5855 }
5856
5857 return (u32)i;
5858}
5859
5860/**
5861 * ufshcd_calc_icc_level - calculate the max ICC level
5862 * In case regulators are not initialized we'll return 0
5863 * @hba: per-adapter instance
5864 * @desc_buf: power descriptor buffer to extract ICC levels from.
5865 * @len: length of desc_buff
5866 *
5867 * Returns calculated ICC level
5868 */
5869static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
5870 u8 *desc_buf, int len)
5871{
5872 u32 icc_level = 0;
5873
5874 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
5875 !hba->vreg_info.vccq2) {
5876 dev_err(hba->dev,
5877 "%s: Regulator capability was not set, actvIccLevel=%d",
5878 __func__, icc_level);
5879 goto out;
5880 }
5881
5882 if (hba->vreg_info.vcc)
5883 icc_level = ufshcd_get_max_icc_level(
5884 hba->vreg_info.vcc->max_uA,
5885 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
5886 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
5887
5888 if (hba->vreg_info.vccq)
5889 icc_level = ufshcd_get_max_icc_level(
5890 hba->vreg_info.vccq->max_uA,
5891 icc_level,
5892 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
5893
5894 if (hba->vreg_info.vccq2)
5895 icc_level = ufshcd_get_max_icc_level(
5896 hba->vreg_info.vccq2->max_uA,
5897 icc_level,
5898 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
5899out:
5900 return icc_level;
5901}
5902
5903static void ufshcd_init_icc_levels(struct ufs_hba *hba)
5904{
5905 int ret;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00005906 int buff_len = hba->desc_size.pwr_desc;
5907 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005908
5909 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
5910 if (ret) {
5911 dev_err(hba->dev,
5912 "%s: Failed reading power descriptor.len = %d ret = %d",
5913 __func__, buff_len, ret);
5914 return;
5915 }
5916
5917 hba->init_prefetch_data.icc_level =
5918 ufshcd_find_max_sup_active_icc_level(hba,
5919 desc_buf, buff_len);
5920 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
5921 __func__, hba->init_prefetch_data.icc_level);
5922
Szymon Mielczarekdbd34a62017-03-29 08:19:21 +02005923 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5924 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
5925 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005926
5927 if (ret)
5928 dev_err(hba->dev,
5929 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5930 __func__, hba->init_prefetch_data.icc_level , ret);
5931
5932}
5933
5934/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005935 * ufshcd_scsi_add_wlus - Adds required W-LUs
5936 * @hba: per-adapter instance
5937 *
5938 * UFS device specification requires the UFS devices to support 4 well known
5939 * logical units:
5940 * "REPORT_LUNS" (address: 01h)
5941 * "UFS Device" (address: 50h)
5942 * "RPMB" (address: 44h)
5943 * "BOOT" (address: 30h)
5944 * UFS device's power management needs to be controlled by "POWER CONDITION"
5945 * field of SSU (START STOP UNIT) command. But this "power condition" field
5946 * will take effect only when its sent to "UFS device" well known logical unit
5947 * hence we require the scsi_device instance to represent this logical unit in
5948 * order for the UFS host driver to send the SSU command for power management.
5949
5950 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5951 * Block) LU so user space process can control this LU. User space may also
5952 * want to have access to BOOT LU.
5953
5954 * This function adds scsi device instances for each of all well known LUs
5955 * (except "REPORT LUNS" LU).
5956 *
5957 * Returns zero on success (all required W-LUs are added successfully),
5958 * non-zero error value on failure (if failed to add any of the required W-LU).
5959 */
5960static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
5961{
5962 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005963 struct scsi_device *sdev_rpmb;
5964 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005965
5966 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
5967 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
5968 if (IS_ERR(hba->sdev_ufs_device)) {
5969 ret = PTR_ERR(hba->sdev_ufs_device);
5970 hba->sdev_ufs_device = NULL;
5971 goto out;
5972 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005973 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005974
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005975 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005976 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005977 if (IS_ERR(sdev_rpmb)) {
5978 ret = PTR_ERR(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08005979 goto remove_sdev_ufs_device;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005980 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005981 scsi_device_put(sdev_rpmb);
Huanlin Ke3d21fbd2017-09-22 18:31:47 +08005982
5983 sdev_boot = __scsi_add_device(hba->host, 0, 0,
5984 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
5985 if (IS_ERR(sdev_boot))
5986 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__);
5987 else
5988 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005989 goto out;
5990
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005991remove_sdev_ufs_device:
5992 scsi_remove_device(hba->sdev_ufs_device);
5993out:
5994 return ret;
5995}
5996
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02005997static int ufs_get_device_desc(struct ufs_hba *hba,
5998 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005999{
6000 int err;
6001 u8 model_index;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006002 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
6003 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006004
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006005 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006006 if (err) {
6007 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6008 __func__, err);
6009 goto out;
6010 }
6011
6012 /*
6013 * getting vendor (manufacturerID) and Bank Index in big endian
6014 * format
6015 */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006016 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006017 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6018
6019 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
6020
6021 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006022 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006023 if (err) {
6024 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
6025 __func__, err);
6026 goto out;
6027 }
6028
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006029 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006030 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006031 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
6032 MAX_MODEL_LEN));
6033
6034 /* Null terminate the model string */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006035 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006036
6037out:
6038 return err;
6039}
6040
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006041static void ufs_fixup_device_setup(struct ufs_hba *hba,
6042 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006043{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006044 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006045
6046 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006047 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
6048 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
6049 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006050 !strcmp(f->card.model, UFS_ANY_MODEL)))
6051 hba->dev_quirks |= f->quirk;
6052 }
6053}
6054
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006055/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006056 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6057 * @hba: per-adapter instance
6058 *
6059 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6060 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6061 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6062 * the hibern8 exit latency.
6063 *
6064 * Returns zero on success, non-zero error value on failure.
6065 */
6066static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6067{
6068 int ret = 0;
6069 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6070
6071 ret = ufshcd_dme_peer_get(hba,
6072 UIC_ARG_MIB_SEL(
6073 RX_MIN_ACTIVATETIME_CAPABILITY,
6074 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6075 &peer_rx_min_activatetime);
6076 if (ret)
6077 goto out;
6078
6079 /* make sure proper unit conversion is applied */
6080 tuned_pa_tactivate =
6081 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6082 / PA_TACTIVATE_TIME_UNIT_US);
6083 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6084 tuned_pa_tactivate);
6085
6086out:
6087 return ret;
6088}
6089
6090/**
6091 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6092 * @hba: per-adapter instance
6093 *
6094 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6095 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6096 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6097 * This optimal value can help reduce the hibern8 exit latency.
6098 *
6099 * Returns zero on success, non-zero error value on failure.
6100 */
6101static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6102{
6103 int ret = 0;
6104 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6105 u32 max_hibern8_time, tuned_pa_hibern8time;
6106
6107 ret = ufshcd_dme_get(hba,
6108 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6109 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6110 &local_tx_hibern8_time_cap);
6111 if (ret)
6112 goto out;
6113
6114 ret = ufshcd_dme_peer_get(hba,
6115 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6116 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6117 &peer_rx_hibern8_time_cap);
6118 if (ret)
6119 goto out;
6120
6121 max_hibern8_time = max(local_tx_hibern8_time_cap,
6122 peer_rx_hibern8_time_cap);
6123 /* make sure proper unit conversion is applied */
6124 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6125 / PA_HIBERN8_TIME_UNIT_US);
6126 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6127 tuned_pa_hibern8time);
6128out:
6129 return ret;
6130}
6131
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006132/**
6133 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6134 * less than device PA_TACTIVATE time.
6135 * @hba: per-adapter instance
6136 *
6137 * Some UFS devices require host PA_TACTIVATE to be lower than device
6138 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6139 * for such devices.
6140 *
6141 * Returns zero on success, non-zero error value on failure.
6142 */
6143static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6144{
6145 int ret = 0;
6146 u32 granularity, peer_granularity;
6147 u32 pa_tactivate, peer_pa_tactivate;
6148 u32 pa_tactivate_us, peer_pa_tactivate_us;
6149 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6150
6151 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6152 &granularity);
6153 if (ret)
6154 goto out;
6155
6156 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6157 &peer_granularity);
6158 if (ret)
6159 goto out;
6160
6161 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6162 (granularity > PA_GRANULARITY_MAX_VAL)) {
6163 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6164 __func__, granularity);
6165 return -EINVAL;
6166 }
6167
6168 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6169 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6170 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6171 __func__, peer_granularity);
6172 return -EINVAL;
6173 }
6174
6175 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6176 if (ret)
6177 goto out;
6178
6179 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6180 &peer_pa_tactivate);
6181 if (ret)
6182 goto out;
6183
6184 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6185 peer_pa_tactivate_us = peer_pa_tactivate *
6186 gran_to_us_table[peer_granularity - 1];
6187
6188 if (pa_tactivate_us > peer_pa_tactivate_us) {
6189 u32 new_peer_pa_tactivate;
6190
6191 new_peer_pa_tactivate = pa_tactivate_us /
6192 gran_to_us_table[peer_granularity - 1];
6193 new_peer_pa_tactivate++;
6194 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6195 new_peer_pa_tactivate);
6196 }
6197
6198out:
6199 return ret;
6200}
6201
Yaniv Gardi37113102016-03-10 17:37:16 +02006202static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
6203{
6204 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6205 ufshcd_tune_pa_tactivate(hba);
6206 ufshcd_tune_pa_hibern8time(hba);
6207 }
6208
6209 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6210 /* set 1ms timeout for PA_TACTIVATE */
6211 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006212
6213 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6214 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08006215
6216 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006217}
6218
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006219static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6220{
6221 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
6222
6223 hba->ufs_stats.hibern8_exit_cnt = 0;
6224 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
6225
6226 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
6227 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
6228 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
6229 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
6230 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
Gilad Broner7fabb772017-02-03 16:56:50 -08006231
6232 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006233}
6234
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006235static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
6236{
6237 int err;
6238
6239 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
6240 &hba->desc_size.dev_desc);
6241 if (err)
6242 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
6243
6244 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
6245 &hba->desc_size.pwr_desc);
6246 if (err)
6247 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
6248
6249 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
6250 &hba->desc_size.interc_desc);
6251 if (err)
6252 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
6253
6254 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
6255 &hba->desc_size.conf_desc);
6256 if (err)
6257 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
6258
6259 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
6260 &hba->desc_size.unit_desc);
6261 if (err)
6262 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
6263
6264 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
6265 &hba->desc_size.geom_desc);
6266 if (err)
6267 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02006268 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_HEALTH, 0,
6269 &hba->desc_size.hlth_desc);
6270 if (err)
6271 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006272}
6273
6274static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
6275{
6276 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
6277 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
6278 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
6279 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
6280 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
6281 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
Stanislav Nijnikovc648c2d2018-02-15 14:14:05 +02006282 hba->desc_size.hlth_desc = QUERY_DESC_HEALTH_DEF_SIZE;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006283}
6284
Yaniv Gardi37113102016-03-10 17:37:16 +02006285/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006286 * ufshcd_probe_hba - probe hba to detect device and initialize
6287 * @hba: per-adapter instance
6288 *
6289 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306290 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006291static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306292{
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006293 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306294 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006295 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306296
6297 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306298 if (ret)
6299 goto out;
6300
Yaniv Gardiafdfff52016-03-10 17:37:15 +02006301 /* set the default level for urgent bkops */
6302 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
6303 hba->is_urgent_bkops_lvl_checked = false;
6304
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006305 /* Debug counters initialization */
6306 ufshcd_clear_dbg_ufs_stats(hba);
6307
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006308 /* UniPro link is active now */
6309 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05306310
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306311 ret = ufshcd_verify_dev_init(hba);
6312 if (ret)
6313 goto out;
6314
Dolev Raviv68078d52013-07-30 00:35:58 +05306315 ret = ufshcd_complete_dev_init(hba);
6316 if (ret)
6317 goto out;
6318
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006319 /* Init check for device descriptor sizes */
6320 ufshcd_init_desc_sizes(hba);
6321
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006322 ret = ufs_get_device_desc(hba, &card);
6323 if (ret) {
6324 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
6325 __func__, ret);
6326 goto out;
6327 }
6328
6329 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02006330 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006331
6332 ret = ufshcd_set_vccq_rail_unused(hba,
6333 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
6334 if (ret)
6335 goto out;
6336
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006337 /* UFS device is also active now */
6338 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306339 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006340 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306341
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006342 if (ufshcd_get_max_pwr_mode(hba)) {
6343 dev_err(hba->dev,
6344 "%s: Failed getting max supported power mode\n",
6345 __func__);
6346 } else {
6347 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006348 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006349 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
6350 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006351 goto out;
6352 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006353 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006354
Yaniv Gardi53c12d02016-02-01 15:02:45 +02006355 /* set the state as operational after switching to desired gear */
6356 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00006357
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006358 /*
6359 * If we are in error handling context or in power management callbacks
6360 * context, no need to scan the host
6361 */
6362 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6363 bool flag;
6364
6365 /* clear any previous UFS device information */
6366 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02006367 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6368 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006369 hba->dev_info.f_power_on_wp_en = flag;
6370
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006371 if (!hba->is_init_prefetch)
6372 ufshcd_init_icc_levels(hba);
6373
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006374 /* Add required well known logical units to scsi mid layer */
6375 if (ufshcd_scsi_add_wlus(hba))
6376 goto out;
6377
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006378 /* Initialize devfreq after UFS device is detected */
6379 if (ufshcd_is_clkscaling_supported(hba)) {
6380 memcpy(&hba->clk_scaling.saved_pwr_info.info,
6381 &hba->pwr_info,
6382 sizeof(struct ufs_pa_layer_attr));
6383 hba->clk_scaling.saved_pwr_info.is_valid = true;
6384 if (!hba->devfreq) {
6385 hba->devfreq = devm_devfreq_add_device(hba->dev,
6386 &ufs_devfreq_profile,
6387 "simple_ondemand",
6388 NULL);
6389 if (IS_ERR(hba->devfreq)) {
6390 ret = PTR_ERR(hba->devfreq);
6391 dev_err(hba->dev, "Unable to register with devfreq %d\n",
6392 ret);
6393 goto out;
6394 }
6395 }
6396 hba->clk_scaling.is_allowed = true;
6397 }
6398
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306399 scsi_scan_host(hba->host);
6400 pm_runtime_put_sync(hba->dev);
6401 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006402
6403 if (!hba->is_init_prefetch)
6404 hba->is_init_prefetch = true;
6405
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306406out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006407 /*
6408 * If we failed to initialize the device or the device is not
6409 * present, turn off the power/clocks etc.
6410 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006411 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6412 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006413 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006414 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006415
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006416 trace_ufshcd_init(dev_name(hba->dev), ret,
6417 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08006418 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006419 return ret;
6420}
6421
6422/**
6423 * ufshcd_async_scan - asynchronous execution for probing hba
6424 * @data: data pointer to pass to this function
6425 * @cookie: cookie data
6426 */
6427static void ufshcd_async_scan(void *data, async_cookie_t cookie)
6428{
6429 struct ufs_hba *hba = (struct ufs_hba *)data;
6430
6431 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306432}
6433
Yaniv Gardif550c652016-03-10 17:37:07 +02006434static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
6435{
6436 unsigned long flags;
6437 struct Scsi_Host *host;
6438 struct ufs_hba *hba;
6439 int index;
6440 bool found = false;
6441
6442 if (!scmd || !scmd->device || !scmd->device->host)
6443 return BLK_EH_NOT_HANDLED;
6444
6445 host = scmd->device->host;
6446 hba = shost_priv(host);
6447 if (!hba)
6448 return BLK_EH_NOT_HANDLED;
6449
6450 spin_lock_irqsave(host->host_lock, flags);
6451
6452 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
6453 if (hba->lrb[index].cmd == scmd) {
6454 found = true;
6455 break;
6456 }
6457 }
6458
6459 spin_unlock_irqrestore(host->host_lock, flags);
6460
6461 /*
6462 * Bypass SCSI error handling and reset the block layer timer if this
6463 * SCSI command was not actually dispatched to UFS driver, otherwise
6464 * let SCSI layer handle the error as usual.
6465 */
6466 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
6467}
6468
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02006469static const struct attribute_group *ufshcd_driver_groups[] = {
6470 &ufs_sysfs_unit_descriptor_group,
Stanislav Nijnikovec92b592018-02-15 14:14:11 +02006471 &ufs_sysfs_lun_attributes_group,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02006472 NULL,
6473};
6474
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306475static struct scsi_host_template ufshcd_driver_template = {
6476 .module = THIS_MODULE,
6477 .name = UFSHCD,
6478 .proc_name = UFSHCD,
6479 .queuecommand = ufshcd_queuecommand,
6480 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09006481 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306482 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03006483 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306484 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306485 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
6486 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02006487 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306488 .this_id = -1,
6489 .sg_tablesize = SG_ALL,
6490 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
6491 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006492 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006493 .track_queue_depth = 1,
Stanislav Nijnikovd829fc82018-02-15 14:14:09 +02006494 .sdev_groups = ufshcd_driver_groups,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306495};
6496
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006497static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
6498 int ua)
6499{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006500 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006501
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006502 if (!vreg)
6503 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006504
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006505 ret = regulator_set_load(vreg->reg, ua);
6506 if (ret < 0) {
6507 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
6508 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006509 }
6510
6511 return ret;
6512}
6513
6514static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
6515 struct ufs_vreg *vreg)
6516{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006517 if (!vreg)
6518 return 0;
6519 else if (vreg->unused)
6520 return 0;
6521 else
6522 return ufshcd_config_vreg_load(hba->dev, vreg,
6523 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006524}
6525
6526static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
6527 struct ufs_vreg *vreg)
6528{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006529 if (!vreg)
6530 return 0;
6531 else if (vreg->unused)
6532 return 0;
6533 else
6534 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006535}
6536
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006537static int ufshcd_config_vreg(struct device *dev,
6538 struct ufs_vreg *vreg, bool on)
6539{
6540 int ret = 0;
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06006541 struct regulator *reg;
6542 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006543 int min_uV, uA_load;
6544
6545 BUG_ON(!vreg);
6546
Gustavo A. R. Silva72753592017-11-20 08:12:29 -06006547 reg = vreg->reg;
6548 name = vreg->name;
6549
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006550 if (regulator_count_voltages(reg) > 0) {
6551 min_uV = on ? vreg->min_uV : 0;
6552 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
6553 if (ret) {
6554 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
6555 __func__, name, ret);
6556 goto out;
6557 }
6558
6559 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006560 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
6561 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006562 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006563 }
6564out:
6565 return ret;
6566}
6567
6568static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
6569{
6570 int ret = 0;
6571
Yaniv Gardi60f01872016-03-10 17:37:11 +02006572 if (!vreg)
6573 goto out;
6574 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006575 goto out;
6576
6577 ret = ufshcd_config_vreg(dev, vreg, true);
6578 if (!ret)
6579 ret = regulator_enable(vreg->reg);
6580
6581 if (!ret)
6582 vreg->enabled = true;
6583 else
6584 dev_err(dev, "%s: %s enable failed, err=%d\n",
6585 __func__, vreg->name, ret);
6586out:
6587 return ret;
6588}
6589
6590static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
6591{
6592 int ret = 0;
6593
Yaniv Gardi60f01872016-03-10 17:37:11 +02006594 if (!vreg)
6595 goto out;
6596 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006597 goto out;
6598
6599 ret = regulator_disable(vreg->reg);
6600
6601 if (!ret) {
6602 /* ignore errors on applying disable config */
6603 ufshcd_config_vreg(dev, vreg, false);
6604 vreg->enabled = false;
6605 } else {
6606 dev_err(dev, "%s: %s disable failed, err=%d\n",
6607 __func__, vreg->name, ret);
6608 }
6609out:
6610 return ret;
6611}
6612
6613static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
6614{
6615 int ret = 0;
6616 struct device *dev = hba->dev;
6617 struct ufs_vreg_info *info = &hba->vreg_info;
6618
6619 if (!info)
6620 goto out;
6621
6622 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
6623 if (ret)
6624 goto out;
6625
6626 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
6627 if (ret)
6628 goto out;
6629
6630 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
6631 if (ret)
6632 goto out;
6633
6634out:
6635 if (ret) {
6636 ufshcd_toggle_vreg(dev, info->vccq2, false);
6637 ufshcd_toggle_vreg(dev, info->vccq, false);
6638 ufshcd_toggle_vreg(dev, info->vcc, false);
6639 }
6640 return ret;
6641}
6642
Raviv Shvili6a771a62014-09-25 15:32:24 +03006643static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
6644{
6645 struct ufs_vreg_info *info = &hba->vreg_info;
6646
6647 if (info)
6648 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
6649
6650 return 0;
6651}
6652
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006653static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
6654{
6655 int ret = 0;
6656
6657 if (!vreg)
6658 goto out;
6659
6660 vreg->reg = devm_regulator_get(dev, vreg->name);
6661 if (IS_ERR(vreg->reg)) {
6662 ret = PTR_ERR(vreg->reg);
6663 dev_err(dev, "%s: %s get failed, err=%d\n",
6664 __func__, vreg->name, ret);
6665 }
6666out:
6667 return ret;
6668}
6669
6670static int ufshcd_init_vreg(struct ufs_hba *hba)
6671{
6672 int ret = 0;
6673 struct device *dev = hba->dev;
6674 struct ufs_vreg_info *info = &hba->vreg_info;
6675
6676 if (!info)
6677 goto out;
6678
6679 ret = ufshcd_get_vreg(dev, info->vcc);
6680 if (ret)
6681 goto out;
6682
6683 ret = ufshcd_get_vreg(dev, info->vccq);
6684 if (ret)
6685 goto out;
6686
6687 ret = ufshcd_get_vreg(dev, info->vccq2);
6688out:
6689 return ret;
6690}
6691
Raviv Shvili6a771a62014-09-25 15:32:24 +03006692static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
6693{
6694 struct ufs_vreg_info *info = &hba->vreg_info;
6695
6696 if (info)
6697 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
6698
6699 return 0;
6700}
6701
Yaniv Gardi60f01872016-03-10 17:37:11 +02006702static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
6703{
6704 int ret = 0;
6705 struct ufs_vreg_info *info = &hba->vreg_info;
6706
6707 if (!info)
6708 goto out;
6709 else if (!info->vccq)
6710 goto out;
6711
6712 if (unused) {
6713 /* shut off the rail here */
6714 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
6715 /*
6716 * Mark this rail as no longer used, so it doesn't get enabled
6717 * later by mistake
6718 */
6719 if (!ret)
6720 info->vccq->unused = true;
6721 } else {
6722 /*
6723 * rail should have been already enabled hence just make sure
6724 * that unused flag is cleared.
6725 */
6726 info->vccq->unused = false;
6727 }
6728out:
6729 return ret;
6730}
6731
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006732static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
6733 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006734{
6735 int ret = 0;
6736 struct ufs_clk_info *clki;
6737 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006738 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006739 ktime_t start = ktime_get();
6740 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006741
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03006742 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006743 goto out;
6744
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006745 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
6746 if (ret)
6747 return ret;
6748
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006749 list_for_each_entry(clki, head, list) {
6750 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006751 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
6752 continue;
6753
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006754 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006755 if (on && !clki->enabled) {
6756 ret = clk_prepare_enable(clki->clk);
6757 if (ret) {
6758 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
6759 __func__, clki->name, ret);
6760 goto out;
6761 }
6762 } else if (!on && clki->enabled) {
6763 clk_disable_unprepare(clki->clk);
6764 }
6765 clki->enabled = on;
6766 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
6767 clki->name, on ? "en" : "dis");
6768 }
6769 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006770
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006771 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
6772 if (ret)
6773 return ret;
6774
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006775out:
6776 if (ret) {
6777 list_for_each_entry(clki, head, list) {
6778 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
6779 clk_disable_unprepare(clki->clk);
6780 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006781 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006782 spin_lock_irqsave(hba->host->host_lock, flags);
6783 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006784 trace_ufshcd_clk_gating(dev_name(hba->dev),
6785 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006786 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006787 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006788
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006789 if (clk_state_changed)
6790 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
6791 (on ? "on" : "off"),
6792 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006793 return ret;
6794}
6795
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006796static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
6797{
6798 return __ufshcd_setup_clocks(hba, on, false);
6799}
6800
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006801static int ufshcd_init_clocks(struct ufs_hba *hba)
6802{
6803 int ret = 0;
6804 struct ufs_clk_info *clki;
6805 struct device *dev = hba->dev;
6806 struct list_head *head = &hba->clk_list_head;
6807
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03006808 if (list_empty(head))
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006809 goto out;
6810
6811 list_for_each_entry(clki, head, list) {
6812 if (!clki->name)
6813 continue;
6814
6815 clki->clk = devm_clk_get(dev, clki->name);
6816 if (IS_ERR(clki->clk)) {
6817 ret = PTR_ERR(clki->clk);
6818 dev_err(dev, "%s: %s clk get failed, %d\n",
6819 __func__, clki->name, ret);
6820 goto out;
6821 }
6822
6823 if (clki->max_freq) {
6824 ret = clk_set_rate(clki->clk, clki->max_freq);
6825 if (ret) {
6826 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6827 __func__, clki->name,
6828 clki->max_freq, ret);
6829 goto out;
6830 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03006831 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006832 }
6833 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
6834 clki->name, clk_get_rate(clki->clk));
6835 }
6836out:
6837 return ret;
6838}
6839
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006840static int ufshcd_variant_hba_init(struct ufs_hba *hba)
6841{
6842 int err = 0;
6843
6844 if (!hba->vops)
6845 goto out;
6846
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006847 err = ufshcd_vops_init(hba);
6848 if (err)
6849 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006850
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006851 err = ufshcd_vops_setup_regulators(hba, true);
6852 if (err)
6853 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006854
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006855 goto out;
6856
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006857out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006858 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006859out:
6860 if (err)
6861 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006862 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006863 return err;
6864}
6865
6866static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
6867{
6868 if (!hba->vops)
6869 return;
6870
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006871 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006872
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006873 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006874}
6875
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006876static int ufshcd_hba_init(struct ufs_hba *hba)
6877{
6878 int err;
6879
Raviv Shvili6a771a62014-09-25 15:32:24 +03006880 /*
6881 * Handle host controller power separately from the UFS device power
6882 * rails as it will help controlling the UFS host controller power
6883 * collapse easily which is different than UFS device power collapse.
6884 * Also, enable the host controller power before we go ahead with rest
6885 * of the initialization here.
6886 */
6887 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006888 if (err)
6889 goto out;
6890
Raviv Shvili6a771a62014-09-25 15:32:24 +03006891 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006892 if (err)
6893 goto out;
6894
Raviv Shvili6a771a62014-09-25 15:32:24 +03006895 err = ufshcd_init_clocks(hba);
6896 if (err)
6897 goto out_disable_hba_vreg;
6898
6899 err = ufshcd_setup_clocks(hba, true);
6900 if (err)
6901 goto out_disable_hba_vreg;
6902
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006903 err = ufshcd_init_vreg(hba);
6904 if (err)
6905 goto out_disable_clks;
6906
6907 err = ufshcd_setup_vreg(hba, true);
6908 if (err)
6909 goto out_disable_clks;
6910
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006911 err = ufshcd_variant_hba_init(hba);
6912 if (err)
6913 goto out_disable_vreg;
6914
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006915 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006916 goto out;
6917
6918out_disable_vreg:
6919 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006920out_disable_clks:
6921 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03006922out_disable_hba_vreg:
6923 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006924out:
6925 return err;
6926}
6927
6928static void ufshcd_hba_exit(struct ufs_hba *hba)
6929{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006930 if (hba->is_powered) {
6931 ufshcd_variant_hba_exit(hba);
6932 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07006933 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006934 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006935 if (hba->devfreq)
6936 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006937 destroy_workqueue(hba->clk_scaling.workq);
6938 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006939 ufshcd_setup_clocks(hba, false);
6940 ufshcd_setup_hba_vreg(hba, false);
6941 hba->is_powered = false;
6942 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006943}
6944
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006945static int
6946ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306947{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006948 unsigned char cmd[6] = {REQUEST_SENSE,
6949 0,
6950 0,
6951 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006952 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006953 0};
6954 char *buffer;
6955 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306956
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006957 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006958 if (!buffer) {
6959 ret = -ENOMEM;
6960 goto out;
6961 }
6962
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01006963 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
6964 UFSHCD_REQ_SENSE_SIZE, NULL, NULL,
6965 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006966 if (ret)
6967 pr_err("%s: failed with err %d\n", __func__, ret);
6968
6969 kfree(buffer);
6970out:
6971 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306972}
6973
6974/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006975 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6976 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306977 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006978 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306979 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006980 * Returns 0 if requested power mode is set successfully
6981 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306982 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006983static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
6984 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306985{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006986 unsigned char cmd[6] = { START_STOP };
6987 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006988 struct scsi_device *sdp;
6989 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006990 int ret;
6991
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006992 spin_lock_irqsave(hba->host->host_lock, flags);
6993 sdp = hba->sdev_ufs_device;
6994 if (sdp) {
6995 ret = scsi_device_get(sdp);
6996 if (!ret && !scsi_device_online(sdp)) {
6997 ret = -ENODEV;
6998 scsi_device_put(sdp);
6999 }
7000 } else {
7001 ret = -ENODEV;
7002 }
7003 spin_unlock_irqrestore(hba->host->host_lock, flags);
7004
7005 if (ret)
7006 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007007
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307008 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007009 * If scsi commands fail, the scsi mid-layer schedules scsi error-
7010 * handling, which would wait for host to be resumed. Since we know
7011 * we are functional while we are here, skip host resume in error
7012 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307013 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007014 hba->host->eh_noresume = 1;
7015 if (hba->wlun_dev_clr_ua) {
7016 ret = ufshcd_send_request_sense(hba, sdp);
7017 if (ret)
7018 goto out;
7019 /* Unit attention condition is cleared now */
7020 hba->wlun_dev_clr_ua = false;
7021 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307022
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007023 cmd[4] = pwr_mode << 4;
7024
7025 /*
7026 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02007027 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007028 * already suspended childs.
7029 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01007030 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
7031 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007032 if (ret) {
7033 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02007034 "START_STOP failed for power mode: %d, result %x\n",
7035 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01007036 if (driver_byte(ret) & DRIVER_SENSE)
7037 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007038 }
7039
7040 if (!ret)
7041 hba->curr_dev_pwr_mode = pwr_mode;
7042out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03007043 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007044 hba->host->eh_noresume = 0;
7045 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307046}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307047
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007048static int ufshcd_link_state_transition(struct ufs_hba *hba,
7049 enum uic_link_state req_link_state,
7050 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307051{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007052 int ret = 0;
7053
7054 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307055 return 0;
7056
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007057 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7058 ret = ufshcd_uic_hibern8_enter(hba);
7059 if (!ret)
7060 ufshcd_set_link_hibern8(hba);
7061 else
7062 goto out;
7063 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307064 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007065 * If autobkops is enabled, link can't be turned off because
7066 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307067 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007068 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
7069 (!check_for_bkops || (check_for_bkops &&
7070 !hba->auto_bkops_enabled))) {
7071 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007072 * Let's make sure that link is in low power mode, we are doing
7073 * this currently by putting the link in Hibern8. Otherway to
7074 * put the link in low power mode is to send the DME end point
7075 * to device and then send the DME reset command to local
7076 * unipro. But putting the link in hibern8 is much faster.
7077 */
7078 ret = ufshcd_uic_hibern8_enter(hba);
7079 if (ret)
7080 goto out;
7081 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007082 * Change controller state to "reset state" which
7083 * should also put the link in off/reset state
7084 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007085 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007086 /*
7087 * TODO: Check if we need any delay to make sure that
7088 * controller is reset
7089 */
7090 ufshcd_set_link_off(hba);
7091 }
7092
7093out:
7094 return ret;
7095}
7096
7097static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7098{
7099 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007100 * It seems some UFS devices may keep drawing more than sleep current
7101 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7102 * To avoid this situation, add 2ms delay before putting these UFS
7103 * rails in LPM mode.
7104 */
7105 if (!ufshcd_is_link_active(hba) &&
7106 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
7107 usleep_range(2000, 2100);
7108
7109 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007110 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7111 * power.
7112 *
7113 * If UFS device and link is in OFF state, all power supplies (VCC,
7114 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7115 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7116 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7117 *
7118 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7119 * in low power state which would save some power.
7120 */
7121 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7122 !hba->dev_info.is_lu_power_on_wp) {
7123 ufshcd_setup_vreg(hba, false);
7124 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7125 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7126 if (!ufshcd_is_link_active(hba)) {
7127 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7128 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7129 }
7130 }
7131}
7132
7133static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7134{
7135 int ret = 0;
7136
7137 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7138 !hba->dev_info.is_lu_power_on_wp) {
7139 ret = ufshcd_setup_vreg(hba, true);
7140 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007141 if (!ret && !ufshcd_is_link_active(hba)) {
7142 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7143 if (ret)
7144 goto vcc_disable;
7145 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7146 if (ret)
7147 goto vccq_lpm;
7148 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07007149 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007150 }
7151 goto out;
7152
7153vccq_lpm:
7154 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7155vcc_disable:
7156 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7157out:
7158 return ret;
7159}
7160
7161static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7162{
7163 if (ufshcd_is_link_off(hba))
7164 ufshcd_setup_hba_vreg(hba, false);
7165}
7166
7167static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7168{
7169 if (ufshcd_is_link_off(hba))
7170 ufshcd_setup_hba_vreg(hba, true);
7171}
7172
7173/**
7174 * ufshcd_suspend - helper function for suspend operations
7175 * @hba: per adapter instance
7176 * @pm_op: desired low power operation type
7177 *
7178 * This function will try to put the UFS device and link into low power
7179 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7180 * (System PM level).
7181 *
7182 * If this function is called during shutdown, it will make sure that
7183 * both UFS device and UFS link is powered off.
7184 *
7185 * NOTE: UFS device & link must be active before we enter in this function.
7186 *
7187 * Returns 0 for success and non-zero for failure
7188 */
7189static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7190{
7191 int ret = 0;
7192 enum ufs_pm_level pm_lvl;
7193 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7194 enum uic_link_state req_link_state;
7195
7196 hba->pm_op_in_progress = 1;
7197 if (!ufshcd_is_shutdown_pm(pm_op)) {
7198 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7199 hba->rpm_lvl : hba->spm_lvl;
7200 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7201 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7202 } else {
7203 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7204 req_link_state = UIC_LINK_OFF_STATE;
7205 }
7206
7207 /*
7208 * If we can't transition into any of the low power modes
7209 * just gate the clocks.
7210 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007211 ufshcd_hold(hba, false);
7212 hba->clk_gating.is_suspended = true;
7213
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007214 if (hba->clk_scaling.is_allowed) {
7215 cancel_work_sync(&hba->clk_scaling.suspend_work);
7216 cancel_work_sync(&hba->clk_scaling.resume_work);
7217 ufshcd_suspend_clkscaling(hba);
7218 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007219
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007220 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
7221 req_link_state == UIC_LINK_ACTIVE_STATE) {
7222 goto disable_clks;
7223 }
7224
7225 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
7226 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007227 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007228
7229 /* UFS device & link must be active before we enter in this function */
7230 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
7231 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007232 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007233 }
7234
7235 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03007236 if (ufshcd_can_autobkops_during_suspend(hba)) {
7237 /*
7238 * The device is idle with no requests in the queue,
7239 * allow background operations if bkops status shows
7240 * that performance might be impacted.
7241 */
7242 ret = ufshcd_urgent_bkops(hba);
7243 if (ret)
7244 goto enable_gating;
7245 } else {
7246 /* make sure that auto bkops is disabled */
7247 ufshcd_disable_auto_bkops(hba);
7248 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007249 }
7250
7251 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
7252 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
7253 !ufshcd_is_runtime_pm(pm_op))) {
7254 /* ensure that bkops is disabled */
7255 ufshcd_disable_auto_bkops(hba);
7256 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
7257 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007258 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007259 }
7260
7261 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
7262 if (ret)
7263 goto set_dev_active;
7264
7265 ufshcd_vreg_set_lpm(hba);
7266
7267disable_clks:
7268 /*
7269 * Call vendor specific suspend callback. As these callbacks may access
7270 * vendor specific host controller register space call them before the
7271 * host clocks are ON.
7272 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007273 ret = ufshcd_vops_suspend(hba, pm_op);
7274 if (ret)
7275 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007276
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007277 if (!ufshcd_is_link_active(hba))
7278 ufshcd_setup_clocks(hba, false);
7279 else
7280 /* If link is active, device ref_clk can't be switched off */
7281 __ufshcd_setup_clocks(hba, false, true);
7282
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007283 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007284 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007285 /*
7286 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007287 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007288 */
7289 ufshcd_disable_irq(hba);
7290 /* Put the host controller in low power mode if possible */
7291 ufshcd_hba_vreg_set_lpm(hba);
7292 goto out;
7293
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007294set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007295 if (hba->clk_scaling.is_allowed)
7296 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007297 ufshcd_vreg_set_hpm(hba);
7298 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
7299 ufshcd_set_link_active(hba);
7300 else if (ufshcd_is_link_off(hba))
7301 ufshcd_host_reset_and_restore(hba);
7302set_dev_active:
7303 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
7304 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007305enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007306 if (hba->clk_scaling.is_allowed)
7307 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007308 hba->clk_gating.is_suspended = false;
7309 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007310out:
7311 hba->pm_op_in_progress = 0;
7312 return ret;
7313}
7314
7315/**
7316 * ufshcd_resume - helper function for resume operations
7317 * @hba: per adapter instance
7318 * @pm_op: runtime PM or system PM
7319 *
7320 * This function basically brings the UFS device, UniPro link and controller
7321 * to active state.
7322 *
7323 * Returns 0 for success and non-zero for failure
7324 */
7325static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7326{
7327 int ret;
7328 enum uic_link_state old_link_state;
7329
7330 hba->pm_op_in_progress = 1;
7331 old_link_state = hba->uic_link_state;
7332
7333 ufshcd_hba_vreg_set_hpm(hba);
7334 /* Make sure clocks are enabled before accessing controller */
7335 ret = ufshcd_setup_clocks(hba, true);
7336 if (ret)
7337 goto out;
7338
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007339 /* enable the host irq as host controller would be active soon */
7340 ret = ufshcd_enable_irq(hba);
7341 if (ret)
7342 goto disable_irq_and_vops_clks;
7343
7344 ret = ufshcd_vreg_set_hpm(hba);
7345 if (ret)
7346 goto disable_irq_and_vops_clks;
7347
7348 /*
7349 * Call vendor specific resume callback. As these callbacks may access
7350 * vendor specific host controller register space call them when the
7351 * host clocks are ON.
7352 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007353 ret = ufshcd_vops_resume(hba, pm_op);
7354 if (ret)
7355 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007356
7357 if (ufshcd_is_link_hibern8(hba)) {
7358 ret = ufshcd_uic_hibern8_exit(hba);
7359 if (!ret)
7360 ufshcd_set_link_active(hba);
7361 else
7362 goto vendor_suspend;
7363 } else if (ufshcd_is_link_off(hba)) {
7364 ret = ufshcd_host_reset_and_restore(hba);
7365 /*
7366 * ufshcd_host_reset_and_restore() should have already
7367 * set the link state as active
7368 */
7369 if (ret || !ufshcd_is_link_active(hba))
7370 goto vendor_suspend;
7371 }
7372
7373 if (!ufshcd_is_ufs_dev_active(hba)) {
7374 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
7375 if (ret)
7376 goto set_old_link_state;
7377 }
7378
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08007379 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
7380 ufshcd_enable_auto_bkops(hba);
7381 else
7382 /*
7383 * If BKOPs operations are urgently needed at this moment then
7384 * keep auto-bkops enabled or else disable it.
7385 */
7386 ufshcd_urgent_bkops(hba);
7387
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007388 hba->clk_gating.is_suspended = false;
7389
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007390 if (hba->clk_scaling.is_allowed)
7391 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007392
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007393 /* Schedule clock gating in case of no access to UFS device yet */
7394 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007395 goto out;
7396
7397set_old_link_state:
7398 ufshcd_link_state_transition(hba, old_link_state, 0);
7399vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007400 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007401disable_vreg:
7402 ufshcd_vreg_set_lpm(hba);
7403disable_irq_and_vops_clks:
7404 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007405 if (hba->clk_scaling.is_allowed)
7406 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007407 ufshcd_setup_clocks(hba, false);
7408out:
7409 hba->pm_op_in_progress = 0;
7410 return ret;
7411}
7412
7413/**
7414 * ufshcd_system_suspend - system suspend routine
7415 * @hba: per adapter instance
7416 * @pm_op: runtime PM or system PM
7417 *
7418 * Check the description of ufshcd_suspend() function for more details.
7419 *
7420 * Returns 0 for success and non-zero for failure
7421 */
7422int ufshcd_system_suspend(struct ufs_hba *hba)
7423{
7424 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007425 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007426
7427 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03007428 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007429
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007430 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
7431 hba->curr_dev_pwr_mode) &&
7432 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
7433 hba->uic_link_state))
7434 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007435
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007436 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007437 /*
7438 * UFS device and/or UFS link low power states during runtime
7439 * suspend seems to be different than what is expected during
7440 * system suspend. Hence runtime resume the devic & link and
7441 * let the system suspend low power states to take effect.
7442 * TODO: If resume takes longer time, we might have optimize
7443 * it in future by not resuming everything if possible.
7444 */
7445 ret = ufshcd_runtime_resume(hba);
7446 if (ret)
7447 goto out;
7448 }
7449
7450 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
7451out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007452 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
7453 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007454 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03007455 if (!ret)
7456 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007457 return ret;
7458}
7459EXPORT_SYMBOL(ufshcd_system_suspend);
7460
7461/**
7462 * ufshcd_system_resume - system resume routine
7463 * @hba: per adapter instance
7464 *
7465 * Returns 0 for success and non-zero for failure
7466 */
7467
7468int ufshcd_system_resume(struct ufs_hba *hba)
7469{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007470 int ret = 0;
7471 ktime_t start = ktime_get();
7472
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007473 if (!hba)
7474 return -EINVAL;
7475
7476 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007477 /*
7478 * Let the runtime resume take care of resuming
7479 * if runtime suspended.
7480 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007481 goto out;
7482 else
7483 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
7484out:
7485 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
7486 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007487 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007488 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007489}
7490EXPORT_SYMBOL(ufshcd_system_resume);
7491
7492/**
7493 * ufshcd_runtime_suspend - runtime suspend routine
7494 * @hba: per adapter instance
7495 *
7496 * Check the description of ufshcd_suspend() function for more details.
7497 *
7498 * Returns 0 for success and non-zero for failure
7499 */
7500int ufshcd_runtime_suspend(struct ufs_hba *hba)
7501{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007502 int ret = 0;
7503 ktime_t start = ktime_get();
7504
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007505 if (!hba)
7506 return -EINVAL;
7507
7508 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007509 goto out;
7510 else
7511 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
7512out:
7513 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
7514 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007515 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007516 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307517}
7518EXPORT_SYMBOL(ufshcd_runtime_suspend);
7519
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007520/**
7521 * ufshcd_runtime_resume - runtime resume routine
7522 * @hba: per adapter instance
7523 *
7524 * This function basically brings the UFS device, UniPro link and controller
7525 * to active state. Following operations are done in this function:
7526 *
7527 * 1. Turn on all the controller related clocks
7528 * 2. Bring the UniPro link out of Hibernate state
7529 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
7530 * to active state.
7531 * 4. If auto-bkops is enabled on the device, disable it.
7532 *
7533 * So following would be the possible power state after this function return
7534 * successfully:
7535 * S1: UFS device in Active state with VCC rail ON
7536 * UniPro link in Active state
7537 * All the UFS/UniPro controller clocks are ON
7538 *
7539 * Returns 0 for success and non-zero for failure
7540 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307541int ufshcd_runtime_resume(struct ufs_hba *hba)
7542{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007543 int ret = 0;
7544 ktime_t start = ktime_get();
7545
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007546 if (!hba)
7547 return -EINVAL;
7548
7549 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007550 goto out;
7551 else
7552 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
7553out:
7554 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
7555 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007556 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007557 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307558}
7559EXPORT_SYMBOL(ufshcd_runtime_resume);
7560
7561int ufshcd_runtime_idle(struct ufs_hba *hba)
7562{
7563 return 0;
7564}
7565EXPORT_SYMBOL(ufshcd_runtime_idle);
7566
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307567/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007568 * ufshcd_shutdown - shutdown routine
7569 * @hba: per adapter instance
7570 *
7571 * This function would power off both UFS device and UFS link.
7572 *
7573 * Returns 0 always to allow force shutdown even in case of errors.
7574 */
7575int ufshcd_shutdown(struct ufs_hba *hba)
7576{
7577 int ret = 0;
7578
7579 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
7580 goto out;
7581
7582 if (pm_runtime_suspended(hba->dev)) {
7583 ret = ufshcd_runtime_resume(hba);
7584 if (ret)
7585 goto out;
7586 }
7587
7588 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
7589out:
7590 if (ret)
7591 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
7592 /* allow force shutdown even in case of errors */
7593 return 0;
7594}
7595EXPORT_SYMBOL(ufshcd_shutdown);
7596
7597/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307598 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307599 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307600 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307601 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307602void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307603{
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02007604 ufs_sysfs_remove_nodes(hba->dev);
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05307605 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307606 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307607 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02007608 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307609
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007610 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007611 if (ufshcd_is_clkscaling_supported(hba))
7612 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007613 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307614}
7615EXPORT_SYMBOL_GPL(ufshcd_remove);
7616
7617/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02007618 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
7619 * @hba: pointer to Host Bus Adapter (HBA)
7620 */
7621void ufshcd_dealloc_host(struct ufs_hba *hba)
7622{
7623 scsi_host_put(hba->host);
7624}
7625EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
7626
7627/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007628 * ufshcd_set_dma_mask - Set dma mask based on the controller
7629 * addressing capability
7630 * @hba: per adapter instance
7631 *
7632 * Returns 0 for success, non-zero for failure
7633 */
7634static int ufshcd_set_dma_mask(struct ufs_hba *hba)
7635{
7636 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
7637 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
7638 return 0;
7639 }
7640 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
7641}
7642
7643/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007644 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307645 * @dev: pointer to device handle
7646 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307647 * Returns 0 on success, non-zero value on failure
7648 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007649int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307650{
7651 struct Scsi_Host *host;
7652 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007653 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307654
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307655 if (!dev) {
7656 dev_err(dev,
7657 "Invalid memory reference for dev is NULL\n");
7658 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307659 goto out_error;
7660 }
7661
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307662 host = scsi_host_alloc(&ufshcd_driver_template,
7663 sizeof(struct ufs_hba));
7664 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307665 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307666 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307667 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307668 }
7669 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307670 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307671 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007672 *hba_handle = hba;
7673
Szymon Mielczarek566ec9a2017-06-05 11:36:54 +03007674 INIT_LIST_HEAD(&hba->clk_list_head);
7675
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007676out_error:
7677 return err;
7678}
7679EXPORT_SYMBOL(ufshcd_alloc_host);
7680
7681/**
7682 * ufshcd_init - Driver initialization routine
7683 * @hba: per-adapter instance
7684 * @mmio_base: base register address
7685 * @irq: Interrupt line of device
7686 * Returns 0 on success, non-zero value on failure
7687 */
7688int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
7689{
7690 int err;
7691 struct Scsi_Host *host = hba->host;
7692 struct device *dev = hba->dev;
7693
7694 if (!mmio_base) {
7695 dev_err(hba->dev,
7696 "Invalid memory reference for mmio_base is NULL\n");
7697 err = -ENODEV;
7698 goto out_error;
7699 }
7700
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307701 hba->mmio_base = mmio_base;
7702 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307703
Potomski, MichalXa4b0e8a2017-02-23 09:05:30 +00007704 /* Set descriptor lengths to specification defaults */
7705 ufshcd_def_desc_sizes(hba);
7706
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007707 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007708 if (err)
7709 goto out_error;
7710
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307711 /* Read capabilities registers */
7712 ufshcd_hba_capabilities(hba);
7713
7714 /* Get UFS version supported by the controller */
7715 hba->ufs_version = ufshcd_get_ufs_version(hba);
7716
Yaniv Gardic01848c2016-12-05 19:25:02 -08007717 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
7718 (hba->ufs_version != UFSHCI_VERSION_11) &&
7719 (hba->ufs_version != UFSHCI_VERSION_20) &&
7720 (hba->ufs_version != UFSHCI_VERSION_21))
7721 dev_err(hba->dev, "invalid UFS version 0x%x\n",
7722 hba->ufs_version);
7723
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307724 /* Get Interrupt bit mask per version */
7725 hba->intr_mask = ufshcd_get_intr_mask(hba);
7726
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007727 err = ufshcd_set_dma_mask(hba);
7728 if (err) {
7729 dev_err(hba->dev, "set dma mask failed\n");
7730 goto out_disable;
7731 }
7732
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307733 /* Allocate memory for host memory space */
7734 err = ufshcd_memory_alloc(hba);
7735 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307736 dev_err(hba->dev, "Memory allocation failed\n");
7737 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307738 }
7739
7740 /* Configure LRB */
7741 ufshcd_host_memory_configure(hba);
7742
7743 host->can_queue = hba->nutrs;
7744 host->cmd_per_lun = hba->nutrs;
7745 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03007746 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307747 host->max_channel = UFSHCD_MAX_CHANNEL;
7748 host->unique_id = host->host_no;
7749 host->max_cmd_len = MAX_CDB_SIZE;
7750
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007751 hba->max_pwr_info.is_valid = false;
7752
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307753 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05307754 init_waitqueue_head(&hba->tm_wq);
7755 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307756
7757 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05307758 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307759 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307760
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307761 /* Initialize UIC command mutex */
7762 mutex_init(&hba->uic_cmd_mutex);
7763
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307764 /* Initialize mutex for device management commands */
7765 mutex_init(&hba->dev_cmd.lock);
7766
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08007767 init_rwsem(&hba->clk_scaling_lock);
7768
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307769 /* Initialize device management tag acquire wait queue */
7770 init_waitqueue_head(&hba->dev_cmd.tag_wq);
7771
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007772 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02007773
7774 /*
7775 * In order to avoid any spurious interrupt immediately after
7776 * registering UFS controller interrupt handler, clear any pending UFS
7777 * interrupt status and disable all the UFS interrupts.
7778 */
7779 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
7780 REG_INTERRUPT_STATUS);
7781 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
7782 /*
7783 * Make sure that UFS interrupts are disabled and any pending interrupt
7784 * status is cleared before registering UFS interrupt handler.
7785 */
7786 mb();
7787
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307788 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09007789 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307790 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307791 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007792 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007793 } else {
7794 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307795 }
7796
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307797 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307798 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307799 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007800 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307801 }
7802
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307803 /* Host controller enable */
7804 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307805 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307806 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08007807 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08007808 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307809 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307810 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307811
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007812 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007813 char wq_name[sizeof("ufs_clkscaling_00")];
7814
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007815 INIT_WORK(&hba->clk_scaling.suspend_work,
7816 ufshcd_clk_scaling_suspend_work);
7817 INIT_WORK(&hba->clk_scaling.resume_work,
7818 ufshcd_clk_scaling_resume_work);
7819
Tomohiro Kusumid985c6ea2017-03-28 16:49:29 +03007820 snprintf(wq_name, sizeof(wq_name), "ufs_clkscaling_%d",
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007821 host->host_no);
7822 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
7823
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007824 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007825 }
7826
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08007827 /*
7828 * Set the default power management level for runtime and system PM.
7829 * Default power saving mode is to keep UFS link in Hibern8 state
7830 * and UFS device in sleep state.
7831 */
7832 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7833 UFS_SLEEP_PWR_MODE,
7834 UIC_LINK_HIBERN8_STATE);
7835 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7836 UFS_SLEEP_PWR_MODE,
7837 UIC_LINK_HIBERN8_STATE);
7838
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05307839 /* Hold auto suspend until async scan completes */
7840 pm_runtime_get_sync(dev);
7841
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007842 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007843 * We are assuming that device wasn't put in sleep/power-down
7844 * state exclusively during the boot stage before kernel.
7845 * This assumption helps avoid doing link startup twice during
7846 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007847 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007848 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007849
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307850 async_schedule(ufshcd_async_scan, hba);
Stanislav Nijnikovcbb68132018-02-15 14:14:01 +02007851 ufs_sysfs_add_nodes(hba->dev);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307852
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307853 return 0;
7854
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307855out_remove_scsi_host:
7856 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007857exit_gating:
7858 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307859out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007860 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007861 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307862out_error:
7863 return err;
7864}
7865EXPORT_SYMBOL_GPL(ufshcd_init);
7866
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307867MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7868MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05307869MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307870MODULE_LICENSE("GPL");
7871MODULE_VERSION(UFSHCD_DRIVER_VERSION);