blob: 089b76f43ad963fd98dfa5c1d568745b37655434 [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"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053047
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -080048#define CREATE_TRACE_POINTS
49#include <trace/events/ufs.h>
50
Gilad Bronerdcea0bf2016-10-17 17:09:48 -070051#define UFSHCD_REQ_SENSE_SIZE 18
52
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053053#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
54 UTP_TASK_REQ_COMPL |\
55 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053056/* UIC command timeout, unit: ms */
57#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053058
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053059/* NOP OUT retries waiting for NOP IN response */
60#define NOP_OUT_RETRIES 10
61/* Timeout after 30 msecs if NOP OUT hangs without response */
62#define NOP_OUT_TIMEOUT 30 /* msecs */
63
Dolev Raviv68078d52013-07-30 00:35:58 +053064/* Query request retries */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080065#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +053066/* Query request timeout */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080067#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +053068
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053069/* Task management command timeout */
70#define TM_CMD_TIMEOUT 100 /* msecs */
71
Yaniv Gardi64238fb2016-02-01 15:02:43 +020072/* maximum number of retries for a general UIC command */
73#define UFS_UIC_COMMAND_RETRIES 3
74
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030075/* maximum number of link-startup retries */
76#define DME_LINKSTARTUP_RETRIES 3
77
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020078/* Maximum retries for Hibern8 enter */
79#define UIC_HIBERN8_ENTER_RETRIES 3
80
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030081/* maximum number of reset retries before giving up */
82#define MAX_HOST_RESET_RETRIES 5
83
Dolev Raviv68078d52013-07-30 00:35:58 +053084/* Expose the flag value from utp_upiu_query.value */
85#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
86
Seungwon Jeon7d568652013-08-31 21:40:20 +053087/* Interrupt aggregation default timeout, unit: 40us */
88#define INT_AGGR_DEF_TO 0x02
89
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030090#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
91 ({ \
92 int _ret; \
93 if (_on) \
94 _ret = ufshcd_enable_vreg(_dev, _vreg); \
95 else \
96 _ret = ufshcd_disable_vreg(_dev, _vreg); \
97 _ret; \
98 })
99
Dolev Raviv66cc8202016-12-22 18:39:42 -0800100#define ufshcd_hex_dump(prefix_str, buf, len) \
101print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false)
102
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300103static u32 ufs_query_desc_max_size[] = {
104 QUERY_DESC_DEVICE_MAX_SIZE,
105 QUERY_DESC_CONFIGURAION_MAX_SIZE,
106 QUERY_DESC_UNIT_MAX_SIZE,
107 QUERY_DESC_RFU_MAX_SIZE,
108 QUERY_DESC_INTERCONNECT_MAX_SIZE,
109 QUERY_DESC_STRING_MAX_SIZE,
110 QUERY_DESC_RFU_MAX_SIZE,
Tomas Winkler1ce21792016-02-09 10:25:40 +0200111 QUERY_DESC_GEOMETRY_MAX_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300112 QUERY_DESC_POWER_MAX_SIZE,
113 QUERY_DESC_RFU_MAX_SIZE,
114};
115
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530116enum {
117 UFSHCD_MAX_CHANNEL = 0,
118 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530119 UFSHCD_CMD_PER_LUN = 32,
120 UFSHCD_CAN_QUEUE = 32,
121};
122
123/* UFSHCD states */
124enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530125 UFSHCD_STATE_RESET,
126 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530127 UFSHCD_STATE_OPERATIONAL,
Zang Leigang141f8162016-11-16 11:29:37 +0800128 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530129};
130
131/* UFSHCD error handling flags */
132enum {
133 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530134};
135
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530136/* UFSHCD UIC layer error flags */
137enum {
138 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200139 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
140 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
141 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
142 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
143 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530144};
145
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530146/* Interrupt configuration options */
147enum {
148 UFSHCD_INT_DISABLE,
149 UFSHCD_INT_ENABLE,
150 UFSHCD_INT_CLEAR,
151};
152
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530153#define ufshcd_set_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300154 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530155#define ufshcd_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300156 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530157#define ufshcd_clear_eh_in_progress(h) \
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +0300158 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530159
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300160#define ufshcd_set_ufs_dev_active(h) \
161 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
162#define ufshcd_set_ufs_dev_sleep(h) \
163 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
164#define ufshcd_set_ufs_dev_poweroff(h) \
165 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
166#define ufshcd_is_ufs_dev_active(h) \
167 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
168#define ufshcd_is_ufs_dev_sleep(h) \
169 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
170#define ufshcd_is_ufs_dev_poweroff(h) \
171 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
172
173static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
174 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
175 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
176 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
177 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
178 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
179 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
180};
181
182static inline enum ufs_dev_pwr_mode
183ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
184{
185 return ufs_pm_lvl_states[lvl].dev_state;
186}
187
188static inline enum uic_link_state
189ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
190{
191 return ufs_pm_lvl_states[lvl].link_state;
192}
193
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -0800194static inline enum ufs_pm_level
195ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state,
196 enum uic_link_state link_state)
197{
198 enum ufs_pm_level lvl;
199
200 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++) {
201 if ((ufs_pm_lvl_states[lvl].dev_state == dev_state) &&
202 (ufs_pm_lvl_states[lvl].link_state == link_state))
203 return lvl;
204 }
205
206 /* if no match found, return the level 0 */
207 return UFS_PM_LVL_0;
208}
209
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800210static struct ufs_dev_fix ufs_fixups[] = {
211 /* UFS cards deviations table */
212 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
213 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
214 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
215 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
216 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
217 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
218 UFS_DEVICE_NO_FASTAUTO),
219 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
220 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
221 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
222 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
223 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
224 UFS_DEVICE_QUIRK_PA_TACTIVATE),
225 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
226 UFS_DEVICE_QUIRK_PA_TACTIVATE),
227 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
228 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
229 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
230
231 END_FIX
232};
233
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530234static void ufshcd_tmc_handler(struct ufs_hba *hba);
235static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530236static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800237static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530238static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300239static void ufshcd_hba_exit(struct ufs_hba *hba);
240static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300241static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
242 bool skip_ref_clk);
243static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200244static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300245static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
246static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300247static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300248static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800249static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
250static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -0800251static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800252static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300253static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300254static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
255 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300256static int ufshcd_change_power_mode(struct ufs_hba *hba,
257 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200258static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
259{
260 return tag >= 0 && tag < hba->nutrs;
261}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300262
263static inline int ufshcd_enable_irq(struct ufs_hba *hba)
264{
265 int ret = 0;
266
267 if (!hba->is_irq_enabled) {
268 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
269 hba);
270 if (ret)
271 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
272 __func__, ret);
273 hba->is_irq_enabled = true;
274 }
275
276 return ret;
277}
278
279static inline void ufshcd_disable_irq(struct ufs_hba *hba)
280{
281 if (hba->is_irq_enabled) {
282 free_irq(hba->irq, hba);
283 hba->is_irq_enabled = false;
284 }
285}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530286
Yaniv Gardib573d482016-03-10 17:37:09 +0200287/* replace non-printable or non-ASCII characters with spaces */
288static inline void ufshcd_remove_non_printable(char *val)
289{
290 if (!val)
291 return;
292
293 if (*val < 0x20 || *val > 0x7e)
294 *val = ' ';
295}
296
Lee Susman1a07f2d2016-12-22 18:42:03 -0800297static void ufshcd_add_command_trace(struct ufs_hba *hba,
298 unsigned int tag, const char *str)
299{
300 sector_t lba = -1;
301 u8 opcode = 0;
302 u32 intr, doorbell;
303 struct ufshcd_lrb *lrbp;
304 int transfer_len = -1;
305
306 if (!trace_ufshcd_command_enabled())
307 return;
308
309 lrbp = &hba->lrb[tag];
310
311 if (lrbp->cmd) { /* data phase exists */
312 opcode = (u8)(*lrbp->cmd->cmnd);
313 if ((opcode == READ_10) || (opcode == WRITE_10)) {
314 /*
315 * Currently we only fully trace read(10) and write(10)
316 * commands
317 */
318 if (lrbp->cmd->request && lrbp->cmd->request->bio)
319 lba =
320 lrbp->cmd->request->bio->bi_iter.bi_sector;
321 transfer_len = be32_to_cpu(
322 lrbp->ucd_req_ptr->sc.exp_data_transfer_len);
323 }
324 }
325
326 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
327 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
328 trace_ufshcd_command(dev_name(hba->dev), str, tag,
329 doorbell, transfer_len, intr, lba, opcode);
330}
331
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800332static void ufshcd_print_clk_freqs(struct ufs_hba *hba)
333{
334 struct ufs_clk_info *clki;
335 struct list_head *head = &hba->clk_list_head;
336
337 if (!head || list_empty(head))
338 return;
339
340 list_for_each_entry(clki, head, list) {
341 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq &&
342 clki->max_freq)
343 dev_err(hba->dev, "clk: %s, rate: %u\n",
344 clki->name, clki->curr_freq);
345 }
346}
347
348static void ufshcd_print_uic_err_hist(struct ufs_hba *hba,
349 struct ufs_uic_err_reg_hist *err_hist, char *err_name)
350{
351 int i;
352
353 for (i = 0; i < UIC_ERR_REG_HIST_LENGTH; i++) {
354 int p = (i + err_hist->pos - 1) % UIC_ERR_REG_HIST_LENGTH;
355
356 if (err_hist->reg[p] == 0)
357 continue;
358 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, i,
359 err_hist->reg[p], ktime_to_us(err_hist->tstamp[p]));
360 }
361}
362
Dolev Raviv66cc8202016-12-22 18:39:42 -0800363static void ufshcd_print_host_regs(struct ufs_hba *hba)
364{
365 /*
366 * hex_dump reads its data without the readl macro. This might
367 * cause inconsistency issues on some platform, as the printed
368 * values may be from cache and not the most recent value.
369 * To know whether you are looking at an un-cached version verify
370 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
371 * during platform/pci probe function.
372 */
373 ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE);
374 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
375 hba->ufs_version, hba->capabilities);
376 dev_err(hba->dev,
377 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
378 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800379 dev_err(hba->dev,
380 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n",
381 ktime_to_us(hba->ufs_stats.last_hibern8_exit_tstamp),
382 hba->ufs_stats.hibern8_exit_cnt);
383
384 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.pa_err, "pa_err");
385 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dl_err, "dl_err");
386 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.nl_err, "nl_err");
387 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.tl_err, "tl_err");
388 ufshcd_print_uic_err_hist(hba, &hba->ufs_stats.dme_err, "dme_err");
389
390 ufshcd_print_clk_freqs(hba);
391
392 if (hba->vops && hba->vops->dbg_register_dump)
393 hba->vops->dbg_register_dump(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800394}
395
396static
397void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
398{
399 struct ufshcd_lrb *lrbp;
Gilad Broner7fabb772017-02-03 16:56:50 -0800400 int prdt_length;
Dolev Raviv66cc8202016-12-22 18:39:42 -0800401 int tag;
402
403 for_each_set_bit(tag, &bitmap, hba->nutrs) {
404 lrbp = &hba->lrb[tag];
405
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800406 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n",
407 tag, ktime_to_us(lrbp->issue_time_stamp));
408 dev_err(hba->dev,
409 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
410 tag, (u64)lrbp->utrd_dma_addr);
411
Dolev Raviv66cc8202016-12-22 18:39:42 -0800412 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
413 sizeof(struct utp_transfer_req_desc));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800414 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag,
415 (u64)lrbp->ucd_req_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800416 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
417 sizeof(struct utp_upiu_req));
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800418 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag,
419 (u64)lrbp->ucd_rsp_dma_addr);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800420 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
421 sizeof(struct utp_upiu_rsp));
Dolev Raviv66cc8202016-12-22 18:39:42 -0800422
Gilad Broner7fabb772017-02-03 16:56:50 -0800423 prdt_length = le16_to_cpu(
424 lrbp->utr_descriptor_ptr->prd_table_length);
425 dev_err(hba->dev,
426 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
427 tag, prdt_length,
428 (u64)lrbp->ucd_prdt_dma_addr);
429
430 if (pr_prdt)
Dolev Raviv66cc8202016-12-22 18:39:42 -0800431 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
Gilad Broner7fabb772017-02-03 16:56:50 -0800432 sizeof(struct ufshcd_sg_entry) * prdt_length);
Dolev Raviv66cc8202016-12-22 18:39:42 -0800433 }
434}
435
436static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
437{
438 struct utp_task_req_desc *tmrdp;
439 int tag;
440
441 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
442 tmrdp = &hba->utmrdl_base_addr[tag];
443 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
444 ufshcd_hex_dump("TM TRD: ", &tmrdp->header,
445 sizeof(struct request_desc_header));
446 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU\n",
447 tag);
448 ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu,
449 sizeof(struct utp_upiu_req));
450 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU\n",
451 tag);
452 ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu,
453 sizeof(struct utp_task_req_desc));
454 }
455}
456
Gilad Broner6ba65582017-02-03 16:57:28 -0800457static void ufshcd_print_host_state(struct ufs_hba *hba)
458{
459 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state);
460 dev_err(hba->dev, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n",
461 hba->lrb_in_use, hba->outstanding_tasks, hba->outstanding_reqs);
462 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n",
463 hba->saved_err, hba->saved_uic_err);
464 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n",
465 hba->curr_dev_pwr_mode, hba->uic_link_state);
466 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n",
467 hba->pm_op_in_progress, hba->is_sys_suspended);
468 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n",
469 hba->auto_bkops_enabled, hba->host->host_self_blocked);
470 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state);
471 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n",
472 hba->eh_flags, hba->req_abort_count);
473 dev_err(hba->dev, "Host capabilities=0x%x, caps=0x%x\n",
474 hba->capabilities, hba->caps);
475 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks,
476 hba->dev_quirks);
477}
478
Dolev Ravivff8e20c2016-12-22 18:42:18 -0800479/**
480 * ufshcd_print_pwr_info - print power params as saved in hba
481 * power info
482 * @hba: per-adapter instance
483 */
484static void ufshcd_print_pwr_info(struct ufs_hba *hba)
485{
486 static const char * const names[] = {
487 "INVALID MODE",
488 "FAST MODE",
489 "SLOW_MODE",
490 "INVALID MODE",
491 "FASTAUTO_MODE",
492 "SLOWAUTO_MODE",
493 "INVALID MODE",
494 };
495
496 dev_err(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
497 __func__,
498 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
499 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
500 names[hba->pwr_info.pwr_rx],
501 names[hba->pwr_info.pwr_tx],
502 hba->pwr_info.hs_rate);
503}
504
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530505/*
506 * ufshcd_wait_for_register - wait for register value to change
507 * @hba - per-adapter interface
508 * @reg - mmio register offset
509 * @mask - mask to apply to read register value
510 * @val - wait condition
511 * @interval_us - polling interval in microsecs
512 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200513 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530514 *
515 * Returns -ETIMEDOUT on error, zero on success
516 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200517int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
518 u32 val, unsigned long interval_us,
519 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530520{
521 int err = 0;
522 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
523
524 /* ignore bits that we don't intend to wait on */
525 val = val & mask;
526
527 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200528 if (can_sleep)
529 usleep_range(interval_us, interval_us + 50);
530 else
531 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530532 if (time_after(jiffies, timeout)) {
533 if ((ufshcd_readl(hba, reg) & mask) != val)
534 err = -ETIMEDOUT;
535 break;
536 }
537 }
538
539 return err;
540}
541
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530542/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530543 * ufshcd_get_intr_mask - Get the interrupt bit mask
544 * @hba - Pointer to adapter instance
545 *
546 * Returns interrupt bit mask per version
547 */
548static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
549{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800550 u32 intr_mask = 0;
551
552 switch (hba->ufs_version) {
553 case UFSHCI_VERSION_10:
554 intr_mask = INTERRUPT_MASK_ALL_VER_10;
555 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800556 case UFSHCI_VERSION_11:
557 case UFSHCI_VERSION_20:
558 intr_mask = INTERRUPT_MASK_ALL_VER_11;
559 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800560 case UFSHCI_VERSION_21:
561 default:
562 intr_mask = INTERRUPT_MASK_ALL_VER_21;
Tomohiro Kusumi031d1e02017-03-23 12:49:04 +0200563 break;
Yaniv Gardic01848c2016-12-05 19:25:02 -0800564 }
565
566 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530567}
568
569/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530570 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
571 * @hba - Pointer to adapter instance
572 *
573 * Returns UFSHCI version supported by the controller
574 */
575static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
576{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200577 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
578 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300579
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530580 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530581}
582
583/**
584 * ufshcd_is_device_present - Check if any device connected to
585 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300586 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530587 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300588 * Returns true if device present, false if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530589 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300590static inline bool ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530591{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300592 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300593 DEVICE_PRESENT) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530594}
595
596/**
597 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
598 * @lrb: pointer to local command reference block
599 *
600 * This function is used to get the OCS field from UTRD
601 * Returns the OCS field in the UTRD
602 */
603static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
604{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530605 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530606}
607
608/**
609 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
610 * @task_req_descp: pointer to utp_task_req_desc structure
611 *
612 * This function is used to get the OCS field from UTMRD
613 * Returns the OCS field in the UTMRD
614 */
615static inline int
616ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
617{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530618 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530619}
620
621/**
622 * ufshcd_get_tm_free_slot - get a free slot for task management request
623 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530624 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530625 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530626 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
627 * Returns 0 if free slot is not available, else return 1 with tag value
628 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530629 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530630static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530631{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530632 int tag;
633 bool ret = false;
634
635 if (!free_slot)
636 goto out;
637
638 do {
639 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
640 if (tag >= hba->nutmrs)
641 goto out;
642 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
643
644 *free_slot = tag;
645 ret = true;
646out:
647 return ret;
648}
649
650static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
651{
652 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530653}
654
655/**
656 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
657 * @hba: per adapter instance
658 * @pos: position of the bit to be cleared
659 */
660static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
661{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530662 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530663}
664
665/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200666 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
667 * @hba: per adapter instance
668 * @tag: position of the bit to be cleared
669 */
670static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
671{
672 __clear_bit(tag, &hba->outstanding_reqs);
673}
674
675/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530676 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
677 * @reg: Register value of host controller status
678 *
679 * Returns integer, 0 on Success and positive value if failed
680 */
681static inline int ufshcd_get_lists_status(u32 reg)
682{
683 /*
684 * The mask 0xFF is for the following HCS register bits
685 * Bit Description
686 * 0 Device Present
687 * 1 UTRLRDY
688 * 2 UTMRLRDY
689 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200690 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530691 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200692 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530693}
694
695/**
696 * ufshcd_get_uic_cmd_result - Get the UIC command result
697 * @hba: Pointer to adapter instance
698 *
699 * This function gets the result of UIC command completion
700 * Returns 0 on success, non zero value on error
701 */
702static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
703{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530704 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530705 MASK_UIC_COMMAND_RESULT;
706}
707
708/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530709 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
710 * @hba: Pointer to adapter instance
711 *
712 * This function gets UIC command argument3
713 * Returns 0 on success, non zero value on error
714 */
715static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
716{
717 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
718}
719
720/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530721 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530722 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530723 */
724static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530725ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530726{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530727 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530728}
729
730/**
731 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
732 * @ucd_rsp_ptr: pointer to response UPIU
733 *
734 * This function gets the response status and scsi_status from response UPIU
735 * Returns the response result code.
736 */
737static inline int
738ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
739{
740 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
741}
742
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530743/*
744 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
745 * from response UPIU
746 * @ucd_rsp_ptr: pointer to response UPIU
747 *
748 * Return the data segment length.
749 */
750static inline unsigned int
751ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
752{
753 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
754 MASK_RSP_UPIU_DATA_SEG_LEN;
755}
756
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530757/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530758 * ufshcd_is_exception_event - Check if the device raised an exception event
759 * @ucd_rsp_ptr: pointer to response UPIU
760 *
761 * The function checks if the device raised an exception event indicated in
762 * the Device Information field of response UPIU.
763 *
764 * Returns true if exception is raised, false otherwise.
765 */
766static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
767{
768 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
769 MASK_RSP_EXCEPTION_EVENT ? true : false;
770}
771
772/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530773 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530774 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530775 */
776static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530777ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530778{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530779 ufshcd_writel(hba, INT_AGGR_ENABLE |
780 INT_AGGR_COUNTER_AND_TIMER_RESET,
781 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
782}
783
784/**
785 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
786 * @hba: per adapter instance
787 * @cnt: Interrupt aggregation counter threshold
788 * @tmout: Interrupt aggregation timeout value
789 */
790static inline void
791ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
792{
793 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
794 INT_AGGR_COUNTER_THLD_VAL(cnt) |
795 INT_AGGR_TIMEOUT_VAL(tmout),
796 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530797}
798
799/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300800 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
801 * @hba: per adapter instance
802 */
803static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
804{
805 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
806}
807
808/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530809 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
810 * When run-stop registers are set to 1, it indicates the
811 * host controller that it can process the requests
812 * @hba: per adapter instance
813 */
814static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
815{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530816 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
817 REG_UTP_TASK_REQ_LIST_RUN_STOP);
818 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
819 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530820}
821
822/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530823 * ufshcd_hba_start - Start controller initialization sequence
824 * @hba: per adapter instance
825 */
826static inline void ufshcd_hba_start(struct ufs_hba *hba)
827{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530828 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530829}
830
831/**
832 * ufshcd_is_hba_active - Get controller state
833 * @hba: per adapter instance
834 *
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300835 * Returns false if controller is active, true otherwise
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530836 */
Tomohiro Kusumic9e60102017-03-28 16:49:24 +0300837static inline bool ufshcd_is_hba_active(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530838{
Tomohiro Kusumi4a8eec22017-03-28 16:49:25 +0300839 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE)
840 ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530841}
842
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -0800843static const char *ufschd_uic_link_state_to_string(
844 enum uic_link_state state)
845{
846 switch (state) {
847 case UIC_LINK_OFF_STATE: return "OFF";
848 case UIC_LINK_ACTIVE_STATE: return "ACTIVE";
849 case UIC_LINK_HIBERN8_STATE: return "HIBERN8";
850 default: return "UNKNOWN";
851 }
852}
853
854static const char *ufschd_ufs_dev_pwr_mode_to_string(
855 enum ufs_dev_pwr_mode state)
856{
857 switch (state) {
858 case UFS_ACTIVE_PWR_MODE: return "ACTIVE";
859 case UFS_SLEEP_PWR_MODE: return "SLEEP";
860 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN";
861 default: return "UNKNOWN";
862 }
863}
864
Yaniv Gardi37113102016-03-10 17:37:16 +0200865u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
866{
867 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
868 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
869 (hba->ufs_version == UFSHCI_VERSION_11))
870 return UFS_UNIPRO_VER_1_41;
871 else
872 return UFS_UNIPRO_VER_1_6;
873}
874EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
875
876static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
877{
878 /*
879 * If both host and device support UniPro ver1.6 or later, PA layer
880 * parameters tuning happens during link startup itself.
881 *
882 * We can manually tune PA layer parameters if either host or device
883 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
884 * logic simple, we will only do manual tuning if local unipro version
885 * doesn't support ver1.6 or later.
886 */
887 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
888 return true;
889 else
890 return false;
891}
892
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800893static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
894{
895 int ret = 0;
896 struct ufs_clk_info *clki;
897 struct list_head *head = &hba->clk_list_head;
898 ktime_t start = ktime_get();
899 bool clk_state_changed = false;
900
901 if (!head || list_empty(head))
902 goto out;
903
904 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
905 if (ret)
906 return ret;
907
908 list_for_each_entry(clki, head, list) {
909 if (!IS_ERR_OR_NULL(clki->clk)) {
910 if (scale_up && clki->max_freq) {
911 if (clki->curr_freq == clki->max_freq)
912 continue;
913
914 clk_state_changed = true;
915 ret = clk_set_rate(clki->clk, clki->max_freq);
916 if (ret) {
917 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
918 __func__, clki->name,
919 clki->max_freq, ret);
920 break;
921 }
922 trace_ufshcd_clk_scaling(dev_name(hba->dev),
923 "scaled up", clki->name,
924 clki->curr_freq,
925 clki->max_freq);
926
927 clki->curr_freq = clki->max_freq;
928
929 } else if (!scale_up && clki->min_freq) {
930 if (clki->curr_freq == clki->min_freq)
931 continue;
932
933 clk_state_changed = true;
934 ret = clk_set_rate(clki->clk, clki->min_freq);
935 if (ret) {
936 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
937 __func__, clki->name,
938 clki->min_freq, ret);
939 break;
940 }
941 trace_ufshcd_clk_scaling(dev_name(hba->dev),
942 "scaled down", clki->name,
943 clki->curr_freq,
944 clki->min_freq);
945 clki->curr_freq = clki->min_freq;
946 }
947 }
948 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
949 clki->name, clk_get_rate(clki->clk));
950 }
951
952 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
953
954out:
955 if (clk_state_changed)
956 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
957 (scale_up ? "up" : "down"),
958 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
959 return ret;
960}
961
962/**
963 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
964 * @hba: per adapter instance
965 * @scale_up: True if scaling up and false if scaling down
966 *
967 * Returns true if scaling is required, false otherwise.
968 */
969static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
970 bool scale_up)
971{
972 struct ufs_clk_info *clki;
973 struct list_head *head = &hba->clk_list_head;
974
975 if (!head || list_empty(head))
976 return false;
977
978 list_for_each_entry(clki, head, list) {
979 if (!IS_ERR_OR_NULL(clki->clk)) {
980 if (scale_up && clki->max_freq) {
981 if (clki->curr_freq == clki->max_freq)
982 continue;
983 return true;
984 } else if (!scale_up && clki->min_freq) {
985 if (clki->curr_freq == clki->min_freq)
986 continue;
987 return true;
988 }
989 }
990 }
991
992 return false;
993}
994
995static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
996 u64 wait_timeout_us)
997{
998 unsigned long flags;
999 int ret = 0;
1000 u32 tm_doorbell;
1001 u32 tr_doorbell;
1002 bool timeout = false, do_last_check = false;
1003 ktime_t start;
1004
1005 ufshcd_hold(hba, false);
1006 spin_lock_irqsave(hba->host->host_lock, flags);
1007 /*
1008 * Wait for all the outstanding tasks/transfer requests.
1009 * Verify by checking the doorbell registers are clear.
1010 */
1011 start = ktime_get();
1012 do {
1013 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1014 ret = -EBUSY;
1015 goto out;
1016 }
1017
1018 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1019 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1020 if (!tm_doorbell && !tr_doorbell) {
1021 timeout = false;
1022 break;
1023 } else if (do_last_check) {
1024 break;
1025 }
1026
1027 spin_unlock_irqrestore(hba->host->host_lock, flags);
1028 schedule();
1029 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1030 wait_timeout_us) {
1031 timeout = true;
1032 /*
1033 * We might have scheduled out for long time so make
1034 * sure to check if doorbells are cleared by this time
1035 * or not.
1036 */
1037 do_last_check = true;
1038 }
1039 spin_lock_irqsave(hba->host->host_lock, flags);
1040 } while (tm_doorbell || tr_doorbell);
1041
1042 if (timeout) {
1043 dev_err(hba->dev,
1044 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1045 __func__, tm_doorbell, tr_doorbell);
1046 ret = -EBUSY;
1047 }
1048out:
1049 spin_unlock_irqrestore(hba->host->host_lock, flags);
1050 ufshcd_release(hba);
1051 return ret;
1052}
1053
1054/**
1055 * ufshcd_scale_gear - scale up/down UFS gear
1056 * @hba: per adapter instance
1057 * @scale_up: True for scaling up gear and false for scaling down
1058 *
1059 * Returns 0 for success,
1060 * Returns -EBUSY if scaling can't happen at this time
1061 * Returns non-zero for any other errors
1062 */
1063static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1064{
1065 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1066 int ret = 0;
1067 struct ufs_pa_layer_attr new_pwr_info;
1068
1069 if (scale_up) {
1070 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1071 sizeof(struct ufs_pa_layer_attr));
1072 } else {
1073 memcpy(&new_pwr_info, &hba->pwr_info,
1074 sizeof(struct ufs_pa_layer_attr));
1075
1076 if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN
1077 || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) {
1078 /* save the current power mode */
1079 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1080 &hba->pwr_info,
1081 sizeof(struct ufs_pa_layer_attr));
1082
1083 /* scale down gear */
1084 new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1085 new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1086 }
1087 }
1088
1089 /* check if the power mode needs to be changed or not? */
1090 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
1091
1092 if (ret)
1093 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1094 __func__, ret,
1095 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1096 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1097
1098 return ret;
1099}
1100
1101static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1102{
1103 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1104 int ret = 0;
1105 /*
1106 * make sure that there are no outstanding requests when
1107 * clock scaling is in progress
1108 */
1109 scsi_block_requests(hba->host);
1110 down_write(&hba->clk_scaling_lock);
1111 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1112 ret = -EBUSY;
1113 up_write(&hba->clk_scaling_lock);
1114 scsi_unblock_requests(hba->host);
1115 }
1116
1117 return ret;
1118}
1119
1120static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1121{
1122 up_write(&hba->clk_scaling_lock);
1123 scsi_unblock_requests(hba->host);
1124}
1125
1126/**
1127 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1128 * @hba: per adapter instance
1129 * @scale_up: True for scaling up and false for scalin down
1130 *
1131 * Returns 0 for success,
1132 * Returns -EBUSY if scaling can't happen at this time
1133 * Returns non-zero for any other errors
1134 */
1135static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1136{
1137 int ret = 0;
1138
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001139 /* let's not get into low power until clock scaling is completed */
1140 ufshcd_hold(hba, false);
1141
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001142 ret = ufshcd_clock_scaling_prepare(hba);
1143 if (ret)
1144 return ret;
1145
1146 /* scale down the gear before scaling down clocks */
1147 if (!scale_up) {
1148 ret = ufshcd_scale_gear(hba, false);
1149 if (ret)
1150 goto out;
1151 }
1152
1153 ret = ufshcd_scale_clks(hba, scale_up);
1154 if (ret) {
1155 if (!scale_up)
1156 ufshcd_scale_gear(hba, true);
1157 goto out;
1158 }
1159
1160 /* scale up the gear after scaling up clocks */
1161 if (scale_up) {
1162 ret = ufshcd_scale_gear(hba, true);
1163 if (ret) {
1164 ufshcd_scale_clks(hba, false);
1165 goto out;
1166 }
1167 }
1168
1169 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1170
1171out:
1172 ufshcd_clock_scaling_unprepare(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001173 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001174 return ret;
1175}
1176
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001177static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1178{
1179 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1180 clk_scaling.suspend_work);
1181 unsigned long irq_flags;
1182
1183 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1184 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1185 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1186 return;
1187 }
1188 hba->clk_scaling.is_suspended = true;
1189 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1190
1191 __ufshcd_suspend_clkscaling(hba);
1192}
1193
1194static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1195{
1196 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1197 clk_scaling.resume_work);
1198 unsigned long irq_flags;
1199
1200 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1201 if (!hba->clk_scaling.is_suspended) {
1202 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1203 return;
1204 }
1205 hba->clk_scaling.is_suspended = false;
1206 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1207
1208 devfreq_resume_device(hba->devfreq);
1209}
1210
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001211static int ufshcd_devfreq_target(struct device *dev,
1212 unsigned long *freq, u32 flags)
1213{
1214 int ret = 0;
1215 struct ufs_hba *hba = dev_get_drvdata(dev);
1216 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001217 bool scale_up, sched_clk_scaling_suspend_work = false;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001218 unsigned long irq_flags;
1219
1220 if (!ufshcd_is_clkscaling_supported(hba))
1221 return -EINVAL;
1222
1223 if ((*freq > 0) && (*freq < UINT_MAX)) {
1224 dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq);
1225 return -EINVAL;
1226 }
1227
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001228 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1229 if (ufshcd_eh_in_progress(hba)) {
1230 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1231 return 0;
1232 }
1233
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001234 if (!hba->clk_scaling.active_reqs)
1235 sched_clk_scaling_suspend_work = true;
1236
1237 scale_up = (*freq == UINT_MAX) ? true : false;
1238 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1239 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1240 ret = 0;
1241 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001242 }
1243 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1244
1245 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001246 ret = ufshcd_devfreq_scale(hba, scale_up);
1247
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001248 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1249 (scale_up ? "up" : "down"),
1250 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1251
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001252out:
1253 if (sched_clk_scaling_suspend_work)
1254 queue_work(hba->clk_scaling.workq,
1255 &hba->clk_scaling.suspend_work);
1256
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001257 return ret;
1258}
1259
1260
1261static int ufshcd_devfreq_get_dev_status(struct device *dev,
1262 struct devfreq_dev_status *stat)
1263{
1264 struct ufs_hba *hba = dev_get_drvdata(dev);
1265 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1266 unsigned long flags;
1267
1268 if (!ufshcd_is_clkscaling_supported(hba))
1269 return -EINVAL;
1270
1271 memset(stat, 0, sizeof(*stat));
1272
1273 spin_lock_irqsave(hba->host->host_lock, flags);
1274 if (!scaling->window_start_t)
1275 goto start_window;
1276
1277 if (scaling->is_busy_started)
1278 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1279 scaling->busy_start_t));
1280
1281 stat->total_time = jiffies_to_usecs((long)jiffies -
1282 (long)scaling->window_start_t);
1283 stat->busy_time = scaling->tot_busy_t;
1284start_window:
1285 scaling->window_start_t = jiffies;
1286 scaling->tot_busy_t = 0;
1287
1288 if (hba->outstanding_reqs) {
1289 scaling->busy_start_t = ktime_get();
1290 scaling->is_busy_started = true;
1291 } else {
1292 scaling->busy_start_t = 0;
1293 scaling->is_busy_started = false;
1294 }
1295 spin_unlock_irqrestore(hba->host->host_lock, flags);
1296 return 0;
1297}
1298
1299static struct devfreq_dev_profile ufs_devfreq_profile = {
1300 .polling_ms = 100,
1301 .target = ufshcd_devfreq_target,
1302 .get_dev_status = ufshcd_devfreq_get_dev_status,
1303};
1304
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001305static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1306{
1307 unsigned long flags;
1308
1309 devfreq_suspend_device(hba->devfreq);
1310 spin_lock_irqsave(hba->host->host_lock, flags);
1311 hba->clk_scaling.window_start_t = 0;
1312 spin_unlock_irqrestore(hba->host->host_lock, flags);
1313}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001314
Gilad Bronera5082532016-10-17 17:10:00 -07001315static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1316{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001317 unsigned long flags;
1318 bool suspend = false;
1319
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001320 if (!ufshcd_is_clkscaling_supported(hba))
1321 return;
1322
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001323 spin_lock_irqsave(hba->host->host_lock, flags);
1324 if (!hba->clk_scaling.is_suspended) {
1325 suspend = true;
1326 hba->clk_scaling.is_suspended = true;
1327 }
1328 spin_unlock_irqrestore(hba->host->host_lock, flags);
1329
1330 if (suspend)
1331 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001332}
1333
1334static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1335{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001336 unsigned long flags;
1337 bool resume = false;
1338
1339 if (!ufshcd_is_clkscaling_supported(hba))
1340 return;
1341
1342 spin_lock_irqsave(hba->host->host_lock, flags);
1343 if (hba->clk_scaling.is_suspended) {
1344 resume = true;
1345 hba->clk_scaling.is_suspended = false;
1346 }
1347 spin_unlock_irqrestore(hba->host->host_lock, flags);
1348
1349 if (resume)
1350 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001351}
1352
1353static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1354 struct device_attribute *attr, char *buf)
1355{
1356 struct ufs_hba *hba = dev_get_drvdata(dev);
1357
1358 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1359}
1360
1361static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1362 struct device_attribute *attr, const char *buf, size_t count)
1363{
1364 struct ufs_hba *hba = dev_get_drvdata(dev);
1365 u32 value;
1366 int err;
1367
1368 if (kstrtou32(buf, 0, &value))
1369 return -EINVAL;
1370
1371 value = !!value;
1372 if (value == hba->clk_scaling.is_allowed)
1373 goto out;
1374
1375 pm_runtime_get_sync(hba->dev);
1376 ufshcd_hold(hba, false);
1377
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001378 cancel_work_sync(&hba->clk_scaling.suspend_work);
1379 cancel_work_sync(&hba->clk_scaling.resume_work);
1380
1381 hba->clk_scaling.is_allowed = value;
1382
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001383 if (value) {
1384 ufshcd_resume_clkscaling(hba);
1385 } else {
1386 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001387 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001388 if (err)
1389 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1390 __func__, err);
1391 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001392
1393 ufshcd_release(hba);
1394 pm_runtime_put_sync(hba->dev);
1395out:
1396 return count;
Gilad Bronera5082532016-10-17 17:10:00 -07001397}
1398
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001399static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1400{
1401 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1402 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1403 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1404 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1405 hba->clk_scaling.enable_attr.attr.mode = 0644;
1406 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1407 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1408}
1409
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001410static void ufshcd_ungate_work(struct work_struct *work)
1411{
1412 int ret;
1413 unsigned long flags;
1414 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1415 clk_gating.ungate_work);
1416
1417 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1418
1419 spin_lock_irqsave(hba->host->host_lock, flags);
1420 if (hba->clk_gating.state == CLKS_ON) {
1421 spin_unlock_irqrestore(hba->host->host_lock, flags);
1422 goto unblock_reqs;
1423 }
1424
1425 spin_unlock_irqrestore(hba->host->host_lock, flags);
1426 ufshcd_setup_clocks(hba, true);
1427
1428 /* Exit from hibern8 */
1429 if (ufshcd_can_hibern8_during_gating(hba)) {
1430 /* Prevent gating in this path */
1431 hba->clk_gating.is_suspended = true;
1432 if (ufshcd_is_link_hibern8(hba)) {
1433 ret = ufshcd_uic_hibern8_exit(hba);
1434 if (ret)
1435 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1436 __func__, ret);
1437 else
1438 ufshcd_set_link_active(hba);
1439 }
1440 hba->clk_gating.is_suspended = false;
1441 }
1442unblock_reqs:
1443 scsi_unblock_requests(hba->host);
1444}
1445
1446/**
1447 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1448 * Also, exit from hibern8 mode and set the link as active.
1449 * @hba: per adapter instance
1450 * @async: This indicates whether caller should ungate clocks asynchronously.
1451 */
1452int ufshcd_hold(struct ufs_hba *hba, bool async)
1453{
1454 int rc = 0;
1455 unsigned long flags;
1456
1457 if (!ufshcd_is_clkgating_allowed(hba))
1458 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001459 spin_lock_irqsave(hba->host->host_lock, flags);
1460 hba->clk_gating.active_reqs++;
1461
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001462 if (ufshcd_eh_in_progress(hba)) {
1463 spin_unlock_irqrestore(hba->host->host_lock, flags);
1464 return 0;
1465 }
1466
Sahitya Tummala856b3482014-09-25 15:32:34 +03001467start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001468 switch (hba->clk_gating.state) {
1469 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001470 /*
1471 * Wait for the ungate work to complete if in progress.
1472 * Though the clocks may be in ON state, the link could
1473 * still be in hibner8 state if hibern8 is allowed
1474 * during clock gating.
1475 * Make sure we exit hibern8 state also in addition to
1476 * clocks being ON.
1477 */
1478 if (ufshcd_can_hibern8_during_gating(hba) &&
1479 ufshcd_is_link_hibern8(hba)) {
1480 spin_unlock_irqrestore(hba->host->host_lock, flags);
1481 flush_work(&hba->clk_gating.ungate_work);
1482 spin_lock_irqsave(hba->host->host_lock, flags);
1483 goto start;
1484 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001485 break;
1486 case REQ_CLKS_OFF:
1487 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1488 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001489 trace_ufshcd_clk_gating(dev_name(hba->dev),
1490 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001491 break;
1492 }
1493 /*
Tomohiro Kusumi9c490d22017-03-28 16:49:26 +03001494 * If we are here, it means gating work is either done or
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001495 * currently running. Hence, fall through to cancel gating
1496 * work and to enable clocks.
1497 */
1498 case CLKS_OFF:
1499 scsi_block_requests(hba->host);
1500 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001501 trace_ufshcd_clk_gating(dev_name(hba->dev),
1502 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001503 schedule_work(&hba->clk_gating.ungate_work);
1504 /*
1505 * fall through to check if we should wait for this
1506 * work to be done or not.
1507 */
1508 case REQ_CLKS_ON:
1509 if (async) {
1510 rc = -EAGAIN;
1511 hba->clk_gating.active_reqs--;
1512 break;
1513 }
1514
1515 spin_unlock_irqrestore(hba->host->host_lock, flags);
1516 flush_work(&hba->clk_gating.ungate_work);
1517 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001518 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001519 goto start;
1520 default:
1521 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1522 __func__, hba->clk_gating.state);
1523 break;
1524 }
1525 spin_unlock_irqrestore(hba->host->host_lock, flags);
1526out:
1527 return rc;
1528}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001529EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001530
1531static void ufshcd_gate_work(struct work_struct *work)
1532{
1533 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1534 clk_gating.gate_work.work);
1535 unsigned long flags;
1536
1537 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001538 /*
1539 * In case you are here to cancel this work the gating state
1540 * would be marked as REQ_CLKS_ON. In this case save time by
1541 * skipping the gating work and exit after changing the clock
1542 * state to CLKS_ON.
1543 */
1544 if (hba->clk_gating.is_suspended ||
1545 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001546 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001547 trace_ufshcd_clk_gating(dev_name(hba->dev),
1548 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001549 goto rel_lock;
1550 }
1551
1552 if (hba->clk_gating.active_reqs
1553 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1554 || hba->lrb_in_use || hba->outstanding_tasks
1555 || hba->active_uic_cmd || hba->uic_async_done)
1556 goto rel_lock;
1557
1558 spin_unlock_irqrestore(hba->host->host_lock, flags);
1559
1560 /* put the link into hibern8 mode before turning off clocks */
1561 if (ufshcd_can_hibern8_during_gating(hba)) {
1562 if (ufshcd_uic_hibern8_enter(hba)) {
1563 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001564 trace_ufshcd_clk_gating(dev_name(hba->dev),
1565 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001566 goto out;
1567 }
1568 ufshcd_set_link_hibern8(hba);
1569 }
1570
1571 if (!ufshcd_is_link_active(hba))
1572 ufshcd_setup_clocks(hba, false);
1573 else
1574 /* If link is active, device ref_clk can't be switched off */
1575 __ufshcd_setup_clocks(hba, false, true);
1576
1577 /*
1578 * In case you are here to cancel this work the gating state
1579 * would be marked as REQ_CLKS_ON. In this case keep the state
1580 * as REQ_CLKS_ON which would anyway imply that clocks are off
1581 * and a request to turn them on is pending. By doing this way,
1582 * we keep the state machine in tact and this would ultimately
1583 * prevent from doing cancel work multiple times when there are
1584 * new requests arriving before the current cancel work is done.
1585 */
1586 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001587 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001588 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001589 trace_ufshcd_clk_gating(dev_name(hba->dev),
1590 hba->clk_gating.state);
1591 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001592rel_lock:
1593 spin_unlock_irqrestore(hba->host->host_lock, flags);
1594out:
1595 return;
1596}
1597
1598/* host lock must be held before calling this variant */
1599static void __ufshcd_release(struct ufs_hba *hba)
1600{
1601 if (!ufshcd_is_clkgating_allowed(hba))
1602 return;
1603
1604 hba->clk_gating.active_reqs--;
1605
1606 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1607 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1608 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001609 || hba->active_uic_cmd || hba->uic_async_done
1610 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001611 return;
1612
1613 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001614 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001615 schedule_delayed_work(&hba->clk_gating.gate_work,
1616 msecs_to_jiffies(hba->clk_gating.delay_ms));
1617}
1618
1619void ufshcd_release(struct ufs_hba *hba)
1620{
1621 unsigned long flags;
1622
1623 spin_lock_irqsave(hba->host->host_lock, flags);
1624 __ufshcd_release(hba);
1625 spin_unlock_irqrestore(hba->host->host_lock, flags);
1626}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001627EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001628
1629static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1630 struct device_attribute *attr, char *buf)
1631{
1632 struct ufs_hba *hba = dev_get_drvdata(dev);
1633
1634 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1635}
1636
1637static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1638 struct device_attribute *attr, const char *buf, size_t count)
1639{
1640 struct ufs_hba *hba = dev_get_drvdata(dev);
1641 unsigned long flags, value;
1642
1643 if (kstrtoul(buf, 0, &value))
1644 return -EINVAL;
1645
1646 spin_lock_irqsave(hba->host->host_lock, flags);
1647 hba->clk_gating.delay_ms = value;
1648 spin_unlock_irqrestore(hba->host->host_lock, flags);
1649 return count;
1650}
1651
Sahitya Tummalab4274112016-12-22 18:40:39 -08001652static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1653 struct device_attribute *attr, char *buf)
1654{
1655 struct ufs_hba *hba = dev_get_drvdata(dev);
1656
1657 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1658}
1659
1660static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1661 struct device_attribute *attr, const char *buf, size_t count)
1662{
1663 struct ufs_hba *hba = dev_get_drvdata(dev);
1664 unsigned long flags;
1665 u32 value;
1666
1667 if (kstrtou32(buf, 0, &value))
1668 return -EINVAL;
1669
1670 value = !!value;
1671 if (value == hba->clk_gating.is_enabled)
1672 goto out;
1673
1674 if (value) {
1675 ufshcd_release(hba);
1676 } else {
1677 spin_lock_irqsave(hba->host->host_lock, flags);
1678 hba->clk_gating.active_reqs++;
1679 spin_unlock_irqrestore(hba->host->host_lock, flags);
1680 }
1681
1682 hba->clk_gating.is_enabled = value;
1683out:
1684 return count;
1685}
1686
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001687static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1688{
1689 if (!ufshcd_is_clkgating_allowed(hba))
1690 return;
1691
1692 hba->clk_gating.delay_ms = 150;
1693 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1694 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1695
Sahitya Tummalab4274112016-12-22 18:40:39 -08001696 hba->clk_gating.is_enabled = true;
1697
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001698 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1699 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1700 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1701 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001702 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001703 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1704 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001705
1706 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1707 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1708 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1709 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1710 hba->clk_gating.enable_attr.attr.mode = 0644;
1711 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1712 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001713}
1714
1715static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1716{
1717 if (!ufshcd_is_clkgating_allowed(hba))
1718 return;
1719 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001720 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001721 cancel_work_sync(&hba->clk_gating.ungate_work);
1722 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001723}
1724
Sahitya Tummala856b3482014-09-25 15:32:34 +03001725/* Must be called with host lock acquired */
1726static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1727{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001728 bool queue_resume_work = false;
1729
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001730 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001731 return;
1732
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001733 if (!hba->clk_scaling.active_reqs++)
1734 queue_resume_work = true;
1735
1736 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1737 return;
1738
1739 if (queue_resume_work)
1740 queue_work(hba->clk_scaling.workq,
1741 &hba->clk_scaling.resume_work);
1742
1743 if (!hba->clk_scaling.window_start_t) {
1744 hba->clk_scaling.window_start_t = jiffies;
1745 hba->clk_scaling.tot_busy_t = 0;
1746 hba->clk_scaling.is_busy_started = false;
1747 }
1748
Sahitya Tummala856b3482014-09-25 15:32:34 +03001749 if (!hba->clk_scaling.is_busy_started) {
1750 hba->clk_scaling.busy_start_t = ktime_get();
1751 hba->clk_scaling.is_busy_started = true;
1752 }
1753}
1754
1755static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1756{
1757 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1758
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001759 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001760 return;
1761
1762 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1763 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1764 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001765 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001766 scaling->is_busy_started = false;
1767 }
1768}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301769/**
1770 * ufshcd_send_command - Send SCSI or device management commands
1771 * @hba: per adapter instance
1772 * @task_tag: Task tag of the command
1773 */
1774static inline
1775void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1776{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08001777 hba->lrb[task_tag].issue_time_stamp = ktime_get();
Sahitya Tummala856b3482014-09-25 15:32:34 +03001778 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301779 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301780 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001781 /* Make sure that doorbell is committed immediately */
1782 wmb();
Lee Susman1a07f2d2016-12-22 18:42:03 -08001783 ufshcd_add_command_trace(hba, task_tag, "send");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301784}
1785
1786/**
1787 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1788 * @lrb - pointer to local reference block
1789 */
1790static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1791{
1792 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301793 if (lrbp->sense_buffer &&
1794 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001795 int len_to_copy;
1796
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301797 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001798 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
1799
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301800 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301801 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001802 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301803 }
1804}
1805
1806/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301807 * ufshcd_copy_query_response() - Copy the Query Response and the data
1808 * descriptor
1809 * @hba: per adapter instance
1810 * @lrb - pointer to local reference block
1811 */
1812static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001813int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301814{
1815 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1816
Dolev Raviv68078d52013-07-30 00:35:58 +05301817 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301818
Dolev Raviv68078d52013-07-30 00:35:58 +05301819 /* Get the descriptor */
1820 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001821 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301822 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001823 u16 resp_len;
1824 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301825
1826 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001827 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301828 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001829 buf_len = be16_to_cpu(
1830 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001831 if (likely(buf_len >= resp_len)) {
1832 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1833 } else {
1834 dev_warn(hba->dev,
1835 "%s: Response size is bigger than buffer",
1836 __func__);
1837 return -EINVAL;
1838 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301839 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001840
1841 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301842}
1843
1844/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301845 * ufshcd_hba_capabilities - Read controller capabilities
1846 * @hba: per adapter instance
1847 */
1848static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
1849{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301850 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301851
1852 /* nutrs and nutmrs are 0 based values */
1853 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
1854 hba->nutmrs =
1855 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
1856}
1857
1858/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301859 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1860 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301861 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301862 * Return true on success, else false
1863 */
1864static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
1865{
1866 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1867 return true;
1868 else
1869 return false;
1870}
1871
1872/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301873 * ufshcd_get_upmcrs - Get the power mode change request status
1874 * @hba: Pointer to adapter instance
1875 *
1876 * This function gets the UPMCRS field of HCS register
1877 * Returns value of UPMCRS field
1878 */
1879static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1880{
1881 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1882}
1883
1884/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301885 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1886 * @hba: per adapter instance
1887 * @uic_cmd: UIC command
1888 *
1889 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301890 */
1891static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301892ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301893{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301894 WARN_ON(hba->active_uic_cmd);
1895
1896 hba->active_uic_cmd = uic_cmd;
1897
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301898 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301899 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1900 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1901 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301902
1903 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301904 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301905 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301906}
1907
1908/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301909 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1910 * @hba: per adapter instance
1911 * @uic_command: UIC command
1912 *
1913 * Must be called with mutex held.
1914 * Returns 0 only if success.
1915 */
1916static int
1917ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1918{
1919 int ret;
1920 unsigned long flags;
1921
1922 if (wait_for_completion_timeout(&uic_cmd->done,
1923 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1924 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1925 else
1926 ret = -ETIMEDOUT;
1927
1928 spin_lock_irqsave(hba->host->host_lock, flags);
1929 hba->active_uic_cmd = NULL;
1930 spin_unlock_irqrestore(hba->host->host_lock, flags);
1931
1932 return ret;
1933}
1934
1935/**
1936 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1937 * @hba: per adapter instance
1938 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001939 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301940 *
1941 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001942 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301943 * Returns 0 only if success.
1944 */
1945static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001946__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1947 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301948{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301949 if (!ufshcd_ready_for_uic_cmd(hba)) {
1950 dev_err(hba->dev,
1951 "Controller not ready to accept UIC commands\n");
1952 return -EIO;
1953 }
1954
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001955 if (completion)
1956 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301957
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301958 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301959
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001960 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301961}
1962
1963/**
1964 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1965 * @hba: per adapter instance
1966 * @uic_cmd: UIC command
1967 *
1968 * Returns 0 only if success.
1969 */
1970static int
1971ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1972{
1973 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001974 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301975
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001976 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301977 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001978 ufshcd_add_delay_before_dme_cmd(hba);
1979
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001980 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001981 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001982 spin_unlock_irqrestore(hba->host->host_lock, flags);
1983 if (!ret)
1984 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1985
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301986 mutex_unlock(&hba->uic_cmd_mutex);
1987
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001988 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301989 return ret;
1990}
1991
1992/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301993 * ufshcd_map_sg - Map scatter-gather list to prdt
1994 * @lrbp - pointer to local reference block
1995 *
1996 * Returns 0 in case of success, non-zero value in case of failure
1997 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001998static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301999{
2000 struct ufshcd_sg_entry *prd_table;
2001 struct scatterlist *sg;
2002 struct scsi_cmnd *cmd;
2003 int sg_segments;
2004 int i;
2005
2006 cmd = lrbp->cmd;
2007 sg_segments = scsi_dma_map(cmd);
2008 if (sg_segments < 0)
2009 return sg_segments;
2010
2011 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002012 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2013 lrbp->utr_descriptor_ptr->prd_table_length =
2014 cpu_to_le16((u16)(sg_segments *
2015 sizeof(struct ufshcd_sg_entry)));
2016 else
2017 lrbp->utr_descriptor_ptr->prd_table_length =
2018 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302019
2020 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2021
2022 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2023 prd_table[i].size =
2024 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2025 prd_table[i].base_addr =
2026 cpu_to_le32(lower_32_bits(sg->dma_address));
2027 prd_table[i].upper_addr =
2028 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002029 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302030 }
2031 } else {
2032 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2033 }
2034
2035 return 0;
2036}
2037
2038/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302039 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302040 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302041 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302042 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302043static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302044{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302045 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2046
2047 if (hba->ufs_version == UFSHCI_VERSION_10) {
2048 u32 rw;
2049 rw = set & INTERRUPT_MASK_RW_VER_10;
2050 set = rw | ((set ^ intrs) & intrs);
2051 } else {
2052 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302053 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302054
2055 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2056}
2057
2058/**
2059 * ufshcd_disable_intr - disable interrupts
2060 * @hba: per adapter instance
2061 * @intrs: interrupt bits
2062 */
2063static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2064{
2065 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2066
2067 if (hba->ufs_version == UFSHCI_VERSION_10) {
2068 u32 rw;
2069 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2070 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2071 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2072
2073 } else {
2074 set &= ~intrs;
2075 }
2076
2077 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302078}
2079
2080/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302081 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2082 * descriptor according to request
2083 * @lrbp: pointer to local reference block
2084 * @upiu_flags: flags required in the header
2085 * @cmd_dir: requests data direction
2086 */
2087static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01002088 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302089{
2090 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2091 u32 data_direction;
2092 u32 dword_0;
2093
2094 if (cmd_dir == DMA_FROM_DEVICE) {
2095 data_direction = UTP_DEVICE_TO_HOST;
2096 *upiu_flags = UPIU_CMD_FLAGS_READ;
2097 } else if (cmd_dir == DMA_TO_DEVICE) {
2098 data_direction = UTP_HOST_TO_DEVICE;
2099 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2100 } else {
2101 data_direction = UTP_NO_DATA_TRANSFER;
2102 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2103 }
2104
2105 dword_0 = data_direction | (lrbp->command_type
2106 << UPIU_COMMAND_TYPE_OFFSET);
2107 if (lrbp->intr_cmd)
2108 dword_0 |= UTP_REQ_DESC_INT_CMD;
2109
2110 /* Transfer request descriptor header fields */
2111 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002112 /* dword_1 is reserved, hence it is set to 0 */
2113 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302114 /*
2115 * assigning invalid value for command status. Controller
2116 * updates OCS on command completion, with the command
2117 * status
2118 */
2119 req_desc->header.dword_2 =
2120 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002121 /* dword_3 is reserved, hence it is set to 0 */
2122 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002123
2124 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302125}
2126
2127/**
2128 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2129 * for scsi commands
2130 * @lrbp - local reference block pointer
2131 * @upiu_flags - flags
2132 */
2133static
2134void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2135{
2136 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002137 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302138
2139 /* command descriptor fields */
2140 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2141 UPIU_TRANSACTION_COMMAND, upiu_flags,
2142 lrbp->lun, lrbp->task_tag);
2143 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2144 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2145
2146 /* Total EHS length and Data segment length will be zero */
2147 ucd_req_ptr->header.dword_2 = 0;
2148
2149 ucd_req_ptr->sc.exp_data_transfer_len =
2150 cpu_to_be32(lrbp->cmd->sdb.length);
2151
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002152 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
2153 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
2154 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
2155
2156 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302157}
2158
Dolev Raviv68078d52013-07-30 00:35:58 +05302159/**
2160 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2161 * for query requsts
2162 * @hba: UFS hba
2163 * @lrbp: local reference block pointer
2164 * @upiu_flags: flags
2165 */
2166static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2167 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2168{
2169 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2170 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302171 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302172 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
2173
2174 /* Query request header */
2175 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2176 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2177 lrbp->lun, lrbp->task_tag);
2178 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2179 0, query->request.query_func, 0, 0);
2180
Zang Leigang68612852016-08-25 17:39:19 +08002181 /* Data segment length only need for WRITE_DESC */
2182 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2183 ucd_req_ptr->header.dword_2 =
2184 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2185 else
2186 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302187
2188 /* Copy the Query Request buffer as is */
2189 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2190 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302191
2192 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002193 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2194 memcpy(descp, query->descriptor, len);
2195
Yaniv Gardi51047262016-02-01 15:02:38 +02002196 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302197}
2198
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302199static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2200{
2201 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2202
2203 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2204
2205 /* command descriptor fields */
2206 ucd_req_ptr->header.dword_0 =
2207 UPIU_HEADER_DWORD(
2208 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002209 /* clear rest of the fields of basic header */
2210 ucd_req_ptr->header.dword_1 = 0;
2211 ucd_req_ptr->header.dword_2 = 0;
2212
2213 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302214}
2215
2216/**
Joao Pinto300bb132016-05-11 12:21:27 +01002217 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2218 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302219 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302220 * @lrb - pointer to local reference block
2221 */
Joao Pinto300bb132016-05-11 12:21:27 +01002222static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302223{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302224 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302225 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302226
Joao Pinto300bb132016-05-11 12:21:27 +01002227 if (hba->ufs_version == UFSHCI_VERSION_20)
2228 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2229 else
2230 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
2231
2232 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2233 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2234 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2235 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2236 ufshcd_prepare_utp_nop_upiu(lrbp);
2237 else
2238 ret = -EINVAL;
2239
2240 return ret;
2241}
2242
2243/**
2244 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2245 * for SCSI Purposes
2246 * @hba - per adapter instance
2247 * @lrb - pointer to local reference block
2248 */
2249static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2250{
2251 u32 upiu_flags;
2252 int ret = 0;
2253
2254 if (hba->ufs_version == UFSHCI_VERSION_20)
2255 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2256 else
2257 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2258
2259 if (likely(lrbp->cmd)) {
2260 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2261 lrbp->cmd->sc_data_direction);
2262 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2263 } else {
2264 ret = -EINVAL;
2265 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302266
2267 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302268}
2269
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002270/*
2271 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2272 * @scsi_lun: scsi LUN id
2273 *
2274 * Returns UPIU LUN id
2275 */
2276static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
2277{
2278 if (scsi_is_wlun(scsi_lun))
2279 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
2280 | UFS_UPIU_WLUN_ID;
2281 else
2282 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
2283}
2284
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302285/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002286 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2287 * @scsi_lun: UPIU W-LUN id
2288 *
2289 * Returns SCSI W-LUN id
2290 */
2291static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2292{
2293 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2294}
2295
2296/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302297 * ufshcd_queuecommand - main entry point for SCSI requests
2298 * @cmd: command from SCSI Midlayer
2299 * @done: call back function
2300 *
2301 * Returns 0 for success, non-zero in case of failure
2302 */
2303static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2304{
2305 struct ufshcd_lrb *lrbp;
2306 struct ufs_hba *hba;
2307 unsigned long flags;
2308 int tag;
2309 int err = 0;
2310
2311 hba = shost_priv(host);
2312
2313 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002314 if (!ufshcd_valid_tag(hba, tag)) {
2315 dev_err(hba->dev,
2316 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2317 __func__, tag, cmd, cmd->request);
2318 BUG();
2319 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302320
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002321 if (!down_read_trylock(&hba->clk_scaling_lock))
2322 return SCSI_MLQUEUE_HOST_BUSY;
2323
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302324 spin_lock_irqsave(hba->host->host_lock, flags);
2325 switch (hba->ufshcd_state) {
2326 case UFSHCD_STATE_OPERATIONAL:
2327 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002328 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302329 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302330 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302331 goto out_unlock;
2332 case UFSHCD_STATE_ERROR:
2333 set_host_byte(cmd, DID_ERROR);
2334 cmd->scsi_done(cmd);
2335 goto out_unlock;
2336 default:
2337 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2338 __func__, hba->ufshcd_state);
2339 set_host_byte(cmd, DID_BAD_TARGET);
2340 cmd->scsi_done(cmd);
2341 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302342 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002343
2344 /* if error handling is in progress, don't issue commands */
2345 if (ufshcd_eh_in_progress(hba)) {
2346 set_host_byte(cmd, DID_ERROR);
2347 cmd->scsi_done(cmd);
2348 goto out_unlock;
2349 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302350 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302351
Gilad Broner7fabb772017-02-03 16:56:50 -08002352 hba->req_abort_count = 0;
2353
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302354 /* acquire the tag to make sure device cmds don't use it */
2355 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2356 /*
2357 * Dev manage command in progress, requeue the command.
2358 * Requeuing the command helps in cases where the request *may*
2359 * find different tag instead of waiting for dev manage command
2360 * completion.
2361 */
2362 err = SCSI_MLQUEUE_HOST_BUSY;
2363 goto out;
2364 }
2365
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002366 err = ufshcd_hold(hba, true);
2367 if (err) {
2368 err = SCSI_MLQUEUE_HOST_BUSY;
2369 clear_bit_unlock(tag, &hba->lrb_in_use);
2370 goto out;
2371 }
2372 WARN_ON(hba->clk_gating.state != CLKS_ON);
2373
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302374 lrbp = &hba->lrb[tag];
2375
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302376 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302377 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07002378 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302379 lrbp->sense_buffer = cmd->sense_buffer;
2380 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002381 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002382 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Gilad Bronere0b299e2017-02-03 16:56:40 -08002383 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302384
Joao Pinto300bb132016-05-11 12:21:27 +01002385 ufshcd_comp_scsi_upiu(hba, lrbp);
2386
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002387 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302388 if (err) {
2389 lrbp->cmd = NULL;
2390 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302391 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302392 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002393 /* Make sure descriptors are ready before ringing the doorbell */
2394 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302395
2396 /* issue command to the controller */
2397 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002398 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302399 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302400out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302401 spin_unlock_irqrestore(hba->host->host_lock, flags);
2402out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002403 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302404 return err;
2405}
2406
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302407static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2408 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2409{
2410 lrbp->cmd = NULL;
2411 lrbp->sense_bufflen = 0;
2412 lrbp->sense_buffer = NULL;
2413 lrbp->task_tag = tag;
2414 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302415 lrbp->intr_cmd = true; /* No interrupt aggregation */
2416 hba->dev_cmd.type = cmd_type;
2417
Joao Pinto300bb132016-05-11 12:21:27 +01002418 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302419}
2420
2421static int
2422ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2423{
2424 int err = 0;
2425 unsigned long flags;
2426 u32 mask = 1 << tag;
2427
2428 /* clear outstanding transaction before retry */
2429 spin_lock_irqsave(hba->host->host_lock, flags);
2430 ufshcd_utrl_clear(hba, tag);
2431 spin_unlock_irqrestore(hba->host->host_lock, flags);
2432
2433 /*
2434 * wait for for h/w to clear corresponding bit in door-bell.
2435 * max. wait is 1 sec.
2436 */
2437 err = ufshcd_wait_for_register(hba,
2438 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002439 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302440
2441 return err;
2442}
2443
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002444static int
2445ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2446{
2447 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2448
2449 /* Get the UPIU response */
2450 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2451 UPIU_RSP_CODE_OFFSET;
2452 return query_res->response;
2453}
2454
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302455/**
2456 * ufshcd_dev_cmd_completion() - handles device management command responses
2457 * @hba: per adapter instance
2458 * @lrbp: pointer to local reference block
2459 */
2460static int
2461ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2462{
2463 int resp;
2464 int err = 0;
2465
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002466 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302467 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2468
2469 switch (resp) {
2470 case UPIU_TRANSACTION_NOP_IN:
2471 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2472 err = -EINVAL;
2473 dev_err(hba->dev, "%s: unexpected response %x\n",
2474 __func__, resp);
2475 }
2476 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302477 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002478 err = ufshcd_check_query_response(hba, lrbp);
2479 if (!err)
2480 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302481 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302482 case UPIU_TRANSACTION_REJECT_UPIU:
2483 /* TODO: handle Reject UPIU Response */
2484 err = -EPERM;
2485 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2486 __func__);
2487 break;
2488 default:
2489 err = -EINVAL;
2490 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2491 __func__, resp);
2492 break;
2493 }
2494
2495 return err;
2496}
2497
2498static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2499 struct ufshcd_lrb *lrbp, int max_timeout)
2500{
2501 int err = 0;
2502 unsigned long time_left;
2503 unsigned long flags;
2504
2505 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2506 msecs_to_jiffies(max_timeout));
2507
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002508 /* Make sure descriptors are ready before ringing the doorbell */
2509 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302510 spin_lock_irqsave(hba->host->host_lock, flags);
2511 hba->dev_cmd.complete = NULL;
2512 if (likely(time_left)) {
2513 err = ufshcd_get_tr_ocs(lrbp);
2514 if (!err)
2515 err = ufshcd_dev_cmd_completion(hba, lrbp);
2516 }
2517 spin_unlock_irqrestore(hba->host->host_lock, flags);
2518
2519 if (!time_left) {
2520 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002521 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2522 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302523 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002524 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302525 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002526 /*
2527 * in case of an error, after clearing the doorbell,
2528 * we also need to clear the outstanding_request
2529 * field in hba
2530 */
2531 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302532 }
2533
2534 return err;
2535}
2536
2537/**
2538 * ufshcd_get_dev_cmd_tag - Get device management command tag
2539 * @hba: per-adapter instance
2540 * @tag: pointer to variable with available slot value
2541 *
2542 * Get a free slot and lock it until device management command
2543 * completes.
2544 *
2545 * Returns false if free slot is unavailable for locking, else
2546 * return true with tag value in @tag.
2547 */
2548static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
2549{
2550 int tag;
2551 bool ret = false;
2552 unsigned long tmp;
2553
2554 if (!tag_out)
2555 goto out;
2556
2557 do {
2558 tmp = ~hba->lrb_in_use;
2559 tag = find_last_bit(&tmp, hba->nutrs);
2560 if (tag >= hba->nutrs)
2561 goto out;
2562 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
2563
2564 *tag_out = tag;
2565 ret = true;
2566out:
2567 return ret;
2568}
2569
2570static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
2571{
2572 clear_bit_unlock(tag, &hba->lrb_in_use);
2573}
2574
2575/**
2576 * ufshcd_exec_dev_cmd - API for sending device management requests
2577 * @hba - UFS hba
2578 * @cmd_type - specifies the type (NOP, Query...)
2579 * @timeout - time in seconds
2580 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302581 * NOTE: Since there is only one available tag for device management commands,
2582 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302583 */
2584static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2585 enum dev_cmd_type cmd_type, int timeout)
2586{
2587 struct ufshcd_lrb *lrbp;
2588 int err;
2589 int tag;
2590 struct completion wait;
2591 unsigned long flags;
2592
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002593 down_read(&hba->clk_scaling_lock);
2594
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302595 /*
2596 * Get free slot, sleep if slots are unavailable.
2597 * Even though we use wait_event() which sleeps indefinitely,
2598 * the maximum wait time is bounded by SCSI request timeout.
2599 */
2600 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
2601
2602 init_completion(&wait);
2603 lrbp = &hba->lrb[tag];
2604 WARN_ON(lrbp->cmd);
2605 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2606 if (unlikely(err))
2607 goto out_put_tag;
2608
2609 hba->dev_cmd.complete = &wait;
2610
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002611 /* Make sure descriptors are ready before ringing the doorbell */
2612 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302613 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002614 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302615 ufshcd_send_command(hba, tag);
2616 spin_unlock_irqrestore(hba->host->host_lock, flags);
2617
2618 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2619
2620out_put_tag:
2621 ufshcd_put_dev_cmd_tag(hba, tag);
2622 wake_up(&hba->dev_cmd.tag_wq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002623 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302624 return err;
2625}
2626
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302627/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002628 * ufshcd_init_query() - init the query response and request parameters
2629 * @hba: per-adapter instance
2630 * @request: address of the request pointer to be initialized
2631 * @response: address of the response pointer to be initialized
2632 * @opcode: operation to perform
2633 * @idn: flag idn to access
2634 * @index: LU number to access
2635 * @selector: query/flag/descriptor further identification
2636 */
2637static inline void ufshcd_init_query(struct ufs_hba *hba,
2638 struct ufs_query_req **request, struct ufs_query_res **response,
2639 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2640{
2641 *request = &hba->dev_cmd.query.request;
2642 *response = &hba->dev_cmd.query.response;
2643 memset(*request, 0, sizeof(struct ufs_query_req));
2644 memset(*response, 0, sizeof(struct ufs_query_res));
2645 (*request)->upiu_req.opcode = opcode;
2646 (*request)->upiu_req.idn = idn;
2647 (*request)->upiu_req.index = index;
2648 (*request)->upiu_req.selector = selector;
2649}
2650
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002651static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2652 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2653{
2654 int ret;
2655 int retries;
2656
2657 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2658 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
2659 if (ret)
2660 dev_dbg(hba->dev,
2661 "%s: failed with error %d, retries %d\n",
2662 __func__, ret, retries);
2663 else
2664 break;
2665 }
2666
2667 if (ret)
2668 dev_err(hba->dev,
2669 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2670 __func__, opcode, idn, ret, retries);
2671 return ret;
2672}
2673
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002674/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302675 * ufshcd_query_flag() - API function for sending flag query requests
2676 * hba: per-adapter instance
2677 * query_opcode: flag query to perform
2678 * idn: flag idn to access
2679 * flag_res: the flag value after the query request completes
2680 *
2681 * Returns 0 for success, non-zero in case of failure
2682 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002683int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05302684 enum flag_idn idn, bool *flag_res)
2685{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002686 struct ufs_query_req *request = NULL;
2687 struct ufs_query_res *response = NULL;
2688 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002689 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302690
2691 BUG_ON(!hba);
2692
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002693 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302694 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002695 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2696 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302697
2698 switch (opcode) {
2699 case UPIU_QUERY_OPCODE_SET_FLAG:
2700 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2701 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2702 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2703 break;
2704 case UPIU_QUERY_OPCODE_READ_FLAG:
2705 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2706 if (!flag_res) {
2707 /* No dummy reads */
2708 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2709 __func__);
2710 err = -EINVAL;
2711 goto out_unlock;
2712 }
2713 break;
2714 default:
2715 dev_err(hba->dev,
2716 "%s: Expected query flag opcode but got = %d\n",
2717 __func__, opcode);
2718 err = -EINVAL;
2719 goto out_unlock;
2720 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302721
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002722 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302723
2724 if (err) {
2725 dev_err(hba->dev,
2726 "%s: Sending flag query for idn %d failed, err = %d\n",
2727 __func__, idn, err);
2728 goto out_unlock;
2729 }
2730
2731 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302732 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302733 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2734
2735out_unlock:
2736 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002737 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302738 return err;
2739}
2740
2741/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302742 * ufshcd_query_attr - API function for sending attribute requests
2743 * hba: per-adapter instance
2744 * opcode: attribute opcode
2745 * idn: attribute idn to access
2746 * index: index field
2747 * selector: selector field
2748 * attr_val: the attribute value after the query request completes
2749 *
2750 * Returns 0 for success, non-zero in case of failure
2751*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302752static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302753 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
2754{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002755 struct ufs_query_req *request = NULL;
2756 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302757 int err;
2758
2759 BUG_ON(!hba);
2760
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002761 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302762 if (!attr_val) {
2763 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2764 __func__, opcode);
2765 err = -EINVAL;
2766 goto out;
2767 }
2768
2769 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002770 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2771 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302772
2773 switch (opcode) {
2774 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2775 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302776 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302777 break;
2778 case UPIU_QUERY_OPCODE_READ_ATTR:
2779 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2780 break;
2781 default:
2782 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2783 __func__, opcode);
2784 err = -EINVAL;
2785 goto out_unlock;
2786 }
2787
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002788 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302789
2790 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002791 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2792 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302793 goto out_unlock;
2794 }
2795
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302796 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302797
2798out_unlock:
2799 mutex_unlock(&hba->dev_cmd.lock);
2800out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002801 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302802 return err;
2803}
2804
2805/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002806 * ufshcd_query_attr_retry() - API function for sending query
2807 * attribute with retries
2808 * @hba: per-adapter instance
2809 * @opcode: attribute opcode
2810 * @idn: attribute idn to access
2811 * @index: index field
2812 * @selector: selector field
2813 * @attr_val: the attribute value after the query request
2814 * completes
2815 *
2816 * Returns 0 for success, non-zero in case of failure
2817*/
2818static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2819 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2820 u32 *attr_val)
2821{
2822 int ret = 0;
2823 u32 retries;
2824
2825 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2826 ret = ufshcd_query_attr(hba, opcode, idn, index,
2827 selector, attr_val);
2828 if (ret)
2829 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2830 __func__, ret, retries);
2831 else
2832 break;
2833 }
2834
2835 if (ret)
2836 dev_err(hba->dev,
2837 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2838 __func__, idn, ret, QUERY_REQ_RETRIES);
2839 return ret;
2840}
2841
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002842static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002843 enum query_opcode opcode, enum desc_idn idn, u8 index,
2844 u8 selector, u8 *desc_buf, int *buf_len)
2845{
2846 struct ufs_query_req *request = NULL;
2847 struct ufs_query_res *response = NULL;
2848 int err;
2849
2850 BUG_ON(!hba);
2851
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002852 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002853 if (!desc_buf) {
2854 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2855 __func__, opcode);
2856 err = -EINVAL;
2857 goto out;
2858 }
2859
2860 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
2861 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2862 __func__, *buf_len);
2863 err = -EINVAL;
2864 goto out;
2865 }
2866
2867 mutex_lock(&hba->dev_cmd.lock);
2868 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2869 selector);
2870 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002871 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002872
2873 switch (opcode) {
2874 case UPIU_QUERY_OPCODE_WRITE_DESC:
2875 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2876 break;
2877 case UPIU_QUERY_OPCODE_READ_DESC:
2878 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2879 break;
2880 default:
2881 dev_err(hba->dev,
2882 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2883 __func__, opcode);
2884 err = -EINVAL;
2885 goto out_unlock;
2886 }
2887
2888 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2889
2890 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002891 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2892 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002893 goto out_unlock;
2894 }
2895
2896 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002897 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002898
2899out_unlock:
2900 mutex_unlock(&hba->dev_cmd.lock);
2901out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002902 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002903 return err;
2904}
2905
2906/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002907 * ufshcd_query_descriptor_retry - API function for sending descriptor
2908 * requests
2909 * hba: per-adapter instance
2910 * opcode: attribute opcode
2911 * idn: attribute idn to access
2912 * index: index field
2913 * selector: selector field
2914 * desc_buf: the buffer that contains the descriptor
2915 * buf_len: length parameter passed to the device
2916 *
2917 * Returns 0 for success, non-zero in case of failure.
2918 * The buf_len parameter will contain, on return, the length parameter
2919 * received on the response.
2920 */
Tomas Winkler26cf9152017-01-05 10:45:09 +02002921static int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2922 enum query_opcode opcode,
2923 enum desc_idn idn, u8 index,
2924 u8 selector,
2925 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002926{
2927 int err;
2928 int retries;
2929
2930 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2931 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2932 selector, desc_buf, buf_len);
2933 if (!err || err == -EINVAL)
2934 break;
2935 }
2936
2937 return err;
2938}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002939
2940/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002941 * ufshcd_read_desc_param - read the specified descriptor parameter
2942 * @hba: Pointer to adapter instance
2943 * @desc_id: descriptor idn value
2944 * @desc_index: descriptor index
2945 * @param_offset: offset of the parameter to read
2946 * @param_read_buf: pointer to buffer where parameter would be read
2947 * @param_size: sizeof(param_read_buf)
2948 *
2949 * Return 0 in case of success, non-zero otherwise
2950 */
2951static int ufshcd_read_desc_param(struct ufs_hba *hba,
2952 enum desc_idn desc_id,
2953 int desc_index,
2954 u32 param_offset,
2955 u8 *param_read_buf,
2956 u32 param_size)
2957{
2958 int ret;
2959 u8 *desc_buf;
2960 u32 buff_len;
2961 bool is_kmalloc = true;
2962
2963 /* safety checks */
2964 if (desc_id >= QUERY_DESC_IDN_MAX)
2965 return -EINVAL;
2966
2967 buff_len = ufs_query_desc_max_size[desc_id];
2968 if ((param_offset + param_size) > buff_len)
2969 return -EINVAL;
2970
2971 if (!param_offset && (param_size == buff_len)) {
2972 /* memory space already available to hold full descriptor */
2973 desc_buf = param_read_buf;
2974 is_kmalloc = false;
2975 } else {
2976 /* allocate memory to hold full descriptor */
2977 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2978 if (!desc_buf)
2979 return -ENOMEM;
2980 }
2981
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002982 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2983 desc_id, desc_index, 0, desc_buf,
2984 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002985
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002986 if (ret) {
2987 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2988 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002989
2990 goto out;
2991 }
2992
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002993 /* Sanity check */
2994 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2995 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2996 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2997 ret = -EINVAL;
2998 goto out;
2999 }
3000
3001 /*
3002 * While reading variable size descriptors (like string descriptor),
3003 * some UFS devices may report the "LENGTH" (field in "Transaction
3004 * Specific fields" of Query Response UPIU) same as what was requested
3005 * in Query Request UPIU instead of reporting the actual size of the
3006 * variable size descriptor.
3007 * Although it's safe to ignore the "LENGTH" field for variable size
3008 * descriptors as we can always derive the length of the descriptor from
3009 * the descriptor header fields. Hence this change impose the length
3010 * match check only for fixed size descriptors (for which we always
3011 * request the correct size as part of Query Request UPIU).
3012 */
3013 if ((desc_id != QUERY_DESC_IDN_STRING) &&
3014 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
3015 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
3016 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
3017 ret = -EINVAL;
3018 goto out;
3019 }
3020
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003021 if (is_kmalloc)
3022 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3023out:
3024 if (is_kmalloc)
3025 kfree(desc_buf);
3026 return ret;
3027}
3028
3029static inline int ufshcd_read_desc(struct ufs_hba *hba,
3030 enum desc_idn desc_id,
3031 int desc_index,
3032 u8 *buf,
3033 u32 size)
3034{
3035 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3036}
3037
3038static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3039 u8 *buf,
3040 u32 size)
3041{
Dolev Raviv61e07352016-11-23 16:30:49 -08003042 int err = 0;
3043 int retries;
3044
3045 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3046 /* Read descriptor*/
3047 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
3048 if (!err)
3049 break;
3050 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3051 }
3052
3053 return err;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003054}
3055
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003056static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
Yaniv Gardib573d482016-03-10 17:37:09 +02003057{
3058 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3059}
Yaniv Gardib573d482016-03-10 17:37:09 +02003060
3061/**
3062 * ufshcd_read_string_desc - read string descriptor
3063 * @hba: pointer to adapter instance
3064 * @desc_index: descriptor index
3065 * @buf: pointer to buffer where descriptor would be read
3066 * @size: size of buf
3067 * @ascii: if true convert from unicode to ascii characters
3068 *
3069 * Return 0 in case of success, non-zero otherwise
3070 */
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003071#define ASCII_STD true
3072static int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
3073 u8 *buf, u32 size, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003074{
3075 int err = 0;
3076
3077 err = ufshcd_read_desc(hba,
3078 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3079
3080 if (err) {
3081 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3082 __func__, QUERY_REQ_RETRIES, err);
3083 goto out;
3084 }
3085
3086 if (ascii) {
3087 int desc_len;
3088 int ascii_len;
3089 int i;
3090 char *buff_ascii;
3091
3092 desc_len = buf[0];
3093 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3094 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3095 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3096 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3097 __func__);
3098 err = -ENOMEM;
3099 goto out;
3100 }
3101
3102 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3103 if (!buff_ascii) {
3104 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003105 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003106 }
3107
3108 /*
3109 * the descriptor contains string in UTF16 format
3110 * we need to convert to utf-8 so it can be displayed
3111 */
3112 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3113 desc_len - QUERY_DESC_HDR_SIZE,
3114 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3115
3116 /* replace non-printable or non-ASCII characters with spaces */
3117 for (i = 0; i < ascii_len; i++)
3118 ufshcd_remove_non_printable(&buff_ascii[i]);
3119
3120 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3121 size - QUERY_DESC_HDR_SIZE);
3122 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3123 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02003124 kfree(buff_ascii);
3125 }
3126out:
3127 return err;
3128}
Yaniv Gardib573d482016-03-10 17:37:09 +02003129
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003130/**
3131 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3132 * @hba: Pointer to adapter instance
3133 * @lun: lun id
3134 * @param_offset: offset of the parameter to read
3135 * @param_read_buf: pointer to buffer where parameter would be read
3136 * @param_size: sizeof(param_read_buf)
3137 *
3138 * Return 0 in case of success, non-zero otherwise
3139 */
3140static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3141 int lun,
3142 enum unit_desc_param param_offset,
3143 u8 *param_read_buf,
3144 u32 param_size)
3145{
3146 /*
3147 * Unit descriptors are only available for general purpose LUs (LUN id
3148 * from 0 to 7) and RPMB Well known LU.
3149 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003150 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003151 return -EOPNOTSUPP;
3152
3153 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3154 param_offset, param_read_buf, param_size);
3155}
3156
3157/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303158 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3159 * @hba: per adapter instance
3160 *
3161 * 1. Allocate DMA memory for Command Descriptor array
3162 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3163 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3164 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3165 * (UTMRDL)
3166 * 4. Allocate memory for local reference block(lrb).
3167 *
3168 * Returns 0 for success, non-zero in case of failure
3169 */
3170static int ufshcd_memory_alloc(struct ufs_hba *hba)
3171{
3172 size_t utmrdl_size, utrdl_size, ucdl_size;
3173
3174 /* Allocate memory for UTP command descriptors */
3175 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003176 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3177 ucdl_size,
3178 &hba->ucdl_dma_addr,
3179 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303180
3181 /*
3182 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3183 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3184 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3185 * be aligned to 128 bytes as well
3186 */
3187 if (!hba->ucdl_base_addr ||
3188 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303189 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303190 "Command Descriptor Memory allocation failed\n");
3191 goto out;
3192 }
3193
3194 /*
3195 * Allocate memory for UTP Transfer descriptors
3196 * UFSHCI requires 1024 byte alignment of UTRD
3197 */
3198 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003199 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3200 utrdl_size,
3201 &hba->utrdl_dma_addr,
3202 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303203 if (!hba->utrdl_base_addr ||
3204 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303205 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303206 "Transfer Descriptor Memory allocation failed\n");
3207 goto out;
3208 }
3209
3210 /*
3211 * Allocate memory for UTP Task Management descriptors
3212 * UFSHCI requires 1024 byte alignment of UTMRD
3213 */
3214 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003215 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3216 utmrdl_size,
3217 &hba->utmrdl_dma_addr,
3218 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303219 if (!hba->utmrdl_base_addr ||
3220 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303221 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303222 "Task Management Descriptor Memory allocation failed\n");
3223 goto out;
3224 }
3225
3226 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003227 hba->lrb = devm_kzalloc(hba->dev,
3228 hba->nutrs * sizeof(struct ufshcd_lrb),
3229 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303230 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303231 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303232 goto out;
3233 }
3234 return 0;
3235out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303236 return -ENOMEM;
3237}
3238
3239/**
3240 * ufshcd_host_memory_configure - configure local reference block with
3241 * memory offsets
3242 * @hba: per adapter instance
3243 *
3244 * Configure Host memory space
3245 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3246 * address.
3247 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3248 * and PRDT offset.
3249 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3250 * into local reference block.
3251 */
3252static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3253{
3254 struct utp_transfer_cmd_desc *cmd_descp;
3255 struct utp_transfer_req_desc *utrdlp;
3256 dma_addr_t cmd_desc_dma_addr;
3257 dma_addr_t cmd_desc_element_addr;
3258 u16 response_offset;
3259 u16 prdt_offset;
3260 int cmd_desc_size;
3261 int i;
3262
3263 utrdlp = hba->utrdl_base_addr;
3264 cmd_descp = hba->ucdl_base_addr;
3265
3266 response_offset =
3267 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3268 prdt_offset =
3269 offsetof(struct utp_transfer_cmd_desc, prd_table);
3270
3271 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3272 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3273
3274 for (i = 0; i < hba->nutrs; i++) {
3275 /* Configure UTRD with command descriptor base address */
3276 cmd_desc_element_addr =
3277 (cmd_desc_dma_addr + (cmd_desc_size * i));
3278 utrdlp[i].command_desc_base_addr_lo =
3279 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3280 utrdlp[i].command_desc_base_addr_hi =
3281 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3282
3283 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003284 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3285 utrdlp[i].response_upiu_offset =
3286 cpu_to_le16(response_offset);
3287 utrdlp[i].prd_table_offset =
3288 cpu_to_le16(prdt_offset);
3289 utrdlp[i].response_upiu_length =
3290 cpu_to_le16(ALIGNED_UPIU_SIZE);
3291 } else {
3292 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303293 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003294 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303295 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003296 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303297 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003298 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303299
3300 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003301 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3302 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303303 hba->lrb[i].ucd_req_ptr =
3304 (struct utp_upiu_req *)(cmd_descp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003305 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303306 hba->lrb[i].ucd_rsp_ptr =
3307 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003308 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3309 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303310 hba->lrb[i].ucd_prdt_ptr =
3311 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003312 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3313 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303314 }
3315}
3316
3317/**
3318 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3319 * @hba: per adapter instance
3320 *
3321 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3322 * in order to initialize the Unipro link startup procedure.
3323 * Once the Unipro links are up, the device connected to the controller
3324 * is detected.
3325 *
3326 * Returns 0 on success, non-zero value on failure
3327 */
3328static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3329{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303330 struct uic_command uic_cmd = {0};
3331 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303332
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303333 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3334
3335 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3336 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003337 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303338 "dme-link-startup: error code %d\n", ret);
3339 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303340}
3341
Yaniv Gardicad2e032015-03-31 17:37:14 +03003342static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3343{
3344 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3345 unsigned long min_sleep_time_us;
3346
3347 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3348 return;
3349
3350 /*
3351 * last_dme_cmd_tstamp will be 0 only for 1st call to
3352 * this function
3353 */
3354 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3355 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3356 } else {
3357 unsigned long delta =
3358 (unsigned long) ktime_to_us(
3359 ktime_sub(ktime_get(),
3360 hba->last_dme_cmd_tstamp));
3361
3362 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3363 min_sleep_time_us =
3364 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3365 else
3366 return; /* no more delay required */
3367 }
3368
3369 /* allow sleep for extra 50us if needed */
3370 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3371}
3372
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303373/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303374 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3375 * @hba: per adapter instance
3376 * @attr_sel: uic command argument1
3377 * @attr_set: attribute set type as uic command argument2
3378 * @mib_val: setting value as uic command argument3
3379 * @peer: indicate whether peer or local
3380 *
3381 * Returns 0 on success, non-zero value on failure
3382 */
3383int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3384 u8 attr_set, u32 mib_val, u8 peer)
3385{
3386 struct uic_command uic_cmd = {0};
3387 static const char *const action[] = {
3388 "dme-set",
3389 "dme-peer-set"
3390 };
3391 const char *set = action[!!peer];
3392 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003393 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303394
3395 uic_cmd.command = peer ?
3396 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3397 uic_cmd.argument1 = attr_sel;
3398 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3399 uic_cmd.argument3 = mib_val;
3400
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003401 do {
3402 /* for peer attributes we retry upon failure */
3403 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3404 if (ret)
3405 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3406 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3407 } while (ret && peer && --retries);
3408
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003409 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003410 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003411 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3412 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303413
3414 return ret;
3415}
3416EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3417
3418/**
3419 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3420 * @hba: per adapter instance
3421 * @attr_sel: uic command argument1
3422 * @mib_val: the value of the attribute as returned by the UIC command
3423 * @peer: indicate whether peer or local
3424 *
3425 * Returns 0 on success, non-zero value on failure
3426 */
3427int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3428 u32 *mib_val, u8 peer)
3429{
3430 struct uic_command uic_cmd = {0};
3431 static const char *const action[] = {
3432 "dme-get",
3433 "dme-peer-get"
3434 };
3435 const char *get = action[!!peer];
3436 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003437 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003438 struct ufs_pa_layer_attr orig_pwr_info;
3439 struct ufs_pa_layer_attr temp_pwr_info;
3440 bool pwr_mode_change = false;
3441
3442 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3443 orig_pwr_info = hba->pwr_info;
3444 temp_pwr_info = orig_pwr_info;
3445
3446 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3447 orig_pwr_info.pwr_rx == FAST_MODE) {
3448 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3449 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3450 pwr_mode_change = true;
3451 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3452 orig_pwr_info.pwr_rx == SLOW_MODE) {
3453 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3454 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3455 pwr_mode_change = true;
3456 }
3457 if (pwr_mode_change) {
3458 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3459 if (ret)
3460 goto out;
3461 }
3462 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303463
3464 uic_cmd.command = peer ?
3465 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3466 uic_cmd.argument1 = attr_sel;
3467
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003468 do {
3469 /* for peer attributes we retry upon failure */
3470 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3471 if (ret)
3472 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3473 get, UIC_GET_ATTR_ID(attr_sel), ret);
3474 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303475
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003476 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003477 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003478 get, UIC_GET_ATTR_ID(attr_sel),
3479 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003480
3481 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303482 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003483
3484 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3485 && pwr_mode_change)
3486 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303487out:
3488 return ret;
3489}
3490EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3491
3492/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003493 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3494 * state) and waits for it to take effect.
3495 *
3496 * @hba: per adapter instance
3497 * @cmd: UIC command to execute
3498 *
3499 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3500 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3501 * and device UniPro link and hence it's final completion would be indicated by
3502 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3503 * addition to normal UIC command completion Status (UCCS). This function only
3504 * returns after the relevant status bits indicate the completion.
3505 *
3506 * Returns 0 on success, non-zero value on failure
3507 */
3508static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3509{
3510 struct completion uic_async_done;
3511 unsigned long flags;
3512 u8 status;
3513 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003514 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003515
3516 mutex_lock(&hba->uic_cmd_mutex);
3517 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003518 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003519
3520 spin_lock_irqsave(hba->host->host_lock, flags);
3521 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003522 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3523 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3524 /*
3525 * Make sure UIC command completion interrupt is disabled before
3526 * issuing UIC command.
3527 */
3528 wmb();
3529 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003530 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003531 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3532 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003533 if (ret) {
3534 dev_err(hba->dev,
3535 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3536 cmd->command, cmd->argument3, ret);
3537 goto out;
3538 }
3539
3540 if (!wait_for_completion_timeout(hba->uic_async_done,
3541 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3542 dev_err(hba->dev,
3543 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3544 cmd->command, cmd->argument3);
3545 ret = -ETIMEDOUT;
3546 goto out;
3547 }
3548
3549 status = ufshcd_get_upmcrs(hba);
3550 if (status != PWR_LOCAL) {
3551 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09003552 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003553 cmd->command, status);
3554 ret = (status != PWR_OK) ? status : -1;
3555 }
3556out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003557 if (ret) {
3558 ufshcd_print_host_state(hba);
3559 ufshcd_print_pwr_info(hba);
3560 ufshcd_print_host_regs(hba);
3561 }
3562
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003563 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003564 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003565 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003566 if (reenable_intr)
3567 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003568 spin_unlock_irqrestore(hba->host->host_lock, flags);
3569 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003570
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003571 return ret;
3572}
3573
3574/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303575 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3576 * using DME_SET primitives.
3577 * @hba: per adapter instance
3578 * @mode: powr mode value
3579 *
3580 * Returns 0 on success, non-zero value on failure
3581 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303582static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303583{
3584 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003585 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303586
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003587 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3588 ret = ufshcd_dme_set(hba,
3589 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3590 if (ret) {
3591 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3592 __func__, ret);
3593 goto out;
3594 }
3595 }
3596
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303597 uic_cmd.command = UIC_CMD_DME_SET;
3598 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3599 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003600 ufshcd_hold(hba, false);
3601 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3602 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303603
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003604out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003605 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003606}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303607
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003608static int ufshcd_link_recovery(struct ufs_hba *hba)
3609{
3610 int ret;
3611 unsigned long flags;
3612
3613 spin_lock_irqsave(hba->host->host_lock, flags);
3614 hba->ufshcd_state = UFSHCD_STATE_RESET;
3615 ufshcd_set_eh_in_progress(hba);
3616 spin_unlock_irqrestore(hba->host->host_lock, flags);
3617
3618 ret = ufshcd_host_reset_and_restore(hba);
3619
3620 spin_lock_irqsave(hba->host->host_lock, flags);
3621 if (ret)
3622 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3623 ufshcd_clear_eh_in_progress(hba);
3624 spin_unlock_irqrestore(hba->host->host_lock, flags);
3625
3626 if (ret)
3627 dev_err(hba->dev, "%s: link recovery failed, err %d",
3628 __func__, ret);
3629
3630 return ret;
3631}
3632
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003633static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003634{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003635 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003636 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003637 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003638
Kiwoong Kimee32c902016-11-10 21:17:43 +09003639 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3640
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003641 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003642 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003643 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3644 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003645
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003646 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003647 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3648 __func__, ret);
3649
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003650 /*
3651 * If link recovery fails then return error so that caller
3652 * don't retry the hibern8 enter again.
3653 */
3654 if (ufshcd_link_recovery(hba))
3655 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09003656 } else
3657 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3658 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003659
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003660 return ret;
3661}
3662
3663static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3664{
3665 int ret = 0, retries;
3666
3667 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3668 ret = __ufshcd_uic_hibern8_enter(hba);
3669 if (!ret || ret == -ENOLINK)
3670 goto out;
3671 }
3672out:
3673 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003674}
3675
3676static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
3677{
3678 struct uic_command uic_cmd = {0};
3679 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003680 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003681
Kiwoong Kimee32c902016-11-10 21:17:43 +09003682 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3683
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003684 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3685 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003686 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3687 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3688
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303689 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003690 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3691 __func__, ret);
3692 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003693 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003694 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3695 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003696 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3697 hba->ufs_stats.hibern8_exit_cnt++;
3698 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303699
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303700 return ret;
3701}
3702
Yaniv Gardi50646362014-10-23 13:25:13 +03003703 /**
3704 * ufshcd_init_pwr_info - setting the POR (power on reset)
3705 * values in hba power info
3706 * @hba: per-adapter instance
3707 */
3708static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3709{
3710 hba->pwr_info.gear_rx = UFS_PWM_G1;
3711 hba->pwr_info.gear_tx = UFS_PWM_G1;
3712 hba->pwr_info.lane_rx = 1;
3713 hba->pwr_info.lane_tx = 1;
3714 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3715 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3716 hba->pwr_info.hs_rate = 0;
3717}
3718
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303719/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003720 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3721 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303722 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003723static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303724{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003725 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3726
3727 if (hba->max_pwr_info.is_valid)
3728 return 0;
3729
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003730 pwr_info->pwr_tx = FAST_MODE;
3731 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003732 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303733
3734 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003735 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3736 &pwr_info->lane_rx);
3737 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3738 &pwr_info->lane_tx);
3739
3740 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3741 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3742 __func__,
3743 pwr_info->lane_rx,
3744 pwr_info->lane_tx);
3745 return -EINVAL;
3746 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303747
3748 /*
3749 * First, get the maximum gears of HS speed.
3750 * If a zero value, it means there is no HSGEAR capability.
3751 * Then, get the maximum gears of PWM speed.
3752 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003753 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3754 if (!pwr_info->gear_rx) {
3755 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3756 &pwr_info->gear_rx);
3757 if (!pwr_info->gear_rx) {
3758 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3759 __func__, pwr_info->gear_rx);
3760 return -EINVAL;
3761 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003762 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303763 }
3764
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003765 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3766 &pwr_info->gear_tx);
3767 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303768 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003769 &pwr_info->gear_tx);
3770 if (!pwr_info->gear_tx) {
3771 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3772 __func__, pwr_info->gear_tx);
3773 return -EINVAL;
3774 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003775 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003776 }
3777
3778 hba->max_pwr_info.is_valid = true;
3779 return 0;
3780}
3781
3782static int ufshcd_change_power_mode(struct ufs_hba *hba,
3783 struct ufs_pa_layer_attr *pwr_mode)
3784{
3785 int ret;
3786
3787 /* if already configured to the requested pwr_mode */
3788 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
3789 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
3790 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
3791 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
3792 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
3793 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
3794 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
3795 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
3796 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303797 }
3798
3799 /*
3800 * Configure attributes for power mode change with below.
3801 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3802 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3803 * - PA_HSSERIES
3804 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003805 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
3806 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
3807 pwr_mode->lane_rx);
3808 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3809 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303810 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003811 else
3812 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303813
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003814 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
3815 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
3816 pwr_mode->lane_tx);
3817 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
3818 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303819 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003820 else
3821 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303822
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003823 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3824 pwr_mode->pwr_tx == FASTAUTO_MODE ||
3825 pwr_mode->pwr_rx == FAST_MODE ||
3826 pwr_mode->pwr_tx == FAST_MODE)
3827 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
3828 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303829
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003830 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
3831 | pwr_mode->pwr_tx);
3832
3833 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303834 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003835 "%s: power mode change failed %d\n", __func__, ret);
3836 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003837 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
3838 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003839
3840 memcpy(&hba->pwr_info, pwr_mode,
3841 sizeof(struct ufs_pa_layer_attr));
3842 }
3843
3844 return ret;
3845}
3846
3847/**
3848 * ufshcd_config_pwr_mode - configure a new power mode
3849 * @hba: per-adapter instance
3850 * @desired_pwr_mode: desired power configuration
3851 */
3852static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3853 struct ufs_pa_layer_attr *desired_pwr_mode)
3854{
3855 struct ufs_pa_layer_attr final_params = { 0 };
3856 int ret;
3857
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003858 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3859 desired_pwr_mode, &final_params);
3860
3861 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003862 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3863
3864 ret = ufshcd_change_power_mode(hba, &final_params);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08003865 if (!ret)
3866 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303867
3868 return ret;
3869}
3870
3871/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303872 * ufshcd_complete_dev_init() - checks device readiness
3873 * hba: per-adapter instance
3874 *
3875 * Set fDeviceInit flag and poll until device toggles it.
3876 */
3877static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3878{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003879 int i;
3880 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303881 bool flag_res = 1;
3882
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003883 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3884 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303885 if (err) {
3886 dev_err(hba->dev,
3887 "%s setting fDeviceInit flag failed with error %d\n",
3888 __func__, err);
3889 goto out;
3890 }
3891
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003892 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3893 for (i = 0; i < 1000 && !err && flag_res; i++)
3894 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3895 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3896
Dolev Raviv68078d52013-07-30 00:35:58 +05303897 if (err)
3898 dev_err(hba->dev,
3899 "%s reading fDeviceInit flag failed with error %d\n",
3900 __func__, err);
3901 else if (flag_res)
3902 dev_err(hba->dev,
3903 "%s fDeviceInit was not cleared by the device\n",
3904 __func__);
3905
3906out:
3907 return err;
3908}
3909
3910/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303911 * ufshcd_make_hba_operational - Make UFS controller operational
3912 * @hba: per adapter instance
3913 *
3914 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003915 * 1. Enable required interrupts
3916 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003917 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003918 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303919 *
3920 * Returns 0 on success, non-zero value on failure
3921 */
3922static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3923{
3924 int err = 0;
3925 u32 reg;
3926
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303927 /* Enable required interrupts */
3928 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3929
3930 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003931 if (ufshcd_is_intr_aggr_allowed(hba))
3932 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3933 else
3934 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303935
3936 /* Configure UTRL and UTMRL base address registers */
3937 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3938 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3939 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3940 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3941 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3942 REG_UTP_TASK_REQ_LIST_BASE_L);
3943 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3944 REG_UTP_TASK_REQ_LIST_BASE_H);
3945
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303946 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003947 * Make sure base address and interrupt setup are updated before
3948 * enabling the run/stop registers below.
3949 */
3950 wmb();
3951
3952 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303953 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303954 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003955 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303956 if (!(ufshcd_get_lists_status(reg))) {
3957 ufshcd_enable_run_stop_reg(hba);
3958 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303959 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303960 "Host controller not ready to process requests");
3961 err = -EIO;
3962 goto out;
3963 }
3964
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303965out:
3966 return err;
3967}
3968
3969/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003970 * ufshcd_hba_stop - Send controller to reset state
3971 * @hba: per adapter instance
3972 * @can_sleep: perform sleep or just spin
3973 */
3974static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3975{
3976 int err;
3977
3978 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3979 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3980 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3981 10, 1, can_sleep);
3982 if (err)
3983 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3984}
3985
3986/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303987 * ufshcd_hba_enable - initialize the controller
3988 * @hba: per adapter instance
3989 *
3990 * The controller resets itself and controller firmware initialization
3991 * sequence kicks off. When controller is ready it will set
3992 * the Host Controller Enable bit to 1.
3993 *
3994 * Returns 0 on success, non-zero value on failure
3995 */
3996static int ufshcd_hba_enable(struct ufs_hba *hba)
3997{
3998 int retry;
3999
4000 /*
4001 * msleep of 1 and 5 used in this function might result in msleep(20),
4002 * but it was necessary to send the UFS FPGA to reset mode during
4003 * development and testing of this driver. msleep can be changed to
4004 * mdelay and retry count can be reduced based on the controller.
4005 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004006 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304007 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004008 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304009
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004010 /* UniPro link is disabled at this point */
4011 ufshcd_set_link_off(hba);
4012
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004013 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004014
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304015 /* start controller initialization sequence */
4016 ufshcd_hba_start(hba);
4017
4018 /*
4019 * To initialize a UFS host controller HCE bit must be set to 1.
4020 * During initialization the HCE bit value changes from 1->0->1.
4021 * When the host controller completes initialization sequence
4022 * it sets the value of HCE bit to 1. The same HCE bit is read back
4023 * to check if the controller has completed initialization sequence.
4024 * So without this delay the value HCE = 1, set in the previous
4025 * instruction might be read back.
4026 * This delay can be changed based on the controller.
4027 */
4028 msleep(1);
4029
4030 /* wait for the host controller to complete initialization */
4031 retry = 10;
4032 while (ufshcd_is_hba_active(hba)) {
4033 if (retry) {
4034 retry--;
4035 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304036 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304037 "Controller enable failed\n");
4038 return -EIO;
4039 }
4040 msleep(5);
4041 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004042
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004043 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004044 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004045
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004046 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004047
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304048 return 0;
4049}
4050
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004051static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4052{
4053 int tx_lanes, i, err = 0;
4054
4055 if (!peer)
4056 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4057 &tx_lanes);
4058 else
4059 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4060 &tx_lanes);
4061 for (i = 0; i < tx_lanes; i++) {
4062 if (!peer)
4063 err = ufshcd_dme_set(hba,
4064 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4065 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4066 0);
4067 else
4068 err = ufshcd_dme_peer_set(hba,
4069 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4070 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4071 0);
4072 if (err) {
4073 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4074 __func__, peer, i, err);
4075 break;
4076 }
4077 }
4078
4079 return err;
4080}
4081
4082static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4083{
4084 return ufshcd_disable_tx_lcc(hba, true);
4085}
4086
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304087/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304088 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304089 * @hba: per adapter instance
4090 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304091 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304092 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304093static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304094{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304095 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004096 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004097 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304098
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004099 /*
4100 * If UFS device isn't active then we will have to issue link startup
4101 * 2 times to make sure the device state move to active.
4102 */
4103 if (!ufshcd_is_ufs_dev_active(hba))
4104 link_startup_again = true;
4105
4106link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004107 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004108 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304109
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004110 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004111
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004112 /* check if device is detected by inter-connect layer */
4113 if (!ret && !ufshcd_is_device_present(hba)) {
4114 dev_err(hba->dev, "%s: Device not present\n", __func__);
4115 ret = -ENXIO;
4116 goto out;
4117 }
4118
4119 /*
4120 * DME link lost indication is only received when link is up,
4121 * but we can't be sure if the link is up until link startup
4122 * succeeds. So reset the local Uni-Pro and try again.
4123 */
4124 if (ret && ufshcd_hba_enable(hba))
4125 goto out;
4126 } while (ret && retries--);
4127
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304128 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004129 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304130 goto out;
4131
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004132 if (link_startup_again) {
4133 link_startup_again = false;
4134 retries = DME_LINKSTARTUP_RETRIES;
4135 goto link_startup;
4136 }
4137
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004138 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4139 ufshcd_init_pwr_info(hba);
4140 ufshcd_print_pwr_info(hba);
4141
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004142 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4143 ret = ufshcd_disable_device_tx_lcc(hba);
4144 if (ret)
4145 goto out;
4146 }
4147
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004148 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004149 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4150 if (ret)
4151 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004152
4153 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304154out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004155 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304156 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004157 ufshcd_print_host_state(hba);
4158 ufshcd_print_pwr_info(hba);
4159 ufshcd_print_host_regs(hba);
4160 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304161 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304162}
4163
4164/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304165 * ufshcd_verify_dev_init() - Verify device initialization
4166 * @hba: per-adapter instance
4167 *
4168 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4169 * device Transport Protocol (UTP) layer is ready after a reset.
4170 * If the UTP layer at the device side is not initialized, it may
4171 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4172 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4173 */
4174static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4175{
4176 int err = 0;
4177 int retries;
4178
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004179 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304180 mutex_lock(&hba->dev_cmd.lock);
4181 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4182 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4183 NOP_OUT_TIMEOUT);
4184
4185 if (!err || err == -ETIMEDOUT)
4186 break;
4187
4188 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4189 }
4190 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004191 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304192
4193 if (err)
4194 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4195 return err;
4196}
4197
4198/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004199 * ufshcd_set_queue_depth - set lun queue depth
4200 * @sdev: pointer to SCSI device
4201 *
4202 * Read bLUQueueDepth value and activate scsi tagged command
4203 * queueing. For WLUN, queue depth is set to 1. For best-effort
4204 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4205 * value that host can queue.
4206 */
4207static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4208{
4209 int ret = 0;
4210 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08004211 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004212 struct ufs_hba *hba;
4213
4214 hba = shost_priv(sdev->host);
4215
4216 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08004217 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
4218 /* Read descriptor*/
4219 ret = ufshcd_read_unit_desc_param(hba,
4220 ufshcd_scsi_to_upiu_lun(sdev->lun),
4221 UNIT_DESC_PARAM_LU_Q_DEPTH,
4222 &lun_qdepth,
4223 sizeof(lun_qdepth));
4224 if (!ret || ret == -ENOTSUPP)
4225 break;
4226
4227 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
4228 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004229
4230 /* Some WLUN doesn't support unit descriptor */
4231 if (ret == -EOPNOTSUPP)
4232 lun_qdepth = 1;
4233 else if (!lun_qdepth)
4234 /* eventually, we can figure out the real queue depth */
4235 lun_qdepth = hba->nutrs;
4236 else
4237 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4238
4239 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4240 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004241 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004242}
4243
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004244/*
4245 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4246 * @hba: per-adapter instance
4247 * @lun: UFS device lun id
4248 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4249 *
4250 * Returns 0 in case of success and b_lu_write_protect status would be returned
4251 * @b_lu_write_protect parameter.
4252 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4253 * Returns -EINVAL in case of invalid parameters passed to this function.
4254 */
4255static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4256 u8 lun,
4257 u8 *b_lu_write_protect)
4258{
4259 int ret;
4260
4261 if (!b_lu_write_protect)
4262 ret = -EINVAL;
4263 /*
4264 * According to UFS device spec, RPMB LU can't be write
4265 * protected so skip reading bLUWriteProtect parameter for
4266 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4267 */
4268 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4269 ret = -ENOTSUPP;
4270 else
4271 ret = ufshcd_read_unit_desc_param(hba,
4272 lun,
4273 UNIT_DESC_PARAM_LU_WR_PROTECT,
4274 b_lu_write_protect,
4275 sizeof(*b_lu_write_protect));
4276 return ret;
4277}
4278
4279/**
4280 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4281 * status
4282 * @hba: per-adapter instance
4283 * @sdev: pointer to SCSI device
4284 *
4285 */
4286static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4287 struct scsi_device *sdev)
4288{
4289 if (hba->dev_info.f_power_on_wp_en &&
4290 !hba->dev_info.is_lu_power_on_wp) {
4291 u8 b_lu_write_protect;
4292
4293 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4294 &b_lu_write_protect) &&
4295 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4296 hba->dev_info.is_lu_power_on_wp = true;
4297 }
4298}
4299
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004300/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304301 * ufshcd_slave_alloc - handle initial SCSI device configurations
4302 * @sdev: pointer to SCSI device
4303 *
4304 * Returns success
4305 */
4306static int ufshcd_slave_alloc(struct scsi_device *sdev)
4307{
4308 struct ufs_hba *hba;
4309
4310 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304311
4312 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4313 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304314
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304315 /* allow SCSI layer to restart the device in case of errors */
4316 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004317
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004318 /* REPORT SUPPORTED OPERATION CODES is not supported */
4319 sdev->no_report_opcodes = 1;
4320
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004321
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004322 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004323
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004324 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4325
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004326 return 0;
4327}
4328
4329/**
4330 * ufshcd_change_queue_depth - change queue depth
4331 * @sdev: pointer to SCSI device
4332 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004333 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004334 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004335 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004336static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004337{
4338 struct ufs_hba *hba = shost_priv(sdev->host);
4339
4340 if (depth > hba->nutrs)
4341 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004342 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304343}
4344
4345/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004346 * ufshcd_slave_configure - adjust SCSI device configurations
4347 * @sdev: pointer to SCSI device
4348 */
4349static int ufshcd_slave_configure(struct scsi_device *sdev)
4350{
4351 struct request_queue *q = sdev->request_queue;
4352
4353 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4354 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
4355
4356 return 0;
4357}
4358
4359/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304360 * ufshcd_slave_destroy - remove SCSI device configurations
4361 * @sdev: pointer to SCSI device
4362 */
4363static void ufshcd_slave_destroy(struct scsi_device *sdev)
4364{
4365 struct ufs_hba *hba;
4366
4367 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004368 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004369 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4370 unsigned long flags;
4371
4372 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004373 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004374 spin_unlock_irqrestore(hba->host->host_lock, flags);
4375 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304376}
4377
4378/**
4379 * ufshcd_task_req_compl - handle task management request completion
4380 * @hba: per adapter instance
4381 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304382 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304383 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304384 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304385 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304386static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304387{
4388 struct utp_task_req_desc *task_req_descp;
4389 struct utp_upiu_task_rsp *task_rsp_upiup;
4390 unsigned long flags;
4391 int ocs_value;
4392 int task_result;
4393
4394 spin_lock_irqsave(hba->host->host_lock, flags);
4395
4396 /* Clear completed tasks from outstanding_tasks */
4397 __clear_bit(index, &hba->outstanding_tasks);
4398
4399 task_req_descp = hba->utmrdl_base_addr;
4400 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
4401
4402 if (ocs_value == OCS_SUCCESS) {
4403 task_rsp_upiup = (struct utp_upiu_task_rsp *)
4404 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09004405 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
4406 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304407 if (resp)
4408 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304409 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304410 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
4411 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304412 }
4413 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304414
4415 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304416}
4417
4418/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304419 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4420 * @lrb: pointer to local reference block of completed command
4421 * @scsi_status: SCSI command status
4422 *
4423 * Returns value base on SCSI command status
4424 */
4425static inline int
4426ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4427{
4428 int result = 0;
4429
4430 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304431 case SAM_STAT_CHECK_CONDITION:
4432 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304433 case SAM_STAT_GOOD:
4434 result |= DID_OK << 16 |
4435 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304436 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304437 break;
4438 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304439 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304440 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304441 ufshcd_copy_sense_data(lrbp);
4442 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304443 break;
4444 default:
4445 result |= DID_ERROR << 16;
4446 break;
4447 } /* end of switch */
4448
4449 return result;
4450}
4451
4452/**
4453 * ufshcd_transfer_rsp_status - Get overall status of the response
4454 * @hba: per adapter instance
4455 * @lrb: pointer to local reference block of completed command
4456 *
4457 * Returns result of the command to notify SCSI midlayer
4458 */
4459static inline int
4460ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4461{
4462 int result = 0;
4463 int scsi_status;
4464 int ocs;
4465
4466 /* overall command status of utrd */
4467 ocs = ufshcd_get_tr_ocs(lrbp);
4468
4469 switch (ocs) {
4470 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304471 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004472 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304473 switch (result) {
4474 case UPIU_TRANSACTION_RESPONSE:
4475 /*
4476 * get the response UPIU result to extract
4477 * the SCSI command status
4478 */
4479 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4480
4481 /*
4482 * get the result based on SCSI status response
4483 * to notify the SCSI midlayer of the command status
4484 */
4485 scsi_status = result & MASK_SCSI_STATUS;
4486 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304487
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004488 /*
4489 * Currently we are only supporting BKOPs exception
4490 * events hence we can ignore BKOPs exception event
4491 * during power management callbacks. BKOPs exception
4492 * event is not expected to be raised in runtime suspend
4493 * callback as it allows the urgent bkops.
4494 * During system suspend, we are anyway forcefully
4495 * disabling the bkops and if urgent bkops is needed
4496 * it will be enabled on system resume. Long term
4497 * solution could be to abort the system suspend if
4498 * UFS device needs urgent BKOPs.
4499 */
4500 if (!hba->pm_op_in_progress &&
4501 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304502 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304503 break;
4504 case UPIU_TRANSACTION_REJECT_UPIU:
4505 /* TODO: handle Reject UPIU Response */
4506 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304507 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304508 "Reject UPIU not fully implemented\n");
4509 break;
4510 default:
4511 result = DID_ERROR << 16;
4512 dev_err(hba->dev,
4513 "Unexpected request response code = %x\n",
4514 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304515 break;
4516 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304517 break;
4518 case OCS_ABORTED:
4519 result |= DID_ABORT << 16;
4520 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304521 case OCS_INVALID_COMMAND_STATUS:
4522 result |= DID_REQUEUE << 16;
4523 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304524 case OCS_INVALID_CMD_TABLE_ATTR:
4525 case OCS_INVALID_PRDT_ATTR:
4526 case OCS_MISMATCH_DATA_BUF_SIZE:
4527 case OCS_MISMATCH_RESP_UPIU_SIZE:
4528 case OCS_PEER_COMM_FAILURE:
4529 case OCS_FATAL_ERROR:
4530 default:
4531 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304532 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004533 "OCS error from controller = %x for tag %d\n",
4534 ocs, lrbp->task_tag);
4535 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004536 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304537 break;
4538 } /* end of switch */
4539
Dolev Raviv66cc8202016-12-22 18:39:42 -08004540 if (host_byte(result) != DID_OK)
4541 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304542 return result;
4543}
4544
4545/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304546 * ufshcd_uic_cmd_compl - handle completion of uic command
4547 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304548 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304549 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304550static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304551{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304552 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304553 hba->active_uic_cmd->argument2 |=
4554 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304555 hba->active_uic_cmd->argument3 =
4556 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304557 complete(&hba->active_uic_cmd->done);
4558 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304559
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004560 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
4561 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304562}
4563
4564/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004565 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304566 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004567 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304568 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004569static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4570 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304571{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304572 struct ufshcd_lrb *lrbp;
4573 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304574 int result;
4575 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004576
Dolev Ravive9d501b2014-07-01 12:22:37 +03004577 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4578 lrbp = &hba->lrb[index];
4579 cmd = lrbp->cmd;
4580 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004581 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004582 result = ufshcd_transfer_rsp_status(hba, lrbp);
4583 scsi_dma_unmap(cmd);
4584 cmd->result = result;
4585 /* Mark completed command as NULL in LRB */
4586 lrbp->cmd = NULL;
4587 clear_bit_unlock(index, &hba->lrb_in_use);
4588 /* Do not touch lrbp after scsi done */
4589 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004590 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004591 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4592 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004593 if (hba->dev_cmd.complete) {
4594 ufshcd_add_command_trace(hba, index,
4595 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004596 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004597 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004598 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004599 if (ufshcd_is_clkscaling_supported(hba))
4600 hba->clk_scaling.active_reqs--;
4601 if (ufshcd_is_clkscaling_supported(hba))
4602 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004603 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304604
4605 /* clear corresponding bits of completed commands */
4606 hba->outstanding_reqs ^= completed_reqs;
4607
Sahitya Tummala856b3482014-09-25 15:32:34 +03004608 ufshcd_clk_scaling_update_busy(hba);
4609
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304610 /* we might have free'd some tags above */
4611 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304612}
4613
4614/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004615 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4616 * @hba: per adapter instance
4617 */
4618static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
4619{
4620 unsigned long completed_reqs;
4621 u32 tr_doorbell;
4622
4623 /* Resetting interrupt aggregation counters first and reading the
4624 * DOOR_BELL afterward allows us to handle all the completed requests.
4625 * In order to prevent other interrupts starvation the DB is read once
4626 * after reset. The down side of this solution is the possibility of
4627 * false interrupt if device completes another request after resetting
4628 * aggregation and before reading the DB.
4629 */
4630 if (ufshcd_is_intr_aggr_allowed(hba))
4631 ufshcd_reset_intr_aggr(hba);
4632
4633 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4634 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4635
4636 __ufshcd_transfer_req_compl(hba, completed_reqs);
4637}
4638
4639/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304640 * ufshcd_disable_ee - disable exception event
4641 * @hba: per-adapter instance
4642 * @mask: exception event to disable
4643 *
4644 * Disables exception event in the device so that the EVENT_ALERT
4645 * bit is not set.
4646 *
4647 * Returns zero on success, non-zero error value on failure.
4648 */
4649static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4650{
4651 int err = 0;
4652 u32 val;
4653
4654 if (!(hba->ee_ctrl_mask & mask))
4655 goto out;
4656
4657 val = hba->ee_ctrl_mask & ~mask;
4658 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004659 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304660 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4661 if (!err)
4662 hba->ee_ctrl_mask &= ~mask;
4663out:
4664 return err;
4665}
4666
4667/**
4668 * ufshcd_enable_ee - enable exception event
4669 * @hba: per-adapter instance
4670 * @mask: exception event to enable
4671 *
4672 * Enable corresponding exception event in the device to allow
4673 * device to alert host in critical scenarios.
4674 *
4675 * Returns zero on success, non-zero error value on failure.
4676 */
4677static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4678{
4679 int err = 0;
4680 u32 val;
4681
4682 if (hba->ee_ctrl_mask & mask)
4683 goto out;
4684
4685 val = hba->ee_ctrl_mask | mask;
4686 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004687 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304688 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4689 if (!err)
4690 hba->ee_ctrl_mask |= mask;
4691out:
4692 return err;
4693}
4694
4695/**
4696 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4697 * @hba: per-adapter instance
4698 *
4699 * Allow device to manage background operations on its own. Enabling
4700 * this might lead to inconsistent latencies during normal data transfers
4701 * as the device is allowed to manage its own way of handling background
4702 * operations.
4703 *
4704 * Returns zero on success, non-zero on failure.
4705 */
4706static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4707{
4708 int err = 0;
4709
4710 if (hba->auto_bkops_enabled)
4711 goto out;
4712
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004713 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304714 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4715 if (err) {
4716 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4717 __func__, err);
4718 goto out;
4719 }
4720
4721 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004722 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304723
4724 /* No need of URGENT_BKOPS exception from the device */
4725 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4726 if (err)
4727 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4728 __func__, err);
4729out:
4730 return err;
4731}
4732
4733/**
4734 * ufshcd_disable_auto_bkops - block device in doing background operations
4735 * @hba: per-adapter instance
4736 *
4737 * Disabling background operations improves command response latency but
4738 * has drawback of device moving into critical state where the device is
4739 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4740 * host is idle so that BKOPS are managed effectively without any negative
4741 * impacts.
4742 *
4743 * Returns zero on success, non-zero on failure.
4744 */
4745static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
4746{
4747 int err = 0;
4748
4749 if (!hba->auto_bkops_enabled)
4750 goto out;
4751
4752 /*
4753 * If host assisted BKOPs is to be enabled, make sure
4754 * urgent bkops exception is allowed.
4755 */
4756 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
4757 if (err) {
4758 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
4759 __func__, err);
4760 goto out;
4761 }
4762
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004763 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304764 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4765 if (err) {
4766 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
4767 __func__, err);
4768 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4769 goto out;
4770 }
4771
4772 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004773 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304774out:
4775 return err;
4776}
4777
4778/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004779 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304780 * @hba: per adapter instance
4781 *
4782 * After a device reset the device may toggle the BKOPS_EN flag
4783 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004784 * as well. This function would change the auto-bkops state based on
4785 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304786 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004787static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304788{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004789 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
4790 hba->auto_bkops_enabled = false;
4791 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
4792 ufshcd_enable_auto_bkops(hba);
4793 } else {
4794 hba->auto_bkops_enabled = true;
4795 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
4796 ufshcd_disable_auto_bkops(hba);
4797 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304798}
4799
4800static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
4801{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004802 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304803 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
4804}
4805
4806/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004807 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4808 * @hba: per-adapter instance
4809 * @status: bkops_status value
4810 *
4811 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4812 * flag in the device to permit background operations if the device
4813 * bkops_status is greater than or equal to "status" argument passed to
4814 * this function, disable otherwise.
4815 *
4816 * Returns 0 for success, non-zero in case of failure.
4817 *
4818 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4819 * to know whether auto bkops is enabled or disabled after this function
4820 * returns control to it.
4821 */
4822static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
4823 enum bkops_status status)
4824{
4825 int err;
4826 u32 curr_status = 0;
4827
4828 err = ufshcd_get_bkops_status(hba, &curr_status);
4829 if (err) {
4830 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4831 __func__, err);
4832 goto out;
4833 } else if (curr_status > BKOPS_STATUS_MAX) {
4834 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
4835 __func__, curr_status);
4836 err = -EINVAL;
4837 goto out;
4838 }
4839
4840 if (curr_status >= status)
4841 err = ufshcd_enable_auto_bkops(hba);
4842 else
4843 err = ufshcd_disable_auto_bkops(hba);
4844out:
4845 return err;
4846}
4847
4848/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304849 * ufshcd_urgent_bkops - handle urgent bkops exception event
4850 * @hba: per-adapter instance
4851 *
4852 * Enable fBackgroundOpsEn flag in the device to permit background
4853 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004854 *
4855 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4856 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304857 */
4858static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4859{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004860 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304861}
4862
4863static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4864{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004865 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304866 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4867}
4868
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004869static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4870{
4871 int err;
4872 u32 curr_status = 0;
4873
4874 if (hba->is_urgent_bkops_lvl_checked)
4875 goto enable_auto_bkops;
4876
4877 err = ufshcd_get_bkops_status(hba, &curr_status);
4878 if (err) {
4879 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4880 __func__, err);
4881 goto out;
4882 }
4883
4884 /*
4885 * We are seeing that some devices are raising the urgent bkops
4886 * exception events even when BKOPS status doesn't indicate performace
4887 * impacted or critical. Handle these device by determining their urgent
4888 * bkops status at runtime.
4889 */
4890 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4891 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4892 __func__, curr_status);
4893 /* update the current status as the urgent bkops level */
4894 hba->urgent_bkops_lvl = curr_status;
4895 hba->is_urgent_bkops_lvl_checked = true;
4896 }
4897
4898enable_auto_bkops:
4899 err = ufshcd_enable_auto_bkops(hba);
4900out:
4901 if (err < 0)
4902 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4903 __func__, err);
4904}
4905
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304906/**
4907 * ufshcd_exception_event_handler - handle exceptions raised by device
4908 * @work: pointer to work data
4909 *
4910 * Read bExceptionEventStatus attribute from the device and handle the
4911 * exception event accordingly.
4912 */
4913static void ufshcd_exception_event_handler(struct work_struct *work)
4914{
4915 struct ufs_hba *hba;
4916 int err;
4917 u32 status = 0;
4918 hba = container_of(work, struct ufs_hba, eeh_work);
4919
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304920 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304921 err = ufshcd_get_ee_status(hba, &status);
4922 if (err) {
4923 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4924 __func__, err);
4925 goto out;
4926 }
4927
4928 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004929
4930 if (status & MASK_EE_URGENT_BKOPS)
4931 ufshcd_bkops_exception_event_handler(hba);
4932
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304933out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304934 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304935 return;
4936}
4937
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004938/* Complete requests that have door-bell cleared */
4939static void ufshcd_complete_requests(struct ufs_hba *hba)
4940{
4941 ufshcd_transfer_req_compl(hba);
4942 ufshcd_tmc_handler(hba);
4943}
4944
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304945/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004946 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4947 * to recover from the DL NAC errors or not.
4948 * @hba: per-adapter instance
4949 *
4950 * Returns true if error handling is required, false otherwise
4951 */
4952static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4953{
4954 unsigned long flags;
4955 bool err_handling = true;
4956
4957 spin_lock_irqsave(hba->host->host_lock, flags);
4958 /*
4959 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4960 * device fatal error and/or DL NAC & REPLAY timeout errors.
4961 */
4962 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4963 goto out;
4964
4965 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4966 ((hba->saved_err & UIC_ERROR) &&
4967 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4968 goto out;
4969
4970 if ((hba->saved_err & UIC_ERROR) &&
4971 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4972 int err;
4973 /*
4974 * wait for 50ms to see if we can get any other errors or not.
4975 */
4976 spin_unlock_irqrestore(hba->host->host_lock, flags);
4977 msleep(50);
4978 spin_lock_irqsave(hba->host->host_lock, flags);
4979
4980 /*
4981 * now check if we have got any other severe errors other than
4982 * DL NAC error?
4983 */
4984 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4985 ((hba->saved_err & UIC_ERROR) &&
4986 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4987 goto out;
4988
4989 /*
4990 * As DL NAC is the only error received so far, send out NOP
4991 * command to confirm if link is still active or not.
4992 * - If we don't get any response then do error recovery.
4993 * - If we get response then clear the DL NAC error bit.
4994 */
4995
4996 spin_unlock_irqrestore(hba->host->host_lock, flags);
4997 err = ufshcd_verify_dev_init(hba);
4998 spin_lock_irqsave(hba->host->host_lock, flags);
4999
5000 if (err)
5001 goto out;
5002
5003 /* Link seems to be alive hence ignore the DL NAC errors */
5004 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5005 hba->saved_err &= ~UIC_ERROR;
5006 /* clear NAC error */
5007 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5008 if (!hba->saved_uic_err) {
5009 err_handling = false;
5010 goto out;
5011 }
5012 }
5013out:
5014 spin_unlock_irqrestore(hba->host->host_lock, flags);
5015 return err_handling;
5016}
5017
5018/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305019 * ufshcd_err_handler - handle UFS errors that require s/w attention
5020 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305021 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305022static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305023{
5024 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305025 unsigned long flags;
5026 u32 err_xfer = 0;
5027 u32 err_tm = 0;
5028 int err = 0;
5029 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005030 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305031
5032 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305033
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305034 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005035 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305036
5037 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005038 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305039 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305040
5041 hba->ufshcd_state = UFSHCD_STATE_RESET;
5042 ufshcd_set_eh_in_progress(hba);
5043
5044 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005045 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005046
5047 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5048 bool ret;
5049
5050 spin_unlock_irqrestore(hba->host->host_lock, flags);
5051 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5052 ret = ufshcd_quirk_dl_nac_errors(hba);
5053 spin_lock_irqsave(hba->host->host_lock, flags);
5054 if (!ret)
5055 goto skip_err_handling;
5056 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005057 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5058 ((hba->saved_err & UIC_ERROR) &&
5059 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5060 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5061 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5062 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305063
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005064 /*
5065 * if host reset is required then skip clearing the pending
5066 * transfers forcefully because they will automatically get
5067 * cleared after link startup.
5068 */
5069 if (needs_reset)
5070 goto skip_pending_xfer_clear;
5071
5072 /* release lock as clear command might sleep */
5073 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305074 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005075 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5076 if (ufshcd_clear_cmd(hba, tag)) {
5077 err_xfer = true;
5078 goto lock_skip_pending_xfer_clear;
5079 }
5080 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305081
5082 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005083 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5084 if (ufshcd_clear_tm_cmd(hba, tag)) {
5085 err_tm = true;
5086 goto lock_skip_pending_xfer_clear;
5087 }
5088 }
5089
5090lock_skip_pending_xfer_clear:
5091 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305092
5093 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005094 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305095
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005096 if (err_xfer || err_tm)
5097 needs_reset = true;
5098
5099skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305100 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005101 if (needs_reset) {
5102 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5103
5104 /*
5105 * ufshcd_reset_and_restore() does the link reinitialization
5106 * which will need atleast one empty doorbell slot to send the
5107 * device management commands (NOP and query commands).
5108 * If there is no slot empty at this moment then free up last
5109 * slot forcefully.
5110 */
5111 if (hba->outstanding_reqs == max_doorbells)
5112 __ufshcd_transfer_req_compl(hba,
5113 (1UL << (hba->nutrs - 1)));
5114
5115 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305116 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005117 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305118 if (err) {
5119 dev_err(hba->dev, "%s: reset and restore failed\n",
5120 __func__);
5121 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5122 }
5123 /*
5124 * Inform scsi mid-layer that we did reset and allow to handle
5125 * Unit Attention properly.
5126 */
5127 scsi_report_bus_reset(hba->host, 0);
5128 hba->saved_err = 0;
5129 hba->saved_uic_err = 0;
5130 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005131
Yaniv Gardi583fa622016-03-10 17:37:13 +02005132skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005133 if (!needs_reset) {
5134 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5135 if (hba->saved_err || hba->saved_uic_err)
5136 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5137 __func__, hba->saved_err, hba->saved_uic_err);
5138 }
5139
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305140 ufshcd_clear_eh_in_progress(hba);
5141
5142out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005143 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305144 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005145 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305146 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305147}
5148
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005149static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
5150 u32 reg)
5151{
5152 reg_hist->reg[reg_hist->pos] = reg;
5153 reg_hist->tstamp[reg_hist->pos] = ktime_get();
5154 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
5155}
5156
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305157/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305158 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5159 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305160 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305161static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305162{
5163 u32 reg;
5164
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005165 /* PHY layer lane error */
5166 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5167 /* Ignore LINERESET indication, as this is not an error */
5168 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005169 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005170 /*
5171 * To know whether this error is fatal or not, DB timeout
5172 * must be checked but this error is handled separately.
5173 */
5174 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005175 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
5176 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005177
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305178 /* PA_INIT_ERROR is fatal and needs UIC reset */
5179 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005180 if (reg)
5181 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
5182
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305183 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5184 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005185 else if (hba->dev_quirks &
5186 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5187 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5188 hba->uic_error |=
5189 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5190 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5191 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5192 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305193
5194 /* UIC NL/TL/DME errors needs software retry */
5195 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005196 if (reg) {
5197 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305198 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005199 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305200
5201 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005202 if (reg) {
5203 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305204 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005205 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305206
5207 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005208 if (reg) {
5209 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305210 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005211 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305212
5213 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5214 __func__, hba->uic_error);
5215}
5216
5217/**
5218 * ufshcd_check_errors - Check for errors that need s/w attention
5219 * @hba: per-adapter instance
5220 */
5221static void ufshcd_check_errors(struct ufs_hba *hba)
5222{
5223 bool queue_eh_work = false;
5224
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305225 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305226 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305227
5228 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305229 hba->uic_error = 0;
5230 ufshcd_update_uic_error(hba);
5231 if (hba->uic_error)
5232 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305233 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305234
5235 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005236 /*
5237 * update the transfer error masks to sticky bits, let's do this
5238 * irrespective of current ufshcd_state.
5239 */
5240 hba->saved_err |= hba->errors;
5241 hba->saved_uic_err |= hba->uic_error;
5242
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305243 /* handle fatal errors only when link is functional */
5244 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5245 /* block commands from scsi mid-layer */
5246 scsi_block_requests(hba->host);
5247
Zang Leigang141f8162016-11-16 11:29:37 +08005248 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005249
5250 /* dump controller state before resetting */
5251 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5252 bool pr_prdt = !!(hba->saved_err &
5253 SYSTEM_BUS_FATAL_ERROR);
5254
5255 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5256 __func__, hba->saved_err,
5257 hba->saved_uic_err);
5258
5259 ufshcd_print_host_regs(hba);
5260 ufshcd_print_pwr_info(hba);
5261 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5262 ufshcd_print_trs(hba, hba->outstanding_reqs,
5263 pr_prdt);
5264 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305265 schedule_work(&hba->eh_work);
5266 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305267 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305268 /*
5269 * if (!queue_eh_work) -
5270 * Other errors are either non-fatal where host recovers
5271 * itself without s/w intervention or errors that will be
5272 * handled by the SCSI core layer.
5273 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305274}
5275
5276/**
5277 * ufshcd_tmc_handler - handle task management function completion
5278 * @hba: per adapter instance
5279 */
5280static void ufshcd_tmc_handler(struct ufs_hba *hba)
5281{
5282 u32 tm_doorbell;
5283
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305284 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305285 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305286 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305287}
5288
5289/**
5290 * ufshcd_sl_intr - Interrupt service routine
5291 * @hba: per adapter instance
5292 * @intr_status: contains interrupts generated by the controller
5293 */
5294static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
5295{
5296 hba->errors = UFSHCD_ERROR_MASK & intr_status;
5297 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305298 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305299
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305300 if (intr_status & UFSHCD_UIC_MASK)
5301 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305302
5303 if (intr_status & UTP_TASK_REQ_COMPL)
5304 ufshcd_tmc_handler(hba);
5305
5306 if (intr_status & UTP_TRANSFER_REQ_COMPL)
5307 ufshcd_transfer_req_compl(hba);
5308}
5309
5310/**
5311 * ufshcd_intr - Main interrupt service routine
5312 * @irq: irq number
5313 * @__hba: pointer to adapter instance
5314 *
5315 * Returns IRQ_HANDLED - If interrupt is valid
5316 * IRQ_NONE - If invalid interrupt
5317 */
5318static irqreturn_t ufshcd_intr(int irq, void *__hba)
5319{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005320 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305321 irqreturn_t retval = IRQ_NONE;
5322 struct ufs_hba *hba = __hba;
5323
5324 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305325 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005326 enabled_intr_status =
5327 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305328
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005329 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05305330 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005331
5332 if (enabled_intr_status) {
5333 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305334 retval = IRQ_HANDLED;
5335 }
5336 spin_unlock(hba->host->host_lock);
5337 return retval;
5338}
5339
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305340static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5341{
5342 int err = 0;
5343 u32 mask = 1 << tag;
5344 unsigned long flags;
5345
5346 if (!test_bit(tag, &hba->outstanding_tasks))
5347 goto out;
5348
5349 spin_lock_irqsave(hba->host->host_lock, flags);
5350 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
5351 spin_unlock_irqrestore(hba->host->host_lock, flags);
5352
5353 /* poll for max. 1 sec to clear door bell register by h/w */
5354 err = ufshcd_wait_for_register(hba,
5355 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005356 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305357out:
5358 return err;
5359}
5360
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305361/**
5362 * ufshcd_issue_tm_cmd - issues task management commands to controller
5363 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305364 * @lun_id: LUN ID to which TM command is sent
5365 * @task_id: task ID to which the TM command is applicable
5366 * @tm_function: task management function opcode
5367 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305368 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305369 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305370 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305371static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5372 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305373{
5374 struct utp_task_req_desc *task_req_descp;
5375 struct utp_upiu_task_req *task_req_upiup;
5376 struct Scsi_Host *host;
5377 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305378 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305379 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305380 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305381
5382 host = hba->host;
5383
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305384 /*
5385 * Get free slot, sleep if slots are unavailable.
5386 * Even though we use wait_event() which sleeps indefinitely,
5387 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5388 */
5389 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005390 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305391
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305392 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305393 task_req_descp = hba->utmrdl_base_addr;
5394 task_req_descp += free_slot;
5395
5396 /* Configure task request descriptor */
5397 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5398 task_req_descp->header.dword_2 =
5399 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5400
5401 /* Configure task request UPIU */
5402 task_req_upiup =
5403 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305404 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305405 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305406 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305407 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305408 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305409 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005410 /*
5411 * The host shall provide the same value for LUN field in the basic
5412 * header and for Input Parameter.
5413 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305414 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
5415 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305416
Kiwoong Kimd2877be2016-11-10 21:16:15 +09005417 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
5418
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305419 /* send command to the controller */
5420 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005421
5422 /* Make sure descriptors are ready before ringing the task doorbell */
5423 wmb();
5424
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305425 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07005426 /* Make sure that doorbell is committed immediately */
5427 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305428
5429 spin_unlock_irqrestore(host->host_lock, flags);
5430
5431 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305432 err = wait_event_timeout(hba->tm_wq,
5433 test_bit(free_slot, &hba->tm_condition),
5434 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305435 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305436 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
5437 __func__, tm_function);
5438 if (ufshcd_clear_tm_cmd(hba, free_slot))
5439 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
5440 __func__, free_slot);
5441 err = -ETIMEDOUT;
5442 } else {
5443 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305444 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305445
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305446 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305447 ufshcd_put_tm_slot(hba, free_slot);
5448 wake_up(&hba->tm_tag_wq);
5449
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005450 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305451 return err;
5452}
5453
5454/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305455 * ufshcd_eh_device_reset_handler - device reset handler registered to
5456 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305457 * @cmd: SCSI command pointer
5458 *
5459 * Returns SUCCESS/FAILED
5460 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305461static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305462{
5463 struct Scsi_Host *host;
5464 struct ufs_hba *hba;
5465 unsigned int tag;
5466 u32 pos;
5467 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305468 u8 resp = 0xF;
5469 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305470 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305471
5472 host = cmd->device->host;
5473 hba = shost_priv(host);
5474 tag = cmd->request->tag;
5475
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305476 lrbp = &hba->lrb[tag];
5477 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
5478 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305479 if (!err)
5480 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305481 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305482 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305483
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305484 /* clear the commands that were pending for corresponding LUN */
5485 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
5486 if (hba->lrb[pos].lun == lrbp->lun) {
5487 err = ufshcd_clear_cmd(hba, pos);
5488 if (err)
5489 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305490 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305491 }
5492 spin_lock_irqsave(host->host_lock, flags);
5493 ufshcd_transfer_req_compl(hba);
5494 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08005495
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305496out:
Gilad Broner7fabb772017-02-03 16:56:50 -08005497 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305498 if (!err) {
5499 err = SUCCESS;
5500 } else {
5501 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
5502 err = FAILED;
5503 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305504 return err;
5505}
5506
Gilad Bronere0b299e2017-02-03 16:56:40 -08005507static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
5508{
5509 struct ufshcd_lrb *lrbp;
5510 int tag;
5511
5512 for_each_set_bit(tag, &bitmap, hba->nutrs) {
5513 lrbp = &hba->lrb[tag];
5514 lrbp->req_abort_skip = true;
5515 }
5516}
5517
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305518/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305519 * ufshcd_abort - abort a specific command
5520 * @cmd: SCSI command pointer
5521 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305522 * Abort the pending command in device by sending UFS_ABORT_TASK task management
5523 * command, and in host controller by clearing the door-bell register. There can
5524 * be race between controller sending the command to the device while abort is
5525 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
5526 * really issued and then try to abort it.
5527 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305528 * Returns SUCCESS/FAILED
5529 */
5530static int ufshcd_abort(struct scsi_cmnd *cmd)
5531{
5532 struct Scsi_Host *host;
5533 struct ufs_hba *hba;
5534 unsigned long flags;
5535 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305536 int err = 0;
5537 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305538 u8 resp = 0xF;
5539 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005540 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305541
5542 host = cmd->device->host;
5543 hba = shost_priv(host);
5544 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08005545 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02005546 if (!ufshcd_valid_tag(hba, tag)) {
5547 dev_err(hba->dev,
5548 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
5549 __func__, tag, cmd, cmd->request);
5550 BUG();
5551 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305552
Dolev Ravive7d38252016-12-22 18:40:07 -08005553 /*
5554 * Task abort to the device W-LUN is illegal. When this command
5555 * will fail, due to spec violation, scsi err handling next step
5556 * will be to send LU reset which, again, is a spec violation.
5557 * To avoid these unnecessary/illegal step we skip to the last error
5558 * handling stage: reset and restore.
5559 */
5560 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
5561 return ufshcd_eh_host_reset_handler(cmd);
5562
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005563 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005564 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02005565 /* If command is already aborted/completed, return SUCCESS */
5566 if (!(test_bit(tag, &hba->outstanding_reqs))) {
5567 dev_err(hba->dev,
5568 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
5569 __func__, tag, hba->outstanding_reqs, reg);
5570 goto out;
5571 }
5572
Dolev Ravive9d501b2014-07-01 12:22:37 +03005573 if (!(reg & (1 << tag))) {
5574 dev_err(hba->dev,
5575 "%s: cmd was completed, but without a notifying intr, tag = %d",
5576 __func__, tag);
5577 }
5578
Dolev Raviv66cc8202016-12-22 18:39:42 -08005579 /* Print Transfer Request of aborted task */
5580 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08005581
Gilad Broner7fabb772017-02-03 16:56:50 -08005582 /*
5583 * Print detailed info about aborted request.
5584 * As more than one request might get aborted at the same time,
5585 * print full information only for the first aborted request in order
5586 * to reduce repeated printouts. For other aborted requests only print
5587 * basic details.
5588 */
5589 scsi_print_command(hba->lrb[tag].cmd);
5590 if (!hba->req_abort_count) {
5591 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08005592 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08005593 ufshcd_print_pwr_info(hba);
5594 ufshcd_print_trs(hba, 1 << tag, true);
5595 } else {
5596 ufshcd_print_trs(hba, 1 << tag, false);
5597 }
5598 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08005599
5600 /* Skip task abort in case previous aborts failed and report failure */
5601 if (lrbp->req_abort_skip) {
5602 err = -EIO;
5603 goto out;
5604 }
5605
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305606 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
5607 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5608 UFS_QUERY_TASK, &resp);
5609 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
5610 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005611 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
5612 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305613 break;
5614 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305615 /*
5616 * cmd not pending in the device, check if it is
5617 * in transition.
5618 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005619 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
5620 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305621 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5622 if (reg & (1 << tag)) {
5623 /* sleep for max. 200us to stabilize */
5624 usleep_range(100, 200);
5625 continue;
5626 }
5627 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005628 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
5629 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305630 goto out;
5631 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005632 dev_err(hba->dev,
5633 "%s: no response from device. tag = %d, err %d\n",
5634 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305635 if (!err)
5636 err = resp; /* service response error */
5637 goto out;
5638 }
5639 }
5640
5641 if (!poll_cnt) {
5642 err = -EBUSY;
5643 goto out;
5644 }
5645
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305646 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5647 UFS_ABORT_TASK, &resp);
5648 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005649 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305650 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005651 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
5652 __func__, tag, err);
5653 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305654 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305655 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305656
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305657 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005658 if (err) {
5659 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
5660 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305661 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005662 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305663
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305664 scsi_dma_unmap(cmd);
5665
5666 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02005667 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305668 hba->lrb[tag].cmd = NULL;
5669 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305670
5671 clear_bit_unlock(tag, &hba->lrb_in_use);
5672 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005673
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305674out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305675 if (!err) {
5676 err = SUCCESS;
5677 } else {
5678 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08005679 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305680 err = FAILED;
5681 }
5682
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005683 /*
5684 * This ufshcd_release() corresponds to the original scsi cmd that got
5685 * aborted here (as we won't get any IRQ for it).
5686 */
5687 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305688 return err;
5689}
5690
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305691/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305692 * ufshcd_host_reset_and_restore - reset and restore host controller
5693 * @hba: per-adapter instance
5694 *
5695 * Note that host controller reset may issue DME_RESET to
5696 * local and remote (device) Uni-Pro stack and the attributes
5697 * are reset to default state.
5698 *
5699 * Returns zero on success, non-zero on failure
5700 */
5701static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
5702{
5703 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305704 unsigned long flags;
5705
5706 /* Reset the host controller */
5707 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02005708 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305709 spin_unlock_irqrestore(hba->host->host_lock, flags);
5710
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08005711 /* scale up clocks to max frequency before full reinitialization */
5712 ufshcd_scale_clks(hba, true);
5713
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305714 err = ufshcd_hba_enable(hba);
5715 if (err)
5716 goto out;
5717
5718 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005719 err = ufshcd_probe_hba(hba);
5720
5721 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305722 err = -EIO;
5723out:
5724 if (err)
5725 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
5726
5727 return err;
5728}
5729
5730/**
5731 * ufshcd_reset_and_restore - reset and re-initialize host/device
5732 * @hba: per-adapter instance
5733 *
5734 * Reset and recover device, host and re-establish link. This
5735 * is helpful to recover the communication in fatal error conditions.
5736 *
5737 * Returns zero on success, non-zero on failure
5738 */
5739static int ufshcd_reset_and_restore(struct ufs_hba *hba)
5740{
5741 int err = 0;
5742 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005743 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305744
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005745 do {
5746 err = ufshcd_host_reset_and_restore(hba);
5747 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305748
5749 /*
5750 * After reset the door-bell might be cleared, complete
5751 * outstanding requests in s/w here.
5752 */
5753 spin_lock_irqsave(hba->host->host_lock, flags);
5754 ufshcd_transfer_req_compl(hba);
5755 ufshcd_tmc_handler(hba);
5756 spin_unlock_irqrestore(hba->host->host_lock, flags);
5757
5758 return err;
5759}
5760
5761/**
5762 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
5763 * @cmd - SCSI command pointer
5764 *
5765 * Returns SUCCESS/FAILED
5766 */
5767static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
5768{
5769 int err;
5770 unsigned long flags;
5771 struct ufs_hba *hba;
5772
5773 hba = shost_priv(cmd->device->host);
5774
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005775 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305776 /*
5777 * Check if there is any race with fatal error handling.
5778 * If so, wait for it to complete. Even though fatal error
5779 * handling does reset and restore in some cases, don't assume
5780 * anything out of it. We are just avoiding race here.
5781 */
5782 do {
5783 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305784 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305785 hba->ufshcd_state == UFSHCD_STATE_RESET))
5786 break;
5787 spin_unlock_irqrestore(hba->host->host_lock, flags);
5788 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305789 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305790 } while (1);
5791
5792 hba->ufshcd_state = UFSHCD_STATE_RESET;
5793 ufshcd_set_eh_in_progress(hba);
5794 spin_unlock_irqrestore(hba->host->host_lock, flags);
5795
5796 err = ufshcd_reset_and_restore(hba);
5797
5798 spin_lock_irqsave(hba->host->host_lock, flags);
5799 if (!err) {
5800 err = SUCCESS;
5801 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5802 } else {
5803 err = FAILED;
5804 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5805 }
5806 ufshcd_clear_eh_in_progress(hba);
5807 spin_unlock_irqrestore(hba->host->host_lock, flags);
5808
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005809 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305810 return err;
5811}
5812
5813/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005814 * ufshcd_get_max_icc_level - calculate the ICC level
5815 * @sup_curr_uA: max. current supported by the regulator
5816 * @start_scan: row at the desc table to start scan from
5817 * @buff: power descriptor buffer
5818 *
5819 * Returns calculated max ICC level for specific regulator
5820 */
5821static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
5822{
5823 int i;
5824 int curr_uA;
5825 u16 data;
5826 u16 unit;
5827
5828 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02005829 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005830 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
5831 ATTR_ICC_LVL_UNIT_OFFSET;
5832 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
5833 switch (unit) {
5834 case UFSHCD_NANO_AMP:
5835 curr_uA = curr_uA / 1000;
5836 break;
5837 case UFSHCD_MILI_AMP:
5838 curr_uA = curr_uA * 1000;
5839 break;
5840 case UFSHCD_AMP:
5841 curr_uA = curr_uA * 1000 * 1000;
5842 break;
5843 case UFSHCD_MICRO_AMP:
5844 default:
5845 break;
5846 }
5847 if (sup_curr_uA >= curr_uA)
5848 break;
5849 }
5850 if (i < 0) {
5851 i = 0;
5852 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
5853 }
5854
5855 return (u32)i;
5856}
5857
5858/**
5859 * ufshcd_calc_icc_level - calculate the max ICC level
5860 * In case regulators are not initialized we'll return 0
5861 * @hba: per-adapter instance
5862 * @desc_buf: power descriptor buffer to extract ICC levels from.
5863 * @len: length of desc_buff
5864 *
5865 * Returns calculated ICC level
5866 */
5867static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
5868 u8 *desc_buf, int len)
5869{
5870 u32 icc_level = 0;
5871
5872 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
5873 !hba->vreg_info.vccq2) {
5874 dev_err(hba->dev,
5875 "%s: Regulator capability was not set, actvIccLevel=%d",
5876 __func__, icc_level);
5877 goto out;
5878 }
5879
5880 if (hba->vreg_info.vcc)
5881 icc_level = ufshcd_get_max_icc_level(
5882 hba->vreg_info.vcc->max_uA,
5883 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
5884 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
5885
5886 if (hba->vreg_info.vccq)
5887 icc_level = ufshcd_get_max_icc_level(
5888 hba->vreg_info.vccq->max_uA,
5889 icc_level,
5890 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
5891
5892 if (hba->vreg_info.vccq2)
5893 icc_level = ufshcd_get_max_icc_level(
5894 hba->vreg_info.vccq2->max_uA,
5895 icc_level,
5896 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
5897out:
5898 return icc_level;
5899}
5900
Dolev Raviv61e07352016-11-23 16:30:49 -08005901static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
5902{
5903 int ret = 0;
5904 int retries;
5905
5906 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
5907 /* write attribute */
5908 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5909 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
5910 if (!ret)
5911 break;
5912
5913 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
5914 }
5915
5916 return ret;
5917}
5918
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005919static void ufshcd_init_icc_levels(struct ufs_hba *hba)
5920{
5921 int ret;
5922 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
5923 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
5924
5925 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
5926 if (ret) {
5927 dev_err(hba->dev,
5928 "%s: Failed reading power descriptor.len = %d ret = %d",
5929 __func__, buff_len, ret);
5930 return;
5931 }
5932
5933 hba->init_prefetch_data.icc_level =
5934 ufshcd_find_max_sup_active_icc_level(hba,
5935 desc_buf, buff_len);
5936 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
5937 __func__, hba->init_prefetch_data.icc_level);
5938
Dolev Raviv61e07352016-11-23 16:30:49 -08005939 ret = ufshcd_set_icc_levels_attr(hba,
5940 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005941
5942 if (ret)
5943 dev_err(hba->dev,
5944 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5945 __func__, hba->init_prefetch_data.icc_level , ret);
5946
5947}
5948
5949/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005950 * ufshcd_scsi_add_wlus - Adds required W-LUs
5951 * @hba: per-adapter instance
5952 *
5953 * UFS device specification requires the UFS devices to support 4 well known
5954 * logical units:
5955 * "REPORT_LUNS" (address: 01h)
5956 * "UFS Device" (address: 50h)
5957 * "RPMB" (address: 44h)
5958 * "BOOT" (address: 30h)
5959 * UFS device's power management needs to be controlled by "POWER CONDITION"
5960 * field of SSU (START STOP UNIT) command. But this "power condition" field
5961 * will take effect only when its sent to "UFS device" well known logical unit
5962 * hence we require the scsi_device instance to represent this logical unit in
5963 * order for the UFS host driver to send the SSU command for power management.
5964
5965 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5966 * Block) LU so user space process can control this LU. User space may also
5967 * want to have access to BOOT LU.
5968
5969 * This function adds scsi device instances for each of all well known LUs
5970 * (except "REPORT LUNS" LU).
5971 *
5972 * Returns zero on success (all required W-LUs are added successfully),
5973 * non-zero error value on failure (if failed to add any of the required W-LU).
5974 */
5975static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
5976{
5977 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005978 struct scsi_device *sdev_rpmb;
5979 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005980
5981 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
5982 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
5983 if (IS_ERR(hba->sdev_ufs_device)) {
5984 ret = PTR_ERR(hba->sdev_ufs_device);
5985 hba->sdev_ufs_device = NULL;
5986 goto out;
5987 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005988 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005989
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005990 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005991 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005992 if (IS_ERR(sdev_boot)) {
5993 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005994 goto remove_sdev_ufs_device;
5995 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005996 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005997
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005998 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005999 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006000 if (IS_ERR(sdev_rpmb)) {
6001 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006002 goto remove_sdev_boot;
6003 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006004 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006005 goto out;
6006
6007remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006008 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006009remove_sdev_ufs_device:
6010 scsi_remove_device(hba->sdev_ufs_device);
6011out:
6012 return ret;
6013}
6014
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006015static int ufs_get_device_desc(struct ufs_hba *hba,
6016 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006017{
6018 int err;
6019 u8 model_index;
6020 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
6021 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
6022
6023 err = ufshcd_read_device_desc(hba, desc_buf,
6024 QUERY_DESC_DEVICE_MAX_SIZE);
6025 if (err) {
6026 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6027 __func__, err);
6028 goto out;
6029 }
6030
6031 /*
6032 * getting vendor (manufacturerID) and Bank Index in big endian
6033 * format
6034 */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006035 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006036 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6037
6038 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
6039
6040 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
6041 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
6042 if (err) {
6043 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
6044 __func__, err);
6045 goto out;
6046 }
6047
6048 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006049 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006050 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
6051 MAX_MODEL_LEN));
6052
6053 /* Null terminate the model string */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006054 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006055
6056out:
6057 return err;
6058}
6059
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006060static void ufs_fixup_device_setup(struct ufs_hba *hba,
6061 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006062{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006063 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006064
6065 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006066 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
6067 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
6068 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006069 !strcmp(f->card.model, UFS_ANY_MODEL)))
6070 hba->dev_quirks |= f->quirk;
6071 }
6072}
6073
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006074/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006075 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6076 * @hba: per-adapter instance
6077 *
6078 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6079 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6080 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6081 * the hibern8 exit latency.
6082 *
6083 * Returns zero on success, non-zero error value on failure.
6084 */
6085static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6086{
6087 int ret = 0;
6088 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6089
6090 ret = ufshcd_dme_peer_get(hba,
6091 UIC_ARG_MIB_SEL(
6092 RX_MIN_ACTIVATETIME_CAPABILITY,
6093 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6094 &peer_rx_min_activatetime);
6095 if (ret)
6096 goto out;
6097
6098 /* make sure proper unit conversion is applied */
6099 tuned_pa_tactivate =
6100 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6101 / PA_TACTIVATE_TIME_UNIT_US);
6102 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6103 tuned_pa_tactivate);
6104
6105out:
6106 return ret;
6107}
6108
6109/**
6110 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6111 * @hba: per-adapter instance
6112 *
6113 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6114 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6115 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6116 * This optimal value can help reduce the hibern8 exit latency.
6117 *
6118 * Returns zero on success, non-zero error value on failure.
6119 */
6120static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6121{
6122 int ret = 0;
6123 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6124 u32 max_hibern8_time, tuned_pa_hibern8time;
6125
6126 ret = ufshcd_dme_get(hba,
6127 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6128 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6129 &local_tx_hibern8_time_cap);
6130 if (ret)
6131 goto out;
6132
6133 ret = ufshcd_dme_peer_get(hba,
6134 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6135 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6136 &peer_rx_hibern8_time_cap);
6137 if (ret)
6138 goto out;
6139
6140 max_hibern8_time = max(local_tx_hibern8_time_cap,
6141 peer_rx_hibern8_time_cap);
6142 /* make sure proper unit conversion is applied */
6143 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6144 / PA_HIBERN8_TIME_UNIT_US);
6145 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6146 tuned_pa_hibern8time);
6147out:
6148 return ret;
6149}
6150
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006151/**
6152 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6153 * less than device PA_TACTIVATE time.
6154 * @hba: per-adapter instance
6155 *
6156 * Some UFS devices require host PA_TACTIVATE to be lower than device
6157 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6158 * for such devices.
6159 *
6160 * Returns zero on success, non-zero error value on failure.
6161 */
6162static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6163{
6164 int ret = 0;
6165 u32 granularity, peer_granularity;
6166 u32 pa_tactivate, peer_pa_tactivate;
6167 u32 pa_tactivate_us, peer_pa_tactivate_us;
6168 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6169
6170 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6171 &granularity);
6172 if (ret)
6173 goto out;
6174
6175 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6176 &peer_granularity);
6177 if (ret)
6178 goto out;
6179
6180 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6181 (granularity > PA_GRANULARITY_MAX_VAL)) {
6182 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6183 __func__, granularity);
6184 return -EINVAL;
6185 }
6186
6187 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6188 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6189 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6190 __func__, peer_granularity);
6191 return -EINVAL;
6192 }
6193
6194 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6195 if (ret)
6196 goto out;
6197
6198 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6199 &peer_pa_tactivate);
6200 if (ret)
6201 goto out;
6202
6203 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6204 peer_pa_tactivate_us = peer_pa_tactivate *
6205 gran_to_us_table[peer_granularity - 1];
6206
6207 if (pa_tactivate_us > peer_pa_tactivate_us) {
6208 u32 new_peer_pa_tactivate;
6209
6210 new_peer_pa_tactivate = pa_tactivate_us /
6211 gran_to_us_table[peer_granularity - 1];
6212 new_peer_pa_tactivate++;
6213 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6214 new_peer_pa_tactivate);
6215 }
6216
6217out:
6218 return ret;
6219}
6220
Yaniv Gardi37113102016-03-10 17:37:16 +02006221static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
6222{
6223 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6224 ufshcd_tune_pa_tactivate(hba);
6225 ufshcd_tune_pa_hibern8time(hba);
6226 }
6227
6228 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6229 /* set 1ms timeout for PA_TACTIVATE */
6230 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006231
6232 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6233 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08006234
6235 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006236}
6237
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006238static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6239{
6240 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
6241
6242 hba->ufs_stats.hibern8_exit_cnt = 0;
6243 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
6244
6245 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
6246 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
6247 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
6248 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
6249 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
Gilad Broner7fabb772017-02-03 16:56:50 -08006250
6251 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006252}
6253
Yaniv Gardi37113102016-03-10 17:37:16 +02006254/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006255 * ufshcd_probe_hba - probe hba to detect device and initialize
6256 * @hba: per-adapter instance
6257 *
6258 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306259 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006260static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306261{
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006262 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306263 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006264 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306265
6266 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306267 if (ret)
6268 goto out;
6269
Yaniv Gardiafdfff52016-03-10 17:37:15 +02006270 /* set the default level for urgent bkops */
6271 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
6272 hba->is_urgent_bkops_lvl_checked = false;
6273
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006274 /* Debug counters initialization */
6275 ufshcd_clear_dbg_ufs_stats(hba);
6276
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006277 /* UniPro link is active now */
6278 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05306279
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306280 ret = ufshcd_verify_dev_init(hba);
6281 if (ret)
6282 goto out;
6283
Dolev Raviv68078d52013-07-30 00:35:58 +05306284 ret = ufshcd_complete_dev_init(hba);
6285 if (ret)
6286 goto out;
6287
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006288 ret = ufs_get_device_desc(hba, &card);
6289 if (ret) {
6290 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
6291 __func__, ret);
6292 goto out;
6293 }
6294
6295 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02006296 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006297
6298 ret = ufshcd_set_vccq_rail_unused(hba,
6299 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
6300 if (ret)
6301 goto out;
6302
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006303 /* UFS device is also active now */
6304 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306305 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006306 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306307
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006308 if (ufshcd_get_max_pwr_mode(hba)) {
6309 dev_err(hba->dev,
6310 "%s: Failed getting max supported power mode\n",
6311 __func__);
6312 } else {
6313 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006314 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006315 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
6316 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006317 goto out;
6318 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006319 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006320
Yaniv Gardi53c12d02016-02-01 15:02:45 +02006321 /* set the state as operational after switching to desired gear */
6322 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006323 /*
6324 * If we are in error handling context or in power management callbacks
6325 * context, no need to scan the host
6326 */
6327 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6328 bool flag;
6329
6330 /* clear any previous UFS device information */
6331 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02006332 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6333 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006334 hba->dev_info.f_power_on_wp_en = flag;
6335
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006336 if (!hba->is_init_prefetch)
6337 ufshcd_init_icc_levels(hba);
6338
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006339 /* Add required well known logical units to scsi mid layer */
6340 if (ufshcd_scsi_add_wlus(hba))
6341 goto out;
6342
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006343 /* Initialize devfreq after UFS device is detected */
6344 if (ufshcd_is_clkscaling_supported(hba)) {
6345 memcpy(&hba->clk_scaling.saved_pwr_info.info,
6346 &hba->pwr_info,
6347 sizeof(struct ufs_pa_layer_attr));
6348 hba->clk_scaling.saved_pwr_info.is_valid = true;
6349 if (!hba->devfreq) {
6350 hba->devfreq = devm_devfreq_add_device(hba->dev,
6351 &ufs_devfreq_profile,
6352 "simple_ondemand",
6353 NULL);
6354 if (IS_ERR(hba->devfreq)) {
6355 ret = PTR_ERR(hba->devfreq);
6356 dev_err(hba->dev, "Unable to register with devfreq %d\n",
6357 ret);
6358 goto out;
6359 }
6360 }
6361 hba->clk_scaling.is_allowed = true;
6362 }
6363
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306364 scsi_scan_host(hba->host);
6365 pm_runtime_put_sync(hba->dev);
6366 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006367
6368 if (!hba->is_init_prefetch)
6369 hba->is_init_prefetch = true;
6370
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306371out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006372 /*
6373 * If we failed to initialize the device or the device is not
6374 * present, turn off the power/clocks etc.
6375 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006376 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6377 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006378 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006379 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006380
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006381 trace_ufshcd_init(dev_name(hba->dev), ret,
6382 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08006383 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006384 return ret;
6385}
6386
6387/**
6388 * ufshcd_async_scan - asynchronous execution for probing hba
6389 * @data: data pointer to pass to this function
6390 * @cookie: cookie data
6391 */
6392static void ufshcd_async_scan(void *data, async_cookie_t cookie)
6393{
6394 struct ufs_hba *hba = (struct ufs_hba *)data;
6395
6396 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306397}
6398
Yaniv Gardif550c652016-03-10 17:37:07 +02006399static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
6400{
6401 unsigned long flags;
6402 struct Scsi_Host *host;
6403 struct ufs_hba *hba;
6404 int index;
6405 bool found = false;
6406
6407 if (!scmd || !scmd->device || !scmd->device->host)
6408 return BLK_EH_NOT_HANDLED;
6409
6410 host = scmd->device->host;
6411 hba = shost_priv(host);
6412 if (!hba)
6413 return BLK_EH_NOT_HANDLED;
6414
6415 spin_lock_irqsave(host->host_lock, flags);
6416
6417 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
6418 if (hba->lrb[index].cmd == scmd) {
6419 found = true;
6420 break;
6421 }
6422 }
6423
6424 spin_unlock_irqrestore(host->host_lock, flags);
6425
6426 /*
6427 * Bypass SCSI error handling and reset the block layer timer if this
6428 * SCSI command was not actually dispatched to UFS driver, otherwise
6429 * let SCSI layer handle the error as usual.
6430 */
6431 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
6432}
6433
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306434static struct scsi_host_template ufshcd_driver_template = {
6435 .module = THIS_MODULE,
6436 .name = UFSHCD,
6437 .proc_name = UFSHCD,
6438 .queuecommand = ufshcd_queuecommand,
6439 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09006440 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306441 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03006442 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306443 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306444 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
6445 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02006446 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306447 .this_id = -1,
6448 .sg_tablesize = SG_ALL,
6449 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
6450 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006451 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006452 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306453};
6454
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006455static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
6456 int ua)
6457{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006458 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006459
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006460 if (!vreg)
6461 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006462
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006463 ret = regulator_set_load(vreg->reg, ua);
6464 if (ret < 0) {
6465 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
6466 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006467 }
6468
6469 return ret;
6470}
6471
6472static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
6473 struct ufs_vreg *vreg)
6474{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006475 if (!vreg)
6476 return 0;
6477 else if (vreg->unused)
6478 return 0;
6479 else
6480 return ufshcd_config_vreg_load(hba->dev, vreg,
6481 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006482}
6483
6484static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
6485 struct ufs_vreg *vreg)
6486{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006487 if (!vreg)
6488 return 0;
6489 else if (vreg->unused)
6490 return 0;
6491 else
6492 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006493}
6494
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006495static int ufshcd_config_vreg(struct device *dev,
6496 struct ufs_vreg *vreg, bool on)
6497{
6498 int ret = 0;
6499 struct regulator *reg = vreg->reg;
6500 const char *name = vreg->name;
6501 int min_uV, uA_load;
6502
6503 BUG_ON(!vreg);
6504
6505 if (regulator_count_voltages(reg) > 0) {
6506 min_uV = on ? vreg->min_uV : 0;
6507 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
6508 if (ret) {
6509 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
6510 __func__, name, ret);
6511 goto out;
6512 }
6513
6514 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006515 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
6516 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006517 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006518 }
6519out:
6520 return ret;
6521}
6522
6523static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
6524{
6525 int ret = 0;
6526
Yaniv Gardi60f01872016-03-10 17:37:11 +02006527 if (!vreg)
6528 goto out;
6529 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006530 goto out;
6531
6532 ret = ufshcd_config_vreg(dev, vreg, true);
6533 if (!ret)
6534 ret = regulator_enable(vreg->reg);
6535
6536 if (!ret)
6537 vreg->enabled = true;
6538 else
6539 dev_err(dev, "%s: %s enable failed, err=%d\n",
6540 __func__, vreg->name, ret);
6541out:
6542 return ret;
6543}
6544
6545static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
6546{
6547 int ret = 0;
6548
Yaniv Gardi60f01872016-03-10 17:37:11 +02006549 if (!vreg)
6550 goto out;
6551 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006552 goto out;
6553
6554 ret = regulator_disable(vreg->reg);
6555
6556 if (!ret) {
6557 /* ignore errors on applying disable config */
6558 ufshcd_config_vreg(dev, vreg, false);
6559 vreg->enabled = false;
6560 } else {
6561 dev_err(dev, "%s: %s disable failed, err=%d\n",
6562 __func__, vreg->name, ret);
6563 }
6564out:
6565 return ret;
6566}
6567
6568static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
6569{
6570 int ret = 0;
6571 struct device *dev = hba->dev;
6572 struct ufs_vreg_info *info = &hba->vreg_info;
6573
6574 if (!info)
6575 goto out;
6576
6577 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
6578 if (ret)
6579 goto out;
6580
6581 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
6582 if (ret)
6583 goto out;
6584
6585 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
6586 if (ret)
6587 goto out;
6588
6589out:
6590 if (ret) {
6591 ufshcd_toggle_vreg(dev, info->vccq2, false);
6592 ufshcd_toggle_vreg(dev, info->vccq, false);
6593 ufshcd_toggle_vreg(dev, info->vcc, false);
6594 }
6595 return ret;
6596}
6597
Raviv Shvili6a771a62014-09-25 15:32:24 +03006598static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
6599{
6600 struct ufs_vreg_info *info = &hba->vreg_info;
6601
6602 if (info)
6603 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
6604
6605 return 0;
6606}
6607
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006608static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
6609{
6610 int ret = 0;
6611
6612 if (!vreg)
6613 goto out;
6614
6615 vreg->reg = devm_regulator_get(dev, vreg->name);
6616 if (IS_ERR(vreg->reg)) {
6617 ret = PTR_ERR(vreg->reg);
6618 dev_err(dev, "%s: %s get failed, err=%d\n",
6619 __func__, vreg->name, ret);
6620 }
6621out:
6622 return ret;
6623}
6624
6625static int ufshcd_init_vreg(struct ufs_hba *hba)
6626{
6627 int ret = 0;
6628 struct device *dev = hba->dev;
6629 struct ufs_vreg_info *info = &hba->vreg_info;
6630
6631 if (!info)
6632 goto out;
6633
6634 ret = ufshcd_get_vreg(dev, info->vcc);
6635 if (ret)
6636 goto out;
6637
6638 ret = ufshcd_get_vreg(dev, info->vccq);
6639 if (ret)
6640 goto out;
6641
6642 ret = ufshcd_get_vreg(dev, info->vccq2);
6643out:
6644 return ret;
6645}
6646
Raviv Shvili6a771a62014-09-25 15:32:24 +03006647static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
6648{
6649 struct ufs_vreg_info *info = &hba->vreg_info;
6650
6651 if (info)
6652 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
6653
6654 return 0;
6655}
6656
Yaniv Gardi60f01872016-03-10 17:37:11 +02006657static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
6658{
6659 int ret = 0;
6660 struct ufs_vreg_info *info = &hba->vreg_info;
6661
6662 if (!info)
6663 goto out;
6664 else if (!info->vccq)
6665 goto out;
6666
6667 if (unused) {
6668 /* shut off the rail here */
6669 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
6670 /*
6671 * Mark this rail as no longer used, so it doesn't get enabled
6672 * later by mistake
6673 */
6674 if (!ret)
6675 info->vccq->unused = true;
6676 } else {
6677 /*
6678 * rail should have been already enabled hence just make sure
6679 * that unused flag is cleared.
6680 */
6681 info->vccq->unused = false;
6682 }
6683out:
6684 return ret;
6685}
6686
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006687static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
6688 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006689{
6690 int ret = 0;
6691 struct ufs_clk_info *clki;
6692 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006693 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006694 ktime_t start = ktime_get();
6695 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006696
6697 if (!head || list_empty(head))
6698 goto out;
6699
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006700 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
6701 if (ret)
6702 return ret;
6703
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006704 list_for_each_entry(clki, head, list) {
6705 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006706 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
6707 continue;
6708
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006709 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006710 if (on && !clki->enabled) {
6711 ret = clk_prepare_enable(clki->clk);
6712 if (ret) {
6713 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
6714 __func__, clki->name, ret);
6715 goto out;
6716 }
6717 } else if (!on && clki->enabled) {
6718 clk_disable_unprepare(clki->clk);
6719 }
6720 clki->enabled = on;
6721 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
6722 clki->name, on ? "en" : "dis");
6723 }
6724 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006725
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006726 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
6727 if (ret)
6728 return ret;
6729
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006730out:
6731 if (ret) {
6732 list_for_each_entry(clki, head, list) {
6733 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
6734 clk_disable_unprepare(clki->clk);
6735 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006736 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006737 spin_lock_irqsave(hba->host->host_lock, flags);
6738 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006739 trace_ufshcd_clk_gating(dev_name(hba->dev),
6740 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006741 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006742 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006743
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006744 if (clk_state_changed)
6745 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
6746 (on ? "on" : "off"),
6747 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006748 return ret;
6749}
6750
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006751static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
6752{
6753 return __ufshcd_setup_clocks(hba, on, false);
6754}
6755
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006756static int ufshcd_init_clocks(struct ufs_hba *hba)
6757{
6758 int ret = 0;
6759 struct ufs_clk_info *clki;
6760 struct device *dev = hba->dev;
6761 struct list_head *head = &hba->clk_list_head;
6762
6763 if (!head || list_empty(head))
6764 goto out;
6765
6766 list_for_each_entry(clki, head, list) {
6767 if (!clki->name)
6768 continue;
6769
6770 clki->clk = devm_clk_get(dev, clki->name);
6771 if (IS_ERR(clki->clk)) {
6772 ret = PTR_ERR(clki->clk);
6773 dev_err(dev, "%s: %s clk get failed, %d\n",
6774 __func__, clki->name, ret);
6775 goto out;
6776 }
6777
6778 if (clki->max_freq) {
6779 ret = clk_set_rate(clki->clk, clki->max_freq);
6780 if (ret) {
6781 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6782 __func__, clki->name,
6783 clki->max_freq, ret);
6784 goto out;
6785 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03006786 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006787 }
6788 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
6789 clki->name, clk_get_rate(clki->clk));
6790 }
6791out:
6792 return ret;
6793}
6794
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006795static int ufshcd_variant_hba_init(struct ufs_hba *hba)
6796{
6797 int err = 0;
6798
6799 if (!hba->vops)
6800 goto out;
6801
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006802 err = ufshcd_vops_init(hba);
6803 if (err)
6804 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006805
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006806 err = ufshcd_vops_setup_regulators(hba, true);
6807 if (err)
6808 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006809
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006810 goto out;
6811
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006812out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006813 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006814out:
6815 if (err)
6816 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006817 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006818 return err;
6819}
6820
6821static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
6822{
6823 if (!hba->vops)
6824 return;
6825
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006826 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006827
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006828 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006829}
6830
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006831static int ufshcd_hba_init(struct ufs_hba *hba)
6832{
6833 int err;
6834
Raviv Shvili6a771a62014-09-25 15:32:24 +03006835 /*
6836 * Handle host controller power separately from the UFS device power
6837 * rails as it will help controlling the UFS host controller power
6838 * collapse easily which is different than UFS device power collapse.
6839 * Also, enable the host controller power before we go ahead with rest
6840 * of the initialization here.
6841 */
6842 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006843 if (err)
6844 goto out;
6845
Raviv Shvili6a771a62014-09-25 15:32:24 +03006846 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006847 if (err)
6848 goto out;
6849
Raviv Shvili6a771a62014-09-25 15:32:24 +03006850 err = ufshcd_init_clocks(hba);
6851 if (err)
6852 goto out_disable_hba_vreg;
6853
6854 err = ufshcd_setup_clocks(hba, true);
6855 if (err)
6856 goto out_disable_hba_vreg;
6857
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006858 err = ufshcd_init_vreg(hba);
6859 if (err)
6860 goto out_disable_clks;
6861
6862 err = ufshcd_setup_vreg(hba, true);
6863 if (err)
6864 goto out_disable_clks;
6865
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006866 err = ufshcd_variant_hba_init(hba);
6867 if (err)
6868 goto out_disable_vreg;
6869
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006870 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006871 goto out;
6872
6873out_disable_vreg:
6874 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006875out_disable_clks:
6876 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03006877out_disable_hba_vreg:
6878 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006879out:
6880 return err;
6881}
6882
6883static void ufshcd_hba_exit(struct ufs_hba *hba)
6884{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006885 if (hba->is_powered) {
6886 ufshcd_variant_hba_exit(hba);
6887 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07006888 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006889 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006890 if (hba->devfreq)
6891 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006892 destroy_workqueue(hba->clk_scaling.workq);
6893 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006894 ufshcd_setup_clocks(hba, false);
6895 ufshcd_setup_hba_vreg(hba, false);
6896 hba->is_powered = false;
6897 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006898}
6899
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006900static int
6901ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306902{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006903 unsigned char cmd[6] = {REQUEST_SENSE,
6904 0,
6905 0,
6906 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006907 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006908 0};
6909 char *buffer;
6910 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306911
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006912 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006913 if (!buffer) {
6914 ret = -ENOMEM;
6915 goto out;
6916 }
6917
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01006918 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
6919 UFSHCD_REQ_SENSE_SIZE, NULL, NULL,
6920 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006921 if (ret)
6922 pr_err("%s: failed with err %d\n", __func__, ret);
6923
6924 kfree(buffer);
6925out:
6926 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306927}
6928
6929/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006930 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6931 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306932 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006933 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306934 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006935 * Returns 0 if requested power mode is set successfully
6936 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306937 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006938static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
6939 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306940{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006941 unsigned char cmd[6] = { START_STOP };
6942 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006943 struct scsi_device *sdp;
6944 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006945 int ret;
6946
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006947 spin_lock_irqsave(hba->host->host_lock, flags);
6948 sdp = hba->sdev_ufs_device;
6949 if (sdp) {
6950 ret = scsi_device_get(sdp);
6951 if (!ret && !scsi_device_online(sdp)) {
6952 ret = -ENODEV;
6953 scsi_device_put(sdp);
6954 }
6955 } else {
6956 ret = -ENODEV;
6957 }
6958 spin_unlock_irqrestore(hba->host->host_lock, flags);
6959
6960 if (ret)
6961 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006962
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306963 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006964 * If scsi commands fail, the scsi mid-layer schedules scsi error-
6965 * handling, which would wait for host to be resumed. Since we know
6966 * we are functional while we are here, skip host resume in error
6967 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306968 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006969 hba->host->eh_noresume = 1;
6970 if (hba->wlun_dev_clr_ua) {
6971 ret = ufshcd_send_request_sense(hba, sdp);
6972 if (ret)
6973 goto out;
6974 /* Unit attention condition is cleared now */
6975 hba->wlun_dev_clr_ua = false;
6976 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306977
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006978 cmd[4] = pwr_mode << 4;
6979
6980 /*
6981 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02006982 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006983 * already suspended childs.
6984 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01006985 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
6986 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006987 if (ret) {
6988 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02006989 "START_STOP failed for power mode: %d, result %x\n",
6990 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01006991 if (driver_byte(ret) & DRIVER_SENSE)
6992 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006993 }
6994
6995 if (!ret)
6996 hba->curr_dev_pwr_mode = pwr_mode;
6997out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006998 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006999 hba->host->eh_noresume = 0;
7000 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307001}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307002
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007003static int ufshcd_link_state_transition(struct ufs_hba *hba,
7004 enum uic_link_state req_link_state,
7005 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307006{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007007 int ret = 0;
7008
7009 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307010 return 0;
7011
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007012 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7013 ret = ufshcd_uic_hibern8_enter(hba);
7014 if (!ret)
7015 ufshcd_set_link_hibern8(hba);
7016 else
7017 goto out;
7018 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307019 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007020 * If autobkops is enabled, link can't be turned off because
7021 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307022 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007023 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
7024 (!check_for_bkops || (check_for_bkops &&
7025 !hba->auto_bkops_enabled))) {
7026 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007027 * Let's make sure that link is in low power mode, we are doing
7028 * this currently by putting the link in Hibern8. Otherway to
7029 * put the link in low power mode is to send the DME end point
7030 * to device and then send the DME reset command to local
7031 * unipro. But putting the link in hibern8 is much faster.
7032 */
7033 ret = ufshcd_uic_hibern8_enter(hba);
7034 if (ret)
7035 goto out;
7036 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007037 * Change controller state to "reset state" which
7038 * should also put the link in off/reset state
7039 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007040 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007041 /*
7042 * TODO: Check if we need any delay to make sure that
7043 * controller is reset
7044 */
7045 ufshcd_set_link_off(hba);
7046 }
7047
7048out:
7049 return ret;
7050}
7051
7052static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7053{
7054 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007055 * It seems some UFS devices may keep drawing more than sleep current
7056 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7057 * To avoid this situation, add 2ms delay before putting these UFS
7058 * rails in LPM mode.
7059 */
7060 if (!ufshcd_is_link_active(hba) &&
7061 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
7062 usleep_range(2000, 2100);
7063
7064 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007065 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7066 * power.
7067 *
7068 * If UFS device and link is in OFF state, all power supplies (VCC,
7069 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7070 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7071 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7072 *
7073 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7074 * in low power state which would save some power.
7075 */
7076 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7077 !hba->dev_info.is_lu_power_on_wp) {
7078 ufshcd_setup_vreg(hba, false);
7079 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7080 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7081 if (!ufshcd_is_link_active(hba)) {
7082 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7083 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7084 }
7085 }
7086}
7087
7088static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7089{
7090 int ret = 0;
7091
7092 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7093 !hba->dev_info.is_lu_power_on_wp) {
7094 ret = ufshcd_setup_vreg(hba, true);
7095 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007096 if (!ret && !ufshcd_is_link_active(hba)) {
7097 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7098 if (ret)
7099 goto vcc_disable;
7100 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7101 if (ret)
7102 goto vccq_lpm;
7103 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07007104 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007105 }
7106 goto out;
7107
7108vccq_lpm:
7109 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7110vcc_disable:
7111 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7112out:
7113 return ret;
7114}
7115
7116static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7117{
7118 if (ufshcd_is_link_off(hba))
7119 ufshcd_setup_hba_vreg(hba, false);
7120}
7121
7122static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7123{
7124 if (ufshcd_is_link_off(hba))
7125 ufshcd_setup_hba_vreg(hba, true);
7126}
7127
7128/**
7129 * ufshcd_suspend - helper function for suspend operations
7130 * @hba: per adapter instance
7131 * @pm_op: desired low power operation type
7132 *
7133 * This function will try to put the UFS device and link into low power
7134 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7135 * (System PM level).
7136 *
7137 * If this function is called during shutdown, it will make sure that
7138 * both UFS device and UFS link is powered off.
7139 *
7140 * NOTE: UFS device & link must be active before we enter in this function.
7141 *
7142 * Returns 0 for success and non-zero for failure
7143 */
7144static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7145{
7146 int ret = 0;
7147 enum ufs_pm_level pm_lvl;
7148 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7149 enum uic_link_state req_link_state;
7150
7151 hba->pm_op_in_progress = 1;
7152 if (!ufshcd_is_shutdown_pm(pm_op)) {
7153 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7154 hba->rpm_lvl : hba->spm_lvl;
7155 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7156 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7157 } else {
7158 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7159 req_link_state = UIC_LINK_OFF_STATE;
7160 }
7161
7162 /*
7163 * If we can't transition into any of the low power modes
7164 * just gate the clocks.
7165 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007166 ufshcd_hold(hba, false);
7167 hba->clk_gating.is_suspended = true;
7168
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007169 if (hba->clk_scaling.is_allowed) {
7170 cancel_work_sync(&hba->clk_scaling.suspend_work);
7171 cancel_work_sync(&hba->clk_scaling.resume_work);
7172 ufshcd_suspend_clkscaling(hba);
7173 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007174
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007175 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
7176 req_link_state == UIC_LINK_ACTIVE_STATE) {
7177 goto disable_clks;
7178 }
7179
7180 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
7181 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007182 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007183
7184 /* UFS device & link must be active before we enter in this function */
7185 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
7186 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007187 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007188 }
7189
7190 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03007191 if (ufshcd_can_autobkops_during_suspend(hba)) {
7192 /*
7193 * The device is idle with no requests in the queue,
7194 * allow background operations if bkops status shows
7195 * that performance might be impacted.
7196 */
7197 ret = ufshcd_urgent_bkops(hba);
7198 if (ret)
7199 goto enable_gating;
7200 } else {
7201 /* make sure that auto bkops is disabled */
7202 ufshcd_disable_auto_bkops(hba);
7203 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007204 }
7205
7206 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
7207 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
7208 !ufshcd_is_runtime_pm(pm_op))) {
7209 /* ensure that bkops is disabled */
7210 ufshcd_disable_auto_bkops(hba);
7211 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
7212 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007213 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007214 }
7215
7216 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
7217 if (ret)
7218 goto set_dev_active;
7219
7220 ufshcd_vreg_set_lpm(hba);
7221
7222disable_clks:
7223 /*
7224 * Call vendor specific suspend callback. As these callbacks may access
7225 * vendor specific host controller register space call them before the
7226 * host clocks are ON.
7227 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007228 ret = ufshcd_vops_suspend(hba, pm_op);
7229 if (ret)
7230 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007231
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007232 if (!ufshcd_is_link_active(hba))
7233 ufshcd_setup_clocks(hba, false);
7234 else
7235 /* If link is active, device ref_clk can't be switched off */
7236 __ufshcd_setup_clocks(hba, false, true);
7237
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007238 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007239 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007240 /*
7241 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007242 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007243 */
7244 ufshcd_disable_irq(hba);
7245 /* Put the host controller in low power mode if possible */
7246 ufshcd_hba_vreg_set_lpm(hba);
7247 goto out;
7248
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007249set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007250 if (hba->clk_scaling.is_allowed)
7251 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007252 ufshcd_vreg_set_hpm(hba);
7253 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
7254 ufshcd_set_link_active(hba);
7255 else if (ufshcd_is_link_off(hba))
7256 ufshcd_host_reset_and_restore(hba);
7257set_dev_active:
7258 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
7259 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007260enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007261 if (hba->clk_scaling.is_allowed)
7262 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007263 hba->clk_gating.is_suspended = false;
7264 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007265out:
7266 hba->pm_op_in_progress = 0;
7267 return ret;
7268}
7269
7270/**
7271 * ufshcd_resume - helper function for resume operations
7272 * @hba: per adapter instance
7273 * @pm_op: runtime PM or system PM
7274 *
7275 * This function basically brings the UFS device, UniPro link and controller
7276 * to active state.
7277 *
7278 * Returns 0 for success and non-zero for failure
7279 */
7280static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7281{
7282 int ret;
7283 enum uic_link_state old_link_state;
7284
7285 hba->pm_op_in_progress = 1;
7286 old_link_state = hba->uic_link_state;
7287
7288 ufshcd_hba_vreg_set_hpm(hba);
7289 /* Make sure clocks are enabled before accessing controller */
7290 ret = ufshcd_setup_clocks(hba, true);
7291 if (ret)
7292 goto out;
7293
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007294 /* enable the host irq as host controller would be active soon */
7295 ret = ufshcd_enable_irq(hba);
7296 if (ret)
7297 goto disable_irq_and_vops_clks;
7298
7299 ret = ufshcd_vreg_set_hpm(hba);
7300 if (ret)
7301 goto disable_irq_and_vops_clks;
7302
7303 /*
7304 * Call vendor specific resume callback. As these callbacks may access
7305 * vendor specific host controller register space call them when the
7306 * host clocks are ON.
7307 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007308 ret = ufshcd_vops_resume(hba, pm_op);
7309 if (ret)
7310 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007311
7312 if (ufshcd_is_link_hibern8(hba)) {
7313 ret = ufshcd_uic_hibern8_exit(hba);
7314 if (!ret)
7315 ufshcd_set_link_active(hba);
7316 else
7317 goto vendor_suspend;
7318 } else if (ufshcd_is_link_off(hba)) {
7319 ret = ufshcd_host_reset_and_restore(hba);
7320 /*
7321 * ufshcd_host_reset_and_restore() should have already
7322 * set the link state as active
7323 */
7324 if (ret || !ufshcd_is_link_active(hba))
7325 goto vendor_suspend;
7326 }
7327
7328 if (!ufshcd_is_ufs_dev_active(hba)) {
7329 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
7330 if (ret)
7331 goto set_old_link_state;
7332 }
7333
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08007334 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
7335 ufshcd_enable_auto_bkops(hba);
7336 else
7337 /*
7338 * If BKOPs operations are urgently needed at this moment then
7339 * keep auto-bkops enabled or else disable it.
7340 */
7341 ufshcd_urgent_bkops(hba);
7342
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007343 hba->clk_gating.is_suspended = false;
7344
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007345 if (hba->clk_scaling.is_allowed)
7346 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007347
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007348 /* Schedule clock gating in case of no access to UFS device yet */
7349 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007350 goto out;
7351
7352set_old_link_state:
7353 ufshcd_link_state_transition(hba, old_link_state, 0);
7354vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007355 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007356disable_vreg:
7357 ufshcd_vreg_set_lpm(hba);
7358disable_irq_and_vops_clks:
7359 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007360 if (hba->clk_scaling.is_allowed)
7361 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007362 ufshcd_setup_clocks(hba, false);
7363out:
7364 hba->pm_op_in_progress = 0;
7365 return ret;
7366}
7367
7368/**
7369 * ufshcd_system_suspend - system suspend routine
7370 * @hba: per adapter instance
7371 * @pm_op: runtime PM or system PM
7372 *
7373 * Check the description of ufshcd_suspend() function for more details.
7374 *
7375 * Returns 0 for success and non-zero for failure
7376 */
7377int ufshcd_system_suspend(struct ufs_hba *hba)
7378{
7379 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007380 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007381
7382 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03007383 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007384
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007385 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
7386 hba->curr_dev_pwr_mode) &&
7387 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
7388 hba->uic_link_state))
7389 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007390
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007391 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007392 /*
7393 * UFS device and/or UFS link low power states during runtime
7394 * suspend seems to be different than what is expected during
7395 * system suspend. Hence runtime resume the devic & link and
7396 * let the system suspend low power states to take effect.
7397 * TODO: If resume takes longer time, we might have optimize
7398 * it in future by not resuming everything if possible.
7399 */
7400 ret = ufshcd_runtime_resume(hba);
7401 if (ret)
7402 goto out;
7403 }
7404
7405 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
7406out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007407 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
7408 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007409 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03007410 if (!ret)
7411 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007412 return ret;
7413}
7414EXPORT_SYMBOL(ufshcd_system_suspend);
7415
7416/**
7417 * ufshcd_system_resume - system resume routine
7418 * @hba: per adapter instance
7419 *
7420 * Returns 0 for success and non-zero for failure
7421 */
7422
7423int ufshcd_system_resume(struct ufs_hba *hba)
7424{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007425 int ret = 0;
7426 ktime_t start = ktime_get();
7427
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007428 if (!hba)
7429 return -EINVAL;
7430
7431 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007432 /*
7433 * Let the runtime resume take care of resuming
7434 * if runtime suspended.
7435 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007436 goto out;
7437 else
7438 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
7439out:
7440 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
7441 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007442 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007443 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007444}
7445EXPORT_SYMBOL(ufshcd_system_resume);
7446
7447/**
7448 * ufshcd_runtime_suspend - runtime suspend routine
7449 * @hba: per adapter instance
7450 *
7451 * Check the description of ufshcd_suspend() function for more details.
7452 *
7453 * Returns 0 for success and non-zero for failure
7454 */
7455int ufshcd_runtime_suspend(struct ufs_hba *hba)
7456{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007457 int ret = 0;
7458 ktime_t start = ktime_get();
7459
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007460 if (!hba)
7461 return -EINVAL;
7462
7463 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007464 goto out;
7465 else
7466 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
7467out:
7468 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
7469 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007470 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007471 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307472}
7473EXPORT_SYMBOL(ufshcd_runtime_suspend);
7474
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007475/**
7476 * ufshcd_runtime_resume - runtime resume routine
7477 * @hba: per adapter instance
7478 *
7479 * This function basically brings the UFS device, UniPro link and controller
7480 * to active state. Following operations are done in this function:
7481 *
7482 * 1. Turn on all the controller related clocks
7483 * 2. Bring the UniPro link out of Hibernate state
7484 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
7485 * to active state.
7486 * 4. If auto-bkops is enabled on the device, disable it.
7487 *
7488 * So following would be the possible power state after this function return
7489 * successfully:
7490 * S1: UFS device in Active state with VCC rail ON
7491 * UniPro link in Active state
7492 * All the UFS/UniPro controller clocks are ON
7493 *
7494 * Returns 0 for success and non-zero for failure
7495 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307496int ufshcd_runtime_resume(struct ufs_hba *hba)
7497{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007498 int ret = 0;
7499 ktime_t start = ktime_get();
7500
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007501 if (!hba)
7502 return -EINVAL;
7503
7504 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007505 goto out;
7506 else
7507 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
7508out:
7509 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
7510 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007511 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007512 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307513}
7514EXPORT_SYMBOL(ufshcd_runtime_resume);
7515
7516int ufshcd_runtime_idle(struct ufs_hba *hba)
7517{
7518 return 0;
7519}
7520EXPORT_SYMBOL(ufshcd_runtime_idle);
7521
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -08007522static inline ssize_t ufshcd_pm_lvl_store(struct device *dev,
7523 struct device_attribute *attr,
7524 const char *buf, size_t count,
7525 bool rpm)
7526{
7527 struct ufs_hba *hba = dev_get_drvdata(dev);
7528 unsigned long flags, value;
7529
7530 if (kstrtoul(buf, 0, &value))
7531 return -EINVAL;
7532
7533 if ((value < UFS_PM_LVL_0) || (value >= UFS_PM_LVL_MAX))
7534 return -EINVAL;
7535
7536 spin_lock_irqsave(hba->host->host_lock, flags);
7537 if (rpm)
7538 hba->rpm_lvl = value;
7539 else
7540 hba->spm_lvl = value;
7541 spin_unlock_irqrestore(hba->host->host_lock, flags);
7542 return count;
7543}
7544
7545static ssize_t ufshcd_rpm_lvl_show(struct device *dev,
7546 struct device_attribute *attr, char *buf)
7547{
7548 struct ufs_hba *hba = dev_get_drvdata(dev);
7549 int curr_len;
7550 u8 lvl;
7551
7552 curr_len = snprintf(buf, PAGE_SIZE,
7553 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
7554 hba->rpm_lvl,
7555 ufschd_ufs_dev_pwr_mode_to_string(
7556 ufs_pm_lvl_states[hba->rpm_lvl].dev_state),
7557 ufschd_uic_link_state_to_string(
7558 ufs_pm_lvl_states[hba->rpm_lvl].link_state));
7559
7560 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7561 "\nAll available Runtime PM levels info:\n");
7562 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
7563 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7564 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
7565 lvl,
7566 ufschd_ufs_dev_pwr_mode_to_string(
7567 ufs_pm_lvl_states[lvl].dev_state),
7568 ufschd_uic_link_state_to_string(
7569 ufs_pm_lvl_states[lvl].link_state));
7570
7571 return curr_len;
7572}
7573
7574static ssize_t ufshcd_rpm_lvl_store(struct device *dev,
7575 struct device_attribute *attr, const char *buf, size_t count)
7576{
7577 return ufshcd_pm_lvl_store(dev, attr, buf, count, true);
7578}
7579
7580static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba)
7581{
7582 hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show;
7583 hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store;
7584 sysfs_attr_init(&hba->rpm_lvl_attr.attr);
7585 hba->rpm_lvl_attr.attr.name = "rpm_lvl";
7586 hba->rpm_lvl_attr.attr.mode = 0644;
7587 if (device_create_file(hba->dev, &hba->rpm_lvl_attr))
7588 dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n");
7589}
7590
7591static ssize_t ufshcd_spm_lvl_show(struct device *dev,
7592 struct device_attribute *attr, char *buf)
7593{
7594 struct ufs_hba *hba = dev_get_drvdata(dev);
7595 int curr_len;
7596 u8 lvl;
7597
7598 curr_len = snprintf(buf, PAGE_SIZE,
7599 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
7600 hba->spm_lvl,
7601 ufschd_ufs_dev_pwr_mode_to_string(
7602 ufs_pm_lvl_states[hba->spm_lvl].dev_state),
7603 ufschd_uic_link_state_to_string(
7604 ufs_pm_lvl_states[hba->spm_lvl].link_state));
7605
7606 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7607 "\nAll available System PM levels info:\n");
7608 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
7609 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7610 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
7611 lvl,
7612 ufschd_ufs_dev_pwr_mode_to_string(
7613 ufs_pm_lvl_states[lvl].dev_state),
7614 ufschd_uic_link_state_to_string(
7615 ufs_pm_lvl_states[lvl].link_state));
7616
7617 return curr_len;
7618}
7619
7620static ssize_t ufshcd_spm_lvl_store(struct device *dev,
7621 struct device_attribute *attr, const char *buf, size_t count)
7622{
7623 return ufshcd_pm_lvl_store(dev, attr, buf, count, false);
7624}
7625
7626static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba)
7627{
7628 hba->spm_lvl_attr.show = ufshcd_spm_lvl_show;
7629 hba->spm_lvl_attr.store = ufshcd_spm_lvl_store;
7630 sysfs_attr_init(&hba->spm_lvl_attr.attr);
7631 hba->spm_lvl_attr.attr.name = "spm_lvl";
7632 hba->spm_lvl_attr.attr.mode = 0644;
7633 if (device_create_file(hba->dev, &hba->spm_lvl_attr))
7634 dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n");
7635}
7636
7637static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba)
7638{
7639 ufshcd_add_rpm_lvl_sysfs_nodes(hba);
7640 ufshcd_add_spm_lvl_sysfs_nodes(hba);
7641}
7642
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307643/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007644 * ufshcd_shutdown - shutdown routine
7645 * @hba: per adapter instance
7646 *
7647 * This function would power off both UFS device and UFS link.
7648 *
7649 * Returns 0 always to allow force shutdown even in case of errors.
7650 */
7651int ufshcd_shutdown(struct ufs_hba *hba)
7652{
7653 int ret = 0;
7654
7655 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
7656 goto out;
7657
7658 if (pm_runtime_suspended(hba->dev)) {
7659 ret = ufshcd_runtime_resume(hba);
7660 if (ret)
7661 goto out;
7662 }
7663
7664 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
7665out:
7666 if (ret)
7667 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
7668 /* allow force shutdown even in case of errors */
7669 return 0;
7670}
7671EXPORT_SYMBOL(ufshcd_shutdown);
7672
7673/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307674 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307675 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307676 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307677 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307678void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307679{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05307680 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307681 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307682 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02007683 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307684
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007685 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007686 if (ufshcd_is_clkscaling_supported(hba))
7687 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007688 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307689}
7690EXPORT_SYMBOL_GPL(ufshcd_remove);
7691
7692/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02007693 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
7694 * @hba: pointer to Host Bus Adapter (HBA)
7695 */
7696void ufshcd_dealloc_host(struct ufs_hba *hba)
7697{
7698 scsi_host_put(hba->host);
7699}
7700EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
7701
7702/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007703 * ufshcd_set_dma_mask - Set dma mask based on the controller
7704 * addressing capability
7705 * @hba: per adapter instance
7706 *
7707 * Returns 0 for success, non-zero for failure
7708 */
7709static int ufshcd_set_dma_mask(struct ufs_hba *hba)
7710{
7711 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
7712 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
7713 return 0;
7714 }
7715 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
7716}
7717
7718/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007719 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307720 * @dev: pointer to device handle
7721 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307722 * Returns 0 on success, non-zero value on failure
7723 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007724int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307725{
7726 struct Scsi_Host *host;
7727 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007728 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307729
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307730 if (!dev) {
7731 dev_err(dev,
7732 "Invalid memory reference for dev is NULL\n");
7733 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307734 goto out_error;
7735 }
7736
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307737 host = scsi_host_alloc(&ufshcd_driver_template,
7738 sizeof(struct ufs_hba));
7739 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307740 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307741 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307742 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307743 }
7744 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307745 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307746 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007747 *hba_handle = hba;
7748
7749out_error:
7750 return err;
7751}
7752EXPORT_SYMBOL(ufshcd_alloc_host);
7753
7754/**
7755 * ufshcd_init - Driver initialization routine
7756 * @hba: per-adapter instance
7757 * @mmio_base: base register address
7758 * @irq: Interrupt line of device
7759 * Returns 0 on success, non-zero value on failure
7760 */
7761int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
7762{
7763 int err;
7764 struct Scsi_Host *host = hba->host;
7765 struct device *dev = hba->dev;
7766
7767 if (!mmio_base) {
7768 dev_err(hba->dev,
7769 "Invalid memory reference for mmio_base is NULL\n");
7770 err = -ENODEV;
7771 goto out_error;
7772 }
7773
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307774 hba->mmio_base = mmio_base;
7775 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307776
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007777 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007778 if (err)
7779 goto out_error;
7780
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307781 /* Read capabilities registers */
7782 ufshcd_hba_capabilities(hba);
7783
7784 /* Get UFS version supported by the controller */
7785 hba->ufs_version = ufshcd_get_ufs_version(hba);
7786
Yaniv Gardic01848c2016-12-05 19:25:02 -08007787 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
7788 (hba->ufs_version != UFSHCI_VERSION_11) &&
7789 (hba->ufs_version != UFSHCI_VERSION_20) &&
7790 (hba->ufs_version != UFSHCI_VERSION_21))
7791 dev_err(hba->dev, "invalid UFS version 0x%x\n",
7792 hba->ufs_version);
7793
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307794 /* Get Interrupt bit mask per version */
7795 hba->intr_mask = ufshcd_get_intr_mask(hba);
7796
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007797 err = ufshcd_set_dma_mask(hba);
7798 if (err) {
7799 dev_err(hba->dev, "set dma mask failed\n");
7800 goto out_disable;
7801 }
7802
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307803 /* Allocate memory for host memory space */
7804 err = ufshcd_memory_alloc(hba);
7805 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307806 dev_err(hba->dev, "Memory allocation failed\n");
7807 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307808 }
7809
7810 /* Configure LRB */
7811 ufshcd_host_memory_configure(hba);
7812
7813 host->can_queue = hba->nutrs;
7814 host->cmd_per_lun = hba->nutrs;
7815 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03007816 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307817 host->max_channel = UFSHCD_MAX_CHANNEL;
7818 host->unique_id = host->host_no;
7819 host->max_cmd_len = MAX_CDB_SIZE;
7820
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007821 hba->max_pwr_info.is_valid = false;
7822
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307823 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05307824 init_waitqueue_head(&hba->tm_wq);
7825 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307826
7827 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05307828 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307829 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307830
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307831 /* Initialize UIC command mutex */
7832 mutex_init(&hba->uic_cmd_mutex);
7833
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307834 /* Initialize mutex for device management commands */
7835 mutex_init(&hba->dev_cmd.lock);
7836
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08007837 init_rwsem(&hba->clk_scaling_lock);
7838
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307839 /* Initialize device management tag acquire wait queue */
7840 init_waitqueue_head(&hba->dev_cmd.tag_wq);
7841
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007842 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02007843
7844 /*
7845 * In order to avoid any spurious interrupt immediately after
7846 * registering UFS controller interrupt handler, clear any pending UFS
7847 * interrupt status and disable all the UFS interrupts.
7848 */
7849 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
7850 REG_INTERRUPT_STATUS);
7851 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
7852 /*
7853 * Make sure that UFS interrupts are disabled and any pending interrupt
7854 * status is cleared before registering UFS interrupt handler.
7855 */
7856 mb();
7857
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307858 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09007859 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307860 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307861 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007862 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007863 } else {
7864 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307865 }
7866
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307867 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307868 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307869 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007870 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307871 }
7872
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307873 /* Host controller enable */
7874 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307875 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307876 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08007877 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08007878 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307879 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307880 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307881
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007882 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007883 char wq_name[sizeof("ufs_clkscaling_00")];
7884
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007885 INIT_WORK(&hba->clk_scaling.suspend_work,
7886 ufshcd_clk_scaling_suspend_work);
7887 INIT_WORK(&hba->clk_scaling.resume_work,
7888 ufshcd_clk_scaling_resume_work);
7889
7890 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d",
7891 host->host_no);
7892 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
7893
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007894 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007895 }
7896
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08007897 /*
7898 * Set the default power management level for runtime and system PM.
7899 * Default power saving mode is to keep UFS link in Hibern8 state
7900 * and UFS device in sleep state.
7901 */
7902 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7903 UFS_SLEEP_PWR_MODE,
7904 UIC_LINK_HIBERN8_STATE);
7905 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7906 UFS_SLEEP_PWR_MODE,
7907 UIC_LINK_HIBERN8_STATE);
7908
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05307909 /* Hold auto suspend until async scan completes */
7910 pm_runtime_get_sync(dev);
7911
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007912 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007913 * We are assuming that device wasn't put in sleep/power-down
7914 * state exclusively during the boot stage before kernel.
7915 * This assumption helps avoid doing link startup twice during
7916 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007917 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007918 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007919
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307920 async_schedule(ufshcd_async_scan, hba);
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -08007921 ufshcd_add_sysfs_nodes(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307922
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307923 return 0;
7924
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307925out_remove_scsi_host:
7926 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007927exit_gating:
7928 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307929out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007930 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007931 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307932out_error:
7933 return err;
7934}
7935EXPORT_SYMBOL_GPL(ufshcd_init);
7936
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307937MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7938MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05307939MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307940MODULE_LICENSE("GPL");
7941MODULE_VERSION(UFSHCD_DRIVER_VERSION);