blob: a6a4a4302ca99efabf9ae886badff7a76843e2cb [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
Subhash Jadavani56d4a182016-12-05 19:25:32 -0800194static struct ufs_dev_fix ufs_fixups[] = {
195 /* UFS cards deviations table */
196 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
197 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
198 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
199 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
200 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
201 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
202 UFS_DEVICE_NO_FASTAUTO),
203 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
204 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
205 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
206 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
207 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
208 UFS_DEVICE_QUIRK_PA_TACTIVATE),
209 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
210 UFS_DEVICE_QUIRK_PA_TACTIVATE),
211 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
212 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
213 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
214
215 END_FIX
216};
217
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530218static void ufshcd_tmc_handler(struct ufs_hba *hba);
219static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530220static int ufshcd_reset_and_restore(struct ufs_hba *hba);
Dolev Ravive7d38252016-12-22 18:40:07 -0800221static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530222static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300223static void ufshcd_hba_exit(struct ufs_hba *hba);
224static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300225static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
226 bool skip_ref_clk);
227static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200228static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300229static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
230static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300231static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300232static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
233static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300234static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
235 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300236static int ufshcd_change_power_mode(struct ufs_hba *hba,
237 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200238static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
239{
240 return tag >= 0 && tag < hba->nutrs;
241}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300242
243static inline int ufshcd_enable_irq(struct ufs_hba *hba)
244{
245 int ret = 0;
246
247 if (!hba->is_irq_enabled) {
248 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
249 hba);
250 if (ret)
251 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
252 __func__, ret);
253 hba->is_irq_enabled = true;
254 }
255
256 return ret;
257}
258
259static inline void ufshcd_disable_irq(struct ufs_hba *hba)
260{
261 if (hba->is_irq_enabled) {
262 free_irq(hba->irq, hba);
263 hba->is_irq_enabled = false;
264 }
265}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530266
Yaniv Gardib573d482016-03-10 17:37:09 +0200267/* replace non-printable or non-ASCII characters with spaces */
268static inline void ufshcd_remove_non_printable(char *val)
269{
270 if (!val)
271 return;
272
273 if (*val < 0x20 || *val > 0x7e)
274 *val = ' ';
275}
276
Dolev Raviv66cc8202016-12-22 18:39:42 -0800277static void ufshcd_print_host_regs(struct ufs_hba *hba)
278{
279 /*
280 * hex_dump reads its data without the readl macro. This might
281 * cause inconsistency issues on some platform, as the printed
282 * values may be from cache and not the most recent value.
283 * To know whether you are looking at an un-cached version verify
284 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
285 * during platform/pci probe function.
286 */
287 ufshcd_hex_dump("host regs: ", hba->mmio_base, UFSHCI_REG_SPACE_SIZE);
288 dev_err(hba->dev, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
289 hba->ufs_version, hba->capabilities);
290 dev_err(hba->dev,
291 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
292 (u32)hba->outstanding_reqs, (u32)hba->outstanding_tasks);
293}
294
295static
296void ufshcd_print_trs(struct ufs_hba *hba, unsigned long bitmap, bool pr_prdt)
297{
298 struct ufshcd_lrb *lrbp;
299 int tag;
300
301 for_each_set_bit(tag, &bitmap, hba->nutrs) {
302 lrbp = &hba->lrb[tag];
303
304 dev_err(hba->dev, "UPIU[%d] - Transfer Request Descriptor\n",
305 tag);
306 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr,
307 sizeof(struct utp_transfer_req_desc));
308 dev_err(hba->dev, "UPIU[%d] - Request UPIU\n", tag);
309 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr,
310 sizeof(struct utp_upiu_req));
311 dev_err(hba->dev, "UPIU[%d] - Response UPIU\n", tag);
312 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr,
313 sizeof(struct utp_upiu_rsp));
314 if (pr_prdt) {
315 int prdt_length = le16_to_cpu(
316 lrbp->utr_descriptor_ptr->prd_table_length);
317
318 dev_err(hba->dev, "UPIU[%d] - PRDT - %d entries\n", tag,
319 prdt_length);
320 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr,
321 sizeof(struct ufshcd_sg_entry) *
322 prdt_length);
323 }
324 }
325}
326
327static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap)
328{
329 struct utp_task_req_desc *tmrdp;
330 int tag;
331
332 for_each_set_bit(tag, &bitmap, hba->nutmrs) {
333 tmrdp = &hba->utmrdl_base_addr[tag];
334 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag);
335 ufshcd_hex_dump("TM TRD: ", &tmrdp->header,
336 sizeof(struct request_desc_header));
337 dev_err(hba->dev, "TM[%d] - Task Management Request UPIU\n",
338 tag);
339 ufshcd_hex_dump("TM REQ: ", tmrdp->task_req_upiu,
340 sizeof(struct utp_upiu_req));
341 dev_err(hba->dev, "TM[%d] - Task Management Response UPIU\n",
342 tag);
343 ufshcd_hex_dump("TM RSP: ", tmrdp->task_rsp_upiu,
344 sizeof(struct utp_task_req_desc));
345 }
346}
347
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530348/*
349 * ufshcd_wait_for_register - wait for register value to change
350 * @hba - per-adapter interface
351 * @reg - mmio register offset
352 * @mask - mask to apply to read register value
353 * @val - wait condition
354 * @interval_us - polling interval in microsecs
355 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200356 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530357 *
358 * Returns -ETIMEDOUT on error, zero on success
359 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200360int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
361 u32 val, unsigned long interval_us,
362 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530363{
364 int err = 0;
365 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
366
367 /* ignore bits that we don't intend to wait on */
368 val = val & mask;
369
370 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200371 if (can_sleep)
372 usleep_range(interval_us, interval_us + 50);
373 else
374 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530375 if (time_after(jiffies, timeout)) {
376 if ((ufshcd_readl(hba, reg) & mask) != val)
377 err = -ETIMEDOUT;
378 break;
379 }
380 }
381
382 return err;
383}
384
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530385/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530386 * ufshcd_get_intr_mask - Get the interrupt bit mask
387 * @hba - Pointer to adapter instance
388 *
389 * Returns interrupt bit mask per version
390 */
391static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
392{
Yaniv Gardic01848c2016-12-05 19:25:02 -0800393 u32 intr_mask = 0;
394
395 switch (hba->ufs_version) {
396 case UFSHCI_VERSION_10:
397 intr_mask = INTERRUPT_MASK_ALL_VER_10;
398 break;
399 /* allow fall through */
400 case UFSHCI_VERSION_11:
401 case UFSHCI_VERSION_20:
402 intr_mask = INTERRUPT_MASK_ALL_VER_11;
403 break;
404 /* allow fall through */
405 case UFSHCI_VERSION_21:
406 default:
407 intr_mask = INTERRUPT_MASK_ALL_VER_21;
408 }
409
410 return intr_mask;
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530411}
412
413/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530414 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
415 * @hba - Pointer to adapter instance
416 *
417 * Returns UFSHCI version supported by the controller
418 */
419static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
420{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200421 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
422 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300423
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530424 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530425}
426
427/**
428 * ufshcd_is_device_present - Check if any device connected to
429 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300430 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530431 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530432 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530433 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300434static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530435{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300436 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
437 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530438}
439
440/**
441 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
442 * @lrb: pointer to local command reference block
443 *
444 * This function is used to get the OCS field from UTRD
445 * Returns the OCS field in the UTRD
446 */
447static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
448{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530449 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530450}
451
452/**
453 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
454 * @task_req_descp: pointer to utp_task_req_desc structure
455 *
456 * This function is used to get the OCS field from UTMRD
457 * Returns the OCS field in the UTMRD
458 */
459static inline int
460ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
461{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530462 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530463}
464
465/**
466 * ufshcd_get_tm_free_slot - get a free slot for task management request
467 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530468 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530469 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530470 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
471 * Returns 0 if free slot is not available, else return 1 with tag value
472 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530473 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530474static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530475{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530476 int tag;
477 bool ret = false;
478
479 if (!free_slot)
480 goto out;
481
482 do {
483 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
484 if (tag >= hba->nutmrs)
485 goto out;
486 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
487
488 *free_slot = tag;
489 ret = true;
490out:
491 return ret;
492}
493
494static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
495{
496 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530497}
498
499/**
500 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
501 * @hba: per adapter instance
502 * @pos: position of the bit to be cleared
503 */
504static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
505{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530506 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530507}
508
509/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200510 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
511 * @hba: per adapter instance
512 * @tag: position of the bit to be cleared
513 */
514static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
515{
516 __clear_bit(tag, &hba->outstanding_reqs);
517}
518
519/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530520 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
521 * @reg: Register value of host controller status
522 *
523 * Returns integer, 0 on Success and positive value if failed
524 */
525static inline int ufshcd_get_lists_status(u32 reg)
526{
527 /*
528 * The mask 0xFF is for the following HCS register bits
529 * Bit Description
530 * 0 Device Present
531 * 1 UTRLRDY
532 * 2 UTMRLRDY
533 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200534 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530535 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200536 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530537}
538
539/**
540 * ufshcd_get_uic_cmd_result - Get the UIC command result
541 * @hba: Pointer to adapter instance
542 *
543 * This function gets the result of UIC command completion
544 * Returns 0 on success, non zero value on error
545 */
546static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
547{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530548 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530549 MASK_UIC_COMMAND_RESULT;
550}
551
552/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530553 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
554 * @hba: Pointer to adapter instance
555 *
556 * This function gets UIC command argument3
557 * Returns 0 on success, non zero value on error
558 */
559static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
560{
561 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
562}
563
564/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530565 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530566 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530567 */
568static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530569ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530570{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530571 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530572}
573
574/**
575 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
576 * @ucd_rsp_ptr: pointer to response UPIU
577 *
578 * This function gets the response status and scsi_status from response UPIU
579 * Returns the response result code.
580 */
581static inline int
582ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
583{
584 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
585}
586
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530587/*
588 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
589 * from response UPIU
590 * @ucd_rsp_ptr: pointer to response UPIU
591 *
592 * Return the data segment length.
593 */
594static inline unsigned int
595ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
596{
597 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
598 MASK_RSP_UPIU_DATA_SEG_LEN;
599}
600
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530601/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530602 * ufshcd_is_exception_event - Check if the device raised an exception event
603 * @ucd_rsp_ptr: pointer to response UPIU
604 *
605 * The function checks if the device raised an exception event indicated in
606 * the Device Information field of response UPIU.
607 *
608 * Returns true if exception is raised, false otherwise.
609 */
610static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
611{
612 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
613 MASK_RSP_EXCEPTION_EVENT ? true : false;
614}
615
616/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530617 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530618 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530619 */
620static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530621ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530622{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530623 ufshcd_writel(hba, INT_AGGR_ENABLE |
624 INT_AGGR_COUNTER_AND_TIMER_RESET,
625 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
626}
627
628/**
629 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
630 * @hba: per adapter instance
631 * @cnt: Interrupt aggregation counter threshold
632 * @tmout: Interrupt aggregation timeout value
633 */
634static inline void
635ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
636{
637 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
638 INT_AGGR_COUNTER_THLD_VAL(cnt) |
639 INT_AGGR_TIMEOUT_VAL(tmout),
640 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530641}
642
643/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300644 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
645 * @hba: per adapter instance
646 */
647static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
648{
649 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
650}
651
652/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530653 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
654 * When run-stop registers are set to 1, it indicates the
655 * host controller that it can process the requests
656 * @hba: per adapter instance
657 */
658static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
659{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530660 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
661 REG_UTP_TASK_REQ_LIST_RUN_STOP);
662 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
663 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530664}
665
666/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530667 * ufshcd_hba_start - Start controller initialization sequence
668 * @hba: per adapter instance
669 */
670static inline void ufshcd_hba_start(struct ufs_hba *hba)
671{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530672 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530673}
674
675/**
676 * ufshcd_is_hba_active - Get controller state
677 * @hba: per adapter instance
678 *
679 * Returns zero if controller is active, 1 otherwise
680 */
681static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
682{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530683 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530684}
685
Yaniv Gardi37113102016-03-10 17:37:16 +0200686u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
687{
688 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
689 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
690 (hba->ufs_version == UFSHCI_VERSION_11))
691 return UFS_UNIPRO_VER_1_41;
692 else
693 return UFS_UNIPRO_VER_1_6;
694}
695EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
696
697static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
698{
699 /*
700 * If both host and device support UniPro ver1.6 or later, PA layer
701 * parameters tuning happens during link startup itself.
702 *
703 * We can manually tune PA layer parameters if either host or device
704 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
705 * logic simple, we will only do manual tuning if local unipro version
706 * doesn't support ver1.6 or later.
707 */
708 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
709 return true;
710 else
711 return false;
712}
713
Gilad Bronera5082532016-10-17 17:10:00 -0700714static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
715{
716 if (ufshcd_is_clkscaling_enabled(hba)) {
717 devfreq_suspend_device(hba->devfreq);
718 hba->clk_scaling.window_start_t = 0;
719 }
720}
721
722static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
723{
724 if (ufshcd_is_clkscaling_enabled(hba))
725 devfreq_resume_device(hba->devfreq);
726}
727
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300728static void ufshcd_ungate_work(struct work_struct *work)
729{
730 int ret;
731 unsigned long flags;
732 struct ufs_hba *hba = container_of(work, struct ufs_hba,
733 clk_gating.ungate_work);
734
735 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
736
737 spin_lock_irqsave(hba->host->host_lock, flags);
738 if (hba->clk_gating.state == CLKS_ON) {
739 spin_unlock_irqrestore(hba->host->host_lock, flags);
740 goto unblock_reqs;
741 }
742
743 spin_unlock_irqrestore(hba->host->host_lock, flags);
744 ufshcd_setup_clocks(hba, true);
745
746 /* Exit from hibern8 */
747 if (ufshcd_can_hibern8_during_gating(hba)) {
748 /* Prevent gating in this path */
749 hba->clk_gating.is_suspended = true;
750 if (ufshcd_is_link_hibern8(hba)) {
751 ret = ufshcd_uic_hibern8_exit(hba);
752 if (ret)
753 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
754 __func__, ret);
755 else
756 ufshcd_set_link_active(hba);
757 }
758 hba->clk_gating.is_suspended = false;
759 }
760unblock_reqs:
Gilad Bronera5082532016-10-17 17:10:00 -0700761 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300762 scsi_unblock_requests(hba->host);
763}
764
765/**
766 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
767 * Also, exit from hibern8 mode and set the link as active.
768 * @hba: per adapter instance
769 * @async: This indicates whether caller should ungate clocks asynchronously.
770 */
771int ufshcd_hold(struct ufs_hba *hba, bool async)
772{
773 int rc = 0;
774 unsigned long flags;
775
776 if (!ufshcd_is_clkgating_allowed(hba))
777 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300778 spin_lock_irqsave(hba->host->host_lock, flags);
779 hba->clk_gating.active_reqs++;
780
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200781 if (ufshcd_eh_in_progress(hba)) {
782 spin_unlock_irqrestore(hba->host->host_lock, flags);
783 return 0;
784 }
785
Sahitya Tummala856b3482014-09-25 15:32:34 +0300786start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300787 switch (hba->clk_gating.state) {
788 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -0700789 /*
790 * Wait for the ungate work to complete if in progress.
791 * Though the clocks may be in ON state, the link could
792 * still be in hibner8 state if hibern8 is allowed
793 * during clock gating.
794 * Make sure we exit hibern8 state also in addition to
795 * clocks being ON.
796 */
797 if (ufshcd_can_hibern8_during_gating(hba) &&
798 ufshcd_is_link_hibern8(hba)) {
799 spin_unlock_irqrestore(hba->host->host_lock, flags);
800 flush_work(&hba->clk_gating.ungate_work);
801 spin_lock_irqsave(hba->host->host_lock, flags);
802 goto start;
803 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300804 break;
805 case REQ_CLKS_OFF:
806 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
807 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800808 trace_ufshcd_clk_gating(dev_name(hba->dev),
809 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300810 break;
811 }
812 /*
813 * If we here, it means gating work is either done or
814 * currently running. Hence, fall through to cancel gating
815 * work and to enable clocks.
816 */
817 case CLKS_OFF:
818 scsi_block_requests(hba->host);
819 hba->clk_gating.state = REQ_CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800820 trace_ufshcd_clk_gating(dev_name(hba->dev),
821 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300822 schedule_work(&hba->clk_gating.ungate_work);
823 /*
824 * fall through to check if we should wait for this
825 * work to be done or not.
826 */
827 case REQ_CLKS_ON:
828 if (async) {
829 rc = -EAGAIN;
830 hba->clk_gating.active_reqs--;
831 break;
832 }
833
834 spin_unlock_irqrestore(hba->host->host_lock, flags);
835 flush_work(&hba->clk_gating.ungate_work);
836 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300837 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300838 goto start;
839 default:
840 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
841 __func__, hba->clk_gating.state);
842 break;
843 }
844 spin_unlock_irqrestore(hba->host->host_lock, flags);
845out:
846 return rc;
847}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200848EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300849
850static void ufshcd_gate_work(struct work_struct *work)
851{
852 struct ufs_hba *hba = container_of(work, struct ufs_hba,
853 clk_gating.gate_work.work);
854 unsigned long flags;
855
856 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -0700857 /*
858 * In case you are here to cancel this work the gating state
859 * would be marked as REQ_CLKS_ON. In this case save time by
860 * skipping the gating work and exit after changing the clock
861 * state to CLKS_ON.
862 */
863 if (hba->clk_gating.is_suspended ||
864 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300865 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800866 trace_ufshcd_clk_gating(dev_name(hba->dev),
867 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300868 goto rel_lock;
869 }
870
871 if (hba->clk_gating.active_reqs
872 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
873 || hba->lrb_in_use || hba->outstanding_tasks
874 || hba->active_uic_cmd || hba->uic_async_done)
875 goto rel_lock;
876
877 spin_unlock_irqrestore(hba->host->host_lock, flags);
878
879 /* put the link into hibern8 mode before turning off clocks */
880 if (ufshcd_can_hibern8_during_gating(hba)) {
881 if (ufshcd_uic_hibern8_enter(hba)) {
882 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800883 trace_ufshcd_clk_gating(dev_name(hba->dev),
884 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300885 goto out;
886 }
887 ufshcd_set_link_hibern8(hba);
888 }
889
Gilad Bronera5082532016-10-17 17:10:00 -0700890 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +0300891
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300892 if (!ufshcd_is_link_active(hba))
893 ufshcd_setup_clocks(hba, false);
894 else
895 /* If link is active, device ref_clk can't be switched off */
896 __ufshcd_setup_clocks(hba, false, true);
897
898 /*
899 * In case you are here to cancel this work the gating state
900 * would be marked as REQ_CLKS_ON. In this case keep the state
901 * as REQ_CLKS_ON which would anyway imply that clocks are off
902 * and a request to turn them on is pending. By doing this way,
903 * we keep the state machine in tact and this would ultimately
904 * prevent from doing cancel work multiple times when there are
905 * new requests arriving before the current cancel work is done.
906 */
907 spin_lock_irqsave(hba->host->host_lock, flags);
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800908 if (hba->clk_gating.state == REQ_CLKS_OFF) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300909 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800910 trace_ufshcd_clk_gating(dev_name(hba->dev),
911 hba->clk_gating.state);
912 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300913rel_lock:
914 spin_unlock_irqrestore(hba->host->host_lock, flags);
915out:
916 return;
917}
918
919/* host lock must be held before calling this variant */
920static void __ufshcd_release(struct ufs_hba *hba)
921{
922 if (!ufshcd_is_clkgating_allowed(hba))
923 return;
924
925 hba->clk_gating.active_reqs--;
926
927 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
928 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
929 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200930 || hba->active_uic_cmd || hba->uic_async_done
931 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300932 return;
933
934 hba->clk_gating.state = REQ_CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -0800935 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300936 schedule_delayed_work(&hba->clk_gating.gate_work,
937 msecs_to_jiffies(hba->clk_gating.delay_ms));
938}
939
940void ufshcd_release(struct ufs_hba *hba)
941{
942 unsigned long flags;
943
944 spin_lock_irqsave(hba->host->host_lock, flags);
945 __ufshcd_release(hba);
946 spin_unlock_irqrestore(hba->host->host_lock, flags);
947}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200948EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300949
950static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
951 struct device_attribute *attr, char *buf)
952{
953 struct ufs_hba *hba = dev_get_drvdata(dev);
954
955 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
956}
957
958static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
959 struct device_attribute *attr, const char *buf, size_t count)
960{
961 struct ufs_hba *hba = dev_get_drvdata(dev);
962 unsigned long flags, value;
963
964 if (kstrtoul(buf, 0, &value))
965 return -EINVAL;
966
967 spin_lock_irqsave(hba->host->host_lock, flags);
968 hba->clk_gating.delay_ms = value;
969 spin_unlock_irqrestore(hba->host->host_lock, flags);
970 return count;
971}
972
973static void ufshcd_init_clk_gating(struct ufs_hba *hba)
974{
975 if (!ufshcd_is_clkgating_allowed(hba))
976 return;
977
978 hba->clk_gating.delay_ms = 150;
979 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
980 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
981
982 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
983 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
984 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
985 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
986 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
987 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
988 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
989}
990
991static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
992{
993 if (!ufshcd_is_clkgating_allowed(hba))
994 return;
995 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900996 cancel_work_sync(&hba->clk_gating.ungate_work);
997 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300998}
999
Sahitya Tummala856b3482014-09-25 15:32:34 +03001000/* Must be called with host lock acquired */
1001static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
1002{
1003 if (!ufshcd_is_clkscaling_enabled(hba))
1004 return;
1005
1006 if (!hba->clk_scaling.is_busy_started) {
1007 hba->clk_scaling.busy_start_t = ktime_get();
1008 hba->clk_scaling.is_busy_started = true;
1009 }
1010}
1011
1012static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
1013{
1014 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
1015
1016 if (!ufshcd_is_clkscaling_enabled(hba))
1017 return;
1018
1019 if (!hba->outstanding_reqs && scaling->is_busy_started) {
1020 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
1021 scaling->busy_start_t));
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01001022 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03001023 scaling->is_busy_started = false;
1024 }
1025}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301026/**
1027 * ufshcd_send_command - Send SCSI or device management commands
1028 * @hba: per adapter instance
1029 * @task_tag: Task tag of the command
1030 */
1031static inline
1032void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
1033{
Sahitya Tummala856b3482014-09-25 15:32:34 +03001034 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301035 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301036 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001037 /* Make sure that doorbell is committed immediately */
1038 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301039}
1040
1041/**
1042 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1043 * @lrb - pointer to local reference block
1044 */
1045static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
1046{
1047 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05301048 if (lrbp->sense_buffer &&
1049 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001050 int len_to_copy;
1051
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301052 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07001053 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
1054
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301055 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301056 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001057 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301058 }
1059}
1060
1061/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301062 * ufshcd_copy_query_response() - Copy the Query Response and the data
1063 * descriptor
1064 * @hba: per adapter instance
1065 * @lrb - pointer to local reference block
1066 */
1067static
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001068int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +05301069{
1070 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1071
Dolev Raviv68078d52013-07-30 00:35:58 +05301072 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301073
Dolev Raviv68078d52013-07-30 00:35:58 +05301074 /* Get the descriptor */
1075 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001076 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +05301077 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001078 u16 resp_len;
1079 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +05301080
1081 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001082 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301083 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001084 buf_len = be16_to_cpu(
1085 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001086 if (likely(buf_len >= resp_len)) {
1087 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
1088 } else {
1089 dev_warn(hba->dev,
1090 "%s: Response size is bigger than buffer",
1091 __func__);
1092 return -EINVAL;
1093 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301094 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001095
1096 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301097}
1098
1099/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301100 * ufshcd_hba_capabilities - Read controller capabilities
1101 * @hba: per adapter instance
1102 */
1103static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
1104{
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301105 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301106
1107 /* nutrs and nutmrs are 0 based values */
1108 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
1109 hba->nutmrs =
1110 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
1111}
1112
1113/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301114 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1115 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301116 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301117 * Return true on success, else false
1118 */
1119static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
1120{
1121 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1122 return true;
1123 else
1124 return false;
1125}
1126
1127/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301128 * ufshcd_get_upmcrs - Get the power mode change request status
1129 * @hba: Pointer to adapter instance
1130 *
1131 * This function gets the UPMCRS field of HCS register
1132 * Returns value of UPMCRS field
1133 */
1134static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1135{
1136 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1137}
1138
1139/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301140 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1141 * @hba: per adapter instance
1142 * @uic_cmd: UIC command
1143 *
1144 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301145 */
1146static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301147ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301148{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301149 WARN_ON(hba->active_uic_cmd);
1150
1151 hba->active_uic_cmd = uic_cmd;
1152
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301153 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301154 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1155 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1156 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301157
1158 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301159 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301160 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301161}
1162
1163/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301164 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1165 * @hba: per adapter instance
1166 * @uic_command: UIC command
1167 *
1168 * Must be called with mutex held.
1169 * Returns 0 only if success.
1170 */
1171static int
1172ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1173{
1174 int ret;
1175 unsigned long flags;
1176
1177 if (wait_for_completion_timeout(&uic_cmd->done,
1178 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1179 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1180 else
1181 ret = -ETIMEDOUT;
1182
1183 spin_lock_irqsave(hba->host->host_lock, flags);
1184 hba->active_uic_cmd = NULL;
1185 spin_unlock_irqrestore(hba->host->host_lock, flags);
1186
1187 return ret;
1188}
1189
1190/**
1191 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1192 * @hba: per adapter instance
1193 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001194 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301195 *
1196 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001197 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301198 * Returns 0 only if success.
1199 */
1200static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001201__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1202 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301203{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301204 if (!ufshcd_ready_for_uic_cmd(hba)) {
1205 dev_err(hba->dev,
1206 "Controller not ready to accept UIC commands\n");
1207 return -EIO;
1208 }
1209
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001210 if (completion)
1211 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301212
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301213 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301214
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001215 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301216}
1217
1218/**
1219 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1220 * @hba: per adapter instance
1221 * @uic_cmd: UIC command
1222 *
1223 * Returns 0 only if success.
1224 */
1225static int
1226ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1227{
1228 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001229 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301230
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001231 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301232 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001233 ufshcd_add_delay_before_dme_cmd(hba);
1234
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001235 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001236 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001237 spin_unlock_irqrestore(hba->host->host_lock, flags);
1238 if (!ret)
1239 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1240
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301241 mutex_unlock(&hba->uic_cmd_mutex);
1242
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001243 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301244 return ret;
1245}
1246
1247/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301248 * ufshcd_map_sg - Map scatter-gather list to prdt
1249 * @lrbp - pointer to local reference block
1250 *
1251 * Returns 0 in case of success, non-zero value in case of failure
1252 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001253static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301254{
1255 struct ufshcd_sg_entry *prd_table;
1256 struct scatterlist *sg;
1257 struct scsi_cmnd *cmd;
1258 int sg_segments;
1259 int i;
1260
1261 cmd = lrbp->cmd;
1262 sg_segments = scsi_dma_map(cmd);
1263 if (sg_segments < 0)
1264 return sg_segments;
1265
1266 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001267 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1268 lrbp->utr_descriptor_ptr->prd_table_length =
1269 cpu_to_le16((u16)(sg_segments *
1270 sizeof(struct ufshcd_sg_entry)));
1271 else
1272 lrbp->utr_descriptor_ptr->prd_table_length =
1273 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301274
1275 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1276
1277 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1278 prd_table[i].size =
1279 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1280 prd_table[i].base_addr =
1281 cpu_to_le32(lower_32_bits(sg->dma_address));
1282 prd_table[i].upper_addr =
1283 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001284 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301285 }
1286 } else {
1287 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1288 }
1289
1290 return 0;
1291}
1292
1293/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301294 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301295 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301296 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301297 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301298static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301299{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301300 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1301
1302 if (hba->ufs_version == UFSHCI_VERSION_10) {
1303 u32 rw;
1304 rw = set & INTERRUPT_MASK_RW_VER_10;
1305 set = rw | ((set ^ intrs) & intrs);
1306 } else {
1307 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301308 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301309
1310 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1311}
1312
1313/**
1314 * ufshcd_disable_intr - disable interrupts
1315 * @hba: per adapter instance
1316 * @intrs: interrupt bits
1317 */
1318static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1319{
1320 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1321
1322 if (hba->ufs_version == UFSHCI_VERSION_10) {
1323 u32 rw;
1324 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1325 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1326 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1327
1328 } else {
1329 set &= ~intrs;
1330 }
1331
1332 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301333}
1334
1335/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301336 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1337 * descriptor according to request
1338 * @lrbp: pointer to local reference block
1339 * @upiu_flags: flags required in the header
1340 * @cmd_dir: requests data direction
1341 */
1342static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001343 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301344{
1345 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1346 u32 data_direction;
1347 u32 dword_0;
1348
1349 if (cmd_dir == DMA_FROM_DEVICE) {
1350 data_direction = UTP_DEVICE_TO_HOST;
1351 *upiu_flags = UPIU_CMD_FLAGS_READ;
1352 } else if (cmd_dir == DMA_TO_DEVICE) {
1353 data_direction = UTP_HOST_TO_DEVICE;
1354 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1355 } else {
1356 data_direction = UTP_NO_DATA_TRANSFER;
1357 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1358 }
1359
1360 dword_0 = data_direction | (lrbp->command_type
1361 << UPIU_COMMAND_TYPE_OFFSET);
1362 if (lrbp->intr_cmd)
1363 dword_0 |= UTP_REQ_DESC_INT_CMD;
1364
1365 /* Transfer request descriptor header fields */
1366 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001367 /* dword_1 is reserved, hence it is set to 0 */
1368 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301369 /*
1370 * assigning invalid value for command status. Controller
1371 * updates OCS on command completion, with the command
1372 * status
1373 */
1374 req_desc->header.dword_2 =
1375 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001376 /* dword_3 is reserved, hence it is set to 0 */
1377 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001378
1379 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301380}
1381
1382/**
1383 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1384 * for scsi commands
1385 * @lrbp - local reference block pointer
1386 * @upiu_flags - flags
1387 */
1388static
1389void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1390{
1391 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001392 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301393
1394 /* command descriptor fields */
1395 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1396 UPIU_TRANSACTION_COMMAND, upiu_flags,
1397 lrbp->lun, lrbp->task_tag);
1398 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1399 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1400
1401 /* Total EHS length and Data segment length will be zero */
1402 ucd_req_ptr->header.dword_2 = 0;
1403
1404 ucd_req_ptr->sc.exp_data_transfer_len =
1405 cpu_to_be32(lrbp->cmd->sdb.length);
1406
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001407 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1408 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1409 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1410
1411 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301412}
1413
Dolev Raviv68078d52013-07-30 00:35:58 +05301414/**
1415 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1416 * for query requsts
1417 * @hba: UFS hba
1418 * @lrbp: local reference block pointer
1419 * @upiu_flags: flags
1420 */
1421static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1422 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1423{
1424 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1425 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301426 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301427 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1428
1429 /* Query request header */
1430 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1431 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1432 lrbp->lun, lrbp->task_tag);
1433 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1434 0, query->request.query_func, 0, 0);
1435
Zang Leigang68612852016-08-25 17:39:19 +08001436 /* Data segment length only need for WRITE_DESC */
1437 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1438 ucd_req_ptr->header.dword_2 =
1439 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1440 else
1441 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301442
1443 /* Copy the Query Request buffer as is */
1444 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1445 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301446
1447 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001448 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1449 memcpy(descp, query->descriptor, len);
1450
Yaniv Gardi51047262016-02-01 15:02:38 +02001451 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301452}
1453
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301454static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1455{
1456 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1457
1458 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1459
1460 /* command descriptor fields */
1461 ucd_req_ptr->header.dword_0 =
1462 UPIU_HEADER_DWORD(
1463 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001464 /* clear rest of the fields of basic header */
1465 ucd_req_ptr->header.dword_1 = 0;
1466 ucd_req_ptr->header.dword_2 = 0;
1467
1468 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301469}
1470
1471/**
Joao Pinto300bb132016-05-11 12:21:27 +01001472 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1473 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301474 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301475 * @lrb - pointer to local reference block
1476 */
Joao Pinto300bb132016-05-11 12:21:27 +01001477static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301478{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301479 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301480 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301481
Joao Pinto300bb132016-05-11 12:21:27 +01001482 if (hba->ufs_version == UFSHCI_VERSION_20)
1483 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1484 else
1485 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1486
1487 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1488 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1489 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1490 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1491 ufshcd_prepare_utp_nop_upiu(lrbp);
1492 else
1493 ret = -EINVAL;
1494
1495 return ret;
1496}
1497
1498/**
1499 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1500 * for SCSI Purposes
1501 * @hba - per adapter instance
1502 * @lrb - pointer to local reference block
1503 */
1504static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1505{
1506 u32 upiu_flags;
1507 int ret = 0;
1508
1509 if (hba->ufs_version == UFSHCI_VERSION_20)
1510 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1511 else
1512 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1513
1514 if (likely(lrbp->cmd)) {
1515 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1516 lrbp->cmd->sc_data_direction);
1517 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1518 } else {
1519 ret = -EINVAL;
1520 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301521
1522 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301523}
1524
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001525/*
1526 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1527 * @scsi_lun: scsi LUN id
1528 *
1529 * Returns UPIU LUN id
1530 */
1531static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1532{
1533 if (scsi_is_wlun(scsi_lun))
1534 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1535 | UFS_UPIU_WLUN_ID;
1536 else
1537 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1538}
1539
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301540/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001541 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1542 * @scsi_lun: UPIU W-LUN id
1543 *
1544 * Returns SCSI W-LUN id
1545 */
1546static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1547{
1548 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1549}
1550
1551/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301552 * ufshcd_queuecommand - main entry point for SCSI requests
1553 * @cmd: command from SCSI Midlayer
1554 * @done: call back function
1555 *
1556 * Returns 0 for success, non-zero in case of failure
1557 */
1558static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1559{
1560 struct ufshcd_lrb *lrbp;
1561 struct ufs_hba *hba;
1562 unsigned long flags;
1563 int tag;
1564 int err = 0;
1565
1566 hba = shost_priv(host);
1567
1568 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001569 if (!ufshcd_valid_tag(hba, tag)) {
1570 dev_err(hba->dev,
1571 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1572 __func__, tag, cmd, cmd->request);
1573 BUG();
1574 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301575
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301576 spin_lock_irqsave(hba->host->host_lock, flags);
1577 switch (hba->ufshcd_state) {
1578 case UFSHCD_STATE_OPERATIONAL:
1579 break;
Zang Leigang141f8162016-11-16 11:29:37 +08001580 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301581 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301582 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301583 goto out_unlock;
1584 case UFSHCD_STATE_ERROR:
1585 set_host_byte(cmd, DID_ERROR);
1586 cmd->scsi_done(cmd);
1587 goto out_unlock;
1588 default:
1589 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1590 __func__, hba->ufshcd_state);
1591 set_host_byte(cmd, DID_BAD_TARGET);
1592 cmd->scsi_done(cmd);
1593 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301594 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001595
1596 /* if error handling is in progress, don't issue commands */
1597 if (ufshcd_eh_in_progress(hba)) {
1598 set_host_byte(cmd, DID_ERROR);
1599 cmd->scsi_done(cmd);
1600 goto out_unlock;
1601 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301602 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301603
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301604 /* acquire the tag to make sure device cmds don't use it */
1605 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1606 /*
1607 * Dev manage command in progress, requeue the command.
1608 * Requeuing the command helps in cases where the request *may*
1609 * find different tag instead of waiting for dev manage command
1610 * completion.
1611 */
1612 err = SCSI_MLQUEUE_HOST_BUSY;
1613 goto out;
1614 }
1615
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001616 err = ufshcd_hold(hba, true);
1617 if (err) {
1618 err = SCSI_MLQUEUE_HOST_BUSY;
1619 clear_bit_unlock(tag, &hba->lrb_in_use);
1620 goto out;
1621 }
1622 WARN_ON(hba->clk_gating.state != CLKS_ON);
1623
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301624 lrbp = &hba->lrb[tag];
1625
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301626 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301627 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001628 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301629 lrbp->sense_buffer = cmd->sense_buffer;
1630 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001631 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001632 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301633
Joao Pinto300bb132016-05-11 12:21:27 +01001634 ufshcd_comp_scsi_upiu(hba, lrbp);
1635
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001636 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301637 if (err) {
1638 lrbp->cmd = NULL;
1639 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301640 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301641 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001642 /* Make sure descriptors are ready before ringing the doorbell */
1643 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301644
1645 /* issue command to the controller */
1646 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001647 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301648 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301649out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301650 spin_unlock_irqrestore(hba->host->host_lock, flags);
1651out:
1652 return err;
1653}
1654
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301655static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1656 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1657{
1658 lrbp->cmd = NULL;
1659 lrbp->sense_bufflen = 0;
1660 lrbp->sense_buffer = NULL;
1661 lrbp->task_tag = tag;
1662 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301663 lrbp->intr_cmd = true; /* No interrupt aggregation */
1664 hba->dev_cmd.type = cmd_type;
1665
Joao Pinto300bb132016-05-11 12:21:27 +01001666 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301667}
1668
1669static int
1670ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1671{
1672 int err = 0;
1673 unsigned long flags;
1674 u32 mask = 1 << tag;
1675
1676 /* clear outstanding transaction before retry */
1677 spin_lock_irqsave(hba->host->host_lock, flags);
1678 ufshcd_utrl_clear(hba, tag);
1679 spin_unlock_irqrestore(hba->host->host_lock, flags);
1680
1681 /*
1682 * wait for for h/w to clear corresponding bit in door-bell.
1683 * max. wait is 1 sec.
1684 */
1685 err = ufshcd_wait_for_register(hba,
1686 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001687 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301688
1689 return err;
1690}
1691
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001692static int
1693ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1694{
1695 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1696
1697 /* Get the UPIU response */
1698 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1699 UPIU_RSP_CODE_OFFSET;
1700 return query_res->response;
1701}
1702
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301703/**
1704 * ufshcd_dev_cmd_completion() - handles device management command responses
1705 * @hba: per adapter instance
1706 * @lrbp: pointer to local reference block
1707 */
1708static int
1709ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1710{
1711 int resp;
1712 int err = 0;
1713
1714 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1715
1716 switch (resp) {
1717 case UPIU_TRANSACTION_NOP_IN:
1718 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1719 err = -EINVAL;
1720 dev_err(hba->dev, "%s: unexpected response %x\n",
1721 __func__, resp);
1722 }
1723 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301724 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001725 err = ufshcd_check_query_response(hba, lrbp);
1726 if (!err)
1727 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301728 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301729 case UPIU_TRANSACTION_REJECT_UPIU:
1730 /* TODO: handle Reject UPIU Response */
1731 err = -EPERM;
1732 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1733 __func__);
1734 break;
1735 default:
1736 err = -EINVAL;
1737 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1738 __func__, resp);
1739 break;
1740 }
1741
1742 return err;
1743}
1744
1745static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1746 struct ufshcd_lrb *lrbp, int max_timeout)
1747{
1748 int err = 0;
1749 unsigned long time_left;
1750 unsigned long flags;
1751
1752 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1753 msecs_to_jiffies(max_timeout));
1754
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001755 /* Make sure descriptors are ready before ringing the doorbell */
1756 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301757 spin_lock_irqsave(hba->host->host_lock, flags);
1758 hba->dev_cmd.complete = NULL;
1759 if (likely(time_left)) {
1760 err = ufshcd_get_tr_ocs(lrbp);
1761 if (!err)
1762 err = ufshcd_dev_cmd_completion(hba, lrbp);
1763 }
1764 spin_unlock_irqrestore(hba->host->host_lock, flags);
1765
1766 if (!time_left) {
1767 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001768 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1769 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301770 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001771 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301772 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001773 /*
1774 * in case of an error, after clearing the doorbell,
1775 * we also need to clear the outstanding_request
1776 * field in hba
1777 */
1778 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301779 }
1780
1781 return err;
1782}
1783
1784/**
1785 * ufshcd_get_dev_cmd_tag - Get device management command tag
1786 * @hba: per-adapter instance
1787 * @tag: pointer to variable with available slot value
1788 *
1789 * Get a free slot and lock it until device management command
1790 * completes.
1791 *
1792 * Returns false if free slot is unavailable for locking, else
1793 * return true with tag value in @tag.
1794 */
1795static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1796{
1797 int tag;
1798 bool ret = false;
1799 unsigned long tmp;
1800
1801 if (!tag_out)
1802 goto out;
1803
1804 do {
1805 tmp = ~hba->lrb_in_use;
1806 tag = find_last_bit(&tmp, hba->nutrs);
1807 if (tag >= hba->nutrs)
1808 goto out;
1809 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1810
1811 *tag_out = tag;
1812 ret = true;
1813out:
1814 return ret;
1815}
1816
1817static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1818{
1819 clear_bit_unlock(tag, &hba->lrb_in_use);
1820}
1821
1822/**
1823 * ufshcd_exec_dev_cmd - API for sending device management requests
1824 * @hba - UFS hba
1825 * @cmd_type - specifies the type (NOP, Query...)
1826 * @timeout - time in seconds
1827 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301828 * NOTE: Since there is only one available tag for device management commands,
1829 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301830 */
1831static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1832 enum dev_cmd_type cmd_type, int timeout)
1833{
1834 struct ufshcd_lrb *lrbp;
1835 int err;
1836 int tag;
1837 struct completion wait;
1838 unsigned long flags;
1839
1840 /*
1841 * Get free slot, sleep if slots are unavailable.
1842 * Even though we use wait_event() which sleeps indefinitely,
1843 * the maximum wait time is bounded by SCSI request timeout.
1844 */
1845 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1846
1847 init_completion(&wait);
1848 lrbp = &hba->lrb[tag];
1849 WARN_ON(lrbp->cmd);
1850 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1851 if (unlikely(err))
1852 goto out_put_tag;
1853
1854 hba->dev_cmd.complete = &wait;
1855
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001856 /* Make sure descriptors are ready before ringing the doorbell */
1857 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301858 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001859 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301860 ufshcd_send_command(hba, tag);
1861 spin_unlock_irqrestore(hba->host->host_lock, flags);
1862
1863 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1864
1865out_put_tag:
1866 ufshcd_put_dev_cmd_tag(hba, tag);
1867 wake_up(&hba->dev_cmd.tag_wq);
1868 return err;
1869}
1870
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301871/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001872 * ufshcd_init_query() - init the query response and request parameters
1873 * @hba: per-adapter instance
1874 * @request: address of the request pointer to be initialized
1875 * @response: address of the response pointer to be initialized
1876 * @opcode: operation to perform
1877 * @idn: flag idn to access
1878 * @index: LU number to access
1879 * @selector: query/flag/descriptor further identification
1880 */
1881static inline void ufshcd_init_query(struct ufs_hba *hba,
1882 struct ufs_query_req **request, struct ufs_query_res **response,
1883 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1884{
1885 *request = &hba->dev_cmd.query.request;
1886 *response = &hba->dev_cmd.query.response;
1887 memset(*request, 0, sizeof(struct ufs_query_req));
1888 memset(*response, 0, sizeof(struct ufs_query_res));
1889 (*request)->upiu_req.opcode = opcode;
1890 (*request)->upiu_req.idn = idn;
1891 (*request)->upiu_req.index = index;
1892 (*request)->upiu_req.selector = selector;
1893}
1894
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001895static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1896 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1897{
1898 int ret;
1899 int retries;
1900
1901 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1902 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1903 if (ret)
1904 dev_dbg(hba->dev,
1905 "%s: failed with error %d, retries %d\n",
1906 __func__, ret, retries);
1907 else
1908 break;
1909 }
1910
1911 if (ret)
1912 dev_err(hba->dev,
1913 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1914 __func__, opcode, idn, ret, retries);
1915 return ret;
1916}
1917
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001918/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301919 * ufshcd_query_flag() - API function for sending flag query requests
1920 * hba: per-adapter instance
1921 * query_opcode: flag query to perform
1922 * idn: flag idn to access
1923 * flag_res: the flag value after the query request completes
1924 *
1925 * Returns 0 for success, non-zero in case of failure
1926 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001927int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301928 enum flag_idn idn, bool *flag_res)
1929{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001930 struct ufs_query_req *request = NULL;
1931 struct ufs_query_res *response = NULL;
1932 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001933 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301934
1935 BUG_ON(!hba);
1936
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001937 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301938 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001939 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1940 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301941
1942 switch (opcode) {
1943 case UPIU_QUERY_OPCODE_SET_FLAG:
1944 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1945 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1946 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1947 break;
1948 case UPIU_QUERY_OPCODE_READ_FLAG:
1949 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1950 if (!flag_res) {
1951 /* No dummy reads */
1952 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1953 __func__);
1954 err = -EINVAL;
1955 goto out_unlock;
1956 }
1957 break;
1958 default:
1959 dev_err(hba->dev,
1960 "%s: Expected query flag opcode but got = %d\n",
1961 __func__, opcode);
1962 err = -EINVAL;
1963 goto out_unlock;
1964 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301965
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001966 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301967
1968 if (err) {
1969 dev_err(hba->dev,
1970 "%s: Sending flag query for idn %d failed, err = %d\n",
1971 __func__, idn, err);
1972 goto out_unlock;
1973 }
1974
1975 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301976 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301977 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1978
1979out_unlock:
1980 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001981 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301982 return err;
1983}
1984
1985/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301986 * ufshcd_query_attr - API function for sending attribute requests
1987 * hba: per-adapter instance
1988 * opcode: attribute opcode
1989 * idn: attribute idn to access
1990 * index: index field
1991 * selector: selector field
1992 * attr_val: the attribute value after the query request completes
1993 *
1994 * Returns 0 for success, non-zero in case of failure
1995*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301996static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301997 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1998{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001999 struct ufs_query_req *request = NULL;
2000 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302001 int err;
2002
2003 BUG_ON(!hba);
2004
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002005 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302006 if (!attr_val) {
2007 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
2008 __func__, opcode);
2009 err = -EINVAL;
2010 goto out;
2011 }
2012
2013 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002014 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2015 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302016
2017 switch (opcode) {
2018 case UPIU_QUERY_OPCODE_WRITE_ATTR:
2019 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302020 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302021 break;
2022 case UPIU_QUERY_OPCODE_READ_ATTR:
2023 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2024 break;
2025 default:
2026 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
2027 __func__, opcode);
2028 err = -EINVAL;
2029 goto out_unlock;
2030 }
2031
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002032 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302033
2034 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002035 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2036 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302037 goto out_unlock;
2038 }
2039
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05302040 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302041
2042out_unlock:
2043 mutex_unlock(&hba->dev_cmd.lock);
2044out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002045 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302046 return err;
2047}
2048
2049/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02002050 * ufshcd_query_attr_retry() - API function for sending query
2051 * attribute with retries
2052 * @hba: per-adapter instance
2053 * @opcode: attribute opcode
2054 * @idn: attribute idn to access
2055 * @index: index field
2056 * @selector: selector field
2057 * @attr_val: the attribute value after the query request
2058 * completes
2059 *
2060 * Returns 0 for success, non-zero in case of failure
2061*/
2062static int ufshcd_query_attr_retry(struct ufs_hba *hba,
2063 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
2064 u32 *attr_val)
2065{
2066 int ret = 0;
2067 u32 retries;
2068
2069 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2070 ret = ufshcd_query_attr(hba, opcode, idn, index,
2071 selector, attr_val);
2072 if (ret)
2073 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
2074 __func__, ret, retries);
2075 else
2076 break;
2077 }
2078
2079 if (ret)
2080 dev_err(hba->dev,
2081 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2082 __func__, idn, ret, QUERY_REQ_RETRIES);
2083 return ret;
2084}
2085
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002086static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002087 enum query_opcode opcode, enum desc_idn idn, u8 index,
2088 u8 selector, u8 *desc_buf, int *buf_len)
2089{
2090 struct ufs_query_req *request = NULL;
2091 struct ufs_query_res *response = NULL;
2092 int err;
2093
2094 BUG_ON(!hba);
2095
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002096 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002097 if (!desc_buf) {
2098 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
2099 __func__, opcode);
2100 err = -EINVAL;
2101 goto out;
2102 }
2103
2104 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
2105 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
2106 __func__, *buf_len);
2107 err = -EINVAL;
2108 goto out;
2109 }
2110
2111 mutex_lock(&hba->dev_cmd.lock);
2112 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
2113 selector);
2114 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002115 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002116
2117 switch (opcode) {
2118 case UPIU_QUERY_OPCODE_WRITE_DESC:
2119 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2120 break;
2121 case UPIU_QUERY_OPCODE_READ_DESC:
2122 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2123 break;
2124 default:
2125 dev_err(hba->dev,
2126 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2127 __func__, opcode);
2128 err = -EINVAL;
2129 goto out_unlock;
2130 }
2131
2132 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2133
2134 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002135 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2136 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002137 goto out_unlock;
2138 }
2139
2140 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002141 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002142
2143out_unlock:
2144 mutex_unlock(&hba->dev_cmd.lock);
2145out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002146 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002147 return err;
2148}
2149
2150/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002151 * ufshcd_query_descriptor_retry - API function for sending descriptor
2152 * requests
2153 * hba: per-adapter instance
2154 * opcode: attribute opcode
2155 * idn: attribute idn to access
2156 * index: index field
2157 * selector: selector field
2158 * desc_buf: the buffer that contains the descriptor
2159 * buf_len: length parameter passed to the device
2160 *
2161 * Returns 0 for success, non-zero in case of failure.
2162 * The buf_len parameter will contain, on return, the length parameter
2163 * received on the response.
2164 */
2165int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2166 enum query_opcode opcode, enum desc_idn idn, u8 index,
2167 u8 selector, u8 *desc_buf, int *buf_len)
2168{
2169 int err;
2170 int retries;
2171
2172 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2173 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2174 selector, desc_buf, buf_len);
2175 if (!err || err == -EINVAL)
2176 break;
2177 }
2178
2179 return err;
2180}
2181EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2182
2183/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002184 * ufshcd_read_desc_param - read the specified descriptor parameter
2185 * @hba: Pointer to adapter instance
2186 * @desc_id: descriptor idn value
2187 * @desc_index: descriptor index
2188 * @param_offset: offset of the parameter to read
2189 * @param_read_buf: pointer to buffer where parameter would be read
2190 * @param_size: sizeof(param_read_buf)
2191 *
2192 * Return 0 in case of success, non-zero otherwise
2193 */
2194static int ufshcd_read_desc_param(struct ufs_hba *hba,
2195 enum desc_idn desc_id,
2196 int desc_index,
2197 u32 param_offset,
2198 u8 *param_read_buf,
2199 u32 param_size)
2200{
2201 int ret;
2202 u8 *desc_buf;
2203 u32 buff_len;
2204 bool is_kmalloc = true;
2205
2206 /* safety checks */
2207 if (desc_id >= QUERY_DESC_IDN_MAX)
2208 return -EINVAL;
2209
2210 buff_len = ufs_query_desc_max_size[desc_id];
2211 if ((param_offset + param_size) > buff_len)
2212 return -EINVAL;
2213
2214 if (!param_offset && (param_size == buff_len)) {
2215 /* memory space already available to hold full descriptor */
2216 desc_buf = param_read_buf;
2217 is_kmalloc = false;
2218 } else {
2219 /* allocate memory to hold full descriptor */
2220 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2221 if (!desc_buf)
2222 return -ENOMEM;
2223 }
2224
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002225 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2226 desc_id, desc_index, 0, desc_buf,
2227 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002228
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002229 if (ret) {
2230 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2231 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002232
2233 goto out;
2234 }
2235
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002236 /* Sanity check */
2237 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2238 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2239 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2240 ret = -EINVAL;
2241 goto out;
2242 }
2243
2244 /*
2245 * While reading variable size descriptors (like string descriptor),
2246 * some UFS devices may report the "LENGTH" (field in "Transaction
2247 * Specific fields" of Query Response UPIU) same as what was requested
2248 * in Query Request UPIU instead of reporting the actual size of the
2249 * variable size descriptor.
2250 * Although it's safe to ignore the "LENGTH" field for variable size
2251 * descriptors as we can always derive the length of the descriptor from
2252 * the descriptor header fields. Hence this change impose the length
2253 * match check only for fixed size descriptors (for which we always
2254 * request the correct size as part of Query Request UPIU).
2255 */
2256 if ((desc_id != QUERY_DESC_IDN_STRING) &&
2257 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
2258 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
2259 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
2260 ret = -EINVAL;
2261 goto out;
2262 }
2263
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002264 if (is_kmalloc)
2265 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2266out:
2267 if (is_kmalloc)
2268 kfree(desc_buf);
2269 return ret;
2270}
2271
2272static inline int ufshcd_read_desc(struct ufs_hba *hba,
2273 enum desc_idn desc_id,
2274 int desc_index,
2275 u8 *buf,
2276 u32 size)
2277{
2278 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2279}
2280
2281static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2282 u8 *buf,
2283 u32 size)
2284{
Dolev Raviv61e07352016-11-23 16:30:49 -08002285 int err = 0;
2286 int retries;
2287
2288 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2289 /* Read descriptor*/
2290 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2291 if (!err)
2292 break;
2293 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2294 }
2295
2296 return err;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002297}
2298
Yaniv Gardib573d482016-03-10 17:37:09 +02002299int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2300{
2301 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2302}
2303EXPORT_SYMBOL(ufshcd_read_device_desc);
2304
2305/**
2306 * ufshcd_read_string_desc - read string descriptor
2307 * @hba: pointer to adapter instance
2308 * @desc_index: descriptor index
2309 * @buf: pointer to buffer where descriptor would be read
2310 * @size: size of buf
2311 * @ascii: if true convert from unicode to ascii characters
2312 *
2313 * Return 0 in case of success, non-zero otherwise
2314 */
2315int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2316 u32 size, bool ascii)
2317{
2318 int err = 0;
2319
2320 err = ufshcd_read_desc(hba,
2321 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2322
2323 if (err) {
2324 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2325 __func__, QUERY_REQ_RETRIES, err);
2326 goto out;
2327 }
2328
2329 if (ascii) {
2330 int desc_len;
2331 int ascii_len;
2332 int i;
2333 char *buff_ascii;
2334
2335 desc_len = buf[0];
2336 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2337 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2338 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2339 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2340 __func__);
2341 err = -ENOMEM;
2342 goto out;
2343 }
2344
2345 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2346 if (!buff_ascii) {
2347 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002348 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002349 }
2350
2351 /*
2352 * the descriptor contains string in UTF16 format
2353 * we need to convert to utf-8 so it can be displayed
2354 */
2355 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2356 desc_len - QUERY_DESC_HDR_SIZE,
2357 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2358
2359 /* replace non-printable or non-ASCII characters with spaces */
2360 for (i = 0; i < ascii_len; i++)
2361 ufshcd_remove_non_printable(&buff_ascii[i]);
2362
2363 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2364 size - QUERY_DESC_HDR_SIZE);
2365 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2366 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002367 kfree(buff_ascii);
2368 }
2369out:
2370 return err;
2371}
2372EXPORT_SYMBOL(ufshcd_read_string_desc);
2373
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002374/**
2375 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2376 * @hba: Pointer to adapter instance
2377 * @lun: lun id
2378 * @param_offset: offset of the parameter to read
2379 * @param_read_buf: pointer to buffer where parameter would be read
2380 * @param_size: sizeof(param_read_buf)
2381 *
2382 * Return 0 in case of success, non-zero otherwise
2383 */
2384static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2385 int lun,
2386 enum unit_desc_param param_offset,
2387 u8 *param_read_buf,
2388 u32 param_size)
2389{
2390 /*
2391 * Unit descriptors are only available for general purpose LUs (LUN id
2392 * from 0 to 7) and RPMB Well known LU.
2393 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002394 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002395 return -EOPNOTSUPP;
2396
2397 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2398 param_offset, param_read_buf, param_size);
2399}
2400
2401/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302402 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2403 * @hba: per adapter instance
2404 *
2405 * 1. Allocate DMA memory for Command Descriptor array
2406 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2407 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2408 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2409 * (UTMRDL)
2410 * 4. Allocate memory for local reference block(lrb).
2411 *
2412 * Returns 0 for success, non-zero in case of failure
2413 */
2414static int ufshcd_memory_alloc(struct ufs_hba *hba)
2415{
2416 size_t utmrdl_size, utrdl_size, ucdl_size;
2417
2418 /* Allocate memory for UTP command descriptors */
2419 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002420 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2421 ucdl_size,
2422 &hba->ucdl_dma_addr,
2423 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302424
2425 /*
2426 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2427 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2428 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2429 * be aligned to 128 bytes as well
2430 */
2431 if (!hba->ucdl_base_addr ||
2432 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302433 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302434 "Command Descriptor Memory allocation failed\n");
2435 goto out;
2436 }
2437
2438 /*
2439 * Allocate memory for UTP Transfer descriptors
2440 * UFSHCI requires 1024 byte alignment of UTRD
2441 */
2442 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002443 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2444 utrdl_size,
2445 &hba->utrdl_dma_addr,
2446 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302447 if (!hba->utrdl_base_addr ||
2448 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302449 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302450 "Transfer Descriptor Memory allocation failed\n");
2451 goto out;
2452 }
2453
2454 /*
2455 * Allocate memory for UTP Task Management descriptors
2456 * UFSHCI requires 1024 byte alignment of UTMRD
2457 */
2458 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002459 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2460 utmrdl_size,
2461 &hba->utmrdl_dma_addr,
2462 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302463 if (!hba->utmrdl_base_addr ||
2464 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302465 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302466 "Task Management Descriptor Memory allocation failed\n");
2467 goto out;
2468 }
2469
2470 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002471 hba->lrb = devm_kzalloc(hba->dev,
2472 hba->nutrs * sizeof(struct ufshcd_lrb),
2473 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302474 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302475 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302476 goto out;
2477 }
2478 return 0;
2479out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302480 return -ENOMEM;
2481}
2482
2483/**
Dolev Raviv66cc8202016-12-22 18:39:42 -08002484 * ufshcd_print_pwr_info - print power params as saved in hba
2485 * power info
2486 * @hba: per-adapter instance
2487 */
2488static void ufshcd_print_pwr_info(struct ufs_hba *hba)
2489{
2490 static const char * const names[] = {
2491 "INVALID MODE",
2492 "FAST MODE",
2493 "SLOW_MODE",
2494 "INVALID MODE",
2495 "FASTAUTO_MODE",
2496 "SLOWAUTO_MODE",
2497 "INVALID MODE",
2498 };
2499
2500 dev_info(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
2501 __func__,
2502 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx,
2503 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx,
2504 names[hba->pwr_info.pwr_rx],
2505 names[hba->pwr_info.pwr_tx],
2506 hba->pwr_info.hs_rate);
2507}
2508
2509/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302510 * ufshcd_host_memory_configure - configure local reference block with
2511 * memory offsets
2512 * @hba: per adapter instance
2513 *
2514 * Configure Host memory space
2515 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2516 * address.
2517 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2518 * and PRDT offset.
2519 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2520 * into local reference block.
2521 */
2522static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2523{
2524 struct utp_transfer_cmd_desc *cmd_descp;
2525 struct utp_transfer_req_desc *utrdlp;
2526 dma_addr_t cmd_desc_dma_addr;
2527 dma_addr_t cmd_desc_element_addr;
2528 u16 response_offset;
2529 u16 prdt_offset;
2530 int cmd_desc_size;
2531 int i;
2532
2533 utrdlp = hba->utrdl_base_addr;
2534 cmd_descp = hba->ucdl_base_addr;
2535
2536 response_offset =
2537 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2538 prdt_offset =
2539 offsetof(struct utp_transfer_cmd_desc, prd_table);
2540
2541 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2542 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2543
2544 for (i = 0; i < hba->nutrs; i++) {
2545 /* Configure UTRD with command descriptor base address */
2546 cmd_desc_element_addr =
2547 (cmd_desc_dma_addr + (cmd_desc_size * i));
2548 utrdlp[i].command_desc_base_addr_lo =
2549 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2550 utrdlp[i].command_desc_base_addr_hi =
2551 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2552
2553 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002554 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2555 utrdlp[i].response_upiu_offset =
2556 cpu_to_le16(response_offset);
2557 utrdlp[i].prd_table_offset =
2558 cpu_to_le16(prdt_offset);
2559 utrdlp[i].response_upiu_length =
2560 cpu_to_le16(ALIGNED_UPIU_SIZE);
2561 } else {
2562 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302563 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002564 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302565 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002566 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302567 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002568 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302569
2570 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302571 hba->lrb[i].ucd_req_ptr =
2572 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302573 hba->lrb[i].ucd_rsp_ptr =
2574 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2575 hba->lrb[i].ucd_prdt_ptr =
2576 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2577 }
2578}
2579
2580/**
2581 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2582 * @hba: per adapter instance
2583 *
2584 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2585 * in order to initialize the Unipro link startup procedure.
2586 * Once the Unipro links are up, the device connected to the controller
2587 * is detected.
2588 *
2589 * Returns 0 on success, non-zero value on failure
2590 */
2591static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2592{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302593 struct uic_command uic_cmd = {0};
2594 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302595
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302596 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2597
2598 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2599 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302600 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302601 "dme-link-startup: error code %d\n", ret);
2602 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302603}
2604
Yaniv Gardicad2e032015-03-31 17:37:14 +03002605static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2606{
2607 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2608 unsigned long min_sleep_time_us;
2609
2610 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2611 return;
2612
2613 /*
2614 * last_dme_cmd_tstamp will be 0 only for 1st call to
2615 * this function
2616 */
2617 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2618 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2619 } else {
2620 unsigned long delta =
2621 (unsigned long) ktime_to_us(
2622 ktime_sub(ktime_get(),
2623 hba->last_dme_cmd_tstamp));
2624
2625 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2626 min_sleep_time_us =
2627 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2628 else
2629 return; /* no more delay required */
2630 }
2631
2632 /* allow sleep for extra 50us if needed */
2633 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2634}
2635
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302636/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302637 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2638 * @hba: per adapter instance
2639 * @attr_sel: uic command argument1
2640 * @attr_set: attribute set type as uic command argument2
2641 * @mib_val: setting value as uic command argument3
2642 * @peer: indicate whether peer or local
2643 *
2644 * Returns 0 on success, non-zero value on failure
2645 */
2646int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2647 u8 attr_set, u32 mib_val, u8 peer)
2648{
2649 struct uic_command uic_cmd = {0};
2650 static const char *const action[] = {
2651 "dme-set",
2652 "dme-peer-set"
2653 };
2654 const char *set = action[!!peer];
2655 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002656 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302657
2658 uic_cmd.command = peer ?
2659 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2660 uic_cmd.argument1 = attr_sel;
2661 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2662 uic_cmd.argument3 = mib_val;
2663
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002664 do {
2665 /* for peer attributes we retry upon failure */
2666 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2667 if (ret)
2668 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2669 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2670 } while (ret && peer && --retries);
2671
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002672 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002673 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002674 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2675 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302676
2677 return ret;
2678}
2679EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2680
2681/**
2682 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2683 * @hba: per adapter instance
2684 * @attr_sel: uic command argument1
2685 * @mib_val: the value of the attribute as returned by the UIC command
2686 * @peer: indicate whether peer or local
2687 *
2688 * Returns 0 on success, non-zero value on failure
2689 */
2690int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2691 u32 *mib_val, u8 peer)
2692{
2693 struct uic_command uic_cmd = {0};
2694 static const char *const action[] = {
2695 "dme-get",
2696 "dme-peer-get"
2697 };
2698 const char *get = action[!!peer];
2699 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002700 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002701 struct ufs_pa_layer_attr orig_pwr_info;
2702 struct ufs_pa_layer_attr temp_pwr_info;
2703 bool pwr_mode_change = false;
2704
2705 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2706 orig_pwr_info = hba->pwr_info;
2707 temp_pwr_info = orig_pwr_info;
2708
2709 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2710 orig_pwr_info.pwr_rx == FAST_MODE) {
2711 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2712 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2713 pwr_mode_change = true;
2714 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2715 orig_pwr_info.pwr_rx == SLOW_MODE) {
2716 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2717 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2718 pwr_mode_change = true;
2719 }
2720 if (pwr_mode_change) {
2721 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2722 if (ret)
2723 goto out;
2724 }
2725 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302726
2727 uic_cmd.command = peer ?
2728 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2729 uic_cmd.argument1 = attr_sel;
2730
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002731 do {
2732 /* for peer attributes we retry upon failure */
2733 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2734 if (ret)
2735 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2736 get, UIC_GET_ATTR_ID(attr_sel), ret);
2737 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302738
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002739 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002740 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002741 get, UIC_GET_ATTR_ID(attr_sel),
2742 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002743
2744 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302745 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002746
2747 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2748 && pwr_mode_change)
2749 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302750out:
2751 return ret;
2752}
2753EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2754
2755/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002756 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2757 * state) and waits for it to take effect.
2758 *
2759 * @hba: per adapter instance
2760 * @cmd: UIC command to execute
2761 *
2762 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2763 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2764 * and device UniPro link and hence it's final completion would be indicated by
2765 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2766 * addition to normal UIC command completion Status (UCCS). This function only
2767 * returns after the relevant status bits indicate the completion.
2768 *
2769 * Returns 0 on success, non-zero value on failure
2770 */
2771static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2772{
2773 struct completion uic_async_done;
2774 unsigned long flags;
2775 u8 status;
2776 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002777 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002778
2779 mutex_lock(&hba->uic_cmd_mutex);
2780 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002781 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002782
2783 spin_lock_irqsave(hba->host->host_lock, flags);
2784 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002785 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2786 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2787 /*
2788 * Make sure UIC command completion interrupt is disabled before
2789 * issuing UIC command.
2790 */
2791 wmb();
2792 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002793 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002794 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2795 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002796 if (ret) {
2797 dev_err(hba->dev,
2798 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2799 cmd->command, cmd->argument3, ret);
2800 goto out;
2801 }
2802
2803 if (!wait_for_completion_timeout(hba->uic_async_done,
2804 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2805 dev_err(hba->dev,
2806 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2807 cmd->command, cmd->argument3);
2808 ret = -ETIMEDOUT;
2809 goto out;
2810 }
2811
2812 status = ufshcd_get_upmcrs(hba);
2813 if (status != PWR_LOCAL) {
2814 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002815 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002816 cmd->command, status);
2817 ret = (status != PWR_OK) ? status : -1;
2818 }
2819out:
2820 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002821 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002822 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002823 if (reenable_intr)
2824 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002825 spin_unlock_irqrestore(hba->host->host_lock, flags);
2826 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002827
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002828 return ret;
2829}
2830
2831/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302832 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2833 * using DME_SET primitives.
2834 * @hba: per adapter instance
2835 * @mode: powr mode value
2836 *
2837 * Returns 0 on success, non-zero value on failure
2838 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302839static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302840{
2841 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002842 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302843
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002844 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2845 ret = ufshcd_dme_set(hba,
2846 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2847 if (ret) {
2848 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2849 __func__, ret);
2850 goto out;
2851 }
2852 }
2853
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302854 uic_cmd.command = UIC_CMD_DME_SET;
2855 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2856 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002857 ufshcd_hold(hba, false);
2858 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2859 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302860
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002861out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002862 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002863}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302864
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002865static int ufshcd_link_recovery(struct ufs_hba *hba)
2866{
2867 int ret;
2868 unsigned long flags;
2869
2870 spin_lock_irqsave(hba->host->host_lock, flags);
2871 hba->ufshcd_state = UFSHCD_STATE_RESET;
2872 ufshcd_set_eh_in_progress(hba);
2873 spin_unlock_irqrestore(hba->host->host_lock, flags);
2874
2875 ret = ufshcd_host_reset_and_restore(hba);
2876
2877 spin_lock_irqsave(hba->host->host_lock, flags);
2878 if (ret)
2879 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2880 ufshcd_clear_eh_in_progress(hba);
2881 spin_unlock_irqrestore(hba->host->host_lock, flags);
2882
2883 if (ret)
2884 dev_err(hba->dev, "%s: link recovery failed, err %d",
2885 __func__, ret);
2886
2887 return ret;
2888}
2889
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002890static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002891{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002892 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002893 struct uic_command uic_cmd = {0};
2894
Kiwoong Kimee32c902016-11-10 21:17:43 +09002895 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
2896
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002897 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002898 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002899
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002900 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002901 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2902 __func__, ret);
2903
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002904 /*
2905 * If link recovery fails then return error so that caller
2906 * don't retry the hibern8 enter again.
2907 */
2908 if (ufshcd_link_recovery(hba))
2909 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09002910 } else
2911 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
2912 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002913
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002914 return ret;
2915}
2916
2917static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2918{
2919 int ret = 0, retries;
2920
2921 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2922 ret = __ufshcd_uic_hibern8_enter(hba);
2923 if (!ret || ret == -ENOLINK)
2924 goto out;
2925 }
2926out:
2927 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002928}
2929
2930static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2931{
2932 struct uic_command uic_cmd = {0};
2933 int ret;
2934
Kiwoong Kimee32c902016-11-10 21:17:43 +09002935 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
2936
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002937 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2938 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302939 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002940 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2941 __func__, ret);
2942 ret = ufshcd_link_recovery(hba);
Kiwoong Kimee32c902016-11-10 21:17:43 +09002943 } else
2944 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
2945 POST_CHANGE);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302946
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302947 return ret;
2948}
2949
Yaniv Gardi50646362014-10-23 13:25:13 +03002950 /**
2951 * ufshcd_init_pwr_info - setting the POR (power on reset)
2952 * values in hba power info
2953 * @hba: per-adapter instance
2954 */
2955static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2956{
2957 hba->pwr_info.gear_rx = UFS_PWM_G1;
2958 hba->pwr_info.gear_tx = UFS_PWM_G1;
2959 hba->pwr_info.lane_rx = 1;
2960 hba->pwr_info.lane_tx = 1;
2961 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2962 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2963 hba->pwr_info.hs_rate = 0;
2964}
2965
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302966/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002967 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2968 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302969 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002970static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302971{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002972 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2973
2974 if (hba->max_pwr_info.is_valid)
2975 return 0;
2976
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08002977 pwr_info->pwr_tx = FAST_MODE;
2978 pwr_info->pwr_rx = FAST_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002979 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302980
2981 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002982 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2983 &pwr_info->lane_rx);
2984 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2985 &pwr_info->lane_tx);
2986
2987 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2988 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2989 __func__,
2990 pwr_info->lane_rx,
2991 pwr_info->lane_tx);
2992 return -EINVAL;
2993 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302994
2995 /*
2996 * First, get the maximum gears of HS speed.
2997 * If a zero value, it means there is no HSGEAR capability.
2998 * Then, get the maximum gears of PWM speed.
2999 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003000 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
3001 if (!pwr_info->gear_rx) {
3002 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
3003 &pwr_info->gear_rx);
3004 if (!pwr_info->gear_rx) {
3005 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
3006 __func__, pwr_info->gear_rx);
3007 return -EINVAL;
3008 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003009 pwr_info->pwr_rx = SLOW_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303010 }
3011
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003012 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
3013 &pwr_info->gear_tx);
3014 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303015 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003016 &pwr_info->gear_tx);
3017 if (!pwr_info->gear_tx) {
3018 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
3019 __func__, pwr_info->gear_tx);
3020 return -EINVAL;
3021 }
subhashj@codeaurora.org2349b532016-11-23 16:33:19 -08003022 pwr_info->pwr_tx = SLOW_MODE;
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003023 }
3024
3025 hba->max_pwr_info.is_valid = true;
3026 return 0;
3027}
3028
3029static int ufshcd_change_power_mode(struct ufs_hba *hba,
3030 struct ufs_pa_layer_attr *pwr_mode)
3031{
3032 int ret;
3033
3034 /* if already configured to the requested pwr_mode */
3035 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
3036 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
3037 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
3038 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
3039 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
3040 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
3041 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
3042 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
3043 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303044 }
3045
3046 /*
3047 * Configure attributes for power mode change with below.
3048 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3049 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3050 * - PA_HSSERIES
3051 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003052 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
3053 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
3054 pwr_mode->lane_rx);
3055 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3056 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303057 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003058 else
3059 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303060
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003061 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
3062 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
3063 pwr_mode->lane_tx);
3064 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
3065 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303066 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003067 else
3068 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303069
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003070 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
3071 pwr_mode->pwr_tx == FASTAUTO_MODE ||
3072 pwr_mode->pwr_rx == FAST_MODE ||
3073 pwr_mode->pwr_tx == FAST_MODE)
3074 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
3075 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303076
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003077 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
3078 | pwr_mode->pwr_tx);
3079
3080 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303081 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003082 "%s: power mode change failed %d\n", __func__, ret);
3083 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003084 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
3085 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003086
3087 memcpy(&hba->pwr_info, pwr_mode,
3088 sizeof(struct ufs_pa_layer_attr));
3089 }
3090
Dolev Raviv66cc8202016-12-22 18:39:42 -08003091 ufshcd_print_pwr_info(hba);
3092
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003093 return ret;
3094}
3095
3096/**
3097 * ufshcd_config_pwr_mode - configure a new power mode
3098 * @hba: per-adapter instance
3099 * @desired_pwr_mode: desired power configuration
3100 */
3101static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3102 struct ufs_pa_layer_attr *desired_pwr_mode)
3103{
3104 struct ufs_pa_layer_attr final_params = { 0 };
3105 int ret;
3106
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003107 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3108 desired_pwr_mode, &final_params);
3109
3110 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003111 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3112
3113 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303114
3115 return ret;
3116}
3117
3118/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303119 * ufshcd_complete_dev_init() - checks device readiness
3120 * hba: per-adapter instance
3121 *
3122 * Set fDeviceInit flag and poll until device toggles it.
3123 */
3124static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3125{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003126 int i;
3127 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303128 bool flag_res = 1;
3129
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003130 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3131 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303132 if (err) {
3133 dev_err(hba->dev,
3134 "%s setting fDeviceInit flag failed with error %d\n",
3135 __func__, err);
3136 goto out;
3137 }
3138
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003139 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3140 for (i = 0; i < 1000 && !err && flag_res; i++)
3141 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3142 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3143
Dolev Raviv68078d52013-07-30 00:35:58 +05303144 if (err)
3145 dev_err(hba->dev,
3146 "%s reading fDeviceInit flag failed with error %d\n",
3147 __func__, err);
3148 else if (flag_res)
3149 dev_err(hba->dev,
3150 "%s fDeviceInit was not cleared by the device\n",
3151 __func__);
3152
3153out:
3154 return err;
3155}
3156
3157/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303158 * ufshcd_make_hba_operational - Make UFS controller operational
3159 * @hba: per adapter instance
3160 *
3161 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003162 * 1. Enable required interrupts
3163 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003164 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003165 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303166 *
3167 * Returns 0 on success, non-zero value on failure
3168 */
3169static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3170{
3171 int err = 0;
3172 u32 reg;
3173
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303174 /* Enable required interrupts */
3175 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3176
3177 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003178 if (ufshcd_is_intr_aggr_allowed(hba))
3179 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3180 else
3181 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303182
3183 /* Configure UTRL and UTMRL base address registers */
3184 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3185 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3186 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3187 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3188 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3189 REG_UTP_TASK_REQ_LIST_BASE_L);
3190 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3191 REG_UTP_TASK_REQ_LIST_BASE_H);
3192
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303193 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003194 * Make sure base address and interrupt setup are updated before
3195 * enabling the run/stop registers below.
3196 */
3197 wmb();
3198
3199 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303200 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303201 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003202 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303203 if (!(ufshcd_get_lists_status(reg))) {
3204 ufshcd_enable_run_stop_reg(hba);
3205 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303206 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303207 "Host controller not ready to process requests");
3208 err = -EIO;
3209 goto out;
3210 }
3211
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303212out:
3213 return err;
3214}
3215
3216/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003217 * ufshcd_hba_stop - Send controller to reset state
3218 * @hba: per adapter instance
3219 * @can_sleep: perform sleep or just spin
3220 */
3221static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3222{
3223 int err;
3224
3225 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3226 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3227 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3228 10, 1, can_sleep);
3229 if (err)
3230 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3231}
3232
3233/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303234 * ufshcd_hba_enable - initialize the controller
3235 * @hba: per adapter instance
3236 *
3237 * The controller resets itself and controller firmware initialization
3238 * sequence kicks off. When controller is ready it will set
3239 * the Host Controller Enable bit to 1.
3240 *
3241 * Returns 0 on success, non-zero value on failure
3242 */
3243static int ufshcd_hba_enable(struct ufs_hba *hba)
3244{
3245 int retry;
3246
3247 /*
3248 * msleep of 1 and 5 used in this function might result in msleep(20),
3249 * but it was necessary to send the UFS FPGA to reset mode during
3250 * development and testing of this driver. msleep can be changed to
3251 * mdelay and retry count can be reduced based on the controller.
3252 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003253 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303254 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003255 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303256
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003257 /* UniPro link is disabled at this point */
3258 ufshcd_set_link_off(hba);
3259
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003260 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003261
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303262 /* start controller initialization sequence */
3263 ufshcd_hba_start(hba);
3264
3265 /*
3266 * To initialize a UFS host controller HCE bit must be set to 1.
3267 * During initialization the HCE bit value changes from 1->0->1.
3268 * When the host controller completes initialization sequence
3269 * it sets the value of HCE bit to 1. The same HCE bit is read back
3270 * to check if the controller has completed initialization sequence.
3271 * So without this delay the value HCE = 1, set in the previous
3272 * instruction might be read back.
3273 * This delay can be changed based on the controller.
3274 */
3275 msleep(1);
3276
3277 /* wait for the host controller to complete initialization */
3278 retry = 10;
3279 while (ufshcd_is_hba_active(hba)) {
3280 if (retry) {
3281 retry--;
3282 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303283 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303284 "Controller enable failed\n");
3285 return -EIO;
3286 }
3287 msleep(5);
3288 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003289
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003290 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003291 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003292
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003293 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003294
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303295 return 0;
3296}
3297
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003298static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3299{
3300 int tx_lanes, i, err = 0;
3301
3302 if (!peer)
3303 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3304 &tx_lanes);
3305 else
3306 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3307 &tx_lanes);
3308 for (i = 0; i < tx_lanes; i++) {
3309 if (!peer)
3310 err = ufshcd_dme_set(hba,
3311 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3312 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3313 0);
3314 else
3315 err = ufshcd_dme_peer_set(hba,
3316 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3317 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3318 0);
3319 if (err) {
3320 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3321 __func__, peer, i, err);
3322 break;
3323 }
3324 }
3325
3326 return err;
3327}
3328
3329static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3330{
3331 return ufshcd_disable_tx_lcc(hba, true);
3332}
3333
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303334/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303335 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303336 * @hba: per adapter instance
3337 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303338 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303339 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303340static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303341{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303342 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003343 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003344 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303345
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003346 /*
3347 * If UFS device isn't active then we will have to issue link startup
3348 * 2 times to make sure the device state move to active.
3349 */
3350 if (!ufshcd_is_ufs_dev_active(hba))
3351 link_startup_again = true;
3352
3353link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003354 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003355 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303356
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003357 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003358
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003359 /* check if device is detected by inter-connect layer */
3360 if (!ret && !ufshcd_is_device_present(hba)) {
3361 dev_err(hba->dev, "%s: Device not present\n", __func__);
3362 ret = -ENXIO;
3363 goto out;
3364 }
3365
3366 /*
3367 * DME link lost indication is only received when link is up,
3368 * but we can't be sure if the link is up until link startup
3369 * succeeds. So reset the local Uni-Pro and try again.
3370 */
3371 if (ret && ufshcd_hba_enable(hba))
3372 goto out;
3373 } while (ret && retries--);
3374
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303375 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003376 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303377 goto out;
3378
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003379 if (link_startup_again) {
3380 link_startup_again = false;
3381 retries = DME_LINKSTARTUP_RETRIES;
3382 goto link_startup;
3383 }
3384
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003385 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3386 ret = ufshcd_disable_device_tx_lcc(hba);
3387 if (ret)
3388 goto out;
3389 }
3390
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003391 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003392 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3393 if (ret)
3394 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003395
3396 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303397out:
3398 if (ret)
3399 dev_err(hba->dev, "link startup failed %d\n", ret);
3400 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303401}
3402
3403/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303404 * ufshcd_verify_dev_init() - Verify device initialization
3405 * @hba: per-adapter instance
3406 *
3407 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3408 * device Transport Protocol (UTP) layer is ready after a reset.
3409 * If the UTP layer at the device side is not initialized, it may
3410 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3411 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3412 */
3413static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3414{
3415 int err = 0;
3416 int retries;
3417
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003418 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303419 mutex_lock(&hba->dev_cmd.lock);
3420 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3421 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3422 NOP_OUT_TIMEOUT);
3423
3424 if (!err || err == -ETIMEDOUT)
3425 break;
3426
3427 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3428 }
3429 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003430 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303431
3432 if (err)
3433 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3434 return err;
3435}
3436
3437/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003438 * ufshcd_set_queue_depth - set lun queue depth
3439 * @sdev: pointer to SCSI device
3440 *
3441 * Read bLUQueueDepth value and activate scsi tagged command
3442 * queueing. For WLUN, queue depth is set to 1. For best-effort
3443 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3444 * value that host can queue.
3445 */
3446static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3447{
3448 int ret = 0;
3449 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08003450 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003451 struct ufs_hba *hba;
3452
3453 hba = shost_priv(sdev->host);
3454
3455 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08003456 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3457 /* Read descriptor*/
3458 ret = ufshcd_read_unit_desc_param(hba,
3459 ufshcd_scsi_to_upiu_lun(sdev->lun),
3460 UNIT_DESC_PARAM_LU_Q_DEPTH,
3461 &lun_qdepth,
3462 sizeof(lun_qdepth));
3463 if (!ret || ret == -ENOTSUPP)
3464 break;
3465
3466 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
3467 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003468
3469 /* Some WLUN doesn't support unit descriptor */
3470 if (ret == -EOPNOTSUPP)
3471 lun_qdepth = 1;
3472 else if (!lun_qdepth)
3473 /* eventually, we can figure out the real queue depth */
3474 lun_qdepth = hba->nutrs;
3475 else
3476 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3477
3478 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3479 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003480 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003481}
3482
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003483/*
3484 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3485 * @hba: per-adapter instance
3486 * @lun: UFS device lun id
3487 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3488 *
3489 * Returns 0 in case of success and b_lu_write_protect status would be returned
3490 * @b_lu_write_protect parameter.
3491 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3492 * Returns -EINVAL in case of invalid parameters passed to this function.
3493 */
3494static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3495 u8 lun,
3496 u8 *b_lu_write_protect)
3497{
3498 int ret;
3499
3500 if (!b_lu_write_protect)
3501 ret = -EINVAL;
3502 /*
3503 * According to UFS device spec, RPMB LU can't be write
3504 * protected so skip reading bLUWriteProtect parameter for
3505 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3506 */
3507 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3508 ret = -ENOTSUPP;
3509 else
3510 ret = ufshcd_read_unit_desc_param(hba,
3511 lun,
3512 UNIT_DESC_PARAM_LU_WR_PROTECT,
3513 b_lu_write_protect,
3514 sizeof(*b_lu_write_protect));
3515 return ret;
3516}
3517
3518/**
3519 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3520 * status
3521 * @hba: per-adapter instance
3522 * @sdev: pointer to SCSI device
3523 *
3524 */
3525static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3526 struct scsi_device *sdev)
3527{
3528 if (hba->dev_info.f_power_on_wp_en &&
3529 !hba->dev_info.is_lu_power_on_wp) {
3530 u8 b_lu_write_protect;
3531
3532 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3533 &b_lu_write_protect) &&
3534 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3535 hba->dev_info.is_lu_power_on_wp = true;
3536 }
3537}
3538
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003539/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303540 * ufshcd_slave_alloc - handle initial SCSI device configurations
3541 * @sdev: pointer to SCSI device
3542 *
3543 * Returns success
3544 */
3545static int ufshcd_slave_alloc(struct scsi_device *sdev)
3546{
3547 struct ufs_hba *hba;
3548
3549 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303550
3551 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3552 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303553
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303554 /* allow SCSI layer to restart the device in case of errors */
3555 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003556
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003557 /* REPORT SUPPORTED OPERATION CODES is not supported */
3558 sdev->no_report_opcodes = 1;
3559
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003560
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003561 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003562
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003563 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3564
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003565 return 0;
3566}
3567
3568/**
3569 * ufshcd_change_queue_depth - change queue depth
3570 * @sdev: pointer to SCSI device
3571 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003572 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003573 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003574 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003575static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003576{
3577 struct ufs_hba *hba = shost_priv(sdev->host);
3578
3579 if (depth > hba->nutrs)
3580 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003581 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303582}
3583
3584/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003585 * ufshcd_slave_configure - adjust SCSI device configurations
3586 * @sdev: pointer to SCSI device
3587 */
3588static int ufshcd_slave_configure(struct scsi_device *sdev)
3589{
3590 struct request_queue *q = sdev->request_queue;
3591
3592 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3593 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3594
3595 return 0;
3596}
3597
3598/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303599 * ufshcd_slave_destroy - remove SCSI device configurations
3600 * @sdev: pointer to SCSI device
3601 */
3602static void ufshcd_slave_destroy(struct scsi_device *sdev)
3603{
3604 struct ufs_hba *hba;
3605
3606 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003607 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003608 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3609 unsigned long flags;
3610
3611 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003612 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003613 spin_unlock_irqrestore(hba->host->host_lock, flags);
3614 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303615}
3616
3617/**
3618 * ufshcd_task_req_compl - handle task management request completion
3619 * @hba: per adapter instance
3620 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303621 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303622 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303623 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303624 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303625static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303626{
3627 struct utp_task_req_desc *task_req_descp;
3628 struct utp_upiu_task_rsp *task_rsp_upiup;
3629 unsigned long flags;
3630 int ocs_value;
3631 int task_result;
3632
3633 spin_lock_irqsave(hba->host->host_lock, flags);
3634
3635 /* Clear completed tasks from outstanding_tasks */
3636 __clear_bit(index, &hba->outstanding_tasks);
3637
3638 task_req_descp = hba->utmrdl_base_addr;
3639 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3640
3641 if (ocs_value == OCS_SUCCESS) {
3642 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3643 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003644 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3645 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303646 if (resp)
3647 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303648 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303649 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3650 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303651 }
3652 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303653
3654 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303655}
3656
3657/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303658 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3659 * @lrb: pointer to local reference block of completed command
3660 * @scsi_status: SCSI command status
3661 *
3662 * Returns value base on SCSI command status
3663 */
3664static inline int
3665ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3666{
3667 int result = 0;
3668
3669 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303670 case SAM_STAT_CHECK_CONDITION:
3671 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303672 case SAM_STAT_GOOD:
3673 result |= DID_OK << 16 |
3674 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303675 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303676 break;
3677 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303678 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303679 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303680 ufshcd_copy_sense_data(lrbp);
3681 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303682 break;
3683 default:
3684 result |= DID_ERROR << 16;
3685 break;
3686 } /* end of switch */
3687
3688 return result;
3689}
3690
3691/**
3692 * ufshcd_transfer_rsp_status - Get overall status of the response
3693 * @hba: per adapter instance
3694 * @lrb: pointer to local reference block of completed command
3695 *
3696 * Returns result of the command to notify SCSI midlayer
3697 */
3698static inline int
3699ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3700{
3701 int result = 0;
3702 int scsi_status;
3703 int ocs;
3704
3705 /* overall command status of utrd */
3706 ocs = ufshcd_get_tr_ocs(lrbp);
3707
3708 switch (ocs) {
3709 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303710 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303711
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303712 switch (result) {
3713 case UPIU_TRANSACTION_RESPONSE:
3714 /*
3715 * get the response UPIU result to extract
3716 * the SCSI command status
3717 */
3718 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3719
3720 /*
3721 * get the result based on SCSI status response
3722 * to notify the SCSI midlayer of the command status
3723 */
3724 scsi_status = result & MASK_SCSI_STATUS;
3725 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303726
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003727 /*
3728 * Currently we are only supporting BKOPs exception
3729 * events hence we can ignore BKOPs exception event
3730 * during power management callbacks. BKOPs exception
3731 * event is not expected to be raised in runtime suspend
3732 * callback as it allows the urgent bkops.
3733 * During system suspend, we are anyway forcefully
3734 * disabling the bkops and if urgent bkops is needed
3735 * it will be enabled on system resume. Long term
3736 * solution could be to abort the system suspend if
3737 * UFS device needs urgent BKOPs.
3738 */
3739 if (!hba->pm_op_in_progress &&
3740 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303741 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303742 break;
3743 case UPIU_TRANSACTION_REJECT_UPIU:
3744 /* TODO: handle Reject UPIU Response */
3745 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303746 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303747 "Reject UPIU not fully implemented\n");
3748 break;
3749 default:
3750 result = DID_ERROR << 16;
3751 dev_err(hba->dev,
3752 "Unexpected request response code = %x\n",
3753 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303754 break;
3755 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303756 break;
3757 case OCS_ABORTED:
3758 result |= DID_ABORT << 16;
3759 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303760 case OCS_INVALID_COMMAND_STATUS:
3761 result |= DID_REQUEUE << 16;
3762 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303763 case OCS_INVALID_CMD_TABLE_ATTR:
3764 case OCS_INVALID_PRDT_ATTR:
3765 case OCS_MISMATCH_DATA_BUF_SIZE:
3766 case OCS_MISMATCH_RESP_UPIU_SIZE:
3767 case OCS_PEER_COMM_FAILURE:
3768 case OCS_FATAL_ERROR:
3769 default:
3770 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303771 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303772 "OCS error from controller = %x\n", ocs);
3773 break;
3774 } /* end of switch */
3775
Dolev Raviv66cc8202016-12-22 18:39:42 -08003776 if (host_byte(result) != DID_OK)
3777 ufshcd_print_trs(hba, 1 << lrbp->task_tag, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303778 return result;
3779}
3780
3781/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303782 * ufshcd_uic_cmd_compl - handle completion of uic command
3783 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303784 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303785 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303786static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303787{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303788 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303789 hba->active_uic_cmd->argument2 |=
3790 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303791 hba->active_uic_cmd->argument3 =
3792 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303793 complete(&hba->active_uic_cmd->done);
3794 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303795
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003796 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3797 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303798}
3799
3800/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003801 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303802 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003803 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303804 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003805static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3806 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303807{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303808 struct ufshcd_lrb *lrbp;
3809 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303810 int result;
3811 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003812
Dolev Ravive9d501b2014-07-01 12:22:37 +03003813 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3814 lrbp = &hba->lrb[index];
3815 cmd = lrbp->cmd;
3816 if (cmd) {
3817 result = ufshcd_transfer_rsp_status(hba, lrbp);
3818 scsi_dma_unmap(cmd);
3819 cmd->result = result;
3820 /* Mark completed command as NULL in LRB */
3821 lrbp->cmd = NULL;
3822 clear_bit_unlock(index, &hba->lrb_in_use);
3823 /* Do not touch lrbp after scsi done */
3824 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003825 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003826 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3827 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003828 if (hba->dev_cmd.complete)
3829 complete(hba->dev_cmd.complete);
3830 }
3831 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303832
3833 /* clear corresponding bits of completed commands */
3834 hba->outstanding_reqs ^= completed_reqs;
3835
Sahitya Tummala856b3482014-09-25 15:32:34 +03003836 ufshcd_clk_scaling_update_busy(hba);
3837
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303838 /* we might have free'd some tags above */
3839 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303840}
3841
3842/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003843 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3844 * @hba: per adapter instance
3845 */
3846static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3847{
3848 unsigned long completed_reqs;
3849 u32 tr_doorbell;
3850
3851 /* Resetting interrupt aggregation counters first and reading the
3852 * DOOR_BELL afterward allows us to handle all the completed requests.
3853 * In order to prevent other interrupts starvation the DB is read once
3854 * after reset. The down side of this solution is the possibility of
3855 * false interrupt if device completes another request after resetting
3856 * aggregation and before reading the DB.
3857 */
3858 if (ufshcd_is_intr_aggr_allowed(hba))
3859 ufshcd_reset_intr_aggr(hba);
3860
3861 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3862 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3863
3864 __ufshcd_transfer_req_compl(hba, completed_reqs);
3865}
3866
3867/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303868 * ufshcd_disable_ee - disable exception event
3869 * @hba: per-adapter instance
3870 * @mask: exception event to disable
3871 *
3872 * Disables exception event in the device so that the EVENT_ALERT
3873 * bit is not set.
3874 *
3875 * Returns zero on success, non-zero error value on failure.
3876 */
3877static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3878{
3879 int err = 0;
3880 u32 val;
3881
3882 if (!(hba->ee_ctrl_mask & mask))
3883 goto out;
3884
3885 val = hba->ee_ctrl_mask & ~mask;
3886 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003887 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303888 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3889 if (!err)
3890 hba->ee_ctrl_mask &= ~mask;
3891out:
3892 return err;
3893}
3894
3895/**
3896 * ufshcd_enable_ee - enable exception event
3897 * @hba: per-adapter instance
3898 * @mask: exception event to enable
3899 *
3900 * Enable corresponding exception event in the device to allow
3901 * device to alert host in critical scenarios.
3902 *
3903 * Returns zero on success, non-zero error value on failure.
3904 */
3905static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3906{
3907 int err = 0;
3908 u32 val;
3909
3910 if (hba->ee_ctrl_mask & mask)
3911 goto out;
3912
3913 val = hba->ee_ctrl_mask | mask;
3914 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003915 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303916 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3917 if (!err)
3918 hba->ee_ctrl_mask |= mask;
3919out:
3920 return err;
3921}
3922
3923/**
3924 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3925 * @hba: per-adapter instance
3926 *
3927 * Allow device to manage background operations on its own. Enabling
3928 * this might lead to inconsistent latencies during normal data transfers
3929 * as the device is allowed to manage its own way of handling background
3930 * operations.
3931 *
3932 * Returns zero on success, non-zero on failure.
3933 */
3934static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3935{
3936 int err = 0;
3937
3938 if (hba->auto_bkops_enabled)
3939 goto out;
3940
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003941 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303942 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3943 if (err) {
3944 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3945 __func__, err);
3946 goto out;
3947 }
3948
3949 hba->auto_bkops_enabled = true;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08003950 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303951
3952 /* No need of URGENT_BKOPS exception from the device */
3953 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3954 if (err)
3955 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3956 __func__, err);
3957out:
3958 return err;
3959}
3960
3961/**
3962 * ufshcd_disable_auto_bkops - block device in doing background operations
3963 * @hba: per-adapter instance
3964 *
3965 * Disabling background operations improves command response latency but
3966 * has drawback of device moving into critical state where the device is
3967 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3968 * host is idle so that BKOPS are managed effectively without any negative
3969 * impacts.
3970 *
3971 * Returns zero on success, non-zero on failure.
3972 */
3973static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3974{
3975 int err = 0;
3976
3977 if (!hba->auto_bkops_enabled)
3978 goto out;
3979
3980 /*
3981 * If host assisted BKOPs is to be enabled, make sure
3982 * urgent bkops exception is allowed.
3983 */
3984 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3985 if (err) {
3986 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3987 __func__, err);
3988 goto out;
3989 }
3990
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003991 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303992 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3993 if (err) {
3994 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3995 __func__, err);
3996 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3997 goto out;
3998 }
3999
4000 hba->auto_bkops_enabled = false;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08004001 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled");
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304002out:
4003 return err;
4004}
4005
4006/**
4007 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
4008 * @hba: per adapter instance
4009 *
4010 * After a device reset the device may toggle the BKOPS_EN flag
4011 * to default value. The s/w tracking variables should be updated
4012 * as well. Do this by forcing enable of auto bkops.
4013 */
4014static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
4015{
4016 hba->auto_bkops_enabled = false;
4017 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
4018 ufshcd_enable_auto_bkops(hba);
4019}
4020
4021static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
4022{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004023 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304024 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
4025}
4026
4027/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004028 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4029 * @hba: per-adapter instance
4030 * @status: bkops_status value
4031 *
4032 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4033 * flag in the device to permit background operations if the device
4034 * bkops_status is greater than or equal to "status" argument passed to
4035 * this function, disable otherwise.
4036 *
4037 * Returns 0 for success, non-zero in case of failure.
4038 *
4039 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4040 * to know whether auto bkops is enabled or disabled after this function
4041 * returns control to it.
4042 */
4043static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
4044 enum bkops_status status)
4045{
4046 int err;
4047 u32 curr_status = 0;
4048
4049 err = ufshcd_get_bkops_status(hba, &curr_status);
4050 if (err) {
4051 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4052 __func__, err);
4053 goto out;
4054 } else if (curr_status > BKOPS_STATUS_MAX) {
4055 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
4056 __func__, curr_status);
4057 err = -EINVAL;
4058 goto out;
4059 }
4060
4061 if (curr_status >= status)
4062 err = ufshcd_enable_auto_bkops(hba);
4063 else
4064 err = ufshcd_disable_auto_bkops(hba);
4065out:
4066 return err;
4067}
4068
4069/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304070 * ufshcd_urgent_bkops - handle urgent bkops exception event
4071 * @hba: per-adapter instance
4072 *
4073 * Enable fBackgroundOpsEn flag in the device to permit background
4074 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004075 *
4076 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4077 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304078 */
4079static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4080{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004081 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304082}
4083
4084static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4085{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004086 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304087 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4088}
4089
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004090static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4091{
4092 int err;
4093 u32 curr_status = 0;
4094
4095 if (hba->is_urgent_bkops_lvl_checked)
4096 goto enable_auto_bkops;
4097
4098 err = ufshcd_get_bkops_status(hba, &curr_status);
4099 if (err) {
4100 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4101 __func__, err);
4102 goto out;
4103 }
4104
4105 /*
4106 * We are seeing that some devices are raising the urgent bkops
4107 * exception events even when BKOPS status doesn't indicate performace
4108 * impacted or critical. Handle these device by determining their urgent
4109 * bkops status at runtime.
4110 */
4111 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4112 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4113 __func__, curr_status);
4114 /* update the current status as the urgent bkops level */
4115 hba->urgent_bkops_lvl = curr_status;
4116 hba->is_urgent_bkops_lvl_checked = true;
4117 }
4118
4119enable_auto_bkops:
4120 err = ufshcd_enable_auto_bkops(hba);
4121out:
4122 if (err < 0)
4123 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4124 __func__, err);
4125}
4126
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304127/**
4128 * ufshcd_exception_event_handler - handle exceptions raised by device
4129 * @work: pointer to work data
4130 *
4131 * Read bExceptionEventStatus attribute from the device and handle the
4132 * exception event accordingly.
4133 */
4134static void ufshcd_exception_event_handler(struct work_struct *work)
4135{
4136 struct ufs_hba *hba;
4137 int err;
4138 u32 status = 0;
4139 hba = container_of(work, struct ufs_hba, eeh_work);
4140
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304141 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304142 err = ufshcd_get_ee_status(hba, &status);
4143 if (err) {
4144 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4145 __func__, err);
4146 goto out;
4147 }
4148
4149 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004150
4151 if (status & MASK_EE_URGENT_BKOPS)
4152 ufshcd_bkops_exception_event_handler(hba);
4153
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304154out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304155 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304156 return;
4157}
4158
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004159/* Complete requests that have door-bell cleared */
4160static void ufshcd_complete_requests(struct ufs_hba *hba)
4161{
4162 ufshcd_transfer_req_compl(hba);
4163 ufshcd_tmc_handler(hba);
4164}
4165
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304166/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004167 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4168 * to recover from the DL NAC errors or not.
4169 * @hba: per-adapter instance
4170 *
4171 * Returns true if error handling is required, false otherwise
4172 */
4173static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4174{
4175 unsigned long flags;
4176 bool err_handling = true;
4177
4178 spin_lock_irqsave(hba->host->host_lock, flags);
4179 /*
4180 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4181 * device fatal error and/or DL NAC & REPLAY timeout errors.
4182 */
4183 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4184 goto out;
4185
4186 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4187 ((hba->saved_err & UIC_ERROR) &&
4188 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4189 goto out;
4190
4191 if ((hba->saved_err & UIC_ERROR) &&
4192 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4193 int err;
4194 /*
4195 * wait for 50ms to see if we can get any other errors or not.
4196 */
4197 spin_unlock_irqrestore(hba->host->host_lock, flags);
4198 msleep(50);
4199 spin_lock_irqsave(hba->host->host_lock, flags);
4200
4201 /*
4202 * now check if we have got any other severe errors other than
4203 * DL NAC error?
4204 */
4205 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4206 ((hba->saved_err & UIC_ERROR) &&
4207 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4208 goto out;
4209
4210 /*
4211 * As DL NAC is the only error received so far, send out NOP
4212 * command to confirm if link is still active or not.
4213 * - If we don't get any response then do error recovery.
4214 * - If we get response then clear the DL NAC error bit.
4215 */
4216
4217 spin_unlock_irqrestore(hba->host->host_lock, flags);
4218 err = ufshcd_verify_dev_init(hba);
4219 spin_lock_irqsave(hba->host->host_lock, flags);
4220
4221 if (err)
4222 goto out;
4223
4224 /* Link seems to be alive hence ignore the DL NAC errors */
4225 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4226 hba->saved_err &= ~UIC_ERROR;
4227 /* clear NAC error */
4228 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4229 if (!hba->saved_uic_err) {
4230 err_handling = false;
4231 goto out;
4232 }
4233 }
4234out:
4235 spin_unlock_irqrestore(hba->host->host_lock, flags);
4236 return err_handling;
4237}
4238
4239/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304240 * ufshcd_err_handler - handle UFS errors that require s/w attention
4241 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304242 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304243static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304244{
4245 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304246 unsigned long flags;
4247 u32 err_xfer = 0;
4248 u32 err_tm = 0;
4249 int err = 0;
4250 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004251 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304252
4253 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304254
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304255 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004256 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304257
4258 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004259 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304260 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304261
4262 hba->ufshcd_state = UFSHCD_STATE_RESET;
4263 ufshcd_set_eh_in_progress(hba);
4264
4265 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004266 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004267
4268 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4269 bool ret;
4270
4271 spin_unlock_irqrestore(hba->host->host_lock, flags);
4272 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4273 ret = ufshcd_quirk_dl_nac_errors(hba);
4274 spin_lock_irqsave(hba->host->host_lock, flags);
4275 if (!ret)
4276 goto skip_err_handling;
4277 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004278 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4279 ((hba->saved_err & UIC_ERROR) &&
4280 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4281 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4282 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4283 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304284
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004285 /*
4286 * if host reset is required then skip clearing the pending
4287 * transfers forcefully because they will automatically get
4288 * cleared after link startup.
4289 */
4290 if (needs_reset)
4291 goto skip_pending_xfer_clear;
4292
4293 /* release lock as clear command might sleep */
4294 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304295 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004296 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4297 if (ufshcd_clear_cmd(hba, tag)) {
4298 err_xfer = true;
4299 goto lock_skip_pending_xfer_clear;
4300 }
4301 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304302
4303 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004304 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4305 if (ufshcd_clear_tm_cmd(hba, tag)) {
4306 err_tm = true;
4307 goto lock_skip_pending_xfer_clear;
4308 }
4309 }
4310
4311lock_skip_pending_xfer_clear:
4312 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304313
4314 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004315 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304316
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004317 if (err_xfer || err_tm)
4318 needs_reset = true;
4319
4320skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304321 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004322 if (needs_reset) {
4323 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4324
4325 /*
4326 * ufshcd_reset_and_restore() does the link reinitialization
4327 * which will need atleast one empty doorbell slot to send the
4328 * device management commands (NOP and query commands).
4329 * If there is no slot empty at this moment then free up last
4330 * slot forcefully.
4331 */
4332 if (hba->outstanding_reqs == max_doorbells)
4333 __ufshcd_transfer_req_compl(hba,
4334 (1UL << (hba->nutrs - 1)));
4335
4336 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304337 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004338 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304339 if (err) {
4340 dev_err(hba->dev, "%s: reset and restore failed\n",
4341 __func__);
4342 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4343 }
4344 /*
4345 * Inform scsi mid-layer that we did reset and allow to handle
4346 * Unit Attention properly.
4347 */
4348 scsi_report_bus_reset(hba->host, 0);
4349 hba->saved_err = 0;
4350 hba->saved_uic_err = 0;
4351 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004352
Yaniv Gardi583fa622016-03-10 17:37:13 +02004353skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004354 if (!needs_reset) {
4355 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4356 if (hba->saved_err || hba->saved_uic_err)
4357 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4358 __func__, hba->saved_err, hba->saved_uic_err);
4359 }
4360
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304361 ufshcd_clear_eh_in_progress(hba);
4362
4363out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004364 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304365 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004366 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304367 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304368}
4369
4370/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304371 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4372 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304373 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304374static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304375{
4376 u32 reg;
4377
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08004378 /* PHY layer lane error */
4379 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
4380 /* Ignore LINERESET indication, as this is not an error */
4381 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
4382 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK))
4383 /*
4384 * To know whether this error is fatal or not, DB timeout
4385 * must be checked but this error is handled separately.
4386 */
4387 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
4388
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304389 /* PA_INIT_ERROR is fatal and needs UIC reset */
4390 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4391 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4392 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004393 else if (hba->dev_quirks &
4394 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4395 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4396 hba->uic_error |=
4397 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4398 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4399 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4400 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304401
4402 /* UIC NL/TL/DME errors needs software retry */
4403 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4404 if (reg)
4405 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4406
4407 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4408 if (reg)
4409 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4410
4411 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4412 if (reg)
4413 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4414
4415 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4416 __func__, hba->uic_error);
4417}
4418
4419/**
4420 * ufshcd_check_errors - Check for errors that need s/w attention
4421 * @hba: per-adapter instance
4422 */
4423static void ufshcd_check_errors(struct ufs_hba *hba)
4424{
4425 bool queue_eh_work = false;
4426
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304427 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304428 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304429
4430 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304431 hba->uic_error = 0;
4432 ufshcd_update_uic_error(hba);
4433 if (hba->uic_error)
4434 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304435 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304436
4437 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004438 /*
4439 * update the transfer error masks to sticky bits, let's do this
4440 * irrespective of current ufshcd_state.
4441 */
4442 hba->saved_err |= hba->errors;
4443 hba->saved_uic_err |= hba->uic_error;
4444
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304445 /* handle fatal errors only when link is functional */
4446 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4447 /* block commands from scsi mid-layer */
4448 scsi_block_requests(hba->host);
4449
Zang Leigang141f8162016-11-16 11:29:37 +08004450 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Dolev Raviv66cc8202016-12-22 18:39:42 -08004451
4452 /* dump controller state before resetting */
4453 if (hba->saved_err & (INT_FATAL_ERRORS | UIC_ERROR)) {
4454 bool pr_prdt = !!(hba->saved_err &
4455 SYSTEM_BUS_FATAL_ERROR);
4456
4457 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
4458 __func__, hba->saved_err,
4459 hba->saved_uic_err);
4460
4461 ufshcd_print_host_regs(hba);
4462 ufshcd_print_pwr_info(hba);
4463 ufshcd_print_tmrs(hba, hba->outstanding_tasks);
4464 ufshcd_print_trs(hba, hba->outstanding_reqs,
4465 pr_prdt);
4466 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304467 schedule_work(&hba->eh_work);
4468 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304469 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304470 /*
4471 * if (!queue_eh_work) -
4472 * Other errors are either non-fatal where host recovers
4473 * itself without s/w intervention or errors that will be
4474 * handled by the SCSI core layer.
4475 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304476}
4477
4478/**
4479 * ufshcd_tmc_handler - handle task management function completion
4480 * @hba: per adapter instance
4481 */
4482static void ufshcd_tmc_handler(struct ufs_hba *hba)
4483{
4484 u32 tm_doorbell;
4485
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304486 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304487 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304488 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304489}
4490
4491/**
4492 * ufshcd_sl_intr - Interrupt service routine
4493 * @hba: per adapter instance
4494 * @intr_status: contains interrupts generated by the controller
4495 */
4496static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4497{
4498 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4499 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304500 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304501
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304502 if (intr_status & UFSHCD_UIC_MASK)
4503 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304504
4505 if (intr_status & UTP_TASK_REQ_COMPL)
4506 ufshcd_tmc_handler(hba);
4507
4508 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4509 ufshcd_transfer_req_compl(hba);
4510}
4511
4512/**
4513 * ufshcd_intr - Main interrupt service routine
4514 * @irq: irq number
4515 * @__hba: pointer to adapter instance
4516 *
4517 * Returns IRQ_HANDLED - If interrupt is valid
4518 * IRQ_NONE - If invalid interrupt
4519 */
4520static irqreturn_t ufshcd_intr(int irq, void *__hba)
4521{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004522 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304523 irqreturn_t retval = IRQ_NONE;
4524 struct ufs_hba *hba = __hba;
4525
4526 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304527 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004528 enabled_intr_status =
4529 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304530
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004531 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304532 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004533
4534 if (enabled_intr_status) {
4535 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304536 retval = IRQ_HANDLED;
4537 }
4538 spin_unlock(hba->host->host_lock);
4539 return retval;
4540}
4541
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304542static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4543{
4544 int err = 0;
4545 u32 mask = 1 << tag;
4546 unsigned long flags;
4547
4548 if (!test_bit(tag, &hba->outstanding_tasks))
4549 goto out;
4550
4551 spin_lock_irqsave(hba->host->host_lock, flags);
4552 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4553 spin_unlock_irqrestore(hba->host->host_lock, flags);
4554
4555 /* poll for max. 1 sec to clear door bell register by h/w */
4556 err = ufshcd_wait_for_register(hba,
4557 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004558 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304559out:
4560 return err;
4561}
4562
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304563/**
4564 * ufshcd_issue_tm_cmd - issues task management commands to controller
4565 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304566 * @lun_id: LUN ID to which TM command is sent
4567 * @task_id: task ID to which the TM command is applicable
4568 * @tm_function: task management function opcode
4569 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304570 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304571 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304572 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304573static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4574 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304575{
4576 struct utp_task_req_desc *task_req_descp;
4577 struct utp_upiu_task_req *task_req_upiup;
4578 struct Scsi_Host *host;
4579 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304580 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304581 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304582 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304583
4584 host = hba->host;
4585
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304586 /*
4587 * Get free slot, sleep if slots are unavailable.
4588 * Even though we use wait_event() which sleeps indefinitely,
4589 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4590 */
4591 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004592 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304593
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304594 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304595 task_req_descp = hba->utmrdl_base_addr;
4596 task_req_descp += free_slot;
4597
4598 /* Configure task request descriptor */
4599 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4600 task_req_descp->header.dword_2 =
4601 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4602
4603 /* Configure task request UPIU */
4604 task_req_upiup =
4605 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304606 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304607 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304608 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304609 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304610 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304611 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004612 /*
4613 * The host shall provide the same value for LUN field in the basic
4614 * header and for Input Parameter.
4615 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304616 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4617 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304618
Kiwoong Kimd2877be2016-11-10 21:16:15 +09004619 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
4620
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304621 /* send command to the controller */
4622 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004623
4624 /* Make sure descriptors are ready before ringing the task doorbell */
4625 wmb();
4626
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304627 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004628 /* Make sure that doorbell is committed immediately */
4629 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304630
4631 spin_unlock_irqrestore(host->host_lock, flags);
4632
4633 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304634 err = wait_event_timeout(hba->tm_wq,
4635 test_bit(free_slot, &hba->tm_condition),
4636 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304637 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304638 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4639 __func__, tm_function);
4640 if (ufshcd_clear_tm_cmd(hba, free_slot))
4641 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4642 __func__, free_slot);
4643 err = -ETIMEDOUT;
4644 } else {
4645 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304646 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304647
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304648 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304649 ufshcd_put_tm_slot(hba, free_slot);
4650 wake_up(&hba->tm_tag_wq);
4651
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004652 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304653 return err;
4654}
4655
4656/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304657 * ufshcd_eh_device_reset_handler - device reset handler registered to
4658 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304659 * @cmd: SCSI command pointer
4660 *
4661 * Returns SUCCESS/FAILED
4662 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304663static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304664{
4665 struct Scsi_Host *host;
4666 struct ufs_hba *hba;
4667 unsigned int tag;
4668 u32 pos;
4669 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304670 u8 resp = 0xF;
4671 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304672 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304673
4674 host = cmd->device->host;
4675 hba = shost_priv(host);
4676 tag = cmd->request->tag;
4677
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304678 lrbp = &hba->lrb[tag];
4679 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4680 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304681 if (!err)
4682 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304683 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304684 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304685
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304686 /* clear the commands that were pending for corresponding LUN */
4687 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4688 if (hba->lrb[pos].lun == lrbp->lun) {
4689 err = ufshcd_clear_cmd(hba, pos);
4690 if (err)
4691 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304692 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304693 }
4694 spin_lock_irqsave(host->host_lock, flags);
4695 ufshcd_transfer_req_compl(hba);
4696 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304697out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304698 if (!err) {
4699 err = SUCCESS;
4700 } else {
4701 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4702 err = FAILED;
4703 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304704 return err;
4705}
4706
4707/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304708 * ufshcd_abort - abort a specific command
4709 * @cmd: SCSI command pointer
4710 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304711 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4712 * command, and in host controller by clearing the door-bell register. There can
4713 * be race between controller sending the command to the device while abort is
4714 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4715 * really issued and then try to abort it.
4716 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304717 * Returns SUCCESS/FAILED
4718 */
4719static int ufshcd_abort(struct scsi_cmnd *cmd)
4720{
4721 struct Scsi_Host *host;
4722 struct ufs_hba *hba;
4723 unsigned long flags;
4724 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304725 int err = 0;
4726 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304727 u8 resp = 0xF;
4728 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004729 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304730
4731 host = cmd->device->host;
4732 hba = shost_priv(host);
4733 tag = cmd->request->tag;
Dolev Ravive7d38252016-12-22 18:40:07 -08004734 lrbp = &hba->lrb[tag];
Yaniv Gardi14497322016-02-01 15:02:39 +02004735 if (!ufshcd_valid_tag(hba, tag)) {
4736 dev_err(hba->dev,
4737 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4738 __func__, tag, cmd, cmd->request);
4739 BUG();
4740 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304741
Dolev Ravive7d38252016-12-22 18:40:07 -08004742 /*
4743 * Task abort to the device W-LUN is illegal. When this command
4744 * will fail, due to spec violation, scsi err handling next step
4745 * will be to send LU reset which, again, is a spec violation.
4746 * To avoid these unnecessary/illegal step we skip to the last error
4747 * handling stage: reset and restore.
4748 */
4749 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN)
4750 return ufshcd_eh_host_reset_handler(cmd);
4751
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004752 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004753 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004754 /* If command is already aborted/completed, return SUCCESS */
4755 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4756 dev_err(hba->dev,
4757 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4758 __func__, tag, hba->outstanding_reqs, reg);
4759 goto out;
4760 }
4761
Dolev Ravive9d501b2014-07-01 12:22:37 +03004762 if (!(reg & (1 << tag))) {
4763 dev_err(hba->dev,
4764 "%s: cmd was completed, but without a notifying intr, tag = %d",
4765 __func__, tag);
4766 }
4767
Dolev Raviv66cc8202016-12-22 18:39:42 -08004768 /* Print Transfer Request of aborted task */
4769 dev_err(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag);
4770 scsi_print_command(hba->lrb[tag].cmd);
4771 ufshcd_print_host_regs(hba);
4772 ufshcd_print_pwr_info(hba);
4773 ufshcd_print_trs(hba, 1 << tag, true);
4774
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304775 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4776 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4777 UFS_QUERY_TASK, &resp);
4778 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4779 /* cmd pending in the device */
4780 break;
4781 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304782 /*
4783 * cmd not pending in the device, check if it is
4784 * in transition.
4785 */
4786 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4787 if (reg & (1 << tag)) {
4788 /* sleep for max. 200us to stabilize */
4789 usleep_range(100, 200);
4790 continue;
4791 }
4792 /* command completed already */
4793 goto out;
4794 } else {
4795 if (!err)
4796 err = resp; /* service response error */
4797 goto out;
4798 }
4799 }
4800
4801 if (!poll_cnt) {
4802 err = -EBUSY;
4803 goto out;
4804 }
4805
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304806 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4807 UFS_ABORT_TASK, &resp);
4808 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304809 if (!err)
4810 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304811 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304812 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304813
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304814 err = ufshcd_clear_cmd(hba, tag);
4815 if (err)
4816 goto out;
4817
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304818 scsi_dma_unmap(cmd);
4819
4820 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004821 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304822 hba->lrb[tag].cmd = NULL;
4823 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304824
4825 clear_bit_unlock(tag, &hba->lrb_in_use);
4826 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004827
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304828out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304829 if (!err) {
4830 err = SUCCESS;
4831 } else {
4832 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4833 err = FAILED;
4834 }
4835
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004836 /*
4837 * This ufshcd_release() corresponds to the original scsi cmd that got
4838 * aborted here (as we won't get any IRQ for it).
4839 */
4840 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304841 return err;
4842}
4843
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304844/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304845 * ufshcd_host_reset_and_restore - reset and restore host controller
4846 * @hba: per-adapter instance
4847 *
4848 * Note that host controller reset may issue DME_RESET to
4849 * local and remote (device) Uni-Pro stack and the attributes
4850 * are reset to default state.
4851 *
4852 * Returns zero on success, non-zero on failure
4853 */
4854static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4855{
4856 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304857 unsigned long flags;
4858
4859 /* Reset the host controller */
4860 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004861 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304862 spin_unlock_irqrestore(hba->host->host_lock, flags);
4863
4864 err = ufshcd_hba_enable(hba);
4865 if (err)
4866 goto out;
4867
4868 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004869 err = ufshcd_probe_hba(hba);
4870
4871 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304872 err = -EIO;
4873out:
4874 if (err)
4875 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4876
4877 return err;
4878}
4879
4880/**
4881 * ufshcd_reset_and_restore - reset and re-initialize host/device
4882 * @hba: per-adapter instance
4883 *
4884 * Reset and recover device, host and re-establish link. This
4885 * is helpful to recover the communication in fatal error conditions.
4886 *
4887 * Returns zero on success, non-zero on failure
4888 */
4889static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4890{
4891 int err = 0;
4892 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004893 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304894
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004895 do {
4896 err = ufshcd_host_reset_and_restore(hba);
4897 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304898
4899 /*
4900 * After reset the door-bell might be cleared, complete
4901 * outstanding requests in s/w here.
4902 */
4903 spin_lock_irqsave(hba->host->host_lock, flags);
4904 ufshcd_transfer_req_compl(hba);
4905 ufshcd_tmc_handler(hba);
4906 spin_unlock_irqrestore(hba->host->host_lock, flags);
4907
4908 return err;
4909}
4910
4911/**
4912 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4913 * @cmd - SCSI command pointer
4914 *
4915 * Returns SUCCESS/FAILED
4916 */
4917static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4918{
4919 int err;
4920 unsigned long flags;
4921 struct ufs_hba *hba;
4922
4923 hba = shost_priv(cmd->device->host);
4924
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004925 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304926 /*
4927 * Check if there is any race with fatal error handling.
4928 * If so, wait for it to complete. Even though fatal error
4929 * handling does reset and restore in some cases, don't assume
4930 * anything out of it. We are just avoiding race here.
4931 */
4932 do {
4933 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304934 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304935 hba->ufshcd_state == UFSHCD_STATE_RESET))
4936 break;
4937 spin_unlock_irqrestore(hba->host->host_lock, flags);
4938 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304939 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304940 } while (1);
4941
4942 hba->ufshcd_state = UFSHCD_STATE_RESET;
4943 ufshcd_set_eh_in_progress(hba);
4944 spin_unlock_irqrestore(hba->host->host_lock, flags);
4945
4946 err = ufshcd_reset_and_restore(hba);
4947
4948 spin_lock_irqsave(hba->host->host_lock, flags);
4949 if (!err) {
4950 err = SUCCESS;
4951 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4952 } else {
4953 err = FAILED;
4954 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4955 }
4956 ufshcd_clear_eh_in_progress(hba);
4957 spin_unlock_irqrestore(hba->host->host_lock, flags);
4958
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004959 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304960 return err;
4961}
4962
4963/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004964 * ufshcd_get_max_icc_level - calculate the ICC level
4965 * @sup_curr_uA: max. current supported by the regulator
4966 * @start_scan: row at the desc table to start scan from
4967 * @buff: power descriptor buffer
4968 *
4969 * Returns calculated max ICC level for specific regulator
4970 */
4971static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4972{
4973 int i;
4974 int curr_uA;
4975 u16 data;
4976 u16 unit;
4977
4978 for (i = start_scan; i >= 0; i--) {
4979 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4980 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4981 ATTR_ICC_LVL_UNIT_OFFSET;
4982 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4983 switch (unit) {
4984 case UFSHCD_NANO_AMP:
4985 curr_uA = curr_uA / 1000;
4986 break;
4987 case UFSHCD_MILI_AMP:
4988 curr_uA = curr_uA * 1000;
4989 break;
4990 case UFSHCD_AMP:
4991 curr_uA = curr_uA * 1000 * 1000;
4992 break;
4993 case UFSHCD_MICRO_AMP:
4994 default:
4995 break;
4996 }
4997 if (sup_curr_uA >= curr_uA)
4998 break;
4999 }
5000 if (i < 0) {
5001 i = 0;
5002 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
5003 }
5004
5005 return (u32)i;
5006}
5007
5008/**
5009 * ufshcd_calc_icc_level - calculate the max ICC level
5010 * In case regulators are not initialized we'll return 0
5011 * @hba: per-adapter instance
5012 * @desc_buf: power descriptor buffer to extract ICC levels from.
5013 * @len: length of desc_buff
5014 *
5015 * Returns calculated ICC level
5016 */
5017static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
5018 u8 *desc_buf, int len)
5019{
5020 u32 icc_level = 0;
5021
5022 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
5023 !hba->vreg_info.vccq2) {
5024 dev_err(hba->dev,
5025 "%s: Regulator capability was not set, actvIccLevel=%d",
5026 __func__, icc_level);
5027 goto out;
5028 }
5029
5030 if (hba->vreg_info.vcc)
5031 icc_level = ufshcd_get_max_icc_level(
5032 hba->vreg_info.vcc->max_uA,
5033 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
5034 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
5035
5036 if (hba->vreg_info.vccq)
5037 icc_level = ufshcd_get_max_icc_level(
5038 hba->vreg_info.vccq->max_uA,
5039 icc_level,
5040 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
5041
5042 if (hba->vreg_info.vccq2)
5043 icc_level = ufshcd_get_max_icc_level(
5044 hba->vreg_info.vccq2->max_uA,
5045 icc_level,
5046 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
5047out:
5048 return icc_level;
5049}
5050
Dolev Raviv61e07352016-11-23 16:30:49 -08005051static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
5052{
5053 int ret = 0;
5054 int retries;
5055
5056 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
5057 /* write attribute */
5058 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
5059 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
5060 if (!ret)
5061 break;
5062
5063 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
5064 }
5065
5066 return ret;
5067}
5068
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005069static void ufshcd_init_icc_levels(struct ufs_hba *hba)
5070{
5071 int ret;
5072 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
5073 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
5074
5075 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
5076 if (ret) {
5077 dev_err(hba->dev,
5078 "%s: Failed reading power descriptor.len = %d ret = %d",
5079 __func__, buff_len, ret);
5080 return;
5081 }
5082
5083 hba->init_prefetch_data.icc_level =
5084 ufshcd_find_max_sup_active_icc_level(hba,
5085 desc_buf, buff_len);
5086 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
5087 __func__, hba->init_prefetch_data.icc_level);
5088
Dolev Raviv61e07352016-11-23 16:30:49 -08005089 ret = ufshcd_set_icc_levels_attr(hba,
5090 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005091
5092 if (ret)
5093 dev_err(hba->dev,
5094 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5095 __func__, hba->init_prefetch_data.icc_level , ret);
5096
5097}
5098
5099/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005100 * ufshcd_scsi_add_wlus - Adds required W-LUs
5101 * @hba: per-adapter instance
5102 *
5103 * UFS device specification requires the UFS devices to support 4 well known
5104 * logical units:
5105 * "REPORT_LUNS" (address: 01h)
5106 * "UFS Device" (address: 50h)
5107 * "RPMB" (address: 44h)
5108 * "BOOT" (address: 30h)
5109 * UFS device's power management needs to be controlled by "POWER CONDITION"
5110 * field of SSU (START STOP UNIT) command. But this "power condition" field
5111 * will take effect only when its sent to "UFS device" well known logical unit
5112 * hence we require the scsi_device instance to represent this logical unit in
5113 * order for the UFS host driver to send the SSU command for power management.
5114
5115 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5116 * Block) LU so user space process can control this LU. User space may also
5117 * want to have access to BOOT LU.
5118
5119 * This function adds scsi device instances for each of all well known LUs
5120 * (except "REPORT LUNS" LU).
5121 *
5122 * Returns zero on success (all required W-LUs are added successfully),
5123 * non-zero error value on failure (if failed to add any of the required W-LU).
5124 */
5125static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
5126{
5127 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005128 struct scsi_device *sdev_rpmb;
5129 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005130
5131 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
5132 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
5133 if (IS_ERR(hba->sdev_ufs_device)) {
5134 ret = PTR_ERR(hba->sdev_ufs_device);
5135 hba->sdev_ufs_device = NULL;
5136 goto out;
5137 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005138 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005139
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005140 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005141 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005142 if (IS_ERR(sdev_boot)) {
5143 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005144 goto remove_sdev_ufs_device;
5145 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005146 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005147
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005148 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005149 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005150 if (IS_ERR(sdev_rpmb)) {
5151 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005152 goto remove_sdev_boot;
5153 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005154 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005155 goto out;
5156
5157remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005158 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005159remove_sdev_ufs_device:
5160 scsi_remove_device(hba->sdev_ufs_device);
5161out:
5162 return ret;
5163}
5164
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005165static int ufs_get_device_info(struct ufs_hba *hba,
5166 struct ufs_device_info *card_data)
5167{
5168 int err;
5169 u8 model_index;
5170 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
5171 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
5172
5173 err = ufshcd_read_device_desc(hba, desc_buf,
5174 QUERY_DESC_DEVICE_MAX_SIZE);
5175 if (err) {
5176 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
5177 __func__, err);
5178 goto out;
5179 }
5180
5181 /*
5182 * getting vendor (manufacturerID) and Bank Index in big endian
5183 * format
5184 */
5185 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
5186 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
5187
5188 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
5189
5190 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
5191 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
5192 if (err) {
5193 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5194 __func__, err);
5195 goto out;
5196 }
5197
5198 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
5199 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
5200 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5201 MAX_MODEL_LEN));
5202
5203 /* Null terminate the model string */
5204 card_data->model[MAX_MODEL_LEN] = '\0';
5205
5206out:
5207 return err;
5208}
5209
5210void ufs_advertise_fixup_device(struct ufs_hba *hba)
5211{
5212 int err;
5213 struct ufs_dev_fix *f;
5214 struct ufs_device_info card_data;
5215
5216 card_data.wmanufacturerid = 0;
5217
5218 err = ufs_get_device_info(hba, &card_data);
5219 if (err) {
5220 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5221 __func__, err);
5222 return;
5223 }
5224
5225 for (f = ufs_fixups; f->quirk; f++) {
5226 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
5227 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
5228 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
5229 !strcmp(f->card.model, UFS_ANY_MODEL)))
5230 hba->dev_quirks |= f->quirk;
5231 }
5232}
5233
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005234/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005235 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5236 * @hba: per-adapter instance
5237 *
5238 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5239 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5240 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5241 * the hibern8 exit latency.
5242 *
5243 * Returns zero on success, non-zero error value on failure.
5244 */
5245static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5246{
5247 int ret = 0;
5248 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5249
5250 ret = ufshcd_dme_peer_get(hba,
5251 UIC_ARG_MIB_SEL(
5252 RX_MIN_ACTIVATETIME_CAPABILITY,
5253 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5254 &peer_rx_min_activatetime);
5255 if (ret)
5256 goto out;
5257
5258 /* make sure proper unit conversion is applied */
5259 tuned_pa_tactivate =
5260 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5261 / PA_TACTIVATE_TIME_UNIT_US);
5262 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5263 tuned_pa_tactivate);
5264
5265out:
5266 return ret;
5267}
5268
5269/**
5270 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5271 * @hba: per-adapter instance
5272 *
5273 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5274 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5275 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5276 * This optimal value can help reduce the hibern8 exit latency.
5277 *
5278 * Returns zero on success, non-zero error value on failure.
5279 */
5280static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5281{
5282 int ret = 0;
5283 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5284 u32 max_hibern8_time, tuned_pa_hibern8time;
5285
5286 ret = ufshcd_dme_get(hba,
5287 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5288 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5289 &local_tx_hibern8_time_cap);
5290 if (ret)
5291 goto out;
5292
5293 ret = ufshcd_dme_peer_get(hba,
5294 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5295 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5296 &peer_rx_hibern8_time_cap);
5297 if (ret)
5298 goto out;
5299
5300 max_hibern8_time = max(local_tx_hibern8_time_cap,
5301 peer_rx_hibern8_time_cap);
5302 /* make sure proper unit conversion is applied */
5303 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5304 / PA_HIBERN8_TIME_UNIT_US);
5305 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5306 tuned_pa_hibern8time);
5307out:
5308 return ret;
5309}
5310
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005311/**
5312 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5313 * less than device PA_TACTIVATE time.
5314 * @hba: per-adapter instance
5315 *
5316 * Some UFS devices require host PA_TACTIVATE to be lower than device
5317 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5318 * for such devices.
5319 *
5320 * Returns zero on success, non-zero error value on failure.
5321 */
5322static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5323{
5324 int ret = 0;
5325 u32 granularity, peer_granularity;
5326 u32 pa_tactivate, peer_pa_tactivate;
5327 u32 pa_tactivate_us, peer_pa_tactivate_us;
5328 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5329
5330 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5331 &granularity);
5332 if (ret)
5333 goto out;
5334
5335 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5336 &peer_granularity);
5337 if (ret)
5338 goto out;
5339
5340 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5341 (granularity > PA_GRANULARITY_MAX_VAL)) {
5342 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5343 __func__, granularity);
5344 return -EINVAL;
5345 }
5346
5347 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5348 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5349 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5350 __func__, peer_granularity);
5351 return -EINVAL;
5352 }
5353
5354 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5355 if (ret)
5356 goto out;
5357
5358 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5359 &peer_pa_tactivate);
5360 if (ret)
5361 goto out;
5362
5363 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5364 peer_pa_tactivate_us = peer_pa_tactivate *
5365 gran_to_us_table[peer_granularity - 1];
5366
5367 if (pa_tactivate_us > peer_pa_tactivate_us) {
5368 u32 new_peer_pa_tactivate;
5369
5370 new_peer_pa_tactivate = pa_tactivate_us /
5371 gran_to_us_table[peer_granularity - 1];
5372 new_peer_pa_tactivate++;
5373 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5374 new_peer_pa_tactivate);
5375 }
5376
5377out:
5378 return ret;
5379}
5380
Yaniv Gardi37113102016-03-10 17:37:16 +02005381static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5382{
5383 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5384 ufshcd_tune_pa_tactivate(hba);
5385 ufshcd_tune_pa_hibern8time(hba);
5386 }
5387
5388 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5389 /* set 1ms timeout for PA_TACTIVATE */
5390 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005391
5392 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5393 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavani56d4a182016-12-05 19:25:32 -08005394
5395 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005396}
5397
5398/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005399 * ufshcd_probe_hba - probe hba to detect device and initialize
5400 * @hba: per-adapter instance
5401 *
5402 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305403 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005404static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305405{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305406 int ret;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005407 ktime_t start = ktime_get();
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305408
5409 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305410 if (ret)
5411 goto out;
5412
Yaniv Gardi50646362014-10-23 13:25:13 +03005413 ufshcd_init_pwr_info(hba);
Dolev Raviv66cc8202016-12-22 18:39:42 -08005414 ufshcd_print_pwr_info(hba);
Yaniv Gardi50646362014-10-23 13:25:13 +03005415
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005416 /* set the default level for urgent bkops */
5417 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5418 hba->is_urgent_bkops_lvl_checked = false;
5419
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005420 /* UniPro link is active now */
5421 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305422
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305423 ret = ufshcd_verify_dev_init(hba);
5424 if (ret)
5425 goto out;
5426
Dolev Raviv68078d52013-07-30 00:35:58 +05305427 ret = ufshcd_complete_dev_init(hba);
5428 if (ret)
5429 goto out;
5430
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005431 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005432 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005433
5434 ret = ufshcd_set_vccq_rail_unused(hba,
5435 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5436 if (ret)
5437 goto out;
5438
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005439 /* UFS device is also active now */
5440 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305441 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005442 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305443
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005444 if (ufshcd_get_max_pwr_mode(hba)) {
5445 dev_err(hba->dev,
5446 "%s: Failed getting max supported power mode\n",
5447 __func__);
5448 } else {
5449 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005450 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005451 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5452 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005453 goto out;
5454 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005455 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005456
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005457 /* set the state as operational after switching to desired gear */
5458 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005459 /*
5460 * If we are in error handling context or in power management callbacks
5461 * context, no need to scan the host
5462 */
5463 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5464 bool flag;
5465
5466 /* clear any previous UFS device information */
5467 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005468 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5469 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005470 hba->dev_info.f_power_on_wp_en = flag;
5471
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005472 if (!hba->is_init_prefetch)
5473 ufshcd_init_icc_levels(hba);
5474
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005475 /* Add required well known logical units to scsi mid layer */
5476 if (ufshcd_scsi_add_wlus(hba))
5477 goto out;
5478
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305479 scsi_scan_host(hba->host);
5480 pm_runtime_put_sync(hba->dev);
5481 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005482
5483 if (!hba->is_init_prefetch)
5484 hba->is_init_prefetch = true;
5485
Sahitya Tummala856b3482014-09-25 15:32:34 +03005486 /* Resume devfreq after UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07005487 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005488
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305489out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005490 /*
5491 * If we failed to initialize the device or the device is not
5492 * present, turn off the power/clocks etc.
5493 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005494 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5495 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005496 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005497 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005498
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005499 trace_ufshcd_init(dev_name(hba->dev), ret,
5500 ktime_to_us(ktime_sub(ktime_get(), start)),
5501 hba->uic_link_state, hba->curr_dev_pwr_mode);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005502 return ret;
5503}
5504
5505/**
5506 * ufshcd_async_scan - asynchronous execution for probing hba
5507 * @data: data pointer to pass to this function
5508 * @cookie: cookie data
5509 */
5510static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5511{
5512 struct ufs_hba *hba = (struct ufs_hba *)data;
5513
5514 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305515}
5516
Yaniv Gardif550c652016-03-10 17:37:07 +02005517static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5518{
5519 unsigned long flags;
5520 struct Scsi_Host *host;
5521 struct ufs_hba *hba;
5522 int index;
5523 bool found = false;
5524
5525 if (!scmd || !scmd->device || !scmd->device->host)
5526 return BLK_EH_NOT_HANDLED;
5527
5528 host = scmd->device->host;
5529 hba = shost_priv(host);
5530 if (!hba)
5531 return BLK_EH_NOT_HANDLED;
5532
5533 spin_lock_irqsave(host->host_lock, flags);
5534
5535 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5536 if (hba->lrb[index].cmd == scmd) {
5537 found = true;
5538 break;
5539 }
5540 }
5541
5542 spin_unlock_irqrestore(host->host_lock, flags);
5543
5544 /*
5545 * Bypass SCSI error handling and reset the block layer timer if this
5546 * SCSI command was not actually dispatched to UFS driver, otherwise
5547 * let SCSI layer handle the error as usual.
5548 */
5549 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5550}
5551
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305552static struct scsi_host_template ufshcd_driver_template = {
5553 .module = THIS_MODULE,
5554 .name = UFSHCD,
5555 .proc_name = UFSHCD,
5556 .queuecommand = ufshcd_queuecommand,
5557 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005558 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305559 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005560 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305561 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305562 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5563 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005564 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305565 .this_id = -1,
5566 .sg_tablesize = SG_ALL,
5567 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5568 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005569 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005570 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305571};
5572
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005573static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5574 int ua)
5575{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005576 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005577
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005578 if (!vreg)
5579 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005580
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005581 ret = regulator_set_load(vreg->reg, ua);
5582 if (ret < 0) {
5583 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5584 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005585 }
5586
5587 return ret;
5588}
5589
5590static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5591 struct ufs_vreg *vreg)
5592{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005593 if (!vreg)
5594 return 0;
5595 else if (vreg->unused)
5596 return 0;
5597 else
5598 return ufshcd_config_vreg_load(hba->dev, vreg,
5599 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005600}
5601
5602static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5603 struct ufs_vreg *vreg)
5604{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005605 if (!vreg)
5606 return 0;
5607 else if (vreg->unused)
5608 return 0;
5609 else
5610 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005611}
5612
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005613static int ufshcd_config_vreg(struct device *dev,
5614 struct ufs_vreg *vreg, bool on)
5615{
5616 int ret = 0;
5617 struct regulator *reg = vreg->reg;
5618 const char *name = vreg->name;
5619 int min_uV, uA_load;
5620
5621 BUG_ON(!vreg);
5622
5623 if (regulator_count_voltages(reg) > 0) {
5624 min_uV = on ? vreg->min_uV : 0;
5625 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5626 if (ret) {
5627 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5628 __func__, name, ret);
5629 goto out;
5630 }
5631
5632 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005633 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5634 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005635 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005636 }
5637out:
5638 return ret;
5639}
5640
5641static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5642{
5643 int ret = 0;
5644
Yaniv Gardi60f01872016-03-10 17:37:11 +02005645 if (!vreg)
5646 goto out;
5647 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005648 goto out;
5649
5650 ret = ufshcd_config_vreg(dev, vreg, true);
5651 if (!ret)
5652 ret = regulator_enable(vreg->reg);
5653
5654 if (!ret)
5655 vreg->enabled = true;
5656 else
5657 dev_err(dev, "%s: %s enable failed, err=%d\n",
5658 __func__, vreg->name, ret);
5659out:
5660 return ret;
5661}
5662
5663static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5664{
5665 int ret = 0;
5666
Yaniv Gardi60f01872016-03-10 17:37:11 +02005667 if (!vreg)
5668 goto out;
5669 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005670 goto out;
5671
5672 ret = regulator_disable(vreg->reg);
5673
5674 if (!ret) {
5675 /* ignore errors on applying disable config */
5676 ufshcd_config_vreg(dev, vreg, false);
5677 vreg->enabled = false;
5678 } else {
5679 dev_err(dev, "%s: %s disable failed, err=%d\n",
5680 __func__, vreg->name, ret);
5681 }
5682out:
5683 return ret;
5684}
5685
5686static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5687{
5688 int ret = 0;
5689 struct device *dev = hba->dev;
5690 struct ufs_vreg_info *info = &hba->vreg_info;
5691
5692 if (!info)
5693 goto out;
5694
5695 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5696 if (ret)
5697 goto out;
5698
5699 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5700 if (ret)
5701 goto out;
5702
5703 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5704 if (ret)
5705 goto out;
5706
5707out:
5708 if (ret) {
5709 ufshcd_toggle_vreg(dev, info->vccq2, false);
5710 ufshcd_toggle_vreg(dev, info->vccq, false);
5711 ufshcd_toggle_vreg(dev, info->vcc, false);
5712 }
5713 return ret;
5714}
5715
Raviv Shvili6a771a62014-09-25 15:32:24 +03005716static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5717{
5718 struct ufs_vreg_info *info = &hba->vreg_info;
5719
5720 if (info)
5721 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5722
5723 return 0;
5724}
5725
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005726static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5727{
5728 int ret = 0;
5729
5730 if (!vreg)
5731 goto out;
5732
5733 vreg->reg = devm_regulator_get(dev, vreg->name);
5734 if (IS_ERR(vreg->reg)) {
5735 ret = PTR_ERR(vreg->reg);
5736 dev_err(dev, "%s: %s get failed, err=%d\n",
5737 __func__, vreg->name, ret);
5738 }
5739out:
5740 return ret;
5741}
5742
5743static int ufshcd_init_vreg(struct ufs_hba *hba)
5744{
5745 int ret = 0;
5746 struct device *dev = hba->dev;
5747 struct ufs_vreg_info *info = &hba->vreg_info;
5748
5749 if (!info)
5750 goto out;
5751
5752 ret = ufshcd_get_vreg(dev, info->vcc);
5753 if (ret)
5754 goto out;
5755
5756 ret = ufshcd_get_vreg(dev, info->vccq);
5757 if (ret)
5758 goto out;
5759
5760 ret = ufshcd_get_vreg(dev, info->vccq2);
5761out:
5762 return ret;
5763}
5764
Raviv Shvili6a771a62014-09-25 15:32:24 +03005765static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5766{
5767 struct ufs_vreg_info *info = &hba->vreg_info;
5768
5769 if (info)
5770 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5771
5772 return 0;
5773}
5774
Yaniv Gardi60f01872016-03-10 17:37:11 +02005775static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5776{
5777 int ret = 0;
5778 struct ufs_vreg_info *info = &hba->vreg_info;
5779
5780 if (!info)
5781 goto out;
5782 else if (!info->vccq)
5783 goto out;
5784
5785 if (unused) {
5786 /* shut off the rail here */
5787 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5788 /*
5789 * Mark this rail as no longer used, so it doesn't get enabled
5790 * later by mistake
5791 */
5792 if (!ret)
5793 info->vccq->unused = true;
5794 } else {
5795 /*
5796 * rail should have been already enabled hence just make sure
5797 * that unused flag is cleared.
5798 */
5799 info->vccq->unused = false;
5800 }
5801out:
5802 return ret;
5803}
5804
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005805static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5806 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005807{
5808 int ret = 0;
5809 struct ufs_clk_info *clki;
5810 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005811 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005812
5813 if (!head || list_empty(head))
5814 goto out;
5815
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005816 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5817 if (ret)
5818 return ret;
5819
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005820 list_for_each_entry(clki, head, list) {
5821 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005822 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5823 continue;
5824
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005825 if (on && !clki->enabled) {
5826 ret = clk_prepare_enable(clki->clk);
5827 if (ret) {
5828 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5829 __func__, clki->name, ret);
5830 goto out;
5831 }
5832 } else if (!on && clki->enabled) {
5833 clk_disable_unprepare(clki->clk);
5834 }
5835 clki->enabled = on;
5836 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5837 clki->name, on ? "en" : "dis");
5838 }
5839 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005840
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005841 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5842 if (ret)
5843 return ret;
5844
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005845out:
5846 if (ret) {
5847 list_for_each_entry(clki, head, list) {
5848 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5849 clk_disable_unprepare(clki->clk);
5850 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005851 } else if (!ret && on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005852 spin_lock_irqsave(hba->host->host_lock, flags);
5853 hba->clk_gating.state = CLKS_ON;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005854 trace_ufshcd_clk_gating(dev_name(hba->dev),
5855 hba->clk_gating.state);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005856 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005857 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08005858
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005859 return ret;
5860}
5861
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005862static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5863{
5864 return __ufshcd_setup_clocks(hba, on, false);
5865}
5866
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005867static int ufshcd_init_clocks(struct ufs_hba *hba)
5868{
5869 int ret = 0;
5870 struct ufs_clk_info *clki;
5871 struct device *dev = hba->dev;
5872 struct list_head *head = &hba->clk_list_head;
5873
5874 if (!head || list_empty(head))
5875 goto out;
5876
5877 list_for_each_entry(clki, head, list) {
5878 if (!clki->name)
5879 continue;
5880
5881 clki->clk = devm_clk_get(dev, clki->name);
5882 if (IS_ERR(clki->clk)) {
5883 ret = PTR_ERR(clki->clk);
5884 dev_err(dev, "%s: %s clk get failed, %d\n",
5885 __func__, clki->name, ret);
5886 goto out;
5887 }
5888
5889 if (clki->max_freq) {
5890 ret = clk_set_rate(clki->clk, clki->max_freq);
5891 if (ret) {
5892 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5893 __func__, clki->name,
5894 clki->max_freq, ret);
5895 goto out;
5896 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005897 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005898 }
5899 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5900 clki->name, clk_get_rate(clki->clk));
5901 }
5902out:
5903 return ret;
5904}
5905
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005906static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5907{
5908 int err = 0;
5909
5910 if (!hba->vops)
5911 goto out;
5912
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005913 err = ufshcd_vops_init(hba);
5914 if (err)
5915 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005916
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005917 err = ufshcd_vops_setup_regulators(hba, true);
5918 if (err)
5919 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005920
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005921 goto out;
5922
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005923out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005924 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005925out:
5926 if (err)
5927 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005928 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005929 return err;
5930}
5931
5932static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5933{
5934 if (!hba->vops)
5935 return;
5936
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005937 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005938
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005939 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005940}
5941
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005942static int ufshcd_hba_init(struct ufs_hba *hba)
5943{
5944 int err;
5945
Raviv Shvili6a771a62014-09-25 15:32:24 +03005946 /*
5947 * Handle host controller power separately from the UFS device power
5948 * rails as it will help controlling the UFS host controller power
5949 * collapse easily which is different than UFS device power collapse.
5950 * Also, enable the host controller power before we go ahead with rest
5951 * of the initialization here.
5952 */
5953 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005954 if (err)
5955 goto out;
5956
Raviv Shvili6a771a62014-09-25 15:32:24 +03005957 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005958 if (err)
5959 goto out;
5960
Raviv Shvili6a771a62014-09-25 15:32:24 +03005961 err = ufshcd_init_clocks(hba);
5962 if (err)
5963 goto out_disable_hba_vreg;
5964
5965 err = ufshcd_setup_clocks(hba, true);
5966 if (err)
5967 goto out_disable_hba_vreg;
5968
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005969 err = ufshcd_init_vreg(hba);
5970 if (err)
5971 goto out_disable_clks;
5972
5973 err = ufshcd_setup_vreg(hba, true);
5974 if (err)
5975 goto out_disable_clks;
5976
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005977 err = ufshcd_variant_hba_init(hba);
5978 if (err)
5979 goto out_disable_vreg;
5980
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005981 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005982 goto out;
5983
5984out_disable_vreg:
5985 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005986out_disable_clks:
5987 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005988out_disable_hba_vreg:
5989 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005990out:
5991 return err;
5992}
5993
5994static void ufshcd_hba_exit(struct ufs_hba *hba)
5995{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005996 if (hba->is_powered) {
5997 ufshcd_variant_hba_exit(hba);
5998 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07005999 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03006000 ufshcd_setup_clocks(hba, false);
6001 ufshcd_setup_hba_vreg(hba, false);
6002 hba->is_powered = false;
6003 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006004}
6005
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006006static int
6007ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306008{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006009 unsigned char cmd[6] = {REQUEST_SENSE,
6010 0,
6011 0,
6012 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006013 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006014 0};
6015 char *buffer;
6016 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306017
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006018 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006019 if (!buffer) {
6020 ret = -ENOMEM;
6021 goto out;
6022 }
6023
6024 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07006025 UFSHCD_REQ_SENSE_SIZE, NULL,
Christoph Hellwige8064022016-10-20 15:12:13 +02006026 msecs_to_jiffies(1000), 3, NULL, 0, RQF_PM);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006027 if (ret)
6028 pr_err("%s: failed with err %d\n", __func__, ret);
6029
6030 kfree(buffer);
6031out:
6032 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306033}
6034
6035/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006036 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6037 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306038 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006039 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306040 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006041 * Returns 0 if requested power mode is set successfully
6042 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306043 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006044static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
6045 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306046{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006047 unsigned char cmd[6] = { START_STOP };
6048 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006049 struct scsi_device *sdp;
6050 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006051 int ret;
6052
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006053 spin_lock_irqsave(hba->host->host_lock, flags);
6054 sdp = hba->sdev_ufs_device;
6055 if (sdp) {
6056 ret = scsi_device_get(sdp);
6057 if (!ret && !scsi_device_online(sdp)) {
6058 ret = -ENODEV;
6059 scsi_device_put(sdp);
6060 }
6061 } else {
6062 ret = -ENODEV;
6063 }
6064 spin_unlock_irqrestore(hba->host->host_lock, flags);
6065
6066 if (ret)
6067 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006068
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306069 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006070 * If scsi commands fail, the scsi mid-layer schedules scsi error-
6071 * handling, which would wait for host to be resumed. Since we know
6072 * we are functional while we are here, skip host resume in error
6073 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306074 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006075 hba->host->eh_noresume = 1;
6076 if (hba->wlun_dev_clr_ua) {
6077 ret = ufshcd_send_request_sense(hba, sdp);
6078 if (ret)
6079 goto out;
6080 /* Unit attention condition is cleared now */
6081 hba->wlun_dev_clr_ua = false;
6082 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306083
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006084 cmd[4] = pwr_mode << 4;
6085
6086 /*
6087 * Current function would be generally called from the power management
Christoph Hellwige8064022016-10-20 15:12:13 +02006088 * callbacks hence set the RQF_PM flag so that it doesn't resume the
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006089 * already suspended childs.
6090 */
6091 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
Christoph Hellwige8064022016-10-20 15:12:13 +02006092 START_STOP_TIMEOUT, 0, NULL, 0, RQF_PM);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006093 if (ret) {
6094 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02006095 "START_STOP failed for power mode: %d, result %x\n",
6096 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01006097 if (driver_byte(ret) & DRIVER_SENSE)
6098 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006099 }
6100
6101 if (!ret)
6102 hba->curr_dev_pwr_mode = pwr_mode;
6103out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03006104 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006105 hba->host->eh_noresume = 0;
6106 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306107}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306108
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006109static int ufshcd_link_state_transition(struct ufs_hba *hba,
6110 enum uic_link_state req_link_state,
6111 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306112{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006113 int ret = 0;
6114
6115 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306116 return 0;
6117
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006118 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
6119 ret = ufshcd_uic_hibern8_enter(hba);
6120 if (!ret)
6121 ufshcd_set_link_hibern8(hba);
6122 else
6123 goto out;
6124 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306125 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006126 * If autobkops is enabled, link can't be turned off because
6127 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306128 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006129 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
6130 (!check_for_bkops || (check_for_bkops &&
6131 !hba->auto_bkops_enabled))) {
6132 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02006133 * Let's make sure that link is in low power mode, we are doing
6134 * this currently by putting the link in Hibern8. Otherway to
6135 * put the link in low power mode is to send the DME end point
6136 * to device and then send the DME reset command to local
6137 * unipro. But putting the link in hibern8 is much faster.
6138 */
6139 ret = ufshcd_uic_hibern8_enter(hba);
6140 if (ret)
6141 goto out;
6142 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006143 * Change controller state to "reset state" which
6144 * should also put the link in off/reset state
6145 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02006146 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006147 /*
6148 * TODO: Check if we need any delay to make sure that
6149 * controller is reset
6150 */
6151 ufshcd_set_link_off(hba);
6152 }
6153
6154out:
6155 return ret;
6156}
6157
6158static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6159{
6160 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006161 * It seems some UFS devices may keep drawing more than sleep current
6162 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6163 * To avoid this situation, add 2ms delay before putting these UFS
6164 * rails in LPM mode.
6165 */
6166 if (!ufshcd_is_link_active(hba) &&
6167 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6168 usleep_range(2000, 2100);
6169
6170 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006171 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6172 * power.
6173 *
6174 * If UFS device and link is in OFF state, all power supplies (VCC,
6175 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6176 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6177 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6178 *
6179 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6180 * in low power state which would save some power.
6181 */
6182 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6183 !hba->dev_info.is_lu_power_on_wp) {
6184 ufshcd_setup_vreg(hba, false);
6185 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6186 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6187 if (!ufshcd_is_link_active(hba)) {
6188 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6189 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6190 }
6191 }
6192}
6193
6194static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6195{
6196 int ret = 0;
6197
6198 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6199 !hba->dev_info.is_lu_power_on_wp) {
6200 ret = ufshcd_setup_vreg(hba, true);
6201 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006202 if (!ret && !ufshcd_is_link_active(hba)) {
6203 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6204 if (ret)
6205 goto vcc_disable;
6206 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6207 if (ret)
6208 goto vccq_lpm;
6209 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07006210 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006211 }
6212 goto out;
6213
6214vccq_lpm:
6215 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6216vcc_disable:
6217 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6218out:
6219 return ret;
6220}
6221
6222static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6223{
6224 if (ufshcd_is_link_off(hba))
6225 ufshcd_setup_hba_vreg(hba, false);
6226}
6227
6228static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6229{
6230 if (ufshcd_is_link_off(hba))
6231 ufshcd_setup_hba_vreg(hba, true);
6232}
6233
6234/**
6235 * ufshcd_suspend - helper function for suspend operations
6236 * @hba: per adapter instance
6237 * @pm_op: desired low power operation type
6238 *
6239 * This function will try to put the UFS device and link into low power
6240 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6241 * (System PM level).
6242 *
6243 * If this function is called during shutdown, it will make sure that
6244 * both UFS device and UFS link is powered off.
6245 *
6246 * NOTE: UFS device & link must be active before we enter in this function.
6247 *
6248 * Returns 0 for success and non-zero for failure
6249 */
6250static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6251{
6252 int ret = 0;
6253 enum ufs_pm_level pm_lvl;
6254 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6255 enum uic_link_state req_link_state;
6256
6257 hba->pm_op_in_progress = 1;
6258 if (!ufshcd_is_shutdown_pm(pm_op)) {
6259 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6260 hba->rpm_lvl : hba->spm_lvl;
6261 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6262 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6263 } else {
6264 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6265 req_link_state = UIC_LINK_OFF_STATE;
6266 }
6267
6268 /*
6269 * If we can't transition into any of the low power modes
6270 * just gate the clocks.
6271 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006272 ufshcd_hold(hba, false);
6273 hba->clk_gating.is_suspended = true;
6274
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006275 ufshcd_suspend_clkscaling(hba);
6276
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006277 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6278 req_link_state == UIC_LINK_ACTIVE_STATE) {
6279 goto disable_clks;
6280 }
6281
6282 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6283 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006284 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006285
6286 /* UFS device & link must be active before we enter in this function */
6287 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6288 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006289 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006290 }
6291
6292 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006293 if (ufshcd_can_autobkops_during_suspend(hba)) {
6294 /*
6295 * The device is idle with no requests in the queue,
6296 * allow background operations if bkops status shows
6297 * that performance might be impacted.
6298 */
6299 ret = ufshcd_urgent_bkops(hba);
6300 if (ret)
6301 goto enable_gating;
6302 } else {
6303 /* make sure that auto bkops is disabled */
6304 ufshcd_disable_auto_bkops(hba);
6305 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006306 }
6307
6308 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6309 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6310 !ufshcd_is_runtime_pm(pm_op))) {
6311 /* ensure that bkops is disabled */
6312 ufshcd_disable_auto_bkops(hba);
6313 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6314 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006315 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006316 }
6317
6318 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6319 if (ret)
6320 goto set_dev_active;
6321
6322 ufshcd_vreg_set_lpm(hba);
6323
6324disable_clks:
6325 /*
6326 * Call vendor specific suspend callback. As these callbacks may access
6327 * vendor specific host controller register space call them before the
6328 * host clocks are ON.
6329 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006330 ret = ufshcd_vops_suspend(hba, pm_op);
6331 if (ret)
6332 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006333
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006334 if (!ufshcd_is_link_active(hba))
6335 ufshcd_setup_clocks(hba, false);
6336 else
6337 /* If link is active, device ref_clk can't be switched off */
6338 __ufshcd_setup_clocks(hba, false, true);
6339
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006340 hba->clk_gating.state = CLKS_OFF;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006341 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006342 /*
6343 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006344 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006345 */
6346 ufshcd_disable_irq(hba);
6347 /* Put the host controller in low power mode if possible */
6348 ufshcd_hba_vreg_set_lpm(hba);
6349 goto out;
6350
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006351set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07006352 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006353 ufshcd_vreg_set_hpm(hba);
6354 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6355 ufshcd_set_link_active(hba);
6356 else if (ufshcd_is_link_off(hba))
6357 ufshcd_host_reset_and_restore(hba);
6358set_dev_active:
6359 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6360 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006361enable_gating:
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006362 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006363 hba->clk_gating.is_suspended = false;
6364 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006365out:
6366 hba->pm_op_in_progress = 0;
6367 return ret;
6368}
6369
6370/**
6371 * ufshcd_resume - helper function for resume operations
6372 * @hba: per adapter instance
6373 * @pm_op: runtime PM or system PM
6374 *
6375 * This function basically brings the UFS device, UniPro link and controller
6376 * to active state.
6377 *
6378 * Returns 0 for success and non-zero for failure
6379 */
6380static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6381{
6382 int ret;
6383 enum uic_link_state old_link_state;
6384
6385 hba->pm_op_in_progress = 1;
6386 old_link_state = hba->uic_link_state;
6387
6388 ufshcd_hba_vreg_set_hpm(hba);
6389 /* Make sure clocks are enabled before accessing controller */
6390 ret = ufshcd_setup_clocks(hba, true);
6391 if (ret)
6392 goto out;
6393
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006394 /* enable the host irq as host controller would be active soon */
6395 ret = ufshcd_enable_irq(hba);
6396 if (ret)
6397 goto disable_irq_and_vops_clks;
6398
6399 ret = ufshcd_vreg_set_hpm(hba);
6400 if (ret)
6401 goto disable_irq_and_vops_clks;
6402
6403 /*
6404 * Call vendor specific resume callback. As these callbacks may access
6405 * vendor specific host controller register space call them when the
6406 * host clocks are ON.
6407 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006408 ret = ufshcd_vops_resume(hba, pm_op);
6409 if (ret)
6410 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006411
6412 if (ufshcd_is_link_hibern8(hba)) {
6413 ret = ufshcd_uic_hibern8_exit(hba);
6414 if (!ret)
6415 ufshcd_set_link_active(hba);
6416 else
6417 goto vendor_suspend;
6418 } else if (ufshcd_is_link_off(hba)) {
6419 ret = ufshcd_host_reset_and_restore(hba);
6420 /*
6421 * ufshcd_host_reset_and_restore() should have already
6422 * set the link state as active
6423 */
6424 if (ret || !ufshcd_is_link_active(hba))
6425 goto vendor_suspend;
6426 }
6427
6428 if (!ufshcd_is_ufs_dev_active(hba)) {
6429 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6430 if (ret)
6431 goto set_old_link_state;
6432 }
6433
Subhash Jadavani374a2462014-09-25 15:32:35 +03006434 /*
6435 * If BKOPs operations are urgently needed at this moment then
6436 * keep auto-bkops enabled or else disable it.
6437 */
6438 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006439 hba->clk_gating.is_suspended = false;
6440
Gilad Bronera5082532016-10-17 17:10:00 -07006441 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006442
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006443 /* Schedule clock gating in case of no access to UFS device yet */
6444 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006445 goto out;
6446
6447set_old_link_state:
6448 ufshcd_link_state_transition(hba, old_link_state, 0);
6449vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006450 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006451disable_vreg:
6452 ufshcd_vreg_set_lpm(hba);
6453disable_irq_and_vops_clks:
6454 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006455 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006456 ufshcd_setup_clocks(hba, false);
6457out:
6458 hba->pm_op_in_progress = 0;
6459 return ret;
6460}
6461
6462/**
6463 * ufshcd_system_suspend - system suspend routine
6464 * @hba: per adapter instance
6465 * @pm_op: runtime PM or system PM
6466 *
6467 * Check the description of ufshcd_suspend() function for more details.
6468 *
6469 * Returns 0 for success and non-zero for failure
6470 */
6471int ufshcd_system_suspend(struct ufs_hba *hba)
6472{
6473 int ret = 0;
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006474 ktime_t start = ktime_get();
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006475
6476 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006477 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006478
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08006479 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6480 hba->curr_dev_pwr_mode) &&
6481 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
6482 hba->uic_link_state))
6483 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006484
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08006485 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006486 /*
6487 * UFS device and/or UFS link low power states during runtime
6488 * suspend seems to be different than what is expected during
6489 * system suspend. Hence runtime resume the devic & link and
6490 * let the system suspend low power states to take effect.
6491 * TODO: If resume takes longer time, we might have optimize
6492 * it in future by not resuming everything if possible.
6493 */
6494 ret = ufshcd_runtime_resume(hba);
6495 if (ret)
6496 goto out;
6497 }
6498
6499 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6500out:
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006501 trace_ufshcd_system_suspend(dev_name(hba->dev), ret,
6502 ktime_to_us(ktime_sub(ktime_get(), start)),
6503 hba->uic_link_state, hba->curr_dev_pwr_mode);
Dolev Ravive7850602014-09-25 15:32:36 +03006504 if (!ret)
6505 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006506 return ret;
6507}
6508EXPORT_SYMBOL(ufshcd_system_suspend);
6509
6510/**
6511 * ufshcd_system_resume - system resume routine
6512 * @hba: per adapter instance
6513 *
6514 * Returns 0 for success and non-zero for failure
6515 */
6516
6517int ufshcd_system_resume(struct ufs_hba *hba)
6518{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006519 int ret = 0;
6520 ktime_t start = ktime_get();
6521
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006522 if (!hba)
6523 return -EINVAL;
6524
6525 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006526 /*
6527 * Let the runtime resume take care of resuming
6528 * if runtime suspended.
6529 */
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006530 goto out;
6531 else
6532 ret = ufshcd_resume(hba, UFS_SYSTEM_PM);
6533out:
6534 trace_ufshcd_system_resume(dev_name(hba->dev), ret,
6535 ktime_to_us(ktime_sub(ktime_get(), start)),
6536 hba->uic_link_state, hba->curr_dev_pwr_mode);
6537 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006538}
6539EXPORT_SYMBOL(ufshcd_system_resume);
6540
6541/**
6542 * ufshcd_runtime_suspend - runtime suspend routine
6543 * @hba: per adapter instance
6544 *
6545 * Check the description of ufshcd_suspend() function for more details.
6546 *
6547 * Returns 0 for success and non-zero for failure
6548 */
6549int ufshcd_runtime_suspend(struct ufs_hba *hba)
6550{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006551 int ret = 0;
6552 ktime_t start = ktime_get();
6553
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006554 if (!hba)
6555 return -EINVAL;
6556
6557 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006558 goto out;
6559 else
6560 ret = ufshcd_suspend(hba, UFS_RUNTIME_PM);
6561out:
6562 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret,
6563 ktime_to_us(ktime_sub(ktime_get(), start)),
6564 hba->uic_link_state, hba->curr_dev_pwr_mode);
6565 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306566}
6567EXPORT_SYMBOL(ufshcd_runtime_suspend);
6568
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006569/**
6570 * ufshcd_runtime_resume - runtime resume routine
6571 * @hba: per adapter instance
6572 *
6573 * This function basically brings the UFS device, UniPro link and controller
6574 * to active state. Following operations are done in this function:
6575 *
6576 * 1. Turn on all the controller related clocks
6577 * 2. Bring the UniPro link out of Hibernate state
6578 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6579 * to active state.
6580 * 4. If auto-bkops is enabled on the device, disable it.
6581 *
6582 * So following would be the possible power state after this function return
6583 * successfully:
6584 * S1: UFS device in Active state with VCC rail ON
6585 * UniPro link in Active state
6586 * All the UFS/UniPro controller clocks are ON
6587 *
6588 * Returns 0 for success and non-zero for failure
6589 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306590int ufshcd_runtime_resume(struct ufs_hba *hba)
6591{
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006592 int ret = 0;
6593 ktime_t start = ktime_get();
6594
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006595 if (!hba)
6596 return -EINVAL;
6597
6598 if (!hba->is_powered)
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006599 goto out;
6600 else
6601 ret = ufshcd_resume(hba, UFS_RUNTIME_PM);
6602out:
6603 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret,
6604 ktime_to_us(ktime_sub(ktime_get(), start)),
6605 hba->uic_link_state, hba->curr_dev_pwr_mode);
6606 return ret;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306607}
6608EXPORT_SYMBOL(ufshcd_runtime_resume);
6609
6610int ufshcd_runtime_idle(struct ufs_hba *hba)
6611{
6612 return 0;
6613}
6614EXPORT_SYMBOL(ufshcd_runtime_idle);
6615
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306616/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006617 * ufshcd_shutdown - shutdown routine
6618 * @hba: per adapter instance
6619 *
6620 * This function would power off both UFS device and UFS link.
6621 *
6622 * Returns 0 always to allow force shutdown even in case of errors.
6623 */
6624int ufshcd_shutdown(struct ufs_hba *hba)
6625{
6626 int ret = 0;
6627
6628 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6629 goto out;
6630
6631 if (pm_runtime_suspended(hba->dev)) {
6632 ret = ufshcd_runtime_resume(hba);
6633 if (ret)
6634 goto out;
6635 }
6636
6637 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6638out:
6639 if (ret)
6640 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6641 /* allow force shutdown even in case of errors */
6642 return 0;
6643}
6644EXPORT_SYMBOL(ufshcd_shutdown);
6645
6646/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306647 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306648 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306649 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306650 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306651void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306652{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306653 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306654 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306655 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006656 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306657
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006658 ufshcd_exit_clk_gating(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006659 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306660}
6661EXPORT_SYMBOL_GPL(ufshcd_remove);
6662
6663/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006664 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6665 * @hba: pointer to Host Bus Adapter (HBA)
6666 */
6667void ufshcd_dealloc_host(struct ufs_hba *hba)
6668{
6669 scsi_host_put(hba->host);
6670}
6671EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6672
6673/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006674 * ufshcd_set_dma_mask - Set dma mask based on the controller
6675 * addressing capability
6676 * @hba: per adapter instance
6677 *
6678 * Returns 0 for success, non-zero for failure
6679 */
6680static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6681{
6682 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6683 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6684 return 0;
6685 }
6686 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6687}
6688
6689/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006690 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306691 * @dev: pointer to device handle
6692 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306693 * Returns 0 on success, non-zero value on failure
6694 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006695int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306696{
6697 struct Scsi_Host *host;
6698 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006699 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306700
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306701 if (!dev) {
6702 dev_err(dev,
6703 "Invalid memory reference for dev is NULL\n");
6704 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306705 goto out_error;
6706 }
6707
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306708 host = scsi_host_alloc(&ufshcd_driver_template,
6709 sizeof(struct ufs_hba));
6710 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306711 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306712 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306713 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306714 }
6715 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306716 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306717 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006718 *hba_handle = hba;
6719
6720out_error:
6721 return err;
6722}
6723EXPORT_SYMBOL(ufshcd_alloc_host);
6724
Sahitya Tummala856b3482014-09-25 15:32:34 +03006725static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6726{
6727 int ret = 0;
6728 struct ufs_clk_info *clki;
6729 struct list_head *head = &hba->clk_list_head;
6730
6731 if (!head || list_empty(head))
6732 goto out;
6733
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006734 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6735 if (ret)
6736 return ret;
6737
Sahitya Tummala856b3482014-09-25 15:32:34 +03006738 list_for_each_entry(clki, head, list) {
6739 if (!IS_ERR_OR_NULL(clki->clk)) {
6740 if (scale_up && clki->max_freq) {
6741 if (clki->curr_freq == clki->max_freq)
6742 continue;
6743 ret = clk_set_rate(clki->clk, clki->max_freq);
6744 if (ret) {
6745 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6746 __func__, clki->name,
6747 clki->max_freq, ret);
6748 break;
6749 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006750 trace_ufshcd_clk_scaling(dev_name(hba->dev),
6751 "scaled up", clki->name,
6752 clki->curr_freq,
6753 clki->max_freq);
6754
Sahitya Tummala856b3482014-09-25 15:32:34 +03006755 clki->curr_freq = clki->max_freq;
6756
6757 } else if (!scale_up && clki->min_freq) {
6758 if (clki->curr_freq == clki->min_freq)
6759 continue;
6760 ret = clk_set_rate(clki->clk, clki->min_freq);
6761 if (ret) {
6762 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6763 __func__, clki->name,
6764 clki->min_freq, ret);
6765 break;
6766 }
subhashj@codeaurora.org7ff5ab42016-12-22 18:39:51 -08006767 trace_ufshcd_clk_scaling(dev_name(hba->dev),
6768 "scaled down", clki->name,
6769 clki->curr_freq,
6770 clki->min_freq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006771 clki->curr_freq = clki->min_freq;
6772 }
6773 }
6774 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6775 clki->name, clk_get_rate(clki->clk));
6776 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006777
6778 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6779
Sahitya Tummala856b3482014-09-25 15:32:34 +03006780out:
6781 return ret;
6782}
6783
6784static int ufshcd_devfreq_target(struct device *dev,
6785 unsigned long *freq, u32 flags)
6786{
6787 int err = 0;
6788 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006789 bool release_clk_hold = false;
6790 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006791
6792 if (!ufshcd_is_clkscaling_enabled(hba))
6793 return -EINVAL;
6794
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006795 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6796 if (ufshcd_eh_in_progress(hba)) {
6797 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6798 return 0;
6799 }
6800
6801 if (ufshcd_is_clkgating_allowed(hba) &&
6802 (hba->clk_gating.state != CLKS_ON)) {
6803 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6804 /* hold the vote until the scaling work is completed */
6805 hba->clk_gating.active_reqs++;
6806 release_clk_hold = true;
6807 hba->clk_gating.state = CLKS_ON;
6808 } else {
6809 /*
6810 * Clock gating work seems to be running in parallel
6811 * hence skip scaling work to avoid deadlock between
6812 * current scaling work and gating work.
6813 */
6814 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6815 return 0;
6816 }
6817 }
6818 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6819
Sahitya Tummala856b3482014-09-25 15:32:34 +03006820 if (*freq == UINT_MAX)
6821 err = ufshcd_scale_clks(hba, true);
6822 else if (*freq == 0)
6823 err = ufshcd_scale_clks(hba, false);
6824
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006825 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6826 if (release_clk_hold)
6827 __ufshcd_release(hba);
6828 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6829
Sahitya Tummala856b3482014-09-25 15:32:34 +03006830 return err;
6831}
6832
6833static int ufshcd_devfreq_get_dev_status(struct device *dev,
6834 struct devfreq_dev_status *stat)
6835{
6836 struct ufs_hba *hba = dev_get_drvdata(dev);
6837 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6838 unsigned long flags;
6839
6840 if (!ufshcd_is_clkscaling_enabled(hba))
6841 return -EINVAL;
6842
6843 memset(stat, 0, sizeof(*stat));
6844
6845 spin_lock_irqsave(hba->host->host_lock, flags);
6846 if (!scaling->window_start_t)
6847 goto start_window;
6848
6849 if (scaling->is_busy_started)
6850 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6851 scaling->busy_start_t));
6852
6853 stat->total_time = jiffies_to_usecs((long)jiffies -
6854 (long)scaling->window_start_t);
6855 stat->busy_time = scaling->tot_busy_t;
6856start_window:
6857 scaling->window_start_t = jiffies;
6858 scaling->tot_busy_t = 0;
6859
6860 if (hba->outstanding_reqs) {
6861 scaling->busy_start_t = ktime_get();
6862 scaling->is_busy_started = true;
6863 } else {
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01006864 scaling->busy_start_t = 0;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006865 scaling->is_busy_started = false;
6866 }
6867 spin_unlock_irqrestore(hba->host->host_lock, flags);
6868 return 0;
6869}
6870
6871static struct devfreq_dev_profile ufs_devfreq_profile = {
6872 .polling_ms = 100,
6873 .target = ufshcd_devfreq_target,
6874 .get_dev_status = ufshcd_devfreq_get_dev_status,
6875};
6876
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006877/**
6878 * ufshcd_init - Driver initialization routine
6879 * @hba: per-adapter instance
6880 * @mmio_base: base register address
6881 * @irq: Interrupt line of device
6882 * Returns 0 on success, non-zero value on failure
6883 */
6884int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6885{
6886 int err;
6887 struct Scsi_Host *host = hba->host;
6888 struct device *dev = hba->dev;
6889
6890 if (!mmio_base) {
6891 dev_err(hba->dev,
6892 "Invalid memory reference for mmio_base is NULL\n");
6893 err = -ENODEV;
6894 goto out_error;
6895 }
6896
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306897 hba->mmio_base = mmio_base;
6898 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306899
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006900 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006901 if (err)
6902 goto out_error;
6903
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306904 /* Read capabilities registers */
6905 ufshcd_hba_capabilities(hba);
6906
6907 /* Get UFS version supported by the controller */
6908 hba->ufs_version = ufshcd_get_ufs_version(hba);
6909
Yaniv Gardic01848c2016-12-05 19:25:02 -08006910 if ((hba->ufs_version != UFSHCI_VERSION_10) &&
6911 (hba->ufs_version != UFSHCI_VERSION_11) &&
6912 (hba->ufs_version != UFSHCI_VERSION_20) &&
6913 (hba->ufs_version != UFSHCI_VERSION_21))
6914 dev_err(hba->dev, "invalid UFS version 0x%x\n",
6915 hba->ufs_version);
6916
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306917 /* Get Interrupt bit mask per version */
6918 hba->intr_mask = ufshcd_get_intr_mask(hba);
6919
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006920 err = ufshcd_set_dma_mask(hba);
6921 if (err) {
6922 dev_err(hba->dev, "set dma mask failed\n");
6923 goto out_disable;
6924 }
6925
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306926 /* Allocate memory for host memory space */
6927 err = ufshcd_memory_alloc(hba);
6928 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306929 dev_err(hba->dev, "Memory allocation failed\n");
6930 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306931 }
6932
6933 /* Configure LRB */
6934 ufshcd_host_memory_configure(hba);
6935
6936 host->can_queue = hba->nutrs;
6937 host->cmd_per_lun = hba->nutrs;
6938 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006939 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306940 host->max_channel = UFSHCD_MAX_CHANNEL;
6941 host->unique_id = host->host_no;
6942 host->max_cmd_len = MAX_CDB_SIZE;
6943
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006944 hba->max_pwr_info.is_valid = false;
6945
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306946 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306947 init_waitqueue_head(&hba->tm_wq);
6948 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306949
6950 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306951 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306952 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306953
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306954 /* Initialize UIC command mutex */
6955 mutex_init(&hba->uic_cmd_mutex);
6956
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306957 /* Initialize mutex for device management commands */
6958 mutex_init(&hba->dev_cmd.lock);
6959
6960 /* Initialize device management tag acquire wait queue */
6961 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6962
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006963 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006964
6965 /*
6966 * In order to avoid any spurious interrupt immediately after
6967 * registering UFS controller interrupt handler, clear any pending UFS
6968 * interrupt status and disable all the UFS interrupts.
6969 */
6970 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6971 REG_INTERRUPT_STATUS);
6972 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6973 /*
6974 * Make sure that UFS interrupts are disabled and any pending interrupt
6975 * status is cleared before registering UFS interrupt handler.
6976 */
6977 mb();
6978
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306979 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006980 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306981 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306982 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006983 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006984 } else {
6985 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306986 }
6987
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306988 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306989 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306990 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006991 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306992 }
6993
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306994 /* Host controller enable */
6995 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306996 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306997 dev_err(hba->dev, "Host controller enable failed\n");
Dolev Raviv66cc8202016-12-22 18:39:42 -08006998 ufshcd_print_host_regs(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306999 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307000 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307001
Sahitya Tummala856b3482014-09-25 15:32:34 +03007002 if (ufshcd_is_clkscaling_enabled(hba)) {
Chanwoo Choi4861ee12016-11-08 18:13:28 +09007003 hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile,
Sahitya Tummala856b3482014-09-25 15:32:34 +03007004 "simple_ondemand", NULL);
7005 if (IS_ERR(hba->devfreq)) {
7006 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
7007 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00007008 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007009 goto out_remove_scsi_host;
7010 }
7011 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07007012 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03007013 }
7014
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05307015 /* Hold auto suspend until async scan completes */
7016 pm_runtime_get_sync(dev);
7017
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007018 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007019 * We are assuming that device wasn't put in sleep/power-down
7020 * state exclusively during the boot stage before kernel.
7021 * This assumption helps avoid doing link startup twice during
7022 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007023 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08007024 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007025
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05307026 async_schedule(ufshcd_async_scan, hba);
7027
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307028 return 0;
7029
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307030out_remove_scsi_host:
7031 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03007032exit_gating:
7033 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307034out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03007035 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03007036 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307037out_error:
7038 return err;
7039}
7040EXPORT_SYMBOL_GPL(ufshcd_init);
7041
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307042MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7043MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05307044MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307045MODULE_LICENSE("GPL");
7046MODULE_VERSION(UFSHCD_DRIVER_VERSION);