blob: 184106fa21da586c0341e5bf559c6208c2f8c6f9 [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);
251static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300252static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300253static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
254 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300255static int ufshcd_change_power_mode(struct ufs_hba *hba,
256 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200257static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
258{
259 return tag >= 0 && tag < hba->nutrs;
260}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300261
262static inline int ufshcd_enable_irq(struct ufs_hba *hba)
263{
264 int ret = 0;
265
266 if (!hba->is_irq_enabled) {
267 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
268 hba);
269 if (ret)
270 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
271 __func__, ret);
272 hba->is_irq_enabled = true;
273 }
274
275 return ret;
276}
277
278static inline void ufshcd_disable_irq(struct ufs_hba *hba)
279{
280 if (hba->is_irq_enabled) {
281 free_irq(hba->irq, hba);
282 hba->is_irq_enabled = false;
283 }
284}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530285
Yaniv Gardib573d482016-03-10 17:37:09 +0200286/* replace non-printable or non-ASCII characters with spaces */
287static inline void ufshcd_remove_non_printable(char *val)
288{
289 if (!val)
290 return;
291
292 if (*val < 0x20 || *val > 0x7e)
293 *val = ' ';
294}
295
Dolev Raviv66cc8202016-12-22 18:39:42 -0800296static void ufshcd_print_host_regs(struct ufs_hba *hba)
297{
298 /*
299 * hex_dump reads its data without the readl macro. This might
300 * cause inconsistency issues on some platform, as the printed
301 * values may be from cache and not the most recent value.
302 * To know whether you are looking at an un-cached version verify
303 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
304 * during platform/pci probe function.
305 */
306 ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE);
307 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
308 hba->ufs_version, hba->capabilities);
309 dev_err(hba->dev,
310 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
311 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
312}
313
314static
315void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
316{
317 struct ufshcd_lrb *lrbp;
318 int tag;
319
320 for_each_set_bit(tag, &bitmap, hba->nutrs) {
321 lrbp = &hba->lrb[tag];
322
323 dev_err(hba->dev, "UPIU[%d] - Transfer Request Descriptor\n",
324 tag);
325 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
326 sizeof(struct utp_transfer_req_desc));
327 dev_err(hba->dev, "UPIU[%d] - Request UPIU\n", tag);
328 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
329 sizeof(struct utp_upiu_req));
330 dev_err(hba->dev, "UPIU[%d] - Response UPIU\n", tag);
331 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
332 sizeof(struct utp_upiu_rsp));
333 if (pr_prdt) {
334 int prdt_length = le16_to_cpu(
335 lrbp->utr_descriptor_ptr->prd_table_length);
336
337 dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries\n", tag,
338 prdt_length);
339 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
340 sizeof(struct ufshcd_sg_entry) *
341 prdt_length);
342 }
343 }
344}
345
346static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
347{
348 struct utp_task_req_desc *tmrdp;
349 int tag;
350
351 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
352 tmrdp = &hba->utmrdl_base_addr[tag];
353 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
354 ufshcd_hex_dump("TM TRD: ", &tmrdp->header,
355 sizeof(struct request_desc_header));
356 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU\n",
357 tag);
358 ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu,
359 sizeof(struct utp_upiu_req));
360 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU\n",
361 tag);
362 ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu,
363 sizeof(struct utp_task_req_desc));
364 }
365}
366
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530367/*
368 * ufshcd_wait_for_register - wait for register value to change
369 * @hba - per-adapter interface
370 * @reg - mmio register offset
371 * @mask - mask to apply to read register value
372 * @val - wait condition
373 * @interval_us - polling interval in microsecs
374 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200375 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530376 *
377 * Returns -ETIMEDOUT on error, zero on success
378 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200379int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
380 u32 val, unsigned long interval_us,
381 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530382{
383 int err = 0;
384 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
385
386 /* ignore bits that we don't intend to wait on */
387 val = val & mask;
388
389 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200390 if (can_sleep)
391 usleep_range(interval_us, interval_us + 50);
392 else
393 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530394 if (time_after(jiffies, timeout)) {
395 if ((ufshcd_readl(hba, reg) & mask) != val)
396 err = -ETIMEDOUT;
397 break;
398 }
399 }
400
401 return err;
402}
403
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530404/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530405 * ufshcd_get_intr_mask - Get the interrupt bit mask
406 * @hba - Pointer to adapter instance
407 *
408 * Returns interrupt bit mask per version
409 */
410static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
411{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800412 u32 intr_mask = 0;
413
414 switch (hba->ufs_version) {
415 case UFSHCI_VERSION_10:
416 intr_mask = INTERRUPT_MASK_ALL_VER_10;
417 break;
418 /* allow fall through */
419 case UFSHCI_VERSION_11:
420 case UFSHCI_VERSION_20:
421 intr_mask = INTERRUPT_MASK_ALL_VER_11;
422 break;
423 /* allow fall through */
424 case UFSHCI_VERSION_21:
425 default:
426 intr_mask = INTERRUPT_MASK_ALL_VER_21;
427 }
428
429 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530430}
431
432/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530433 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
434 * @hba - Pointer to adapter instance
435 *
436 * Returns UFSHCI version supported by the controller
437 */
438static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
439{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200440 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
441 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300442
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530443 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530444}
445
446/**
447 * ufshcd_is_device_present - Check if any device connected to
448 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300449 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530450 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530451 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530452 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300453static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530454{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300455 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
456 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530457}
458
459/**
460 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
461 * @lrb: pointer to local command reference block
462 *
463 * This function is used to get the OCS field from UTRD
464 * Returns the OCS field in the UTRD
465 */
466static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
467{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530468 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530469}
470
471/**
472 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
473 * @task_req_descp: pointer to utp_task_req_desc structure
474 *
475 * This function is used to get the OCS field from UTMRD
476 * Returns the OCS field in the UTMRD
477 */
478static inline int
479ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
480{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530481 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530482}
483
484/**
485 * ufshcd_get_tm_free_slot - get a free slot for task management request
486 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530487 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530488 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530489 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
490 * Returns 0 if free slot is not available, else return 1 with tag value
491 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530492 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530493static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530494{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530495 int tag;
496 bool ret = false;
497
498 if (!free_slot)
499 goto out;
500
501 do {
502 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
503 if (tag >= hba->nutmrs)
504 goto out;
505 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
506
507 *free_slot = tag;
508 ret = true;
509out:
510 return ret;
511}
512
513static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
514{
515 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530516}
517
518/**
519 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
520 * @hba: per adapter instance
521 * @pos: position of the bit to be cleared
522 */
523static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
524{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530525 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530526}
527
528/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200529 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
530 * @hba: per adapter instance
531 * @tag: position of the bit to be cleared
532 */
533static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
534{
535 __clear_bit(tag, &hba->outstanding_reqs);
536}
537
538/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530539 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
540 * @reg: Register value of host controller status
541 *
542 * Returns integer, 0 on Success and positive value if failed
543 */
544static inline int ufshcd_get_lists_status(u32 reg)
545{
546 /*
547 * The mask 0xFF is for the following HCS register bits
548 * Bit Description
549 * 0 Device Present
550 * 1 UTRLRDY
551 * 2 UTMRLRDY
552 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200553 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530554 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200555 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530556}
557
558/**
559 * ufshcd_get_uic_cmd_result - Get the UIC command result
560 * @hba: Pointer to adapter instance
561 *
562 * This function gets the result of UIC command completion
563 * Returns 0 on success, non zero value on error
564 */
565static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
566{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530567 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530568 MASK_UIC_COMMAND_RESULT;
569}
570
571/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530572 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
573 * @hba: Pointer to adapter instance
574 *
575 * This function gets UIC command argument3
576 * Returns 0 on success, non zero value on error
577 */
578static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
579{
580 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
581}
582
583/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530584 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530585 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530586 */
587static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530588ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530589{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530590 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530591}
592
593/**
594 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
595 * @ucd_rsp_ptr: pointer to response UPIU
596 *
597 * This function gets the response status and scsi_status from response UPIU
598 * Returns the response result code.
599 */
600static inline int
601ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
602{
603 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
604}
605
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530606/*
607 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
608 * from response UPIU
609 * @ucd_rsp_ptr: pointer to response UPIU
610 *
611 * Return the data segment length.
612 */
613static inline unsigned int
614ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
615{
616 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
617 MASK_RSP_UPIU_DATA_SEG_LEN;
618}
619
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530620/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530621 * ufshcd_is_exception_event - Check if the device raised an exception event
622 * @ucd_rsp_ptr: pointer to response UPIU
623 *
624 * The function checks if the device raised an exception event indicated in
625 * the Device Information field of response UPIU.
626 *
627 * Returns true if exception is raised, false otherwise.
628 */
629static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
630{
631 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
632 MASK_RSP_EXCEPTION_EVENT ? true : false;
633}
634
635/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530636 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530637 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530638 */
639static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530640ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530641{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530642 ufshcd_writel(hba, INT_AGGR_ENABLE |
643 INT_AGGR_COUNTER_AND_TIMER_RESET,
644 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
645}
646
647/**
648 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
649 * @hba: per adapter instance
650 * @cnt: Interrupt aggregation counter threshold
651 * @tmout: Interrupt aggregation timeout value
652 */
653static inline void
654ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
655{
656 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
657 INT_AGGR_COUNTER_THLD_VAL(cnt) |
658 INT_AGGR_TIMEOUT_VAL(tmout),
659 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530660}
661
662/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300663 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
664 * @hba: per adapter instance
665 */
666static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
667{
668 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
669}
670
671/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530672 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
673 * When run-stop registers are set to 1, it indicates the
674 * host controller that it can process the requests
675 * @hba: per adapter instance
676 */
677static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
678{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530679 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
680 REG_UTP_TASK_REQ_LIST_RUN_STOP);
681 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
682 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530683}
684
685/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530686 * ufshcd_hba_start - Start controller initialization sequence
687 * @hba: per adapter instance
688 */
689static inline void ufshcd_hba_start(struct ufs_hba *hba)
690{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530691 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530692}
693
694/**
695 * ufshcd_is_hba_active - Get controller state
696 * @hba: per adapter instance
697 *
698 * Returns zero if controller is active, 1 otherwise
699 */
700static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
701{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530702 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530703}
704
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -0800705static const char *ufschd_uic_link_state_to_string(
706 enum uic_link_state state)
707{
708 switch (state) {
709 case UIC_LINK_OFF_STATE: return "OFF";
710 case UIC_LINK_ACTIVE_STATE: return "ACTIVE";
711 case UIC_LINK_HIBERN8_STATE: return "HIBERN8";
712 default: return "UNKNOWN";
713 }
714}
715
716static const char *ufschd_ufs_dev_pwr_mode_to_string(
717 enum ufs_dev_pwr_mode state)
718{
719 switch (state) {
720 case UFS_ACTIVE_PWR_MODE: return "ACTIVE";
721 case UFS_SLEEP_PWR_MODE: return "SLEEP";
722 case UFS_POWERDOWN_PWR_MODE: return "POWERDOWN";
723 default: return "UNKNOWN";
724 }
725}
726
Yaniv Gardi37113102016-03-10 17:37:16 +0200727u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
728{
729 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
730 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
731 (hba->ufs_version == UFSHCI_VERSION_11))
732 return UFS_UNIPRO_VER_1_41;
733 else
734 return UFS_UNIPRO_VER_1_6;
735}
736EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
737
738static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
739{
740 /*
741 * If both host and device support UniPro ver1.6 or later, PA layer
742 * parameters tuning happens during link startup itself.
743 *
744 * We can manually tune PA layer parameters if either host or device
745 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
746 * logic simple, we will only do manual tuning if local unipro version
747 * doesn't support ver1.6 or later.
748 */
749 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
750 return true;
751 else
752 return false;
753}
754
Gilad Bronera5082532016-10-17 17:10:00 -0700755static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
756{
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800757 if (!ufshcd_is_clkscaling_supported(hba))
758 return;
759
760 devfreq_suspend_device(hba->devfreq);
761 hba->clk_scaling.window_start_t = 0;
Gilad Bronera5082532016-10-17 17:10:00 -0700762}
763
764static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
765{
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800766 devfreq_resume_device(hba->devfreq);
767}
768
769static ssize_t ufshcd_clkscale_enable_show(struct device *dev,
770 struct device_attribute *attr, char *buf)
771{
772 struct ufs_hba *hba = dev_get_drvdata(dev);
773
774 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_scaling.is_allowed);
775}
776
777static ssize_t ufshcd_clkscale_enable_store(struct device *dev,
778 struct device_attribute *attr, const char *buf, size_t count)
779{
780 struct ufs_hba *hba = dev_get_drvdata(dev);
781 u32 value;
782 int err;
783
784 if (kstrtou32(buf, 0, &value))
785 return -EINVAL;
786
787 value = !!value;
788 if (value == hba->clk_scaling.is_allowed)
789 goto out;
790
791 pm_runtime_get_sync(hba->dev);
792 ufshcd_hold(hba, false);
793
794 if (value) {
795 ufshcd_resume_clkscaling(hba);
796 } else {
797 ufshcd_suspend_clkscaling(hba);
798 err = ufshcd_scale_clks(hba, true);
799 if (err)
800 dev_err(hba->dev, "%s: failed to scale clocks up %d\n",
801 __func__, err);
802 }
803 hba->clk_scaling.is_allowed = value;
804
805 ufshcd_release(hba);
806 pm_runtime_put_sync(hba->dev);
807out:
808 return count;
Gilad Bronera5082532016-10-17 17:10:00 -0700809}
810
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300811static void ufshcd_ungate_work(struct work_struct *work)
812{
813 int ret;
814 unsigned long flags;
815 struct ufs_hba *hba = container_of(work, struct ufs_hba,
816 clk_gating.ungate_work);
817
818 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
819
820 spin_lock_irqsave(hba->host->host_lock, flags);
821 if (hba->clk_gating.state == CLKS_ON) {
822 spin_unlock_irqrestore(hba->host->host_lock, flags);
823 goto unblock_reqs;
824 }
825
826 spin_unlock_irqrestore(hba->host->host_lock, flags);
827 ufshcd_setup_clocks(hba, true);
828
829 /* Exit from hibern8 */
830 if (ufshcd_can_hibern8_during_gating(hba)) {
831 /* Prevent gating in this path */
832 hba->clk_gating.is_suspended = true;
833 if (ufshcd_is_link_hibern8(hba)) {
834 ret = ufshcd_uic_hibern8_exit(hba);
835 if (ret)
836 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
837 __func__, ret);
838 else
839 ufshcd_set_link_active(hba);
840 }
841 hba->clk_gating.is_suspended = false;
842 }
843unblock_reqs:
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -0800844 if (hba->clk_scaling.is_allowed)
845 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300846 scsi_unblock_requests(hba->host);
847}
848
849/**
850 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
851 * Also, exit from hibern8 mode and set the link as active.
852 * @hba: per adapter instance
853 * @async: This indicates whether caller should ungate clocks asynchronously.
854 */
855int ufshcd_hold(struct ufs_hba *hba, bool async)
856{
857 int rc = 0;
858 unsigned long flags;
859
860 if (!ufshcd_is_clkgating_allowed(hba))
861 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300862 spin_lock_irqsave(hba->host->host_lock, flags);
863 hba->clk_gating.active_reqs++;
864
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200865 if (ufshcd_eh_in_progress(hba)) {
866 spin_unlock_irqrestore(hba->host->host_lock, flags);
867 return 0;
868 }
869
Sahitya Tummala856b3482014-09-25 15:32:34 +0300870start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300871 switch (hba->clk_gating.state) {
872 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -0700873 /*
874 * Wait for the ungate work to complete if in progress.
875 * Though the clocks may be in ON state, the link could
876 * still be in hibner8 state if hibern8 is allowed
877 * during clock gating.
878 * Make sure we exit hibern8 state also in addition to
879 * clocks being ON.
880 */
881 if (ufshcd_can_hibern8_during_gating(hba) &&
882 ufshcd_is_link_hibern8(hba)) {
883 spin_unlock_irqrestore(hba->host->host_lock, flags);
884 flush_work(&hba->clk_gating.ungate_work);
885 spin_lock_irqsave(hba->host->host_lock, flags);
886 goto start;
887 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300888 break;
889 case REQ_CLKS_OFF:
890 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
891 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800892 trace_ufshcd_clk_gating(dev_name(hba->dev),
893 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300894 break;
895 }
896 /*
897 * If we here, it means gating work is either done or
898 * currently running. Hence, fall through to cancel gating
899 * work and to enable clocks.
900 */
901 case CLKS_OFF:
902 scsi_block_requests(hba->host);
903 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800904 trace_ufshcd_clk_gating(dev_name(hba->dev),
905 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300906 schedule_work(&hba->clk_gating.ungate_work);
907 /*
908 * fall through to check if we should wait for this
909 * work to be done or not.
910 */
911 case REQ_CLKS_ON:
912 if (async) {
913 rc = -EAGAIN;
914 hba->clk_gating.active_reqs--;
915 break;
916 }
917
918 spin_unlock_irqrestore(hba->host->host_lock, flags);
919 flush_work(&hba->clk_gating.ungate_work);
920 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300921 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300922 goto start;
923 default:
924 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
925 __func__, hba->clk_gating.state);
926 break;
927 }
928 spin_unlock_irqrestore(hba->host->host_lock, flags);
929out:
930 return rc;
931}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200932EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300933
934static void ufshcd_gate_work(struct work_struct *work)
935{
936 struct ufs_hba *hba = container_of(work, struct ufs_hba,
937 clk_gating.gate_work.work);
938 unsigned long flags;
939
940 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -0700941 /*
942 * In case you are here to cancel this work the gating state
943 * would be marked as REQ_CLKS_ON. In this case save time by
944 * skipping the gating work and exit after changing the clock
945 * state to CLKS_ON.
946 */
947 if (hba->clk_gating.is_suspended ||
948 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300949 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800950 trace_ufshcd_clk_gating(dev_name(hba->dev),
951 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300952 goto rel_lock;
953 }
954
955 if (hba->clk_gating.active_reqs
956 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
957 || hba->lrb_in_use || hba->outstanding_tasks
958 || hba->active_uic_cmd || hba->uic_async_done)
959 goto rel_lock;
960
961 spin_unlock_irqrestore(hba->host->host_lock, flags);
962
963 /* put the link into hibern8 mode before turning off clocks */
964 if (ufshcd_can_hibern8_during_gating(hba)) {
965 if (ufshcd_uic_hibern8_enter(hba)) {
966 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800967 trace_ufshcd_clk_gating(dev_name(hba->dev),
968 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300969 goto out;
970 }
971 ufshcd_set_link_hibern8(hba);
972 }
973
Gilad Bronera5082532016-10-17 17:10:00 -0700974 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +0300975
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300976 if (!ufshcd_is_link_active(hba))
977 ufshcd_setup_clocks(hba, false);
978 else
979 /* If link is active, device ref_clk can't be switched off */
980 __ufshcd_setup_clocks(hba, false, true);
981
982 /*
983 * In case you are here to cancel this work the gating state
984 * would be marked as REQ_CLKS_ON. In this case keep the state
985 * as REQ_CLKS_ON which would anyway imply that clocks are off
986 * and a request to turn them on is pending. By doing this way,
987 * we keep the state machine in tact and this would ultimately
988 * prevent from doing cancel work multiple times when there are
989 * new requests arriving before the current cancel work is done.
990 */
991 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800992 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300993 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800994 trace_ufshcd_clk_gating(dev_name(hba->dev),
995 hba->clk_gating.state);
996 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300997rel_lock:
998 spin_unlock_irqrestore(hba->host->host_lock, flags);
999out:
1000 return;
1001}
1002
1003/* host lock must be held before calling this variant */
1004static void __ufshcd_release(struct ufs_hba *hba)
1005{
1006 if (!ufshcd_is_clkgating_allowed(hba))
1007 return;
1008
1009 hba->clk_gating.active_reqs--;
1010
1011 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
1012 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
1013 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001014 || hba->active_uic_cmd || hba->uic_async_done
1015 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001016 return;
1017
1018 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08001019 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001020 schedule_delayed_work(&hba->clk_gating.gate_work,
1021 msecs_to_jiffies(hba->clk_gating.delay_ms));
1022}
1023
1024void ufshcd_release(struct ufs_hba *hba)
1025{
1026 unsigned long flags;
1027
1028 spin_lock_irqsave(hba->host->host_lock, flags);
1029 __ufshcd_release(hba);
1030 spin_unlock_irqrestore(hba->host->host_lock, flags);
1031}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +02001032EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001033
1034static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
1035 struct device_attribute *attr, char *buf)
1036{
1037 struct ufs_hba *hba = dev_get_drvdata(dev);
1038
1039 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
1040}
1041
1042static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
1043 struct device_attribute *attr, const char *buf, size_t count)
1044{
1045 struct ufs_hba *hba = dev_get_drvdata(dev);
1046 unsigned long flags, value;
1047
1048 if (kstrtoul(buf, 0, &value))
1049 return -EINVAL;
1050
1051 spin_lock_irqsave(hba->host->host_lock, flags);
1052 hba->clk_gating.delay_ms = value;
1053 spin_unlock_irqrestore(hba->host->host_lock, flags);
1054 return count;
1055}
1056
Sahitya Tummalab4274112016-12-22 18:40:39 -08001057static ssize_t ufshcd_clkgate_enable_show(struct device *dev,
1058 struct device_attribute *attr, char *buf)
1059{
1060 struct ufs_hba *hba = dev_get_drvdata(dev);
1061
1062 return snprintf(buf, PAGE_SIZE, "%d\n", hba->clk_gating.is_enabled);
1063}
1064
1065static ssize_t ufshcd_clkgate_enable_store(struct device *dev,
1066 struct device_attribute *attr, const char *buf, size_t count)
1067{
1068 struct ufs_hba *hba = dev_get_drvdata(dev);
1069 unsigned long flags;
1070 u32 value;
1071
1072 if (kstrtou32(buf, 0, &value))
1073 return -EINVAL;
1074
1075 value = !!value;
1076 if (value == hba->clk_gating.is_enabled)
1077 goto out;
1078
1079 if (value) {
1080 ufshcd_release(hba);
1081 } else {
1082 spin_lock_irqsave(hba->host->host_lock, flags);
1083 hba->clk_gating.active_reqs++;
1084 spin_unlock_irqrestore(hba->host->host_lock, flags);
1085 }
1086
1087 hba->clk_gating.is_enabled = value;
1088out:
1089 return count;
1090}
1091
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001092static void ufshcd_init_clk_gating(struct ufs_hba *hba)
1093{
1094 if (!ufshcd_is_clkgating_allowed(hba))
1095 return;
1096
1097 hba->clk_gating.delay_ms = 150;
1098 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
1099 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
1100
Sahitya Tummalab4274112016-12-22 18:40:39 -08001101 hba->clk_gating.is_enabled = true;
1102
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001103 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
1104 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
1105 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
1106 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
Sahitya Tummalab4274112016-12-22 18:40:39 -08001107 hba->clk_gating.delay_attr.attr.mode = 0644;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001108 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
1109 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
Sahitya Tummalab4274112016-12-22 18:40:39 -08001110
1111 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show;
1112 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store;
1113 sysfs_attr_init(&hba->clk_gating.enable_attr.attr);
1114 hba->clk_gating.enable_attr.attr.name = "clkgate_enable";
1115 hba->clk_gating.enable_attr.attr.mode = 0644;
1116 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr))
1117 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001118}
1119
1120static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
1121{
1122 if (!ufshcd_is_clkgating_allowed(hba))
1123 return;
1124 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Sahitya Tummalab4274112016-12-22 18:40:39 -08001125 device_remove_file(hba->dev, &hba->clk_gating.enable_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +09001126 cancel_work_sync(&hba->clk_gating.ungate_work);
1127 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001128}
1129
Sahitya Tummala856b3482014-09-25 15:32:34 +03001130/* Must be called with host lock acquired */
1131static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1132{
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001133 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001134 return;
1135
1136 if (!hba->clk_scaling.is_busy_started) {
1137 hba->clk_scaling.busy_start_t = ktime_get();
1138 hba->clk_scaling.is_busy_started = true;
1139 }
1140}
1141
1142static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1143{
1144 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1145
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08001146 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03001147 return;
1148
1149 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1150 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1151 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001152 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001153 scaling->is_busy_started = false;
1154 }
1155}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301156/**
1157 * ufshcd_send_command - Send SCSI or device management commands
1158 * @hba: per adapter instance
1159 * @task_tag: Task tag of the command
1160 */
1161static inline
1162void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1163{
Sahitya Tummala856b3482014-09-25 15:32:34 +03001164 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301165 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301166 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001167 /* Make sure that doorbell is committed immediately */
1168 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301169}
1170
1171/**
1172 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1173 * @lrb - pointer to local reference block
1174 */
1175static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1176{
1177 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301178 if (lrbp->sense_buffer &&
1179 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001180 int len_to_copy;
1181
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301182 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001183 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
1184
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301185 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301186 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001187 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301188 }
1189}
1190
1191/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301192 * ufshcd_copy_query_response() - Copy the Query Response and the data
1193 * descriptor
1194 * @hba: per adapter instance
1195 * @lrb - pointer to local reference block
1196 */
1197static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001198int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301199{
1200 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1201
Dolev Raviv68078d52013-07-30 00:35:58 +05301202 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301203
Dolev Raviv68078d52013-07-30 00:35:58 +05301204 /* Get the descriptor */
1205 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001206 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301207 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001208 u16 resp_len;
1209 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301210
1211 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001212 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301213 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001214 buf_len = be16_to_cpu(
1215 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001216 if (likely(buf_len >= resp_len)) {
1217 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1218 } else {
1219 dev_warn(hba->dev,
1220 "%s: Response size is bigger than buffer",
1221 __func__);
1222 return -EINVAL;
1223 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301224 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001225
1226 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301227}
1228
1229/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301230 * ufshcd_hba_capabilities - Read controller capabilities
1231 * @hba: per adapter instance
1232 */
1233static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
1234{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301235 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301236
1237 /* nutrs and nutmrs are 0 based values */
1238 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
1239 hba->nutmrs =
1240 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
1241}
1242
1243/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301244 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1245 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301246 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301247 * Return true on success, else false
1248 */
1249static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
1250{
1251 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1252 return true;
1253 else
1254 return false;
1255}
1256
1257/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301258 * ufshcd_get_upmcrs - Get the power mode change request status
1259 * @hba: Pointer to adapter instance
1260 *
1261 * This function gets the UPMCRS field of HCS register
1262 * Returns value of UPMCRS field
1263 */
1264static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1265{
1266 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1267}
1268
1269/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301270 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1271 * @hba: per adapter instance
1272 * @uic_cmd: UIC command
1273 *
1274 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301275 */
1276static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301277ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301278{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301279 WARN_ON(hba->active_uic_cmd);
1280
1281 hba->active_uic_cmd = uic_cmd;
1282
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301283 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301284 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1285 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1286 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301287
1288 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301289 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301290 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301291}
1292
1293/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301294 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1295 * @hba: per adapter instance
1296 * @uic_command: UIC command
1297 *
1298 * Must be called with mutex held.
1299 * Returns 0 only if success.
1300 */
1301static int
1302ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1303{
1304 int ret;
1305 unsigned long flags;
1306
1307 if (wait_for_completion_timeout(&uic_cmd->done,
1308 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1309 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1310 else
1311 ret = -ETIMEDOUT;
1312
1313 spin_lock_irqsave(hba->host->host_lock, flags);
1314 hba->active_uic_cmd = NULL;
1315 spin_unlock_irqrestore(hba->host->host_lock, flags);
1316
1317 return ret;
1318}
1319
1320/**
1321 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1322 * @hba: per adapter instance
1323 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001324 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301325 *
1326 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001327 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301328 * Returns 0 only if success.
1329 */
1330static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001331__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1332 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301333{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301334 if (!ufshcd_ready_for_uic_cmd(hba)) {
1335 dev_err(hba->dev,
1336 "Controller not ready to accept UIC commands\n");
1337 return -EIO;
1338 }
1339
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001340 if (completion)
1341 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301342
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301343 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301344
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001345 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301346}
1347
1348/**
1349 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1350 * @hba: per adapter instance
1351 * @uic_cmd: UIC command
1352 *
1353 * Returns 0 only if success.
1354 */
1355static int
1356ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1357{
1358 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001359 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301360
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001361 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301362 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001363 ufshcd_add_delay_before_dme_cmd(hba);
1364
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001365 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001366 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001367 spin_unlock_irqrestore(hba->host->host_lock, flags);
1368 if (!ret)
1369 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1370
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301371 mutex_unlock(&hba->uic_cmd_mutex);
1372
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001373 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301374 return ret;
1375}
1376
1377/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301378 * ufshcd_map_sg - Map scatter-gather list to prdt
1379 * @lrbp - pointer to local reference block
1380 *
1381 * Returns 0 in case of success, non-zero value in case of failure
1382 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001383static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301384{
1385 struct ufshcd_sg_entry *prd_table;
1386 struct scatterlist *sg;
1387 struct scsi_cmnd *cmd;
1388 int sg_segments;
1389 int i;
1390
1391 cmd = lrbp->cmd;
1392 sg_segments = scsi_dma_map(cmd);
1393 if (sg_segments < 0)
1394 return sg_segments;
1395
1396 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001397 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1398 lrbp->utr_descriptor_ptr->prd_table_length =
1399 cpu_to_le16((u16)(sg_segments *
1400 sizeof(struct ufshcd_sg_entry)));
1401 else
1402 lrbp->utr_descriptor_ptr->prd_table_length =
1403 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301404
1405 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1406
1407 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1408 prd_table[i].size =
1409 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1410 prd_table[i].base_addr =
1411 cpu_to_le32(lower_32_bits(sg->dma_address));
1412 prd_table[i].upper_addr =
1413 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001414 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301415 }
1416 } else {
1417 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1418 }
1419
1420 return 0;
1421}
1422
1423/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301424 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301425 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301426 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301427 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301428static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301429{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301430 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1431
1432 if (hba->ufs_version == UFSHCI_VERSION_10) {
1433 u32 rw;
1434 rw = set & INTERRUPT_MASK_RW_VER_10;
1435 set = rw | ((set ^ intrs) & intrs);
1436 } else {
1437 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301438 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301439
1440 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1441}
1442
1443/**
1444 * ufshcd_disable_intr - disable interrupts
1445 * @hba: per adapter instance
1446 * @intrs: interrupt bits
1447 */
1448static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1449{
1450 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1451
1452 if (hba->ufs_version == UFSHCI_VERSION_10) {
1453 u32 rw;
1454 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1455 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1456 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1457
1458 } else {
1459 set &= ~intrs;
1460 }
1461
1462 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301463}
1464
1465/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301466 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1467 * descriptor according to request
1468 * @lrbp: pointer to local reference block
1469 * @upiu_flags: flags required in the header
1470 * @cmd_dir: requests data direction
1471 */
1472static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001473 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301474{
1475 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1476 u32 data_direction;
1477 u32 dword_0;
1478
1479 if (cmd_dir == DMA_FROM_DEVICE) {
1480 data_direction = UTP_DEVICE_TO_HOST;
1481 *upiu_flags = UPIU_CMD_FLAGS_READ;
1482 } else if (cmd_dir == DMA_TO_DEVICE) {
1483 data_direction = UTP_HOST_TO_DEVICE;
1484 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1485 } else {
1486 data_direction = UTP_NO_DATA_TRANSFER;
1487 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1488 }
1489
1490 dword_0 = data_direction | (lrbp->command_type
1491 << UPIU_COMMAND_TYPE_OFFSET);
1492 if (lrbp->intr_cmd)
1493 dword_0 |= UTP_REQ_DESC_INT_CMD;
1494
1495 /* Transfer request descriptor header fields */
1496 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001497 /* dword_1 is reserved, hence it is set to 0 */
1498 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301499 /*
1500 * assigning invalid value for command status. Controller
1501 * updates OCS on command completion, with the command
1502 * status
1503 */
1504 req_desc->header.dword_2 =
1505 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001506 /* dword_3 is reserved, hence it is set to 0 */
1507 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001508
1509 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301510}
1511
1512/**
1513 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1514 * for scsi commands
1515 * @lrbp - local reference block pointer
1516 * @upiu_flags - flags
1517 */
1518static
1519void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1520{
1521 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001522 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301523
1524 /* command descriptor fields */
1525 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1526 UPIU_TRANSACTION_COMMAND, upiu_flags,
1527 lrbp->lun, lrbp->task_tag);
1528 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1529 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1530
1531 /* Total EHS length and Data segment length will be zero */
1532 ucd_req_ptr->header.dword_2 = 0;
1533
1534 ucd_req_ptr->sc.exp_data_transfer_len =
1535 cpu_to_be32(lrbp->cmd->sdb.length);
1536
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001537 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1538 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1539 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1540
1541 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301542}
1543
Dolev Raviv68078d52013-07-30 00:35:58 +05301544/**
1545 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1546 * for query requsts
1547 * @hba: UFS hba
1548 * @lrbp: local reference block pointer
1549 * @upiu_flags: flags
1550 */
1551static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1552 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1553{
1554 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1555 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301556 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301557 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1558
1559 /* Query request header */
1560 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1561 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1562 lrbp->lun, lrbp->task_tag);
1563 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1564 0, query->request.query_func, 0, 0);
1565
Zang Leigang68612852016-08-25 17:39:19 +08001566 /* Data segment length only need for WRITE_DESC */
1567 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1568 ucd_req_ptr->header.dword_2 =
1569 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1570 else
1571 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301572
1573 /* Copy the Query Request buffer as is */
1574 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1575 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301576
1577 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001578 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1579 memcpy(descp, query->descriptor, len);
1580
Yaniv Gardi51047262016-02-01 15:02:38 +02001581 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301582}
1583
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301584static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1585{
1586 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1587
1588 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1589
1590 /* command descriptor fields */
1591 ucd_req_ptr->header.dword_0 =
1592 UPIU_HEADER_DWORD(
1593 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001594 /* clear rest of the fields of basic header */
1595 ucd_req_ptr->header.dword_1 = 0;
1596 ucd_req_ptr->header.dword_2 = 0;
1597
1598 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301599}
1600
1601/**
Joao Pinto300bb132016-05-11 12:21:27 +01001602 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1603 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301604 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301605 * @lrb - pointer to local reference block
1606 */
Joao Pinto300bb132016-05-11 12:21:27 +01001607static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301608{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301609 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301610 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301611
Joao Pinto300bb132016-05-11 12:21:27 +01001612 if (hba->ufs_version == UFSHCI_VERSION_20)
1613 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1614 else
1615 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1616
1617 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1618 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1619 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1620 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1621 ufshcd_prepare_utp_nop_upiu(lrbp);
1622 else
1623 ret = -EINVAL;
1624
1625 return ret;
1626}
1627
1628/**
1629 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1630 * for SCSI Purposes
1631 * @hba - per adapter instance
1632 * @lrb - pointer to local reference block
1633 */
1634static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1635{
1636 u32 upiu_flags;
1637 int ret = 0;
1638
1639 if (hba->ufs_version == UFSHCI_VERSION_20)
1640 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1641 else
1642 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1643
1644 if (likely(lrbp->cmd)) {
1645 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1646 lrbp->cmd->sc_data_direction);
1647 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1648 } else {
1649 ret = -EINVAL;
1650 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301651
1652 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301653}
1654
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001655/*
1656 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1657 * @scsi_lun: scsi LUN id
1658 *
1659 * Returns UPIU LUN id
1660 */
1661static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1662{
1663 if (scsi_is_wlun(scsi_lun))
1664 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1665 | UFS_UPIU_WLUN_ID;
1666 else
1667 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1668}
1669
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301670/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001671 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1672 * @scsi_lun: UPIU W-LUN id
1673 *
1674 * Returns SCSI W-LUN id
1675 */
1676static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1677{
1678 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1679}
1680
1681/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301682 * ufshcd_queuecommand - main entry point for SCSI requests
1683 * @cmd: command from SCSI Midlayer
1684 * @done: call back function
1685 *
1686 * Returns 0 for success, non-zero in case of failure
1687 */
1688static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1689{
1690 struct ufshcd_lrb *lrbp;
1691 struct ufs_hba *hba;
1692 unsigned long flags;
1693 int tag;
1694 int err = 0;
1695
1696 hba = shost_priv(host);
1697
1698 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001699 if (!ufshcd_valid_tag(hba, tag)) {
1700 dev_err(hba->dev,
1701 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1702 __func__, tag, cmd, cmd->request);
1703 BUG();
1704 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301705
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301706 spin_lock_irqsave(hba->host->host_lock, flags);
1707 switch (hba->ufshcd_state) {
1708 case UFSHCD_STATE_OPERATIONAL:
1709 break;
Zang Leigang141f8162016-11-16 11:29:37 +08001710 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301711 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301712 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301713 goto out_unlock;
1714 case UFSHCD_STATE_ERROR:
1715 set_host_byte(cmd, DID_ERROR);
1716 cmd->scsi_done(cmd);
1717 goto out_unlock;
1718 default:
1719 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1720 __func__, hba->ufshcd_state);
1721 set_host_byte(cmd, DID_BAD_TARGET);
1722 cmd->scsi_done(cmd);
1723 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301724 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001725
1726 /* if error handling is in progress, don't issue commands */
1727 if (ufshcd_eh_in_progress(hba)) {
1728 set_host_byte(cmd, DID_ERROR);
1729 cmd->scsi_done(cmd);
1730 goto out_unlock;
1731 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301732 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301733
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301734 /* acquire the tag to make sure device cmds don't use it */
1735 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1736 /*
1737 * Dev manage command in progress, requeue the command.
1738 * Requeuing the command helps in cases where the request *may*
1739 * find different tag instead of waiting for dev manage command
1740 * completion.
1741 */
1742 err = SCSI_MLQUEUE_HOST_BUSY;
1743 goto out;
1744 }
1745
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001746 err = ufshcd_hold(hba, true);
1747 if (err) {
1748 err = SCSI_MLQUEUE_HOST_BUSY;
1749 clear_bit_unlock(tag, &hba->lrb_in_use);
1750 goto out;
1751 }
1752 WARN_ON(hba->clk_gating.state != CLKS_ON);
1753
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301754 lrbp = &hba->lrb[tag];
1755
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301756 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301757 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001758 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301759 lrbp->sense_buffer = cmd->sense_buffer;
1760 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001761 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001762 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301763
Joao Pinto300bb132016-05-11 12:21:27 +01001764 ufshcd_comp_scsi_upiu(hba, lrbp);
1765
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001766 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301767 if (err) {
1768 lrbp->cmd = NULL;
1769 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301770 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301771 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001772 /* Make sure descriptors are ready before ringing the doorbell */
1773 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301774
1775 /* issue command to the controller */
1776 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001777 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301778 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301779out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301780 spin_unlock_irqrestore(hba->host->host_lock, flags);
1781out:
1782 return err;
1783}
1784
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301785static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1786 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1787{
1788 lrbp->cmd = NULL;
1789 lrbp->sense_bufflen = 0;
1790 lrbp->sense_buffer = NULL;
1791 lrbp->task_tag = tag;
1792 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301793 lrbp->intr_cmd = true; /* No interrupt aggregation */
1794 hba->dev_cmd.type = cmd_type;
1795
Joao Pinto300bb132016-05-11 12:21:27 +01001796 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301797}
1798
1799static int
1800ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1801{
1802 int err = 0;
1803 unsigned long flags;
1804 u32 mask = 1 << tag;
1805
1806 /* clear outstanding transaction before retry */
1807 spin_lock_irqsave(hba->host->host_lock, flags);
1808 ufshcd_utrl_clear(hba, tag);
1809 spin_unlock_irqrestore(hba->host->host_lock, flags);
1810
1811 /*
1812 * wait for for h/w to clear corresponding bit in door-bell.
1813 * max. wait is 1 sec.
1814 */
1815 err = ufshcd_wait_for_register(hba,
1816 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001817 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301818
1819 return err;
1820}
1821
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001822static int
1823ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1824{
1825 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1826
1827 /* Get the UPIU response */
1828 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1829 UPIU_RSP_CODE_OFFSET;
1830 return query_res->response;
1831}
1832
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301833/**
1834 * ufshcd_dev_cmd_completion() - handles device management command responses
1835 * @hba: per adapter instance
1836 * @lrbp: pointer to local reference block
1837 */
1838static int
1839ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1840{
1841 int resp;
1842 int err = 0;
1843
1844 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1845
1846 switch (resp) {
1847 case UPIU_TRANSACTION_NOP_IN:
1848 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1849 err = -EINVAL;
1850 dev_err(hba->dev, "%s: unexpected response %x\n",
1851 __func__, resp);
1852 }
1853 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301854 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001855 err = ufshcd_check_query_response(hba, lrbp);
1856 if (!err)
1857 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301858 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301859 case UPIU_TRANSACTION_REJECT_UPIU:
1860 /* TODO: handle Reject UPIU Response */
1861 err = -EPERM;
1862 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1863 __func__);
1864 break;
1865 default:
1866 err = -EINVAL;
1867 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1868 __func__, resp);
1869 break;
1870 }
1871
1872 return err;
1873}
1874
1875static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1876 struct ufshcd_lrb *lrbp, int max_timeout)
1877{
1878 int err = 0;
1879 unsigned long time_left;
1880 unsigned long flags;
1881
1882 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1883 msecs_to_jiffies(max_timeout));
1884
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001885 /* Make sure descriptors are ready before ringing the doorbell */
1886 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301887 spin_lock_irqsave(hba->host->host_lock, flags);
1888 hba->dev_cmd.complete = NULL;
1889 if (likely(time_left)) {
1890 err = ufshcd_get_tr_ocs(lrbp);
1891 if (!err)
1892 err = ufshcd_dev_cmd_completion(hba, lrbp);
1893 }
1894 spin_unlock_irqrestore(hba->host->host_lock, flags);
1895
1896 if (!time_left) {
1897 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001898 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1899 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301900 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001901 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301902 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001903 /*
1904 * in case of an error, after clearing the doorbell,
1905 * we also need to clear the outstanding_request
1906 * field in hba
1907 */
1908 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301909 }
1910
1911 return err;
1912}
1913
1914/**
1915 * ufshcd_get_dev_cmd_tag - Get device management command tag
1916 * @hba: per-adapter instance
1917 * @tag: pointer to variable with available slot value
1918 *
1919 * Get a free slot and lock it until device management command
1920 * completes.
1921 *
1922 * Returns false if free slot is unavailable for locking, else
1923 * return true with tag value in @tag.
1924 */
1925static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1926{
1927 int tag;
1928 bool ret = false;
1929 unsigned long tmp;
1930
1931 if (!tag_out)
1932 goto out;
1933
1934 do {
1935 tmp = ~hba->lrb_in_use;
1936 tag = find_last_bit(&tmp, hba->nutrs);
1937 if (tag >= hba->nutrs)
1938 goto out;
1939 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1940
1941 *tag_out = tag;
1942 ret = true;
1943out:
1944 return ret;
1945}
1946
1947static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1948{
1949 clear_bit_unlock(tag, &hba->lrb_in_use);
1950}
1951
1952/**
1953 * ufshcd_exec_dev_cmd - API for sending device management requests
1954 * @hba - UFS hba
1955 * @cmd_type - specifies the type (NOP, Query...)
1956 * @timeout - time in seconds
1957 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301958 * NOTE: Since there is only one available tag for device management commands,
1959 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301960 */
1961static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1962 enum dev_cmd_type cmd_type, int timeout)
1963{
1964 struct ufshcd_lrb *lrbp;
1965 int err;
1966 int tag;
1967 struct completion wait;
1968 unsigned long flags;
1969
1970 /*
1971 * Get free slot, sleep if slots are unavailable.
1972 * Even though we use wait_event() which sleeps indefinitely,
1973 * the maximum wait time is bounded by SCSI request timeout.
1974 */
1975 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1976
1977 init_completion(&wait);
1978 lrbp = &hba->lrb[tag];
1979 WARN_ON(lrbp->cmd);
1980 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1981 if (unlikely(err))
1982 goto out_put_tag;
1983
1984 hba->dev_cmd.complete = &wait;
1985
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001986 /* Make sure descriptors are ready before ringing the doorbell */
1987 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301988 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001989 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301990 ufshcd_send_command(hba, tag);
1991 spin_unlock_irqrestore(hba->host->host_lock, flags);
1992
1993 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1994
1995out_put_tag:
1996 ufshcd_put_dev_cmd_tag(hba, tag);
1997 wake_up(&hba->dev_cmd.tag_wq);
1998 return err;
1999}
2000
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302001/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002002 * ufshcd_init_query() - init the query response and request parameters
2003 * @hba: per-adapter instance
2004 * @request: address of the request pointer to be initialized
2005 * @response: address of the response pointer to be initialized
2006 * @opcode: operation to perform
2007 * @idn: flag idn to access
2008 * @index: LU number to access
2009 * @selector: query/flag/descriptor further identification
2010 */
2011static inline void ufshcd_init_query(struct ufs_hba *hba,
2012 struct ufs_query_req **request, struct ufs_query_res **response,
2013 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
2014{
2015 *request = &hba->dev_cmd.query.request;
2016 *response = &hba->dev_cmd.query.response;
2017 memset(*request, 0, sizeof(struct ufs_query_req));
2018 memset(*response, 0, sizeof(struct ufs_query_res));
2019 (*request)->upiu_req.opcode = opcode;
2020 (*request)->upiu_req.idn = idn;
2021 (*request)->upiu_req.index = index;
2022 (*request)->upiu_req.selector = selector;
2023}
2024
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002025static int ufshcd_query_flag_retry(struct ufs_hba *hba,
2026 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
2027{
2028 int ret;
2029 int retries;
2030
2031 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
2032 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
2033 if (ret)
2034 dev_dbg(hba->dev,
2035 "%s: failed with error %d, retries %d\n",
2036 __func__, ret, retries);
2037 else
2038 break;
2039 }
2040
2041 if (ret)
2042 dev_err(hba->dev,
2043 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2044 __func__, opcode, idn, ret, retries);
2045 return ret;
2046}
2047
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002048/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302049 * ufshcd_query_flag() - API function for sending flag query requests
2050 * hba: per-adapter instance
2051 * query_opcode: flag query to perform
2052 * idn: flag idn to access
2053 * flag_res: the flag value after the query request completes
2054 *
2055 * Returns 0 for success, non-zero in case of failure
2056 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002057int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05302058 enum flag_idn idn, bool *flag_res)
2059{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002060 struct ufs_query_req *request = NULL;
2061 struct ufs_query_res *response = NULL;
2062 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002063 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05302064
2065 BUG_ON(!hba);
2066
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002067 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05302068 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002069 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2070 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05302071
2072 switch (opcode) {
2073 case UPIU_QUERY_OPCODE_SET_FLAG:
2074 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
2075 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
2076 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2077 break;
2078 case UPIU_QUERY_OPCODE_READ_FLAG:
2079 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2080 if (!flag_res) {
2081 /* No dummy reads */
2082 dev_err(hba->dev, "%s: Invalid argument for read request\n",
2083 __func__);
2084 err = -EINVAL;
2085 goto out_unlock;
2086 }
2087 break;
2088 default:
2089 dev_err(hba->dev,
2090 "%s: Expected query flag opcode but got = %d\n",
2091 __func__, opcode);
2092 err = -EINVAL;
2093 goto out_unlock;
2094 }
Dolev Raviv68078d52013-07-30 00:35:58 +05302095
Yaniv Gardie5ad4062016-02-01 15:02:41 +02002096 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05302097
2098 if (err) {
2099 dev_err(hba->dev,
2100 "%s: Sending flag query for idn %d failed, err = %d\n",
2101 __func__, idn, err);
2102 goto out_unlock;
2103 }
2104
2105 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302106 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05302107 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
2108
2109out_unlock:
2110 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002111 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05302112 return err;
2113}
2114
2115/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302116 * ufshcd_query_attr - API function for sending attribute requests
2117 * hba: per-adapter instance
2118 * opcode: attribute opcode
2119 * idn: attribute idn to access
2120 * index: index field
2121 * selector: selector field
2122 * attr_val: the attribute value after the query request completes
2123 *
2124 * Returns 0 for success, non-zero in case of failure
2125*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302126static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302127 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
2128{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002129 struct ufs_query_req *request = NULL;
2130 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302131 int err;
2132
2133 BUG_ON(!hba);
2134
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002135 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302136 if (!attr_val) {
2137 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2138 __func__, opcode);
2139 err = -EINVAL;
2140 goto out;
2141 }
2142
2143 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002144 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2145 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302146
2147 switch (opcode) {
2148 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2149 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302150 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302151 break;
2152 case UPIU_QUERY_OPCODE_READ_ATTR:
2153 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2154 break;
2155 default:
2156 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2157 __func__, opcode);
2158 err = -EINVAL;
2159 goto out_unlock;
2160 }
2161
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002162 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302163
2164 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002165 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2166 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302167 goto out_unlock;
2168 }
2169
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302170 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302171
2172out_unlock:
2173 mutex_unlock(&hba->dev_cmd.lock);
2174out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002175 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302176 return err;
2177}
2178
2179/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002180 * ufshcd_query_attr_retry() - API function for sending query
2181 * attribute with retries
2182 * @hba: per-adapter instance
2183 * @opcode: attribute opcode
2184 * @idn: attribute idn to access
2185 * @index: index field
2186 * @selector: selector field
2187 * @attr_val: the attribute value after the query request
2188 * completes
2189 *
2190 * Returns 0 for success, non-zero in case of failure
2191*/
2192static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2193 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2194 u32 *attr_val)
2195{
2196 int ret = 0;
2197 u32 retries;
2198
2199 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2200 ret = ufshcd_query_attr(hba, opcode, idn, index,
2201 selector, attr_val);
2202 if (ret)
2203 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2204 __func__, ret, retries);
2205 else
2206 break;
2207 }
2208
2209 if (ret)
2210 dev_err(hba->dev,
2211 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2212 __func__, idn, ret, QUERY_REQ_RETRIES);
2213 return ret;
2214}
2215
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002216static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002217 enum query_opcode opcode, enum desc_idn idn, u8 index,
2218 u8 selector, u8 *desc_buf, int *buf_len)
2219{
2220 struct ufs_query_req *request = NULL;
2221 struct ufs_query_res *response = NULL;
2222 int err;
2223
2224 BUG_ON(!hba);
2225
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002226 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002227 if (!desc_buf) {
2228 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2229 __func__, opcode);
2230 err = -EINVAL;
2231 goto out;
2232 }
2233
2234 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
2235 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2236 __func__, *buf_len);
2237 err = -EINVAL;
2238 goto out;
2239 }
2240
2241 mutex_lock(&hba->dev_cmd.lock);
2242 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2243 selector);
2244 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002245 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002246
2247 switch (opcode) {
2248 case UPIU_QUERY_OPCODE_WRITE_DESC:
2249 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2250 break;
2251 case UPIU_QUERY_OPCODE_READ_DESC:
2252 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2253 break;
2254 default:
2255 dev_err(hba->dev,
2256 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2257 __func__, opcode);
2258 err = -EINVAL;
2259 goto out_unlock;
2260 }
2261
2262 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2263
2264 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002265 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2266 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002267 goto out_unlock;
2268 }
2269
2270 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002271 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002272
2273out_unlock:
2274 mutex_unlock(&hba->dev_cmd.lock);
2275out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002276 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002277 return err;
2278}
2279
2280/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002281 * ufshcd_query_descriptor_retry - API function for sending descriptor
2282 * requests
2283 * hba: per-adapter instance
2284 * opcode: attribute opcode
2285 * idn: attribute idn to access
2286 * index: index field
2287 * selector: selector field
2288 * desc_buf: the buffer that contains the descriptor
2289 * buf_len: length parameter passed to the device
2290 *
2291 * Returns 0 for success, non-zero in case of failure.
2292 * The buf_len parameter will contain, on return, the length parameter
2293 * received on the response.
2294 */
2295int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2296 enum query_opcode opcode, enum desc_idn idn, u8 index,
2297 u8 selector, u8 *desc_buf, int *buf_len)
2298{
2299 int err;
2300 int retries;
2301
2302 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2303 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2304 selector, desc_buf, buf_len);
2305 if (!err || err == -EINVAL)
2306 break;
2307 }
2308
2309 return err;
2310}
2311EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2312
2313/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002314 * ufshcd_read_desc_param - read the specified descriptor parameter
2315 * @hba: Pointer to adapter instance
2316 * @desc_id: descriptor idn value
2317 * @desc_index: descriptor index
2318 * @param_offset: offset of the parameter to read
2319 * @param_read_buf: pointer to buffer where parameter would be read
2320 * @param_size: sizeof(param_read_buf)
2321 *
2322 * Return 0 in case of success, non-zero otherwise
2323 */
2324static int ufshcd_read_desc_param(struct ufs_hba *hba,
2325 enum desc_idn desc_id,
2326 int desc_index,
2327 u32 param_offset,
2328 u8 *param_read_buf,
2329 u32 param_size)
2330{
2331 int ret;
2332 u8 *desc_buf;
2333 u32 buff_len;
2334 bool is_kmalloc = true;
2335
2336 /* safety checks */
2337 if (desc_id >= QUERY_DESC_IDN_MAX)
2338 return -EINVAL;
2339
2340 buff_len = ufs_query_desc_max_size[desc_id];
2341 if ((param_offset + param_size) > buff_len)
2342 return -EINVAL;
2343
2344 if (!param_offset && (param_size == buff_len)) {
2345 /* memory space already available to hold full descriptor */
2346 desc_buf = param_read_buf;
2347 is_kmalloc = false;
2348 } else {
2349 /* allocate memory to hold full descriptor */
2350 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2351 if (!desc_buf)
2352 return -ENOMEM;
2353 }
2354
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002355 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2356 desc_id, desc_index, 0, desc_buf,
2357 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002358
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002359 if (ret) {
2360 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2361 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002362
2363 goto out;
2364 }
2365
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002366 /* Sanity check */
2367 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2368 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2369 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2370 ret = -EINVAL;
2371 goto out;
2372 }
2373
2374 /*
2375 * While reading variable size descriptors (like string descriptor),
2376 * some UFS devices may report the "LENGTH" (field in "Transaction
2377 * Specific fields" of Query Response UPIU) same as what was requested
2378 * in Query Request UPIU instead of reporting the actual size of the
2379 * variable size descriptor.
2380 * Although it's safe to ignore the "LENGTH" field for variable size
2381 * descriptors as we can always derive the length of the descriptor from
2382 * the descriptor header fields. Hence this change impose the length
2383 * match check only for fixed size descriptors (for which we always
2384 * request the correct size as part of Query Request UPIU).
2385 */
2386 if ((desc_id != QUERY_DESC_IDN_STRING) &&
2387 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
2388 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
2389 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
2390 ret = -EINVAL;
2391 goto out;
2392 }
2393
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002394 if (is_kmalloc)
2395 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2396out:
2397 if (is_kmalloc)
2398 kfree(desc_buf);
2399 return ret;
2400}
2401
2402static inline int ufshcd_read_desc(struct ufs_hba *hba,
2403 enum desc_idn desc_id,
2404 int desc_index,
2405 u8 *buf,
2406 u32 size)
2407{
2408 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2409}
2410
2411static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2412 u8 *buf,
2413 u32 size)
2414{
Dolev Raviv61e07352016-11-23 16:30:49 -08002415 int err = 0;
2416 int retries;
2417
2418 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2419 /* Read descriptor*/
2420 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2421 if (!err)
2422 break;
2423 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2424 }
2425
2426 return err;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002427}
2428
Yaniv Gardib573d482016-03-10 17:37:09 +02002429int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2430{
2431 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2432}
2433EXPORT_SYMBOL(ufshcd_read_device_desc);
2434
2435/**
2436 * ufshcd_read_string_desc - read string descriptor
2437 * @hba: pointer to adapter instance
2438 * @desc_index: descriptor index
2439 * @buf: pointer to buffer where descriptor would be read
2440 * @size: size of buf
2441 * @ascii: if true convert from unicode to ascii characters
2442 *
2443 * Return 0 in case of success, non-zero otherwise
2444 */
2445int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2446 u32 size, bool ascii)
2447{
2448 int err = 0;
2449
2450 err = ufshcd_read_desc(hba,
2451 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2452
2453 if (err) {
2454 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2455 __func__, QUERY_REQ_RETRIES, err);
2456 goto out;
2457 }
2458
2459 if (ascii) {
2460 int desc_len;
2461 int ascii_len;
2462 int i;
2463 char *buff_ascii;
2464
2465 desc_len = buf[0];
2466 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2467 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2468 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2469 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2470 __func__);
2471 err = -ENOMEM;
2472 goto out;
2473 }
2474
2475 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2476 if (!buff_ascii) {
2477 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002478 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002479 }
2480
2481 /*
2482 * the descriptor contains string in UTF16 format
2483 * we need to convert to utf-8 so it can be displayed
2484 */
2485 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2486 desc_len - QUERY_DESC_HDR_SIZE,
2487 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2488
2489 /* replace non-printable or non-ASCII characters with spaces */
2490 for (i = 0; i < ascii_len; i++)
2491 ufshcd_remove_non_printable(&buff_ascii[i]);
2492
2493 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2494 size - QUERY_DESC_HDR_SIZE);
2495 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2496 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002497 kfree(buff_ascii);
2498 }
2499out:
2500 return err;
2501}
2502EXPORT_SYMBOL(ufshcd_read_string_desc);
2503
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002504/**
2505 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2506 * @hba: Pointer to adapter instance
2507 * @lun: lun id
2508 * @param_offset: offset of the parameter to read
2509 * @param_read_buf: pointer to buffer where parameter would be read
2510 * @param_size: sizeof(param_read_buf)
2511 *
2512 * Return 0 in case of success, non-zero otherwise
2513 */
2514static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2515 int lun,
2516 enum unit_desc_param param_offset,
2517 u8 *param_read_buf,
2518 u32 param_size)
2519{
2520 /*
2521 * Unit descriptors are only available for general purpose LUs (LUN id
2522 * from 0 to 7) and RPMB Well known LU.
2523 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002524 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002525 return -EOPNOTSUPP;
2526
2527 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2528 param_offset, param_read_buf, param_size);
2529}
2530
2531/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302532 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2533 * @hba: per adapter instance
2534 *
2535 * 1. Allocate DMA memory for Command Descriptor array
2536 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2537 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2538 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2539 * (UTMRDL)
2540 * 4. Allocate memory for local reference block(lrb).
2541 *
2542 * Returns 0 for success, non-zero in case of failure
2543 */
2544static int ufshcd_memory_alloc(struct ufs_hba *hba)
2545{
2546 size_t utmrdl_size, utrdl_size, ucdl_size;
2547
2548 /* Allocate memory for UTP command descriptors */
2549 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002550 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2551 ucdl_size,
2552 &hba->ucdl_dma_addr,
2553 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302554
2555 /*
2556 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2557 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2558 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2559 * be aligned to 128 bytes as well
2560 */
2561 if (!hba->ucdl_base_addr ||
2562 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302563 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302564 "Command Descriptor Memory allocation failed\n");
2565 goto out;
2566 }
2567
2568 /*
2569 * Allocate memory for UTP Transfer descriptors
2570 * UFSHCI requires 1024 byte alignment of UTRD
2571 */
2572 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002573 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2574 utrdl_size,
2575 &hba->utrdl_dma_addr,
2576 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302577 if (!hba->utrdl_base_addr ||
2578 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302579 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302580 "Transfer Descriptor Memory allocation failed\n");
2581 goto out;
2582 }
2583
2584 /*
2585 * Allocate memory for UTP Task Management descriptors
2586 * UFSHCI requires 1024 byte alignment of UTMRD
2587 */
2588 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002589 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2590 utmrdl_size,
2591 &hba->utmrdl_dma_addr,
2592 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302593 if (!hba->utmrdl_base_addr ||
2594 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302595 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302596 "Task Management Descriptor Memory allocation failed\n");
2597 goto out;
2598 }
2599
2600 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002601 hba->lrb = devm_kzalloc(hba->dev,
2602 hba->nutrs * sizeof(struct ufshcd_lrb),
2603 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302604 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302605 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302606 goto out;
2607 }
2608 return 0;
2609out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302610 return -ENOMEM;
2611}
2612
2613/**
Dolev Raviv66cc8202016-12-22 18:39:42 -08002614 * ufshcd_print_pwr_info - print power params as saved in hba
2615 * power info
2616 * @hba: per-adapter instance
2617 */
2618static void ufshcd_print_pwr_info(struct ufs_hba *hba)
2619{
2620 static const char * const names[] = {
2621 "INVALID MODE",
2622 "FAST MODE",
2623 "SLOW_MODE",
2624 "INVALID MODE",
2625 "FASTAUTO_MODE",
2626 "SLOWAUTO_MODE",
2627 "INVALID MODE",
2628 };
2629
2630 dev_info(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
2631 __func__,
2632 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
2633 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
2634 names[hba->pwr_info.pwr_rx],
2635 names[hba->pwr_info.pwr_tx],
2636 hba->pwr_info.hs_rate);
2637}
2638
2639/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302640 * ufshcd_host_memory_configure - configure local reference block with
2641 * memory offsets
2642 * @hba: per adapter instance
2643 *
2644 * Configure Host memory space
2645 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2646 * address.
2647 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2648 * and PRDT offset.
2649 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2650 * into local reference block.
2651 */
2652static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2653{
2654 struct utp_transfer_cmd_desc *cmd_descp;
2655 struct utp_transfer_req_desc *utrdlp;
2656 dma_addr_t cmd_desc_dma_addr;
2657 dma_addr_t cmd_desc_element_addr;
2658 u16 response_offset;
2659 u16 prdt_offset;
2660 int cmd_desc_size;
2661 int i;
2662
2663 utrdlp = hba->utrdl_base_addr;
2664 cmd_descp = hba->ucdl_base_addr;
2665
2666 response_offset =
2667 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2668 prdt_offset =
2669 offsetof(struct utp_transfer_cmd_desc, prd_table);
2670
2671 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2672 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2673
2674 for (i = 0; i < hba->nutrs; i++) {
2675 /* Configure UTRD with command descriptor base address */
2676 cmd_desc_element_addr =
2677 (cmd_desc_dma_addr + (cmd_desc_size * i));
2678 utrdlp[i].command_desc_base_addr_lo =
2679 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2680 utrdlp[i].command_desc_base_addr_hi =
2681 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2682
2683 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002684 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2685 utrdlp[i].response_upiu_offset =
2686 cpu_to_le16(response_offset);
2687 utrdlp[i].prd_table_offset =
2688 cpu_to_le16(prdt_offset);
2689 utrdlp[i].response_upiu_length =
2690 cpu_to_le16(ALIGNED_UPIU_SIZE);
2691 } else {
2692 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302693 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002694 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302695 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002696 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302697 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002698 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302699
2700 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302701 hba->lrb[i].ucd_req_ptr =
2702 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302703 hba->lrb[i].ucd_rsp_ptr =
2704 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2705 hba->lrb[i].ucd_prdt_ptr =
2706 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2707 }
2708}
2709
2710/**
2711 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2712 * @hba: per adapter instance
2713 *
2714 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2715 * in order to initialize the Unipro link startup procedure.
2716 * Once the Unipro links are up, the device connected to the controller
2717 * is detected.
2718 *
2719 * Returns 0 on success, non-zero value on failure
2720 */
2721static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2722{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302723 struct uic_command uic_cmd = {0};
2724 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302725
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302726 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2727
2728 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2729 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302730 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302731 "dme-link-startup: error code %d\n", ret);
2732 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302733}
2734
Yaniv Gardicad2e032015-03-31 17:37:14 +03002735static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2736{
2737 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2738 unsigned long min_sleep_time_us;
2739
2740 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2741 return;
2742
2743 /*
2744 * last_dme_cmd_tstamp will be 0 only for 1st call to
2745 * this function
2746 */
2747 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2748 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2749 } else {
2750 unsigned long delta =
2751 (unsigned long) ktime_to_us(
2752 ktime_sub(ktime_get(),
2753 hba->last_dme_cmd_tstamp));
2754
2755 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2756 min_sleep_time_us =
2757 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2758 else
2759 return; /* no more delay required */
2760 }
2761
2762 /* allow sleep for extra 50us if needed */
2763 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2764}
2765
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302766/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302767 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2768 * @hba: per adapter instance
2769 * @attr_sel: uic command argument1
2770 * @attr_set: attribute set type as uic command argument2
2771 * @mib_val: setting value as uic command argument3
2772 * @peer: indicate whether peer or local
2773 *
2774 * Returns 0 on success, non-zero value on failure
2775 */
2776int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2777 u8 attr_set, u32 mib_val, u8 peer)
2778{
2779 struct uic_command uic_cmd = {0};
2780 static const char *const action[] = {
2781 "dme-set",
2782 "dme-peer-set"
2783 };
2784 const char *set = action[!!peer];
2785 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002786 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302787
2788 uic_cmd.command = peer ?
2789 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2790 uic_cmd.argument1 = attr_sel;
2791 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2792 uic_cmd.argument3 = mib_val;
2793
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002794 do {
2795 /* for peer attributes we retry upon failure */
2796 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2797 if (ret)
2798 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2799 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2800 } while (ret && peer && --retries);
2801
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002802 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002803 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002804 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2805 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302806
2807 return ret;
2808}
2809EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2810
2811/**
2812 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2813 * @hba: per adapter instance
2814 * @attr_sel: uic command argument1
2815 * @mib_val: the value of the attribute as returned by the UIC command
2816 * @peer: indicate whether peer or local
2817 *
2818 * Returns 0 on success, non-zero value on failure
2819 */
2820int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2821 u32 *mib_val, u8 peer)
2822{
2823 struct uic_command uic_cmd = {0};
2824 static const char *const action[] = {
2825 "dme-get",
2826 "dme-peer-get"
2827 };
2828 const char *get = action[!!peer];
2829 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002830 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002831 struct ufs_pa_layer_attr orig_pwr_info;
2832 struct ufs_pa_layer_attr temp_pwr_info;
2833 bool pwr_mode_change = false;
2834
2835 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2836 orig_pwr_info = hba->pwr_info;
2837 temp_pwr_info = orig_pwr_info;
2838
2839 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2840 orig_pwr_info.pwr_rx == FAST_MODE) {
2841 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2842 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2843 pwr_mode_change = true;
2844 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2845 orig_pwr_info.pwr_rx == SLOW_MODE) {
2846 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2847 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2848 pwr_mode_change = true;
2849 }
2850 if (pwr_mode_change) {
2851 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2852 if (ret)
2853 goto out;
2854 }
2855 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302856
2857 uic_cmd.command = peer ?
2858 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2859 uic_cmd.argument1 = attr_sel;
2860
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002861 do {
2862 /* for peer attributes we retry upon failure */
2863 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2864 if (ret)
2865 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2866 get, UIC_GET_ATTR_ID(attr_sel), ret);
2867 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302868
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002869 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002870 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002871 get, UIC_GET_ATTR_ID(attr_sel),
2872 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002873
2874 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302875 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002876
2877 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2878 && pwr_mode_change)
2879 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302880out:
2881 return ret;
2882}
2883EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2884
2885/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002886 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2887 * state) and waits for it to take effect.
2888 *
2889 * @hba: per adapter instance
2890 * @cmd: UIC command to execute
2891 *
2892 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2893 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2894 * and device UniPro link and hence it's final completion would be indicated by
2895 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2896 * addition to normal UIC command completion Status (UCCS). This function only
2897 * returns after the relevant status bits indicate the completion.
2898 *
2899 * Returns 0 on success, non-zero value on failure
2900 */
2901static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2902{
2903 struct completion uic_async_done;
2904 unsigned long flags;
2905 u8 status;
2906 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002907 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002908
2909 mutex_lock(&hba->uic_cmd_mutex);
2910 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002911 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002912
2913 spin_lock_irqsave(hba->host->host_lock, flags);
2914 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002915 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2916 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2917 /*
2918 * Make sure UIC command completion interrupt is disabled before
2919 * issuing UIC command.
2920 */
2921 wmb();
2922 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002923 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002924 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2925 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002926 if (ret) {
2927 dev_err(hba->dev,
2928 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2929 cmd->command, cmd->argument3, ret);
2930 goto out;
2931 }
2932
2933 if (!wait_for_completion_timeout(hba->uic_async_done,
2934 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2935 dev_err(hba->dev,
2936 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2937 cmd->command, cmd->argument3);
2938 ret = -ETIMEDOUT;
2939 goto out;
2940 }
2941
2942 status = ufshcd_get_upmcrs(hba);
2943 if (status != PWR_LOCAL) {
2944 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002945 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002946 cmd->command, status);
2947 ret = (status != PWR_OK) ? status : -1;
2948 }
2949out:
2950 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002951 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002952 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002953 if (reenable_intr)
2954 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002955 spin_unlock_irqrestore(hba->host->host_lock, flags);
2956 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002957
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002958 return ret;
2959}
2960
2961/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302962 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2963 * using DME_SET primitives.
2964 * @hba: per adapter instance
2965 * @mode: powr mode value
2966 *
2967 * Returns 0 on success, non-zero value on failure
2968 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302969static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302970{
2971 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002972 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302973
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002974 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2975 ret = ufshcd_dme_set(hba,
2976 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2977 if (ret) {
2978 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2979 __func__, ret);
2980 goto out;
2981 }
2982 }
2983
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302984 uic_cmd.command = UIC_CMD_DME_SET;
2985 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2986 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002987 ufshcd_hold(hba, false);
2988 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2989 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302990
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002991out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002992 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002993}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302994
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002995static int ufshcd_link_recovery(struct ufs_hba *hba)
2996{
2997 int ret;
2998 unsigned long flags;
2999
3000 spin_lock_irqsave(hba->host->host_lock, flags);
3001 hba->ufshcd_state = UFSHCD_STATE_RESET;
3002 ufshcd_set_eh_in_progress(hba);
3003 spin_unlock_irqrestore(hba->host->host_lock, flags);
3004
3005 ret = ufshcd_host_reset_and_restore(hba);
3006
3007 spin_lock_irqsave(hba->host->host_lock, flags);
3008 if (ret)
3009 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3010 ufshcd_clear_eh_in_progress(hba);
3011 spin_unlock_irqrestore(hba->host->host_lock, flags);
3012
3013 if (ret)
3014 dev_err(hba->dev, "%s: link recovery failed, err %d",
3015 __func__, ret);
3016
3017 return ret;
3018}
3019
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003020static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003021{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003022 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003023 struct uic_command uic_cmd = {0};
3024
Kiwoong Kimee32c902016-11-10 21:17:43 +09003025 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
3026
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003027 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003028 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003029
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003030 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003031 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
3032 __func__, ret);
3033
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003034 /*
3035 * If link recovery fails then return error so that caller
3036 * don't retry the hibern8 enter again.
3037 */
3038 if (ufshcd_link_recovery(hba))
3039 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09003040 } else
3041 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
3042 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003043
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02003044 return ret;
3045}
3046
3047static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
3048{
3049 int ret = 0, retries;
3050
3051 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
3052 ret = __ufshcd_uic_hibern8_enter(hba);
3053 if (!ret || ret == -ENOLINK)
3054 goto out;
3055 }
3056out:
3057 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003058}
3059
3060static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
3061{
3062 struct uic_command uic_cmd = {0};
3063 int ret;
3064
Kiwoong Kimee32c902016-11-10 21:17:43 +09003065 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
3066
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003067 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
3068 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303069 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02003070 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
3071 __func__, ret);
3072 ret = ufshcd_link_recovery(hba);
Kiwoong Kimee32c902016-11-10 21:17:43 +09003073 } else
3074 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
3075 POST_CHANGE);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303076
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303077 return ret;
3078}
3079
Yaniv Gardi50646362014-10-23 13:25:13 +03003080 /**
3081 * ufshcd_init_pwr_info - setting the POR (power on reset)
3082 * values in hba power info
3083 * @hba: per-adapter instance
3084 */
3085static void ufshcd_init_pwr_info(struct ufs_hba *hba)
3086{
3087 hba->pwr_info.gear_rx = UFS_PWM_G1;
3088 hba->pwr_info.gear_tx = UFS_PWM_G1;
3089 hba->pwr_info.lane_rx = 1;
3090 hba->pwr_info.lane_tx = 1;
3091 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
3092 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
3093 hba->pwr_info.hs_rate = 0;
3094}
3095
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303096/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003097 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3098 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303099 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003100static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303101{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003102 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
3103
3104 if (hba->max_pwr_info.is_valid)
3105 return 0;
3106
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003107 pwr_info->pwr_tx = FAST_MODE;
3108 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003109 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303110
3111 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003112 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
3113 &pwr_info->lane_rx);
3114 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3115 &pwr_info->lane_tx);
3116
3117 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
3118 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3119 __func__,
3120 pwr_info->lane_rx,
3121 pwr_info->lane_tx);
3122 return -EINVAL;
3123 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303124
3125 /*
3126 * First, get the maximum gears of HS speed.
3127 * If a zero value, it means there is no HSGEAR capability.
3128 * Then, get the maximum gears of PWM speed.
3129 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003130 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3131 if (!pwr_info->gear_rx) {
3132 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3133 &pwr_info->gear_rx);
3134 if (!pwr_info->gear_rx) {
3135 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3136 __func__, pwr_info->gear_rx);
3137 return -EINVAL;
3138 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003139 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303140 }
3141
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003142 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3143 &pwr_info->gear_tx);
3144 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303145 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003146 &pwr_info->gear_tx);
3147 if (!pwr_info->gear_tx) {
3148 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3149 __func__, pwr_info->gear_tx);
3150 return -EINVAL;
3151 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003152 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003153 }
3154
3155 hba->max_pwr_info.is_valid = true;
3156 return 0;
3157}
3158
3159static int ufshcd_change_power_mode(struct ufs_hba *hba,
3160 struct ufs_pa_layer_attr *pwr_mode)
3161{
3162 int ret;
3163
3164 /* if already configured to the requested pwr_mode */
3165 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
3166 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
3167 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
3168 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
3169 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
3170 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
3171 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
3172 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
3173 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303174 }
3175
3176 /*
3177 * Configure attributes for power mode change with below.
3178 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3179 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3180 * - PA_HSSERIES
3181 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003182 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
3183 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
3184 pwr_mode->lane_rx);
3185 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3186 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303187 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003188 else
3189 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303190
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003191 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
3192 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
3193 pwr_mode->lane_tx);
3194 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
3195 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303196 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003197 else
3198 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303199
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003200 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3201 pwr_mode->pwr_tx == FASTAUTO_MODE ||
3202 pwr_mode->pwr_rx == FAST_MODE ||
3203 pwr_mode->pwr_tx == FAST_MODE)
3204 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
3205 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303206
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003207 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
3208 | pwr_mode->pwr_tx);
3209
3210 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303211 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003212 "%s: power mode change failed %d\n", __func__, ret);
3213 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003214 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
3215 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003216
3217 memcpy(&hba->pwr_info, pwr_mode,
3218 sizeof(struct ufs_pa_layer_attr));
3219 }
3220
Dolev Raviv66cc8202016-12-22 18:39:42 -08003221 ufshcd_print_pwr_info(hba);
3222
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003223 return ret;
3224}
3225
3226/**
3227 * ufshcd_config_pwr_mode - configure a new power mode
3228 * @hba: per-adapter instance
3229 * @desired_pwr_mode: desired power configuration
3230 */
3231static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3232 struct ufs_pa_layer_attr *desired_pwr_mode)
3233{
3234 struct ufs_pa_layer_attr final_params = { 0 };
3235 int ret;
3236
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003237 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3238 desired_pwr_mode, &final_params);
3239
3240 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003241 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3242
3243 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303244
3245 return ret;
3246}
3247
3248/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303249 * ufshcd_complete_dev_init() - checks device readiness
3250 * hba: per-adapter instance
3251 *
3252 * Set fDeviceInit flag and poll until device toggles it.
3253 */
3254static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3255{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003256 int i;
3257 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303258 bool flag_res = 1;
3259
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003260 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3261 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303262 if (err) {
3263 dev_err(hba->dev,
3264 "%s setting fDeviceInit flag failed with error %d\n",
3265 __func__, err);
3266 goto out;
3267 }
3268
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003269 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3270 for (i = 0; i < 1000 && !err && flag_res; i++)
3271 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3272 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3273
Dolev Raviv68078d52013-07-30 00:35:58 +05303274 if (err)
3275 dev_err(hba->dev,
3276 "%s reading fDeviceInit flag failed with error %d\n",
3277 __func__, err);
3278 else if (flag_res)
3279 dev_err(hba->dev,
3280 "%s fDeviceInit was not cleared by the device\n",
3281 __func__);
3282
3283out:
3284 return err;
3285}
3286
3287/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303288 * ufshcd_make_hba_operational - Make UFS controller operational
3289 * @hba: per adapter instance
3290 *
3291 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003292 * 1. Enable required interrupts
3293 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003294 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003295 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303296 *
3297 * Returns 0 on success, non-zero value on failure
3298 */
3299static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3300{
3301 int err = 0;
3302 u32 reg;
3303
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303304 /* Enable required interrupts */
3305 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3306
3307 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003308 if (ufshcd_is_intr_aggr_allowed(hba))
3309 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3310 else
3311 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303312
3313 /* Configure UTRL and UTMRL base address registers */
3314 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3315 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3316 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3317 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3318 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3319 REG_UTP_TASK_REQ_LIST_BASE_L);
3320 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3321 REG_UTP_TASK_REQ_LIST_BASE_H);
3322
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303323 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003324 * Make sure base address and interrupt setup are updated before
3325 * enabling the run/stop registers below.
3326 */
3327 wmb();
3328
3329 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303330 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303331 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003332 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303333 if (!(ufshcd_get_lists_status(reg))) {
3334 ufshcd_enable_run_stop_reg(hba);
3335 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303336 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303337 "Host controller not ready to process requests");
3338 err = -EIO;
3339 goto out;
3340 }
3341
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303342out:
3343 return err;
3344}
3345
3346/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003347 * ufshcd_hba_stop - Send controller to reset state
3348 * @hba: per adapter instance
3349 * @can_sleep: perform sleep or just spin
3350 */
3351static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3352{
3353 int err;
3354
3355 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3356 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3357 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3358 10, 1, can_sleep);
3359 if (err)
3360 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3361}
3362
3363/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303364 * ufshcd_hba_enable - initialize the controller
3365 * @hba: per adapter instance
3366 *
3367 * The controller resets itself and controller firmware initialization
3368 * sequence kicks off. When controller is ready it will set
3369 * the Host Controller Enable bit to 1.
3370 *
3371 * Returns 0 on success, non-zero value on failure
3372 */
3373static int ufshcd_hba_enable(struct ufs_hba *hba)
3374{
3375 int retry;
3376
3377 /*
3378 * msleep of 1 and 5 used in this function might result in msleep(20),
3379 * but it was necessary to send the UFS FPGA to reset mode during
3380 * development and testing of this driver. msleep can be changed to
3381 * mdelay and retry count can be reduced based on the controller.
3382 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003383 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303384 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003385 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303386
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003387 /* UniPro link is disabled at this point */
3388 ufshcd_set_link_off(hba);
3389
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003390 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003391
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303392 /* start controller initialization sequence */
3393 ufshcd_hba_start(hba);
3394
3395 /*
3396 * To initialize a UFS host controller HCE bit must be set to 1.
3397 * During initialization the HCE bit value changes from 1->0->1.
3398 * When the host controller completes initialization sequence
3399 * it sets the value of HCE bit to 1. The same HCE bit is read back
3400 * to check if the controller has completed initialization sequence.
3401 * So without this delay the value HCE = 1, set in the previous
3402 * instruction might be read back.
3403 * This delay can be changed based on the controller.
3404 */
3405 msleep(1);
3406
3407 /* wait for the host controller to complete initialization */
3408 retry = 10;
3409 while (ufshcd_is_hba_active(hba)) {
3410 if (retry) {
3411 retry--;
3412 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303413 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303414 "Controller enable failed\n");
3415 return -EIO;
3416 }
3417 msleep(5);
3418 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003419
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003420 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003421 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003422
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003423 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003424
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303425 return 0;
3426}
3427
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003428static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3429{
3430 int tx_lanes, i, err = 0;
3431
3432 if (!peer)
3433 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3434 &tx_lanes);
3435 else
3436 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3437 &tx_lanes);
3438 for (i = 0; i < tx_lanes; i++) {
3439 if (!peer)
3440 err = ufshcd_dme_set(hba,
3441 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3442 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3443 0);
3444 else
3445 err = ufshcd_dme_peer_set(hba,
3446 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3447 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3448 0);
3449 if (err) {
3450 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3451 __func__, peer, i, err);
3452 break;
3453 }
3454 }
3455
3456 return err;
3457}
3458
3459static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3460{
3461 return ufshcd_disable_tx_lcc(hba, true);
3462}
3463
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303464/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303465 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303466 * @hba: per adapter instance
3467 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303468 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303469 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303470static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303471{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303472 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003473 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003474 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303475
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003476 /*
3477 * If UFS device isn't active then we will have to issue link startup
3478 * 2 times to make sure the device state move to active.
3479 */
3480 if (!ufshcd_is_ufs_dev_active(hba))
3481 link_startup_again = true;
3482
3483link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003484 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003485 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303486
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003487 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003488
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003489 /* check if device is detected by inter-connect layer */
3490 if (!ret && !ufshcd_is_device_present(hba)) {
3491 dev_err(hba->dev, "%s: Device not present\n", __func__);
3492 ret = -ENXIO;
3493 goto out;
3494 }
3495
3496 /*
3497 * DME link lost indication is only received when link is up,
3498 * but we can't be sure if the link is up until link startup
3499 * succeeds. So reset the local Uni-Pro and try again.
3500 */
3501 if (ret && ufshcd_hba_enable(hba))
3502 goto out;
3503 } while (ret && retries--);
3504
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303505 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003506 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303507 goto out;
3508
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003509 if (link_startup_again) {
3510 link_startup_again = false;
3511 retries = DME_LINKSTARTUP_RETRIES;
3512 goto link_startup;
3513 }
3514
subhashj@codeaurora.orgd2aebb92016-12-22 18:41:33 -08003515 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
3516 ufshcd_init_pwr_info(hba);
3517 ufshcd_print_pwr_info(hba);
3518
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003519 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3520 ret = ufshcd_disable_device_tx_lcc(hba);
3521 if (ret)
3522 goto out;
3523 }
3524
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003525 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003526 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3527 if (ret)
3528 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003529
3530 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303531out:
3532 if (ret)
3533 dev_err(hba->dev, "link startup failed %d\n", ret);
3534 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303535}
3536
3537/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303538 * ufshcd_verify_dev_init() - Verify device initialization
3539 * @hba: per-adapter instance
3540 *
3541 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3542 * device Transport Protocol (UTP) layer is ready after a reset.
3543 * If the UTP layer at the device side is not initialized, it may
3544 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3545 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3546 */
3547static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3548{
3549 int err = 0;
3550 int retries;
3551
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003552 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303553 mutex_lock(&hba->dev_cmd.lock);
3554 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3555 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3556 NOP_OUT_TIMEOUT);
3557
3558 if (!err || err == -ETIMEDOUT)
3559 break;
3560
3561 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3562 }
3563 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003564 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303565
3566 if (err)
3567 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3568 return err;
3569}
3570
3571/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003572 * ufshcd_set_queue_depth - set lun queue depth
3573 * @sdev: pointer to SCSI device
3574 *
3575 * Read bLUQueueDepth value and activate scsi tagged command
3576 * queueing. For WLUN, queue depth is set to 1. For best-effort
3577 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3578 * value that host can queue.
3579 */
3580static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3581{
3582 int ret = 0;
3583 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08003584 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003585 struct ufs_hba *hba;
3586
3587 hba = shost_priv(sdev->host);
3588
3589 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08003590 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3591 /* Read descriptor*/
3592 ret = ufshcd_read_unit_desc_param(hba,
3593 ufshcd_scsi_to_upiu_lun(sdev->lun),
3594 UNIT_DESC_PARAM_LU_Q_DEPTH,
3595 &lun_qdepth,
3596 sizeof(lun_qdepth));
3597 if (!ret || ret == -ENOTSUPP)
3598 break;
3599
3600 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
3601 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003602
3603 /* Some WLUN doesn't support unit descriptor */
3604 if (ret == -EOPNOTSUPP)
3605 lun_qdepth = 1;
3606 else if (!lun_qdepth)
3607 /* eventually, we can figure out the real queue depth */
3608 lun_qdepth = hba->nutrs;
3609 else
3610 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3611
3612 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3613 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003614 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003615}
3616
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003617/*
3618 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3619 * @hba: per-adapter instance
3620 * @lun: UFS device lun id
3621 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3622 *
3623 * Returns 0 in case of success and b_lu_write_protect status would be returned
3624 * @b_lu_write_protect parameter.
3625 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3626 * Returns -EINVAL in case of invalid parameters passed to this function.
3627 */
3628static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3629 u8 lun,
3630 u8 *b_lu_write_protect)
3631{
3632 int ret;
3633
3634 if (!b_lu_write_protect)
3635 ret = -EINVAL;
3636 /*
3637 * According to UFS device spec, RPMB LU can't be write
3638 * protected so skip reading bLUWriteProtect parameter for
3639 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3640 */
3641 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3642 ret = -ENOTSUPP;
3643 else
3644 ret = ufshcd_read_unit_desc_param(hba,
3645 lun,
3646 UNIT_DESC_PARAM_LU_WR_PROTECT,
3647 b_lu_write_protect,
3648 sizeof(*b_lu_write_protect));
3649 return ret;
3650}
3651
3652/**
3653 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3654 * status
3655 * @hba: per-adapter instance
3656 * @sdev: pointer to SCSI device
3657 *
3658 */
3659static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3660 struct scsi_device *sdev)
3661{
3662 if (hba->dev_info.f_power_on_wp_en &&
3663 !hba->dev_info.is_lu_power_on_wp) {
3664 u8 b_lu_write_protect;
3665
3666 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3667 &b_lu_write_protect) &&
3668 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3669 hba->dev_info.is_lu_power_on_wp = true;
3670 }
3671}
3672
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003673/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303674 * ufshcd_slave_alloc - handle initial SCSI device configurations
3675 * @sdev: pointer to SCSI device
3676 *
3677 * Returns success
3678 */
3679static int ufshcd_slave_alloc(struct scsi_device *sdev)
3680{
3681 struct ufs_hba *hba;
3682
3683 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303684
3685 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3686 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303687
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303688 /* allow SCSI layer to restart the device in case of errors */
3689 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003690
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003691 /* REPORT SUPPORTED OPERATION CODES is not supported */
3692 sdev->no_report_opcodes = 1;
3693
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003694
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003695 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003696
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003697 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3698
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003699 return 0;
3700}
3701
3702/**
3703 * ufshcd_change_queue_depth - change queue depth
3704 * @sdev: pointer to SCSI device
3705 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003706 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003707 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003708 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003709static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003710{
3711 struct ufs_hba *hba = shost_priv(sdev->host);
3712
3713 if (depth > hba->nutrs)
3714 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003715 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303716}
3717
3718/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003719 * ufshcd_slave_configure - adjust SCSI device configurations
3720 * @sdev: pointer to SCSI device
3721 */
3722static int ufshcd_slave_configure(struct scsi_device *sdev)
3723{
3724 struct request_queue *q = sdev->request_queue;
3725
3726 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3727 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3728
3729 return 0;
3730}
3731
3732/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303733 * ufshcd_slave_destroy - remove SCSI device configurations
3734 * @sdev: pointer to SCSI device
3735 */
3736static void ufshcd_slave_destroy(struct scsi_device *sdev)
3737{
3738 struct ufs_hba *hba;
3739
3740 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003741 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003742 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3743 unsigned long flags;
3744
3745 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003746 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003747 spin_unlock_irqrestore(hba->host->host_lock, flags);
3748 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303749}
3750
3751/**
3752 * ufshcd_task_req_compl - handle task management request completion
3753 * @hba: per adapter instance
3754 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303755 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303756 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303757 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303758 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303759static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303760{
3761 struct utp_task_req_desc *task_req_descp;
3762 struct utp_upiu_task_rsp *task_rsp_upiup;
3763 unsigned long flags;
3764 int ocs_value;
3765 int task_result;
3766
3767 spin_lock_irqsave(hba->host->host_lock, flags);
3768
3769 /* Clear completed tasks from outstanding_tasks */
3770 __clear_bit(index, &hba->outstanding_tasks);
3771
3772 task_req_descp = hba->utmrdl_base_addr;
3773 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3774
3775 if (ocs_value == OCS_SUCCESS) {
3776 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3777 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003778 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3779 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303780 if (resp)
3781 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303782 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303783 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3784 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303785 }
3786 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303787
3788 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303789}
3790
3791/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303792 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3793 * @lrb: pointer to local reference block of completed command
3794 * @scsi_status: SCSI command status
3795 *
3796 * Returns value base on SCSI command status
3797 */
3798static inline int
3799ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3800{
3801 int result = 0;
3802
3803 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303804 case SAM_STAT_CHECK_CONDITION:
3805 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303806 case SAM_STAT_GOOD:
3807 result |= DID_OK << 16 |
3808 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303809 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303810 break;
3811 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303812 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303813 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303814 ufshcd_copy_sense_data(lrbp);
3815 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303816 break;
3817 default:
3818 result |= DID_ERROR << 16;
3819 break;
3820 } /* end of switch */
3821
3822 return result;
3823}
3824
3825/**
3826 * ufshcd_transfer_rsp_status - Get overall status of the response
3827 * @hba: per adapter instance
3828 * @lrb: pointer to local reference block of completed command
3829 *
3830 * Returns result of the command to notify SCSI midlayer
3831 */
3832static inline int
3833ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3834{
3835 int result = 0;
3836 int scsi_status;
3837 int ocs;
3838
3839 /* overall command status of utrd */
3840 ocs = ufshcd_get_tr_ocs(lrbp);
3841
3842 switch (ocs) {
3843 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303844 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303845
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303846 switch (result) {
3847 case UPIU_TRANSACTION_RESPONSE:
3848 /*
3849 * get the response UPIU result to extract
3850 * the SCSI command status
3851 */
3852 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3853
3854 /*
3855 * get the result based on SCSI status response
3856 * to notify the SCSI midlayer of the command status
3857 */
3858 scsi_status = result & MASK_SCSI_STATUS;
3859 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303860
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003861 /*
3862 * Currently we are only supporting BKOPs exception
3863 * events hence we can ignore BKOPs exception event
3864 * during power management callbacks. BKOPs exception
3865 * event is not expected to be raised in runtime suspend
3866 * callback as it allows the urgent bkops.
3867 * During system suspend, we are anyway forcefully
3868 * disabling the bkops and if urgent bkops is needed
3869 * it will be enabled on system resume. Long term
3870 * solution could be to abort the system suspend if
3871 * UFS device needs urgent BKOPs.
3872 */
3873 if (!hba->pm_op_in_progress &&
3874 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303875 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303876 break;
3877 case UPIU_TRANSACTION_REJECT_UPIU:
3878 /* TODO: handle Reject UPIU Response */
3879 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303880 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303881 "Reject UPIU not fully implemented\n");
3882 break;
3883 default:
3884 result = DID_ERROR << 16;
3885 dev_err(hba->dev,
3886 "Unexpected request response code = %x\n",
3887 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303888 break;
3889 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303890 break;
3891 case OCS_ABORTED:
3892 result |= DID_ABORT << 16;
3893 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303894 case OCS_INVALID_COMMAND_STATUS:
3895 result |= DID_REQUEUE << 16;
3896 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303897 case OCS_INVALID_CMD_TABLE_ATTR:
3898 case OCS_INVALID_PRDT_ATTR:
3899 case OCS_MISMATCH_DATA_BUF_SIZE:
3900 case OCS_MISMATCH_RESP_UPIU_SIZE:
3901 case OCS_PEER_COMM_FAILURE:
3902 case OCS_FATAL_ERROR:
3903 default:
3904 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303905 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303906 "OCS error from controller = %x\n", ocs);
3907 break;
3908 } /* end of switch */
3909
Dolev Raviv66cc8202016-12-22 18:39:42 -08003910 if (host_byte(result) != DID_OK)
3911 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303912 return result;
3913}
3914
3915/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303916 * ufshcd_uic_cmd_compl - handle completion of uic command
3917 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303918 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303919 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303920static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303921{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303922 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303923 hba->active_uic_cmd->argument2 |=
3924 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303925 hba->active_uic_cmd->argument3 =
3926 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303927 complete(&hba->active_uic_cmd->done);
3928 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303929
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003930 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3931 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303932}
3933
3934/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003935 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303936 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003937 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303938 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003939static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3940 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303941{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303942 struct ufshcd_lrb *lrbp;
3943 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303944 int result;
3945 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003946
Dolev Ravive9d501b2014-07-01 12:22:37 +03003947 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3948 lrbp = &hba->lrb[index];
3949 cmd = lrbp->cmd;
3950 if (cmd) {
3951 result = ufshcd_transfer_rsp_status(hba, lrbp);
3952 scsi_dma_unmap(cmd);
3953 cmd->result = result;
3954 /* Mark completed command as NULL in LRB */
3955 lrbp->cmd = NULL;
3956 clear_bit_unlock(index, &hba->lrb_in_use);
3957 /* Do not touch lrbp after scsi done */
3958 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003959 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003960 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3961 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003962 if (hba->dev_cmd.complete)
3963 complete(hba->dev_cmd.complete);
3964 }
3965 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303966
3967 /* clear corresponding bits of completed commands */
3968 hba->outstanding_reqs ^= completed_reqs;
3969
Sahitya Tummala856b3482014-09-25 15:32:34 +03003970 ufshcd_clk_scaling_update_busy(hba);
3971
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303972 /* we might have free'd some tags above */
3973 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303974}
3975
3976/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003977 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3978 * @hba: per adapter instance
3979 */
3980static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3981{
3982 unsigned long completed_reqs;
3983 u32 tr_doorbell;
3984
3985 /* Resetting interrupt aggregation counters first and reading the
3986 * DOOR_BELL afterward allows us to handle all the completed requests.
3987 * In order to prevent other interrupts starvation the DB is read once
3988 * after reset. The down side of this solution is the possibility of
3989 * false interrupt if device completes another request after resetting
3990 * aggregation and before reading the DB.
3991 */
3992 if (ufshcd_is_intr_aggr_allowed(hba))
3993 ufshcd_reset_intr_aggr(hba);
3994
3995 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3996 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3997
3998 __ufshcd_transfer_req_compl(hba, completed_reqs);
3999}
4000
4001/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304002 * ufshcd_disable_ee - disable exception event
4003 * @hba: per-adapter instance
4004 * @mask: exception event to disable
4005 *
4006 * Disables exception event in the device so that the EVENT_ALERT
4007 * bit is not set.
4008 *
4009 * Returns zero on success, non-zero error value on failure.
4010 */
4011static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
4012{
4013 int err = 0;
4014 u32 val;
4015
4016 if (!(hba->ee_ctrl_mask & mask))
4017 goto out;
4018
4019 val = hba->ee_ctrl_mask & ~mask;
4020 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004021 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304022 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4023 if (!err)
4024 hba->ee_ctrl_mask &= ~mask;
4025out:
4026 return err;
4027}
4028
4029/**
4030 * ufshcd_enable_ee - enable exception event
4031 * @hba: per-adapter instance
4032 * @mask: exception event to enable
4033 *
4034 * Enable corresponding exception event in the device to allow
4035 * device to alert host in critical scenarios.
4036 *
4037 * Returns zero on success, non-zero error value on failure.
4038 */
4039static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
4040{
4041 int err = 0;
4042 u32 val;
4043
4044 if (hba->ee_ctrl_mask & mask)
4045 goto out;
4046
4047 val = hba->ee_ctrl_mask | mask;
4048 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004049 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304050 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
4051 if (!err)
4052 hba->ee_ctrl_mask |= mask;
4053out:
4054 return err;
4055}
4056
4057/**
4058 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4059 * @hba: per-adapter instance
4060 *
4061 * Allow device to manage background operations on its own. Enabling
4062 * this might lead to inconsistent latencies during normal data transfers
4063 * as the device is allowed to manage its own way of handling background
4064 * operations.
4065 *
4066 * Returns zero on success, non-zero on failure.
4067 */
4068static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
4069{
4070 int err = 0;
4071
4072 if (hba->auto_bkops_enabled)
4073 goto out;
4074
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004075 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304076 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4077 if (err) {
4078 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
4079 __func__, err);
4080 goto out;
4081 }
4082
4083 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004084 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304085
4086 /* No need of URGENT_BKOPS exception from the device */
4087 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4088 if (err)
4089 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
4090 __func__, err);
4091out:
4092 return err;
4093}
4094
4095/**
4096 * ufshcd_disable_auto_bkops - block device in doing background operations
4097 * @hba: per-adapter instance
4098 *
4099 * Disabling background operations improves command response latency but
4100 * has drawback of device moving into critical state where the device is
4101 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4102 * host is idle so that BKOPS are managed effectively without any negative
4103 * impacts.
4104 *
4105 * Returns zero on success, non-zero on failure.
4106 */
4107static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
4108{
4109 int err = 0;
4110
4111 if (!hba->auto_bkops_enabled)
4112 goto out;
4113
4114 /*
4115 * If host assisted BKOPs is to be enabled, make sure
4116 * urgent bkops exception is allowed.
4117 */
4118 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
4119 if (err) {
4120 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
4121 __func__, err);
4122 goto out;
4123 }
4124
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004125 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304126 QUERY_FLAG_IDN_BKOPS_EN, NULL);
4127 if (err) {
4128 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
4129 __func__, err);
4130 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
4131 goto out;
4132 }
4133
4134 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004135 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304136out:
4137 return err;
4138}
4139
4140/**
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004141 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304142 * @hba: per adapter instance
4143 *
4144 * After a device reset the device may toggle the BKOPS_EN flag
4145 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004146 * as well. This function would change the auto-bkops state based on
4147 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304148 */
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004149static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304150{
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08004151 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
4152 hba->auto_bkops_enabled = false;
4153 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
4154 ufshcd_enable_auto_bkops(hba);
4155 } else {
4156 hba->auto_bkops_enabled = true;
4157 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
4158 ufshcd_disable_auto_bkops(hba);
4159 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304160}
4161
4162static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
4163{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004164 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304165 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
4166}
4167
4168/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004169 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4170 * @hba: per-adapter instance
4171 * @status: bkops_status value
4172 *
4173 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4174 * flag in the device to permit background operations if the device
4175 * bkops_status is greater than or equal to "status" argument passed to
4176 * this function, disable otherwise.
4177 *
4178 * Returns 0 for success, non-zero in case of failure.
4179 *
4180 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4181 * to know whether auto bkops is enabled or disabled after this function
4182 * returns control to it.
4183 */
4184static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
4185 enum bkops_status status)
4186{
4187 int err;
4188 u32 curr_status = 0;
4189
4190 err = ufshcd_get_bkops_status(hba, &curr_status);
4191 if (err) {
4192 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4193 __func__, err);
4194 goto out;
4195 } else if (curr_status > BKOPS_STATUS_MAX) {
4196 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
4197 __func__, curr_status);
4198 err = -EINVAL;
4199 goto out;
4200 }
4201
4202 if (curr_status >= status)
4203 err = ufshcd_enable_auto_bkops(hba);
4204 else
4205 err = ufshcd_disable_auto_bkops(hba);
4206out:
4207 return err;
4208}
4209
4210/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304211 * ufshcd_urgent_bkops - handle urgent bkops exception event
4212 * @hba: per-adapter instance
4213 *
4214 * Enable fBackgroundOpsEn flag in the device to permit background
4215 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004216 *
4217 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4218 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304219 */
4220static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4221{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004222 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304223}
4224
4225static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4226{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004227 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304228 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4229}
4230
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004231static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4232{
4233 int err;
4234 u32 curr_status = 0;
4235
4236 if (hba->is_urgent_bkops_lvl_checked)
4237 goto enable_auto_bkops;
4238
4239 err = ufshcd_get_bkops_status(hba, &curr_status);
4240 if (err) {
4241 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4242 __func__, err);
4243 goto out;
4244 }
4245
4246 /*
4247 * We are seeing that some devices are raising the urgent bkops
4248 * exception events even when BKOPS status doesn't indicate performace
4249 * impacted or critical. Handle these device by determining their urgent
4250 * bkops status at runtime.
4251 */
4252 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4253 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4254 __func__, curr_status);
4255 /* update the current status as the urgent bkops level */
4256 hba->urgent_bkops_lvl = curr_status;
4257 hba->is_urgent_bkops_lvl_checked = true;
4258 }
4259
4260enable_auto_bkops:
4261 err = ufshcd_enable_auto_bkops(hba);
4262out:
4263 if (err < 0)
4264 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4265 __func__, err);
4266}
4267
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304268/**
4269 * ufshcd_exception_event_handler - handle exceptions raised by device
4270 * @work: pointer to work data
4271 *
4272 * Read bExceptionEventStatus attribute from the device and handle the
4273 * exception event accordingly.
4274 */
4275static void ufshcd_exception_event_handler(struct work_struct *work)
4276{
4277 struct ufs_hba *hba;
4278 int err;
4279 u32 status = 0;
4280 hba = container_of(work, struct ufs_hba, eeh_work);
4281
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304282 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304283 err = ufshcd_get_ee_status(hba, &status);
4284 if (err) {
4285 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4286 __func__, err);
4287 goto out;
4288 }
4289
4290 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004291
4292 if (status & MASK_EE_URGENT_BKOPS)
4293 ufshcd_bkops_exception_event_handler(hba);
4294
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304295out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304296 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304297 return;
4298}
4299
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004300/* Complete requests that have door-bell cleared */
4301static void ufshcd_complete_requests(struct ufs_hba *hba)
4302{
4303 ufshcd_transfer_req_compl(hba);
4304 ufshcd_tmc_handler(hba);
4305}
4306
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304307/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004308 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4309 * to recover from the DL NAC errors or not.
4310 * @hba: per-adapter instance
4311 *
4312 * Returns true if error handling is required, false otherwise
4313 */
4314static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4315{
4316 unsigned long flags;
4317 bool err_handling = true;
4318
4319 spin_lock_irqsave(hba->host->host_lock, flags);
4320 /*
4321 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4322 * device fatal error and/or DL NAC & REPLAY timeout errors.
4323 */
4324 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4325 goto out;
4326
4327 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4328 ((hba->saved_err & UIC_ERROR) &&
4329 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4330 goto out;
4331
4332 if ((hba->saved_err & UIC_ERROR) &&
4333 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4334 int err;
4335 /*
4336 * wait for 50ms to see if we can get any other errors or not.
4337 */
4338 spin_unlock_irqrestore(hba->host->host_lock, flags);
4339 msleep(50);
4340 spin_lock_irqsave(hba->host->host_lock, flags);
4341
4342 /*
4343 * now check if we have got any other severe errors other than
4344 * DL NAC error?
4345 */
4346 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4347 ((hba->saved_err & UIC_ERROR) &&
4348 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4349 goto out;
4350
4351 /*
4352 * As DL NAC is the only error received so far, send out NOP
4353 * command to confirm if link is still active or not.
4354 * - If we don't get any response then do error recovery.
4355 * - If we get response then clear the DL NAC error bit.
4356 */
4357
4358 spin_unlock_irqrestore(hba->host->host_lock, flags);
4359 err = ufshcd_verify_dev_init(hba);
4360 spin_lock_irqsave(hba->host->host_lock, flags);
4361
4362 if (err)
4363 goto out;
4364
4365 /* Link seems to be alive hence ignore the DL NAC errors */
4366 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4367 hba->saved_err &= ~UIC_ERROR;
4368 /* clear NAC error */
4369 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4370 if (!hba->saved_uic_err) {
4371 err_handling = false;
4372 goto out;
4373 }
4374 }
4375out:
4376 spin_unlock_irqrestore(hba->host->host_lock, flags);
4377 return err_handling;
4378}
4379
4380/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304381 * ufshcd_err_handler - handle UFS errors that require s/w attention
4382 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304383 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304384static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304385{
4386 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304387 unsigned long flags;
4388 u32 err_xfer = 0;
4389 u32 err_tm = 0;
4390 int err = 0;
4391 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004392 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304393
4394 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304395
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304396 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004397 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304398
4399 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004400 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304401 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304402
4403 hba->ufshcd_state = UFSHCD_STATE_RESET;
4404 ufshcd_set_eh_in_progress(hba);
4405
4406 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004407 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004408
4409 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4410 bool ret;
4411
4412 spin_unlock_irqrestore(hba->host->host_lock, flags);
4413 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4414 ret = ufshcd_quirk_dl_nac_errors(hba);
4415 spin_lock_irqsave(hba->host->host_lock, flags);
4416 if (!ret)
4417 goto skip_err_handling;
4418 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004419 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4420 ((hba->saved_err & UIC_ERROR) &&
4421 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4422 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4423 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4424 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304425
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004426 /*
4427 * if host reset is required then skip clearing the pending
4428 * transfers forcefully because they will automatically get
4429 * cleared after link startup.
4430 */
4431 if (needs_reset)
4432 goto skip_pending_xfer_clear;
4433
4434 /* release lock as clear command might sleep */
4435 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304436 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004437 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4438 if (ufshcd_clear_cmd(hba, tag)) {
4439 err_xfer = true;
4440 goto lock_skip_pending_xfer_clear;
4441 }
4442 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304443
4444 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004445 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4446 if (ufshcd_clear_tm_cmd(hba, tag)) {
4447 err_tm = true;
4448 goto lock_skip_pending_xfer_clear;
4449 }
4450 }
4451
4452lock_skip_pending_xfer_clear:
4453 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304454
4455 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004456 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304457
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004458 if (err_xfer || err_tm)
4459 needs_reset = true;
4460
4461skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304462 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004463 if (needs_reset) {
4464 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4465
4466 /*
4467 * ufshcd_reset_and_restore() does the link reinitialization
4468 * which will need atleast one empty doorbell slot to send the
4469 * device management commands (NOP and query commands).
4470 * If there is no slot empty at this moment then free up last
4471 * slot forcefully.
4472 */
4473 if (hba->outstanding_reqs == max_doorbells)
4474 __ufshcd_transfer_req_compl(hba,
4475 (1UL << (hba->nutrs - 1)));
4476
4477 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304478 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004479 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304480 if (err) {
4481 dev_err(hba->dev, "%s: reset and restore failed\n",
4482 __func__);
4483 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4484 }
4485 /*
4486 * Inform scsi mid-layer that we did reset and allow to handle
4487 * Unit Attention properly.
4488 */
4489 scsi_report_bus_reset(hba->host, 0);
4490 hba->saved_err = 0;
4491 hba->saved_uic_err = 0;
4492 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004493
Yaniv Gardi583fa622016-03-10 17:37:13 +02004494skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004495 if (!needs_reset) {
4496 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4497 if (hba->saved_err || hba->saved_uic_err)
4498 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4499 __func__, hba->saved_err, hba->saved_uic_err);
4500 }
4501
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304502 ufshcd_clear_eh_in_progress(hba);
4503
4504out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004505 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304506 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004507 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304508 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304509}
4510
4511/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304512 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4513 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304514 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304515static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304516{
4517 u32 reg;
4518
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08004519 /* PHY layer lane error */
4520 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
4521 /* Ignore LINERESET indication, as this is not an error */
4522 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
4523 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK))
4524 /*
4525 * To know whether this error is fatal or not, DB timeout
4526 * must be checked but this error is handled separately.
4527 */
4528 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
4529
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304530 /* PA_INIT_ERROR is fatal and needs UIC reset */
4531 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4532 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4533 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004534 else if (hba->dev_quirks &
4535 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4536 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4537 hba->uic_error |=
4538 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4539 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4540 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4541 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304542
4543 /* UIC NL/TL/DME errors needs software retry */
4544 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4545 if (reg)
4546 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4547
4548 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4549 if (reg)
4550 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4551
4552 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4553 if (reg)
4554 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4555
4556 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4557 __func__, hba->uic_error);
4558}
4559
4560/**
4561 * ufshcd_check_errors - Check for errors that need s/w attention
4562 * @hba: per-adapter instance
4563 */
4564static void ufshcd_check_errors(struct ufs_hba *hba)
4565{
4566 bool queue_eh_work = false;
4567
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304568 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304569 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304570
4571 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304572 hba->uic_error = 0;
4573 ufshcd_update_uic_error(hba);
4574 if (hba->uic_error)
4575 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304576 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304577
4578 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004579 /*
4580 * update the transfer error masks to sticky bits, let's do this
4581 * irrespective of current ufshcd_state.
4582 */
4583 hba->saved_err |= hba->errors;
4584 hba->saved_uic_err |= hba->uic_error;
4585
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304586 /* handle fatal errors only when link is functional */
4587 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4588 /* block commands from scsi mid-layer */
4589 scsi_block_requests(hba->host);
4590
Zang Leigang141f8162016-11-16 11:29:37 +08004591 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08004592
4593 /* dump controller state before resetting */
4594 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
4595 bool pr_prdt = !!(hba->saved_err &
4596 SYSTEM_BUS_FATAL_ERROR);
4597
4598 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
4599 __func__, hba->saved_err,
4600 hba->saved_uic_err);
4601
4602 ufshcd_print_host_regs(hba);
4603 ufshcd_print_pwr_info(hba);
4604 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
4605 ufshcd_print_trs(hba, hba->outstanding_reqs,
4606 pr_prdt);
4607 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304608 schedule_work(&hba->eh_work);
4609 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304610 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304611 /*
4612 * if (!queue_eh_work) -
4613 * Other errors are either non-fatal where host recovers
4614 * itself without s/w intervention or errors that will be
4615 * handled by the SCSI core layer.
4616 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304617}
4618
4619/**
4620 * ufshcd_tmc_handler - handle task management function completion
4621 * @hba: per adapter instance
4622 */
4623static void ufshcd_tmc_handler(struct ufs_hba *hba)
4624{
4625 u32 tm_doorbell;
4626
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304627 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304628 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304629 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304630}
4631
4632/**
4633 * ufshcd_sl_intr - Interrupt service routine
4634 * @hba: per adapter instance
4635 * @intr_status: contains interrupts generated by the controller
4636 */
4637static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4638{
4639 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4640 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304641 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304642
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304643 if (intr_status & UFSHCD_UIC_MASK)
4644 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304645
4646 if (intr_status & UTP_TASK_REQ_COMPL)
4647 ufshcd_tmc_handler(hba);
4648
4649 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4650 ufshcd_transfer_req_compl(hba);
4651}
4652
4653/**
4654 * ufshcd_intr - Main interrupt service routine
4655 * @irq: irq number
4656 * @__hba: pointer to adapter instance
4657 *
4658 * Returns IRQ_HANDLED - If interrupt is valid
4659 * IRQ_NONE - If invalid interrupt
4660 */
4661static irqreturn_t ufshcd_intr(int irq, void *__hba)
4662{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004663 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304664 irqreturn_t retval = IRQ_NONE;
4665 struct ufs_hba *hba = __hba;
4666
4667 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304668 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004669 enabled_intr_status =
4670 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304671
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004672 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304673 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004674
4675 if (enabled_intr_status) {
4676 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304677 retval = IRQ_HANDLED;
4678 }
4679 spin_unlock(hba->host->host_lock);
4680 return retval;
4681}
4682
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304683static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4684{
4685 int err = 0;
4686 u32 mask = 1 << tag;
4687 unsigned long flags;
4688
4689 if (!test_bit(tag, &hba->outstanding_tasks))
4690 goto out;
4691
4692 spin_lock_irqsave(hba->host->host_lock, flags);
4693 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4694 spin_unlock_irqrestore(hba->host->host_lock, flags);
4695
4696 /* poll for max. 1 sec to clear door bell register by h/w */
4697 err = ufshcd_wait_for_register(hba,
4698 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004699 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304700out:
4701 return err;
4702}
4703
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304704/**
4705 * ufshcd_issue_tm_cmd - issues task management commands to controller
4706 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304707 * @lun_id: LUN ID to which TM command is sent
4708 * @task_id: task ID to which the TM command is applicable
4709 * @tm_function: task management function opcode
4710 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304711 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304712 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304713 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304714static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4715 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304716{
4717 struct utp_task_req_desc *task_req_descp;
4718 struct utp_upiu_task_req *task_req_upiup;
4719 struct Scsi_Host *host;
4720 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304721 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304722 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304723 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304724
4725 host = hba->host;
4726
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304727 /*
4728 * Get free slot, sleep if slots are unavailable.
4729 * Even though we use wait_event() which sleeps indefinitely,
4730 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4731 */
4732 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004733 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304734
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304735 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304736 task_req_descp = hba->utmrdl_base_addr;
4737 task_req_descp += free_slot;
4738
4739 /* Configure task request descriptor */
4740 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4741 task_req_descp->header.dword_2 =
4742 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4743
4744 /* Configure task request UPIU */
4745 task_req_upiup =
4746 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304747 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304748 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304749 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304750 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304751 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304752 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004753 /*
4754 * The host shall provide the same value for LUN field in the basic
4755 * header and for Input Parameter.
4756 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304757 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4758 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304759
Kiwoong Kimd2877be2016-11-10 21:16:15 +09004760 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
4761
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304762 /* send command to the controller */
4763 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004764
4765 /* Make sure descriptors are ready before ringing the task doorbell */
4766 wmb();
4767
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304768 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004769 /* Make sure that doorbell is committed immediately */
4770 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304771
4772 spin_unlock_irqrestore(host->host_lock, flags);
4773
4774 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304775 err = wait_event_timeout(hba->tm_wq,
4776 test_bit(free_slot, &hba->tm_condition),
4777 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304778 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304779 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4780 __func__, tm_function);
4781 if (ufshcd_clear_tm_cmd(hba, free_slot))
4782 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4783 __func__, free_slot);
4784 err = -ETIMEDOUT;
4785 } else {
4786 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304787 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304788
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304789 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304790 ufshcd_put_tm_slot(hba, free_slot);
4791 wake_up(&hba->tm_tag_wq);
4792
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004793 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304794 return err;
4795}
4796
4797/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304798 * ufshcd_eh_device_reset_handler - device reset handler registered to
4799 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304800 * @cmd: SCSI command pointer
4801 *
4802 * Returns SUCCESS/FAILED
4803 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304804static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304805{
4806 struct Scsi_Host *host;
4807 struct ufs_hba *hba;
4808 unsigned int tag;
4809 u32 pos;
4810 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304811 u8 resp = 0xF;
4812 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304813 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304814
4815 host = cmd->device->host;
4816 hba = shost_priv(host);
4817 tag = cmd->request->tag;
4818
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304819 lrbp = &hba->lrb[tag];
4820 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4821 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304822 if (!err)
4823 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304824 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304825 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304826
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304827 /* clear the commands that were pending for corresponding LUN */
4828 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4829 if (hba->lrb[pos].lun == lrbp->lun) {
4830 err = ufshcd_clear_cmd(hba, pos);
4831 if (err)
4832 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304833 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304834 }
4835 spin_lock_irqsave(host->host_lock, flags);
4836 ufshcd_transfer_req_compl(hba);
4837 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304838out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304839 if (!err) {
4840 err = SUCCESS;
4841 } else {
4842 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4843 err = FAILED;
4844 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304845 return err;
4846}
4847
4848/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304849 * ufshcd_abort - abort a specific command
4850 * @cmd: SCSI command pointer
4851 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304852 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4853 * command, and in host controller by clearing the door-bell register. There can
4854 * be race between controller sending the command to the device while abort is
4855 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4856 * really issued and then try to abort it.
4857 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304858 * Returns SUCCESS/FAILED
4859 */
4860static int ufshcd_abort(struct scsi_cmnd *cmd)
4861{
4862 struct Scsi_Host *host;
4863 struct ufs_hba *hba;
4864 unsigned long flags;
4865 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304866 int err = 0;
4867 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304868 u8 resp = 0xF;
4869 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004870 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304871
4872 host = cmd->device->host;
4873 hba = shost_priv(host);
4874 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08004875 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02004876 if (!ufshcd_valid_tag(hba, tag)) {
4877 dev_err(hba->dev,
4878 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4879 __func__, tag, cmd, cmd->request);
4880 BUG();
4881 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304882
Dolev Ravive7d38252016-12-22 18:40:07 -08004883 /*
4884 * Task abort to the device W-LUN is illegal. When this command
4885 * will fail, due to spec violation, scsi err handling next step
4886 * will be to send LU reset which, again, is a spec violation.
4887 * To avoid these unnecessary/illegal step we skip to the last error
4888 * handling stage: reset and restore.
4889 */
4890 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
4891 return ufshcd_eh_host_reset_handler(cmd);
4892
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004893 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004894 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004895 /* If command is already aborted/completed, return SUCCESS */
4896 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4897 dev_err(hba->dev,
4898 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4899 __func__, tag, hba->outstanding_reqs, reg);
4900 goto out;
4901 }
4902
Dolev Ravive9d501b2014-07-01 12:22:37 +03004903 if (!(reg & (1 << tag))) {
4904 dev_err(hba->dev,
4905 "%s: cmd was completed, but without a notifying intr, tag = %d",
4906 __func__, tag);
4907 }
4908
Dolev Raviv66cc8202016-12-22 18:39:42 -08004909 /* Print Transfer Request of aborted task */
4910 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
4911 scsi_print_command(hba->lrb[tag].cmd);
4912 ufshcd_print_host_regs(hba);
4913 ufshcd_print_pwr_info(hba);
4914 ufshcd_print_trs(hba, 1 << tag, true);
4915
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304916 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4917 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4918 UFS_QUERY_TASK, &resp);
4919 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4920 /* cmd pending in the device */
4921 break;
4922 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304923 /*
4924 * cmd not pending in the device, check if it is
4925 * in transition.
4926 */
4927 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4928 if (reg & (1 << tag)) {
4929 /* sleep for max. 200us to stabilize */
4930 usleep_range(100, 200);
4931 continue;
4932 }
4933 /* command completed already */
4934 goto out;
4935 } else {
4936 if (!err)
4937 err = resp; /* service response error */
4938 goto out;
4939 }
4940 }
4941
4942 if (!poll_cnt) {
4943 err = -EBUSY;
4944 goto out;
4945 }
4946
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304947 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4948 UFS_ABORT_TASK, &resp);
4949 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304950 if (!err)
4951 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304952 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304953 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304954
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304955 err = ufshcd_clear_cmd(hba, tag);
4956 if (err)
4957 goto out;
4958
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304959 scsi_dma_unmap(cmd);
4960
4961 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004962 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304963 hba->lrb[tag].cmd = NULL;
4964 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304965
4966 clear_bit_unlock(tag, &hba->lrb_in_use);
4967 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004968
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304969out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304970 if (!err) {
4971 err = SUCCESS;
4972 } else {
4973 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4974 err = FAILED;
4975 }
4976
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004977 /*
4978 * This ufshcd_release() corresponds to the original scsi cmd that got
4979 * aborted here (as we won't get any IRQ for it).
4980 */
4981 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304982 return err;
4983}
4984
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304985/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304986 * ufshcd_host_reset_and_restore - reset and restore host controller
4987 * @hba: per-adapter instance
4988 *
4989 * Note that host controller reset may issue DME_RESET to
4990 * local and remote (device) Uni-Pro stack and the attributes
4991 * are reset to default state.
4992 *
4993 * Returns zero on success, non-zero on failure
4994 */
4995static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4996{
4997 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304998 unsigned long flags;
4999
5000 /* Reset the host controller */
5001 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02005002 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305003 spin_unlock_irqrestore(hba->host->host_lock, flags);
5004
5005 err = ufshcd_hba_enable(hba);
5006 if (err)
5007 goto out;
5008
5009 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005010 err = ufshcd_probe_hba(hba);
5011
5012 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305013 err = -EIO;
5014out:
5015 if (err)
5016 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
5017
5018 return err;
5019}
5020
5021/**
5022 * ufshcd_reset_and_restore - reset and re-initialize host/device
5023 * @hba: per-adapter instance
5024 *
5025 * Reset and recover device, host and re-establish link. This
5026 * is helpful to recover the communication in fatal error conditions.
5027 *
5028 * Returns zero on success, non-zero on failure
5029 */
5030static int ufshcd_reset_and_restore(struct ufs_hba *hba)
5031{
5032 int err = 0;
5033 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005034 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305035
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005036 do {
5037 err = ufshcd_host_reset_and_restore(hba);
5038 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305039
5040 /*
5041 * After reset the door-bell might be cleared, complete
5042 * outstanding requests in s/w here.
5043 */
5044 spin_lock_irqsave(hba->host->host_lock, flags);
5045 ufshcd_transfer_req_compl(hba);
5046 ufshcd_tmc_handler(hba);
5047 spin_unlock_irqrestore(hba->host->host_lock, flags);
5048
5049 return err;
5050}
5051
5052/**
5053 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
5054 * @cmd - SCSI command pointer
5055 *
5056 * Returns SUCCESS/FAILED
5057 */
5058static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
5059{
5060 int err;
5061 unsigned long flags;
5062 struct ufs_hba *hba;
5063
5064 hba = shost_priv(cmd->device->host);
5065
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005066 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305067 /*
5068 * Check if there is any race with fatal error handling.
5069 * If so, wait for it to complete. Even though fatal error
5070 * handling does reset and restore in some cases, don't assume
5071 * anything out of it. We are just avoiding race here.
5072 */
5073 do {
5074 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305075 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305076 hba->ufshcd_state == UFSHCD_STATE_RESET))
5077 break;
5078 spin_unlock_irqrestore(hba->host->host_lock, flags);
5079 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305080 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305081 } while (1);
5082
5083 hba->ufshcd_state = UFSHCD_STATE_RESET;
5084 ufshcd_set_eh_in_progress(hba);
5085 spin_unlock_irqrestore(hba->host->host_lock, flags);
5086
5087 err = ufshcd_reset_and_restore(hba);
5088
5089 spin_lock_irqsave(hba->host->host_lock, flags);
5090 if (!err) {
5091 err = SUCCESS;
5092 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
5093 } else {
5094 err = FAILED;
5095 hba->ufshcd_state = UFSHCD_STATE_ERROR;
5096 }
5097 ufshcd_clear_eh_in_progress(hba);
5098 spin_unlock_irqrestore(hba->host->host_lock, flags);
5099
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005100 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305101 return err;
5102}
5103
5104/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005105 * ufshcd_get_max_icc_level - calculate the ICC level
5106 * @sup_curr_uA: max. current supported by the regulator
5107 * @start_scan: row at the desc table to start scan from
5108 * @buff: power descriptor buffer
5109 *
5110 * Returns calculated max ICC level for specific regulator
5111 */
5112static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
5113{
5114 int i;
5115 int curr_uA;
5116 u16 data;
5117 u16 unit;
5118
5119 for (i = start_scan; i >= 0; i--) {
5120 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
5121 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
5122 ATTR_ICC_LVL_UNIT_OFFSET;
5123 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
5124 switch (unit) {
5125 case UFSHCD_NANO_AMP:
5126 curr_uA = curr_uA / 1000;
5127 break;
5128 case UFSHCD_MILI_AMP:
5129 curr_uA = curr_uA * 1000;
5130 break;
5131 case UFSHCD_AMP:
5132 curr_uA = curr_uA * 1000 * 1000;
5133 break;
5134 case UFSHCD_MICRO_AMP:
5135 default:
5136 break;
5137 }
5138 if (sup_curr_uA >= curr_uA)
5139 break;
5140 }
5141 if (i < 0) {
5142 i = 0;
5143 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
5144 }
5145
5146 return (u32)i;
5147}
5148
5149/**
5150 * ufshcd_calc_icc_level - calculate the max ICC level
5151 * In case regulators are not initialized we'll return 0
5152 * @hba: per-adapter instance
5153 * @desc_buf: power descriptor buffer to extract ICC levels from.
5154 * @len: length of desc_buff
5155 *
5156 * Returns calculated ICC level
5157 */
5158static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
5159 u8 *desc_buf, int len)
5160{
5161 u32 icc_level = 0;
5162
5163 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
5164 !hba->vreg_info.vccq2) {
5165 dev_err(hba->dev,
5166 "%s: Regulator capability was not set, actvIccLevel=%d",
5167 __func__, icc_level);
5168 goto out;
5169 }
5170
5171 if (hba->vreg_info.vcc)
5172 icc_level = ufshcd_get_max_icc_level(
5173 hba->vreg_info.vcc->max_uA,
5174 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
5175 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
5176
5177 if (hba->vreg_info.vccq)
5178 icc_level = ufshcd_get_max_icc_level(
5179 hba->vreg_info.vccq->max_uA,
5180 icc_level,
5181 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
5182
5183 if (hba->vreg_info.vccq2)
5184 icc_level = ufshcd_get_max_icc_level(
5185 hba->vreg_info.vccq2->max_uA,
5186 icc_level,
5187 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
5188out:
5189 return icc_level;
5190}
5191
Dolev Raviv61e07352016-11-23 16:30:49 -08005192static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
5193{
5194 int ret = 0;
5195 int retries;
5196
5197 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
5198 /* write attribute */
5199 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5200 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
5201 if (!ret)
5202 break;
5203
5204 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
5205 }
5206
5207 return ret;
5208}
5209
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005210static void ufshcd_init_icc_levels(struct ufs_hba *hba)
5211{
5212 int ret;
5213 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
5214 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
5215
5216 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
5217 if (ret) {
5218 dev_err(hba->dev,
5219 "%s: Failed reading power descriptor.len = %d ret = %d",
5220 __func__, buff_len, ret);
5221 return;
5222 }
5223
5224 hba->init_prefetch_data.icc_level =
5225 ufshcd_find_max_sup_active_icc_level(hba,
5226 desc_buf, buff_len);
5227 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
5228 __func__, hba->init_prefetch_data.icc_level);
5229
Dolev Raviv61e07352016-11-23 16:30:49 -08005230 ret = ufshcd_set_icc_levels_attr(hba,
5231 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005232
5233 if (ret)
5234 dev_err(hba->dev,
5235 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5236 __func__, hba->init_prefetch_data.icc_level , ret);
5237
5238}
5239
5240/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005241 * ufshcd_scsi_add_wlus - Adds required W-LUs
5242 * @hba: per-adapter instance
5243 *
5244 * UFS device specification requires the UFS devices to support 4 well known
5245 * logical units:
5246 * "REPORT_LUNS" (address: 01h)
5247 * "UFS Device" (address: 50h)
5248 * "RPMB" (address: 44h)
5249 * "BOOT" (address: 30h)
5250 * UFS device's power management needs to be controlled by "POWER CONDITION"
5251 * field of SSU (START STOP UNIT) command. But this "power condition" field
5252 * will take effect only when its sent to "UFS device" well known logical unit
5253 * hence we require the scsi_device instance to represent this logical unit in
5254 * order for the UFS host driver to send the SSU command for power management.
5255
5256 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5257 * Block) LU so user space process can control this LU. User space may also
5258 * want to have access to BOOT LU.
5259
5260 * This function adds scsi device instances for each of all well known LUs
5261 * (except "REPORT LUNS" LU).
5262 *
5263 * Returns zero on success (all required W-LUs are added successfully),
5264 * non-zero error value on failure (if failed to add any of the required W-LU).
5265 */
5266static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
5267{
5268 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005269 struct scsi_device *sdev_rpmb;
5270 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005271
5272 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
5273 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
5274 if (IS_ERR(hba->sdev_ufs_device)) {
5275 ret = PTR_ERR(hba->sdev_ufs_device);
5276 hba->sdev_ufs_device = NULL;
5277 goto out;
5278 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005279 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005280
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005281 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005282 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005283 if (IS_ERR(sdev_boot)) {
5284 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005285 goto remove_sdev_ufs_device;
5286 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005287 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005288
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005289 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005290 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005291 if (IS_ERR(sdev_rpmb)) {
5292 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005293 goto remove_sdev_boot;
5294 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005295 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005296 goto out;
5297
5298remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005299 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005300remove_sdev_ufs_device:
5301 scsi_remove_device(hba->sdev_ufs_device);
5302out:
5303 return ret;
5304}
5305
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005306static int ufs_get_device_info(struct ufs_hba *hba,
5307 struct ufs_device_info *card_data)
5308{
5309 int err;
5310 u8 model_index;
5311 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
5312 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
5313
5314 err = ufshcd_read_device_desc(hba, desc_buf,
5315 QUERY_DESC_DEVICE_MAX_SIZE);
5316 if (err) {
5317 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
5318 __func__, err);
5319 goto out;
5320 }
5321
5322 /*
5323 * getting vendor (manufacturerID) and Bank Index in big endian
5324 * format
5325 */
5326 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
5327 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
5328
5329 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
5330
5331 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
5332 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
5333 if (err) {
5334 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5335 __func__, err);
5336 goto out;
5337 }
5338
5339 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
5340 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
5341 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5342 MAX_MODEL_LEN));
5343
5344 /* Null terminate the model string */
5345 card_data->model[MAX_MODEL_LEN] = '\0';
5346
5347out:
5348 return err;
5349}
5350
5351void ufs_advertise_fixup_device(struct ufs_hba *hba)
5352{
5353 int err;
5354 struct ufs_dev_fix *f;
5355 struct ufs_device_info card_data;
5356
5357 card_data.wmanufacturerid = 0;
5358
5359 err = ufs_get_device_info(hba, &card_data);
5360 if (err) {
5361 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5362 __func__, err);
5363 return;
5364 }
5365
5366 for (f = ufs_fixups; f->quirk; f++) {
5367 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
5368 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
5369 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
5370 !strcmp(f->card.model, UFS_ANY_MODEL)))
5371 hba->dev_quirks |= f->quirk;
5372 }
5373}
5374
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005375/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005376 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5377 * @hba: per-adapter instance
5378 *
5379 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5380 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5381 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5382 * the hibern8 exit latency.
5383 *
5384 * Returns zero on success, non-zero error value on failure.
5385 */
5386static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5387{
5388 int ret = 0;
5389 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5390
5391 ret = ufshcd_dme_peer_get(hba,
5392 UIC_ARG_MIB_SEL(
5393 RX_MIN_ACTIVATETIME_CAPABILITY,
5394 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5395 &peer_rx_min_activatetime);
5396 if (ret)
5397 goto out;
5398
5399 /* make sure proper unit conversion is applied */
5400 tuned_pa_tactivate =
5401 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5402 / PA_TACTIVATE_TIME_UNIT_US);
5403 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5404 tuned_pa_tactivate);
5405
5406out:
5407 return ret;
5408}
5409
5410/**
5411 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5412 * @hba: per-adapter instance
5413 *
5414 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5415 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5416 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5417 * This optimal value can help reduce the hibern8 exit latency.
5418 *
5419 * Returns zero on success, non-zero error value on failure.
5420 */
5421static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5422{
5423 int ret = 0;
5424 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5425 u32 max_hibern8_time, tuned_pa_hibern8time;
5426
5427 ret = ufshcd_dme_get(hba,
5428 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5429 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5430 &local_tx_hibern8_time_cap);
5431 if (ret)
5432 goto out;
5433
5434 ret = ufshcd_dme_peer_get(hba,
5435 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5436 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5437 &peer_rx_hibern8_time_cap);
5438 if (ret)
5439 goto out;
5440
5441 max_hibern8_time = max(local_tx_hibern8_time_cap,
5442 peer_rx_hibern8_time_cap);
5443 /* make sure proper unit conversion is applied */
5444 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5445 / PA_HIBERN8_TIME_UNIT_US);
5446 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5447 tuned_pa_hibern8time);
5448out:
5449 return ret;
5450}
5451
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005452/**
5453 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5454 * less than device PA_TACTIVATE time.
5455 * @hba: per-adapter instance
5456 *
5457 * Some UFS devices require host PA_TACTIVATE to be lower than device
5458 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5459 * for such devices.
5460 *
5461 * Returns zero on success, non-zero error value on failure.
5462 */
5463static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5464{
5465 int ret = 0;
5466 u32 granularity, peer_granularity;
5467 u32 pa_tactivate, peer_pa_tactivate;
5468 u32 pa_tactivate_us, peer_pa_tactivate_us;
5469 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5470
5471 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5472 &granularity);
5473 if (ret)
5474 goto out;
5475
5476 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5477 &peer_granularity);
5478 if (ret)
5479 goto out;
5480
5481 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5482 (granularity > PA_GRANULARITY_MAX_VAL)) {
5483 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5484 __func__, granularity);
5485 return -EINVAL;
5486 }
5487
5488 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5489 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5490 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5491 __func__, peer_granularity);
5492 return -EINVAL;
5493 }
5494
5495 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5496 if (ret)
5497 goto out;
5498
5499 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5500 &peer_pa_tactivate);
5501 if (ret)
5502 goto out;
5503
5504 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5505 peer_pa_tactivate_us = peer_pa_tactivate *
5506 gran_to_us_table[peer_granularity - 1];
5507
5508 if (pa_tactivate_us > peer_pa_tactivate_us) {
5509 u32 new_peer_pa_tactivate;
5510
5511 new_peer_pa_tactivate = pa_tactivate_us /
5512 gran_to_us_table[peer_granularity - 1];
5513 new_peer_pa_tactivate++;
5514 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5515 new_peer_pa_tactivate);
5516 }
5517
5518out:
5519 return ret;
5520}
5521
Yaniv Gardi37113102016-03-10 17:37:16 +02005522static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5523{
5524 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5525 ufshcd_tune_pa_tactivate(hba);
5526 ufshcd_tune_pa_hibern8time(hba);
5527 }
5528
5529 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5530 /* set 1ms timeout for PA_TACTIVATE */
5531 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005532
5533 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5534 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08005535
5536 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005537}
5538
5539/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005540 * ufshcd_probe_hba - probe hba to detect device and initialize
5541 * @hba: per-adapter instance
5542 *
5543 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305544 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005545static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305546{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305547 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005548 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305549
5550 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305551 if (ret)
5552 goto out;
5553
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005554 /* set the default level for urgent bkops */
5555 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5556 hba->is_urgent_bkops_lvl_checked = false;
5557
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005558 /* UniPro link is active now */
5559 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305560
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305561 ret = ufshcd_verify_dev_init(hba);
5562 if (ret)
5563 goto out;
5564
Dolev Raviv68078d52013-07-30 00:35:58 +05305565 ret = ufshcd_complete_dev_init(hba);
5566 if (ret)
5567 goto out;
5568
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005569 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005570 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005571
5572 ret = ufshcd_set_vccq_rail_unused(hba,
5573 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5574 if (ret)
5575 goto out;
5576
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005577 /* UFS device is also active now */
5578 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305579 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005580 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305581
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005582 if (ufshcd_get_max_pwr_mode(hba)) {
5583 dev_err(hba->dev,
5584 "%s: Failed getting max supported power mode\n",
5585 __func__);
5586 } else {
5587 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005588 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005589 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5590 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005591 goto out;
5592 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005593 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005594
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005595 /* set the state as operational after switching to desired gear */
5596 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005597 /*
5598 * If we are in error handling context or in power management callbacks
5599 * context, no need to scan the host
5600 */
5601 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5602 bool flag;
5603
5604 /* clear any previous UFS device information */
5605 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005606 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5607 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005608 hba->dev_info.f_power_on_wp_en = flag;
5609
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005610 if (!hba->is_init_prefetch)
5611 ufshcd_init_icc_levels(hba);
5612
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005613 /* Add required well known logical units to scsi mid layer */
5614 if (ufshcd_scsi_add_wlus(hba))
5615 goto out;
5616
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305617 scsi_scan_host(hba->host);
5618 pm_runtime_put_sync(hba->dev);
5619 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005620
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08005621 /* Resume devfreq after UFS device is detected */
5622 if (ufshcd_is_clkscaling_supported(hba)) {
5623 ufshcd_resume_clkscaling(hba);
5624 hba->clk_scaling.is_allowed = true;
5625 }
5626
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005627 if (!hba->is_init_prefetch)
5628 hba->is_init_prefetch = true;
5629
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305630out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005631 /*
5632 * If we failed to initialize the device or the device is not
5633 * present, turn off the power/clocks etc.
5634 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005635 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5636 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005637 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005638 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005639
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005640 trace_ufshcd_init(dev_name(hba->dev), ret,
5641 ktime_to_us(ktime_sub(ktime_get(), start)),
5642 hba->uic_link_state, hba->curr_dev_pwr_mode);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005643 return ret;
5644}
5645
5646/**
5647 * ufshcd_async_scan - asynchronous execution for probing hba
5648 * @data: data pointer to pass to this function
5649 * @cookie: cookie data
5650 */
5651static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5652{
5653 struct ufs_hba *hba = (struct ufs_hba *)data;
5654
5655 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305656}
5657
Yaniv Gardif550c652016-03-10 17:37:07 +02005658static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5659{
5660 unsigned long flags;
5661 struct Scsi_Host *host;
5662 struct ufs_hba *hba;
5663 int index;
5664 bool found = false;
5665
5666 if (!scmd || !scmd->device || !scmd->device->host)
5667 return BLK_EH_NOT_HANDLED;
5668
5669 host = scmd->device->host;
5670 hba = shost_priv(host);
5671 if (!hba)
5672 return BLK_EH_NOT_HANDLED;
5673
5674 spin_lock_irqsave(host->host_lock, flags);
5675
5676 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5677 if (hba->lrb[index].cmd == scmd) {
5678 found = true;
5679 break;
5680 }
5681 }
5682
5683 spin_unlock_irqrestore(host->host_lock, flags);
5684
5685 /*
5686 * Bypass SCSI error handling and reset the block layer timer if this
5687 * SCSI command was not actually dispatched to UFS driver, otherwise
5688 * let SCSI layer handle the error as usual.
5689 */
5690 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5691}
5692
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305693static struct scsi_host_template ufshcd_driver_template = {
5694 .module = THIS_MODULE,
5695 .name = UFSHCD,
5696 .proc_name = UFSHCD,
5697 .queuecommand = ufshcd_queuecommand,
5698 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005699 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305700 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005701 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305702 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305703 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5704 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005705 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305706 .this_id = -1,
5707 .sg_tablesize = SG_ALL,
5708 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5709 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005710 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005711 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305712};
5713
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005714static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5715 int ua)
5716{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005717 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005718
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005719 if (!vreg)
5720 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005721
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005722 ret = regulator_set_load(vreg->reg, ua);
5723 if (ret < 0) {
5724 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5725 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005726 }
5727
5728 return ret;
5729}
5730
5731static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5732 struct ufs_vreg *vreg)
5733{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005734 if (!vreg)
5735 return 0;
5736 else if (vreg->unused)
5737 return 0;
5738 else
5739 return ufshcd_config_vreg_load(hba->dev, vreg,
5740 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005741}
5742
5743static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5744 struct ufs_vreg *vreg)
5745{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005746 if (!vreg)
5747 return 0;
5748 else if (vreg->unused)
5749 return 0;
5750 else
5751 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005752}
5753
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005754static int ufshcd_config_vreg(struct device *dev,
5755 struct ufs_vreg *vreg, bool on)
5756{
5757 int ret = 0;
5758 struct regulator *reg = vreg->reg;
5759 const char *name = vreg->name;
5760 int min_uV, uA_load;
5761
5762 BUG_ON(!vreg);
5763
5764 if (regulator_count_voltages(reg) > 0) {
5765 min_uV = on ? vreg->min_uV : 0;
5766 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5767 if (ret) {
5768 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5769 __func__, name, ret);
5770 goto out;
5771 }
5772
5773 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005774 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5775 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005776 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005777 }
5778out:
5779 return ret;
5780}
5781
5782static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5783{
5784 int ret = 0;
5785
Yaniv Gardi60f01872016-03-10 17:37:11 +02005786 if (!vreg)
5787 goto out;
5788 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005789 goto out;
5790
5791 ret = ufshcd_config_vreg(dev, vreg, true);
5792 if (!ret)
5793 ret = regulator_enable(vreg->reg);
5794
5795 if (!ret)
5796 vreg->enabled = true;
5797 else
5798 dev_err(dev, "%s: %s enable failed, err=%d\n",
5799 __func__, vreg->name, ret);
5800out:
5801 return ret;
5802}
5803
5804static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5805{
5806 int ret = 0;
5807
Yaniv Gardi60f01872016-03-10 17:37:11 +02005808 if (!vreg)
5809 goto out;
5810 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005811 goto out;
5812
5813 ret = regulator_disable(vreg->reg);
5814
5815 if (!ret) {
5816 /* ignore errors on applying disable config */
5817 ufshcd_config_vreg(dev, vreg, false);
5818 vreg->enabled = false;
5819 } else {
5820 dev_err(dev, "%s: %s disable failed, err=%d\n",
5821 __func__, vreg->name, ret);
5822 }
5823out:
5824 return ret;
5825}
5826
5827static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5828{
5829 int ret = 0;
5830 struct device *dev = hba->dev;
5831 struct ufs_vreg_info *info = &hba->vreg_info;
5832
5833 if (!info)
5834 goto out;
5835
5836 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5837 if (ret)
5838 goto out;
5839
5840 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5841 if (ret)
5842 goto out;
5843
5844 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5845 if (ret)
5846 goto out;
5847
5848out:
5849 if (ret) {
5850 ufshcd_toggle_vreg(dev, info->vccq2, false);
5851 ufshcd_toggle_vreg(dev, info->vccq, false);
5852 ufshcd_toggle_vreg(dev, info->vcc, false);
5853 }
5854 return ret;
5855}
5856
Raviv Shvili6a771a62014-09-25 15:32:24 +03005857static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5858{
5859 struct ufs_vreg_info *info = &hba->vreg_info;
5860
5861 if (info)
5862 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5863
5864 return 0;
5865}
5866
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005867static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5868{
5869 int ret = 0;
5870
5871 if (!vreg)
5872 goto out;
5873
5874 vreg->reg = devm_regulator_get(dev, vreg->name);
5875 if (IS_ERR(vreg->reg)) {
5876 ret = PTR_ERR(vreg->reg);
5877 dev_err(dev, "%s: %s get failed, err=%d\n",
5878 __func__, vreg->name, ret);
5879 }
5880out:
5881 return ret;
5882}
5883
5884static int ufshcd_init_vreg(struct ufs_hba *hba)
5885{
5886 int ret = 0;
5887 struct device *dev = hba->dev;
5888 struct ufs_vreg_info *info = &hba->vreg_info;
5889
5890 if (!info)
5891 goto out;
5892
5893 ret = ufshcd_get_vreg(dev, info->vcc);
5894 if (ret)
5895 goto out;
5896
5897 ret = ufshcd_get_vreg(dev, info->vccq);
5898 if (ret)
5899 goto out;
5900
5901 ret = ufshcd_get_vreg(dev, info->vccq2);
5902out:
5903 return ret;
5904}
5905
Raviv Shvili6a771a62014-09-25 15:32:24 +03005906static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5907{
5908 struct ufs_vreg_info *info = &hba->vreg_info;
5909
5910 if (info)
5911 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5912
5913 return 0;
5914}
5915
Yaniv Gardi60f01872016-03-10 17:37:11 +02005916static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5917{
5918 int ret = 0;
5919 struct ufs_vreg_info *info = &hba->vreg_info;
5920
5921 if (!info)
5922 goto out;
5923 else if (!info->vccq)
5924 goto out;
5925
5926 if (unused) {
5927 /* shut off the rail here */
5928 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5929 /*
5930 * Mark this rail as no longer used, so it doesn't get enabled
5931 * later by mistake
5932 */
5933 if (!ret)
5934 info->vccq->unused = true;
5935 } else {
5936 /*
5937 * rail should have been already enabled hence just make sure
5938 * that unused flag is cleared.
5939 */
5940 info->vccq->unused = false;
5941 }
5942out:
5943 return ret;
5944}
5945
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005946static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5947 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005948{
5949 int ret = 0;
5950 struct ufs_clk_info *clki;
5951 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005952 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005953
5954 if (!head || list_empty(head))
5955 goto out;
5956
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005957 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5958 if (ret)
5959 return ret;
5960
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005961 list_for_each_entry(clki, head, list) {
5962 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005963 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5964 continue;
5965
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005966 if (on && !clki->enabled) {
5967 ret = clk_prepare_enable(clki->clk);
5968 if (ret) {
5969 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5970 __func__, clki->name, ret);
5971 goto out;
5972 }
5973 } else if (!on && clki->enabled) {
5974 clk_disable_unprepare(clki->clk);
5975 }
5976 clki->enabled = on;
5977 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5978 clki->name, on ? "en" : "dis");
5979 }
5980 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005981
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005982 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5983 if (ret)
5984 return ret;
5985
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005986out:
5987 if (ret) {
5988 list_for_each_entry(clki, head, list) {
5989 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5990 clk_disable_unprepare(clki->clk);
5991 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005992 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005993 spin_lock_irqsave(hba->host->host_lock, flags);
5994 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005995 trace_ufshcd_clk_gating(dev_name(hba->dev),
5996 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005997 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005998 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005999
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006000 return ret;
6001}
6002
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006003static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
6004{
6005 return __ufshcd_setup_clocks(hba, on, false);
6006}
6007
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006008static int ufshcd_init_clocks(struct ufs_hba *hba)
6009{
6010 int ret = 0;
6011 struct ufs_clk_info *clki;
6012 struct device *dev = hba->dev;
6013 struct list_head *head = &hba->clk_list_head;
6014
6015 if (!head || list_empty(head))
6016 goto out;
6017
6018 list_for_each_entry(clki, head, list) {
6019 if (!clki->name)
6020 continue;
6021
6022 clki->clk = devm_clk_get(dev, clki->name);
6023 if (IS_ERR(clki->clk)) {
6024 ret = PTR_ERR(clki->clk);
6025 dev_err(dev, "%s: %s clk get failed, %d\n",
6026 __func__, clki->name, ret);
6027 goto out;
6028 }
6029
6030 if (clki->max_freq) {
6031 ret = clk_set_rate(clki->clk, clki->max_freq);
6032 if (ret) {
6033 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6034 __func__, clki->name,
6035 clki->max_freq, ret);
6036 goto out;
6037 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03006038 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006039 }
6040 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
6041 clki->name, clk_get_rate(clki->clk));
6042 }
6043out:
6044 return ret;
6045}
6046
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006047static int ufshcd_variant_hba_init(struct ufs_hba *hba)
6048{
6049 int err = 0;
6050
6051 if (!hba->vops)
6052 goto out;
6053
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006054 err = ufshcd_vops_init(hba);
6055 if (err)
6056 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006057
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006058 err = ufshcd_vops_setup_regulators(hba, true);
6059 if (err)
6060 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006061
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006062 goto out;
6063
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006064out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006065 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006066out:
6067 if (err)
6068 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006069 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006070 return err;
6071}
6072
6073static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
6074{
6075 if (!hba->vops)
6076 return;
6077
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006078 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006079
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006080 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006081}
6082
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006083static int ufshcd_hba_init(struct ufs_hba *hba)
6084{
6085 int err;
6086
Raviv Shvili6a771a62014-09-25 15:32:24 +03006087 /*
6088 * Handle host controller power separately from the UFS device power
6089 * rails as it will help controlling the UFS host controller power
6090 * collapse easily which is different than UFS device power collapse.
6091 * Also, enable the host controller power before we go ahead with rest
6092 * of the initialization here.
6093 */
6094 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006095 if (err)
6096 goto out;
6097
Raviv Shvili6a771a62014-09-25 15:32:24 +03006098 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006099 if (err)
6100 goto out;
6101
Raviv Shvili6a771a62014-09-25 15:32:24 +03006102 err = ufshcd_init_clocks(hba);
6103 if (err)
6104 goto out_disable_hba_vreg;
6105
6106 err = ufshcd_setup_clocks(hba, true);
6107 if (err)
6108 goto out_disable_hba_vreg;
6109
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006110 err = ufshcd_init_vreg(hba);
6111 if (err)
6112 goto out_disable_clks;
6113
6114 err = ufshcd_setup_vreg(hba, true);
6115 if (err)
6116 goto out_disable_clks;
6117
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006118 err = ufshcd_variant_hba_init(hba);
6119 if (err)
6120 goto out_disable_vreg;
6121
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006122 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006123 goto out;
6124
6125out_disable_vreg:
6126 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03006127out_disable_clks:
6128 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03006129out_disable_hba_vreg:
6130 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006131out:
6132 return err;
6133}
6134
6135static void ufshcd_hba_exit(struct ufs_hba *hba)
6136{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006137 if (hba->is_powered) {
6138 ufshcd_variant_hba_exit(hba);
6139 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07006140 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006141 ufshcd_setup_clocks(hba, false);
6142 ufshcd_setup_hba_vreg(hba, false);
6143 hba->is_powered = false;
6144 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006145}
6146
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006147static int
6148ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306149{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006150 unsigned char cmd[6] = {REQUEST_SENSE,
6151 0,
6152 0,
6153 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006154 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006155 0};
6156 char *buffer;
6157 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306158
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006159 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006160 if (!buffer) {
6161 ret = -ENOMEM;
6162 goto out;
6163 }
6164
6165 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006166 UFSHCD_REQ_SENSE_SIZE, NULL,
Christoph Hellwige8064022016-10-20 15:12:13 +02006167 msecs_to_jiffies(1000), 3, NULL, 0, RQF_PM);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006168 if (ret)
6169 pr_err("%s: failed with err %d\n", __func__, ret);
6170
6171 kfree(buffer);
6172out:
6173 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306174}
6175
6176/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006177 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6178 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306179 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006180 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306181 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006182 * Returns 0 if requested power mode is set successfully
6183 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306184 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006185static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
6186 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306187{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006188 unsigned char cmd[6] = { START_STOP };
6189 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006190 struct scsi_device *sdp;
6191 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006192 int ret;
6193
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006194 spin_lock_irqsave(hba->host->host_lock, flags);
6195 sdp = hba->sdev_ufs_device;
6196 if (sdp) {
6197 ret = scsi_device_get(sdp);
6198 if (!ret && !scsi_device_online(sdp)) {
6199 ret = -ENODEV;
6200 scsi_device_put(sdp);
6201 }
6202 } else {
6203 ret = -ENODEV;
6204 }
6205 spin_unlock_irqrestore(hba->host->host_lock, flags);
6206
6207 if (ret)
6208 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006209
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306210 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006211 * If scsi commands fail, the scsi mid-layer schedules scsi error-
6212 * handling, which would wait for host to be resumed. Since we know
6213 * we are functional while we are here, skip host resume in error
6214 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306215 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006216 hba->host->eh_noresume = 1;
6217 if (hba->wlun_dev_clr_ua) {
6218 ret = ufshcd_send_request_sense(hba, sdp);
6219 if (ret)
6220 goto out;
6221 /* Unit attention condition is cleared now */
6222 hba->wlun_dev_clr_ua = false;
6223 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306224
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006225 cmd[4] = pwr_mode << 4;
6226
6227 /*
6228 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02006229 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006230 * already suspended childs.
6231 */
6232 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
Christoph Hellwige8064022016-10-20 15:12:13 +02006233 START_STOP_TIMEOUT, 0, NULL, 0, RQF_PM);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006234 if (ret) {
6235 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02006236 "START_STOP failed for power mode: %d, result %x\n",
6237 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01006238 if (driver_byte(ret) & DRIVER_SENSE)
6239 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006240 }
6241
6242 if (!ret)
6243 hba->curr_dev_pwr_mode = pwr_mode;
6244out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006245 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006246 hba->host->eh_noresume = 0;
6247 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306248}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306249
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006250static int ufshcd_link_state_transition(struct ufs_hba *hba,
6251 enum uic_link_state req_link_state,
6252 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306253{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006254 int ret = 0;
6255
6256 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306257 return 0;
6258
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006259 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
6260 ret = ufshcd_uic_hibern8_enter(hba);
6261 if (!ret)
6262 ufshcd_set_link_hibern8(hba);
6263 else
6264 goto out;
6265 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306266 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006267 * If autobkops is enabled, link can't be turned off because
6268 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306269 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006270 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
6271 (!check_for_bkops || (check_for_bkops &&
6272 !hba->auto_bkops_enabled))) {
6273 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02006274 * Let's make sure that link is in low power mode, we are doing
6275 * this currently by putting the link in Hibern8. Otherway to
6276 * put the link in low power mode is to send the DME end point
6277 * to device and then send the DME reset command to local
6278 * unipro. But putting the link in hibern8 is much faster.
6279 */
6280 ret = ufshcd_uic_hibern8_enter(hba);
6281 if (ret)
6282 goto out;
6283 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006284 * Change controller state to "reset state" which
6285 * should also put the link in off/reset state
6286 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02006287 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006288 /*
6289 * TODO: Check if we need any delay to make sure that
6290 * controller is reset
6291 */
6292 ufshcd_set_link_off(hba);
6293 }
6294
6295out:
6296 return ret;
6297}
6298
6299static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6300{
6301 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006302 * It seems some UFS devices may keep drawing more than sleep current
6303 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6304 * To avoid this situation, add 2ms delay before putting these UFS
6305 * rails in LPM mode.
6306 */
6307 if (!ufshcd_is_link_active(hba) &&
6308 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6309 usleep_range(2000, 2100);
6310
6311 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006312 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6313 * power.
6314 *
6315 * If UFS device and link is in OFF state, all power supplies (VCC,
6316 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6317 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6318 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6319 *
6320 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6321 * in low power state which would save some power.
6322 */
6323 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6324 !hba->dev_info.is_lu_power_on_wp) {
6325 ufshcd_setup_vreg(hba, false);
6326 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6327 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6328 if (!ufshcd_is_link_active(hba)) {
6329 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6330 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6331 }
6332 }
6333}
6334
6335static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6336{
6337 int ret = 0;
6338
6339 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6340 !hba->dev_info.is_lu_power_on_wp) {
6341 ret = ufshcd_setup_vreg(hba, true);
6342 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006343 if (!ret && !ufshcd_is_link_active(hba)) {
6344 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6345 if (ret)
6346 goto vcc_disable;
6347 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6348 if (ret)
6349 goto vccq_lpm;
6350 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07006351 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006352 }
6353 goto out;
6354
6355vccq_lpm:
6356 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6357vcc_disable:
6358 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6359out:
6360 return ret;
6361}
6362
6363static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6364{
6365 if (ufshcd_is_link_off(hba))
6366 ufshcd_setup_hba_vreg(hba, false);
6367}
6368
6369static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6370{
6371 if (ufshcd_is_link_off(hba))
6372 ufshcd_setup_hba_vreg(hba, true);
6373}
6374
6375/**
6376 * ufshcd_suspend - helper function for suspend operations
6377 * @hba: per adapter instance
6378 * @pm_op: desired low power operation type
6379 *
6380 * This function will try to put the UFS device and link into low power
6381 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6382 * (System PM level).
6383 *
6384 * If this function is called during shutdown, it will make sure that
6385 * both UFS device and UFS link is powered off.
6386 *
6387 * NOTE: UFS device & link must be active before we enter in this function.
6388 *
6389 * Returns 0 for success and non-zero for failure
6390 */
6391static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6392{
6393 int ret = 0;
6394 enum ufs_pm_level pm_lvl;
6395 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6396 enum uic_link_state req_link_state;
6397
6398 hba->pm_op_in_progress = 1;
6399 if (!ufshcd_is_shutdown_pm(pm_op)) {
6400 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6401 hba->rpm_lvl : hba->spm_lvl;
6402 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6403 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6404 } else {
6405 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6406 req_link_state = UIC_LINK_OFF_STATE;
6407 }
6408
6409 /*
6410 * If we can't transition into any of the low power modes
6411 * just gate the clocks.
6412 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006413 ufshcd_hold(hba, false);
6414 hba->clk_gating.is_suspended = true;
6415
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006416 ufshcd_suspend_clkscaling(hba);
6417
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006418 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6419 req_link_state == UIC_LINK_ACTIVE_STATE) {
6420 goto disable_clks;
6421 }
6422
6423 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6424 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006425 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006426
6427 /* UFS device & link must be active before we enter in this function */
6428 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6429 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006430 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006431 }
6432
6433 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006434 if (ufshcd_can_autobkops_during_suspend(hba)) {
6435 /*
6436 * The device is idle with no requests in the queue,
6437 * allow background operations if bkops status shows
6438 * that performance might be impacted.
6439 */
6440 ret = ufshcd_urgent_bkops(hba);
6441 if (ret)
6442 goto enable_gating;
6443 } else {
6444 /* make sure that auto bkops is disabled */
6445 ufshcd_disable_auto_bkops(hba);
6446 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006447 }
6448
6449 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6450 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6451 !ufshcd_is_runtime_pm(pm_op))) {
6452 /* ensure that bkops is disabled */
6453 ufshcd_disable_auto_bkops(hba);
6454 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6455 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006456 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006457 }
6458
6459 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6460 if (ret)
6461 goto set_dev_active;
6462
6463 ufshcd_vreg_set_lpm(hba);
6464
6465disable_clks:
6466 /*
6467 * Call vendor specific suspend callback. As these callbacks may access
6468 * vendor specific host controller register space call them before the
6469 * host clocks are ON.
6470 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006471 ret = ufshcd_vops_suspend(hba, pm_op);
6472 if (ret)
6473 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006474
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006475 if (!ufshcd_is_link_active(hba))
6476 ufshcd_setup_clocks(hba, false);
6477 else
6478 /* If link is active, device ref_clk can't be switched off */
6479 __ufshcd_setup_clocks(hba, false, true);
6480
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006481 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006482 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006483 /*
6484 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006485 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006486 */
6487 ufshcd_disable_irq(hba);
6488 /* Put the host controller in low power mode if possible */
6489 ufshcd_hba_vreg_set_lpm(hba);
6490 goto out;
6491
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006492set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07006493 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006494 ufshcd_vreg_set_hpm(hba);
6495 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6496 ufshcd_set_link_active(hba);
6497 else if (ufshcd_is_link_off(hba))
6498 ufshcd_host_reset_and_restore(hba);
6499set_dev_active:
6500 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6501 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006502enable_gating:
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006503 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006504 hba->clk_gating.is_suspended = false;
6505 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006506out:
6507 hba->pm_op_in_progress = 0;
6508 return ret;
6509}
6510
6511/**
6512 * ufshcd_resume - helper function for resume operations
6513 * @hba: per adapter instance
6514 * @pm_op: runtime PM or system PM
6515 *
6516 * This function basically brings the UFS device, UniPro link and controller
6517 * to active state.
6518 *
6519 * Returns 0 for success and non-zero for failure
6520 */
6521static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6522{
6523 int ret;
6524 enum uic_link_state old_link_state;
6525
6526 hba->pm_op_in_progress = 1;
6527 old_link_state = hba->uic_link_state;
6528
6529 ufshcd_hba_vreg_set_hpm(hba);
6530 /* Make sure clocks are enabled before accessing controller */
6531 ret = ufshcd_setup_clocks(hba, true);
6532 if (ret)
6533 goto out;
6534
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006535 /* enable the host irq as host controller would be active soon */
6536 ret = ufshcd_enable_irq(hba);
6537 if (ret)
6538 goto disable_irq_and_vops_clks;
6539
6540 ret = ufshcd_vreg_set_hpm(hba);
6541 if (ret)
6542 goto disable_irq_and_vops_clks;
6543
6544 /*
6545 * Call vendor specific resume callback. As these callbacks may access
6546 * vendor specific host controller register space call them when the
6547 * host clocks are ON.
6548 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006549 ret = ufshcd_vops_resume(hba, pm_op);
6550 if (ret)
6551 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006552
6553 if (ufshcd_is_link_hibern8(hba)) {
6554 ret = ufshcd_uic_hibern8_exit(hba);
6555 if (!ret)
6556 ufshcd_set_link_active(hba);
6557 else
6558 goto vendor_suspend;
6559 } else if (ufshcd_is_link_off(hba)) {
6560 ret = ufshcd_host_reset_and_restore(hba);
6561 /*
6562 * ufshcd_host_reset_and_restore() should have already
6563 * set the link state as active
6564 */
6565 if (ret || !ufshcd_is_link_active(hba))
6566 goto vendor_suspend;
6567 }
6568
6569 if (!ufshcd_is_ufs_dev_active(hba)) {
6570 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6571 if (ret)
6572 goto set_old_link_state;
6573 }
6574
subhashj@codeaurora.org4e768e72016-12-22 18:41:22 -08006575 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
6576 ufshcd_enable_auto_bkops(hba);
6577 else
6578 /*
6579 * If BKOPs operations are urgently needed at this moment then
6580 * keep auto-bkops enabled or else disable it.
6581 */
6582 ufshcd_urgent_bkops(hba);
6583
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006584 hba->clk_gating.is_suspended = false;
6585
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08006586 if (hba->clk_scaling.is_allowed)
6587 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006588
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006589 /* Schedule clock gating in case of no access to UFS device yet */
6590 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006591 goto out;
6592
6593set_old_link_state:
6594 ufshcd_link_state_transition(hba, old_link_state, 0);
6595vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006596 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006597disable_vreg:
6598 ufshcd_vreg_set_lpm(hba);
6599disable_irq_and_vops_clks:
6600 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006601 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006602 ufshcd_setup_clocks(hba, false);
6603out:
6604 hba->pm_op_in_progress = 0;
6605 return ret;
6606}
6607
6608/**
6609 * ufshcd_system_suspend - system suspend routine
6610 * @hba: per adapter instance
6611 * @pm_op: runtime PM or system PM
6612 *
6613 * Check the description of ufshcd_suspend() function for more details.
6614 *
6615 * Returns 0 for success and non-zero for failure
6616 */
6617int ufshcd_system_suspend(struct ufs_hba *hba)
6618{
6619 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006620 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006621
6622 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006623 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006624
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08006625 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6626 hba->curr_dev_pwr_mode) &&
6627 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
6628 hba->uic_link_state))
6629 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006630
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08006631 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006632 /*
6633 * UFS device and/or UFS link low power states during runtime
6634 * suspend seems to be different than what is expected during
6635 * system suspend. Hence runtime resume the devic & link and
6636 * let the system suspend low power states to take effect.
6637 * TODO: If resume takes longer time, we might have optimize
6638 * it in future by not resuming everything if possible.
6639 */
6640 ret = ufshcd_runtime_resume(hba);
6641 if (ret)
6642 goto out;
6643 }
6644
6645 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6646out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006647 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
6648 ktime_to_us(ktime_sub(ktime_get(), start)),
6649 hba->uic_link_state, hba->curr_dev_pwr_mode);
Dolev Ravive7850602014-09-25 15:32:36 +03006650 if (!ret)
6651 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006652 return ret;
6653}
6654EXPORT_SYMBOL(ufshcd_system_suspend);
6655
6656/**
6657 * ufshcd_system_resume - system resume routine
6658 * @hba: per adapter instance
6659 *
6660 * Returns 0 for success and non-zero for failure
6661 */
6662
6663int ufshcd_system_resume(struct ufs_hba *hba)
6664{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006665 int ret = 0;
6666 ktime_t start = ktime_get();
6667
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006668 if (!hba)
6669 return -EINVAL;
6670
6671 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006672 /*
6673 * Let the runtime resume take care of resuming
6674 * if runtime suspended.
6675 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006676 goto out;
6677 else
6678 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
6679out:
6680 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
6681 ktime_to_us(ktime_sub(ktime_get(), start)),
6682 hba->uic_link_state, hba->curr_dev_pwr_mode);
6683 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006684}
6685EXPORT_SYMBOL(ufshcd_system_resume);
6686
6687/**
6688 * ufshcd_runtime_suspend - runtime suspend routine
6689 * @hba: per adapter instance
6690 *
6691 * Check the description of ufshcd_suspend() function for more details.
6692 *
6693 * Returns 0 for success and non-zero for failure
6694 */
6695int ufshcd_runtime_suspend(struct ufs_hba *hba)
6696{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006697 int ret = 0;
6698 ktime_t start = ktime_get();
6699
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006700 if (!hba)
6701 return -EINVAL;
6702
6703 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006704 goto out;
6705 else
6706 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
6707out:
6708 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
6709 ktime_to_us(ktime_sub(ktime_get(), start)),
6710 hba->uic_link_state, hba->curr_dev_pwr_mode);
6711 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306712}
6713EXPORT_SYMBOL(ufshcd_runtime_suspend);
6714
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006715/**
6716 * ufshcd_runtime_resume - runtime resume routine
6717 * @hba: per adapter instance
6718 *
6719 * This function basically brings the UFS device, UniPro link and controller
6720 * to active state. Following operations are done in this function:
6721 *
6722 * 1. Turn on all the controller related clocks
6723 * 2. Bring the UniPro link out of Hibernate state
6724 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6725 * to active state.
6726 * 4. If auto-bkops is enabled on the device, disable it.
6727 *
6728 * So following would be the possible power state after this function return
6729 * successfully:
6730 * S1: UFS device in Active state with VCC rail ON
6731 * UniPro link in Active state
6732 * All the UFS/UniPro controller clocks are ON
6733 *
6734 * Returns 0 for success and non-zero for failure
6735 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306736int ufshcd_runtime_resume(struct ufs_hba *hba)
6737{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006738 int ret = 0;
6739 ktime_t start = ktime_get();
6740
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006741 if (!hba)
6742 return -EINVAL;
6743
6744 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006745 goto out;
6746 else
6747 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
6748out:
6749 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
6750 ktime_to_us(ktime_sub(ktime_get(), start)),
6751 hba->uic_link_state, hba->curr_dev_pwr_mode);
6752 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306753}
6754EXPORT_SYMBOL(ufshcd_runtime_resume);
6755
6756int ufshcd_runtime_idle(struct ufs_hba *hba)
6757{
6758 return 0;
6759}
6760EXPORT_SYMBOL(ufshcd_runtime_idle);
6761
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -08006762static inline ssize_t ufshcd_pm_lvl_store(struct device *dev,
6763 struct device_attribute *attr,
6764 const char *buf, size_t count,
6765 bool rpm)
6766{
6767 struct ufs_hba *hba = dev_get_drvdata(dev);
6768 unsigned long flags, value;
6769
6770 if (kstrtoul(buf, 0, &value))
6771 return -EINVAL;
6772
6773 if ((value < UFS_PM_LVL_0) || (value >= UFS_PM_LVL_MAX))
6774 return -EINVAL;
6775
6776 spin_lock_irqsave(hba->host->host_lock, flags);
6777 if (rpm)
6778 hba->rpm_lvl = value;
6779 else
6780 hba->spm_lvl = value;
6781 spin_unlock_irqrestore(hba->host->host_lock, flags);
6782 return count;
6783}
6784
6785static ssize_t ufshcd_rpm_lvl_show(struct device *dev,
6786 struct device_attribute *attr, char *buf)
6787{
6788 struct ufs_hba *hba = dev_get_drvdata(dev);
6789 int curr_len;
6790 u8 lvl;
6791
6792 curr_len = snprintf(buf, PAGE_SIZE,
6793 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
6794 hba->rpm_lvl,
6795 ufschd_ufs_dev_pwr_mode_to_string(
6796 ufs_pm_lvl_states[hba->rpm_lvl].dev_state),
6797 ufschd_uic_link_state_to_string(
6798 ufs_pm_lvl_states[hba->rpm_lvl].link_state));
6799
6800 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
6801 "\nAll available Runtime PM levels info:\n");
6802 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
6803 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
6804 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
6805 lvl,
6806 ufschd_ufs_dev_pwr_mode_to_string(
6807 ufs_pm_lvl_states[lvl].dev_state),
6808 ufschd_uic_link_state_to_string(
6809 ufs_pm_lvl_states[lvl].link_state));
6810
6811 return curr_len;
6812}
6813
6814static ssize_t ufshcd_rpm_lvl_store(struct device *dev,
6815 struct device_attribute *attr, const char *buf, size_t count)
6816{
6817 return ufshcd_pm_lvl_store(dev, attr, buf, count, true);
6818}
6819
6820static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba *hba)
6821{
6822 hba->rpm_lvl_attr.show = ufshcd_rpm_lvl_show;
6823 hba->rpm_lvl_attr.store = ufshcd_rpm_lvl_store;
6824 sysfs_attr_init(&hba->rpm_lvl_attr.attr);
6825 hba->rpm_lvl_attr.attr.name = "rpm_lvl";
6826 hba->rpm_lvl_attr.attr.mode = 0644;
6827 if (device_create_file(hba->dev, &hba->rpm_lvl_attr))
6828 dev_err(hba->dev, "Failed to create sysfs for rpm_lvl\n");
6829}
6830
6831static ssize_t ufshcd_spm_lvl_show(struct device *dev,
6832 struct device_attribute *attr, char *buf)
6833{
6834 struct ufs_hba *hba = dev_get_drvdata(dev);
6835 int curr_len;
6836 u8 lvl;
6837
6838 curr_len = snprintf(buf, PAGE_SIZE,
6839 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
6840 hba->spm_lvl,
6841 ufschd_ufs_dev_pwr_mode_to_string(
6842 ufs_pm_lvl_states[hba->spm_lvl].dev_state),
6843 ufschd_uic_link_state_to_string(
6844 ufs_pm_lvl_states[hba->spm_lvl].link_state));
6845
6846 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
6847 "\nAll available System PM levels info:\n");
6848 for (lvl = UFS_PM_LVL_0; lvl < UFS_PM_LVL_MAX; lvl++)
6849 curr_len += snprintf((buf + curr_len), (PAGE_SIZE - curr_len),
6850 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
6851 lvl,
6852 ufschd_ufs_dev_pwr_mode_to_string(
6853 ufs_pm_lvl_states[lvl].dev_state),
6854 ufschd_uic_link_state_to_string(
6855 ufs_pm_lvl_states[lvl].link_state));
6856
6857 return curr_len;
6858}
6859
6860static ssize_t ufshcd_spm_lvl_store(struct device *dev,
6861 struct device_attribute *attr, const char *buf, size_t count)
6862{
6863 return ufshcd_pm_lvl_store(dev, attr, buf, count, false);
6864}
6865
6866static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba *hba)
6867{
6868 hba->spm_lvl_attr.show = ufshcd_spm_lvl_show;
6869 hba->spm_lvl_attr.store = ufshcd_spm_lvl_store;
6870 sysfs_attr_init(&hba->spm_lvl_attr.attr);
6871 hba->spm_lvl_attr.attr.name = "spm_lvl";
6872 hba->spm_lvl_attr.attr.mode = 0644;
6873 if (device_create_file(hba->dev, &hba->spm_lvl_attr))
6874 dev_err(hba->dev, "Failed to create sysfs for spm_lvl\n");
6875}
6876
6877static inline void ufshcd_add_sysfs_nodes(struct ufs_hba *hba)
6878{
6879 ufshcd_add_rpm_lvl_sysfs_nodes(hba);
6880 ufshcd_add_spm_lvl_sysfs_nodes(hba);
6881}
6882
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306883/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006884 * ufshcd_shutdown - shutdown routine
6885 * @hba: per adapter instance
6886 *
6887 * This function would power off both UFS device and UFS link.
6888 *
6889 * Returns 0 always to allow force shutdown even in case of errors.
6890 */
6891int ufshcd_shutdown(struct ufs_hba *hba)
6892{
6893 int ret = 0;
6894
6895 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6896 goto out;
6897
6898 if (pm_runtime_suspended(hba->dev)) {
6899 ret = ufshcd_runtime_resume(hba);
6900 if (ret)
6901 goto out;
6902 }
6903
6904 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6905out:
6906 if (ret)
6907 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6908 /* allow force shutdown even in case of errors */
6909 return 0;
6910}
6911EXPORT_SYMBOL(ufshcd_shutdown);
6912
6913/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306914 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306915 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306916 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306917 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306918void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306919{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306920 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306921 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306922 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006923 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306924
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006925 ufshcd_exit_clk_gating(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08006926 if (ufshcd_is_clkscaling_supported(hba))
6927 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006928 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306929}
6930EXPORT_SYMBOL_GPL(ufshcd_remove);
6931
6932/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006933 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6934 * @hba: pointer to Host Bus Adapter (HBA)
6935 */
6936void ufshcd_dealloc_host(struct ufs_hba *hba)
6937{
6938 scsi_host_put(hba->host);
6939}
6940EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6941
6942/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006943 * ufshcd_set_dma_mask - Set dma mask based on the controller
6944 * addressing capability
6945 * @hba: per adapter instance
6946 *
6947 * Returns 0 for success, non-zero for failure
6948 */
6949static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6950{
6951 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6952 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6953 return 0;
6954 }
6955 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6956}
6957
6958/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006959 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306960 * @dev: pointer to device handle
6961 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306962 * Returns 0 on success, non-zero value on failure
6963 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006964int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306965{
6966 struct Scsi_Host *host;
6967 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006968 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306969
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306970 if (!dev) {
6971 dev_err(dev,
6972 "Invalid memory reference for dev is NULL\n");
6973 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306974 goto out_error;
6975 }
6976
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306977 host = scsi_host_alloc(&ufshcd_driver_template,
6978 sizeof(struct ufs_hba));
6979 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306980 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306981 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306982 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306983 }
6984 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306985 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306986 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006987 *hba_handle = hba;
6988
6989out_error:
6990 return err;
6991}
6992EXPORT_SYMBOL(ufshcd_alloc_host);
6993
Sahitya Tummala856b3482014-09-25 15:32:34 +03006994static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6995{
6996 int ret = 0;
6997 struct ufs_clk_info *clki;
6998 struct list_head *head = &hba->clk_list_head;
6999
7000 if (!head || list_empty(head))
7001 goto out;
7002
Yaniv Gardif06fcc72015-10-28 13:15:51 +02007003 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
7004 if (ret)
7005 return ret;
7006
Sahitya Tummala856b3482014-09-25 15:32:34 +03007007 list_for_each_entry(clki, head, list) {
7008 if (!IS_ERR_OR_NULL(clki->clk)) {
7009 if (scale_up && clki->max_freq) {
7010 if (clki->curr_freq == clki->max_freq)
7011 continue;
7012 ret = clk_set_rate(clki->clk, clki->max_freq);
7013 if (ret) {
7014 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7015 __func__, clki->name,
7016 clki->max_freq, ret);
7017 break;
7018 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007019 trace_ufshcd_clk_scaling(dev_name(hba->dev),
7020 "scaled up", clki->name,
7021 clki->curr_freq,
7022 clki->max_freq);
7023
Sahitya Tummala856b3482014-09-25 15:32:34 +03007024 clki->curr_freq = clki->max_freq;
7025
7026 } else if (!scale_up && clki->min_freq) {
7027 if (clki->curr_freq == clki->min_freq)
7028 continue;
7029 ret = clk_set_rate(clki->clk, clki->min_freq);
7030 if (ret) {
7031 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
7032 __func__, clki->name,
7033 clki->min_freq, ret);
7034 break;
7035 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08007036 trace_ufshcd_clk_scaling(dev_name(hba->dev),
7037 "scaled down", clki->name,
7038 clki->curr_freq,
7039 clki->min_freq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007040 clki->curr_freq = clki->min_freq;
7041 }
7042 }
7043 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
7044 clki->name, clk_get_rate(clki->clk));
7045 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02007046
7047 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
7048
Sahitya Tummala856b3482014-09-25 15:32:34 +03007049out:
7050 return ret;
7051}
7052
7053static int ufshcd_devfreq_target(struct device *dev,
7054 unsigned long *freq, u32 flags)
7055{
7056 int err = 0;
7057 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07007058 bool release_clk_hold = false;
7059 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03007060
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007061 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03007062 return -EINVAL;
7063
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07007064 spin_lock_irqsave(hba->host->host_lock, irq_flags);
7065 if (ufshcd_eh_in_progress(hba)) {
7066 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
7067 return 0;
7068 }
7069
7070 if (ufshcd_is_clkgating_allowed(hba) &&
7071 (hba->clk_gating.state != CLKS_ON)) {
7072 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
7073 /* hold the vote until the scaling work is completed */
7074 hba->clk_gating.active_reqs++;
7075 release_clk_hold = true;
7076 hba->clk_gating.state = CLKS_ON;
7077 } else {
7078 /*
7079 * Clock gating work seems to be running in parallel
7080 * hence skip scaling work to avoid deadlock between
7081 * current scaling work and gating work.
7082 */
7083 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
7084 return 0;
7085 }
7086 }
7087 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
7088
Sahitya Tummala856b3482014-09-25 15:32:34 +03007089 if (*freq == UINT_MAX)
7090 err = ufshcd_scale_clks(hba, true);
7091 else if (*freq == 0)
7092 err = ufshcd_scale_clks(hba, false);
7093
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07007094 spin_lock_irqsave(hba->host->host_lock, irq_flags);
7095 if (release_clk_hold)
7096 __ufshcd_release(hba);
7097 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
7098
Sahitya Tummala856b3482014-09-25 15:32:34 +03007099 return err;
7100}
7101
7102static int ufshcd_devfreq_get_dev_status(struct device *dev,
7103 struct devfreq_dev_status *stat)
7104{
7105 struct ufs_hba *hba = dev_get_drvdata(dev);
7106 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
7107 unsigned long flags;
7108
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007109 if (!ufshcd_is_clkscaling_supported(hba))
Sahitya Tummala856b3482014-09-25 15:32:34 +03007110 return -EINVAL;
7111
7112 memset(stat, 0, sizeof(*stat));
7113
7114 spin_lock_irqsave(hba->host->host_lock, flags);
7115 if (!scaling->window_start_t)
7116 goto start_window;
7117
7118 if (scaling->is_busy_started)
7119 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
7120 scaling->busy_start_t));
7121
7122 stat->total_time = jiffies_to_usecs((long)jiffies -
7123 (long)scaling->window_start_t);
7124 stat->busy_time = scaling->tot_busy_t;
7125start_window:
7126 scaling->window_start_t = jiffies;
7127 scaling->tot_busy_t = 0;
7128
7129 if (hba->outstanding_reqs) {
7130 scaling->busy_start_t = ktime_get();
7131 scaling->is_busy_started = true;
7132 } else {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01007133 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03007134 scaling->is_busy_started = false;
7135 }
7136 spin_unlock_irqrestore(hba->host->host_lock, flags);
7137 return 0;
7138}
7139
7140static struct devfreq_dev_profile ufs_devfreq_profile = {
7141 .polling_ms = 100,
7142 .target = ufshcd_devfreq_target,
7143 .get_dev_status = ufshcd_devfreq_get_dev_status,
7144};
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007145static void ufshcd_clkscaling_init_sysfs(struct ufs_hba *hba)
7146{
7147 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show;
7148 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store;
7149 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr);
7150 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable";
7151 hba->clk_scaling.enable_attr.attr.mode = 0644;
7152 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr))
7153 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n");
7154}
Sahitya Tummala856b3482014-09-25 15:32:34 +03007155
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007156/**
7157 * ufshcd_init - Driver initialization routine
7158 * @hba: per-adapter instance
7159 * @mmio_base: base register address
7160 * @irq: Interrupt line of device
7161 * Returns 0 on success, non-zero value on failure
7162 */
7163int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
7164{
7165 int err;
7166 struct Scsi_Host *host = hba->host;
7167 struct device *dev = hba->dev;
7168
7169 if (!mmio_base) {
7170 dev_err(hba->dev,
7171 "Invalid memory reference for mmio_base is NULL\n");
7172 err = -ENODEV;
7173 goto out_error;
7174 }
7175
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307176 hba->mmio_base = mmio_base;
7177 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307178
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007179 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03007180 if (err)
7181 goto out_error;
7182
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307183 /* Read capabilities registers */
7184 ufshcd_hba_capabilities(hba);
7185
7186 /* Get UFS version supported by the controller */
7187 hba->ufs_version = ufshcd_get_ufs_version(hba);
7188
Yaniv Gardic01848c2016-12-05 19:25:02 -08007189 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
7190 (hba->ufs_version != UFSHCI_VERSION_11) &&
7191 (hba->ufs_version != UFSHCI_VERSION_20) &&
7192 (hba->ufs_version != UFSHCI_VERSION_21))
7193 dev_err(hba->dev, "invalid UFS version 0x%x\n",
7194 hba->ufs_version);
7195
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05307196 /* Get Interrupt bit mask per version */
7197 hba->intr_mask = ufshcd_get_intr_mask(hba);
7198
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09007199 err = ufshcd_set_dma_mask(hba);
7200 if (err) {
7201 dev_err(hba->dev, "set dma mask failed\n");
7202 goto out_disable;
7203 }
7204
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307205 /* Allocate memory for host memory space */
7206 err = ufshcd_memory_alloc(hba);
7207 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307208 dev_err(hba->dev, "Memory allocation failed\n");
7209 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307210 }
7211
7212 /* Configure LRB */
7213 ufshcd_host_memory_configure(hba);
7214
7215 host->can_queue = hba->nutrs;
7216 host->cmd_per_lun = hba->nutrs;
7217 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03007218 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307219 host->max_channel = UFSHCD_MAX_CHANNEL;
7220 host->unique_id = host->host_no;
7221 host->max_cmd_len = MAX_CDB_SIZE;
7222
Dolev Raviv7eb584d2014-09-25 15:32:31 +03007223 hba->max_pwr_info.is_valid = false;
7224
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307225 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05307226 init_waitqueue_head(&hba->tm_wq);
7227 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307228
7229 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05307230 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05307231 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307232
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307233 /* Initialize UIC command mutex */
7234 mutex_init(&hba->uic_cmd_mutex);
7235
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05307236 /* Initialize mutex for device management commands */
7237 mutex_init(&hba->dev_cmd.lock);
7238
7239 /* Initialize device management tag acquire wait queue */
7240 init_waitqueue_head(&hba->dev_cmd.tag_wq);
7241
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007242 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02007243
7244 /*
7245 * In order to avoid any spurious interrupt immediately after
7246 * registering UFS controller interrupt handler, clear any pending UFS
7247 * interrupt status and disable all the UFS interrupts.
7248 */
7249 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
7250 REG_INTERRUPT_STATUS);
7251 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
7252 /*
7253 * Make sure that UFS interrupts are disabled and any pending interrupt
7254 * status is cleared before registering UFS interrupt handler.
7255 */
7256 mb();
7257
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307258 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09007259 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307260 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307261 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007262 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007263 } else {
7264 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307265 }
7266
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307267 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307268 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307269 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007270 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307271 }
7272
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307273 /* Host controller enable */
7274 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307275 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307276 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08007277 ufshcd_print_host_regs(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307278 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307279 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307280
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007281 if (ufshcd_is_clkscaling_supported(hba)) {
Chanwoo Choi4861ee12016-11-08 18:13:28 +09007282 hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile,
Sahitya Tummala856b3482014-09-25 15:32:34 +03007283 "simple_ondemand", NULL);
7284 if (IS_ERR(hba->devfreq)) {
7285 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
7286 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00007287 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007288 goto out_remove_scsi_host;
7289 }
7290 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07007291 ufshcd_suspend_clkscaling(hba);
Sahitya Tummalafcb0c4b2016-12-22 18:40:50 -08007292 ufshcd_clkscaling_init_sysfs(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007293 }
7294
subhashj@codeaurora.org0c8f7582016-12-22 18:41:11 -08007295 /*
7296 * Set the default power management level for runtime and system PM.
7297 * Default power saving mode is to keep UFS link in Hibern8 state
7298 * and UFS device in sleep state.
7299 */
7300 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7301 UFS_SLEEP_PWR_MODE,
7302 UIC_LINK_HIBERN8_STATE);
7303 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state(
7304 UFS_SLEEP_PWR_MODE,
7305 UIC_LINK_HIBERN8_STATE);
7306
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05307307 /* Hold auto suspend until async scan completes */
7308 pm_runtime_get_sync(dev);
7309
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007310 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007311 * We are assuming that device wasn't put in sleep/power-down
7312 * state exclusively during the boot stage before kernel.
7313 * This assumption helps avoid doing link startup twice during
7314 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007315 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007316 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007317
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307318 async_schedule(ufshcd_async_scan, hba);
subhashj@codeaurora.org09690d52016-12-22 18:41:00 -08007319 ufshcd_add_sysfs_nodes(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307320
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307321 return 0;
7322
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307323out_remove_scsi_host:
7324 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007325exit_gating:
7326 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307327out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007328 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007329 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307330out_error:
7331 return err;
7332}
7333EXPORT_SYMBOL_GPL(ufshcd_init);
7334
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307335MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7336MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05307337MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307338MODULE_LICENSE("GPL");
7339MODULE_VERSION(UFSHCD_DRIVER_VERSION);