blob: 421488161cf3317f95fd1de7e40cb08adddb6772 [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 */
1132static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1133{
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) {
1146 lrbp->utr_descriptor_ptr->prd_table_length =
1147 cpu_to_le16((u16) (sg_segments));
1148
1149 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1150
1151 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1152 prd_table[i].size =
1153 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1154 prd_table[i].base_addr =
1155 cpu_to_le32(lower_32_bits(sg->dma_address));
1156 prd_table[i].upper_addr =
1157 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001158 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301159 }
1160 } else {
1161 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1162 }
1163
1164 return 0;
1165}
1166
1167/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301168 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301169 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301170 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301171 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301172static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301173{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301174 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1175
1176 if (hba->ufs_version == UFSHCI_VERSION_10) {
1177 u32 rw;
1178 rw = set & INTERRUPT_MASK_RW_VER_10;
1179 set = rw | ((set ^ intrs) & intrs);
1180 } else {
1181 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301182 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301183
1184 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1185}
1186
1187/**
1188 * ufshcd_disable_intr - disable interrupts
1189 * @hba: per adapter instance
1190 * @intrs: interrupt bits
1191 */
1192static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1193{
1194 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1195
1196 if (hba->ufs_version == UFSHCI_VERSION_10) {
1197 u32 rw;
1198 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1199 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1200 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1201
1202 } else {
1203 set &= ~intrs;
1204 }
1205
1206 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301207}
1208
1209/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301210 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1211 * descriptor according to request
1212 * @lrbp: pointer to local reference block
1213 * @upiu_flags: flags required in the header
1214 * @cmd_dir: requests data direction
1215 */
1216static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001217 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301218{
1219 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1220 u32 data_direction;
1221 u32 dword_0;
1222
1223 if (cmd_dir == DMA_FROM_DEVICE) {
1224 data_direction = UTP_DEVICE_TO_HOST;
1225 *upiu_flags = UPIU_CMD_FLAGS_READ;
1226 } else if (cmd_dir == DMA_TO_DEVICE) {
1227 data_direction = UTP_HOST_TO_DEVICE;
1228 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1229 } else {
1230 data_direction = UTP_NO_DATA_TRANSFER;
1231 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1232 }
1233
1234 dword_0 = data_direction | (lrbp->command_type
1235 << UPIU_COMMAND_TYPE_OFFSET);
1236 if (lrbp->intr_cmd)
1237 dword_0 |= UTP_REQ_DESC_INT_CMD;
1238
1239 /* Transfer request descriptor header fields */
1240 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001241 /* dword_1 is reserved, hence it is set to 0 */
1242 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301243 /*
1244 * assigning invalid value for command status. Controller
1245 * updates OCS on command completion, with the command
1246 * status
1247 */
1248 req_desc->header.dword_2 =
1249 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001250 /* dword_3 is reserved, hence it is set to 0 */
1251 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001252
1253 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301254}
1255
1256/**
1257 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1258 * for scsi commands
1259 * @lrbp - local reference block pointer
1260 * @upiu_flags - flags
1261 */
1262static
1263void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1264{
1265 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001266 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301267
1268 /* command descriptor fields */
1269 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1270 UPIU_TRANSACTION_COMMAND, upiu_flags,
1271 lrbp->lun, lrbp->task_tag);
1272 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1273 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1274
1275 /* Total EHS length and Data segment length will be zero */
1276 ucd_req_ptr->header.dword_2 = 0;
1277
1278 ucd_req_ptr->sc.exp_data_transfer_len =
1279 cpu_to_be32(lrbp->cmd->sdb.length);
1280
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001281 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1282 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1283 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1284
1285 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301286}
1287
Dolev Raviv68078d52013-07-30 00:35:58 +05301288/**
1289 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1290 * for query requsts
1291 * @hba: UFS hba
1292 * @lrbp: local reference block pointer
1293 * @upiu_flags: flags
1294 */
1295static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1296 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1297{
1298 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1299 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301300 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301301 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1302
1303 /* Query request header */
1304 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1305 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1306 lrbp->lun, lrbp->task_tag);
1307 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1308 0, query->request.query_func, 0, 0);
1309
Zang Leigang68612852016-08-25 17:39:19 +08001310 /* Data segment length only need for WRITE_DESC */
1311 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1312 ucd_req_ptr->header.dword_2 =
1313 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1314 else
1315 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301316
1317 /* Copy the Query Request buffer as is */
1318 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1319 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301320
1321 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001322 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1323 memcpy(descp, query->descriptor, len);
1324
Yaniv Gardi51047262016-02-01 15:02:38 +02001325 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301326}
1327
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301328static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1329{
1330 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1331
1332 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1333
1334 /* command descriptor fields */
1335 ucd_req_ptr->header.dword_0 =
1336 UPIU_HEADER_DWORD(
1337 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001338 /* clear rest of the fields of basic header */
1339 ucd_req_ptr->header.dword_1 = 0;
1340 ucd_req_ptr->header.dword_2 = 0;
1341
1342 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301343}
1344
1345/**
Joao Pinto300bb132016-05-11 12:21:27 +01001346 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1347 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301348 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301349 * @lrb - pointer to local reference block
1350 */
Joao Pinto300bb132016-05-11 12:21:27 +01001351static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301352{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301353 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301354 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301355
Joao Pinto300bb132016-05-11 12:21:27 +01001356 if (hba->ufs_version == UFSHCI_VERSION_20)
1357 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1358 else
1359 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1360
1361 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1362 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1363 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1364 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1365 ufshcd_prepare_utp_nop_upiu(lrbp);
1366 else
1367 ret = -EINVAL;
1368
1369 return ret;
1370}
1371
1372/**
1373 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1374 * for SCSI Purposes
1375 * @hba - per adapter instance
1376 * @lrb - pointer to local reference block
1377 */
1378static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1379{
1380 u32 upiu_flags;
1381 int ret = 0;
1382
1383 if (hba->ufs_version == UFSHCI_VERSION_20)
1384 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1385 else
1386 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1387
1388 if (likely(lrbp->cmd)) {
1389 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1390 lrbp->cmd->sc_data_direction);
1391 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1392 } else {
1393 ret = -EINVAL;
1394 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301395
1396 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301397}
1398
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001399/*
1400 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1401 * @scsi_lun: scsi LUN id
1402 *
1403 * Returns UPIU LUN id
1404 */
1405static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1406{
1407 if (scsi_is_wlun(scsi_lun))
1408 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1409 | UFS_UPIU_WLUN_ID;
1410 else
1411 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1412}
1413
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301414/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001415 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1416 * @scsi_lun: UPIU W-LUN id
1417 *
1418 * Returns SCSI W-LUN id
1419 */
1420static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1421{
1422 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1423}
1424
1425/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301426 * ufshcd_queuecommand - main entry point for SCSI requests
1427 * @cmd: command from SCSI Midlayer
1428 * @done: call back function
1429 *
1430 * Returns 0 for success, non-zero in case of failure
1431 */
1432static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1433{
1434 struct ufshcd_lrb *lrbp;
1435 struct ufs_hba *hba;
1436 unsigned long flags;
1437 int tag;
1438 int err = 0;
1439
1440 hba = shost_priv(host);
1441
1442 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001443 if (!ufshcd_valid_tag(hba, tag)) {
1444 dev_err(hba->dev,
1445 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1446 __func__, tag, cmd, cmd->request);
1447 BUG();
1448 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301449
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301450 spin_lock_irqsave(hba->host->host_lock, flags);
1451 switch (hba->ufshcd_state) {
1452 case UFSHCD_STATE_OPERATIONAL:
1453 break;
Zang Leigang141f8162016-11-16 11:29:37 +08001454 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301455 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301456 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301457 goto out_unlock;
1458 case UFSHCD_STATE_ERROR:
1459 set_host_byte(cmd, DID_ERROR);
1460 cmd->scsi_done(cmd);
1461 goto out_unlock;
1462 default:
1463 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1464 __func__, hba->ufshcd_state);
1465 set_host_byte(cmd, DID_BAD_TARGET);
1466 cmd->scsi_done(cmd);
1467 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301468 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001469
1470 /* if error handling is in progress, don't issue commands */
1471 if (ufshcd_eh_in_progress(hba)) {
1472 set_host_byte(cmd, DID_ERROR);
1473 cmd->scsi_done(cmd);
1474 goto out_unlock;
1475 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301476 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301477
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301478 /* acquire the tag to make sure device cmds don't use it */
1479 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1480 /*
1481 * Dev manage command in progress, requeue the command.
1482 * Requeuing the command helps in cases where the request *may*
1483 * find different tag instead of waiting for dev manage command
1484 * completion.
1485 */
1486 err = SCSI_MLQUEUE_HOST_BUSY;
1487 goto out;
1488 }
1489
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001490 err = ufshcd_hold(hba, true);
1491 if (err) {
1492 err = SCSI_MLQUEUE_HOST_BUSY;
1493 clear_bit_unlock(tag, &hba->lrb_in_use);
1494 goto out;
1495 }
1496 WARN_ON(hba->clk_gating.state != CLKS_ON);
1497
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301498 lrbp = &hba->lrb[tag];
1499
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301500 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301501 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001502 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301503 lrbp->sense_buffer = cmd->sense_buffer;
1504 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001505 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001506 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301507
Joao Pinto300bb132016-05-11 12:21:27 +01001508 ufshcd_comp_scsi_upiu(hba, lrbp);
1509
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301510 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301511 if (err) {
1512 lrbp->cmd = NULL;
1513 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301514 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301515 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001516 /* Make sure descriptors are ready before ringing the doorbell */
1517 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301518
1519 /* issue command to the controller */
1520 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001521 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301522 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301523out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301524 spin_unlock_irqrestore(hba->host->host_lock, flags);
1525out:
1526 return err;
1527}
1528
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301529static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1530 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1531{
1532 lrbp->cmd = NULL;
1533 lrbp->sense_bufflen = 0;
1534 lrbp->sense_buffer = NULL;
1535 lrbp->task_tag = tag;
1536 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301537 lrbp->intr_cmd = true; /* No interrupt aggregation */
1538 hba->dev_cmd.type = cmd_type;
1539
Joao Pinto300bb132016-05-11 12:21:27 +01001540 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301541}
1542
1543static int
1544ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1545{
1546 int err = 0;
1547 unsigned long flags;
1548 u32 mask = 1 << tag;
1549
1550 /* clear outstanding transaction before retry */
1551 spin_lock_irqsave(hba->host->host_lock, flags);
1552 ufshcd_utrl_clear(hba, tag);
1553 spin_unlock_irqrestore(hba->host->host_lock, flags);
1554
1555 /*
1556 * wait for for h/w to clear corresponding bit in door-bell.
1557 * max. wait is 1 sec.
1558 */
1559 err = ufshcd_wait_for_register(hba,
1560 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001561 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301562
1563 return err;
1564}
1565
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001566static int
1567ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1568{
1569 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1570
1571 /* Get the UPIU response */
1572 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1573 UPIU_RSP_CODE_OFFSET;
1574 return query_res->response;
1575}
1576
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301577/**
1578 * ufshcd_dev_cmd_completion() - handles device management command responses
1579 * @hba: per adapter instance
1580 * @lrbp: pointer to local reference block
1581 */
1582static int
1583ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1584{
1585 int resp;
1586 int err = 0;
1587
1588 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1589
1590 switch (resp) {
1591 case UPIU_TRANSACTION_NOP_IN:
1592 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1593 err = -EINVAL;
1594 dev_err(hba->dev, "%s: unexpected response %x\n",
1595 __func__, resp);
1596 }
1597 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301598 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001599 err = ufshcd_check_query_response(hba, lrbp);
1600 if (!err)
1601 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301602 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301603 case UPIU_TRANSACTION_REJECT_UPIU:
1604 /* TODO: handle Reject UPIU Response */
1605 err = -EPERM;
1606 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1607 __func__);
1608 break;
1609 default:
1610 err = -EINVAL;
1611 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1612 __func__, resp);
1613 break;
1614 }
1615
1616 return err;
1617}
1618
1619static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1620 struct ufshcd_lrb *lrbp, int max_timeout)
1621{
1622 int err = 0;
1623 unsigned long time_left;
1624 unsigned long flags;
1625
1626 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1627 msecs_to_jiffies(max_timeout));
1628
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001629 /* Make sure descriptors are ready before ringing the doorbell */
1630 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301631 spin_lock_irqsave(hba->host->host_lock, flags);
1632 hba->dev_cmd.complete = NULL;
1633 if (likely(time_left)) {
1634 err = ufshcd_get_tr_ocs(lrbp);
1635 if (!err)
1636 err = ufshcd_dev_cmd_completion(hba, lrbp);
1637 }
1638 spin_unlock_irqrestore(hba->host->host_lock, flags);
1639
1640 if (!time_left) {
1641 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001642 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1643 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301644 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001645 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301646 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001647 /*
1648 * in case of an error, after clearing the doorbell,
1649 * we also need to clear the outstanding_request
1650 * field in hba
1651 */
1652 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301653 }
1654
1655 return err;
1656}
1657
1658/**
1659 * ufshcd_get_dev_cmd_tag - Get device management command tag
1660 * @hba: per-adapter instance
1661 * @tag: pointer to variable with available slot value
1662 *
1663 * Get a free slot and lock it until device management command
1664 * completes.
1665 *
1666 * Returns false if free slot is unavailable for locking, else
1667 * return true with tag value in @tag.
1668 */
1669static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1670{
1671 int tag;
1672 bool ret = false;
1673 unsigned long tmp;
1674
1675 if (!tag_out)
1676 goto out;
1677
1678 do {
1679 tmp = ~hba->lrb_in_use;
1680 tag = find_last_bit(&tmp, hba->nutrs);
1681 if (tag >= hba->nutrs)
1682 goto out;
1683 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1684
1685 *tag_out = tag;
1686 ret = true;
1687out:
1688 return ret;
1689}
1690
1691static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1692{
1693 clear_bit_unlock(tag, &hba->lrb_in_use);
1694}
1695
1696/**
1697 * ufshcd_exec_dev_cmd - API for sending device management requests
1698 * @hba - UFS hba
1699 * @cmd_type - specifies the type (NOP, Query...)
1700 * @timeout - time in seconds
1701 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301702 * NOTE: Since there is only one available tag for device management commands,
1703 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301704 */
1705static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1706 enum dev_cmd_type cmd_type, int timeout)
1707{
1708 struct ufshcd_lrb *lrbp;
1709 int err;
1710 int tag;
1711 struct completion wait;
1712 unsigned long flags;
1713
1714 /*
1715 * Get free slot, sleep if slots are unavailable.
1716 * Even though we use wait_event() which sleeps indefinitely,
1717 * the maximum wait time is bounded by SCSI request timeout.
1718 */
1719 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1720
1721 init_completion(&wait);
1722 lrbp = &hba->lrb[tag];
1723 WARN_ON(lrbp->cmd);
1724 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1725 if (unlikely(err))
1726 goto out_put_tag;
1727
1728 hba->dev_cmd.complete = &wait;
1729
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001730 /* Make sure descriptors are ready before ringing the doorbell */
1731 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301732 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001733 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301734 ufshcd_send_command(hba, tag);
1735 spin_unlock_irqrestore(hba->host->host_lock, flags);
1736
1737 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1738
1739out_put_tag:
1740 ufshcd_put_dev_cmd_tag(hba, tag);
1741 wake_up(&hba->dev_cmd.tag_wq);
1742 return err;
1743}
1744
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301745/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001746 * ufshcd_init_query() - init the query response and request parameters
1747 * @hba: per-adapter instance
1748 * @request: address of the request pointer to be initialized
1749 * @response: address of the response pointer to be initialized
1750 * @opcode: operation to perform
1751 * @idn: flag idn to access
1752 * @index: LU number to access
1753 * @selector: query/flag/descriptor further identification
1754 */
1755static inline void ufshcd_init_query(struct ufs_hba *hba,
1756 struct ufs_query_req **request, struct ufs_query_res **response,
1757 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1758{
1759 *request = &hba->dev_cmd.query.request;
1760 *response = &hba->dev_cmd.query.response;
1761 memset(*request, 0, sizeof(struct ufs_query_req));
1762 memset(*response, 0, sizeof(struct ufs_query_res));
1763 (*request)->upiu_req.opcode = opcode;
1764 (*request)->upiu_req.idn = idn;
1765 (*request)->upiu_req.index = index;
1766 (*request)->upiu_req.selector = selector;
1767}
1768
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001769static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1770 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1771{
1772 int ret;
1773 int retries;
1774
1775 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1776 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1777 if (ret)
1778 dev_dbg(hba->dev,
1779 "%s: failed with error %d, retries %d\n",
1780 __func__, ret, retries);
1781 else
1782 break;
1783 }
1784
1785 if (ret)
1786 dev_err(hba->dev,
1787 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1788 __func__, opcode, idn, ret, retries);
1789 return ret;
1790}
1791
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001792/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301793 * ufshcd_query_flag() - API function for sending flag query requests
1794 * hba: per-adapter instance
1795 * query_opcode: flag query to perform
1796 * idn: flag idn to access
1797 * flag_res: the flag value after the query request completes
1798 *
1799 * Returns 0 for success, non-zero in case of failure
1800 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001801int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301802 enum flag_idn idn, bool *flag_res)
1803{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001804 struct ufs_query_req *request = NULL;
1805 struct ufs_query_res *response = NULL;
1806 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001807 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301808
1809 BUG_ON(!hba);
1810
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001811 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301812 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001813 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1814 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301815
1816 switch (opcode) {
1817 case UPIU_QUERY_OPCODE_SET_FLAG:
1818 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1819 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1820 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1821 break;
1822 case UPIU_QUERY_OPCODE_READ_FLAG:
1823 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1824 if (!flag_res) {
1825 /* No dummy reads */
1826 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1827 __func__);
1828 err = -EINVAL;
1829 goto out_unlock;
1830 }
1831 break;
1832 default:
1833 dev_err(hba->dev,
1834 "%s: Expected query flag opcode but got = %d\n",
1835 __func__, opcode);
1836 err = -EINVAL;
1837 goto out_unlock;
1838 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301839
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001840 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1841 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1842
1843 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301844
1845 if (err) {
1846 dev_err(hba->dev,
1847 "%s: Sending flag query for idn %d failed, err = %d\n",
1848 __func__, idn, err);
1849 goto out_unlock;
1850 }
1851
1852 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301853 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301854 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1855
1856out_unlock:
1857 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001858 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301859 return err;
1860}
1861
1862/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301863 * ufshcd_query_attr - API function for sending attribute requests
1864 * hba: per-adapter instance
1865 * opcode: attribute opcode
1866 * idn: attribute idn to access
1867 * index: index field
1868 * selector: selector field
1869 * attr_val: the attribute value after the query request completes
1870 *
1871 * Returns 0 for success, non-zero in case of failure
1872*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301873static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301874 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1875{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001876 struct ufs_query_req *request = NULL;
1877 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301878 int err;
1879
1880 BUG_ON(!hba);
1881
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001882 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301883 if (!attr_val) {
1884 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1885 __func__, opcode);
1886 err = -EINVAL;
1887 goto out;
1888 }
1889
1890 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001891 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1892 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301893
1894 switch (opcode) {
1895 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1896 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301897 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301898 break;
1899 case UPIU_QUERY_OPCODE_READ_ATTR:
1900 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1901 break;
1902 default:
1903 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1904 __func__, opcode);
1905 err = -EINVAL;
1906 goto out_unlock;
1907 }
1908
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001909 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301910
1911 if (err) {
1912 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1913 __func__, opcode, idn, err);
1914 goto out_unlock;
1915 }
1916
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301917 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301918
1919out_unlock:
1920 mutex_unlock(&hba->dev_cmd.lock);
1921out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001922 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301923 return err;
1924}
1925
1926/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001927 * ufshcd_query_attr_retry() - API function for sending query
1928 * attribute with retries
1929 * @hba: per-adapter instance
1930 * @opcode: attribute opcode
1931 * @idn: attribute idn to access
1932 * @index: index field
1933 * @selector: selector field
1934 * @attr_val: the attribute value after the query request
1935 * completes
1936 *
1937 * Returns 0 for success, non-zero in case of failure
1938*/
1939static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1940 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1941 u32 *attr_val)
1942{
1943 int ret = 0;
1944 u32 retries;
1945
1946 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1947 ret = ufshcd_query_attr(hba, opcode, idn, index,
1948 selector, attr_val);
1949 if (ret)
1950 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1951 __func__, ret, retries);
1952 else
1953 break;
1954 }
1955
1956 if (ret)
1957 dev_err(hba->dev,
1958 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1959 __func__, idn, ret, QUERY_REQ_RETRIES);
1960 return ret;
1961}
1962
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001963static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001964 enum query_opcode opcode, enum desc_idn idn, u8 index,
1965 u8 selector, u8 *desc_buf, int *buf_len)
1966{
1967 struct ufs_query_req *request = NULL;
1968 struct ufs_query_res *response = NULL;
1969 int err;
1970
1971 BUG_ON(!hba);
1972
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001973 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001974 if (!desc_buf) {
1975 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1976 __func__, opcode);
1977 err = -EINVAL;
1978 goto out;
1979 }
1980
1981 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1982 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1983 __func__, *buf_len);
1984 err = -EINVAL;
1985 goto out;
1986 }
1987
1988 mutex_lock(&hba->dev_cmd.lock);
1989 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1990 selector);
1991 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001992 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001993
1994 switch (opcode) {
1995 case UPIU_QUERY_OPCODE_WRITE_DESC:
1996 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1997 break;
1998 case UPIU_QUERY_OPCODE_READ_DESC:
1999 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2000 break;
2001 default:
2002 dev_err(hba->dev,
2003 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2004 __func__, opcode);
2005 err = -EINVAL;
2006 goto out_unlock;
2007 }
2008
2009 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2010
2011 if (err) {
2012 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
2013 __func__, opcode, idn, err);
2014 goto out_unlock;
2015 }
2016
2017 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002018 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002019
2020out_unlock:
2021 mutex_unlock(&hba->dev_cmd.lock);
2022out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002023 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002024 return err;
2025}
2026
2027/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002028 * ufshcd_query_descriptor_retry - API function for sending descriptor
2029 * requests
2030 * hba: per-adapter instance
2031 * opcode: attribute opcode
2032 * idn: attribute idn to access
2033 * index: index field
2034 * selector: selector field
2035 * desc_buf: the buffer that contains the descriptor
2036 * buf_len: length parameter passed to the device
2037 *
2038 * Returns 0 for success, non-zero in case of failure.
2039 * The buf_len parameter will contain, on return, the length parameter
2040 * received on the response.
2041 */
2042int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2043 enum query_opcode opcode, enum desc_idn idn, u8 index,
2044 u8 selector, u8 *desc_buf, int *buf_len)
2045{
2046 int err;
2047 int retries;
2048
2049 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2050 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2051 selector, desc_buf, buf_len);
2052 if (!err || err == -EINVAL)
2053 break;
2054 }
2055
2056 return err;
2057}
2058EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2059
2060/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002061 * ufshcd_read_desc_param - read the specified descriptor parameter
2062 * @hba: Pointer to adapter instance
2063 * @desc_id: descriptor idn value
2064 * @desc_index: descriptor index
2065 * @param_offset: offset of the parameter to read
2066 * @param_read_buf: pointer to buffer where parameter would be read
2067 * @param_size: sizeof(param_read_buf)
2068 *
2069 * Return 0 in case of success, non-zero otherwise
2070 */
2071static int ufshcd_read_desc_param(struct ufs_hba *hba,
2072 enum desc_idn desc_id,
2073 int desc_index,
2074 u32 param_offset,
2075 u8 *param_read_buf,
2076 u32 param_size)
2077{
2078 int ret;
2079 u8 *desc_buf;
2080 u32 buff_len;
2081 bool is_kmalloc = true;
2082
2083 /* safety checks */
2084 if (desc_id >= QUERY_DESC_IDN_MAX)
2085 return -EINVAL;
2086
2087 buff_len = ufs_query_desc_max_size[desc_id];
2088 if ((param_offset + param_size) > buff_len)
2089 return -EINVAL;
2090
2091 if (!param_offset && (param_size == buff_len)) {
2092 /* memory space already available to hold full descriptor */
2093 desc_buf = param_read_buf;
2094 is_kmalloc = false;
2095 } else {
2096 /* allocate memory to hold full descriptor */
2097 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2098 if (!desc_buf)
2099 return -ENOMEM;
2100 }
2101
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002102 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2103 desc_id, desc_index, 0, desc_buf,
2104 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002105
2106 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
2107 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
2108 ufs_query_desc_max_size[desc_id])
2109 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
2110 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
2111 __func__, desc_id, param_offset, buff_len, ret);
2112 if (!ret)
2113 ret = -EINVAL;
2114
2115 goto out;
2116 }
2117
2118 if (is_kmalloc)
2119 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2120out:
2121 if (is_kmalloc)
2122 kfree(desc_buf);
2123 return ret;
2124}
2125
2126static inline int ufshcd_read_desc(struct ufs_hba *hba,
2127 enum desc_idn desc_id,
2128 int desc_index,
2129 u8 *buf,
2130 u32 size)
2131{
2132 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2133}
2134
2135static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2136 u8 *buf,
2137 u32 size)
2138{
2139 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2140}
2141
Yaniv Gardib573d482016-03-10 17:37:09 +02002142int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2143{
2144 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2145}
2146EXPORT_SYMBOL(ufshcd_read_device_desc);
2147
2148/**
2149 * ufshcd_read_string_desc - read string descriptor
2150 * @hba: pointer to adapter instance
2151 * @desc_index: descriptor index
2152 * @buf: pointer to buffer where descriptor would be read
2153 * @size: size of buf
2154 * @ascii: if true convert from unicode to ascii characters
2155 *
2156 * Return 0 in case of success, non-zero otherwise
2157 */
2158int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2159 u32 size, bool ascii)
2160{
2161 int err = 0;
2162
2163 err = ufshcd_read_desc(hba,
2164 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2165
2166 if (err) {
2167 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2168 __func__, QUERY_REQ_RETRIES, err);
2169 goto out;
2170 }
2171
2172 if (ascii) {
2173 int desc_len;
2174 int ascii_len;
2175 int i;
2176 char *buff_ascii;
2177
2178 desc_len = buf[0];
2179 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2180 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2181 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2182 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2183 __func__);
2184 err = -ENOMEM;
2185 goto out;
2186 }
2187
2188 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2189 if (!buff_ascii) {
2190 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002191 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002192 }
2193
2194 /*
2195 * the descriptor contains string in UTF16 format
2196 * we need to convert to utf-8 so it can be displayed
2197 */
2198 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2199 desc_len - QUERY_DESC_HDR_SIZE,
2200 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2201
2202 /* replace non-printable or non-ASCII characters with spaces */
2203 for (i = 0; i < ascii_len; i++)
2204 ufshcd_remove_non_printable(&buff_ascii[i]);
2205
2206 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2207 size - QUERY_DESC_HDR_SIZE);
2208 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2209 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002210 kfree(buff_ascii);
2211 }
2212out:
2213 return err;
2214}
2215EXPORT_SYMBOL(ufshcd_read_string_desc);
2216
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002217/**
2218 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2219 * @hba: Pointer to adapter instance
2220 * @lun: lun id
2221 * @param_offset: offset of the parameter to read
2222 * @param_read_buf: pointer to buffer where parameter would be read
2223 * @param_size: sizeof(param_read_buf)
2224 *
2225 * Return 0 in case of success, non-zero otherwise
2226 */
2227static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2228 int lun,
2229 enum unit_desc_param param_offset,
2230 u8 *param_read_buf,
2231 u32 param_size)
2232{
2233 /*
2234 * Unit descriptors are only available for general purpose LUs (LUN id
2235 * from 0 to 7) and RPMB Well known LU.
2236 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002237 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002238 return -EOPNOTSUPP;
2239
2240 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2241 param_offset, param_read_buf, param_size);
2242}
2243
2244/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302245 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2246 * @hba: per adapter instance
2247 *
2248 * 1. Allocate DMA memory for Command Descriptor array
2249 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2250 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2251 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2252 * (UTMRDL)
2253 * 4. Allocate memory for local reference block(lrb).
2254 *
2255 * Returns 0 for success, non-zero in case of failure
2256 */
2257static int ufshcd_memory_alloc(struct ufs_hba *hba)
2258{
2259 size_t utmrdl_size, utrdl_size, ucdl_size;
2260
2261 /* Allocate memory for UTP command descriptors */
2262 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002263 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2264 ucdl_size,
2265 &hba->ucdl_dma_addr,
2266 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302267
2268 /*
2269 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2270 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2271 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2272 * be aligned to 128 bytes as well
2273 */
2274 if (!hba->ucdl_base_addr ||
2275 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302276 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302277 "Command Descriptor Memory allocation failed\n");
2278 goto out;
2279 }
2280
2281 /*
2282 * Allocate memory for UTP Transfer descriptors
2283 * UFSHCI requires 1024 byte alignment of UTRD
2284 */
2285 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002286 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2287 utrdl_size,
2288 &hba->utrdl_dma_addr,
2289 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302290 if (!hba->utrdl_base_addr ||
2291 WARN_ON(hba->utrdl_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 "Transfer Descriptor Memory allocation failed\n");
2294 goto out;
2295 }
2296
2297 /*
2298 * Allocate memory for UTP Task Management descriptors
2299 * UFSHCI requires 1024 byte alignment of UTMRD
2300 */
2301 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002302 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2303 utmrdl_size,
2304 &hba->utmrdl_dma_addr,
2305 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302306 if (!hba->utmrdl_base_addr ||
2307 WARN_ON(hba->utmrdl_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 "Task Management Descriptor Memory allocation failed\n");
2310 goto out;
2311 }
2312
2313 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002314 hba->lrb = devm_kzalloc(hba->dev,
2315 hba->nutrs * sizeof(struct ufshcd_lrb),
2316 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302317 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302318 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302319 goto out;
2320 }
2321 return 0;
2322out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302323 return -ENOMEM;
2324}
2325
2326/**
2327 * ufshcd_host_memory_configure - configure local reference block with
2328 * memory offsets
2329 * @hba: per adapter instance
2330 *
2331 * Configure Host memory space
2332 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2333 * address.
2334 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2335 * and PRDT offset.
2336 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2337 * into local reference block.
2338 */
2339static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2340{
2341 struct utp_transfer_cmd_desc *cmd_descp;
2342 struct utp_transfer_req_desc *utrdlp;
2343 dma_addr_t cmd_desc_dma_addr;
2344 dma_addr_t cmd_desc_element_addr;
2345 u16 response_offset;
2346 u16 prdt_offset;
2347 int cmd_desc_size;
2348 int i;
2349
2350 utrdlp = hba->utrdl_base_addr;
2351 cmd_descp = hba->ucdl_base_addr;
2352
2353 response_offset =
2354 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2355 prdt_offset =
2356 offsetof(struct utp_transfer_cmd_desc, prd_table);
2357
2358 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2359 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2360
2361 for (i = 0; i < hba->nutrs; i++) {
2362 /* Configure UTRD with command descriptor base address */
2363 cmd_desc_element_addr =
2364 (cmd_desc_dma_addr + (cmd_desc_size * i));
2365 utrdlp[i].command_desc_base_addr_lo =
2366 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2367 utrdlp[i].command_desc_base_addr_hi =
2368 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2369
2370 /* Response upiu and prdt offset should be in double words */
2371 utrdlp[i].response_upiu_offset =
2372 cpu_to_le16((response_offset >> 2));
2373 utrdlp[i].prd_table_offset =
2374 cpu_to_le16((prdt_offset >> 2));
2375 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302376 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302377
2378 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302379 hba->lrb[i].ucd_req_ptr =
2380 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302381 hba->lrb[i].ucd_rsp_ptr =
2382 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2383 hba->lrb[i].ucd_prdt_ptr =
2384 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2385 }
2386}
2387
2388/**
2389 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2390 * @hba: per adapter instance
2391 *
2392 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2393 * in order to initialize the Unipro link startup procedure.
2394 * Once the Unipro links are up, the device connected to the controller
2395 * is detected.
2396 *
2397 * Returns 0 on success, non-zero value on failure
2398 */
2399static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2400{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302401 struct uic_command uic_cmd = {0};
2402 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302403
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302404 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2405
2406 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2407 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302408 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302409 "dme-link-startup: error code %d\n", ret);
2410 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302411}
2412
Yaniv Gardicad2e032015-03-31 17:37:14 +03002413static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2414{
2415 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2416 unsigned long min_sleep_time_us;
2417
2418 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2419 return;
2420
2421 /*
2422 * last_dme_cmd_tstamp will be 0 only for 1st call to
2423 * this function
2424 */
2425 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2426 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2427 } else {
2428 unsigned long delta =
2429 (unsigned long) ktime_to_us(
2430 ktime_sub(ktime_get(),
2431 hba->last_dme_cmd_tstamp));
2432
2433 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2434 min_sleep_time_us =
2435 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2436 else
2437 return; /* no more delay required */
2438 }
2439
2440 /* allow sleep for extra 50us if needed */
2441 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2442}
2443
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302444/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302445 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2446 * @hba: per adapter instance
2447 * @attr_sel: uic command argument1
2448 * @attr_set: attribute set type as uic command argument2
2449 * @mib_val: setting value as uic command argument3
2450 * @peer: indicate whether peer or local
2451 *
2452 * Returns 0 on success, non-zero value on failure
2453 */
2454int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2455 u8 attr_set, u32 mib_val, u8 peer)
2456{
2457 struct uic_command uic_cmd = {0};
2458 static const char *const action[] = {
2459 "dme-set",
2460 "dme-peer-set"
2461 };
2462 const char *set = action[!!peer];
2463 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002464 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302465
2466 uic_cmd.command = peer ?
2467 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2468 uic_cmd.argument1 = attr_sel;
2469 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2470 uic_cmd.argument3 = mib_val;
2471
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002472 do {
2473 /* for peer attributes we retry upon failure */
2474 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2475 if (ret)
2476 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2477 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2478 } while (ret && peer && --retries);
2479
2480 if (!retries)
2481 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2482 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2483 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302484
2485 return ret;
2486}
2487EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2488
2489/**
2490 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2491 * @hba: per adapter instance
2492 * @attr_sel: uic command argument1
2493 * @mib_val: the value of the attribute as returned by the UIC command
2494 * @peer: indicate whether peer or local
2495 *
2496 * Returns 0 on success, non-zero value on failure
2497 */
2498int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2499 u32 *mib_val, u8 peer)
2500{
2501 struct uic_command uic_cmd = {0};
2502 static const char *const action[] = {
2503 "dme-get",
2504 "dme-peer-get"
2505 };
2506 const char *get = action[!!peer];
2507 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002508 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002509 struct ufs_pa_layer_attr orig_pwr_info;
2510 struct ufs_pa_layer_attr temp_pwr_info;
2511 bool pwr_mode_change = false;
2512
2513 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2514 orig_pwr_info = hba->pwr_info;
2515 temp_pwr_info = orig_pwr_info;
2516
2517 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2518 orig_pwr_info.pwr_rx == FAST_MODE) {
2519 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2520 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2521 pwr_mode_change = true;
2522 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2523 orig_pwr_info.pwr_rx == SLOW_MODE) {
2524 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2525 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2526 pwr_mode_change = true;
2527 }
2528 if (pwr_mode_change) {
2529 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2530 if (ret)
2531 goto out;
2532 }
2533 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302534
2535 uic_cmd.command = peer ?
2536 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2537 uic_cmd.argument1 = attr_sel;
2538
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002539 do {
2540 /* for peer attributes we retry upon failure */
2541 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2542 if (ret)
2543 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2544 get, UIC_GET_ATTR_ID(attr_sel), ret);
2545 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302546
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002547 if (!retries)
2548 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2549 get, UIC_GET_ATTR_ID(attr_sel), retries);
2550
2551 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302552 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002553
2554 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2555 && pwr_mode_change)
2556 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302557out:
2558 return ret;
2559}
2560EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2561
2562/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002563 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2564 * state) and waits for it to take effect.
2565 *
2566 * @hba: per adapter instance
2567 * @cmd: UIC command to execute
2568 *
2569 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2570 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2571 * and device UniPro link and hence it's final completion would be indicated by
2572 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2573 * addition to normal UIC command completion Status (UCCS). This function only
2574 * returns after the relevant status bits indicate the completion.
2575 *
2576 * Returns 0 on success, non-zero value on failure
2577 */
2578static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2579{
2580 struct completion uic_async_done;
2581 unsigned long flags;
2582 u8 status;
2583 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002584 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002585
2586 mutex_lock(&hba->uic_cmd_mutex);
2587 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002588 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002589
2590 spin_lock_irqsave(hba->host->host_lock, flags);
2591 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002592 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2593 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2594 /*
2595 * Make sure UIC command completion interrupt is disabled before
2596 * issuing UIC command.
2597 */
2598 wmb();
2599 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002600 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002601 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2602 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002603 if (ret) {
2604 dev_err(hba->dev,
2605 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2606 cmd->command, cmd->argument3, ret);
2607 goto out;
2608 }
2609
2610 if (!wait_for_completion_timeout(hba->uic_async_done,
2611 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2612 dev_err(hba->dev,
2613 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2614 cmd->command, cmd->argument3);
2615 ret = -ETIMEDOUT;
2616 goto out;
2617 }
2618
2619 status = ufshcd_get_upmcrs(hba);
2620 if (status != PWR_LOCAL) {
2621 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002622 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002623 cmd->command, status);
2624 ret = (status != PWR_OK) ? status : -1;
2625 }
2626out:
2627 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002628 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002629 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002630 if (reenable_intr)
2631 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002632 spin_unlock_irqrestore(hba->host->host_lock, flags);
2633 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002634
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002635 return ret;
2636}
2637
2638/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302639 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2640 * using DME_SET primitives.
2641 * @hba: per adapter instance
2642 * @mode: powr mode value
2643 *
2644 * Returns 0 on success, non-zero value on failure
2645 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302646static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302647{
2648 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002649 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302650
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002651 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2652 ret = ufshcd_dme_set(hba,
2653 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2654 if (ret) {
2655 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2656 __func__, ret);
2657 goto out;
2658 }
2659 }
2660
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302661 uic_cmd.command = UIC_CMD_DME_SET;
2662 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2663 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002664 ufshcd_hold(hba, false);
2665 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2666 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302667
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002668out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002669 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002670}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302671
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002672static int ufshcd_link_recovery(struct ufs_hba *hba)
2673{
2674 int ret;
2675 unsigned long flags;
2676
2677 spin_lock_irqsave(hba->host->host_lock, flags);
2678 hba->ufshcd_state = UFSHCD_STATE_RESET;
2679 ufshcd_set_eh_in_progress(hba);
2680 spin_unlock_irqrestore(hba->host->host_lock, flags);
2681
2682 ret = ufshcd_host_reset_and_restore(hba);
2683
2684 spin_lock_irqsave(hba->host->host_lock, flags);
2685 if (ret)
2686 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2687 ufshcd_clear_eh_in_progress(hba);
2688 spin_unlock_irqrestore(hba->host->host_lock, flags);
2689
2690 if (ret)
2691 dev_err(hba->dev, "%s: link recovery failed, err %d",
2692 __func__, ret);
2693
2694 return ret;
2695}
2696
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002697static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002698{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002699 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002700 struct uic_command uic_cmd = {0};
2701
Kiwoong Kimee32c902016-11-10 21:17:43 +09002702 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
2703
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002704 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002705 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002706
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002707 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002708 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2709 __func__, ret);
2710
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002711 /*
2712 * If link recovery fails then return error so that caller
2713 * don't retry the hibern8 enter again.
2714 */
2715 if (ufshcd_link_recovery(hba))
2716 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09002717 } else
2718 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
2719 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002720
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002721 return ret;
2722}
2723
2724static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2725{
2726 int ret = 0, retries;
2727
2728 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2729 ret = __ufshcd_uic_hibern8_enter(hba);
2730 if (!ret || ret == -ENOLINK)
2731 goto out;
2732 }
2733out:
2734 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002735}
2736
2737static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2738{
2739 struct uic_command uic_cmd = {0};
2740 int ret;
2741
Kiwoong Kimee32c902016-11-10 21:17:43 +09002742 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
2743
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002744 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2745 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302746 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002747 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2748 __func__, ret);
2749 ret = ufshcd_link_recovery(hba);
Kiwoong Kimee32c902016-11-10 21:17:43 +09002750 } else
2751 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
2752 POST_CHANGE);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302753
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302754 return ret;
2755}
2756
Yaniv Gardi50646362014-10-23 13:25:13 +03002757 /**
2758 * ufshcd_init_pwr_info - setting the POR (power on reset)
2759 * values in hba power info
2760 * @hba: per-adapter instance
2761 */
2762static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2763{
2764 hba->pwr_info.gear_rx = UFS_PWM_G1;
2765 hba->pwr_info.gear_tx = UFS_PWM_G1;
2766 hba->pwr_info.lane_rx = 1;
2767 hba->pwr_info.lane_tx = 1;
2768 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2769 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2770 hba->pwr_info.hs_rate = 0;
2771}
2772
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302773/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002774 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2775 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302776 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002777static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302778{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002779 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2780
2781 if (hba->max_pwr_info.is_valid)
2782 return 0;
2783
2784 pwr_info->pwr_tx = FASTAUTO_MODE;
2785 pwr_info->pwr_rx = FASTAUTO_MODE;
2786 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302787
2788 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002789 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2790 &pwr_info->lane_rx);
2791 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2792 &pwr_info->lane_tx);
2793
2794 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2795 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2796 __func__,
2797 pwr_info->lane_rx,
2798 pwr_info->lane_tx);
2799 return -EINVAL;
2800 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302801
2802 /*
2803 * First, get the maximum gears of HS speed.
2804 * If a zero value, it means there is no HSGEAR capability.
2805 * Then, get the maximum gears of PWM speed.
2806 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002807 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2808 if (!pwr_info->gear_rx) {
2809 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2810 &pwr_info->gear_rx);
2811 if (!pwr_info->gear_rx) {
2812 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2813 __func__, pwr_info->gear_rx);
2814 return -EINVAL;
2815 }
2816 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302817 }
2818
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002819 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2820 &pwr_info->gear_tx);
2821 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302822 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002823 &pwr_info->gear_tx);
2824 if (!pwr_info->gear_tx) {
2825 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2826 __func__, pwr_info->gear_tx);
2827 return -EINVAL;
2828 }
2829 pwr_info->pwr_tx = SLOWAUTO_MODE;
2830 }
2831
2832 hba->max_pwr_info.is_valid = true;
2833 return 0;
2834}
2835
2836static int ufshcd_change_power_mode(struct ufs_hba *hba,
2837 struct ufs_pa_layer_attr *pwr_mode)
2838{
2839 int ret;
2840
2841 /* if already configured to the requested pwr_mode */
2842 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2843 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2844 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2845 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2846 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2847 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2848 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2849 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2850 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302851 }
2852
2853 /*
2854 * Configure attributes for power mode change with below.
2855 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2856 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2857 * - PA_HSSERIES
2858 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002859 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2860 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2861 pwr_mode->lane_rx);
2862 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2863 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302864 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002865 else
2866 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302867
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002868 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2869 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2870 pwr_mode->lane_tx);
2871 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2872 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302873 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002874 else
2875 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302876
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002877 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2878 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2879 pwr_mode->pwr_rx == FAST_MODE ||
2880 pwr_mode->pwr_tx == FAST_MODE)
2881 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2882 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302883
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002884 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2885 | pwr_mode->pwr_tx);
2886
2887 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302888 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002889 "%s: power mode change failed %d\n", __func__, ret);
2890 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002891 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2892 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002893
2894 memcpy(&hba->pwr_info, pwr_mode,
2895 sizeof(struct ufs_pa_layer_attr));
2896 }
2897
2898 return ret;
2899}
2900
2901/**
2902 * ufshcd_config_pwr_mode - configure a new power mode
2903 * @hba: per-adapter instance
2904 * @desired_pwr_mode: desired power configuration
2905 */
2906static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2907 struct ufs_pa_layer_attr *desired_pwr_mode)
2908{
2909 struct ufs_pa_layer_attr final_params = { 0 };
2910 int ret;
2911
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002912 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2913 desired_pwr_mode, &final_params);
2914
2915 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002916 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2917
2918 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302919
2920 return ret;
2921}
2922
2923/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302924 * ufshcd_complete_dev_init() - checks device readiness
2925 * hba: per-adapter instance
2926 *
2927 * Set fDeviceInit flag and poll until device toggles it.
2928 */
2929static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2930{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002931 int i;
2932 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302933 bool flag_res = 1;
2934
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002935 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2936 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302937 if (err) {
2938 dev_err(hba->dev,
2939 "%s setting fDeviceInit flag failed with error %d\n",
2940 __func__, err);
2941 goto out;
2942 }
2943
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002944 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2945 for (i = 0; i < 1000 && !err && flag_res; i++)
2946 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2947 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2948
Dolev Raviv68078d52013-07-30 00:35:58 +05302949 if (err)
2950 dev_err(hba->dev,
2951 "%s reading fDeviceInit flag failed with error %d\n",
2952 __func__, err);
2953 else if (flag_res)
2954 dev_err(hba->dev,
2955 "%s fDeviceInit was not cleared by the device\n",
2956 __func__);
2957
2958out:
2959 return err;
2960}
2961
2962/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302963 * ufshcd_make_hba_operational - Make UFS controller operational
2964 * @hba: per adapter instance
2965 *
2966 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002967 * 1. Enable required interrupts
2968 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02002969 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002970 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302971 *
2972 * Returns 0 on success, non-zero value on failure
2973 */
2974static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2975{
2976 int err = 0;
2977 u32 reg;
2978
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302979 /* Enable required interrupts */
2980 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2981
2982 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002983 if (ufshcd_is_intr_aggr_allowed(hba))
2984 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2985 else
2986 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302987
2988 /* Configure UTRL and UTMRL base address registers */
2989 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2990 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2991 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2992 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2993 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2994 REG_UTP_TASK_REQ_LIST_BASE_L);
2995 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2996 REG_UTP_TASK_REQ_LIST_BASE_H);
2997
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302998 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02002999 * Make sure base address and interrupt setup are updated before
3000 * enabling the run/stop registers below.
3001 */
3002 wmb();
3003
3004 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303005 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303006 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003007 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303008 if (!(ufshcd_get_lists_status(reg))) {
3009 ufshcd_enable_run_stop_reg(hba);
3010 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303011 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303012 "Host controller not ready to process requests");
3013 err = -EIO;
3014 goto out;
3015 }
3016
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303017out:
3018 return err;
3019}
3020
3021/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003022 * ufshcd_hba_stop - Send controller to reset state
3023 * @hba: per adapter instance
3024 * @can_sleep: perform sleep or just spin
3025 */
3026static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3027{
3028 int err;
3029
3030 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3031 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3032 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3033 10, 1, can_sleep);
3034 if (err)
3035 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3036}
3037
3038/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303039 * ufshcd_hba_enable - initialize the controller
3040 * @hba: per adapter instance
3041 *
3042 * The controller resets itself and controller firmware initialization
3043 * sequence kicks off. When controller is ready it will set
3044 * the Host Controller Enable bit to 1.
3045 *
3046 * Returns 0 on success, non-zero value on failure
3047 */
3048static int ufshcd_hba_enable(struct ufs_hba *hba)
3049{
3050 int retry;
3051
3052 /*
3053 * msleep of 1 and 5 used in this function might result in msleep(20),
3054 * but it was necessary to send the UFS FPGA to reset mode during
3055 * development and testing of this driver. msleep can be changed to
3056 * mdelay and retry count can be reduced based on the controller.
3057 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003058 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303059 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003060 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303061
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003062 /* UniPro link is disabled at this point */
3063 ufshcd_set_link_off(hba);
3064
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003065 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003066
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303067 /* start controller initialization sequence */
3068 ufshcd_hba_start(hba);
3069
3070 /*
3071 * To initialize a UFS host controller HCE bit must be set to 1.
3072 * During initialization the HCE bit value changes from 1->0->1.
3073 * When the host controller completes initialization sequence
3074 * it sets the value of HCE bit to 1. The same HCE bit is read back
3075 * to check if the controller has completed initialization sequence.
3076 * So without this delay the value HCE = 1, set in the previous
3077 * instruction might be read back.
3078 * This delay can be changed based on the controller.
3079 */
3080 msleep(1);
3081
3082 /* wait for the host controller to complete initialization */
3083 retry = 10;
3084 while (ufshcd_is_hba_active(hba)) {
3085 if (retry) {
3086 retry--;
3087 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303088 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303089 "Controller enable failed\n");
3090 return -EIO;
3091 }
3092 msleep(5);
3093 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003094
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003095 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003096 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003097
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003098 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003099
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303100 return 0;
3101}
3102
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003103static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3104{
3105 int tx_lanes, i, err = 0;
3106
3107 if (!peer)
3108 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3109 &tx_lanes);
3110 else
3111 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3112 &tx_lanes);
3113 for (i = 0; i < tx_lanes; i++) {
3114 if (!peer)
3115 err = ufshcd_dme_set(hba,
3116 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3117 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3118 0);
3119 else
3120 err = ufshcd_dme_peer_set(hba,
3121 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3122 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3123 0);
3124 if (err) {
3125 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3126 __func__, peer, i, err);
3127 break;
3128 }
3129 }
3130
3131 return err;
3132}
3133
3134static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3135{
3136 return ufshcd_disable_tx_lcc(hba, true);
3137}
3138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303139/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303140 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303141 * @hba: per adapter instance
3142 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303143 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303144 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303145static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303146{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303147 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003148 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303149
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003150 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003151 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303152
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003153 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003154
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003155 /* check if device is detected by inter-connect layer */
3156 if (!ret && !ufshcd_is_device_present(hba)) {
3157 dev_err(hba->dev, "%s: Device not present\n", __func__);
3158 ret = -ENXIO;
3159 goto out;
3160 }
3161
3162 /*
3163 * DME link lost indication is only received when link is up,
3164 * but we can't be sure if the link is up until link startup
3165 * succeeds. So reset the local Uni-Pro and try again.
3166 */
3167 if (ret && ufshcd_hba_enable(hba))
3168 goto out;
3169 } while (ret && retries--);
3170
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303171 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003172 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303173 goto out;
3174
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003175 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3176 ret = ufshcd_disable_device_tx_lcc(hba);
3177 if (ret)
3178 goto out;
3179 }
3180
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003181 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003182 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3183 if (ret)
3184 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003185
3186 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303187out:
3188 if (ret)
3189 dev_err(hba->dev, "link startup failed %d\n", ret);
3190 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303191}
3192
3193/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303194 * ufshcd_verify_dev_init() - Verify device initialization
3195 * @hba: per-adapter instance
3196 *
3197 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3198 * device Transport Protocol (UTP) layer is ready after a reset.
3199 * If the UTP layer at the device side is not initialized, it may
3200 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3201 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3202 */
3203static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3204{
3205 int err = 0;
3206 int retries;
3207
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003208 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303209 mutex_lock(&hba->dev_cmd.lock);
3210 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3211 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3212 NOP_OUT_TIMEOUT);
3213
3214 if (!err || err == -ETIMEDOUT)
3215 break;
3216
3217 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3218 }
3219 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003220 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303221
3222 if (err)
3223 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3224 return err;
3225}
3226
3227/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003228 * ufshcd_set_queue_depth - set lun queue depth
3229 * @sdev: pointer to SCSI device
3230 *
3231 * Read bLUQueueDepth value and activate scsi tagged command
3232 * queueing. For WLUN, queue depth is set to 1. For best-effort
3233 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3234 * value that host can queue.
3235 */
3236static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3237{
3238 int ret = 0;
3239 u8 lun_qdepth;
3240 struct ufs_hba *hba;
3241
3242 hba = shost_priv(sdev->host);
3243
3244 lun_qdepth = hba->nutrs;
3245 ret = ufshcd_read_unit_desc_param(hba,
3246 ufshcd_scsi_to_upiu_lun(sdev->lun),
3247 UNIT_DESC_PARAM_LU_Q_DEPTH,
3248 &lun_qdepth,
3249 sizeof(lun_qdepth));
3250
3251 /* Some WLUN doesn't support unit descriptor */
3252 if (ret == -EOPNOTSUPP)
3253 lun_qdepth = 1;
3254 else if (!lun_qdepth)
3255 /* eventually, we can figure out the real queue depth */
3256 lun_qdepth = hba->nutrs;
3257 else
3258 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3259
3260 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3261 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003262 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003263}
3264
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003265/*
3266 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3267 * @hba: per-adapter instance
3268 * @lun: UFS device lun id
3269 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3270 *
3271 * Returns 0 in case of success and b_lu_write_protect status would be returned
3272 * @b_lu_write_protect parameter.
3273 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3274 * Returns -EINVAL in case of invalid parameters passed to this function.
3275 */
3276static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3277 u8 lun,
3278 u8 *b_lu_write_protect)
3279{
3280 int ret;
3281
3282 if (!b_lu_write_protect)
3283 ret = -EINVAL;
3284 /*
3285 * According to UFS device spec, RPMB LU can't be write
3286 * protected so skip reading bLUWriteProtect parameter for
3287 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3288 */
3289 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3290 ret = -ENOTSUPP;
3291 else
3292 ret = ufshcd_read_unit_desc_param(hba,
3293 lun,
3294 UNIT_DESC_PARAM_LU_WR_PROTECT,
3295 b_lu_write_protect,
3296 sizeof(*b_lu_write_protect));
3297 return ret;
3298}
3299
3300/**
3301 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3302 * status
3303 * @hba: per-adapter instance
3304 * @sdev: pointer to SCSI device
3305 *
3306 */
3307static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3308 struct scsi_device *sdev)
3309{
3310 if (hba->dev_info.f_power_on_wp_en &&
3311 !hba->dev_info.is_lu_power_on_wp) {
3312 u8 b_lu_write_protect;
3313
3314 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3315 &b_lu_write_protect) &&
3316 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3317 hba->dev_info.is_lu_power_on_wp = true;
3318 }
3319}
3320
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003321/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303322 * ufshcd_slave_alloc - handle initial SCSI device configurations
3323 * @sdev: pointer to SCSI device
3324 *
3325 * Returns success
3326 */
3327static int ufshcd_slave_alloc(struct scsi_device *sdev)
3328{
3329 struct ufs_hba *hba;
3330
3331 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303332
3333 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3334 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303335
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303336 /* allow SCSI layer to restart the device in case of errors */
3337 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003338
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003339 /* REPORT SUPPORTED OPERATION CODES is not supported */
3340 sdev->no_report_opcodes = 1;
3341
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003342
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003343 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003344
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003345 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3346
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003347 return 0;
3348}
3349
3350/**
3351 * ufshcd_change_queue_depth - change queue depth
3352 * @sdev: pointer to SCSI device
3353 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003354 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003355 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003356 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003357static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003358{
3359 struct ufs_hba *hba = shost_priv(sdev->host);
3360
3361 if (depth > hba->nutrs)
3362 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003363 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303364}
3365
3366/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003367 * ufshcd_slave_configure - adjust SCSI device configurations
3368 * @sdev: pointer to SCSI device
3369 */
3370static int ufshcd_slave_configure(struct scsi_device *sdev)
3371{
3372 struct request_queue *q = sdev->request_queue;
3373
3374 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3375 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3376
3377 return 0;
3378}
3379
3380/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303381 * ufshcd_slave_destroy - remove SCSI device configurations
3382 * @sdev: pointer to SCSI device
3383 */
3384static void ufshcd_slave_destroy(struct scsi_device *sdev)
3385{
3386 struct ufs_hba *hba;
3387
3388 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003389 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003390 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3391 unsigned long flags;
3392
3393 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003394 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003395 spin_unlock_irqrestore(hba->host->host_lock, flags);
3396 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303397}
3398
3399/**
3400 * ufshcd_task_req_compl - handle task management request completion
3401 * @hba: per adapter instance
3402 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303403 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303404 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303405 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303406 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303407static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303408{
3409 struct utp_task_req_desc *task_req_descp;
3410 struct utp_upiu_task_rsp *task_rsp_upiup;
3411 unsigned long flags;
3412 int ocs_value;
3413 int task_result;
3414
3415 spin_lock_irqsave(hba->host->host_lock, flags);
3416
3417 /* Clear completed tasks from outstanding_tasks */
3418 __clear_bit(index, &hba->outstanding_tasks);
3419
3420 task_req_descp = hba->utmrdl_base_addr;
3421 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3422
3423 if (ocs_value == OCS_SUCCESS) {
3424 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3425 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003426 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3427 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303428 if (resp)
3429 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303430 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303431 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3432 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303433 }
3434 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303435
3436 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303437}
3438
3439/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303440 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3441 * @lrb: pointer to local reference block of completed command
3442 * @scsi_status: SCSI command status
3443 *
3444 * Returns value base on SCSI command status
3445 */
3446static inline int
3447ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3448{
3449 int result = 0;
3450
3451 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303452 case SAM_STAT_CHECK_CONDITION:
3453 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303454 case SAM_STAT_GOOD:
3455 result |= DID_OK << 16 |
3456 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303457 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303458 break;
3459 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303460 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303461 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303462 ufshcd_copy_sense_data(lrbp);
3463 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303464 break;
3465 default:
3466 result |= DID_ERROR << 16;
3467 break;
3468 } /* end of switch */
3469
3470 return result;
3471}
3472
3473/**
3474 * ufshcd_transfer_rsp_status - Get overall status of the response
3475 * @hba: per adapter instance
3476 * @lrb: pointer to local reference block of completed command
3477 *
3478 * Returns result of the command to notify SCSI midlayer
3479 */
3480static inline int
3481ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3482{
3483 int result = 0;
3484 int scsi_status;
3485 int ocs;
3486
3487 /* overall command status of utrd */
3488 ocs = ufshcd_get_tr_ocs(lrbp);
3489
3490 switch (ocs) {
3491 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303492 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303493
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303494 switch (result) {
3495 case UPIU_TRANSACTION_RESPONSE:
3496 /*
3497 * get the response UPIU result to extract
3498 * the SCSI command status
3499 */
3500 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3501
3502 /*
3503 * get the result based on SCSI status response
3504 * to notify the SCSI midlayer of the command status
3505 */
3506 scsi_status = result & MASK_SCSI_STATUS;
3507 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303508
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003509 /*
3510 * Currently we are only supporting BKOPs exception
3511 * events hence we can ignore BKOPs exception event
3512 * during power management callbacks. BKOPs exception
3513 * event is not expected to be raised in runtime suspend
3514 * callback as it allows the urgent bkops.
3515 * During system suspend, we are anyway forcefully
3516 * disabling the bkops and if urgent bkops is needed
3517 * it will be enabled on system resume. Long term
3518 * solution could be to abort the system suspend if
3519 * UFS device needs urgent BKOPs.
3520 */
3521 if (!hba->pm_op_in_progress &&
3522 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303523 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303524 break;
3525 case UPIU_TRANSACTION_REJECT_UPIU:
3526 /* TODO: handle Reject UPIU Response */
3527 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303528 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303529 "Reject UPIU not fully implemented\n");
3530 break;
3531 default:
3532 result = DID_ERROR << 16;
3533 dev_err(hba->dev,
3534 "Unexpected request response code = %x\n",
3535 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303536 break;
3537 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303538 break;
3539 case OCS_ABORTED:
3540 result |= DID_ABORT << 16;
3541 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303542 case OCS_INVALID_COMMAND_STATUS:
3543 result |= DID_REQUEUE << 16;
3544 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303545 case OCS_INVALID_CMD_TABLE_ATTR:
3546 case OCS_INVALID_PRDT_ATTR:
3547 case OCS_MISMATCH_DATA_BUF_SIZE:
3548 case OCS_MISMATCH_RESP_UPIU_SIZE:
3549 case OCS_PEER_COMM_FAILURE:
3550 case OCS_FATAL_ERROR:
3551 default:
3552 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303553 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303554 "OCS error from controller = %x\n", ocs);
3555 break;
3556 } /* end of switch */
3557
3558 return result;
3559}
3560
3561/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303562 * ufshcd_uic_cmd_compl - handle completion of uic command
3563 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303564 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303565 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303566static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303567{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303568 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303569 hba->active_uic_cmd->argument2 |=
3570 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303571 hba->active_uic_cmd->argument3 =
3572 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303573 complete(&hba->active_uic_cmd->done);
3574 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303575
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003576 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3577 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303578}
3579
3580/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003581 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303582 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003583 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303584 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003585static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3586 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303587{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303588 struct ufshcd_lrb *lrbp;
3589 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303590 int result;
3591 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003592
Dolev Ravive9d501b2014-07-01 12:22:37 +03003593 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3594 lrbp = &hba->lrb[index];
3595 cmd = lrbp->cmd;
3596 if (cmd) {
3597 result = ufshcd_transfer_rsp_status(hba, lrbp);
3598 scsi_dma_unmap(cmd);
3599 cmd->result = result;
3600 /* Mark completed command as NULL in LRB */
3601 lrbp->cmd = NULL;
3602 clear_bit_unlock(index, &hba->lrb_in_use);
3603 /* Do not touch lrbp after scsi done */
3604 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003605 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003606 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3607 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003608 if (hba->dev_cmd.complete)
3609 complete(hba->dev_cmd.complete);
3610 }
3611 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303612
3613 /* clear corresponding bits of completed commands */
3614 hba->outstanding_reqs ^= completed_reqs;
3615
Sahitya Tummala856b3482014-09-25 15:32:34 +03003616 ufshcd_clk_scaling_update_busy(hba);
3617
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303618 /* we might have free'd some tags above */
3619 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303620}
3621
3622/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003623 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3624 * @hba: per adapter instance
3625 */
3626static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3627{
3628 unsigned long completed_reqs;
3629 u32 tr_doorbell;
3630
3631 /* Resetting interrupt aggregation counters first and reading the
3632 * DOOR_BELL afterward allows us to handle all the completed requests.
3633 * In order to prevent other interrupts starvation the DB is read once
3634 * after reset. The down side of this solution is the possibility of
3635 * false interrupt if device completes another request after resetting
3636 * aggregation and before reading the DB.
3637 */
3638 if (ufshcd_is_intr_aggr_allowed(hba))
3639 ufshcd_reset_intr_aggr(hba);
3640
3641 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3642 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3643
3644 __ufshcd_transfer_req_compl(hba, completed_reqs);
3645}
3646
3647/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303648 * ufshcd_disable_ee - disable exception event
3649 * @hba: per-adapter instance
3650 * @mask: exception event to disable
3651 *
3652 * Disables exception event in the device so that the EVENT_ALERT
3653 * bit is not set.
3654 *
3655 * Returns zero on success, non-zero error value on failure.
3656 */
3657static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3658{
3659 int err = 0;
3660 u32 val;
3661
3662 if (!(hba->ee_ctrl_mask & mask))
3663 goto out;
3664
3665 val = hba->ee_ctrl_mask & ~mask;
3666 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003667 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303668 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3669 if (!err)
3670 hba->ee_ctrl_mask &= ~mask;
3671out:
3672 return err;
3673}
3674
3675/**
3676 * ufshcd_enable_ee - enable exception event
3677 * @hba: per-adapter instance
3678 * @mask: exception event to enable
3679 *
3680 * Enable corresponding exception event in the device to allow
3681 * device to alert host in critical scenarios.
3682 *
3683 * Returns zero on success, non-zero error value on failure.
3684 */
3685static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3686{
3687 int err = 0;
3688 u32 val;
3689
3690 if (hba->ee_ctrl_mask & mask)
3691 goto out;
3692
3693 val = hba->ee_ctrl_mask | mask;
3694 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003695 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303696 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3697 if (!err)
3698 hba->ee_ctrl_mask |= mask;
3699out:
3700 return err;
3701}
3702
3703/**
3704 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3705 * @hba: per-adapter instance
3706 *
3707 * Allow device to manage background operations on its own. Enabling
3708 * this might lead to inconsistent latencies during normal data transfers
3709 * as the device is allowed to manage its own way of handling background
3710 * operations.
3711 *
3712 * Returns zero on success, non-zero on failure.
3713 */
3714static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3715{
3716 int err = 0;
3717
3718 if (hba->auto_bkops_enabled)
3719 goto out;
3720
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003721 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303722 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3723 if (err) {
3724 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3725 __func__, err);
3726 goto out;
3727 }
3728
3729 hba->auto_bkops_enabled = true;
3730
3731 /* No need of URGENT_BKOPS exception from the device */
3732 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3733 if (err)
3734 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3735 __func__, err);
3736out:
3737 return err;
3738}
3739
3740/**
3741 * ufshcd_disable_auto_bkops - block device in doing background operations
3742 * @hba: per-adapter instance
3743 *
3744 * Disabling background operations improves command response latency but
3745 * has drawback of device moving into critical state where the device is
3746 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3747 * host is idle so that BKOPS are managed effectively without any negative
3748 * impacts.
3749 *
3750 * Returns zero on success, non-zero on failure.
3751 */
3752static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3753{
3754 int err = 0;
3755
3756 if (!hba->auto_bkops_enabled)
3757 goto out;
3758
3759 /*
3760 * If host assisted BKOPs is to be enabled, make sure
3761 * urgent bkops exception is allowed.
3762 */
3763 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3764 if (err) {
3765 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3766 __func__, err);
3767 goto out;
3768 }
3769
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003770 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303771 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3772 if (err) {
3773 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3774 __func__, err);
3775 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3776 goto out;
3777 }
3778
3779 hba->auto_bkops_enabled = false;
3780out:
3781 return err;
3782}
3783
3784/**
3785 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3786 * @hba: per adapter instance
3787 *
3788 * After a device reset the device may toggle the BKOPS_EN flag
3789 * to default value. The s/w tracking variables should be updated
3790 * as well. Do this by forcing enable of auto bkops.
3791 */
3792static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3793{
3794 hba->auto_bkops_enabled = false;
3795 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3796 ufshcd_enable_auto_bkops(hba);
3797}
3798
3799static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3800{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003801 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303802 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3803}
3804
3805/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003806 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3807 * @hba: per-adapter instance
3808 * @status: bkops_status value
3809 *
3810 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3811 * flag in the device to permit background operations if the device
3812 * bkops_status is greater than or equal to "status" argument passed to
3813 * this function, disable otherwise.
3814 *
3815 * Returns 0 for success, non-zero in case of failure.
3816 *
3817 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3818 * to know whether auto bkops is enabled or disabled after this function
3819 * returns control to it.
3820 */
3821static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3822 enum bkops_status status)
3823{
3824 int err;
3825 u32 curr_status = 0;
3826
3827 err = ufshcd_get_bkops_status(hba, &curr_status);
3828 if (err) {
3829 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3830 __func__, err);
3831 goto out;
3832 } else if (curr_status > BKOPS_STATUS_MAX) {
3833 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3834 __func__, curr_status);
3835 err = -EINVAL;
3836 goto out;
3837 }
3838
3839 if (curr_status >= status)
3840 err = ufshcd_enable_auto_bkops(hba);
3841 else
3842 err = ufshcd_disable_auto_bkops(hba);
3843out:
3844 return err;
3845}
3846
3847/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303848 * ufshcd_urgent_bkops - handle urgent bkops exception event
3849 * @hba: per-adapter instance
3850 *
3851 * Enable fBackgroundOpsEn flag in the device to permit background
3852 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003853 *
3854 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3855 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303856 */
3857static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3858{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003859 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303860}
3861
3862static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3863{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003864 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303865 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3866}
3867
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003868static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3869{
3870 int err;
3871 u32 curr_status = 0;
3872
3873 if (hba->is_urgent_bkops_lvl_checked)
3874 goto enable_auto_bkops;
3875
3876 err = ufshcd_get_bkops_status(hba, &curr_status);
3877 if (err) {
3878 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3879 __func__, err);
3880 goto out;
3881 }
3882
3883 /*
3884 * We are seeing that some devices are raising the urgent bkops
3885 * exception events even when BKOPS status doesn't indicate performace
3886 * impacted or critical. Handle these device by determining their urgent
3887 * bkops status at runtime.
3888 */
3889 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
3890 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
3891 __func__, curr_status);
3892 /* update the current status as the urgent bkops level */
3893 hba->urgent_bkops_lvl = curr_status;
3894 hba->is_urgent_bkops_lvl_checked = true;
3895 }
3896
3897enable_auto_bkops:
3898 err = ufshcd_enable_auto_bkops(hba);
3899out:
3900 if (err < 0)
3901 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3902 __func__, err);
3903}
3904
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303905/**
3906 * ufshcd_exception_event_handler - handle exceptions raised by device
3907 * @work: pointer to work data
3908 *
3909 * Read bExceptionEventStatus attribute from the device and handle the
3910 * exception event accordingly.
3911 */
3912static void ufshcd_exception_event_handler(struct work_struct *work)
3913{
3914 struct ufs_hba *hba;
3915 int err;
3916 u32 status = 0;
3917 hba = container_of(work, struct ufs_hba, eeh_work);
3918
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303919 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303920 err = ufshcd_get_ee_status(hba, &status);
3921 if (err) {
3922 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3923 __func__, err);
3924 goto out;
3925 }
3926
3927 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003928
3929 if (status & MASK_EE_URGENT_BKOPS)
3930 ufshcd_bkops_exception_event_handler(hba);
3931
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303932out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303933 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303934 return;
3935}
3936
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003937/* Complete requests that have door-bell cleared */
3938static void ufshcd_complete_requests(struct ufs_hba *hba)
3939{
3940 ufshcd_transfer_req_compl(hba);
3941 ufshcd_tmc_handler(hba);
3942}
3943
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303944/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02003945 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
3946 * to recover from the DL NAC errors or not.
3947 * @hba: per-adapter instance
3948 *
3949 * Returns true if error handling is required, false otherwise
3950 */
3951static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
3952{
3953 unsigned long flags;
3954 bool err_handling = true;
3955
3956 spin_lock_irqsave(hba->host->host_lock, flags);
3957 /*
3958 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
3959 * device fatal error and/or DL NAC & REPLAY timeout errors.
3960 */
3961 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
3962 goto out;
3963
3964 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
3965 ((hba->saved_err & UIC_ERROR) &&
3966 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
3967 goto out;
3968
3969 if ((hba->saved_err & UIC_ERROR) &&
3970 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
3971 int err;
3972 /*
3973 * wait for 50ms to see if we can get any other errors or not.
3974 */
3975 spin_unlock_irqrestore(hba->host->host_lock, flags);
3976 msleep(50);
3977 spin_lock_irqsave(hba->host->host_lock, flags);
3978
3979 /*
3980 * now check if we have got any other severe errors other than
3981 * DL NAC error?
3982 */
3983 if ((hba->saved_err & INT_FATAL_ERRORS) ||
3984 ((hba->saved_err & UIC_ERROR) &&
3985 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
3986 goto out;
3987
3988 /*
3989 * As DL NAC is the only error received so far, send out NOP
3990 * command to confirm if link is still active or not.
3991 * - If we don't get any response then do error recovery.
3992 * - If we get response then clear the DL NAC error bit.
3993 */
3994
3995 spin_unlock_irqrestore(hba->host->host_lock, flags);
3996 err = ufshcd_verify_dev_init(hba);
3997 spin_lock_irqsave(hba->host->host_lock, flags);
3998
3999 if (err)
4000 goto out;
4001
4002 /* Link seems to be alive hence ignore the DL NAC errors */
4003 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4004 hba->saved_err &= ~UIC_ERROR;
4005 /* clear NAC error */
4006 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4007 if (!hba->saved_uic_err) {
4008 err_handling = false;
4009 goto out;
4010 }
4011 }
4012out:
4013 spin_unlock_irqrestore(hba->host->host_lock, flags);
4014 return err_handling;
4015}
4016
4017/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304018 * ufshcd_err_handler - handle UFS errors that require s/w attention
4019 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304020 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304021static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304022{
4023 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304024 unsigned long flags;
4025 u32 err_xfer = 0;
4026 u32 err_tm = 0;
4027 int err = 0;
4028 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004029 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304030
4031 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304032
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304033 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004034 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304035
4036 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004037 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304038 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304039
4040 hba->ufshcd_state = UFSHCD_STATE_RESET;
4041 ufshcd_set_eh_in_progress(hba);
4042
4043 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004044 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004045
4046 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4047 bool ret;
4048
4049 spin_unlock_irqrestore(hba->host->host_lock, flags);
4050 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4051 ret = ufshcd_quirk_dl_nac_errors(hba);
4052 spin_lock_irqsave(hba->host->host_lock, flags);
4053 if (!ret)
4054 goto skip_err_handling;
4055 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004056 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4057 ((hba->saved_err & UIC_ERROR) &&
4058 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4059 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4060 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4061 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304062
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004063 /*
4064 * if host reset is required then skip clearing the pending
4065 * transfers forcefully because they will automatically get
4066 * cleared after link startup.
4067 */
4068 if (needs_reset)
4069 goto skip_pending_xfer_clear;
4070
4071 /* release lock as clear command might sleep */
4072 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304073 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004074 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4075 if (ufshcd_clear_cmd(hba, tag)) {
4076 err_xfer = true;
4077 goto lock_skip_pending_xfer_clear;
4078 }
4079 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304080
4081 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004082 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4083 if (ufshcd_clear_tm_cmd(hba, tag)) {
4084 err_tm = true;
4085 goto lock_skip_pending_xfer_clear;
4086 }
4087 }
4088
4089lock_skip_pending_xfer_clear:
4090 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304091
4092 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004093 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304094
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004095 if (err_xfer || err_tm)
4096 needs_reset = true;
4097
4098skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304099 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004100 if (needs_reset) {
4101 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4102
4103 /*
4104 * ufshcd_reset_and_restore() does the link reinitialization
4105 * which will need atleast one empty doorbell slot to send the
4106 * device management commands (NOP and query commands).
4107 * If there is no slot empty at this moment then free up last
4108 * slot forcefully.
4109 */
4110 if (hba->outstanding_reqs == max_doorbells)
4111 __ufshcd_transfer_req_compl(hba,
4112 (1UL << (hba->nutrs - 1)));
4113
4114 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304115 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004116 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304117 if (err) {
4118 dev_err(hba->dev, "%s: reset and restore failed\n",
4119 __func__);
4120 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4121 }
4122 /*
4123 * Inform scsi mid-layer that we did reset and allow to handle
4124 * Unit Attention properly.
4125 */
4126 scsi_report_bus_reset(hba->host, 0);
4127 hba->saved_err = 0;
4128 hba->saved_uic_err = 0;
4129 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004130
Yaniv Gardi583fa622016-03-10 17:37:13 +02004131skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004132 if (!needs_reset) {
4133 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4134 if (hba->saved_err || hba->saved_uic_err)
4135 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4136 __func__, hba->saved_err, hba->saved_uic_err);
4137 }
4138
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304139 ufshcd_clear_eh_in_progress(hba);
4140
4141out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004142 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304143 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004144 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304145 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304146}
4147
4148/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304149 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4150 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304151 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304152static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304153{
4154 u32 reg;
4155
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304156 /* PA_INIT_ERROR is fatal and needs UIC reset */
4157 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4158 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4159 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004160 else if (hba->dev_quirks &
4161 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4162 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4163 hba->uic_error |=
4164 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4165 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4166 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4167 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304168
4169 /* UIC NL/TL/DME errors needs software retry */
4170 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4171 if (reg)
4172 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4173
4174 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4175 if (reg)
4176 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4177
4178 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4179 if (reg)
4180 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4181
4182 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4183 __func__, hba->uic_error);
4184}
4185
4186/**
4187 * ufshcd_check_errors - Check for errors that need s/w attention
4188 * @hba: per-adapter instance
4189 */
4190static void ufshcd_check_errors(struct ufs_hba *hba)
4191{
4192 bool queue_eh_work = false;
4193
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304194 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304195 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304196
4197 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304198 hba->uic_error = 0;
4199 ufshcd_update_uic_error(hba);
4200 if (hba->uic_error)
4201 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304202 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304203
4204 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004205 /*
4206 * update the transfer error masks to sticky bits, let's do this
4207 * irrespective of current ufshcd_state.
4208 */
4209 hba->saved_err |= hba->errors;
4210 hba->saved_uic_err |= hba->uic_error;
4211
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304212 /* handle fatal errors only when link is functional */
4213 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4214 /* block commands from scsi mid-layer */
4215 scsi_block_requests(hba->host);
4216
Zang Leigang141f8162016-11-16 11:29:37 +08004217 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304218 schedule_work(&hba->eh_work);
4219 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304220 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304221 /*
4222 * if (!queue_eh_work) -
4223 * Other errors are either non-fatal where host recovers
4224 * itself without s/w intervention or errors that will be
4225 * handled by the SCSI core layer.
4226 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304227}
4228
4229/**
4230 * ufshcd_tmc_handler - handle task management function completion
4231 * @hba: per adapter instance
4232 */
4233static void ufshcd_tmc_handler(struct ufs_hba *hba)
4234{
4235 u32 tm_doorbell;
4236
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304237 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304238 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304239 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304240}
4241
4242/**
4243 * ufshcd_sl_intr - Interrupt service routine
4244 * @hba: per adapter instance
4245 * @intr_status: contains interrupts generated by the controller
4246 */
4247static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4248{
4249 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4250 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304251 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304252
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304253 if (intr_status & UFSHCD_UIC_MASK)
4254 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304255
4256 if (intr_status & UTP_TASK_REQ_COMPL)
4257 ufshcd_tmc_handler(hba);
4258
4259 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4260 ufshcd_transfer_req_compl(hba);
4261}
4262
4263/**
4264 * ufshcd_intr - Main interrupt service routine
4265 * @irq: irq number
4266 * @__hba: pointer to adapter instance
4267 *
4268 * Returns IRQ_HANDLED - If interrupt is valid
4269 * IRQ_NONE - If invalid interrupt
4270 */
4271static irqreturn_t ufshcd_intr(int irq, void *__hba)
4272{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004273 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304274 irqreturn_t retval = IRQ_NONE;
4275 struct ufs_hba *hba = __hba;
4276
4277 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304278 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004279 enabled_intr_status =
4280 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304281
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004282 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304283 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004284
4285 if (enabled_intr_status) {
4286 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304287 retval = IRQ_HANDLED;
4288 }
4289 spin_unlock(hba->host->host_lock);
4290 return retval;
4291}
4292
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304293static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4294{
4295 int err = 0;
4296 u32 mask = 1 << tag;
4297 unsigned long flags;
4298
4299 if (!test_bit(tag, &hba->outstanding_tasks))
4300 goto out;
4301
4302 spin_lock_irqsave(hba->host->host_lock, flags);
4303 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4304 spin_unlock_irqrestore(hba->host->host_lock, flags);
4305
4306 /* poll for max. 1 sec to clear door bell register by h/w */
4307 err = ufshcd_wait_for_register(hba,
4308 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004309 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304310out:
4311 return err;
4312}
4313
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304314/**
4315 * ufshcd_issue_tm_cmd - issues task management commands to controller
4316 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304317 * @lun_id: LUN ID to which TM command is sent
4318 * @task_id: task ID to which the TM command is applicable
4319 * @tm_function: task management function opcode
4320 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304321 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304322 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304323 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304324static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4325 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304326{
4327 struct utp_task_req_desc *task_req_descp;
4328 struct utp_upiu_task_req *task_req_upiup;
4329 struct Scsi_Host *host;
4330 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304331 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304332 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304333 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304334
4335 host = hba->host;
4336
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304337 /*
4338 * Get free slot, sleep if slots are unavailable.
4339 * Even though we use wait_event() which sleeps indefinitely,
4340 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4341 */
4342 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004343 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304344
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304345 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304346 task_req_descp = hba->utmrdl_base_addr;
4347 task_req_descp += free_slot;
4348
4349 /* Configure task request descriptor */
4350 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4351 task_req_descp->header.dword_2 =
4352 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4353
4354 /* Configure task request UPIU */
4355 task_req_upiup =
4356 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304357 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304358 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304359 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304360 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304361 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304362 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004363 /*
4364 * The host shall provide the same value for LUN field in the basic
4365 * header and for Input Parameter.
4366 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304367 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4368 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304369
Kiwoong Kimd2877be2016-11-10 21:16:15 +09004370 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
4371
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304372 /* send command to the controller */
4373 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004374
4375 /* Make sure descriptors are ready before ringing the task doorbell */
4376 wmb();
4377
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304378 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004379 /* Make sure that doorbell is committed immediately */
4380 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304381
4382 spin_unlock_irqrestore(host->host_lock, flags);
4383
4384 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304385 err = wait_event_timeout(hba->tm_wq,
4386 test_bit(free_slot, &hba->tm_condition),
4387 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304388 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304389 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4390 __func__, tm_function);
4391 if (ufshcd_clear_tm_cmd(hba, free_slot))
4392 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4393 __func__, free_slot);
4394 err = -ETIMEDOUT;
4395 } else {
4396 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304397 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304398
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304399 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304400 ufshcd_put_tm_slot(hba, free_slot);
4401 wake_up(&hba->tm_tag_wq);
4402
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004403 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304404 return err;
4405}
4406
4407/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304408 * ufshcd_eh_device_reset_handler - device reset handler registered to
4409 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304410 * @cmd: SCSI command pointer
4411 *
4412 * Returns SUCCESS/FAILED
4413 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304414static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304415{
4416 struct Scsi_Host *host;
4417 struct ufs_hba *hba;
4418 unsigned int tag;
4419 u32 pos;
4420 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304421 u8 resp = 0xF;
4422 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304423 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304424
4425 host = cmd->device->host;
4426 hba = shost_priv(host);
4427 tag = cmd->request->tag;
4428
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304429 lrbp = &hba->lrb[tag];
4430 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4431 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304432 if (!err)
4433 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304434 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304435 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304436
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304437 /* clear the commands that were pending for corresponding LUN */
4438 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4439 if (hba->lrb[pos].lun == lrbp->lun) {
4440 err = ufshcd_clear_cmd(hba, pos);
4441 if (err)
4442 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304443 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304444 }
4445 spin_lock_irqsave(host->host_lock, flags);
4446 ufshcd_transfer_req_compl(hba);
4447 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304448out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304449 if (!err) {
4450 err = SUCCESS;
4451 } else {
4452 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4453 err = FAILED;
4454 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304455 return err;
4456}
4457
4458/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304459 * ufshcd_abort - abort a specific command
4460 * @cmd: SCSI command pointer
4461 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304462 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4463 * command, and in host controller by clearing the door-bell register. There can
4464 * be race between controller sending the command to the device while abort is
4465 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4466 * really issued and then try to abort it.
4467 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304468 * Returns SUCCESS/FAILED
4469 */
4470static int ufshcd_abort(struct scsi_cmnd *cmd)
4471{
4472 struct Scsi_Host *host;
4473 struct ufs_hba *hba;
4474 unsigned long flags;
4475 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304476 int err = 0;
4477 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304478 u8 resp = 0xF;
4479 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004480 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304481
4482 host = cmd->device->host;
4483 hba = shost_priv(host);
4484 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004485 if (!ufshcd_valid_tag(hba, tag)) {
4486 dev_err(hba->dev,
4487 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4488 __func__, tag, cmd, cmd->request);
4489 BUG();
4490 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304491
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004492 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004493 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004494 /* If command is already aborted/completed, return SUCCESS */
4495 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4496 dev_err(hba->dev,
4497 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4498 __func__, tag, hba->outstanding_reqs, reg);
4499 goto out;
4500 }
4501
Dolev Ravive9d501b2014-07-01 12:22:37 +03004502 if (!(reg & (1 << tag))) {
4503 dev_err(hba->dev,
4504 "%s: cmd was completed, but without a notifying intr, tag = %d",
4505 __func__, tag);
4506 }
4507
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304508 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304509 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4510 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4511 UFS_QUERY_TASK, &resp);
4512 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4513 /* cmd pending in the device */
4514 break;
4515 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304516 /*
4517 * cmd not pending in the device, check if it is
4518 * in transition.
4519 */
4520 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4521 if (reg & (1 << tag)) {
4522 /* sleep for max. 200us to stabilize */
4523 usleep_range(100, 200);
4524 continue;
4525 }
4526 /* command completed already */
4527 goto out;
4528 } else {
4529 if (!err)
4530 err = resp; /* service response error */
4531 goto out;
4532 }
4533 }
4534
4535 if (!poll_cnt) {
4536 err = -EBUSY;
4537 goto out;
4538 }
4539
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304540 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4541 UFS_ABORT_TASK, &resp);
4542 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304543 if (!err)
4544 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304545 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304546 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304547
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304548 err = ufshcd_clear_cmd(hba, tag);
4549 if (err)
4550 goto out;
4551
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304552 scsi_dma_unmap(cmd);
4553
4554 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004555 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304556 hba->lrb[tag].cmd = NULL;
4557 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304558
4559 clear_bit_unlock(tag, &hba->lrb_in_use);
4560 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004561
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304562out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304563 if (!err) {
4564 err = SUCCESS;
4565 } else {
4566 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4567 err = FAILED;
4568 }
4569
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004570 /*
4571 * This ufshcd_release() corresponds to the original scsi cmd that got
4572 * aborted here (as we won't get any IRQ for it).
4573 */
4574 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304575 return err;
4576}
4577
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304578/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304579 * ufshcd_host_reset_and_restore - reset and restore host controller
4580 * @hba: per-adapter instance
4581 *
4582 * Note that host controller reset may issue DME_RESET to
4583 * local and remote (device) Uni-Pro stack and the attributes
4584 * are reset to default state.
4585 *
4586 * Returns zero on success, non-zero on failure
4587 */
4588static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4589{
4590 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304591 unsigned long flags;
4592
4593 /* Reset the host controller */
4594 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004595 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304596 spin_unlock_irqrestore(hba->host->host_lock, flags);
4597
4598 err = ufshcd_hba_enable(hba);
4599 if (err)
4600 goto out;
4601
4602 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004603 err = ufshcd_probe_hba(hba);
4604
4605 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304606 err = -EIO;
4607out:
4608 if (err)
4609 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4610
4611 return err;
4612}
4613
4614/**
4615 * ufshcd_reset_and_restore - reset and re-initialize host/device
4616 * @hba: per-adapter instance
4617 *
4618 * Reset and recover device, host and re-establish link. This
4619 * is helpful to recover the communication in fatal error conditions.
4620 *
4621 * Returns zero on success, non-zero on failure
4622 */
4623static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4624{
4625 int err = 0;
4626 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004627 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304628
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004629 do {
4630 err = ufshcd_host_reset_and_restore(hba);
4631 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304632
4633 /*
4634 * After reset the door-bell might be cleared, complete
4635 * outstanding requests in s/w here.
4636 */
4637 spin_lock_irqsave(hba->host->host_lock, flags);
4638 ufshcd_transfer_req_compl(hba);
4639 ufshcd_tmc_handler(hba);
4640 spin_unlock_irqrestore(hba->host->host_lock, flags);
4641
4642 return err;
4643}
4644
4645/**
4646 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4647 * @cmd - SCSI command pointer
4648 *
4649 * Returns SUCCESS/FAILED
4650 */
4651static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4652{
4653 int err;
4654 unsigned long flags;
4655 struct ufs_hba *hba;
4656
4657 hba = shost_priv(cmd->device->host);
4658
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004659 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304660 /*
4661 * Check if there is any race with fatal error handling.
4662 * If so, wait for it to complete. Even though fatal error
4663 * handling does reset and restore in some cases, don't assume
4664 * anything out of it. We are just avoiding race here.
4665 */
4666 do {
4667 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304668 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304669 hba->ufshcd_state == UFSHCD_STATE_RESET))
4670 break;
4671 spin_unlock_irqrestore(hba->host->host_lock, flags);
4672 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304673 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304674 } while (1);
4675
4676 hba->ufshcd_state = UFSHCD_STATE_RESET;
4677 ufshcd_set_eh_in_progress(hba);
4678 spin_unlock_irqrestore(hba->host->host_lock, flags);
4679
4680 err = ufshcd_reset_and_restore(hba);
4681
4682 spin_lock_irqsave(hba->host->host_lock, flags);
4683 if (!err) {
4684 err = SUCCESS;
4685 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4686 } else {
4687 err = FAILED;
4688 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4689 }
4690 ufshcd_clear_eh_in_progress(hba);
4691 spin_unlock_irqrestore(hba->host->host_lock, flags);
4692
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004693 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304694 return err;
4695}
4696
4697/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004698 * ufshcd_get_max_icc_level - calculate the ICC level
4699 * @sup_curr_uA: max. current supported by the regulator
4700 * @start_scan: row at the desc table to start scan from
4701 * @buff: power descriptor buffer
4702 *
4703 * Returns calculated max ICC level for specific regulator
4704 */
4705static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4706{
4707 int i;
4708 int curr_uA;
4709 u16 data;
4710 u16 unit;
4711
4712 for (i = start_scan; i >= 0; i--) {
4713 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4714 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4715 ATTR_ICC_LVL_UNIT_OFFSET;
4716 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4717 switch (unit) {
4718 case UFSHCD_NANO_AMP:
4719 curr_uA = curr_uA / 1000;
4720 break;
4721 case UFSHCD_MILI_AMP:
4722 curr_uA = curr_uA * 1000;
4723 break;
4724 case UFSHCD_AMP:
4725 curr_uA = curr_uA * 1000 * 1000;
4726 break;
4727 case UFSHCD_MICRO_AMP:
4728 default:
4729 break;
4730 }
4731 if (sup_curr_uA >= curr_uA)
4732 break;
4733 }
4734 if (i < 0) {
4735 i = 0;
4736 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4737 }
4738
4739 return (u32)i;
4740}
4741
4742/**
4743 * ufshcd_calc_icc_level - calculate the max ICC level
4744 * In case regulators are not initialized we'll return 0
4745 * @hba: per-adapter instance
4746 * @desc_buf: power descriptor buffer to extract ICC levels from.
4747 * @len: length of desc_buff
4748 *
4749 * Returns calculated ICC level
4750 */
4751static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4752 u8 *desc_buf, int len)
4753{
4754 u32 icc_level = 0;
4755
4756 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4757 !hba->vreg_info.vccq2) {
4758 dev_err(hba->dev,
4759 "%s: Regulator capability was not set, actvIccLevel=%d",
4760 __func__, icc_level);
4761 goto out;
4762 }
4763
4764 if (hba->vreg_info.vcc)
4765 icc_level = ufshcd_get_max_icc_level(
4766 hba->vreg_info.vcc->max_uA,
4767 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4768 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4769
4770 if (hba->vreg_info.vccq)
4771 icc_level = ufshcd_get_max_icc_level(
4772 hba->vreg_info.vccq->max_uA,
4773 icc_level,
4774 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4775
4776 if (hba->vreg_info.vccq2)
4777 icc_level = ufshcd_get_max_icc_level(
4778 hba->vreg_info.vccq2->max_uA,
4779 icc_level,
4780 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4781out:
4782 return icc_level;
4783}
4784
4785static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4786{
4787 int ret;
4788 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4789 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4790
4791 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4792 if (ret) {
4793 dev_err(hba->dev,
4794 "%s: Failed reading power descriptor.len = %d ret = %d",
4795 __func__, buff_len, ret);
4796 return;
4797 }
4798
4799 hba->init_prefetch_data.icc_level =
4800 ufshcd_find_max_sup_active_icc_level(hba,
4801 desc_buf, buff_len);
4802 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4803 __func__, hba->init_prefetch_data.icc_level);
4804
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004805 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4806 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4807 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004808
4809 if (ret)
4810 dev_err(hba->dev,
4811 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4812 __func__, hba->init_prefetch_data.icc_level , ret);
4813
4814}
4815
4816/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004817 * ufshcd_scsi_add_wlus - Adds required W-LUs
4818 * @hba: per-adapter instance
4819 *
4820 * UFS device specification requires the UFS devices to support 4 well known
4821 * logical units:
4822 * "REPORT_LUNS" (address: 01h)
4823 * "UFS Device" (address: 50h)
4824 * "RPMB" (address: 44h)
4825 * "BOOT" (address: 30h)
4826 * UFS device's power management needs to be controlled by "POWER CONDITION"
4827 * field of SSU (START STOP UNIT) command. But this "power condition" field
4828 * will take effect only when its sent to "UFS device" well known logical unit
4829 * hence we require the scsi_device instance to represent this logical unit in
4830 * order for the UFS host driver to send the SSU command for power management.
4831
4832 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4833 * Block) LU so user space process can control this LU. User space may also
4834 * want to have access to BOOT LU.
4835
4836 * This function adds scsi device instances for each of all well known LUs
4837 * (except "REPORT LUNS" LU).
4838 *
4839 * Returns zero on success (all required W-LUs are added successfully),
4840 * non-zero error value on failure (if failed to add any of the required W-LU).
4841 */
4842static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4843{
4844 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004845 struct scsi_device *sdev_rpmb;
4846 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004847
4848 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4849 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4850 if (IS_ERR(hba->sdev_ufs_device)) {
4851 ret = PTR_ERR(hba->sdev_ufs_device);
4852 hba->sdev_ufs_device = NULL;
4853 goto out;
4854 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004855 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004856
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004857 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004858 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004859 if (IS_ERR(sdev_boot)) {
4860 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004861 goto remove_sdev_ufs_device;
4862 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004863 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004864
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004865 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004866 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004867 if (IS_ERR(sdev_rpmb)) {
4868 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004869 goto remove_sdev_boot;
4870 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004871 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004872 goto out;
4873
4874remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004875 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004876remove_sdev_ufs_device:
4877 scsi_remove_device(hba->sdev_ufs_device);
4878out:
4879 return ret;
4880}
4881
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004882static int ufs_get_device_info(struct ufs_hba *hba,
4883 struct ufs_device_info *card_data)
4884{
4885 int err;
4886 u8 model_index;
4887 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
4888 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
4889
4890 err = ufshcd_read_device_desc(hba, desc_buf,
4891 QUERY_DESC_DEVICE_MAX_SIZE);
4892 if (err) {
4893 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4894 __func__, err);
4895 goto out;
4896 }
4897
4898 /*
4899 * getting vendor (manufacturerID) and Bank Index in big endian
4900 * format
4901 */
4902 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
4903 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4904
4905 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4906
4907 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
4908 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
4909 if (err) {
4910 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
4911 __func__, err);
4912 goto out;
4913 }
4914
4915 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
4916 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
4917 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
4918 MAX_MODEL_LEN));
4919
4920 /* Null terminate the model string */
4921 card_data->model[MAX_MODEL_LEN] = '\0';
4922
4923out:
4924 return err;
4925}
4926
4927void ufs_advertise_fixup_device(struct ufs_hba *hba)
4928{
4929 int err;
4930 struct ufs_dev_fix *f;
4931 struct ufs_device_info card_data;
4932
4933 card_data.wmanufacturerid = 0;
4934
4935 err = ufs_get_device_info(hba, &card_data);
4936 if (err) {
4937 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
4938 __func__, err);
4939 return;
4940 }
4941
4942 for (f = ufs_fixups; f->quirk; f++) {
4943 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
4944 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
4945 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
4946 !strcmp(f->card.model, UFS_ANY_MODEL)))
4947 hba->dev_quirks |= f->quirk;
4948 }
4949}
4950
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004951/**
Yaniv Gardi37113102016-03-10 17:37:16 +02004952 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
4953 * @hba: per-adapter instance
4954 *
4955 * PA_TActivate parameter can be tuned manually if UniPro version is less than
4956 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
4957 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
4958 * the hibern8 exit latency.
4959 *
4960 * Returns zero on success, non-zero error value on failure.
4961 */
4962static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
4963{
4964 int ret = 0;
4965 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
4966
4967 ret = ufshcd_dme_peer_get(hba,
4968 UIC_ARG_MIB_SEL(
4969 RX_MIN_ACTIVATETIME_CAPABILITY,
4970 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
4971 &peer_rx_min_activatetime);
4972 if (ret)
4973 goto out;
4974
4975 /* make sure proper unit conversion is applied */
4976 tuned_pa_tactivate =
4977 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
4978 / PA_TACTIVATE_TIME_UNIT_US);
4979 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
4980 tuned_pa_tactivate);
4981
4982out:
4983 return ret;
4984}
4985
4986/**
4987 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
4988 * @hba: per-adapter instance
4989 *
4990 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
4991 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
4992 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
4993 * This optimal value can help reduce the hibern8 exit latency.
4994 *
4995 * Returns zero on success, non-zero error value on failure.
4996 */
4997static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
4998{
4999 int ret = 0;
5000 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5001 u32 max_hibern8_time, tuned_pa_hibern8time;
5002
5003 ret = ufshcd_dme_get(hba,
5004 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5005 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5006 &local_tx_hibern8_time_cap);
5007 if (ret)
5008 goto out;
5009
5010 ret = ufshcd_dme_peer_get(hba,
5011 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5012 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5013 &peer_rx_hibern8_time_cap);
5014 if (ret)
5015 goto out;
5016
5017 max_hibern8_time = max(local_tx_hibern8_time_cap,
5018 peer_rx_hibern8_time_cap);
5019 /* make sure proper unit conversion is applied */
5020 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5021 / PA_HIBERN8_TIME_UNIT_US);
5022 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5023 tuned_pa_hibern8time);
5024out:
5025 return ret;
5026}
5027
5028static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5029{
5030 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5031 ufshcd_tune_pa_tactivate(hba);
5032 ufshcd_tune_pa_hibern8time(hba);
5033 }
5034
5035 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5036 /* set 1ms timeout for PA_TACTIVATE */
5037 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
5038}
5039
5040/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005041 * ufshcd_probe_hba - probe hba to detect device and initialize
5042 * @hba: per-adapter instance
5043 *
5044 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305045 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005046static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305047{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305048 int ret;
5049
5050 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305051 if (ret)
5052 goto out;
5053
Yaniv Gardi50646362014-10-23 13:25:13 +03005054 ufshcd_init_pwr_info(hba);
5055
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005056 /* set the default level for urgent bkops */
5057 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5058 hba->is_urgent_bkops_lvl_checked = false;
5059
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005060 /* UniPro link is active now */
5061 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305062
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305063 ret = ufshcd_verify_dev_init(hba);
5064 if (ret)
5065 goto out;
5066
Dolev Raviv68078d52013-07-30 00:35:58 +05305067 ret = ufshcd_complete_dev_init(hba);
5068 if (ret)
5069 goto out;
5070
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005071 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005072 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005073
5074 ret = ufshcd_set_vccq_rail_unused(hba,
5075 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5076 if (ret)
5077 goto out;
5078
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005079 /* UFS device is also active now */
5080 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305081 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005082 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305083
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005084 if (ufshcd_get_max_pwr_mode(hba)) {
5085 dev_err(hba->dev,
5086 "%s: Failed getting max supported power mode\n",
5087 __func__);
5088 } else {
5089 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005090 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005091 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5092 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005093 goto out;
5094 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005095 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005096
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005097 /* set the state as operational after switching to desired gear */
5098 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005099 /*
5100 * If we are in error handling context or in power management callbacks
5101 * context, no need to scan the host
5102 */
5103 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5104 bool flag;
5105
5106 /* clear any previous UFS device information */
5107 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005108 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5109 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005110 hba->dev_info.f_power_on_wp_en = flag;
5111
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005112 if (!hba->is_init_prefetch)
5113 ufshcd_init_icc_levels(hba);
5114
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005115 /* Add required well known logical units to scsi mid layer */
5116 if (ufshcd_scsi_add_wlus(hba))
5117 goto out;
5118
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305119 scsi_scan_host(hba->host);
5120 pm_runtime_put_sync(hba->dev);
5121 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005122
5123 if (!hba->is_init_prefetch)
5124 hba->is_init_prefetch = true;
5125
Sahitya Tummala856b3482014-09-25 15:32:34 +03005126 /* Resume devfreq after UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07005127 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005128
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305129out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005130 /*
5131 * If we failed to initialize the device or the device is not
5132 * present, turn off the power/clocks etc.
5133 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005134 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5135 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005136 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005137 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005138
5139 return ret;
5140}
5141
5142/**
5143 * ufshcd_async_scan - asynchronous execution for probing hba
5144 * @data: data pointer to pass to this function
5145 * @cookie: cookie data
5146 */
5147static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5148{
5149 struct ufs_hba *hba = (struct ufs_hba *)data;
5150
5151 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305152}
5153
Yaniv Gardif550c652016-03-10 17:37:07 +02005154static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5155{
5156 unsigned long flags;
5157 struct Scsi_Host *host;
5158 struct ufs_hba *hba;
5159 int index;
5160 bool found = false;
5161
5162 if (!scmd || !scmd->device || !scmd->device->host)
5163 return BLK_EH_NOT_HANDLED;
5164
5165 host = scmd->device->host;
5166 hba = shost_priv(host);
5167 if (!hba)
5168 return BLK_EH_NOT_HANDLED;
5169
5170 spin_lock_irqsave(host->host_lock, flags);
5171
5172 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5173 if (hba->lrb[index].cmd == scmd) {
5174 found = true;
5175 break;
5176 }
5177 }
5178
5179 spin_unlock_irqrestore(host->host_lock, flags);
5180
5181 /*
5182 * Bypass SCSI error handling and reset the block layer timer if this
5183 * SCSI command was not actually dispatched to UFS driver, otherwise
5184 * let SCSI layer handle the error as usual.
5185 */
5186 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5187}
5188
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305189static struct scsi_host_template ufshcd_driver_template = {
5190 .module = THIS_MODULE,
5191 .name = UFSHCD,
5192 .proc_name = UFSHCD,
5193 .queuecommand = ufshcd_queuecommand,
5194 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005195 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305196 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005197 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305198 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305199 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5200 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005201 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305202 .this_id = -1,
5203 .sg_tablesize = SG_ALL,
5204 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5205 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005206 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005207 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305208};
5209
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005210static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5211 int ua)
5212{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005213 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005214
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005215 if (!vreg)
5216 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005217
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005218 ret = regulator_set_load(vreg->reg, ua);
5219 if (ret < 0) {
5220 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5221 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005222 }
5223
5224 return ret;
5225}
5226
5227static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5228 struct ufs_vreg *vreg)
5229{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005230 if (!vreg)
5231 return 0;
5232 else if (vreg->unused)
5233 return 0;
5234 else
5235 return ufshcd_config_vreg_load(hba->dev, vreg,
5236 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005237}
5238
5239static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5240 struct ufs_vreg *vreg)
5241{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005242 if (!vreg)
5243 return 0;
5244 else if (vreg->unused)
5245 return 0;
5246 else
5247 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005248}
5249
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005250static int ufshcd_config_vreg(struct device *dev,
5251 struct ufs_vreg *vreg, bool on)
5252{
5253 int ret = 0;
5254 struct regulator *reg = vreg->reg;
5255 const char *name = vreg->name;
5256 int min_uV, uA_load;
5257
5258 BUG_ON(!vreg);
5259
5260 if (regulator_count_voltages(reg) > 0) {
5261 min_uV = on ? vreg->min_uV : 0;
5262 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5263 if (ret) {
5264 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5265 __func__, name, ret);
5266 goto out;
5267 }
5268
5269 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005270 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5271 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005272 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005273 }
5274out:
5275 return ret;
5276}
5277
5278static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5279{
5280 int ret = 0;
5281
Yaniv Gardi60f01872016-03-10 17:37:11 +02005282 if (!vreg)
5283 goto out;
5284 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005285 goto out;
5286
5287 ret = ufshcd_config_vreg(dev, vreg, true);
5288 if (!ret)
5289 ret = regulator_enable(vreg->reg);
5290
5291 if (!ret)
5292 vreg->enabled = true;
5293 else
5294 dev_err(dev, "%s: %s enable failed, err=%d\n",
5295 __func__, vreg->name, ret);
5296out:
5297 return ret;
5298}
5299
5300static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5301{
5302 int ret = 0;
5303
Yaniv Gardi60f01872016-03-10 17:37:11 +02005304 if (!vreg)
5305 goto out;
5306 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005307 goto out;
5308
5309 ret = regulator_disable(vreg->reg);
5310
5311 if (!ret) {
5312 /* ignore errors on applying disable config */
5313 ufshcd_config_vreg(dev, vreg, false);
5314 vreg->enabled = false;
5315 } else {
5316 dev_err(dev, "%s: %s disable failed, err=%d\n",
5317 __func__, vreg->name, ret);
5318 }
5319out:
5320 return ret;
5321}
5322
5323static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5324{
5325 int ret = 0;
5326 struct device *dev = hba->dev;
5327 struct ufs_vreg_info *info = &hba->vreg_info;
5328
5329 if (!info)
5330 goto out;
5331
5332 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5333 if (ret)
5334 goto out;
5335
5336 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5337 if (ret)
5338 goto out;
5339
5340 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5341 if (ret)
5342 goto out;
5343
5344out:
5345 if (ret) {
5346 ufshcd_toggle_vreg(dev, info->vccq2, false);
5347 ufshcd_toggle_vreg(dev, info->vccq, false);
5348 ufshcd_toggle_vreg(dev, info->vcc, false);
5349 }
5350 return ret;
5351}
5352
Raviv Shvili6a771a62014-09-25 15:32:24 +03005353static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5354{
5355 struct ufs_vreg_info *info = &hba->vreg_info;
5356
5357 if (info)
5358 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5359
5360 return 0;
5361}
5362
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005363static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5364{
5365 int ret = 0;
5366
5367 if (!vreg)
5368 goto out;
5369
5370 vreg->reg = devm_regulator_get(dev, vreg->name);
5371 if (IS_ERR(vreg->reg)) {
5372 ret = PTR_ERR(vreg->reg);
5373 dev_err(dev, "%s: %s get failed, err=%d\n",
5374 __func__, vreg->name, ret);
5375 }
5376out:
5377 return ret;
5378}
5379
5380static int ufshcd_init_vreg(struct ufs_hba *hba)
5381{
5382 int ret = 0;
5383 struct device *dev = hba->dev;
5384 struct ufs_vreg_info *info = &hba->vreg_info;
5385
5386 if (!info)
5387 goto out;
5388
5389 ret = ufshcd_get_vreg(dev, info->vcc);
5390 if (ret)
5391 goto out;
5392
5393 ret = ufshcd_get_vreg(dev, info->vccq);
5394 if (ret)
5395 goto out;
5396
5397 ret = ufshcd_get_vreg(dev, info->vccq2);
5398out:
5399 return ret;
5400}
5401
Raviv Shvili6a771a62014-09-25 15:32:24 +03005402static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5403{
5404 struct ufs_vreg_info *info = &hba->vreg_info;
5405
5406 if (info)
5407 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5408
5409 return 0;
5410}
5411
Yaniv Gardi60f01872016-03-10 17:37:11 +02005412static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5413{
5414 int ret = 0;
5415 struct ufs_vreg_info *info = &hba->vreg_info;
5416
5417 if (!info)
5418 goto out;
5419 else if (!info->vccq)
5420 goto out;
5421
5422 if (unused) {
5423 /* shut off the rail here */
5424 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5425 /*
5426 * Mark this rail as no longer used, so it doesn't get enabled
5427 * later by mistake
5428 */
5429 if (!ret)
5430 info->vccq->unused = true;
5431 } else {
5432 /*
5433 * rail should have been already enabled hence just make sure
5434 * that unused flag is cleared.
5435 */
5436 info->vccq->unused = false;
5437 }
5438out:
5439 return ret;
5440}
5441
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005442static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5443 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005444{
5445 int ret = 0;
5446 struct ufs_clk_info *clki;
5447 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005448 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005449
5450 if (!head || list_empty(head))
5451 goto out;
5452
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005453 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5454 if (ret)
5455 return ret;
5456
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005457 list_for_each_entry(clki, head, list) {
5458 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005459 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5460 continue;
5461
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005462 if (on && !clki->enabled) {
5463 ret = clk_prepare_enable(clki->clk);
5464 if (ret) {
5465 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5466 __func__, clki->name, ret);
5467 goto out;
5468 }
5469 } else if (!on && clki->enabled) {
5470 clk_disable_unprepare(clki->clk);
5471 }
5472 clki->enabled = on;
5473 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5474 clki->name, on ? "en" : "dis");
5475 }
5476 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005477
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005478 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5479 if (ret)
5480 return ret;
5481
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005482out:
5483 if (ret) {
5484 list_for_each_entry(clki, head, list) {
5485 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5486 clk_disable_unprepare(clki->clk);
5487 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005488 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005489 spin_lock_irqsave(hba->host->host_lock, flags);
5490 hba->clk_gating.state = CLKS_ON;
5491 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005492 }
5493 return ret;
5494}
5495
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005496static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5497{
5498 return __ufshcd_setup_clocks(hba, on, false);
5499}
5500
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005501static int ufshcd_init_clocks(struct ufs_hba *hba)
5502{
5503 int ret = 0;
5504 struct ufs_clk_info *clki;
5505 struct device *dev = hba->dev;
5506 struct list_head *head = &hba->clk_list_head;
5507
5508 if (!head || list_empty(head))
5509 goto out;
5510
5511 list_for_each_entry(clki, head, list) {
5512 if (!clki->name)
5513 continue;
5514
5515 clki->clk = devm_clk_get(dev, clki->name);
5516 if (IS_ERR(clki->clk)) {
5517 ret = PTR_ERR(clki->clk);
5518 dev_err(dev, "%s: %s clk get failed, %d\n",
5519 __func__, clki->name, ret);
5520 goto out;
5521 }
5522
5523 if (clki->max_freq) {
5524 ret = clk_set_rate(clki->clk, clki->max_freq);
5525 if (ret) {
5526 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5527 __func__, clki->name,
5528 clki->max_freq, ret);
5529 goto out;
5530 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005531 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005532 }
5533 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5534 clki->name, clk_get_rate(clki->clk));
5535 }
5536out:
5537 return ret;
5538}
5539
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005540static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5541{
5542 int err = 0;
5543
5544 if (!hba->vops)
5545 goto out;
5546
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005547 err = ufshcd_vops_init(hba);
5548 if (err)
5549 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005550
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005551 err = ufshcd_vops_setup_regulators(hba, true);
5552 if (err)
5553 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005554
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005555 goto out;
5556
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005557out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005558 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005559out:
5560 if (err)
5561 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005562 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005563 return err;
5564}
5565
5566static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5567{
5568 if (!hba->vops)
5569 return;
5570
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005571 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005572
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005573 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005574}
5575
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005576static int ufshcd_hba_init(struct ufs_hba *hba)
5577{
5578 int err;
5579
Raviv Shvili6a771a62014-09-25 15:32:24 +03005580 /*
5581 * Handle host controller power separately from the UFS device power
5582 * rails as it will help controlling the UFS host controller power
5583 * collapse easily which is different than UFS device power collapse.
5584 * Also, enable the host controller power before we go ahead with rest
5585 * of the initialization here.
5586 */
5587 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005588 if (err)
5589 goto out;
5590
Raviv Shvili6a771a62014-09-25 15:32:24 +03005591 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005592 if (err)
5593 goto out;
5594
Raviv Shvili6a771a62014-09-25 15:32:24 +03005595 err = ufshcd_init_clocks(hba);
5596 if (err)
5597 goto out_disable_hba_vreg;
5598
5599 err = ufshcd_setup_clocks(hba, true);
5600 if (err)
5601 goto out_disable_hba_vreg;
5602
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005603 err = ufshcd_init_vreg(hba);
5604 if (err)
5605 goto out_disable_clks;
5606
5607 err = ufshcd_setup_vreg(hba, true);
5608 if (err)
5609 goto out_disable_clks;
5610
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005611 err = ufshcd_variant_hba_init(hba);
5612 if (err)
5613 goto out_disable_vreg;
5614
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005615 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005616 goto out;
5617
5618out_disable_vreg:
5619 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005620out_disable_clks:
5621 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005622out_disable_hba_vreg:
5623 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005624out:
5625 return err;
5626}
5627
5628static void ufshcd_hba_exit(struct ufs_hba *hba)
5629{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005630 if (hba->is_powered) {
5631 ufshcd_variant_hba_exit(hba);
5632 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07005633 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005634 ufshcd_setup_clocks(hba, false);
5635 ufshcd_setup_hba_vreg(hba, false);
5636 hba->is_powered = false;
5637 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005638}
5639
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005640static int
5641ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305642{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005643 unsigned char cmd[6] = {REQUEST_SENSE,
5644 0,
5645 0,
5646 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005647 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005648 0};
5649 char *buffer;
5650 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305651
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005652 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005653 if (!buffer) {
5654 ret = -ENOMEM;
5655 goto out;
5656 }
5657
5658 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005659 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005660 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5661 if (ret)
5662 pr_err("%s: failed with err %d\n", __func__, ret);
5663
5664 kfree(buffer);
5665out:
5666 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305667}
5668
5669/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005670 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5671 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305672 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005673 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305674 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005675 * Returns 0 if requested power mode is set successfully
5676 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305677 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005678static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5679 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305680{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005681 unsigned char cmd[6] = { START_STOP };
5682 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005683 struct scsi_device *sdp;
5684 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005685 int ret;
5686
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005687 spin_lock_irqsave(hba->host->host_lock, flags);
5688 sdp = hba->sdev_ufs_device;
5689 if (sdp) {
5690 ret = scsi_device_get(sdp);
5691 if (!ret && !scsi_device_online(sdp)) {
5692 ret = -ENODEV;
5693 scsi_device_put(sdp);
5694 }
5695 } else {
5696 ret = -ENODEV;
5697 }
5698 spin_unlock_irqrestore(hba->host->host_lock, flags);
5699
5700 if (ret)
5701 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005702
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305703 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005704 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5705 * handling, which would wait for host to be resumed. Since we know
5706 * we are functional while we are here, skip host resume in error
5707 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305708 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005709 hba->host->eh_noresume = 1;
5710 if (hba->wlun_dev_clr_ua) {
5711 ret = ufshcd_send_request_sense(hba, sdp);
5712 if (ret)
5713 goto out;
5714 /* Unit attention condition is cleared now */
5715 hba->wlun_dev_clr_ua = false;
5716 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305717
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005718 cmd[4] = pwr_mode << 4;
5719
5720 /*
5721 * Current function would be generally called from the power management
5722 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5723 * already suspended childs.
5724 */
5725 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5726 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5727 if (ret) {
5728 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005729 "START_STOP failed for power mode: %d, result %x\n",
5730 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005731 if (driver_byte(ret) & DRIVER_SENSE)
5732 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005733 }
5734
5735 if (!ret)
5736 hba->curr_dev_pwr_mode = pwr_mode;
5737out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005738 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005739 hba->host->eh_noresume = 0;
5740 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305741}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305742
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005743static int ufshcd_link_state_transition(struct ufs_hba *hba,
5744 enum uic_link_state req_link_state,
5745 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305746{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005747 int ret = 0;
5748
5749 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305750 return 0;
5751
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005752 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5753 ret = ufshcd_uic_hibern8_enter(hba);
5754 if (!ret)
5755 ufshcd_set_link_hibern8(hba);
5756 else
5757 goto out;
5758 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305759 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005760 * If autobkops is enabled, link can't be turned off because
5761 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305762 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005763 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5764 (!check_for_bkops || (check_for_bkops &&
5765 !hba->auto_bkops_enabled))) {
5766 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005767 * Let's make sure that link is in low power mode, we are doing
5768 * this currently by putting the link in Hibern8. Otherway to
5769 * put the link in low power mode is to send the DME end point
5770 * to device and then send the DME reset command to local
5771 * unipro. But putting the link in hibern8 is much faster.
5772 */
5773 ret = ufshcd_uic_hibern8_enter(hba);
5774 if (ret)
5775 goto out;
5776 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005777 * Change controller state to "reset state" which
5778 * should also put the link in off/reset state
5779 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005780 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005781 /*
5782 * TODO: Check if we need any delay to make sure that
5783 * controller is reset
5784 */
5785 ufshcd_set_link_off(hba);
5786 }
5787
5788out:
5789 return ret;
5790}
5791
5792static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5793{
5794 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02005795 * It seems some UFS devices may keep drawing more than sleep current
5796 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
5797 * To avoid this situation, add 2ms delay before putting these UFS
5798 * rails in LPM mode.
5799 */
5800 if (!ufshcd_is_link_active(hba) &&
5801 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
5802 usleep_range(2000, 2100);
5803
5804 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005805 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5806 * power.
5807 *
5808 * If UFS device and link is in OFF state, all power supplies (VCC,
5809 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5810 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5811 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5812 *
5813 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5814 * in low power state which would save some power.
5815 */
5816 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5817 !hba->dev_info.is_lu_power_on_wp) {
5818 ufshcd_setup_vreg(hba, false);
5819 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5820 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5821 if (!ufshcd_is_link_active(hba)) {
5822 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5823 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5824 }
5825 }
5826}
5827
5828static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5829{
5830 int ret = 0;
5831
5832 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5833 !hba->dev_info.is_lu_power_on_wp) {
5834 ret = ufshcd_setup_vreg(hba, true);
5835 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005836 if (!ret && !ufshcd_is_link_active(hba)) {
5837 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5838 if (ret)
5839 goto vcc_disable;
5840 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5841 if (ret)
5842 goto vccq_lpm;
5843 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07005844 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005845 }
5846 goto out;
5847
5848vccq_lpm:
5849 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5850vcc_disable:
5851 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5852out:
5853 return ret;
5854}
5855
5856static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5857{
5858 if (ufshcd_is_link_off(hba))
5859 ufshcd_setup_hba_vreg(hba, false);
5860}
5861
5862static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5863{
5864 if (ufshcd_is_link_off(hba))
5865 ufshcd_setup_hba_vreg(hba, true);
5866}
5867
5868/**
5869 * ufshcd_suspend - helper function for suspend operations
5870 * @hba: per adapter instance
5871 * @pm_op: desired low power operation type
5872 *
5873 * This function will try to put the UFS device and link into low power
5874 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5875 * (System PM level).
5876 *
5877 * If this function is called during shutdown, it will make sure that
5878 * both UFS device and UFS link is powered off.
5879 *
5880 * NOTE: UFS device & link must be active before we enter in this function.
5881 *
5882 * Returns 0 for success and non-zero for failure
5883 */
5884static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5885{
5886 int ret = 0;
5887 enum ufs_pm_level pm_lvl;
5888 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5889 enum uic_link_state req_link_state;
5890
5891 hba->pm_op_in_progress = 1;
5892 if (!ufshcd_is_shutdown_pm(pm_op)) {
5893 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5894 hba->rpm_lvl : hba->spm_lvl;
5895 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5896 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5897 } else {
5898 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5899 req_link_state = UIC_LINK_OFF_STATE;
5900 }
5901
5902 /*
5903 * If we can't transition into any of the low power modes
5904 * just gate the clocks.
5905 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005906 ufshcd_hold(hba, false);
5907 hba->clk_gating.is_suspended = true;
5908
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005909 ufshcd_suspend_clkscaling(hba);
5910
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005911 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5912 req_link_state == UIC_LINK_ACTIVE_STATE) {
5913 goto disable_clks;
5914 }
5915
5916 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5917 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005918 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005919
5920 /* UFS device & link must be active before we enter in this function */
5921 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5922 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005923 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005924 }
5925
5926 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005927 if (ufshcd_can_autobkops_during_suspend(hba)) {
5928 /*
5929 * The device is idle with no requests in the queue,
5930 * allow background operations if bkops status shows
5931 * that performance might be impacted.
5932 */
5933 ret = ufshcd_urgent_bkops(hba);
5934 if (ret)
5935 goto enable_gating;
5936 } else {
5937 /* make sure that auto bkops is disabled */
5938 ufshcd_disable_auto_bkops(hba);
5939 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005940 }
5941
5942 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5943 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5944 !ufshcd_is_runtime_pm(pm_op))) {
5945 /* ensure that bkops is disabled */
5946 ufshcd_disable_auto_bkops(hba);
5947 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5948 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005949 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005950 }
5951
5952 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5953 if (ret)
5954 goto set_dev_active;
5955
5956 ufshcd_vreg_set_lpm(hba);
5957
5958disable_clks:
5959 /*
5960 * Call vendor specific suspend callback. As these callbacks may access
5961 * vendor specific host controller register space call them before the
5962 * host clocks are ON.
5963 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005964 ret = ufshcd_vops_suspend(hba, pm_op);
5965 if (ret)
5966 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005967
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005968 if (!ufshcd_is_link_active(hba))
5969 ufshcd_setup_clocks(hba, false);
5970 else
5971 /* If link is active, device ref_clk can't be switched off */
5972 __ufshcd_setup_clocks(hba, false, true);
5973
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005974 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005975 /*
5976 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005977 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005978 */
5979 ufshcd_disable_irq(hba);
5980 /* Put the host controller in low power mode if possible */
5981 ufshcd_hba_vreg_set_lpm(hba);
5982 goto out;
5983
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005984set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07005985 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005986 ufshcd_vreg_set_hpm(hba);
5987 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5988 ufshcd_set_link_active(hba);
5989 else if (ufshcd_is_link_off(hba))
5990 ufshcd_host_reset_and_restore(hba);
5991set_dev_active:
5992 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5993 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005994enable_gating:
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07005995 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005996 hba->clk_gating.is_suspended = false;
5997 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005998out:
5999 hba->pm_op_in_progress = 0;
6000 return ret;
6001}
6002
6003/**
6004 * ufshcd_resume - helper function for resume operations
6005 * @hba: per adapter instance
6006 * @pm_op: runtime PM or system PM
6007 *
6008 * This function basically brings the UFS device, UniPro link and controller
6009 * to active state.
6010 *
6011 * Returns 0 for success and non-zero for failure
6012 */
6013static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6014{
6015 int ret;
6016 enum uic_link_state old_link_state;
6017
6018 hba->pm_op_in_progress = 1;
6019 old_link_state = hba->uic_link_state;
6020
6021 ufshcd_hba_vreg_set_hpm(hba);
6022 /* Make sure clocks are enabled before accessing controller */
6023 ret = ufshcd_setup_clocks(hba, true);
6024 if (ret)
6025 goto out;
6026
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006027 /* enable the host irq as host controller would be active soon */
6028 ret = ufshcd_enable_irq(hba);
6029 if (ret)
6030 goto disable_irq_and_vops_clks;
6031
6032 ret = ufshcd_vreg_set_hpm(hba);
6033 if (ret)
6034 goto disable_irq_and_vops_clks;
6035
6036 /*
6037 * Call vendor specific resume callback. As these callbacks may access
6038 * vendor specific host controller register space call them when the
6039 * host clocks are ON.
6040 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006041 ret = ufshcd_vops_resume(hba, pm_op);
6042 if (ret)
6043 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006044
6045 if (ufshcd_is_link_hibern8(hba)) {
6046 ret = ufshcd_uic_hibern8_exit(hba);
6047 if (!ret)
6048 ufshcd_set_link_active(hba);
6049 else
6050 goto vendor_suspend;
6051 } else if (ufshcd_is_link_off(hba)) {
6052 ret = ufshcd_host_reset_and_restore(hba);
6053 /*
6054 * ufshcd_host_reset_and_restore() should have already
6055 * set the link state as active
6056 */
6057 if (ret || !ufshcd_is_link_active(hba))
6058 goto vendor_suspend;
6059 }
6060
6061 if (!ufshcd_is_ufs_dev_active(hba)) {
6062 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6063 if (ret)
6064 goto set_old_link_state;
6065 }
6066
Subhash Jadavani374a2462014-09-25 15:32:35 +03006067 /*
6068 * If BKOPs operations are urgently needed at this moment then
6069 * keep auto-bkops enabled or else disable it.
6070 */
6071 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006072 hba->clk_gating.is_suspended = false;
6073
Gilad Bronera5082532016-10-17 17:10:00 -07006074 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006075
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006076 /* Schedule clock gating in case of no access to UFS device yet */
6077 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006078 goto out;
6079
6080set_old_link_state:
6081 ufshcd_link_state_transition(hba, old_link_state, 0);
6082vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006083 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006084disable_vreg:
6085 ufshcd_vreg_set_lpm(hba);
6086disable_irq_and_vops_clks:
6087 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006088 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006089 ufshcd_setup_clocks(hba, false);
6090out:
6091 hba->pm_op_in_progress = 0;
6092 return ret;
6093}
6094
6095/**
6096 * ufshcd_system_suspend - system suspend routine
6097 * @hba: per adapter instance
6098 * @pm_op: runtime PM or system PM
6099 *
6100 * Check the description of ufshcd_suspend() function for more details.
6101 *
6102 * Returns 0 for success and non-zero for failure
6103 */
6104int ufshcd_system_suspend(struct ufs_hba *hba)
6105{
6106 int ret = 0;
6107
6108 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006109 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006110
6111 if (pm_runtime_suspended(hba->dev)) {
6112 if (hba->rpm_lvl == hba->spm_lvl)
6113 /*
6114 * There is possibility that device may still be in
6115 * active state during the runtime suspend.
6116 */
6117 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6118 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6119 goto out;
6120
6121 /*
6122 * UFS device and/or UFS link low power states during runtime
6123 * suspend seems to be different than what is expected during
6124 * system suspend. Hence runtime resume the devic & link and
6125 * let the system suspend low power states to take effect.
6126 * TODO: If resume takes longer time, we might have optimize
6127 * it in future by not resuming everything if possible.
6128 */
6129 ret = ufshcd_runtime_resume(hba);
6130 if (ret)
6131 goto out;
6132 }
6133
6134 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6135out:
Dolev Ravive7850602014-09-25 15:32:36 +03006136 if (!ret)
6137 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006138 return ret;
6139}
6140EXPORT_SYMBOL(ufshcd_system_suspend);
6141
6142/**
6143 * ufshcd_system_resume - system resume routine
6144 * @hba: per adapter instance
6145 *
6146 * Returns 0 for success and non-zero for failure
6147 */
6148
6149int ufshcd_system_resume(struct ufs_hba *hba)
6150{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006151 if (!hba)
6152 return -EINVAL;
6153
6154 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006155 /*
6156 * Let the runtime resume take care of resuming
6157 * if runtime suspended.
6158 */
6159 return 0;
6160
6161 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6162}
6163EXPORT_SYMBOL(ufshcd_system_resume);
6164
6165/**
6166 * ufshcd_runtime_suspend - runtime suspend routine
6167 * @hba: per adapter instance
6168 *
6169 * Check the description of ufshcd_suspend() function for more details.
6170 *
6171 * Returns 0 for success and non-zero for failure
6172 */
6173int ufshcd_runtime_suspend(struct ufs_hba *hba)
6174{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006175 if (!hba)
6176 return -EINVAL;
6177
6178 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006179 return 0;
6180
6181 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306182}
6183EXPORT_SYMBOL(ufshcd_runtime_suspend);
6184
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006185/**
6186 * ufshcd_runtime_resume - runtime resume routine
6187 * @hba: per adapter instance
6188 *
6189 * This function basically brings the UFS device, UniPro link and controller
6190 * to active state. Following operations are done in this function:
6191 *
6192 * 1. Turn on all the controller related clocks
6193 * 2. Bring the UniPro link out of Hibernate state
6194 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6195 * to active state.
6196 * 4. If auto-bkops is enabled on the device, disable it.
6197 *
6198 * So following would be the possible power state after this function return
6199 * successfully:
6200 * S1: UFS device in Active state with VCC rail ON
6201 * UniPro link in Active state
6202 * All the UFS/UniPro controller clocks are ON
6203 *
6204 * Returns 0 for success and non-zero for failure
6205 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306206int ufshcd_runtime_resume(struct ufs_hba *hba)
6207{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006208 if (!hba)
6209 return -EINVAL;
6210
6211 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306212 return 0;
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006213
6214 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306215}
6216EXPORT_SYMBOL(ufshcd_runtime_resume);
6217
6218int ufshcd_runtime_idle(struct ufs_hba *hba)
6219{
6220 return 0;
6221}
6222EXPORT_SYMBOL(ufshcd_runtime_idle);
6223
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306224/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006225 * ufshcd_shutdown - shutdown routine
6226 * @hba: per adapter instance
6227 *
6228 * This function would power off both UFS device and UFS link.
6229 *
6230 * Returns 0 always to allow force shutdown even in case of errors.
6231 */
6232int ufshcd_shutdown(struct ufs_hba *hba)
6233{
6234 int ret = 0;
6235
6236 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6237 goto out;
6238
6239 if (pm_runtime_suspended(hba->dev)) {
6240 ret = ufshcd_runtime_resume(hba);
6241 if (ret)
6242 goto out;
6243 }
6244
6245 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6246out:
6247 if (ret)
6248 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6249 /* allow force shutdown even in case of errors */
6250 return 0;
6251}
6252EXPORT_SYMBOL(ufshcd_shutdown);
6253
6254/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306255 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306256 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306257 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306258 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306259void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306260{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306261 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306262 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306263 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006264 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306265
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006266 ufshcd_exit_clk_gating(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006267 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306268}
6269EXPORT_SYMBOL_GPL(ufshcd_remove);
6270
6271/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006272 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6273 * @hba: pointer to Host Bus Adapter (HBA)
6274 */
6275void ufshcd_dealloc_host(struct ufs_hba *hba)
6276{
6277 scsi_host_put(hba->host);
6278}
6279EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6280
6281/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006282 * ufshcd_set_dma_mask - Set dma mask based on the controller
6283 * addressing capability
6284 * @hba: per adapter instance
6285 *
6286 * Returns 0 for success, non-zero for failure
6287 */
6288static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6289{
6290 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6291 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6292 return 0;
6293 }
6294 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6295}
6296
6297/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006298 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306299 * @dev: pointer to device handle
6300 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306301 * Returns 0 on success, non-zero value on failure
6302 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006303int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306304{
6305 struct Scsi_Host *host;
6306 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006307 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306308
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306309 if (!dev) {
6310 dev_err(dev,
6311 "Invalid memory reference for dev is NULL\n");
6312 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306313 goto out_error;
6314 }
6315
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306316 host = scsi_host_alloc(&ufshcd_driver_template,
6317 sizeof(struct ufs_hba));
6318 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306319 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306320 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306321 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306322 }
6323 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306324 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306325 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006326 *hba_handle = hba;
6327
6328out_error:
6329 return err;
6330}
6331EXPORT_SYMBOL(ufshcd_alloc_host);
6332
Sahitya Tummala856b3482014-09-25 15:32:34 +03006333static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6334{
6335 int ret = 0;
6336 struct ufs_clk_info *clki;
6337 struct list_head *head = &hba->clk_list_head;
6338
6339 if (!head || list_empty(head))
6340 goto out;
6341
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006342 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6343 if (ret)
6344 return ret;
6345
Sahitya Tummala856b3482014-09-25 15:32:34 +03006346 list_for_each_entry(clki, head, list) {
6347 if (!IS_ERR_OR_NULL(clki->clk)) {
6348 if (scale_up && clki->max_freq) {
6349 if (clki->curr_freq == clki->max_freq)
6350 continue;
6351 ret = clk_set_rate(clki->clk, clki->max_freq);
6352 if (ret) {
6353 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6354 __func__, clki->name,
6355 clki->max_freq, ret);
6356 break;
6357 }
6358 clki->curr_freq = clki->max_freq;
6359
6360 } else if (!scale_up && clki->min_freq) {
6361 if (clki->curr_freq == clki->min_freq)
6362 continue;
6363 ret = clk_set_rate(clki->clk, clki->min_freq);
6364 if (ret) {
6365 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6366 __func__, clki->name,
6367 clki->min_freq, ret);
6368 break;
6369 }
6370 clki->curr_freq = clki->min_freq;
6371 }
6372 }
6373 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6374 clki->name, clk_get_rate(clki->clk));
6375 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006376
6377 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6378
Sahitya Tummala856b3482014-09-25 15:32:34 +03006379out:
6380 return ret;
6381}
6382
6383static int ufshcd_devfreq_target(struct device *dev,
6384 unsigned long *freq, u32 flags)
6385{
6386 int err = 0;
6387 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006388 bool release_clk_hold = false;
6389 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006390
6391 if (!ufshcd_is_clkscaling_enabled(hba))
6392 return -EINVAL;
6393
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006394 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6395 if (ufshcd_eh_in_progress(hba)) {
6396 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6397 return 0;
6398 }
6399
6400 if (ufshcd_is_clkgating_allowed(hba) &&
6401 (hba->clk_gating.state != CLKS_ON)) {
6402 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6403 /* hold the vote until the scaling work is completed */
6404 hba->clk_gating.active_reqs++;
6405 release_clk_hold = true;
6406 hba->clk_gating.state = CLKS_ON;
6407 } else {
6408 /*
6409 * Clock gating work seems to be running in parallel
6410 * hence skip scaling work to avoid deadlock between
6411 * current scaling work and gating work.
6412 */
6413 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6414 return 0;
6415 }
6416 }
6417 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6418
Sahitya Tummala856b3482014-09-25 15:32:34 +03006419 if (*freq == UINT_MAX)
6420 err = ufshcd_scale_clks(hba, true);
6421 else if (*freq == 0)
6422 err = ufshcd_scale_clks(hba, false);
6423
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006424 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6425 if (release_clk_hold)
6426 __ufshcd_release(hba);
6427 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6428
Sahitya Tummala856b3482014-09-25 15:32:34 +03006429 return err;
6430}
6431
6432static int ufshcd_devfreq_get_dev_status(struct device *dev,
6433 struct devfreq_dev_status *stat)
6434{
6435 struct ufs_hba *hba = dev_get_drvdata(dev);
6436 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6437 unsigned long flags;
6438
6439 if (!ufshcd_is_clkscaling_enabled(hba))
6440 return -EINVAL;
6441
6442 memset(stat, 0, sizeof(*stat));
6443
6444 spin_lock_irqsave(hba->host->host_lock, flags);
6445 if (!scaling->window_start_t)
6446 goto start_window;
6447
6448 if (scaling->is_busy_started)
6449 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6450 scaling->busy_start_t));
6451
6452 stat->total_time = jiffies_to_usecs((long)jiffies -
6453 (long)scaling->window_start_t);
6454 stat->busy_time = scaling->tot_busy_t;
6455start_window:
6456 scaling->window_start_t = jiffies;
6457 scaling->tot_busy_t = 0;
6458
6459 if (hba->outstanding_reqs) {
6460 scaling->busy_start_t = ktime_get();
6461 scaling->is_busy_started = true;
6462 } else {
6463 scaling->busy_start_t = ktime_set(0, 0);
6464 scaling->is_busy_started = false;
6465 }
6466 spin_unlock_irqrestore(hba->host->host_lock, flags);
6467 return 0;
6468}
6469
6470static struct devfreq_dev_profile ufs_devfreq_profile = {
6471 .polling_ms = 100,
6472 .target = ufshcd_devfreq_target,
6473 .get_dev_status = ufshcd_devfreq_get_dev_status,
6474};
6475
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006476/**
6477 * ufshcd_init - Driver initialization routine
6478 * @hba: per-adapter instance
6479 * @mmio_base: base register address
6480 * @irq: Interrupt line of device
6481 * Returns 0 on success, non-zero value on failure
6482 */
6483int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6484{
6485 int err;
6486 struct Scsi_Host *host = hba->host;
6487 struct device *dev = hba->dev;
6488
6489 if (!mmio_base) {
6490 dev_err(hba->dev,
6491 "Invalid memory reference for mmio_base is NULL\n");
6492 err = -ENODEV;
6493 goto out_error;
6494 }
6495
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306496 hba->mmio_base = mmio_base;
6497 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306498
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006499 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006500 if (err)
6501 goto out_error;
6502
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306503 /* Read capabilities registers */
6504 ufshcd_hba_capabilities(hba);
6505
6506 /* Get UFS version supported by the controller */
6507 hba->ufs_version = ufshcd_get_ufs_version(hba);
6508
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306509 /* Get Interrupt bit mask per version */
6510 hba->intr_mask = ufshcd_get_intr_mask(hba);
6511
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006512 err = ufshcd_set_dma_mask(hba);
6513 if (err) {
6514 dev_err(hba->dev, "set dma mask failed\n");
6515 goto out_disable;
6516 }
6517
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306518 /* Allocate memory for host memory space */
6519 err = ufshcd_memory_alloc(hba);
6520 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306521 dev_err(hba->dev, "Memory allocation failed\n");
6522 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306523 }
6524
6525 /* Configure LRB */
6526 ufshcd_host_memory_configure(hba);
6527
6528 host->can_queue = hba->nutrs;
6529 host->cmd_per_lun = hba->nutrs;
6530 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006531 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306532 host->max_channel = UFSHCD_MAX_CHANNEL;
6533 host->unique_id = host->host_no;
6534 host->max_cmd_len = MAX_CDB_SIZE;
6535
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006536 hba->max_pwr_info.is_valid = false;
6537
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306538 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306539 init_waitqueue_head(&hba->tm_wq);
6540 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306541
6542 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306543 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306544 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306545
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306546 /* Initialize UIC command mutex */
6547 mutex_init(&hba->uic_cmd_mutex);
6548
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306549 /* Initialize mutex for device management commands */
6550 mutex_init(&hba->dev_cmd.lock);
6551
6552 /* Initialize device management tag acquire wait queue */
6553 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6554
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006555 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006556
6557 /*
6558 * In order to avoid any spurious interrupt immediately after
6559 * registering UFS controller interrupt handler, clear any pending UFS
6560 * interrupt status and disable all the UFS interrupts.
6561 */
6562 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6563 REG_INTERRUPT_STATUS);
6564 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6565 /*
6566 * Make sure that UFS interrupts are disabled and any pending interrupt
6567 * status is cleared before registering UFS interrupt handler.
6568 */
6569 mb();
6570
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306571 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006572 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306573 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306574 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006575 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006576 } else {
6577 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306578 }
6579
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306580 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306581 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306582 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006583 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306584 }
6585
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306586 /* Host controller enable */
6587 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306588 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306589 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306590 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306591 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306592
Sahitya Tummala856b3482014-09-25 15:32:34 +03006593 if (ufshcd_is_clkscaling_enabled(hba)) {
Chanwoo Choi4861ee12016-11-08 18:13:28 +09006594 hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile,
Sahitya Tummala856b3482014-09-25 15:32:34 +03006595 "simple_ondemand", NULL);
6596 if (IS_ERR(hba->devfreq)) {
6597 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6598 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006599 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006600 goto out_remove_scsi_host;
6601 }
6602 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07006603 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006604 }
6605
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306606 /* Hold auto suspend until async scan completes */
6607 pm_runtime_get_sync(dev);
6608
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006609 /*
6610 * The device-initialize-sequence hasn't been invoked yet.
6611 * Set the device to power-off state
6612 */
6613 ufshcd_set_ufs_dev_poweroff(hba);
6614
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306615 async_schedule(ufshcd_async_scan, hba);
6616
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306617 return 0;
6618
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306619out_remove_scsi_host:
6620 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006621exit_gating:
6622 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306623out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006624 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006625 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306626out_error:
6627 return err;
6628}
6629EXPORT_SYMBOL_GPL(ufshcd_init);
6630
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306631MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6632MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306633MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306634MODULE_LICENSE("GPL");
6635MODULE_VERSION(UFSHCD_DRIVER_VERSION);