blob: 0b278a1596c53c068e1e9bc35fc029fe50b23b63 [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
Gilad Bronerdcea0bf2016-10-17 17:09:48 -070048#define UFSHCD_REQ_SENSE_SIZE 18
49
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053050#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
51 UTP_TASK_REQ_COMPL |\
52 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053053/* UIC command timeout, unit: ms */
54#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053055
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053056/* NOP OUT retries waiting for NOP IN response */
57#define NOP_OUT_RETRIES 10
58/* Timeout after 30 msecs if NOP OUT hangs without response */
59#define NOP_OUT_TIMEOUT 30 /* msecs */
60
Dolev Raviv68078d52013-07-30 00:35:58 +053061/* Query request retries */
62#define QUERY_REQ_RETRIES 10
63/* Query request timeout */
64#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020065/*
66 * Query request timeout for fDeviceInit flag
67 * fDeviceInit query response time for some devices is too large that default
68 * QUERY_REQ_TIMEOUT may not be enough for such devices.
69 */
70#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053071
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053072/* Task management command timeout */
73#define TM_CMD_TIMEOUT 100 /* msecs */
74
Yaniv Gardi64238fb2016-02-01 15:02:43 +020075/* maximum number of retries for a general UIC command */
76#define UFS_UIC_COMMAND_RETRIES 3
77
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030078/* maximum number of link-startup retries */
79#define DME_LINKSTARTUP_RETRIES 3
80
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020081/* Maximum retries for Hibern8 enter */
82#define UIC_HIBERN8_ENTER_RETRIES 3
83
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030084/* maximum number of reset retries before giving up */
85#define MAX_HOST_RESET_RETRIES 5
86
Dolev Raviv68078d52013-07-30 00:35:58 +053087/* Expose the flag value from utp_upiu_query.value */
88#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
89
Seungwon Jeon7d568652013-08-31 21:40:20 +053090/* Interrupt aggregation default timeout, unit: 40us */
91#define INT_AGGR_DEF_TO 0x02
92
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030093#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
94 ({ \
95 int _ret; \
96 if (_on) \
97 _ret = ufshcd_enable_vreg(_dev, _vreg); \
98 else \
99 _ret = ufshcd_disable_vreg(_dev, _vreg); \
100 _ret; \
101 })
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
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530194static void ufshcd_tmc_handler(struct ufs_hba *hba);
195static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530196static int ufshcd_reset_and_restore(struct ufs_hba *hba);
197static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300198static void ufshcd_hba_exit(struct ufs_hba *hba);
199static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300200static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
201 bool skip_ref_clk);
202static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200203static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300204static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
205static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300206static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300207static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
208static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300209static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
210 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300211static int ufshcd_change_power_mode(struct ufs_hba *hba,
212 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200213static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
214{
215 return tag >= 0 && tag < hba->nutrs;
216}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300217
218static inline int ufshcd_enable_irq(struct ufs_hba *hba)
219{
220 int ret = 0;
221
222 if (!hba->is_irq_enabled) {
223 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
224 hba);
225 if (ret)
226 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
227 __func__, ret);
228 hba->is_irq_enabled = true;
229 }
230
231 return ret;
232}
233
234static inline void ufshcd_disable_irq(struct ufs_hba *hba)
235{
236 if (hba->is_irq_enabled) {
237 free_irq(hba->irq, hba);
238 hba->is_irq_enabled = false;
239 }
240}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530241
Yaniv Gardib573d482016-03-10 17:37:09 +0200242/* replace non-printable or non-ASCII characters with spaces */
243static inline void ufshcd_remove_non_printable(char *val)
244{
245 if (!val)
246 return;
247
248 if (*val < 0x20 || *val > 0x7e)
249 *val = ' ';
250}
251
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530252/*
253 * ufshcd_wait_for_register - wait for register value to change
254 * @hba - per-adapter interface
255 * @reg - mmio register offset
256 * @mask - mask to apply to read register value
257 * @val - wait condition
258 * @interval_us - polling interval in microsecs
259 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200260 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530261 *
262 * Returns -ETIMEDOUT on error, zero on success
263 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200264int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
265 u32 val, unsigned long interval_us,
266 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530267{
268 int err = 0;
269 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
270
271 /* ignore bits that we don't intend to wait on */
272 val = val & mask;
273
274 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200275 if (can_sleep)
276 usleep_range(interval_us, interval_us + 50);
277 else
278 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530279 if (time_after(jiffies, timeout)) {
280 if ((ufshcd_readl(hba, reg) & mask) != val)
281 err = -ETIMEDOUT;
282 break;
283 }
284 }
285
286 return err;
287}
288
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530289/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530290 * ufshcd_get_intr_mask - Get the interrupt bit mask
291 * @hba - Pointer to adapter instance
292 *
293 * Returns interrupt bit mask per version
294 */
295static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
296{
297 if (hba->ufs_version == UFSHCI_VERSION_10)
298 return INTERRUPT_MASK_ALL_VER_10;
299 else
300 return INTERRUPT_MASK_ALL_VER_11;
301}
302
303/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530304 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
305 * @hba - Pointer to adapter instance
306 *
307 * Returns UFSHCI version supported by the controller
308 */
309static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
310{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200311 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
312 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300313
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530314 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530315}
316
317/**
318 * ufshcd_is_device_present - Check if any device connected to
319 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300320 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530321 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530322 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530323 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300324static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530325{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300326 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
327 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530328}
329
330/**
331 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
332 * @lrb: pointer to local command reference block
333 *
334 * This function is used to get the OCS field from UTRD
335 * Returns the OCS field in the UTRD
336 */
337static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
338{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530339 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530340}
341
342/**
343 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
344 * @task_req_descp: pointer to utp_task_req_desc structure
345 *
346 * This function is used to get the OCS field from UTMRD
347 * Returns the OCS field in the UTMRD
348 */
349static inline int
350ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
351{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530352 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530353}
354
355/**
356 * ufshcd_get_tm_free_slot - get a free slot for task management request
357 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530358 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530359 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530360 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
361 * Returns 0 if free slot is not available, else return 1 with tag value
362 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530363 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530364static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530365{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530366 int tag;
367 bool ret = false;
368
369 if (!free_slot)
370 goto out;
371
372 do {
373 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
374 if (tag >= hba->nutmrs)
375 goto out;
376 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
377
378 *free_slot = tag;
379 ret = true;
380out:
381 return ret;
382}
383
384static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
385{
386 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530387}
388
389/**
390 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
391 * @hba: per adapter instance
392 * @pos: position of the bit to be cleared
393 */
394static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
395{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530396 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530397}
398
399/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200400 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
401 * @hba: per adapter instance
402 * @tag: position of the bit to be cleared
403 */
404static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
405{
406 __clear_bit(tag, &hba->outstanding_reqs);
407}
408
409/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530410 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
411 * @reg: Register value of host controller status
412 *
413 * Returns integer, 0 on Success and positive value if failed
414 */
415static inline int ufshcd_get_lists_status(u32 reg)
416{
417 /*
418 * The mask 0xFF is for the following HCS register bits
419 * Bit Description
420 * 0 Device Present
421 * 1 UTRLRDY
422 * 2 UTMRLRDY
423 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200424 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530425 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200426 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530427}
428
429/**
430 * ufshcd_get_uic_cmd_result - Get the UIC command result
431 * @hba: Pointer to adapter instance
432 *
433 * This function gets the result of UIC command completion
434 * Returns 0 on success, non zero value on error
435 */
436static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
437{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530438 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530439 MASK_UIC_COMMAND_RESULT;
440}
441
442/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530443 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
444 * @hba: Pointer to adapter instance
445 *
446 * This function gets UIC command argument3
447 * Returns 0 on success, non zero value on error
448 */
449static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
450{
451 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
452}
453
454/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530455 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530456 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530457 */
458static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530459ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530460{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530461 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530462}
463
464/**
465 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
466 * @ucd_rsp_ptr: pointer to response UPIU
467 *
468 * This function gets the response status and scsi_status from response UPIU
469 * Returns the response result code.
470 */
471static inline int
472ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
473{
474 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
475}
476
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530477/*
478 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
479 * from response UPIU
480 * @ucd_rsp_ptr: pointer to response UPIU
481 *
482 * Return the data segment length.
483 */
484static inline unsigned int
485ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
486{
487 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
488 MASK_RSP_UPIU_DATA_SEG_LEN;
489}
490
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530491/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530492 * ufshcd_is_exception_event - Check if the device raised an exception event
493 * @ucd_rsp_ptr: pointer to response UPIU
494 *
495 * The function checks if the device raised an exception event indicated in
496 * the Device Information field of response UPIU.
497 *
498 * Returns true if exception is raised, false otherwise.
499 */
500static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
501{
502 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
503 MASK_RSP_EXCEPTION_EVENT ? true : false;
504}
505
506/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530507 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530508 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530509 */
510static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530511ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530512{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530513 ufshcd_writel(hba, INT_AGGR_ENABLE |
514 INT_AGGR_COUNTER_AND_TIMER_RESET,
515 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
516}
517
518/**
519 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
520 * @hba: per adapter instance
521 * @cnt: Interrupt aggregation counter threshold
522 * @tmout: Interrupt aggregation timeout value
523 */
524static inline void
525ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
526{
527 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
528 INT_AGGR_COUNTER_THLD_VAL(cnt) |
529 INT_AGGR_TIMEOUT_VAL(tmout),
530 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530531}
532
533/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300534 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
535 * @hba: per adapter instance
536 */
537static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
538{
539 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
540}
541
542/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530543 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
544 * When run-stop registers are set to 1, it indicates the
545 * host controller that it can process the requests
546 * @hba: per adapter instance
547 */
548static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
549{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530550 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
551 REG_UTP_TASK_REQ_LIST_RUN_STOP);
552 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
553 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530554}
555
556/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530557 * ufshcd_hba_start - Start controller initialization sequence
558 * @hba: per adapter instance
559 */
560static inline void ufshcd_hba_start(struct ufs_hba *hba)
561{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530562 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530563}
564
565/**
566 * ufshcd_is_hba_active - Get controller state
567 * @hba: per adapter instance
568 *
569 * Returns zero if controller is active, 1 otherwise
570 */
571static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
572{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530573 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530574}
575
Yaniv Gardi37113102016-03-10 17:37:16 +0200576u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
577{
578 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
579 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
580 (hba->ufs_version == UFSHCI_VERSION_11))
581 return UFS_UNIPRO_VER_1_41;
582 else
583 return UFS_UNIPRO_VER_1_6;
584}
585EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
586
587static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
588{
589 /*
590 * If both host and device support UniPro ver1.6 or later, PA layer
591 * parameters tuning happens during link startup itself.
592 *
593 * We can manually tune PA layer parameters if either host or device
594 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
595 * logic simple, we will only do manual tuning if local unipro version
596 * doesn't support ver1.6 or later.
597 */
598 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
599 return true;
600 else
601 return false;
602}
603
Gilad Bronera5082532016-10-17 17:10:00 -0700604static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
605{
606 if (ufshcd_is_clkscaling_enabled(hba)) {
607 devfreq_suspend_device(hba->devfreq);
608 hba->clk_scaling.window_start_t = 0;
609 }
610}
611
612static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
613{
614 if (ufshcd_is_clkscaling_enabled(hba))
615 devfreq_resume_device(hba->devfreq);
616}
617
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300618static void ufshcd_ungate_work(struct work_struct *work)
619{
620 int ret;
621 unsigned long flags;
622 struct ufs_hba *hba = container_of(work, struct ufs_hba,
623 clk_gating.ungate_work);
624
625 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
626
627 spin_lock_irqsave(hba->host->host_lock, flags);
628 if (hba->clk_gating.state == CLKS_ON) {
629 spin_unlock_irqrestore(hba->host->host_lock, flags);
630 goto unblock_reqs;
631 }
632
633 spin_unlock_irqrestore(hba->host->host_lock, flags);
634 ufshcd_setup_clocks(hba, true);
635
636 /* Exit from hibern8 */
637 if (ufshcd_can_hibern8_during_gating(hba)) {
638 /* Prevent gating in this path */
639 hba->clk_gating.is_suspended = true;
640 if (ufshcd_is_link_hibern8(hba)) {
641 ret = ufshcd_uic_hibern8_exit(hba);
642 if (ret)
643 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
644 __func__, ret);
645 else
646 ufshcd_set_link_active(hba);
647 }
648 hba->clk_gating.is_suspended = false;
649 }
650unblock_reqs:
Gilad Bronera5082532016-10-17 17:10:00 -0700651 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300652 scsi_unblock_requests(hba->host);
653}
654
655/**
656 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
657 * Also, exit from hibern8 mode and set the link as active.
658 * @hba: per adapter instance
659 * @async: This indicates whether caller should ungate clocks asynchronously.
660 */
661int ufshcd_hold(struct ufs_hba *hba, bool async)
662{
663 int rc = 0;
664 unsigned long flags;
665
666 if (!ufshcd_is_clkgating_allowed(hba))
667 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300668 spin_lock_irqsave(hba->host->host_lock, flags);
669 hba->clk_gating.active_reqs++;
670
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200671 if (ufshcd_eh_in_progress(hba)) {
672 spin_unlock_irqrestore(hba->host->host_lock, flags);
673 return 0;
674 }
675
Sahitya Tummala856b3482014-09-25 15:32:34 +0300676start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300677 switch (hba->clk_gating.state) {
678 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -0700679 /*
680 * Wait for the ungate work to complete if in progress.
681 * Though the clocks may be in ON state, the link could
682 * still be in hibner8 state if hibern8 is allowed
683 * during clock gating.
684 * Make sure we exit hibern8 state also in addition to
685 * clocks being ON.
686 */
687 if (ufshcd_can_hibern8_during_gating(hba) &&
688 ufshcd_is_link_hibern8(hba)) {
689 spin_unlock_irqrestore(hba->host->host_lock, flags);
690 flush_work(&hba->clk_gating.ungate_work);
691 spin_lock_irqsave(hba->host->host_lock, flags);
692 goto start;
693 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300694 break;
695 case REQ_CLKS_OFF:
696 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
697 hba->clk_gating.state = CLKS_ON;
698 break;
699 }
700 /*
701 * If we here, it means gating work is either done or
702 * currently running. Hence, fall through to cancel gating
703 * work and to enable clocks.
704 */
705 case CLKS_OFF:
706 scsi_block_requests(hba->host);
707 hba->clk_gating.state = REQ_CLKS_ON;
708 schedule_work(&hba->clk_gating.ungate_work);
709 /*
710 * fall through to check if we should wait for this
711 * work to be done or not.
712 */
713 case REQ_CLKS_ON:
714 if (async) {
715 rc = -EAGAIN;
716 hba->clk_gating.active_reqs--;
717 break;
718 }
719
720 spin_unlock_irqrestore(hba->host->host_lock, flags);
721 flush_work(&hba->clk_gating.ungate_work);
722 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300723 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300724 goto start;
725 default:
726 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
727 __func__, hba->clk_gating.state);
728 break;
729 }
730 spin_unlock_irqrestore(hba->host->host_lock, flags);
731out:
732 return rc;
733}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200734EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300735
736static void ufshcd_gate_work(struct work_struct *work)
737{
738 struct ufs_hba *hba = container_of(work, struct ufs_hba,
739 clk_gating.gate_work.work);
740 unsigned long flags;
741
742 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -0700743 /*
744 * In case you are here to cancel this work the gating state
745 * would be marked as REQ_CLKS_ON. In this case save time by
746 * skipping the gating work and exit after changing the clock
747 * state to CLKS_ON.
748 */
749 if (hba->clk_gating.is_suspended ||
750 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300751 hba->clk_gating.state = CLKS_ON;
752 goto rel_lock;
753 }
754
755 if (hba->clk_gating.active_reqs
756 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
757 || hba->lrb_in_use || hba->outstanding_tasks
758 || hba->active_uic_cmd || hba->uic_async_done)
759 goto rel_lock;
760
761 spin_unlock_irqrestore(hba->host->host_lock, flags);
762
763 /* put the link into hibern8 mode before turning off clocks */
764 if (ufshcd_can_hibern8_during_gating(hba)) {
765 if (ufshcd_uic_hibern8_enter(hba)) {
766 hba->clk_gating.state = CLKS_ON;
767 goto out;
768 }
769 ufshcd_set_link_hibern8(hba);
770 }
771
Gilad Bronera5082532016-10-17 17:10:00 -0700772 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +0300773
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300774 if (!ufshcd_is_link_active(hba))
775 ufshcd_setup_clocks(hba, false);
776 else
777 /* If link is active, device ref_clk can't be switched off */
778 __ufshcd_setup_clocks(hba, false, true);
779
780 /*
781 * In case you are here to cancel this work the gating state
782 * would be marked as REQ_CLKS_ON. In this case keep the state
783 * as REQ_CLKS_ON which would anyway imply that clocks are off
784 * and a request to turn them on is pending. By doing this way,
785 * we keep the state machine in tact and this would ultimately
786 * prevent from doing cancel work multiple times when there are
787 * new requests arriving before the current cancel work is done.
788 */
789 spin_lock_irqsave(hba->host->host_lock, flags);
790 if (hba->clk_gating.state == REQ_CLKS_OFF)
791 hba->clk_gating.state = CLKS_OFF;
792
793rel_lock:
794 spin_unlock_irqrestore(hba->host->host_lock, flags);
795out:
796 return;
797}
798
799/* host lock must be held before calling this variant */
800static void __ufshcd_release(struct ufs_hba *hba)
801{
802 if (!ufshcd_is_clkgating_allowed(hba))
803 return;
804
805 hba->clk_gating.active_reqs--;
806
807 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
808 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
809 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200810 || hba->active_uic_cmd || hba->uic_async_done
811 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300812 return;
813
814 hba->clk_gating.state = REQ_CLKS_OFF;
815 schedule_delayed_work(&hba->clk_gating.gate_work,
816 msecs_to_jiffies(hba->clk_gating.delay_ms));
817}
818
819void ufshcd_release(struct ufs_hba *hba)
820{
821 unsigned long flags;
822
823 spin_lock_irqsave(hba->host->host_lock, flags);
824 __ufshcd_release(hba);
825 spin_unlock_irqrestore(hba->host->host_lock, flags);
826}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200827EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300828
829static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
830 struct device_attribute *attr, char *buf)
831{
832 struct ufs_hba *hba = dev_get_drvdata(dev);
833
834 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
835}
836
837static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
838 struct device_attribute *attr, const char *buf, size_t count)
839{
840 struct ufs_hba *hba = dev_get_drvdata(dev);
841 unsigned long flags, value;
842
843 if (kstrtoul(buf, 0, &value))
844 return -EINVAL;
845
846 spin_lock_irqsave(hba->host->host_lock, flags);
847 hba->clk_gating.delay_ms = value;
848 spin_unlock_irqrestore(hba->host->host_lock, flags);
849 return count;
850}
851
852static void ufshcd_init_clk_gating(struct ufs_hba *hba)
853{
854 if (!ufshcd_is_clkgating_allowed(hba))
855 return;
856
857 hba->clk_gating.delay_ms = 150;
858 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
859 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
860
861 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
862 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
863 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
864 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
865 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
866 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
867 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
868}
869
870static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
871{
872 if (!ufshcd_is_clkgating_allowed(hba))
873 return;
874 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900875 cancel_work_sync(&hba->clk_gating.ungate_work);
876 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300877}
878
Sahitya Tummala856b3482014-09-25 15:32:34 +0300879/* Must be called with host lock acquired */
880static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
881{
882 if (!ufshcd_is_clkscaling_enabled(hba))
883 return;
884
885 if (!hba->clk_scaling.is_busy_started) {
886 hba->clk_scaling.busy_start_t = ktime_get();
887 hba->clk_scaling.is_busy_started = true;
888 }
889}
890
891static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
892{
893 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
894
895 if (!ufshcd_is_clkscaling_enabled(hba))
896 return;
897
898 if (!hba->outstanding_reqs && scaling->is_busy_started) {
899 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
900 scaling->busy_start_t));
901 scaling->busy_start_t = ktime_set(0, 0);
902 scaling->is_busy_started = false;
903 }
904}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530905/**
906 * ufshcd_send_command - Send SCSI or device management commands
907 * @hba: per adapter instance
908 * @task_tag: Task tag of the command
909 */
910static inline
911void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
912{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300913 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530914 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530915 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -0700916 /* Make sure that doorbell is committed immediately */
917 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530918}
919
920/**
921 * ufshcd_copy_sense_data - Copy sense data in case of check condition
922 * @lrb - pointer to local reference block
923 */
924static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
925{
926 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530927 if (lrbp->sense_buffer &&
928 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -0700929 int len_to_copy;
930
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530931 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -0700932 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
933
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530934 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530935 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -0700936 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530937 }
938}
939
940/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530941 * ufshcd_copy_query_response() - Copy the Query Response and the data
942 * descriptor
943 * @hba: per adapter instance
944 * @lrb - pointer to local reference block
945 */
946static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300947int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530948{
949 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
950
Dolev Raviv68078d52013-07-30 00:35:58 +0530951 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530952
Dolev Raviv68078d52013-07-30 00:35:58 +0530953 /* Get the descriptor */
954 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300955 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530956 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300957 u16 resp_len;
958 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530959
960 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300961 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530962 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300963 buf_len = be16_to_cpu(
964 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300965 if (likely(buf_len >= resp_len)) {
966 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
967 } else {
968 dev_warn(hba->dev,
969 "%s: Response size is bigger than buffer",
970 __func__);
971 return -EINVAL;
972 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530973 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300974
975 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530976}
977
978/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530979 * ufshcd_hba_capabilities - Read controller capabilities
980 * @hba: per adapter instance
981 */
982static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
983{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530984 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530985
986 /* nutrs and nutmrs are 0 based values */
987 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
988 hba->nutmrs =
989 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
990}
991
992/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530993 * ufshcd_ready_for_uic_cmd - Check if controller is ready
994 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530995 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530996 * Return true on success, else false
997 */
998static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
999{
1000 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
1001 return true;
1002 else
1003 return false;
1004}
1005
1006/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301007 * ufshcd_get_upmcrs - Get the power mode change request status
1008 * @hba: Pointer to adapter instance
1009 *
1010 * This function gets the UPMCRS field of HCS register
1011 * Returns value of UPMCRS field
1012 */
1013static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1014{
1015 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1016}
1017
1018/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301019 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1020 * @hba: per adapter instance
1021 * @uic_cmd: UIC command
1022 *
1023 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301024 */
1025static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301026ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301027{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301028 WARN_ON(hba->active_uic_cmd);
1029
1030 hba->active_uic_cmd = uic_cmd;
1031
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301032 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301033 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1034 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1035 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301036
1037 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301038 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301039 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301040}
1041
1042/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301043 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1044 * @hba: per adapter instance
1045 * @uic_command: UIC command
1046 *
1047 * Must be called with mutex held.
1048 * Returns 0 only if success.
1049 */
1050static int
1051ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1052{
1053 int ret;
1054 unsigned long flags;
1055
1056 if (wait_for_completion_timeout(&uic_cmd->done,
1057 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1058 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1059 else
1060 ret = -ETIMEDOUT;
1061
1062 spin_lock_irqsave(hba->host->host_lock, flags);
1063 hba->active_uic_cmd = NULL;
1064 spin_unlock_irqrestore(hba->host->host_lock, flags);
1065
1066 return ret;
1067}
1068
1069/**
1070 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1071 * @hba: per adapter instance
1072 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001073 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301074 *
1075 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001076 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301077 * Returns 0 only if success.
1078 */
1079static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001080__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1081 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301082{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301083 if (!ufshcd_ready_for_uic_cmd(hba)) {
1084 dev_err(hba->dev,
1085 "Controller not ready to accept UIC commands\n");
1086 return -EIO;
1087 }
1088
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001089 if (completion)
1090 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301091
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301092 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301093
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001094 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301095}
1096
1097/**
1098 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1099 * @hba: per adapter instance
1100 * @uic_cmd: UIC command
1101 *
1102 * Returns 0 only if success.
1103 */
1104static int
1105ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1106{
1107 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001108 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301109
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001110 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301111 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001112 ufshcd_add_delay_before_dme_cmd(hba);
1113
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001114 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001115 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001116 spin_unlock_irqrestore(hba->host->host_lock, flags);
1117 if (!ret)
1118 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1119
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301120 mutex_unlock(&hba->uic_cmd_mutex);
1121
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001122 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301123 return ret;
1124}
1125
1126/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301127 * ufshcd_map_sg - Map scatter-gather list to prdt
1128 * @lrbp - pointer to local reference block
1129 *
1130 * Returns 0 in case of success, non-zero value in case of failure
1131 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001132static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301133{
1134 struct ufshcd_sg_entry *prd_table;
1135 struct scatterlist *sg;
1136 struct scsi_cmnd *cmd;
1137 int sg_segments;
1138 int i;
1139
1140 cmd = lrbp->cmd;
1141 sg_segments = scsi_dma_map(cmd);
1142 if (sg_segments < 0)
1143 return sg_segments;
1144
1145 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001146 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1147 lrbp->utr_descriptor_ptr->prd_table_length =
1148 cpu_to_le16((u16)(sg_segments *
1149 sizeof(struct ufshcd_sg_entry)));
1150 else
1151 lrbp->utr_descriptor_ptr->prd_table_length =
1152 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301153
1154 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1155
1156 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1157 prd_table[i].size =
1158 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1159 prd_table[i].base_addr =
1160 cpu_to_le32(lower_32_bits(sg->dma_address));
1161 prd_table[i].upper_addr =
1162 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001163 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301164 }
1165 } else {
1166 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1167 }
1168
1169 return 0;
1170}
1171
1172/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301173 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301174 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301175 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301176 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301177static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301178{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301179 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1180
1181 if (hba->ufs_version == UFSHCI_VERSION_10) {
1182 u32 rw;
1183 rw = set & INTERRUPT_MASK_RW_VER_10;
1184 set = rw | ((set ^ intrs) & intrs);
1185 } else {
1186 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301187 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301188
1189 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1190}
1191
1192/**
1193 * ufshcd_disable_intr - disable interrupts
1194 * @hba: per adapter instance
1195 * @intrs: interrupt bits
1196 */
1197static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1198{
1199 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1200
1201 if (hba->ufs_version == UFSHCI_VERSION_10) {
1202 u32 rw;
1203 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1204 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1205 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1206
1207 } else {
1208 set &= ~intrs;
1209 }
1210
1211 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301212}
1213
1214/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301215 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1216 * descriptor according to request
1217 * @lrbp: pointer to local reference block
1218 * @upiu_flags: flags required in the header
1219 * @cmd_dir: requests data direction
1220 */
1221static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001222 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301223{
1224 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1225 u32 data_direction;
1226 u32 dword_0;
1227
1228 if (cmd_dir == DMA_FROM_DEVICE) {
1229 data_direction = UTP_DEVICE_TO_HOST;
1230 *upiu_flags = UPIU_CMD_FLAGS_READ;
1231 } else if (cmd_dir == DMA_TO_DEVICE) {
1232 data_direction = UTP_HOST_TO_DEVICE;
1233 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1234 } else {
1235 data_direction = UTP_NO_DATA_TRANSFER;
1236 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1237 }
1238
1239 dword_0 = data_direction | (lrbp->command_type
1240 << UPIU_COMMAND_TYPE_OFFSET);
1241 if (lrbp->intr_cmd)
1242 dword_0 |= UTP_REQ_DESC_INT_CMD;
1243
1244 /* Transfer request descriptor header fields */
1245 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001246 /* dword_1 is reserved, hence it is set to 0 */
1247 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301248 /*
1249 * assigning invalid value for command status. Controller
1250 * updates OCS on command completion, with the command
1251 * status
1252 */
1253 req_desc->header.dword_2 =
1254 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001255 /* dword_3 is reserved, hence it is set to 0 */
1256 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001257
1258 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301259}
1260
1261/**
1262 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1263 * for scsi commands
1264 * @lrbp - local reference block pointer
1265 * @upiu_flags - flags
1266 */
1267static
1268void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1269{
1270 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001271 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301272
1273 /* command descriptor fields */
1274 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1275 UPIU_TRANSACTION_COMMAND, upiu_flags,
1276 lrbp->lun, lrbp->task_tag);
1277 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1278 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1279
1280 /* Total EHS length and Data segment length will be zero */
1281 ucd_req_ptr->header.dword_2 = 0;
1282
1283 ucd_req_ptr->sc.exp_data_transfer_len =
1284 cpu_to_be32(lrbp->cmd->sdb.length);
1285
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001286 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1287 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1288 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1289
1290 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301291}
1292
Dolev Raviv68078d52013-07-30 00:35:58 +05301293/**
1294 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1295 * for query requsts
1296 * @hba: UFS hba
1297 * @lrbp: local reference block pointer
1298 * @upiu_flags: flags
1299 */
1300static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1301 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1302{
1303 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1304 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301305 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301306 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1307
1308 /* Query request header */
1309 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1310 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1311 lrbp->lun, lrbp->task_tag);
1312 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1313 0, query->request.query_func, 0, 0);
1314
Zang Leigang68612852016-08-25 17:39:19 +08001315 /* Data segment length only need for WRITE_DESC */
1316 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1317 ucd_req_ptr->header.dword_2 =
1318 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1319 else
1320 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301321
1322 /* Copy the Query Request buffer as is */
1323 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1324 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301325
1326 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001327 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1328 memcpy(descp, query->descriptor, len);
1329
Yaniv Gardi51047262016-02-01 15:02:38 +02001330 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301331}
1332
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301333static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1334{
1335 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1336
1337 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1338
1339 /* command descriptor fields */
1340 ucd_req_ptr->header.dword_0 =
1341 UPIU_HEADER_DWORD(
1342 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001343 /* clear rest of the fields of basic header */
1344 ucd_req_ptr->header.dword_1 = 0;
1345 ucd_req_ptr->header.dword_2 = 0;
1346
1347 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301348}
1349
1350/**
Joao Pinto300bb132016-05-11 12:21:27 +01001351 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1352 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301353 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301354 * @lrb - pointer to local reference block
1355 */
Joao Pinto300bb132016-05-11 12:21:27 +01001356static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301357{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301358 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301359 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301360
Joao Pinto300bb132016-05-11 12:21:27 +01001361 if (hba->ufs_version == UFSHCI_VERSION_20)
1362 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1363 else
1364 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1365
1366 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1367 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1368 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1369 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1370 ufshcd_prepare_utp_nop_upiu(lrbp);
1371 else
1372 ret = -EINVAL;
1373
1374 return ret;
1375}
1376
1377/**
1378 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1379 * for SCSI Purposes
1380 * @hba - per adapter instance
1381 * @lrb - pointer to local reference block
1382 */
1383static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1384{
1385 u32 upiu_flags;
1386 int ret = 0;
1387
1388 if (hba->ufs_version == UFSHCI_VERSION_20)
1389 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1390 else
1391 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1392
1393 if (likely(lrbp->cmd)) {
1394 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1395 lrbp->cmd->sc_data_direction);
1396 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1397 } else {
1398 ret = -EINVAL;
1399 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301400
1401 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301402}
1403
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001404/*
1405 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1406 * @scsi_lun: scsi LUN id
1407 *
1408 * Returns UPIU LUN id
1409 */
1410static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1411{
1412 if (scsi_is_wlun(scsi_lun))
1413 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1414 | UFS_UPIU_WLUN_ID;
1415 else
1416 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1417}
1418
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301419/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001420 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1421 * @scsi_lun: UPIU W-LUN id
1422 *
1423 * Returns SCSI W-LUN id
1424 */
1425static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1426{
1427 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1428}
1429
1430/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301431 * ufshcd_queuecommand - main entry point for SCSI requests
1432 * @cmd: command from SCSI Midlayer
1433 * @done: call back function
1434 *
1435 * Returns 0 for success, non-zero in case of failure
1436 */
1437static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1438{
1439 struct ufshcd_lrb *lrbp;
1440 struct ufs_hba *hba;
1441 unsigned long flags;
1442 int tag;
1443 int err = 0;
1444
1445 hba = shost_priv(host);
1446
1447 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001448 if (!ufshcd_valid_tag(hba, tag)) {
1449 dev_err(hba->dev,
1450 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1451 __func__, tag, cmd, cmd->request);
1452 BUG();
1453 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301454
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301455 spin_lock_irqsave(hba->host->host_lock, flags);
1456 switch (hba->ufshcd_state) {
1457 case UFSHCD_STATE_OPERATIONAL:
1458 break;
Zang Leigang141f8162016-11-16 11:29:37 +08001459 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301460 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301461 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301462 goto out_unlock;
1463 case UFSHCD_STATE_ERROR:
1464 set_host_byte(cmd, DID_ERROR);
1465 cmd->scsi_done(cmd);
1466 goto out_unlock;
1467 default:
1468 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1469 __func__, hba->ufshcd_state);
1470 set_host_byte(cmd, DID_BAD_TARGET);
1471 cmd->scsi_done(cmd);
1472 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301473 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001474
1475 /* if error handling is in progress, don't issue commands */
1476 if (ufshcd_eh_in_progress(hba)) {
1477 set_host_byte(cmd, DID_ERROR);
1478 cmd->scsi_done(cmd);
1479 goto out_unlock;
1480 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301481 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301482
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301483 /* acquire the tag to make sure device cmds don't use it */
1484 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1485 /*
1486 * Dev manage command in progress, requeue the command.
1487 * Requeuing the command helps in cases where the request *may*
1488 * find different tag instead of waiting for dev manage command
1489 * completion.
1490 */
1491 err = SCSI_MLQUEUE_HOST_BUSY;
1492 goto out;
1493 }
1494
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001495 err = ufshcd_hold(hba, true);
1496 if (err) {
1497 err = SCSI_MLQUEUE_HOST_BUSY;
1498 clear_bit_unlock(tag, &hba->lrb_in_use);
1499 goto out;
1500 }
1501 WARN_ON(hba->clk_gating.state != CLKS_ON);
1502
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301503 lrbp = &hba->lrb[tag];
1504
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301505 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301506 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001507 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301508 lrbp->sense_buffer = cmd->sense_buffer;
1509 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001510 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001511 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301512
Joao Pinto300bb132016-05-11 12:21:27 +01001513 ufshcd_comp_scsi_upiu(hba, lrbp);
1514
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001515 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301516 if (err) {
1517 lrbp->cmd = NULL;
1518 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301519 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301520 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001521 /* Make sure descriptors are ready before ringing the doorbell */
1522 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301523
1524 /* issue command to the controller */
1525 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001526 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301527 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301528out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301529 spin_unlock_irqrestore(hba->host->host_lock, flags);
1530out:
1531 return err;
1532}
1533
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301534static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1535 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1536{
1537 lrbp->cmd = NULL;
1538 lrbp->sense_bufflen = 0;
1539 lrbp->sense_buffer = NULL;
1540 lrbp->task_tag = tag;
1541 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301542 lrbp->intr_cmd = true; /* No interrupt aggregation */
1543 hba->dev_cmd.type = cmd_type;
1544
Joao Pinto300bb132016-05-11 12:21:27 +01001545 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301546}
1547
1548static int
1549ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1550{
1551 int err = 0;
1552 unsigned long flags;
1553 u32 mask = 1 << tag;
1554
1555 /* clear outstanding transaction before retry */
1556 spin_lock_irqsave(hba->host->host_lock, flags);
1557 ufshcd_utrl_clear(hba, tag);
1558 spin_unlock_irqrestore(hba->host->host_lock, flags);
1559
1560 /*
1561 * wait for for h/w to clear corresponding bit in door-bell.
1562 * max. wait is 1 sec.
1563 */
1564 err = ufshcd_wait_for_register(hba,
1565 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001566 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301567
1568 return err;
1569}
1570
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001571static int
1572ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1573{
1574 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1575
1576 /* Get the UPIU response */
1577 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1578 UPIU_RSP_CODE_OFFSET;
1579 return query_res->response;
1580}
1581
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301582/**
1583 * ufshcd_dev_cmd_completion() - handles device management command responses
1584 * @hba: per adapter instance
1585 * @lrbp: pointer to local reference block
1586 */
1587static int
1588ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1589{
1590 int resp;
1591 int err = 0;
1592
1593 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1594
1595 switch (resp) {
1596 case UPIU_TRANSACTION_NOP_IN:
1597 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1598 err = -EINVAL;
1599 dev_err(hba->dev, "%s: unexpected response %x\n",
1600 __func__, resp);
1601 }
1602 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301603 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001604 err = ufshcd_check_query_response(hba, lrbp);
1605 if (!err)
1606 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301607 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301608 case UPIU_TRANSACTION_REJECT_UPIU:
1609 /* TODO: handle Reject UPIU Response */
1610 err = -EPERM;
1611 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1612 __func__);
1613 break;
1614 default:
1615 err = -EINVAL;
1616 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1617 __func__, resp);
1618 break;
1619 }
1620
1621 return err;
1622}
1623
1624static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1625 struct ufshcd_lrb *lrbp, int max_timeout)
1626{
1627 int err = 0;
1628 unsigned long time_left;
1629 unsigned long flags;
1630
1631 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1632 msecs_to_jiffies(max_timeout));
1633
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001634 /* Make sure descriptors are ready before ringing the doorbell */
1635 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301636 spin_lock_irqsave(hba->host->host_lock, flags);
1637 hba->dev_cmd.complete = NULL;
1638 if (likely(time_left)) {
1639 err = ufshcd_get_tr_ocs(lrbp);
1640 if (!err)
1641 err = ufshcd_dev_cmd_completion(hba, lrbp);
1642 }
1643 spin_unlock_irqrestore(hba->host->host_lock, flags);
1644
1645 if (!time_left) {
1646 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001647 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1648 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301649 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001650 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301651 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001652 /*
1653 * in case of an error, after clearing the doorbell,
1654 * we also need to clear the outstanding_request
1655 * field in hba
1656 */
1657 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301658 }
1659
1660 return err;
1661}
1662
1663/**
1664 * ufshcd_get_dev_cmd_tag - Get device management command tag
1665 * @hba: per-adapter instance
1666 * @tag: pointer to variable with available slot value
1667 *
1668 * Get a free slot and lock it until device management command
1669 * completes.
1670 *
1671 * Returns false if free slot is unavailable for locking, else
1672 * return true with tag value in @tag.
1673 */
1674static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1675{
1676 int tag;
1677 bool ret = false;
1678 unsigned long tmp;
1679
1680 if (!tag_out)
1681 goto out;
1682
1683 do {
1684 tmp = ~hba->lrb_in_use;
1685 tag = find_last_bit(&tmp, hba->nutrs);
1686 if (tag >= hba->nutrs)
1687 goto out;
1688 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1689
1690 *tag_out = tag;
1691 ret = true;
1692out:
1693 return ret;
1694}
1695
1696static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1697{
1698 clear_bit_unlock(tag, &hba->lrb_in_use);
1699}
1700
1701/**
1702 * ufshcd_exec_dev_cmd - API for sending device management requests
1703 * @hba - UFS hba
1704 * @cmd_type - specifies the type (NOP, Query...)
1705 * @timeout - time in seconds
1706 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301707 * NOTE: Since there is only one available tag for device management commands,
1708 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301709 */
1710static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1711 enum dev_cmd_type cmd_type, int timeout)
1712{
1713 struct ufshcd_lrb *lrbp;
1714 int err;
1715 int tag;
1716 struct completion wait;
1717 unsigned long flags;
1718
1719 /*
1720 * Get free slot, sleep if slots are unavailable.
1721 * Even though we use wait_event() which sleeps indefinitely,
1722 * the maximum wait time is bounded by SCSI request timeout.
1723 */
1724 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1725
1726 init_completion(&wait);
1727 lrbp = &hba->lrb[tag];
1728 WARN_ON(lrbp->cmd);
1729 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1730 if (unlikely(err))
1731 goto out_put_tag;
1732
1733 hba->dev_cmd.complete = &wait;
1734
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001735 /* Make sure descriptors are ready before ringing the doorbell */
1736 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301737 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001738 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301739 ufshcd_send_command(hba, tag);
1740 spin_unlock_irqrestore(hba->host->host_lock, flags);
1741
1742 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1743
1744out_put_tag:
1745 ufshcd_put_dev_cmd_tag(hba, tag);
1746 wake_up(&hba->dev_cmd.tag_wq);
1747 return err;
1748}
1749
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301750/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001751 * ufshcd_init_query() - init the query response and request parameters
1752 * @hba: per-adapter instance
1753 * @request: address of the request pointer to be initialized
1754 * @response: address of the response pointer to be initialized
1755 * @opcode: operation to perform
1756 * @idn: flag idn to access
1757 * @index: LU number to access
1758 * @selector: query/flag/descriptor further identification
1759 */
1760static inline void ufshcd_init_query(struct ufs_hba *hba,
1761 struct ufs_query_req **request, struct ufs_query_res **response,
1762 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1763{
1764 *request = &hba->dev_cmd.query.request;
1765 *response = &hba->dev_cmd.query.response;
1766 memset(*request, 0, sizeof(struct ufs_query_req));
1767 memset(*response, 0, sizeof(struct ufs_query_res));
1768 (*request)->upiu_req.opcode = opcode;
1769 (*request)->upiu_req.idn = idn;
1770 (*request)->upiu_req.index = index;
1771 (*request)->upiu_req.selector = selector;
1772}
1773
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001774static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1775 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1776{
1777 int ret;
1778 int retries;
1779
1780 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1781 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1782 if (ret)
1783 dev_dbg(hba->dev,
1784 "%s: failed with error %d, retries %d\n",
1785 __func__, ret, retries);
1786 else
1787 break;
1788 }
1789
1790 if (ret)
1791 dev_err(hba->dev,
1792 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1793 __func__, opcode, idn, ret, retries);
1794 return ret;
1795}
1796
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001797/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301798 * ufshcd_query_flag() - API function for sending flag query requests
1799 * hba: per-adapter instance
1800 * query_opcode: flag query to perform
1801 * idn: flag idn to access
1802 * flag_res: the flag value after the query request completes
1803 *
1804 * Returns 0 for success, non-zero in case of failure
1805 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001806int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301807 enum flag_idn idn, bool *flag_res)
1808{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001809 struct ufs_query_req *request = NULL;
1810 struct ufs_query_res *response = NULL;
1811 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001812 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301813
1814 BUG_ON(!hba);
1815
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001816 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301817 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001818 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1819 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301820
1821 switch (opcode) {
1822 case UPIU_QUERY_OPCODE_SET_FLAG:
1823 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1824 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1825 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1826 break;
1827 case UPIU_QUERY_OPCODE_READ_FLAG:
1828 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1829 if (!flag_res) {
1830 /* No dummy reads */
1831 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1832 __func__);
1833 err = -EINVAL;
1834 goto out_unlock;
1835 }
1836 break;
1837 default:
1838 dev_err(hba->dev,
1839 "%s: Expected query flag opcode but got = %d\n",
1840 __func__, opcode);
1841 err = -EINVAL;
1842 goto out_unlock;
1843 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301844
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001845 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1846 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1847
1848 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301849
1850 if (err) {
1851 dev_err(hba->dev,
1852 "%s: Sending flag query for idn %d failed, err = %d\n",
1853 __func__, idn, err);
1854 goto out_unlock;
1855 }
1856
1857 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301858 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301859 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1860
1861out_unlock:
1862 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001863 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301864 return err;
1865}
1866
1867/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301868 * ufshcd_query_attr - API function for sending attribute requests
1869 * hba: per-adapter instance
1870 * opcode: attribute opcode
1871 * idn: attribute idn to access
1872 * index: index field
1873 * selector: selector field
1874 * attr_val: the attribute value after the query request completes
1875 *
1876 * Returns 0 for success, non-zero in case of failure
1877*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301878static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301879 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1880{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001881 struct ufs_query_req *request = NULL;
1882 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301883 int err;
1884
1885 BUG_ON(!hba);
1886
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001887 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301888 if (!attr_val) {
1889 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1890 __func__, opcode);
1891 err = -EINVAL;
1892 goto out;
1893 }
1894
1895 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001896 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1897 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301898
1899 switch (opcode) {
1900 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1901 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301902 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301903 break;
1904 case UPIU_QUERY_OPCODE_READ_ATTR:
1905 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1906 break;
1907 default:
1908 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1909 __func__, opcode);
1910 err = -EINVAL;
1911 goto out_unlock;
1912 }
1913
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001914 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301915
1916 if (err) {
1917 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1918 __func__, opcode, idn, err);
1919 goto out_unlock;
1920 }
1921
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301922 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301923
1924out_unlock:
1925 mutex_unlock(&hba->dev_cmd.lock);
1926out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001927 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301928 return err;
1929}
1930
1931/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001932 * ufshcd_query_attr_retry() - API function for sending query
1933 * attribute with retries
1934 * @hba: per-adapter instance
1935 * @opcode: attribute opcode
1936 * @idn: attribute idn to access
1937 * @index: index field
1938 * @selector: selector field
1939 * @attr_val: the attribute value after the query request
1940 * completes
1941 *
1942 * Returns 0 for success, non-zero in case of failure
1943*/
1944static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1945 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1946 u32 *attr_val)
1947{
1948 int ret = 0;
1949 u32 retries;
1950
1951 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1952 ret = ufshcd_query_attr(hba, opcode, idn, index,
1953 selector, attr_val);
1954 if (ret)
1955 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1956 __func__, ret, retries);
1957 else
1958 break;
1959 }
1960
1961 if (ret)
1962 dev_err(hba->dev,
1963 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1964 __func__, idn, ret, QUERY_REQ_RETRIES);
1965 return ret;
1966}
1967
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001968static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001969 enum query_opcode opcode, enum desc_idn idn, u8 index,
1970 u8 selector, u8 *desc_buf, int *buf_len)
1971{
1972 struct ufs_query_req *request = NULL;
1973 struct ufs_query_res *response = NULL;
1974 int err;
1975
1976 BUG_ON(!hba);
1977
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001978 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001979 if (!desc_buf) {
1980 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1981 __func__, opcode);
1982 err = -EINVAL;
1983 goto out;
1984 }
1985
1986 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1987 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1988 __func__, *buf_len);
1989 err = -EINVAL;
1990 goto out;
1991 }
1992
1993 mutex_lock(&hba->dev_cmd.lock);
1994 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1995 selector);
1996 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001997 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001998
1999 switch (opcode) {
2000 case UPIU_QUERY_OPCODE_WRITE_DESC:
2001 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
2002 break;
2003 case UPIU_QUERY_OPCODE_READ_DESC:
2004 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2005 break;
2006 default:
2007 dev_err(hba->dev,
2008 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2009 __func__, opcode);
2010 err = -EINVAL;
2011 goto out_unlock;
2012 }
2013
2014 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2015
2016 if (err) {
2017 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
2018 __func__, opcode, idn, err);
2019 goto out_unlock;
2020 }
2021
2022 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002023 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002024
2025out_unlock:
2026 mutex_unlock(&hba->dev_cmd.lock);
2027out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002028 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002029 return err;
2030}
2031
2032/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002033 * ufshcd_query_descriptor_retry - API function for sending descriptor
2034 * requests
2035 * hba: per-adapter instance
2036 * opcode: attribute opcode
2037 * idn: attribute idn to access
2038 * index: index field
2039 * selector: selector field
2040 * desc_buf: the buffer that contains the descriptor
2041 * buf_len: length parameter passed to the device
2042 *
2043 * Returns 0 for success, non-zero in case of failure.
2044 * The buf_len parameter will contain, on return, the length parameter
2045 * received on the response.
2046 */
2047int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2048 enum query_opcode opcode, enum desc_idn idn, u8 index,
2049 u8 selector, u8 *desc_buf, int *buf_len)
2050{
2051 int err;
2052 int retries;
2053
2054 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2055 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2056 selector, desc_buf, buf_len);
2057 if (!err || err == -EINVAL)
2058 break;
2059 }
2060
2061 return err;
2062}
2063EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2064
2065/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002066 * ufshcd_read_desc_param - read the specified descriptor parameter
2067 * @hba: Pointer to adapter instance
2068 * @desc_id: descriptor idn value
2069 * @desc_index: descriptor index
2070 * @param_offset: offset of the parameter to read
2071 * @param_read_buf: pointer to buffer where parameter would be read
2072 * @param_size: sizeof(param_read_buf)
2073 *
2074 * Return 0 in case of success, non-zero otherwise
2075 */
2076static int ufshcd_read_desc_param(struct ufs_hba *hba,
2077 enum desc_idn desc_id,
2078 int desc_index,
2079 u32 param_offset,
2080 u8 *param_read_buf,
2081 u32 param_size)
2082{
2083 int ret;
2084 u8 *desc_buf;
2085 u32 buff_len;
2086 bool is_kmalloc = true;
2087
2088 /* safety checks */
2089 if (desc_id >= QUERY_DESC_IDN_MAX)
2090 return -EINVAL;
2091
2092 buff_len = ufs_query_desc_max_size[desc_id];
2093 if ((param_offset + param_size) > buff_len)
2094 return -EINVAL;
2095
2096 if (!param_offset && (param_size == buff_len)) {
2097 /* memory space already available to hold full descriptor */
2098 desc_buf = param_read_buf;
2099 is_kmalloc = false;
2100 } else {
2101 /* allocate memory to hold full descriptor */
2102 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2103 if (!desc_buf)
2104 return -ENOMEM;
2105 }
2106
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002107 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2108 desc_id, desc_index, 0, desc_buf,
2109 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002110
2111 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
2112 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
2113 ufs_query_desc_max_size[desc_id])
2114 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
2115 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
2116 __func__, desc_id, param_offset, buff_len, ret);
2117 if (!ret)
2118 ret = -EINVAL;
2119
2120 goto out;
2121 }
2122
2123 if (is_kmalloc)
2124 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2125out:
2126 if (is_kmalloc)
2127 kfree(desc_buf);
2128 return ret;
2129}
2130
2131static inline int ufshcd_read_desc(struct ufs_hba *hba,
2132 enum desc_idn desc_id,
2133 int desc_index,
2134 u8 *buf,
2135 u32 size)
2136{
2137 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2138}
2139
2140static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2141 u8 *buf,
2142 u32 size)
2143{
Dolev Raviv61e07352016-11-23 16:30:49 -08002144 int err = 0;
2145 int retries;
2146
2147 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2148 /* Read descriptor*/
2149 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2150 if (!err)
2151 break;
2152 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2153 }
2154
2155 return err;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002156}
2157
Yaniv Gardib573d482016-03-10 17:37:09 +02002158int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2159{
2160 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2161}
2162EXPORT_SYMBOL(ufshcd_read_device_desc);
2163
2164/**
2165 * ufshcd_read_string_desc - read string descriptor
2166 * @hba: pointer to adapter instance
2167 * @desc_index: descriptor index
2168 * @buf: pointer to buffer where descriptor would be read
2169 * @size: size of buf
2170 * @ascii: if true convert from unicode to ascii characters
2171 *
2172 * Return 0 in case of success, non-zero otherwise
2173 */
2174int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2175 u32 size, bool ascii)
2176{
2177 int err = 0;
2178
2179 err = ufshcd_read_desc(hba,
2180 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2181
2182 if (err) {
2183 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2184 __func__, QUERY_REQ_RETRIES, err);
2185 goto out;
2186 }
2187
2188 if (ascii) {
2189 int desc_len;
2190 int ascii_len;
2191 int i;
2192 char *buff_ascii;
2193
2194 desc_len = buf[0];
2195 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2196 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2197 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2198 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2199 __func__);
2200 err = -ENOMEM;
2201 goto out;
2202 }
2203
2204 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2205 if (!buff_ascii) {
2206 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002207 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002208 }
2209
2210 /*
2211 * the descriptor contains string in UTF16 format
2212 * we need to convert to utf-8 so it can be displayed
2213 */
2214 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2215 desc_len - QUERY_DESC_HDR_SIZE,
2216 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2217
2218 /* replace non-printable or non-ASCII characters with spaces */
2219 for (i = 0; i < ascii_len; i++)
2220 ufshcd_remove_non_printable(&buff_ascii[i]);
2221
2222 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2223 size - QUERY_DESC_HDR_SIZE);
2224 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2225 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002226 kfree(buff_ascii);
2227 }
2228out:
2229 return err;
2230}
2231EXPORT_SYMBOL(ufshcd_read_string_desc);
2232
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002233/**
2234 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2235 * @hba: Pointer to adapter instance
2236 * @lun: lun id
2237 * @param_offset: offset of the parameter to read
2238 * @param_read_buf: pointer to buffer where parameter would be read
2239 * @param_size: sizeof(param_read_buf)
2240 *
2241 * Return 0 in case of success, non-zero otherwise
2242 */
2243static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2244 int lun,
2245 enum unit_desc_param param_offset,
2246 u8 *param_read_buf,
2247 u32 param_size)
2248{
2249 /*
2250 * Unit descriptors are only available for general purpose LUs (LUN id
2251 * from 0 to 7) and RPMB Well known LU.
2252 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002253 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002254 return -EOPNOTSUPP;
2255
2256 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2257 param_offset, param_read_buf, param_size);
2258}
2259
2260/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302261 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2262 * @hba: per adapter instance
2263 *
2264 * 1. Allocate DMA memory for Command Descriptor array
2265 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2266 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2267 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2268 * (UTMRDL)
2269 * 4. Allocate memory for local reference block(lrb).
2270 *
2271 * Returns 0 for success, non-zero in case of failure
2272 */
2273static int ufshcd_memory_alloc(struct ufs_hba *hba)
2274{
2275 size_t utmrdl_size, utrdl_size, ucdl_size;
2276
2277 /* Allocate memory for UTP command descriptors */
2278 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002279 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2280 ucdl_size,
2281 &hba->ucdl_dma_addr,
2282 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302283
2284 /*
2285 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2286 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2287 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2288 * be aligned to 128 bytes as well
2289 */
2290 if (!hba->ucdl_base_addr ||
2291 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302292 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302293 "Command Descriptor Memory allocation failed\n");
2294 goto out;
2295 }
2296
2297 /*
2298 * Allocate memory for UTP Transfer descriptors
2299 * UFSHCI requires 1024 byte alignment of UTRD
2300 */
2301 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002302 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2303 utrdl_size,
2304 &hba->utrdl_dma_addr,
2305 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302306 if (!hba->utrdl_base_addr ||
2307 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302308 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302309 "Transfer Descriptor Memory allocation failed\n");
2310 goto out;
2311 }
2312
2313 /*
2314 * Allocate memory for UTP Task Management descriptors
2315 * UFSHCI requires 1024 byte alignment of UTMRD
2316 */
2317 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002318 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2319 utmrdl_size,
2320 &hba->utmrdl_dma_addr,
2321 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302322 if (!hba->utmrdl_base_addr ||
2323 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302324 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302325 "Task Management Descriptor Memory allocation failed\n");
2326 goto out;
2327 }
2328
2329 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002330 hba->lrb = devm_kzalloc(hba->dev,
2331 hba->nutrs * sizeof(struct ufshcd_lrb),
2332 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302333 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302334 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302335 goto out;
2336 }
2337 return 0;
2338out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302339 return -ENOMEM;
2340}
2341
2342/**
2343 * ufshcd_host_memory_configure - configure local reference block with
2344 * memory offsets
2345 * @hba: per adapter instance
2346 *
2347 * Configure Host memory space
2348 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2349 * address.
2350 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2351 * and PRDT offset.
2352 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2353 * into local reference block.
2354 */
2355static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2356{
2357 struct utp_transfer_cmd_desc *cmd_descp;
2358 struct utp_transfer_req_desc *utrdlp;
2359 dma_addr_t cmd_desc_dma_addr;
2360 dma_addr_t cmd_desc_element_addr;
2361 u16 response_offset;
2362 u16 prdt_offset;
2363 int cmd_desc_size;
2364 int i;
2365
2366 utrdlp = hba->utrdl_base_addr;
2367 cmd_descp = hba->ucdl_base_addr;
2368
2369 response_offset =
2370 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2371 prdt_offset =
2372 offsetof(struct utp_transfer_cmd_desc, prd_table);
2373
2374 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2375 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2376
2377 for (i = 0; i < hba->nutrs; i++) {
2378 /* Configure UTRD with command descriptor base address */
2379 cmd_desc_element_addr =
2380 (cmd_desc_dma_addr + (cmd_desc_size * i));
2381 utrdlp[i].command_desc_base_addr_lo =
2382 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2383 utrdlp[i].command_desc_base_addr_hi =
2384 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2385
2386 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002387 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2388 utrdlp[i].response_upiu_offset =
2389 cpu_to_le16(response_offset);
2390 utrdlp[i].prd_table_offset =
2391 cpu_to_le16(prdt_offset);
2392 utrdlp[i].response_upiu_length =
2393 cpu_to_le16(ALIGNED_UPIU_SIZE);
2394 } else {
2395 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302396 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002397 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302398 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002399 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302400 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002401 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302402
2403 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302404 hba->lrb[i].ucd_req_ptr =
2405 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302406 hba->lrb[i].ucd_rsp_ptr =
2407 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2408 hba->lrb[i].ucd_prdt_ptr =
2409 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2410 }
2411}
2412
2413/**
2414 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2415 * @hba: per adapter instance
2416 *
2417 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2418 * in order to initialize the Unipro link startup procedure.
2419 * Once the Unipro links are up, the device connected to the controller
2420 * is detected.
2421 *
2422 * Returns 0 on success, non-zero value on failure
2423 */
2424static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2425{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302426 struct uic_command uic_cmd = {0};
2427 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302428
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302429 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2430
2431 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2432 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302433 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302434 "dme-link-startup: error code %d\n", ret);
2435 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302436}
2437
Yaniv Gardicad2e032015-03-31 17:37:14 +03002438static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2439{
2440 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2441 unsigned long min_sleep_time_us;
2442
2443 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2444 return;
2445
2446 /*
2447 * last_dme_cmd_tstamp will be 0 only for 1st call to
2448 * this function
2449 */
2450 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2451 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2452 } else {
2453 unsigned long delta =
2454 (unsigned long) ktime_to_us(
2455 ktime_sub(ktime_get(),
2456 hba->last_dme_cmd_tstamp));
2457
2458 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2459 min_sleep_time_us =
2460 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2461 else
2462 return; /* no more delay required */
2463 }
2464
2465 /* allow sleep for extra 50us if needed */
2466 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2467}
2468
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302469/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302470 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2471 * @hba: per adapter instance
2472 * @attr_sel: uic command argument1
2473 * @attr_set: attribute set type as uic command argument2
2474 * @mib_val: setting value as uic command argument3
2475 * @peer: indicate whether peer or local
2476 *
2477 * Returns 0 on success, non-zero value on failure
2478 */
2479int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2480 u8 attr_set, u32 mib_val, u8 peer)
2481{
2482 struct uic_command uic_cmd = {0};
2483 static const char *const action[] = {
2484 "dme-set",
2485 "dme-peer-set"
2486 };
2487 const char *set = action[!!peer];
2488 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002489 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302490
2491 uic_cmd.command = peer ?
2492 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2493 uic_cmd.argument1 = attr_sel;
2494 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2495 uic_cmd.argument3 = mib_val;
2496
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002497 do {
2498 /* for peer attributes we retry upon failure */
2499 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2500 if (ret)
2501 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2502 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2503 } while (ret && peer && --retries);
2504
2505 if (!retries)
2506 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2507 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2508 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302509
2510 return ret;
2511}
2512EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2513
2514/**
2515 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2516 * @hba: per adapter instance
2517 * @attr_sel: uic command argument1
2518 * @mib_val: the value of the attribute as returned by the UIC command
2519 * @peer: indicate whether peer or local
2520 *
2521 * Returns 0 on success, non-zero value on failure
2522 */
2523int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2524 u32 *mib_val, u8 peer)
2525{
2526 struct uic_command uic_cmd = {0};
2527 static const char *const action[] = {
2528 "dme-get",
2529 "dme-peer-get"
2530 };
2531 const char *get = action[!!peer];
2532 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002533 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002534 struct ufs_pa_layer_attr orig_pwr_info;
2535 struct ufs_pa_layer_attr temp_pwr_info;
2536 bool pwr_mode_change = false;
2537
2538 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2539 orig_pwr_info = hba->pwr_info;
2540 temp_pwr_info = orig_pwr_info;
2541
2542 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2543 orig_pwr_info.pwr_rx == FAST_MODE) {
2544 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2545 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2546 pwr_mode_change = true;
2547 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2548 orig_pwr_info.pwr_rx == SLOW_MODE) {
2549 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2550 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2551 pwr_mode_change = true;
2552 }
2553 if (pwr_mode_change) {
2554 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2555 if (ret)
2556 goto out;
2557 }
2558 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302559
2560 uic_cmd.command = peer ?
2561 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2562 uic_cmd.argument1 = attr_sel;
2563
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002564 do {
2565 /* for peer attributes we retry upon failure */
2566 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2567 if (ret)
2568 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2569 get, UIC_GET_ATTR_ID(attr_sel), ret);
2570 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302571
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002572 if (!retries)
2573 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2574 get, UIC_GET_ATTR_ID(attr_sel), retries);
2575
2576 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302577 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002578
2579 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2580 && pwr_mode_change)
2581 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302582out:
2583 return ret;
2584}
2585EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2586
2587/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002588 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2589 * state) and waits for it to take effect.
2590 *
2591 * @hba: per adapter instance
2592 * @cmd: UIC command to execute
2593 *
2594 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2595 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2596 * and device UniPro link and hence it's final completion would be indicated by
2597 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2598 * addition to normal UIC command completion Status (UCCS). This function only
2599 * returns after the relevant status bits indicate the completion.
2600 *
2601 * Returns 0 on success, non-zero value on failure
2602 */
2603static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2604{
2605 struct completion uic_async_done;
2606 unsigned long flags;
2607 u8 status;
2608 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002609 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002610
2611 mutex_lock(&hba->uic_cmd_mutex);
2612 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002613 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002614
2615 spin_lock_irqsave(hba->host->host_lock, flags);
2616 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002617 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2618 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2619 /*
2620 * Make sure UIC command completion interrupt is disabled before
2621 * issuing UIC command.
2622 */
2623 wmb();
2624 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002625 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002626 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2627 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002628 if (ret) {
2629 dev_err(hba->dev,
2630 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2631 cmd->command, cmd->argument3, ret);
2632 goto out;
2633 }
2634
2635 if (!wait_for_completion_timeout(hba->uic_async_done,
2636 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2637 dev_err(hba->dev,
2638 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2639 cmd->command, cmd->argument3);
2640 ret = -ETIMEDOUT;
2641 goto out;
2642 }
2643
2644 status = ufshcd_get_upmcrs(hba);
2645 if (status != PWR_LOCAL) {
2646 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002647 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002648 cmd->command, status);
2649 ret = (status != PWR_OK) ? status : -1;
2650 }
2651out:
2652 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002653 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002654 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002655 if (reenable_intr)
2656 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002657 spin_unlock_irqrestore(hba->host->host_lock, flags);
2658 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002659
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002660 return ret;
2661}
2662
2663/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302664 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2665 * using DME_SET primitives.
2666 * @hba: per adapter instance
2667 * @mode: powr mode value
2668 *
2669 * Returns 0 on success, non-zero value on failure
2670 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302671static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302672{
2673 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002674 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302675
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002676 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2677 ret = ufshcd_dme_set(hba,
2678 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2679 if (ret) {
2680 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2681 __func__, ret);
2682 goto out;
2683 }
2684 }
2685
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302686 uic_cmd.command = UIC_CMD_DME_SET;
2687 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2688 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002689 ufshcd_hold(hba, false);
2690 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2691 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302692
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002693out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002694 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002695}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302696
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002697static int ufshcd_link_recovery(struct ufs_hba *hba)
2698{
2699 int ret;
2700 unsigned long flags;
2701
2702 spin_lock_irqsave(hba->host->host_lock, flags);
2703 hba->ufshcd_state = UFSHCD_STATE_RESET;
2704 ufshcd_set_eh_in_progress(hba);
2705 spin_unlock_irqrestore(hba->host->host_lock, flags);
2706
2707 ret = ufshcd_host_reset_and_restore(hba);
2708
2709 spin_lock_irqsave(hba->host->host_lock, flags);
2710 if (ret)
2711 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2712 ufshcd_clear_eh_in_progress(hba);
2713 spin_unlock_irqrestore(hba->host->host_lock, flags);
2714
2715 if (ret)
2716 dev_err(hba->dev, "%s: link recovery failed, err %d",
2717 __func__, ret);
2718
2719 return ret;
2720}
2721
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002722static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002723{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002724 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002725 struct uic_command uic_cmd = {0};
2726
Kiwoong Kimee32c902016-11-10 21:17:43 +09002727 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
2728
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002729 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002730 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002731
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002732 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002733 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2734 __func__, ret);
2735
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002736 /*
2737 * If link recovery fails then return error so that caller
2738 * don't retry the hibern8 enter again.
2739 */
2740 if (ufshcd_link_recovery(hba))
2741 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09002742 } else
2743 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
2744 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002745
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002746 return ret;
2747}
2748
2749static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2750{
2751 int ret = 0, retries;
2752
2753 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2754 ret = __ufshcd_uic_hibern8_enter(hba);
2755 if (!ret || ret == -ENOLINK)
2756 goto out;
2757 }
2758out:
2759 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002760}
2761
2762static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2763{
2764 struct uic_command uic_cmd = {0};
2765 int ret;
2766
Kiwoong Kimee32c902016-11-10 21:17:43 +09002767 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
2768
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002769 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2770 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302771 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002772 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2773 __func__, ret);
2774 ret = ufshcd_link_recovery(hba);
Kiwoong Kimee32c902016-11-10 21:17:43 +09002775 } else
2776 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
2777 POST_CHANGE);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302778
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302779 return ret;
2780}
2781
Yaniv Gardi50646362014-10-23 13:25:13 +03002782 /**
2783 * ufshcd_init_pwr_info - setting the POR (power on reset)
2784 * values in hba power info
2785 * @hba: per-adapter instance
2786 */
2787static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2788{
2789 hba->pwr_info.gear_rx = UFS_PWM_G1;
2790 hba->pwr_info.gear_tx = UFS_PWM_G1;
2791 hba->pwr_info.lane_rx = 1;
2792 hba->pwr_info.lane_tx = 1;
2793 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2794 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2795 hba->pwr_info.hs_rate = 0;
2796}
2797
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302798/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002799 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2800 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302801 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002802static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302803{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002804 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2805
2806 if (hba->max_pwr_info.is_valid)
2807 return 0;
2808
2809 pwr_info->pwr_tx = FASTAUTO_MODE;
2810 pwr_info->pwr_rx = FASTAUTO_MODE;
2811 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302812
2813 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002814 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2815 &pwr_info->lane_rx);
2816 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2817 &pwr_info->lane_tx);
2818
2819 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2820 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2821 __func__,
2822 pwr_info->lane_rx,
2823 pwr_info->lane_tx);
2824 return -EINVAL;
2825 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302826
2827 /*
2828 * First, get the maximum gears of HS speed.
2829 * If a zero value, it means there is no HSGEAR capability.
2830 * Then, get the maximum gears of PWM speed.
2831 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002832 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2833 if (!pwr_info->gear_rx) {
2834 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2835 &pwr_info->gear_rx);
2836 if (!pwr_info->gear_rx) {
2837 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2838 __func__, pwr_info->gear_rx);
2839 return -EINVAL;
2840 }
2841 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302842 }
2843
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002844 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2845 &pwr_info->gear_tx);
2846 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302847 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002848 &pwr_info->gear_tx);
2849 if (!pwr_info->gear_tx) {
2850 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2851 __func__, pwr_info->gear_tx);
2852 return -EINVAL;
2853 }
2854 pwr_info->pwr_tx = SLOWAUTO_MODE;
2855 }
2856
2857 hba->max_pwr_info.is_valid = true;
2858 return 0;
2859}
2860
2861static int ufshcd_change_power_mode(struct ufs_hba *hba,
2862 struct ufs_pa_layer_attr *pwr_mode)
2863{
2864 int ret;
2865
2866 /* if already configured to the requested pwr_mode */
2867 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2868 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2869 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2870 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2871 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2872 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2873 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2874 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2875 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302876 }
2877
2878 /*
2879 * Configure attributes for power mode change with below.
2880 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2881 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2882 * - PA_HSSERIES
2883 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002884 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2885 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2886 pwr_mode->lane_rx);
2887 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2888 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302889 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002890 else
2891 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302892
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002893 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2894 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2895 pwr_mode->lane_tx);
2896 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2897 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302898 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002899 else
2900 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302901
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002902 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2903 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2904 pwr_mode->pwr_rx == FAST_MODE ||
2905 pwr_mode->pwr_tx == FAST_MODE)
2906 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2907 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302908
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002909 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2910 | pwr_mode->pwr_tx);
2911
2912 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302913 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002914 "%s: power mode change failed %d\n", __func__, ret);
2915 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002916 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2917 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002918
2919 memcpy(&hba->pwr_info, pwr_mode,
2920 sizeof(struct ufs_pa_layer_attr));
2921 }
2922
2923 return ret;
2924}
2925
2926/**
2927 * ufshcd_config_pwr_mode - configure a new power mode
2928 * @hba: per-adapter instance
2929 * @desired_pwr_mode: desired power configuration
2930 */
2931static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2932 struct ufs_pa_layer_attr *desired_pwr_mode)
2933{
2934 struct ufs_pa_layer_attr final_params = { 0 };
2935 int ret;
2936
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002937 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2938 desired_pwr_mode, &final_params);
2939
2940 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002941 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2942
2943 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302944
2945 return ret;
2946}
2947
2948/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302949 * ufshcd_complete_dev_init() - checks device readiness
2950 * hba: per-adapter instance
2951 *
2952 * Set fDeviceInit flag and poll until device toggles it.
2953 */
2954static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2955{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002956 int i;
2957 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302958 bool flag_res = 1;
2959
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002960 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2961 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302962 if (err) {
2963 dev_err(hba->dev,
2964 "%s setting fDeviceInit flag failed with error %d\n",
2965 __func__, err);
2966 goto out;
2967 }
2968
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002969 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2970 for (i = 0; i < 1000 && !err && flag_res; i++)
2971 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2972 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2973
Dolev Raviv68078d52013-07-30 00:35:58 +05302974 if (err)
2975 dev_err(hba->dev,
2976 "%s reading fDeviceInit flag failed with error %d\n",
2977 __func__, err);
2978 else if (flag_res)
2979 dev_err(hba->dev,
2980 "%s fDeviceInit was not cleared by the device\n",
2981 __func__);
2982
2983out:
2984 return err;
2985}
2986
2987/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302988 * ufshcd_make_hba_operational - Make UFS controller operational
2989 * @hba: per adapter instance
2990 *
2991 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002992 * 1. Enable required interrupts
2993 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02002994 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002995 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302996 *
2997 * Returns 0 on success, non-zero value on failure
2998 */
2999static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3000{
3001 int err = 0;
3002 u32 reg;
3003
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303004 /* Enable required interrupts */
3005 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3006
3007 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003008 if (ufshcd_is_intr_aggr_allowed(hba))
3009 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3010 else
3011 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303012
3013 /* Configure UTRL and UTMRL base address registers */
3014 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3015 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3016 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3017 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3018 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3019 REG_UTP_TASK_REQ_LIST_BASE_L);
3020 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3021 REG_UTP_TASK_REQ_LIST_BASE_H);
3022
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303023 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003024 * Make sure base address and interrupt setup are updated before
3025 * enabling the run/stop registers below.
3026 */
3027 wmb();
3028
3029 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303030 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303031 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003032 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303033 if (!(ufshcd_get_lists_status(reg))) {
3034 ufshcd_enable_run_stop_reg(hba);
3035 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303036 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303037 "Host controller not ready to process requests");
3038 err = -EIO;
3039 goto out;
3040 }
3041
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303042out:
3043 return err;
3044}
3045
3046/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003047 * ufshcd_hba_stop - Send controller to reset state
3048 * @hba: per adapter instance
3049 * @can_sleep: perform sleep or just spin
3050 */
3051static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3052{
3053 int err;
3054
3055 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3056 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3057 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3058 10, 1, can_sleep);
3059 if (err)
3060 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3061}
3062
3063/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303064 * ufshcd_hba_enable - initialize the controller
3065 * @hba: per adapter instance
3066 *
3067 * The controller resets itself and controller firmware initialization
3068 * sequence kicks off. When controller is ready it will set
3069 * the Host Controller Enable bit to 1.
3070 *
3071 * Returns 0 on success, non-zero value on failure
3072 */
3073static int ufshcd_hba_enable(struct ufs_hba *hba)
3074{
3075 int retry;
3076
3077 /*
3078 * msleep of 1 and 5 used in this function might result in msleep(20),
3079 * but it was necessary to send the UFS FPGA to reset mode during
3080 * development and testing of this driver. msleep can be changed to
3081 * mdelay and retry count can be reduced based on the controller.
3082 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003083 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303084 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003085 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303086
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003087 /* UniPro link is disabled at this point */
3088 ufshcd_set_link_off(hba);
3089
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003090 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003091
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303092 /* start controller initialization sequence */
3093 ufshcd_hba_start(hba);
3094
3095 /*
3096 * To initialize a UFS host controller HCE bit must be set to 1.
3097 * During initialization the HCE bit value changes from 1->0->1.
3098 * When the host controller completes initialization sequence
3099 * it sets the value of HCE bit to 1. The same HCE bit is read back
3100 * to check if the controller has completed initialization sequence.
3101 * So without this delay the value HCE = 1, set in the previous
3102 * instruction might be read back.
3103 * This delay can be changed based on the controller.
3104 */
3105 msleep(1);
3106
3107 /* wait for the host controller to complete initialization */
3108 retry = 10;
3109 while (ufshcd_is_hba_active(hba)) {
3110 if (retry) {
3111 retry--;
3112 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303113 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303114 "Controller enable failed\n");
3115 return -EIO;
3116 }
3117 msleep(5);
3118 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003119
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003120 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003121 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003122
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003123 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003124
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303125 return 0;
3126}
3127
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003128static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3129{
3130 int tx_lanes, i, err = 0;
3131
3132 if (!peer)
3133 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3134 &tx_lanes);
3135 else
3136 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3137 &tx_lanes);
3138 for (i = 0; i < tx_lanes; i++) {
3139 if (!peer)
3140 err = ufshcd_dme_set(hba,
3141 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3142 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3143 0);
3144 else
3145 err = ufshcd_dme_peer_set(hba,
3146 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3147 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3148 0);
3149 if (err) {
3150 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3151 __func__, peer, i, err);
3152 break;
3153 }
3154 }
3155
3156 return err;
3157}
3158
3159static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3160{
3161 return ufshcd_disable_tx_lcc(hba, true);
3162}
3163
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303164/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303165 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303166 * @hba: per adapter instance
3167 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303168 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303169 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303170static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303171{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303172 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003173 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303174
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003175 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003176 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303177
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003178 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003179
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003180 /* check if device is detected by inter-connect layer */
3181 if (!ret && !ufshcd_is_device_present(hba)) {
3182 dev_err(hba->dev, "%s: Device not present\n", __func__);
3183 ret = -ENXIO;
3184 goto out;
3185 }
3186
3187 /*
3188 * DME link lost indication is only received when link is up,
3189 * but we can't be sure if the link is up until link startup
3190 * succeeds. So reset the local Uni-Pro and try again.
3191 */
3192 if (ret && ufshcd_hba_enable(hba))
3193 goto out;
3194 } while (ret && retries--);
3195
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303196 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003197 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303198 goto out;
3199
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003200 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3201 ret = ufshcd_disable_device_tx_lcc(hba);
3202 if (ret)
3203 goto out;
3204 }
3205
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003206 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003207 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3208 if (ret)
3209 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003210
3211 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303212out:
3213 if (ret)
3214 dev_err(hba->dev, "link startup failed %d\n", ret);
3215 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303216}
3217
3218/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303219 * ufshcd_verify_dev_init() - Verify device initialization
3220 * @hba: per-adapter instance
3221 *
3222 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3223 * device Transport Protocol (UTP) layer is ready after a reset.
3224 * If the UTP layer at the device side is not initialized, it may
3225 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3226 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3227 */
3228static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3229{
3230 int err = 0;
3231 int retries;
3232
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003233 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303234 mutex_lock(&hba->dev_cmd.lock);
3235 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3236 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3237 NOP_OUT_TIMEOUT);
3238
3239 if (!err || err == -ETIMEDOUT)
3240 break;
3241
3242 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3243 }
3244 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003245 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303246
3247 if (err)
3248 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3249 return err;
3250}
3251
3252/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003253 * ufshcd_set_queue_depth - set lun queue depth
3254 * @sdev: pointer to SCSI device
3255 *
3256 * Read bLUQueueDepth value and activate scsi tagged command
3257 * queueing. For WLUN, queue depth is set to 1. For best-effort
3258 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3259 * value that host can queue.
3260 */
3261static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3262{
3263 int ret = 0;
3264 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08003265 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003266 struct ufs_hba *hba;
3267
3268 hba = shost_priv(sdev->host);
3269
3270 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08003271 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3272 /* Read descriptor*/
3273 ret = ufshcd_read_unit_desc_param(hba,
3274 ufshcd_scsi_to_upiu_lun(sdev->lun),
3275 UNIT_DESC_PARAM_LU_Q_DEPTH,
3276 &lun_qdepth,
3277 sizeof(lun_qdepth));
3278 if (!ret || ret == -ENOTSUPP)
3279 break;
3280
3281 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
3282 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003283
3284 /* Some WLUN doesn't support unit descriptor */
3285 if (ret == -EOPNOTSUPP)
3286 lun_qdepth = 1;
3287 else if (!lun_qdepth)
3288 /* eventually, we can figure out the real queue depth */
3289 lun_qdepth = hba->nutrs;
3290 else
3291 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3292
3293 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3294 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003295 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003296}
3297
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003298/*
3299 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3300 * @hba: per-adapter instance
3301 * @lun: UFS device lun id
3302 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3303 *
3304 * Returns 0 in case of success and b_lu_write_protect status would be returned
3305 * @b_lu_write_protect parameter.
3306 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3307 * Returns -EINVAL in case of invalid parameters passed to this function.
3308 */
3309static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3310 u8 lun,
3311 u8 *b_lu_write_protect)
3312{
3313 int ret;
3314
3315 if (!b_lu_write_protect)
3316 ret = -EINVAL;
3317 /*
3318 * According to UFS device spec, RPMB LU can't be write
3319 * protected so skip reading bLUWriteProtect parameter for
3320 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3321 */
3322 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3323 ret = -ENOTSUPP;
3324 else
3325 ret = ufshcd_read_unit_desc_param(hba,
3326 lun,
3327 UNIT_DESC_PARAM_LU_WR_PROTECT,
3328 b_lu_write_protect,
3329 sizeof(*b_lu_write_protect));
3330 return ret;
3331}
3332
3333/**
3334 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3335 * status
3336 * @hba: per-adapter instance
3337 * @sdev: pointer to SCSI device
3338 *
3339 */
3340static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3341 struct scsi_device *sdev)
3342{
3343 if (hba->dev_info.f_power_on_wp_en &&
3344 !hba->dev_info.is_lu_power_on_wp) {
3345 u8 b_lu_write_protect;
3346
3347 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3348 &b_lu_write_protect) &&
3349 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3350 hba->dev_info.is_lu_power_on_wp = true;
3351 }
3352}
3353
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003354/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303355 * ufshcd_slave_alloc - handle initial SCSI device configurations
3356 * @sdev: pointer to SCSI device
3357 *
3358 * Returns success
3359 */
3360static int ufshcd_slave_alloc(struct scsi_device *sdev)
3361{
3362 struct ufs_hba *hba;
3363
3364 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303365
3366 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3367 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303368
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303369 /* allow SCSI layer to restart the device in case of errors */
3370 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003371
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003372 /* REPORT SUPPORTED OPERATION CODES is not supported */
3373 sdev->no_report_opcodes = 1;
3374
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003375
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003376 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003377
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003378 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3379
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003380 return 0;
3381}
3382
3383/**
3384 * ufshcd_change_queue_depth - change queue depth
3385 * @sdev: pointer to SCSI device
3386 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003387 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003388 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003389 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003390static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003391{
3392 struct ufs_hba *hba = shost_priv(sdev->host);
3393
3394 if (depth > hba->nutrs)
3395 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003396 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303397}
3398
3399/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003400 * ufshcd_slave_configure - adjust SCSI device configurations
3401 * @sdev: pointer to SCSI device
3402 */
3403static int ufshcd_slave_configure(struct scsi_device *sdev)
3404{
3405 struct request_queue *q = sdev->request_queue;
3406
3407 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3408 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3409
3410 return 0;
3411}
3412
3413/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303414 * ufshcd_slave_destroy - remove SCSI device configurations
3415 * @sdev: pointer to SCSI device
3416 */
3417static void ufshcd_slave_destroy(struct scsi_device *sdev)
3418{
3419 struct ufs_hba *hba;
3420
3421 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003422 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003423 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3424 unsigned long flags;
3425
3426 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003427 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003428 spin_unlock_irqrestore(hba->host->host_lock, flags);
3429 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303430}
3431
3432/**
3433 * ufshcd_task_req_compl - handle task management request completion
3434 * @hba: per adapter instance
3435 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303436 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303437 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303438 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303439 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303440static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303441{
3442 struct utp_task_req_desc *task_req_descp;
3443 struct utp_upiu_task_rsp *task_rsp_upiup;
3444 unsigned long flags;
3445 int ocs_value;
3446 int task_result;
3447
3448 spin_lock_irqsave(hba->host->host_lock, flags);
3449
3450 /* Clear completed tasks from outstanding_tasks */
3451 __clear_bit(index, &hba->outstanding_tasks);
3452
3453 task_req_descp = hba->utmrdl_base_addr;
3454 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3455
3456 if (ocs_value == OCS_SUCCESS) {
3457 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3458 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003459 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3460 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303461 if (resp)
3462 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303463 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303464 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3465 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303466 }
3467 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303468
3469 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303470}
3471
3472/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303473 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3474 * @lrb: pointer to local reference block of completed command
3475 * @scsi_status: SCSI command status
3476 *
3477 * Returns value base on SCSI command status
3478 */
3479static inline int
3480ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3481{
3482 int result = 0;
3483
3484 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303485 case SAM_STAT_CHECK_CONDITION:
3486 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303487 case SAM_STAT_GOOD:
3488 result |= DID_OK << 16 |
3489 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303490 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303491 break;
3492 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303493 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303494 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303495 ufshcd_copy_sense_data(lrbp);
3496 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303497 break;
3498 default:
3499 result |= DID_ERROR << 16;
3500 break;
3501 } /* end of switch */
3502
3503 return result;
3504}
3505
3506/**
3507 * ufshcd_transfer_rsp_status - Get overall status of the response
3508 * @hba: per adapter instance
3509 * @lrb: pointer to local reference block of completed command
3510 *
3511 * Returns result of the command to notify SCSI midlayer
3512 */
3513static inline int
3514ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3515{
3516 int result = 0;
3517 int scsi_status;
3518 int ocs;
3519
3520 /* overall command status of utrd */
3521 ocs = ufshcd_get_tr_ocs(lrbp);
3522
3523 switch (ocs) {
3524 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303525 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303526
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303527 switch (result) {
3528 case UPIU_TRANSACTION_RESPONSE:
3529 /*
3530 * get the response UPIU result to extract
3531 * the SCSI command status
3532 */
3533 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3534
3535 /*
3536 * get the result based on SCSI status response
3537 * to notify the SCSI midlayer of the command status
3538 */
3539 scsi_status = result & MASK_SCSI_STATUS;
3540 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303541
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003542 /*
3543 * Currently we are only supporting BKOPs exception
3544 * events hence we can ignore BKOPs exception event
3545 * during power management callbacks. BKOPs exception
3546 * event is not expected to be raised in runtime suspend
3547 * callback as it allows the urgent bkops.
3548 * During system suspend, we are anyway forcefully
3549 * disabling the bkops and if urgent bkops is needed
3550 * it will be enabled on system resume. Long term
3551 * solution could be to abort the system suspend if
3552 * UFS device needs urgent BKOPs.
3553 */
3554 if (!hba->pm_op_in_progress &&
3555 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303556 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303557 break;
3558 case UPIU_TRANSACTION_REJECT_UPIU:
3559 /* TODO: handle Reject UPIU Response */
3560 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303561 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303562 "Reject UPIU not fully implemented\n");
3563 break;
3564 default:
3565 result = DID_ERROR << 16;
3566 dev_err(hba->dev,
3567 "Unexpected request response code = %x\n",
3568 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303569 break;
3570 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303571 break;
3572 case OCS_ABORTED:
3573 result |= DID_ABORT << 16;
3574 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303575 case OCS_INVALID_COMMAND_STATUS:
3576 result |= DID_REQUEUE << 16;
3577 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303578 case OCS_INVALID_CMD_TABLE_ATTR:
3579 case OCS_INVALID_PRDT_ATTR:
3580 case OCS_MISMATCH_DATA_BUF_SIZE:
3581 case OCS_MISMATCH_RESP_UPIU_SIZE:
3582 case OCS_PEER_COMM_FAILURE:
3583 case OCS_FATAL_ERROR:
3584 default:
3585 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303586 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303587 "OCS error from controller = %x\n", ocs);
3588 break;
3589 } /* end of switch */
3590
3591 return result;
3592}
3593
3594/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303595 * ufshcd_uic_cmd_compl - handle completion of uic command
3596 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303597 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303598 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303599static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303600{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303601 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303602 hba->active_uic_cmd->argument2 |=
3603 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303604 hba->active_uic_cmd->argument3 =
3605 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303606 complete(&hba->active_uic_cmd->done);
3607 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303608
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003609 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3610 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303611}
3612
3613/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003614 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303615 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003616 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303617 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003618static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3619 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303620{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303621 struct ufshcd_lrb *lrbp;
3622 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303623 int result;
3624 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003625
Dolev Ravive9d501b2014-07-01 12:22:37 +03003626 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3627 lrbp = &hba->lrb[index];
3628 cmd = lrbp->cmd;
3629 if (cmd) {
3630 result = ufshcd_transfer_rsp_status(hba, lrbp);
3631 scsi_dma_unmap(cmd);
3632 cmd->result = result;
3633 /* Mark completed command as NULL in LRB */
3634 lrbp->cmd = NULL;
3635 clear_bit_unlock(index, &hba->lrb_in_use);
3636 /* Do not touch lrbp after scsi done */
3637 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003638 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003639 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3640 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003641 if (hba->dev_cmd.complete)
3642 complete(hba->dev_cmd.complete);
3643 }
3644 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303645
3646 /* clear corresponding bits of completed commands */
3647 hba->outstanding_reqs ^= completed_reqs;
3648
Sahitya Tummala856b3482014-09-25 15:32:34 +03003649 ufshcd_clk_scaling_update_busy(hba);
3650
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303651 /* we might have free'd some tags above */
3652 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303653}
3654
3655/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003656 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3657 * @hba: per adapter instance
3658 */
3659static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3660{
3661 unsigned long completed_reqs;
3662 u32 tr_doorbell;
3663
3664 /* Resetting interrupt aggregation counters first and reading the
3665 * DOOR_BELL afterward allows us to handle all the completed requests.
3666 * In order to prevent other interrupts starvation the DB is read once
3667 * after reset. The down side of this solution is the possibility of
3668 * false interrupt if device completes another request after resetting
3669 * aggregation and before reading the DB.
3670 */
3671 if (ufshcd_is_intr_aggr_allowed(hba))
3672 ufshcd_reset_intr_aggr(hba);
3673
3674 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3675 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3676
3677 __ufshcd_transfer_req_compl(hba, completed_reqs);
3678}
3679
3680/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303681 * ufshcd_disable_ee - disable exception event
3682 * @hba: per-adapter instance
3683 * @mask: exception event to disable
3684 *
3685 * Disables exception event in the device so that the EVENT_ALERT
3686 * bit is not set.
3687 *
3688 * Returns zero on success, non-zero error value on failure.
3689 */
3690static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3691{
3692 int err = 0;
3693 u32 val;
3694
3695 if (!(hba->ee_ctrl_mask & mask))
3696 goto out;
3697
3698 val = hba->ee_ctrl_mask & ~mask;
3699 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003700 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303701 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3702 if (!err)
3703 hba->ee_ctrl_mask &= ~mask;
3704out:
3705 return err;
3706}
3707
3708/**
3709 * ufshcd_enable_ee - enable exception event
3710 * @hba: per-adapter instance
3711 * @mask: exception event to enable
3712 *
3713 * Enable corresponding exception event in the device to allow
3714 * device to alert host in critical scenarios.
3715 *
3716 * Returns zero on success, non-zero error value on failure.
3717 */
3718static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3719{
3720 int err = 0;
3721 u32 val;
3722
3723 if (hba->ee_ctrl_mask & mask)
3724 goto out;
3725
3726 val = hba->ee_ctrl_mask | mask;
3727 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003728 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303729 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3730 if (!err)
3731 hba->ee_ctrl_mask |= mask;
3732out:
3733 return err;
3734}
3735
3736/**
3737 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3738 * @hba: per-adapter instance
3739 *
3740 * Allow device to manage background operations on its own. Enabling
3741 * this might lead to inconsistent latencies during normal data transfers
3742 * as the device is allowed to manage its own way of handling background
3743 * operations.
3744 *
3745 * Returns zero on success, non-zero on failure.
3746 */
3747static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3748{
3749 int err = 0;
3750
3751 if (hba->auto_bkops_enabled)
3752 goto out;
3753
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003754 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303755 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3756 if (err) {
3757 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3758 __func__, err);
3759 goto out;
3760 }
3761
3762 hba->auto_bkops_enabled = true;
3763
3764 /* No need of URGENT_BKOPS exception from the device */
3765 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3766 if (err)
3767 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3768 __func__, err);
3769out:
3770 return err;
3771}
3772
3773/**
3774 * ufshcd_disable_auto_bkops - block device in doing background operations
3775 * @hba: per-adapter instance
3776 *
3777 * Disabling background operations improves command response latency but
3778 * has drawback of device moving into critical state where the device is
3779 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3780 * host is idle so that BKOPS are managed effectively without any negative
3781 * impacts.
3782 *
3783 * Returns zero on success, non-zero on failure.
3784 */
3785static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3786{
3787 int err = 0;
3788
3789 if (!hba->auto_bkops_enabled)
3790 goto out;
3791
3792 /*
3793 * If host assisted BKOPs is to be enabled, make sure
3794 * urgent bkops exception is allowed.
3795 */
3796 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3797 if (err) {
3798 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3799 __func__, err);
3800 goto out;
3801 }
3802
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003803 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303804 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3805 if (err) {
3806 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3807 __func__, err);
3808 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3809 goto out;
3810 }
3811
3812 hba->auto_bkops_enabled = false;
3813out:
3814 return err;
3815}
3816
3817/**
3818 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3819 * @hba: per adapter instance
3820 *
3821 * After a device reset the device may toggle the BKOPS_EN flag
3822 * to default value. The s/w tracking variables should be updated
3823 * as well. Do this by forcing enable of auto bkops.
3824 */
3825static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3826{
3827 hba->auto_bkops_enabled = false;
3828 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3829 ufshcd_enable_auto_bkops(hba);
3830}
3831
3832static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3833{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003834 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303835 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3836}
3837
3838/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003839 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3840 * @hba: per-adapter instance
3841 * @status: bkops_status value
3842 *
3843 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3844 * flag in the device to permit background operations if the device
3845 * bkops_status is greater than or equal to "status" argument passed to
3846 * this function, disable otherwise.
3847 *
3848 * Returns 0 for success, non-zero in case of failure.
3849 *
3850 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3851 * to know whether auto bkops is enabled or disabled after this function
3852 * returns control to it.
3853 */
3854static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3855 enum bkops_status status)
3856{
3857 int err;
3858 u32 curr_status = 0;
3859
3860 err = ufshcd_get_bkops_status(hba, &curr_status);
3861 if (err) {
3862 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3863 __func__, err);
3864 goto out;
3865 } else if (curr_status > BKOPS_STATUS_MAX) {
3866 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3867 __func__, curr_status);
3868 err = -EINVAL;
3869 goto out;
3870 }
3871
3872 if (curr_status >= status)
3873 err = ufshcd_enable_auto_bkops(hba);
3874 else
3875 err = ufshcd_disable_auto_bkops(hba);
3876out:
3877 return err;
3878}
3879
3880/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303881 * ufshcd_urgent_bkops - handle urgent bkops exception event
3882 * @hba: per-adapter instance
3883 *
3884 * Enable fBackgroundOpsEn flag in the device to permit background
3885 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003886 *
3887 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3888 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303889 */
3890static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3891{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003892 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303893}
3894
3895static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3896{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003897 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303898 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3899}
3900
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003901static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3902{
3903 int err;
3904 u32 curr_status = 0;
3905
3906 if (hba->is_urgent_bkops_lvl_checked)
3907 goto enable_auto_bkops;
3908
3909 err = ufshcd_get_bkops_status(hba, &curr_status);
3910 if (err) {
3911 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3912 __func__, err);
3913 goto out;
3914 }
3915
3916 /*
3917 * We are seeing that some devices are raising the urgent bkops
3918 * exception events even when BKOPS status doesn't indicate performace
3919 * impacted or critical. Handle these device by determining their urgent
3920 * bkops status at runtime.
3921 */
3922 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
3923 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
3924 __func__, curr_status);
3925 /* update the current status as the urgent bkops level */
3926 hba->urgent_bkops_lvl = curr_status;
3927 hba->is_urgent_bkops_lvl_checked = true;
3928 }
3929
3930enable_auto_bkops:
3931 err = ufshcd_enable_auto_bkops(hba);
3932out:
3933 if (err < 0)
3934 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3935 __func__, err);
3936}
3937
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303938/**
3939 * ufshcd_exception_event_handler - handle exceptions raised by device
3940 * @work: pointer to work data
3941 *
3942 * Read bExceptionEventStatus attribute from the device and handle the
3943 * exception event accordingly.
3944 */
3945static void ufshcd_exception_event_handler(struct work_struct *work)
3946{
3947 struct ufs_hba *hba;
3948 int err;
3949 u32 status = 0;
3950 hba = container_of(work, struct ufs_hba, eeh_work);
3951
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303952 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303953 err = ufshcd_get_ee_status(hba, &status);
3954 if (err) {
3955 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3956 __func__, err);
3957 goto out;
3958 }
3959
3960 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003961
3962 if (status & MASK_EE_URGENT_BKOPS)
3963 ufshcd_bkops_exception_event_handler(hba);
3964
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303965out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303966 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303967 return;
3968}
3969
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003970/* Complete requests that have door-bell cleared */
3971static void ufshcd_complete_requests(struct ufs_hba *hba)
3972{
3973 ufshcd_transfer_req_compl(hba);
3974 ufshcd_tmc_handler(hba);
3975}
3976
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303977/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02003978 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
3979 * to recover from the DL NAC errors or not.
3980 * @hba: per-adapter instance
3981 *
3982 * Returns true if error handling is required, false otherwise
3983 */
3984static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
3985{
3986 unsigned long flags;
3987 bool err_handling = true;
3988
3989 spin_lock_irqsave(hba->host->host_lock, flags);
3990 /*
3991 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
3992 * device fatal error and/or DL NAC & REPLAY timeout errors.
3993 */
3994 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
3995 goto out;
3996
3997 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
3998 ((hba->saved_err & UIC_ERROR) &&
3999 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4000 goto out;
4001
4002 if ((hba->saved_err & UIC_ERROR) &&
4003 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4004 int err;
4005 /*
4006 * wait for 50ms to see if we can get any other errors or not.
4007 */
4008 spin_unlock_irqrestore(hba->host->host_lock, flags);
4009 msleep(50);
4010 spin_lock_irqsave(hba->host->host_lock, flags);
4011
4012 /*
4013 * now check if we have got any other severe errors other than
4014 * DL NAC error?
4015 */
4016 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4017 ((hba->saved_err & UIC_ERROR) &&
4018 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4019 goto out;
4020
4021 /*
4022 * As DL NAC is the only error received so far, send out NOP
4023 * command to confirm if link is still active or not.
4024 * - If we don't get any response then do error recovery.
4025 * - If we get response then clear the DL NAC error bit.
4026 */
4027
4028 spin_unlock_irqrestore(hba->host->host_lock, flags);
4029 err = ufshcd_verify_dev_init(hba);
4030 spin_lock_irqsave(hba->host->host_lock, flags);
4031
4032 if (err)
4033 goto out;
4034
4035 /* Link seems to be alive hence ignore the DL NAC errors */
4036 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4037 hba->saved_err &= ~UIC_ERROR;
4038 /* clear NAC error */
4039 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4040 if (!hba->saved_uic_err) {
4041 err_handling = false;
4042 goto out;
4043 }
4044 }
4045out:
4046 spin_unlock_irqrestore(hba->host->host_lock, flags);
4047 return err_handling;
4048}
4049
4050/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304051 * ufshcd_err_handler - handle UFS errors that require s/w attention
4052 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304053 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304054static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304055{
4056 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304057 unsigned long flags;
4058 u32 err_xfer = 0;
4059 u32 err_tm = 0;
4060 int err = 0;
4061 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004062 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304063
4064 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304065
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304066 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004067 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304068
4069 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004070 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304071 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304072
4073 hba->ufshcd_state = UFSHCD_STATE_RESET;
4074 ufshcd_set_eh_in_progress(hba);
4075
4076 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004077 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004078
4079 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4080 bool ret;
4081
4082 spin_unlock_irqrestore(hba->host->host_lock, flags);
4083 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4084 ret = ufshcd_quirk_dl_nac_errors(hba);
4085 spin_lock_irqsave(hba->host->host_lock, flags);
4086 if (!ret)
4087 goto skip_err_handling;
4088 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004089 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4090 ((hba->saved_err & UIC_ERROR) &&
4091 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4092 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4093 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4094 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304095
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004096 /*
4097 * if host reset is required then skip clearing the pending
4098 * transfers forcefully because they will automatically get
4099 * cleared after link startup.
4100 */
4101 if (needs_reset)
4102 goto skip_pending_xfer_clear;
4103
4104 /* release lock as clear command might sleep */
4105 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304106 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004107 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4108 if (ufshcd_clear_cmd(hba, tag)) {
4109 err_xfer = true;
4110 goto lock_skip_pending_xfer_clear;
4111 }
4112 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304113
4114 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004115 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4116 if (ufshcd_clear_tm_cmd(hba, tag)) {
4117 err_tm = true;
4118 goto lock_skip_pending_xfer_clear;
4119 }
4120 }
4121
4122lock_skip_pending_xfer_clear:
4123 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304124
4125 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004126 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304127
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004128 if (err_xfer || err_tm)
4129 needs_reset = true;
4130
4131skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304132 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004133 if (needs_reset) {
4134 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4135
4136 /*
4137 * ufshcd_reset_and_restore() does the link reinitialization
4138 * which will need atleast one empty doorbell slot to send the
4139 * device management commands (NOP and query commands).
4140 * If there is no slot empty at this moment then free up last
4141 * slot forcefully.
4142 */
4143 if (hba->outstanding_reqs == max_doorbells)
4144 __ufshcd_transfer_req_compl(hba,
4145 (1UL << (hba->nutrs - 1)));
4146
4147 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304148 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004149 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304150 if (err) {
4151 dev_err(hba->dev, "%s: reset and restore failed\n",
4152 __func__);
4153 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4154 }
4155 /*
4156 * Inform scsi mid-layer that we did reset and allow to handle
4157 * Unit Attention properly.
4158 */
4159 scsi_report_bus_reset(hba->host, 0);
4160 hba->saved_err = 0;
4161 hba->saved_uic_err = 0;
4162 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004163
Yaniv Gardi583fa622016-03-10 17:37:13 +02004164skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004165 if (!needs_reset) {
4166 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4167 if (hba->saved_err || hba->saved_uic_err)
4168 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4169 __func__, hba->saved_err, hba->saved_uic_err);
4170 }
4171
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304172 ufshcd_clear_eh_in_progress(hba);
4173
4174out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004175 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304176 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004177 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304178 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304179}
4180
4181/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304182 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4183 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304184 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304185static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304186{
4187 u32 reg;
4188
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304189 /* PA_INIT_ERROR is fatal and needs UIC reset */
4190 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4191 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4192 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004193 else if (hba->dev_quirks &
4194 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4195 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4196 hba->uic_error |=
4197 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4198 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4199 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4200 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304201
4202 /* UIC NL/TL/DME errors needs software retry */
4203 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4204 if (reg)
4205 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4206
4207 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4208 if (reg)
4209 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4210
4211 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4212 if (reg)
4213 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4214
4215 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4216 __func__, hba->uic_error);
4217}
4218
4219/**
4220 * ufshcd_check_errors - Check for errors that need s/w attention
4221 * @hba: per-adapter instance
4222 */
4223static void ufshcd_check_errors(struct ufs_hba *hba)
4224{
4225 bool queue_eh_work = false;
4226
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304227 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304228 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304229
4230 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304231 hba->uic_error = 0;
4232 ufshcd_update_uic_error(hba);
4233 if (hba->uic_error)
4234 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304235 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304236
4237 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004238 /*
4239 * update the transfer error masks to sticky bits, let's do this
4240 * irrespective of current ufshcd_state.
4241 */
4242 hba->saved_err |= hba->errors;
4243 hba->saved_uic_err |= hba->uic_error;
4244
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304245 /* handle fatal errors only when link is functional */
4246 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4247 /* block commands from scsi mid-layer */
4248 scsi_block_requests(hba->host);
4249
Zang Leigang141f8162016-11-16 11:29:37 +08004250 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304251 schedule_work(&hba->eh_work);
4252 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304253 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304254 /*
4255 * if (!queue_eh_work) -
4256 * Other errors are either non-fatal where host recovers
4257 * itself without s/w intervention or errors that will be
4258 * handled by the SCSI core layer.
4259 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304260}
4261
4262/**
4263 * ufshcd_tmc_handler - handle task management function completion
4264 * @hba: per adapter instance
4265 */
4266static void ufshcd_tmc_handler(struct ufs_hba *hba)
4267{
4268 u32 tm_doorbell;
4269
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304270 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304271 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304272 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304273}
4274
4275/**
4276 * ufshcd_sl_intr - Interrupt service routine
4277 * @hba: per adapter instance
4278 * @intr_status: contains interrupts generated by the controller
4279 */
4280static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4281{
4282 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4283 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304284 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304285
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304286 if (intr_status & UFSHCD_UIC_MASK)
4287 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304288
4289 if (intr_status & UTP_TASK_REQ_COMPL)
4290 ufshcd_tmc_handler(hba);
4291
4292 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4293 ufshcd_transfer_req_compl(hba);
4294}
4295
4296/**
4297 * ufshcd_intr - Main interrupt service routine
4298 * @irq: irq number
4299 * @__hba: pointer to adapter instance
4300 *
4301 * Returns IRQ_HANDLED - If interrupt is valid
4302 * IRQ_NONE - If invalid interrupt
4303 */
4304static irqreturn_t ufshcd_intr(int irq, void *__hba)
4305{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004306 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304307 irqreturn_t retval = IRQ_NONE;
4308 struct ufs_hba *hba = __hba;
4309
4310 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304311 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004312 enabled_intr_status =
4313 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304314
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004315 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304316 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004317
4318 if (enabled_intr_status) {
4319 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304320 retval = IRQ_HANDLED;
4321 }
4322 spin_unlock(hba->host->host_lock);
4323 return retval;
4324}
4325
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304326static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4327{
4328 int err = 0;
4329 u32 mask = 1 << tag;
4330 unsigned long flags;
4331
4332 if (!test_bit(tag, &hba->outstanding_tasks))
4333 goto out;
4334
4335 spin_lock_irqsave(hba->host->host_lock, flags);
4336 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4337 spin_unlock_irqrestore(hba->host->host_lock, flags);
4338
4339 /* poll for max. 1 sec to clear door bell register by h/w */
4340 err = ufshcd_wait_for_register(hba,
4341 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004342 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304343out:
4344 return err;
4345}
4346
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304347/**
4348 * ufshcd_issue_tm_cmd - issues task management commands to controller
4349 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304350 * @lun_id: LUN ID to which TM command is sent
4351 * @task_id: task ID to which the TM command is applicable
4352 * @tm_function: task management function opcode
4353 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304354 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304355 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304356 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304357static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4358 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304359{
4360 struct utp_task_req_desc *task_req_descp;
4361 struct utp_upiu_task_req *task_req_upiup;
4362 struct Scsi_Host *host;
4363 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304364 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304365 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304366 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304367
4368 host = hba->host;
4369
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304370 /*
4371 * Get free slot, sleep if slots are unavailable.
4372 * Even though we use wait_event() which sleeps indefinitely,
4373 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4374 */
4375 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004376 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304377
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304378 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304379 task_req_descp = hba->utmrdl_base_addr;
4380 task_req_descp += free_slot;
4381
4382 /* Configure task request descriptor */
4383 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4384 task_req_descp->header.dword_2 =
4385 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4386
4387 /* Configure task request UPIU */
4388 task_req_upiup =
4389 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304390 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304391 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304392 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304393 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304394 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304395 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004396 /*
4397 * The host shall provide the same value for LUN field in the basic
4398 * header and for Input Parameter.
4399 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304400 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4401 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304402
Kiwoong Kimd2877be2016-11-10 21:16:15 +09004403 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
4404
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304405 /* send command to the controller */
4406 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004407
4408 /* Make sure descriptors are ready before ringing the task doorbell */
4409 wmb();
4410
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304411 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004412 /* Make sure that doorbell is committed immediately */
4413 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304414
4415 spin_unlock_irqrestore(host->host_lock, flags);
4416
4417 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304418 err = wait_event_timeout(hba->tm_wq,
4419 test_bit(free_slot, &hba->tm_condition),
4420 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304421 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304422 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4423 __func__, tm_function);
4424 if (ufshcd_clear_tm_cmd(hba, free_slot))
4425 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4426 __func__, free_slot);
4427 err = -ETIMEDOUT;
4428 } else {
4429 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304430 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304431
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304433 ufshcd_put_tm_slot(hba, free_slot);
4434 wake_up(&hba->tm_tag_wq);
4435
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004436 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304437 return err;
4438}
4439
4440/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304441 * ufshcd_eh_device_reset_handler - device reset handler registered to
4442 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304443 * @cmd: SCSI command pointer
4444 *
4445 * Returns SUCCESS/FAILED
4446 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304447static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304448{
4449 struct Scsi_Host *host;
4450 struct ufs_hba *hba;
4451 unsigned int tag;
4452 u32 pos;
4453 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304454 u8 resp = 0xF;
4455 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304456 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304457
4458 host = cmd->device->host;
4459 hba = shost_priv(host);
4460 tag = cmd->request->tag;
4461
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304462 lrbp = &hba->lrb[tag];
4463 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4464 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304465 if (!err)
4466 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304467 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304468 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304469
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304470 /* clear the commands that were pending for corresponding LUN */
4471 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4472 if (hba->lrb[pos].lun == lrbp->lun) {
4473 err = ufshcd_clear_cmd(hba, pos);
4474 if (err)
4475 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304476 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304477 }
4478 spin_lock_irqsave(host->host_lock, flags);
4479 ufshcd_transfer_req_compl(hba);
4480 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304481out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304482 if (!err) {
4483 err = SUCCESS;
4484 } else {
4485 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4486 err = FAILED;
4487 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304488 return err;
4489}
4490
4491/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304492 * ufshcd_abort - abort a specific command
4493 * @cmd: SCSI command pointer
4494 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304495 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4496 * command, and in host controller by clearing the door-bell register. There can
4497 * be race between controller sending the command to the device while abort is
4498 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4499 * really issued and then try to abort it.
4500 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304501 * Returns SUCCESS/FAILED
4502 */
4503static int ufshcd_abort(struct scsi_cmnd *cmd)
4504{
4505 struct Scsi_Host *host;
4506 struct ufs_hba *hba;
4507 unsigned long flags;
4508 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304509 int err = 0;
4510 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304511 u8 resp = 0xF;
4512 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004513 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304514
4515 host = cmd->device->host;
4516 hba = shost_priv(host);
4517 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004518 if (!ufshcd_valid_tag(hba, tag)) {
4519 dev_err(hba->dev,
4520 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4521 __func__, tag, cmd, cmd->request);
4522 BUG();
4523 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304524
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004525 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004526 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004527 /* If command is already aborted/completed, return SUCCESS */
4528 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4529 dev_err(hba->dev,
4530 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4531 __func__, tag, hba->outstanding_reqs, reg);
4532 goto out;
4533 }
4534
Dolev Ravive9d501b2014-07-01 12:22:37 +03004535 if (!(reg & (1 << tag))) {
4536 dev_err(hba->dev,
4537 "%s: cmd was completed, but without a notifying intr, tag = %d",
4538 __func__, tag);
4539 }
4540
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304541 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304542 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4543 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4544 UFS_QUERY_TASK, &resp);
4545 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4546 /* cmd pending in the device */
4547 break;
4548 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304549 /*
4550 * cmd not pending in the device, check if it is
4551 * in transition.
4552 */
4553 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4554 if (reg & (1 << tag)) {
4555 /* sleep for max. 200us to stabilize */
4556 usleep_range(100, 200);
4557 continue;
4558 }
4559 /* command completed already */
4560 goto out;
4561 } else {
4562 if (!err)
4563 err = resp; /* service response error */
4564 goto out;
4565 }
4566 }
4567
4568 if (!poll_cnt) {
4569 err = -EBUSY;
4570 goto out;
4571 }
4572
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304573 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4574 UFS_ABORT_TASK, &resp);
4575 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304576 if (!err)
4577 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304578 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304579 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304580
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304581 err = ufshcd_clear_cmd(hba, tag);
4582 if (err)
4583 goto out;
4584
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304585 scsi_dma_unmap(cmd);
4586
4587 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004588 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304589 hba->lrb[tag].cmd = NULL;
4590 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304591
4592 clear_bit_unlock(tag, &hba->lrb_in_use);
4593 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004594
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304595out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304596 if (!err) {
4597 err = SUCCESS;
4598 } else {
4599 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4600 err = FAILED;
4601 }
4602
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004603 /*
4604 * This ufshcd_release() corresponds to the original scsi cmd that got
4605 * aborted here (as we won't get any IRQ for it).
4606 */
4607 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304608 return err;
4609}
4610
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304611/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304612 * ufshcd_host_reset_and_restore - reset and restore host controller
4613 * @hba: per-adapter instance
4614 *
4615 * Note that host controller reset may issue DME_RESET to
4616 * local and remote (device) Uni-Pro stack and the attributes
4617 * are reset to default state.
4618 *
4619 * Returns zero on success, non-zero on failure
4620 */
4621static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4622{
4623 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304624 unsigned long flags;
4625
4626 /* Reset the host controller */
4627 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004628 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304629 spin_unlock_irqrestore(hba->host->host_lock, flags);
4630
4631 err = ufshcd_hba_enable(hba);
4632 if (err)
4633 goto out;
4634
4635 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004636 err = ufshcd_probe_hba(hba);
4637
4638 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304639 err = -EIO;
4640out:
4641 if (err)
4642 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4643
4644 return err;
4645}
4646
4647/**
4648 * ufshcd_reset_and_restore - reset and re-initialize host/device
4649 * @hba: per-adapter instance
4650 *
4651 * Reset and recover device, host and re-establish link. This
4652 * is helpful to recover the communication in fatal error conditions.
4653 *
4654 * Returns zero on success, non-zero on failure
4655 */
4656static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4657{
4658 int err = 0;
4659 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004660 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304661
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004662 do {
4663 err = ufshcd_host_reset_and_restore(hba);
4664 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304665
4666 /*
4667 * After reset the door-bell might be cleared, complete
4668 * outstanding requests in s/w here.
4669 */
4670 spin_lock_irqsave(hba->host->host_lock, flags);
4671 ufshcd_transfer_req_compl(hba);
4672 ufshcd_tmc_handler(hba);
4673 spin_unlock_irqrestore(hba->host->host_lock, flags);
4674
4675 return err;
4676}
4677
4678/**
4679 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4680 * @cmd - SCSI command pointer
4681 *
4682 * Returns SUCCESS/FAILED
4683 */
4684static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4685{
4686 int err;
4687 unsigned long flags;
4688 struct ufs_hba *hba;
4689
4690 hba = shost_priv(cmd->device->host);
4691
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004692 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304693 /*
4694 * Check if there is any race with fatal error handling.
4695 * If so, wait for it to complete. Even though fatal error
4696 * handling does reset and restore in some cases, don't assume
4697 * anything out of it. We are just avoiding race here.
4698 */
4699 do {
4700 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304701 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304702 hba->ufshcd_state == UFSHCD_STATE_RESET))
4703 break;
4704 spin_unlock_irqrestore(hba->host->host_lock, flags);
4705 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304706 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304707 } while (1);
4708
4709 hba->ufshcd_state = UFSHCD_STATE_RESET;
4710 ufshcd_set_eh_in_progress(hba);
4711 spin_unlock_irqrestore(hba->host->host_lock, flags);
4712
4713 err = ufshcd_reset_and_restore(hba);
4714
4715 spin_lock_irqsave(hba->host->host_lock, flags);
4716 if (!err) {
4717 err = SUCCESS;
4718 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4719 } else {
4720 err = FAILED;
4721 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4722 }
4723 ufshcd_clear_eh_in_progress(hba);
4724 spin_unlock_irqrestore(hba->host->host_lock, flags);
4725
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004726 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304727 return err;
4728}
4729
4730/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004731 * ufshcd_get_max_icc_level - calculate the ICC level
4732 * @sup_curr_uA: max. current supported by the regulator
4733 * @start_scan: row at the desc table to start scan from
4734 * @buff: power descriptor buffer
4735 *
4736 * Returns calculated max ICC level for specific regulator
4737 */
4738static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4739{
4740 int i;
4741 int curr_uA;
4742 u16 data;
4743 u16 unit;
4744
4745 for (i = start_scan; i >= 0; i--) {
4746 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4747 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4748 ATTR_ICC_LVL_UNIT_OFFSET;
4749 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4750 switch (unit) {
4751 case UFSHCD_NANO_AMP:
4752 curr_uA = curr_uA / 1000;
4753 break;
4754 case UFSHCD_MILI_AMP:
4755 curr_uA = curr_uA * 1000;
4756 break;
4757 case UFSHCD_AMP:
4758 curr_uA = curr_uA * 1000 * 1000;
4759 break;
4760 case UFSHCD_MICRO_AMP:
4761 default:
4762 break;
4763 }
4764 if (sup_curr_uA >= curr_uA)
4765 break;
4766 }
4767 if (i < 0) {
4768 i = 0;
4769 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4770 }
4771
4772 return (u32)i;
4773}
4774
4775/**
4776 * ufshcd_calc_icc_level - calculate the max ICC level
4777 * In case regulators are not initialized we'll return 0
4778 * @hba: per-adapter instance
4779 * @desc_buf: power descriptor buffer to extract ICC levels from.
4780 * @len: length of desc_buff
4781 *
4782 * Returns calculated ICC level
4783 */
4784static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4785 u8 *desc_buf, int len)
4786{
4787 u32 icc_level = 0;
4788
4789 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4790 !hba->vreg_info.vccq2) {
4791 dev_err(hba->dev,
4792 "%s: Regulator capability was not set, actvIccLevel=%d",
4793 __func__, icc_level);
4794 goto out;
4795 }
4796
4797 if (hba->vreg_info.vcc)
4798 icc_level = ufshcd_get_max_icc_level(
4799 hba->vreg_info.vcc->max_uA,
4800 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4801 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4802
4803 if (hba->vreg_info.vccq)
4804 icc_level = ufshcd_get_max_icc_level(
4805 hba->vreg_info.vccq->max_uA,
4806 icc_level,
4807 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4808
4809 if (hba->vreg_info.vccq2)
4810 icc_level = ufshcd_get_max_icc_level(
4811 hba->vreg_info.vccq2->max_uA,
4812 icc_level,
4813 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4814out:
4815 return icc_level;
4816}
4817
Dolev Raviv61e07352016-11-23 16:30:49 -08004818static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
4819{
4820 int ret = 0;
4821 int retries;
4822
4823 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
4824 /* write attribute */
4825 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4826 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
4827 if (!ret)
4828 break;
4829
4830 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
4831 }
4832
4833 return ret;
4834}
4835
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004836static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4837{
4838 int ret;
4839 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4840 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4841
4842 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4843 if (ret) {
4844 dev_err(hba->dev,
4845 "%s: Failed reading power descriptor.len = %d ret = %d",
4846 __func__, buff_len, ret);
4847 return;
4848 }
4849
4850 hba->init_prefetch_data.icc_level =
4851 ufshcd_find_max_sup_active_icc_level(hba,
4852 desc_buf, buff_len);
4853 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4854 __func__, hba->init_prefetch_data.icc_level);
4855
Dolev Raviv61e07352016-11-23 16:30:49 -08004856 ret = ufshcd_set_icc_levels_attr(hba,
4857 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004858
4859 if (ret)
4860 dev_err(hba->dev,
4861 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4862 __func__, hba->init_prefetch_data.icc_level , ret);
4863
4864}
4865
4866/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004867 * ufshcd_scsi_add_wlus - Adds required W-LUs
4868 * @hba: per-adapter instance
4869 *
4870 * UFS device specification requires the UFS devices to support 4 well known
4871 * logical units:
4872 * "REPORT_LUNS" (address: 01h)
4873 * "UFS Device" (address: 50h)
4874 * "RPMB" (address: 44h)
4875 * "BOOT" (address: 30h)
4876 * UFS device's power management needs to be controlled by "POWER CONDITION"
4877 * field of SSU (START STOP UNIT) command. But this "power condition" field
4878 * will take effect only when its sent to "UFS device" well known logical unit
4879 * hence we require the scsi_device instance to represent this logical unit in
4880 * order for the UFS host driver to send the SSU command for power management.
4881
4882 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4883 * Block) LU so user space process can control this LU. User space may also
4884 * want to have access to BOOT LU.
4885
4886 * This function adds scsi device instances for each of all well known LUs
4887 * (except "REPORT LUNS" LU).
4888 *
4889 * Returns zero on success (all required W-LUs are added successfully),
4890 * non-zero error value on failure (if failed to add any of the required W-LU).
4891 */
4892static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4893{
4894 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004895 struct scsi_device *sdev_rpmb;
4896 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004897
4898 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4899 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4900 if (IS_ERR(hba->sdev_ufs_device)) {
4901 ret = PTR_ERR(hba->sdev_ufs_device);
4902 hba->sdev_ufs_device = NULL;
4903 goto out;
4904 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004905 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004906
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004907 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004908 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004909 if (IS_ERR(sdev_boot)) {
4910 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004911 goto remove_sdev_ufs_device;
4912 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004913 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004914
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004915 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004916 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004917 if (IS_ERR(sdev_rpmb)) {
4918 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004919 goto remove_sdev_boot;
4920 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004921 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004922 goto out;
4923
4924remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004925 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004926remove_sdev_ufs_device:
4927 scsi_remove_device(hba->sdev_ufs_device);
4928out:
4929 return ret;
4930}
4931
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004932static int ufs_get_device_info(struct ufs_hba *hba,
4933 struct ufs_device_info *card_data)
4934{
4935 int err;
4936 u8 model_index;
4937 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
4938 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
4939
4940 err = ufshcd_read_device_desc(hba, desc_buf,
4941 QUERY_DESC_DEVICE_MAX_SIZE);
4942 if (err) {
4943 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4944 __func__, err);
4945 goto out;
4946 }
4947
4948 /*
4949 * getting vendor (manufacturerID) and Bank Index in big endian
4950 * format
4951 */
4952 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
4953 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4954
4955 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4956
4957 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
4958 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
4959 if (err) {
4960 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
4961 __func__, err);
4962 goto out;
4963 }
4964
4965 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
4966 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
4967 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
4968 MAX_MODEL_LEN));
4969
4970 /* Null terminate the model string */
4971 card_data->model[MAX_MODEL_LEN] = '\0';
4972
4973out:
4974 return err;
4975}
4976
4977void ufs_advertise_fixup_device(struct ufs_hba *hba)
4978{
4979 int err;
4980 struct ufs_dev_fix *f;
4981 struct ufs_device_info card_data;
4982
4983 card_data.wmanufacturerid = 0;
4984
4985 err = ufs_get_device_info(hba, &card_data);
4986 if (err) {
4987 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
4988 __func__, err);
4989 return;
4990 }
4991
4992 for (f = ufs_fixups; f->quirk; f++) {
4993 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
4994 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
4995 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
4996 !strcmp(f->card.model, UFS_ANY_MODEL)))
4997 hba->dev_quirks |= f->quirk;
4998 }
4999}
5000
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005001/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005002 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5003 * @hba: per-adapter instance
5004 *
5005 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5006 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5007 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5008 * the hibern8 exit latency.
5009 *
5010 * Returns zero on success, non-zero error value on failure.
5011 */
5012static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5013{
5014 int ret = 0;
5015 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5016
5017 ret = ufshcd_dme_peer_get(hba,
5018 UIC_ARG_MIB_SEL(
5019 RX_MIN_ACTIVATETIME_CAPABILITY,
5020 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5021 &peer_rx_min_activatetime);
5022 if (ret)
5023 goto out;
5024
5025 /* make sure proper unit conversion is applied */
5026 tuned_pa_tactivate =
5027 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5028 / PA_TACTIVATE_TIME_UNIT_US);
5029 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5030 tuned_pa_tactivate);
5031
5032out:
5033 return ret;
5034}
5035
5036/**
5037 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5038 * @hba: per-adapter instance
5039 *
5040 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5041 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5042 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5043 * This optimal value can help reduce the hibern8 exit latency.
5044 *
5045 * Returns zero on success, non-zero error value on failure.
5046 */
5047static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5048{
5049 int ret = 0;
5050 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5051 u32 max_hibern8_time, tuned_pa_hibern8time;
5052
5053 ret = ufshcd_dme_get(hba,
5054 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5055 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5056 &local_tx_hibern8_time_cap);
5057 if (ret)
5058 goto out;
5059
5060 ret = ufshcd_dme_peer_get(hba,
5061 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5062 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5063 &peer_rx_hibern8_time_cap);
5064 if (ret)
5065 goto out;
5066
5067 max_hibern8_time = max(local_tx_hibern8_time_cap,
5068 peer_rx_hibern8_time_cap);
5069 /* make sure proper unit conversion is applied */
5070 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5071 / PA_HIBERN8_TIME_UNIT_US);
5072 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5073 tuned_pa_hibern8time);
5074out:
5075 return ret;
5076}
5077
5078static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5079{
5080 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5081 ufshcd_tune_pa_tactivate(hba);
5082 ufshcd_tune_pa_hibern8time(hba);
5083 }
5084
5085 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5086 /* set 1ms timeout for PA_TACTIVATE */
5087 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
5088}
5089
5090/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005091 * ufshcd_probe_hba - probe hba to detect device and initialize
5092 * @hba: per-adapter instance
5093 *
5094 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305095 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005096static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305097{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305098 int ret;
5099
5100 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305101 if (ret)
5102 goto out;
5103
Yaniv Gardi50646362014-10-23 13:25:13 +03005104 ufshcd_init_pwr_info(hba);
5105
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005106 /* set the default level for urgent bkops */
5107 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5108 hba->is_urgent_bkops_lvl_checked = false;
5109
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005110 /* UniPro link is active now */
5111 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305112
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305113 ret = ufshcd_verify_dev_init(hba);
5114 if (ret)
5115 goto out;
5116
Dolev Raviv68078d52013-07-30 00:35:58 +05305117 ret = ufshcd_complete_dev_init(hba);
5118 if (ret)
5119 goto out;
5120
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005121 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005122 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005123
5124 ret = ufshcd_set_vccq_rail_unused(hba,
5125 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5126 if (ret)
5127 goto out;
5128
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005129 /* UFS device is also active now */
5130 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305131 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005132 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305133
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005134 if (ufshcd_get_max_pwr_mode(hba)) {
5135 dev_err(hba->dev,
5136 "%s: Failed getting max supported power mode\n",
5137 __func__);
5138 } else {
5139 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005140 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005141 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5142 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005143 goto out;
5144 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005145 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005146
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005147 /* set the state as operational after switching to desired gear */
5148 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005149 /*
5150 * If we are in error handling context or in power management callbacks
5151 * context, no need to scan the host
5152 */
5153 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5154 bool flag;
5155
5156 /* clear any previous UFS device information */
5157 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005158 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5159 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005160 hba->dev_info.f_power_on_wp_en = flag;
5161
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005162 if (!hba->is_init_prefetch)
5163 ufshcd_init_icc_levels(hba);
5164
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005165 /* Add required well known logical units to scsi mid layer */
5166 if (ufshcd_scsi_add_wlus(hba))
5167 goto out;
5168
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305169 scsi_scan_host(hba->host);
5170 pm_runtime_put_sync(hba->dev);
5171 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005172
5173 if (!hba->is_init_prefetch)
5174 hba->is_init_prefetch = true;
5175
Sahitya Tummala856b3482014-09-25 15:32:34 +03005176 /* Resume devfreq after UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07005177 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005178
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305179out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005180 /*
5181 * If we failed to initialize the device or the device is not
5182 * present, turn off the power/clocks etc.
5183 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005184 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5185 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005186 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005187 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005188
5189 return ret;
5190}
5191
5192/**
5193 * ufshcd_async_scan - asynchronous execution for probing hba
5194 * @data: data pointer to pass to this function
5195 * @cookie: cookie data
5196 */
5197static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5198{
5199 struct ufs_hba *hba = (struct ufs_hba *)data;
5200
5201 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305202}
5203
Yaniv Gardif550c652016-03-10 17:37:07 +02005204static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5205{
5206 unsigned long flags;
5207 struct Scsi_Host *host;
5208 struct ufs_hba *hba;
5209 int index;
5210 bool found = false;
5211
5212 if (!scmd || !scmd->device || !scmd->device->host)
5213 return BLK_EH_NOT_HANDLED;
5214
5215 host = scmd->device->host;
5216 hba = shost_priv(host);
5217 if (!hba)
5218 return BLK_EH_NOT_HANDLED;
5219
5220 spin_lock_irqsave(host->host_lock, flags);
5221
5222 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5223 if (hba->lrb[index].cmd == scmd) {
5224 found = true;
5225 break;
5226 }
5227 }
5228
5229 spin_unlock_irqrestore(host->host_lock, flags);
5230
5231 /*
5232 * Bypass SCSI error handling and reset the block layer timer if this
5233 * SCSI command was not actually dispatched to UFS driver, otherwise
5234 * let SCSI layer handle the error as usual.
5235 */
5236 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5237}
5238
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305239static struct scsi_host_template ufshcd_driver_template = {
5240 .module = THIS_MODULE,
5241 .name = UFSHCD,
5242 .proc_name = UFSHCD,
5243 .queuecommand = ufshcd_queuecommand,
5244 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005245 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305246 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005247 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305248 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305249 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5250 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005251 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305252 .this_id = -1,
5253 .sg_tablesize = SG_ALL,
5254 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5255 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005256 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005257 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305258};
5259
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005260static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5261 int ua)
5262{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005263 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005264
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005265 if (!vreg)
5266 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005267
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005268 ret = regulator_set_load(vreg->reg, ua);
5269 if (ret < 0) {
5270 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5271 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005272 }
5273
5274 return ret;
5275}
5276
5277static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5278 struct ufs_vreg *vreg)
5279{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005280 if (!vreg)
5281 return 0;
5282 else if (vreg->unused)
5283 return 0;
5284 else
5285 return ufshcd_config_vreg_load(hba->dev, vreg,
5286 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005287}
5288
5289static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5290 struct ufs_vreg *vreg)
5291{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005292 if (!vreg)
5293 return 0;
5294 else if (vreg->unused)
5295 return 0;
5296 else
5297 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005298}
5299
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005300static int ufshcd_config_vreg(struct device *dev,
5301 struct ufs_vreg *vreg, bool on)
5302{
5303 int ret = 0;
5304 struct regulator *reg = vreg->reg;
5305 const char *name = vreg->name;
5306 int min_uV, uA_load;
5307
5308 BUG_ON(!vreg);
5309
5310 if (regulator_count_voltages(reg) > 0) {
5311 min_uV = on ? vreg->min_uV : 0;
5312 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5313 if (ret) {
5314 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5315 __func__, name, ret);
5316 goto out;
5317 }
5318
5319 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005320 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5321 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005322 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005323 }
5324out:
5325 return ret;
5326}
5327
5328static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5329{
5330 int ret = 0;
5331
Yaniv Gardi60f01872016-03-10 17:37:11 +02005332 if (!vreg)
5333 goto out;
5334 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005335 goto out;
5336
5337 ret = ufshcd_config_vreg(dev, vreg, true);
5338 if (!ret)
5339 ret = regulator_enable(vreg->reg);
5340
5341 if (!ret)
5342 vreg->enabled = true;
5343 else
5344 dev_err(dev, "%s: %s enable failed, err=%d\n",
5345 __func__, vreg->name, ret);
5346out:
5347 return ret;
5348}
5349
5350static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5351{
5352 int ret = 0;
5353
Yaniv Gardi60f01872016-03-10 17:37:11 +02005354 if (!vreg)
5355 goto out;
5356 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005357 goto out;
5358
5359 ret = regulator_disable(vreg->reg);
5360
5361 if (!ret) {
5362 /* ignore errors on applying disable config */
5363 ufshcd_config_vreg(dev, vreg, false);
5364 vreg->enabled = false;
5365 } else {
5366 dev_err(dev, "%s: %s disable failed, err=%d\n",
5367 __func__, vreg->name, ret);
5368 }
5369out:
5370 return ret;
5371}
5372
5373static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5374{
5375 int ret = 0;
5376 struct device *dev = hba->dev;
5377 struct ufs_vreg_info *info = &hba->vreg_info;
5378
5379 if (!info)
5380 goto out;
5381
5382 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5383 if (ret)
5384 goto out;
5385
5386 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5387 if (ret)
5388 goto out;
5389
5390 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5391 if (ret)
5392 goto out;
5393
5394out:
5395 if (ret) {
5396 ufshcd_toggle_vreg(dev, info->vccq2, false);
5397 ufshcd_toggle_vreg(dev, info->vccq, false);
5398 ufshcd_toggle_vreg(dev, info->vcc, false);
5399 }
5400 return ret;
5401}
5402
Raviv Shvili6a771a62014-09-25 15:32:24 +03005403static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5404{
5405 struct ufs_vreg_info *info = &hba->vreg_info;
5406
5407 if (info)
5408 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5409
5410 return 0;
5411}
5412
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005413static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5414{
5415 int ret = 0;
5416
5417 if (!vreg)
5418 goto out;
5419
5420 vreg->reg = devm_regulator_get(dev, vreg->name);
5421 if (IS_ERR(vreg->reg)) {
5422 ret = PTR_ERR(vreg->reg);
5423 dev_err(dev, "%s: %s get failed, err=%d\n",
5424 __func__, vreg->name, ret);
5425 }
5426out:
5427 return ret;
5428}
5429
5430static int ufshcd_init_vreg(struct ufs_hba *hba)
5431{
5432 int ret = 0;
5433 struct device *dev = hba->dev;
5434 struct ufs_vreg_info *info = &hba->vreg_info;
5435
5436 if (!info)
5437 goto out;
5438
5439 ret = ufshcd_get_vreg(dev, info->vcc);
5440 if (ret)
5441 goto out;
5442
5443 ret = ufshcd_get_vreg(dev, info->vccq);
5444 if (ret)
5445 goto out;
5446
5447 ret = ufshcd_get_vreg(dev, info->vccq2);
5448out:
5449 return ret;
5450}
5451
Raviv Shvili6a771a62014-09-25 15:32:24 +03005452static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5453{
5454 struct ufs_vreg_info *info = &hba->vreg_info;
5455
5456 if (info)
5457 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5458
5459 return 0;
5460}
5461
Yaniv Gardi60f01872016-03-10 17:37:11 +02005462static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5463{
5464 int ret = 0;
5465 struct ufs_vreg_info *info = &hba->vreg_info;
5466
5467 if (!info)
5468 goto out;
5469 else if (!info->vccq)
5470 goto out;
5471
5472 if (unused) {
5473 /* shut off the rail here */
5474 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5475 /*
5476 * Mark this rail as no longer used, so it doesn't get enabled
5477 * later by mistake
5478 */
5479 if (!ret)
5480 info->vccq->unused = true;
5481 } else {
5482 /*
5483 * rail should have been already enabled hence just make sure
5484 * that unused flag is cleared.
5485 */
5486 info->vccq->unused = false;
5487 }
5488out:
5489 return ret;
5490}
5491
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005492static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5493 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005494{
5495 int ret = 0;
5496 struct ufs_clk_info *clki;
5497 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005498 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005499
5500 if (!head || list_empty(head))
5501 goto out;
5502
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005503 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5504 if (ret)
5505 return ret;
5506
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005507 list_for_each_entry(clki, head, list) {
5508 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005509 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5510 continue;
5511
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005512 if (on && !clki->enabled) {
5513 ret = clk_prepare_enable(clki->clk);
5514 if (ret) {
5515 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5516 __func__, clki->name, ret);
5517 goto out;
5518 }
5519 } else if (!on && clki->enabled) {
5520 clk_disable_unprepare(clki->clk);
5521 }
5522 clki->enabled = on;
5523 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5524 clki->name, on ? "en" : "dis");
5525 }
5526 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005527
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005528 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5529 if (ret)
5530 return ret;
5531
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005532out:
5533 if (ret) {
5534 list_for_each_entry(clki, head, list) {
5535 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5536 clk_disable_unprepare(clki->clk);
5537 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005538 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005539 spin_lock_irqsave(hba->host->host_lock, flags);
5540 hba->clk_gating.state = CLKS_ON;
5541 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005542 }
5543 return ret;
5544}
5545
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005546static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5547{
5548 return __ufshcd_setup_clocks(hba, on, false);
5549}
5550
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005551static int ufshcd_init_clocks(struct ufs_hba *hba)
5552{
5553 int ret = 0;
5554 struct ufs_clk_info *clki;
5555 struct device *dev = hba->dev;
5556 struct list_head *head = &hba->clk_list_head;
5557
5558 if (!head || list_empty(head))
5559 goto out;
5560
5561 list_for_each_entry(clki, head, list) {
5562 if (!clki->name)
5563 continue;
5564
5565 clki->clk = devm_clk_get(dev, clki->name);
5566 if (IS_ERR(clki->clk)) {
5567 ret = PTR_ERR(clki->clk);
5568 dev_err(dev, "%s: %s clk get failed, %d\n",
5569 __func__, clki->name, ret);
5570 goto out;
5571 }
5572
5573 if (clki->max_freq) {
5574 ret = clk_set_rate(clki->clk, clki->max_freq);
5575 if (ret) {
5576 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5577 __func__, clki->name,
5578 clki->max_freq, ret);
5579 goto out;
5580 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005581 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005582 }
5583 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5584 clki->name, clk_get_rate(clki->clk));
5585 }
5586out:
5587 return ret;
5588}
5589
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005590static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5591{
5592 int err = 0;
5593
5594 if (!hba->vops)
5595 goto out;
5596
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005597 err = ufshcd_vops_init(hba);
5598 if (err)
5599 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005600
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005601 err = ufshcd_vops_setup_regulators(hba, true);
5602 if (err)
5603 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005604
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005605 goto out;
5606
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005607out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005608 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005609out:
5610 if (err)
5611 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005612 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005613 return err;
5614}
5615
5616static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5617{
5618 if (!hba->vops)
5619 return;
5620
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005621 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005622
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005623 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005624}
5625
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005626static int ufshcd_hba_init(struct ufs_hba *hba)
5627{
5628 int err;
5629
Raviv Shvili6a771a62014-09-25 15:32:24 +03005630 /*
5631 * Handle host controller power separately from the UFS device power
5632 * rails as it will help controlling the UFS host controller power
5633 * collapse easily which is different than UFS device power collapse.
5634 * Also, enable the host controller power before we go ahead with rest
5635 * of the initialization here.
5636 */
5637 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005638 if (err)
5639 goto out;
5640
Raviv Shvili6a771a62014-09-25 15:32:24 +03005641 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005642 if (err)
5643 goto out;
5644
Raviv Shvili6a771a62014-09-25 15:32:24 +03005645 err = ufshcd_init_clocks(hba);
5646 if (err)
5647 goto out_disable_hba_vreg;
5648
5649 err = ufshcd_setup_clocks(hba, true);
5650 if (err)
5651 goto out_disable_hba_vreg;
5652
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005653 err = ufshcd_init_vreg(hba);
5654 if (err)
5655 goto out_disable_clks;
5656
5657 err = ufshcd_setup_vreg(hba, true);
5658 if (err)
5659 goto out_disable_clks;
5660
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005661 err = ufshcd_variant_hba_init(hba);
5662 if (err)
5663 goto out_disable_vreg;
5664
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005665 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005666 goto out;
5667
5668out_disable_vreg:
5669 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005670out_disable_clks:
5671 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005672out_disable_hba_vreg:
5673 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005674out:
5675 return err;
5676}
5677
5678static void ufshcd_hba_exit(struct ufs_hba *hba)
5679{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005680 if (hba->is_powered) {
5681 ufshcd_variant_hba_exit(hba);
5682 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07005683 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005684 ufshcd_setup_clocks(hba, false);
5685 ufshcd_setup_hba_vreg(hba, false);
5686 hba->is_powered = false;
5687 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005688}
5689
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005690static int
5691ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305692{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005693 unsigned char cmd[6] = {REQUEST_SENSE,
5694 0,
5695 0,
5696 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005697 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005698 0};
5699 char *buffer;
5700 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305701
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005702 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005703 if (!buffer) {
5704 ret = -ENOMEM;
5705 goto out;
5706 }
5707
5708 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005709 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005710 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5711 if (ret)
5712 pr_err("%s: failed with err %d\n", __func__, ret);
5713
5714 kfree(buffer);
5715out:
5716 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305717}
5718
5719/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005720 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5721 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305722 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005723 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305724 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005725 * Returns 0 if requested power mode is set successfully
5726 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305727 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005728static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5729 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305730{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005731 unsigned char cmd[6] = { START_STOP };
5732 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005733 struct scsi_device *sdp;
5734 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005735 int ret;
5736
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005737 spin_lock_irqsave(hba->host->host_lock, flags);
5738 sdp = hba->sdev_ufs_device;
5739 if (sdp) {
5740 ret = scsi_device_get(sdp);
5741 if (!ret && !scsi_device_online(sdp)) {
5742 ret = -ENODEV;
5743 scsi_device_put(sdp);
5744 }
5745 } else {
5746 ret = -ENODEV;
5747 }
5748 spin_unlock_irqrestore(hba->host->host_lock, flags);
5749
5750 if (ret)
5751 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005752
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305753 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005754 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5755 * handling, which would wait for host to be resumed. Since we know
5756 * we are functional while we are here, skip host resume in error
5757 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305758 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005759 hba->host->eh_noresume = 1;
5760 if (hba->wlun_dev_clr_ua) {
5761 ret = ufshcd_send_request_sense(hba, sdp);
5762 if (ret)
5763 goto out;
5764 /* Unit attention condition is cleared now */
5765 hba->wlun_dev_clr_ua = false;
5766 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305767
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005768 cmd[4] = pwr_mode << 4;
5769
5770 /*
5771 * Current function would be generally called from the power management
5772 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5773 * already suspended childs.
5774 */
5775 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5776 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5777 if (ret) {
5778 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005779 "START_STOP failed for power mode: %d, result %x\n",
5780 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005781 if (driver_byte(ret) & DRIVER_SENSE)
5782 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005783 }
5784
5785 if (!ret)
5786 hba->curr_dev_pwr_mode = pwr_mode;
5787out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005788 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005789 hba->host->eh_noresume = 0;
5790 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305791}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305792
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005793static int ufshcd_link_state_transition(struct ufs_hba *hba,
5794 enum uic_link_state req_link_state,
5795 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305796{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005797 int ret = 0;
5798
5799 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305800 return 0;
5801
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005802 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5803 ret = ufshcd_uic_hibern8_enter(hba);
5804 if (!ret)
5805 ufshcd_set_link_hibern8(hba);
5806 else
5807 goto out;
5808 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305809 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005810 * If autobkops is enabled, link can't be turned off because
5811 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305812 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005813 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5814 (!check_for_bkops || (check_for_bkops &&
5815 !hba->auto_bkops_enabled))) {
5816 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005817 * Let's make sure that link is in low power mode, we are doing
5818 * this currently by putting the link in Hibern8. Otherway to
5819 * put the link in low power mode is to send the DME end point
5820 * to device and then send the DME reset command to local
5821 * unipro. But putting the link in hibern8 is much faster.
5822 */
5823 ret = ufshcd_uic_hibern8_enter(hba);
5824 if (ret)
5825 goto out;
5826 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005827 * Change controller state to "reset state" which
5828 * should also put the link in off/reset state
5829 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005830 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005831 /*
5832 * TODO: Check if we need any delay to make sure that
5833 * controller is reset
5834 */
5835 ufshcd_set_link_off(hba);
5836 }
5837
5838out:
5839 return ret;
5840}
5841
5842static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5843{
5844 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02005845 * It seems some UFS devices may keep drawing more than sleep current
5846 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
5847 * To avoid this situation, add 2ms delay before putting these UFS
5848 * rails in LPM mode.
5849 */
5850 if (!ufshcd_is_link_active(hba) &&
5851 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
5852 usleep_range(2000, 2100);
5853
5854 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005855 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5856 * power.
5857 *
5858 * If UFS device and link is in OFF state, all power supplies (VCC,
5859 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5860 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5861 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5862 *
5863 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5864 * in low power state which would save some power.
5865 */
5866 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5867 !hba->dev_info.is_lu_power_on_wp) {
5868 ufshcd_setup_vreg(hba, false);
5869 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5870 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5871 if (!ufshcd_is_link_active(hba)) {
5872 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5873 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5874 }
5875 }
5876}
5877
5878static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5879{
5880 int ret = 0;
5881
5882 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5883 !hba->dev_info.is_lu_power_on_wp) {
5884 ret = ufshcd_setup_vreg(hba, true);
5885 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005886 if (!ret && !ufshcd_is_link_active(hba)) {
5887 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5888 if (ret)
5889 goto vcc_disable;
5890 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5891 if (ret)
5892 goto vccq_lpm;
5893 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07005894 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005895 }
5896 goto out;
5897
5898vccq_lpm:
5899 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5900vcc_disable:
5901 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5902out:
5903 return ret;
5904}
5905
5906static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5907{
5908 if (ufshcd_is_link_off(hba))
5909 ufshcd_setup_hba_vreg(hba, false);
5910}
5911
5912static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5913{
5914 if (ufshcd_is_link_off(hba))
5915 ufshcd_setup_hba_vreg(hba, true);
5916}
5917
5918/**
5919 * ufshcd_suspend - helper function for suspend operations
5920 * @hba: per adapter instance
5921 * @pm_op: desired low power operation type
5922 *
5923 * This function will try to put the UFS device and link into low power
5924 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5925 * (System PM level).
5926 *
5927 * If this function is called during shutdown, it will make sure that
5928 * both UFS device and UFS link is powered off.
5929 *
5930 * NOTE: UFS device & link must be active before we enter in this function.
5931 *
5932 * Returns 0 for success and non-zero for failure
5933 */
5934static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5935{
5936 int ret = 0;
5937 enum ufs_pm_level pm_lvl;
5938 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5939 enum uic_link_state req_link_state;
5940
5941 hba->pm_op_in_progress = 1;
5942 if (!ufshcd_is_shutdown_pm(pm_op)) {
5943 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5944 hba->rpm_lvl : hba->spm_lvl;
5945 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5946 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5947 } else {
5948 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5949 req_link_state = UIC_LINK_OFF_STATE;
5950 }
5951
5952 /*
5953 * If we can't transition into any of the low power modes
5954 * just gate the clocks.
5955 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005956 ufshcd_hold(hba, false);
5957 hba->clk_gating.is_suspended = true;
5958
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005959 ufshcd_suspend_clkscaling(hba);
5960
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005961 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5962 req_link_state == UIC_LINK_ACTIVE_STATE) {
5963 goto disable_clks;
5964 }
5965
5966 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5967 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005968 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005969
5970 /* UFS device & link must be active before we enter in this function */
5971 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5972 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005973 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005974 }
5975
5976 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005977 if (ufshcd_can_autobkops_during_suspend(hba)) {
5978 /*
5979 * The device is idle with no requests in the queue,
5980 * allow background operations if bkops status shows
5981 * that performance might be impacted.
5982 */
5983 ret = ufshcd_urgent_bkops(hba);
5984 if (ret)
5985 goto enable_gating;
5986 } else {
5987 /* make sure that auto bkops is disabled */
5988 ufshcd_disable_auto_bkops(hba);
5989 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005990 }
5991
5992 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5993 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5994 !ufshcd_is_runtime_pm(pm_op))) {
5995 /* ensure that bkops is disabled */
5996 ufshcd_disable_auto_bkops(hba);
5997 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5998 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005999 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006000 }
6001
6002 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6003 if (ret)
6004 goto set_dev_active;
6005
6006 ufshcd_vreg_set_lpm(hba);
6007
6008disable_clks:
6009 /*
6010 * Call vendor specific suspend callback. As these callbacks may access
6011 * vendor specific host controller register space call them before the
6012 * host clocks are ON.
6013 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006014 ret = ufshcd_vops_suspend(hba, pm_op);
6015 if (ret)
6016 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006017
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006018 if (!ufshcd_is_link_active(hba))
6019 ufshcd_setup_clocks(hba, false);
6020 else
6021 /* If link is active, device ref_clk can't be switched off */
6022 __ufshcd_setup_clocks(hba, false, true);
6023
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006024 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006025 /*
6026 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006027 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006028 */
6029 ufshcd_disable_irq(hba);
6030 /* Put the host controller in low power mode if possible */
6031 ufshcd_hba_vreg_set_lpm(hba);
6032 goto out;
6033
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006034set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07006035 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006036 ufshcd_vreg_set_hpm(hba);
6037 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6038 ufshcd_set_link_active(hba);
6039 else if (ufshcd_is_link_off(hba))
6040 ufshcd_host_reset_and_restore(hba);
6041set_dev_active:
6042 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6043 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006044enable_gating:
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006045 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006046 hba->clk_gating.is_suspended = false;
6047 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006048out:
6049 hba->pm_op_in_progress = 0;
6050 return ret;
6051}
6052
6053/**
6054 * ufshcd_resume - helper function for resume operations
6055 * @hba: per adapter instance
6056 * @pm_op: runtime PM or system PM
6057 *
6058 * This function basically brings the UFS device, UniPro link and controller
6059 * to active state.
6060 *
6061 * Returns 0 for success and non-zero for failure
6062 */
6063static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6064{
6065 int ret;
6066 enum uic_link_state old_link_state;
6067
6068 hba->pm_op_in_progress = 1;
6069 old_link_state = hba->uic_link_state;
6070
6071 ufshcd_hba_vreg_set_hpm(hba);
6072 /* Make sure clocks are enabled before accessing controller */
6073 ret = ufshcd_setup_clocks(hba, true);
6074 if (ret)
6075 goto out;
6076
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006077 /* enable the host irq as host controller would be active soon */
6078 ret = ufshcd_enable_irq(hba);
6079 if (ret)
6080 goto disable_irq_and_vops_clks;
6081
6082 ret = ufshcd_vreg_set_hpm(hba);
6083 if (ret)
6084 goto disable_irq_and_vops_clks;
6085
6086 /*
6087 * Call vendor specific resume callback. As these callbacks may access
6088 * vendor specific host controller register space call them when the
6089 * host clocks are ON.
6090 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006091 ret = ufshcd_vops_resume(hba, pm_op);
6092 if (ret)
6093 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006094
6095 if (ufshcd_is_link_hibern8(hba)) {
6096 ret = ufshcd_uic_hibern8_exit(hba);
6097 if (!ret)
6098 ufshcd_set_link_active(hba);
6099 else
6100 goto vendor_suspend;
6101 } else if (ufshcd_is_link_off(hba)) {
6102 ret = ufshcd_host_reset_and_restore(hba);
6103 /*
6104 * ufshcd_host_reset_and_restore() should have already
6105 * set the link state as active
6106 */
6107 if (ret || !ufshcd_is_link_active(hba))
6108 goto vendor_suspend;
6109 }
6110
6111 if (!ufshcd_is_ufs_dev_active(hba)) {
6112 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6113 if (ret)
6114 goto set_old_link_state;
6115 }
6116
Subhash Jadavani374a2462014-09-25 15:32:35 +03006117 /*
6118 * If BKOPs operations are urgently needed at this moment then
6119 * keep auto-bkops enabled or else disable it.
6120 */
6121 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006122 hba->clk_gating.is_suspended = false;
6123
Gilad Bronera5082532016-10-17 17:10:00 -07006124 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006125
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006126 /* Schedule clock gating in case of no access to UFS device yet */
6127 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006128 goto out;
6129
6130set_old_link_state:
6131 ufshcd_link_state_transition(hba, old_link_state, 0);
6132vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006133 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006134disable_vreg:
6135 ufshcd_vreg_set_lpm(hba);
6136disable_irq_and_vops_clks:
6137 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006138 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006139 ufshcd_setup_clocks(hba, false);
6140out:
6141 hba->pm_op_in_progress = 0;
6142 return ret;
6143}
6144
6145/**
6146 * ufshcd_system_suspend - system suspend routine
6147 * @hba: per adapter instance
6148 * @pm_op: runtime PM or system PM
6149 *
6150 * Check the description of ufshcd_suspend() function for more details.
6151 *
6152 * Returns 0 for success and non-zero for failure
6153 */
6154int ufshcd_system_suspend(struct ufs_hba *hba)
6155{
6156 int ret = 0;
6157
6158 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006159 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006160
6161 if (pm_runtime_suspended(hba->dev)) {
6162 if (hba->rpm_lvl == hba->spm_lvl)
6163 /*
6164 * There is possibility that device may still be in
6165 * active state during the runtime suspend.
6166 */
6167 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6168 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6169 goto out;
6170
6171 /*
6172 * UFS device and/or UFS link low power states during runtime
6173 * suspend seems to be different than what is expected during
6174 * system suspend. Hence runtime resume the devic & link and
6175 * let the system suspend low power states to take effect.
6176 * TODO: If resume takes longer time, we might have optimize
6177 * it in future by not resuming everything if possible.
6178 */
6179 ret = ufshcd_runtime_resume(hba);
6180 if (ret)
6181 goto out;
6182 }
6183
6184 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6185out:
Dolev Ravive7850602014-09-25 15:32:36 +03006186 if (!ret)
6187 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006188 return ret;
6189}
6190EXPORT_SYMBOL(ufshcd_system_suspend);
6191
6192/**
6193 * ufshcd_system_resume - system resume routine
6194 * @hba: per adapter instance
6195 *
6196 * Returns 0 for success and non-zero for failure
6197 */
6198
6199int ufshcd_system_resume(struct ufs_hba *hba)
6200{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006201 if (!hba)
6202 return -EINVAL;
6203
6204 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006205 /*
6206 * Let the runtime resume take care of resuming
6207 * if runtime suspended.
6208 */
6209 return 0;
6210
6211 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6212}
6213EXPORT_SYMBOL(ufshcd_system_resume);
6214
6215/**
6216 * ufshcd_runtime_suspend - runtime suspend routine
6217 * @hba: per adapter instance
6218 *
6219 * Check the description of ufshcd_suspend() function for more details.
6220 *
6221 * Returns 0 for success and non-zero for failure
6222 */
6223int ufshcd_runtime_suspend(struct ufs_hba *hba)
6224{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006225 if (!hba)
6226 return -EINVAL;
6227
6228 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006229 return 0;
6230
6231 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306232}
6233EXPORT_SYMBOL(ufshcd_runtime_suspend);
6234
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006235/**
6236 * ufshcd_runtime_resume - runtime resume routine
6237 * @hba: per adapter instance
6238 *
6239 * This function basically brings the UFS device, UniPro link and controller
6240 * to active state. Following operations are done in this function:
6241 *
6242 * 1. Turn on all the controller related clocks
6243 * 2. Bring the UniPro link out of Hibernate state
6244 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6245 * to active state.
6246 * 4. If auto-bkops is enabled on the device, disable it.
6247 *
6248 * So following would be the possible power state after this function return
6249 * successfully:
6250 * S1: UFS device in Active state with VCC rail ON
6251 * UniPro link in Active state
6252 * All the UFS/UniPro controller clocks are ON
6253 *
6254 * Returns 0 for success and non-zero for failure
6255 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306256int ufshcd_runtime_resume(struct ufs_hba *hba)
6257{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006258 if (!hba)
6259 return -EINVAL;
6260
6261 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306262 return 0;
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006263
6264 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306265}
6266EXPORT_SYMBOL(ufshcd_runtime_resume);
6267
6268int ufshcd_runtime_idle(struct ufs_hba *hba)
6269{
6270 return 0;
6271}
6272EXPORT_SYMBOL(ufshcd_runtime_idle);
6273
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306274/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006275 * ufshcd_shutdown - shutdown routine
6276 * @hba: per adapter instance
6277 *
6278 * This function would power off both UFS device and UFS link.
6279 *
6280 * Returns 0 always to allow force shutdown even in case of errors.
6281 */
6282int ufshcd_shutdown(struct ufs_hba *hba)
6283{
6284 int ret = 0;
6285
6286 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6287 goto out;
6288
6289 if (pm_runtime_suspended(hba->dev)) {
6290 ret = ufshcd_runtime_resume(hba);
6291 if (ret)
6292 goto out;
6293 }
6294
6295 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6296out:
6297 if (ret)
6298 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6299 /* allow force shutdown even in case of errors */
6300 return 0;
6301}
6302EXPORT_SYMBOL(ufshcd_shutdown);
6303
6304/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306305 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306306 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306307 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306308 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306309void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306310{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306311 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306312 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306313 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006314 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306315
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006316 ufshcd_exit_clk_gating(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006317 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306318}
6319EXPORT_SYMBOL_GPL(ufshcd_remove);
6320
6321/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006322 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6323 * @hba: pointer to Host Bus Adapter (HBA)
6324 */
6325void ufshcd_dealloc_host(struct ufs_hba *hba)
6326{
6327 scsi_host_put(hba->host);
6328}
6329EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6330
6331/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006332 * ufshcd_set_dma_mask - Set dma mask based on the controller
6333 * addressing capability
6334 * @hba: per adapter instance
6335 *
6336 * Returns 0 for success, non-zero for failure
6337 */
6338static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6339{
6340 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6341 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6342 return 0;
6343 }
6344 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6345}
6346
6347/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006348 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306349 * @dev: pointer to device handle
6350 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306351 * Returns 0 on success, non-zero value on failure
6352 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006353int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306354{
6355 struct Scsi_Host *host;
6356 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006357 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306358
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306359 if (!dev) {
6360 dev_err(dev,
6361 "Invalid memory reference for dev is NULL\n");
6362 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306363 goto out_error;
6364 }
6365
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306366 host = scsi_host_alloc(&ufshcd_driver_template,
6367 sizeof(struct ufs_hba));
6368 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306369 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306370 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306371 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306372 }
6373 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306374 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306375 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006376 *hba_handle = hba;
6377
6378out_error:
6379 return err;
6380}
6381EXPORT_SYMBOL(ufshcd_alloc_host);
6382
Sahitya Tummala856b3482014-09-25 15:32:34 +03006383static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6384{
6385 int ret = 0;
6386 struct ufs_clk_info *clki;
6387 struct list_head *head = &hba->clk_list_head;
6388
6389 if (!head || list_empty(head))
6390 goto out;
6391
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006392 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6393 if (ret)
6394 return ret;
6395
Sahitya Tummala856b3482014-09-25 15:32:34 +03006396 list_for_each_entry(clki, head, list) {
6397 if (!IS_ERR_OR_NULL(clki->clk)) {
6398 if (scale_up && clki->max_freq) {
6399 if (clki->curr_freq == clki->max_freq)
6400 continue;
6401 ret = clk_set_rate(clki->clk, clki->max_freq);
6402 if (ret) {
6403 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6404 __func__, clki->name,
6405 clki->max_freq, ret);
6406 break;
6407 }
6408 clki->curr_freq = clki->max_freq;
6409
6410 } else if (!scale_up && clki->min_freq) {
6411 if (clki->curr_freq == clki->min_freq)
6412 continue;
6413 ret = clk_set_rate(clki->clk, clki->min_freq);
6414 if (ret) {
6415 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6416 __func__, clki->name,
6417 clki->min_freq, ret);
6418 break;
6419 }
6420 clki->curr_freq = clki->min_freq;
6421 }
6422 }
6423 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6424 clki->name, clk_get_rate(clki->clk));
6425 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006426
6427 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6428
Sahitya Tummala856b3482014-09-25 15:32:34 +03006429out:
6430 return ret;
6431}
6432
6433static int ufshcd_devfreq_target(struct device *dev,
6434 unsigned long *freq, u32 flags)
6435{
6436 int err = 0;
6437 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006438 bool release_clk_hold = false;
6439 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006440
6441 if (!ufshcd_is_clkscaling_enabled(hba))
6442 return -EINVAL;
6443
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006444 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6445 if (ufshcd_eh_in_progress(hba)) {
6446 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6447 return 0;
6448 }
6449
6450 if (ufshcd_is_clkgating_allowed(hba) &&
6451 (hba->clk_gating.state != CLKS_ON)) {
6452 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6453 /* hold the vote until the scaling work is completed */
6454 hba->clk_gating.active_reqs++;
6455 release_clk_hold = true;
6456 hba->clk_gating.state = CLKS_ON;
6457 } else {
6458 /*
6459 * Clock gating work seems to be running in parallel
6460 * hence skip scaling work to avoid deadlock between
6461 * current scaling work and gating work.
6462 */
6463 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6464 return 0;
6465 }
6466 }
6467 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6468
Sahitya Tummala856b3482014-09-25 15:32:34 +03006469 if (*freq == UINT_MAX)
6470 err = ufshcd_scale_clks(hba, true);
6471 else if (*freq == 0)
6472 err = ufshcd_scale_clks(hba, false);
6473
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006474 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6475 if (release_clk_hold)
6476 __ufshcd_release(hba);
6477 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6478
Sahitya Tummala856b3482014-09-25 15:32:34 +03006479 return err;
6480}
6481
6482static int ufshcd_devfreq_get_dev_status(struct device *dev,
6483 struct devfreq_dev_status *stat)
6484{
6485 struct ufs_hba *hba = dev_get_drvdata(dev);
6486 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6487 unsigned long flags;
6488
6489 if (!ufshcd_is_clkscaling_enabled(hba))
6490 return -EINVAL;
6491
6492 memset(stat, 0, sizeof(*stat));
6493
6494 spin_lock_irqsave(hba->host->host_lock, flags);
6495 if (!scaling->window_start_t)
6496 goto start_window;
6497
6498 if (scaling->is_busy_started)
6499 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6500 scaling->busy_start_t));
6501
6502 stat->total_time = jiffies_to_usecs((long)jiffies -
6503 (long)scaling->window_start_t);
6504 stat->busy_time = scaling->tot_busy_t;
6505start_window:
6506 scaling->window_start_t = jiffies;
6507 scaling->tot_busy_t = 0;
6508
6509 if (hba->outstanding_reqs) {
6510 scaling->busy_start_t = ktime_get();
6511 scaling->is_busy_started = true;
6512 } else {
6513 scaling->busy_start_t = ktime_set(0, 0);
6514 scaling->is_busy_started = false;
6515 }
6516 spin_unlock_irqrestore(hba->host->host_lock, flags);
6517 return 0;
6518}
6519
6520static struct devfreq_dev_profile ufs_devfreq_profile = {
6521 .polling_ms = 100,
6522 .target = ufshcd_devfreq_target,
6523 .get_dev_status = ufshcd_devfreq_get_dev_status,
6524};
6525
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006526/**
6527 * ufshcd_init - Driver initialization routine
6528 * @hba: per-adapter instance
6529 * @mmio_base: base register address
6530 * @irq: Interrupt line of device
6531 * Returns 0 on success, non-zero value on failure
6532 */
6533int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6534{
6535 int err;
6536 struct Scsi_Host *host = hba->host;
6537 struct device *dev = hba->dev;
6538
6539 if (!mmio_base) {
6540 dev_err(hba->dev,
6541 "Invalid memory reference for mmio_base is NULL\n");
6542 err = -ENODEV;
6543 goto out_error;
6544 }
6545
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306546 hba->mmio_base = mmio_base;
6547 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306548
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006549 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006550 if (err)
6551 goto out_error;
6552
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306553 /* Read capabilities registers */
6554 ufshcd_hba_capabilities(hba);
6555
6556 /* Get UFS version supported by the controller */
6557 hba->ufs_version = ufshcd_get_ufs_version(hba);
6558
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306559 /* Get Interrupt bit mask per version */
6560 hba->intr_mask = ufshcd_get_intr_mask(hba);
6561
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006562 err = ufshcd_set_dma_mask(hba);
6563 if (err) {
6564 dev_err(hba->dev, "set dma mask failed\n");
6565 goto out_disable;
6566 }
6567
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306568 /* Allocate memory for host memory space */
6569 err = ufshcd_memory_alloc(hba);
6570 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306571 dev_err(hba->dev, "Memory allocation failed\n");
6572 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306573 }
6574
6575 /* Configure LRB */
6576 ufshcd_host_memory_configure(hba);
6577
6578 host->can_queue = hba->nutrs;
6579 host->cmd_per_lun = hba->nutrs;
6580 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006581 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306582 host->max_channel = UFSHCD_MAX_CHANNEL;
6583 host->unique_id = host->host_no;
6584 host->max_cmd_len = MAX_CDB_SIZE;
6585
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006586 hba->max_pwr_info.is_valid = false;
6587
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306588 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306589 init_waitqueue_head(&hba->tm_wq);
6590 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306591
6592 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306593 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306594 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306595
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306596 /* Initialize UIC command mutex */
6597 mutex_init(&hba->uic_cmd_mutex);
6598
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306599 /* Initialize mutex for device management commands */
6600 mutex_init(&hba->dev_cmd.lock);
6601
6602 /* Initialize device management tag acquire wait queue */
6603 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6604
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006605 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006606
6607 /*
6608 * In order to avoid any spurious interrupt immediately after
6609 * registering UFS controller interrupt handler, clear any pending UFS
6610 * interrupt status and disable all the UFS interrupts.
6611 */
6612 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6613 REG_INTERRUPT_STATUS);
6614 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6615 /*
6616 * Make sure that UFS interrupts are disabled and any pending interrupt
6617 * status is cleared before registering UFS interrupt handler.
6618 */
6619 mb();
6620
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306621 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006622 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306623 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306624 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006625 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006626 } else {
6627 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306628 }
6629
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306630 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306631 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306632 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006633 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306634 }
6635
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306636 /* Host controller enable */
6637 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306638 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306639 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306640 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306641 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306642
Sahitya Tummala856b3482014-09-25 15:32:34 +03006643 if (ufshcd_is_clkscaling_enabled(hba)) {
Chanwoo Choi4861ee12016-11-08 18:13:28 +09006644 hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile,
Sahitya Tummala856b3482014-09-25 15:32:34 +03006645 "simple_ondemand", NULL);
6646 if (IS_ERR(hba->devfreq)) {
6647 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6648 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006649 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006650 goto out_remove_scsi_host;
6651 }
6652 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07006653 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006654 }
6655
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306656 /* Hold auto suspend until async scan completes */
6657 pm_runtime_get_sync(dev);
6658
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006659 /*
6660 * The device-initialize-sequence hasn't been invoked yet.
6661 * Set the device to power-off state
6662 */
6663 ufshcd_set_ufs_dev_poweroff(hba);
6664
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306665 async_schedule(ufshcd_async_scan, hba);
6666
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306667 return 0;
6668
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306669out_remove_scsi_host:
6670 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006671exit_gating:
6672 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306673out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006674 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006675 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306676out_error:
6677 return err;
6678}
6679EXPORT_SYMBOL_GPL(ufshcd_init);
6680
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306681MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6682MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306683MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306684MODULE_LICENSE("GPL");
6685MODULE_VERSION(UFSHCD_DRIVER_VERSION);