blob: b006f1e51aae291d874090d20bad8715b54357cb [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) \
154 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
155#define ufshcd_eh_in_progress(h) \
156 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
157#define ufshcd_clear_eh_in_progress(h) \
158 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
159
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 Kusumic9e60102017-03-28 16:49:24 +0300839 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? false : true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530840}
841
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -0800842static const char *ufschd_uic_link_state_to_string(
843 enum uic_link_state state)
844{
845 switch (state) {
846 case UIC_LINK_OFF_STATE: return "OFF";
847 case UIC_LINK_ACTIVE_STATE: return "ACTIVE";
848 case UIC_LINK_HIBERN8_STATE: return "HIBERN8";
849 default: return "UNKNOWN";
850 }
851}
852
853static const char *ufschd_ufs_dev_pwr_mode_to_string(
854 enum ufs_dev_pwr_mode state)
855{
856 switch (state) {
857 case UFS_ACTIVE_PWR_MODE: return "ACTIVE";
858 case UFS_SLEEP_PWR_MODE: return "SLEEP";
859 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN";
860 default: return "UNKNOWN";
861 }
862}
863
Yaniv Gardi37113102016-03-10 17:37:16 +0200864u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
865{
866 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
867 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
868 (hba->ufs_version == UFSHCI_VERSION_11))
869 return UFS_UNIPRO_VER_1_41;
870 else
871 return UFS_UNIPRO_VER_1_6;
872}
873EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
874
875static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
876{
877 /*
878 * If both host and device support UniPro ver1.6 or later, PA layer
879 * parameters tuning happens during link startup itself.
880 *
881 * We can manually tune PA layer parameters if either host or device
882 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
883 * logic simple, we will only do manual tuning if local unipro version
884 * doesn't support ver1.6 or later.
885 */
886 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
887 return true;
888 else
889 return false;
890}
891
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -0800892static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
893{
894 int ret = 0;
895 struct ufs_clk_info *clki;
896 struct list_head *head = &hba->clk_list_head;
897 ktime_t start = ktime_get();
898 bool clk_state_changed = false;
899
900 if (!head || list_empty(head))
901 goto out;
902
903 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
904 if (ret)
905 return ret;
906
907 list_for_each_entry(clki, head, list) {
908 if (!IS_ERR_OR_NULL(clki->clk)) {
909 if (scale_up && clki->max_freq) {
910 if (clki->curr_freq == clki->max_freq)
911 continue;
912
913 clk_state_changed = true;
914 ret = clk_set_rate(clki->clk, clki->max_freq);
915 if (ret) {
916 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
917 __func__, clki->name,
918 clki->max_freq, ret);
919 break;
920 }
921 trace_ufshcd_clk_scaling(dev_name(hba->dev),
922 "scaled up", clki->name,
923 clki->curr_freq,
924 clki->max_freq);
925
926 clki->curr_freq = clki->max_freq;
927
928 } else if (!scale_up && clki->min_freq) {
929 if (clki->curr_freq == clki->min_freq)
930 continue;
931
932 clk_state_changed = true;
933 ret = clk_set_rate(clki->clk, clki->min_freq);
934 if (ret) {
935 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
936 __func__, clki->name,
937 clki->min_freq, ret);
938 break;
939 }
940 trace_ufshcd_clk_scaling(dev_name(hba->dev),
941 "scaled down", clki->name,
942 clki->curr_freq,
943 clki->min_freq);
944 clki->curr_freq = clki->min_freq;
945 }
946 }
947 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
948 clki->name, clk_get_rate(clki->clk));
949 }
950
951 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
952
953out:
954 if (clk_state_changed)
955 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
956 (scale_up ? "up" : "down"),
957 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
958 return ret;
959}
960
961/**
962 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
963 * @hba: per adapter instance
964 * @scale_up: True if scaling up and false if scaling down
965 *
966 * Returns true if scaling is required, false otherwise.
967 */
968static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba,
969 bool scale_up)
970{
971 struct ufs_clk_info *clki;
972 struct list_head *head = &hba->clk_list_head;
973
974 if (!head || list_empty(head))
975 return false;
976
977 list_for_each_entry(clki, head, list) {
978 if (!IS_ERR_OR_NULL(clki->clk)) {
979 if (scale_up && clki->max_freq) {
980 if (clki->curr_freq == clki->max_freq)
981 continue;
982 return true;
983 } else if (!scale_up && clki->min_freq) {
984 if (clki->curr_freq == clki->min_freq)
985 continue;
986 return true;
987 }
988 }
989 }
990
991 return false;
992}
993
994static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba,
995 u64 wait_timeout_us)
996{
997 unsigned long flags;
998 int ret = 0;
999 u32 tm_doorbell;
1000 u32 tr_doorbell;
1001 bool timeout = false, do_last_check = false;
1002 ktime_t start;
1003
1004 ufshcd_hold(hba, false);
1005 spin_lock_irqsave(hba->host->host_lock, flags);
1006 /*
1007 * Wait for all the outstanding tasks/transfer requests.
1008 * Verify by checking the doorbell registers are clear.
1009 */
1010 start = ktime_get();
1011 do {
1012 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
1013 ret = -EBUSY;
1014 goto out;
1015 }
1016
1017 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1018 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1019 if (!tm_doorbell && !tr_doorbell) {
1020 timeout = false;
1021 break;
1022 } else if (do_last_check) {
1023 break;
1024 }
1025
1026 spin_unlock_irqrestore(hba->host->host_lock, flags);
1027 schedule();
1028 if (ktime_to_us(ktime_sub(ktime_get(), start)) >
1029 wait_timeout_us) {
1030 timeout = true;
1031 /*
1032 * We might have scheduled out for long time so make
1033 * sure to check if doorbells are cleared by this time
1034 * or not.
1035 */
1036 do_last_check = true;
1037 }
1038 spin_lock_irqsave(hba->host->host_lock, flags);
1039 } while (tm_doorbell || tr_doorbell);
1040
1041 if (timeout) {
1042 dev_err(hba->dev,
1043 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1044 __func__, tm_doorbell, tr_doorbell);
1045 ret = -EBUSY;
1046 }
1047out:
1048 spin_unlock_irqrestore(hba->host->host_lock, flags);
1049 ufshcd_release(hba);
1050 return ret;
1051}
1052
1053/**
1054 * ufshcd_scale_gear - scale up/down UFS gear
1055 * @hba: per adapter instance
1056 * @scale_up: True for scaling up gear and false for scaling down
1057 *
1058 * Returns 0 for success,
1059 * Returns -EBUSY if scaling can't happen at this time
1060 * Returns non-zero for any other errors
1061 */
1062static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up)
1063{
1064 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1065 int ret = 0;
1066 struct ufs_pa_layer_attr new_pwr_info;
1067
1068 if (scale_up) {
1069 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info.info,
1070 sizeof(struct ufs_pa_layer_attr));
1071 } else {
1072 memcpy(&new_pwr_info, &hba->pwr_info,
1073 sizeof(struct ufs_pa_layer_attr));
1074
1075 if (hba->pwr_info.gear_tx > UFS_MIN_GEAR_TO_SCALE_DOWN
1076 || hba->pwr_info.gear_rx > UFS_MIN_GEAR_TO_SCALE_DOWN) {
1077 /* save the current power mode */
1078 memcpy(&hba->clk_scaling.saved_pwr_info.info,
1079 &hba->pwr_info,
1080 sizeof(struct ufs_pa_layer_attr));
1081
1082 /* scale down gear */
1083 new_pwr_info.gear_tx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1084 new_pwr_info.gear_rx = UFS_MIN_GEAR_TO_SCALE_DOWN;
1085 }
1086 }
1087
1088 /* check if the power mode needs to be changed or not? */
1089 ret = ufshcd_change_power_mode(hba, &new_pwr_info);
1090
1091 if (ret)
1092 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1093 __func__, ret,
1094 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx,
1095 new_pwr_info.gear_tx, new_pwr_info.gear_rx);
1096
1097 return ret;
1098}
1099
1100static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba)
1101{
1102 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1103 int ret = 0;
1104 /*
1105 * make sure that there are no outstanding requests when
1106 * clock scaling is in progress
1107 */
1108 scsi_block_requests(hba->host);
1109 down_write(&hba->clk_scaling_lock);
1110 if (ufshcd_wait_for_doorbell_clr(hba, DOORBELL_CLR_TOUT_US)) {
1111 ret = -EBUSY;
1112 up_write(&hba->clk_scaling_lock);
1113 scsi_unblock_requests(hba->host);
1114 }
1115
1116 return ret;
1117}
1118
1119static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba)
1120{
1121 up_write(&hba->clk_scaling_lock);
1122 scsi_unblock_requests(hba->host);
1123}
1124
1125/**
1126 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1127 * @hba: per adapter instance
1128 * @scale_up: True for scaling up and false for scalin down
1129 *
1130 * Returns 0 for success,
1131 * Returns -EBUSY if scaling can't happen at this time
1132 * Returns non-zero for any other errors
1133 */
1134static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up)
1135{
1136 int ret = 0;
1137
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001138 /* let's not get into low power until clock scaling is completed */
1139 ufshcd_hold(hba, false);
1140
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001141 ret = ufshcd_clock_scaling_prepare(hba);
1142 if (ret)
1143 return ret;
1144
1145 /* scale down the gear before scaling down clocks */
1146 if (!scale_up) {
1147 ret = ufshcd_scale_gear(hba, false);
1148 if (ret)
1149 goto out;
1150 }
1151
1152 ret = ufshcd_scale_clks(hba, scale_up);
1153 if (ret) {
1154 if (!scale_up)
1155 ufshcd_scale_gear(hba, true);
1156 goto out;
1157 }
1158
1159 /* scale up the gear after scaling up clocks */
1160 if (scale_up) {
1161 ret = ufshcd_scale_gear(hba, true);
1162 if (ret) {
1163 ufshcd_scale_clks(hba, false);
1164 goto out;
1165 }
1166 }
1167
1168 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
1169
1170out:
1171 ufshcd_clock_scaling_unprepare(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001172 ufshcd_release(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001173 return ret;
1174}
1175
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001176static void ufshcd_clk_scaling_suspend_work(struct work_struct *work)
1177{
1178 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1179 clk_scaling.suspend_work);
1180 unsigned long irq_flags;
1181
1182 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1183 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) {
1184 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1185 return;
1186 }
1187 hba->clk_scaling.is_suspended = true;
1188 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1189
1190 __ufshcd_suspend_clkscaling(hba);
1191}
1192
1193static void ufshcd_clk_scaling_resume_work(struct work_struct *work)
1194{
1195 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1196 clk_scaling.resume_work);
1197 unsigned long irq_flags;
1198
1199 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1200 if (!hba->clk_scaling.is_suspended) {
1201 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1202 return;
1203 }
1204 hba->clk_scaling.is_suspended = false;
1205 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1206
1207 devfreq_resume_device(hba->devfreq);
1208}
1209
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001210static int ufshcd_devfreq_target(struct device *dev,
1211 unsigned long *freq, u32 flags)
1212{
1213 int ret = 0;
1214 struct ufs_hba *hba = dev_get_drvdata(dev);
1215 ktime_t start;
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001216 bool scale_up, sched_clk_scaling_suspend_work = false;
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001217 unsigned long irq_flags;
1218
1219 if (!ufshcd_is_clkscaling_supported(hba))
1220 return -EINVAL;
1221
1222 if ((*freq > 0) && (*freq < UINT_MAX)) {
1223 dev_err(hba->dev, "%s: invalid freq = %lu\n", __func__, *freq);
1224 return -EINVAL;
1225 }
1226
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001227 spin_lock_irqsave(hba->host->host_lock, irq_flags);
1228 if (ufshcd_eh_in_progress(hba)) {
1229 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1230 return 0;
1231 }
1232
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001233 if (!hba->clk_scaling.active_reqs)
1234 sched_clk_scaling_suspend_work = true;
1235
1236 scale_up = (*freq == UINT_MAX) ? true : false;
1237 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) {
1238 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1239 ret = 0;
1240 goto out; /* no state change required */
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001241 }
1242 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
1243
1244 start = ktime_get();
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001245 ret = ufshcd_devfreq_scale(hba, scale_up);
1246
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001247 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev),
1248 (scale_up ? "up" : "down"),
1249 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
1250
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001251out:
1252 if (sched_clk_scaling_suspend_work)
1253 queue_work(hba->clk_scaling.workq,
1254 &hba->clk_scaling.suspend_work);
1255
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001256 return ret;
1257}
1258
1259
1260static int ufshcd_devfreq_get_dev_status(struct device *dev,
1261 struct devfreq_dev_status *stat)
1262{
1263 struct ufs_hba *hba = dev_get_drvdata(dev);
1264 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1265 unsigned long flags;
1266
1267 if (!ufshcd_is_clkscaling_supported(hba))
1268 return -EINVAL;
1269
1270 memset(stat, 0, sizeof(*stat));
1271
1272 spin_lock_irqsave(hba->host->host_lock, flags);
1273 if (!scaling->window_start_t)
1274 goto start_window;
1275
1276 if (scaling->is_busy_started)
1277 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1278 scaling->busy_start_t));
1279
1280 stat->total_time = jiffies_to_usecs((long)jiffies -
1281 (long)scaling->window_start_t);
1282 stat->busy_time = scaling->tot_busy_t;
1283start_window:
1284 scaling->window_start_t = jiffies;
1285 scaling->tot_busy_t = 0;
1286
1287 if (hba->outstanding_reqs) {
1288 scaling->busy_start_t = ktime_get();
1289 scaling->is_busy_started = true;
1290 } else {
1291 scaling->busy_start_t = 0;
1292 scaling->is_busy_started = false;
1293 }
1294 spin_unlock_irqrestore(hba->host->host_lock, flags);
1295 return 0;
1296}
1297
1298static struct devfreq_dev_profile ufs_devfreq_profile = {
1299 .polling_ms = 100,
1300 .target = ufshcd_devfreq_target,
1301 .get_dev_status = ufshcd_devfreq_get_dev_status,
1302};
1303
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001304static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1305{
1306 unsigned long flags;
1307
1308 devfreq_suspend_device(hba->devfreq);
1309 spin_lock_irqsave(hba->host->host_lock, flags);
1310 hba->clk_scaling.window_start_t = 0;
1311 spin_unlock_irqrestore(hba->host->host_lock, flags);
1312}
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001313
Gilad Bronera5082532016-10-17 17:10:00 -07001314static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
1315{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001316 unsigned long flags;
1317 bool suspend = false;
1318
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001319 if (!ufshcd_is_clkscaling_supported(hba))
1320 return;
1321
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001322 spin_lock_irqsave(hba->host->host_lock, flags);
1323 if (!hba->clk_scaling.is_suspended) {
1324 suspend = true;
1325 hba->clk_scaling.is_suspended = true;
1326 }
1327 spin_unlock_irqrestore(hba->host->host_lock, flags);
1328
1329 if (suspend)
1330 __ufshcd_suspend_clkscaling(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07001331}
1332
1333static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
1334{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001335 unsigned long flags;
1336 bool resume = false;
1337
1338 if (!ufshcd_is_clkscaling_supported(hba))
1339 return;
1340
1341 spin_lock_irqsave(hba->host->host_lock, flags);
1342 if (hba->clk_scaling.is_suspended) {
1343 resume = true;
1344 hba->clk_scaling.is_suspended = false;
1345 }
1346 spin_unlock_irqrestore(hba->host->host_lock, flags);
1347
1348 if (resume)
1349 devfreq_resume_device(hba->devfreq);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001350}
1351
1352static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
1353 struct device_attribute *attr, char *buf)
1354{
1355 struct ufs_hba *hba = dev_get_drvdata(dev);
1356
1357 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
1358}
1359
1360static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
1361 struct device_attribute *attr, const char *buf, size_t count)
1362{
1363 struct ufs_hba *hba = dev_get_drvdata(dev);
1364 u32 value;
1365 int err;
1366
1367 if (kstrtou32(buf, 0, &value))
1368 return -EINVAL;
1369
1370 value = !!value;
1371 if (value == hba->clk_scaling.is_allowed)
1372 goto out;
1373
1374 pm_runtime_get_sync(hba->dev);
1375 ufshcd_hold(hba, false);
1376
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001377 cancel_work_sync(&hba->clk_scaling.suspend_work);
1378 cancel_work_sync(&hba->clk_scaling.resume_work);
1379
1380 hba->clk_scaling.is_allowed = value;
1381
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001382 if (value) {
1383 ufshcd_resume_clkscaling(hba);
1384 } else {
1385 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001386 err = ufshcd_devfreq_scale(hba, true);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001387 if (err)
1388 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
1389 __func__, err);
1390 }
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001391
1392 ufshcd_release(hba);
1393 pm_runtime_put_sync(hba->dev);
1394out:
1395 return count;
Gilad Bronera5082532016-10-17 17:10:00 -07001396}
1397
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08001398static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
1399{
1400 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
1401 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
1402 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
1403 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
1404 hba->clk_scaling.enable_attr.attr.mode = 0644;
1405 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
1406 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
1407}
1408
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001409static void ufshcd_ungate_work(struct work_struct *work)
1410{
1411 int ret;
1412 unsigned long flags;
1413 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1414 clk_gating.ungate_work);
1415
1416 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
1417
1418 spin_lock_irqsave(hba->host->host_lock, flags);
1419 if (hba->clk_gating.state == CLKS_ON) {
1420 spin_unlock_irqrestore(hba->host->host_lock, flags);
1421 goto unblock_reqs;
1422 }
1423
1424 spin_unlock_irqrestore(hba->host->host_lock, flags);
1425 ufshcd_setup_clocks(hba, true);
1426
1427 /* Exit from hibern8 */
1428 if (ufshcd_can_hibern8_during_gating(hba)) {
1429 /* Prevent gating in this path */
1430 hba->clk_gating.is_suspended = true;
1431 if (ufshcd_is_link_hibern8(hba)) {
1432 ret = ufshcd_uic_hibern8_exit(hba);
1433 if (ret)
1434 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
1435 __func__, ret);
1436 else
1437 ufshcd_set_link_active(hba);
1438 }
1439 hba->clk_gating.is_suspended = false;
1440 }
1441unblock_reqs:
1442 scsi_unblock_requests(hba->host);
1443}
1444
1445/**
1446 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1447 * Also, exit from hibern8 mode and set the link as active.
1448 * @hba: per adapter instance
1449 * @async: This indicates whether caller should ungate clocks asynchronously.
1450 */
1451int ufshcd_hold(struct ufs_hba *hba, bool async)
1452{
1453 int rc = 0;
1454 unsigned long flags;
1455
1456 if (!ufshcd_is_clkgating_allowed(hba))
1457 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001458 spin_lock_irqsave(hba->host->host_lock, flags);
1459 hba->clk_gating.active_reqs++;
1460
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001461 if (ufshcd_eh_in_progress(hba)) {
1462 spin_unlock_irqrestore(hba->host->host_lock, flags);
1463 return 0;
1464 }
1465
Sahitya Tummala856b3482014-09-25 15:32:34 +03001466start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001467 switch (hba->clk_gating.state) {
1468 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -07001469 /*
1470 * Wait for the ungate work to complete if in progress.
1471 * Though the clocks may be in ON state, the link could
1472 * still be in hibner8 state if hibern8 is allowed
1473 * during clock gating.
1474 * Make sure we exit hibern8 state also in addition to
1475 * clocks being ON.
1476 */
1477 if (ufshcd_can_hibern8_during_gating(hba) &&
1478 ufshcd_is_link_hibern8(hba)) {
1479 spin_unlock_irqrestore(hba->host->host_lock, flags);
1480 flush_work(&hba->clk_gating.ungate_work);
1481 spin_lock_irqsave(hba->host->host_lock, flags);
1482 goto start;
1483 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001484 break;
1485 case REQ_CLKS_OFF:
1486 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
1487 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001488 trace_ufshcd_clk_gating(dev_name(hba->dev),
1489 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001490 break;
1491 }
1492 /*
1493 * If we here, it means gating work is either done or
1494 * currently running. Hence, fall through to cancel gating
1495 * work and to enable clocks.
1496 */
1497 case CLKS_OFF:
1498 scsi_block_requests(hba->host);
1499 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001500 trace_ufshcd_clk_gating(dev_name(hba->dev),
1501 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001502 schedule_work(&hba->clk_gating.ungate_work);
1503 /*
1504 * fall through to check if we should wait for this
1505 * work to be done or not.
1506 */
1507 case REQ_CLKS_ON:
1508 if (async) {
1509 rc = -EAGAIN;
1510 hba->clk_gating.active_reqs--;
1511 break;
1512 }
1513
1514 spin_unlock_irqrestore(hba->host->host_lock, flags);
1515 flush_work(&hba->clk_gating.ungate_work);
1516 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +03001517 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001518 goto start;
1519 default:
1520 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
1521 __func__, hba->clk_gating.state);
1522 break;
1523 }
1524 spin_unlock_irqrestore(hba->host->host_lock, flags);
1525out:
1526 return rc;
1527}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001528EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001529
1530static void ufshcd_gate_work(struct work_struct *work)
1531{
1532 struct ufs_hba *hba = container_of(work, struct ufs_hba,
1533 clk_gating.gate_work.work);
1534 unsigned long flags;
1535
1536 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -07001537 /*
1538 * In case you are here to cancel this work the gating state
1539 * would be marked as REQ_CLKS_ON. In this case save time by
1540 * skipping the gating work and exit after changing the clock
1541 * state to CLKS_ON.
1542 */
1543 if (hba->clk_gating.is_suspended ||
1544 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001545 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001546 trace_ufshcd_clk_gating(dev_name(hba->dev),
1547 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001548 goto rel_lock;
1549 }
1550
1551 if (hba->clk_gating.active_reqs
1552 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1553 || hba->lrb_in_use || hba->outstanding_tasks
1554 || hba->active_uic_cmd || hba->uic_async_done)
1555 goto rel_lock;
1556
1557 spin_unlock_irqrestore(hba->host->host_lock, flags);
1558
1559 /* put the link into hibern8 mode before turning off clocks */
1560 if (ufshcd_can_hibern8_during_gating(hba)) {
1561 if (ufshcd_uic_hibern8_enter(hba)) {
1562 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001563 trace_ufshcd_clk_gating(dev_name(hba->dev),
1564 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001565 goto out;
1566 }
1567 ufshcd_set_link_hibern8(hba);
1568 }
1569
1570 if (!ufshcd_is_link_active(hba))
1571 ufshcd_setup_clocks(hba, false);
1572 else
1573 /* If link is active, device ref_clk can't be switched off */
1574 __ufshcd_setup_clocks(hba, false, true);
1575
1576 /*
1577 * In case you are here to cancel this work the gating state
1578 * would be marked as REQ_CLKS_ON. In this case keep the state
1579 * as REQ_CLKS_ON which would anyway imply that clocks are off
1580 * and a request to turn them on is pending. By doing this way,
1581 * we keep the state machine in tact and this would ultimately
1582 * prevent from doing cancel work multiple times when there are
1583 * new requests arriving before the current cancel work is done.
1584 */
1585 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001586 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001587 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001588 trace_ufshcd_clk_gating(dev_name(hba->dev),
1589 hba->clk_gating.state);
1590 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001591rel_lock:
1592 spin_unlock_irqrestore(hba->host->host_lock, flags);
1593out:
1594 return;
1595}
1596
1597/* host lock must be held before calling this variant */
1598static void __ufshcd_release(struct ufs_hba *hba)
1599{
1600 if (!ufshcd_is_clkgating_allowed(hba))
1601 return;
1602
1603 hba->clk_gating.active_reqs--;
1604
1605 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1606 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1607 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001608 || hba->active_uic_cmd || hba->uic_async_done
1609 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001610 return;
1611
1612 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001613 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001614 schedule_delayed_work(&hba->clk_gating.gate_work,
1615 msecs_to_jiffies(hba->clk_gating.delay_ms));
1616}
1617
1618void ufshcd_release(struct ufs_hba *hba)
1619{
1620 unsigned long flags;
1621
1622 spin_lock_irqsave(hba->host->host_lock, flags);
1623 __ufshcd_release(hba);
1624 spin_unlock_irqrestore(hba->host->host_lock, flags);
1625}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001626EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001627
1628static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1629 struct device_attribute *attr, char *buf)
1630{
1631 struct ufs_hba *hba = dev_get_drvdata(dev);
1632
1633 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1634}
1635
1636static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1637 struct device_attribute *attr, const char *buf, size_t count)
1638{
1639 struct ufs_hba *hba = dev_get_drvdata(dev);
1640 unsigned long flags, value;
1641
1642 if (kstrtoul(buf, 0, &value))
1643 return -EINVAL;
1644
1645 spin_lock_irqsave(hba->host->host_lock, flags);
1646 hba->clk_gating.delay_ms = value;
1647 spin_unlock_irqrestore(hba->host->host_lock, flags);
1648 return count;
1649}
1650
Sahitya Tummalab4274112016-12-22 18:40:39 -08001651static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1652 struct device_attribute *attr, char *buf)
1653{
1654 struct ufs_hba *hba = dev_get_drvdata(dev);
1655
1656 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1657}
1658
1659static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1660 struct device_attribute *attr, const char *buf, size_t count)
1661{
1662 struct ufs_hba *hba = dev_get_drvdata(dev);
1663 unsigned long flags;
1664 u32 value;
1665
1666 if (kstrtou32(buf, 0, &value))
1667 return -EINVAL;
1668
1669 value = !!value;
1670 if (value == hba->clk_gating.is_enabled)
1671 goto out;
1672
1673 if (value) {
1674 ufshcd_release(hba);
1675 } else {
1676 spin_lock_irqsave(hba->host->host_lock, flags);
1677 hba->clk_gating.active_reqs++;
1678 spin_unlock_irqrestore(hba->host->host_lock, flags);
1679 }
1680
1681 hba->clk_gating.is_enabled = value;
1682out:
1683 return count;
1684}
1685
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001686static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1687{
1688 if (!ufshcd_is_clkgating_allowed(hba))
1689 return;
1690
1691 hba->clk_gating.delay_ms = 150;
1692 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1693 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1694
Sahitya Tummalab4274112016-12-22 18:40:39 -08001695 hba->clk_gating.is_enabled = true;
1696
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001697 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1698 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1699 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1700 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001701 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001702 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1703 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001704
1705 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1706 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1707 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1708 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1709 hba->clk_gating.enable_attr.attr.mode = 0644;
1710 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1711 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001712}
1713
1714static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1715{
1716 if (!ufshcd_is_clkgating_allowed(hba))
1717 return;
1718 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001719 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001720 cancel_work_sync(&hba->clk_gating.ungate_work);
1721 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001722}
1723
Sahitya Tummala856b3482014-09-25 15:32:34 +03001724/* Must be called with host lock acquired */
1725static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1726{
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001727 bool queue_resume_work = false;
1728
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001729 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001730 return;
1731
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08001732 if (!hba->clk_scaling.active_reqs++)
1733 queue_resume_work = true;
1734
1735 if (!hba->clk_scaling.is_allowed || hba->pm_op_in_progress)
1736 return;
1737
1738 if (queue_resume_work)
1739 queue_work(hba->clk_scaling.workq,
1740 &hba->clk_scaling.resume_work);
1741
1742 if (!hba->clk_scaling.window_start_t) {
1743 hba->clk_scaling.window_start_t = jiffies;
1744 hba->clk_scaling.tot_busy_t = 0;
1745 hba->clk_scaling.is_busy_started = false;
1746 }
1747
Sahitya Tummala856b3482014-09-25 15:32:34 +03001748 if (!hba->clk_scaling.is_busy_started) {
1749 hba->clk_scaling.busy_start_t = ktime_get();
1750 hba->clk_scaling.is_busy_started = true;
1751 }
1752}
1753
1754static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1755{
1756 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1757
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001758 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001759 return;
1760
1761 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1762 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1763 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001764 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001765 scaling->is_busy_started = false;
1766 }
1767}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301768/**
1769 * ufshcd_send_command - Send SCSI or device management commands
1770 * @hba: per adapter instance
1771 * @task_tag: Task tag of the command
1772 */
1773static inline
1774void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1775{
Dolev Ravivff8e20c2016-12-22 18:42:18 -08001776 hba->lrb[task_tag].issue_time_stamp = ktime_get();
Sahitya Tummala856b3482014-09-25 15:32:34 +03001777 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301778 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301779 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001780 /* Make sure that doorbell is committed immediately */
1781 wmb();
Lee Susman1a07f2d2016-12-22 18:42:03 -08001782 ufshcd_add_command_trace(hba, task_tag, "send");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301783}
1784
1785/**
1786 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1787 * @lrb - pointer to local reference block
1788 */
1789static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1790{
1791 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301792 if (lrbp->sense_buffer &&
1793 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001794 int len_to_copy;
1795
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301796 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001797 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
1798
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301799 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301800 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001801 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301802 }
1803}
1804
1805/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301806 * ufshcd_copy_query_response() - Copy the Query Response and the data
1807 * descriptor
1808 * @hba: per adapter instance
1809 * @lrb - pointer to local reference block
1810 */
1811static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001812int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301813{
1814 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1815
Dolev Raviv68078d52013-07-30 00:35:58 +05301816 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301817
Dolev Raviv68078d52013-07-30 00:35:58 +05301818 /* Get the descriptor */
1819 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001820 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301821 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001822 u16 resp_len;
1823 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301824
1825 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001826 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301827 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001828 buf_len = be16_to_cpu(
1829 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001830 if (likely(buf_len >= resp_len)) {
1831 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1832 } else {
1833 dev_warn(hba->dev,
1834 "%s: Response size is bigger than buffer",
1835 __func__);
1836 return -EINVAL;
1837 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301838 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001839
1840 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301841}
1842
1843/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301844 * ufshcd_hba_capabilities - Read controller capabilities
1845 * @hba: per adapter instance
1846 */
1847static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
1848{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301849 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301850
1851 /* nutrs and nutmrs are 0 based values */
1852 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
1853 hba->nutmrs =
1854 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
1855}
1856
1857/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301858 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1859 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301860 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301861 * Return true on success, else false
1862 */
1863static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
1864{
1865 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1866 return true;
1867 else
1868 return false;
1869}
1870
1871/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301872 * ufshcd_get_upmcrs - Get the power mode change request status
1873 * @hba: Pointer to adapter instance
1874 *
1875 * This function gets the UPMCRS field of HCS register
1876 * Returns value of UPMCRS field
1877 */
1878static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1879{
1880 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1881}
1882
1883/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301884 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1885 * @hba: per adapter instance
1886 * @uic_cmd: UIC command
1887 *
1888 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301889 */
1890static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301891ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301892{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301893 WARN_ON(hba->active_uic_cmd);
1894
1895 hba->active_uic_cmd = uic_cmd;
1896
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301897 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301898 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1899 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1900 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301901
1902 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301903 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301904 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301905}
1906
1907/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301908 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1909 * @hba: per adapter instance
1910 * @uic_command: UIC command
1911 *
1912 * Must be called with mutex held.
1913 * Returns 0 only if success.
1914 */
1915static int
1916ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1917{
1918 int ret;
1919 unsigned long flags;
1920
1921 if (wait_for_completion_timeout(&uic_cmd->done,
1922 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1923 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1924 else
1925 ret = -ETIMEDOUT;
1926
1927 spin_lock_irqsave(hba->host->host_lock, flags);
1928 hba->active_uic_cmd = NULL;
1929 spin_unlock_irqrestore(hba->host->host_lock, flags);
1930
1931 return ret;
1932}
1933
1934/**
1935 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1936 * @hba: per adapter instance
1937 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001938 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301939 *
1940 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001941 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301942 * Returns 0 only if success.
1943 */
1944static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001945__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1946 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301947{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301948 if (!ufshcd_ready_for_uic_cmd(hba)) {
1949 dev_err(hba->dev,
1950 "Controller not ready to accept UIC commands\n");
1951 return -EIO;
1952 }
1953
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001954 if (completion)
1955 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301956
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301957 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301958
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001959 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301960}
1961
1962/**
1963 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1964 * @hba: per adapter instance
1965 * @uic_cmd: UIC command
1966 *
1967 * Returns 0 only if success.
1968 */
1969static int
1970ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1971{
1972 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001973 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301974
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001975 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301976 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001977 ufshcd_add_delay_before_dme_cmd(hba);
1978
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001979 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001980 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001981 spin_unlock_irqrestore(hba->host->host_lock, flags);
1982 if (!ret)
1983 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1984
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301985 mutex_unlock(&hba->uic_cmd_mutex);
1986
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001987 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301988 return ret;
1989}
1990
1991/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301992 * ufshcd_map_sg - Map scatter-gather list to prdt
1993 * @lrbp - pointer to local reference block
1994 *
1995 * Returns 0 in case of success, non-zero value in case of failure
1996 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001997static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301998{
1999 struct ufshcd_sg_entry *prd_table;
2000 struct scatterlist *sg;
2001 struct scsi_cmnd *cmd;
2002 int sg_segments;
2003 int i;
2004
2005 cmd = lrbp->cmd;
2006 sg_segments = scsi_dma_map(cmd);
2007 if (sg_segments < 0)
2008 return sg_segments;
2009
2010 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002011 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
2012 lrbp->utr_descriptor_ptr->prd_table_length =
2013 cpu_to_le16((u16)(sg_segments *
2014 sizeof(struct ufshcd_sg_entry)));
2015 else
2016 lrbp->utr_descriptor_ptr->prd_table_length =
2017 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302018
2019 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
2020
2021 scsi_for_each_sg(cmd, sg, sg_segments, i) {
2022 prd_table[i].size =
2023 cpu_to_le32(((u32) sg_dma_len(sg))-1);
2024 prd_table[i].base_addr =
2025 cpu_to_le32(lower_32_bits(sg->dma_address));
2026 prd_table[i].upper_addr =
2027 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002028 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302029 }
2030 } else {
2031 lrbp->utr_descriptor_ptr->prd_table_length = 0;
2032 }
2033
2034 return 0;
2035}
2036
2037/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302038 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302039 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302040 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302041 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302042static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302043{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302044 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2045
2046 if (hba->ufs_version == UFSHCI_VERSION_10) {
2047 u32 rw;
2048 rw = set & INTERRUPT_MASK_RW_VER_10;
2049 set = rw | ((set ^ intrs) & intrs);
2050 } else {
2051 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302052 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05302053
2054 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
2055}
2056
2057/**
2058 * ufshcd_disable_intr - disable interrupts
2059 * @hba: per adapter instance
2060 * @intrs: interrupt bits
2061 */
2062static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
2063{
2064 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
2065
2066 if (hba->ufs_version == UFSHCI_VERSION_10) {
2067 u32 rw;
2068 rw = (set & INTERRUPT_MASK_RW_VER_10) &
2069 ~(intrs & INTERRUPT_MASK_RW_VER_10);
2070 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
2071
2072 } else {
2073 set &= ~intrs;
2074 }
2075
2076 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302077}
2078
2079/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302080 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2081 * descriptor according to request
2082 * @lrbp: pointer to local reference block
2083 * @upiu_flags: flags required in the header
2084 * @cmd_dir: requests data direction
2085 */
2086static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01002087 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302088{
2089 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
2090 u32 data_direction;
2091 u32 dword_0;
2092
2093 if (cmd_dir == DMA_FROM_DEVICE) {
2094 data_direction = UTP_DEVICE_TO_HOST;
2095 *upiu_flags = UPIU_CMD_FLAGS_READ;
2096 } else if (cmd_dir == DMA_TO_DEVICE) {
2097 data_direction = UTP_HOST_TO_DEVICE;
2098 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
2099 } else {
2100 data_direction = UTP_NO_DATA_TRANSFER;
2101 *upiu_flags = UPIU_CMD_FLAGS_NONE;
2102 }
2103
2104 dword_0 = data_direction | (lrbp->command_type
2105 << UPIU_COMMAND_TYPE_OFFSET);
2106 if (lrbp->intr_cmd)
2107 dword_0 |= UTP_REQ_DESC_INT_CMD;
2108
2109 /* Transfer request descriptor header fields */
2110 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002111 /* dword_1 is reserved, hence it is set to 0 */
2112 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302113 /*
2114 * assigning invalid value for command status. Controller
2115 * updates OCS on command completion, with the command
2116 * status
2117 */
2118 req_desc->header.dword_2 =
2119 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002120 /* dword_3 is reserved, hence it is set to 0 */
2121 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02002122
2123 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302124}
2125
2126/**
2127 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2128 * for scsi commands
2129 * @lrbp - local reference block pointer
2130 * @upiu_flags - flags
2131 */
2132static
2133void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
2134{
2135 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002136 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302137
2138 /* command descriptor fields */
2139 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2140 UPIU_TRANSACTION_COMMAND, upiu_flags,
2141 lrbp->lun, lrbp->task_tag);
2142 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2143 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
2144
2145 /* Total EHS length and Data segment length will be zero */
2146 ucd_req_ptr->header.dword_2 = 0;
2147
2148 ucd_req_ptr->sc.exp_data_transfer_len =
2149 cpu_to_be32(lrbp->cmd->sdb.length);
2150
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02002151 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
2152 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
2153 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
2154
2155 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302156}
2157
Dolev Raviv68078d52013-07-30 00:35:58 +05302158/**
2159 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2160 * for query requsts
2161 * @hba: UFS hba
2162 * @lrbp: local reference block pointer
2163 * @upiu_flags: flags
2164 */
2165static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
2166 struct ufshcd_lrb *lrbp, u32 upiu_flags)
2167{
2168 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2169 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302170 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05302171 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
2172
2173 /* Query request header */
2174 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
2175 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
2176 lrbp->lun, lrbp->task_tag);
2177 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
2178 0, query->request.query_func, 0, 0);
2179
Zang Leigang68612852016-08-25 17:39:19 +08002180 /* Data segment length only need for WRITE_DESC */
2181 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2182 ucd_req_ptr->header.dword_2 =
2183 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
2184 else
2185 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05302186
2187 /* Copy the Query Request buffer as is */
2188 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
2189 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05302190
2191 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002192 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
2193 memcpy(descp, query->descriptor, len);
2194
Yaniv Gardi51047262016-02-01 15:02:38 +02002195 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05302196}
2197
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302198static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
2199{
2200 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
2201
2202 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
2203
2204 /* command descriptor fields */
2205 ucd_req_ptr->header.dword_0 =
2206 UPIU_HEADER_DWORD(
2207 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02002208 /* clear rest of the fields of basic header */
2209 ucd_req_ptr->header.dword_1 = 0;
2210 ucd_req_ptr->header.dword_2 = 0;
2211
2212 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302213}
2214
2215/**
Joao Pinto300bb132016-05-11 12:21:27 +01002216 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2217 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302218 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302219 * @lrb - pointer to local reference block
2220 */
Joao Pinto300bb132016-05-11 12:21:27 +01002221static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302222{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302223 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302224 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302225
Joao Pinto300bb132016-05-11 12:21:27 +01002226 if (hba->ufs_version == UFSHCI_VERSION_20)
2227 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2228 else
2229 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
2230
2231 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
2232 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
2233 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
2234 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
2235 ufshcd_prepare_utp_nop_upiu(lrbp);
2236 else
2237 ret = -EINVAL;
2238
2239 return ret;
2240}
2241
2242/**
2243 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2244 * for SCSI Purposes
2245 * @hba - per adapter instance
2246 * @lrb - pointer to local reference block
2247 */
2248static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2249{
2250 u32 upiu_flags;
2251 int ret = 0;
2252
2253 if (hba->ufs_version == UFSHCI_VERSION_20)
2254 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
2255 else
2256 lrbp->command_type = UTP_CMD_TYPE_SCSI;
2257
2258 if (likely(lrbp->cmd)) {
2259 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
2260 lrbp->cmd->sc_data_direction);
2261 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
2262 } else {
2263 ret = -EINVAL;
2264 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302265
2266 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302267}
2268
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002269/*
2270 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2271 * @scsi_lun: scsi LUN id
2272 *
2273 * Returns UPIU LUN id
2274 */
2275static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
2276{
2277 if (scsi_is_wlun(scsi_lun))
2278 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
2279 | UFS_UPIU_WLUN_ID;
2280 else
2281 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
2282}
2283
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302284/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03002285 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2286 * @scsi_lun: UPIU W-LUN id
2287 *
2288 * Returns SCSI W-LUN id
2289 */
2290static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
2291{
2292 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
2293}
2294
2295/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302296 * ufshcd_queuecommand - main entry point for SCSI requests
2297 * @cmd: command from SCSI Midlayer
2298 * @done: call back function
2299 *
2300 * Returns 0 for success, non-zero in case of failure
2301 */
2302static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
2303{
2304 struct ufshcd_lrb *lrbp;
2305 struct ufs_hba *hba;
2306 unsigned long flags;
2307 int tag;
2308 int err = 0;
2309
2310 hba = shost_priv(host);
2311
2312 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02002313 if (!ufshcd_valid_tag(hba, tag)) {
2314 dev_err(hba->dev,
2315 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2316 __func__, tag, cmd, cmd->request);
2317 BUG();
2318 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302319
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002320 if (!down_read_trylock(&hba->clk_scaling_lock))
2321 return SCSI_MLQUEUE_HOST_BUSY;
2322
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302323 spin_lock_irqsave(hba->host->host_lock, flags);
2324 switch (hba->ufshcd_state) {
2325 case UFSHCD_STATE_OPERATIONAL:
2326 break;
Zang Leigang141f8162016-11-16 11:29:37 +08002327 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302328 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302329 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302330 goto out_unlock;
2331 case UFSHCD_STATE_ERROR:
2332 set_host_byte(cmd, DID_ERROR);
2333 cmd->scsi_done(cmd);
2334 goto out_unlock;
2335 default:
2336 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
2337 __func__, hba->ufshcd_state);
2338 set_host_byte(cmd, DID_BAD_TARGET);
2339 cmd->scsi_done(cmd);
2340 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302341 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002342
2343 /* if error handling is in progress, don't issue commands */
2344 if (ufshcd_eh_in_progress(hba)) {
2345 set_host_byte(cmd, DID_ERROR);
2346 cmd->scsi_done(cmd);
2347 goto out_unlock;
2348 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302349 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302350
Gilad Broner7fabb772017-02-03 16:56:50 -08002351 hba->req_abort_count = 0;
2352
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302353 /* acquire the tag to make sure device cmds don't use it */
2354 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
2355 /*
2356 * Dev manage command in progress, requeue the command.
2357 * Requeuing the command helps in cases where the request *may*
2358 * find different tag instead of waiting for dev manage command
2359 * completion.
2360 */
2361 err = SCSI_MLQUEUE_HOST_BUSY;
2362 goto out;
2363 }
2364
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002365 err = ufshcd_hold(hba, true);
2366 if (err) {
2367 err = SCSI_MLQUEUE_HOST_BUSY;
2368 clear_bit_unlock(tag, &hba->lrb_in_use);
2369 goto out;
2370 }
2371 WARN_ON(hba->clk_gating.state != CLKS_ON);
2372
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302373 lrbp = &hba->lrb[tag];
2374
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302375 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302376 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07002377 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302378 lrbp->sense_buffer = cmd->sense_buffer;
2379 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002380 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03002381 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Gilad Bronere0b299e2017-02-03 16:56:40 -08002382 lrbp->req_abort_skip = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302383
Joao Pinto300bb132016-05-11 12:21:27 +01002384 ufshcd_comp_scsi_upiu(hba, lrbp);
2385
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002386 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302387 if (err) {
2388 lrbp->cmd = NULL;
2389 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302390 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302391 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002392 /* Make sure descriptors are ready before ringing the doorbell */
2393 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302394
2395 /* issue command to the controller */
2396 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002397 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302398 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302399out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302400 spin_unlock_irqrestore(hba->host->host_lock, flags);
2401out:
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002402 up_read(&hba->clk_scaling_lock);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302403 return err;
2404}
2405
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302406static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
2407 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
2408{
2409 lrbp->cmd = NULL;
2410 lrbp->sense_bufflen = 0;
2411 lrbp->sense_buffer = NULL;
2412 lrbp->task_tag = tag;
2413 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302414 lrbp->intr_cmd = true; /* No interrupt aggregation */
2415 hba->dev_cmd.type = cmd_type;
2416
Joao Pinto300bb132016-05-11 12:21:27 +01002417 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302418}
2419
2420static int
2421ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
2422{
2423 int err = 0;
2424 unsigned long flags;
2425 u32 mask = 1 << tag;
2426
2427 /* clear outstanding transaction before retry */
2428 spin_lock_irqsave(hba->host->host_lock, flags);
2429 ufshcd_utrl_clear(hba, tag);
2430 spin_unlock_irqrestore(hba->host->host_lock, flags);
2431
2432 /*
2433 * wait for for h/w to clear corresponding bit in door-bell.
2434 * max. wait is 1 sec.
2435 */
2436 err = ufshcd_wait_for_register(hba,
2437 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02002438 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302439
2440 return err;
2441}
2442
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002443static int
2444ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2445{
2446 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
2447
2448 /* Get the UPIU response */
2449 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
2450 UPIU_RSP_CODE_OFFSET;
2451 return query_res->response;
2452}
2453
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302454/**
2455 * ufshcd_dev_cmd_completion() - handles device management command responses
2456 * @hba: per adapter instance
2457 * @lrbp: pointer to local reference block
2458 */
2459static int
2460ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2461{
2462 int resp;
2463 int err = 0;
2464
Dolev Ravivff8e20c2016-12-22 18:42:18 -08002465 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302466 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2467
2468 switch (resp) {
2469 case UPIU_TRANSACTION_NOP_IN:
2470 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
2471 err = -EINVAL;
2472 dev_err(hba->dev, "%s: unexpected response %x\n",
2473 __func__, resp);
2474 }
2475 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05302476 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03002477 err = ufshcd_check_query_response(hba, lrbp);
2478 if (!err)
2479 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05302480 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302481 case UPIU_TRANSACTION_REJECT_UPIU:
2482 /* TODO: handle Reject UPIU Response */
2483 err = -EPERM;
2484 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
2485 __func__);
2486 break;
2487 default:
2488 err = -EINVAL;
2489 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
2490 __func__, resp);
2491 break;
2492 }
2493
2494 return err;
2495}
2496
2497static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
2498 struct ufshcd_lrb *lrbp, int max_timeout)
2499{
2500 int err = 0;
2501 unsigned long time_left;
2502 unsigned long flags;
2503
2504 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
2505 msecs_to_jiffies(max_timeout));
2506
Gilad Bronerad1a1b92016-10-17 17:09:36 -07002507 /* Make sure descriptors are ready before ringing the doorbell */
2508 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302509 spin_lock_irqsave(hba->host->host_lock, flags);
2510 hba->dev_cmd.complete = NULL;
2511 if (likely(time_left)) {
2512 err = ufshcd_get_tr_ocs(lrbp);
2513 if (!err)
2514 err = ufshcd_dev_cmd_completion(hba, lrbp);
2515 }
2516 spin_unlock_irqrestore(hba->host->host_lock, flags);
2517
2518 if (!time_left) {
2519 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002520 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
2521 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302522 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02002523 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302524 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02002525 /*
2526 * in case of an error, after clearing the doorbell,
2527 * we also need to clear the outstanding_request
2528 * field in hba
2529 */
2530 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302531 }
2532
2533 return err;
2534}
2535
2536/**
2537 * ufshcd_get_dev_cmd_tag - Get device management command tag
2538 * @hba: per-adapter instance
2539 * @tag: pointer to variable with available slot value
2540 *
2541 * Get a free slot and lock it until device management command
2542 * completes.
2543 *
2544 * Returns false if free slot is unavailable for locking, else
2545 * return true with tag value in @tag.
2546 */
2547static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
2548{
2549 int tag;
2550 bool ret = false;
2551 unsigned long tmp;
2552
2553 if (!tag_out)
2554 goto out;
2555
2556 do {
2557 tmp = ~hba->lrb_in_use;
2558 tag = find_last_bit(&tmp, hba->nutrs);
2559 if (tag >= hba->nutrs)
2560 goto out;
2561 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
2562
2563 *tag_out = tag;
2564 ret = true;
2565out:
2566 return ret;
2567}
2568
2569static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
2570{
2571 clear_bit_unlock(tag, &hba->lrb_in_use);
2572}
2573
2574/**
2575 * ufshcd_exec_dev_cmd - API for sending device management requests
2576 * @hba - UFS hba
2577 * @cmd_type - specifies the type (NOP, Query...)
2578 * @timeout - time in seconds
2579 *
Dolev Raviv68078d52013-07-30 00:35:58 +05302580 * NOTE: Since there is only one available tag for device management commands,
2581 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302582 */
2583static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
2584 enum dev_cmd_type cmd_type, int timeout)
2585{
2586 struct ufshcd_lrb *lrbp;
2587 int err;
2588 int tag;
2589 struct completion wait;
2590 unsigned long flags;
2591
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002592 down_read(&hba->clk_scaling_lock);
2593
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302594 /*
2595 * Get free slot, sleep if slots are unavailable.
2596 * Even though we use wait_event() which sleeps indefinitely,
2597 * the maximum wait time is bounded by SCSI request timeout.
2598 */
2599 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
2600
2601 init_completion(&wait);
2602 lrbp = &hba->lrb[tag];
2603 WARN_ON(lrbp->cmd);
2604 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
2605 if (unlikely(err))
2606 goto out_put_tag;
2607
2608 hba->dev_cmd.complete = &wait;
2609
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02002610 /* Make sure descriptors are ready before ringing the doorbell */
2611 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302612 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09002613 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302614 ufshcd_send_command(hba, tag);
2615 spin_unlock_irqrestore(hba->host->host_lock, flags);
2616
2617 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
2618
2619out_put_tag:
2620 ufshcd_put_dev_cmd_tag(hba, tag);
2621 wake_up(&hba->dev_cmd.tag_wq);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08002622 up_read(&hba->clk_scaling_lock);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302623 return err;
2624}
2625
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302626/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002627 * ufshcd_init_query() - init the query response and request parameters
2628 * @hba: per-adapter instance
2629 * @request: address of the request pointer to be initialized
2630 * @response: address of the response pointer to be initialized
2631 * @opcode: operation to perform
2632 * @idn: flag idn to access
2633 * @index: LU number to access
2634 * @selector: query/flag/descriptor further identification
2635 */
2636static inline void ufshcd_init_query(struct ufs_hba *hba,
2637 struct ufs_query_req **request, struct ufs_query_res **response,
2638 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2639{
2640 *request = &hba->dev_cmd.query.request;
2641 *response = &hba->dev_cmd.query.response;
2642 memset(*request, 0, sizeof(struct ufs_query_req));
2643 memset(*response, 0, sizeof(struct ufs_query_res));
2644 (*request)->upiu_req.opcode = opcode;
2645 (*request)->upiu_req.idn = idn;
2646 (*request)->upiu_req.index = index;
2647 (*request)->upiu_req.selector = selector;
2648}
2649
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002650static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2651 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2652{
2653 int ret;
2654 int retries;
2655
2656 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2657 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
2658 if (ret)
2659 dev_dbg(hba->dev,
2660 "%s: failed with error %d, retries %d\n",
2661 __func__, ret, retries);
2662 else
2663 break;
2664 }
2665
2666 if (ret)
2667 dev_err(hba->dev,
2668 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2669 __func__, opcode, idn, ret, retries);
2670 return ret;
2671}
2672
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002673/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302674 * ufshcd_query_flag() - API function for sending flag query requests
2675 * hba: per-adapter instance
2676 * query_opcode: flag query to perform
2677 * idn: flag idn to access
2678 * flag_res: the flag value after the query request completes
2679 *
2680 * Returns 0 for success, non-zero in case of failure
2681 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002682int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05302683 enum flag_idn idn, bool *flag_res)
2684{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002685 struct ufs_query_req *request = NULL;
2686 struct ufs_query_res *response = NULL;
2687 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002688 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302689
2690 BUG_ON(!hba);
2691
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002692 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302693 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002694 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2695 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302696
2697 switch (opcode) {
2698 case UPIU_QUERY_OPCODE_SET_FLAG:
2699 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2700 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2701 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2702 break;
2703 case UPIU_QUERY_OPCODE_READ_FLAG:
2704 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2705 if (!flag_res) {
2706 /* No dummy reads */
2707 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2708 __func__);
2709 err = -EINVAL;
2710 goto out_unlock;
2711 }
2712 break;
2713 default:
2714 dev_err(hba->dev,
2715 "%s: Expected query flag opcode but got = %d\n",
2716 __func__, opcode);
2717 err = -EINVAL;
2718 goto out_unlock;
2719 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302720
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002721 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302722
2723 if (err) {
2724 dev_err(hba->dev,
2725 "%s: Sending flag query for idn %d failed, err = %d\n",
2726 __func__, idn, err);
2727 goto out_unlock;
2728 }
2729
2730 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302731 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302732 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2733
2734out_unlock:
2735 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002736 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302737 return err;
2738}
2739
2740/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302741 * ufshcd_query_attr - API function for sending attribute requests
2742 * hba: per-adapter instance
2743 * opcode: attribute opcode
2744 * idn: attribute idn to access
2745 * index: index field
2746 * selector: selector field
2747 * attr_val: the attribute value after the query request completes
2748 *
2749 * Returns 0 for success, non-zero in case of failure
2750*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302751static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302752 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
2753{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002754 struct ufs_query_req *request = NULL;
2755 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302756 int err;
2757
2758 BUG_ON(!hba);
2759
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002760 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302761 if (!attr_val) {
2762 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2763 __func__, opcode);
2764 err = -EINVAL;
2765 goto out;
2766 }
2767
2768 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002769 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2770 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302771
2772 switch (opcode) {
2773 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2774 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302775 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302776 break;
2777 case UPIU_QUERY_OPCODE_READ_ATTR:
2778 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2779 break;
2780 default:
2781 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2782 __func__, opcode);
2783 err = -EINVAL;
2784 goto out_unlock;
2785 }
2786
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002787 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302788
2789 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002790 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2791 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302792 goto out_unlock;
2793 }
2794
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302795 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302796
2797out_unlock:
2798 mutex_unlock(&hba->dev_cmd.lock);
2799out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002800 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302801 return err;
2802}
2803
2804/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002805 * ufshcd_query_attr_retry() - API function for sending query
2806 * attribute with retries
2807 * @hba: per-adapter instance
2808 * @opcode: attribute opcode
2809 * @idn: attribute idn to access
2810 * @index: index field
2811 * @selector: selector field
2812 * @attr_val: the attribute value after the query request
2813 * completes
2814 *
2815 * Returns 0 for success, non-zero in case of failure
2816*/
2817static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2818 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2819 u32 *attr_val)
2820{
2821 int ret = 0;
2822 u32 retries;
2823
2824 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2825 ret = ufshcd_query_attr(hba, opcode, idn, index,
2826 selector, attr_val);
2827 if (ret)
2828 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2829 __func__, ret, retries);
2830 else
2831 break;
2832 }
2833
2834 if (ret)
2835 dev_err(hba->dev,
2836 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2837 __func__, idn, ret, QUERY_REQ_RETRIES);
2838 return ret;
2839}
2840
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002841static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002842 enum query_opcode opcode, enum desc_idn idn, u8 index,
2843 u8 selector, u8 *desc_buf, int *buf_len)
2844{
2845 struct ufs_query_req *request = NULL;
2846 struct ufs_query_res *response = NULL;
2847 int err;
2848
2849 BUG_ON(!hba);
2850
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002851 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002852 if (!desc_buf) {
2853 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2854 __func__, opcode);
2855 err = -EINVAL;
2856 goto out;
2857 }
2858
2859 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
2860 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2861 __func__, *buf_len);
2862 err = -EINVAL;
2863 goto out;
2864 }
2865
2866 mutex_lock(&hba->dev_cmd.lock);
2867 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2868 selector);
2869 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002870 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002871
2872 switch (opcode) {
2873 case UPIU_QUERY_OPCODE_WRITE_DESC:
2874 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2875 break;
2876 case UPIU_QUERY_OPCODE_READ_DESC:
2877 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2878 break;
2879 default:
2880 dev_err(hba->dev,
2881 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2882 __func__, opcode);
2883 err = -EINVAL;
2884 goto out_unlock;
2885 }
2886
2887 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2888
2889 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002890 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2891 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002892 goto out_unlock;
2893 }
2894
2895 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002896 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002897
2898out_unlock:
2899 mutex_unlock(&hba->dev_cmd.lock);
2900out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002901 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002902 return err;
2903}
2904
2905/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002906 * ufshcd_query_descriptor_retry - API function for sending descriptor
2907 * requests
2908 * hba: per-adapter instance
2909 * opcode: attribute opcode
2910 * idn: attribute idn to access
2911 * index: index field
2912 * selector: selector field
2913 * desc_buf: the buffer that contains the descriptor
2914 * buf_len: length parameter passed to the device
2915 *
2916 * Returns 0 for success, non-zero in case of failure.
2917 * The buf_len parameter will contain, on return, the length parameter
2918 * received on the response.
2919 */
Tomas Winkler26cf9152017-01-05 10:45:09 +02002920static int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2921 enum query_opcode opcode,
2922 enum desc_idn idn, u8 index,
2923 u8 selector,
2924 u8 *desc_buf, int *buf_len)
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002925{
2926 int err;
2927 int retries;
2928
2929 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2930 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2931 selector, desc_buf, buf_len);
2932 if (!err || err == -EINVAL)
2933 break;
2934 }
2935
2936 return err;
2937}
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002938
2939/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002940 * ufshcd_read_desc_param - read the specified descriptor parameter
2941 * @hba: Pointer to adapter instance
2942 * @desc_id: descriptor idn value
2943 * @desc_index: descriptor index
2944 * @param_offset: offset of the parameter to read
2945 * @param_read_buf: pointer to buffer where parameter would be read
2946 * @param_size: sizeof(param_read_buf)
2947 *
2948 * Return 0 in case of success, non-zero otherwise
2949 */
2950static int ufshcd_read_desc_param(struct ufs_hba *hba,
2951 enum desc_idn desc_id,
2952 int desc_index,
2953 u32 param_offset,
2954 u8 *param_read_buf,
2955 u32 param_size)
2956{
2957 int ret;
2958 u8 *desc_buf;
2959 u32 buff_len;
2960 bool is_kmalloc = true;
2961
2962 /* safety checks */
2963 if (desc_id >= QUERY_DESC_IDN_MAX)
2964 return -EINVAL;
2965
2966 buff_len = ufs_query_desc_max_size[desc_id];
2967 if ((param_offset + param_size) > buff_len)
2968 return -EINVAL;
2969
2970 if (!param_offset && (param_size == buff_len)) {
2971 /* memory space already available to hold full descriptor */
2972 desc_buf = param_read_buf;
2973 is_kmalloc = false;
2974 } else {
2975 /* allocate memory to hold full descriptor */
2976 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2977 if (!desc_buf)
2978 return -ENOMEM;
2979 }
2980
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002981 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2982 desc_id, desc_index, 0, desc_buf,
2983 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002984
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002985 if (ret) {
2986 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2987 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002988
2989 goto out;
2990 }
2991
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002992 /* Sanity check */
2993 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2994 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2995 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2996 ret = -EINVAL;
2997 goto out;
2998 }
2999
3000 /*
3001 * While reading variable size descriptors (like string descriptor),
3002 * some UFS devices may report the "LENGTH" (field in "Transaction
3003 * Specific fields" of Query Response UPIU) same as what was requested
3004 * in Query Request UPIU instead of reporting the actual size of the
3005 * variable size descriptor.
3006 * Although it's safe to ignore the "LENGTH" field for variable size
3007 * descriptors as we can always derive the length of the descriptor from
3008 * the descriptor header fields. Hence this change impose the length
3009 * match check only for fixed size descriptors (for which we always
3010 * request the correct size as part of Query Request UPIU).
3011 */
3012 if ((desc_id != QUERY_DESC_IDN_STRING) &&
3013 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
3014 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
3015 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
3016 ret = -EINVAL;
3017 goto out;
3018 }
3019
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003020 if (is_kmalloc)
3021 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
3022out:
3023 if (is_kmalloc)
3024 kfree(desc_buf);
3025 return ret;
3026}
3027
3028static inline int ufshcd_read_desc(struct ufs_hba *hba,
3029 enum desc_idn desc_id,
3030 int desc_index,
3031 u8 *buf,
3032 u32 size)
3033{
3034 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
3035}
3036
3037static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
3038 u8 *buf,
3039 u32 size)
3040{
Dolev Raviv61e07352016-11-23 16:30:49 -08003041 int err = 0;
3042 int retries;
3043
3044 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3045 /* Read descriptor*/
3046 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
3047 if (!err)
3048 break;
3049 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3050 }
3051
3052 return err;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003053}
3054
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003055static int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
Yaniv Gardib573d482016-03-10 17:37:09 +02003056{
3057 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
3058}
Yaniv Gardib573d482016-03-10 17:37:09 +02003059
3060/**
3061 * ufshcd_read_string_desc - read string descriptor
3062 * @hba: pointer to adapter instance
3063 * @desc_index: descriptor index
3064 * @buf: pointer to buffer where descriptor would be read
3065 * @size: size of buf
3066 * @ascii: if true convert from unicode to ascii characters
3067 *
3068 * Return 0 in case of success, non-zero otherwise
3069 */
Tomas Winkler8209b6d2017-01-05 10:45:10 +02003070#define ASCII_STD true
3071static int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index,
3072 u8 *buf, u32 size, bool ascii)
Yaniv Gardib573d482016-03-10 17:37:09 +02003073{
3074 int err = 0;
3075
3076 err = ufshcd_read_desc(hba,
3077 QUERY_DESC_IDN_STRING, desc_index, buf, size);
3078
3079 if (err) {
3080 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
3081 __func__, QUERY_REQ_RETRIES, err);
3082 goto out;
3083 }
3084
3085 if (ascii) {
3086 int desc_len;
3087 int ascii_len;
3088 int i;
3089 char *buff_ascii;
3090
3091 desc_len = buf[0];
3092 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3093 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
3094 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
3095 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
3096 __func__);
3097 err = -ENOMEM;
3098 goto out;
3099 }
3100
3101 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
3102 if (!buff_ascii) {
3103 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08003104 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02003105 }
3106
3107 /*
3108 * the descriptor contains string in UTF16 format
3109 * we need to convert to utf-8 so it can be displayed
3110 */
3111 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
3112 desc_len - QUERY_DESC_HDR_SIZE,
3113 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
3114
3115 /* replace non-printable or non-ASCII characters with spaces */
3116 for (i = 0; i < ascii_len; i++)
3117 ufshcd_remove_non_printable(&buff_ascii[i]);
3118
3119 memset(buf + QUERY_DESC_HDR_SIZE, 0,
3120 size - QUERY_DESC_HDR_SIZE);
3121 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
3122 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02003123 kfree(buff_ascii);
3124 }
3125out:
3126 return err;
3127}
Yaniv Gardib573d482016-03-10 17:37:09 +02003128
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003129/**
3130 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3131 * @hba: Pointer to adapter instance
3132 * @lun: lun id
3133 * @param_offset: offset of the parameter to read
3134 * @param_read_buf: pointer to buffer where parameter would be read
3135 * @param_size: sizeof(param_read_buf)
3136 *
3137 * Return 0 in case of success, non-zero otherwise
3138 */
3139static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
3140 int lun,
3141 enum unit_desc_param param_offset,
3142 u8 *param_read_buf,
3143 u32 param_size)
3144{
3145 /*
3146 * Unit descriptors are only available for general purpose LUs (LUN id
3147 * from 0 to 7) and RPMB Well known LU.
3148 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003149 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03003150 return -EOPNOTSUPP;
3151
3152 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
3153 param_offset, param_read_buf, param_size);
3154}
3155
3156/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303157 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3158 * @hba: per adapter instance
3159 *
3160 * 1. Allocate DMA memory for Command Descriptor array
3161 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3162 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3163 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3164 * (UTMRDL)
3165 * 4. Allocate memory for local reference block(lrb).
3166 *
3167 * Returns 0 for success, non-zero in case of failure
3168 */
3169static int ufshcd_memory_alloc(struct ufs_hba *hba)
3170{
3171 size_t utmrdl_size, utrdl_size, ucdl_size;
3172
3173 /* Allocate memory for UTP command descriptors */
3174 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003175 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
3176 ucdl_size,
3177 &hba->ucdl_dma_addr,
3178 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303179
3180 /*
3181 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3182 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3183 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3184 * be aligned to 128 bytes as well
3185 */
3186 if (!hba->ucdl_base_addr ||
3187 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303188 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303189 "Command Descriptor Memory allocation failed\n");
3190 goto out;
3191 }
3192
3193 /*
3194 * Allocate memory for UTP Transfer descriptors
3195 * UFSHCI requires 1024 byte alignment of UTRD
3196 */
3197 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09003198 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
3199 utrdl_size,
3200 &hba->utrdl_dma_addr,
3201 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303202 if (!hba->utrdl_base_addr ||
3203 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303204 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303205 "Transfer Descriptor Memory allocation failed\n");
3206 goto out;
3207 }
3208
3209 /*
3210 * Allocate memory for UTP Task Management descriptors
3211 * UFSHCI requires 1024 byte alignment of UTMRD
3212 */
3213 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09003214 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
3215 utmrdl_size,
3216 &hba->utmrdl_dma_addr,
3217 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303218 if (!hba->utmrdl_base_addr ||
3219 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303220 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303221 "Task Management Descriptor Memory allocation failed\n");
3222 goto out;
3223 }
3224
3225 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003226 hba->lrb = devm_kzalloc(hba->dev,
3227 hba->nutrs * sizeof(struct ufshcd_lrb),
3228 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303229 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303230 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303231 goto out;
3232 }
3233 return 0;
3234out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303235 return -ENOMEM;
3236}
3237
3238/**
3239 * ufshcd_host_memory_configure - configure local reference block with
3240 * memory offsets
3241 * @hba: per adapter instance
3242 *
3243 * Configure Host memory space
3244 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3245 * address.
3246 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3247 * and PRDT offset.
3248 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3249 * into local reference block.
3250 */
3251static void ufshcd_host_memory_configure(struct ufs_hba *hba)
3252{
3253 struct utp_transfer_cmd_desc *cmd_descp;
3254 struct utp_transfer_req_desc *utrdlp;
3255 dma_addr_t cmd_desc_dma_addr;
3256 dma_addr_t cmd_desc_element_addr;
3257 u16 response_offset;
3258 u16 prdt_offset;
3259 int cmd_desc_size;
3260 int i;
3261
3262 utrdlp = hba->utrdl_base_addr;
3263 cmd_descp = hba->ucdl_base_addr;
3264
3265 response_offset =
3266 offsetof(struct utp_transfer_cmd_desc, response_upiu);
3267 prdt_offset =
3268 offsetof(struct utp_transfer_cmd_desc, prd_table);
3269
3270 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
3271 cmd_desc_dma_addr = hba->ucdl_dma_addr;
3272
3273 for (i = 0; i < hba->nutrs; i++) {
3274 /* Configure UTRD with command descriptor base address */
3275 cmd_desc_element_addr =
3276 (cmd_desc_dma_addr + (cmd_desc_size * i));
3277 utrdlp[i].command_desc_base_addr_lo =
3278 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
3279 utrdlp[i].command_desc_base_addr_hi =
3280 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
3281
3282 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003283 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
3284 utrdlp[i].response_upiu_offset =
3285 cpu_to_le16(response_offset);
3286 utrdlp[i].prd_table_offset =
3287 cpu_to_le16(prdt_offset);
3288 utrdlp[i].response_upiu_length =
3289 cpu_to_le16(ALIGNED_UPIU_SIZE);
3290 } else {
3291 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303292 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003293 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303294 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003295 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05303296 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09003297 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303298
3299 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003300 hba->lrb[i].utrd_dma_addr = hba->utrdl_dma_addr +
3301 (i * sizeof(struct utp_transfer_req_desc));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303302 hba->lrb[i].ucd_req_ptr =
3303 (struct utp_upiu_req *)(cmd_descp + i);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003304 hba->lrb[i].ucd_req_dma_addr = cmd_desc_element_addr;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303305 hba->lrb[i].ucd_rsp_ptr =
3306 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003307 hba->lrb[i].ucd_rsp_dma_addr = cmd_desc_element_addr +
3308 response_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303309 hba->lrb[i].ucd_prdt_ptr =
3310 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003311 hba->lrb[i].ucd_prdt_dma_addr = cmd_desc_element_addr +
3312 prdt_offset;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303313 }
3314}
3315
3316/**
3317 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3318 * @hba: per adapter instance
3319 *
3320 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3321 * in order to initialize the Unipro link startup procedure.
3322 * Once the Unipro links are up, the device connected to the controller
3323 * is detected.
3324 *
3325 * Returns 0 on success, non-zero value on failure
3326 */
3327static int ufshcd_dme_link_startup(struct ufs_hba *hba)
3328{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303329 struct uic_command uic_cmd = {0};
3330 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303331
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303332 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
3333
3334 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3335 if (ret)
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003336 dev_dbg(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303337 "dme-link-startup: error code %d\n", ret);
3338 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303339}
3340
Yaniv Gardicad2e032015-03-31 17:37:14 +03003341static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
3342{
3343 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3344 unsigned long min_sleep_time_us;
3345
3346 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
3347 return;
3348
3349 /*
3350 * last_dme_cmd_tstamp will be 0 only for 1st call to
3351 * this function
3352 */
3353 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
3354 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
3355 } else {
3356 unsigned long delta =
3357 (unsigned long) ktime_to_us(
3358 ktime_sub(ktime_get(),
3359 hba->last_dme_cmd_tstamp));
3360
3361 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
3362 min_sleep_time_us =
3363 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
3364 else
3365 return; /* no more delay required */
3366 }
3367
3368 /* allow sleep for extra 50us if needed */
3369 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
3370}
3371
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303372/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303373 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3374 * @hba: per adapter instance
3375 * @attr_sel: uic command argument1
3376 * @attr_set: attribute set type as uic command argument2
3377 * @mib_val: setting value as uic command argument3
3378 * @peer: indicate whether peer or local
3379 *
3380 * Returns 0 on success, non-zero value on failure
3381 */
3382int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
3383 u8 attr_set, u32 mib_val, u8 peer)
3384{
3385 struct uic_command uic_cmd = {0};
3386 static const char *const action[] = {
3387 "dme-set",
3388 "dme-peer-set"
3389 };
3390 const char *set = action[!!peer];
3391 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003392 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303393
3394 uic_cmd.command = peer ?
3395 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
3396 uic_cmd.argument1 = attr_sel;
3397 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
3398 uic_cmd.argument3 = mib_val;
3399
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003400 do {
3401 /* for peer attributes we retry upon failure */
3402 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3403 if (ret)
3404 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
3405 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
3406 } while (ret && peer && --retries);
3407
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003408 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003409 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003410 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
3411 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303412
3413 return ret;
3414}
3415EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
3416
3417/**
3418 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3419 * @hba: per adapter instance
3420 * @attr_sel: uic command argument1
3421 * @mib_val: the value of the attribute as returned by the UIC command
3422 * @peer: indicate whether peer or local
3423 *
3424 * Returns 0 on success, non-zero value on failure
3425 */
3426int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
3427 u32 *mib_val, u8 peer)
3428{
3429 struct uic_command uic_cmd = {0};
3430 static const char *const action[] = {
3431 "dme-get",
3432 "dme-peer-get"
3433 };
3434 const char *get = action[!!peer];
3435 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003436 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003437 struct ufs_pa_layer_attr orig_pwr_info;
3438 struct ufs_pa_layer_attr temp_pwr_info;
3439 bool pwr_mode_change = false;
3440
3441 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
3442 orig_pwr_info = hba->pwr_info;
3443 temp_pwr_info = orig_pwr_info;
3444
3445 if (orig_pwr_info.pwr_tx == FAST_MODE ||
3446 orig_pwr_info.pwr_rx == FAST_MODE) {
3447 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
3448 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
3449 pwr_mode_change = true;
3450 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
3451 orig_pwr_info.pwr_rx == SLOW_MODE) {
3452 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
3453 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
3454 pwr_mode_change = true;
3455 }
3456 if (pwr_mode_change) {
3457 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
3458 if (ret)
3459 goto out;
3460 }
3461 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303462
3463 uic_cmd.command = peer ?
3464 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
3465 uic_cmd.argument1 = attr_sel;
3466
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003467 do {
3468 /* for peer attributes we retry upon failure */
3469 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
3470 if (ret)
3471 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
3472 get, UIC_GET_ATTR_ID(attr_sel), ret);
3473 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303474
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003475 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003476 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08003477 get, UIC_GET_ATTR_ID(attr_sel),
3478 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02003479
3480 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303481 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03003482
3483 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
3484 && pwr_mode_change)
3485 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303486out:
3487 return ret;
3488}
3489EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
3490
3491/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003492 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3493 * state) and waits for it to take effect.
3494 *
3495 * @hba: per adapter instance
3496 * @cmd: UIC command to execute
3497 *
3498 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3499 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3500 * and device UniPro link and hence it's final completion would be indicated by
3501 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3502 * addition to normal UIC command completion Status (UCCS). This function only
3503 * returns after the relevant status bits indicate the completion.
3504 *
3505 * Returns 0 on success, non-zero value on failure
3506 */
3507static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
3508{
3509 struct completion uic_async_done;
3510 unsigned long flags;
3511 u8 status;
3512 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003513 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003514
3515 mutex_lock(&hba->uic_cmd_mutex);
3516 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03003517 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003518
3519 spin_lock_irqsave(hba->host->host_lock, flags);
3520 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003521 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
3522 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
3523 /*
3524 * Make sure UIC command completion interrupt is disabled before
3525 * issuing UIC command.
3526 */
3527 wmb();
3528 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003529 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003530 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
3531 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003532 if (ret) {
3533 dev_err(hba->dev,
3534 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3535 cmd->command, cmd->argument3, ret);
3536 goto out;
3537 }
3538
3539 if (!wait_for_completion_timeout(hba->uic_async_done,
3540 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
3541 dev_err(hba->dev,
3542 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3543 cmd->command, cmd->argument3);
3544 ret = -ETIMEDOUT;
3545 goto out;
3546 }
3547
3548 status = ufshcd_get_upmcrs(hba);
3549 if (status != PWR_LOCAL) {
3550 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09003551 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003552 cmd->command, status);
3553 ret = (status != PWR_OK) ? status : -1;
3554 }
3555out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08003556 if (ret) {
3557 ufshcd_print_host_state(hba);
3558 ufshcd_print_pwr_info(hba);
3559 ufshcd_print_host_regs(hba);
3560 }
3561
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003562 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003563 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003564 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003565 if (reenable_intr)
3566 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003567 spin_unlock_irqrestore(hba->host->host_lock, flags);
3568 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003569
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003570 return ret;
3571}
3572
3573/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303574 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3575 * using DME_SET primitives.
3576 * @hba: per adapter instance
3577 * @mode: powr mode value
3578 *
3579 * Returns 0 on success, non-zero value on failure
3580 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05303581static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303582{
3583 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003584 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303585
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003586 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
3587 ret = ufshcd_dme_set(hba,
3588 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
3589 if (ret) {
3590 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3591 __func__, ret);
3592 goto out;
3593 }
3594 }
3595
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303596 uic_cmd.command = UIC_CMD_DME_SET;
3597 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
3598 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003599 ufshcd_hold(hba, false);
3600 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
3601 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303602
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03003603out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003604 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003605}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303606
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003607static int ufshcd_link_recovery(struct ufs_hba *hba)
3608{
3609 int ret;
3610 unsigned long flags;
3611
3612 spin_lock_irqsave(hba->host->host_lock, flags);
3613 hba->ufshcd_state = UFSHCD_STATE_RESET;
3614 ufshcd_set_eh_in_progress(hba);
3615 spin_unlock_irqrestore(hba->host->host_lock, flags);
3616
3617 ret = ufshcd_host_reset_and_restore(hba);
3618
3619 spin_lock_irqsave(hba->host->host_lock, flags);
3620 if (ret)
3621 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3622 ufshcd_clear_eh_in_progress(hba);
3623 spin_unlock_irqrestore(hba->host->host_lock, flags);
3624
3625 if (ret)
3626 dev_err(hba->dev, "%s: link recovery failed, err %d",
3627 __func__, ret);
3628
3629 return ret;
3630}
3631
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003632static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003633{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003634 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003635 struct uic_command uic_cmd = {0};
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003636 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003637
Kiwoong Kimee32c902016-11-10 21:17:43 +09003638 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3639
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003640 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003641 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003642 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter",
3643 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003644
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003645 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003646 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3647 __func__, ret);
3648
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003649 /*
3650 * If link recovery fails then return error so that caller
3651 * don't retry the hibern8 enter again.
3652 */
3653 if (ufshcd_link_recovery(hba))
3654 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09003655 } else
3656 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3657 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003658
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003659 return ret;
3660}
3661
3662static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3663{
3664 int ret = 0, retries;
3665
3666 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3667 ret = __ufshcd_uic_hibern8_enter(hba);
3668 if (!ret || ret == -ENOLINK)
3669 goto out;
3670 }
3671out:
3672 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003673}
3674
3675static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
3676{
3677 struct uic_command uic_cmd = {0};
3678 int ret;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003679 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003680
Kiwoong Kimee32c902016-11-10 21:17:43 +09003681 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3682
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003683 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3684 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08003685 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit",
3686 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
3687
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303688 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003689 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3690 __func__, ret);
3691 ret = ufshcd_link_recovery(hba);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003692 } else {
Kiwoong Kimee32c902016-11-10 21:17:43 +09003693 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3694 POST_CHANGE);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08003695 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_get();
3696 hba->ufs_stats.hibern8_exit_cnt++;
3697 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303698
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303699 return ret;
3700}
3701
Yaniv Gardi50646362014-10-23 13:25:13 +03003702 /**
3703 * ufshcd_init_pwr_info - setting the POR (power on reset)
3704 * values in hba power info
3705 * @hba: per-adapter instance
3706 */
3707static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3708{
3709 hba->pwr_info.gear_rx = UFS_PWM_G1;
3710 hba->pwr_info.gear_tx = UFS_PWM_G1;
3711 hba->pwr_info.lane_rx = 1;
3712 hba->pwr_info.lane_tx = 1;
3713 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3714 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3715 hba->pwr_info.hs_rate = 0;
3716}
3717
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303718/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003719 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3720 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303721 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003722static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303723{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003724 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3725
3726 if (hba->max_pwr_info.is_valid)
3727 return 0;
3728
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003729 pwr_info->pwr_tx = FAST_MODE;
3730 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003731 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303732
3733 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003734 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3735 &pwr_info->lane_rx);
3736 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3737 &pwr_info->lane_tx);
3738
3739 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3740 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3741 __func__,
3742 pwr_info->lane_rx,
3743 pwr_info->lane_tx);
3744 return -EINVAL;
3745 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303746
3747 /*
3748 * First, get the maximum gears of HS speed.
3749 * If a zero value, it means there is no HSGEAR capability.
3750 * Then, get the maximum gears of PWM speed.
3751 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003752 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3753 if (!pwr_info->gear_rx) {
3754 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3755 &pwr_info->gear_rx);
3756 if (!pwr_info->gear_rx) {
3757 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3758 __func__, pwr_info->gear_rx);
3759 return -EINVAL;
3760 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003761 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303762 }
3763
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003764 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3765 &pwr_info->gear_tx);
3766 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303767 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003768 &pwr_info->gear_tx);
3769 if (!pwr_info->gear_tx) {
3770 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3771 __func__, pwr_info->gear_tx);
3772 return -EINVAL;
3773 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003774 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003775 }
3776
3777 hba->max_pwr_info.is_valid = true;
3778 return 0;
3779}
3780
3781static int ufshcd_change_power_mode(struct ufs_hba *hba,
3782 struct ufs_pa_layer_attr *pwr_mode)
3783{
3784 int ret;
3785
3786 /* if already configured to the requested pwr_mode */
3787 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
3788 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
3789 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
3790 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
3791 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
3792 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
3793 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
3794 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
3795 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303796 }
3797
3798 /*
3799 * Configure attributes for power mode change with below.
3800 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3801 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3802 * - PA_HSSERIES
3803 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003804 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
3805 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
3806 pwr_mode->lane_rx);
3807 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3808 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303809 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003810 else
3811 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303812
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003813 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
3814 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
3815 pwr_mode->lane_tx);
3816 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
3817 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303818 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003819 else
3820 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303821
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003822 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3823 pwr_mode->pwr_tx == FASTAUTO_MODE ||
3824 pwr_mode->pwr_rx == FAST_MODE ||
3825 pwr_mode->pwr_tx == FAST_MODE)
3826 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
3827 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303828
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003829 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
3830 | pwr_mode->pwr_tx);
3831
3832 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303833 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003834 "%s: power mode change failed %d\n", __func__, ret);
3835 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003836 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
3837 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003838
3839 memcpy(&hba->pwr_info, pwr_mode,
3840 sizeof(struct ufs_pa_layer_attr));
3841 }
3842
3843 return ret;
3844}
3845
3846/**
3847 * ufshcd_config_pwr_mode - configure a new power mode
3848 * @hba: per-adapter instance
3849 * @desired_pwr_mode: desired power configuration
3850 */
3851static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3852 struct ufs_pa_layer_attr *desired_pwr_mode)
3853{
3854 struct ufs_pa_layer_attr final_params = { 0 };
3855 int ret;
3856
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003857 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3858 desired_pwr_mode, &final_params);
3859
3860 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003861 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3862
3863 ret = ufshcd_change_power_mode(hba, &final_params);
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08003864 if (!ret)
3865 ufshcd_print_pwr_info(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303866
3867 return ret;
3868}
3869
3870/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303871 * ufshcd_complete_dev_init() - checks device readiness
3872 * hba: per-adapter instance
3873 *
3874 * Set fDeviceInit flag and poll until device toggles it.
3875 */
3876static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3877{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003878 int i;
3879 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303880 bool flag_res = 1;
3881
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003882 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3883 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303884 if (err) {
3885 dev_err(hba->dev,
3886 "%s setting fDeviceInit flag failed with error %d\n",
3887 __func__, err);
3888 goto out;
3889 }
3890
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003891 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3892 for (i = 0; i < 1000 && !err && flag_res; i++)
3893 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3894 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3895
Dolev Raviv68078d52013-07-30 00:35:58 +05303896 if (err)
3897 dev_err(hba->dev,
3898 "%s reading fDeviceInit flag failed with error %d\n",
3899 __func__, err);
3900 else if (flag_res)
3901 dev_err(hba->dev,
3902 "%s fDeviceInit was not cleared by the device\n",
3903 __func__);
3904
3905out:
3906 return err;
3907}
3908
3909/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303910 * ufshcd_make_hba_operational - Make UFS controller operational
3911 * @hba: per adapter instance
3912 *
3913 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003914 * 1. Enable required interrupts
3915 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003916 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003917 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303918 *
3919 * Returns 0 on success, non-zero value on failure
3920 */
3921static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3922{
3923 int err = 0;
3924 u32 reg;
3925
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303926 /* Enable required interrupts */
3927 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3928
3929 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003930 if (ufshcd_is_intr_aggr_allowed(hba))
3931 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3932 else
3933 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303934
3935 /* Configure UTRL and UTMRL base address registers */
3936 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3937 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3938 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3939 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3940 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3941 REG_UTP_TASK_REQ_LIST_BASE_L);
3942 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3943 REG_UTP_TASK_REQ_LIST_BASE_H);
3944
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303945 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003946 * Make sure base address and interrupt setup are updated before
3947 * enabling the run/stop registers below.
3948 */
3949 wmb();
3950
3951 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303952 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303953 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003954 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303955 if (!(ufshcd_get_lists_status(reg))) {
3956 ufshcd_enable_run_stop_reg(hba);
3957 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303958 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303959 "Host controller not ready to process requests");
3960 err = -EIO;
3961 goto out;
3962 }
3963
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303964out:
3965 return err;
3966}
3967
3968/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003969 * ufshcd_hba_stop - Send controller to reset state
3970 * @hba: per adapter instance
3971 * @can_sleep: perform sleep or just spin
3972 */
3973static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3974{
3975 int err;
3976
3977 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3978 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3979 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3980 10, 1, can_sleep);
3981 if (err)
3982 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3983}
3984
3985/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303986 * ufshcd_hba_enable - initialize the controller
3987 * @hba: per adapter instance
3988 *
3989 * The controller resets itself and controller firmware initialization
3990 * sequence kicks off. When controller is ready it will set
3991 * the Host Controller Enable bit to 1.
3992 *
3993 * Returns 0 on success, non-zero value on failure
3994 */
3995static int ufshcd_hba_enable(struct ufs_hba *hba)
3996{
3997 int retry;
3998
3999 /*
4000 * msleep of 1 and 5 used in this function might result in msleep(20),
4001 * but it was necessary to send the UFS FPGA to reset mode during
4002 * development and testing of this driver. msleep can be changed to
4003 * mdelay and retry count can be reduced based on the controller.
4004 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004005 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304006 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02004007 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304008
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004009 /* UniPro link is disabled at this point */
4010 ufshcd_set_link_off(hba);
4011
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004012 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004013
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304014 /* start controller initialization sequence */
4015 ufshcd_hba_start(hba);
4016
4017 /*
4018 * To initialize a UFS host controller HCE bit must be set to 1.
4019 * During initialization the HCE bit value changes from 1->0->1.
4020 * When the host controller completes initialization sequence
4021 * it sets the value of HCE bit to 1. The same HCE bit is read back
4022 * to check if the controller has completed initialization sequence.
4023 * So without this delay the value HCE = 1, set in the previous
4024 * instruction might be read back.
4025 * This delay can be changed based on the controller.
4026 */
4027 msleep(1);
4028
4029 /* wait for the host controller to complete initialization */
4030 retry = 10;
4031 while (ufshcd_is_hba_active(hba)) {
4032 if (retry) {
4033 retry--;
4034 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304035 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304036 "Controller enable failed\n");
4037 return -EIO;
4038 }
4039 msleep(5);
4040 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004041
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004042 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004043 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004044
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004045 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004046
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304047 return 0;
4048}
4049
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004050static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
4051{
4052 int tx_lanes, i, err = 0;
4053
4054 if (!peer)
4055 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4056 &tx_lanes);
4057 else
4058 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
4059 &tx_lanes);
4060 for (i = 0; i < tx_lanes; i++) {
4061 if (!peer)
4062 err = ufshcd_dme_set(hba,
4063 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4064 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4065 0);
4066 else
4067 err = ufshcd_dme_peer_set(hba,
4068 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
4069 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
4070 0);
4071 if (err) {
4072 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4073 __func__, peer, i, err);
4074 break;
4075 }
4076 }
4077
4078 return err;
4079}
4080
4081static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
4082{
4083 return ufshcd_disable_tx_lcc(hba, true);
4084}
4085
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304086/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304087 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304088 * @hba: per adapter instance
4089 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304090 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304091 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304092static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304093{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304094 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004095 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004096 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304097
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004098 /*
4099 * If UFS device isn't active then we will have to issue link startup
4100 * 2 times to make sure the device state move to active.
4101 */
4102 if (!ufshcd_is_ufs_dev_active(hba))
4103 link_startup_again = true;
4104
4105link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004106 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004107 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304108
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004109 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004110
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004111 /* check if device is detected by inter-connect layer */
4112 if (!ret && !ufshcd_is_device_present(hba)) {
4113 dev_err(hba->dev, "%s: Device not present\n", __func__);
4114 ret = -ENXIO;
4115 goto out;
4116 }
4117
4118 /*
4119 * DME link lost indication is only received when link is up,
4120 * but we can't be sure if the link is up until link startup
4121 * succeeds. So reset the local Uni-Pro and try again.
4122 */
4123 if (ret && ufshcd_hba_enable(hba))
4124 goto out;
4125 } while (ret && retries--);
4126
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304127 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004128 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304129 goto out;
4130
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08004131 if (link_startup_again) {
4132 link_startup_again = false;
4133 retries = DME_LINKSTARTUP_RETRIES;
4134 goto link_startup;
4135 }
4136
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08004137 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4138 ufshcd_init_pwr_info(hba);
4139 ufshcd_print_pwr_info(hba);
4140
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03004141 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
4142 ret = ufshcd_disable_device_tx_lcc(hba);
4143 if (ret)
4144 goto out;
4145 }
4146
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004147 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004148 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
4149 if (ret)
4150 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004151
4152 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304153out:
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004154 if (ret) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304155 dev_err(hba->dev, "link startup failed %d\n", ret);
Venkat Gopalakrishnan7942f7b2017-02-03 16:58:24 -08004156 ufshcd_print_host_state(hba);
4157 ufshcd_print_pwr_info(hba);
4158 ufshcd_print_host_regs(hba);
4159 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304160 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304161}
4162
4163/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304164 * ufshcd_verify_dev_init() - Verify device initialization
4165 * @hba: per-adapter instance
4166 *
4167 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4168 * device Transport Protocol (UTP) layer is ready after a reset.
4169 * If the UTP layer at the device side is not initialized, it may
4170 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4171 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4172 */
4173static int ufshcd_verify_dev_init(struct ufs_hba *hba)
4174{
4175 int err = 0;
4176 int retries;
4177
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004178 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304179 mutex_lock(&hba->dev_cmd.lock);
4180 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
4181 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
4182 NOP_OUT_TIMEOUT);
4183
4184 if (!err || err == -ETIMEDOUT)
4185 break;
4186
4187 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
4188 }
4189 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004190 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304191
4192 if (err)
4193 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
4194 return err;
4195}
4196
4197/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004198 * ufshcd_set_queue_depth - set lun queue depth
4199 * @sdev: pointer to SCSI device
4200 *
4201 * Read bLUQueueDepth value and activate scsi tagged command
4202 * queueing. For WLUN, queue depth is set to 1. For best-effort
4203 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4204 * value that host can queue.
4205 */
4206static void ufshcd_set_queue_depth(struct scsi_device *sdev)
4207{
4208 int ret = 0;
4209 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08004210 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004211 struct ufs_hba *hba;
4212
4213 hba = shost_priv(sdev->host);
4214
4215 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08004216 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
4217 /* Read descriptor*/
4218 ret = ufshcd_read_unit_desc_param(hba,
4219 ufshcd_scsi_to_upiu_lun(sdev->lun),
4220 UNIT_DESC_PARAM_LU_Q_DEPTH,
4221 &lun_qdepth,
4222 sizeof(lun_qdepth));
4223 if (!ret || ret == -ENOTSUPP)
4224 break;
4225
4226 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
4227 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004228
4229 /* Some WLUN doesn't support unit descriptor */
4230 if (ret == -EOPNOTSUPP)
4231 lun_qdepth = 1;
4232 else if (!lun_qdepth)
4233 /* eventually, we can figure out the real queue depth */
4234 lun_qdepth = hba->nutrs;
4235 else
4236 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
4237
4238 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
4239 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004240 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004241}
4242
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004243/*
4244 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4245 * @hba: per-adapter instance
4246 * @lun: UFS device lun id
4247 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4248 *
4249 * Returns 0 in case of success and b_lu_write_protect status would be returned
4250 * @b_lu_write_protect parameter.
4251 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4252 * Returns -EINVAL in case of invalid parameters passed to this function.
4253 */
4254static int ufshcd_get_lu_wp(struct ufs_hba *hba,
4255 u8 lun,
4256 u8 *b_lu_write_protect)
4257{
4258 int ret;
4259
4260 if (!b_lu_write_protect)
4261 ret = -EINVAL;
4262 /*
4263 * According to UFS device spec, RPMB LU can't be write
4264 * protected so skip reading bLUWriteProtect parameter for
4265 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4266 */
4267 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
4268 ret = -ENOTSUPP;
4269 else
4270 ret = ufshcd_read_unit_desc_param(hba,
4271 lun,
4272 UNIT_DESC_PARAM_LU_WR_PROTECT,
4273 b_lu_write_protect,
4274 sizeof(*b_lu_write_protect));
4275 return ret;
4276}
4277
4278/**
4279 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4280 * status
4281 * @hba: per-adapter instance
4282 * @sdev: pointer to SCSI device
4283 *
4284 */
4285static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
4286 struct scsi_device *sdev)
4287{
4288 if (hba->dev_info.f_power_on_wp_en &&
4289 !hba->dev_info.is_lu_power_on_wp) {
4290 u8 b_lu_write_protect;
4291
4292 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
4293 &b_lu_write_protect) &&
4294 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
4295 hba->dev_info.is_lu_power_on_wp = true;
4296 }
4297}
4298
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004299/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304300 * ufshcd_slave_alloc - handle initial SCSI device configurations
4301 * @sdev: pointer to SCSI device
4302 *
4303 * Returns success
4304 */
4305static int ufshcd_slave_alloc(struct scsi_device *sdev)
4306{
4307 struct ufs_hba *hba;
4308
4309 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304310
4311 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4312 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304313
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304314 /* allow SCSI layer to restart the device in case of errors */
4315 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004316
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03004317 /* REPORT SUPPORTED OPERATION CODES is not supported */
4318 sdev->no_report_opcodes = 1;
4319
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004320
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004321 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004322
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004323 ufshcd_get_lu_power_on_wp_status(hba, sdev);
4324
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004325 return 0;
4326}
4327
4328/**
4329 * ufshcd_change_queue_depth - change queue depth
4330 * @sdev: pointer to SCSI device
4331 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004332 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004333 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004334 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004335static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004336{
4337 struct ufs_hba *hba = shost_priv(sdev->host);
4338
4339 if (depth > hba->nutrs)
4340 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01004341 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304342}
4343
4344/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004345 * ufshcd_slave_configure - adjust SCSI device configurations
4346 * @sdev: pointer to SCSI device
4347 */
4348static int ufshcd_slave_configure(struct scsi_device *sdev)
4349{
4350 struct request_queue *q = sdev->request_queue;
4351
4352 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
4353 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
4354
4355 return 0;
4356}
4357
4358/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304359 * ufshcd_slave_destroy - remove SCSI device configurations
4360 * @sdev: pointer to SCSI device
4361 */
4362static void ufshcd_slave_destroy(struct scsi_device *sdev)
4363{
4364 struct ufs_hba *hba;
4365
4366 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004367 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004368 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
4369 unsigned long flags;
4370
4371 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004372 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004373 spin_unlock_irqrestore(hba->host->host_lock, flags);
4374 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304375}
4376
4377/**
4378 * ufshcd_task_req_compl - handle task management request completion
4379 * @hba: per adapter instance
4380 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304381 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304382 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304383 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304384 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304385static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304386{
4387 struct utp_task_req_desc *task_req_descp;
4388 struct utp_upiu_task_rsp *task_rsp_upiup;
4389 unsigned long flags;
4390 int ocs_value;
4391 int task_result;
4392
4393 spin_lock_irqsave(hba->host->host_lock, flags);
4394
4395 /* Clear completed tasks from outstanding_tasks */
4396 __clear_bit(index, &hba->outstanding_tasks);
4397
4398 task_req_descp = hba->utmrdl_base_addr;
4399 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
4400
4401 if (ocs_value == OCS_SUCCESS) {
4402 task_rsp_upiup = (struct utp_upiu_task_rsp *)
4403 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09004404 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
4405 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304406 if (resp)
4407 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304408 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304409 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
4410 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304411 }
4412 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304413
4414 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304415}
4416
4417/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304418 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4419 * @lrb: pointer to local reference block of completed command
4420 * @scsi_status: SCSI command status
4421 *
4422 * Returns value base on SCSI command status
4423 */
4424static inline int
4425ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
4426{
4427 int result = 0;
4428
4429 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304430 case SAM_STAT_CHECK_CONDITION:
4431 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432 case SAM_STAT_GOOD:
4433 result |= DID_OK << 16 |
4434 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304435 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304436 break;
4437 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304438 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304439 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05304440 ufshcd_copy_sense_data(lrbp);
4441 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304442 break;
4443 default:
4444 result |= DID_ERROR << 16;
4445 break;
4446 } /* end of switch */
4447
4448 return result;
4449}
4450
4451/**
4452 * ufshcd_transfer_rsp_status - Get overall status of the response
4453 * @hba: per adapter instance
4454 * @lrb: pointer to local reference block of completed command
4455 *
4456 * Returns result of the command to notify SCSI midlayer
4457 */
4458static inline int
4459ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
4460{
4461 int result = 0;
4462 int scsi_status;
4463 int ocs;
4464
4465 /* overall command status of utrd */
4466 ocs = ufshcd_get_tr_ocs(lrbp);
4467
4468 switch (ocs) {
4469 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304470 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004471 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304472 switch (result) {
4473 case UPIU_TRANSACTION_RESPONSE:
4474 /*
4475 * get the response UPIU result to extract
4476 * the SCSI command status
4477 */
4478 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
4479
4480 /*
4481 * get the result based on SCSI status response
4482 * to notify the SCSI midlayer of the command status
4483 */
4484 scsi_status = result & MASK_SCSI_STATUS;
4485 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304486
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02004487 /*
4488 * Currently we are only supporting BKOPs exception
4489 * events hence we can ignore BKOPs exception event
4490 * during power management callbacks. BKOPs exception
4491 * event is not expected to be raised in runtime suspend
4492 * callback as it allows the urgent bkops.
4493 * During system suspend, we are anyway forcefully
4494 * disabling the bkops and if urgent bkops is needed
4495 * it will be enabled on system resume. Long term
4496 * solution could be to abort the system suspend if
4497 * UFS device needs urgent BKOPs.
4498 */
4499 if (!hba->pm_op_in_progress &&
4500 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304501 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304502 break;
4503 case UPIU_TRANSACTION_REJECT_UPIU:
4504 /* TODO: handle Reject UPIU Response */
4505 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304506 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304507 "Reject UPIU not fully implemented\n");
4508 break;
4509 default:
4510 result = DID_ERROR << 16;
4511 dev_err(hba->dev,
4512 "Unexpected request response code = %x\n",
4513 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304514 break;
4515 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304516 break;
4517 case OCS_ABORTED:
4518 result |= DID_ABORT << 16;
4519 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304520 case OCS_INVALID_COMMAND_STATUS:
4521 result |= DID_REQUEUE << 16;
4522 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304523 case OCS_INVALID_CMD_TABLE_ATTR:
4524 case OCS_INVALID_PRDT_ATTR:
4525 case OCS_MISMATCH_DATA_BUF_SIZE:
4526 case OCS_MISMATCH_RESP_UPIU_SIZE:
4527 case OCS_PEER_COMM_FAILURE:
4528 case OCS_FATAL_ERROR:
4529 default:
4530 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304531 dev_err(hba->dev,
Dolev Ravivff8e20c2016-12-22 18:42:18 -08004532 "OCS error from controller = %x for tag %d\n",
4533 ocs, lrbp->task_tag);
4534 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08004535 ufshcd_print_host_state(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304536 break;
4537 } /* end of switch */
4538
Dolev Raviv66cc8202016-12-22 18:39:42 -08004539 if (host_byte(result) != DID_OK)
4540 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304541 return result;
4542}
4543
4544/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304545 * ufshcd_uic_cmd_compl - handle completion of uic command
4546 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304547 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304548 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304549static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304550{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304551 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304552 hba->active_uic_cmd->argument2 |=
4553 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05304554 hba->active_uic_cmd->argument3 =
4555 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304556 complete(&hba->active_uic_cmd->done);
4557 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304558
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004559 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
4560 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304561}
4562
4563/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004564 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304565 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004566 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304567 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004568static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
4569 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304570{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304571 struct ufshcd_lrb *lrbp;
4572 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304573 int result;
4574 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004575
Dolev Ravive9d501b2014-07-01 12:22:37 +03004576 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
4577 lrbp = &hba->lrb[index];
4578 cmd = lrbp->cmd;
4579 if (cmd) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004580 ufshcd_add_command_trace(hba, index, "complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004581 result = ufshcd_transfer_rsp_status(hba, lrbp);
4582 scsi_dma_unmap(cmd);
4583 cmd->result = result;
4584 /* Mark completed command as NULL in LRB */
4585 lrbp->cmd = NULL;
4586 clear_bit_unlock(index, &hba->lrb_in_use);
4587 /* Do not touch lrbp after scsi done */
4588 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004589 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01004590 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
4591 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Lee Susman1a07f2d2016-12-22 18:42:03 -08004592 if (hba->dev_cmd.complete) {
4593 ufshcd_add_command_trace(hba, index,
4594 "dev_complete");
Dolev Ravive9d501b2014-07-01 12:22:37 +03004595 complete(hba->dev_cmd.complete);
Lee Susman1a07f2d2016-12-22 18:42:03 -08004596 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03004597 }
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08004598 if (ufshcd_is_clkscaling_supported(hba))
4599 hba->clk_scaling.active_reqs--;
4600 if (ufshcd_is_clkscaling_supported(hba))
4601 hba->clk_scaling.active_reqs--;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004602 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304603
4604 /* clear corresponding bits of completed commands */
4605 hba->outstanding_reqs ^= completed_reqs;
4606
Sahitya Tummala856b3482014-09-25 15:32:34 +03004607 ufshcd_clk_scaling_update_busy(hba);
4608
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304609 /* we might have free'd some tags above */
4610 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304611}
4612
4613/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004614 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4615 * @hba: per adapter instance
4616 */
4617static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
4618{
4619 unsigned long completed_reqs;
4620 u32 tr_doorbell;
4621
4622 /* Resetting interrupt aggregation counters first and reading the
4623 * DOOR_BELL afterward allows us to handle all the completed requests.
4624 * In order to prevent other interrupts starvation the DB is read once
4625 * after reset. The down side of this solution is the possibility of
4626 * false interrupt if device completes another request after resetting
4627 * aggregation and before reading the DB.
4628 */
4629 if (ufshcd_is_intr_aggr_allowed(hba))
4630 ufshcd_reset_intr_aggr(hba);
4631
4632 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4633 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
4634
4635 __ufshcd_transfer_req_compl(hba, completed_reqs);
4636}
4637
4638/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304639 * ufshcd_disable_ee - disable exception event
4640 * @hba: per-adapter instance
4641 * @mask: exception event to disable
4642 *
4643 * Disables exception event in the device so that the EVENT_ALERT
4644 * bit is not set.
4645 *
4646 * Returns zero on success, non-zero error value on failure.
4647 */
4648static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4649{
4650 int err = 0;
4651 u32 val;
4652
4653 if (!(hba->ee_ctrl_mask & mask))
4654 goto out;
4655
4656 val = hba->ee_ctrl_mask & ~mask;
4657 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004658 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304659 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4660 if (!err)
4661 hba->ee_ctrl_mask &= ~mask;
4662out:
4663 return err;
4664}
4665
4666/**
4667 * ufshcd_enable_ee - enable exception event
4668 * @hba: per-adapter instance
4669 * @mask: exception event to enable
4670 *
4671 * Enable corresponding exception event in the device to allow
4672 * device to alert host in critical scenarios.
4673 *
4674 * Returns zero on success, non-zero error value on failure.
4675 */
4676static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4677{
4678 int err = 0;
4679 u32 val;
4680
4681 if (hba->ee_ctrl_mask & mask)
4682 goto out;
4683
4684 val = hba->ee_ctrl_mask | mask;
4685 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004686 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304687 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4688 if (!err)
4689 hba->ee_ctrl_mask |= mask;
4690out:
4691 return err;
4692}
4693
4694/**
4695 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4696 * @hba: per-adapter instance
4697 *
4698 * Allow device to manage background operations on its own. Enabling
4699 * this might lead to inconsistent latencies during normal data transfers
4700 * as the device is allowed to manage its own way of handling background
4701 * operations.
4702 *
4703 * Returns zero on success, non-zero on failure.
4704 */
4705static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4706{
4707 int err = 0;
4708
4709 if (hba->auto_bkops_enabled)
4710 goto out;
4711
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004712 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304713 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4714 if (err) {
4715 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4716 __func__, err);
4717 goto out;
4718 }
4719
4720 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004721 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304722
4723 /* No need of URGENT_BKOPS exception from the device */
4724 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4725 if (err)
4726 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4727 __func__, err);
4728out:
4729 return err;
4730}
4731
4732/**
4733 * ufshcd_disable_auto_bkops - block device in doing background operations
4734 * @hba: per-adapter instance
4735 *
4736 * Disabling background operations improves command response latency but
4737 * has drawback of device moving into critical state where the device is
4738 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4739 * host is idle so that BKOPS are managed effectively without any negative
4740 * impacts.
4741 *
4742 * Returns zero on success, non-zero on failure.
4743 */
4744static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
4745{
4746 int err = 0;
4747
4748 if (!hba->auto_bkops_enabled)
4749 goto out;
4750
4751 /*
4752 * If host assisted BKOPs is to be enabled, make sure
4753 * urgent bkops exception is allowed.
4754 */
4755 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
4756 if (err) {
4757 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
4758 __func__, err);
4759 goto out;
4760 }
4761
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004762 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304763 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4764 if (err) {
4765 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
4766 __func__, err);
4767 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4768 goto out;
4769 }
4770
4771 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004772 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304773out:
4774 return err;
4775}
4776
4777/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004778 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304779 * @hba: per adapter instance
4780 *
4781 * After a device reset the device may toggle the BKOPS_EN flag
4782 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004783 * as well. This function would change the auto-bkops state based on
4784 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304785 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004786static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304787{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004788 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
4789 hba->auto_bkops_enabled = false;
4790 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
4791 ufshcd_enable_auto_bkops(hba);
4792 } else {
4793 hba->auto_bkops_enabled = true;
4794 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
4795 ufshcd_disable_auto_bkops(hba);
4796 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304797}
4798
4799static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
4800{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004801 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304802 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
4803}
4804
4805/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004806 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4807 * @hba: per-adapter instance
4808 * @status: bkops_status value
4809 *
4810 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4811 * flag in the device to permit background operations if the device
4812 * bkops_status is greater than or equal to "status" argument passed to
4813 * this function, disable otherwise.
4814 *
4815 * Returns 0 for success, non-zero in case of failure.
4816 *
4817 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4818 * to know whether auto bkops is enabled or disabled after this function
4819 * returns control to it.
4820 */
4821static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
4822 enum bkops_status status)
4823{
4824 int err;
4825 u32 curr_status = 0;
4826
4827 err = ufshcd_get_bkops_status(hba, &curr_status);
4828 if (err) {
4829 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4830 __func__, err);
4831 goto out;
4832 } else if (curr_status > BKOPS_STATUS_MAX) {
4833 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
4834 __func__, curr_status);
4835 err = -EINVAL;
4836 goto out;
4837 }
4838
4839 if (curr_status >= status)
4840 err = ufshcd_enable_auto_bkops(hba);
4841 else
4842 err = ufshcd_disable_auto_bkops(hba);
4843out:
4844 return err;
4845}
4846
4847/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304848 * ufshcd_urgent_bkops - handle urgent bkops exception event
4849 * @hba: per-adapter instance
4850 *
4851 * Enable fBackgroundOpsEn flag in the device to permit background
4852 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004853 *
4854 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4855 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304856 */
4857static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4858{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004859 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304860}
4861
4862static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4863{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004864 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304865 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4866}
4867
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004868static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4869{
4870 int err;
4871 u32 curr_status = 0;
4872
4873 if (hba->is_urgent_bkops_lvl_checked)
4874 goto enable_auto_bkops;
4875
4876 err = ufshcd_get_bkops_status(hba, &curr_status);
4877 if (err) {
4878 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4879 __func__, err);
4880 goto out;
4881 }
4882
4883 /*
4884 * We are seeing that some devices are raising the urgent bkops
4885 * exception events even when BKOPS status doesn't indicate performace
4886 * impacted or critical. Handle these device by determining their urgent
4887 * bkops status at runtime.
4888 */
4889 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4890 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4891 __func__, curr_status);
4892 /* update the current status as the urgent bkops level */
4893 hba->urgent_bkops_lvl = curr_status;
4894 hba->is_urgent_bkops_lvl_checked = true;
4895 }
4896
4897enable_auto_bkops:
4898 err = ufshcd_enable_auto_bkops(hba);
4899out:
4900 if (err < 0)
4901 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4902 __func__, err);
4903}
4904
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304905/**
4906 * ufshcd_exception_event_handler - handle exceptions raised by device
4907 * @work: pointer to work data
4908 *
4909 * Read bExceptionEventStatus attribute from the device and handle the
4910 * exception event accordingly.
4911 */
4912static void ufshcd_exception_event_handler(struct work_struct *work)
4913{
4914 struct ufs_hba *hba;
4915 int err;
4916 u32 status = 0;
4917 hba = container_of(work, struct ufs_hba, eeh_work);
4918
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304919 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304920 err = ufshcd_get_ee_status(hba, &status);
4921 if (err) {
4922 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4923 __func__, err);
4924 goto out;
4925 }
4926
4927 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004928
4929 if (status & MASK_EE_URGENT_BKOPS)
4930 ufshcd_bkops_exception_event_handler(hba);
4931
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304932out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304933 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304934 return;
4935}
4936
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004937/* Complete requests that have door-bell cleared */
4938static void ufshcd_complete_requests(struct ufs_hba *hba)
4939{
4940 ufshcd_transfer_req_compl(hba);
4941 ufshcd_tmc_handler(hba);
4942}
4943
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304944/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004945 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4946 * to recover from the DL NAC errors or not.
4947 * @hba: per-adapter instance
4948 *
4949 * Returns true if error handling is required, false otherwise
4950 */
4951static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4952{
4953 unsigned long flags;
4954 bool err_handling = true;
4955
4956 spin_lock_irqsave(hba->host->host_lock, flags);
4957 /*
4958 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4959 * device fatal error and/or DL NAC & REPLAY timeout errors.
4960 */
4961 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4962 goto out;
4963
4964 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4965 ((hba->saved_err & UIC_ERROR) &&
4966 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4967 goto out;
4968
4969 if ((hba->saved_err & UIC_ERROR) &&
4970 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4971 int err;
4972 /*
4973 * wait for 50ms to see if we can get any other errors or not.
4974 */
4975 spin_unlock_irqrestore(hba->host->host_lock, flags);
4976 msleep(50);
4977 spin_lock_irqsave(hba->host->host_lock, flags);
4978
4979 /*
4980 * now check if we have got any other severe errors other than
4981 * DL NAC error?
4982 */
4983 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4984 ((hba->saved_err & UIC_ERROR) &&
4985 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4986 goto out;
4987
4988 /*
4989 * As DL NAC is the only error received so far, send out NOP
4990 * command to confirm if link is still active or not.
4991 * - If we don't get any response then do error recovery.
4992 * - If we get response then clear the DL NAC error bit.
4993 */
4994
4995 spin_unlock_irqrestore(hba->host->host_lock, flags);
4996 err = ufshcd_verify_dev_init(hba);
4997 spin_lock_irqsave(hba->host->host_lock, flags);
4998
4999 if (err)
5000 goto out;
5001
5002 /* Link seems to be alive hence ignore the DL NAC errors */
5003 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
5004 hba->saved_err &= ~UIC_ERROR;
5005 /* clear NAC error */
5006 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5007 if (!hba->saved_uic_err) {
5008 err_handling = false;
5009 goto out;
5010 }
5011 }
5012out:
5013 spin_unlock_irqrestore(hba->host->host_lock, flags);
5014 return err_handling;
5015}
5016
5017/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305018 * ufshcd_err_handler - handle UFS errors that require s/w attention
5019 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305020 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305021static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305022{
5023 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305024 unsigned long flags;
5025 u32 err_xfer = 0;
5026 u32 err_tm = 0;
5027 int err = 0;
5028 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005029 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305030
5031 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305032
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305033 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005034 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305035
5036 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005037 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305038 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305039
5040 hba->ufshcd_state = UFSHCD_STATE_RESET;
5041 ufshcd_set_eh_in_progress(hba);
5042
5043 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005044 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02005045
5046 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5047 bool ret;
5048
5049 spin_unlock_irqrestore(hba->host->host_lock, flags);
5050 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5051 ret = ufshcd_quirk_dl_nac_errors(hba);
5052 spin_lock_irqsave(hba->host->host_lock, flags);
5053 if (!ret)
5054 goto skip_err_handling;
5055 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005056 if ((hba->saved_err & INT_FATAL_ERRORS) ||
5057 ((hba->saved_err & UIC_ERROR) &&
5058 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
5059 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
5060 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
5061 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305062
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005063 /*
5064 * if host reset is required then skip clearing the pending
5065 * transfers forcefully because they will automatically get
5066 * cleared after link startup.
5067 */
5068 if (needs_reset)
5069 goto skip_pending_xfer_clear;
5070
5071 /* release lock as clear command might sleep */
5072 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305073 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005074 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
5075 if (ufshcd_clear_cmd(hba, tag)) {
5076 err_xfer = true;
5077 goto lock_skip_pending_xfer_clear;
5078 }
5079 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305080
5081 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005082 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
5083 if (ufshcd_clear_tm_cmd(hba, tag)) {
5084 err_tm = true;
5085 goto lock_skip_pending_xfer_clear;
5086 }
5087 }
5088
5089lock_skip_pending_xfer_clear:
5090 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305091
5092 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005093 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305094
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005095 if (err_xfer || err_tm)
5096 needs_reset = true;
5097
5098skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305099 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005100 if (needs_reset) {
5101 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
5102
5103 /*
5104 * ufshcd_reset_and_restore() does the link reinitialization
5105 * which will need atleast one empty doorbell slot to send the
5106 * device management commands (NOP and query commands).
5107 * If there is no slot empty at this moment then free up last
5108 * slot forcefully.
5109 */
5110 if (hba->outstanding_reqs == max_doorbells)
5111 __ufshcd_transfer_req_compl(hba,
5112 (1UL << (hba->nutrs - 1)));
5113
5114 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305115 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005116 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305117 if (err) {
5118 dev_err(hba->dev, "%s: reset and restore failed\n",
5119 __func__);
5120 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5121 }
5122 /*
5123 * Inform scsi mid-layer that we did reset and allow to handle
5124 * Unit Attention properly.
5125 */
5126 scsi_report_bus_reset(hba->host, 0);
5127 hba->saved_err = 0;
5128 hba->saved_uic_err = 0;
5129 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005130
Yaniv Gardi583fa622016-03-10 17:37:13 +02005131skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005132 if (!needs_reset) {
5133 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5134 if (hba->saved_err || hba->saved_uic_err)
5135 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5136 __func__, hba->saved_err, hba->saved_uic_err);
5137 }
5138
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305139 ufshcd_clear_eh_in_progress(hba);
5140
5141out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005142 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305143 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005144 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305145 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305146}
5147
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005148static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist *reg_hist,
5149 u32 reg)
5150{
5151 reg_hist->reg[reg_hist->pos] = reg;
5152 reg_hist->tstamp[reg_hist->pos] = ktime_get();
5153 reg_hist->pos = (reg_hist->pos + 1) % UIC_ERR_REG_HIST_LENGTH;
5154}
5155
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305156/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305157 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5158 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305159 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305160static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305161{
5162 u32 reg;
5163
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005164 /* PHY layer lane error */
5165 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
5166 /* Ignore LINERESET indication, as this is not an error */
5167 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005168 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK)) {
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005169 /*
5170 * To know whether this error is fatal or not, DB timeout
5171 * must be checked but this error is handled separately.
5172 */
5173 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005174 ufshcd_update_uic_reg_hist(&hba->ufs_stats.pa_err, reg);
5175 }
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08005176
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305177 /* PA_INIT_ERROR is fatal and needs UIC reset */
5178 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005179 if (reg)
5180 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dl_err, reg);
5181
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305182 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
5183 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02005184 else if (hba->dev_quirks &
5185 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
5186 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
5187 hba->uic_error |=
5188 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
5189 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
5190 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
5191 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305192
5193 /* UIC NL/TL/DME errors needs software retry */
5194 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005195 if (reg) {
5196 ufshcd_update_uic_reg_hist(&hba->ufs_stats.nl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305197 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005198 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305199
5200 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005201 if (reg) {
5202 ufshcd_update_uic_reg_hist(&hba->ufs_stats.tl_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305203 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005204 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305205
5206 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005207 if (reg) {
5208 ufshcd_update_uic_reg_hist(&hba->ufs_stats.dme_err, reg);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305209 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005210 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305211
5212 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
5213 __func__, hba->uic_error);
5214}
5215
5216/**
5217 * ufshcd_check_errors - Check for errors that need s/w attention
5218 * @hba: per-adapter instance
5219 */
5220static void ufshcd_check_errors(struct ufs_hba *hba)
5221{
5222 bool queue_eh_work = false;
5223
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305224 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305225 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305226
5227 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305228 hba->uic_error = 0;
5229 ufshcd_update_uic_error(hba);
5230 if (hba->uic_error)
5231 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305232 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305233
5234 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02005235 /*
5236 * update the transfer error masks to sticky bits, let's do this
5237 * irrespective of current ufshcd_state.
5238 */
5239 hba->saved_err |= hba->errors;
5240 hba->saved_uic_err |= hba->uic_error;
5241
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305242 /* handle fatal errors only when link is functional */
5243 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
5244 /* block commands from scsi mid-layer */
5245 scsi_block_requests(hba->host);
5246
Zang Leigang141f8162016-11-16 11:29:37 +08005247 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08005248
5249 /* dump controller state before resetting */
5250 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
5251 bool pr_prdt = !!(hba->saved_err &
5252 SYSTEM_BUS_FATAL_ERROR);
5253
5254 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5255 __func__, hba->saved_err,
5256 hba->saved_uic_err);
5257
5258 ufshcd_print_host_regs(hba);
5259 ufshcd_print_pwr_info(hba);
5260 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
5261 ufshcd_print_trs(hba, hba->outstanding_reqs,
5262 pr_prdt);
5263 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305264 schedule_work(&hba->eh_work);
5265 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305266 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305267 /*
5268 * if (!queue_eh_work) -
5269 * Other errors are either non-fatal where host recovers
5270 * itself without s/w intervention or errors that will be
5271 * handled by the SCSI core layer.
5272 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305273}
5274
5275/**
5276 * ufshcd_tmc_handler - handle task management function completion
5277 * @hba: per adapter instance
5278 */
5279static void ufshcd_tmc_handler(struct ufs_hba *hba)
5280{
5281 u32 tm_doorbell;
5282
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305283 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305284 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305285 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305286}
5287
5288/**
5289 * ufshcd_sl_intr - Interrupt service routine
5290 * @hba: per adapter instance
5291 * @intr_status: contains interrupts generated by the controller
5292 */
5293static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
5294{
5295 hba->errors = UFSHCD_ERROR_MASK & intr_status;
5296 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305297 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305298
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05305299 if (intr_status & UFSHCD_UIC_MASK)
5300 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305301
5302 if (intr_status & UTP_TASK_REQ_COMPL)
5303 ufshcd_tmc_handler(hba);
5304
5305 if (intr_status & UTP_TRANSFER_REQ_COMPL)
5306 ufshcd_transfer_req_compl(hba);
5307}
5308
5309/**
5310 * ufshcd_intr - Main interrupt service routine
5311 * @irq: irq number
5312 * @__hba: pointer to adapter instance
5313 *
5314 * Returns IRQ_HANDLED - If interrupt is valid
5315 * IRQ_NONE - If invalid interrupt
5316 */
5317static irqreturn_t ufshcd_intr(int irq, void *__hba)
5318{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005319 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305320 irqreturn_t retval = IRQ_NONE;
5321 struct ufs_hba *hba = __hba;
5322
5323 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305324 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005325 enabled_intr_status =
5326 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305327
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005328 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05305329 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02005330
5331 if (enabled_intr_status) {
5332 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305333 retval = IRQ_HANDLED;
5334 }
5335 spin_unlock(hba->host->host_lock);
5336 return retval;
5337}
5338
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305339static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
5340{
5341 int err = 0;
5342 u32 mask = 1 << tag;
5343 unsigned long flags;
5344
5345 if (!test_bit(tag, &hba->outstanding_tasks))
5346 goto out;
5347
5348 spin_lock_irqsave(hba->host->host_lock, flags);
5349 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
5350 spin_unlock_irqrestore(hba->host->host_lock, flags);
5351
5352 /* poll for max. 1 sec to clear door bell register by h/w */
5353 err = ufshcd_wait_for_register(hba,
5354 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02005355 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305356out:
5357 return err;
5358}
5359
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305360/**
5361 * ufshcd_issue_tm_cmd - issues task management commands to controller
5362 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305363 * @lun_id: LUN ID to which TM command is sent
5364 * @task_id: task ID to which the TM command is applicable
5365 * @tm_function: task management function opcode
5366 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305367 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305368 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305369 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305370static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
5371 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305372{
5373 struct utp_task_req_desc *task_req_descp;
5374 struct utp_upiu_task_req *task_req_upiup;
5375 struct Scsi_Host *host;
5376 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305377 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305378 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305379 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305380
5381 host = hba->host;
5382
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305383 /*
5384 * Get free slot, sleep if slots are unavailable.
5385 * Even though we use wait_event() which sleeps indefinitely,
5386 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5387 */
5388 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005389 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305390
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305391 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305392 task_req_descp = hba->utmrdl_base_addr;
5393 task_req_descp += free_slot;
5394
5395 /* Configure task request descriptor */
5396 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
5397 task_req_descp->header.dword_2 =
5398 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
5399
5400 /* Configure task request UPIU */
5401 task_req_upiup =
5402 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305403 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305404 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305405 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305406 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305407 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305408 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005409 /*
5410 * The host shall provide the same value for LUN field in the basic
5411 * header and for Input Parameter.
5412 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305413 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
5414 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305415
Kiwoong Kimd2877be2016-11-10 21:16:15 +09005416 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
5417
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305418 /* send command to the controller */
5419 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02005420
5421 /* Make sure descriptors are ready before ringing the task doorbell */
5422 wmb();
5423
Seungwon Jeonb873a2752013-06-26 22:39:26 +05305424 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07005425 /* Make sure that doorbell is committed immediately */
5426 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305427
5428 spin_unlock_irqrestore(host->host_lock, flags);
5429
5430 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305431 err = wait_event_timeout(hba->tm_wq,
5432 test_bit(free_slot, &hba->tm_condition),
5433 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305434 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305435 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
5436 __func__, tm_function);
5437 if (ufshcd_clear_tm_cmd(hba, free_slot))
5438 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
5439 __func__, free_slot);
5440 err = -ETIMEDOUT;
5441 } else {
5442 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305443 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305444
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305445 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305446 ufshcd_put_tm_slot(hba, free_slot);
5447 wake_up(&hba->tm_tag_wq);
5448
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005449 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305450 return err;
5451}
5452
5453/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305454 * ufshcd_eh_device_reset_handler - device reset handler registered to
5455 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305456 * @cmd: SCSI command pointer
5457 *
5458 * Returns SUCCESS/FAILED
5459 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305460static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305461{
5462 struct Scsi_Host *host;
5463 struct ufs_hba *hba;
5464 unsigned int tag;
5465 u32 pos;
5466 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305467 u8 resp = 0xF;
5468 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305469 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305470
5471 host = cmd->device->host;
5472 hba = shost_priv(host);
5473 tag = cmd->request->tag;
5474
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305475 lrbp = &hba->lrb[tag];
5476 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
5477 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305478 if (!err)
5479 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305480 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305481 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305482
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305483 /* clear the commands that were pending for corresponding LUN */
5484 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
5485 if (hba->lrb[pos].lun == lrbp->lun) {
5486 err = ufshcd_clear_cmd(hba, pos);
5487 if (err)
5488 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305489 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305490 }
5491 spin_lock_irqsave(host->host_lock, flags);
5492 ufshcd_transfer_req_compl(hba);
5493 spin_unlock_irqrestore(host->host_lock, flags);
Gilad Broner7fabb772017-02-03 16:56:50 -08005494
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305495out:
Gilad Broner7fabb772017-02-03 16:56:50 -08005496 hba->req_abort_count = 0;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305497 if (!err) {
5498 err = SUCCESS;
5499 } else {
5500 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
5501 err = FAILED;
5502 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305503 return err;
5504}
5505
Gilad Bronere0b299e2017-02-03 16:56:40 -08005506static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap)
5507{
5508 struct ufshcd_lrb *lrbp;
5509 int tag;
5510
5511 for_each_set_bit(tag, &bitmap, hba->nutrs) {
5512 lrbp = &hba->lrb[tag];
5513 lrbp->req_abort_skip = true;
5514 }
5515}
5516
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305517/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305518 * ufshcd_abort - abort a specific command
5519 * @cmd: SCSI command pointer
5520 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305521 * Abort the pending command in device by sending UFS_ABORT_TASK task management
5522 * command, and in host controller by clearing the door-bell register. There can
5523 * be race between controller sending the command to the device while abort is
5524 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
5525 * really issued and then try to abort it.
5526 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305527 * Returns SUCCESS/FAILED
5528 */
5529static int ufshcd_abort(struct scsi_cmnd *cmd)
5530{
5531 struct Scsi_Host *host;
5532 struct ufs_hba *hba;
5533 unsigned long flags;
5534 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305535 int err = 0;
5536 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305537 u8 resp = 0xF;
5538 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03005539 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305540
5541 host = cmd->device->host;
5542 hba = shost_priv(host);
5543 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08005544 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02005545 if (!ufshcd_valid_tag(hba, tag)) {
5546 dev_err(hba->dev,
5547 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
5548 __func__, tag, cmd, cmd->request);
5549 BUG();
5550 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305551
Dolev Ravive7d38252016-12-22 18:40:07 -08005552 /*
5553 * Task abort to the device W-LUN is illegal. When this command
5554 * will fail, due to spec violation, scsi err handling next step
5555 * will be to send LU reset which, again, is a spec violation.
5556 * To avoid these unnecessary/illegal step we skip to the last error
5557 * handling stage: reset and restore.
5558 */
5559 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
5560 return ufshcd_eh_host_reset_handler(cmd);
5561
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005562 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03005563 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02005564 /* If command is already aborted/completed, return SUCCESS */
5565 if (!(test_bit(tag, &hba->outstanding_reqs))) {
5566 dev_err(hba->dev,
5567 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
5568 __func__, tag, hba->outstanding_reqs, reg);
5569 goto out;
5570 }
5571
Dolev Ravive9d501b2014-07-01 12:22:37 +03005572 if (!(reg & (1 << tag))) {
5573 dev_err(hba->dev,
5574 "%s: cmd was completed, but without a notifying intr, tag = %d",
5575 __func__, tag);
5576 }
5577
Dolev Raviv66cc8202016-12-22 18:39:42 -08005578 /* Print Transfer Request of aborted task */
5579 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
Dolev Raviv66cc8202016-12-22 18:39:42 -08005580
Gilad Broner7fabb772017-02-03 16:56:50 -08005581 /*
5582 * Print detailed info about aborted request.
5583 * As more than one request might get aborted at the same time,
5584 * print full information only for the first aborted request in order
5585 * to reduce repeated printouts. For other aborted requests only print
5586 * basic details.
5587 */
5588 scsi_print_command(hba->lrb[tag].cmd);
5589 if (!hba->req_abort_count) {
5590 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08005591 ufshcd_print_host_state(hba);
Gilad Broner7fabb772017-02-03 16:56:50 -08005592 ufshcd_print_pwr_info(hba);
5593 ufshcd_print_trs(hba, 1 << tag, true);
5594 } else {
5595 ufshcd_print_trs(hba, 1 << tag, false);
5596 }
5597 hba->req_abort_count++;
Gilad Bronere0b299e2017-02-03 16:56:40 -08005598
5599 /* Skip task abort in case previous aborts failed and report failure */
5600 if (lrbp->req_abort_skip) {
5601 err = -EIO;
5602 goto out;
5603 }
5604
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305605 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
5606 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5607 UFS_QUERY_TASK, &resp);
5608 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
5609 /* cmd pending in the device */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005610 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n",
5611 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305612 break;
5613 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305614 /*
5615 * cmd not pending in the device, check if it is
5616 * in transition.
5617 */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005618 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n",
5619 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305620 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
5621 if (reg & (1 << tag)) {
5622 /* sleep for max. 200us to stabilize */
5623 usleep_range(100, 200);
5624 continue;
5625 }
5626 /* command completed already */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005627 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n",
5628 __func__, tag);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305629 goto out;
5630 } else {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005631 dev_err(hba->dev,
5632 "%s: no response from device. tag = %d, err %d\n",
5633 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305634 if (!err)
5635 err = resp; /* service response error */
5636 goto out;
5637 }
5638 }
5639
5640 if (!poll_cnt) {
5641 err = -EBUSY;
5642 goto out;
5643 }
5644
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305645 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
5646 UFS_ABORT_TASK, &resp);
5647 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005648 if (!err) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305649 err = resp; /* service response error */
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005650 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n",
5651 __func__, tag, err);
5652 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305653 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305654 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305655
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305656 err = ufshcd_clear_cmd(hba, tag);
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005657 if (err) {
5658 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n",
5659 __func__, tag, err);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305660 goto out;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08005661 }
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305662
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305663 scsi_dma_unmap(cmd);
5664
5665 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02005666 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305667 hba->lrb[tag].cmd = NULL;
5668 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305669
5670 clear_bit_unlock(tag, &hba->lrb_in_use);
5671 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005672
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305673out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305674 if (!err) {
5675 err = SUCCESS;
5676 } else {
5677 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
Gilad Bronere0b299e2017-02-03 16:56:40 -08005678 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05305679 err = FAILED;
5680 }
5681
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005682 /*
5683 * This ufshcd_release() corresponds to the original scsi cmd that got
5684 * aborted here (as we won't get any IRQ for it).
5685 */
5686 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305687 return err;
5688}
5689
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305690/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305691 * ufshcd_host_reset_and_restore - reset and restore host controller
5692 * @hba: per-adapter instance
5693 *
5694 * Note that host controller reset may issue DME_RESET to
5695 * local and remote (device) Uni-Pro stack and the attributes
5696 * are reset to default state.
5697 *
5698 * Returns zero on success, non-zero on failure
5699 */
5700static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
5701{
5702 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305703 unsigned long flags;
5704
5705 /* Reset the host controller */
5706 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02005707 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305708 spin_unlock_irqrestore(hba->host->host_lock, flags);
5709
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08005710 /* scale up clocks to max frequency before full reinitialization */
5711 ufshcd_scale_clks(hba, true);
5712
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305713 err = ufshcd_hba_enable(hba);
5714 if (err)
5715 goto out;
5716
5717 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005718 err = ufshcd_probe_hba(hba);
5719
5720 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305721 err = -EIO;
5722out:
5723 if (err)
5724 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
5725
5726 return err;
5727}
5728
5729/**
5730 * ufshcd_reset_and_restore - reset and re-initialize host/device
5731 * @hba: per-adapter instance
5732 *
5733 * Reset and recover device, host and re-establish link. This
5734 * is helpful to recover the communication in fatal error conditions.
5735 *
5736 * Returns zero on success, non-zero on failure
5737 */
5738static int ufshcd_reset_and_restore(struct ufs_hba *hba)
5739{
5740 int err = 0;
5741 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005742 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305743
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005744 do {
5745 err = ufshcd_host_reset_and_restore(hba);
5746 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305747
5748 /*
5749 * After reset the door-bell might be cleared, complete
5750 * outstanding requests in s/w here.
5751 */
5752 spin_lock_irqsave(hba->host->host_lock, flags);
5753 ufshcd_transfer_req_compl(hba);
5754 ufshcd_tmc_handler(hba);
5755 spin_unlock_irqrestore(hba->host->host_lock, flags);
5756
5757 return err;
5758}
5759
5760/**
5761 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
5762 * @cmd - SCSI command pointer
5763 *
5764 * Returns SUCCESS/FAILED
5765 */
5766static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
5767{
5768 int err;
5769 unsigned long flags;
5770 struct ufs_hba *hba;
5771
5772 hba = shost_priv(cmd->device->host);
5773
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005774 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305775 /*
5776 * Check if there is any race with fatal error handling.
5777 * If so, wait for it to complete. Even though fatal error
5778 * handling does reset and restore in some cases, don't assume
5779 * anything out of it. We are just avoiding race here.
5780 */
5781 do {
5782 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305783 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305784 hba->ufshcd_state == UFSHCD_STATE_RESET))
5785 break;
5786 spin_unlock_irqrestore(hba->host->host_lock, flags);
5787 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305788 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305789 } while (1);
5790
5791 hba->ufshcd_state = UFSHCD_STATE_RESET;
5792 ufshcd_set_eh_in_progress(hba);
5793 spin_unlock_irqrestore(hba->host->host_lock, flags);
5794
5795 err = ufshcd_reset_and_restore(hba);
5796
5797 spin_lock_irqsave(hba->host->host_lock, flags);
5798 if (!err) {
5799 err = SUCCESS;
5800 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5801 } else {
5802 err = FAILED;
5803 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5804 }
5805 ufshcd_clear_eh_in_progress(hba);
5806 spin_unlock_irqrestore(hba->host->host_lock, flags);
5807
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005808 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305809 return err;
5810}
5811
5812/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005813 * ufshcd_get_max_icc_level - calculate the ICC level
5814 * @sup_curr_uA: max. current supported by the regulator
5815 * @start_scan: row at the desc table to start scan from
5816 * @buff: power descriptor buffer
5817 *
5818 * Returns calculated max ICC level for specific regulator
5819 */
5820static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
5821{
5822 int i;
5823 int curr_uA;
5824 u16 data;
5825 u16 unit;
5826
5827 for (i = start_scan; i >= 0; i--) {
Tomas Winklerd79713f2017-01-05 10:45:11 +02005828 data = be16_to_cpup((__be16 *)&buff[2 * i]);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005829 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
5830 ATTR_ICC_LVL_UNIT_OFFSET;
5831 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
5832 switch (unit) {
5833 case UFSHCD_NANO_AMP:
5834 curr_uA = curr_uA / 1000;
5835 break;
5836 case UFSHCD_MILI_AMP:
5837 curr_uA = curr_uA * 1000;
5838 break;
5839 case UFSHCD_AMP:
5840 curr_uA = curr_uA * 1000 * 1000;
5841 break;
5842 case UFSHCD_MICRO_AMP:
5843 default:
5844 break;
5845 }
5846 if (sup_curr_uA >= curr_uA)
5847 break;
5848 }
5849 if (i < 0) {
5850 i = 0;
5851 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
5852 }
5853
5854 return (u32)i;
5855}
5856
5857/**
5858 * ufshcd_calc_icc_level - calculate the max ICC level
5859 * In case regulators are not initialized we'll return 0
5860 * @hba: per-adapter instance
5861 * @desc_buf: power descriptor buffer to extract ICC levels from.
5862 * @len: length of desc_buff
5863 *
5864 * Returns calculated ICC level
5865 */
5866static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
5867 u8 *desc_buf, int len)
5868{
5869 u32 icc_level = 0;
5870
5871 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
5872 !hba->vreg_info.vccq2) {
5873 dev_err(hba->dev,
5874 "%s: Regulator capability was not set, actvIccLevel=%d",
5875 __func__, icc_level);
5876 goto out;
5877 }
5878
5879 if (hba->vreg_info.vcc)
5880 icc_level = ufshcd_get_max_icc_level(
5881 hba->vreg_info.vcc->max_uA,
5882 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
5883 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
5884
5885 if (hba->vreg_info.vccq)
5886 icc_level = ufshcd_get_max_icc_level(
5887 hba->vreg_info.vccq->max_uA,
5888 icc_level,
5889 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
5890
5891 if (hba->vreg_info.vccq2)
5892 icc_level = ufshcd_get_max_icc_level(
5893 hba->vreg_info.vccq2->max_uA,
5894 icc_level,
5895 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
5896out:
5897 return icc_level;
5898}
5899
Dolev Raviv61e07352016-11-23 16:30:49 -08005900static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
5901{
5902 int ret = 0;
5903 int retries;
5904
5905 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
5906 /* write attribute */
5907 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5908 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
5909 if (!ret)
5910 break;
5911
5912 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
5913 }
5914
5915 return ret;
5916}
5917
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005918static void ufshcd_init_icc_levels(struct ufs_hba *hba)
5919{
5920 int ret;
5921 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
5922 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
5923
5924 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
5925 if (ret) {
5926 dev_err(hba->dev,
5927 "%s: Failed reading power descriptor.len = %d ret = %d",
5928 __func__, buff_len, ret);
5929 return;
5930 }
5931
5932 hba->init_prefetch_data.icc_level =
5933 ufshcd_find_max_sup_active_icc_level(hba,
5934 desc_buf, buff_len);
5935 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
5936 __func__, hba->init_prefetch_data.icc_level);
5937
Dolev Raviv61e07352016-11-23 16:30:49 -08005938 ret = ufshcd_set_icc_levels_attr(hba,
5939 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005940
5941 if (ret)
5942 dev_err(hba->dev,
5943 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5944 __func__, hba->init_prefetch_data.icc_level , ret);
5945
5946}
5947
5948/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005949 * ufshcd_scsi_add_wlus - Adds required W-LUs
5950 * @hba: per-adapter instance
5951 *
5952 * UFS device specification requires the UFS devices to support 4 well known
5953 * logical units:
5954 * "REPORT_LUNS" (address: 01h)
5955 * "UFS Device" (address: 50h)
5956 * "RPMB" (address: 44h)
5957 * "BOOT" (address: 30h)
5958 * UFS device's power management needs to be controlled by "POWER CONDITION"
5959 * field of SSU (START STOP UNIT) command. But this "power condition" field
5960 * will take effect only when its sent to "UFS device" well known logical unit
5961 * hence we require the scsi_device instance to represent this logical unit in
5962 * order for the UFS host driver to send the SSU command for power management.
5963
5964 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5965 * Block) LU so user space process can control this LU. User space may also
5966 * want to have access to BOOT LU.
5967
5968 * This function adds scsi device instances for each of all well known LUs
5969 * (except "REPORT LUNS" LU).
5970 *
5971 * Returns zero on success (all required W-LUs are added successfully),
5972 * non-zero error value on failure (if failed to add any of the required W-LU).
5973 */
5974static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
5975{
5976 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005977 struct scsi_device *sdev_rpmb;
5978 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005979
5980 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
5981 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
5982 if (IS_ERR(hba->sdev_ufs_device)) {
5983 ret = PTR_ERR(hba->sdev_ufs_device);
5984 hba->sdev_ufs_device = NULL;
5985 goto out;
5986 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005987 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005988
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005989 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005990 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005991 if (IS_ERR(sdev_boot)) {
5992 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005993 goto remove_sdev_ufs_device;
5994 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005995 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005996
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005997 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005998 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005999 if (IS_ERR(sdev_rpmb)) {
6000 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006001 goto remove_sdev_boot;
6002 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006003 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006004 goto out;
6005
6006remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006007 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006008remove_sdev_ufs_device:
6009 scsi_remove_device(hba->sdev_ufs_device);
6010out:
6011 return ret;
6012}
6013
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006014static int ufs_get_device_desc(struct ufs_hba *hba,
6015 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006016{
6017 int err;
6018 u8 model_index;
6019 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
6020 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
6021
6022 err = ufshcd_read_device_desc(hba, desc_buf,
6023 QUERY_DESC_DEVICE_MAX_SIZE);
6024 if (err) {
6025 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
6026 __func__, err);
6027 goto out;
6028 }
6029
6030 /*
6031 * getting vendor (manufacturerID) and Bank Index in big endian
6032 * format
6033 */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006034 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006035 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
6036
6037 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
6038
6039 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
6040 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
6041 if (err) {
6042 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
6043 __func__, err);
6044 goto out;
6045 }
6046
6047 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006048 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006049 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
6050 MAX_MODEL_LEN));
6051
6052 /* Null terminate the model string */
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006053 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006054
6055out:
6056 return err;
6057}
6058
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006059static void ufs_fixup_device_setup(struct ufs_hba *hba,
6060 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006061{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006062 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006063
6064 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006065 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
6066 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
6067 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02006068 !strcmp(f->card.model, UFS_ANY_MODEL)))
6069 hba->dev_quirks |= f->quirk;
6070 }
6071}
6072
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006073/**
Yaniv Gardi37113102016-03-10 17:37:16 +02006074 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6075 * @hba: per-adapter instance
6076 *
6077 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6078 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6079 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6080 * the hibern8 exit latency.
6081 *
6082 * Returns zero on success, non-zero error value on failure.
6083 */
6084static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
6085{
6086 int ret = 0;
6087 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
6088
6089 ret = ufshcd_dme_peer_get(hba,
6090 UIC_ARG_MIB_SEL(
6091 RX_MIN_ACTIVATETIME_CAPABILITY,
6092 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6093 &peer_rx_min_activatetime);
6094 if (ret)
6095 goto out;
6096
6097 /* make sure proper unit conversion is applied */
6098 tuned_pa_tactivate =
6099 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
6100 / PA_TACTIVATE_TIME_UNIT_US);
6101 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6102 tuned_pa_tactivate);
6103
6104out:
6105 return ret;
6106}
6107
6108/**
6109 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6110 * @hba: per-adapter instance
6111 *
6112 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6113 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6114 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6115 * This optimal value can help reduce the hibern8 exit latency.
6116 *
6117 * Returns zero on success, non-zero error value on failure.
6118 */
6119static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
6120{
6121 int ret = 0;
6122 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
6123 u32 max_hibern8_time, tuned_pa_hibern8time;
6124
6125 ret = ufshcd_dme_get(hba,
6126 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
6127 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6128 &local_tx_hibern8_time_cap);
6129 if (ret)
6130 goto out;
6131
6132 ret = ufshcd_dme_peer_get(hba,
6133 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
6134 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6135 &peer_rx_hibern8_time_cap);
6136 if (ret)
6137 goto out;
6138
6139 max_hibern8_time = max(local_tx_hibern8_time_cap,
6140 peer_rx_hibern8_time_cap);
6141 /* make sure proper unit conversion is applied */
6142 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
6143 / PA_HIBERN8_TIME_UNIT_US);
6144 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
6145 tuned_pa_hibern8time);
6146out:
6147 return ret;
6148}
6149
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006150/**
6151 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6152 * less than device PA_TACTIVATE time.
6153 * @hba: per-adapter instance
6154 *
6155 * Some UFS devices require host PA_TACTIVATE to be lower than device
6156 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6157 * for such devices.
6158 *
6159 * Returns zero on success, non-zero error value on failure.
6160 */
6161static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
6162{
6163 int ret = 0;
6164 u32 granularity, peer_granularity;
6165 u32 pa_tactivate, peer_pa_tactivate;
6166 u32 pa_tactivate_us, peer_pa_tactivate_us;
6167 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
6168
6169 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6170 &granularity);
6171 if (ret)
6172 goto out;
6173
6174 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
6175 &peer_granularity);
6176 if (ret)
6177 goto out;
6178
6179 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
6180 (granularity > PA_GRANULARITY_MAX_VAL)) {
6181 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
6182 __func__, granularity);
6183 return -EINVAL;
6184 }
6185
6186 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
6187 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
6188 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
6189 __func__, peer_granularity);
6190 return -EINVAL;
6191 }
6192
6193 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
6194 if (ret)
6195 goto out;
6196
6197 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
6198 &peer_pa_tactivate);
6199 if (ret)
6200 goto out;
6201
6202 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
6203 peer_pa_tactivate_us = peer_pa_tactivate *
6204 gran_to_us_table[peer_granularity - 1];
6205
6206 if (pa_tactivate_us > peer_pa_tactivate_us) {
6207 u32 new_peer_pa_tactivate;
6208
6209 new_peer_pa_tactivate = pa_tactivate_us /
6210 gran_to_us_table[peer_granularity - 1];
6211 new_peer_pa_tactivate++;
6212 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
6213 new_peer_pa_tactivate);
6214 }
6215
6216out:
6217 return ret;
6218}
6219
Yaniv Gardi37113102016-03-10 17:37:16 +02006220static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
6221{
6222 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
6223 ufshcd_tune_pa_tactivate(hba);
6224 ufshcd_tune_pa_hibern8time(hba);
6225 }
6226
6227 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
6228 /* set 1ms timeout for PA_TACTIVATE */
6229 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08006230
6231 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
6232 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08006233
6234 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02006235}
6236
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006237static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba)
6238{
6239 int err_reg_hist_size = sizeof(struct ufs_uic_err_reg_hist);
6240
6241 hba->ufs_stats.hibern8_exit_cnt = 0;
6242 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0);
6243
6244 memset(&hba->ufs_stats.pa_err, 0, err_reg_hist_size);
6245 memset(&hba->ufs_stats.dl_err, 0, err_reg_hist_size);
6246 memset(&hba->ufs_stats.nl_err, 0, err_reg_hist_size);
6247 memset(&hba->ufs_stats.tl_err, 0, err_reg_hist_size);
6248 memset(&hba->ufs_stats.dme_err, 0, err_reg_hist_size);
Gilad Broner7fabb772017-02-03 16:56:50 -08006249
6250 hba->req_abort_count = 0;
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006251}
6252
Yaniv Gardi37113102016-03-10 17:37:16 +02006253/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006254 * ufshcd_probe_hba - probe hba to detect device and initialize
6255 * @hba: per-adapter instance
6256 *
6257 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306258 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006259static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306260{
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006261 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306262 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006263 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306264
6265 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306266 if (ret)
6267 goto out;
6268
Yaniv Gardiafdfff52016-03-10 17:37:15 +02006269 /* set the default level for urgent bkops */
6270 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
6271 hba->is_urgent_bkops_lvl_checked = false;
6272
Dolev Ravivff8e20c2016-12-22 18:42:18 -08006273 /* Debug counters initialization */
6274 ufshcd_clear_dbg_ufs_stats(hba);
6275
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006276 /* UniPro link is active now */
6277 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05306278
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306279 ret = ufshcd_verify_dev_init(hba);
6280 if (ret)
6281 goto out;
6282
Dolev Raviv68078d52013-07-30 00:35:58 +05306283 ret = ufshcd_complete_dev_init(hba);
6284 if (ret)
6285 goto out;
6286
Tomas Winkler93fdd5a2017-01-05 10:45:12 +02006287 ret = ufs_get_device_desc(hba, &card);
6288 if (ret) {
6289 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
6290 __func__, ret);
6291 goto out;
6292 }
6293
6294 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02006295 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02006296
6297 ret = ufshcd_set_vccq_rail_unused(hba,
6298 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
6299 if (ret)
6300 goto out;
6301
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006302 /* UFS device is also active now */
6303 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306304 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006305 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306306
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006307 if (ufshcd_get_max_pwr_mode(hba)) {
6308 dev_err(hba->dev,
6309 "%s: Failed getting max supported power mode\n",
6310 __func__);
6311 } else {
6312 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006313 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006314 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
6315 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07006316 goto out;
6317 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006318 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006319
Yaniv Gardi53c12d02016-02-01 15:02:45 +02006320 /* set the state as operational after switching to desired gear */
6321 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006322 /*
6323 * If we are in error handling context or in power management callbacks
6324 * context, no need to scan the host
6325 */
6326 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6327 bool flag;
6328
6329 /* clear any previous UFS device information */
6330 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02006331 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
6332 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006333 hba->dev_info.f_power_on_wp_en = flag;
6334
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006335 if (!hba->is_init_prefetch)
6336 ufshcd_init_icc_levels(hba);
6337
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03006338 /* Add required well known logical units to scsi mid layer */
6339 if (ufshcd_scsi_add_wlus(hba))
6340 goto out;
6341
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006342 /* Initialize devfreq after UFS device is detected */
6343 if (ufshcd_is_clkscaling_supported(hba)) {
6344 memcpy(&hba->clk_scaling.saved_pwr_info.info,
6345 &hba->pwr_info,
6346 sizeof(struct ufs_pa_layer_attr));
6347 hba->clk_scaling.saved_pwr_info.is_valid = true;
6348 if (!hba->devfreq) {
6349 hba->devfreq = devm_devfreq_add_device(hba->dev,
6350 &ufs_devfreq_profile,
6351 "simple_ondemand",
6352 NULL);
6353 if (IS_ERR(hba->devfreq)) {
6354 ret = PTR_ERR(hba->devfreq);
6355 dev_err(hba->dev, "Unable to register with devfreq %d\n",
6356 ret);
6357 goto out;
6358 }
6359 }
6360 hba->clk_scaling.is_allowed = true;
6361 }
6362
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306363 scsi_scan_host(hba->host);
6364 pm_runtime_put_sync(hba->dev);
6365 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03006366
6367 if (!hba->is_init_prefetch)
6368 hba->is_init_prefetch = true;
6369
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306370out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006371 /*
6372 * If we failed to initialize the device or the device is not
6373 * present, turn off the power/clocks etc.
6374 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006375 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
6376 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006377 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006378 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006379
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006380 trace_ufshcd_init(dev_name(hba->dev), ret,
6381 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08006382 hba->curr_dev_pwr_mode, hba->uic_link_state);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006383 return ret;
6384}
6385
6386/**
6387 * ufshcd_async_scan - asynchronous execution for probing hba
6388 * @data: data pointer to pass to this function
6389 * @cookie: cookie data
6390 */
6391static void ufshcd_async_scan(void *data, async_cookie_t cookie)
6392{
6393 struct ufs_hba *hba = (struct ufs_hba *)data;
6394
6395 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306396}
6397
Yaniv Gardif550c652016-03-10 17:37:07 +02006398static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
6399{
6400 unsigned long flags;
6401 struct Scsi_Host *host;
6402 struct ufs_hba *hba;
6403 int index;
6404 bool found = false;
6405
6406 if (!scmd || !scmd->device || !scmd->device->host)
6407 return BLK_EH_NOT_HANDLED;
6408
6409 host = scmd->device->host;
6410 hba = shost_priv(host);
6411 if (!hba)
6412 return BLK_EH_NOT_HANDLED;
6413
6414 spin_lock_irqsave(host->host_lock, flags);
6415
6416 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
6417 if (hba->lrb[index].cmd == scmd) {
6418 found = true;
6419 break;
6420 }
6421 }
6422
6423 spin_unlock_irqrestore(host->host_lock, flags);
6424
6425 /*
6426 * Bypass SCSI error handling and reset the block layer timer if this
6427 * SCSI command was not actually dispatched to UFS driver, otherwise
6428 * let SCSI layer handle the error as usual.
6429 */
6430 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
6431}
6432
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306433static struct scsi_host_template ufshcd_driver_template = {
6434 .module = THIS_MODULE,
6435 .name = UFSHCD,
6436 .proc_name = UFSHCD,
6437 .queuecommand = ufshcd_queuecommand,
6438 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09006439 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306440 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03006441 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306442 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05306443 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
6444 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02006445 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306446 .this_id = -1,
6447 .sg_tablesize = SG_ALL,
6448 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
6449 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006450 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01006451 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306452};
6453
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006454static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
6455 int ua)
6456{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006457 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006458
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006459 if (!vreg)
6460 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006461
Bjorn Andersson7b16a072015-02-11 19:35:28 -08006462 ret = regulator_set_load(vreg->reg, ua);
6463 if (ret < 0) {
6464 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
6465 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006466 }
6467
6468 return ret;
6469}
6470
6471static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
6472 struct ufs_vreg *vreg)
6473{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006474 if (!vreg)
6475 return 0;
6476 else if (vreg->unused)
6477 return 0;
6478 else
6479 return ufshcd_config_vreg_load(hba->dev, vreg,
6480 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006481}
6482
6483static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
6484 struct ufs_vreg *vreg)
6485{
Yaniv Gardi60f01872016-03-10 17:37:11 +02006486 if (!vreg)
6487 return 0;
6488 else if (vreg->unused)
6489 return 0;
6490 else
6491 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006492}
6493
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006494static int ufshcd_config_vreg(struct device *dev,
6495 struct ufs_vreg *vreg, bool on)
6496{
6497 int ret = 0;
6498 struct regulator *reg = vreg->reg;
6499 const char *name = vreg->name;
6500 int min_uV, uA_load;
6501
6502 BUG_ON(!vreg);
6503
6504 if (regulator_count_voltages(reg) > 0) {
6505 min_uV = on ? vreg->min_uV : 0;
6506 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
6507 if (ret) {
6508 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
6509 __func__, name, ret);
6510 goto out;
6511 }
6512
6513 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006514 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
6515 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006516 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006517 }
6518out:
6519 return ret;
6520}
6521
6522static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
6523{
6524 int ret = 0;
6525
Yaniv Gardi60f01872016-03-10 17:37:11 +02006526 if (!vreg)
6527 goto out;
6528 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006529 goto out;
6530
6531 ret = ufshcd_config_vreg(dev, vreg, true);
6532 if (!ret)
6533 ret = regulator_enable(vreg->reg);
6534
6535 if (!ret)
6536 vreg->enabled = true;
6537 else
6538 dev_err(dev, "%s: %s enable failed, err=%d\n",
6539 __func__, vreg->name, ret);
6540out:
6541 return ret;
6542}
6543
6544static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
6545{
6546 int ret = 0;
6547
Yaniv Gardi60f01872016-03-10 17:37:11 +02006548 if (!vreg)
6549 goto out;
6550 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006551 goto out;
6552
6553 ret = regulator_disable(vreg->reg);
6554
6555 if (!ret) {
6556 /* ignore errors on applying disable config */
6557 ufshcd_config_vreg(dev, vreg, false);
6558 vreg->enabled = false;
6559 } else {
6560 dev_err(dev, "%s: %s disable failed, err=%d\n",
6561 __func__, vreg->name, ret);
6562 }
6563out:
6564 return ret;
6565}
6566
6567static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
6568{
6569 int ret = 0;
6570 struct device *dev = hba->dev;
6571 struct ufs_vreg_info *info = &hba->vreg_info;
6572
6573 if (!info)
6574 goto out;
6575
6576 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
6577 if (ret)
6578 goto out;
6579
6580 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
6581 if (ret)
6582 goto out;
6583
6584 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
6585 if (ret)
6586 goto out;
6587
6588out:
6589 if (ret) {
6590 ufshcd_toggle_vreg(dev, info->vccq2, false);
6591 ufshcd_toggle_vreg(dev, info->vccq, false);
6592 ufshcd_toggle_vreg(dev, info->vcc, false);
6593 }
6594 return ret;
6595}
6596
Raviv Shvili6a771a62014-09-25 15:32:24 +03006597static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
6598{
6599 struct ufs_vreg_info *info = &hba->vreg_info;
6600
6601 if (info)
6602 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
6603
6604 return 0;
6605}
6606
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006607static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
6608{
6609 int ret = 0;
6610
6611 if (!vreg)
6612 goto out;
6613
6614 vreg->reg = devm_regulator_get(dev, vreg->name);
6615 if (IS_ERR(vreg->reg)) {
6616 ret = PTR_ERR(vreg->reg);
6617 dev_err(dev, "%s: %s get failed, err=%d\n",
6618 __func__, vreg->name, ret);
6619 }
6620out:
6621 return ret;
6622}
6623
6624static int ufshcd_init_vreg(struct ufs_hba *hba)
6625{
6626 int ret = 0;
6627 struct device *dev = hba->dev;
6628 struct ufs_vreg_info *info = &hba->vreg_info;
6629
6630 if (!info)
6631 goto out;
6632
6633 ret = ufshcd_get_vreg(dev, info->vcc);
6634 if (ret)
6635 goto out;
6636
6637 ret = ufshcd_get_vreg(dev, info->vccq);
6638 if (ret)
6639 goto out;
6640
6641 ret = ufshcd_get_vreg(dev, info->vccq2);
6642out:
6643 return ret;
6644}
6645
Raviv Shvili6a771a62014-09-25 15:32:24 +03006646static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
6647{
6648 struct ufs_vreg_info *info = &hba->vreg_info;
6649
6650 if (info)
6651 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
6652
6653 return 0;
6654}
6655
Yaniv Gardi60f01872016-03-10 17:37:11 +02006656static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
6657{
6658 int ret = 0;
6659 struct ufs_vreg_info *info = &hba->vreg_info;
6660
6661 if (!info)
6662 goto out;
6663 else if (!info->vccq)
6664 goto out;
6665
6666 if (unused) {
6667 /* shut off the rail here */
6668 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
6669 /*
6670 * Mark this rail as no longer used, so it doesn't get enabled
6671 * later by mistake
6672 */
6673 if (!ret)
6674 info->vccq->unused = true;
6675 } else {
6676 /*
6677 * rail should have been already enabled hence just make sure
6678 * that unused flag is cleared.
6679 */
6680 info->vccq->unused = false;
6681 }
6682out:
6683 return ret;
6684}
6685
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006686static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
6687 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006688{
6689 int ret = 0;
6690 struct ufs_clk_info *clki;
6691 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006692 unsigned long flags;
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006693 ktime_t start = ktime_get();
6694 bool clk_state_changed = false;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006695
6696 if (!head || list_empty(head))
6697 goto out;
6698
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006699 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
6700 if (ret)
6701 return ret;
6702
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006703 list_for_each_entry(clki, head, list) {
6704 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006705 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
6706 continue;
6707
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006708 clk_state_changed = on ^ clki->enabled;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006709 if (on && !clki->enabled) {
6710 ret = clk_prepare_enable(clki->clk);
6711 if (ret) {
6712 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
6713 __func__, clki->name, ret);
6714 goto out;
6715 }
6716 } else if (!on && clki->enabled) {
6717 clk_disable_unprepare(clki->clk);
6718 }
6719 clki->enabled = on;
6720 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
6721 clki->name, on ? "en" : "dis");
6722 }
6723 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006724
Subhash Jadavani1e879e82016-10-06 21:48:22 -07006725 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
6726 if (ret)
6727 return ret;
6728
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006729out:
6730 if (ret) {
6731 list_for_each_entry(clki, head, list) {
6732 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
6733 clk_disable_unprepare(clki->clk);
6734 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006735 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006736 spin_lock_irqsave(hba->host->host_lock, flags);
6737 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006738 trace_ufshcd_clk_gating(dev_name(hba->dev),
6739 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006740 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006741 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006742
subhashj@codeaurora.org911a0772016-12-22 18:41:48 -08006743 if (clk_state_changed)
6744 trace_ufshcd_profile_clk_gating(dev_name(hba->dev),
6745 (on ? "on" : "off"),
6746 ktime_to_us(ktime_sub(ktime_get(), start)), ret);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006747 return ret;
6748}
6749
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006750static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
6751{
6752 return __ufshcd_setup_clocks(hba, on, false);
6753}
6754
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006755static int ufshcd_init_clocks(struct ufs_hba *hba)
6756{
6757 int ret = 0;
6758 struct ufs_clk_info *clki;
6759 struct device *dev = hba->dev;
6760 struct list_head *head = &hba->clk_list_head;
6761
6762 if (!head || list_empty(head))
6763 goto out;
6764
6765 list_for_each_entry(clki, head, list) {
6766 if (!clki->name)
6767 continue;
6768
6769 clki->clk = devm_clk_get(dev, clki->name);
6770 if (IS_ERR(clki->clk)) {
6771 ret = PTR_ERR(clki->clk);
6772 dev_err(dev, "%s: %s clk get failed, %d\n",
6773 __func__, clki->name, ret);
6774 goto out;
6775 }
6776
6777 if (clki->max_freq) {
6778 ret = clk_set_rate(clki->clk, clki->max_freq);
6779 if (ret) {
6780 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6781 __func__, clki->name,
6782 clki->max_freq, ret);
6783 goto out;
6784 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03006785 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006786 }
6787 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
6788 clki->name, clk_get_rate(clki->clk));
6789 }
6790out:
6791 return ret;
6792}
6793
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006794static int ufshcd_variant_hba_init(struct ufs_hba *hba)
6795{
6796 int err = 0;
6797
6798 if (!hba->vops)
6799 goto out;
6800
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006801 err = ufshcd_vops_init(hba);
6802 if (err)
6803 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006804
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006805 err = ufshcd_vops_setup_regulators(hba, true);
6806 if (err)
6807 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006808
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006809 goto out;
6810
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006811out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006812 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006813out:
6814 if (err)
6815 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006816 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006817 return err;
6818}
6819
6820static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
6821{
6822 if (!hba->vops)
6823 return;
6824
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006825 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006826
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006827 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006828}
6829
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006830static int ufshcd_hba_init(struct ufs_hba *hba)
6831{
6832 int err;
6833
Raviv Shvili6a771a62014-09-25 15:32:24 +03006834 /*
6835 * Handle host controller power separately from the UFS device power
6836 * rails as it will help controlling the UFS host controller power
6837 * collapse easily which is different than UFS device power collapse.
6838 * Also, enable the host controller power before we go ahead with rest
6839 * of the initialization here.
6840 */
6841 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006842 if (err)
6843 goto out;
6844
Raviv Shvili6a771a62014-09-25 15:32:24 +03006845 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006846 if (err)
6847 goto out;
6848
Raviv Shvili6a771a62014-09-25 15:32:24 +03006849 err = ufshcd_init_clocks(hba);
6850 if (err)
6851 goto out_disable_hba_vreg;
6852
6853 err = ufshcd_setup_clocks(hba, true);
6854 if (err)
6855 goto out_disable_hba_vreg;
6856
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006857 err = ufshcd_init_vreg(hba);
6858 if (err)
6859 goto out_disable_clks;
6860
6861 err = ufshcd_setup_vreg(hba, true);
6862 if (err)
6863 goto out_disable_clks;
6864
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006865 err = ufshcd_variant_hba_init(hba);
6866 if (err)
6867 goto out_disable_vreg;
6868
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006869 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006870 goto out;
6871
6872out_disable_vreg:
6873 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006874out_disable_clks:
6875 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03006876out_disable_hba_vreg:
6877 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006878out:
6879 return err;
6880}
6881
6882static void ufshcd_hba_exit(struct ufs_hba *hba)
6883{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006884 if (hba->is_powered) {
6885 ufshcd_variant_hba_exit(hba);
6886 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07006887 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006888 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org0701e492017-02-03 16:58:01 -08006889 if (hba->devfreq)
6890 ufshcd_suspend_clkscaling(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08006891 destroy_workqueue(hba->clk_scaling.workq);
6892 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006893 ufshcd_setup_clocks(hba, false);
6894 ufshcd_setup_hba_vreg(hba, false);
6895 hba->is_powered = false;
6896 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006897}
6898
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006899static int
6900ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306901{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006902 unsigned char cmd[6] = {REQUEST_SENSE,
6903 0,
6904 0,
6905 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006906 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006907 0};
6908 char *buffer;
6909 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306910
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006911 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006912 if (!buffer) {
6913 ret = -ENOMEM;
6914 goto out;
6915 }
6916
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01006917 ret = scsi_execute(sdp, cmd, DMA_FROM_DEVICE, buffer,
6918 UFSHCD_REQ_SENSE_SIZE, NULL, NULL,
6919 msecs_to_jiffies(1000), 3, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006920 if (ret)
6921 pr_err("%s: failed with err %d\n", __func__, ret);
6922
6923 kfree(buffer);
6924out:
6925 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306926}
6927
6928/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006929 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6930 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306931 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006932 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306933 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006934 * Returns 0 if requested power mode is set successfully
6935 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306936 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006937static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
6938 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306939{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006940 unsigned char cmd[6] = { START_STOP };
6941 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006942 struct scsi_device *sdp;
6943 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006944 int ret;
6945
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006946 spin_lock_irqsave(hba->host->host_lock, flags);
6947 sdp = hba->sdev_ufs_device;
6948 if (sdp) {
6949 ret = scsi_device_get(sdp);
6950 if (!ret && !scsi_device_online(sdp)) {
6951 ret = -ENODEV;
6952 scsi_device_put(sdp);
6953 }
6954 } else {
6955 ret = -ENODEV;
6956 }
6957 spin_unlock_irqrestore(hba->host->host_lock, flags);
6958
6959 if (ret)
6960 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006961
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306962 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006963 * If scsi commands fail, the scsi mid-layer schedules scsi error-
6964 * handling, which would wait for host to be resumed. Since we know
6965 * we are functional while we are here, skip host resume in error
6966 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306967 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006968 hba->host->eh_noresume = 1;
6969 if (hba->wlun_dev_clr_ua) {
6970 ret = ufshcd_send_request_sense(hba, sdp);
6971 if (ret)
6972 goto out;
6973 /* Unit attention condition is cleared now */
6974 hba->wlun_dev_clr_ua = false;
6975 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306976
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006977 cmd[4] = pwr_mode << 4;
6978
6979 /*
6980 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02006981 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006982 * already suspended childs.
6983 */
Christoph Hellwigfcbfffe2017-02-23 16:02:37 +01006984 ret = scsi_execute(sdp, cmd, DMA_NONE, NULL, 0, NULL, &sshdr,
6985 START_STOP_TIMEOUT, 0, 0, RQF_PM, NULL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006986 if (ret) {
6987 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02006988 "START_STOP failed for power mode: %d, result %x\n",
6989 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01006990 if (driver_byte(ret) & DRIVER_SENSE)
6991 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006992 }
6993
6994 if (!ret)
6995 hba->curr_dev_pwr_mode = pwr_mode;
6996out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006997 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006998 hba->host->eh_noresume = 0;
6999 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307000}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307001
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007002static int ufshcd_link_state_transition(struct ufs_hba *hba,
7003 enum uic_link_state req_link_state,
7004 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307005{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007006 int ret = 0;
7007
7008 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307009 return 0;
7010
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007011 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
7012 ret = ufshcd_uic_hibern8_enter(hba);
7013 if (!ret)
7014 ufshcd_set_link_hibern8(hba);
7015 else
7016 goto out;
7017 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307018 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007019 * If autobkops is enabled, link can't be turned off because
7020 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307021 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007022 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
7023 (!check_for_bkops || (check_for_bkops &&
7024 !hba->auto_bkops_enabled))) {
7025 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02007026 * Let's make sure that link is in low power mode, we are doing
7027 * this currently by putting the link in Hibern8. Otherway to
7028 * put the link in low power mode is to send the DME end point
7029 * to device and then send the DME reset command to local
7030 * unipro. But putting the link in hibern8 is much faster.
7031 */
7032 ret = ufshcd_uic_hibern8_enter(hba);
7033 if (ret)
7034 goto out;
7035 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007036 * Change controller state to "reset state" which
7037 * should also put the link in off/reset state
7038 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02007039 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007040 /*
7041 * TODO: Check if we need any delay to make sure that
7042 * controller is reset
7043 */
7044 ufshcd_set_link_off(hba);
7045 }
7046
7047out:
7048 return ret;
7049}
7050
7051static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
7052{
7053 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02007054 * It seems some UFS devices may keep drawing more than sleep current
7055 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7056 * To avoid this situation, add 2ms delay before putting these UFS
7057 * rails in LPM mode.
7058 */
7059 if (!ufshcd_is_link_active(hba) &&
7060 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
7061 usleep_range(2000, 2100);
7062
7063 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007064 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7065 * power.
7066 *
7067 * If UFS device and link is in OFF state, all power supplies (VCC,
7068 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7069 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7070 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7071 *
7072 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7073 * in low power state which would save some power.
7074 */
7075 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7076 !hba->dev_info.is_lu_power_on_wp) {
7077 ufshcd_setup_vreg(hba, false);
7078 } else if (!ufshcd_is_ufs_dev_active(hba)) {
7079 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7080 if (!ufshcd_is_link_active(hba)) {
7081 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7082 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
7083 }
7084 }
7085}
7086
7087static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
7088{
7089 int ret = 0;
7090
7091 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
7092 !hba->dev_info.is_lu_power_on_wp) {
7093 ret = ufshcd_setup_vreg(hba, true);
7094 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007095 if (!ret && !ufshcd_is_link_active(hba)) {
7096 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
7097 if (ret)
7098 goto vcc_disable;
7099 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
7100 if (ret)
7101 goto vccq_lpm;
7102 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07007103 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007104 }
7105 goto out;
7106
7107vccq_lpm:
7108 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
7109vcc_disable:
7110 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
7111out:
7112 return ret;
7113}
7114
7115static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
7116{
7117 if (ufshcd_is_link_off(hba))
7118 ufshcd_setup_hba_vreg(hba, false);
7119}
7120
7121static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
7122{
7123 if (ufshcd_is_link_off(hba))
7124 ufshcd_setup_hba_vreg(hba, true);
7125}
7126
7127/**
7128 * ufshcd_suspend - helper function for suspend operations
7129 * @hba: per adapter instance
7130 * @pm_op: desired low power operation type
7131 *
7132 * This function will try to put the UFS device and link into low power
7133 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7134 * (System PM level).
7135 *
7136 * If this function is called during shutdown, it will make sure that
7137 * both UFS device and UFS link is powered off.
7138 *
7139 * NOTE: UFS device & link must be active before we enter in this function.
7140 *
7141 * Returns 0 for success and non-zero for failure
7142 */
7143static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7144{
7145 int ret = 0;
7146 enum ufs_pm_level pm_lvl;
7147 enum ufs_dev_pwr_mode req_dev_pwr_mode;
7148 enum uic_link_state req_link_state;
7149
7150 hba->pm_op_in_progress = 1;
7151 if (!ufshcd_is_shutdown_pm(pm_op)) {
7152 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
7153 hba->rpm_lvl : hba->spm_lvl;
7154 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
7155 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
7156 } else {
7157 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
7158 req_link_state = UIC_LINK_OFF_STATE;
7159 }
7160
7161 /*
7162 * If we can't transition into any of the low power modes
7163 * just gate the clocks.
7164 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007165 ufshcd_hold(hba, false);
7166 hba->clk_gating.is_suspended = true;
7167
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007168 if (hba->clk_scaling.is_allowed) {
7169 cancel_work_sync(&hba->clk_scaling.suspend_work);
7170 cancel_work_sync(&hba->clk_scaling.resume_work);
7171 ufshcd_suspend_clkscaling(hba);
7172 }
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007173
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007174 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
7175 req_link_state == UIC_LINK_ACTIVE_STATE) {
7176 goto disable_clks;
7177 }
7178
7179 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
7180 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007181 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007182
7183 /* UFS device & link must be active before we enter in this function */
7184 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
7185 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07007186 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007187 }
7188
7189 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03007190 if (ufshcd_can_autobkops_during_suspend(hba)) {
7191 /*
7192 * The device is idle with no requests in the queue,
7193 * allow background operations if bkops status shows
7194 * that performance might be impacted.
7195 */
7196 ret = ufshcd_urgent_bkops(hba);
7197 if (ret)
7198 goto enable_gating;
7199 } else {
7200 /* make sure that auto bkops is disabled */
7201 ufshcd_disable_auto_bkops(hba);
7202 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007203 }
7204
7205 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
7206 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
7207 !ufshcd_is_runtime_pm(pm_op))) {
7208 /* ensure that bkops is disabled */
7209 ufshcd_disable_auto_bkops(hba);
7210 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
7211 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007212 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007213 }
7214
7215 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
7216 if (ret)
7217 goto set_dev_active;
7218
7219 ufshcd_vreg_set_lpm(hba);
7220
7221disable_clks:
7222 /*
7223 * Call vendor specific suspend callback. As these callbacks may access
7224 * vendor specific host controller register space call them before the
7225 * host clocks are ON.
7226 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007227 ret = ufshcd_vops_suspend(hba, pm_op);
7228 if (ret)
7229 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007230
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007231 if (!ufshcd_is_link_active(hba))
7232 ufshcd_setup_clocks(hba, false);
7233 else
7234 /* If link is active, device ref_clk can't be switched off */
7235 __ufshcd_setup_clocks(hba, false, true);
7236
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007237 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007238 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007239 /*
7240 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007241 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007242 */
7243 ufshcd_disable_irq(hba);
7244 /* Put the host controller in low power mode if possible */
7245 ufshcd_hba_vreg_set_lpm(hba);
7246 goto out;
7247
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007248set_link_active:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007249 if (hba->clk_scaling.is_allowed)
7250 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007251 ufshcd_vreg_set_hpm(hba);
7252 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
7253 ufshcd_set_link_active(hba);
7254 else if (ufshcd_is_link_off(hba))
7255 ufshcd_host_reset_and_restore(hba);
7256set_dev_active:
7257 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
7258 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007259enable_gating:
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007260 if (hba->clk_scaling.is_allowed)
7261 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007262 hba->clk_gating.is_suspended = false;
7263 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007264out:
7265 hba->pm_op_in_progress = 0;
7266 return ret;
7267}
7268
7269/**
7270 * ufshcd_resume - helper function for resume operations
7271 * @hba: per adapter instance
7272 * @pm_op: runtime PM or system PM
7273 *
7274 * This function basically brings the UFS device, UniPro link and controller
7275 * to active state.
7276 *
7277 * Returns 0 for success and non-zero for failure
7278 */
7279static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
7280{
7281 int ret;
7282 enum uic_link_state old_link_state;
7283
7284 hba->pm_op_in_progress = 1;
7285 old_link_state = hba->uic_link_state;
7286
7287 ufshcd_hba_vreg_set_hpm(hba);
7288 /* Make sure clocks are enabled before accessing controller */
7289 ret = ufshcd_setup_clocks(hba, true);
7290 if (ret)
7291 goto out;
7292
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007293 /* enable the host irq as host controller would be active soon */
7294 ret = ufshcd_enable_irq(hba);
7295 if (ret)
7296 goto disable_irq_and_vops_clks;
7297
7298 ret = ufshcd_vreg_set_hpm(hba);
7299 if (ret)
7300 goto disable_irq_and_vops_clks;
7301
7302 /*
7303 * Call vendor specific resume callback. As these callbacks may access
7304 * vendor specific host controller register space call them when the
7305 * host clocks are ON.
7306 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007307 ret = ufshcd_vops_resume(hba, pm_op);
7308 if (ret)
7309 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007310
7311 if (ufshcd_is_link_hibern8(hba)) {
7312 ret = ufshcd_uic_hibern8_exit(hba);
7313 if (!ret)
7314 ufshcd_set_link_active(hba);
7315 else
7316 goto vendor_suspend;
7317 } else if (ufshcd_is_link_off(hba)) {
7318 ret = ufshcd_host_reset_and_restore(hba);
7319 /*
7320 * ufshcd_host_reset_and_restore() should have already
7321 * set the link state as active
7322 */
7323 if (ret || !ufshcd_is_link_active(hba))
7324 goto vendor_suspend;
7325 }
7326
7327 if (!ufshcd_is_ufs_dev_active(hba)) {
7328 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
7329 if (ret)
7330 goto set_old_link_state;
7331 }
7332
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08007333 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
7334 ufshcd_enable_auto_bkops(hba);
7335 else
7336 /*
7337 * If BKOPs operations are urgently needed at this moment then
7338 * keep auto-bkops enabled or else disable it.
7339 */
7340 ufshcd_urgent_bkops(hba);
7341
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007342 hba->clk_gating.is_suspended = false;
7343
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007344 if (hba->clk_scaling.is_allowed)
7345 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007346
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007347 /* Schedule clock gating in case of no access to UFS device yet */
7348 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007349 goto out;
7350
7351set_old_link_state:
7352 ufshcd_link_state_transition(hba, old_link_state, 0);
7353vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02007354 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007355disable_vreg:
7356 ufshcd_vreg_set_lpm(hba);
7357disable_irq_and_vops_clks:
7358 ufshcd_disable_irq(hba);
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007359 if (hba->clk_scaling.is_allowed)
7360 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007361 ufshcd_setup_clocks(hba, false);
7362out:
7363 hba->pm_op_in_progress = 0;
7364 return ret;
7365}
7366
7367/**
7368 * ufshcd_system_suspend - system suspend routine
7369 * @hba: per adapter instance
7370 * @pm_op: runtime PM or system PM
7371 *
7372 * Check the description of ufshcd_suspend() function for more details.
7373 *
7374 * Returns 0 for success and non-zero for failure
7375 */
7376int ufshcd_system_suspend(struct ufs_hba *hba)
7377{
7378 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007379 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007380
7381 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03007382 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007383
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007384 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
7385 hba->curr_dev_pwr_mode) &&
7386 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
7387 hba->uic_link_state))
7388 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007389
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08007390 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007391 /*
7392 * UFS device and/or UFS link low power states during runtime
7393 * suspend seems to be different than what is expected during
7394 * system suspend. Hence runtime resume the devic & link and
7395 * let the system suspend low power states to take effect.
7396 * TODO: If resume takes longer time, we might have optimize
7397 * it in future by not resuming everything if possible.
7398 */
7399 ret = ufshcd_runtime_resume(hba);
7400 if (ret)
7401 goto out;
7402 }
7403
7404 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
7405out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007406 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
7407 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007408 hba->curr_dev_pwr_mode, hba->uic_link_state);
Dolev Ravive7850602014-09-25 15:32:36 +03007409 if (!ret)
7410 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007411 return ret;
7412}
7413EXPORT_SYMBOL(ufshcd_system_suspend);
7414
7415/**
7416 * ufshcd_system_resume - system resume routine
7417 * @hba: per adapter instance
7418 *
7419 * Returns 0 for success and non-zero for failure
7420 */
7421
7422int ufshcd_system_resume(struct ufs_hba *hba)
7423{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007424 int ret = 0;
7425 ktime_t start = ktime_get();
7426
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007427 if (!hba)
7428 return -EINVAL;
7429
7430 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007431 /*
7432 * Let the runtime resume take care of resuming
7433 * if runtime suspended.
7434 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007435 goto out;
7436 else
7437 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
7438out:
7439 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
7440 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007441 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007442 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007443}
7444EXPORT_SYMBOL(ufshcd_system_resume);
7445
7446/**
7447 * ufshcd_runtime_suspend - runtime suspend routine
7448 * @hba: per adapter instance
7449 *
7450 * Check the description of ufshcd_suspend() function for more details.
7451 *
7452 * Returns 0 for success and non-zero for failure
7453 */
7454int ufshcd_runtime_suspend(struct ufs_hba *hba)
7455{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007456 int ret = 0;
7457 ktime_t start = ktime_get();
7458
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007459 if (!hba)
7460 return -EINVAL;
7461
7462 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007463 goto out;
7464 else
7465 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
7466out:
7467 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
7468 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007469 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007470 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307471}
7472EXPORT_SYMBOL(ufshcd_runtime_suspend);
7473
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007474/**
7475 * ufshcd_runtime_resume - runtime resume routine
7476 * @hba: per adapter instance
7477 *
7478 * This function basically brings the UFS device, UniPro link and controller
7479 * to active state. Following operations are done in this function:
7480 *
7481 * 1. Turn on all the controller related clocks
7482 * 2. Bring the UniPro link out of Hibernate state
7483 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
7484 * to active state.
7485 * 4. If auto-bkops is enabled on the device, disable it.
7486 *
7487 * So following would be the possible power state after this function return
7488 * successfully:
7489 * S1: UFS device in Active state with VCC rail ON
7490 * UniPro link in Active state
7491 * All the UFS/UniPro controller clocks are ON
7492 *
7493 * Returns 0 for success and non-zero for failure
7494 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307495int ufshcd_runtime_resume(struct ufs_hba *hba)
7496{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007497 int ret = 0;
7498 ktime_t start = ktime_get();
7499
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07007500 if (!hba)
7501 return -EINVAL;
7502
7503 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007504 goto out;
7505 else
7506 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
7507out:
7508 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
7509 ktime_to_us(ktime_sub(ktime_get(), start)),
Subhash Jadavani73eba2b2017-01-10 16:48:25 -08007510 hba->curr_dev_pwr_mode, hba->uic_link_state);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007511 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307512}
7513EXPORT_SYMBOL(ufshcd_runtime_resume);
7514
7515int ufshcd_runtime_idle(struct ufs_hba *hba)
7516{
7517 return 0;
7518}
7519EXPORT_SYMBOL(ufshcd_runtime_idle);
7520
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -08007521static inline ssize_t ufshcd_pm_lvl_store(struct device *dev,
7522 struct device_attribute *attr,
7523 const char *buf, size_t count,
7524 bool rpm)
7525{
7526 struct ufs_hba *hba = dev_get_drvdata(dev);
7527 unsigned long flags, value;
7528
7529 if (kstrtoul(buf, 0, &value))
7530 return -EINVAL;
7531
7532 if ((value < UFS_PM_LVL_0) || (value >= UFS_PM_LVL_MAX))
7533 return -EINVAL;
7534
7535 spin_lock_irqsave(hba->host->host_lock, flags);
7536 if (rpm)
7537 hba->rpm_lvl = value;
7538 else
7539 hba->spm_lvl = value;
7540 spin_unlock_irqrestore(hba->host->host_lock, flags);
7541 return count;
7542}
7543
7544static ssize_t ufshcd_rpm_lvl_show(struct device *dev,
7545 struct device_attribute *attr, char *buf)
7546{
7547 struct ufs_hba *hba = dev_get_drvdata(dev);
7548 int curr_len;
7549 u8 lvl;
7550
7551 curr_len = snprintf(buf, PAGE_SIZE,
7552 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
7553 hba->rpm_lvl,
7554 ufschd_ufs_dev_pwr_mode_to_string(
7555 ufs_pm_lvl_states[hba->rpm_lvl].dev_state),
7556 ufschd_uic_link_state_to_string(
7557 ufs_pm_lvl_states[hba->rpm_lvl].link_state));
7558
7559 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7560 "\nAll available Runtime PM levels info:\n");
7561 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
7562 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7563 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
7564 lvl,
7565 ufschd_ufs_dev_pwr_mode_to_string(
7566 ufs_pm_lvl_states[lvl].dev_state),
7567 ufschd_uic_link_state_to_string(
7568 ufs_pm_lvl_states[lvl].link_state));
7569
7570 return curr_len;
7571}
7572
7573static ssize_t ufshcd_rpm_lvl_store(struct device *dev,
7574 struct device_attribute *attr, const char *buf, size_t count)
7575{
7576 return ufshcd_pm_lvl_store(dev, attr, buf, count, true);
7577}
7578
7579static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba)
7580{
7581 hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show;
7582 hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store;
7583 sysfs_attr_init(&hba->rpm_lvl_attr.attr);
7584 hba->rpm_lvl_attr.attr.name = "rpm_lvl";
7585 hba->rpm_lvl_attr.attr.mode = 0644;
7586 if (device_create_file(hba->dev, &hba->rpm_lvl_attr))
7587 dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n");
7588}
7589
7590static ssize_t ufshcd_spm_lvl_show(struct device *dev,
7591 struct device_attribute *attr, char *buf)
7592{
7593 struct ufs_hba *hba = dev_get_drvdata(dev);
7594 int curr_len;
7595 u8 lvl;
7596
7597 curr_len = snprintf(buf, PAGE_SIZE,
7598 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
7599 hba->spm_lvl,
7600 ufschd_ufs_dev_pwr_mode_to_string(
7601 ufs_pm_lvl_states[hba->spm_lvl].dev_state),
7602 ufschd_uic_link_state_to_string(
7603 ufs_pm_lvl_states[hba->spm_lvl].link_state));
7604
7605 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7606 "\nAll available System PM levels info:\n");
7607 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
7608 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
7609 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
7610 lvl,
7611 ufschd_ufs_dev_pwr_mode_to_string(
7612 ufs_pm_lvl_states[lvl].dev_state),
7613 ufschd_uic_link_state_to_string(
7614 ufs_pm_lvl_states[lvl].link_state));
7615
7616 return curr_len;
7617}
7618
7619static ssize_t ufshcd_spm_lvl_store(struct device *dev,
7620 struct device_attribute *attr, const char *buf, size_t count)
7621{
7622 return ufshcd_pm_lvl_store(dev, attr, buf, count, false);
7623}
7624
7625static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba)
7626{
7627 hba->spm_lvl_attr.show = ufshcd_spm_lvl_show;
7628 hba->spm_lvl_attr.store = ufshcd_spm_lvl_store;
7629 sysfs_attr_init(&hba->spm_lvl_attr.attr);
7630 hba->spm_lvl_attr.attr.name = "spm_lvl";
7631 hba->spm_lvl_attr.attr.mode = 0644;
7632 if (device_create_file(hba->dev, &hba->spm_lvl_attr))
7633 dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n");
7634}
7635
7636static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba)
7637{
7638 ufshcd_add_rpm_lvl_sysfs_nodes(hba);
7639 ufshcd_add_spm_lvl_sysfs_nodes(hba);
7640}
7641
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307642/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007643 * ufshcd_shutdown - shutdown routine
7644 * @hba: per adapter instance
7645 *
7646 * This function would power off both UFS device and UFS link.
7647 *
7648 * Returns 0 always to allow force shutdown even in case of errors.
7649 */
7650int ufshcd_shutdown(struct ufs_hba *hba)
7651{
7652 int ret = 0;
7653
7654 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
7655 goto out;
7656
7657 if (pm_runtime_suspended(hba->dev)) {
7658 ret = ufshcd_runtime_resume(hba);
7659 if (ret)
7660 goto out;
7661 }
7662
7663 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
7664out:
7665 if (ret)
7666 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
7667 /* allow force shutdown even in case of errors */
7668 return 0;
7669}
7670EXPORT_SYMBOL(ufshcd_shutdown);
7671
7672/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307673 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307674 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307675 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307676 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307677void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307678{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05307679 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307680 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307681 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02007682 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307683
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007684 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007685 if (ufshcd_is_clkscaling_supported(hba))
7686 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007687 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307688}
7689EXPORT_SYMBOL_GPL(ufshcd_remove);
7690
7691/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02007692 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
7693 * @hba: pointer to Host Bus Adapter (HBA)
7694 */
7695void ufshcd_dealloc_host(struct ufs_hba *hba)
7696{
7697 scsi_host_put(hba->host);
7698}
7699EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
7700
7701/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007702 * ufshcd_set_dma_mask - Set dma mask based on the controller
7703 * addressing capability
7704 * @hba: per adapter instance
7705 *
7706 * Returns 0 for success, non-zero for failure
7707 */
7708static int ufshcd_set_dma_mask(struct ufs_hba *hba)
7709{
7710 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
7711 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
7712 return 0;
7713 }
7714 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
7715}
7716
7717/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007718 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307719 * @dev: pointer to device handle
7720 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307721 * Returns 0 on success, non-zero value on failure
7722 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007723int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307724{
7725 struct Scsi_Host *host;
7726 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007727 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307728
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307729 if (!dev) {
7730 dev_err(dev,
7731 "Invalid memory reference for dev is NULL\n");
7732 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307733 goto out_error;
7734 }
7735
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307736 host = scsi_host_alloc(&ufshcd_driver_template,
7737 sizeof(struct ufs_hba));
7738 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307739 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307740 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307741 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307742 }
7743 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307744 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307745 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007746 *hba_handle = hba;
7747
7748out_error:
7749 return err;
7750}
7751EXPORT_SYMBOL(ufshcd_alloc_host);
7752
7753/**
7754 * ufshcd_init - Driver initialization routine
7755 * @hba: per-adapter instance
7756 * @mmio_base: base register address
7757 * @irq: Interrupt line of device
7758 * Returns 0 on success, non-zero value on failure
7759 */
7760int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
7761{
7762 int err;
7763 struct Scsi_Host *host = hba->host;
7764 struct device *dev = hba->dev;
7765
7766 if (!mmio_base) {
7767 dev_err(hba->dev,
7768 "Invalid memory reference for mmio_base is NULL\n");
7769 err = -ENODEV;
7770 goto out_error;
7771 }
7772
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307773 hba->mmio_base = mmio_base;
7774 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307775
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007776 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007777 if (err)
7778 goto out_error;
7779
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307780 /* Read capabilities registers */
7781 ufshcd_hba_capabilities(hba);
7782
7783 /* Get UFS version supported by the controller */
7784 hba->ufs_version = ufshcd_get_ufs_version(hba);
7785
Yaniv Gardic01848c2016-12-05 19:25:02 -08007786 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
7787 (hba->ufs_version != UFSHCI_VERSION_11) &&
7788 (hba->ufs_version != UFSHCI_VERSION_20) &&
7789 (hba->ufs_version != UFSHCI_VERSION_21))
7790 dev_err(hba->dev, "invalid UFS version 0x%x\n",
7791 hba->ufs_version);
7792
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307793 /* Get Interrupt bit mask per version */
7794 hba->intr_mask = ufshcd_get_intr_mask(hba);
7795
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007796 err = ufshcd_set_dma_mask(hba);
7797 if (err) {
7798 dev_err(hba->dev, "set dma mask failed\n");
7799 goto out_disable;
7800 }
7801
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307802 /* Allocate memory for host memory space */
7803 err = ufshcd_memory_alloc(hba);
7804 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307805 dev_err(hba->dev, "Memory allocation failed\n");
7806 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307807 }
7808
7809 /* Configure LRB */
7810 ufshcd_host_memory_configure(hba);
7811
7812 host->can_queue = hba->nutrs;
7813 host->cmd_per_lun = hba->nutrs;
7814 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03007815 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307816 host->max_channel = UFSHCD_MAX_CHANNEL;
7817 host->unique_id = host->host_no;
7818 host->max_cmd_len = MAX_CDB_SIZE;
7819
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007820 hba->max_pwr_info.is_valid = false;
7821
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307822 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05307823 init_waitqueue_head(&hba->tm_wq);
7824 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307825
7826 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05307827 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307828 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307829
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307830 /* Initialize UIC command mutex */
7831 mutex_init(&hba->uic_cmd_mutex);
7832
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307833 /* Initialize mutex for device management commands */
7834 mutex_init(&hba->dev_cmd.lock);
7835
subhashj@codeaurora.orga3cd5ec2017-02-03 16:57:02 -08007836 init_rwsem(&hba->clk_scaling_lock);
7837
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307838 /* Initialize device management tag acquire wait queue */
7839 init_waitqueue_head(&hba->dev_cmd.tag_wq);
7840
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007841 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02007842
7843 /*
7844 * In order to avoid any spurious interrupt immediately after
7845 * registering UFS controller interrupt handler, clear any pending UFS
7846 * interrupt status and disable all the UFS interrupts.
7847 */
7848 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
7849 REG_INTERRUPT_STATUS);
7850 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
7851 /*
7852 * Make sure that UFS interrupts are disabled and any pending interrupt
7853 * status is cleared before registering UFS interrupt handler.
7854 */
7855 mb();
7856
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307857 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09007858 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307859 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307860 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007861 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007862 } else {
7863 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307864 }
7865
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307866 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307867 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307868 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007869 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307870 }
7871
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307872 /* Host controller enable */
7873 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307874 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307875 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08007876 ufshcd_print_host_regs(hba);
Gilad Broner6ba65582017-02-03 16:57:28 -08007877 ufshcd_print_host_state(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307878 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307879 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307880
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007881 if (ufshcd_is_clkscaling_supported(hba)) {
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007882 char wq_name[sizeof("ufs_clkscaling_00")];
7883
subhashj@codeaurora.org401f1e42017-02-03 16:57:39 -08007884 INIT_WORK(&hba->clk_scaling.suspend_work,
7885 ufshcd_clk_scaling_suspend_work);
7886 INIT_WORK(&hba->clk_scaling.resume_work,
7887 ufshcd_clk_scaling_resume_work);
7888
7889 snprintf(wq_name, ARRAY_SIZE(wq_name), "ufs_clkscaling_%d",
7890 host->host_no);
7891 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name);
7892
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007893 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007894 }
7895
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08007896 /*
7897 * Set the default power management level for runtime and system PM.
7898 * Default power saving mode is to keep UFS link in Hibern8 state
7899 * and UFS device in sleep state.
7900 */
7901 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7902 UFS_SLEEP_PWR_MODE,
7903 UIC_LINK_HIBERN8_STATE);
7904 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7905 UFS_SLEEP_PWR_MODE,
7906 UIC_LINK_HIBERN8_STATE);
7907
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05307908 /* Hold auto suspend until async scan completes */
7909 pm_runtime_get_sync(dev);
7910
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007911 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007912 * We are assuming that device wasn't put in sleep/power-down
7913 * state exclusively during the boot stage before kernel.
7914 * This assumption helps avoid doing link startup twice during
7915 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007916 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007917 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007918
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307919 async_schedule(ufshcd_async_scan, hba);
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -08007920 ufshcd_add_sysfs_nodes(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307921
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307922 return 0;
7923
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307924out_remove_scsi_host:
7925 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007926exit_gating:
7927 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307928out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007929 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007930 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307931out_error:
7932 return err;
7933}
7934EXPORT_SYMBOL_GPL(ufshcd_init);
7935
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307936MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7937MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05307938MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307939MODULE_LICENSE("GPL");
7940MODULE_VERSION(UFSHCD_DRIVER_VERSION);