blob: 4b6cc07736814274a28041f657fe1506bba7be54 [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 */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080062#define QUERY_REQ_RETRIES 3
Dolev Raviv68078d52013-07-30 00:35:58 +053063/* Query request timeout */
subhashj@codeaurora.org10fe5882016-11-23 16:31:52 -080064#define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
Dolev Raviv68078d52013-07-30 00:35:58 +053065
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053066/* Task management command timeout */
67#define TM_CMD_TIMEOUT 100 /* msecs */
68
Yaniv Gardi64238fb2016-02-01 15:02:43 +020069/* maximum number of retries for a general UIC command */
70#define UFS_UIC_COMMAND_RETRIES 3
71
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030072/* maximum number of link-startup retries */
73#define DME_LINKSTARTUP_RETRIES 3
74
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020075/* Maximum retries for Hibern8 enter */
76#define UIC_HIBERN8_ENTER_RETRIES 3
77
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030078/* maximum number of reset retries before giving up */
79#define MAX_HOST_RESET_RETRIES 5
80
Dolev Raviv68078d52013-07-30 00:35:58 +053081/* Expose the flag value from utp_upiu_query.value */
82#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
83
Seungwon Jeon7d568652013-08-31 21:40:20 +053084/* Interrupt aggregation default timeout, unit: 40us */
85#define INT_AGGR_DEF_TO 0x02
86
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030087#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
88 ({ \
89 int _ret; \
90 if (_on) \
91 _ret = ufshcd_enable_vreg(_dev, _vreg); \
92 else \
93 _ret = ufshcd_disable_vreg(_dev, _vreg); \
94 _ret; \
95 })
96
Subhash Jadavanida461ce2014-09-25 15:32:25 +030097static u32 ufs_query_desc_max_size[] = {
98 QUERY_DESC_DEVICE_MAX_SIZE,
99 QUERY_DESC_CONFIGURAION_MAX_SIZE,
100 QUERY_DESC_UNIT_MAX_SIZE,
101 QUERY_DESC_RFU_MAX_SIZE,
102 QUERY_DESC_INTERCONNECT_MAX_SIZE,
103 QUERY_DESC_STRING_MAX_SIZE,
104 QUERY_DESC_RFU_MAX_SIZE,
Tomas Winkler1ce21792016-02-09 10:25:40 +0200105 QUERY_DESC_GEOMETRY_MAX_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300106 QUERY_DESC_POWER_MAX_SIZE,
107 QUERY_DESC_RFU_MAX_SIZE,
108};
109
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530110enum {
111 UFSHCD_MAX_CHANNEL = 0,
112 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530113 UFSHCD_CMD_PER_LUN = 32,
114 UFSHCD_CAN_QUEUE = 32,
115};
116
117/* UFSHCD states */
118enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530119 UFSHCD_STATE_RESET,
120 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530121 UFSHCD_STATE_OPERATIONAL,
Zang Leigang141f8162016-11-16 11:29:37 +0800122 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530123};
124
125/* UFSHCD error handling flags */
126enum {
127 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530128};
129
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530130/* UFSHCD UIC layer error flags */
131enum {
132 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200133 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
134 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
135 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
136 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
137 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530138};
139
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530140/* Interrupt configuration options */
141enum {
142 UFSHCD_INT_DISABLE,
143 UFSHCD_INT_ENABLE,
144 UFSHCD_INT_CLEAR,
145};
146
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530147#define ufshcd_set_eh_in_progress(h) \
148 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
149#define ufshcd_eh_in_progress(h) \
150 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
151#define ufshcd_clear_eh_in_progress(h) \
152 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
153
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300154#define ufshcd_set_ufs_dev_active(h) \
155 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
156#define ufshcd_set_ufs_dev_sleep(h) \
157 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
158#define ufshcd_set_ufs_dev_poweroff(h) \
159 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
160#define ufshcd_is_ufs_dev_active(h) \
161 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
162#define ufshcd_is_ufs_dev_sleep(h) \
163 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
164#define ufshcd_is_ufs_dev_poweroff(h) \
165 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
166
167static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
168 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
169 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
170 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
171 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
172 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
173 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
174};
175
176static inline enum ufs_dev_pwr_mode
177ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
178{
179 return ufs_pm_lvl_states[lvl].dev_state;
180}
181
182static inline enum uic_link_state
183ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
184{
185 return ufs_pm_lvl_states[lvl].link_state;
186}
187
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530188static void ufshcd_tmc_handler(struct ufs_hba *hba);
189static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530190static int ufshcd_reset_and_restore(struct ufs_hba *hba);
191static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300192static void ufshcd_hba_exit(struct ufs_hba *hba);
193static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300194static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
195 bool skip_ref_clk);
196static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200197static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300198static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
199static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300200static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300201static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
202static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300203static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
204 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300205static int ufshcd_change_power_mode(struct ufs_hba *hba,
206 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200207static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
208{
209 return tag >= 0 && tag < hba->nutrs;
210}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300211
212static inline int ufshcd_enable_irq(struct ufs_hba *hba)
213{
214 int ret = 0;
215
216 if (!hba->is_irq_enabled) {
217 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
218 hba);
219 if (ret)
220 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
221 __func__, ret);
222 hba->is_irq_enabled = true;
223 }
224
225 return ret;
226}
227
228static inline void ufshcd_disable_irq(struct ufs_hba *hba)
229{
230 if (hba->is_irq_enabled) {
231 free_irq(hba->irq, hba);
232 hba->is_irq_enabled = false;
233 }
234}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530235
Yaniv Gardib573d482016-03-10 17:37:09 +0200236/* replace non-printable or non-ASCII characters with spaces */
237static inline void ufshcd_remove_non_printable(char *val)
238{
239 if (!val)
240 return;
241
242 if (*val < 0x20 || *val > 0x7e)
243 *val = ' ';
244}
245
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530246/*
247 * ufshcd_wait_for_register - wait for register value to change
248 * @hba - per-adapter interface
249 * @reg - mmio register offset
250 * @mask - mask to apply to read register value
251 * @val - wait condition
252 * @interval_us - polling interval in microsecs
253 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200254 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530255 *
256 * Returns -ETIMEDOUT on error, zero on success
257 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200258int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
259 u32 val, unsigned long interval_us,
260 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530261{
262 int err = 0;
263 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
264
265 /* ignore bits that we don't intend to wait on */
266 val = val & mask;
267
268 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200269 if (can_sleep)
270 usleep_range(interval_us, interval_us + 50);
271 else
272 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530273 if (time_after(jiffies, timeout)) {
274 if ((ufshcd_readl(hba, reg) & mask) != val)
275 err = -ETIMEDOUT;
276 break;
277 }
278 }
279
280 return err;
281}
282
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530283/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530284 * ufshcd_get_intr_mask - Get the interrupt bit mask
285 * @hba - Pointer to adapter instance
286 *
287 * Returns interrupt bit mask per version
288 */
289static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
290{
291 if (hba->ufs_version == UFSHCI_VERSION_10)
292 return INTERRUPT_MASK_ALL_VER_10;
293 else
294 return INTERRUPT_MASK_ALL_VER_11;
295}
296
297/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530298 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
299 * @hba - Pointer to adapter instance
300 *
301 * Returns UFSHCI version supported by the controller
302 */
303static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
304{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200305 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
306 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300307
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530308 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530309}
310
311/**
312 * ufshcd_is_device_present - Check if any device connected to
313 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300314 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530315 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530316 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530317 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300318static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530319{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300320 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
321 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530322}
323
324/**
325 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
326 * @lrb: pointer to local command reference block
327 *
328 * This function is used to get the OCS field from UTRD
329 * Returns the OCS field in the UTRD
330 */
331static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
332{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530333 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530334}
335
336/**
337 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
338 * @task_req_descp: pointer to utp_task_req_desc structure
339 *
340 * This function is used to get the OCS field from UTMRD
341 * Returns the OCS field in the UTMRD
342 */
343static inline int
344ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
345{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530346 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530347}
348
349/**
350 * ufshcd_get_tm_free_slot - get a free slot for task management request
351 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530352 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530353 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530354 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
355 * Returns 0 if free slot is not available, else return 1 with tag value
356 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530357 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530358static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530359{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530360 int tag;
361 bool ret = false;
362
363 if (!free_slot)
364 goto out;
365
366 do {
367 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
368 if (tag >= hba->nutmrs)
369 goto out;
370 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
371
372 *free_slot = tag;
373 ret = true;
374out:
375 return ret;
376}
377
378static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
379{
380 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530381}
382
383/**
384 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
385 * @hba: per adapter instance
386 * @pos: position of the bit to be cleared
387 */
388static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
389{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530390 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530391}
392
393/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200394 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
395 * @hba: per adapter instance
396 * @tag: position of the bit to be cleared
397 */
398static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
399{
400 __clear_bit(tag, &hba->outstanding_reqs);
401}
402
403/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530404 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
405 * @reg: Register value of host controller status
406 *
407 * Returns integer, 0 on Success and positive value if failed
408 */
409static inline int ufshcd_get_lists_status(u32 reg)
410{
411 /*
412 * The mask 0xFF is for the following HCS register bits
413 * Bit Description
414 * 0 Device Present
415 * 1 UTRLRDY
416 * 2 UTMRLRDY
417 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200418 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530419 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200420 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530421}
422
423/**
424 * ufshcd_get_uic_cmd_result - Get the UIC command result
425 * @hba: Pointer to adapter instance
426 *
427 * This function gets the result of UIC command completion
428 * Returns 0 on success, non zero value on error
429 */
430static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
431{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530432 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530433 MASK_UIC_COMMAND_RESULT;
434}
435
436/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530437 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
438 * @hba: Pointer to adapter instance
439 *
440 * This function gets UIC command argument3
441 * Returns 0 on success, non zero value on error
442 */
443static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
444{
445 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
446}
447
448/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530449 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530450 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530451 */
452static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530453ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530454{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530455 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530456}
457
458/**
459 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
460 * @ucd_rsp_ptr: pointer to response UPIU
461 *
462 * This function gets the response status and scsi_status from response UPIU
463 * Returns the response result code.
464 */
465static inline int
466ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
467{
468 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
469}
470
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530471/*
472 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
473 * from response UPIU
474 * @ucd_rsp_ptr: pointer to response UPIU
475 *
476 * Return the data segment length.
477 */
478static inline unsigned int
479ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
480{
481 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
482 MASK_RSP_UPIU_DATA_SEG_LEN;
483}
484
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530485/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530486 * ufshcd_is_exception_event - Check if the device raised an exception event
487 * @ucd_rsp_ptr: pointer to response UPIU
488 *
489 * The function checks if the device raised an exception event indicated in
490 * the Device Information field of response UPIU.
491 *
492 * Returns true if exception is raised, false otherwise.
493 */
494static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
495{
496 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
497 MASK_RSP_EXCEPTION_EVENT ? true : false;
498}
499
500/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530501 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530502 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530503 */
504static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530505ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530506{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530507 ufshcd_writel(hba, INT_AGGR_ENABLE |
508 INT_AGGR_COUNTER_AND_TIMER_RESET,
509 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
510}
511
512/**
513 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
514 * @hba: per adapter instance
515 * @cnt: Interrupt aggregation counter threshold
516 * @tmout: Interrupt aggregation timeout value
517 */
518static inline void
519ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
520{
521 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
522 INT_AGGR_COUNTER_THLD_VAL(cnt) |
523 INT_AGGR_TIMEOUT_VAL(tmout),
524 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530525}
526
527/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300528 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
529 * @hba: per adapter instance
530 */
531static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
532{
533 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
534}
535
536/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530537 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
538 * When run-stop registers are set to 1, it indicates the
539 * host controller that it can process the requests
540 * @hba: per adapter instance
541 */
542static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
543{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530544 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
545 REG_UTP_TASK_REQ_LIST_RUN_STOP);
546 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
547 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530548}
549
550/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530551 * ufshcd_hba_start - Start controller initialization sequence
552 * @hba: per adapter instance
553 */
554static inline void ufshcd_hba_start(struct ufs_hba *hba)
555{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530556 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530557}
558
559/**
560 * ufshcd_is_hba_active - Get controller state
561 * @hba: per adapter instance
562 *
563 * Returns zero if controller is active, 1 otherwise
564 */
565static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
566{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530567 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530568}
569
Yaniv Gardi37113102016-03-10 17:37:16 +0200570u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
571{
572 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
573 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
574 (hba->ufs_version == UFSHCI_VERSION_11))
575 return UFS_UNIPRO_VER_1_41;
576 else
577 return UFS_UNIPRO_VER_1_6;
578}
579EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
580
581static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
582{
583 /*
584 * If both host and device support UniPro ver1.6 or later, PA layer
585 * parameters tuning happens during link startup itself.
586 *
587 * We can manually tune PA layer parameters if either host or device
588 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
589 * logic simple, we will only do manual tuning if local unipro version
590 * doesn't support ver1.6 or later.
591 */
592 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
593 return true;
594 else
595 return false;
596}
597
Gilad Bronera5082532016-10-17 17:10:00 -0700598static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
599{
600 if (ufshcd_is_clkscaling_enabled(hba)) {
601 devfreq_suspend_device(hba->devfreq);
602 hba->clk_scaling.window_start_t = 0;
603 }
604}
605
606static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
607{
608 if (ufshcd_is_clkscaling_enabled(hba))
609 devfreq_resume_device(hba->devfreq);
610}
611
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300612static void ufshcd_ungate_work(struct work_struct *work)
613{
614 int ret;
615 unsigned long flags;
616 struct ufs_hba *hba = container_of(work, struct ufs_hba,
617 clk_gating.ungate_work);
618
619 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
620
621 spin_lock_irqsave(hba->host->host_lock, flags);
622 if (hba->clk_gating.state == CLKS_ON) {
623 spin_unlock_irqrestore(hba->host->host_lock, flags);
624 goto unblock_reqs;
625 }
626
627 spin_unlock_irqrestore(hba->host->host_lock, flags);
628 ufshcd_setup_clocks(hba, true);
629
630 /* Exit from hibern8 */
631 if (ufshcd_can_hibern8_during_gating(hba)) {
632 /* Prevent gating in this path */
633 hba->clk_gating.is_suspended = true;
634 if (ufshcd_is_link_hibern8(hba)) {
635 ret = ufshcd_uic_hibern8_exit(hba);
636 if (ret)
637 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
638 __func__, ret);
639 else
640 ufshcd_set_link_active(hba);
641 }
642 hba->clk_gating.is_suspended = false;
643 }
644unblock_reqs:
Gilad Bronera5082532016-10-17 17:10:00 -0700645 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300646 scsi_unblock_requests(hba->host);
647}
648
649/**
650 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
651 * Also, exit from hibern8 mode and set the link as active.
652 * @hba: per adapter instance
653 * @async: This indicates whether caller should ungate clocks asynchronously.
654 */
655int ufshcd_hold(struct ufs_hba *hba, bool async)
656{
657 int rc = 0;
658 unsigned long flags;
659
660 if (!ufshcd_is_clkgating_allowed(hba))
661 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300662 spin_lock_irqsave(hba->host->host_lock, flags);
663 hba->clk_gating.active_reqs++;
664
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200665 if (ufshcd_eh_in_progress(hba)) {
666 spin_unlock_irqrestore(hba->host->host_lock, flags);
667 return 0;
668 }
669
Sahitya Tummala856b3482014-09-25 15:32:34 +0300670start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300671 switch (hba->clk_gating.state) {
672 case CLKS_ON:
Venkat Gopalakrishnanf2a785a2016-10-17 17:10:53 -0700673 /*
674 * Wait for the ungate work to complete if in progress.
675 * Though the clocks may be in ON state, the link could
676 * still be in hibner8 state if hibern8 is allowed
677 * during clock gating.
678 * Make sure we exit hibern8 state also in addition to
679 * clocks being ON.
680 */
681 if (ufshcd_can_hibern8_during_gating(hba) &&
682 ufshcd_is_link_hibern8(hba)) {
683 spin_unlock_irqrestore(hba->host->host_lock, flags);
684 flush_work(&hba->clk_gating.ungate_work);
685 spin_lock_irqsave(hba->host->host_lock, flags);
686 goto start;
687 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300688 break;
689 case REQ_CLKS_OFF:
690 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
691 hba->clk_gating.state = CLKS_ON;
692 break;
693 }
694 /*
695 * If we here, it means gating work is either done or
696 * currently running. Hence, fall through to cancel gating
697 * work and to enable clocks.
698 */
699 case CLKS_OFF:
700 scsi_block_requests(hba->host);
701 hba->clk_gating.state = REQ_CLKS_ON;
702 schedule_work(&hba->clk_gating.ungate_work);
703 /*
704 * fall through to check if we should wait for this
705 * work to be done or not.
706 */
707 case REQ_CLKS_ON:
708 if (async) {
709 rc = -EAGAIN;
710 hba->clk_gating.active_reqs--;
711 break;
712 }
713
714 spin_unlock_irqrestore(hba->host->host_lock, flags);
715 flush_work(&hba->clk_gating.ungate_work);
716 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300717 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300718 goto start;
719 default:
720 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
721 __func__, hba->clk_gating.state);
722 break;
723 }
724 spin_unlock_irqrestore(hba->host->host_lock, flags);
725out:
726 return rc;
727}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200728EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300729
730static void ufshcd_gate_work(struct work_struct *work)
731{
732 struct ufs_hba *hba = container_of(work, struct ufs_hba,
733 clk_gating.gate_work.work);
734 unsigned long flags;
735
736 spin_lock_irqsave(hba->host->host_lock, flags);
Venkat Gopalakrishnan3f0c06d2016-10-17 17:11:07 -0700737 /*
738 * In case you are here to cancel this work the gating state
739 * would be marked as REQ_CLKS_ON. In this case save time by
740 * skipping the gating work and exit after changing the clock
741 * state to CLKS_ON.
742 */
743 if (hba->clk_gating.is_suspended ||
744 (hba->clk_gating.state == REQ_CLKS_ON)) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300745 hba->clk_gating.state = CLKS_ON;
746 goto rel_lock;
747 }
748
749 if (hba->clk_gating.active_reqs
750 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
751 || hba->lrb_in_use || hba->outstanding_tasks
752 || hba->active_uic_cmd || hba->uic_async_done)
753 goto rel_lock;
754
755 spin_unlock_irqrestore(hba->host->host_lock, flags);
756
757 /* put the link into hibern8 mode before turning off clocks */
758 if (ufshcd_can_hibern8_during_gating(hba)) {
759 if (ufshcd_uic_hibern8_enter(hba)) {
760 hba->clk_gating.state = CLKS_ON;
761 goto out;
762 }
763 ufshcd_set_link_hibern8(hba);
764 }
765
Gilad Bronera5082532016-10-17 17:10:00 -0700766 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +0300767
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300768 if (!ufshcd_is_link_active(hba))
769 ufshcd_setup_clocks(hba, false);
770 else
771 /* If link is active, device ref_clk can't be switched off */
772 __ufshcd_setup_clocks(hba, false, true);
773
774 /*
775 * In case you are here to cancel this work the gating state
776 * would be marked as REQ_CLKS_ON. In this case keep the state
777 * as REQ_CLKS_ON which would anyway imply that clocks are off
778 * and a request to turn them on is pending. By doing this way,
779 * we keep the state machine in tact and this would ultimately
780 * prevent from doing cancel work multiple times when there are
781 * new requests arriving before the current cancel work is done.
782 */
783 spin_lock_irqsave(hba->host->host_lock, flags);
784 if (hba->clk_gating.state == REQ_CLKS_OFF)
785 hba->clk_gating.state = CLKS_OFF;
786
787rel_lock:
788 spin_unlock_irqrestore(hba->host->host_lock, flags);
789out:
790 return;
791}
792
793/* host lock must be held before calling this variant */
794static void __ufshcd_release(struct ufs_hba *hba)
795{
796 if (!ufshcd_is_clkgating_allowed(hba))
797 return;
798
799 hba->clk_gating.active_reqs--;
800
801 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
802 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
803 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200804 || hba->active_uic_cmd || hba->uic_async_done
805 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300806 return;
807
808 hba->clk_gating.state = REQ_CLKS_OFF;
809 schedule_delayed_work(&hba->clk_gating.gate_work,
810 msecs_to_jiffies(hba->clk_gating.delay_ms));
811}
812
813void ufshcd_release(struct ufs_hba *hba)
814{
815 unsigned long flags;
816
817 spin_lock_irqsave(hba->host->host_lock, flags);
818 __ufshcd_release(hba);
819 spin_unlock_irqrestore(hba->host->host_lock, flags);
820}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200821EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300822
823static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
824 struct device_attribute *attr, char *buf)
825{
826 struct ufs_hba *hba = dev_get_drvdata(dev);
827
828 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
829}
830
831static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
832 struct device_attribute *attr, const char *buf, size_t count)
833{
834 struct ufs_hba *hba = dev_get_drvdata(dev);
835 unsigned long flags, value;
836
837 if (kstrtoul(buf, 0, &value))
838 return -EINVAL;
839
840 spin_lock_irqsave(hba->host->host_lock, flags);
841 hba->clk_gating.delay_ms = value;
842 spin_unlock_irqrestore(hba->host->host_lock, flags);
843 return count;
844}
845
846static void ufshcd_init_clk_gating(struct ufs_hba *hba)
847{
848 if (!ufshcd_is_clkgating_allowed(hba))
849 return;
850
851 hba->clk_gating.delay_ms = 150;
852 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
853 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
854
855 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
856 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
857 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
858 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
859 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
860 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
861 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
862}
863
864static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
865{
866 if (!ufshcd_is_clkgating_allowed(hba))
867 return;
868 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900869 cancel_work_sync(&hba->clk_gating.ungate_work);
870 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300871}
872
Sahitya Tummala856b3482014-09-25 15:32:34 +0300873/* Must be called with host lock acquired */
874static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
875{
876 if (!ufshcd_is_clkscaling_enabled(hba))
877 return;
878
879 if (!hba->clk_scaling.is_busy_started) {
880 hba->clk_scaling.busy_start_t = ktime_get();
881 hba->clk_scaling.is_busy_started = true;
882 }
883}
884
885static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
886{
887 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
888
889 if (!ufshcd_is_clkscaling_enabled(hba))
890 return;
891
892 if (!hba->outstanding_reqs && scaling->is_busy_started) {
893 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
894 scaling->busy_start_t));
895 scaling->busy_start_t = ktime_set(0, 0);
896 scaling->is_busy_started = false;
897 }
898}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530899/**
900 * ufshcd_send_command - Send SCSI or device management commands
901 * @hba: per adapter instance
902 * @task_tag: Task tag of the command
903 */
904static inline
905void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
906{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300907 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530908 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530909 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -0700910 /* Make sure that doorbell is committed immediately */
911 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530912}
913
914/**
915 * ufshcd_copy_sense_data - Copy sense data in case of check condition
916 * @lrb - pointer to local reference block
917 */
918static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
919{
920 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530921 if (lrbp->sense_buffer &&
922 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -0700923 int len_to_copy;
924
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530925 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -0700926 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
927
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530928 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530929 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -0700930 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530931 }
932}
933
934/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530935 * ufshcd_copy_query_response() - Copy the Query Response and the data
936 * descriptor
937 * @hba: per adapter instance
938 * @lrb - pointer to local reference block
939 */
940static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300941int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530942{
943 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
944
Dolev Raviv68078d52013-07-30 00:35:58 +0530945 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530946
Dolev Raviv68078d52013-07-30 00:35:58 +0530947 /* Get the descriptor */
948 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300949 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530950 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300951 u16 resp_len;
952 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530953
954 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300955 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530956 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300957 buf_len = be16_to_cpu(
958 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300959 if (likely(buf_len >= resp_len)) {
960 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
961 } else {
962 dev_warn(hba->dev,
963 "%s: Response size is bigger than buffer",
964 __func__);
965 return -EINVAL;
966 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530967 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300968
969 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530970}
971
972/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530973 * ufshcd_hba_capabilities - Read controller capabilities
974 * @hba: per adapter instance
975 */
976static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
977{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530978 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530979
980 /* nutrs and nutmrs are 0 based values */
981 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
982 hba->nutmrs =
983 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
984}
985
986/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530987 * ufshcd_ready_for_uic_cmd - Check if controller is ready
988 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530989 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530990 * Return true on success, else false
991 */
992static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
993{
994 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
995 return true;
996 else
997 return false;
998}
999
1000/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301001 * ufshcd_get_upmcrs - Get the power mode change request status
1002 * @hba: Pointer to adapter instance
1003 *
1004 * This function gets the UPMCRS field of HCS register
1005 * Returns value of UPMCRS field
1006 */
1007static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1008{
1009 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1010}
1011
1012/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301013 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1014 * @hba: per adapter instance
1015 * @uic_cmd: UIC command
1016 *
1017 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301018 */
1019static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301020ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301021{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301022 WARN_ON(hba->active_uic_cmd);
1023
1024 hba->active_uic_cmd = uic_cmd;
1025
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301026 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301027 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1028 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1029 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301030
1031 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301032 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301033 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301034}
1035
1036/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301037 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1038 * @hba: per adapter instance
1039 * @uic_command: UIC command
1040 *
1041 * Must be called with mutex held.
1042 * Returns 0 only if success.
1043 */
1044static int
1045ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1046{
1047 int ret;
1048 unsigned long flags;
1049
1050 if (wait_for_completion_timeout(&uic_cmd->done,
1051 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1052 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1053 else
1054 ret = -ETIMEDOUT;
1055
1056 spin_lock_irqsave(hba->host->host_lock, flags);
1057 hba->active_uic_cmd = NULL;
1058 spin_unlock_irqrestore(hba->host->host_lock, flags);
1059
1060 return ret;
1061}
1062
1063/**
1064 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1065 * @hba: per adapter instance
1066 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001067 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301068 *
1069 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001070 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301071 * Returns 0 only if success.
1072 */
1073static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001074__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1075 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301076{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301077 if (!ufshcd_ready_for_uic_cmd(hba)) {
1078 dev_err(hba->dev,
1079 "Controller not ready to accept UIC commands\n");
1080 return -EIO;
1081 }
1082
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001083 if (completion)
1084 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301085
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301086 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301087
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001088 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301089}
1090
1091/**
1092 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1093 * @hba: per adapter instance
1094 * @uic_cmd: UIC command
1095 *
1096 * Returns 0 only if success.
1097 */
1098static int
1099ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1100{
1101 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001102 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301103
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001104 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301105 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001106 ufshcd_add_delay_before_dme_cmd(hba);
1107
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001108 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001109 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001110 spin_unlock_irqrestore(hba->host->host_lock, flags);
1111 if (!ret)
1112 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1113
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301114 mutex_unlock(&hba->uic_cmd_mutex);
1115
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001116 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301117 return ret;
1118}
1119
1120/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301121 * ufshcd_map_sg - Map scatter-gather list to prdt
1122 * @lrbp - pointer to local reference block
1123 *
1124 * Returns 0 in case of success, non-zero value in case of failure
1125 */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001126static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301127{
1128 struct ufshcd_sg_entry *prd_table;
1129 struct scatterlist *sg;
1130 struct scsi_cmnd *cmd;
1131 int sg_segments;
1132 int i;
1133
1134 cmd = lrbp->cmd;
1135 sg_segments = scsi_dma_map(cmd);
1136 if (sg_segments < 0)
1137 return sg_segments;
1138
1139 if (sg_segments) {
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001140 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1141 lrbp->utr_descriptor_ptr->prd_table_length =
1142 cpu_to_le16((u16)(sg_segments *
1143 sizeof(struct ufshcd_sg_entry)));
1144 else
1145 lrbp->utr_descriptor_ptr->prd_table_length =
1146 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301147
1148 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1149
1150 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1151 prd_table[i].size =
1152 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1153 prd_table[i].base_addr =
1154 cpu_to_le32(lower_32_bits(sg->dma_address));
1155 prd_table[i].upper_addr =
1156 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001157 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301158 }
1159 } else {
1160 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1161 }
1162
1163 return 0;
1164}
1165
1166/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301167 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301168 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301169 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301170 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301171static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301172{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301173 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1174
1175 if (hba->ufs_version == UFSHCI_VERSION_10) {
1176 u32 rw;
1177 rw = set & INTERRUPT_MASK_RW_VER_10;
1178 set = rw | ((set ^ intrs) & intrs);
1179 } else {
1180 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301181 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301182
1183 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1184}
1185
1186/**
1187 * ufshcd_disable_intr - disable interrupts
1188 * @hba: per adapter instance
1189 * @intrs: interrupt bits
1190 */
1191static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1192{
1193 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1194
1195 if (hba->ufs_version == UFSHCI_VERSION_10) {
1196 u32 rw;
1197 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1198 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1199 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1200
1201 } else {
1202 set &= ~intrs;
1203 }
1204
1205 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301206}
1207
1208/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301209 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1210 * descriptor according to request
1211 * @lrbp: pointer to local reference block
1212 * @upiu_flags: flags required in the header
1213 * @cmd_dir: requests data direction
1214 */
1215static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001216 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301217{
1218 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1219 u32 data_direction;
1220 u32 dword_0;
1221
1222 if (cmd_dir == DMA_FROM_DEVICE) {
1223 data_direction = UTP_DEVICE_TO_HOST;
1224 *upiu_flags = UPIU_CMD_FLAGS_READ;
1225 } else if (cmd_dir == DMA_TO_DEVICE) {
1226 data_direction = UTP_HOST_TO_DEVICE;
1227 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1228 } else {
1229 data_direction = UTP_NO_DATA_TRANSFER;
1230 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1231 }
1232
1233 dword_0 = data_direction | (lrbp->command_type
1234 << UPIU_COMMAND_TYPE_OFFSET);
1235 if (lrbp->intr_cmd)
1236 dword_0 |= UTP_REQ_DESC_INT_CMD;
1237
1238 /* Transfer request descriptor header fields */
1239 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001240 /* dword_1 is reserved, hence it is set to 0 */
1241 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301242 /*
1243 * assigning invalid value for command status. Controller
1244 * updates OCS on command completion, with the command
1245 * status
1246 */
1247 req_desc->header.dword_2 =
1248 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001249 /* dword_3 is reserved, hence it is set to 0 */
1250 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001251
1252 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301253}
1254
1255/**
1256 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1257 * for scsi commands
1258 * @lrbp - local reference block pointer
1259 * @upiu_flags - flags
1260 */
1261static
1262void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1263{
1264 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001265 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301266
1267 /* command descriptor fields */
1268 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1269 UPIU_TRANSACTION_COMMAND, upiu_flags,
1270 lrbp->lun, lrbp->task_tag);
1271 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1272 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1273
1274 /* Total EHS length and Data segment length will be zero */
1275 ucd_req_ptr->header.dword_2 = 0;
1276
1277 ucd_req_ptr->sc.exp_data_transfer_len =
1278 cpu_to_be32(lrbp->cmd->sdb.length);
1279
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001280 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1281 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1282 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1283
1284 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301285}
1286
Dolev Raviv68078d52013-07-30 00:35:58 +05301287/**
1288 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1289 * for query requsts
1290 * @hba: UFS hba
1291 * @lrbp: local reference block pointer
1292 * @upiu_flags: flags
1293 */
1294static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1295 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1296{
1297 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1298 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301299 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301300 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1301
1302 /* Query request header */
1303 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1304 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1305 lrbp->lun, lrbp->task_tag);
1306 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1307 0, query->request.query_func, 0, 0);
1308
Zang Leigang68612852016-08-25 17:39:19 +08001309 /* Data segment length only need for WRITE_DESC */
1310 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1311 ucd_req_ptr->header.dword_2 =
1312 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1313 else
1314 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301315
1316 /* Copy the Query Request buffer as is */
1317 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1318 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301319
1320 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001321 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1322 memcpy(descp, query->descriptor, len);
1323
Yaniv Gardi51047262016-02-01 15:02:38 +02001324 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301325}
1326
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301327static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1328{
1329 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1330
1331 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1332
1333 /* command descriptor fields */
1334 ucd_req_ptr->header.dword_0 =
1335 UPIU_HEADER_DWORD(
1336 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001337 /* clear rest of the fields of basic header */
1338 ucd_req_ptr->header.dword_1 = 0;
1339 ucd_req_ptr->header.dword_2 = 0;
1340
1341 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301342}
1343
1344/**
Joao Pinto300bb132016-05-11 12:21:27 +01001345 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1346 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301347 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301348 * @lrb - pointer to local reference block
1349 */
Joao Pinto300bb132016-05-11 12:21:27 +01001350static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301351{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301352 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301353 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301354
Joao Pinto300bb132016-05-11 12:21:27 +01001355 if (hba->ufs_version == UFSHCI_VERSION_20)
1356 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1357 else
1358 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1359
1360 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1361 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1362 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1363 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1364 ufshcd_prepare_utp_nop_upiu(lrbp);
1365 else
1366 ret = -EINVAL;
1367
1368 return ret;
1369}
1370
1371/**
1372 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1373 * for SCSI Purposes
1374 * @hba - per adapter instance
1375 * @lrb - pointer to local reference block
1376 */
1377static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1378{
1379 u32 upiu_flags;
1380 int ret = 0;
1381
1382 if (hba->ufs_version == UFSHCI_VERSION_20)
1383 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1384 else
1385 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1386
1387 if (likely(lrbp->cmd)) {
1388 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1389 lrbp->cmd->sc_data_direction);
1390 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1391 } else {
1392 ret = -EINVAL;
1393 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301394
1395 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301396}
1397
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001398/*
1399 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1400 * @scsi_lun: scsi LUN id
1401 *
1402 * Returns UPIU LUN id
1403 */
1404static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1405{
1406 if (scsi_is_wlun(scsi_lun))
1407 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1408 | UFS_UPIU_WLUN_ID;
1409 else
1410 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1411}
1412
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301413/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001414 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1415 * @scsi_lun: UPIU W-LUN id
1416 *
1417 * Returns SCSI W-LUN id
1418 */
1419static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1420{
1421 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1422}
1423
1424/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301425 * ufshcd_queuecommand - main entry point for SCSI requests
1426 * @cmd: command from SCSI Midlayer
1427 * @done: call back function
1428 *
1429 * Returns 0 for success, non-zero in case of failure
1430 */
1431static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1432{
1433 struct ufshcd_lrb *lrbp;
1434 struct ufs_hba *hba;
1435 unsigned long flags;
1436 int tag;
1437 int err = 0;
1438
1439 hba = shost_priv(host);
1440
1441 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001442 if (!ufshcd_valid_tag(hba, tag)) {
1443 dev_err(hba->dev,
1444 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1445 __func__, tag, cmd, cmd->request);
1446 BUG();
1447 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301448
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301449 spin_lock_irqsave(hba->host->host_lock, flags);
1450 switch (hba->ufshcd_state) {
1451 case UFSHCD_STATE_OPERATIONAL:
1452 break;
Zang Leigang141f8162016-11-16 11:29:37 +08001453 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301454 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301455 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301456 goto out_unlock;
1457 case UFSHCD_STATE_ERROR:
1458 set_host_byte(cmd, DID_ERROR);
1459 cmd->scsi_done(cmd);
1460 goto out_unlock;
1461 default:
1462 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1463 __func__, hba->ufshcd_state);
1464 set_host_byte(cmd, DID_BAD_TARGET);
1465 cmd->scsi_done(cmd);
1466 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301467 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001468
1469 /* if error handling is in progress, don't issue commands */
1470 if (ufshcd_eh_in_progress(hba)) {
1471 set_host_byte(cmd, DID_ERROR);
1472 cmd->scsi_done(cmd);
1473 goto out_unlock;
1474 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301475 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301476
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301477 /* acquire the tag to make sure device cmds don't use it */
1478 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1479 /*
1480 * Dev manage command in progress, requeue the command.
1481 * Requeuing the command helps in cases where the request *may*
1482 * find different tag instead of waiting for dev manage command
1483 * completion.
1484 */
1485 err = SCSI_MLQUEUE_HOST_BUSY;
1486 goto out;
1487 }
1488
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001489 err = ufshcd_hold(hba, true);
1490 if (err) {
1491 err = SCSI_MLQUEUE_HOST_BUSY;
1492 clear_bit_unlock(tag, &hba->lrb_in_use);
1493 goto out;
1494 }
1495 WARN_ON(hba->clk_gating.state != CLKS_ON);
1496
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301497 lrbp = &hba->lrb[tag];
1498
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301499 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301500 lrbp->cmd = cmd;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001501 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301502 lrbp->sense_buffer = cmd->sense_buffer;
1503 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001504 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001505 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301506
Joao Pinto300bb132016-05-11 12:21:27 +01001507 ufshcd_comp_scsi_upiu(hba, lrbp);
1508
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09001509 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301510 if (err) {
1511 lrbp->cmd = NULL;
1512 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301513 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301514 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001515 /* Make sure descriptors are ready before ringing the doorbell */
1516 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301517
1518 /* issue command to the controller */
1519 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001520 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301521 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301522out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301523 spin_unlock_irqrestore(hba->host->host_lock, flags);
1524out:
1525 return err;
1526}
1527
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301528static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1529 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1530{
1531 lrbp->cmd = NULL;
1532 lrbp->sense_bufflen = 0;
1533 lrbp->sense_buffer = NULL;
1534 lrbp->task_tag = tag;
1535 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301536 lrbp->intr_cmd = true; /* No interrupt aggregation */
1537 hba->dev_cmd.type = cmd_type;
1538
Joao Pinto300bb132016-05-11 12:21:27 +01001539 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301540}
1541
1542static int
1543ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1544{
1545 int err = 0;
1546 unsigned long flags;
1547 u32 mask = 1 << tag;
1548
1549 /* clear outstanding transaction before retry */
1550 spin_lock_irqsave(hba->host->host_lock, flags);
1551 ufshcd_utrl_clear(hba, tag);
1552 spin_unlock_irqrestore(hba->host->host_lock, flags);
1553
1554 /*
1555 * wait for for h/w to clear corresponding bit in door-bell.
1556 * max. wait is 1 sec.
1557 */
1558 err = ufshcd_wait_for_register(hba,
1559 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001560 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301561
1562 return err;
1563}
1564
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001565static int
1566ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1567{
1568 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1569
1570 /* Get the UPIU response */
1571 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1572 UPIU_RSP_CODE_OFFSET;
1573 return query_res->response;
1574}
1575
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301576/**
1577 * ufshcd_dev_cmd_completion() - handles device management command responses
1578 * @hba: per adapter instance
1579 * @lrbp: pointer to local reference block
1580 */
1581static int
1582ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1583{
1584 int resp;
1585 int err = 0;
1586
1587 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1588
1589 switch (resp) {
1590 case UPIU_TRANSACTION_NOP_IN:
1591 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1592 err = -EINVAL;
1593 dev_err(hba->dev, "%s: unexpected response %x\n",
1594 __func__, resp);
1595 }
1596 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301597 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001598 err = ufshcd_check_query_response(hba, lrbp);
1599 if (!err)
1600 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301601 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301602 case UPIU_TRANSACTION_REJECT_UPIU:
1603 /* TODO: handle Reject UPIU Response */
1604 err = -EPERM;
1605 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1606 __func__);
1607 break;
1608 default:
1609 err = -EINVAL;
1610 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1611 __func__, resp);
1612 break;
1613 }
1614
1615 return err;
1616}
1617
1618static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1619 struct ufshcd_lrb *lrbp, int max_timeout)
1620{
1621 int err = 0;
1622 unsigned long time_left;
1623 unsigned long flags;
1624
1625 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1626 msecs_to_jiffies(max_timeout));
1627
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001628 /* Make sure descriptors are ready before ringing the doorbell */
1629 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301630 spin_lock_irqsave(hba->host->host_lock, flags);
1631 hba->dev_cmd.complete = NULL;
1632 if (likely(time_left)) {
1633 err = ufshcd_get_tr_ocs(lrbp);
1634 if (!err)
1635 err = ufshcd_dev_cmd_completion(hba, lrbp);
1636 }
1637 spin_unlock_irqrestore(hba->host->host_lock, flags);
1638
1639 if (!time_left) {
1640 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001641 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1642 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301643 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001644 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301645 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001646 /*
1647 * in case of an error, after clearing the doorbell,
1648 * we also need to clear the outstanding_request
1649 * field in hba
1650 */
1651 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301652 }
1653
1654 return err;
1655}
1656
1657/**
1658 * ufshcd_get_dev_cmd_tag - Get device management command tag
1659 * @hba: per-adapter instance
1660 * @tag: pointer to variable with available slot value
1661 *
1662 * Get a free slot and lock it until device management command
1663 * completes.
1664 *
1665 * Returns false if free slot is unavailable for locking, else
1666 * return true with tag value in @tag.
1667 */
1668static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1669{
1670 int tag;
1671 bool ret = false;
1672 unsigned long tmp;
1673
1674 if (!tag_out)
1675 goto out;
1676
1677 do {
1678 tmp = ~hba->lrb_in_use;
1679 tag = find_last_bit(&tmp, hba->nutrs);
1680 if (tag >= hba->nutrs)
1681 goto out;
1682 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1683
1684 *tag_out = tag;
1685 ret = true;
1686out:
1687 return ret;
1688}
1689
1690static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1691{
1692 clear_bit_unlock(tag, &hba->lrb_in_use);
1693}
1694
1695/**
1696 * ufshcd_exec_dev_cmd - API for sending device management requests
1697 * @hba - UFS hba
1698 * @cmd_type - specifies the type (NOP, Query...)
1699 * @timeout - time in seconds
1700 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301701 * NOTE: Since there is only one available tag for device management commands,
1702 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301703 */
1704static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1705 enum dev_cmd_type cmd_type, int timeout)
1706{
1707 struct ufshcd_lrb *lrbp;
1708 int err;
1709 int tag;
1710 struct completion wait;
1711 unsigned long flags;
1712
1713 /*
1714 * Get free slot, sleep if slots are unavailable.
1715 * Even though we use wait_event() which sleeps indefinitely,
1716 * the maximum wait time is bounded by SCSI request timeout.
1717 */
1718 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1719
1720 init_completion(&wait);
1721 lrbp = &hba->lrb[tag];
1722 WARN_ON(lrbp->cmd);
1723 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1724 if (unlikely(err))
1725 goto out_put_tag;
1726
1727 hba->dev_cmd.complete = &wait;
1728
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001729 /* Make sure descriptors are ready before ringing the doorbell */
1730 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301731 spin_lock_irqsave(hba->host->host_lock, flags);
Kiwoong Kim0e675ef2016-11-10 21:14:36 +09001732 ufshcd_vops_setup_xfer_req(hba, tag, (lrbp->cmd ? true : false));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301733 ufshcd_send_command(hba, tag);
1734 spin_unlock_irqrestore(hba->host->host_lock, flags);
1735
1736 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1737
1738out_put_tag:
1739 ufshcd_put_dev_cmd_tag(hba, tag);
1740 wake_up(&hba->dev_cmd.tag_wq);
1741 return err;
1742}
1743
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301744/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001745 * ufshcd_init_query() - init the query response and request parameters
1746 * @hba: per-adapter instance
1747 * @request: address of the request pointer to be initialized
1748 * @response: address of the response pointer to be initialized
1749 * @opcode: operation to perform
1750 * @idn: flag idn to access
1751 * @index: LU number to access
1752 * @selector: query/flag/descriptor further identification
1753 */
1754static inline void ufshcd_init_query(struct ufs_hba *hba,
1755 struct ufs_query_req **request, struct ufs_query_res **response,
1756 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1757{
1758 *request = &hba->dev_cmd.query.request;
1759 *response = &hba->dev_cmd.query.response;
1760 memset(*request, 0, sizeof(struct ufs_query_req));
1761 memset(*response, 0, sizeof(struct ufs_query_res));
1762 (*request)->upiu_req.opcode = opcode;
1763 (*request)->upiu_req.idn = idn;
1764 (*request)->upiu_req.index = index;
1765 (*request)->upiu_req.selector = selector;
1766}
1767
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001768static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1769 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1770{
1771 int ret;
1772 int retries;
1773
1774 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1775 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1776 if (ret)
1777 dev_dbg(hba->dev,
1778 "%s: failed with error %d, retries %d\n",
1779 __func__, ret, retries);
1780 else
1781 break;
1782 }
1783
1784 if (ret)
1785 dev_err(hba->dev,
1786 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1787 __func__, opcode, idn, ret, retries);
1788 return ret;
1789}
1790
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001791/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301792 * ufshcd_query_flag() - API function for sending flag query requests
1793 * hba: per-adapter instance
1794 * query_opcode: flag query to perform
1795 * idn: flag idn to access
1796 * flag_res: the flag value after the query request completes
1797 *
1798 * Returns 0 for success, non-zero in case of failure
1799 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001800int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301801 enum flag_idn idn, bool *flag_res)
1802{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001803 struct ufs_query_req *request = NULL;
1804 struct ufs_query_res *response = NULL;
1805 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001806 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301807
1808 BUG_ON(!hba);
1809
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001810 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301811 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001812 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1813 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301814
1815 switch (opcode) {
1816 case UPIU_QUERY_OPCODE_SET_FLAG:
1817 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1818 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1819 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1820 break;
1821 case UPIU_QUERY_OPCODE_READ_FLAG:
1822 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1823 if (!flag_res) {
1824 /* No dummy reads */
1825 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1826 __func__);
1827 err = -EINVAL;
1828 goto out_unlock;
1829 }
1830 break;
1831 default:
1832 dev_err(hba->dev,
1833 "%s: Expected query flag opcode but got = %d\n",
1834 __func__, opcode);
1835 err = -EINVAL;
1836 goto out_unlock;
1837 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301838
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001839 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301840
1841 if (err) {
1842 dev_err(hba->dev,
1843 "%s: Sending flag query for idn %d failed, err = %d\n",
1844 __func__, idn, err);
1845 goto out_unlock;
1846 }
1847
1848 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301849 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301850 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1851
1852out_unlock:
1853 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001854 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301855 return err;
1856}
1857
1858/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301859 * ufshcd_query_attr - API function for sending attribute requests
1860 * hba: per-adapter instance
1861 * opcode: attribute opcode
1862 * idn: attribute idn to access
1863 * index: index field
1864 * selector: selector field
1865 * attr_val: the attribute value after the query request completes
1866 *
1867 * Returns 0 for success, non-zero in case of failure
1868*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301869static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301870 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1871{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001872 struct ufs_query_req *request = NULL;
1873 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301874 int err;
1875
1876 BUG_ON(!hba);
1877
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001878 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301879 if (!attr_val) {
1880 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1881 __func__, opcode);
1882 err = -EINVAL;
1883 goto out;
1884 }
1885
1886 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001887 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1888 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301889
1890 switch (opcode) {
1891 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1892 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301893 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301894 break;
1895 case UPIU_QUERY_OPCODE_READ_ATTR:
1896 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1897 break;
1898 default:
1899 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1900 __func__, opcode);
1901 err = -EINVAL;
1902 goto out_unlock;
1903 }
1904
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001905 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301906
1907 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08001908 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
1909 __func__, opcode, idn, index, err);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301910 goto out_unlock;
1911 }
1912
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301913 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301914
1915out_unlock:
1916 mutex_unlock(&hba->dev_cmd.lock);
1917out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001918 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301919 return err;
1920}
1921
1922/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001923 * ufshcd_query_attr_retry() - API function for sending query
1924 * attribute with retries
1925 * @hba: per-adapter instance
1926 * @opcode: attribute opcode
1927 * @idn: attribute idn to access
1928 * @index: index field
1929 * @selector: selector field
1930 * @attr_val: the attribute value after the query request
1931 * completes
1932 *
1933 * Returns 0 for success, non-zero in case of failure
1934*/
1935static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1936 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1937 u32 *attr_val)
1938{
1939 int ret = 0;
1940 u32 retries;
1941
1942 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1943 ret = ufshcd_query_attr(hba, opcode, idn, index,
1944 selector, attr_val);
1945 if (ret)
1946 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1947 __func__, ret, retries);
1948 else
1949 break;
1950 }
1951
1952 if (ret)
1953 dev_err(hba->dev,
1954 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1955 __func__, idn, ret, QUERY_REQ_RETRIES);
1956 return ret;
1957}
1958
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001959static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001960 enum query_opcode opcode, enum desc_idn idn, u8 index,
1961 u8 selector, u8 *desc_buf, int *buf_len)
1962{
1963 struct ufs_query_req *request = NULL;
1964 struct ufs_query_res *response = NULL;
1965 int err;
1966
1967 BUG_ON(!hba);
1968
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001969 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001970 if (!desc_buf) {
1971 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1972 __func__, opcode);
1973 err = -EINVAL;
1974 goto out;
1975 }
1976
1977 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1978 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1979 __func__, *buf_len);
1980 err = -EINVAL;
1981 goto out;
1982 }
1983
1984 mutex_lock(&hba->dev_cmd.lock);
1985 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1986 selector);
1987 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001988 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001989
1990 switch (opcode) {
1991 case UPIU_QUERY_OPCODE_WRITE_DESC:
1992 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1993 break;
1994 case UPIU_QUERY_OPCODE_READ_DESC:
1995 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1996 break;
1997 default:
1998 dev_err(hba->dev,
1999 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2000 __func__, opcode);
2001 err = -EINVAL;
2002 goto out_unlock;
2003 }
2004
2005 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2006
2007 if (err) {
Yaniv Gardi4b761b52016-11-23 16:31:18 -08002008 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2009 __func__, opcode, idn, index, err);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002010 goto out_unlock;
2011 }
2012
2013 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002014 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002015
2016out_unlock:
2017 mutex_unlock(&hba->dev_cmd.lock);
2018out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002019 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002020 return err;
2021}
2022
2023/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002024 * ufshcd_query_descriptor_retry - API function for sending descriptor
2025 * requests
2026 * hba: per-adapter instance
2027 * opcode: attribute opcode
2028 * idn: attribute idn to access
2029 * index: index field
2030 * selector: selector field
2031 * desc_buf: the buffer that contains the descriptor
2032 * buf_len: length parameter passed to the device
2033 *
2034 * Returns 0 for success, non-zero in case of failure.
2035 * The buf_len parameter will contain, on return, the length parameter
2036 * received on the response.
2037 */
2038int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2039 enum query_opcode opcode, enum desc_idn idn, u8 index,
2040 u8 selector, u8 *desc_buf, int *buf_len)
2041{
2042 int err;
2043 int retries;
2044
2045 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2046 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2047 selector, desc_buf, buf_len);
2048 if (!err || err == -EINVAL)
2049 break;
2050 }
2051
2052 return err;
2053}
2054EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2055
2056/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002057 * ufshcd_read_desc_param - read the specified descriptor parameter
2058 * @hba: Pointer to adapter instance
2059 * @desc_id: descriptor idn value
2060 * @desc_index: descriptor index
2061 * @param_offset: offset of the parameter to read
2062 * @param_read_buf: pointer to buffer where parameter would be read
2063 * @param_size: sizeof(param_read_buf)
2064 *
2065 * Return 0 in case of success, non-zero otherwise
2066 */
2067static int ufshcd_read_desc_param(struct ufs_hba *hba,
2068 enum desc_idn desc_id,
2069 int desc_index,
2070 u32 param_offset,
2071 u8 *param_read_buf,
2072 u32 param_size)
2073{
2074 int ret;
2075 u8 *desc_buf;
2076 u32 buff_len;
2077 bool is_kmalloc = true;
2078
2079 /* safety checks */
2080 if (desc_id >= QUERY_DESC_IDN_MAX)
2081 return -EINVAL;
2082
2083 buff_len = ufs_query_desc_max_size[desc_id];
2084 if ((param_offset + param_size) > buff_len)
2085 return -EINVAL;
2086
2087 if (!param_offset && (param_size == buff_len)) {
2088 /* memory space already available to hold full descriptor */
2089 desc_buf = param_read_buf;
2090 is_kmalloc = false;
2091 } else {
2092 /* allocate memory to hold full descriptor */
2093 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2094 if (!desc_buf)
2095 return -ENOMEM;
2096 }
2097
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002098 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2099 desc_id, desc_index, 0, desc_buf,
2100 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002101
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002102 if (ret) {
2103 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2104 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002105
2106 goto out;
2107 }
2108
subhashj@codeaurora.orgbde44bb2016-11-23 16:31:41 -08002109 /* Sanity check */
2110 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2111 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2112 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2113 ret = -EINVAL;
2114 goto out;
2115 }
2116
2117 /*
2118 * While reading variable size descriptors (like string descriptor),
2119 * some UFS devices may report the "LENGTH" (field in "Transaction
2120 * Specific fields" of Query Response UPIU) same as what was requested
2121 * in Query Request UPIU instead of reporting the actual size of the
2122 * variable size descriptor.
2123 * Although it's safe to ignore the "LENGTH" field for variable size
2124 * descriptors as we can always derive the length of the descriptor from
2125 * the descriptor header fields. Hence this change impose the length
2126 * match check only for fixed size descriptors (for which we always
2127 * request the correct size as part of Query Request UPIU).
2128 */
2129 if ((desc_id != QUERY_DESC_IDN_STRING) &&
2130 (buff_len != desc_buf[QUERY_DESC_LENGTH_OFFSET])) {
2131 dev_err(hba->dev, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
2132 __func__, buff_len, desc_buf[QUERY_DESC_LENGTH_OFFSET]);
2133 ret = -EINVAL;
2134 goto out;
2135 }
2136
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002137 if (is_kmalloc)
2138 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2139out:
2140 if (is_kmalloc)
2141 kfree(desc_buf);
2142 return ret;
2143}
2144
2145static inline int ufshcd_read_desc(struct ufs_hba *hba,
2146 enum desc_idn desc_id,
2147 int desc_index,
2148 u8 *buf,
2149 u32 size)
2150{
2151 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2152}
2153
2154static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2155 u8 *buf,
2156 u32 size)
2157{
Dolev Raviv61e07352016-11-23 16:30:49 -08002158 int err = 0;
2159 int retries;
2160
2161 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2162 /* Read descriptor*/
2163 err = ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2164 if (!err)
2165 break;
2166 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2167 }
2168
2169 return err;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002170}
2171
Yaniv Gardib573d482016-03-10 17:37:09 +02002172int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2173{
2174 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2175}
2176EXPORT_SYMBOL(ufshcd_read_device_desc);
2177
2178/**
2179 * ufshcd_read_string_desc - read string descriptor
2180 * @hba: pointer to adapter instance
2181 * @desc_index: descriptor index
2182 * @buf: pointer to buffer where descriptor would be read
2183 * @size: size of buf
2184 * @ascii: if true convert from unicode to ascii characters
2185 *
2186 * Return 0 in case of success, non-zero otherwise
2187 */
2188int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2189 u32 size, bool ascii)
2190{
2191 int err = 0;
2192
2193 err = ufshcd_read_desc(hba,
2194 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2195
2196 if (err) {
2197 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2198 __func__, QUERY_REQ_RETRIES, err);
2199 goto out;
2200 }
2201
2202 if (ascii) {
2203 int desc_len;
2204 int ascii_len;
2205 int i;
2206 char *buff_ascii;
2207
2208 desc_len = buf[0];
2209 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2210 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2211 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2212 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2213 __func__);
2214 err = -ENOMEM;
2215 goto out;
2216 }
2217
2218 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2219 if (!buff_ascii) {
2220 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002221 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002222 }
2223
2224 /*
2225 * the descriptor contains string in UTF16 format
2226 * we need to convert to utf-8 so it can be displayed
2227 */
2228 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2229 desc_len - QUERY_DESC_HDR_SIZE,
2230 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2231
2232 /* replace non-printable or non-ASCII characters with spaces */
2233 for (i = 0; i < ascii_len; i++)
2234 ufshcd_remove_non_printable(&buff_ascii[i]);
2235
2236 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2237 size - QUERY_DESC_HDR_SIZE);
2238 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2239 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002240 kfree(buff_ascii);
2241 }
2242out:
2243 return err;
2244}
2245EXPORT_SYMBOL(ufshcd_read_string_desc);
2246
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002247/**
2248 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2249 * @hba: Pointer to adapter instance
2250 * @lun: lun id
2251 * @param_offset: offset of the parameter to read
2252 * @param_read_buf: pointer to buffer where parameter would be read
2253 * @param_size: sizeof(param_read_buf)
2254 *
2255 * Return 0 in case of success, non-zero otherwise
2256 */
2257static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2258 int lun,
2259 enum unit_desc_param param_offset,
2260 u8 *param_read_buf,
2261 u32 param_size)
2262{
2263 /*
2264 * Unit descriptors are only available for general purpose LUs (LUN id
2265 * from 0 to 7) and RPMB Well known LU.
2266 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002267 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002268 return -EOPNOTSUPP;
2269
2270 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2271 param_offset, param_read_buf, param_size);
2272}
2273
2274/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302275 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2276 * @hba: per adapter instance
2277 *
2278 * 1. Allocate DMA memory for Command Descriptor array
2279 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2280 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2281 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2282 * (UTMRDL)
2283 * 4. Allocate memory for local reference block(lrb).
2284 *
2285 * Returns 0 for success, non-zero in case of failure
2286 */
2287static int ufshcd_memory_alloc(struct ufs_hba *hba)
2288{
2289 size_t utmrdl_size, utrdl_size, ucdl_size;
2290
2291 /* Allocate memory for UTP command descriptors */
2292 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002293 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2294 ucdl_size,
2295 &hba->ucdl_dma_addr,
2296 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302297
2298 /*
2299 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2300 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2301 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2302 * be aligned to 128 bytes as well
2303 */
2304 if (!hba->ucdl_base_addr ||
2305 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302306 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302307 "Command Descriptor Memory allocation failed\n");
2308 goto out;
2309 }
2310
2311 /*
2312 * Allocate memory for UTP Transfer descriptors
2313 * UFSHCI requires 1024 byte alignment of UTRD
2314 */
2315 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002316 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2317 utrdl_size,
2318 &hba->utrdl_dma_addr,
2319 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302320 if (!hba->utrdl_base_addr ||
2321 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302322 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302323 "Transfer Descriptor Memory allocation failed\n");
2324 goto out;
2325 }
2326
2327 /*
2328 * Allocate memory for UTP Task Management descriptors
2329 * UFSHCI requires 1024 byte alignment of UTMRD
2330 */
2331 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002332 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2333 utmrdl_size,
2334 &hba->utmrdl_dma_addr,
2335 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302336 if (!hba->utmrdl_base_addr ||
2337 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302338 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302339 "Task Management Descriptor Memory allocation failed\n");
2340 goto out;
2341 }
2342
2343 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002344 hba->lrb = devm_kzalloc(hba->dev,
2345 hba->nutrs * sizeof(struct ufshcd_lrb),
2346 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302347 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302348 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302349 goto out;
2350 }
2351 return 0;
2352out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302353 return -ENOMEM;
2354}
2355
2356/**
2357 * ufshcd_host_memory_configure - configure local reference block with
2358 * memory offsets
2359 * @hba: per adapter instance
2360 *
2361 * Configure Host memory space
2362 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2363 * address.
2364 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2365 * and PRDT offset.
2366 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2367 * into local reference block.
2368 */
2369static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2370{
2371 struct utp_transfer_cmd_desc *cmd_descp;
2372 struct utp_transfer_req_desc *utrdlp;
2373 dma_addr_t cmd_desc_dma_addr;
2374 dma_addr_t cmd_desc_element_addr;
2375 u16 response_offset;
2376 u16 prdt_offset;
2377 int cmd_desc_size;
2378 int i;
2379
2380 utrdlp = hba->utrdl_base_addr;
2381 cmd_descp = hba->ucdl_base_addr;
2382
2383 response_offset =
2384 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2385 prdt_offset =
2386 offsetof(struct utp_transfer_cmd_desc, prd_table);
2387
2388 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2389 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2390
2391 for (i = 0; i < hba->nutrs; i++) {
2392 /* Configure UTRD with command descriptor base address */
2393 cmd_desc_element_addr =
2394 (cmd_desc_dma_addr + (cmd_desc_size * i));
2395 utrdlp[i].command_desc_base_addr_lo =
2396 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2397 utrdlp[i].command_desc_base_addr_hi =
2398 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2399
2400 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002401 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2402 utrdlp[i].response_upiu_offset =
2403 cpu_to_le16(response_offset);
2404 utrdlp[i].prd_table_offset =
2405 cpu_to_le16(prdt_offset);
2406 utrdlp[i].response_upiu_length =
2407 cpu_to_le16(ALIGNED_UPIU_SIZE);
2408 } else {
2409 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302410 cpu_to_le16((response_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002411 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302412 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002413 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302414 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim75b1cc42016-11-22 17:06:59 +09002415 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302416
2417 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302418 hba->lrb[i].ucd_req_ptr =
2419 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302420 hba->lrb[i].ucd_rsp_ptr =
2421 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2422 hba->lrb[i].ucd_prdt_ptr =
2423 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2424 }
2425}
2426
2427/**
2428 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2429 * @hba: per adapter instance
2430 *
2431 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2432 * in order to initialize the Unipro link startup procedure.
2433 * Once the Unipro links are up, the device connected to the controller
2434 * is detected.
2435 *
2436 * Returns 0 on success, non-zero value on failure
2437 */
2438static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2439{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302440 struct uic_command uic_cmd = {0};
2441 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302442
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302443 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2444
2445 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2446 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302447 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302448 "dme-link-startup: error code %d\n", ret);
2449 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302450}
2451
Yaniv Gardicad2e032015-03-31 17:37:14 +03002452static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2453{
2454 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2455 unsigned long min_sleep_time_us;
2456
2457 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2458 return;
2459
2460 /*
2461 * last_dme_cmd_tstamp will be 0 only for 1st call to
2462 * this function
2463 */
2464 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2465 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2466 } else {
2467 unsigned long delta =
2468 (unsigned long) ktime_to_us(
2469 ktime_sub(ktime_get(),
2470 hba->last_dme_cmd_tstamp));
2471
2472 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2473 min_sleep_time_us =
2474 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2475 else
2476 return; /* no more delay required */
2477 }
2478
2479 /* allow sleep for extra 50us if needed */
2480 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2481}
2482
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302483/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302484 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2485 * @hba: per adapter instance
2486 * @attr_sel: uic command argument1
2487 * @attr_set: attribute set type as uic command argument2
2488 * @mib_val: setting value as uic command argument3
2489 * @peer: indicate whether peer or local
2490 *
2491 * Returns 0 on success, non-zero value on failure
2492 */
2493int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2494 u8 attr_set, u32 mib_val, u8 peer)
2495{
2496 struct uic_command uic_cmd = {0};
2497 static const char *const action[] = {
2498 "dme-set",
2499 "dme-peer-set"
2500 };
2501 const char *set = action[!!peer];
2502 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002503 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302504
2505 uic_cmd.command = peer ?
2506 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2507 uic_cmd.argument1 = attr_sel;
2508 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2509 uic_cmd.argument3 = mib_val;
2510
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002511 do {
2512 /* for peer attributes we retry upon failure */
2513 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2514 if (ret)
2515 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2516 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2517 } while (ret && peer && --retries);
2518
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002519 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002520 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002521 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2522 UFS_UIC_COMMAND_RETRIES - retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302523
2524 return ret;
2525}
2526EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2527
2528/**
2529 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2530 * @hba: per adapter instance
2531 * @attr_sel: uic command argument1
2532 * @mib_val: the value of the attribute as returned by the UIC command
2533 * @peer: indicate whether peer or local
2534 *
2535 * Returns 0 on success, non-zero value on failure
2536 */
2537int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2538 u32 *mib_val, u8 peer)
2539{
2540 struct uic_command uic_cmd = {0};
2541 static const char *const action[] = {
2542 "dme-get",
2543 "dme-peer-get"
2544 };
2545 const char *get = action[!!peer];
2546 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002547 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002548 struct ufs_pa_layer_attr orig_pwr_info;
2549 struct ufs_pa_layer_attr temp_pwr_info;
2550 bool pwr_mode_change = false;
2551
2552 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2553 orig_pwr_info = hba->pwr_info;
2554 temp_pwr_info = orig_pwr_info;
2555
2556 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2557 orig_pwr_info.pwr_rx == FAST_MODE) {
2558 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2559 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2560 pwr_mode_change = true;
2561 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2562 orig_pwr_info.pwr_rx == SLOW_MODE) {
2563 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2564 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2565 pwr_mode_change = true;
2566 }
2567 if (pwr_mode_change) {
2568 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2569 if (ret)
2570 goto out;
2571 }
2572 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302573
2574 uic_cmd.command = peer ?
2575 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2576 uic_cmd.argument1 = attr_sel;
2577
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002578 do {
2579 /* for peer attributes we retry upon failure */
2580 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2581 if (ret)
2582 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2583 get, UIC_GET_ATTR_ID(attr_sel), ret);
2584 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302585
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002586 if (ret)
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002587 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
Yaniv Gardif37e9f82016-11-23 16:32:49 -08002588 get, UIC_GET_ATTR_ID(attr_sel),
2589 UFS_UIC_COMMAND_RETRIES - retries);
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002590
2591 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302592 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002593
2594 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2595 && pwr_mode_change)
2596 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302597out:
2598 return ret;
2599}
2600EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2601
2602/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002603 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2604 * state) and waits for it to take effect.
2605 *
2606 * @hba: per adapter instance
2607 * @cmd: UIC command to execute
2608 *
2609 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2610 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2611 * and device UniPro link and hence it's final completion would be indicated by
2612 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2613 * addition to normal UIC command completion Status (UCCS). This function only
2614 * returns after the relevant status bits indicate the completion.
2615 *
2616 * Returns 0 on success, non-zero value on failure
2617 */
2618static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2619{
2620 struct completion uic_async_done;
2621 unsigned long flags;
2622 u8 status;
2623 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002624 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002625
2626 mutex_lock(&hba->uic_cmd_mutex);
2627 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002628 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002629
2630 spin_lock_irqsave(hba->host->host_lock, flags);
2631 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002632 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2633 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2634 /*
2635 * Make sure UIC command completion interrupt is disabled before
2636 * issuing UIC command.
2637 */
2638 wmb();
2639 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002640 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002641 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2642 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002643 if (ret) {
2644 dev_err(hba->dev,
2645 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2646 cmd->command, cmd->argument3, ret);
2647 goto out;
2648 }
2649
2650 if (!wait_for_completion_timeout(hba->uic_async_done,
2651 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2652 dev_err(hba->dev,
2653 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2654 cmd->command, cmd->argument3);
2655 ret = -ETIMEDOUT;
2656 goto out;
2657 }
2658
2659 status = ufshcd_get_upmcrs(hba);
2660 if (status != PWR_LOCAL) {
2661 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002662 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002663 cmd->command, status);
2664 ret = (status != PWR_OK) ? status : -1;
2665 }
2666out:
2667 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002668 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002669 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002670 if (reenable_intr)
2671 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002672 spin_unlock_irqrestore(hba->host->host_lock, flags);
2673 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002674
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002675 return ret;
2676}
2677
2678/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302679 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2680 * using DME_SET primitives.
2681 * @hba: per adapter instance
2682 * @mode: powr mode value
2683 *
2684 * Returns 0 on success, non-zero value on failure
2685 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302686static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302687{
2688 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002689 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302690
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002691 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2692 ret = ufshcd_dme_set(hba,
2693 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2694 if (ret) {
2695 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2696 __func__, ret);
2697 goto out;
2698 }
2699 }
2700
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302701 uic_cmd.command = UIC_CMD_DME_SET;
2702 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2703 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002704 ufshcd_hold(hba, false);
2705 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2706 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302707
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002708out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002709 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002710}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302711
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002712static int ufshcd_link_recovery(struct ufs_hba *hba)
2713{
2714 int ret;
2715 unsigned long flags;
2716
2717 spin_lock_irqsave(hba->host->host_lock, flags);
2718 hba->ufshcd_state = UFSHCD_STATE_RESET;
2719 ufshcd_set_eh_in_progress(hba);
2720 spin_unlock_irqrestore(hba->host->host_lock, flags);
2721
2722 ret = ufshcd_host_reset_and_restore(hba);
2723
2724 spin_lock_irqsave(hba->host->host_lock, flags);
2725 if (ret)
2726 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2727 ufshcd_clear_eh_in_progress(hba);
2728 spin_unlock_irqrestore(hba->host->host_lock, flags);
2729
2730 if (ret)
2731 dev_err(hba->dev, "%s: link recovery failed, err %d",
2732 __func__, ret);
2733
2734 return ret;
2735}
2736
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002737static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002738{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002739 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002740 struct uic_command uic_cmd = {0};
2741
Kiwoong Kimee32c902016-11-10 21:17:43 +09002742 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
2743
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002744 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002745 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002746
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002747 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002748 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2749 __func__, ret);
2750
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002751 /*
2752 * If link recovery fails then return error so that caller
2753 * don't retry the hibern8 enter again.
2754 */
2755 if (ufshcd_link_recovery(hba))
2756 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09002757 } else
2758 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
2759 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002760
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002761 return ret;
2762}
2763
2764static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2765{
2766 int ret = 0, retries;
2767
2768 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2769 ret = __ufshcd_uic_hibern8_enter(hba);
2770 if (!ret || ret == -ENOLINK)
2771 goto out;
2772 }
2773out:
2774 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002775}
2776
2777static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2778{
2779 struct uic_command uic_cmd = {0};
2780 int ret;
2781
Kiwoong Kimee32c902016-11-10 21:17:43 +09002782 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
2783
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002784 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2785 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302786 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002787 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2788 __func__, ret);
2789 ret = ufshcd_link_recovery(hba);
Kiwoong Kimee32c902016-11-10 21:17:43 +09002790 } else
2791 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
2792 POST_CHANGE);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302793
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302794 return ret;
2795}
2796
Yaniv Gardi50646362014-10-23 13:25:13 +03002797 /**
2798 * ufshcd_init_pwr_info - setting the POR (power on reset)
2799 * values in hba power info
2800 * @hba: per-adapter instance
2801 */
2802static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2803{
2804 hba->pwr_info.gear_rx = UFS_PWM_G1;
2805 hba->pwr_info.gear_tx = UFS_PWM_G1;
2806 hba->pwr_info.lane_rx = 1;
2807 hba->pwr_info.lane_tx = 1;
2808 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2809 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2810 hba->pwr_info.hs_rate = 0;
2811}
2812
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302813/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002814 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2815 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302816 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002817static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302818{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002819 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2820
2821 if (hba->max_pwr_info.is_valid)
2822 return 0;
2823
2824 pwr_info->pwr_tx = FASTAUTO_MODE;
2825 pwr_info->pwr_rx = FASTAUTO_MODE;
2826 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302827
2828 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002829 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2830 &pwr_info->lane_rx);
2831 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2832 &pwr_info->lane_tx);
2833
2834 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2835 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2836 __func__,
2837 pwr_info->lane_rx,
2838 pwr_info->lane_tx);
2839 return -EINVAL;
2840 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302841
2842 /*
2843 * First, get the maximum gears of HS speed.
2844 * If a zero value, it means there is no HSGEAR capability.
2845 * Then, get the maximum gears of PWM speed.
2846 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002847 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2848 if (!pwr_info->gear_rx) {
2849 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2850 &pwr_info->gear_rx);
2851 if (!pwr_info->gear_rx) {
2852 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2853 __func__, pwr_info->gear_rx);
2854 return -EINVAL;
2855 }
2856 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302857 }
2858
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002859 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2860 &pwr_info->gear_tx);
2861 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302862 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002863 &pwr_info->gear_tx);
2864 if (!pwr_info->gear_tx) {
2865 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2866 __func__, pwr_info->gear_tx);
2867 return -EINVAL;
2868 }
2869 pwr_info->pwr_tx = SLOWAUTO_MODE;
2870 }
2871
2872 hba->max_pwr_info.is_valid = true;
2873 return 0;
2874}
2875
2876static int ufshcd_change_power_mode(struct ufs_hba *hba,
2877 struct ufs_pa_layer_attr *pwr_mode)
2878{
2879 int ret;
2880
2881 /* if already configured to the requested pwr_mode */
2882 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2883 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2884 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2885 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2886 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2887 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2888 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2889 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2890 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302891 }
2892
2893 /*
2894 * Configure attributes for power mode change with below.
2895 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2896 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2897 * - PA_HSSERIES
2898 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002899 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2900 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2901 pwr_mode->lane_rx);
2902 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2903 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302904 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002905 else
2906 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302907
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002908 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2909 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2910 pwr_mode->lane_tx);
2911 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2912 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302913 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002914 else
2915 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302916
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002917 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2918 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2919 pwr_mode->pwr_rx == FAST_MODE ||
2920 pwr_mode->pwr_tx == FAST_MODE)
2921 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2922 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302923
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002924 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2925 | pwr_mode->pwr_tx);
2926
2927 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302928 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002929 "%s: power mode change failed %d\n", __func__, ret);
2930 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002931 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2932 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002933
2934 memcpy(&hba->pwr_info, pwr_mode,
2935 sizeof(struct ufs_pa_layer_attr));
2936 }
2937
2938 return ret;
2939}
2940
2941/**
2942 * ufshcd_config_pwr_mode - configure a new power mode
2943 * @hba: per-adapter instance
2944 * @desired_pwr_mode: desired power configuration
2945 */
2946static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2947 struct ufs_pa_layer_attr *desired_pwr_mode)
2948{
2949 struct ufs_pa_layer_attr final_params = { 0 };
2950 int ret;
2951
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002952 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2953 desired_pwr_mode, &final_params);
2954
2955 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002956 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2957
2958 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302959
2960 return ret;
2961}
2962
2963/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302964 * ufshcd_complete_dev_init() - checks device readiness
2965 * hba: per-adapter instance
2966 *
2967 * Set fDeviceInit flag and poll until device toggles it.
2968 */
2969static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2970{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002971 int i;
2972 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302973 bool flag_res = 1;
2974
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002975 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2976 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302977 if (err) {
2978 dev_err(hba->dev,
2979 "%s setting fDeviceInit flag failed with error %d\n",
2980 __func__, err);
2981 goto out;
2982 }
2983
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002984 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2985 for (i = 0; i < 1000 && !err && flag_res; i++)
2986 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2987 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2988
Dolev Raviv68078d52013-07-30 00:35:58 +05302989 if (err)
2990 dev_err(hba->dev,
2991 "%s reading fDeviceInit flag failed with error %d\n",
2992 __func__, err);
2993 else if (flag_res)
2994 dev_err(hba->dev,
2995 "%s fDeviceInit was not cleared by the device\n",
2996 __func__);
2997
2998out:
2999 return err;
3000}
3001
3002/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303003 * ufshcd_make_hba_operational - Make UFS controller operational
3004 * @hba: per adapter instance
3005 *
3006 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003007 * 1. Enable required interrupts
3008 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003009 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003010 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303011 *
3012 * Returns 0 on success, non-zero value on failure
3013 */
3014static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3015{
3016 int err = 0;
3017 u32 reg;
3018
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303019 /* Enable required interrupts */
3020 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3021
3022 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003023 if (ufshcd_is_intr_aggr_allowed(hba))
3024 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3025 else
3026 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303027
3028 /* Configure UTRL and UTMRL base address registers */
3029 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3030 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3031 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3032 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3033 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3034 REG_UTP_TASK_REQ_LIST_BASE_L);
3035 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3036 REG_UTP_TASK_REQ_LIST_BASE_H);
3037
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303038 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003039 * Make sure base address and interrupt setup are updated before
3040 * enabling the run/stop registers below.
3041 */
3042 wmb();
3043
3044 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303045 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303046 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003047 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303048 if (!(ufshcd_get_lists_status(reg))) {
3049 ufshcd_enable_run_stop_reg(hba);
3050 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303051 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303052 "Host controller not ready to process requests");
3053 err = -EIO;
3054 goto out;
3055 }
3056
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303057out:
3058 return err;
3059}
3060
3061/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003062 * ufshcd_hba_stop - Send controller to reset state
3063 * @hba: per adapter instance
3064 * @can_sleep: perform sleep or just spin
3065 */
3066static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3067{
3068 int err;
3069
3070 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3071 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3072 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3073 10, 1, can_sleep);
3074 if (err)
3075 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3076}
3077
3078/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303079 * ufshcd_hba_enable - initialize the controller
3080 * @hba: per adapter instance
3081 *
3082 * The controller resets itself and controller firmware initialization
3083 * sequence kicks off. When controller is ready it will set
3084 * the Host Controller Enable bit to 1.
3085 *
3086 * Returns 0 on success, non-zero value on failure
3087 */
3088static int ufshcd_hba_enable(struct ufs_hba *hba)
3089{
3090 int retry;
3091
3092 /*
3093 * msleep of 1 and 5 used in this function might result in msleep(20),
3094 * but it was necessary to send the UFS FPGA to reset mode during
3095 * development and testing of this driver. msleep can be changed to
3096 * mdelay and retry count can be reduced based on the controller.
3097 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003098 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303099 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003100 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303101
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003102 /* UniPro link is disabled at this point */
3103 ufshcd_set_link_off(hba);
3104
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003105 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003106
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303107 /* start controller initialization sequence */
3108 ufshcd_hba_start(hba);
3109
3110 /*
3111 * To initialize a UFS host controller HCE bit must be set to 1.
3112 * During initialization the HCE bit value changes from 1->0->1.
3113 * When the host controller completes initialization sequence
3114 * it sets the value of HCE bit to 1. The same HCE bit is read back
3115 * to check if the controller has completed initialization sequence.
3116 * So without this delay the value HCE = 1, set in the previous
3117 * instruction might be read back.
3118 * This delay can be changed based on the controller.
3119 */
3120 msleep(1);
3121
3122 /* wait for the host controller to complete initialization */
3123 retry = 10;
3124 while (ufshcd_is_hba_active(hba)) {
3125 if (retry) {
3126 retry--;
3127 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303128 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303129 "Controller enable failed\n");
3130 return -EIO;
3131 }
3132 msleep(5);
3133 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003134
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003135 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003136 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003137
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003138 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003139
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303140 return 0;
3141}
3142
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003143static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3144{
3145 int tx_lanes, i, err = 0;
3146
3147 if (!peer)
3148 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3149 &tx_lanes);
3150 else
3151 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3152 &tx_lanes);
3153 for (i = 0; i < tx_lanes; i++) {
3154 if (!peer)
3155 err = ufshcd_dme_set(hba,
3156 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3157 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3158 0);
3159 else
3160 err = ufshcd_dme_peer_set(hba,
3161 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3162 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3163 0);
3164 if (err) {
3165 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3166 __func__, peer, i, err);
3167 break;
3168 }
3169 }
3170
3171 return err;
3172}
3173
3174static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3175{
3176 return ufshcd_disable_tx_lcc(hba, true);
3177}
3178
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303179/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303180 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303181 * @hba: per adapter instance
3182 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303183 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303184 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303185static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303186{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303187 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003188 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003189 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303190
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003191 /*
3192 * If UFS device isn't active then we will have to issue link startup
3193 * 2 times to make sure the device state move to active.
3194 */
3195 if (!ufshcd_is_ufs_dev_active(hba))
3196 link_startup_again = true;
3197
3198link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003199 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003200 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303201
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003202 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003203
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003204 /* check if device is detected by inter-connect layer */
3205 if (!ret && !ufshcd_is_device_present(hba)) {
3206 dev_err(hba->dev, "%s: Device not present\n", __func__);
3207 ret = -ENXIO;
3208 goto out;
3209 }
3210
3211 /*
3212 * DME link lost indication is only received when link is up,
3213 * but we can't be sure if the link is up until link startup
3214 * succeeds. So reset the local Uni-Pro and try again.
3215 */
3216 if (ret && ufshcd_hba_enable(hba))
3217 goto out;
3218 } while (ret && retries--);
3219
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303220 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003221 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303222 goto out;
3223
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003224 if (link_startup_again) {
3225 link_startup_again = false;
3226 retries = DME_LINKSTARTUP_RETRIES;
3227 goto link_startup;
3228 }
3229
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003230 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3231 ret = ufshcd_disable_device_tx_lcc(hba);
3232 if (ret)
3233 goto out;
3234 }
3235
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003236 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003237 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3238 if (ret)
3239 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003240
3241 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303242out:
3243 if (ret)
3244 dev_err(hba->dev, "link startup failed %d\n", ret);
3245 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303246}
3247
3248/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303249 * ufshcd_verify_dev_init() - Verify device initialization
3250 * @hba: per-adapter instance
3251 *
3252 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3253 * device Transport Protocol (UTP) layer is ready after a reset.
3254 * If the UTP layer at the device side is not initialized, it may
3255 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3256 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3257 */
3258static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3259{
3260 int err = 0;
3261 int retries;
3262
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003263 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303264 mutex_lock(&hba->dev_cmd.lock);
3265 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3266 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3267 NOP_OUT_TIMEOUT);
3268
3269 if (!err || err == -ETIMEDOUT)
3270 break;
3271
3272 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3273 }
3274 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003275 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303276
3277 if (err)
3278 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3279 return err;
3280}
3281
3282/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003283 * ufshcd_set_queue_depth - set lun queue depth
3284 * @sdev: pointer to SCSI device
3285 *
3286 * Read bLUQueueDepth value and activate scsi tagged command
3287 * queueing. For WLUN, queue depth is set to 1. For best-effort
3288 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3289 * value that host can queue.
3290 */
3291static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3292{
3293 int ret = 0;
3294 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08003295 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003296 struct ufs_hba *hba;
3297
3298 hba = shost_priv(sdev->host);
3299
3300 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08003301 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3302 /* Read descriptor*/
3303 ret = ufshcd_read_unit_desc_param(hba,
3304 ufshcd_scsi_to_upiu_lun(sdev->lun),
3305 UNIT_DESC_PARAM_LU_Q_DEPTH,
3306 &lun_qdepth,
3307 sizeof(lun_qdepth));
3308 if (!ret || ret == -ENOTSUPP)
3309 break;
3310
3311 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
3312 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003313
3314 /* Some WLUN doesn't support unit descriptor */
3315 if (ret == -EOPNOTSUPP)
3316 lun_qdepth = 1;
3317 else if (!lun_qdepth)
3318 /* eventually, we can figure out the real queue depth */
3319 lun_qdepth = hba->nutrs;
3320 else
3321 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3322
3323 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3324 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003325 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003326}
3327
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003328/*
3329 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3330 * @hba: per-adapter instance
3331 * @lun: UFS device lun id
3332 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3333 *
3334 * Returns 0 in case of success and b_lu_write_protect status would be returned
3335 * @b_lu_write_protect parameter.
3336 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3337 * Returns -EINVAL in case of invalid parameters passed to this function.
3338 */
3339static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3340 u8 lun,
3341 u8 *b_lu_write_protect)
3342{
3343 int ret;
3344
3345 if (!b_lu_write_protect)
3346 ret = -EINVAL;
3347 /*
3348 * According to UFS device spec, RPMB LU can't be write
3349 * protected so skip reading bLUWriteProtect parameter for
3350 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3351 */
3352 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3353 ret = -ENOTSUPP;
3354 else
3355 ret = ufshcd_read_unit_desc_param(hba,
3356 lun,
3357 UNIT_DESC_PARAM_LU_WR_PROTECT,
3358 b_lu_write_protect,
3359 sizeof(*b_lu_write_protect));
3360 return ret;
3361}
3362
3363/**
3364 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3365 * status
3366 * @hba: per-adapter instance
3367 * @sdev: pointer to SCSI device
3368 *
3369 */
3370static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3371 struct scsi_device *sdev)
3372{
3373 if (hba->dev_info.f_power_on_wp_en &&
3374 !hba->dev_info.is_lu_power_on_wp) {
3375 u8 b_lu_write_protect;
3376
3377 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3378 &b_lu_write_protect) &&
3379 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3380 hba->dev_info.is_lu_power_on_wp = true;
3381 }
3382}
3383
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003384/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303385 * ufshcd_slave_alloc - handle initial SCSI device configurations
3386 * @sdev: pointer to SCSI device
3387 *
3388 * Returns success
3389 */
3390static int ufshcd_slave_alloc(struct scsi_device *sdev)
3391{
3392 struct ufs_hba *hba;
3393
3394 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303395
3396 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3397 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303398
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303399 /* allow SCSI layer to restart the device in case of errors */
3400 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003401
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003402 /* REPORT SUPPORTED OPERATION CODES is not supported */
3403 sdev->no_report_opcodes = 1;
3404
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003405
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003406 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003407
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003408 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3409
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003410 return 0;
3411}
3412
3413/**
3414 * ufshcd_change_queue_depth - change queue depth
3415 * @sdev: pointer to SCSI device
3416 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003417 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003418 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003419 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003420static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003421{
3422 struct ufs_hba *hba = shost_priv(sdev->host);
3423
3424 if (depth > hba->nutrs)
3425 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003426 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303427}
3428
3429/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003430 * ufshcd_slave_configure - adjust SCSI device configurations
3431 * @sdev: pointer to SCSI device
3432 */
3433static int ufshcd_slave_configure(struct scsi_device *sdev)
3434{
3435 struct request_queue *q = sdev->request_queue;
3436
3437 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3438 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3439
3440 return 0;
3441}
3442
3443/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303444 * ufshcd_slave_destroy - remove SCSI device configurations
3445 * @sdev: pointer to SCSI device
3446 */
3447static void ufshcd_slave_destroy(struct scsi_device *sdev)
3448{
3449 struct ufs_hba *hba;
3450
3451 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003452 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003453 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3454 unsigned long flags;
3455
3456 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003457 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003458 spin_unlock_irqrestore(hba->host->host_lock, flags);
3459 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303460}
3461
3462/**
3463 * ufshcd_task_req_compl - handle task management request completion
3464 * @hba: per adapter instance
3465 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303466 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303467 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303468 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303469 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303470static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303471{
3472 struct utp_task_req_desc *task_req_descp;
3473 struct utp_upiu_task_rsp *task_rsp_upiup;
3474 unsigned long flags;
3475 int ocs_value;
3476 int task_result;
3477
3478 spin_lock_irqsave(hba->host->host_lock, flags);
3479
3480 /* Clear completed tasks from outstanding_tasks */
3481 __clear_bit(index, &hba->outstanding_tasks);
3482
3483 task_req_descp = hba->utmrdl_base_addr;
3484 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3485
3486 if (ocs_value == OCS_SUCCESS) {
3487 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3488 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003489 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3490 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303491 if (resp)
3492 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303493 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303494 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3495 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303496 }
3497 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303498
3499 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303500}
3501
3502/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303503 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3504 * @lrb: pointer to local reference block of completed command
3505 * @scsi_status: SCSI command status
3506 *
3507 * Returns value base on SCSI command status
3508 */
3509static inline int
3510ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3511{
3512 int result = 0;
3513
3514 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303515 case SAM_STAT_CHECK_CONDITION:
3516 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303517 case SAM_STAT_GOOD:
3518 result |= DID_OK << 16 |
3519 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303520 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303521 break;
3522 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303523 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303524 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303525 ufshcd_copy_sense_data(lrbp);
3526 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303527 break;
3528 default:
3529 result |= DID_ERROR << 16;
3530 break;
3531 } /* end of switch */
3532
3533 return result;
3534}
3535
3536/**
3537 * ufshcd_transfer_rsp_status - Get overall status of the response
3538 * @hba: per adapter instance
3539 * @lrb: pointer to local reference block of completed command
3540 *
3541 * Returns result of the command to notify SCSI midlayer
3542 */
3543static inline int
3544ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3545{
3546 int result = 0;
3547 int scsi_status;
3548 int ocs;
3549
3550 /* overall command status of utrd */
3551 ocs = ufshcd_get_tr_ocs(lrbp);
3552
3553 switch (ocs) {
3554 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303555 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303556
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303557 switch (result) {
3558 case UPIU_TRANSACTION_RESPONSE:
3559 /*
3560 * get the response UPIU result to extract
3561 * the SCSI command status
3562 */
3563 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3564
3565 /*
3566 * get the result based on SCSI status response
3567 * to notify the SCSI midlayer of the command status
3568 */
3569 scsi_status = result & MASK_SCSI_STATUS;
3570 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303571
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003572 /*
3573 * Currently we are only supporting BKOPs exception
3574 * events hence we can ignore BKOPs exception event
3575 * during power management callbacks. BKOPs exception
3576 * event is not expected to be raised in runtime suspend
3577 * callback as it allows the urgent bkops.
3578 * During system suspend, we are anyway forcefully
3579 * disabling the bkops and if urgent bkops is needed
3580 * it will be enabled on system resume. Long term
3581 * solution could be to abort the system suspend if
3582 * UFS device needs urgent BKOPs.
3583 */
3584 if (!hba->pm_op_in_progress &&
3585 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303586 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303587 break;
3588 case UPIU_TRANSACTION_REJECT_UPIU:
3589 /* TODO: handle Reject UPIU Response */
3590 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303591 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303592 "Reject UPIU not fully implemented\n");
3593 break;
3594 default:
3595 result = DID_ERROR << 16;
3596 dev_err(hba->dev,
3597 "Unexpected request response code = %x\n",
3598 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303599 break;
3600 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303601 break;
3602 case OCS_ABORTED:
3603 result |= DID_ABORT << 16;
3604 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303605 case OCS_INVALID_COMMAND_STATUS:
3606 result |= DID_REQUEUE << 16;
3607 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303608 case OCS_INVALID_CMD_TABLE_ATTR:
3609 case OCS_INVALID_PRDT_ATTR:
3610 case OCS_MISMATCH_DATA_BUF_SIZE:
3611 case OCS_MISMATCH_RESP_UPIU_SIZE:
3612 case OCS_PEER_COMM_FAILURE:
3613 case OCS_FATAL_ERROR:
3614 default:
3615 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303616 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303617 "OCS error from controller = %x\n", ocs);
3618 break;
3619 } /* end of switch */
3620
3621 return result;
3622}
3623
3624/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303625 * ufshcd_uic_cmd_compl - handle completion of uic command
3626 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303627 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303628 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303629static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303630{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303631 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303632 hba->active_uic_cmd->argument2 |=
3633 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303634 hba->active_uic_cmd->argument3 =
3635 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303636 complete(&hba->active_uic_cmd->done);
3637 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303638
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003639 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3640 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303641}
3642
3643/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003644 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303645 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003646 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303647 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003648static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3649 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303650{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303651 struct ufshcd_lrb *lrbp;
3652 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303653 int result;
3654 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003655
Dolev Ravive9d501b2014-07-01 12:22:37 +03003656 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3657 lrbp = &hba->lrb[index];
3658 cmd = lrbp->cmd;
3659 if (cmd) {
3660 result = ufshcd_transfer_rsp_status(hba, lrbp);
3661 scsi_dma_unmap(cmd);
3662 cmd->result = result;
3663 /* Mark completed command as NULL in LRB */
3664 lrbp->cmd = NULL;
3665 clear_bit_unlock(index, &hba->lrb_in_use);
3666 /* Do not touch lrbp after scsi done */
3667 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003668 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003669 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3670 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003671 if (hba->dev_cmd.complete)
3672 complete(hba->dev_cmd.complete);
3673 }
3674 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303675
3676 /* clear corresponding bits of completed commands */
3677 hba->outstanding_reqs ^= completed_reqs;
3678
Sahitya Tummala856b3482014-09-25 15:32:34 +03003679 ufshcd_clk_scaling_update_busy(hba);
3680
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303681 /* we might have free'd some tags above */
3682 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303683}
3684
3685/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003686 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3687 * @hba: per adapter instance
3688 */
3689static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3690{
3691 unsigned long completed_reqs;
3692 u32 tr_doorbell;
3693
3694 /* Resetting interrupt aggregation counters first and reading the
3695 * DOOR_BELL afterward allows us to handle all the completed requests.
3696 * In order to prevent other interrupts starvation the DB is read once
3697 * after reset. The down side of this solution is the possibility of
3698 * false interrupt if device completes another request after resetting
3699 * aggregation and before reading the DB.
3700 */
3701 if (ufshcd_is_intr_aggr_allowed(hba))
3702 ufshcd_reset_intr_aggr(hba);
3703
3704 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3705 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3706
3707 __ufshcd_transfer_req_compl(hba, completed_reqs);
3708}
3709
3710/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303711 * ufshcd_disable_ee - disable exception event
3712 * @hba: per-adapter instance
3713 * @mask: exception event to disable
3714 *
3715 * Disables exception event in the device so that the EVENT_ALERT
3716 * bit is not set.
3717 *
3718 * Returns zero on success, non-zero error value on failure.
3719 */
3720static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3721{
3722 int err = 0;
3723 u32 val;
3724
3725 if (!(hba->ee_ctrl_mask & mask))
3726 goto out;
3727
3728 val = hba->ee_ctrl_mask & ~mask;
3729 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003730 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303731 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3732 if (!err)
3733 hba->ee_ctrl_mask &= ~mask;
3734out:
3735 return err;
3736}
3737
3738/**
3739 * ufshcd_enable_ee - enable exception event
3740 * @hba: per-adapter instance
3741 * @mask: exception event to enable
3742 *
3743 * Enable corresponding exception event in the device to allow
3744 * device to alert host in critical scenarios.
3745 *
3746 * Returns zero on success, non-zero error value on failure.
3747 */
3748static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3749{
3750 int err = 0;
3751 u32 val;
3752
3753 if (hba->ee_ctrl_mask & mask)
3754 goto out;
3755
3756 val = hba->ee_ctrl_mask | mask;
3757 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003758 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303759 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3760 if (!err)
3761 hba->ee_ctrl_mask |= mask;
3762out:
3763 return err;
3764}
3765
3766/**
3767 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3768 * @hba: per-adapter instance
3769 *
3770 * Allow device to manage background operations on its own. Enabling
3771 * this might lead to inconsistent latencies during normal data transfers
3772 * as the device is allowed to manage its own way of handling background
3773 * operations.
3774 *
3775 * Returns zero on success, non-zero on failure.
3776 */
3777static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3778{
3779 int err = 0;
3780
3781 if (hba->auto_bkops_enabled)
3782 goto out;
3783
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003784 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303785 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3786 if (err) {
3787 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3788 __func__, err);
3789 goto out;
3790 }
3791
3792 hba->auto_bkops_enabled = true;
3793
3794 /* No need of URGENT_BKOPS exception from the device */
3795 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3796 if (err)
3797 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3798 __func__, err);
3799out:
3800 return err;
3801}
3802
3803/**
3804 * ufshcd_disable_auto_bkops - block device in doing background operations
3805 * @hba: per-adapter instance
3806 *
3807 * Disabling background operations improves command response latency but
3808 * has drawback of device moving into critical state where the device is
3809 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3810 * host is idle so that BKOPS are managed effectively without any negative
3811 * impacts.
3812 *
3813 * Returns zero on success, non-zero on failure.
3814 */
3815static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3816{
3817 int err = 0;
3818
3819 if (!hba->auto_bkops_enabled)
3820 goto out;
3821
3822 /*
3823 * If host assisted BKOPs is to be enabled, make sure
3824 * urgent bkops exception is allowed.
3825 */
3826 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3827 if (err) {
3828 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3829 __func__, err);
3830 goto out;
3831 }
3832
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003833 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303834 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3835 if (err) {
3836 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3837 __func__, err);
3838 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3839 goto out;
3840 }
3841
3842 hba->auto_bkops_enabled = false;
3843out:
3844 return err;
3845}
3846
3847/**
3848 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3849 * @hba: per adapter instance
3850 *
3851 * After a device reset the device may toggle the BKOPS_EN flag
3852 * to default value. The s/w tracking variables should be updated
3853 * as well. Do this by forcing enable of auto bkops.
3854 */
3855static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3856{
3857 hba->auto_bkops_enabled = false;
3858 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3859 ufshcd_enable_auto_bkops(hba);
3860}
3861
3862static inline int ufshcd_get_bkops_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_BKOPS_STATUS, 0, 0, status);
3866}
3867
3868/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003869 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3870 * @hba: per-adapter instance
3871 * @status: bkops_status value
3872 *
3873 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3874 * flag in the device to permit background operations if the device
3875 * bkops_status is greater than or equal to "status" argument passed to
3876 * this function, disable otherwise.
3877 *
3878 * Returns 0 for success, non-zero in case of failure.
3879 *
3880 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3881 * to know whether auto bkops is enabled or disabled after this function
3882 * returns control to it.
3883 */
3884static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3885 enum bkops_status status)
3886{
3887 int err;
3888 u32 curr_status = 0;
3889
3890 err = ufshcd_get_bkops_status(hba, &curr_status);
3891 if (err) {
3892 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3893 __func__, err);
3894 goto out;
3895 } else if (curr_status > BKOPS_STATUS_MAX) {
3896 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3897 __func__, curr_status);
3898 err = -EINVAL;
3899 goto out;
3900 }
3901
3902 if (curr_status >= status)
3903 err = ufshcd_enable_auto_bkops(hba);
3904 else
3905 err = ufshcd_disable_auto_bkops(hba);
3906out:
3907 return err;
3908}
3909
3910/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303911 * ufshcd_urgent_bkops - handle urgent bkops exception event
3912 * @hba: per-adapter instance
3913 *
3914 * Enable fBackgroundOpsEn flag in the device to permit background
3915 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003916 *
3917 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3918 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303919 */
3920static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3921{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003922 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303923}
3924
3925static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3926{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003927 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303928 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3929}
3930
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003931static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3932{
3933 int err;
3934 u32 curr_status = 0;
3935
3936 if (hba->is_urgent_bkops_lvl_checked)
3937 goto enable_auto_bkops;
3938
3939 err = ufshcd_get_bkops_status(hba, &curr_status);
3940 if (err) {
3941 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3942 __func__, err);
3943 goto out;
3944 }
3945
3946 /*
3947 * We are seeing that some devices are raising the urgent bkops
3948 * exception events even when BKOPS status doesn't indicate performace
3949 * impacted or critical. Handle these device by determining their urgent
3950 * bkops status at runtime.
3951 */
3952 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
3953 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
3954 __func__, curr_status);
3955 /* update the current status as the urgent bkops level */
3956 hba->urgent_bkops_lvl = curr_status;
3957 hba->is_urgent_bkops_lvl_checked = true;
3958 }
3959
3960enable_auto_bkops:
3961 err = ufshcd_enable_auto_bkops(hba);
3962out:
3963 if (err < 0)
3964 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3965 __func__, err);
3966}
3967
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303968/**
3969 * ufshcd_exception_event_handler - handle exceptions raised by device
3970 * @work: pointer to work data
3971 *
3972 * Read bExceptionEventStatus attribute from the device and handle the
3973 * exception event accordingly.
3974 */
3975static void ufshcd_exception_event_handler(struct work_struct *work)
3976{
3977 struct ufs_hba *hba;
3978 int err;
3979 u32 status = 0;
3980 hba = container_of(work, struct ufs_hba, eeh_work);
3981
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303982 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303983 err = ufshcd_get_ee_status(hba, &status);
3984 if (err) {
3985 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3986 __func__, err);
3987 goto out;
3988 }
3989
3990 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003991
3992 if (status & MASK_EE_URGENT_BKOPS)
3993 ufshcd_bkops_exception_event_handler(hba);
3994
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303995out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303996 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303997 return;
3998}
3999
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004000/* Complete requests that have door-bell cleared */
4001static void ufshcd_complete_requests(struct ufs_hba *hba)
4002{
4003 ufshcd_transfer_req_compl(hba);
4004 ufshcd_tmc_handler(hba);
4005}
4006
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304007/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004008 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4009 * to recover from the DL NAC errors or not.
4010 * @hba: per-adapter instance
4011 *
4012 * Returns true if error handling is required, false otherwise
4013 */
4014static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4015{
4016 unsigned long flags;
4017 bool err_handling = true;
4018
4019 spin_lock_irqsave(hba->host->host_lock, flags);
4020 /*
4021 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4022 * device fatal error and/or DL NAC & REPLAY timeout errors.
4023 */
4024 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4025 goto out;
4026
4027 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4028 ((hba->saved_err & UIC_ERROR) &&
4029 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4030 goto out;
4031
4032 if ((hba->saved_err & UIC_ERROR) &&
4033 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4034 int err;
4035 /*
4036 * wait for 50ms to see if we can get any other errors or not.
4037 */
4038 spin_unlock_irqrestore(hba->host->host_lock, flags);
4039 msleep(50);
4040 spin_lock_irqsave(hba->host->host_lock, flags);
4041
4042 /*
4043 * now check if we have got any other severe errors other than
4044 * DL NAC error?
4045 */
4046 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4047 ((hba->saved_err & UIC_ERROR) &&
4048 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4049 goto out;
4050
4051 /*
4052 * As DL NAC is the only error received so far, send out NOP
4053 * command to confirm if link is still active or not.
4054 * - If we don't get any response then do error recovery.
4055 * - If we get response then clear the DL NAC error bit.
4056 */
4057
4058 spin_unlock_irqrestore(hba->host->host_lock, flags);
4059 err = ufshcd_verify_dev_init(hba);
4060 spin_lock_irqsave(hba->host->host_lock, flags);
4061
4062 if (err)
4063 goto out;
4064
4065 /* Link seems to be alive hence ignore the DL NAC errors */
4066 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4067 hba->saved_err &= ~UIC_ERROR;
4068 /* clear NAC error */
4069 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4070 if (!hba->saved_uic_err) {
4071 err_handling = false;
4072 goto out;
4073 }
4074 }
4075out:
4076 spin_unlock_irqrestore(hba->host->host_lock, flags);
4077 return err_handling;
4078}
4079
4080/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304081 * ufshcd_err_handler - handle UFS errors that require s/w attention
4082 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304083 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304084static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304085{
4086 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304087 unsigned long flags;
4088 u32 err_xfer = 0;
4089 u32 err_tm = 0;
4090 int err = 0;
4091 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004092 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304093
4094 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304095
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304096 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004097 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304098
4099 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004100 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304101 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304102
4103 hba->ufshcd_state = UFSHCD_STATE_RESET;
4104 ufshcd_set_eh_in_progress(hba);
4105
4106 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004107 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004108
4109 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4110 bool ret;
4111
4112 spin_unlock_irqrestore(hba->host->host_lock, flags);
4113 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4114 ret = ufshcd_quirk_dl_nac_errors(hba);
4115 spin_lock_irqsave(hba->host->host_lock, flags);
4116 if (!ret)
4117 goto skip_err_handling;
4118 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004119 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4120 ((hba->saved_err & UIC_ERROR) &&
4121 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4122 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4123 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4124 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304125
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004126 /*
4127 * if host reset is required then skip clearing the pending
4128 * transfers forcefully because they will automatically get
4129 * cleared after link startup.
4130 */
4131 if (needs_reset)
4132 goto skip_pending_xfer_clear;
4133
4134 /* release lock as clear command might sleep */
4135 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304136 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004137 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4138 if (ufshcd_clear_cmd(hba, tag)) {
4139 err_xfer = true;
4140 goto lock_skip_pending_xfer_clear;
4141 }
4142 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304143
4144 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004145 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4146 if (ufshcd_clear_tm_cmd(hba, tag)) {
4147 err_tm = true;
4148 goto lock_skip_pending_xfer_clear;
4149 }
4150 }
4151
4152lock_skip_pending_xfer_clear:
4153 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304154
4155 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004156 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304157
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004158 if (err_xfer || err_tm)
4159 needs_reset = true;
4160
4161skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304162 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004163 if (needs_reset) {
4164 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4165
4166 /*
4167 * ufshcd_reset_and_restore() does the link reinitialization
4168 * which will need atleast one empty doorbell slot to send the
4169 * device management commands (NOP and query commands).
4170 * If there is no slot empty at this moment then free up last
4171 * slot forcefully.
4172 */
4173 if (hba->outstanding_reqs == max_doorbells)
4174 __ufshcd_transfer_req_compl(hba,
4175 (1UL << (hba->nutrs - 1)));
4176
4177 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304178 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004179 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304180 if (err) {
4181 dev_err(hba->dev, "%s: reset and restore failed\n",
4182 __func__);
4183 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4184 }
4185 /*
4186 * Inform scsi mid-layer that we did reset and allow to handle
4187 * Unit Attention properly.
4188 */
4189 scsi_report_bus_reset(hba->host, 0);
4190 hba->saved_err = 0;
4191 hba->saved_uic_err = 0;
4192 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004193
Yaniv Gardi583fa622016-03-10 17:37:13 +02004194skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004195 if (!needs_reset) {
4196 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4197 if (hba->saved_err || hba->saved_uic_err)
4198 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4199 __func__, hba->saved_err, hba->saved_uic_err);
4200 }
4201
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304202 ufshcd_clear_eh_in_progress(hba);
4203
4204out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004205 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304206 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004207 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304208 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304209}
4210
4211/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304212 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4213 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304214 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304215static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304216{
4217 u32 reg;
4218
Dolev Ravivfb7b45f2016-11-23 16:32:32 -08004219 /* PHY layer lane error */
4220 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
4221 /* Ignore LINERESET indication, as this is not an error */
4222 if ((reg & UIC_PHY_ADAPTER_LAYER_ERROR) &&
4223 (reg & UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK))
4224 /*
4225 * To know whether this error is fatal or not, DB timeout
4226 * must be checked but this error is handled separately.
4227 */
4228 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", __func__);
4229
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304230 /* PA_INIT_ERROR is fatal and needs UIC reset */
4231 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4232 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4233 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004234 else if (hba->dev_quirks &
4235 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4236 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4237 hba->uic_error |=
4238 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4239 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4240 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4241 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304242
4243 /* UIC NL/TL/DME errors needs software retry */
4244 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4245 if (reg)
4246 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4247
4248 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4249 if (reg)
4250 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4251
4252 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4253 if (reg)
4254 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4255
4256 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4257 __func__, hba->uic_error);
4258}
4259
4260/**
4261 * ufshcd_check_errors - Check for errors that need s/w attention
4262 * @hba: per-adapter instance
4263 */
4264static void ufshcd_check_errors(struct ufs_hba *hba)
4265{
4266 bool queue_eh_work = false;
4267
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304268 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304269 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304270
4271 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304272 hba->uic_error = 0;
4273 ufshcd_update_uic_error(hba);
4274 if (hba->uic_error)
4275 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304276 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304277
4278 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004279 /*
4280 * update the transfer error masks to sticky bits, let's do this
4281 * irrespective of current ufshcd_state.
4282 */
4283 hba->saved_err |= hba->errors;
4284 hba->saved_uic_err |= hba->uic_error;
4285
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304286 /* handle fatal errors only when link is functional */
4287 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4288 /* block commands from scsi mid-layer */
4289 scsi_block_requests(hba->host);
4290
Zang Leigang141f8162016-11-16 11:29:37 +08004291 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304292 schedule_work(&hba->eh_work);
4293 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304294 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304295 /*
4296 * if (!queue_eh_work) -
4297 * Other errors are either non-fatal where host recovers
4298 * itself without s/w intervention or errors that will be
4299 * handled by the SCSI core layer.
4300 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304301}
4302
4303/**
4304 * ufshcd_tmc_handler - handle task management function completion
4305 * @hba: per adapter instance
4306 */
4307static void ufshcd_tmc_handler(struct ufs_hba *hba)
4308{
4309 u32 tm_doorbell;
4310
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304311 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304312 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304313 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304314}
4315
4316/**
4317 * ufshcd_sl_intr - Interrupt service routine
4318 * @hba: per adapter instance
4319 * @intr_status: contains interrupts generated by the controller
4320 */
4321static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4322{
4323 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4324 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304325 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304326
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304327 if (intr_status & UFSHCD_UIC_MASK)
4328 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304329
4330 if (intr_status & UTP_TASK_REQ_COMPL)
4331 ufshcd_tmc_handler(hba);
4332
4333 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4334 ufshcd_transfer_req_compl(hba);
4335}
4336
4337/**
4338 * ufshcd_intr - Main interrupt service routine
4339 * @irq: irq number
4340 * @__hba: pointer to adapter instance
4341 *
4342 * Returns IRQ_HANDLED - If interrupt is valid
4343 * IRQ_NONE - If invalid interrupt
4344 */
4345static irqreturn_t ufshcd_intr(int irq, void *__hba)
4346{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004347 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304348 irqreturn_t retval = IRQ_NONE;
4349 struct ufs_hba *hba = __hba;
4350
4351 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304352 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004353 enabled_intr_status =
4354 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304355
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004356 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304357 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004358
4359 if (enabled_intr_status) {
4360 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304361 retval = IRQ_HANDLED;
4362 }
4363 spin_unlock(hba->host->host_lock);
4364 return retval;
4365}
4366
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304367static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4368{
4369 int err = 0;
4370 u32 mask = 1 << tag;
4371 unsigned long flags;
4372
4373 if (!test_bit(tag, &hba->outstanding_tasks))
4374 goto out;
4375
4376 spin_lock_irqsave(hba->host->host_lock, flags);
4377 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4378 spin_unlock_irqrestore(hba->host->host_lock, flags);
4379
4380 /* poll for max. 1 sec to clear door bell register by h/w */
4381 err = ufshcd_wait_for_register(hba,
4382 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004383 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304384out:
4385 return err;
4386}
4387
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304388/**
4389 * ufshcd_issue_tm_cmd - issues task management commands to controller
4390 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304391 * @lun_id: LUN ID to which TM command is sent
4392 * @task_id: task ID to which the TM command is applicable
4393 * @tm_function: task management function opcode
4394 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304395 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304396 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304397 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304398static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4399 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304400{
4401 struct utp_task_req_desc *task_req_descp;
4402 struct utp_upiu_task_req *task_req_upiup;
4403 struct Scsi_Host *host;
4404 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304405 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304406 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304407 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304408
4409 host = hba->host;
4410
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304411 /*
4412 * Get free slot, sleep if slots are unavailable.
4413 * Even though we use wait_event() which sleeps indefinitely,
4414 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4415 */
4416 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004417 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304418
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304419 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304420 task_req_descp = hba->utmrdl_base_addr;
4421 task_req_descp += free_slot;
4422
4423 /* Configure task request descriptor */
4424 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4425 task_req_descp->header.dword_2 =
4426 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4427
4428 /* Configure task request UPIU */
4429 task_req_upiup =
4430 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304431 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304433 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304434 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304435 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304436 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004437 /*
4438 * The host shall provide the same value for LUN field in the basic
4439 * header and for Input Parameter.
4440 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304441 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4442 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304443
Kiwoong Kimd2877be2016-11-10 21:16:15 +09004444 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
4445
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304446 /* send command to the controller */
4447 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004448
4449 /* Make sure descriptors are ready before ringing the task doorbell */
4450 wmb();
4451
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304452 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004453 /* Make sure that doorbell is committed immediately */
4454 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304455
4456 spin_unlock_irqrestore(host->host_lock, flags);
4457
4458 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304459 err = wait_event_timeout(hba->tm_wq,
4460 test_bit(free_slot, &hba->tm_condition),
4461 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304462 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304463 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4464 __func__, tm_function);
4465 if (ufshcd_clear_tm_cmd(hba, free_slot))
4466 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4467 __func__, free_slot);
4468 err = -ETIMEDOUT;
4469 } else {
4470 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304471 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304472
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304473 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304474 ufshcd_put_tm_slot(hba, free_slot);
4475 wake_up(&hba->tm_tag_wq);
4476
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004477 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304478 return err;
4479}
4480
4481/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304482 * ufshcd_eh_device_reset_handler - device reset handler registered to
4483 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304484 * @cmd: SCSI command pointer
4485 *
4486 * Returns SUCCESS/FAILED
4487 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304488static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304489{
4490 struct Scsi_Host *host;
4491 struct ufs_hba *hba;
4492 unsigned int tag;
4493 u32 pos;
4494 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304495 u8 resp = 0xF;
4496 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304497 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304498
4499 host = cmd->device->host;
4500 hba = shost_priv(host);
4501 tag = cmd->request->tag;
4502
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304503 lrbp = &hba->lrb[tag];
4504 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4505 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304506 if (!err)
4507 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304508 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304509 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304510
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304511 /* clear the commands that were pending for corresponding LUN */
4512 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4513 if (hba->lrb[pos].lun == lrbp->lun) {
4514 err = ufshcd_clear_cmd(hba, pos);
4515 if (err)
4516 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304517 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304518 }
4519 spin_lock_irqsave(host->host_lock, flags);
4520 ufshcd_transfer_req_compl(hba);
4521 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304522out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304523 if (!err) {
4524 err = SUCCESS;
4525 } else {
4526 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4527 err = FAILED;
4528 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304529 return err;
4530}
4531
4532/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304533 * ufshcd_abort - abort a specific command
4534 * @cmd: SCSI command pointer
4535 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304536 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4537 * command, and in host controller by clearing the door-bell register. There can
4538 * be race between controller sending the command to the device while abort is
4539 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4540 * really issued and then try to abort it.
4541 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304542 * Returns SUCCESS/FAILED
4543 */
4544static int ufshcd_abort(struct scsi_cmnd *cmd)
4545{
4546 struct Scsi_Host *host;
4547 struct ufs_hba *hba;
4548 unsigned long flags;
4549 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304550 int err = 0;
4551 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304552 u8 resp = 0xF;
4553 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004554 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304555
4556 host = cmd->device->host;
4557 hba = shost_priv(host);
4558 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004559 if (!ufshcd_valid_tag(hba, tag)) {
4560 dev_err(hba->dev,
4561 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4562 __func__, tag, cmd, cmd->request);
4563 BUG();
4564 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304565
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004566 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004567 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004568 /* If command is already aborted/completed, return SUCCESS */
4569 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4570 dev_err(hba->dev,
4571 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4572 __func__, tag, hba->outstanding_reqs, reg);
4573 goto out;
4574 }
4575
Dolev Ravive9d501b2014-07-01 12:22:37 +03004576 if (!(reg & (1 << tag))) {
4577 dev_err(hba->dev,
4578 "%s: cmd was completed, but without a notifying intr, tag = %d",
4579 __func__, tag);
4580 }
4581
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304582 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304583 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4584 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4585 UFS_QUERY_TASK, &resp);
4586 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4587 /* cmd pending in the device */
4588 break;
4589 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304590 /*
4591 * cmd not pending in the device, check if it is
4592 * in transition.
4593 */
4594 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4595 if (reg & (1 << tag)) {
4596 /* sleep for max. 200us to stabilize */
4597 usleep_range(100, 200);
4598 continue;
4599 }
4600 /* command completed already */
4601 goto out;
4602 } else {
4603 if (!err)
4604 err = resp; /* service response error */
4605 goto out;
4606 }
4607 }
4608
4609 if (!poll_cnt) {
4610 err = -EBUSY;
4611 goto out;
4612 }
4613
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304614 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4615 UFS_ABORT_TASK, &resp);
4616 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304617 if (!err)
4618 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304619 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304620 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304621
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304622 err = ufshcd_clear_cmd(hba, tag);
4623 if (err)
4624 goto out;
4625
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304626 scsi_dma_unmap(cmd);
4627
4628 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004629 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304630 hba->lrb[tag].cmd = NULL;
4631 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304632
4633 clear_bit_unlock(tag, &hba->lrb_in_use);
4634 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004635
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304636out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304637 if (!err) {
4638 err = SUCCESS;
4639 } else {
4640 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4641 err = FAILED;
4642 }
4643
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004644 /*
4645 * This ufshcd_release() corresponds to the original scsi cmd that got
4646 * aborted here (as we won't get any IRQ for it).
4647 */
4648 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304649 return err;
4650}
4651
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304652/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304653 * ufshcd_host_reset_and_restore - reset and restore host controller
4654 * @hba: per-adapter instance
4655 *
4656 * Note that host controller reset may issue DME_RESET to
4657 * local and remote (device) Uni-Pro stack and the attributes
4658 * are reset to default state.
4659 *
4660 * Returns zero on success, non-zero on failure
4661 */
4662static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4663{
4664 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304665 unsigned long flags;
4666
4667 /* Reset the host controller */
4668 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004669 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304670 spin_unlock_irqrestore(hba->host->host_lock, flags);
4671
4672 err = ufshcd_hba_enable(hba);
4673 if (err)
4674 goto out;
4675
4676 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004677 err = ufshcd_probe_hba(hba);
4678
4679 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304680 err = -EIO;
4681out:
4682 if (err)
4683 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4684
4685 return err;
4686}
4687
4688/**
4689 * ufshcd_reset_and_restore - reset and re-initialize host/device
4690 * @hba: per-adapter instance
4691 *
4692 * Reset and recover device, host and re-establish link. This
4693 * is helpful to recover the communication in fatal error conditions.
4694 *
4695 * Returns zero on success, non-zero on failure
4696 */
4697static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4698{
4699 int err = 0;
4700 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004701 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304702
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004703 do {
4704 err = ufshcd_host_reset_and_restore(hba);
4705 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304706
4707 /*
4708 * After reset the door-bell might be cleared, complete
4709 * outstanding requests in s/w here.
4710 */
4711 spin_lock_irqsave(hba->host->host_lock, flags);
4712 ufshcd_transfer_req_compl(hba);
4713 ufshcd_tmc_handler(hba);
4714 spin_unlock_irqrestore(hba->host->host_lock, flags);
4715
4716 return err;
4717}
4718
4719/**
4720 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4721 * @cmd - SCSI command pointer
4722 *
4723 * Returns SUCCESS/FAILED
4724 */
4725static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4726{
4727 int err;
4728 unsigned long flags;
4729 struct ufs_hba *hba;
4730
4731 hba = shost_priv(cmd->device->host);
4732
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004733 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304734 /*
4735 * Check if there is any race with fatal error handling.
4736 * If so, wait for it to complete. Even though fatal error
4737 * handling does reset and restore in some cases, don't assume
4738 * anything out of it. We are just avoiding race here.
4739 */
4740 do {
4741 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304742 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304743 hba->ufshcd_state == UFSHCD_STATE_RESET))
4744 break;
4745 spin_unlock_irqrestore(hba->host->host_lock, flags);
4746 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304747 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304748 } while (1);
4749
4750 hba->ufshcd_state = UFSHCD_STATE_RESET;
4751 ufshcd_set_eh_in_progress(hba);
4752 spin_unlock_irqrestore(hba->host->host_lock, flags);
4753
4754 err = ufshcd_reset_and_restore(hba);
4755
4756 spin_lock_irqsave(hba->host->host_lock, flags);
4757 if (!err) {
4758 err = SUCCESS;
4759 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4760 } else {
4761 err = FAILED;
4762 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4763 }
4764 ufshcd_clear_eh_in_progress(hba);
4765 spin_unlock_irqrestore(hba->host->host_lock, flags);
4766
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004767 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304768 return err;
4769}
4770
4771/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004772 * ufshcd_get_max_icc_level - calculate the ICC level
4773 * @sup_curr_uA: max. current supported by the regulator
4774 * @start_scan: row at the desc table to start scan from
4775 * @buff: power descriptor buffer
4776 *
4777 * Returns calculated max ICC level for specific regulator
4778 */
4779static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4780{
4781 int i;
4782 int curr_uA;
4783 u16 data;
4784 u16 unit;
4785
4786 for (i = start_scan; i >= 0; i--) {
4787 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4788 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4789 ATTR_ICC_LVL_UNIT_OFFSET;
4790 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4791 switch (unit) {
4792 case UFSHCD_NANO_AMP:
4793 curr_uA = curr_uA / 1000;
4794 break;
4795 case UFSHCD_MILI_AMP:
4796 curr_uA = curr_uA * 1000;
4797 break;
4798 case UFSHCD_AMP:
4799 curr_uA = curr_uA * 1000 * 1000;
4800 break;
4801 case UFSHCD_MICRO_AMP:
4802 default:
4803 break;
4804 }
4805 if (sup_curr_uA >= curr_uA)
4806 break;
4807 }
4808 if (i < 0) {
4809 i = 0;
4810 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4811 }
4812
4813 return (u32)i;
4814}
4815
4816/**
4817 * ufshcd_calc_icc_level - calculate the max ICC level
4818 * In case regulators are not initialized we'll return 0
4819 * @hba: per-adapter instance
4820 * @desc_buf: power descriptor buffer to extract ICC levels from.
4821 * @len: length of desc_buff
4822 *
4823 * Returns calculated ICC level
4824 */
4825static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4826 u8 *desc_buf, int len)
4827{
4828 u32 icc_level = 0;
4829
4830 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4831 !hba->vreg_info.vccq2) {
4832 dev_err(hba->dev,
4833 "%s: Regulator capability was not set, actvIccLevel=%d",
4834 __func__, icc_level);
4835 goto out;
4836 }
4837
4838 if (hba->vreg_info.vcc)
4839 icc_level = ufshcd_get_max_icc_level(
4840 hba->vreg_info.vcc->max_uA,
4841 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4842 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4843
4844 if (hba->vreg_info.vccq)
4845 icc_level = ufshcd_get_max_icc_level(
4846 hba->vreg_info.vccq->max_uA,
4847 icc_level,
4848 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4849
4850 if (hba->vreg_info.vccq2)
4851 icc_level = ufshcd_get_max_icc_level(
4852 hba->vreg_info.vccq2->max_uA,
4853 icc_level,
4854 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4855out:
4856 return icc_level;
4857}
4858
Dolev Raviv61e07352016-11-23 16:30:49 -08004859static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
4860{
4861 int ret = 0;
4862 int retries;
4863
4864 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
4865 /* write attribute */
4866 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4867 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
4868 if (!ret)
4869 break;
4870
4871 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
4872 }
4873
4874 return ret;
4875}
4876
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004877static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4878{
4879 int ret;
4880 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4881 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4882
4883 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4884 if (ret) {
4885 dev_err(hba->dev,
4886 "%s: Failed reading power descriptor.len = %d ret = %d",
4887 __func__, buff_len, ret);
4888 return;
4889 }
4890
4891 hba->init_prefetch_data.icc_level =
4892 ufshcd_find_max_sup_active_icc_level(hba,
4893 desc_buf, buff_len);
4894 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4895 __func__, hba->init_prefetch_data.icc_level);
4896
Dolev Raviv61e07352016-11-23 16:30:49 -08004897 ret = ufshcd_set_icc_levels_attr(hba,
4898 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004899
4900 if (ret)
4901 dev_err(hba->dev,
4902 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4903 __func__, hba->init_prefetch_data.icc_level , ret);
4904
4905}
4906
4907/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004908 * ufshcd_scsi_add_wlus - Adds required W-LUs
4909 * @hba: per-adapter instance
4910 *
4911 * UFS device specification requires the UFS devices to support 4 well known
4912 * logical units:
4913 * "REPORT_LUNS" (address: 01h)
4914 * "UFS Device" (address: 50h)
4915 * "RPMB" (address: 44h)
4916 * "BOOT" (address: 30h)
4917 * UFS device's power management needs to be controlled by "POWER CONDITION"
4918 * field of SSU (START STOP UNIT) command. But this "power condition" field
4919 * will take effect only when its sent to "UFS device" well known logical unit
4920 * hence we require the scsi_device instance to represent this logical unit in
4921 * order for the UFS host driver to send the SSU command for power management.
4922
4923 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4924 * Block) LU so user space process can control this LU. User space may also
4925 * want to have access to BOOT LU.
4926
4927 * This function adds scsi device instances for each of all well known LUs
4928 * (except "REPORT LUNS" LU).
4929 *
4930 * Returns zero on success (all required W-LUs are added successfully),
4931 * non-zero error value on failure (if failed to add any of the required W-LU).
4932 */
4933static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4934{
4935 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004936 struct scsi_device *sdev_rpmb;
4937 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004938
4939 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4940 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4941 if (IS_ERR(hba->sdev_ufs_device)) {
4942 ret = PTR_ERR(hba->sdev_ufs_device);
4943 hba->sdev_ufs_device = NULL;
4944 goto out;
4945 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004946 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004947
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004948 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004949 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004950 if (IS_ERR(sdev_boot)) {
4951 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004952 goto remove_sdev_ufs_device;
4953 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004954 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004955
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004956 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004957 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004958 if (IS_ERR(sdev_rpmb)) {
4959 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004960 goto remove_sdev_boot;
4961 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004962 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004963 goto out;
4964
4965remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004966 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004967remove_sdev_ufs_device:
4968 scsi_remove_device(hba->sdev_ufs_device);
4969out:
4970 return ret;
4971}
4972
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004973static int ufs_get_device_info(struct ufs_hba *hba,
4974 struct ufs_device_info *card_data)
4975{
4976 int err;
4977 u8 model_index;
4978 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
4979 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
4980
4981 err = ufshcd_read_device_desc(hba, desc_buf,
4982 QUERY_DESC_DEVICE_MAX_SIZE);
4983 if (err) {
4984 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4985 __func__, err);
4986 goto out;
4987 }
4988
4989 /*
4990 * getting vendor (manufacturerID) and Bank Index in big endian
4991 * format
4992 */
4993 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
4994 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4995
4996 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4997
4998 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
4999 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
5000 if (err) {
5001 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5002 __func__, err);
5003 goto out;
5004 }
5005
5006 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
5007 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
5008 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5009 MAX_MODEL_LEN));
5010
5011 /* Null terminate the model string */
5012 card_data->model[MAX_MODEL_LEN] = '\0';
5013
5014out:
5015 return err;
5016}
5017
5018void ufs_advertise_fixup_device(struct ufs_hba *hba)
5019{
5020 int err;
5021 struct ufs_dev_fix *f;
5022 struct ufs_device_info card_data;
5023
5024 card_data.wmanufacturerid = 0;
5025
5026 err = ufs_get_device_info(hba, &card_data);
5027 if (err) {
5028 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5029 __func__, err);
5030 return;
5031 }
5032
5033 for (f = ufs_fixups; f->quirk; f++) {
5034 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
5035 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
5036 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
5037 !strcmp(f->card.model, UFS_ANY_MODEL)))
5038 hba->dev_quirks |= f->quirk;
5039 }
5040}
5041
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005042/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005043 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5044 * @hba: per-adapter instance
5045 *
5046 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5047 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5048 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5049 * the hibern8 exit latency.
5050 *
5051 * Returns zero on success, non-zero error value on failure.
5052 */
5053static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5054{
5055 int ret = 0;
5056 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5057
5058 ret = ufshcd_dme_peer_get(hba,
5059 UIC_ARG_MIB_SEL(
5060 RX_MIN_ACTIVATETIME_CAPABILITY,
5061 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5062 &peer_rx_min_activatetime);
5063 if (ret)
5064 goto out;
5065
5066 /* make sure proper unit conversion is applied */
5067 tuned_pa_tactivate =
5068 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5069 / PA_TACTIVATE_TIME_UNIT_US);
5070 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5071 tuned_pa_tactivate);
5072
5073out:
5074 return ret;
5075}
5076
5077/**
5078 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5079 * @hba: per-adapter instance
5080 *
5081 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5082 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5083 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5084 * This optimal value can help reduce the hibern8 exit latency.
5085 *
5086 * Returns zero on success, non-zero error value on failure.
5087 */
5088static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5089{
5090 int ret = 0;
5091 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5092 u32 max_hibern8_time, tuned_pa_hibern8time;
5093
5094 ret = ufshcd_dme_get(hba,
5095 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5096 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5097 &local_tx_hibern8_time_cap);
5098 if (ret)
5099 goto out;
5100
5101 ret = ufshcd_dme_peer_get(hba,
5102 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5103 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5104 &peer_rx_hibern8_time_cap);
5105 if (ret)
5106 goto out;
5107
5108 max_hibern8_time = max(local_tx_hibern8_time_cap,
5109 peer_rx_hibern8_time_cap);
5110 /* make sure proper unit conversion is applied */
5111 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5112 / PA_HIBERN8_TIME_UNIT_US);
5113 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5114 tuned_pa_hibern8time);
5115out:
5116 return ret;
5117}
5118
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005119/**
5120 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5121 * less than device PA_TACTIVATE time.
5122 * @hba: per-adapter instance
5123 *
5124 * Some UFS devices require host PA_TACTIVATE to be lower than device
5125 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5126 * for such devices.
5127 *
5128 * Returns zero on success, non-zero error value on failure.
5129 */
5130static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5131{
5132 int ret = 0;
5133 u32 granularity, peer_granularity;
5134 u32 pa_tactivate, peer_pa_tactivate;
5135 u32 pa_tactivate_us, peer_pa_tactivate_us;
5136 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5137
5138 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5139 &granularity);
5140 if (ret)
5141 goto out;
5142
5143 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5144 &peer_granularity);
5145 if (ret)
5146 goto out;
5147
5148 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5149 (granularity > PA_GRANULARITY_MAX_VAL)) {
5150 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5151 __func__, granularity);
5152 return -EINVAL;
5153 }
5154
5155 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5156 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5157 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5158 __func__, peer_granularity);
5159 return -EINVAL;
5160 }
5161
5162 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5163 if (ret)
5164 goto out;
5165
5166 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5167 &peer_pa_tactivate);
5168 if (ret)
5169 goto out;
5170
5171 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5172 peer_pa_tactivate_us = peer_pa_tactivate *
5173 gran_to_us_table[peer_granularity - 1];
5174
5175 if (pa_tactivate_us > peer_pa_tactivate_us) {
5176 u32 new_peer_pa_tactivate;
5177
5178 new_peer_pa_tactivate = pa_tactivate_us /
5179 gran_to_us_table[peer_granularity - 1];
5180 new_peer_pa_tactivate++;
5181 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5182 new_peer_pa_tactivate);
5183 }
5184
5185out:
5186 return ret;
5187}
5188
Yaniv Gardi37113102016-03-10 17:37:16 +02005189static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5190{
5191 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5192 ufshcd_tune_pa_tactivate(hba);
5193 ufshcd_tune_pa_hibern8time(hba);
5194 }
5195
5196 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5197 /* set 1ms timeout for PA_TACTIVATE */
5198 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005199
5200 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5201 ufshcd_quirk_tune_host_pa_tactivate(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005202}
5203
5204/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005205 * ufshcd_probe_hba - probe hba to detect device and initialize
5206 * @hba: per-adapter instance
5207 *
5208 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305209 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005210static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305211{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305212 int ret;
5213
5214 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305215 if (ret)
5216 goto out;
5217
Yaniv Gardi50646362014-10-23 13:25:13 +03005218 ufshcd_init_pwr_info(hba);
5219
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005220 /* set the default level for urgent bkops */
5221 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5222 hba->is_urgent_bkops_lvl_checked = false;
5223
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005224 /* UniPro link is active now */
5225 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305226
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305227 ret = ufshcd_verify_dev_init(hba);
5228 if (ret)
5229 goto out;
5230
Dolev Raviv68078d52013-07-30 00:35:58 +05305231 ret = ufshcd_complete_dev_init(hba);
5232 if (ret)
5233 goto out;
5234
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005235 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005236 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005237
5238 ret = ufshcd_set_vccq_rail_unused(hba,
5239 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5240 if (ret)
5241 goto out;
5242
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005243 /* UFS device is also active now */
5244 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305245 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005246 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305247
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005248 if (ufshcd_get_max_pwr_mode(hba)) {
5249 dev_err(hba->dev,
5250 "%s: Failed getting max supported power mode\n",
5251 __func__);
5252 } else {
5253 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005254 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005255 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5256 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005257 goto out;
5258 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005259 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005260
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005261 /* set the state as operational after switching to desired gear */
5262 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005263 /*
5264 * If we are in error handling context or in power management callbacks
5265 * context, no need to scan the host
5266 */
5267 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5268 bool flag;
5269
5270 /* clear any previous UFS device information */
5271 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005272 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5273 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005274 hba->dev_info.f_power_on_wp_en = flag;
5275
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005276 if (!hba->is_init_prefetch)
5277 ufshcd_init_icc_levels(hba);
5278
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005279 /* Add required well known logical units to scsi mid layer */
5280 if (ufshcd_scsi_add_wlus(hba))
5281 goto out;
5282
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305283 scsi_scan_host(hba->host);
5284 pm_runtime_put_sync(hba->dev);
5285 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005286
5287 if (!hba->is_init_prefetch)
5288 hba->is_init_prefetch = true;
5289
Sahitya Tummala856b3482014-09-25 15:32:34 +03005290 /* Resume devfreq after UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07005291 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005292
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305293out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005294 /*
5295 * If we failed to initialize the device or the device is not
5296 * present, turn off the power/clocks etc.
5297 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005298 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5299 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005300 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005301 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005302
5303 return ret;
5304}
5305
5306/**
5307 * ufshcd_async_scan - asynchronous execution for probing hba
5308 * @data: data pointer to pass to this function
5309 * @cookie: cookie data
5310 */
5311static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5312{
5313 struct ufs_hba *hba = (struct ufs_hba *)data;
5314
5315 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305316}
5317
Yaniv Gardif550c652016-03-10 17:37:07 +02005318static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5319{
5320 unsigned long flags;
5321 struct Scsi_Host *host;
5322 struct ufs_hba *hba;
5323 int index;
5324 bool found = false;
5325
5326 if (!scmd || !scmd->device || !scmd->device->host)
5327 return BLK_EH_NOT_HANDLED;
5328
5329 host = scmd->device->host;
5330 hba = shost_priv(host);
5331 if (!hba)
5332 return BLK_EH_NOT_HANDLED;
5333
5334 spin_lock_irqsave(host->host_lock, flags);
5335
5336 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5337 if (hba->lrb[index].cmd == scmd) {
5338 found = true;
5339 break;
5340 }
5341 }
5342
5343 spin_unlock_irqrestore(host->host_lock, flags);
5344
5345 /*
5346 * Bypass SCSI error handling and reset the block layer timer if this
5347 * SCSI command was not actually dispatched to UFS driver, otherwise
5348 * let SCSI layer handle the error as usual.
5349 */
5350 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5351}
5352
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305353static struct scsi_host_template ufshcd_driver_template = {
5354 .module = THIS_MODULE,
5355 .name = UFSHCD,
5356 .proc_name = UFSHCD,
5357 .queuecommand = ufshcd_queuecommand,
5358 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005359 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305360 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005361 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305362 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305363 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5364 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005365 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305366 .this_id = -1,
5367 .sg_tablesize = SG_ALL,
5368 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5369 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005370 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005371 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305372};
5373
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005374static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5375 int ua)
5376{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005377 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005378
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005379 if (!vreg)
5380 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005381
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005382 ret = regulator_set_load(vreg->reg, ua);
5383 if (ret < 0) {
5384 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5385 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005386 }
5387
5388 return ret;
5389}
5390
5391static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5392 struct ufs_vreg *vreg)
5393{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005394 if (!vreg)
5395 return 0;
5396 else if (vreg->unused)
5397 return 0;
5398 else
5399 return ufshcd_config_vreg_load(hba->dev, vreg,
5400 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005401}
5402
5403static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5404 struct ufs_vreg *vreg)
5405{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005406 if (!vreg)
5407 return 0;
5408 else if (vreg->unused)
5409 return 0;
5410 else
5411 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005412}
5413
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005414static int ufshcd_config_vreg(struct device *dev,
5415 struct ufs_vreg *vreg, bool on)
5416{
5417 int ret = 0;
5418 struct regulator *reg = vreg->reg;
5419 const char *name = vreg->name;
5420 int min_uV, uA_load;
5421
5422 BUG_ON(!vreg);
5423
5424 if (regulator_count_voltages(reg) > 0) {
5425 min_uV = on ? vreg->min_uV : 0;
5426 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5427 if (ret) {
5428 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5429 __func__, name, ret);
5430 goto out;
5431 }
5432
5433 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005434 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5435 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005436 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005437 }
5438out:
5439 return ret;
5440}
5441
5442static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5443{
5444 int ret = 0;
5445
Yaniv Gardi60f01872016-03-10 17:37:11 +02005446 if (!vreg)
5447 goto out;
5448 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005449 goto out;
5450
5451 ret = ufshcd_config_vreg(dev, vreg, true);
5452 if (!ret)
5453 ret = regulator_enable(vreg->reg);
5454
5455 if (!ret)
5456 vreg->enabled = true;
5457 else
5458 dev_err(dev, "%s: %s enable failed, err=%d\n",
5459 __func__, vreg->name, ret);
5460out:
5461 return ret;
5462}
5463
5464static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5465{
5466 int ret = 0;
5467
Yaniv Gardi60f01872016-03-10 17:37:11 +02005468 if (!vreg)
5469 goto out;
5470 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005471 goto out;
5472
5473 ret = regulator_disable(vreg->reg);
5474
5475 if (!ret) {
5476 /* ignore errors on applying disable config */
5477 ufshcd_config_vreg(dev, vreg, false);
5478 vreg->enabled = false;
5479 } else {
5480 dev_err(dev, "%s: %s disable failed, err=%d\n",
5481 __func__, vreg->name, ret);
5482 }
5483out:
5484 return ret;
5485}
5486
5487static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5488{
5489 int ret = 0;
5490 struct device *dev = hba->dev;
5491 struct ufs_vreg_info *info = &hba->vreg_info;
5492
5493 if (!info)
5494 goto out;
5495
5496 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5497 if (ret)
5498 goto out;
5499
5500 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5501 if (ret)
5502 goto out;
5503
5504 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5505 if (ret)
5506 goto out;
5507
5508out:
5509 if (ret) {
5510 ufshcd_toggle_vreg(dev, info->vccq2, false);
5511 ufshcd_toggle_vreg(dev, info->vccq, false);
5512 ufshcd_toggle_vreg(dev, info->vcc, false);
5513 }
5514 return ret;
5515}
5516
Raviv Shvili6a771a62014-09-25 15:32:24 +03005517static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5518{
5519 struct ufs_vreg_info *info = &hba->vreg_info;
5520
5521 if (info)
5522 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5523
5524 return 0;
5525}
5526
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005527static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5528{
5529 int ret = 0;
5530
5531 if (!vreg)
5532 goto out;
5533
5534 vreg->reg = devm_regulator_get(dev, vreg->name);
5535 if (IS_ERR(vreg->reg)) {
5536 ret = PTR_ERR(vreg->reg);
5537 dev_err(dev, "%s: %s get failed, err=%d\n",
5538 __func__, vreg->name, ret);
5539 }
5540out:
5541 return ret;
5542}
5543
5544static int ufshcd_init_vreg(struct ufs_hba *hba)
5545{
5546 int ret = 0;
5547 struct device *dev = hba->dev;
5548 struct ufs_vreg_info *info = &hba->vreg_info;
5549
5550 if (!info)
5551 goto out;
5552
5553 ret = ufshcd_get_vreg(dev, info->vcc);
5554 if (ret)
5555 goto out;
5556
5557 ret = ufshcd_get_vreg(dev, info->vccq);
5558 if (ret)
5559 goto out;
5560
5561 ret = ufshcd_get_vreg(dev, info->vccq2);
5562out:
5563 return ret;
5564}
5565
Raviv Shvili6a771a62014-09-25 15:32:24 +03005566static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5567{
5568 struct ufs_vreg_info *info = &hba->vreg_info;
5569
5570 if (info)
5571 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5572
5573 return 0;
5574}
5575
Yaniv Gardi60f01872016-03-10 17:37:11 +02005576static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5577{
5578 int ret = 0;
5579 struct ufs_vreg_info *info = &hba->vreg_info;
5580
5581 if (!info)
5582 goto out;
5583 else if (!info->vccq)
5584 goto out;
5585
5586 if (unused) {
5587 /* shut off the rail here */
5588 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5589 /*
5590 * Mark this rail as no longer used, so it doesn't get enabled
5591 * later by mistake
5592 */
5593 if (!ret)
5594 info->vccq->unused = true;
5595 } else {
5596 /*
5597 * rail should have been already enabled hence just make sure
5598 * that unused flag is cleared.
5599 */
5600 info->vccq->unused = false;
5601 }
5602out:
5603 return ret;
5604}
5605
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005606static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5607 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005608{
5609 int ret = 0;
5610 struct ufs_clk_info *clki;
5611 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005612 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005613
5614 if (!head || list_empty(head))
5615 goto out;
5616
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005617 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5618 if (ret)
5619 return ret;
5620
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005621 list_for_each_entry(clki, head, list) {
5622 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005623 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5624 continue;
5625
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005626 if (on && !clki->enabled) {
5627 ret = clk_prepare_enable(clki->clk);
5628 if (ret) {
5629 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5630 __func__, clki->name, ret);
5631 goto out;
5632 }
5633 } else if (!on && clki->enabled) {
5634 clk_disable_unprepare(clki->clk);
5635 }
5636 clki->enabled = on;
5637 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5638 clki->name, on ? "en" : "dis");
5639 }
5640 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005641
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005642 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5643 if (ret)
5644 return ret;
5645
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005646out:
5647 if (ret) {
5648 list_for_each_entry(clki, head, list) {
5649 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5650 clk_disable_unprepare(clki->clk);
5651 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005652 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005653 spin_lock_irqsave(hba->host->host_lock, flags);
5654 hba->clk_gating.state = CLKS_ON;
5655 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005656 }
5657 return ret;
5658}
5659
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005660static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5661{
5662 return __ufshcd_setup_clocks(hba, on, false);
5663}
5664
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005665static int ufshcd_init_clocks(struct ufs_hba *hba)
5666{
5667 int ret = 0;
5668 struct ufs_clk_info *clki;
5669 struct device *dev = hba->dev;
5670 struct list_head *head = &hba->clk_list_head;
5671
5672 if (!head || list_empty(head))
5673 goto out;
5674
5675 list_for_each_entry(clki, head, list) {
5676 if (!clki->name)
5677 continue;
5678
5679 clki->clk = devm_clk_get(dev, clki->name);
5680 if (IS_ERR(clki->clk)) {
5681 ret = PTR_ERR(clki->clk);
5682 dev_err(dev, "%s: %s clk get failed, %d\n",
5683 __func__, clki->name, ret);
5684 goto out;
5685 }
5686
5687 if (clki->max_freq) {
5688 ret = clk_set_rate(clki->clk, clki->max_freq);
5689 if (ret) {
5690 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5691 __func__, clki->name,
5692 clki->max_freq, ret);
5693 goto out;
5694 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005695 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005696 }
5697 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5698 clki->name, clk_get_rate(clki->clk));
5699 }
5700out:
5701 return ret;
5702}
5703
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005704static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5705{
5706 int err = 0;
5707
5708 if (!hba->vops)
5709 goto out;
5710
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005711 err = ufshcd_vops_init(hba);
5712 if (err)
5713 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005714
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005715 err = ufshcd_vops_setup_regulators(hba, true);
5716 if (err)
5717 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005718
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005719 goto out;
5720
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005721out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005722 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005723out:
5724 if (err)
5725 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005726 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005727 return err;
5728}
5729
5730static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5731{
5732 if (!hba->vops)
5733 return;
5734
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005735 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005736
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005737 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005738}
5739
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005740static int ufshcd_hba_init(struct ufs_hba *hba)
5741{
5742 int err;
5743
Raviv Shvili6a771a62014-09-25 15:32:24 +03005744 /*
5745 * Handle host controller power separately from the UFS device power
5746 * rails as it will help controlling the UFS host controller power
5747 * collapse easily which is different than UFS device power collapse.
5748 * Also, enable the host controller power before we go ahead with rest
5749 * of the initialization here.
5750 */
5751 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005752 if (err)
5753 goto out;
5754
Raviv Shvili6a771a62014-09-25 15:32:24 +03005755 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005756 if (err)
5757 goto out;
5758
Raviv Shvili6a771a62014-09-25 15:32:24 +03005759 err = ufshcd_init_clocks(hba);
5760 if (err)
5761 goto out_disable_hba_vreg;
5762
5763 err = ufshcd_setup_clocks(hba, true);
5764 if (err)
5765 goto out_disable_hba_vreg;
5766
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005767 err = ufshcd_init_vreg(hba);
5768 if (err)
5769 goto out_disable_clks;
5770
5771 err = ufshcd_setup_vreg(hba, true);
5772 if (err)
5773 goto out_disable_clks;
5774
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005775 err = ufshcd_variant_hba_init(hba);
5776 if (err)
5777 goto out_disable_vreg;
5778
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005779 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005780 goto out;
5781
5782out_disable_vreg:
5783 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005784out_disable_clks:
5785 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005786out_disable_hba_vreg:
5787 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005788out:
5789 return err;
5790}
5791
5792static void ufshcd_hba_exit(struct ufs_hba *hba)
5793{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005794 if (hba->is_powered) {
5795 ufshcd_variant_hba_exit(hba);
5796 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07005797 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005798 ufshcd_setup_clocks(hba, false);
5799 ufshcd_setup_hba_vreg(hba, false);
5800 hba->is_powered = false;
5801 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005802}
5803
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005804static int
5805ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305806{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005807 unsigned char cmd[6] = {REQUEST_SENSE,
5808 0,
5809 0,
5810 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005811 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005812 0};
5813 char *buffer;
5814 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305815
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005816 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005817 if (!buffer) {
5818 ret = -ENOMEM;
5819 goto out;
5820 }
5821
5822 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005823 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005824 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5825 if (ret)
5826 pr_err("%s: failed with err %d\n", __func__, ret);
5827
5828 kfree(buffer);
5829out:
5830 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305831}
5832
5833/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005834 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5835 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305836 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005837 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305838 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005839 * Returns 0 if requested power mode is set successfully
5840 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305841 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005842static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5843 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305844{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005845 unsigned char cmd[6] = { START_STOP };
5846 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005847 struct scsi_device *sdp;
5848 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005849 int ret;
5850
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005851 spin_lock_irqsave(hba->host->host_lock, flags);
5852 sdp = hba->sdev_ufs_device;
5853 if (sdp) {
5854 ret = scsi_device_get(sdp);
5855 if (!ret && !scsi_device_online(sdp)) {
5856 ret = -ENODEV;
5857 scsi_device_put(sdp);
5858 }
5859 } else {
5860 ret = -ENODEV;
5861 }
5862 spin_unlock_irqrestore(hba->host->host_lock, flags);
5863
5864 if (ret)
5865 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005866
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305867 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005868 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5869 * handling, which would wait for host to be resumed. Since we know
5870 * we are functional while we are here, skip host resume in error
5871 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305872 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005873 hba->host->eh_noresume = 1;
5874 if (hba->wlun_dev_clr_ua) {
5875 ret = ufshcd_send_request_sense(hba, sdp);
5876 if (ret)
5877 goto out;
5878 /* Unit attention condition is cleared now */
5879 hba->wlun_dev_clr_ua = false;
5880 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305881
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005882 cmd[4] = pwr_mode << 4;
5883
5884 /*
5885 * Current function would be generally called from the power management
5886 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5887 * already suspended childs.
5888 */
5889 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5890 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5891 if (ret) {
5892 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005893 "START_STOP failed for power mode: %d, result %x\n",
5894 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005895 if (driver_byte(ret) & DRIVER_SENSE)
5896 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005897 }
5898
5899 if (!ret)
5900 hba->curr_dev_pwr_mode = pwr_mode;
5901out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005902 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005903 hba->host->eh_noresume = 0;
5904 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305905}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305906
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005907static int ufshcd_link_state_transition(struct ufs_hba *hba,
5908 enum uic_link_state req_link_state,
5909 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305910{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005911 int ret = 0;
5912
5913 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305914 return 0;
5915
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005916 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5917 ret = ufshcd_uic_hibern8_enter(hba);
5918 if (!ret)
5919 ufshcd_set_link_hibern8(hba);
5920 else
5921 goto out;
5922 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305923 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005924 * If autobkops is enabled, link can't be turned off because
5925 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305926 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005927 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5928 (!check_for_bkops || (check_for_bkops &&
5929 !hba->auto_bkops_enabled))) {
5930 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005931 * Let's make sure that link is in low power mode, we are doing
5932 * this currently by putting the link in Hibern8. Otherway to
5933 * put the link in low power mode is to send the DME end point
5934 * to device and then send the DME reset command to local
5935 * unipro. But putting the link in hibern8 is much faster.
5936 */
5937 ret = ufshcd_uic_hibern8_enter(hba);
5938 if (ret)
5939 goto out;
5940 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005941 * Change controller state to "reset state" which
5942 * should also put the link in off/reset state
5943 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005944 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005945 /*
5946 * TODO: Check if we need any delay to make sure that
5947 * controller is reset
5948 */
5949 ufshcd_set_link_off(hba);
5950 }
5951
5952out:
5953 return ret;
5954}
5955
5956static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5957{
5958 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02005959 * It seems some UFS devices may keep drawing more than sleep current
5960 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
5961 * To avoid this situation, add 2ms delay before putting these UFS
5962 * rails in LPM mode.
5963 */
5964 if (!ufshcd_is_link_active(hba) &&
5965 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
5966 usleep_range(2000, 2100);
5967
5968 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005969 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5970 * power.
5971 *
5972 * If UFS device and link is in OFF state, all power supplies (VCC,
5973 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5974 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5975 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5976 *
5977 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5978 * in low power state which would save some power.
5979 */
5980 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5981 !hba->dev_info.is_lu_power_on_wp) {
5982 ufshcd_setup_vreg(hba, false);
5983 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5984 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5985 if (!ufshcd_is_link_active(hba)) {
5986 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5987 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5988 }
5989 }
5990}
5991
5992static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5993{
5994 int ret = 0;
5995
5996 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5997 !hba->dev_info.is_lu_power_on_wp) {
5998 ret = ufshcd_setup_vreg(hba, true);
5999 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006000 if (!ret && !ufshcd_is_link_active(hba)) {
6001 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6002 if (ret)
6003 goto vcc_disable;
6004 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6005 if (ret)
6006 goto vccq_lpm;
6007 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07006008 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006009 }
6010 goto out;
6011
6012vccq_lpm:
6013 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6014vcc_disable:
6015 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6016out:
6017 return ret;
6018}
6019
6020static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6021{
6022 if (ufshcd_is_link_off(hba))
6023 ufshcd_setup_hba_vreg(hba, false);
6024}
6025
6026static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6027{
6028 if (ufshcd_is_link_off(hba))
6029 ufshcd_setup_hba_vreg(hba, true);
6030}
6031
6032/**
6033 * ufshcd_suspend - helper function for suspend operations
6034 * @hba: per adapter instance
6035 * @pm_op: desired low power operation type
6036 *
6037 * This function will try to put the UFS device and link into low power
6038 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6039 * (System PM level).
6040 *
6041 * If this function is called during shutdown, it will make sure that
6042 * both UFS device and UFS link is powered off.
6043 *
6044 * NOTE: UFS device & link must be active before we enter in this function.
6045 *
6046 * Returns 0 for success and non-zero for failure
6047 */
6048static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6049{
6050 int ret = 0;
6051 enum ufs_pm_level pm_lvl;
6052 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6053 enum uic_link_state req_link_state;
6054
6055 hba->pm_op_in_progress = 1;
6056 if (!ufshcd_is_shutdown_pm(pm_op)) {
6057 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6058 hba->rpm_lvl : hba->spm_lvl;
6059 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6060 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6061 } else {
6062 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6063 req_link_state = UIC_LINK_OFF_STATE;
6064 }
6065
6066 /*
6067 * If we can't transition into any of the low power modes
6068 * just gate the clocks.
6069 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006070 ufshcd_hold(hba, false);
6071 hba->clk_gating.is_suspended = true;
6072
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006073 ufshcd_suspend_clkscaling(hba);
6074
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006075 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6076 req_link_state == UIC_LINK_ACTIVE_STATE) {
6077 goto disable_clks;
6078 }
6079
6080 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6081 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006082 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006083
6084 /* UFS device & link must be active before we enter in this function */
6085 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6086 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006087 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006088 }
6089
6090 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006091 if (ufshcd_can_autobkops_during_suspend(hba)) {
6092 /*
6093 * The device is idle with no requests in the queue,
6094 * allow background operations if bkops status shows
6095 * that performance might be impacted.
6096 */
6097 ret = ufshcd_urgent_bkops(hba);
6098 if (ret)
6099 goto enable_gating;
6100 } else {
6101 /* make sure that auto bkops is disabled */
6102 ufshcd_disable_auto_bkops(hba);
6103 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006104 }
6105
6106 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6107 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6108 !ufshcd_is_runtime_pm(pm_op))) {
6109 /* ensure that bkops is disabled */
6110 ufshcd_disable_auto_bkops(hba);
6111 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6112 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006113 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006114 }
6115
6116 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6117 if (ret)
6118 goto set_dev_active;
6119
6120 ufshcd_vreg_set_lpm(hba);
6121
6122disable_clks:
6123 /*
6124 * Call vendor specific suspend callback. As these callbacks may access
6125 * vendor specific host controller register space call them before the
6126 * host clocks are ON.
6127 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006128 ret = ufshcd_vops_suspend(hba, pm_op);
6129 if (ret)
6130 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006131
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006132 if (!ufshcd_is_link_active(hba))
6133 ufshcd_setup_clocks(hba, false);
6134 else
6135 /* If link is active, device ref_clk can't be switched off */
6136 __ufshcd_setup_clocks(hba, false, true);
6137
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006138 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006139 /*
6140 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006141 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006142 */
6143 ufshcd_disable_irq(hba);
6144 /* Put the host controller in low power mode if possible */
6145 ufshcd_hba_vreg_set_lpm(hba);
6146 goto out;
6147
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006148set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07006149 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006150 ufshcd_vreg_set_hpm(hba);
6151 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6152 ufshcd_set_link_active(hba);
6153 else if (ufshcd_is_link_off(hba))
6154 ufshcd_host_reset_and_restore(hba);
6155set_dev_active:
6156 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6157 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006158enable_gating:
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006159 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006160 hba->clk_gating.is_suspended = false;
6161 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006162out:
6163 hba->pm_op_in_progress = 0;
6164 return ret;
6165}
6166
6167/**
6168 * ufshcd_resume - helper function for resume operations
6169 * @hba: per adapter instance
6170 * @pm_op: runtime PM or system PM
6171 *
6172 * This function basically brings the UFS device, UniPro link and controller
6173 * to active state.
6174 *
6175 * Returns 0 for success and non-zero for failure
6176 */
6177static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6178{
6179 int ret;
6180 enum uic_link_state old_link_state;
6181
6182 hba->pm_op_in_progress = 1;
6183 old_link_state = hba->uic_link_state;
6184
6185 ufshcd_hba_vreg_set_hpm(hba);
6186 /* Make sure clocks are enabled before accessing controller */
6187 ret = ufshcd_setup_clocks(hba, true);
6188 if (ret)
6189 goto out;
6190
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006191 /* enable the host irq as host controller would be active soon */
6192 ret = ufshcd_enable_irq(hba);
6193 if (ret)
6194 goto disable_irq_and_vops_clks;
6195
6196 ret = ufshcd_vreg_set_hpm(hba);
6197 if (ret)
6198 goto disable_irq_and_vops_clks;
6199
6200 /*
6201 * Call vendor specific resume callback. As these callbacks may access
6202 * vendor specific host controller register space call them when the
6203 * host clocks are ON.
6204 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006205 ret = ufshcd_vops_resume(hba, pm_op);
6206 if (ret)
6207 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006208
6209 if (ufshcd_is_link_hibern8(hba)) {
6210 ret = ufshcd_uic_hibern8_exit(hba);
6211 if (!ret)
6212 ufshcd_set_link_active(hba);
6213 else
6214 goto vendor_suspend;
6215 } else if (ufshcd_is_link_off(hba)) {
6216 ret = ufshcd_host_reset_and_restore(hba);
6217 /*
6218 * ufshcd_host_reset_and_restore() should have already
6219 * set the link state as active
6220 */
6221 if (ret || !ufshcd_is_link_active(hba))
6222 goto vendor_suspend;
6223 }
6224
6225 if (!ufshcd_is_ufs_dev_active(hba)) {
6226 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6227 if (ret)
6228 goto set_old_link_state;
6229 }
6230
Subhash Jadavani374a2462014-09-25 15:32:35 +03006231 /*
6232 * If BKOPs operations are urgently needed at this moment then
6233 * keep auto-bkops enabled or else disable it.
6234 */
6235 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006236 hba->clk_gating.is_suspended = false;
6237
Gilad Bronera5082532016-10-17 17:10:00 -07006238 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006239
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006240 /* Schedule clock gating in case of no access to UFS device yet */
6241 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006242 goto out;
6243
6244set_old_link_state:
6245 ufshcd_link_state_transition(hba, old_link_state, 0);
6246vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006247 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006248disable_vreg:
6249 ufshcd_vreg_set_lpm(hba);
6250disable_irq_and_vops_clks:
6251 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006252 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006253 ufshcd_setup_clocks(hba, false);
6254out:
6255 hba->pm_op_in_progress = 0;
6256 return ret;
6257}
6258
6259/**
6260 * ufshcd_system_suspend - system suspend routine
6261 * @hba: per adapter instance
6262 * @pm_op: runtime PM or system PM
6263 *
6264 * Check the description of ufshcd_suspend() function for more details.
6265 *
6266 * Returns 0 for success and non-zero for failure
6267 */
6268int ufshcd_system_suspend(struct ufs_hba *hba)
6269{
6270 int ret = 0;
6271
6272 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006273 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006274
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08006275 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6276 hba->curr_dev_pwr_mode) &&
6277 (ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl) ==
6278 hba->uic_link_state))
6279 goto out;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006280
subhashj@codeaurora.org0b257732016-11-23 16:33:08 -08006281 if (pm_runtime_suspended(hba->dev)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006282 /*
6283 * UFS device and/or UFS link low power states during runtime
6284 * suspend seems to be different than what is expected during
6285 * system suspend. Hence runtime resume the devic & link and
6286 * let the system suspend low power states to take effect.
6287 * TODO: If resume takes longer time, we might have optimize
6288 * it in future by not resuming everything if possible.
6289 */
6290 ret = ufshcd_runtime_resume(hba);
6291 if (ret)
6292 goto out;
6293 }
6294
6295 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6296out:
Dolev Ravive7850602014-09-25 15:32:36 +03006297 if (!ret)
6298 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006299 return ret;
6300}
6301EXPORT_SYMBOL(ufshcd_system_suspend);
6302
6303/**
6304 * ufshcd_system_resume - system resume routine
6305 * @hba: per adapter instance
6306 *
6307 * Returns 0 for success and non-zero for failure
6308 */
6309
6310int ufshcd_system_resume(struct ufs_hba *hba)
6311{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006312 if (!hba)
6313 return -EINVAL;
6314
6315 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006316 /*
6317 * Let the runtime resume take care of resuming
6318 * if runtime suspended.
6319 */
6320 return 0;
6321
6322 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6323}
6324EXPORT_SYMBOL(ufshcd_system_resume);
6325
6326/**
6327 * ufshcd_runtime_suspend - runtime suspend routine
6328 * @hba: per adapter instance
6329 *
6330 * Check the description of ufshcd_suspend() function for more details.
6331 *
6332 * Returns 0 for success and non-zero for failure
6333 */
6334int ufshcd_runtime_suspend(struct ufs_hba *hba)
6335{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006336 if (!hba)
6337 return -EINVAL;
6338
6339 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006340 return 0;
6341
6342 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306343}
6344EXPORT_SYMBOL(ufshcd_runtime_suspend);
6345
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006346/**
6347 * ufshcd_runtime_resume - runtime resume routine
6348 * @hba: per adapter instance
6349 *
6350 * This function basically brings the UFS device, UniPro link and controller
6351 * to active state. Following operations are done in this function:
6352 *
6353 * 1. Turn on all the controller related clocks
6354 * 2. Bring the UniPro link out of Hibernate state
6355 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6356 * to active state.
6357 * 4. If auto-bkops is enabled on the device, disable it.
6358 *
6359 * So following would be the possible power state after this function return
6360 * successfully:
6361 * S1: UFS device in Active state with VCC rail ON
6362 * UniPro link in Active state
6363 * All the UFS/UniPro controller clocks are ON
6364 *
6365 * Returns 0 for success and non-zero for failure
6366 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306367int ufshcd_runtime_resume(struct ufs_hba *hba)
6368{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006369 if (!hba)
6370 return -EINVAL;
6371
6372 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306373 return 0;
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006374
6375 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306376}
6377EXPORT_SYMBOL(ufshcd_runtime_resume);
6378
6379int ufshcd_runtime_idle(struct ufs_hba *hba)
6380{
6381 return 0;
6382}
6383EXPORT_SYMBOL(ufshcd_runtime_idle);
6384
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306385/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006386 * ufshcd_shutdown - shutdown routine
6387 * @hba: per adapter instance
6388 *
6389 * This function would power off both UFS device and UFS link.
6390 *
6391 * Returns 0 always to allow force shutdown even in case of errors.
6392 */
6393int ufshcd_shutdown(struct ufs_hba *hba)
6394{
6395 int ret = 0;
6396
6397 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6398 goto out;
6399
6400 if (pm_runtime_suspended(hba->dev)) {
6401 ret = ufshcd_runtime_resume(hba);
6402 if (ret)
6403 goto out;
6404 }
6405
6406 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6407out:
6408 if (ret)
6409 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6410 /* allow force shutdown even in case of errors */
6411 return 0;
6412}
6413EXPORT_SYMBOL(ufshcd_shutdown);
6414
6415/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306416 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306417 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306418 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306419 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306420void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306421{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306422 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306423 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306424 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006425 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306426
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006427 ufshcd_exit_clk_gating(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006428 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306429}
6430EXPORT_SYMBOL_GPL(ufshcd_remove);
6431
6432/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006433 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6434 * @hba: pointer to Host Bus Adapter (HBA)
6435 */
6436void ufshcd_dealloc_host(struct ufs_hba *hba)
6437{
6438 scsi_host_put(hba->host);
6439}
6440EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6441
6442/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006443 * ufshcd_set_dma_mask - Set dma mask based on the controller
6444 * addressing capability
6445 * @hba: per adapter instance
6446 *
6447 * Returns 0 for success, non-zero for failure
6448 */
6449static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6450{
6451 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6452 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6453 return 0;
6454 }
6455 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6456}
6457
6458/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006459 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306460 * @dev: pointer to device handle
6461 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306462 * Returns 0 on success, non-zero value on failure
6463 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006464int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306465{
6466 struct Scsi_Host *host;
6467 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006468 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306469
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306470 if (!dev) {
6471 dev_err(dev,
6472 "Invalid memory reference for dev is NULL\n");
6473 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306474 goto out_error;
6475 }
6476
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306477 host = scsi_host_alloc(&ufshcd_driver_template,
6478 sizeof(struct ufs_hba));
6479 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306480 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306481 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306482 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306483 }
6484 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306485 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306486 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006487 *hba_handle = hba;
6488
6489out_error:
6490 return err;
6491}
6492EXPORT_SYMBOL(ufshcd_alloc_host);
6493
Sahitya Tummala856b3482014-09-25 15:32:34 +03006494static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6495{
6496 int ret = 0;
6497 struct ufs_clk_info *clki;
6498 struct list_head *head = &hba->clk_list_head;
6499
6500 if (!head || list_empty(head))
6501 goto out;
6502
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006503 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6504 if (ret)
6505 return ret;
6506
Sahitya Tummala856b3482014-09-25 15:32:34 +03006507 list_for_each_entry(clki, head, list) {
6508 if (!IS_ERR_OR_NULL(clki->clk)) {
6509 if (scale_up && clki->max_freq) {
6510 if (clki->curr_freq == clki->max_freq)
6511 continue;
6512 ret = clk_set_rate(clki->clk, clki->max_freq);
6513 if (ret) {
6514 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6515 __func__, clki->name,
6516 clki->max_freq, ret);
6517 break;
6518 }
6519 clki->curr_freq = clki->max_freq;
6520
6521 } else if (!scale_up && clki->min_freq) {
6522 if (clki->curr_freq == clki->min_freq)
6523 continue;
6524 ret = clk_set_rate(clki->clk, clki->min_freq);
6525 if (ret) {
6526 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6527 __func__, clki->name,
6528 clki->min_freq, ret);
6529 break;
6530 }
6531 clki->curr_freq = clki->min_freq;
6532 }
6533 }
6534 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6535 clki->name, clk_get_rate(clki->clk));
6536 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006537
6538 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6539
Sahitya Tummala856b3482014-09-25 15:32:34 +03006540out:
6541 return ret;
6542}
6543
6544static int ufshcd_devfreq_target(struct device *dev,
6545 unsigned long *freq, u32 flags)
6546{
6547 int err = 0;
6548 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006549 bool release_clk_hold = false;
6550 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006551
6552 if (!ufshcd_is_clkscaling_enabled(hba))
6553 return -EINVAL;
6554
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006555 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6556 if (ufshcd_eh_in_progress(hba)) {
6557 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6558 return 0;
6559 }
6560
6561 if (ufshcd_is_clkgating_allowed(hba) &&
6562 (hba->clk_gating.state != CLKS_ON)) {
6563 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6564 /* hold the vote until the scaling work is completed */
6565 hba->clk_gating.active_reqs++;
6566 release_clk_hold = true;
6567 hba->clk_gating.state = CLKS_ON;
6568 } else {
6569 /*
6570 * Clock gating work seems to be running in parallel
6571 * hence skip scaling work to avoid deadlock between
6572 * current scaling work and gating work.
6573 */
6574 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6575 return 0;
6576 }
6577 }
6578 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6579
Sahitya Tummala856b3482014-09-25 15:32:34 +03006580 if (*freq == UINT_MAX)
6581 err = ufshcd_scale_clks(hba, true);
6582 else if (*freq == 0)
6583 err = ufshcd_scale_clks(hba, false);
6584
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006585 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6586 if (release_clk_hold)
6587 __ufshcd_release(hba);
6588 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6589
Sahitya Tummala856b3482014-09-25 15:32:34 +03006590 return err;
6591}
6592
6593static int ufshcd_devfreq_get_dev_status(struct device *dev,
6594 struct devfreq_dev_status *stat)
6595{
6596 struct ufs_hba *hba = dev_get_drvdata(dev);
6597 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6598 unsigned long flags;
6599
6600 if (!ufshcd_is_clkscaling_enabled(hba))
6601 return -EINVAL;
6602
6603 memset(stat, 0, sizeof(*stat));
6604
6605 spin_lock_irqsave(hba->host->host_lock, flags);
6606 if (!scaling->window_start_t)
6607 goto start_window;
6608
6609 if (scaling->is_busy_started)
6610 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6611 scaling->busy_start_t));
6612
6613 stat->total_time = jiffies_to_usecs((long)jiffies -
6614 (long)scaling->window_start_t);
6615 stat->busy_time = scaling->tot_busy_t;
6616start_window:
6617 scaling->window_start_t = jiffies;
6618 scaling->tot_busy_t = 0;
6619
6620 if (hba->outstanding_reqs) {
6621 scaling->busy_start_t = ktime_get();
6622 scaling->is_busy_started = true;
6623 } else {
6624 scaling->busy_start_t = ktime_set(0, 0);
6625 scaling->is_busy_started = false;
6626 }
6627 spin_unlock_irqrestore(hba->host->host_lock, flags);
6628 return 0;
6629}
6630
6631static struct devfreq_dev_profile ufs_devfreq_profile = {
6632 .polling_ms = 100,
6633 .target = ufshcd_devfreq_target,
6634 .get_dev_status = ufshcd_devfreq_get_dev_status,
6635};
6636
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006637/**
6638 * ufshcd_init - Driver initialization routine
6639 * @hba: per-adapter instance
6640 * @mmio_base: base register address
6641 * @irq: Interrupt line of device
6642 * Returns 0 on success, non-zero value on failure
6643 */
6644int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6645{
6646 int err;
6647 struct Scsi_Host *host = hba->host;
6648 struct device *dev = hba->dev;
6649
6650 if (!mmio_base) {
6651 dev_err(hba->dev,
6652 "Invalid memory reference for mmio_base is NULL\n");
6653 err = -ENODEV;
6654 goto out_error;
6655 }
6656
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306657 hba->mmio_base = mmio_base;
6658 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306659
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006660 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006661 if (err)
6662 goto out_error;
6663
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306664 /* Read capabilities registers */
6665 ufshcd_hba_capabilities(hba);
6666
6667 /* Get UFS version supported by the controller */
6668 hba->ufs_version = ufshcd_get_ufs_version(hba);
6669
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306670 /* Get Interrupt bit mask per version */
6671 hba->intr_mask = ufshcd_get_intr_mask(hba);
6672
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006673 err = ufshcd_set_dma_mask(hba);
6674 if (err) {
6675 dev_err(hba->dev, "set dma mask failed\n");
6676 goto out_disable;
6677 }
6678
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306679 /* Allocate memory for host memory space */
6680 err = ufshcd_memory_alloc(hba);
6681 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306682 dev_err(hba->dev, "Memory allocation failed\n");
6683 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306684 }
6685
6686 /* Configure LRB */
6687 ufshcd_host_memory_configure(hba);
6688
6689 host->can_queue = hba->nutrs;
6690 host->cmd_per_lun = hba->nutrs;
6691 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006692 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306693 host->max_channel = UFSHCD_MAX_CHANNEL;
6694 host->unique_id = host->host_no;
6695 host->max_cmd_len = MAX_CDB_SIZE;
6696
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006697 hba->max_pwr_info.is_valid = false;
6698
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306699 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306700 init_waitqueue_head(&hba->tm_wq);
6701 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306702
6703 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306704 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306705 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306706
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306707 /* Initialize UIC command mutex */
6708 mutex_init(&hba->uic_cmd_mutex);
6709
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306710 /* Initialize mutex for device management commands */
6711 mutex_init(&hba->dev_cmd.lock);
6712
6713 /* Initialize device management tag acquire wait queue */
6714 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6715
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006716 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006717
6718 /*
6719 * In order to avoid any spurious interrupt immediately after
6720 * registering UFS controller interrupt handler, clear any pending UFS
6721 * interrupt status and disable all the UFS interrupts.
6722 */
6723 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6724 REG_INTERRUPT_STATUS);
6725 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6726 /*
6727 * Make sure that UFS interrupts are disabled and any pending interrupt
6728 * status is cleared before registering UFS interrupt handler.
6729 */
6730 mb();
6731
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306732 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006733 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306734 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306735 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006736 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006737 } else {
6738 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306739 }
6740
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306741 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306742 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306743 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006744 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306745 }
6746
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306747 /* Host controller enable */
6748 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306749 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306750 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306751 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306752 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306753
Sahitya Tummala856b3482014-09-25 15:32:34 +03006754 if (ufshcd_is_clkscaling_enabled(hba)) {
Chanwoo Choi4861ee12016-11-08 18:13:28 +09006755 hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile,
Sahitya Tummala856b3482014-09-25 15:32:34 +03006756 "simple_ondemand", NULL);
6757 if (IS_ERR(hba->devfreq)) {
6758 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6759 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006760 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006761 goto out_remove_scsi_host;
6762 }
6763 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07006764 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006765 }
6766
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306767 /* Hold auto suspend until async scan completes */
6768 pm_runtime_get_sync(dev);
6769
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006770 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08006771 * We are assuming that device wasn't put in sleep/power-down
6772 * state exclusively during the boot stage before kernel.
6773 * This assumption helps avoid doing link startup twice during
6774 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006775 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08006776 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006777
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306778 async_schedule(ufshcd_async_scan, hba);
6779
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306780 return 0;
6781
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306782out_remove_scsi_host:
6783 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006784exit_gating:
6785 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306786out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006787 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006788 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306789out_error:
6790 return err;
6791}
6792EXPORT_SYMBOL_GPL(ufshcd_init);
6793
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306794MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6795MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306796MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306797MODULE_LICENSE("GPL");
6798MODULE_VERSION(UFSHCD_DRIVER_VERSION);