blob: 16c5c50d054d0f24aef36dee132e49dfd8f3b6e0 [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
2519 if (!retries)
2520 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2521 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2522 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 Gardi64238fb2016-02-01 15:02:43 +02002586 if (!retries)
2587 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2588 get, UIC_GET_ATTR_ID(attr_sel), retries);
2589
2590 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302591 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002592
2593 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2594 && pwr_mode_change)
2595 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302596out:
2597 return ret;
2598}
2599EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2600
2601/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002602 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2603 * state) and waits for it to take effect.
2604 *
2605 * @hba: per adapter instance
2606 * @cmd: UIC command to execute
2607 *
2608 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2609 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2610 * and device UniPro link and hence it's final completion would be indicated by
2611 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2612 * addition to normal UIC command completion Status (UCCS). This function only
2613 * returns after the relevant status bits indicate the completion.
2614 *
2615 * Returns 0 on success, non-zero value on failure
2616 */
2617static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2618{
2619 struct completion uic_async_done;
2620 unsigned long flags;
2621 u8 status;
2622 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002623 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002624
2625 mutex_lock(&hba->uic_cmd_mutex);
2626 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002627 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002628
2629 spin_lock_irqsave(hba->host->host_lock, flags);
2630 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002631 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2632 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2633 /*
2634 * Make sure UIC command completion interrupt is disabled before
2635 * issuing UIC command.
2636 */
2637 wmb();
2638 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002639 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002640 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2641 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002642 if (ret) {
2643 dev_err(hba->dev,
2644 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2645 cmd->command, cmd->argument3, ret);
2646 goto out;
2647 }
2648
2649 if (!wait_for_completion_timeout(hba->uic_async_done,
2650 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2651 dev_err(hba->dev,
2652 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2653 cmd->command, cmd->argument3);
2654 ret = -ETIMEDOUT;
2655 goto out;
2656 }
2657
2658 status = ufshcd_get_upmcrs(hba);
2659 if (status != PWR_LOCAL) {
2660 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002661 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002662 cmd->command, status);
2663 ret = (status != PWR_OK) ? status : -1;
2664 }
2665out:
2666 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002667 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002668 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002669 if (reenable_intr)
2670 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002671 spin_unlock_irqrestore(hba->host->host_lock, flags);
2672 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002673
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002674 return ret;
2675}
2676
2677/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302678 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2679 * using DME_SET primitives.
2680 * @hba: per adapter instance
2681 * @mode: powr mode value
2682 *
2683 * Returns 0 on success, non-zero value on failure
2684 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302685static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302686{
2687 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002688 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302689
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002690 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2691 ret = ufshcd_dme_set(hba,
2692 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2693 if (ret) {
2694 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2695 __func__, ret);
2696 goto out;
2697 }
2698 }
2699
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302700 uic_cmd.command = UIC_CMD_DME_SET;
2701 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2702 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002703 ufshcd_hold(hba, false);
2704 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2705 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302706
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002707out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002708 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002709}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302710
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002711static int ufshcd_link_recovery(struct ufs_hba *hba)
2712{
2713 int ret;
2714 unsigned long flags;
2715
2716 spin_lock_irqsave(hba->host->host_lock, flags);
2717 hba->ufshcd_state = UFSHCD_STATE_RESET;
2718 ufshcd_set_eh_in_progress(hba);
2719 spin_unlock_irqrestore(hba->host->host_lock, flags);
2720
2721 ret = ufshcd_host_reset_and_restore(hba);
2722
2723 spin_lock_irqsave(hba->host->host_lock, flags);
2724 if (ret)
2725 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2726 ufshcd_clear_eh_in_progress(hba);
2727 spin_unlock_irqrestore(hba->host->host_lock, flags);
2728
2729 if (ret)
2730 dev_err(hba->dev, "%s: link recovery failed, err %d",
2731 __func__, ret);
2732
2733 return ret;
2734}
2735
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002736static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002737{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002738 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002739 struct uic_command uic_cmd = {0};
2740
Kiwoong Kimee32c902016-11-10 21:17:43 +09002741 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE);
2742
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002743 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002744 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002745
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002746 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002747 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2748 __func__, ret);
2749
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002750 /*
2751 * If link recovery fails then return error so that caller
2752 * don't retry the hibern8 enter again.
2753 */
2754 if (ufshcd_link_recovery(hba))
2755 ret = -ENOLINK;
Kiwoong Kimee32c902016-11-10 21:17:43 +09002756 } else
2757 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER,
2758 POST_CHANGE);
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002759
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002760 return ret;
2761}
2762
2763static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2764{
2765 int ret = 0, retries;
2766
2767 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2768 ret = __ufshcd_uic_hibern8_enter(hba);
2769 if (!ret || ret == -ENOLINK)
2770 goto out;
2771 }
2772out:
2773 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002774}
2775
2776static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2777{
2778 struct uic_command uic_cmd = {0};
2779 int ret;
2780
Kiwoong Kimee32c902016-11-10 21:17:43 +09002781 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE);
2782
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002783 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2784 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302785 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002786 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2787 __func__, ret);
2788 ret = ufshcd_link_recovery(hba);
Kiwoong Kimee32c902016-11-10 21:17:43 +09002789 } else
2790 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT,
2791 POST_CHANGE);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302792
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302793 return ret;
2794}
2795
Yaniv Gardi50646362014-10-23 13:25:13 +03002796 /**
2797 * ufshcd_init_pwr_info - setting the POR (power on reset)
2798 * values in hba power info
2799 * @hba: per-adapter instance
2800 */
2801static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2802{
2803 hba->pwr_info.gear_rx = UFS_PWM_G1;
2804 hba->pwr_info.gear_tx = UFS_PWM_G1;
2805 hba->pwr_info.lane_rx = 1;
2806 hba->pwr_info.lane_tx = 1;
2807 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2808 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2809 hba->pwr_info.hs_rate = 0;
2810}
2811
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302812/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002813 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2814 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302815 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002816static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302817{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002818 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2819
2820 if (hba->max_pwr_info.is_valid)
2821 return 0;
2822
2823 pwr_info->pwr_tx = FASTAUTO_MODE;
2824 pwr_info->pwr_rx = FASTAUTO_MODE;
2825 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302826
2827 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002828 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2829 &pwr_info->lane_rx);
2830 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2831 &pwr_info->lane_tx);
2832
2833 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2834 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2835 __func__,
2836 pwr_info->lane_rx,
2837 pwr_info->lane_tx);
2838 return -EINVAL;
2839 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302840
2841 /*
2842 * First, get the maximum gears of HS speed.
2843 * If a zero value, it means there is no HSGEAR capability.
2844 * Then, get the maximum gears of PWM speed.
2845 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002846 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2847 if (!pwr_info->gear_rx) {
2848 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2849 &pwr_info->gear_rx);
2850 if (!pwr_info->gear_rx) {
2851 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2852 __func__, pwr_info->gear_rx);
2853 return -EINVAL;
2854 }
2855 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302856 }
2857
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002858 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2859 &pwr_info->gear_tx);
2860 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302861 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002862 &pwr_info->gear_tx);
2863 if (!pwr_info->gear_tx) {
2864 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2865 __func__, pwr_info->gear_tx);
2866 return -EINVAL;
2867 }
2868 pwr_info->pwr_tx = SLOWAUTO_MODE;
2869 }
2870
2871 hba->max_pwr_info.is_valid = true;
2872 return 0;
2873}
2874
2875static int ufshcd_change_power_mode(struct ufs_hba *hba,
2876 struct ufs_pa_layer_attr *pwr_mode)
2877{
2878 int ret;
2879
2880 /* if already configured to the requested pwr_mode */
2881 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2882 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2883 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2884 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2885 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2886 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2887 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2888 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2889 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302890 }
2891
2892 /*
2893 * Configure attributes for power mode change with below.
2894 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2895 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2896 * - PA_HSSERIES
2897 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002898 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2899 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2900 pwr_mode->lane_rx);
2901 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2902 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302903 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002904 else
2905 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302906
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002907 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2908 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2909 pwr_mode->lane_tx);
2910 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2911 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302912 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002913 else
2914 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302915
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002916 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2917 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2918 pwr_mode->pwr_rx == FAST_MODE ||
2919 pwr_mode->pwr_tx == FAST_MODE)
2920 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2921 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302922
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002923 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2924 | pwr_mode->pwr_tx);
2925
2926 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302927 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002928 "%s: power mode change failed %d\n", __func__, ret);
2929 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002930 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2931 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002932
2933 memcpy(&hba->pwr_info, pwr_mode,
2934 sizeof(struct ufs_pa_layer_attr));
2935 }
2936
2937 return ret;
2938}
2939
2940/**
2941 * ufshcd_config_pwr_mode - configure a new power mode
2942 * @hba: per-adapter instance
2943 * @desired_pwr_mode: desired power configuration
2944 */
2945static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2946 struct ufs_pa_layer_attr *desired_pwr_mode)
2947{
2948 struct ufs_pa_layer_attr final_params = { 0 };
2949 int ret;
2950
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002951 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2952 desired_pwr_mode, &final_params);
2953
2954 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002955 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2956
2957 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302958
2959 return ret;
2960}
2961
2962/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302963 * ufshcd_complete_dev_init() - checks device readiness
2964 * hba: per-adapter instance
2965 *
2966 * Set fDeviceInit flag and poll until device toggles it.
2967 */
2968static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2969{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002970 int i;
2971 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302972 bool flag_res = 1;
2973
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002974 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2975 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302976 if (err) {
2977 dev_err(hba->dev,
2978 "%s setting fDeviceInit flag failed with error %d\n",
2979 __func__, err);
2980 goto out;
2981 }
2982
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002983 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2984 for (i = 0; i < 1000 && !err && flag_res; i++)
2985 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2986 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2987
Dolev Raviv68078d52013-07-30 00:35:58 +05302988 if (err)
2989 dev_err(hba->dev,
2990 "%s reading fDeviceInit flag failed with error %d\n",
2991 __func__, err);
2992 else if (flag_res)
2993 dev_err(hba->dev,
2994 "%s fDeviceInit was not cleared by the device\n",
2995 __func__);
2996
2997out:
2998 return err;
2999}
3000
3001/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303002 * ufshcd_make_hba_operational - Make UFS controller operational
3003 * @hba: per adapter instance
3004 *
3005 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003006 * 1. Enable required interrupts
3007 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003008 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003009 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303010 *
3011 * Returns 0 on success, non-zero value on failure
3012 */
3013static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3014{
3015 int err = 0;
3016 u32 reg;
3017
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303018 /* Enable required interrupts */
3019 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3020
3021 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003022 if (ufshcd_is_intr_aggr_allowed(hba))
3023 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3024 else
3025 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303026
3027 /* Configure UTRL and UTMRL base address registers */
3028 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3029 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3030 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3031 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3032 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3033 REG_UTP_TASK_REQ_LIST_BASE_L);
3034 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3035 REG_UTP_TASK_REQ_LIST_BASE_H);
3036
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303037 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003038 * Make sure base address and interrupt setup are updated before
3039 * enabling the run/stop registers below.
3040 */
3041 wmb();
3042
3043 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303044 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303045 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003046 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303047 if (!(ufshcd_get_lists_status(reg))) {
3048 ufshcd_enable_run_stop_reg(hba);
3049 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303050 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303051 "Host controller not ready to process requests");
3052 err = -EIO;
3053 goto out;
3054 }
3055
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303056out:
3057 return err;
3058}
3059
3060/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003061 * ufshcd_hba_stop - Send controller to reset state
3062 * @hba: per adapter instance
3063 * @can_sleep: perform sleep or just spin
3064 */
3065static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3066{
3067 int err;
3068
3069 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3070 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3071 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3072 10, 1, can_sleep);
3073 if (err)
3074 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3075}
3076
3077/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303078 * ufshcd_hba_enable - initialize the controller
3079 * @hba: per adapter instance
3080 *
3081 * The controller resets itself and controller firmware initialization
3082 * sequence kicks off. When controller is ready it will set
3083 * the Host Controller Enable bit to 1.
3084 *
3085 * Returns 0 on success, non-zero value on failure
3086 */
3087static int ufshcd_hba_enable(struct ufs_hba *hba)
3088{
3089 int retry;
3090
3091 /*
3092 * msleep of 1 and 5 used in this function might result in msleep(20),
3093 * but it was necessary to send the UFS FPGA to reset mode during
3094 * development and testing of this driver. msleep can be changed to
3095 * mdelay and retry count can be reduced based on the controller.
3096 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003097 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303098 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003099 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303100
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003101 /* UniPro link is disabled at this point */
3102 ufshcd_set_link_off(hba);
3103
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003104 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003105
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303106 /* start controller initialization sequence */
3107 ufshcd_hba_start(hba);
3108
3109 /*
3110 * To initialize a UFS host controller HCE bit must be set to 1.
3111 * During initialization the HCE bit value changes from 1->0->1.
3112 * When the host controller completes initialization sequence
3113 * it sets the value of HCE bit to 1. The same HCE bit is read back
3114 * to check if the controller has completed initialization sequence.
3115 * So without this delay the value HCE = 1, set in the previous
3116 * instruction might be read back.
3117 * This delay can be changed based on the controller.
3118 */
3119 msleep(1);
3120
3121 /* wait for the host controller to complete initialization */
3122 retry = 10;
3123 while (ufshcd_is_hba_active(hba)) {
3124 if (retry) {
3125 retry--;
3126 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303127 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303128 "Controller enable failed\n");
3129 return -EIO;
3130 }
3131 msleep(5);
3132 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003133
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003134 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003135 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003136
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003137 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303139 return 0;
3140}
3141
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003142static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3143{
3144 int tx_lanes, i, err = 0;
3145
3146 if (!peer)
3147 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3148 &tx_lanes);
3149 else
3150 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3151 &tx_lanes);
3152 for (i = 0; i < tx_lanes; i++) {
3153 if (!peer)
3154 err = ufshcd_dme_set(hba,
3155 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3156 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3157 0);
3158 else
3159 err = ufshcd_dme_peer_set(hba,
3160 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3161 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3162 0);
3163 if (err) {
3164 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3165 __func__, peer, i, err);
3166 break;
3167 }
3168 }
3169
3170 return err;
3171}
3172
3173static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3174{
3175 return ufshcd_disable_tx_lcc(hba, true);
3176}
3177
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303178/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303179 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303180 * @hba: per adapter instance
3181 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303182 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303183 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303184static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303185{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303186 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003187 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003188 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303189
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003190 /*
3191 * If UFS device isn't active then we will have to issue link startup
3192 * 2 times to make sure the device state move to active.
3193 */
3194 if (!ufshcd_is_ufs_dev_active(hba))
3195 link_startup_again = true;
3196
3197link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003198 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003199 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303200
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003201 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003202
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003203 /* check if device is detected by inter-connect layer */
3204 if (!ret && !ufshcd_is_device_present(hba)) {
3205 dev_err(hba->dev, "%s: Device not present\n", __func__);
3206 ret = -ENXIO;
3207 goto out;
3208 }
3209
3210 /*
3211 * DME link lost indication is only received when link is up,
3212 * but we can't be sure if the link is up until link startup
3213 * succeeds. So reset the local Uni-Pro and try again.
3214 */
3215 if (ret && ufshcd_hba_enable(hba))
3216 goto out;
3217 } while (ret && retries--);
3218
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303219 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003220 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303221 goto out;
3222
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08003223 if (link_startup_again) {
3224 link_startup_again = false;
3225 retries = DME_LINKSTARTUP_RETRIES;
3226 goto link_startup;
3227 }
3228
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003229 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3230 ret = ufshcd_disable_device_tx_lcc(hba);
3231 if (ret)
3232 goto out;
3233 }
3234
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003235 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003236 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3237 if (ret)
3238 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003239
3240 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303241out:
3242 if (ret)
3243 dev_err(hba->dev, "link startup failed %d\n", ret);
3244 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303245}
3246
3247/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303248 * ufshcd_verify_dev_init() - Verify device initialization
3249 * @hba: per-adapter instance
3250 *
3251 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3252 * device Transport Protocol (UTP) layer is ready after a reset.
3253 * If the UTP layer at the device side is not initialized, it may
3254 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3255 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3256 */
3257static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3258{
3259 int err = 0;
3260 int retries;
3261
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003262 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303263 mutex_lock(&hba->dev_cmd.lock);
3264 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3265 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3266 NOP_OUT_TIMEOUT);
3267
3268 if (!err || err == -ETIMEDOUT)
3269 break;
3270
3271 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3272 }
3273 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003274 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303275
3276 if (err)
3277 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3278 return err;
3279}
3280
3281/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003282 * ufshcd_set_queue_depth - set lun queue depth
3283 * @sdev: pointer to SCSI device
3284 *
3285 * Read bLUQueueDepth value and activate scsi tagged command
3286 * queueing. For WLUN, queue depth is set to 1. For best-effort
3287 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3288 * value that host can queue.
3289 */
3290static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3291{
3292 int ret = 0;
3293 u8 lun_qdepth;
Dolev Raviv61e07352016-11-23 16:30:49 -08003294 int retries;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003295 struct ufs_hba *hba;
3296
3297 hba = shost_priv(sdev->host);
3298
3299 lun_qdepth = hba->nutrs;
Dolev Raviv61e07352016-11-23 16:30:49 -08003300 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
3301 /* Read descriptor*/
3302 ret = ufshcd_read_unit_desc_param(hba,
3303 ufshcd_scsi_to_upiu_lun(sdev->lun),
3304 UNIT_DESC_PARAM_LU_Q_DEPTH,
3305 &lun_qdepth,
3306 sizeof(lun_qdepth));
3307 if (!ret || ret == -ENOTSUPP)
3308 break;
3309
3310 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, ret);
3311 }
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003312
3313 /* Some WLUN doesn't support unit descriptor */
3314 if (ret == -EOPNOTSUPP)
3315 lun_qdepth = 1;
3316 else if (!lun_qdepth)
3317 /* eventually, we can figure out the real queue depth */
3318 lun_qdepth = hba->nutrs;
3319 else
3320 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3321
3322 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3323 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003324 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003325}
3326
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003327/*
3328 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3329 * @hba: per-adapter instance
3330 * @lun: UFS device lun id
3331 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3332 *
3333 * Returns 0 in case of success and b_lu_write_protect status would be returned
3334 * @b_lu_write_protect parameter.
3335 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3336 * Returns -EINVAL in case of invalid parameters passed to this function.
3337 */
3338static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3339 u8 lun,
3340 u8 *b_lu_write_protect)
3341{
3342 int ret;
3343
3344 if (!b_lu_write_protect)
3345 ret = -EINVAL;
3346 /*
3347 * According to UFS device spec, RPMB LU can't be write
3348 * protected so skip reading bLUWriteProtect parameter for
3349 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3350 */
3351 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3352 ret = -ENOTSUPP;
3353 else
3354 ret = ufshcd_read_unit_desc_param(hba,
3355 lun,
3356 UNIT_DESC_PARAM_LU_WR_PROTECT,
3357 b_lu_write_protect,
3358 sizeof(*b_lu_write_protect));
3359 return ret;
3360}
3361
3362/**
3363 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3364 * status
3365 * @hba: per-adapter instance
3366 * @sdev: pointer to SCSI device
3367 *
3368 */
3369static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3370 struct scsi_device *sdev)
3371{
3372 if (hba->dev_info.f_power_on_wp_en &&
3373 !hba->dev_info.is_lu_power_on_wp) {
3374 u8 b_lu_write_protect;
3375
3376 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3377 &b_lu_write_protect) &&
3378 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3379 hba->dev_info.is_lu_power_on_wp = true;
3380 }
3381}
3382
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003383/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303384 * ufshcd_slave_alloc - handle initial SCSI device configurations
3385 * @sdev: pointer to SCSI device
3386 *
3387 * Returns success
3388 */
3389static int ufshcd_slave_alloc(struct scsi_device *sdev)
3390{
3391 struct ufs_hba *hba;
3392
3393 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303394
3395 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3396 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303397
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303398 /* allow SCSI layer to restart the device in case of errors */
3399 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003400
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003401 /* REPORT SUPPORTED OPERATION CODES is not supported */
3402 sdev->no_report_opcodes = 1;
3403
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003404
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003405 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003406
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003407 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3408
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003409 return 0;
3410}
3411
3412/**
3413 * ufshcd_change_queue_depth - change queue depth
3414 * @sdev: pointer to SCSI device
3415 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003416 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003417 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003418 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003419static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003420{
3421 struct ufs_hba *hba = shost_priv(sdev->host);
3422
3423 if (depth > hba->nutrs)
3424 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003425 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303426}
3427
3428/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003429 * ufshcd_slave_configure - adjust SCSI device configurations
3430 * @sdev: pointer to SCSI device
3431 */
3432static int ufshcd_slave_configure(struct scsi_device *sdev)
3433{
3434 struct request_queue *q = sdev->request_queue;
3435
3436 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3437 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3438
3439 return 0;
3440}
3441
3442/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303443 * ufshcd_slave_destroy - remove SCSI device configurations
3444 * @sdev: pointer to SCSI device
3445 */
3446static void ufshcd_slave_destroy(struct scsi_device *sdev)
3447{
3448 struct ufs_hba *hba;
3449
3450 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003451 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003452 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3453 unsigned long flags;
3454
3455 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003456 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003457 spin_unlock_irqrestore(hba->host->host_lock, flags);
3458 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303459}
3460
3461/**
3462 * ufshcd_task_req_compl - handle task management request completion
3463 * @hba: per adapter instance
3464 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303465 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303466 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303467 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303468 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303469static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303470{
3471 struct utp_task_req_desc *task_req_descp;
3472 struct utp_upiu_task_rsp *task_rsp_upiup;
3473 unsigned long flags;
3474 int ocs_value;
3475 int task_result;
3476
3477 spin_lock_irqsave(hba->host->host_lock, flags);
3478
3479 /* Clear completed tasks from outstanding_tasks */
3480 __clear_bit(index, &hba->outstanding_tasks);
3481
3482 task_req_descp = hba->utmrdl_base_addr;
3483 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3484
3485 if (ocs_value == OCS_SUCCESS) {
3486 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3487 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003488 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3489 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303490 if (resp)
3491 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303492 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303493 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3494 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303495 }
3496 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303497
3498 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303499}
3500
3501/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303502 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3503 * @lrb: pointer to local reference block of completed command
3504 * @scsi_status: SCSI command status
3505 *
3506 * Returns value base on SCSI command status
3507 */
3508static inline int
3509ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3510{
3511 int result = 0;
3512
3513 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303514 case SAM_STAT_CHECK_CONDITION:
3515 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303516 case SAM_STAT_GOOD:
3517 result |= DID_OK << 16 |
3518 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303519 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303520 break;
3521 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303522 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303523 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303524 ufshcd_copy_sense_data(lrbp);
3525 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303526 break;
3527 default:
3528 result |= DID_ERROR << 16;
3529 break;
3530 } /* end of switch */
3531
3532 return result;
3533}
3534
3535/**
3536 * ufshcd_transfer_rsp_status - Get overall status of the response
3537 * @hba: per adapter instance
3538 * @lrb: pointer to local reference block of completed command
3539 *
3540 * Returns result of the command to notify SCSI midlayer
3541 */
3542static inline int
3543ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3544{
3545 int result = 0;
3546 int scsi_status;
3547 int ocs;
3548
3549 /* overall command status of utrd */
3550 ocs = ufshcd_get_tr_ocs(lrbp);
3551
3552 switch (ocs) {
3553 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303554 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303555
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303556 switch (result) {
3557 case UPIU_TRANSACTION_RESPONSE:
3558 /*
3559 * get the response UPIU result to extract
3560 * the SCSI command status
3561 */
3562 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3563
3564 /*
3565 * get the result based on SCSI status response
3566 * to notify the SCSI midlayer of the command status
3567 */
3568 scsi_status = result & MASK_SCSI_STATUS;
3569 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303570
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003571 /*
3572 * Currently we are only supporting BKOPs exception
3573 * events hence we can ignore BKOPs exception event
3574 * during power management callbacks. BKOPs exception
3575 * event is not expected to be raised in runtime suspend
3576 * callback as it allows the urgent bkops.
3577 * During system suspend, we are anyway forcefully
3578 * disabling the bkops and if urgent bkops is needed
3579 * it will be enabled on system resume. Long term
3580 * solution could be to abort the system suspend if
3581 * UFS device needs urgent BKOPs.
3582 */
3583 if (!hba->pm_op_in_progress &&
3584 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303585 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303586 break;
3587 case UPIU_TRANSACTION_REJECT_UPIU:
3588 /* TODO: handle Reject UPIU Response */
3589 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303590 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303591 "Reject UPIU not fully implemented\n");
3592 break;
3593 default:
3594 result = DID_ERROR << 16;
3595 dev_err(hba->dev,
3596 "Unexpected request response code = %x\n",
3597 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303598 break;
3599 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303600 break;
3601 case OCS_ABORTED:
3602 result |= DID_ABORT << 16;
3603 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303604 case OCS_INVALID_COMMAND_STATUS:
3605 result |= DID_REQUEUE << 16;
3606 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303607 case OCS_INVALID_CMD_TABLE_ATTR:
3608 case OCS_INVALID_PRDT_ATTR:
3609 case OCS_MISMATCH_DATA_BUF_SIZE:
3610 case OCS_MISMATCH_RESP_UPIU_SIZE:
3611 case OCS_PEER_COMM_FAILURE:
3612 case OCS_FATAL_ERROR:
3613 default:
3614 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303615 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303616 "OCS error from controller = %x\n", ocs);
3617 break;
3618 } /* end of switch */
3619
3620 return result;
3621}
3622
3623/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303624 * ufshcd_uic_cmd_compl - handle completion of uic command
3625 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303626 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303627 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303628static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303629{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303630 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303631 hba->active_uic_cmd->argument2 |=
3632 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303633 hba->active_uic_cmd->argument3 =
3634 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303635 complete(&hba->active_uic_cmd->done);
3636 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303637
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003638 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3639 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303640}
3641
3642/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003643 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303644 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003645 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303646 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003647static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3648 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303649{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303650 struct ufshcd_lrb *lrbp;
3651 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303652 int result;
3653 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003654
Dolev Ravive9d501b2014-07-01 12:22:37 +03003655 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3656 lrbp = &hba->lrb[index];
3657 cmd = lrbp->cmd;
3658 if (cmd) {
3659 result = ufshcd_transfer_rsp_status(hba, lrbp);
3660 scsi_dma_unmap(cmd);
3661 cmd->result = result;
3662 /* Mark completed command as NULL in LRB */
3663 lrbp->cmd = NULL;
3664 clear_bit_unlock(index, &hba->lrb_in_use);
3665 /* Do not touch lrbp after scsi done */
3666 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003667 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003668 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3669 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003670 if (hba->dev_cmd.complete)
3671 complete(hba->dev_cmd.complete);
3672 }
3673 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303674
3675 /* clear corresponding bits of completed commands */
3676 hba->outstanding_reqs ^= completed_reqs;
3677
Sahitya Tummala856b3482014-09-25 15:32:34 +03003678 ufshcd_clk_scaling_update_busy(hba);
3679
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303680 /* we might have free'd some tags above */
3681 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303682}
3683
3684/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003685 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3686 * @hba: per adapter instance
3687 */
3688static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3689{
3690 unsigned long completed_reqs;
3691 u32 tr_doorbell;
3692
3693 /* Resetting interrupt aggregation counters first and reading the
3694 * DOOR_BELL afterward allows us to handle all the completed requests.
3695 * In order to prevent other interrupts starvation the DB is read once
3696 * after reset. The down side of this solution is the possibility of
3697 * false interrupt if device completes another request after resetting
3698 * aggregation and before reading the DB.
3699 */
3700 if (ufshcd_is_intr_aggr_allowed(hba))
3701 ufshcd_reset_intr_aggr(hba);
3702
3703 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3704 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3705
3706 __ufshcd_transfer_req_compl(hba, completed_reqs);
3707}
3708
3709/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303710 * ufshcd_disable_ee - disable exception event
3711 * @hba: per-adapter instance
3712 * @mask: exception event to disable
3713 *
3714 * Disables exception event in the device so that the EVENT_ALERT
3715 * bit is not set.
3716 *
3717 * Returns zero on success, non-zero error value on failure.
3718 */
3719static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3720{
3721 int err = 0;
3722 u32 val;
3723
3724 if (!(hba->ee_ctrl_mask & mask))
3725 goto out;
3726
3727 val = hba->ee_ctrl_mask & ~mask;
3728 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003729 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303730 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3731 if (!err)
3732 hba->ee_ctrl_mask &= ~mask;
3733out:
3734 return err;
3735}
3736
3737/**
3738 * ufshcd_enable_ee - enable exception event
3739 * @hba: per-adapter instance
3740 * @mask: exception event to enable
3741 *
3742 * Enable corresponding exception event in the device to allow
3743 * device to alert host in critical scenarios.
3744 *
3745 * Returns zero on success, non-zero error value on failure.
3746 */
3747static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3748{
3749 int err = 0;
3750 u32 val;
3751
3752 if (hba->ee_ctrl_mask & mask)
3753 goto out;
3754
3755 val = hba->ee_ctrl_mask | mask;
3756 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003757 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303758 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3759 if (!err)
3760 hba->ee_ctrl_mask |= mask;
3761out:
3762 return err;
3763}
3764
3765/**
3766 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3767 * @hba: per-adapter instance
3768 *
3769 * Allow device to manage background operations on its own. Enabling
3770 * this might lead to inconsistent latencies during normal data transfers
3771 * as the device is allowed to manage its own way of handling background
3772 * operations.
3773 *
3774 * Returns zero on success, non-zero on failure.
3775 */
3776static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3777{
3778 int err = 0;
3779
3780 if (hba->auto_bkops_enabled)
3781 goto out;
3782
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003783 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303784 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3785 if (err) {
3786 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3787 __func__, err);
3788 goto out;
3789 }
3790
3791 hba->auto_bkops_enabled = true;
3792
3793 /* No need of URGENT_BKOPS exception from the device */
3794 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3795 if (err)
3796 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3797 __func__, err);
3798out:
3799 return err;
3800}
3801
3802/**
3803 * ufshcd_disable_auto_bkops - block device in doing background operations
3804 * @hba: per-adapter instance
3805 *
3806 * Disabling background operations improves command response latency but
3807 * has drawback of device moving into critical state where the device is
3808 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3809 * host is idle so that BKOPS are managed effectively without any negative
3810 * impacts.
3811 *
3812 * Returns zero on success, non-zero on failure.
3813 */
3814static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3815{
3816 int err = 0;
3817
3818 if (!hba->auto_bkops_enabled)
3819 goto out;
3820
3821 /*
3822 * If host assisted BKOPs is to be enabled, make sure
3823 * urgent bkops exception is allowed.
3824 */
3825 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3826 if (err) {
3827 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3828 __func__, err);
3829 goto out;
3830 }
3831
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003832 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303833 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3834 if (err) {
3835 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3836 __func__, err);
3837 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3838 goto out;
3839 }
3840
3841 hba->auto_bkops_enabled = false;
3842out:
3843 return err;
3844}
3845
3846/**
3847 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3848 * @hba: per adapter instance
3849 *
3850 * After a device reset the device may toggle the BKOPS_EN flag
3851 * to default value. The s/w tracking variables should be updated
3852 * as well. Do this by forcing enable of auto bkops.
3853 */
3854static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3855{
3856 hba->auto_bkops_enabled = false;
3857 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3858 ufshcd_enable_auto_bkops(hba);
3859}
3860
3861static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3862{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003863 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303864 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3865}
3866
3867/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003868 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3869 * @hba: per-adapter instance
3870 * @status: bkops_status value
3871 *
3872 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3873 * flag in the device to permit background operations if the device
3874 * bkops_status is greater than or equal to "status" argument passed to
3875 * this function, disable otherwise.
3876 *
3877 * Returns 0 for success, non-zero in case of failure.
3878 *
3879 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3880 * to know whether auto bkops is enabled or disabled after this function
3881 * returns control to it.
3882 */
3883static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3884 enum bkops_status status)
3885{
3886 int err;
3887 u32 curr_status = 0;
3888
3889 err = ufshcd_get_bkops_status(hba, &curr_status);
3890 if (err) {
3891 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3892 __func__, err);
3893 goto out;
3894 } else if (curr_status > BKOPS_STATUS_MAX) {
3895 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3896 __func__, curr_status);
3897 err = -EINVAL;
3898 goto out;
3899 }
3900
3901 if (curr_status >= status)
3902 err = ufshcd_enable_auto_bkops(hba);
3903 else
3904 err = ufshcd_disable_auto_bkops(hba);
3905out:
3906 return err;
3907}
3908
3909/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303910 * ufshcd_urgent_bkops - handle urgent bkops exception event
3911 * @hba: per-adapter instance
3912 *
3913 * Enable fBackgroundOpsEn flag in the device to permit background
3914 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003915 *
3916 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3917 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303918 */
3919static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3920{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003921 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303922}
3923
3924static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3925{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003926 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303927 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3928}
3929
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003930static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3931{
3932 int err;
3933 u32 curr_status = 0;
3934
3935 if (hba->is_urgent_bkops_lvl_checked)
3936 goto enable_auto_bkops;
3937
3938 err = ufshcd_get_bkops_status(hba, &curr_status);
3939 if (err) {
3940 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3941 __func__, err);
3942 goto out;
3943 }
3944
3945 /*
3946 * We are seeing that some devices are raising the urgent bkops
3947 * exception events even when BKOPS status doesn't indicate performace
3948 * impacted or critical. Handle these device by determining their urgent
3949 * bkops status at runtime.
3950 */
3951 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
3952 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
3953 __func__, curr_status);
3954 /* update the current status as the urgent bkops level */
3955 hba->urgent_bkops_lvl = curr_status;
3956 hba->is_urgent_bkops_lvl_checked = true;
3957 }
3958
3959enable_auto_bkops:
3960 err = ufshcd_enable_auto_bkops(hba);
3961out:
3962 if (err < 0)
3963 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3964 __func__, err);
3965}
3966
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303967/**
3968 * ufshcd_exception_event_handler - handle exceptions raised by device
3969 * @work: pointer to work data
3970 *
3971 * Read bExceptionEventStatus attribute from the device and handle the
3972 * exception event accordingly.
3973 */
3974static void ufshcd_exception_event_handler(struct work_struct *work)
3975{
3976 struct ufs_hba *hba;
3977 int err;
3978 u32 status = 0;
3979 hba = container_of(work, struct ufs_hba, eeh_work);
3980
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303981 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303982 err = ufshcd_get_ee_status(hba, &status);
3983 if (err) {
3984 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3985 __func__, err);
3986 goto out;
3987 }
3988
3989 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003990
3991 if (status & MASK_EE_URGENT_BKOPS)
3992 ufshcd_bkops_exception_event_handler(hba);
3993
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303994out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303995 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303996 return;
3997}
3998
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003999/* Complete requests that have door-bell cleared */
4000static void ufshcd_complete_requests(struct ufs_hba *hba)
4001{
4002 ufshcd_transfer_req_compl(hba);
4003 ufshcd_tmc_handler(hba);
4004}
4005
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304006/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004007 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4008 * to recover from the DL NAC errors or not.
4009 * @hba: per-adapter instance
4010 *
4011 * Returns true if error handling is required, false otherwise
4012 */
4013static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4014{
4015 unsigned long flags;
4016 bool err_handling = true;
4017
4018 spin_lock_irqsave(hba->host->host_lock, flags);
4019 /*
4020 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4021 * device fatal error and/or DL NAC & REPLAY timeout errors.
4022 */
4023 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4024 goto out;
4025
4026 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4027 ((hba->saved_err & UIC_ERROR) &&
4028 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4029 goto out;
4030
4031 if ((hba->saved_err & UIC_ERROR) &&
4032 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4033 int err;
4034 /*
4035 * wait for 50ms to see if we can get any other errors or not.
4036 */
4037 spin_unlock_irqrestore(hba->host->host_lock, flags);
4038 msleep(50);
4039 spin_lock_irqsave(hba->host->host_lock, flags);
4040
4041 /*
4042 * now check if we have got any other severe errors other than
4043 * DL NAC error?
4044 */
4045 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4046 ((hba->saved_err & UIC_ERROR) &&
4047 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4048 goto out;
4049
4050 /*
4051 * As DL NAC is the only error received so far, send out NOP
4052 * command to confirm if link is still active or not.
4053 * - If we don't get any response then do error recovery.
4054 * - If we get response then clear the DL NAC error bit.
4055 */
4056
4057 spin_unlock_irqrestore(hba->host->host_lock, flags);
4058 err = ufshcd_verify_dev_init(hba);
4059 spin_lock_irqsave(hba->host->host_lock, flags);
4060
4061 if (err)
4062 goto out;
4063
4064 /* Link seems to be alive hence ignore the DL NAC errors */
4065 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4066 hba->saved_err &= ~UIC_ERROR;
4067 /* clear NAC error */
4068 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4069 if (!hba->saved_uic_err) {
4070 err_handling = false;
4071 goto out;
4072 }
4073 }
4074out:
4075 spin_unlock_irqrestore(hba->host->host_lock, flags);
4076 return err_handling;
4077}
4078
4079/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304080 * ufshcd_err_handler - handle UFS errors that require s/w attention
4081 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304082 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304083static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304084{
4085 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304086 unsigned long flags;
4087 u32 err_xfer = 0;
4088 u32 err_tm = 0;
4089 int err = 0;
4090 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004091 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304092
4093 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304094
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304095 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004096 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304097
4098 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004099 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304100 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304101
4102 hba->ufshcd_state = UFSHCD_STATE_RESET;
4103 ufshcd_set_eh_in_progress(hba);
4104
4105 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004106 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004107
4108 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4109 bool ret;
4110
4111 spin_unlock_irqrestore(hba->host->host_lock, flags);
4112 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4113 ret = ufshcd_quirk_dl_nac_errors(hba);
4114 spin_lock_irqsave(hba->host->host_lock, flags);
4115 if (!ret)
4116 goto skip_err_handling;
4117 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004118 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4119 ((hba->saved_err & UIC_ERROR) &&
4120 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4121 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4122 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4123 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304124
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004125 /*
4126 * if host reset is required then skip clearing the pending
4127 * transfers forcefully because they will automatically get
4128 * cleared after link startup.
4129 */
4130 if (needs_reset)
4131 goto skip_pending_xfer_clear;
4132
4133 /* release lock as clear command might sleep */
4134 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304135 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004136 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4137 if (ufshcd_clear_cmd(hba, tag)) {
4138 err_xfer = true;
4139 goto lock_skip_pending_xfer_clear;
4140 }
4141 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304142
4143 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004144 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4145 if (ufshcd_clear_tm_cmd(hba, tag)) {
4146 err_tm = true;
4147 goto lock_skip_pending_xfer_clear;
4148 }
4149 }
4150
4151lock_skip_pending_xfer_clear:
4152 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304153
4154 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004155 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304156
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004157 if (err_xfer || err_tm)
4158 needs_reset = true;
4159
4160skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304161 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004162 if (needs_reset) {
4163 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4164
4165 /*
4166 * ufshcd_reset_and_restore() does the link reinitialization
4167 * which will need atleast one empty doorbell slot to send the
4168 * device management commands (NOP and query commands).
4169 * If there is no slot empty at this moment then free up last
4170 * slot forcefully.
4171 */
4172 if (hba->outstanding_reqs == max_doorbells)
4173 __ufshcd_transfer_req_compl(hba,
4174 (1UL << (hba->nutrs - 1)));
4175
4176 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304177 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004178 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304179 if (err) {
4180 dev_err(hba->dev, "%s: reset and restore failed\n",
4181 __func__);
4182 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4183 }
4184 /*
4185 * Inform scsi mid-layer that we did reset and allow to handle
4186 * Unit Attention properly.
4187 */
4188 scsi_report_bus_reset(hba->host, 0);
4189 hba->saved_err = 0;
4190 hba->saved_uic_err = 0;
4191 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004192
Yaniv Gardi583fa622016-03-10 17:37:13 +02004193skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004194 if (!needs_reset) {
4195 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4196 if (hba->saved_err || hba->saved_uic_err)
4197 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4198 __func__, hba->saved_err, hba->saved_uic_err);
4199 }
4200
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304201 ufshcd_clear_eh_in_progress(hba);
4202
4203out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004204 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304205 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004206 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304207 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304208}
4209
4210/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304211 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4212 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304213 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304214static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304215{
4216 u32 reg;
4217
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304218 /* PA_INIT_ERROR is fatal and needs UIC reset */
4219 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4220 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4221 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004222 else if (hba->dev_quirks &
4223 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4224 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4225 hba->uic_error |=
4226 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4227 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4228 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4229 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304230
4231 /* UIC NL/TL/DME errors needs software retry */
4232 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4233 if (reg)
4234 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4235
4236 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4237 if (reg)
4238 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4239
4240 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4241 if (reg)
4242 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4243
4244 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4245 __func__, hba->uic_error);
4246}
4247
4248/**
4249 * ufshcd_check_errors - Check for errors that need s/w attention
4250 * @hba: per-adapter instance
4251 */
4252static void ufshcd_check_errors(struct ufs_hba *hba)
4253{
4254 bool queue_eh_work = false;
4255
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304256 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304257 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304258
4259 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304260 hba->uic_error = 0;
4261 ufshcd_update_uic_error(hba);
4262 if (hba->uic_error)
4263 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304264 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304265
4266 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004267 /*
4268 * update the transfer error masks to sticky bits, let's do this
4269 * irrespective of current ufshcd_state.
4270 */
4271 hba->saved_err |= hba->errors;
4272 hba->saved_uic_err |= hba->uic_error;
4273
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304274 /* handle fatal errors only when link is functional */
4275 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4276 /* block commands from scsi mid-layer */
4277 scsi_block_requests(hba->host);
4278
Zang Leigang141f8162016-11-16 11:29:37 +08004279 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304280 schedule_work(&hba->eh_work);
4281 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304282 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304283 /*
4284 * if (!queue_eh_work) -
4285 * Other errors are either non-fatal where host recovers
4286 * itself without s/w intervention or errors that will be
4287 * handled by the SCSI core layer.
4288 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304289}
4290
4291/**
4292 * ufshcd_tmc_handler - handle task management function completion
4293 * @hba: per adapter instance
4294 */
4295static void ufshcd_tmc_handler(struct ufs_hba *hba)
4296{
4297 u32 tm_doorbell;
4298
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304299 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304300 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304301 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304302}
4303
4304/**
4305 * ufshcd_sl_intr - Interrupt service routine
4306 * @hba: per adapter instance
4307 * @intr_status: contains interrupts generated by the controller
4308 */
4309static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4310{
4311 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4312 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304313 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304314
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304315 if (intr_status & UFSHCD_UIC_MASK)
4316 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304317
4318 if (intr_status & UTP_TASK_REQ_COMPL)
4319 ufshcd_tmc_handler(hba);
4320
4321 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4322 ufshcd_transfer_req_compl(hba);
4323}
4324
4325/**
4326 * ufshcd_intr - Main interrupt service routine
4327 * @irq: irq number
4328 * @__hba: pointer to adapter instance
4329 *
4330 * Returns IRQ_HANDLED - If interrupt is valid
4331 * IRQ_NONE - If invalid interrupt
4332 */
4333static irqreturn_t ufshcd_intr(int irq, void *__hba)
4334{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004335 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304336 irqreturn_t retval = IRQ_NONE;
4337 struct ufs_hba *hba = __hba;
4338
4339 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304340 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004341 enabled_intr_status =
4342 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304343
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004344 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304345 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004346
4347 if (enabled_intr_status) {
4348 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304349 retval = IRQ_HANDLED;
4350 }
4351 spin_unlock(hba->host->host_lock);
4352 return retval;
4353}
4354
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304355static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4356{
4357 int err = 0;
4358 u32 mask = 1 << tag;
4359 unsigned long flags;
4360
4361 if (!test_bit(tag, &hba->outstanding_tasks))
4362 goto out;
4363
4364 spin_lock_irqsave(hba->host->host_lock, flags);
4365 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4366 spin_unlock_irqrestore(hba->host->host_lock, flags);
4367
4368 /* poll for max. 1 sec to clear door bell register by h/w */
4369 err = ufshcd_wait_for_register(hba,
4370 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004371 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304372out:
4373 return err;
4374}
4375
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304376/**
4377 * ufshcd_issue_tm_cmd - issues task management commands to controller
4378 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304379 * @lun_id: LUN ID to which TM command is sent
4380 * @task_id: task ID to which the TM command is applicable
4381 * @tm_function: task management function opcode
4382 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304383 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304384 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304385 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304386static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4387 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304388{
4389 struct utp_task_req_desc *task_req_descp;
4390 struct utp_upiu_task_req *task_req_upiup;
4391 struct Scsi_Host *host;
4392 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304393 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304394 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304395 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304396
4397 host = hba->host;
4398
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304399 /*
4400 * Get free slot, sleep if slots are unavailable.
4401 * Even though we use wait_event() which sleeps indefinitely,
4402 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4403 */
4404 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004405 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304406
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304407 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304408 task_req_descp = hba->utmrdl_base_addr;
4409 task_req_descp += free_slot;
4410
4411 /* Configure task request descriptor */
4412 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4413 task_req_descp->header.dword_2 =
4414 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4415
4416 /* Configure task request UPIU */
4417 task_req_upiup =
4418 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304419 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304420 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304421 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304422 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304423 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304424 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004425 /*
4426 * The host shall provide the same value for LUN field in the basic
4427 * header and for Input Parameter.
4428 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304429 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4430 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304431
Kiwoong Kimd2877be2016-11-10 21:16:15 +09004432 ufshcd_vops_setup_task_mgmt(hba, free_slot, tm_function);
4433
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304434 /* send command to the controller */
4435 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004436
4437 /* Make sure descriptors are ready before ringing the task doorbell */
4438 wmb();
4439
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304440 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004441 /* Make sure that doorbell is committed immediately */
4442 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304443
4444 spin_unlock_irqrestore(host->host_lock, flags);
4445
4446 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304447 err = wait_event_timeout(hba->tm_wq,
4448 test_bit(free_slot, &hba->tm_condition),
4449 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304450 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304451 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4452 __func__, tm_function);
4453 if (ufshcd_clear_tm_cmd(hba, free_slot))
4454 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4455 __func__, free_slot);
4456 err = -ETIMEDOUT;
4457 } else {
4458 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304459 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304460
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304461 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304462 ufshcd_put_tm_slot(hba, free_slot);
4463 wake_up(&hba->tm_tag_wq);
4464
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004465 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304466 return err;
4467}
4468
4469/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304470 * ufshcd_eh_device_reset_handler - device reset handler registered to
4471 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304472 * @cmd: SCSI command pointer
4473 *
4474 * Returns SUCCESS/FAILED
4475 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304476static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304477{
4478 struct Scsi_Host *host;
4479 struct ufs_hba *hba;
4480 unsigned int tag;
4481 u32 pos;
4482 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304483 u8 resp = 0xF;
4484 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304485 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304486
4487 host = cmd->device->host;
4488 hba = shost_priv(host);
4489 tag = cmd->request->tag;
4490
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304491 lrbp = &hba->lrb[tag];
4492 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4493 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304494 if (!err)
4495 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304496 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304497 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304498
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304499 /* clear the commands that were pending for corresponding LUN */
4500 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4501 if (hba->lrb[pos].lun == lrbp->lun) {
4502 err = ufshcd_clear_cmd(hba, pos);
4503 if (err)
4504 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304505 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304506 }
4507 spin_lock_irqsave(host->host_lock, flags);
4508 ufshcd_transfer_req_compl(hba);
4509 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304510out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304511 if (!err) {
4512 err = SUCCESS;
4513 } else {
4514 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4515 err = FAILED;
4516 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304517 return err;
4518}
4519
4520/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304521 * ufshcd_abort - abort a specific command
4522 * @cmd: SCSI command pointer
4523 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304524 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4525 * command, and in host controller by clearing the door-bell register. There can
4526 * be race between controller sending the command to the device while abort is
4527 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4528 * really issued and then try to abort it.
4529 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304530 * Returns SUCCESS/FAILED
4531 */
4532static int ufshcd_abort(struct scsi_cmnd *cmd)
4533{
4534 struct Scsi_Host *host;
4535 struct ufs_hba *hba;
4536 unsigned long flags;
4537 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304538 int err = 0;
4539 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304540 u8 resp = 0xF;
4541 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004542 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304543
4544 host = cmd->device->host;
4545 hba = shost_priv(host);
4546 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004547 if (!ufshcd_valid_tag(hba, tag)) {
4548 dev_err(hba->dev,
4549 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4550 __func__, tag, cmd, cmd->request);
4551 BUG();
4552 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304553
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004554 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004555 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004556 /* If command is already aborted/completed, return SUCCESS */
4557 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4558 dev_err(hba->dev,
4559 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4560 __func__, tag, hba->outstanding_reqs, reg);
4561 goto out;
4562 }
4563
Dolev Ravive9d501b2014-07-01 12:22:37 +03004564 if (!(reg & (1 << tag))) {
4565 dev_err(hba->dev,
4566 "%s: cmd was completed, but without a notifying intr, tag = %d",
4567 __func__, tag);
4568 }
4569
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304570 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304571 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4572 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4573 UFS_QUERY_TASK, &resp);
4574 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4575 /* cmd pending in the device */
4576 break;
4577 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304578 /*
4579 * cmd not pending in the device, check if it is
4580 * in transition.
4581 */
4582 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4583 if (reg & (1 << tag)) {
4584 /* sleep for max. 200us to stabilize */
4585 usleep_range(100, 200);
4586 continue;
4587 }
4588 /* command completed already */
4589 goto out;
4590 } else {
4591 if (!err)
4592 err = resp; /* service response error */
4593 goto out;
4594 }
4595 }
4596
4597 if (!poll_cnt) {
4598 err = -EBUSY;
4599 goto out;
4600 }
4601
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304602 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4603 UFS_ABORT_TASK, &resp);
4604 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304605 if (!err)
4606 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304607 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304608 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304609
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304610 err = ufshcd_clear_cmd(hba, tag);
4611 if (err)
4612 goto out;
4613
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304614 scsi_dma_unmap(cmd);
4615
4616 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004617 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304618 hba->lrb[tag].cmd = NULL;
4619 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304620
4621 clear_bit_unlock(tag, &hba->lrb_in_use);
4622 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004623
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304624out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304625 if (!err) {
4626 err = SUCCESS;
4627 } else {
4628 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4629 err = FAILED;
4630 }
4631
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004632 /*
4633 * This ufshcd_release() corresponds to the original scsi cmd that got
4634 * aborted here (as we won't get any IRQ for it).
4635 */
4636 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304637 return err;
4638}
4639
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304640/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304641 * ufshcd_host_reset_and_restore - reset and restore host controller
4642 * @hba: per-adapter instance
4643 *
4644 * Note that host controller reset may issue DME_RESET to
4645 * local and remote (device) Uni-Pro stack and the attributes
4646 * are reset to default state.
4647 *
4648 * Returns zero on success, non-zero on failure
4649 */
4650static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4651{
4652 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304653 unsigned long flags;
4654
4655 /* Reset the host controller */
4656 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004657 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304658 spin_unlock_irqrestore(hba->host->host_lock, flags);
4659
4660 err = ufshcd_hba_enable(hba);
4661 if (err)
4662 goto out;
4663
4664 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004665 err = ufshcd_probe_hba(hba);
4666
4667 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304668 err = -EIO;
4669out:
4670 if (err)
4671 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4672
4673 return err;
4674}
4675
4676/**
4677 * ufshcd_reset_and_restore - reset and re-initialize host/device
4678 * @hba: per-adapter instance
4679 *
4680 * Reset and recover device, host and re-establish link. This
4681 * is helpful to recover the communication in fatal error conditions.
4682 *
4683 * Returns zero on success, non-zero on failure
4684 */
4685static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4686{
4687 int err = 0;
4688 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004689 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304690
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004691 do {
4692 err = ufshcd_host_reset_and_restore(hba);
4693 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304694
4695 /*
4696 * After reset the door-bell might be cleared, complete
4697 * outstanding requests in s/w here.
4698 */
4699 spin_lock_irqsave(hba->host->host_lock, flags);
4700 ufshcd_transfer_req_compl(hba);
4701 ufshcd_tmc_handler(hba);
4702 spin_unlock_irqrestore(hba->host->host_lock, flags);
4703
4704 return err;
4705}
4706
4707/**
4708 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4709 * @cmd - SCSI command pointer
4710 *
4711 * Returns SUCCESS/FAILED
4712 */
4713static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4714{
4715 int err;
4716 unsigned long flags;
4717 struct ufs_hba *hba;
4718
4719 hba = shost_priv(cmd->device->host);
4720
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004721 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304722 /*
4723 * Check if there is any race with fatal error handling.
4724 * If so, wait for it to complete. Even though fatal error
4725 * handling does reset and restore in some cases, don't assume
4726 * anything out of it. We are just avoiding race here.
4727 */
4728 do {
4729 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304730 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304731 hba->ufshcd_state == UFSHCD_STATE_RESET))
4732 break;
4733 spin_unlock_irqrestore(hba->host->host_lock, flags);
4734 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304735 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304736 } while (1);
4737
4738 hba->ufshcd_state = UFSHCD_STATE_RESET;
4739 ufshcd_set_eh_in_progress(hba);
4740 spin_unlock_irqrestore(hba->host->host_lock, flags);
4741
4742 err = ufshcd_reset_and_restore(hba);
4743
4744 spin_lock_irqsave(hba->host->host_lock, flags);
4745 if (!err) {
4746 err = SUCCESS;
4747 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4748 } else {
4749 err = FAILED;
4750 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4751 }
4752 ufshcd_clear_eh_in_progress(hba);
4753 spin_unlock_irqrestore(hba->host->host_lock, flags);
4754
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004755 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304756 return err;
4757}
4758
4759/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004760 * ufshcd_get_max_icc_level - calculate the ICC level
4761 * @sup_curr_uA: max. current supported by the regulator
4762 * @start_scan: row at the desc table to start scan from
4763 * @buff: power descriptor buffer
4764 *
4765 * Returns calculated max ICC level for specific regulator
4766 */
4767static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4768{
4769 int i;
4770 int curr_uA;
4771 u16 data;
4772 u16 unit;
4773
4774 for (i = start_scan; i >= 0; i--) {
4775 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4776 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4777 ATTR_ICC_LVL_UNIT_OFFSET;
4778 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4779 switch (unit) {
4780 case UFSHCD_NANO_AMP:
4781 curr_uA = curr_uA / 1000;
4782 break;
4783 case UFSHCD_MILI_AMP:
4784 curr_uA = curr_uA * 1000;
4785 break;
4786 case UFSHCD_AMP:
4787 curr_uA = curr_uA * 1000 * 1000;
4788 break;
4789 case UFSHCD_MICRO_AMP:
4790 default:
4791 break;
4792 }
4793 if (sup_curr_uA >= curr_uA)
4794 break;
4795 }
4796 if (i < 0) {
4797 i = 0;
4798 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4799 }
4800
4801 return (u32)i;
4802}
4803
4804/**
4805 * ufshcd_calc_icc_level - calculate the max ICC level
4806 * In case regulators are not initialized we'll return 0
4807 * @hba: per-adapter instance
4808 * @desc_buf: power descriptor buffer to extract ICC levels from.
4809 * @len: length of desc_buff
4810 *
4811 * Returns calculated ICC level
4812 */
4813static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4814 u8 *desc_buf, int len)
4815{
4816 u32 icc_level = 0;
4817
4818 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4819 !hba->vreg_info.vccq2) {
4820 dev_err(hba->dev,
4821 "%s: Regulator capability was not set, actvIccLevel=%d",
4822 __func__, icc_level);
4823 goto out;
4824 }
4825
4826 if (hba->vreg_info.vcc)
4827 icc_level = ufshcd_get_max_icc_level(
4828 hba->vreg_info.vcc->max_uA,
4829 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4830 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4831
4832 if (hba->vreg_info.vccq)
4833 icc_level = ufshcd_get_max_icc_level(
4834 hba->vreg_info.vccq->max_uA,
4835 icc_level,
4836 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4837
4838 if (hba->vreg_info.vccq2)
4839 icc_level = ufshcd_get_max_icc_level(
4840 hba->vreg_info.vccq2->max_uA,
4841 icc_level,
4842 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4843out:
4844 return icc_level;
4845}
4846
Dolev Raviv61e07352016-11-23 16:30:49 -08004847static int ufshcd_set_icc_levels_attr(struct ufs_hba *hba, u32 icc_level)
4848{
4849 int ret = 0;
4850 int retries;
4851
4852 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
4853 /* write attribute */
4854 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4855 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0, &icc_level);
4856 if (!ret)
4857 break;
4858
4859 dev_dbg(hba->dev, "%s: failed with error %d\n", __func__, ret);
4860 }
4861
4862 return ret;
4863}
4864
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004865static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4866{
4867 int ret;
4868 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4869 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4870
4871 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4872 if (ret) {
4873 dev_err(hba->dev,
4874 "%s: Failed reading power descriptor.len = %d ret = %d",
4875 __func__, buff_len, ret);
4876 return;
4877 }
4878
4879 hba->init_prefetch_data.icc_level =
4880 ufshcd_find_max_sup_active_icc_level(hba,
4881 desc_buf, buff_len);
4882 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4883 __func__, hba->init_prefetch_data.icc_level);
4884
Dolev Raviv61e07352016-11-23 16:30:49 -08004885 ret = ufshcd_set_icc_levels_attr(hba,
4886 hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004887
4888 if (ret)
4889 dev_err(hba->dev,
4890 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4891 __func__, hba->init_prefetch_data.icc_level , ret);
4892
4893}
4894
4895/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004896 * ufshcd_scsi_add_wlus - Adds required W-LUs
4897 * @hba: per-adapter instance
4898 *
4899 * UFS device specification requires the UFS devices to support 4 well known
4900 * logical units:
4901 * "REPORT_LUNS" (address: 01h)
4902 * "UFS Device" (address: 50h)
4903 * "RPMB" (address: 44h)
4904 * "BOOT" (address: 30h)
4905 * UFS device's power management needs to be controlled by "POWER CONDITION"
4906 * field of SSU (START STOP UNIT) command. But this "power condition" field
4907 * will take effect only when its sent to "UFS device" well known logical unit
4908 * hence we require the scsi_device instance to represent this logical unit in
4909 * order for the UFS host driver to send the SSU command for power management.
4910
4911 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4912 * Block) LU so user space process can control this LU. User space may also
4913 * want to have access to BOOT LU.
4914
4915 * This function adds scsi device instances for each of all well known LUs
4916 * (except "REPORT LUNS" LU).
4917 *
4918 * Returns zero on success (all required W-LUs are added successfully),
4919 * non-zero error value on failure (if failed to add any of the required W-LU).
4920 */
4921static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4922{
4923 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004924 struct scsi_device *sdev_rpmb;
4925 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004926
4927 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4928 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4929 if (IS_ERR(hba->sdev_ufs_device)) {
4930 ret = PTR_ERR(hba->sdev_ufs_device);
4931 hba->sdev_ufs_device = NULL;
4932 goto out;
4933 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004934 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004935
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004936 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004937 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004938 if (IS_ERR(sdev_boot)) {
4939 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004940 goto remove_sdev_ufs_device;
4941 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004942 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004943
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004944 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004945 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004946 if (IS_ERR(sdev_rpmb)) {
4947 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004948 goto remove_sdev_boot;
4949 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004950 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004951 goto out;
4952
4953remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004954 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004955remove_sdev_ufs_device:
4956 scsi_remove_device(hba->sdev_ufs_device);
4957out:
4958 return ret;
4959}
4960
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004961static int ufs_get_device_info(struct ufs_hba *hba,
4962 struct ufs_device_info *card_data)
4963{
4964 int err;
4965 u8 model_index;
4966 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
4967 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
4968
4969 err = ufshcd_read_device_desc(hba, desc_buf,
4970 QUERY_DESC_DEVICE_MAX_SIZE);
4971 if (err) {
4972 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4973 __func__, err);
4974 goto out;
4975 }
4976
4977 /*
4978 * getting vendor (manufacturerID) and Bank Index in big endian
4979 * format
4980 */
4981 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
4982 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4983
4984 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4985
4986 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
4987 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
4988 if (err) {
4989 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
4990 __func__, err);
4991 goto out;
4992 }
4993
4994 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
4995 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
4996 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
4997 MAX_MODEL_LEN));
4998
4999 /* Null terminate the model string */
5000 card_data->model[MAX_MODEL_LEN] = '\0';
5001
5002out:
5003 return err;
5004}
5005
5006void ufs_advertise_fixup_device(struct ufs_hba *hba)
5007{
5008 int err;
5009 struct ufs_dev_fix *f;
5010 struct ufs_device_info card_data;
5011
5012 card_data.wmanufacturerid = 0;
5013
5014 err = ufs_get_device_info(hba, &card_data);
5015 if (err) {
5016 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5017 __func__, err);
5018 return;
5019 }
5020
5021 for (f = ufs_fixups; f->quirk; f++) {
5022 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
5023 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
5024 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
5025 !strcmp(f->card.model, UFS_ANY_MODEL)))
5026 hba->dev_quirks |= f->quirk;
5027 }
5028}
5029
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005030/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005031 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5032 * @hba: per-adapter instance
5033 *
5034 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5035 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5036 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5037 * the hibern8 exit latency.
5038 *
5039 * Returns zero on success, non-zero error value on failure.
5040 */
5041static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5042{
5043 int ret = 0;
5044 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5045
5046 ret = ufshcd_dme_peer_get(hba,
5047 UIC_ARG_MIB_SEL(
5048 RX_MIN_ACTIVATETIME_CAPABILITY,
5049 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5050 &peer_rx_min_activatetime);
5051 if (ret)
5052 goto out;
5053
5054 /* make sure proper unit conversion is applied */
5055 tuned_pa_tactivate =
5056 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5057 / PA_TACTIVATE_TIME_UNIT_US);
5058 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5059 tuned_pa_tactivate);
5060
5061out:
5062 return ret;
5063}
5064
5065/**
5066 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5067 * @hba: per-adapter instance
5068 *
5069 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5070 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5071 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5072 * This optimal value can help reduce the hibern8 exit latency.
5073 *
5074 * Returns zero on success, non-zero error value on failure.
5075 */
5076static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5077{
5078 int ret = 0;
5079 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5080 u32 max_hibern8_time, tuned_pa_hibern8time;
5081
5082 ret = ufshcd_dme_get(hba,
5083 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5084 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5085 &local_tx_hibern8_time_cap);
5086 if (ret)
5087 goto out;
5088
5089 ret = ufshcd_dme_peer_get(hba,
5090 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5091 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5092 &peer_rx_hibern8_time_cap);
5093 if (ret)
5094 goto out;
5095
5096 max_hibern8_time = max(local_tx_hibern8_time_cap,
5097 peer_rx_hibern8_time_cap);
5098 /* make sure proper unit conversion is applied */
5099 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5100 / PA_HIBERN8_TIME_UNIT_US);
5101 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5102 tuned_pa_hibern8time);
5103out:
5104 return ret;
5105}
5106
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005107/**
5108 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5109 * less than device PA_TACTIVATE time.
5110 * @hba: per-adapter instance
5111 *
5112 * Some UFS devices require host PA_TACTIVATE to be lower than device
5113 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5114 * for such devices.
5115 *
5116 * Returns zero on success, non-zero error value on failure.
5117 */
5118static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5119{
5120 int ret = 0;
5121 u32 granularity, peer_granularity;
5122 u32 pa_tactivate, peer_pa_tactivate;
5123 u32 pa_tactivate_us, peer_pa_tactivate_us;
5124 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5125
5126 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5127 &granularity);
5128 if (ret)
5129 goto out;
5130
5131 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5132 &peer_granularity);
5133 if (ret)
5134 goto out;
5135
5136 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5137 (granularity > PA_GRANULARITY_MAX_VAL)) {
5138 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5139 __func__, granularity);
5140 return -EINVAL;
5141 }
5142
5143 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5144 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5145 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5146 __func__, peer_granularity);
5147 return -EINVAL;
5148 }
5149
5150 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5151 if (ret)
5152 goto out;
5153
5154 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5155 &peer_pa_tactivate);
5156 if (ret)
5157 goto out;
5158
5159 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5160 peer_pa_tactivate_us = peer_pa_tactivate *
5161 gran_to_us_table[peer_granularity - 1];
5162
5163 if (pa_tactivate_us > peer_pa_tactivate_us) {
5164 u32 new_peer_pa_tactivate;
5165
5166 new_peer_pa_tactivate = pa_tactivate_us /
5167 gran_to_us_table[peer_granularity - 1];
5168 new_peer_pa_tactivate++;
5169 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5170 new_peer_pa_tactivate);
5171 }
5172
5173out:
5174 return ret;
5175}
5176
Yaniv Gardi37113102016-03-10 17:37:16 +02005177static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5178{
5179 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5180 ufshcd_tune_pa_tactivate(hba);
5181 ufshcd_tune_pa_hibern8time(hba);
5182 }
5183
5184 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5185 /* set 1ms timeout for PA_TACTIVATE */
5186 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
subhashj@codeaurora.orgc6a6db42016-11-23 16:32:08 -08005187
5188 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5189 ufshcd_quirk_tune_host_pa_tactivate(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005190}
5191
5192/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005193 * ufshcd_probe_hba - probe hba to detect device and initialize
5194 * @hba: per-adapter instance
5195 *
5196 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305197 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005198static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305199{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305200 int ret;
5201
5202 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305203 if (ret)
5204 goto out;
5205
Yaniv Gardi50646362014-10-23 13:25:13 +03005206 ufshcd_init_pwr_info(hba);
5207
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005208 /* set the default level for urgent bkops */
5209 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5210 hba->is_urgent_bkops_lvl_checked = false;
5211
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005212 /* UniPro link is active now */
5213 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305214
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305215 ret = ufshcd_verify_dev_init(hba);
5216 if (ret)
5217 goto out;
5218
Dolev Raviv68078d52013-07-30 00:35:58 +05305219 ret = ufshcd_complete_dev_init(hba);
5220 if (ret)
5221 goto out;
5222
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005223 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005224 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005225
5226 ret = ufshcd_set_vccq_rail_unused(hba,
5227 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5228 if (ret)
5229 goto out;
5230
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005231 /* UFS device is also active now */
5232 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305233 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005234 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305235
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005236 if (ufshcd_get_max_pwr_mode(hba)) {
5237 dev_err(hba->dev,
5238 "%s: Failed getting max supported power mode\n",
5239 __func__);
5240 } else {
5241 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005242 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005243 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5244 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005245 goto out;
5246 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005247 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005248
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005249 /* set the state as operational after switching to desired gear */
5250 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005251 /*
5252 * If we are in error handling context or in power management callbacks
5253 * context, no need to scan the host
5254 */
5255 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5256 bool flag;
5257
5258 /* clear any previous UFS device information */
5259 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005260 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5261 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005262 hba->dev_info.f_power_on_wp_en = flag;
5263
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005264 if (!hba->is_init_prefetch)
5265 ufshcd_init_icc_levels(hba);
5266
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005267 /* Add required well known logical units to scsi mid layer */
5268 if (ufshcd_scsi_add_wlus(hba))
5269 goto out;
5270
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305271 scsi_scan_host(hba->host);
5272 pm_runtime_put_sync(hba->dev);
5273 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005274
5275 if (!hba->is_init_prefetch)
5276 hba->is_init_prefetch = true;
5277
Sahitya Tummala856b3482014-09-25 15:32:34 +03005278 /* Resume devfreq after UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07005279 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005280
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305281out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005282 /*
5283 * If we failed to initialize the device or the device is not
5284 * present, turn off the power/clocks etc.
5285 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005286 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5287 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005288 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005289 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005290
5291 return ret;
5292}
5293
5294/**
5295 * ufshcd_async_scan - asynchronous execution for probing hba
5296 * @data: data pointer to pass to this function
5297 * @cookie: cookie data
5298 */
5299static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5300{
5301 struct ufs_hba *hba = (struct ufs_hba *)data;
5302
5303 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305304}
5305
Yaniv Gardif550c652016-03-10 17:37:07 +02005306static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5307{
5308 unsigned long flags;
5309 struct Scsi_Host *host;
5310 struct ufs_hba *hba;
5311 int index;
5312 bool found = false;
5313
5314 if (!scmd || !scmd->device || !scmd->device->host)
5315 return BLK_EH_NOT_HANDLED;
5316
5317 host = scmd->device->host;
5318 hba = shost_priv(host);
5319 if (!hba)
5320 return BLK_EH_NOT_HANDLED;
5321
5322 spin_lock_irqsave(host->host_lock, flags);
5323
5324 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5325 if (hba->lrb[index].cmd == scmd) {
5326 found = true;
5327 break;
5328 }
5329 }
5330
5331 spin_unlock_irqrestore(host->host_lock, flags);
5332
5333 /*
5334 * Bypass SCSI error handling and reset the block layer timer if this
5335 * SCSI command was not actually dispatched to UFS driver, otherwise
5336 * let SCSI layer handle the error as usual.
5337 */
5338 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5339}
5340
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305341static struct scsi_host_template ufshcd_driver_template = {
5342 .module = THIS_MODULE,
5343 .name = UFSHCD,
5344 .proc_name = UFSHCD,
5345 .queuecommand = ufshcd_queuecommand,
5346 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005347 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305348 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005349 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305350 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305351 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5352 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005353 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305354 .this_id = -1,
5355 .sg_tablesize = SG_ALL,
5356 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5357 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005358 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005359 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305360};
5361
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005362static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5363 int ua)
5364{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005365 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005366
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005367 if (!vreg)
5368 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005369
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005370 ret = regulator_set_load(vreg->reg, ua);
5371 if (ret < 0) {
5372 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5373 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005374 }
5375
5376 return ret;
5377}
5378
5379static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5380 struct ufs_vreg *vreg)
5381{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005382 if (!vreg)
5383 return 0;
5384 else if (vreg->unused)
5385 return 0;
5386 else
5387 return ufshcd_config_vreg_load(hba->dev, vreg,
5388 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005389}
5390
5391static inline int ufshcd_config_vreg_hpm(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, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005400}
5401
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005402static int ufshcd_config_vreg(struct device *dev,
5403 struct ufs_vreg *vreg, bool on)
5404{
5405 int ret = 0;
5406 struct regulator *reg = vreg->reg;
5407 const char *name = vreg->name;
5408 int min_uV, uA_load;
5409
5410 BUG_ON(!vreg);
5411
5412 if (regulator_count_voltages(reg) > 0) {
5413 min_uV = on ? vreg->min_uV : 0;
5414 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5415 if (ret) {
5416 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5417 __func__, name, ret);
5418 goto out;
5419 }
5420
5421 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005422 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5423 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005424 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005425 }
5426out:
5427 return ret;
5428}
5429
5430static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5431{
5432 int ret = 0;
5433
Yaniv Gardi60f01872016-03-10 17:37:11 +02005434 if (!vreg)
5435 goto out;
5436 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005437 goto out;
5438
5439 ret = ufshcd_config_vreg(dev, vreg, true);
5440 if (!ret)
5441 ret = regulator_enable(vreg->reg);
5442
5443 if (!ret)
5444 vreg->enabled = true;
5445 else
5446 dev_err(dev, "%s: %s enable failed, err=%d\n",
5447 __func__, vreg->name, ret);
5448out:
5449 return ret;
5450}
5451
5452static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5453{
5454 int ret = 0;
5455
Yaniv Gardi60f01872016-03-10 17:37:11 +02005456 if (!vreg)
5457 goto out;
5458 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005459 goto out;
5460
5461 ret = regulator_disable(vreg->reg);
5462
5463 if (!ret) {
5464 /* ignore errors on applying disable config */
5465 ufshcd_config_vreg(dev, vreg, false);
5466 vreg->enabled = false;
5467 } else {
5468 dev_err(dev, "%s: %s disable failed, err=%d\n",
5469 __func__, vreg->name, ret);
5470 }
5471out:
5472 return ret;
5473}
5474
5475static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5476{
5477 int ret = 0;
5478 struct device *dev = hba->dev;
5479 struct ufs_vreg_info *info = &hba->vreg_info;
5480
5481 if (!info)
5482 goto out;
5483
5484 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5485 if (ret)
5486 goto out;
5487
5488 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5489 if (ret)
5490 goto out;
5491
5492 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5493 if (ret)
5494 goto out;
5495
5496out:
5497 if (ret) {
5498 ufshcd_toggle_vreg(dev, info->vccq2, false);
5499 ufshcd_toggle_vreg(dev, info->vccq, false);
5500 ufshcd_toggle_vreg(dev, info->vcc, false);
5501 }
5502 return ret;
5503}
5504
Raviv Shvili6a771a62014-09-25 15:32:24 +03005505static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5506{
5507 struct ufs_vreg_info *info = &hba->vreg_info;
5508
5509 if (info)
5510 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5511
5512 return 0;
5513}
5514
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005515static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5516{
5517 int ret = 0;
5518
5519 if (!vreg)
5520 goto out;
5521
5522 vreg->reg = devm_regulator_get(dev, vreg->name);
5523 if (IS_ERR(vreg->reg)) {
5524 ret = PTR_ERR(vreg->reg);
5525 dev_err(dev, "%s: %s get failed, err=%d\n",
5526 __func__, vreg->name, ret);
5527 }
5528out:
5529 return ret;
5530}
5531
5532static int ufshcd_init_vreg(struct ufs_hba *hba)
5533{
5534 int ret = 0;
5535 struct device *dev = hba->dev;
5536 struct ufs_vreg_info *info = &hba->vreg_info;
5537
5538 if (!info)
5539 goto out;
5540
5541 ret = ufshcd_get_vreg(dev, info->vcc);
5542 if (ret)
5543 goto out;
5544
5545 ret = ufshcd_get_vreg(dev, info->vccq);
5546 if (ret)
5547 goto out;
5548
5549 ret = ufshcd_get_vreg(dev, info->vccq2);
5550out:
5551 return ret;
5552}
5553
Raviv Shvili6a771a62014-09-25 15:32:24 +03005554static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5555{
5556 struct ufs_vreg_info *info = &hba->vreg_info;
5557
5558 if (info)
5559 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5560
5561 return 0;
5562}
5563
Yaniv Gardi60f01872016-03-10 17:37:11 +02005564static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5565{
5566 int ret = 0;
5567 struct ufs_vreg_info *info = &hba->vreg_info;
5568
5569 if (!info)
5570 goto out;
5571 else if (!info->vccq)
5572 goto out;
5573
5574 if (unused) {
5575 /* shut off the rail here */
5576 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5577 /*
5578 * Mark this rail as no longer used, so it doesn't get enabled
5579 * later by mistake
5580 */
5581 if (!ret)
5582 info->vccq->unused = true;
5583 } else {
5584 /*
5585 * rail should have been already enabled hence just make sure
5586 * that unused flag is cleared.
5587 */
5588 info->vccq->unused = false;
5589 }
5590out:
5591 return ret;
5592}
5593
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005594static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5595 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005596{
5597 int ret = 0;
5598 struct ufs_clk_info *clki;
5599 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005600 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005601
5602 if (!head || list_empty(head))
5603 goto out;
5604
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005605 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5606 if (ret)
5607 return ret;
5608
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005609 list_for_each_entry(clki, head, list) {
5610 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005611 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5612 continue;
5613
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005614 if (on && !clki->enabled) {
5615 ret = clk_prepare_enable(clki->clk);
5616 if (ret) {
5617 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5618 __func__, clki->name, ret);
5619 goto out;
5620 }
5621 } else if (!on && clki->enabled) {
5622 clk_disable_unprepare(clki->clk);
5623 }
5624 clki->enabled = on;
5625 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5626 clki->name, on ? "en" : "dis");
5627 }
5628 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005629
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005630 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5631 if (ret)
5632 return ret;
5633
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005634out:
5635 if (ret) {
5636 list_for_each_entry(clki, head, list) {
5637 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5638 clk_disable_unprepare(clki->clk);
5639 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005640 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005641 spin_lock_irqsave(hba->host->host_lock, flags);
5642 hba->clk_gating.state = CLKS_ON;
5643 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005644 }
5645 return ret;
5646}
5647
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005648static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5649{
5650 return __ufshcd_setup_clocks(hba, on, false);
5651}
5652
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005653static int ufshcd_init_clocks(struct ufs_hba *hba)
5654{
5655 int ret = 0;
5656 struct ufs_clk_info *clki;
5657 struct device *dev = hba->dev;
5658 struct list_head *head = &hba->clk_list_head;
5659
5660 if (!head || list_empty(head))
5661 goto out;
5662
5663 list_for_each_entry(clki, head, list) {
5664 if (!clki->name)
5665 continue;
5666
5667 clki->clk = devm_clk_get(dev, clki->name);
5668 if (IS_ERR(clki->clk)) {
5669 ret = PTR_ERR(clki->clk);
5670 dev_err(dev, "%s: %s clk get failed, %d\n",
5671 __func__, clki->name, ret);
5672 goto out;
5673 }
5674
5675 if (clki->max_freq) {
5676 ret = clk_set_rate(clki->clk, clki->max_freq);
5677 if (ret) {
5678 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5679 __func__, clki->name,
5680 clki->max_freq, ret);
5681 goto out;
5682 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005683 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005684 }
5685 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5686 clki->name, clk_get_rate(clki->clk));
5687 }
5688out:
5689 return ret;
5690}
5691
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005692static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5693{
5694 int err = 0;
5695
5696 if (!hba->vops)
5697 goto out;
5698
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005699 err = ufshcd_vops_init(hba);
5700 if (err)
5701 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005702
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005703 err = ufshcd_vops_setup_regulators(hba, true);
5704 if (err)
5705 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005706
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005707 goto out;
5708
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005709out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005710 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005711out:
5712 if (err)
5713 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005714 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005715 return err;
5716}
5717
5718static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5719{
5720 if (!hba->vops)
5721 return;
5722
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005723 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005724
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005725 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005726}
5727
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005728static int ufshcd_hba_init(struct ufs_hba *hba)
5729{
5730 int err;
5731
Raviv Shvili6a771a62014-09-25 15:32:24 +03005732 /*
5733 * Handle host controller power separately from the UFS device power
5734 * rails as it will help controlling the UFS host controller power
5735 * collapse easily which is different than UFS device power collapse.
5736 * Also, enable the host controller power before we go ahead with rest
5737 * of the initialization here.
5738 */
5739 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005740 if (err)
5741 goto out;
5742
Raviv Shvili6a771a62014-09-25 15:32:24 +03005743 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005744 if (err)
5745 goto out;
5746
Raviv Shvili6a771a62014-09-25 15:32:24 +03005747 err = ufshcd_init_clocks(hba);
5748 if (err)
5749 goto out_disable_hba_vreg;
5750
5751 err = ufshcd_setup_clocks(hba, true);
5752 if (err)
5753 goto out_disable_hba_vreg;
5754
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005755 err = ufshcd_init_vreg(hba);
5756 if (err)
5757 goto out_disable_clks;
5758
5759 err = ufshcd_setup_vreg(hba, true);
5760 if (err)
5761 goto out_disable_clks;
5762
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005763 err = ufshcd_variant_hba_init(hba);
5764 if (err)
5765 goto out_disable_vreg;
5766
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005767 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005768 goto out;
5769
5770out_disable_vreg:
5771 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005772out_disable_clks:
5773 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005774out_disable_hba_vreg:
5775 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005776out:
5777 return err;
5778}
5779
5780static void ufshcd_hba_exit(struct ufs_hba *hba)
5781{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005782 if (hba->is_powered) {
5783 ufshcd_variant_hba_exit(hba);
5784 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07005785 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005786 ufshcd_setup_clocks(hba, false);
5787 ufshcd_setup_hba_vreg(hba, false);
5788 hba->is_powered = false;
5789 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005790}
5791
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005792static int
5793ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305794{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005795 unsigned char cmd[6] = {REQUEST_SENSE,
5796 0,
5797 0,
5798 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005799 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005800 0};
5801 char *buffer;
5802 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305803
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005804 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005805 if (!buffer) {
5806 ret = -ENOMEM;
5807 goto out;
5808 }
5809
5810 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005811 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005812 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5813 if (ret)
5814 pr_err("%s: failed with err %d\n", __func__, ret);
5815
5816 kfree(buffer);
5817out:
5818 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305819}
5820
5821/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005822 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5823 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305824 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005825 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305826 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005827 * Returns 0 if requested power mode is set successfully
5828 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305829 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005830static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5831 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305832{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005833 unsigned char cmd[6] = { START_STOP };
5834 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005835 struct scsi_device *sdp;
5836 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005837 int ret;
5838
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005839 spin_lock_irqsave(hba->host->host_lock, flags);
5840 sdp = hba->sdev_ufs_device;
5841 if (sdp) {
5842 ret = scsi_device_get(sdp);
5843 if (!ret && !scsi_device_online(sdp)) {
5844 ret = -ENODEV;
5845 scsi_device_put(sdp);
5846 }
5847 } else {
5848 ret = -ENODEV;
5849 }
5850 spin_unlock_irqrestore(hba->host->host_lock, flags);
5851
5852 if (ret)
5853 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005854
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305855 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005856 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5857 * handling, which would wait for host to be resumed. Since we know
5858 * we are functional while we are here, skip host resume in error
5859 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305860 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005861 hba->host->eh_noresume = 1;
5862 if (hba->wlun_dev_clr_ua) {
5863 ret = ufshcd_send_request_sense(hba, sdp);
5864 if (ret)
5865 goto out;
5866 /* Unit attention condition is cleared now */
5867 hba->wlun_dev_clr_ua = false;
5868 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305869
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005870 cmd[4] = pwr_mode << 4;
5871
5872 /*
5873 * Current function would be generally called from the power management
5874 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5875 * already suspended childs.
5876 */
5877 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5878 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5879 if (ret) {
5880 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005881 "START_STOP failed for power mode: %d, result %x\n",
5882 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005883 if (driver_byte(ret) & DRIVER_SENSE)
5884 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005885 }
5886
5887 if (!ret)
5888 hba->curr_dev_pwr_mode = pwr_mode;
5889out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005890 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005891 hba->host->eh_noresume = 0;
5892 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305893}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305894
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005895static int ufshcd_link_state_transition(struct ufs_hba *hba,
5896 enum uic_link_state req_link_state,
5897 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305898{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005899 int ret = 0;
5900
5901 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305902 return 0;
5903
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005904 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5905 ret = ufshcd_uic_hibern8_enter(hba);
5906 if (!ret)
5907 ufshcd_set_link_hibern8(hba);
5908 else
5909 goto out;
5910 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305911 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005912 * If autobkops is enabled, link can't be turned off because
5913 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305914 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005915 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5916 (!check_for_bkops || (check_for_bkops &&
5917 !hba->auto_bkops_enabled))) {
5918 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005919 * Let's make sure that link is in low power mode, we are doing
5920 * this currently by putting the link in Hibern8. Otherway to
5921 * put the link in low power mode is to send the DME end point
5922 * to device and then send the DME reset command to local
5923 * unipro. But putting the link in hibern8 is much faster.
5924 */
5925 ret = ufshcd_uic_hibern8_enter(hba);
5926 if (ret)
5927 goto out;
5928 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005929 * Change controller state to "reset state" which
5930 * should also put the link in off/reset state
5931 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005932 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005933 /*
5934 * TODO: Check if we need any delay to make sure that
5935 * controller is reset
5936 */
5937 ufshcd_set_link_off(hba);
5938 }
5939
5940out:
5941 return ret;
5942}
5943
5944static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5945{
5946 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02005947 * It seems some UFS devices may keep drawing more than sleep current
5948 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
5949 * To avoid this situation, add 2ms delay before putting these UFS
5950 * rails in LPM mode.
5951 */
5952 if (!ufshcd_is_link_active(hba) &&
5953 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
5954 usleep_range(2000, 2100);
5955
5956 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005957 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5958 * power.
5959 *
5960 * If UFS device and link is in OFF state, all power supplies (VCC,
5961 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5962 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5963 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5964 *
5965 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5966 * in low power state which would save some power.
5967 */
5968 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5969 !hba->dev_info.is_lu_power_on_wp) {
5970 ufshcd_setup_vreg(hba, false);
5971 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5972 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5973 if (!ufshcd_is_link_active(hba)) {
5974 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5975 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5976 }
5977 }
5978}
5979
5980static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5981{
5982 int ret = 0;
5983
5984 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5985 !hba->dev_info.is_lu_power_on_wp) {
5986 ret = ufshcd_setup_vreg(hba, true);
5987 } else if (!ufshcd_is_ufs_dev_active(hba)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005988 if (!ret && !ufshcd_is_link_active(hba)) {
5989 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5990 if (ret)
5991 goto vcc_disable;
5992 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5993 if (ret)
5994 goto vccq_lpm;
5995 }
Subhash Jadavani69d72ac2016-10-27 17:26:24 -07005996 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005997 }
5998 goto out;
5999
6000vccq_lpm:
6001 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6002vcc_disable:
6003 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6004out:
6005 return ret;
6006}
6007
6008static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6009{
6010 if (ufshcd_is_link_off(hba))
6011 ufshcd_setup_hba_vreg(hba, false);
6012}
6013
6014static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6015{
6016 if (ufshcd_is_link_off(hba))
6017 ufshcd_setup_hba_vreg(hba, true);
6018}
6019
6020/**
6021 * ufshcd_suspend - helper function for suspend operations
6022 * @hba: per adapter instance
6023 * @pm_op: desired low power operation type
6024 *
6025 * This function will try to put the UFS device and link into low power
6026 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6027 * (System PM level).
6028 *
6029 * If this function is called during shutdown, it will make sure that
6030 * both UFS device and UFS link is powered off.
6031 *
6032 * NOTE: UFS device & link must be active before we enter in this function.
6033 *
6034 * Returns 0 for success and non-zero for failure
6035 */
6036static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6037{
6038 int ret = 0;
6039 enum ufs_pm_level pm_lvl;
6040 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6041 enum uic_link_state req_link_state;
6042
6043 hba->pm_op_in_progress = 1;
6044 if (!ufshcd_is_shutdown_pm(pm_op)) {
6045 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6046 hba->rpm_lvl : hba->spm_lvl;
6047 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6048 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6049 } else {
6050 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6051 req_link_state = UIC_LINK_OFF_STATE;
6052 }
6053
6054 /*
6055 * If we can't transition into any of the low power modes
6056 * just gate the clocks.
6057 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006058 ufshcd_hold(hba, false);
6059 hba->clk_gating.is_suspended = true;
6060
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006061 ufshcd_suspend_clkscaling(hba);
6062
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006063 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6064 req_link_state == UIC_LINK_ACTIVE_STATE) {
6065 goto disable_clks;
6066 }
6067
6068 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6069 (req_link_state == hba->uic_link_state))
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006070 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006071
6072 /* UFS device & link must be active before we enter in this function */
6073 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6074 ret = -EINVAL;
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006075 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006076 }
6077
6078 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006079 if (ufshcd_can_autobkops_during_suspend(hba)) {
6080 /*
6081 * The device is idle with no requests in the queue,
6082 * allow background operations if bkops status shows
6083 * that performance might be impacted.
6084 */
6085 ret = ufshcd_urgent_bkops(hba);
6086 if (ret)
6087 goto enable_gating;
6088 } else {
6089 /* make sure that auto bkops is disabled */
6090 ufshcd_disable_auto_bkops(hba);
6091 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006092 }
6093
6094 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6095 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6096 !ufshcd_is_runtime_pm(pm_op))) {
6097 /* ensure that bkops is disabled */
6098 ufshcd_disable_auto_bkops(hba);
6099 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6100 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006101 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006102 }
6103
6104 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6105 if (ret)
6106 goto set_dev_active;
6107
6108 ufshcd_vreg_set_lpm(hba);
6109
6110disable_clks:
6111 /*
6112 * Call vendor specific suspend callback. As these callbacks may access
6113 * vendor specific host controller register space call them before the
6114 * host clocks are ON.
6115 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006116 ret = ufshcd_vops_suspend(hba, pm_op);
6117 if (ret)
6118 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006119
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006120 if (!ufshcd_is_link_active(hba))
6121 ufshcd_setup_clocks(hba, false);
6122 else
6123 /* If link is active, device ref_clk can't be switched off */
6124 __ufshcd_setup_clocks(hba, false, true);
6125
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006126 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006127 /*
6128 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006129 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006130 */
6131 ufshcd_disable_irq(hba);
6132 /* Put the host controller in low power mode if possible */
6133 ufshcd_hba_vreg_set_lpm(hba);
6134 goto out;
6135
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006136set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07006137 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006138 ufshcd_vreg_set_hpm(hba);
6139 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6140 ufshcd_set_link_active(hba);
6141 else if (ufshcd_is_link_off(hba))
6142 ufshcd_host_reset_and_restore(hba);
6143set_dev_active:
6144 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6145 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006146enable_gating:
Subhash Jadavanid6fcf812016-10-27 17:26:09 -07006147 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006148 hba->clk_gating.is_suspended = false;
6149 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006150out:
6151 hba->pm_op_in_progress = 0;
6152 return ret;
6153}
6154
6155/**
6156 * ufshcd_resume - helper function for resume operations
6157 * @hba: per adapter instance
6158 * @pm_op: runtime PM or system PM
6159 *
6160 * This function basically brings the UFS device, UniPro link and controller
6161 * to active state.
6162 *
6163 * Returns 0 for success and non-zero for failure
6164 */
6165static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6166{
6167 int ret;
6168 enum uic_link_state old_link_state;
6169
6170 hba->pm_op_in_progress = 1;
6171 old_link_state = hba->uic_link_state;
6172
6173 ufshcd_hba_vreg_set_hpm(hba);
6174 /* Make sure clocks are enabled before accessing controller */
6175 ret = ufshcd_setup_clocks(hba, true);
6176 if (ret)
6177 goto out;
6178
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006179 /* enable the host irq as host controller would be active soon */
6180 ret = ufshcd_enable_irq(hba);
6181 if (ret)
6182 goto disable_irq_and_vops_clks;
6183
6184 ret = ufshcd_vreg_set_hpm(hba);
6185 if (ret)
6186 goto disable_irq_and_vops_clks;
6187
6188 /*
6189 * Call vendor specific resume callback. As these callbacks may access
6190 * vendor specific host controller register space call them when the
6191 * host clocks are ON.
6192 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006193 ret = ufshcd_vops_resume(hba, pm_op);
6194 if (ret)
6195 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006196
6197 if (ufshcd_is_link_hibern8(hba)) {
6198 ret = ufshcd_uic_hibern8_exit(hba);
6199 if (!ret)
6200 ufshcd_set_link_active(hba);
6201 else
6202 goto vendor_suspend;
6203 } else if (ufshcd_is_link_off(hba)) {
6204 ret = ufshcd_host_reset_and_restore(hba);
6205 /*
6206 * ufshcd_host_reset_and_restore() should have already
6207 * set the link state as active
6208 */
6209 if (ret || !ufshcd_is_link_active(hba))
6210 goto vendor_suspend;
6211 }
6212
6213 if (!ufshcd_is_ufs_dev_active(hba)) {
6214 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6215 if (ret)
6216 goto set_old_link_state;
6217 }
6218
Subhash Jadavani374a2462014-09-25 15:32:35 +03006219 /*
6220 * If BKOPs operations are urgently needed at this moment then
6221 * keep auto-bkops enabled or else disable it.
6222 */
6223 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006224 hba->clk_gating.is_suspended = false;
6225
Gilad Bronera5082532016-10-17 17:10:00 -07006226 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006227
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006228 /* Schedule clock gating in case of no access to UFS device yet */
6229 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006230 goto out;
6231
6232set_old_link_state:
6233 ufshcd_link_state_transition(hba, old_link_state, 0);
6234vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006235 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006236disable_vreg:
6237 ufshcd_vreg_set_lpm(hba);
6238disable_irq_and_vops_clks:
6239 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006240 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006241 ufshcd_setup_clocks(hba, false);
6242out:
6243 hba->pm_op_in_progress = 0;
6244 return ret;
6245}
6246
6247/**
6248 * ufshcd_system_suspend - system suspend routine
6249 * @hba: per adapter instance
6250 * @pm_op: runtime PM or system PM
6251 *
6252 * Check the description of ufshcd_suspend() function for more details.
6253 *
6254 * Returns 0 for success and non-zero for failure
6255 */
6256int ufshcd_system_suspend(struct ufs_hba *hba)
6257{
6258 int ret = 0;
6259
6260 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006261 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006262
6263 if (pm_runtime_suspended(hba->dev)) {
6264 if (hba->rpm_lvl == hba->spm_lvl)
6265 /*
6266 * There is possibility that device may still be in
6267 * active state during the runtime suspend.
6268 */
6269 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6270 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6271 goto out;
6272
6273 /*
6274 * UFS device and/or UFS link low power states during runtime
6275 * suspend seems to be different than what is expected during
6276 * system suspend. Hence runtime resume the devic & link and
6277 * let the system suspend low power states to take effect.
6278 * TODO: If resume takes longer time, we might have optimize
6279 * it in future by not resuming everything if possible.
6280 */
6281 ret = ufshcd_runtime_resume(hba);
6282 if (ret)
6283 goto out;
6284 }
6285
6286 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6287out:
Dolev Ravive7850602014-09-25 15:32:36 +03006288 if (!ret)
6289 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006290 return ret;
6291}
6292EXPORT_SYMBOL(ufshcd_system_suspend);
6293
6294/**
6295 * ufshcd_system_resume - system resume routine
6296 * @hba: per adapter instance
6297 *
6298 * Returns 0 for success and non-zero for failure
6299 */
6300
6301int ufshcd_system_resume(struct ufs_hba *hba)
6302{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006303 if (!hba)
6304 return -EINVAL;
6305
6306 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006307 /*
6308 * Let the runtime resume take care of resuming
6309 * if runtime suspended.
6310 */
6311 return 0;
6312
6313 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6314}
6315EXPORT_SYMBOL(ufshcd_system_resume);
6316
6317/**
6318 * ufshcd_runtime_suspend - runtime suspend routine
6319 * @hba: per adapter instance
6320 *
6321 * Check the description of ufshcd_suspend() function for more details.
6322 *
6323 * Returns 0 for success and non-zero for failure
6324 */
6325int ufshcd_runtime_suspend(struct ufs_hba *hba)
6326{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006327 if (!hba)
6328 return -EINVAL;
6329
6330 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006331 return 0;
6332
6333 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306334}
6335EXPORT_SYMBOL(ufshcd_runtime_suspend);
6336
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006337/**
6338 * ufshcd_runtime_resume - runtime resume routine
6339 * @hba: per adapter instance
6340 *
6341 * This function basically brings the UFS device, UniPro link and controller
6342 * to active state. Following operations are done in this function:
6343 *
6344 * 1. Turn on all the controller related clocks
6345 * 2. Bring the UniPro link out of Hibernate state
6346 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6347 * to active state.
6348 * 4. If auto-bkops is enabled on the device, disable it.
6349 *
6350 * So following would be the possible power state after this function return
6351 * successfully:
6352 * S1: UFS device in Active state with VCC rail ON
6353 * UniPro link in Active state
6354 * All the UFS/UniPro controller clocks are ON
6355 *
6356 * Returns 0 for success and non-zero for failure
6357 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306358int ufshcd_runtime_resume(struct ufs_hba *hba)
6359{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006360 if (!hba)
6361 return -EINVAL;
6362
6363 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306364 return 0;
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006365
6366 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306367}
6368EXPORT_SYMBOL(ufshcd_runtime_resume);
6369
6370int ufshcd_runtime_idle(struct ufs_hba *hba)
6371{
6372 return 0;
6373}
6374EXPORT_SYMBOL(ufshcd_runtime_idle);
6375
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306376/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006377 * ufshcd_shutdown - shutdown routine
6378 * @hba: per adapter instance
6379 *
6380 * This function would power off both UFS device and UFS link.
6381 *
6382 * Returns 0 always to allow force shutdown even in case of errors.
6383 */
6384int ufshcd_shutdown(struct ufs_hba *hba)
6385{
6386 int ret = 0;
6387
6388 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6389 goto out;
6390
6391 if (pm_runtime_suspended(hba->dev)) {
6392 ret = ufshcd_runtime_resume(hba);
6393 if (ret)
6394 goto out;
6395 }
6396
6397 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6398out:
6399 if (ret)
6400 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6401 /* allow force shutdown even in case of errors */
6402 return 0;
6403}
6404EXPORT_SYMBOL(ufshcd_shutdown);
6405
6406/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306407 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306408 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306409 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306410 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306411void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306412{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306413 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306414 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306415 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006416 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306417
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006418 ufshcd_exit_clk_gating(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006419 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306420}
6421EXPORT_SYMBOL_GPL(ufshcd_remove);
6422
6423/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006424 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6425 * @hba: pointer to Host Bus Adapter (HBA)
6426 */
6427void ufshcd_dealloc_host(struct ufs_hba *hba)
6428{
6429 scsi_host_put(hba->host);
6430}
6431EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6432
6433/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006434 * ufshcd_set_dma_mask - Set dma mask based on the controller
6435 * addressing capability
6436 * @hba: per adapter instance
6437 *
6438 * Returns 0 for success, non-zero for failure
6439 */
6440static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6441{
6442 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6443 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6444 return 0;
6445 }
6446 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6447}
6448
6449/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006450 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306451 * @dev: pointer to device handle
6452 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306453 * Returns 0 on success, non-zero value on failure
6454 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006455int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306456{
6457 struct Scsi_Host *host;
6458 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006459 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306460
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306461 if (!dev) {
6462 dev_err(dev,
6463 "Invalid memory reference for dev is NULL\n");
6464 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306465 goto out_error;
6466 }
6467
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306468 host = scsi_host_alloc(&ufshcd_driver_template,
6469 sizeof(struct ufs_hba));
6470 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306471 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306472 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306473 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306474 }
6475 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306476 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306477 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006478 *hba_handle = hba;
6479
6480out_error:
6481 return err;
6482}
6483EXPORT_SYMBOL(ufshcd_alloc_host);
6484
Sahitya Tummala856b3482014-09-25 15:32:34 +03006485static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6486{
6487 int ret = 0;
6488 struct ufs_clk_info *clki;
6489 struct list_head *head = &hba->clk_list_head;
6490
6491 if (!head || list_empty(head))
6492 goto out;
6493
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006494 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6495 if (ret)
6496 return ret;
6497
Sahitya Tummala856b3482014-09-25 15:32:34 +03006498 list_for_each_entry(clki, head, list) {
6499 if (!IS_ERR_OR_NULL(clki->clk)) {
6500 if (scale_up && clki->max_freq) {
6501 if (clki->curr_freq == clki->max_freq)
6502 continue;
6503 ret = clk_set_rate(clki->clk, clki->max_freq);
6504 if (ret) {
6505 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6506 __func__, clki->name,
6507 clki->max_freq, ret);
6508 break;
6509 }
6510 clki->curr_freq = clki->max_freq;
6511
6512 } else if (!scale_up && clki->min_freq) {
6513 if (clki->curr_freq == clki->min_freq)
6514 continue;
6515 ret = clk_set_rate(clki->clk, clki->min_freq);
6516 if (ret) {
6517 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6518 __func__, clki->name,
6519 clki->min_freq, ret);
6520 break;
6521 }
6522 clki->curr_freq = clki->min_freq;
6523 }
6524 }
6525 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6526 clki->name, clk_get_rate(clki->clk));
6527 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006528
6529 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6530
Sahitya Tummala856b3482014-09-25 15:32:34 +03006531out:
6532 return ret;
6533}
6534
6535static int ufshcd_devfreq_target(struct device *dev,
6536 unsigned long *freq, u32 flags)
6537{
6538 int err = 0;
6539 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006540 bool release_clk_hold = false;
6541 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006542
6543 if (!ufshcd_is_clkscaling_enabled(hba))
6544 return -EINVAL;
6545
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006546 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6547 if (ufshcd_eh_in_progress(hba)) {
6548 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6549 return 0;
6550 }
6551
6552 if (ufshcd_is_clkgating_allowed(hba) &&
6553 (hba->clk_gating.state != CLKS_ON)) {
6554 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6555 /* hold the vote until the scaling work is completed */
6556 hba->clk_gating.active_reqs++;
6557 release_clk_hold = true;
6558 hba->clk_gating.state = CLKS_ON;
6559 } else {
6560 /*
6561 * Clock gating work seems to be running in parallel
6562 * hence skip scaling work to avoid deadlock between
6563 * current scaling work and gating work.
6564 */
6565 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6566 return 0;
6567 }
6568 }
6569 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6570
Sahitya Tummala856b3482014-09-25 15:32:34 +03006571 if (*freq == UINT_MAX)
6572 err = ufshcd_scale_clks(hba, true);
6573 else if (*freq == 0)
6574 err = ufshcd_scale_clks(hba, false);
6575
Subhash Jadavani30fc33f2016-10-27 17:25:47 -07006576 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6577 if (release_clk_hold)
6578 __ufshcd_release(hba);
6579 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6580
Sahitya Tummala856b3482014-09-25 15:32:34 +03006581 return err;
6582}
6583
6584static int ufshcd_devfreq_get_dev_status(struct device *dev,
6585 struct devfreq_dev_status *stat)
6586{
6587 struct ufs_hba *hba = dev_get_drvdata(dev);
6588 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6589 unsigned long flags;
6590
6591 if (!ufshcd_is_clkscaling_enabled(hba))
6592 return -EINVAL;
6593
6594 memset(stat, 0, sizeof(*stat));
6595
6596 spin_lock_irqsave(hba->host->host_lock, flags);
6597 if (!scaling->window_start_t)
6598 goto start_window;
6599
6600 if (scaling->is_busy_started)
6601 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6602 scaling->busy_start_t));
6603
6604 stat->total_time = jiffies_to_usecs((long)jiffies -
6605 (long)scaling->window_start_t);
6606 stat->busy_time = scaling->tot_busy_t;
6607start_window:
6608 scaling->window_start_t = jiffies;
6609 scaling->tot_busy_t = 0;
6610
6611 if (hba->outstanding_reqs) {
6612 scaling->busy_start_t = ktime_get();
6613 scaling->is_busy_started = true;
6614 } else {
6615 scaling->busy_start_t = ktime_set(0, 0);
6616 scaling->is_busy_started = false;
6617 }
6618 spin_unlock_irqrestore(hba->host->host_lock, flags);
6619 return 0;
6620}
6621
6622static struct devfreq_dev_profile ufs_devfreq_profile = {
6623 .polling_ms = 100,
6624 .target = ufshcd_devfreq_target,
6625 .get_dev_status = ufshcd_devfreq_get_dev_status,
6626};
6627
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006628/**
6629 * ufshcd_init - Driver initialization routine
6630 * @hba: per-adapter instance
6631 * @mmio_base: base register address
6632 * @irq: Interrupt line of device
6633 * Returns 0 on success, non-zero value on failure
6634 */
6635int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6636{
6637 int err;
6638 struct Scsi_Host *host = hba->host;
6639 struct device *dev = hba->dev;
6640
6641 if (!mmio_base) {
6642 dev_err(hba->dev,
6643 "Invalid memory reference for mmio_base is NULL\n");
6644 err = -ENODEV;
6645 goto out_error;
6646 }
6647
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306648 hba->mmio_base = mmio_base;
6649 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306650
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006651 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006652 if (err)
6653 goto out_error;
6654
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306655 /* Read capabilities registers */
6656 ufshcd_hba_capabilities(hba);
6657
6658 /* Get UFS version supported by the controller */
6659 hba->ufs_version = ufshcd_get_ufs_version(hba);
6660
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306661 /* Get Interrupt bit mask per version */
6662 hba->intr_mask = ufshcd_get_intr_mask(hba);
6663
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006664 err = ufshcd_set_dma_mask(hba);
6665 if (err) {
6666 dev_err(hba->dev, "set dma mask failed\n");
6667 goto out_disable;
6668 }
6669
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306670 /* Allocate memory for host memory space */
6671 err = ufshcd_memory_alloc(hba);
6672 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306673 dev_err(hba->dev, "Memory allocation failed\n");
6674 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306675 }
6676
6677 /* Configure LRB */
6678 ufshcd_host_memory_configure(hba);
6679
6680 host->can_queue = hba->nutrs;
6681 host->cmd_per_lun = hba->nutrs;
6682 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006683 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306684 host->max_channel = UFSHCD_MAX_CHANNEL;
6685 host->unique_id = host->host_no;
6686 host->max_cmd_len = MAX_CDB_SIZE;
6687
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006688 hba->max_pwr_info.is_valid = false;
6689
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306690 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306691 init_waitqueue_head(&hba->tm_wq);
6692 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306693
6694 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306695 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306696 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306697
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306698 /* Initialize UIC command mutex */
6699 mutex_init(&hba->uic_cmd_mutex);
6700
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306701 /* Initialize mutex for device management commands */
6702 mutex_init(&hba->dev_cmd.lock);
6703
6704 /* Initialize device management tag acquire wait queue */
6705 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6706
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006707 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006708
6709 /*
6710 * In order to avoid any spurious interrupt immediately after
6711 * registering UFS controller interrupt handler, clear any pending UFS
6712 * interrupt status and disable all the UFS interrupts.
6713 */
6714 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6715 REG_INTERRUPT_STATUS);
6716 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6717 /*
6718 * Make sure that UFS interrupts are disabled and any pending interrupt
6719 * status is cleared before registering UFS interrupt handler.
6720 */
6721 mb();
6722
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306723 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006724 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306725 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306726 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006727 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006728 } else {
6729 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306730 }
6731
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306732 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306733 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306734 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006735 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306736 }
6737
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306738 /* Host controller enable */
6739 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306740 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306741 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306742 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306743 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306744
Sahitya Tummala856b3482014-09-25 15:32:34 +03006745 if (ufshcd_is_clkscaling_enabled(hba)) {
Chanwoo Choi4861ee12016-11-08 18:13:28 +09006746 hba->devfreq = devm_devfreq_add_device(dev, &ufs_devfreq_profile,
Sahitya Tummala856b3482014-09-25 15:32:34 +03006747 "simple_ondemand", NULL);
6748 if (IS_ERR(hba->devfreq)) {
6749 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6750 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006751 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006752 goto out_remove_scsi_host;
6753 }
6754 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07006755 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006756 }
6757
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306758 /* Hold auto suspend until async scan completes */
6759 pm_runtime_get_sync(dev);
6760
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006761 /*
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08006762 * We are assuming that device wasn't put in sleep/power-down
6763 * state exclusively during the boot stage before kernel.
6764 * This assumption helps avoid doing link startup twice during
6765 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006766 */
subhashj@codeaurora.org7caf4892016-11-23 16:32:20 -08006767 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006768
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306769 async_schedule(ufshcd_async_scan, hba);
6770
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306771 return 0;
6772
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306773out_remove_scsi_host:
6774 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006775exit_gating:
6776 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306777out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006778 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006779 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306780out_error:
6781 return err;
6782}
6783EXPORT_SYMBOL_GPL(ufshcd_init);
6784
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306785MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6786MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306787MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306788MODULE_LICENSE("GPL");
6789MODULE_VERSION(UFSHCD_DRIVER_VERSION);