blob: 094e879af1213264527fe26445064c9d5b03d8c3 [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
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053048#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
49 UTP_TASK_REQ_COMPL |\
50 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053051/* UIC command timeout, unit: ms */
52#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053053
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053054/* NOP OUT retries waiting for NOP IN response */
55#define NOP_OUT_RETRIES 10
56/* Timeout after 30 msecs if NOP OUT hangs without response */
57#define NOP_OUT_TIMEOUT 30 /* msecs */
58
Dolev Raviv68078d52013-07-30 00:35:58 +053059/* Query request retries */
60#define QUERY_REQ_RETRIES 10
61/* Query request timeout */
62#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020063/*
64 * Query request timeout for fDeviceInit flag
65 * fDeviceInit query response time for some devices is too large that default
66 * QUERY_REQ_TIMEOUT may not be enough for such devices.
67 */
68#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053069
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053070/* Task management command timeout */
71#define TM_CMD_TIMEOUT 100 /* msecs */
72
Yaniv Gardi64238fb2016-02-01 15:02:43 +020073/* maximum number of retries for a general UIC command */
74#define UFS_UIC_COMMAND_RETRIES 3
75
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030076/* maximum number of link-startup retries */
77#define DME_LINKSTARTUP_RETRIES 3
78
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020079/* Maximum retries for Hibern8 enter */
80#define UIC_HIBERN8_ENTER_RETRIES 3
81
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030082/* maximum number of reset retries before giving up */
83#define MAX_HOST_RESET_RETRIES 5
84
Dolev Raviv68078d52013-07-30 00:35:58 +053085/* Expose the flag value from utp_upiu_query.value */
86#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
87
Seungwon Jeon7d568652013-08-31 21:40:20 +053088/* Interrupt aggregation default timeout, unit: 40us */
89#define INT_AGGR_DEF_TO 0x02
90
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030091#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
92 ({ \
93 int _ret; \
94 if (_on) \
95 _ret = ufshcd_enable_vreg(_dev, _vreg); \
96 else \
97 _ret = ufshcd_disable_vreg(_dev, _vreg); \
98 _ret; \
99 })
100
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530101enum {
102 UFSHCD_MAX_CHANNEL = 0,
103 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530104 UFSHCD_CMD_PER_LUN = 32,
105 UFSHCD_CAN_QUEUE = 32,
106};
107
108/* UFSHCD states */
109enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530110 UFSHCD_STATE_RESET,
111 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530112 UFSHCD_STATE_OPERATIONAL,
Zang Leiganga17bddc2017-04-04 19:32:20 +0000113 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530114};
115
116/* UFSHCD error handling flags */
117enum {
118 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530119};
120
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530121/* UFSHCD UIC layer error flags */
122enum {
123 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200124 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
125 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
126 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
127 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
128 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530129};
130
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530131/* Interrupt configuration options */
132enum {
133 UFSHCD_INT_DISABLE,
134 UFSHCD_INT_ENABLE,
135 UFSHCD_INT_CLEAR,
136};
137
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530138#define ufshcd_set_eh_in_progress(h) \
139 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
140#define ufshcd_eh_in_progress(h) \
141 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
142#define ufshcd_clear_eh_in_progress(h) \
143 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
144
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300145#define ufshcd_set_ufs_dev_active(h) \
146 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
147#define ufshcd_set_ufs_dev_sleep(h) \
148 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
149#define ufshcd_set_ufs_dev_poweroff(h) \
150 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
151#define ufshcd_is_ufs_dev_active(h) \
152 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
153#define ufshcd_is_ufs_dev_sleep(h) \
154 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
155#define ufshcd_is_ufs_dev_poweroff(h) \
156 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
157
158static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
159 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
160 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
161 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
162 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
163 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
164 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
165};
166
167static inline enum ufs_dev_pwr_mode
168ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
169{
170 return ufs_pm_lvl_states[lvl].dev_state;
171}
172
173static inline enum uic_link_state
174ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
175{
176 return ufs_pm_lvl_states[lvl].link_state;
177}
178
Subhash Jadavanibedc6292017-04-04 19:32:13 +0000179static struct ufs_dev_fix ufs_fixups[] = {
180 /* UFS cards deviations table */
181 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
182 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
183 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
184 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
185 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
186 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
187 UFS_DEVICE_NO_FASTAUTO),
188 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
189 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
190 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
191 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
192 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
193 UFS_DEVICE_QUIRK_PA_TACTIVATE),
194 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
195 UFS_DEVICE_QUIRK_PA_TACTIVATE),
196 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
197 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
198 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
199
200 END_FIX
201};
202
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530203static void ufshcd_tmc_handler(struct ufs_hba *hba);
204static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530205static int ufshcd_reset_and_restore(struct ufs_hba *hba);
206static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300207static void ufshcd_hba_exit(struct ufs_hba *hba);
208static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300209static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
210 bool skip_ref_clk);
211static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200212static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300213static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
214static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300215static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300216static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
217static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300218static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
219 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300220static int ufshcd_change_power_mode(struct ufs_hba *hba,
221 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200222static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
223{
224 return tag >= 0 && tag < hba->nutrs;
225}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300226
227static inline int ufshcd_enable_irq(struct ufs_hba *hba)
228{
229 int ret = 0;
230
231 if (!hba->is_irq_enabled) {
232 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
233 hba);
234 if (ret)
235 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
236 __func__, ret);
237 hba->is_irq_enabled = true;
238 }
239
240 return ret;
241}
242
243static inline void ufshcd_disable_irq(struct ufs_hba *hba)
244{
245 if (hba->is_irq_enabled) {
246 free_irq(hba->irq, hba);
247 hba->is_irq_enabled = false;
248 }
249}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530250
Yaniv Gardib573d482016-03-10 17:37:09 +0200251/* replace non-printable or non-ASCII characters with spaces */
252static inline void ufshcd_remove_non_printable(char *val)
253{
254 if (!val)
255 return;
256
257 if (*val < 0x20 || *val > 0x7e)
258 *val = ' ';
259}
260
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530261/*
262 * ufshcd_wait_for_register - wait for register value to change
263 * @hba - per-adapter interface
264 * @reg - mmio register offset
265 * @mask - mask to apply to read register value
266 * @val - wait condition
267 * @interval_us - polling interval in microsecs
268 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200269 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530270 *
271 * Returns -ETIMEDOUT on error, zero on success
272 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200273int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
274 u32 val, unsigned long interval_us,
275 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530276{
277 int err = 0;
278 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
279
280 /* ignore bits that we don't intend to wait on */
281 val = val & mask;
282
283 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200284 if (can_sleep)
285 usleep_range(interval_us, interval_us + 50);
286 else
287 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530288 if (time_after(jiffies, timeout)) {
289 if ((ufshcd_readl(hba, reg) & mask) != val)
290 err = -ETIMEDOUT;
291 break;
292 }
293 }
294
295 return err;
296}
297
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530298/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530299 * ufshcd_get_intr_mask - Get the interrupt bit mask
300 * @hba - Pointer to adapter instance
301 *
302 * Returns interrupt bit mask per version
303 */
304static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
305{
306 if (hba->ufs_version == UFSHCI_VERSION_10)
307 return INTERRUPT_MASK_ALL_VER_10;
308 else
309 return INTERRUPT_MASK_ALL_VER_11;
310}
311
312/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530313 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
314 * @hba - Pointer to adapter instance
315 *
316 * Returns UFSHCI version supported by the controller
317 */
318static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
319{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200320 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
321 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300322
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530323 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530324}
325
326/**
327 * ufshcd_is_device_present - Check if any device connected to
328 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300329 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530330 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530331 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530332 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300333static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530334{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300335 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
336 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530337}
338
339/**
340 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
341 * @lrb: pointer to local command reference block
342 *
343 * This function is used to get the OCS field from UTRD
344 * Returns the OCS field in the UTRD
345 */
346static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
347{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530348 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530349}
350
351/**
352 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
353 * @task_req_descp: pointer to utp_task_req_desc structure
354 *
355 * This function is used to get the OCS field from UTMRD
356 * Returns the OCS field in the UTMRD
357 */
358static inline int
359ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
360{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530361 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530362}
363
364/**
365 * ufshcd_get_tm_free_slot - get a free slot for task management request
366 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530367 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530368 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530369 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
370 * Returns 0 if free slot is not available, else return 1 with tag value
371 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530372 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530373static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530374{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530375 int tag;
376 bool ret = false;
377
378 if (!free_slot)
379 goto out;
380
381 do {
382 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
383 if (tag >= hba->nutmrs)
384 goto out;
385 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
386
387 *free_slot = tag;
388 ret = true;
389out:
390 return ret;
391}
392
393static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
394{
395 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530396}
397
398/**
399 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
400 * @hba: per adapter instance
401 * @pos: position of the bit to be cleared
402 */
403static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
404{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530405 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530406}
407
408/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200409 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
410 * @hba: per adapter instance
411 * @tag: position of the bit to be cleared
412 */
413static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
414{
415 __clear_bit(tag, &hba->outstanding_reqs);
416}
417
418/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530419 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
420 * @reg: Register value of host controller status
421 *
422 * Returns integer, 0 on Success and positive value if failed
423 */
424static inline int ufshcd_get_lists_status(u32 reg)
425{
426 /*
427 * The mask 0xFF is for the following HCS register bits
428 * Bit Description
429 * 0 Device Present
430 * 1 UTRLRDY
431 * 2 UTMRLRDY
432 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200433 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530434 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200435 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530436}
437
438/**
439 * ufshcd_get_uic_cmd_result - Get the UIC command result
440 * @hba: Pointer to adapter instance
441 *
442 * This function gets the result of UIC command completion
443 * Returns 0 on success, non zero value on error
444 */
445static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
446{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530447 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530448 MASK_UIC_COMMAND_RESULT;
449}
450
451/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530452 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
453 * @hba: Pointer to adapter instance
454 *
455 * This function gets UIC command argument3
456 * Returns 0 on success, non zero value on error
457 */
458static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
459{
460 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
461}
462
463/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530464 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530465 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530466 */
467static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530468ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530469{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530470 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530471}
472
473/**
474 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
475 * @ucd_rsp_ptr: pointer to response UPIU
476 *
477 * This function gets the response status and scsi_status from response UPIU
478 * Returns the response result code.
479 */
480static inline int
481ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
482{
483 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
484}
485
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530486/*
487 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
488 * from response UPIU
489 * @ucd_rsp_ptr: pointer to response UPIU
490 *
491 * Return the data segment length.
492 */
493static inline unsigned int
494ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
495{
496 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
497 MASK_RSP_UPIU_DATA_SEG_LEN;
498}
499
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530500/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530501 * ufshcd_is_exception_event - Check if the device raised an exception event
502 * @ucd_rsp_ptr: pointer to response UPIU
503 *
504 * The function checks if the device raised an exception event indicated in
505 * the Device Information field of response UPIU.
506 *
507 * Returns true if exception is raised, false otherwise.
508 */
509static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
510{
511 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
512 MASK_RSP_EXCEPTION_EVENT ? true : false;
513}
514
515/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530516 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530517 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530518 */
519static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530520ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530521{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530522 ufshcd_writel(hba, INT_AGGR_ENABLE |
523 INT_AGGR_COUNTER_AND_TIMER_RESET,
524 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
525}
526
527/**
528 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
529 * @hba: per adapter instance
530 * @cnt: Interrupt aggregation counter threshold
531 * @tmout: Interrupt aggregation timeout value
532 */
533static inline void
534ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
535{
536 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
537 INT_AGGR_COUNTER_THLD_VAL(cnt) |
538 INT_AGGR_TIMEOUT_VAL(tmout),
539 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530540}
541
542/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300543 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
544 * @hba: per adapter instance
545 */
546static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
547{
548 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
549}
550
551/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530552 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
553 * When run-stop registers are set to 1, it indicates the
554 * host controller that it can process the requests
555 * @hba: per adapter instance
556 */
557static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
558{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530559 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
560 REG_UTP_TASK_REQ_LIST_RUN_STOP);
561 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
562 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530563}
564
565/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530566 * ufshcd_hba_start - Start controller initialization sequence
567 * @hba: per adapter instance
568 */
569static inline void ufshcd_hba_start(struct ufs_hba *hba)
570{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530571 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530572}
573
574/**
575 * ufshcd_is_hba_active - Get controller state
576 * @hba: per adapter instance
577 *
578 * Returns zero if controller is active, 1 otherwise
579 */
580static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
581{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530582 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530583}
584
Yaniv Gardi37113102016-03-10 17:37:16 +0200585u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
586{
587 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
588 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
589 (hba->ufs_version == UFSHCI_VERSION_11))
590 return UFS_UNIPRO_VER_1_41;
591 else
592 return UFS_UNIPRO_VER_1_6;
593}
594EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
595
596static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
597{
598 /*
599 * If both host and device support UniPro ver1.6 or later, PA layer
600 * parameters tuning happens during link startup itself.
601 *
602 * We can manually tune PA layer parameters if either host or device
603 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
604 * logic simple, we will only do manual tuning if local unipro version
605 * doesn't support ver1.6 or later.
606 */
607 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
608 return true;
609 else
610 return false;
611}
612
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300613static void ufshcd_ungate_work(struct work_struct *work)
614{
615 int ret;
616 unsigned long flags;
617 struct ufs_hba *hba = container_of(work, struct ufs_hba,
618 clk_gating.ungate_work);
619
620 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
621
622 spin_lock_irqsave(hba->host->host_lock, flags);
623 if (hba->clk_gating.state == CLKS_ON) {
624 spin_unlock_irqrestore(hba->host->host_lock, flags);
625 goto unblock_reqs;
626 }
627
628 spin_unlock_irqrestore(hba->host->host_lock, flags);
629 ufshcd_setup_clocks(hba, true);
630
631 /* Exit from hibern8 */
632 if (ufshcd_can_hibern8_during_gating(hba)) {
633 /* Prevent gating in this path */
634 hba->clk_gating.is_suspended = true;
635 if (ufshcd_is_link_hibern8(hba)) {
636 ret = ufshcd_uic_hibern8_exit(hba);
637 if (ret)
638 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
639 __func__, ret);
640 else
641 ufshcd_set_link_active(hba);
642 }
643 hba->clk_gating.is_suspended = false;
644 }
645unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300646 if (ufshcd_is_clkscaling_enabled(hba))
647 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300648 scsi_unblock_requests(hba->host);
649}
650
651/**
652 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
653 * Also, exit from hibern8 mode and set the link as active.
654 * @hba: per adapter instance
655 * @async: This indicates whether caller should ungate clocks asynchronously.
656 */
657int ufshcd_hold(struct ufs_hba *hba, bool async)
658{
659 int rc = 0;
660 unsigned long flags;
661
662 if (!ufshcd_is_clkgating_allowed(hba))
663 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300664 spin_lock_irqsave(hba->host->host_lock, flags);
665 hba->clk_gating.active_reqs++;
666
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200667 if (ufshcd_eh_in_progress(hba)) {
668 spin_unlock_irqrestore(hba->host->host_lock, flags);
669 return 0;
670 }
671
Sahitya Tummala856b3482014-09-25 15:32:34 +0300672start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300673 switch (hba->clk_gating.state) {
674 case CLKS_ON:
Venkat Gopalakrishnan0967f4d2016-10-17 17:10:53 -0700675 /*
676 * Wait for the ungate work to complete if in progress.
677 * Though the clocks may be in ON state, the link could
678 * still be in hibner8 state if hibern8 is allowed
679 * during clock gating.
680 * Make sure we exit hibern8 state also in addition to
681 * clocks being ON.
682 */
683 if (ufshcd_can_hibern8_during_gating(hba) &&
684 ufshcd_is_link_hibern8(hba)) {
685 spin_unlock_irqrestore(hba->host->host_lock, flags);
686 flush_work(&hba->clk_gating.ungate_work);
687 spin_lock_irqsave(hba->host->host_lock, flags);
688 goto start;
689 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300690 break;
691 case REQ_CLKS_OFF:
692 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
693 hba->clk_gating.state = CLKS_ON;
694 break;
695 }
696 /*
697 * If we here, it means gating work is either done or
698 * currently running. Hence, fall through to cancel gating
699 * work and to enable clocks.
700 */
701 case CLKS_OFF:
702 scsi_block_requests(hba->host);
703 hba->clk_gating.state = REQ_CLKS_ON;
704 schedule_work(&hba->clk_gating.ungate_work);
705 /*
706 * fall through to check if we should wait for this
707 * work to be done or not.
708 */
709 case REQ_CLKS_ON:
710 if (async) {
711 rc = -EAGAIN;
712 hba->clk_gating.active_reqs--;
713 break;
714 }
715
716 spin_unlock_irqrestore(hba->host->host_lock, flags);
717 flush_work(&hba->clk_gating.ungate_work);
718 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300719 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300720 goto start;
721 default:
722 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
723 __func__, hba->clk_gating.state);
724 break;
725 }
726 spin_unlock_irqrestore(hba->host->host_lock, flags);
727out:
728 return rc;
729}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200730EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300731
732static void ufshcd_gate_work(struct work_struct *work)
733{
734 struct ufs_hba *hba = container_of(work, struct ufs_hba,
735 clk_gating.gate_work.work);
736 unsigned long flags;
737
738 spin_lock_irqsave(hba->host->host_lock, flags);
739 if (hba->clk_gating.is_suspended) {
740 hba->clk_gating.state = CLKS_ON;
741 goto rel_lock;
742 }
743
744 if (hba->clk_gating.active_reqs
745 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
746 || hba->lrb_in_use || hba->outstanding_tasks
747 || hba->active_uic_cmd || hba->uic_async_done)
748 goto rel_lock;
749
750 spin_unlock_irqrestore(hba->host->host_lock, flags);
751
752 /* put the link into hibern8 mode before turning off clocks */
753 if (ufshcd_can_hibern8_during_gating(hba)) {
754 if (ufshcd_uic_hibern8_enter(hba)) {
755 hba->clk_gating.state = CLKS_ON;
756 goto out;
757 }
758 ufshcd_set_link_hibern8(hba);
759 }
760
Sahitya Tummala856b3482014-09-25 15:32:34 +0300761 if (ufshcd_is_clkscaling_enabled(hba)) {
762 devfreq_suspend_device(hba->devfreq);
763 hba->clk_scaling.window_start_t = 0;
764 }
765
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300766 if (!ufshcd_is_link_active(hba))
767 ufshcd_setup_clocks(hba, false);
768 else
769 /* If link is active, device ref_clk can't be switched off */
770 __ufshcd_setup_clocks(hba, false, true);
771
772 /*
773 * In case you are here to cancel this work the gating state
774 * would be marked as REQ_CLKS_ON. In this case keep the state
775 * as REQ_CLKS_ON which would anyway imply that clocks are off
776 * and a request to turn them on is pending. By doing this way,
777 * we keep the state machine in tact and this would ultimately
778 * prevent from doing cancel work multiple times when there are
779 * new requests arriving before the current cancel work is done.
780 */
781 spin_lock_irqsave(hba->host->host_lock, flags);
782 if (hba->clk_gating.state == REQ_CLKS_OFF)
783 hba->clk_gating.state = CLKS_OFF;
784
785rel_lock:
786 spin_unlock_irqrestore(hba->host->host_lock, flags);
787out:
788 return;
789}
790
791/* host lock must be held before calling this variant */
792static void __ufshcd_release(struct ufs_hba *hba)
793{
794 if (!ufshcd_is_clkgating_allowed(hba))
795 return;
796
797 hba->clk_gating.active_reqs--;
798
799 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
800 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
801 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200802 || hba->active_uic_cmd || hba->uic_async_done
803 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300804 return;
805
806 hba->clk_gating.state = REQ_CLKS_OFF;
807 schedule_delayed_work(&hba->clk_gating.gate_work,
808 msecs_to_jiffies(hba->clk_gating.delay_ms));
809}
810
811void ufshcd_release(struct ufs_hba *hba)
812{
813 unsigned long flags;
814
815 spin_lock_irqsave(hba->host->host_lock, flags);
816 __ufshcd_release(hba);
817 spin_unlock_irqrestore(hba->host->host_lock, flags);
818}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200819EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300820
821static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
823{
824 struct ufs_hba *hba = dev_get_drvdata(dev);
825
826 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
827}
828
829static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
830 struct device_attribute *attr, const char *buf, size_t count)
831{
832 struct ufs_hba *hba = dev_get_drvdata(dev);
833 unsigned long flags, value;
834
835 if (kstrtoul(buf, 0, &value))
836 return -EINVAL;
837
838 spin_lock_irqsave(hba->host->host_lock, flags);
839 hba->clk_gating.delay_ms = value;
840 spin_unlock_irqrestore(hba->host->host_lock, flags);
841 return count;
842}
843
844static void ufshcd_init_clk_gating(struct ufs_hba *hba)
845{
846 if (!ufshcd_is_clkgating_allowed(hba))
847 return;
848
849 hba->clk_gating.delay_ms = 150;
850 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
851 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
852
853 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
854 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
855 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
856 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
857 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
858 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
859 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
860}
861
862static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
863{
864 if (!ufshcd_is_clkgating_allowed(hba))
865 return;
866 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900867 cancel_work_sync(&hba->clk_gating.ungate_work);
868 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300869}
870
Sahitya Tummala856b3482014-09-25 15:32:34 +0300871/* Must be called with host lock acquired */
872static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
873{
874 if (!ufshcd_is_clkscaling_enabled(hba))
875 return;
876
877 if (!hba->clk_scaling.is_busy_started) {
878 hba->clk_scaling.busy_start_t = ktime_get();
879 hba->clk_scaling.is_busy_started = true;
880 }
881}
882
883static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
884{
885 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
886
887 if (!ufshcd_is_clkscaling_enabled(hba))
888 return;
889
890 if (!hba->outstanding_reqs && scaling->is_busy_started) {
891 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
892 scaling->busy_start_t));
893 scaling->busy_start_t = ktime_set(0, 0);
894 scaling->is_busy_started = false;
895 }
896}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530897/**
898 * ufshcd_send_command - Send SCSI or device management commands
899 * @hba: per adapter instance
900 * @task_tag: Task tag of the command
901 */
902static inline
903void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
904{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300905 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530906 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530907 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530908}
909
910/**
911 * ufshcd_copy_sense_data - Copy sense data in case of check condition
912 * @lrb - pointer to local reference block
913 */
914static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
915{
916 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530917 if (lrbp->sense_buffer &&
918 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardi675d9192016-10-17 17:09:24 -0700919 int len_to_copy;
920
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530921 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardi675d9192016-10-17 17:09:24 -0700922 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
923
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530924 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530925 lrbp->ucd_rsp_ptr->sr.sense_data,
Yaniv Gardi675d9192016-10-17 17:09:24 -0700926 min_t(int, len_to_copy, SCSI_SENSE_BUFFERSIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530927 }
928}
929
930/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530931 * ufshcd_copy_query_response() - Copy the Query Response and the data
932 * descriptor
933 * @hba: per adapter instance
934 * @lrb - pointer to local reference block
935 */
936static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300937int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530938{
939 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
940
Dolev Raviv68078d52013-07-30 00:35:58 +0530941 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530942
Dolev Raviv68078d52013-07-30 00:35:58 +0530943 /* Get the descriptor */
Avri Altman48c44d32019-05-21 11:24:22 +0300944 if (hba->dev_cmd.query.descriptor &&
945 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300946 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530947 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300948 u16 resp_len;
949 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530950
951 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300952 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530953 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300954 buf_len = be16_to_cpu(
955 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300956 if (likely(buf_len >= resp_len)) {
957 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
958 } else {
959 dev_warn(hba->dev,
960 "%s: Response size is bigger than buffer",
961 __func__);
962 return -EINVAL;
963 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530964 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300965
966 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530967}
968
969/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530970 * ufshcd_hba_capabilities - Read controller capabilities
971 * @hba: per adapter instance
972 */
973static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
974{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530975 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530976
977 /* nutrs and nutmrs are 0 based values */
978 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
979 hba->nutmrs =
980 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
981}
982
983/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530984 * ufshcd_ready_for_uic_cmd - Check if controller is ready
985 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530986 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530987 * Return true on success, else false
988 */
989static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
990{
991 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
992 return true;
993 else
994 return false;
995}
996
997/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530998 * ufshcd_get_upmcrs - Get the power mode change request status
999 * @hba: Pointer to adapter instance
1000 *
1001 * This function gets the UPMCRS field of HCS register
1002 * Returns value of UPMCRS field
1003 */
1004static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
1005{
1006 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
1007}
1008
1009/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301010 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1011 * @hba: per adapter instance
1012 * @uic_cmd: UIC command
1013 *
1014 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301015 */
1016static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301017ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301018{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301019 WARN_ON(hba->active_uic_cmd);
1020
1021 hba->active_uic_cmd = uic_cmd;
1022
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301023 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301024 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1025 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1026 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301027
1028 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301029 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301030 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301031}
1032
1033/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301034 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1035 * @hba: per adapter instance
1036 * @uic_command: UIC command
1037 *
1038 * Must be called with mutex held.
1039 * Returns 0 only if success.
1040 */
1041static int
1042ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1043{
1044 int ret;
1045 unsigned long flags;
1046
1047 if (wait_for_completion_timeout(&uic_cmd->done,
1048 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1049 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1050 else
1051 ret = -ETIMEDOUT;
1052
1053 spin_lock_irqsave(hba->host->host_lock, flags);
1054 hba->active_uic_cmd = NULL;
1055 spin_unlock_irqrestore(hba->host->host_lock, flags);
1056
1057 return ret;
1058}
1059
1060/**
1061 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1062 * @hba: per adapter instance
1063 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001064 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301065 *
1066 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001067 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301068 * Returns 0 only if success.
1069 */
1070static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001071__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1072 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301073{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301074 if (!ufshcd_ready_for_uic_cmd(hba)) {
1075 dev_err(hba->dev,
1076 "Controller not ready to accept UIC commands\n");
1077 return -EIO;
1078 }
1079
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001080 if (completion)
1081 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301082
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301083 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301084
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001085 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301086}
1087
1088/**
1089 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1090 * @hba: per adapter instance
1091 * @uic_cmd: UIC command
1092 *
1093 * Returns 0 only if success.
1094 */
1095static int
1096ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1097{
1098 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001099 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301100
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001101 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301102 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001103 ufshcd_add_delay_before_dme_cmd(hba);
1104
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001105 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001106 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001107 spin_unlock_irqrestore(hba->host->host_lock, flags);
1108 if (!ret)
1109 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1110
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301111 mutex_unlock(&hba->uic_cmd_mutex);
1112
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001113 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301114 return ret;
1115}
1116
1117/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301118 * ufshcd_map_sg - Map scatter-gather list to prdt
1119 * @lrbp - pointer to local reference block
1120 *
1121 * Returns 0 in case of success, non-zero value in case of failure
1122 */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001123static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301124{
1125 struct ufshcd_sg_entry *prd_table;
1126 struct scatterlist *sg;
1127 struct scsi_cmnd *cmd;
1128 int sg_segments;
1129 int i;
1130
1131 cmd = lrbp->cmd;
1132 sg_segments = scsi_dma_map(cmd);
1133 if (sg_segments < 0)
1134 return sg_segments;
1135
1136 if (sg_segments) {
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001137 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1138 lrbp->utr_descriptor_ptr->prd_table_length =
1139 cpu_to_le16((u16)(sg_segments *
1140 sizeof(struct ufshcd_sg_entry)));
1141 else
1142 lrbp->utr_descriptor_ptr->prd_table_length =
1143 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301144
1145 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1146
1147 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1148 prd_table[i].size =
1149 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1150 prd_table[i].base_addr =
1151 cpu_to_le32(lower_32_bits(sg->dma_address));
1152 prd_table[i].upper_addr =
1153 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001154 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301155 }
1156 } else {
1157 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1158 }
1159
1160 return 0;
1161}
1162
1163/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301164 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301165 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301166 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301167 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301168static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301169{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301170 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1171
1172 if (hba->ufs_version == UFSHCI_VERSION_10) {
1173 u32 rw;
1174 rw = set & INTERRUPT_MASK_RW_VER_10;
1175 set = rw | ((set ^ intrs) & intrs);
1176 } else {
1177 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301178 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301179
1180 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1181}
1182
1183/**
1184 * ufshcd_disable_intr - disable interrupts
1185 * @hba: per adapter instance
1186 * @intrs: interrupt bits
1187 */
1188static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1189{
1190 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1191
1192 if (hba->ufs_version == UFSHCI_VERSION_10) {
1193 u32 rw;
1194 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1195 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1196 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1197
1198 } else {
1199 set &= ~intrs;
1200 }
1201
1202 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301203}
1204
1205/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301206 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1207 * descriptor according to request
1208 * @lrbp: pointer to local reference block
1209 * @upiu_flags: flags required in the header
1210 * @cmd_dir: requests data direction
1211 */
1212static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001213 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301214{
1215 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1216 u32 data_direction;
1217 u32 dword_0;
1218
1219 if (cmd_dir == DMA_FROM_DEVICE) {
1220 data_direction = UTP_DEVICE_TO_HOST;
1221 *upiu_flags = UPIU_CMD_FLAGS_READ;
1222 } else if (cmd_dir == DMA_TO_DEVICE) {
1223 data_direction = UTP_HOST_TO_DEVICE;
1224 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1225 } else {
1226 data_direction = UTP_NO_DATA_TRANSFER;
1227 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1228 }
1229
1230 dword_0 = data_direction | (lrbp->command_type
1231 << UPIU_COMMAND_TYPE_OFFSET);
1232 if (lrbp->intr_cmd)
1233 dword_0 |= UTP_REQ_DESC_INT_CMD;
1234
1235 /* Transfer request descriptor header fields */
1236 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001237 /* dword_1 is reserved, hence it is set to 0 */
1238 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301239 /*
1240 * assigning invalid value for command status. Controller
1241 * updates OCS on command completion, with the command
1242 * status
1243 */
1244 req_desc->header.dword_2 =
1245 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001246 /* dword_3 is reserved, hence it is set to 0 */
1247 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001248
1249 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301250}
1251
1252/**
1253 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1254 * for scsi commands
1255 * @lrbp - local reference block pointer
1256 * @upiu_flags - flags
1257 */
1258static
1259void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1260{
1261 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001262 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301263
1264 /* command descriptor fields */
1265 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1266 UPIU_TRANSACTION_COMMAND, upiu_flags,
1267 lrbp->lun, lrbp->task_tag);
1268 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1269 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1270
1271 /* Total EHS length and Data segment length will be zero */
1272 ucd_req_ptr->header.dword_2 = 0;
1273
1274 ucd_req_ptr->sc.exp_data_transfer_len =
1275 cpu_to_be32(lrbp->cmd->sdb.length);
1276
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001277 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1278 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1279 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1280
1281 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301282}
1283
Dolev Raviv68078d52013-07-30 00:35:58 +05301284/**
1285 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1286 * for query requsts
1287 * @hba: UFS hba
1288 * @lrbp: local reference block pointer
1289 * @upiu_flags: flags
1290 */
1291static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1292 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1293{
1294 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1295 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301296 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301297 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1298
1299 /* Query request header */
1300 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1301 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1302 lrbp->lun, lrbp->task_tag);
1303 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1304 0, query->request.query_func, 0, 0);
1305
Zang Leigang68612852016-08-25 17:39:19 +08001306 /* Data segment length only need for WRITE_DESC */
1307 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1308 ucd_req_ptr->header.dword_2 =
1309 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1310 else
1311 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301312
1313 /* Copy the Query Request buffer as is */
1314 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1315 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301316
1317 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001318 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1319 memcpy(descp, query->descriptor, len);
1320
Yaniv Gardi51047262016-02-01 15:02:38 +02001321 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301322}
1323
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301324static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1325{
1326 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1327
1328 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1329
1330 /* command descriptor fields */
1331 ucd_req_ptr->header.dword_0 =
1332 UPIU_HEADER_DWORD(
1333 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001334 /* clear rest of the fields of basic header */
1335 ucd_req_ptr->header.dword_1 = 0;
1336 ucd_req_ptr->header.dword_2 = 0;
1337
1338 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301339}
1340
1341/**
Joao Pinto300bb132016-05-11 12:21:27 +01001342 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1343 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301344 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301345 * @lrb - pointer to local reference block
1346 */
Joao Pinto300bb132016-05-11 12:21:27 +01001347static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301348{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301349 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301350 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301351
kehuanlin321c5ad2017-09-06 17:58:39 +08001352 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
1353 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01001354 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
kehuanlin321c5ad2017-09-06 17:58:39 +08001355 else
1356 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01001357
1358 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1359 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1360 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1361 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1362 ufshcd_prepare_utp_nop_upiu(lrbp);
1363 else
1364 ret = -EINVAL;
1365
1366 return ret;
1367}
1368
1369/**
1370 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1371 * for SCSI Purposes
1372 * @hba - per adapter instance
1373 * @lrb - pointer to local reference block
1374 */
1375static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1376{
1377 u32 upiu_flags;
1378 int ret = 0;
1379
kehuanlin321c5ad2017-09-06 17:58:39 +08001380 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
1381 (hba->ufs_version == UFSHCI_VERSION_11))
Joao Pinto300bb132016-05-11 12:21:27 +01001382 lrbp->command_type = UTP_CMD_TYPE_SCSI;
kehuanlin321c5ad2017-09-06 17:58:39 +08001383 else
1384 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
Joao Pinto300bb132016-05-11 12:21:27 +01001385
1386 if (likely(lrbp->cmd)) {
1387 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1388 lrbp->cmd->sc_data_direction);
1389 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1390 } else {
1391 ret = -EINVAL;
1392 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301393
1394 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301395}
1396
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001397/*
1398 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1399 * @scsi_lun: scsi LUN id
1400 *
1401 * Returns UPIU LUN id
1402 */
1403static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1404{
1405 if (scsi_is_wlun(scsi_lun))
1406 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1407 | UFS_UPIU_WLUN_ID;
1408 else
1409 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1410}
1411
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301412/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001413 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1414 * @scsi_lun: UPIU W-LUN id
1415 *
1416 * Returns SCSI W-LUN id
1417 */
1418static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1419{
1420 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1421}
1422
1423/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301424 * ufshcd_queuecommand - main entry point for SCSI requests
1425 * @cmd: command from SCSI Midlayer
1426 * @done: call back function
1427 *
1428 * Returns 0 for success, non-zero in case of failure
1429 */
1430static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1431{
1432 struct ufshcd_lrb *lrbp;
1433 struct ufs_hba *hba;
1434 unsigned long flags;
1435 int tag;
1436 int err = 0;
1437
1438 hba = shost_priv(host);
1439
1440 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001441 if (!ufshcd_valid_tag(hba, tag)) {
1442 dev_err(hba->dev,
1443 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1444 __func__, tag, cmd, cmd->request);
1445 BUG();
1446 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301447
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301448 spin_lock_irqsave(hba->host->host_lock, flags);
1449 switch (hba->ufshcd_state) {
1450 case UFSHCD_STATE_OPERATIONAL:
1451 break;
Zang Leiganga17bddc2017-04-04 19:32:20 +00001452 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301453 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301454 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301455 goto out_unlock;
1456 case UFSHCD_STATE_ERROR:
1457 set_host_byte(cmd, DID_ERROR);
1458 cmd->scsi_done(cmd);
1459 goto out_unlock;
1460 default:
1461 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1462 __func__, hba->ufshcd_state);
1463 set_host_byte(cmd, DID_BAD_TARGET);
1464 cmd->scsi_done(cmd);
1465 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301466 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001467
1468 /* if error handling is in progress, don't issue commands */
1469 if (ufshcd_eh_in_progress(hba)) {
1470 set_host_byte(cmd, DID_ERROR);
1471 cmd->scsi_done(cmd);
1472 goto out_unlock;
1473 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301474 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301475
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301476 /* acquire the tag to make sure device cmds don't use it */
1477 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1478 /*
1479 * Dev manage command in progress, requeue the command.
1480 * Requeuing the command helps in cases where the request *may*
1481 * find different tag instead of waiting for dev manage command
1482 * completion.
1483 */
1484 err = SCSI_MLQUEUE_HOST_BUSY;
1485 goto out;
1486 }
1487
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001488 err = ufshcd_hold(hba, true);
1489 if (err) {
1490 err = SCSI_MLQUEUE_HOST_BUSY;
1491 clear_bit_unlock(tag, &hba->lrb_in_use);
1492 goto out;
1493 }
1494 WARN_ON(hba->clk_gating.state != CLKS_ON);
1495
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301496 lrbp = &hba->lrb[tag];
1497
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301498 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301499 lrbp->cmd = cmd;
1500 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1501 lrbp->sense_buffer = cmd->sense_buffer;
1502 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001503 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001504 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301505
Joao Pinto300bb132016-05-11 12:21:27 +01001506 ufshcd_comp_scsi_upiu(hba, lrbp);
1507
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001508 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301509 if (err) {
1510 lrbp->cmd = NULL;
1511 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301512 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301513 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301514
1515 /* issue command to the controller */
1516 spin_lock_irqsave(hba->host->host_lock, flags);
1517 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301518out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301519 spin_unlock_irqrestore(hba->host->host_lock, flags);
1520out:
1521 return err;
1522}
1523
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301524static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1525 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1526{
1527 lrbp->cmd = NULL;
1528 lrbp->sense_bufflen = 0;
1529 lrbp->sense_buffer = NULL;
1530 lrbp->task_tag = tag;
1531 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301532 lrbp->intr_cmd = true; /* No interrupt aggregation */
1533 hba->dev_cmd.type = cmd_type;
1534
Joao Pinto300bb132016-05-11 12:21:27 +01001535 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301536}
1537
1538static int
1539ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1540{
1541 int err = 0;
1542 unsigned long flags;
1543 u32 mask = 1 << tag;
1544
1545 /* clear outstanding transaction before retry */
1546 spin_lock_irqsave(hba->host->host_lock, flags);
1547 ufshcd_utrl_clear(hba, tag);
1548 spin_unlock_irqrestore(hba->host->host_lock, flags);
1549
1550 /*
1551 * wait for for h/w to clear corresponding bit in door-bell.
1552 * max. wait is 1 sec.
1553 */
1554 err = ufshcd_wait_for_register(hba,
1555 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001556 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301557
1558 return err;
1559}
1560
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001561static int
1562ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1563{
1564 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1565
1566 /* Get the UPIU response */
1567 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1568 UPIU_RSP_CODE_OFFSET;
1569 return query_res->response;
1570}
1571
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301572/**
1573 * ufshcd_dev_cmd_completion() - handles device management command responses
1574 * @hba: per adapter instance
1575 * @lrbp: pointer to local reference block
1576 */
1577static int
1578ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1579{
1580 int resp;
1581 int err = 0;
1582
1583 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1584
1585 switch (resp) {
1586 case UPIU_TRANSACTION_NOP_IN:
1587 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1588 err = -EINVAL;
1589 dev_err(hba->dev, "%s: unexpected response %x\n",
1590 __func__, resp);
1591 }
1592 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301593 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001594 err = ufshcd_check_query_response(hba, lrbp);
1595 if (!err)
1596 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301597 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301598 case UPIU_TRANSACTION_REJECT_UPIU:
1599 /* TODO: handle Reject UPIU Response */
1600 err = -EPERM;
1601 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1602 __func__);
1603 break;
1604 default:
1605 err = -EINVAL;
1606 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1607 __func__, resp);
1608 break;
1609 }
1610
1611 return err;
1612}
1613
1614static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1615 struct ufshcd_lrb *lrbp, int max_timeout)
1616{
1617 int err = 0;
1618 unsigned long time_left;
1619 unsigned long flags;
1620
1621 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1622 msecs_to_jiffies(max_timeout));
1623
1624 spin_lock_irqsave(hba->host->host_lock, flags);
1625 hba->dev_cmd.complete = NULL;
1626 if (likely(time_left)) {
1627 err = ufshcd_get_tr_ocs(lrbp);
1628 if (!err)
1629 err = ufshcd_dev_cmd_completion(hba, lrbp);
1630 }
1631 spin_unlock_irqrestore(hba->host->host_lock, flags);
1632
1633 if (!time_left) {
1634 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001635 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1636 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301637 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001638 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301639 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001640 /*
1641 * in case of an error, after clearing the doorbell,
1642 * we also need to clear the outstanding_request
1643 * field in hba
1644 */
1645 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301646 }
1647
1648 return err;
1649}
1650
1651/**
1652 * ufshcd_get_dev_cmd_tag - Get device management command tag
1653 * @hba: per-adapter instance
1654 * @tag: pointer to variable with available slot value
1655 *
1656 * Get a free slot and lock it until device management command
1657 * completes.
1658 *
1659 * Returns false if free slot is unavailable for locking, else
1660 * return true with tag value in @tag.
1661 */
1662static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1663{
1664 int tag;
1665 bool ret = false;
1666 unsigned long tmp;
1667
1668 if (!tag_out)
1669 goto out;
1670
1671 do {
1672 tmp = ~hba->lrb_in_use;
1673 tag = find_last_bit(&tmp, hba->nutrs);
1674 if (tag >= hba->nutrs)
1675 goto out;
1676 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1677
1678 *tag_out = tag;
1679 ret = true;
1680out:
1681 return ret;
1682}
1683
1684static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1685{
1686 clear_bit_unlock(tag, &hba->lrb_in_use);
1687}
1688
1689/**
1690 * ufshcd_exec_dev_cmd - API for sending device management requests
1691 * @hba - UFS hba
1692 * @cmd_type - specifies the type (NOP, Query...)
1693 * @timeout - time in seconds
1694 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301695 * NOTE: Since there is only one available tag for device management commands,
1696 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301697 */
1698static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1699 enum dev_cmd_type cmd_type, int timeout)
1700{
1701 struct ufshcd_lrb *lrbp;
1702 int err;
1703 int tag;
1704 struct completion wait;
1705 unsigned long flags;
1706
1707 /*
1708 * Get free slot, sleep if slots are unavailable.
1709 * Even though we use wait_event() which sleeps indefinitely,
1710 * the maximum wait time is bounded by SCSI request timeout.
1711 */
1712 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1713
1714 init_completion(&wait);
1715 lrbp = &hba->lrb[tag];
1716 WARN_ON(lrbp->cmd);
1717 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1718 if (unlikely(err))
1719 goto out_put_tag;
1720
1721 hba->dev_cmd.complete = &wait;
1722
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001723 /* Make sure descriptors are ready before ringing the doorbell */
1724 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301725 spin_lock_irqsave(hba->host->host_lock, flags);
1726 ufshcd_send_command(hba, tag);
1727 spin_unlock_irqrestore(hba->host->host_lock, flags);
1728
1729 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1730
1731out_put_tag:
1732 ufshcd_put_dev_cmd_tag(hba, tag);
1733 wake_up(&hba->dev_cmd.tag_wq);
1734 return err;
1735}
1736
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301737/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001738 * ufshcd_init_query() - init the query response and request parameters
1739 * @hba: per-adapter instance
1740 * @request: address of the request pointer to be initialized
1741 * @response: address of the response pointer to be initialized
1742 * @opcode: operation to perform
1743 * @idn: flag idn to access
1744 * @index: LU number to access
1745 * @selector: query/flag/descriptor further identification
1746 */
1747static inline void ufshcd_init_query(struct ufs_hba *hba,
1748 struct ufs_query_req **request, struct ufs_query_res **response,
1749 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1750{
1751 *request = &hba->dev_cmd.query.request;
1752 *response = &hba->dev_cmd.query.response;
1753 memset(*request, 0, sizeof(struct ufs_query_req));
1754 memset(*response, 0, sizeof(struct ufs_query_res));
1755 (*request)->upiu_req.opcode = opcode;
1756 (*request)->upiu_req.idn = idn;
1757 (*request)->upiu_req.index = index;
1758 (*request)->upiu_req.selector = selector;
1759}
1760
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001761static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1762 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1763{
1764 int ret;
1765 int retries;
1766
1767 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1768 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1769 if (ret)
1770 dev_dbg(hba->dev,
1771 "%s: failed with error %d, retries %d\n",
1772 __func__, ret, retries);
1773 else
1774 break;
1775 }
1776
1777 if (ret)
1778 dev_err(hba->dev,
1779 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1780 __func__, opcode, idn, ret, retries);
1781 return ret;
1782}
1783
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001784/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301785 * ufshcd_query_flag() - API function for sending flag query requests
1786 * hba: per-adapter instance
1787 * query_opcode: flag query to perform
1788 * idn: flag idn to access
1789 * flag_res: the flag value after the query request completes
1790 *
1791 * Returns 0 for success, non-zero in case of failure
1792 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001793int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301794 enum flag_idn idn, bool *flag_res)
1795{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001796 struct ufs_query_req *request = NULL;
1797 struct ufs_query_res *response = NULL;
1798 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001799 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301800
1801 BUG_ON(!hba);
1802
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001803 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301804 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001805 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1806 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301807
1808 switch (opcode) {
1809 case UPIU_QUERY_OPCODE_SET_FLAG:
1810 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1811 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1812 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1813 break;
1814 case UPIU_QUERY_OPCODE_READ_FLAG:
1815 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1816 if (!flag_res) {
1817 /* No dummy reads */
1818 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1819 __func__);
1820 err = -EINVAL;
1821 goto out_unlock;
1822 }
1823 break;
1824 default:
1825 dev_err(hba->dev,
1826 "%s: Expected query flag opcode but got = %d\n",
1827 __func__, opcode);
1828 err = -EINVAL;
1829 goto out_unlock;
1830 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301831
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001832 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1833 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1834
1835 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301836
1837 if (err) {
1838 dev_err(hba->dev,
1839 "%s: Sending flag query for idn %d failed, err = %d\n",
1840 __func__, idn, err);
1841 goto out_unlock;
1842 }
1843
1844 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301845 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301846 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1847
1848out_unlock:
1849 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001850 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301851 return err;
1852}
1853
1854/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301855 * ufshcd_query_attr - API function for sending attribute requests
1856 * hba: per-adapter instance
1857 * opcode: attribute opcode
1858 * idn: attribute idn to access
1859 * index: index field
1860 * selector: selector field
1861 * attr_val: the attribute value after the query request completes
1862 *
1863 * Returns 0 for success, non-zero in case of failure
1864*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301865static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301866 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1867{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001868 struct ufs_query_req *request = NULL;
1869 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301870 int err;
1871
1872 BUG_ON(!hba);
1873
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001874 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301875 if (!attr_val) {
1876 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1877 __func__, opcode);
1878 err = -EINVAL;
1879 goto out;
1880 }
1881
1882 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001883 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1884 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301885
1886 switch (opcode) {
1887 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1888 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301889 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301890 break;
1891 case UPIU_QUERY_OPCODE_READ_ATTR:
1892 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1893 break;
1894 default:
1895 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1896 __func__, opcode);
1897 err = -EINVAL;
1898 goto out_unlock;
1899 }
1900
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001901 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301902
1903 if (err) {
1904 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1905 __func__, opcode, idn, err);
1906 goto out_unlock;
1907 }
1908
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301909 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301910
1911out_unlock:
1912 mutex_unlock(&hba->dev_cmd.lock);
1913out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001914 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301915 return err;
1916}
1917
1918/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001919 * ufshcd_query_attr_retry() - API function for sending query
1920 * attribute with retries
1921 * @hba: per-adapter instance
1922 * @opcode: attribute opcode
1923 * @idn: attribute idn to access
1924 * @index: index field
1925 * @selector: selector field
1926 * @attr_val: the attribute value after the query request
1927 * completes
1928 *
1929 * Returns 0 for success, non-zero in case of failure
1930*/
1931static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1932 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1933 u32 *attr_val)
1934{
1935 int ret = 0;
1936 u32 retries;
1937
1938 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1939 ret = ufshcd_query_attr(hba, opcode, idn, index,
1940 selector, attr_val);
1941 if (ret)
1942 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1943 __func__, ret, retries);
1944 else
1945 break;
1946 }
1947
1948 if (ret)
1949 dev_err(hba->dev,
1950 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1951 __func__, idn, ret, QUERY_REQ_RETRIES);
1952 return ret;
1953}
1954
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001955static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001956 enum query_opcode opcode, enum desc_idn idn, u8 index,
1957 u8 selector, u8 *desc_buf, int *buf_len)
1958{
1959 struct ufs_query_req *request = NULL;
1960 struct ufs_query_res *response = NULL;
1961 int err;
1962
1963 BUG_ON(!hba);
1964
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001965 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001966 if (!desc_buf) {
1967 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1968 __func__, opcode);
1969 err = -EINVAL;
1970 goto out;
1971 }
1972
Potomski, MichalXe1928452017-02-23 09:05:30 +00001973 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001974 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1975 __func__, *buf_len);
1976 err = -EINVAL;
1977 goto out;
1978 }
1979
1980 mutex_lock(&hba->dev_cmd.lock);
1981 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1982 selector);
1983 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001984 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001985
1986 switch (opcode) {
1987 case UPIU_QUERY_OPCODE_WRITE_DESC:
1988 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1989 break;
1990 case UPIU_QUERY_OPCODE_READ_DESC:
1991 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1992 break;
1993 default:
1994 dev_err(hba->dev,
1995 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1996 __func__, opcode);
1997 err = -EINVAL;
1998 goto out_unlock;
1999 }
2000
2001 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2002
2003 if (err) {
2004 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
2005 __func__, opcode, idn, err);
2006 goto out_unlock;
2007 }
2008
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002009 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002010
2011out_unlock:
Bean Huo6ebc4342019-11-12 23:34:36 +01002012 hba->dev_cmd.query.descriptor = NULL;
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002013 mutex_unlock(&hba->dev_cmd.lock);
2014out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002015 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002016 return err;
2017}
2018
2019/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002020 * ufshcd_query_descriptor_retry - API function for sending descriptor
2021 * requests
2022 * hba: per-adapter instance
2023 * opcode: attribute opcode
2024 * idn: attribute idn to access
2025 * index: index field
2026 * selector: selector field
2027 * desc_buf: the buffer that contains the descriptor
2028 * buf_len: length parameter passed to the device
2029 *
2030 * Returns 0 for success, non-zero in case of failure.
2031 * The buf_len parameter will contain, on return, the length parameter
2032 * received on the response.
2033 */
2034int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2035 enum query_opcode opcode, enum desc_idn idn, u8 index,
2036 u8 selector, u8 *desc_buf, int *buf_len)
2037{
2038 int err;
2039 int retries;
2040
2041 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2042 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2043 selector, desc_buf, buf_len);
2044 if (!err || err == -EINVAL)
2045 break;
2046 }
2047
2048 return err;
2049}
2050EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2051
2052/**
Potomski, MichalXe1928452017-02-23 09:05:30 +00002053 * ufshcd_read_desc_length - read the specified descriptor length from header
2054 * @hba: Pointer to adapter instance
2055 * @desc_id: descriptor idn value
2056 * @desc_index: descriptor index
2057 * @desc_length: pointer to variable to read the length of descriptor
2058 *
2059 * Return 0 in case of success, non-zero otherwise
2060 */
2061static int ufshcd_read_desc_length(struct ufs_hba *hba,
2062 enum desc_idn desc_id,
2063 int desc_index,
2064 int *desc_length)
2065{
2066 int ret;
2067 u8 header[QUERY_DESC_HDR_SIZE];
2068 int header_len = QUERY_DESC_HDR_SIZE;
2069
2070 if (desc_id >= QUERY_DESC_IDN_MAX)
2071 return -EINVAL;
2072
2073 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2074 desc_id, desc_index, 0, header,
2075 &header_len);
2076
2077 if (ret) {
2078 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2079 __func__, desc_id);
2080 return ret;
2081 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
2082 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
2083 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
2084 desc_id);
2085 ret = -EINVAL;
2086 }
2087
2088 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
2089 return ret;
2090
2091}
2092
2093/**
2094 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
2095 * @hba: Pointer to adapter instance
2096 * @desc_id: descriptor idn value
2097 * @desc_len: mapped desc length (out)
2098 *
2099 * Return 0 in case of success, non-zero otherwise
2100 */
2101int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
2102 enum desc_idn desc_id, int *desc_len)
2103{
2104 switch (desc_id) {
2105 case QUERY_DESC_IDN_DEVICE:
2106 *desc_len = hba->desc_size.dev_desc;
2107 break;
2108 case QUERY_DESC_IDN_POWER:
2109 *desc_len = hba->desc_size.pwr_desc;
2110 break;
2111 case QUERY_DESC_IDN_GEOMETRY:
2112 *desc_len = hba->desc_size.geom_desc;
2113 break;
2114 case QUERY_DESC_IDN_CONFIGURATION:
2115 *desc_len = hba->desc_size.conf_desc;
2116 break;
2117 case QUERY_DESC_IDN_UNIT:
2118 *desc_len = hba->desc_size.unit_desc;
2119 break;
2120 case QUERY_DESC_IDN_INTERCONNECT:
2121 *desc_len = hba->desc_size.interc_desc;
2122 break;
2123 case QUERY_DESC_IDN_STRING:
2124 *desc_len = QUERY_DESC_MAX_SIZE;
2125 break;
2126 case QUERY_DESC_IDN_RFU_0:
2127 case QUERY_DESC_IDN_RFU_1:
2128 *desc_len = 0;
2129 break;
2130 default:
2131 *desc_len = 0;
2132 return -EINVAL;
2133 }
2134 return 0;
2135}
2136EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
2137
2138/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002139 * ufshcd_read_desc_param - read the specified descriptor parameter
2140 * @hba: Pointer to adapter instance
2141 * @desc_id: descriptor idn value
2142 * @desc_index: descriptor index
2143 * @param_offset: offset of the parameter to read
2144 * @param_read_buf: pointer to buffer where parameter would be read
2145 * @param_size: sizeof(param_read_buf)
2146 *
2147 * Return 0 in case of success, non-zero otherwise
2148 */
2149static int ufshcd_read_desc_param(struct ufs_hba *hba,
2150 enum desc_idn desc_id,
2151 int desc_index,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002152 u8 param_offset,
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002153 u8 *param_read_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002154 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002155{
2156 int ret;
2157 u8 *desc_buf;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002158 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002159 bool is_kmalloc = true;
2160
Potomski, MichalXe1928452017-02-23 09:05:30 +00002161 /* Safety check */
2162 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002163 return -EINVAL;
2164
Potomski, MichalXe1928452017-02-23 09:05:30 +00002165 /* Get the max length of descriptor from structure filled up at probe
2166 * time.
2167 */
2168 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002169
Potomski, MichalXe1928452017-02-23 09:05:30 +00002170 /* Sanity checks */
2171 if (ret || !buff_len) {
2172 dev_err(hba->dev, "%s: Failed to get full descriptor length",
2173 __func__);
2174 return ret;
2175 }
2176
2177 /* Check whether we need temp memory */
2178 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002179 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2180 if (!desc_buf)
2181 return -ENOMEM;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002182 } else {
2183 desc_buf = param_read_buf;
2184 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002185 }
2186
Potomski, MichalXe1928452017-02-23 09:05:30 +00002187 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002188 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002189 desc_id, desc_index, 0,
2190 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002191
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002192 if (ret) {
2193 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2194 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002195 goto out;
2196 }
2197
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002198 /* Sanity check */
2199 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2200 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2201 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2202 ret = -EINVAL;
2203 goto out;
2204 }
2205
Potomski, MichalXe1928452017-02-23 09:05:30 +00002206 /* Check wherher we will not copy more data, than available */
2207 if (is_kmalloc && param_size > buff_len)
2208 param_size = buff_len;
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002209
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002210 if (is_kmalloc)
2211 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2212out:
2213 if (is_kmalloc)
2214 kfree(desc_buf);
2215 return ret;
2216}
2217
2218static inline int ufshcd_read_desc(struct ufs_hba *hba,
2219 enum desc_idn desc_id,
2220 int desc_index,
2221 u8 *buf,
2222 u32 size)
2223{
2224 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2225}
2226
2227static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2228 u8 *buf,
2229 u32 size)
2230{
2231 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2232}
2233
Yaniv Gardib573d482016-03-10 17:37:09 +02002234int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2235{
2236 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2237}
2238EXPORT_SYMBOL(ufshcd_read_device_desc);
2239
2240/**
2241 * ufshcd_read_string_desc - read string descriptor
2242 * @hba: pointer to adapter instance
2243 * @desc_index: descriptor index
2244 * @buf: pointer to buffer where descriptor would be read
2245 * @size: size of buf
2246 * @ascii: if true convert from unicode to ascii characters
2247 *
2248 * Return 0 in case of success, non-zero otherwise
2249 */
2250int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2251 u32 size, bool ascii)
2252{
2253 int err = 0;
2254
2255 err = ufshcd_read_desc(hba,
2256 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2257
2258 if (err) {
2259 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2260 __func__, QUERY_REQ_RETRIES, err);
2261 goto out;
2262 }
2263
2264 if (ascii) {
2265 int desc_len;
2266 int ascii_len;
2267 int i;
2268 char *buff_ascii;
2269
2270 desc_len = buf[0];
2271 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2272 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2273 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2274 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2275 __func__);
2276 err = -ENOMEM;
2277 goto out;
2278 }
2279
2280 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2281 if (!buff_ascii) {
2282 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002283 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002284 }
2285
2286 /*
2287 * the descriptor contains string in UTF16 format
2288 * we need to convert to utf-8 so it can be displayed
2289 */
2290 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2291 desc_len - QUERY_DESC_HDR_SIZE,
2292 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2293
2294 /* replace non-printable or non-ASCII characters with spaces */
2295 for (i = 0; i < ascii_len; i++)
2296 ufshcd_remove_non_printable(&buff_ascii[i]);
2297
2298 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2299 size - QUERY_DESC_HDR_SIZE);
2300 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2301 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002302 kfree(buff_ascii);
2303 }
2304out:
2305 return err;
2306}
2307EXPORT_SYMBOL(ufshcd_read_string_desc);
2308
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002309/**
2310 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2311 * @hba: Pointer to adapter instance
2312 * @lun: lun id
2313 * @param_offset: offset of the parameter to read
2314 * @param_read_buf: pointer to buffer where parameter would be read
2315 * @param_size: sizeof(param_read_buf)
2316 *
2317 * Return 0 in case of success, non-zero otherwise
2318 */
2319static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2320 int lun,
2321 enum unit_desc_param param_offset,
2322 u8 *param_read_buf,
2323 u32 param_size)
2324{
2325 /*
2326 * Unit descriptors are only available for general purpose LUs (LUN id
2327 * from 0 to 7) and RPMB Well known LU.
2328 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002329 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002330 return -EOPNOTSUPP;
2331
2332 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2333 param_offset, param_read_buf, param_size);
2334}
2335
2336/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302337 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2338 * @hba: per adapter instance
2339 *
2340 * 1. Allocate DMA memory for Command Descriptor array
2341 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2342 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2343 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2344 * (UTMRDL)
2345 * 4. Allocate memory for local reference block(lrb).
2346 *
2347 * Returns 0 for success, non-zero in case of failure
2348 */
2349static int ufshcd_memory_alloc(struct ufs_hba *hba)
2350{
2351 size_t utmrdl_size, utrdl_size, ucdl_size;
2352
2353 /* Allocate memory for UTP command descriptors */
2354 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002355 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2356 ucdl_size,
2357 &hba->ucdl_dma_addr,
2358 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302359
2360 /*
2361 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2362 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2363 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2364 * be aligned to 128 bytes as well
2365 */
2366 if (!hba->ucdl_base_addr ||
2367 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302368 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302369 "Command Descriptor Memory allocation failed\n");
2370 goto out;
2371 }
2372
2373 /*
2374 * Allocate memory for UTP Transfer descriptors
2375 * UFSHCI requires 1024 byte alignment of UTRD
2376 */
2377 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002378 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2379 utrdl_size,
2380 &hba->utrdl_dma_addr,
2381 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302382 if (!hba->utrdl_base_addr ||
2383 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302384 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302385 "Transfer Descriptor Memory allocation failed\n");
2386 goto out;
2387 }
2388
2389 /*
2390 * Allocate memory for UTP Task Management descriptors
2391 * UFSHCI requires 1024 byte alignment of UTMRD
2392 */
2393 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002394 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2395 utmrdl_size,
2396 &hba->utmrdl_dma_addr,
2397 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302398 if (!hba->utmrdl_base_addr ||
2399 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302400 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302401 "Task Management Descriptor Memory allocation failed\n");
2402 goto out;
2403 }
2404
2405 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002406 hba->lrb = devm_kzalloc(hba->dev,
2407 hba->nutrs * sizeof(struct ufshcd_lrb),
2408 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302409 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302410 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302411 goto out;
2412 }
2413 return 0;
2414out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302415 return -ENOMEM;
2416}
2417
2418/**
2419 * ufshcd_host_memory_configure - configure local reference block with
2420 * memory offsets
2421 * @hba: per adapter instance
2422 *
2423 * Configure Host memory space
2424 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2425 * address.
2426 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2427 * and PRDT offset.
2428 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2429 * into local reference block.
2430 */
2431static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2432{
2433 struct utp_transfer_cmd_desc *cmd_descp;
2434 struct utp_transfer_req_desc *utrdlp;
2435 dma_addr_t cmd_desc_dma_addr;
2436 dma_addr_t cmd_desc_element_addr;
2437 u16 response_offset;
2438 u16 prdt_offset;
2439 int cmd_desc_size;
2440 int i;
2441
2442 utrdlp = hba->utrdl_base_addr;
2443 cmd_descp = hba->ucdl_base_addr;
2444
2445 response_offset =
2446 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2447 prdt_offset =
2448 offsetof(struct utp_transfer_cmd_desc, prd_table);
2449
2450 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2451 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2452
2453 for (i = 0; i < hba->nutrs; i++) {
2454 /* Configure UTRD with command descriptor base address */
2455 cmd_desc_element_addr =
2456 (cmd_desc_dma_addr + (cmd_desc_size * i));
2457 utrdlp[i].command_desc_base_addr_lo =
2458 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2459 utrdlp[i].command_desc_base_addr_hi =
2460 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2461
2462 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002463 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2464 utrdlp[i].response_upiu_offset =
2465 cpu_to_le16(response_offset);
2466 utrdlp[i].prd_table_offset =
2467 cpu_to_le16(prdt_offset);
2468 utrdlp[i].response_upiu_length =
2469 cpu_to_le16(ALIGNED_UPIU_SIZE);
2470 } else {
2471 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302472 cpu_to_le16((response_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002473 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302474 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002475 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302476 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002477 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302478
2479 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302480 hba->lrb[i].ucd_req_ptr =
2481 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302482 hba->lrb[i].ucd_rsp_ptr =
2483 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2484 hba->lrb[i].ucd_prdt_ptr =
2485 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2486 }
2487}
2488
2489/**
2490 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2491 * @hba: per adapter instance
2492 *
2493 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2494 * in order to initialize the Unipro link startup procedure.
2495 * Once the Unipro links are up, the device connected to the controller
2496 * is detected.
2497 *
2498 * Returns 0 on success, non-zero value on failure
2499 */
2500static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2501{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302502 struct uic_command uic_cmd = {0};
2503 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302504
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302505 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2506
2507 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2508 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302509 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302510 "dme-link-startup: error code %d\n", ret);
2511 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302512}
2513
Yaniv Gardicad2e032015-03-31 17:37:14 +03002514static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2515{
2516 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2517 unsigned long min_sleep_time_us;
2518
2519 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2520 return;
2521
2522 /*
2523 * last_dme_cmd_tstamp will be 0 only for 1st call to
2524 * this function
2525 */
2526 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2527 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2528 } else {
2529 unsigned long delta =
2530 (unsigned long) ktime_to_us(
2531 ktime_sub(ktime_get(),
2532 hba->last_dme_cmd_tstamp));
2533
2534 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2535 min_sleep_time_us =
2536 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2537 else
2538 return; /* no more delay required */
2539 }
2540
2541 /* allow sleep for extra 50us if needed */
2542 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2543}
2544
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302545/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302546 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2547 * @hba: per adapter instance
2548 * @attr_sel: uic command argument1
2549 * @attr_set: attribute set type as uic command argument2
2550 * @mib_val: setting value as uic command argument3
2551 * @peer: indicate whether peer or local
2552 *
2553 * Returns 0 on success, non-zero value on failure
2554 */
2555int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2556 u8 attr_set, u32 mib_val, u8 peer)
2557{
2558 struct uic_command uic_cmd = {0};
2559 static const char *const action[] = {
2560 "dme-set",
2561 "dme-peer-set"
2562 };
2563 const char *set = action[!!peer];
2564 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002565 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302566
2567 uic_cmd.command = peer ?
2568 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2569 uic_cmd.argument1 = attr_sel;
2570 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2571 uic_cmd.argument3 = mib_val;
2572
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002573 do {
2574 /* for peer attributes we retry upon failure */
2575 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2576 if (ret)
2577 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2578 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2579 } while (ret && peer && --retries);
2580
2581 if (!retries)
2582 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2583 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2584 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302585
2586 return ret;
2587}
2588EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2589
2590/**
2591 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2592 * @hba: per adapter instance
2593 * @attr_sel: uic command argument1
2594 * @mib_val: the value of the attribute as returned by the UIC command
2595 * @peer: indicate whether peer or local
2596 *
2597 * Returns 0 on success, non-zero value on failure
2598 */
2599int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2600 u32 *mib_val, u8 peer)
2601{
2602 struct uic_command uic_cmd = {0};
2603 static const char *const action[] = {
2604 "dme-get",
2605 "dme-peer-get"
2606 };
2607 const char *get = action[!!peer];
2608 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002609 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002610 struct ufs_pa_layer_attr orig_pwr_info;
2611 struct ufs_pa_layer_attr temp_pwr_info;
2612 bool pwr_mode_change = false;
2613
2614 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2615 orig_pwr_info = hba->pwr_info;
2616 temp_pwr_info = orig_pwr_info;
2617
2618 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2619 orig_pwr_info.pwr_rx == FAST_MODE) {
2620 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2621 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2622 pwr_mode_change = true;
2623 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2624 orig_pwr_info.pwr_rx == SLOW_MODE) {
2625 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2626 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2627 pwr_mode_change = true;
2628 }
2629 if (pwr_mode_change) {
2630 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2631 if (ret)
2632 goto out;
2633 }
2634 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302635
2636 uic_cmd.command = peer ?
2637 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2638 uic_cmd.argument1 = attr_sel;
2639
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002640 do {
2641 /* for peer attributes we retry upon failure */
2642 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2643 if (ret)
2644 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2645 get, UIC_GET_ATTR_ID(attr_sel), ret);
2646 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302647
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002648 if (!retries)
2649 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2650 get, UIC_GET_ATTR_ID(attr_sel), retries);
2651
2652 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302653 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002654
2655 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2656 && pwr_mode_change)
2657 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302658out:
2659 return ret;
2660}
2661EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2662
2663/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002664 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2665 * state) and waits for it to take effect.
2666 *
2667 * @hba: per adapter instance
2668 * @cmd: UIC command to execute
2669 *
2670 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2671 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2672 * and device UniPro link and hence it's final completion would be indicated by
2673 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2674 * addition to normal UIC command completion Status (UCCS). This function only
2675 * returns after the relevant status bits indicate the completion.
2676 *
2677 * Returns 0 on success, non-zero value on failure
2678 */
2679static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2680{
2681 struct completion uic_async_done;
2682 unsigned long flags;
2683 u8 status;
2684 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002685 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002686
2687 mutex_lock(&hba->uic_cmd_mutex);
2688 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002689 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002690
2691 spin_lock_irqsave(hba->host->host_lock, flags);
2692 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002693 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2694 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2695 /*
2696 * Make sure UIC command completion interrupt is disabled before
2697 * issuing UIC command.
2698 */
2699 wmb();
2700 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002701 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002702 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2703 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002704 if (ret) {
2705 dev_err(hba->dev,
2706 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2707 cmd->command, cmd->argument3, ret);
2708 goto out;
2709 }
2710
2711 if (!wait_for_completion_timeout(hba->uic_async_done,
2712 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2713 dev_err(hba->dev,
2714 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2715 cmd->command, cmd->argument3);
2716 ret = -ETIMEDOUT;
2717 goto out;
2718 }
2719
2720 status = ufshcd_get_upmcrs(hba);
2721 if (status != PWR_LOCAL) {
2722 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002723 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002724 cmd->command, status);
2725 ret = (status != PWR_OK) ? status : -1;
2726 }
2727out:
2728 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002729 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002730 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002731 if (reenable_intr)
2732 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002733 spin_unlock_irqrestore(hba->host->host_lock, flags);
2734 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002735
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002736 return ret;
2737}
2738
2739/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302740 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2741 * using DME_SET primitives.
2742 * @hba: per adapter instance
2743 * @mode: powr mode value
2744 *
2745 * Returns 0 on success, non-zero value on failure
2746 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302747static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302748{
2749 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002750 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302751
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002752 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2753 ret = ufshcd_dme_set(hba,
2754 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2755 if (ret) {
2756 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2757 __func__, ret);
2758 goto out;
2759 }
2760 }
2761
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302762 uic_cmd.command = UIC_CMD_DME_SET;
2763 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2764 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002765 ufshcd_hold(hba, false);
2766 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2767 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302768
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002769out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002770 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002771}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302772
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002773static int ufshcd_link_recovery(struct ufs_hba *hba)
2774{
2775 int ret;
2776 unsigned long flags;
2777
2778 spin_lock_irqsave(hba->host->host_lock, flags);
2779 hba->ufshcd_state = UFSHCD_STATE_RESET;
2780 ufshcd_set_eh_in_progress(hba);
2781 spin_unlock_irqrestore(hba->host->host_lock, flags);
2782
2783 ret = ufshcd_host_reset_and_restore(hba);
2784
2785 spin_lock_irqsave(hba->host->host_lock, flags);
2786 if (ret)
2787 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2788 ufshcd_clear_eh_in_progress(hba);
2789 spin_unlock_irqrestore(hba->host->host_lock, flags);
2790
2791 if (ret)
2792 dev_err(hba->dev, "%s: link recovery failed, err %d",
2793 __func__, ret);
2794
2795 return ret;
2796}
2797
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002798static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002799{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002800 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002801 struct uic_command uic_cmd = {0};
2802
2803 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002804 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002805
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002806 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002807 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2808 __func__, ret);
2809
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002810 /*
2811 * If link recovery fails then return error so that caller
2812 * don't retry the hibern8 enter again.
2813 */
2814 if (ufshcd_link_recovery(hba))
2815 ret = -ENOLINK;
2816 }
2817
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002818 return ret;
2819}
2820
2821static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2822{
2823 int ret = 0, retries;
2824
2825 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2826 ret = __ufshcd_uic_hibern8_enter(hba);
2827 if (!ret || ret == -ENOLINK)
2828 goto out;
2829 }
2830out:
2831 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002832}
2833
2834static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2835{
2836 struct uic_command uic_cmd = {0};
2837 int ret;
2838
2839 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2840 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302841 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002842 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2843 __func__, ret);
2844 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302845 }
2846
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302847 return ret;
2848}
2849
Yaniv Gardi50646362014-10-23 13:25:13 +03002850 /**
2851 * ufshcd_init_pwr_info - setting the POR (power on reset)
2852 * values in hba power info
2853 * @hba: per-adapter instance
2854 */
2855static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2856{
2857 hba->pwr_info.gear_rx = UFS_PWM_G1;
2858 hba->pwr_info.gear_tx = UFS_PWM_G1;
2859 hba->pwr_info.lane_rx = 1;
2860 hba->pwr_info.lane_tx = 1;
2861 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2862 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2863 hba->pwr_info.hs_rate = 0;
2864}
2865
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302866/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002867 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2868 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302869 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002870static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302871{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002872 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2873
2874 if (hba->max_pwr_info.is_valid)
2875 return 0;
2876
2877 pwr_info->pwr_tx = FASTAUTO_MODE;
2878 pwr_info->pwr_rx = FASTAUTO_MODE;
2879 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302880
2881 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002882 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2883 &pwr_info->lane_rx);
2884 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2885 &pwr_info->lane_tx);
2886
2887 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2888 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2889 __func__,
2890 pwr_info->lane_rx,
2891 pwr_info->lane_tx);
2892 return -EINVAL;
2893 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302894
2895 /*
2896 * First, get the maximum gears of HS speed.
2897 * If a zero value, it means there is no HSGEAR capability.
2898 * Then, get the maximum gears of PWM speed.
2899 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002900 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2901 if (!pwr_info->gear_rx) {
2902 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2903 &pwr_info->gear_rx);
2904 if (!pwr_info->gear_rx) {
2905 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2906 __func__, pwr_info->gear_rx);
2907 return -EINVAL;
2908 }
2909 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302910 }
2911
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002912 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2913 &pwr_info->gear_tx);
2914 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302915 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002916 &pwr_info->gear_tx);
2917 if (!pwr_info->gear_tx) {
2918 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2919 __func__, pwr_info->gear_tx);
2920 return -EINVAL;
2921 }
2922 pwr_info->pwr_tx = SLOWAUTO_MODE;
2923 }
2924
2925 hba->max_pwr_info.is_valid = true;
2926 return 0;
2927}
2928
2929static int ufshcd_change_power_mode(struct ufs_hba *hba,
2930 struct ufs_pa_layer_attr *pwr_mode)
2931{
2932 int ret;
2933
2934 /* if already configured to the requested pwr_mode */
2935 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2936 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2937 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2938 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2939 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2940 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2941 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2942 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2943 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302944 }
2945
2946 /*
2947 * Configure attributes for power mode change with below.
2948 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2949 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2950 * - PA_HSSERIES
2951 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002952 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2953 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2954 pwr_mode->lane_rx);
2955 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2956 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302957 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002958 else
2959 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302960
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002961 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2962 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2963 pwr_mode->lane_tx);
2964 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2965 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302966 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002967 else
2968 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302969
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002970 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2971 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2972 pwr_mode->pwr_rx == FAST_MODE ||
2973 pwr_mode->pwr_tx == FAST_MODE)
2974 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2975 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302976
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002977 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2978 | pwr_mode->pwr_tx);
2979
2980 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302981 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002982 "%s: power mode change failed %d\n", __func__, ret);
2983 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002984 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2985 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002986
2987 memcpy(&hba->pwr_info, pwr_mode,
2988 sizeof(struct ufs_pa_layer_attr));
2989 }
2990
2991 return ret;
2992}
2993
2994/**
2995 * ufshcd_config_pwr_mode - configure a new power mode
2996 * @hba: per-adapter instance
2997 * @desired_pwr_mode: desired power configuration
2998 */
2999static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3000 struct ufs_pa_layer_attr *desired_pwr_mode)
3001{
3002 struct ufs_pa_layer_attr final_params = { 0 };
3003 int ret;
3004
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003005 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3006 desired_pwr_mode, &final_params);
3007
3008 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003009 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3010
3011 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303012
3013 return ret;
3014}
3015
3016/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303017 * ufshcd_complete_dev_init() - checks device readiness
3018 * hba: per-adapter instance
3019 *
3020 * Set fDeviceInit flag and poll until device toggles it.
3021 */
3022static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3023{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003024 int i;
3025 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303026 bool flag_res = 1;
3027
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003028 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3029 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303030 if (err) {
3031 dev_err(hba->dev,
3032 "%s setting fDeviceInit flag failed with error %d\n",
3033 __func__, err);
3034 goto out;
3035 }
3036
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003037 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3038 for (i = 0; i < 1000 && !err && flag_res; i++)
3039 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3040 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3041
Dolev Raviv68078d52013-07-30 00:35:58 +05303042 if (err)
3043 dev_err(hba->dev,
3044 "%s reading fDeviceInit flag failed with error %d\n",
3045 __func__, err);
3046 else if (flag_res)
3047 dev_err(hba->dev,
3048 "%s fDeviceInit was not cleared by the device\n",
3049 __func__);
3050
3051out:
3052 return err;
3053}
3054
3055/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303056 * ufshcd_make_hba_operational - Make UFS controller operational
3057 * @hba: per adapter instance
3058 *
3059 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003060 * 1. Enable required interrupts
3061 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003062 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003063 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303064 *
3065 * Returns 0 on success, non-zero value on failure
3066 */
3067static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3068{
3069 int err = 0;
3070 u32 reg;
3071
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303072 /* Enable required interrupts */
3073 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3074
3075 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003076 if (ufshcd_is_intr_aggr_allowed(hba))
3077 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3078 else
3079 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303080
3081 /* Configure UTRL and UTMRL base address registers */
3082 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3083 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3084 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3085 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3086 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3087 REG_UTP_TASK_REQ_LIST_BASE_L);
3088 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3089 REG_UTP_TASK_REQ_LIST_BASE_H);
3090
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303091 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003092 * Make sure base address and interrupt setup are updated before
3093 * enabling the run/stop registers below.
3094 */
3095 wmb();
3096
3097 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303098 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303099 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003100 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303101 if (!(ufshcd_get_lists_status(reg))) {
3102 ufshcd_enable_run_stop_reg(hba);
3103 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303104 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303105 "Host controller not ready to process requests");
3106 err = -EIO;
3107 goto out;
3108 }
3109
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303110out:
3111 return err;
3112}
3113
3114/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003115 * ufshcd_hba_stop - Send controller to reset state
3116 * @hba: per adapter instance
3117 * @can_sleep: perform sleep or just spin
3118 */
3119static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3120{
3121 int err;
3122
3123 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3124 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3125 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3126 10, 1, can_sleep);
3127 if (err)
3128 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3129}
3130
3131/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303132 * ufshcd_hba_enable - initialize the controller
3133 * @hba: per adapter instance
3134 *
3135 * The controller resets itself and controller firmware initialization
3136 * sequence kicks off. When controller is ready it will set
3137 * the Host Controller Enable bit to 1.
3138 *
3139 * Returns 0 on success, non-zero value on failure
3140 */
3141static int ufshcd_hba_enable(struct ufs_hba *hba)
3142{
3143 int retry;
3144
3145 /*
3146 * msleep of 1 and 5 used in this function might result in msleep(20),
3147 * but it was necessary to send the UFS FPGA to reset mode during
3148 * development and testing of this driver. msleep can be changed to
3149 * mdelay and retry count can be reduced based on the controller.
3150 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003151 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303152 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003153 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303154
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003155 /* UniPro link is disabled at this point */
3156 ufshcd_set_link_off(hba);
3157
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003158 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003159
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303160 /* start controller initialization sequence */
3161 ufshcd_hba_start(hba);
3162
3163 /*
3164 * To initialize a UFS host controller HCE bit must be set to 1.
3165 * During initialization the HCE bit value changes from 1->0->1.
3166 * When the host controller completes initialization sequence
3167 * it sets the value of HCE bit to 1. The same HCE bit is read back
3168 * to check if the controller has completed initialization sequence.
3169 * So without this delay the value HCE = 1, set in the previous
3170 * instruction might be read back.
3171 * This delay can be changed based on the controller.
3172 */
3173 msleep(1);
3174
3175 /* wait for the host controller to complete initialization */
3176 retry = 10;
3177 while (ufshcd_is_hba_active(hba)) {
3178 if (retry) {
3179 retry--;
3180 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303181 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303182 "Controller enable failed\n");
3183 return -EIO;
3184 }
3185 msleep(5);
3186 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003187
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003188 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003189 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003190
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003191 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003192
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303193 return 0;
3194}
3195
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003196static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3197{
3198 int tx_lanes, i, err = 0;
3199
3200 if (!peer)
3201 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3202 &tx_lanes);
3203 else
3204 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3205 &tx_lanes);
3206 for (i = 0; i < tx_lanes; i++) {
3207 if (!peer)
3208 err = ufshcd_dme_set(hba,
3209 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3210 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3211 0);
3212 else
3213 err = ufshcd_dme_peer_set(hba,
3214 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3215 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3216 0);
3217 if (err) {
3218 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3219 __func__, peer, i, err);
3220 break;
3221 }
3222 }
3223
3224 return err;
3225}
3226
3227static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3228{
3229 return ufshcd_disable_tx_lcc(hba, true);
3230}
3231
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303232/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303233 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303234 * @hba: per adapter instance
3235 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303236 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303237 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303238static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303239{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303240 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003241 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003242 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303243
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003244 /*
3245 * If UFS device isn't active then we will have to issue link startup
3246 * 2 times to make sure the device state move to active.
3247 */
3248 if (!ufshcd_is_ufs_dev_active(hba))
3249 link_startup_again = true;
3250
3251link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003252 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003253 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303254
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003255 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003256
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003257 /* check if device is detected by inter-connect layer */
3258 if (!ret && !ufshcd_is_device_present(hba)) {
3259 dev_err(hba->dev, "%s: Device not present\n", __func__);
3260 ret = -ENXIO;
3261 goto out;
3262 }
3263
3264 /*
3265 * DME link lost indication is only received when link is up,
3266 * but we can't be sure if the link is up until link startup
3267 * succeeds. So reset the local Uni-Pro and try again.
3268 */
3269 if (ret && ufshcd_hba_enable(hba))
3270 goto out;
3271 } while (ret && retries--);
3272
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303273 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003274 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303275 goto out;
3276
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003277 if (link_startup_again) {
3278 link_startup_again = false;
3279 retries = DME_LINKSTARTUP_RETRIES;
3280 goto link_startup;
3281 }
3282
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003283 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3284 ret = ufshcd_disable_device_tx_lcc(hba);
3285 if (ret)
3286 goto out;
3287 }
3288
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003289 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003290 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3291 if (ret)
3292 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003293
3294 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303295out:
3296 if (ret)
3297 dev_err(hba->dev, "link startup failed %d\n", ret);
3298 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303299}
3300
3301/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303302 * ufshcd_verify_dev_init() - Verify device initialization
3303 * @hba: per-adapter instance
3304 *
3305 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3306 * device Transport Protocol (UTP) layer is ready after a reset.
3307 * If the UTP layer at the device side is not initialized, it may
3308 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3309 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3310 */
3311static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3312{
3313 int err = 0;
3314 int retries;
3315
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003316 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303317 mutex_lock(&hba->dev_cmd.lock);
3318 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3319 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3320 NOP_OUT_TIMEOUT);
3321
3322 if (!err || err == -ETIMEDOUT)
3323 break;
3324
3325 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3326 }
3327 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003328 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303329
3330 if (err)
3331 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3332 return err;
3333}
3334
3335/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003336 * ufshcd_set_queue_depth - set lun queue depth
3337 * @sdev: pointer to SCSI device
3338 *
3339 * Read bLUQueueDepth value and activate scsi tagged command
3340 * queueing. For WLUN, queue depth is set to 1. For best-effort
3341 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3342 * value that host can queue.
3343 */
3344static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3345{
3346 int ret = 0;
3347 u8 lun_qdepth;
3348 struct ufs_hba *hba;
3349
3350 hba = shost_priv(sdev->host);
3351
3352 lun_qdepth = hba->nutrs;
3353 ret = ufshcd_read_unit_desc_param(hba,
3354 ufshcd_scsi_to_upiu_lun(sdev->lun),
3355 UNIT_DESC_PARAM_LU_Q_DEPTH,
3356 &lun_qdepth,
3357 sizeof(lun_qdepth));
3358
3359 /* Some WLUN doesn't support unit descriptor */
3360 if (ret == -EOPNOTSUPP)
3361 lun_qdepth = 1;
3362 else if (!lun_qdepth)
3363 /* eventually, we can figure out the real queue depth */
3364 lun_qdepth = hba->nutrs;
3365 else
3366 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3367
3368 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3369 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003370 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003371}
3372
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003373/*
3374 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3375 * @hba: per-adapter instance
3376 * @lun: UFS device lun id
3377 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3378 *
3379 * Returns 0 in case of success and b_lu_write_protect status would be returned
3380 * @b_lu_write_protect parameter.
3381 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3382 * Returns -EINVAL in case of invalid parameters passed to this function.
3383 */
3384static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3385 u8 lun,
3386 u8 *b_lu_write_protect)
3387{
3388 int ret;
3389
3390 if (!b_lu_write_protect)
3391 ret = -EINVAL;
3392 /*
3393 * According to UFS device spec, RPMB LU can't be write
3394 * protected so skip reading bLUWriteProtect parameter for
3395 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3396 */
3397 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3398 ret = -ENOTSUPP;
3399 else
3400 ret = ufshcd_read_unit_desc_param(hba,
3401 lun,
3402 UNIT_DESC_PARAM_LU_WR_PROTECT,
3403 b_lu_write_protect,
3404 sizeof(*b_lu_write_protect));
3405 return ret;
3406}
3407
3408/**
3409 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3410 * status
3411 * @hba: per-adapter instance
3412 * @sdev: pointer to SCSI device
3413 *
3414 */
3415static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3416 struct scsi_device *sdev)
3417{
3418 if (hba->dev_info.f_power_on_wp_en &&
3419 !hba->dev_info.is_lu_power_on_wp) {
3420 u8 b_lu_write_protect;
3421
3422 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3423 &b_lu_write_protect) &&
3424 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3425 hba->dev_info.is_lu_power_on_wp = true;
3426 }
3427}
3428
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003429/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303430 * ufshcd_slave_alloc - handle initial SCSI device configurations
3431 * @sdev: pointer to SCSI device
3432 *
3433 * Returns success
3434 */
3435static int ufshcd_slave_alloc(struct scsi_device *sdev)
3436{
3437 struct ufs_hba *hba;
3438
3439 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303440
3441 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3442 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303443
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303444 /* allow SCSI layer to restart the device in case of errors */
3445 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003446
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003447 /* REPORT SUPPORTED OPERATION CODES is not supported */
3448 sdev->no_report_opcodes = 1;
3449
Sujit Reddy Thumma9760af42018-01-24 09:52:35 +05303450 /* WRITE_SAME command is not supported */
3451 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003452
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003453 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003454
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003455 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3456
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003457 return 0;
3458}
3459
3460/**
3461 * ufshcd_change_queue_depth - change queue depth
3462 * @sdev: pointer to SCSI device
3463 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003464 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003465 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003466 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003467static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003468{
3469 struct ufs_hba *hba = shost_priv(sdev->host);
3470
3471 if (depth > hba->nutrs)
3472 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003473 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303474}
3475
3476/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003477 * ufshcd_slave_configure - adjust SCSI device configurations
3478 * @sdev: pointer to SCSI device
3479 */
3480static int ufshcd_slave_configure(struct scsi_device *sdev)
3481{
3482 struct request_queue *q = sdev->request_queue;
3483
3484 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3485 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3486
3487 return 0;
3488}
3489
3490/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303491 * ufshcd_slave_destroy - remove SCSI device configurations
3492 * @sdev: pointer to SCSI device
3493 */
3494static void ufshcd_slave_destroy(struct scsi_device *sdev)
3495{
3496 struct ufs_hba *hba;
3497
3498 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003499 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003500 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3501 unsigned long flags;
3502
3503 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003504 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003505 spin_unlock_irqrestore(hba->host->host_lock, flags);
3506 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303507}
3508
3509/**
3510 * ufshcd_task_req_compl - handle task management request completion
3511 * @hba: per adapter instance
3512 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303513 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303514 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303515 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303516 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303517static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303518{
3519 struct utp_task_req_desc *task_req_descp;
3520 struct utp_upiu_task_rsp *task_rsp_upiup;
3521 unsigned long flags;
3522 int ocs_value;
3523 int task_result;
3524
3525 spin_lock_irqsave(hba->host->host_lock, flags);
3526
3527 /* Clear completed tasks from outstanding_tasks */
3528 __clear_bit(index, &hba->outstanding_tasks);
3529
3530 task_req_descp = hba->utmrdl_base_addr;
3531 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3532
3533 if (ocs_value == OCS_SUCCESS) {
3534 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3535 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003536 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3537 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303538 if (resp)
3539 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303540 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303541 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3542 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303543 }
3544 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303545
3546 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303547}
3548
3549/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303550 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3551 * @lrb: pointer to local reference block of completed command
3552 * @scsi_status: SCSI command status
3553 *
3554 * Returns value base on SCSI command status
3555 */
3556static inline int
3557ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3558{
3559 int result = 0;
3560
3561 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303562 case SAM_STAT_CHECK_CONDITION:
3563 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303564 case SAM_STAT_GOOD:
3565 result |= DID_OK << 16 |
3566 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303567 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303568 break;
3569 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303570 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303571 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303572 ufshcd_copy_sense_data(lrbp);
3573 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303574 break;
3575 default:
3576 result |= DID_ERROR << 16;
3577 break;
3578 } /* end of switch */
3579
3580 return result;
3581}
3582
3583/**
3584 * ufshcd_transfer_rsp_status - Get overall status of the response
3585 * @hba: per adapter instance
3586 * @lrb: pointer to local reference block of completed command
3587 *
3588 * Returns result of the command to notify SCSI midlayer
3589 */
3590static inline int
3591ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3592{
3593 int result = 0;
3594 int scsi_status;
3595 int ocs;
3596
3597 /* overall command status of utrd */
3598 ocs = ufshcd_get_tr_ocs(lrbp);
3599
3600 switch (ocs) {
3601 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303602 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303603
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303604 switch (result) {
3605 case UPIU_TRANSACTION_RESPONSE:
3606 /*
3607 * get the response UPIU result to extract
3608 * the SCSI command status
3609 */
3610 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3611
3612 /*
3613 * get the result based on SCSI status response
3614 * to notify the SCSI midlayer of the command status
3615 */
3616 scsi_status = result & MASK_SCSI_STATUS;
3617 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303618
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003619 /*
3620 * Currently we are only supporting BKOPs exception
3621 * events hence we can ignore BKOPs exception event
3622 * during power management callbacks. BKOPs exception
3623 * event is not expected to be raised in runtime suspend
3624 * callback as it allows the urgent bkops.
3625 * During system suspend, we are anyway forcefully
3626 * disabling the bkops and if urgent bkops is needed
3627 * it will be enabled on system resume. Long term
3628 * solution could be to abort the system suspend if
3629 * UFS device needs urgent BKOPs.
3630 */
3631 if (!hba->pm_op_in_progress &&
3632 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303633 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303634 break;
3635 case UPIU_TRANSACTION_REJECT_UPIU:
3636 /* TODO: handle Reject UPIU Response */
3637 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303638 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303639 "Reject UPIU not fully implemented\n");
3640 break;
3641 default:
3642 result = DID_ERROR << 16;
3643 dev_err(hba->dev,
3644 "Unexpected request response code = %x\n",
3645 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303646 break;
3647 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303648 break;
3649 case OCS_ABORTED:
3650 result |= DID_ABORT << 16;
3651 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303652 case OCS_INVALID_COMMAND_STATUS:
3653 result |= DID_REQUEUE << 16;
3654 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303655 case OCS_INVALID_CMD_TABLE_ATTR:
3656 case OCS_INVALID_PRDT_ATTR:
3657 case OCS_MISMATCH_DATA_BUF_SIZE:
3658 case OCS_MISMATCH_RESP_UPIU_SIZE:
3659 case OCS_PEER_COMM_FAILURE:
3660 case OCS_FATAL_ERROR:
3661 default:
3662 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303663 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303664 "OCS error from controller = %x\n", ocs);
3665 break;
3666 } /* end of switch */
3667
3668 return result;
3669}
3670
3671/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303672 * ufshcd_uic_cmd_compl - handle completion of uic command
3673 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303674 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303675 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303676static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303677{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303678 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303679 hba->active_uic_cmd->argument2 |=
3680 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303681 hba->active_uic_cmd->argument3 =
3682 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303683 complete(&hba->active_uic_cmd->done);
3684 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303685
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003686 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3687 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303688}
3689
3690/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003691 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303692 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003693 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303694 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003695static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3696 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303697{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303698 struct ufshcd_lrb *lrbp;
3699 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303700 int result;
3701 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003702
Dolev Ravive9d501b2014-07-01 12:22:37 +03003703 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3704 lrbp = &hba->lrb[index];
3705 cmd = lrbp->cmd;
3706 if (cmd) {
3707 result = ufshcd_transfer_rsp_status(hba, lrbp);
3708 scsi_dma_unmap(cmd);
3709 cmd->result = result;
3710 /* Mark completed command as NULL in LRB */
3711 lrbp->cmd = NULL;
3712 clear_bit_unlock(index, &hba->lrb_in_use);
3713 /* Do not touch lrbp after scsi done */
3714 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003715 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003716 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3717 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003718 if (hba->dev_cmd.complete)
3719 complete(hba->dev_cmd.complete);
3720 }
3721 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303722
3723 /* clear corresponding bits of completed commands */
3724 hba->outstanding_reqs ^= completed_reqs;
3725
Sahitya Tummala856b3482014-09-25 15:32:34 +03003726 ufshcd_clk_scaling_update_busy(hba);
3727
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303728 /* we might have free'd some tags above */
3729 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303730}
3731
3732/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003733 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3734 * @hba: per adapter instance
3735 */
3736static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3737{
3738 unsigned long completed_reqs;
3739 u32 tr_doorbell;
3740
3741 /* Resetting interrupt aggregation counters first and reading the
3742 * DOOR_BELL afterward allows us to handle all the completed requests.
3743 * In order to prevent other interrupts starvation the DB is read once
3744 * after reset. The down side of this solution is the possibility of
3745 * false interrupt if device completes another request after resetting
3746 * aggregation and before reading the DB.
3747 */
3748 if (ufshcd_is_intr_aggr_allowed(hba))
3749 ufshcd_reset_intr_aggr(hba);
3750
3751 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3752 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3753
3754 __ufshcd_transfer_req_compl(hba, completed_reqs);
3755}
3756
3757/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303758 * ufshcd_disable_ee - disable exception event
3759 * @hba: per-adapter instance
3760 * @mask: exception event to disable
3761 *
3762 * Disables exception event in the device so that the EVENT_ALERT
3763 * bit is not set.
3764 *
3765 * Returns zero on success, non-zero error value on failure.
3766 */
3767static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3768{
3769 int err = 0;
3770 u32 val;
3771
3772 if (!(hba->ee_ctrl_mask & mask))
3773 goto out;
3774
3775 val = hba->ee_ctrl_mask & ~mask;
3776 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003777 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303778 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3779 if (!err)
3780 hba->ee_ctrl_mask &= ~mask;
3781out:
3782 return err;
3783}
3784
3785/**
3786 * ufshcd_enable_ee - enable exception event
3787 * @hba: per-adapter instance
3788 * @mask: exception event to enable
3789 *
3790 * Enable corresponding exception event in the device to allow
3791 * device to alert host in critical scenarios.
3792 *
3793 * Returns zero on success, non-zero error value on failure.
3794 */
3795static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3796{
3797 int err = 0;
3798 u32 val;
3799
3800 if (hba->ee_ctrl_mask & mask)
3801 goto out;
3802
3803 val = hba->ee_ctrl_mask | mask;
3804 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003805 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303806 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3807 if (!err)
3808 hba->ee_ctrl_mask |= mask;
3809out:
3810 return err;
3811}
3812
3813/**
3814 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3815 * @hba: per-adapter instance
3816 *
3817 * Allow device to manage background operations on its own. Enabling
3818 * this might lead to inconsistent latencies during normal data transfers
3819 * as the device is allowed to manage its own way of handling background
3820 * operations.
3821 *
3822 * Returns zero on success, non-zero on failure.
3823 */
3824static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3825{
3826 int err = 0;
3827
3828 if (hba->auto_bkops_enabled)
3829 goto out;
3830
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003831 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303832 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3833 if (err) {
3834 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3835 __func__, err);
3836 goto out;
3837 }
3838
3839 hba->auto_bkops_enabled = true;
3840
3841 /* No need of URGENT_BKOPS exception from the device */
3842 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3843 if (err)
3844 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3845 __func__, err);
3846out:
3847 return err;
3848}
3849
3850/**
3851 * ufshcd_disable_auto_bkops - block device in doing background operations
3852 * @hba: per-adapter instance
3853 *
3854 * Disabling background operations improves command response latency but
3855 * has drawback of device moving into critical state where the device is
3856 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3857 * host is idle so that BKOPS are managed effectively without any negative
3858 * impacts.
3859 *
3860 * Returns zero on success, non-zero on failure.
3861 */
3862static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3863{
3864 int err = 0;
3865
3866 if (!hba->auto_bkops_enabled)
3867 goto out;
3868
3869 /*
3870 * If host assisted BKOPs is to be enabled, make sure
3871 * urgent bkops exception is allowed.
3872 */
3873 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3874 if (err) {
3875 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3876 __func__, err);
3877 goto out;
3878 }
3879
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003880 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303881 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3882 if (err) {
3883 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3884 __func__, err);
3885 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3886 goto out;
3887 }
3888
3889 hba->auto_bkops_enabled = false;
3890out:
3891 return err;
3892}
3893
3894/**
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003895 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303896 * @hba: per adapter instance
3897 *
3898 * After a device reset the device may toggle the BKOPS_EN flag
3899 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003900 * as well. This function would change the auto-bkops state based on
3901 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303902 */
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003903static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303904{
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003905 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
3906 hba->auto_bkops_enabled = false;
3907 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3908 ufshcd_enable_auto_bkops(hba);
3909 } else {
3910 hba->auto_bkops_enabled = true;
3911 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
3912 ufshcd_disable_auto_bkops(hba);
3913 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303914}
3915
3916static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3917{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003918 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303919 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3920}
3921
3922/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003923 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3924 * @hba: per-adapter instance
3925 * @status: bkops_status value
3926 *
3927 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3928 * flag in the device to permit background operations if the device
3929 * bkops_status is greater than or equal to "status" argument passed to
3930 * this function, disable otherwise.
3931 *
3932 * Returns 0 for success, non-zero in case of failure.
3933 *
3934 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3935 * to know whether auto bkops is enabled or disabled after this function
3936 * returns control to it.
3937 */
3938static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3939 enum bkops_status status)
3940{
3941 int err;
3942 u32 curr_status = 0;
3943
3944 err = ufshcd_get_bkops_status(hba, &curr_status);
3945 if (err) {
3946 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3947 __func__, err);
3948 goto out;
3949 } else if (curr_status > BKOPS_STATUS_MAX) {
3950 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3951 __func__, curr_status);
3952 err = -EINVAL;
3953 goto out;
3954 }
3955
3956 if (curr_status >= status)
3957 err = ufshcd_enable_auto_bkops(hba);
3958 else
3959 err = ufshcd_disable_auto_bkops(hba);
3960out:
3961 return err;
3962}
3963
3964/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303965 * ufshcd_urgent_bkops - handle urgent bkops exception event
3966 * @hba: per-adapter instance
3967 *
3968 * Enable fBackgroundOpsEn flag in the device to permit background
3969 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003970 *
3971 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3972 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303973 */
3974static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3975{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003976 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303977}
3978
3979static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3980{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003981 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303982 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3983}
3984
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003985static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3986{
3987 int err;
3988 u32 curr_status = 0;
3989
3990 if (hba->is_urgent_bkops_lvl_checked)
3991 goto enable_auto_bkops;
3992
3993 err = ufshcd_get_bkops_status(hba, &curr_status);
3994 if (err) {
3995 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3996 __func__, err);
3997 goto out;
3998 }
3999
4000 /*
4001 * We are seeing that some devices are raising the urgent bkops
4002 * exception events even when BKOPS status doesn't indicate performace
4003 * impacted or critical. Handle these device by determining their urgent
4004 * bkops status at runtime.
4005 */
4006 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4007 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4008 __func__, curr_status);
4009 /* update the current status as the urgent bkops level */
4010 hba->urgent_bkops_lvl = curr_status;
4011 hba->is_urgent_bkops_lvl_checked = true;
4012 }
4013
4014enable_auto_bkops:
4015 err = ufshcd_enable_auto_bkops(hba);
4016out:
4017 if (err < 0)
4018 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4019 __func__, err);
4020}
4021
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304022/**
4023 * ufshcd_exception_event_handler - handle exceptions raised by device
4024 * @work: pointer to work data
4025 *
4026 * Read bExceptionEventStatus attribute from the device and handle the
4027 * exception event accordingly.
4028 */
4029static void ufshcd_exception_event_handler(struct work_struct *work)
4030{
4031 struct ufs_hba *hba;
4032 int err;
4033 u32 status = 0;
4034 hba = container_of(work, struct ufs_hba, eeh_work);
4035
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304036 pm_runtime_get_sync(hba->dev);
Maya Erez62413ba2018-05-03 16:37:16 +05304037 scsi_block_requests(hba->host);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304038 err = ufshcd_get_ee_status(hba, &status);
4039 if (err) {
4040 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4041 __func__, err);
4042 goto out;
4043 }
4044
4045 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004046
4047 if (status & MASK_EE_URGENT_BKOPS)
4048 ufshcd_bkops_exception_event_handler(hba);
4049
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304050out:
Maya Erez62413ba2018-05-03 16:37:16 +05304051 scsi_unblock_requests(hba->host);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304052 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304053 return;
4054}
4055
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004056/* Complete requests that have door-bell cleared */
4057static void ufshcd_complete_requests(struct ufs_hba *hba)
4058{
4059 ufshcd_transfer_req_compl(hba);
4060 ufshcd_tmc_handler(hba);
4061}
4062
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304063/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004064 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4065 * to recover from the DL NAC errors or not.
4066 * @hba: per-adapter instance
4067 *
4068 * Returns true if error handling is required, false otherwise
4069 */
4070static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4071{
4072 unsigned long flags;
4073 bool err_handling = true;
4074
4075 spin_lock_irqsave(hba->host->host_lock, flags);
4076 /*
4077 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4078 * device fatal error and/or DL NAC & REPLAY timeout errors.
4079 */
4080 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4081 goto out;
4082
4083 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4084 ((hba->saved_err & UIC_ERROR) &&
4085 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4086 goto out;
4087
4088 if ((hba->saved_err & UIC_ERROR) &&
4089 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4090 int err;
4091 /*
4092 * wait for 50ms to see if we can get any other errors or not.
4093 */
4094 spin_unlock_irqrestore(hba->host->host_lock, flags);
4095 msleep(50);
4096 spin_lock_irqsave(hba->host->host_lock, flags);
4097
4098 /*
4099 * now check if we have got any other severe errors other than
4100 * DL NAC error?
4101 */
4102 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4103 ((hba->saved_err & UIC_ERROR) &&
4104 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4105 goto out;
4106
4107 /*
4108 * As DL NAC is the only error received so far, send out NOP
4109 * command to confirm if link is still active or not.
4110 * - If we don't get any response then do error recovery.
4111 * - If we get response then clear the DL NAC error bit.
4112 */
4113
4114 spin_unlock_irqrestore(hba->host->host_lock, flags);
4115 err = ufshcd_verify_dev_init(hba);
4116 spin_lock_irqsave(hba->host->host_lock, flags);
4117
4118 if (err)
4119 goto out;
4120
4121 /* Link seems to be alive hence ignore the DL NAC errors */
4122 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4123 hba->saved_err &= ~UIC_ERROR;
4124 /* clear NAC error */
4125 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4126 if (!hba->saved_uic_err) {
4127 err_handling = false;
4128 goto out;
4129 }
4130 }
4131out:
4132 spin_unlock_irqrestore(hba->host->host_lock, flags);
4133 return err_handling;
4134}
4135
4136/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304137 * ufshcd_err_handler - handle UFS errors that require s/w attention
4138 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304139 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304140static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304141{
4142 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304143 unsigned long flags;
4144 u32 err_xfer = 0;
4145 u32 err_tm = 0;
4146 int err = 0;
4147 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004148 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304149
4150 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304151
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304152 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004153 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304154
4155 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004156 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304157 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304158
4159 hba->ufshcd_state = UFSHCD_STATE_RESET;
4160 ufshcd_set_eh_in_progress(hba);
4161
4162 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004163 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004164
4165 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4166 bool ret;
4167
4168 spin_unlock_irqrestore(hba->host->host_lock, flags);
4169 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4170 ret = ufshcd_quirk_dl_nac_errors(hba);
4171 spin_lock_irqsave(hba->host->host_lock, flags);
4172 if (!ret)
4173 goto skip_err_handling;
4174 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004175 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4176 ((hba->saved_err & UIC_ERROR) &&
4177 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4178 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4179 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4180 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304181
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004182 /*
4183 * if host reset is required then skip clearing the pending
4184 * transfers forcefully because they will automatically get
4185 * cleared after link startup.
4186 */
4187 if (needs_reset)
4188 goto skip_pending_xfer_clear;
4189
4190 /* release lock as clear command might sleep */
4191 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304192 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004193 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4194 if (ufshcd_clear_cmd(hba, tag)) {
4195 err_xfer = true;
4196 goto lock_skip_pending_xfer_clear;
4197 }
4198 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304199
4200 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004201 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4202 if (ufshcd_clear_tm_cmd(hba, tag)) {
4203 err_tm = true;
4204 goto lock_skip_pending_xfer_clear;
4205 }
4206 }
4207
4208lock_skip_pending_xfer_clear:
4209 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304210
4211 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004212 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304213
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004214 if (err_xfer || err_tm)
4215 needs_reset = true;
4216
4217skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304218 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004219 if (needs_reset) {
4220 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4221
4222 /*
4223 * ufshcd_reset_and_restore() does the link reinitialization
4224 * which will need atleast one empty doorbell slot to send the
4225 * device management commands (NOP and query commands).
4226 * If there is no slot empty at this moment then free up last
4227 * slot forcefully.
4228 */
4229 if (hba->outstanding_reqs == max_doorbells)
4230 __ufshcd_transfer_req_compl(hba,
4231 (1UL << (hba->nutrs - 1)));
4232
4233 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304234 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004235 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304236 if (err) {
4237 dev_err(hba->dev, "%s: reset and restore failed\n",
4238 __func__);
4239 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4240 }
4241 /*
4242 * Inform scsi mid-layer that we did reset and allow to handle
4243 * Unit Attention properly.
4244 */
4245 scsi_report_bus_reset(hba->host, 0);
4246 hba->saved_err = 0;
4247 hba->saved_uic_err = 0;
4248 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004249
Yaniv Gardi583fa622016-03-10 17:37:13 +02004250skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004251 if (!needs_reset) {
4252 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4253 if (hba->saved_err || hba->saved_uic_err)
4254 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4255 __func__, hba->saved_err, hba->saved_uic_err);
4256 }
4257
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304258 ufshcd_clear_eh_in_progress(hba);
4259
4260out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004261 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304262 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004263 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304264 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304265}
4266
4267/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304268 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4269 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304270 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304271static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304272{
4273 u32 reg;
4274
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304275 /* PA_INIT_ERROR is fatal and needs UIC reset */
4276 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4277 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4278 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004279 else if (hba->dev_quirks &
4280 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4281 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4282 hba->uic_error |=
4283 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4284 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4285 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4286 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304287
4288 /* UIC NL/TL/DME errors needs software retry */
4289 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4290 if (reg)
4291 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4292
4293 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4294 if (reg)
4295 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4296
4297 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4298 if (reg)
4299 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4300
4301 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4302 __func__, hba->uic_error);
4303}
4304
4305/**
4306 * ufshcd_check_errors - Check for errors that need s/w attention
4307 * @hba: per-adapter instance
4308 */
4309static void ufshcd_check_errors(struct ufs_hba *hba)
4310{
4311 bool queue_eh_work = false;
4312
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304313 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304314 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304315
4316 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304317 hba->uic_error = 0;
4318 ufshcd_update_uic_error(hba);
4319 if (hba->uic_error)
4320 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304321 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304322
4323 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004324 /*
4325 * update the transfer error masks to sticky bits, let's do this
4326 * irrespective of current ufshcd_state.
4327 */
4328 hba->saved_err |= hba->errors;
4329 hba->saved_uic_err |= hba->uic_error;
4330
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304331 /* handle fatal errors only when link is functional */
4332 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4333 /* block commands from scsi mid-layer */
4334 scsi_block_requests(hba->host);
4335
Zang Leiganga17bddc2017-04-04 19:32:20 +00004336 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304337 schedule_work(&hba->eh_work);
4338 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304339 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304340 /*
4341 * if (!queue_eh_work) -
4342 * Other errors are either non-fatal where host recovers
4343 * itself without s/w intervention or errors that will be
4344 * handled by the SCSI core layer.
4345 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304346}
4347
4348/**
4349 * ufshcd_tmc_handler - handle task management function completion
4350 * @hba: per adapter instance
4351 */
4352static void ufshcd_tmc_handler(struct ufs_hba *hba)
4353{
4354 u32 tm_doorbell;
4355
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304356 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304357 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304358 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304359}
4360
4361/**
4362 * ufshcd_sl_intr - Interrupt service routine
4363 * @hba: per adapter instance
4364 * @intr_status: contains interrupts generated by the controller
4365 */
4366static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4367{
4368 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4369 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304370 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304371
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304372 if (intr_status & UFSHCD_UIC_MASK)
4373 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304374
4375 if (intr_status & UTP_TASK_REQ_COMPL)
4376 ufshcd_tmc_handler(hba);
4377
4378 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4379 ufshcd_transfer_req_compl(hba);
4380}
4381
4382/**
4383 * ufshcd_intr - Main interrupt service routine
4384 * @irq: irq number
4385 * @__hba: pointer to adapter instance
4386 *
4387 * Returns IRQ_HANDLED - If interrupt is valid
4388 * IRQ_NONE - If invalid interrupt
4389 */
4390static irqreturn_t ufshcd_intr(int irq, void *__hba)
4391{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004392 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304393 irqreturn_t retval = IRQ_NONE;
4394 struct ufs_hba *hba = __hba;
4395
4396 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304397 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004398 enabled_intr_status =
4399 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304400
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004401 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304402 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004403
4404 if (enabled_intr_status) {
4405 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304406 retval = IRQ_HANDLED;
4407 }
4408 spin_unlock(hba->host->host_lock);
4409 return retval;
4410}
4411
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304412static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4413{
4414 int err = 0;
4415 u32 mask = 1 << tag;
4416 unsigned long flags;
4417
4418 if (!test_bit(tag, &hba->outstanding_tasks))
4419 goto out;
4420
4421 spin_lock_irqsave(hba->host->host_lock, flags);
4422 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4423 spin_unlock_irqrestore(hba->host->host_lock, flags);
4424
4425 /* poll for max. 1 sec to clear door bell register by h/w */
4426 err = ufshcd_wait_for_register(hba,
4427 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004428 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304429out:
4430 return err;
4431}
4432
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304433/**
4434 * ufshcd_issue_tm_cmd - issues task management commands to controller
4435 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304436 * @lun_id: LUN ID to which TM command is sent
4437 * @task_id: task ID to which the TM command is applicable
4438 * @tm_function: task management function opcode
4439 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304440 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304441 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304442 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304443static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4444 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304445{
4446 struct utp_task_req_desc *task_req_descp;
4447 struct utp_upiu_task_req *task_req_upiup;
4448 struct Scsi_Host *host;
4449 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304450 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304451 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304452 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304453
4454 host = hba->host;
4455
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304456 /*
4457 * Get free slot, sleep if slots are unavailable.
4458 * Even though we use wait_event() which sleeps indefinitely,
4459 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4460 */
4461 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004462 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304463
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304464 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304465 task_req_descp = hba->utmrdl_base_addr;
4466 task_req_descp += free_slot;
4467
4468 /* Configure task request descriptor */
4469 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4470 task_req_descp->header.dword_2 =
4471 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4472
4473 /* Configure task request UPIU */
4474 task_req_upiup =
4475 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304476 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304477 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304478 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304479 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304480 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304481 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004482 /*
4483 * The host shall provide the same value for LUN field in the basic
4484 * header and for Input Parameter.
4485 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304486 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4487 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304488
4489 /* send command to the controller */
4490 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004491
4492 /* Make sure descriptors are ready before ringing the task doorbell */
4493 wmb();
4494
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304495 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304496
4497 spin_unlock_irqrestore(host->host_lock, flags);
4498
4499 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304500 err = wait_event_timeout(hba->tm_wq,
4501 test_bit(free_slot, &hba->tm_condition),
4502 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304503 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304504 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4505 __func__, tm_function);
4506 if (ufshcd_clear_tm_cmd(hba, free_slot))
4507 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4508 __func__, free_slot);
4509 err = -ETIMEDOUT;
4510 } else {
4511 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304512 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304513
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304514 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304515 ufshcd_put_tm_slot(hba, free_slot);
4516 wake_up(&hba->tm_tag_wq);
4517
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004518 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304519 return err;
4520}
4521
4522/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304523 * ufshcd_eh_device_reset_handler - device reset handler registered to
4524 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304525 * @cmd: SCSI command pointer
4526 *
4527 * Returns SUCCESS/FAILED
4528 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304529static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304530{
4531 struct Scsi_Host *host;
4532 struct ufs_hba *hba;
4533 unsigned int tag;
4534 u32 pos;
4535 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304536 u8 resp = 0xF;
4537 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304538 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304539
4540 host = cmd->device->host;
4541 hba = shost_priv(host);
4542 tag = cmd->request->tag;
4543
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304544 lrbp = &hba->lrb[tag];
4545 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4546 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304547 if (!err)
4548 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304549 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304550 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304551
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304552 /* clear the commands that were pending for corresponding LUN */
4553 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4554 if (hba->lrb[pos].lun == lrbp->lun) {
4555 err = ufshcd_clear_cmd(hba, pos);
4556 if (err)
4557 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304558 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304559 }
4560 spin_lock_irqsave(host->host_lock, flags);
4561 ufshcd_transfer_req_compl(hba);
4562 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304563out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304564 if (!err) {
4565 err = SUCCESS;
4566 } else {
4567 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4568 err = FAILED;
4569 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304570 return err;
4571}
4572
4573/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304574 * ufshcd_abort - abort a specific command
4575 * @cmd: SCSI command pointer
4576 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304577 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4578 * command, and in host controller by clearing the door-bell register. There can
4579 * be race between controller sending the command to the device while abort is
4580 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4581 * really issued and then try to abort it.
4582 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304583 * Returns SUCCESS/FAILED
4584 */
4585static int ufshcd_abort(struct scsi_cmnd *cmd)
4586{
4587 struct Scsi_Host *host;
4588 struct ufs_hba *hba;
4589 unsigned long flags;
4590 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304591 int err = 0;
4592 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304593 u8 resp = 0xF;
4594 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004595 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304596
4597 host = cmd->device->host;
4598 hba = shost_priv(host);
4599 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004600 if (!ufshcd_valid_tag(hba, tag)) {
4601 dev_err(hba->dev,
4602 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4603 __func__, tag, cmd, cmd->request);
4604 BUG();
4605 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304606
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004607 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004608 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004609 /* If command is already aborted/completed, return SUCCESS */
4610 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4611 dev_err(hba->dev,
4612 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4613 __func__, tag, hba->outstanding_reqs, reg);
4614 goto out;
4615 }
4616
Dolev Ravive9d501b2014-07-01 12:22:37 +03004617 if (!(reg & (1 << tag))) {
4618 dev_err(hba->dev,
4619 "%s: cmd was completed, but without a notifying intr, tag = %d",
4620 __func__, tag);
4621 }
4622
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304623 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304624 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4625 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4626 UFS_QUERY_TASK, &resp);
4627 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4628 /* cmd pending in the device */
4629 break;
4630 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304631 /*
4632 * cmd not pending in the device, check if it is
4633 * in transition.
4634 */
4635 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4636 if (reg & (1 << tag)) {
4637 /* sleep for max. 200us to stabilize */
4638 usleep_range(100, 200);
4639 continue;
4640 }
4641 /* command completed already */
4642 goto out;
4643 } else {
4644 if (!err)
4645 err = resp; /* service response error */
4646 goto out;
4647 }
4648 }
4649
4650 if (!poll_cnt) {
4651 err = -EBUSY;
4652 goto out;
4653 }
4654
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304655 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4656 UFS_ABORT_TASK, &resp);
4657 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304658 if (!err)
4659 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304660 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304661 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304662
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304663 err = ufshcd_clear_cmd(hba, tag);
4664 if (err)
4665 goto out;
4666
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304667 scsi_dma_unmap(cmd);
4668
4669 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004670 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304671 hba->lrb[tag].cmd = NULL;
4672 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304673
4674 clear_bit_unlock(tag, &hba->lrb_in_use);
4675 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004676
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304677out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304678 if (!err) {
4679 err = SUCCESS;
4680 } else {
4681 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4682 err = FAILED;
4683 }
4684
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004685 /*
4686 * This ufshcd_release() corresponds to the original scsi cmd that got
4687 * aborted here (as we won't get any IRQ for it).
4688 */
4689 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304690 return err;
4691}
4692
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304693/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304694 * ufshcd_host_reset_and_restore - reset and restore host controller
4695 * @hba: per-adapter instance
4696 *
4697 * Note that host controller reset may issue DME_RESET to
4698 * local and remote (device) Uni-Pro stack and the attributes
4699 * are reset to default state.
4700 *
4701 * Returns zero on success, non-zero on failure
4702 */
4703static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4704{
4705 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304706 unsigned long flags;
4707
4708 /* Reset the host controller */
4709 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004710 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304711 spin_unlock_irqrestore(hba->host->host_lock, flags);
4712
4713 err = ufshcd_hba_enable(hba);
4714 if (err)
4715 goto out;
4716
4717 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004718 err = ufshcd_probe_hba(hba);
4719
4720 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304721 err = -EIO;
4722out:
4723 if (err)
4724 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4725
4726 return err;
4727}
4728
4729/**
4730 * ufshcd_reset_and_restore - reset and re-initialize host/device
4731 * @hba: per-adapter instance
4732 *
4733 * Reset and recover device, host and re-establish link. This
4734 * is helpful to recover the communication in fatal error conditions.
4735 *
4736 * Returns zero on success, non-zero on failure
4737 */
4738static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4739{
4740 int err = 0;
4741 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004742 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304743
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004744 do {
4745 err = ufshcd_host_reset_and_restore(hba);
4746 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304747
4748 /*
4749 * After reset the door-bell might be cleared, complete
4750 * outstanding requests in s/w here.
4751 */
4752 spin_lock_irqsave(hba->host->host_lock, flags);
4753 ufshcd_transfer_req_compl(hba);
4754 ufshcd_tmc_handler(hba);
4755 spin_unlock_irqrestore(hba->host->host_lock, flags);
4756
4757 return err;
4758}
4759
4760/**
4761 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4762 * @cmd - SCSI command pointer
4763 *
4764 * Returns SUCCESS/FAILED
4765 */
4766static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4767{
4768 int err;
4769 unsigned long flags;
4770 struct ufs_hba *hba;
4771
4772 hba = shost_priv(cmd->device->host);
4773
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004774 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304775 /*
4776 * Check if there is any race with fatal error handling.
4777 * If so, wait for it to complete. Even though fatal error
4778 * handling does reset and restore in some cases, don't assume
4779 * anything out of it. We are just avoiding race here.
4780 */
4781 do {
4782 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304783 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304784 hba->ufshcd_state == UFSHCD_STATE_RESET))
4785 break;
4786 spin_unlock_irqrestore(hba->host->host_lock, flags);
4787 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304788 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304789 } while (1);
4790
4791 hba->ufshcd_state = UFSHCD_STATE_RESET;
4792 ufshcd_set_eh_in_progress(hba);
4793 spin_unlock_irqrestore(hba->host->host_lock, flags);
4794
4795 err = ufshcd_reset_and_restore(hba);
4796
4797 spin_lock_irqsave(hba->host->host_lock, flags);
4798 if (!err) {
4799 err = SUCCESS;
4800 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4801 } else {
4802 err = FAILED;
4803 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4804 }
4805 ufshcd_clear_eh_in_progress(hba);
4806 spin_unlock_irqrestore(hba->host->host_lock, flags);
4807
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004808 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304809 return err;
4810}
4811
4812/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004813 * ufshcd_get_max_icc_level - calculate the ICC level
4814 * @sup_curr_uA: max. current supported by the regulator
4815 * @start_scan: row at the desc table to start scan from
4816 * @buff: power descriptor buffer
4817 *
4818 * Returns calculated max ICC level for specific regulator
4819 */
4820static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4821{
4822 int i;
4823 int curr_uA;
4824 u16 data;
4825 u16 unit;
4826
4827 for (i = start_scan; i >= 0; i--) {
4828 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4829 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4830 ATTR_ICC_LVL_UNIT_OFFSET;
4831 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4832 switch (unit) {
4833 case UFSHCD_NANO_AMP:
4834 curr_uA = curr_uA / 1000;
4835 break;
4836 case UFSHCD_MILI_AMP:
4837 curr_uA = curr_uA * 1000;
4838 break;
4839 case UFSHCD_AMP:
4840 curr_uA = curr_uA * 1000 * 1000;
4841 break;
4842 case UFSHCD_MICRO_AMP:
4843 default:
4844 break;
4845 }
4846 if (sup_curr_uA >= curr_uA)
4847 break;
4848 }
4849 if (i < 0) {
4850 i = 0;
4851 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4852 }
4853
4854 return (u32)i;
4855}
4856
4857/**
4858 * ufshcd_calc_icc_level - calculate the max ICC level
4859 * In case regulators are not initialized we'll return 0
4860 * @hba: per-adapter instance
4861 * @desc_buf: power descriptor buffer to extract ICC levels from.
4862 * @len: length of desc_buff
4863 *
4864 * Returns calculated ICC level
4865 */
4866static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4867 u8 *desc_buf, int len)
4868{
4869 u32 icc_level = 0;
4870
4871 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4872 !hba->vreg_info.vccq2) {
4873 dev_err(hba->dev,
4874 "%s: Regulator capability was not set, actvIccLevel=%d",
4875 __func__, icc_level);
4876 goto out;
4877 }
4878
Stanley Chu75246f572019-03-28 17:16:25 +08004879 if (hba->vreg_info.vcc && hba->vreg_info.vcc->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004880 icc_level = ufshcd_get_max_icc_level(
4881 hba->vreg_info.vcc->max_uA,
4882 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4883 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4884
Stanley Chu75246f572019-03-28 17:16:25 +08004885 if (hba->vreg_info.vccq && hba->vreg_info.vccq->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004886 icc_level = ufshcd_get_max_icc_level(
4887 hba->vreg_info.vccq->max_uA,
4888 icc_level,
4889 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4890
Stanley Chu75246f572019-03-28 17:16:25 +08004891 if (hba->vreg_info.vccq2 && hba->vreg_info.vccq2->max_uA)
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004892 icc_level = ufshcd_get_max_icc_level(
4893 hba->vreg_info.vccq2->max_uA,
4894 icc_level,
4895 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4896out:
4897 return icc_level;
4898}
4899
4900static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4901{
4902 int ret;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004903 int buff_len = hba->desc_size.pwr_desc;
4904 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004905
4906 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4907 if (ret) {
4908 dev_err(hba->dev,
4909 "%s: Failed reading power descriptor.len = %d ret = %d",
4910 __func__, buff_len, ret);
4911 return;
4912 }
4913
4914 hba->init_prefetch_data.icc_level =
4915 ufshcd_find_max_sup_active_icc_level(hba,
4916 desc_buf, buff_len);
4917 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4918 __func__, hba->init_prefetch_data.icc_level);
4919
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004920 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4921 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4922 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004923
4924 if (ret)
4925 dev_err(hba->dev,
4926 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4927 __func__, hba->init_prefetch_data.icc_level , ret);
4928
4929}
4930
4931/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004932 * ufshcd_scsi_add_wlus - Adds required W-LUs
4933 * @hba: per-adapter instance
4934 *
4935 * UFS device specification requires the UFS devices to support 4 well known
4936 * logical units:
4937 * "REPORT_LUNS" (address: 01h)
4938 * "UFS Device" (address: 50h)
4939 * "RPMB" (address: 44h)
4940 * "BOOT" (address: 30h)
4941 * UFS device's power management needs to be controlled by "POWER CONDITION"
4942 * field of SSU (START STOP UNIT) command. But this "power condition" field
4943 * will take effect only when its sent to "UFS device" well known logical unit
4944 * hence we require the scsi_device instance to represent this logical unit in
4945 * order for the UFS host driver to send the SSU command for power management.
4946
4947 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4948 * Block) LU so user space process can control this LU. User space may also
4949 * want to have access to BOOT LU.
4950
4951 * This function adds scsi device instances for each of all well known LUs
4952 * (except "REPORT LUNS" LU).
4953 *
4954 * Returns zero on success (all required W-LUs are added successfully),
4955 * non-zero error value on failure (if failed to add any of the required W-LU).
4956 */
4957static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4958{
4959 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004960 struct scsi_device *sdev_rpmb;
4961 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004962
4963 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4964 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4965 if (IS_ERR(hba->sdev_ufs_device)) {
4966 ret = PTR_ERR(hba->sdev_ufs_device);
4967 hba->sdev_ufs_device = NULL;
4968 goto out;
4969 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004970 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004971
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004972 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004973 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004974 if (IS_ERR(sdev_boot)) {
4975 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004976 goto remove_sdev_ufs_device;
4977 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004978 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004979
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004980 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004981 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004982 if (IS_ERR(sdev_rpmb)) {
4983 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004984 goto remove_sdev_boot;
4985 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004986 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004987 goto out;
4988
4989remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004990 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004991remove_sdev_ufs_device:
4992 scsi_remove_device(hba->sdev_ufs_device);
4993out:
4994 return ret;
4995}
4996
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02004997static int ufs_get_device_desc(struct ufs_hba *hba,
4998 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004999{
5000 int err;
5001 u8 model_index;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005002 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
5003 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005004
Potomski, MichalXe1928452017-02-23 09:05:30 +00005005 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005006 if (err) {
5007 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
5008 __func__, err);
5009 goto out;
5010 }
5011
5012 /*
5013 * getting vendor (manufacturerID) and Bank Index in big endian
5014 * format
5015 */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005016 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005017 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
5018
5019 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
5020
5021 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00005022 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005023 if (err) {
5024 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5025 __func__, err);
5026 goto out;
5027 }
5028
Potomski, MichalXe1928452017-02-23 09:05:30 +00005029 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005030 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005031 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5032 MAX_MODEL_LEN));
5033
5034 /* Null terminate the model string */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005035 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005036
5037out:
5038 return err;
5039}
5040
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005041static void ufs_fixup_device_setup(struct ufs_hba *hba,
5042 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005043{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005044 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005045
5046 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005047 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
5048 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
5049 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005050 !strcmp(f->card.model, UFS_ANY_MODEL)))
5051 hba->dev_quirks |= f->quirk;
5052 }
5053}
5054
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005055/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005056 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5057 * @hba: per-adapter instance
5058 *
5059 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5060 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5061 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5062 * the hibern8 exit latency.
5063 *
5064 * Returns zero on success, non-zero error value on failure.
5065 */
5066static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5067{
5068 int ret = 0;
5069 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5070
5071 ret = ufshcd_dme_peer_get(hba,
5072 UIC_ARG_MIB_SEL(
5073 RX_MIN_ACTIVATETIME_CAPABILITY,
5074 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5075 &peer_rx_min_activatetime);
5076 if (ret)
5077 goto out;
5078
5079 /* make sure proper unit conversion is applied */
5080 tuned_pa_tactivate =
5081 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5082 / PA_TACTIVATE_TIME_UNIT_US);
5083 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5084 tuned_pa_tactivate);
5085
5086out:
5087 return ret;
5088}
5089
5090/**
5091 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5092 * @hba: per-adapter instance
5093 *
5094 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5095 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5096 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5097 * This optimal value can help reduce the hibern8 exit latency.
5098 *
5099 * Returns zero on success, non-zero error value on failure.
5100 */
5101static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5102{
5103 int ret = 0;
5104 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5105 u32 max_hibern8_time, tuned_pa_hibern8time;
5106
5107 ret = ufshcd_dme_get(hba,
5108 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5109 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5110 &local_tx_hibern8_time_cap);
5111 if (ret)
5112 goto out;
5113
5114 ret = ufshcd_dme_peer_get(hba,
5115 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5116 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5117 &peer_rx_hibern8_time_cap);
5118 if (ret)
5119 goto out;
5120
5121 max_hibern8_time = max(local_tx_hibern8_time_cap,
5122 peer_rx_hibern8_time_cap);
5123 /* make sure proper unit conversion is applied */
5124 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5125 / PA_HIBERN8_TIME_UNIT_US);
5126 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5127 tuned_pa_hibern8time);
5128out:
5129 return ret;
5130}
5131
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005132/**
5133 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5134 * less than device PA_TACTIVATE time.
5135 * @hba: per-adapter instance
5136 *
5137 * Some UFS devices require host PA_TACTIVATE to be lower than device
5138 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5139 * for such devices.
5140 *
5141 * Returns zero on success, non-zero error value on failure.
5142 */
5143static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5144{
5145 int ret = 0;
5146 u32 granularity, peer_granularity;
5147 u32 pa_tactivate, peer_pa_tactivate;
5148 u32 pa_tactivate_us, peer_pa_tactivate_us;
5149 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5150
5151 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5152 &granularity);
5153 if (ret)
5154 goto out;
5155
5156 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5157 &peer_granularity);
5158 if (ret)
5159 goto out;
5160
5161 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5162 (granularity > PA_GRANULARITY_MAX_VAL)) {
5163 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5164 __func__, granularity);
5165 return -EINVAL;
5166 }
5167
5168 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5169 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5170 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5171 __func__, peer_granularity);
5172 return -EINVAL;
5173 }
5174
5175 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5176 if (ret)
5177 goto out;
5178
5179 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5180 &peer_pa_tactivate);
5181 if (ret)
5182 goto out;
5183
5184 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5185 peer_pa_tactivate_us = peer_pa_tactivate *
5186 gran_to_us_table[peer_granularity - 1];
5187
5188 if (pa_tactivate_us > peer_pa_tactivate_us) {
5189 u32 new_peer_pa_tactivate;
5190
5191 new_peer_pa_tactivate = pa_tactivate_us /
5192 gran_to_us_table[peer_granularity - 1];
5193 new_peer_pa_tactivate++;
5194 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5195 new_peer_pa_tactivate);
5196 }
5197
5198out:
5199 return ret;
5200}
5201
Yaniv Gardi37113102016-03-10 17:37:16 +02005202static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5203{
5204 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5205 ufshcd_tune_pa_tactivate(hba);
5206 ufshcd_tune_pa_hibern8time(hba);
5207 }
5208
5209 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5210 /* set 1ms timeout for PA_TACTIVATE */
5211 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005212
5213 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5214 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavanibedc6292017-04-04 19:32:13 +00005215
5216 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005217}
5218
Potomski, MichalXe1928452017-02-23 09:05:30 +00005219static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
5220{
5221 int err;
5222
5223 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
5224 &hba->desc_size.dev_desc);
5225 if (err)
5226 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5227
5228 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
5229 &hba->desc_size.pwr_desc);
5230 if (err)
5231 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5232
5233 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
5234 &hba->desc_size.interc_desc);
5235 if (err)
5236 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5237
5238 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
5239 &hba->desc_size.conf_desc);
5240 if (err)
5241 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5242
5243 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
5244 &hba->desc_size.unit_desc);
5245 if (err)
5246 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5247
5248 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
5249 &hba->desc_size.geom_desc);
5250 if (err)
5251 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5252}
5253
5254static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
5255{
5256 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5257 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5258 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5259 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5260 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5261 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5262}
5263
Yaniv Gardi37113102016-03-10 17:37:16 +02005264/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005265 * ufshcd_probe_hba - probe hba to detect device and initialize
5266 * @hba: per-adapter instance
5267 *
5268 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305269 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005270static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305271{
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005272 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305273 int ret;
5274
5275 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305276 if (ret)
5277 goto out;
5278
Yaniv Gardi50646362014-10-23 13:25:13 +03005279 ufshcd_init_pwr_info(hba);
5280
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005281 /* set the default level for urgent bkops */
5282 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5283 hba->is_urgent_bkops_lvl_checked = false;
5284
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005285 /* UniPro link is active now */
5286 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305287
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305288 ret = ufshcd_verify_dev_init(hba);
5289 if (ret)
5290 goto out;
5291
Dolev Raviv68078d52013-07-30 00:35:58 +05305292 ret = ufshcd_complete_dev_init(hba);
5293 if (ret)
5294 goto out;
5295
Potomski, MichalXe1928452017-02-23 09:05:30 +00005296 /* Init check for device descriptor sizes */
5297 ufshcd_init_desc_sizes(hba);
5298
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005299 ret = ufs_get_device_desc(hba, &card);
5300 if (ret) {
5301 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5302 __func__, ret);
5303 goto out;
5304 }
5305
5306 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02005307 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005308
5309 ret = ufshcd_set_vccq_rail_unused(hba,
5310 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5311 if (ret)
5312 goto out;
5313
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005314 /* UFS device is also active now */
5315 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305316 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005317 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305318
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005319 if (ufshcd_get_max_pwr_mode(hba)) {
5320 dev_err(hba->dev,
5321 "%s: Failed getting max supported power mode\n",
5322 __func__);
5323 } else {
5324 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
5325 if (ret)
5326 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5327 __func__, ret);
5328 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005329
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005330 /* set the state as operational after switching to desired gear */
5331 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005332
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005333 /*
5334 * If we are in error handling context or in power management callbacks
5335 * context, no need to scan the host
5336 */
5337 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5338 bool flag;
5339
5340 /* clear any previous UFS device information */
5341 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005342 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5343 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005344 hba->dev_info.f_power_on_wp_en = flag;
5345
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005346 if (!hba->is_init_prefetch)
5347 ufshcd_init_icc_levels(hba);
5348
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005349 /* Add required well known logical units to scsi mid layer */
5350 if (ufshcd_scsi_add_wlus(hba))
5351 goto out;
5352
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305353 scsi_scan_host(hba->host);
5354 pm_runtime_put_sync(hba->dev);
5355 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005356
5357 if (!hba->is_init_prefetch)
5358 hba->is_init_prefetch = true;
5359
Sahitya Tummala856b3482014-09-25 15:32:34 +03005360 /* Resume devfreq after UFS device is detected */
5361 if (ufshcd_is_clkscaling_enabled(hba))
5362 devfreq_resume_device(hba->devfreq);
5363
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305364out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005365 /*
5366 * If we failed to initialize the device or the device is not
5367 * present, turn off the power/clocks etc.
5368 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005369 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5370 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005371 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005372 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005373
5374 return ret;
5375}
5376
5377/**
5378 * ufshcd_async_scan - asynchronous execution for probing hba
5379 * @data: data pointer to pass to this function
5380 * @cookie: cookie data
5381 */
5382static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5383{
5384 struct ufs_hba *hba = (struct ufs_hba *)data;
5385
5386 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305387}
5388
Yaniv Gardif550c652016-03-10 17:37:07 +02005389static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5390{
5391 unsigned long flags;
5392 struct Scsi_Host *host;
5393 struct ufs_hba *hba;
5394 int index;
5395 bool found = false;
5396
5397 if (!scmd || !scmd->device || !scmd->device->host)
5398 return BLK_EH_NOT_HANDLED;
5399
5400 host = scmd->device->host;
5401 hba = shost_priv(host);
5402 if (!hba)
5403 return BLK_EH_NOT_HANDLED;
5404
5405 spin_lock_irqsave(host->host_lock, flags);
5406
5407 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5408 if (hba->lrb[index].cmd == scmd) {
5409 found = true;
5410 break;
5411 }
5412 }
5413
5414 spin_unlock_irqrestore(host->host_lock, flags);
5415
5416 /*
5417 * Bypass SCSI error handling and reset the block layer timer if this
5418 * SCSI command was not actually dispatched to UFS driver, otherwise
5419 * let SCSI layer handle the error as usual.
5420 */
5421 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5422}
5423
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305424static struct scsi_host_template ufshcd_driver_template = {
5425 .module = THIS_MODULE,
5426 .name = UFSHCD,
5427 .proc_name = UFSHCD,
5428 .queuecommand = ufshcd_queuecommand,
5429 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005430 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305431 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005432 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305433 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305434 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5435 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005436 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305437 .this_id = -1,
5438 .sg_tablesize = SG_ALL,
5439 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5440 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005441 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005442 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305443};
5444
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005445static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5446 int ua)
5447{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005448 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005449
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005450 if (!vreg)
5451 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005452
Stanley Chu75246f572019-03-28 17:16:25 +08005453 /*
5454 * "set_load" operation shall be required on those regulators
5455 * which specifically configured current limitation. Otherwise
5456 * zero max_uA may cause unexpected behavior when regulator is
5457 * enabled or set as high power mode.
5458 */
5459 if (!vreg->max_uA)
5460 return 0;
5461
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005462 ret = regulator_set_load(vreg->reg, ua);
5463 if (ret < 0) {
5464 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5465 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005466 }
5467
5468 return ret;
5469}
5470
5471static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5472 struct ufs_vreg *vreg)
5473{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005474 if (!vreg)
5475 return 0;
5476 else if (vreg->unused)
5477 return 0;
5478 else
5479 return ufshcd_config_vreg_load(hba->dev, vreg,
5480 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005481}
5482
5483static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5484 struct ufs_vreg *vreg)
5485{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005486 if (!vreg)
5487 return 0;
5488 else if (vreg->unused)
5489 return 0;
5490 else
5491 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005492}
5493
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005494static int ufshcd_config_vreg(struct device *dev,
5495 struct ufs_vreg *vreg, bool on)
5496{
5497 int ret = 0;
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005498 struct regulator *reg;
5499 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005500 int min_uV, uA_load;
5501
5502 BUG_ON(!vreg);
5503
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005504 reg = vreg->reg;
5505 name = vreg->name;
5506
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005507 if (regulator_count_voltages(reg) > 0) {
Stanley Chu41761292019-03-28 17:16:24 +08005508 if (vreg->min_uV && vreg->max_uV) {
5509 min_uV = on ? vreg->min_uV : 0;
5510 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5511 if (ret) {
5512 dev_err(dev,
5513 "%s: %s set voltage failed, err=%d\n",
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005514 __func__, name, ret);
Stanley Chu41761292019-03-28 17:16:24 +08005515 goto out;
5516 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005517 }
5518
5519 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005520 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5521 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005522 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005523 }
5524out:
5525 return ret;
5526}
5527
5528static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5529{
5530 int ret = 0;
5531
Yaniv Gardi60f01872016-03-10 17:37:11 +02005532 if (!vreg)
5533 goto out;
5534 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005535 goto out;
5536
5537 ret = ufshcd_config_vreg(dev, vreg, true);
5538 if (!ret)
5539 ret = regulator_enable(vreg->reg);
5540
5541 if (!ret)
5542 vreg->enabled = true;
5543 else
5544 dev_err(dev, "%s: %s enable failed, err=%d\n",
5545 __func__, vreg->name, ret);
5546out:
5547 return ret;
5548}
5549
5550static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5551{
5552 int ret = 0;
5553
Yaniv Gardi60f01872016-03-10 17:37:11 +02005554 if (!vreg)
5555 goto out;
5556 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005557 goto out;
5558
5559 ret = regulator_disable(vreg->reg);
5560
5561 if (!ret) {
5562 /* ignore errors on applying disable config */
5563 ufshcd_config_vreg(dev, vreg, false);
5564 vreg->enabled = false;
5565 } else {
5566 dev_err(dev, "%s: %s disable failed, err=%d\n",
5567 __func__, vreg->name, ret);
5568 }
5569out:
5570 return ret;
5571}
5572
5573static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5574{
5575 int ret = 0;
5576 struct device *dev = hba->dev;
5577 struct ufs_vreg_info *info = &hba->vreg_info;
5578
5579 if (!info)
5580 goto out;
5581
5582 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5583 if (ret)
5584 goto out;
5585
5586 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5587 if (ret)
5588 goto out;
5589
5590 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5591 if (ret)
5592 goto out;
5593
5594out:
5595 if (ret) {
5596 ufshcd_toggle_vreg(dev, info->vccq2, false);
5597 ufshcd_toggle_vreg(dev, info->vccq, false);
5598 ufshcd_toggle_vreg(dev, info->vcc, false);
5599 }
5600 return ret;
5601}
5602
Raviv Shvili6a771a62014-09-25 15:32:24 +03005603static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5604{
5605 struct ufs_vreg_info *info = &hba->vreg_info;
5606
5607 if (info)
5608 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5609
5610 return 0;
5611}
5612
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005613static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5614{
5615 int ret = 0;
5616
5617 if (!vreg)
5618 goto out;
5619
5620 vreg->reg = devm_regulator_get(dev, vreg->name);
5621 if (IS_ERR(vreg->reg)) {
5622 ret = PTR_ERR(vreg->reg);
5623 dev_err(dev, "%s: %s get failed, err=%d\n",
5624 __func__, vreg->name, ret);
5625 }
5626out:
5627 return ret;
5628}
5629
5630static int ufshcd_init_vreg(struct ufs_hba *hba)
5631{
5632 int ret = 0;
5633 struct device *dev = hba->dev;
5634 struct ufs_vreg_info *info = &hba->vreg_info;
5635
5636 if (!info)
5637 goto out;
5638
5639 ret = ufshcd_get_vreg(dev, info->vcc);
5640 if (ret)
5641 goto out;
5642
5643 ret = ufshcd_get_vreg(dev, info->vccq);
5644 if (ret)
5645 goto out;
5646
5647 ret = ufshcd_get_vreg(dev, info->vccq2);
5648out:
5649 return ret;
5650}
5651
Raviv Shvili6a771a62014-09-25 15:32:24 +03005652static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5653{
5654 struct ufs_vreg_info *info = &hba->vreg_info;
5655
5656 if (info)
5657 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5658
5659 return 0;
5660}
5661
Yaniv Gardi60f01872016-03-10 17:37:11 +02005662static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5663{
5664 int ret = 0;
5665 struct ufs_vreg_info *info = &hba->vreg_info;
5666
5667 if (!info)
5668 goto out;
5669 else if (!info->vccq)
5670 goto out;
5671
5672 if (unused) {
5673 /* shut off the rail here */
5674 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5675 /*
5676 * Mark this rail as no longer used, so it doesn't get enabled
5677 * later by mistake
5678 */
5679 if (!ret)
5680 info->vccq->unused = true;
5681 } else {
5682 /*
5683 * rail should have been already enabled hence just make sure
5684 * that unused flag is cleared.
5685 */
5686 info->vccq->unused = false;
5687 }
5688out:
5689 return ret;
5690}
5691
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005692static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5693 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005694{
5695 int ret = 0;
5696 struct ufs_clk_info *clki;
5697 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005698 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005699
5700 if (!head || list_empty(head))
5701 goto out;
5702
5703 list_for_each_entry(clki, head, list) {
5704 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005705 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5706 continue;
5707
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005708 if (on && !clki->enabled) {
5709 ret = clk_prepare_enable(clki->clk);
5710 if (ret) {
5711 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5712 __func__, clki->name, ret);
5713 goto out;
5714 }
5715 } else if (!on && clki->enabled) {
5716 clk_disable_unprepare(clki->clk);
5717 }
5718 clki->enabled = on;
5719 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5720 clki->name, on ? "en" : "dis");
5721 }
5722 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005723
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005724 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005725out:
5726 if (ret) {
5727 list_for_each_entry(clki, head, list) {
5728 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5729 clk_disable_unprepare(clki->clk);
5730 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005731 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005732 spin_lock_irqsave(hba->host->host_lock, flags);
5733 hba->clk_gating.state = CLKS_ON;
5734 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005735 }
5736 return ret;
5737}
5738
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005739static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5740{
5741 return __ufshcd_setup_clocks(hba, on, false);
5742}
5743
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005744static int ufshcd_init_clocks(struct ufs_hba *hba)
5745{
5746 int ret = 0;
5747 struct ufs_clk_info *clki;
5748 struct device *dev = hba->dev;
5749 struct list_head *head = &hba->clk_list_head;
5750
5751 if (!head || list_empty(head))
5752 goto out;
5753
5754 list_for_each_entry(clki, head, list) {
5755 if (!clki->name)
5756 continue;
5757
5758 clki->clk = devm_clk_get(dev, clki->name);
5759 if (IS_ERR(clki->clk)) {
5760 ret = PTR_ERR(clki->clk);
5761 dev_err(dev, "%s: %s clk get failed, %d\n",
5762 __func__, clki->name, ret);
5763 goto out;
5764 }
5765
5766 if (clki->max_freq) {
5767 ret = clk_set_rate(clki->clk, clki->max_freq);
5768 if (ret) {
5769 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5770 __func__, clki->name,
5771 clki->max_freq, ret);
5772 goto out;
5773 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005774 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005775 }
5776 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5777 clki->name, clk_get_rate(clki->clk));
5778 }
5779out:
5780 return ret;
5781}
5782
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005783static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5784{
5785 int err = 0;
5786
5787 if (!hba->vops)
5788 goto out;
5789
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005790 err = ufshcd_vops_init(hba);
5791 if (err)
5792 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005793
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005794 err = ufshcd_vops_setup_regulators(hba, true);
5795 if (err)
5796 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005797
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005798 goto out;
5799
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005800out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005801 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005802out:
5803 if (err)
5804 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005805 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005806 return err;
5807}
5808
5809static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5810{
5811 if (!hba->vops)
5812 return;
5813
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005814 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005815
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005816 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005817
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005818 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005819}
5820
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005821static int ufshcd_hba_init(struct ufs_hba *hba)
5822{
5823 int err;
5824
Raviv Shvili6a771a62014-09-25 15:32:24 +03005825 /*
5826 * Handle host controller power separately from the UFS device power
5827 * rails as it will help controlling the UFS host controller power
5828 * collapse easily which is different than UFS device power collapse.
5829 * Also, enable the host controller power before we go ahead with rest
5830 * of the initialization here.
5831 */
5832 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005833 if (err)
5834 goto out;
5835
Raviv Shvili6a771a62014-09-25 15:32:24 +03005836 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005837 if (err)
5838 goto out;
5839
Raviv Shvili6a771a62014-09-25 15:32:24 +03005840 err = ufshcd_init_clocks(hba);
5841 if (err)
5842 goto out_disable_hba_vreg;
5843
5844 err = ufshcd_setup_clocks(hba, true);
5845 if (err)
5846 goto out_disable_hba_vreg;
5847
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005848 err = ufshcd_init_vreg(hba);
5849 if (err)
5850 goto out_disable_clks;
5851
5852 err = ufshcd_setup_vreg(hba, true);
5853 if (err)
5854 goto out_disable_clks;
5855
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005856 err = ufshcd_variant_hba_init(hba);
5857 if (err)
5858 goto out_disable_vreg;
5859
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005860 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005861 goto out;
5862
5863out_disable_vreg:
5864 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005865out_disable_clks:
5866 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005867out_disable_hba_vreg:
5868 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005869out:
5870 return err;
5871}
5872
5873static void ufshcd_hba_exit(struct ufs_hba *hba)
5874{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005875 if (hba->is_powered) {
5876 ufshcd_variant_hba_exit(hba);
5877 ufshcd_setup_vreg(hba, false);
5878 ufshcd_setup_clocks(hba, false);
5879 ufshcd_setup_hba_vreg(hba, false);
5880 hba->is_powered = false;
5881 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005882}
5883
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005884static int
5885ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305886{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005887 unsigned char cmd[6] = {REQUEST_SENSE,
5888 0,
5889 0,
5890 0,
5891 SCSI_SENSE_BUFFERSIZE,
5892 0};
5893 char *buffer;
5894 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305895
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005896 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5897 if (!buffer) {
5898 ret = -ENOMEM;
5899 goto out;
5900 }
5901
5902 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
5903 SCSI_SENSE_BUFFERSIZE, NULL,
5904 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5905 if (ret)
5906 pr_err("%s: failed with err %d\n", __func__, ret);
5907
5908 kfree(buffer);
5909out:
5910 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305911}
5912
5913/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005914 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5915 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305916 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005917 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305918 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005919 * Returns 0 if requested power mode is set successfully
5920 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305921 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005922static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5923 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305924{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005925 unsigned char cmd[6] = { START_STOP };
5926 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005927 struct scsi_device *sdp;
5928 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005929 int ret;
5930
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005931 spin_lock_irqsave(hba->host->host_lock, flags);
5932 sdp = hba->sdev_ufs_device;
5933 if (sdp) {
5934 ret = scsi_device_get(sdp);
5935 if (!ret && !scsi_device_online(sdp)) {
5936 ret = -ENODEV;
5937 scsi_device_put(sdp);
5938 }
5939 } else {
5940 ret = -ENODEV;
5941 }
5942 spin_unlock_irqrestore(hba->host->host_lock, flags);
5943
5944 if (ret)
5945 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005946
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305947 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005948 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5949 * handling, which would wait for host to be resumed. Since we know
5950 * we are functional while we are here, skip host resume in error
5951 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305952 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005953 hba->host->eh_noresume = 1;
5954 if (hba->wlun_dev_clr_ua) {
5955 ret = ufshcd_send_request_sense(hba, sdp);
5956 if (ret)
5957 goto out;
5958 /* Unit attention condition is cleared now */
5959 hba->wlun_dev_clr_ua = false;
5960 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305961
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005962 cmd[4] = pwr_mode << 4;
5963
5964 /*
5965 * Current function would be generally called from the power management
5966 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5967 * already suspended childs.
5968 */
5969 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5970 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5971 if (ret) {
5972 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005973 "START_STOP failed for power mode: %d, result %x\n",
5974 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005975 if (driver_byte(ret) & DRIVER_SENSE)
5976 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005977 }
5978
5979 if (!ret)
5980 hba->curr_dev_pwr_mode = pwr_mode;
5981out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005982 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005983 hba->host->eh_noresume = 0;
5984 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305985}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305986
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005987static int ufshcd_link_state_transition(struct ufs_hba *hba,
5988 enum uic_link_state req_link_state,
5989 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305990{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005991 int ret = 0;
5992
5993 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305994 return 0;
5995
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005996 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5997 ret = ufshcd_uic_hibern8_enter(hba);
5998 if (!ret)
5999 ufshcd_set_link_hibern8(hba);
6000 else
6001 goto out;
6002 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306003 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006004 * If autobkops is enabled, link can't be turned off because
6005 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306006 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006007 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
6008 (!check_for_bkops || (check_for_bkops &&
6009 !hba->auto_bkops_enabled))) {
6010 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02006011 * Let's make sure that link is in low power mode, we are doing
6012 * this currently by putting the link in Hibern8. Otherway to
6013 * put the link in low power mode is to send the DME end point
6014 * to device and then send the DME reset command to local
6015 * unipro. But putting the link in hibern8 is much faster.
6016 */
6017 ret = ufshcd_uic_hibern8_enter(hba);
6018 if (ret)
6019 goto out;
6020 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006021 * Change controller state to "reset state" which
6022 * should also put the link in off/reset state
6023 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02006024 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006025 /*
6026 * TODO: Check if we need any delay to make sure that
6027 * controller is reset
6028 */
6029 ufshcd_set_link_off(hba);
6030 }
6031
6032out:
6033 return ret;
6034}
6035
6036static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6037{
6038 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006039 * It seems some UFS devices may keep drawing more than sleep current
6040 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6041 * To avoid this situation, add 2ms delay before putting these UFS
6042 * rails in LPM mode.
6043 */
6044 if (!ufshcd_is_link_active(hba) &&
6045 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6046 usleep_range(2000, 2100);
6047
6048 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006049 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6050 * power.
6051 *
6052 * If UFS device and link is in OFF state, all power supplies (VCC,
6053 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6054 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6055 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6056 *
6057 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6058 * in low power state which would save some power.
6059 */
6060 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6061 !hba->dev_info.is_lu_power_on_wp) {
6062 ufshcd_setup_vreg(hba, false);
6063 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6064 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6065 if (!ufshcd_is_link_active(hba)) {
6066 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6067 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6068 }
6069 }
6070}
6071
6072static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6073{
6074 int ret = 0;
6075
6076 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6077 !hba->dev_info.is_lu_power_on_wp) {
6078 ret = ufshcd_setup_vreg(hba, true);
6079 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6080 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
6081 if (!ret && !ufshcd_is_link_active(hba)) {
6082 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6083 if (ret)
6084 goto vcc_disable;
6085 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6086 if (ret)
6087 goto vccq_lpm;
6088 }
6089 }
6090 goto out;
6091
6092vccq_lpm:
6093 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6094vcc_disable:
6095 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6096out:
6097 return ret;
6098}
6099
6100static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6101{
6102 if (ufshcd_is_link_off(hba))
6103 ufshcd_setup_hba_vreg(hba, false);
6104}
6105
6106static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6107{
6108 if (ufshcd_is_link_off(hba))
6109 ufshcd_setup_hba_vreg(hba, true);
6110}
6111
6112/**
6113 * ufshcd_suspend - helper function for suspend operations
6114 * @hba: per adapter instance
6115 * @pm_op: desired low power operation type
6116 *
6117 * This function will try to put the UFS device and link into low power
6118 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6119 * (System PM level).
6120 *
6121 * If this function is called during shutdown, it will make sure that
6122 * both UFS device and UFS link is powered off.
6123 *
6124 * NOTE: UFS device & link must be active before we enter in this function.
6125 *
6126 * Returns 0 for success and non-zero for failure
6127 */
6128static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6129{
6130 int ret = 0;
6131 enum ufs_pm_level pm_lvl;
6132 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6133 enum uic_link_state req_link_state;
6134
6135 hba->pm_op_in_progress = 1;
6136 if (!ufshcd_is_shutdown_pm(pm_op)) {
6137 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6138 hba->rpm_lvl : hba->spm_lvl;
6139 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6140 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6141 } else {
6142 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6143 req_link_state = UIC_LINK_OFF_STATE;
6144 }
6145
6146 /*
6147 * If we can't transition into any of the low power modes
6148 * just gate the clocks.
6149 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006150 ufshcd_hold(hba, false);
6151 hba->clk_gating.is_suspended = true;
6152
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006153 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6154 req_link_state == UIC_LINK_ACTIVE_STATE) {
6155 goto disable_clks;
6156 }
6157
6158 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6159 (req_link_state == hba->uic_link_state))
6160 goto out;
6161
6162 /* UFS device & link must be active before we enter in this function */
6163 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6164 ret = -EINVAL;
6165 goto out;
6166 }
6167
6168 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006169 if (ufshcd_can_autobkops_during_suspend(hba)) {
6170 /*
6171 * The device is idle with no requests in the queue,
6172 * allow background operations if bkops status shows
6173 * that performance might be impacted.
6174 */
6175 ret = ufshcd_urgent_bkops(hba);
6176 if (ret)
6177 goto enable_gating;
6178 } else {
6179 /* make sure that auto bkops is disabled */
6180 ufshcd_disable_auto_bkops(hba);
6181 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006182 }
6183
6184 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6185 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6186 !ufshcd_is_runtime_pm(pm_op))) {
6187 /* ensure that bkops is disabled */
6188 ufshcd_disable_auto_bkops(hba);
6189 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6190 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006191 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006192 }
6193
6194 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6195 if (ret)
6196 goto set_dev_active;
6197
6198 ufshcd_vreg_set_lpm(hba);
6199
6200disable_clks:
6201 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03006202 * The clock scaling needs access to controller registers. Hence, Wait
6203 * for pending clock scaling work to be done before clocks are
6204 * turned off.
6205 */
6206 if (ufshcd_is_clkscaling_enabled(hba)) {
6207 devfreq_suspend_device(hba->devfreq);
6208 hba->clk_scaling.window_start_t = 0;
6209 }
6210 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006211 * Call vendor specific suspend callback. As these callbacks may access
6212 * vendor specific host controller register space call them before the
6213 * host clocks are ON.
6214 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006215 ret = ufshcd_vops_suspend(hba, pm_op);
6216 if (ret)
6217 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006218
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006219 ret = ufshcd_vops_setup_clocks(hba, false);
6220 if (ret)
6221 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006222
6223 if (!ufshcd_is_link_active(hba))
6224 ufshcd_setup_clocks(hba, false);
6225 else
6226 /* If link is active, device ref_clk can't be switched off */
6227 __ufshcd_setup_clocks(hba, false, true);
6228
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006229 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006230 /*
6231 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006232 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006233 */
6234 ufshcd_disable_irq(hba);
6235 /* Put the host controller in low power mode if possible */
6236 ufshcd_hba_vreg_set_lpm(hba);
6237 goto out;
6238
6239vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006240 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006241set_link_active:
6242 ufshcd_vreg_set_hpm(hba);
6243 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6244 ufshcd_set_link_active(hba);
6245 else if (ufshcd_is_link_off(hba))
6246 ufshcd_host_reset_and_restore(hba);
6247set_dev_active:
6248 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6249 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006250enable_gating:
6251 hba->clk_gating.is_suspended = false;
6252 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006253out:
6254 hba->pm_op_in_progress = 0;
6255 return ret;
6256}
6257
6258/**
6259 * ufshcd_resume - helper function for resume operations
6260 * @hba: per adapter instance
6261 * @pm_op: runtime PM or system PM
6262 *
6263 * This function basically brings the UFS device, UniPro link and controller
6264 * to active state.
6265 *
6266 * Returns 0 for success and non-zero for failure
6267 */
6268static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6269{
6270 int ret;
6271 enum uic_link_state old_link_state;
6272
6273 hba->pm_op_in_progress = 1;
6274 old_link_state = hba->uic_link_state;
6275
6276 ufshcd_hba_vreg_set_hpm(hba);
6277 /* Make sure clocks are enabled before accessing controller */
6278 ret = ufshcd_setup_clocks(hba, true);
6279 if (ret)
6280 goto out;
6281
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006282 /* enable the host irq as host controller would be active soon */
6283 ret = ufshcd_enable_irq(hba);
6284 if (ret)
6285 goto disable_irq_and_vops_clks;
6286
6287 ret = ufshcd_vreg_set_hpm(hba);
6288 if (ret)
6289 goto disable_irq_and_vops_clks;
6290
6291 /*
6292 * Call vendor specific resume callback. As these callbacks may access
6293 * vendor specific host controller register space call them when the
6294 * host clocks are ON.
6295 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006296 ret = ufshcd_vops_resume(hba, pm_op);
6297 if (ret)
6298 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006299
6300 if (ufshcd_is_link_hibern8(hba)) {
6301 ret = ufshcd_uic_hibern8_exit(hba);
6302 if (!ret)
6303 ufshcd_set_link_active(hba);
6304 else
6305 goto vendor_suspend;
6306 } else if (ufshcd_is_link_off(hba)) {
6307 ret = ufshcd_host_reset_and_restore(hba);
6308 /*
6309 * ufshcd_host_reset_and_restore() should have already
6310 * set the link state as active
6311 */
6312 if (ret || !ufshcd_is_link_active(hba))
6313 goto vendor_suspend;
6314 }
6315
6316 if (!ufshcd_is_ufs_dev_active(hba)) {
6317 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6318 if (ret)
6319 goto set_old_link_state;
6320 }
6321
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08006322 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
6323 ufshcd_enable_auto_bkops(hba);
6324 else
6325 /*
6326 * If BKOPs operations are urgently needed at this moment then
6327 * keep auto-bkops enabled or else disable it.
6328 */
6329 ufshcd_urgent_bkops(hba);
6330
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006331 hba->clk_gating.is_suspended = false;
6332
Sahitya Tummala856b3482014-09-25 15:32:34 +03006333 if (ufshcd_is_clkscaling_enabled(hba))
6334 devfreq_resume_device(hba->devfreq);
6335
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006336 /* Schedule clock gating in case of no access to UFS device yet */
6337 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006338 goto out;
6339
6340set_old_link_state:
6341 ufshcd_link_state_transition(hba, old_link_state, 0);
6342vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006343 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006344disable_vreg:
6345 ufshcd_vreg_set_lpm(hba);
6346disable_irq_and_vops_clks:
6347 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006348 ufshcd_setup_clocks(hba, false);
6349out:
6350 hba->pm_op_in_progress = 0;
6351 return ret;
6352}
6353
6354/**
6355 * ufshcd_system_suspend - system suspend routine
6356 * @hba: per adapter instance
6357 * @pm_op: runtime PM or system PM
6358 *
6359 * Check the description of ufshcd_suspend() function for more details.
6360 *
6361 * Returns 0 for success and non-zero for failure
6362 */
6363int ufshcd_system_suspend(struct ufs_hba *hba)
6364{
6365 int ret = 0;
6366
6367 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006368 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006369
6370 if (pm_runtime_suspended(hba->dev)) {
6371 if (hba->rpm_lvl == hba->spm_lvl)
6372 /*
6373 * There is possibility that device may still be in
6374 * active state during the runtime suspend.
6375 */
6376 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6377 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6378 goto out;
6379
6380 /*
6381 * UFS device and/or UFS link low power states during runtime
6382 * suspend seems to be different than what is expected during
6383 * system suspend. Hence runtime resume the devic & link and
6384 * let the system suspend low power states to take effect.
6385 * TODO: If resume takes longer time, we might have optimize
6386 * it in future by not resuming everything if possible.
6387 */
6388 ret = ufshcd_runtime_resume(hba);
6389 if (ret)
6390 goto out;
6391 }
6392
6393 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6394out:
Dolev Ravive7850602014-09-25 15:32:36 +03006395 if (!ret)
6396 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006397 return ret;
6398}
6399EXPORT_SYMBOL(ufshcd_system_suspend);
6400
6401/**
6402 * ufshcd_system_resume - system resume routine
6403 * @hba: per adapter instance
6404 *
6405 * Returns 0 for success and non-zero for failure
6406 */
6407
6408int ufshcd_system_resume(struct ufs_hba *hba)
6409{
Yaniv Gardi675d9192016-10-17 17:09:24 -07006410 if (!hba)
6411 return -EINVAL;
6412
6413 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006414 /*
6415 * Let the runtime resume take care of resuming
6416 * if runtime suspended.
6417 */
6418 return 0;
6419
6420 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6421}
6422EXPORT_SYMBOL(ufshcd_system_resume);
6423
6424/**
6425 * ufshcd_runtime_suspend - runtime suspend routine
6426 * @hba: per adapter instance
6427 *
6428 * Check the description of ufshcd_suspend() function for more details.
6429 *
6430 * Returns 0 for success and non-zero for failure
6431 */
6432int ufshcd_runtime_suspend(struct ufs_hba *hba)
6433{
Yaniv Gardi675d9192016-10-17 17:09:24 -07006434 if (!hba)
6435 return -EINVAL;
6436
6437 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006438 return 0;
6439
6440 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306441}
6442EXPORT_SYMBOL(ufshcd_runtime_suspend);
6443
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006444/**
6445 * ufshcd_runtime_resume - runtime resume routine
6446 * @hba: per adapter instance
6447 *
6448 * This function basically brings the UFS device, UniPro link and controller
6449 * to active state. Following operations are done in this function:
6450 *
6451 * 1. Turn on all the controller related clocks
6452 * 2. Bring the UniPro link out of Hibernate state
6453 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6454 * to active state.
6455 * 4. If auto-bkops is enabled on the device, disable it.
6456 *
6457 * So following would be the possible power state after this function return
6458 * successfully:
6459 * S1: UFS device in Active state with VCC rail ON
6460 * UniPro link in Active state
6461 * All the UFS/UniPro controller clocks are ON
6462 *
6463 * Returns 0 for success and non-zero for failure
6464 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306465int ufshcd_runtime_resume(struct ufs_hba *hba)
6466{
Yaniv Gardi675d9192016-10-17 17:09:24 -07006467 if (!hba)
6468 return -EINVAL;
6469
6470 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306471 return 0;
Yaniv Gardi675d9192016-10-17 17:09:24 -07006472
6473 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306474}
6475EXPORT_SYMBOL(ufshcd_runtime_resume);
6476
6477int ufshcd_runtime_idle(struct ufs_hba *hba)
6478{
6479 return 0;
6480}
6481EXPORT_SYMBOL(ufshcd_runtime_idle);
6482
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306483/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006484 * ufshcd_shutdown - shutdown routine
6485 * @hba: per adapter instance
6486 *
6487 * This function would power off both UFS device and UFS link.
6488 *
6489 * Returns 0 always to allow force shutdown even in case of errors.
6490 */
6491int ufshcd_shutdown(struct ufs_hba *hba)
6492{
6493 int ret = 0;
6494
Stanley Chuea8a6a52019-09-18 12:20:38 +08006495 if (!hba->is_powered)
6496 goto out;
6497
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006498 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6499 goto out;
6500
6501 if (pm_runtime_suspended(hba->dev)) {
6502 ret = ufshcd_runtime_resume(hba);
6503 if (ret)
6504 goto out;
6505 }
6506
6507 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6508out:
6509 if (ret)
6510 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6511 /* allow force shutdown even in case of errors */
6512 return 0;
6513}
6514EXPORT_SYMBOL(ufshcd_shutdown);
6515
6516/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306517 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306518 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306519 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306520 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306521void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306522{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306523 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306524 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306525 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006526 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306527
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006528 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006529 if (ufshcd_is_clkscaling_enabled(hba))
6530 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006531 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306532}
6533EXPORT_SYMBOL_GPL(ufshcd_remove);
6534
6535/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006536 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6537 * @hba: pointer to Host Bus Adapter (HBA)
6538 */
6539void ufshcd_dealloc_host(struct ufs_hba *hba)
6540{
6541 scsi_host_put(hba->host);
6542}
6543EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6544
6545/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006546 * ufshcd_set_dma_mask - Set dma mask based on the controller
6547 * addressing capability
6548 * @hba: per adapter instance
6549 *
6550 * Returns 0 for success, non-zero for failure
6551 */
6552static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6553{
6554 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6555 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6556 return 0;
6557 }
6558 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6559}
6560
6561/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006562 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306563 * @dev: pointer to device handle
6564 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306565 * Returns 0 on success, non-zero value on failure
6566 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006567int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306568{
6569 struct Scsi_Host *host;
6570 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006571 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306572
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306573 if (!dev) {
6574 dev_err(dev,
6575 "Invalid memory reference for dev is NULL\n");
6576 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306577 goto out_error;
6578 }
6579
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306580 host = scsi_host_alloc(&ufshcd_driver_template,
6581 sizeof(struct ufs_hba));
6582 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306583 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306584 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306585 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306586 }
6587 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306588 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306589 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006590 *hba_handle = hba;
6591
6592out_error:
6593 return err;
6594}
6595EXPORT_SYMBOL(ufshcd_alloc_host);
6596
Sahitya Tummala856b3482014-09-25 15:32:34 +03006597static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6598{
6599 int ret = 0;
6600 struct ufs_clk_info *clki;
6601 struct list_head *head = &hba->clk_list_head;
6602
6603 if (!head || list_empty(head))
6604 goto out;
6605
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006606 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6607 if (ret)
6608 return ret;
6609
Sahitya Tummala856b3482014-09-25 15:32:34 +03006610 list_for_each_entry(clki, head, list) {
6611 if (!IS_ERR_OR_NULL(clki->clk)) {
6612 if (scale_up && clki->max_freq) {
6613 if (clki->curr_freq == clki->max_freq)
6614 continue;
6615 ret = clk_set_rate(clki->clk, clki->max_freq);
6616 if (ret) {
6617 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6618 __func__, clki->name,
6619 clki->max_freq, ret);
6620 break;
6621 }
6622 clki->curr_freq = clki->max_freq;
6623
6624 } else if (!scale_up && clki->min_freq) {
6625 if (clki->curr_freq == clki->min_freq)
6626 continue;
6627 ret = clk_set_rate(clki->clk, clki->min_freq);
6628 if (ret) {
6629 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6630 __func__, clki->name,
6631 clki->min_freq, ret);
6632 break;
6633 }
6634 clki->curr_freq = clki->min_freq;
6635 }
6636 }
6637 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6638 clki->name, clk_get_rate(clki->clk));
6639 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006640
6641 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6642
Sahitya Tummala856b3482014-09-25 15:32:34 +03006643out:
6644 return ret;
6645}
6646
6647static int ufshcd_devfreq_target(struct device *dev,
6648 unsigned long *freq, u32 flags)
6649{
6650 int err = 0;
6651 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani99a3bd32016-10-27 17:25:47 -07006652 bool release_clk_hold = false;
6653 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006654
6655 if (!ufshcd_is_clkscaling_enabled(hba))
6656 return -EINVAL;
6657
Subhash Jadavani99a3bd32016-10-27 17:25:47 -07006658 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6659 if (ufshcd_eh_in_progress(hba)) {
6660 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6661 return 0;
6662 }
6663
6664 if (ufshcd_is_clkgating_allowed(hba) &&
6665 (hba->clk_gating.state != CLKS_ON)) {
6666 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6667 /* hold the vote until the scaling work is completed */
6668 hba->clk_gating.active_reqs++;
6669 release_clk_hold = true;
6670 hba->clk_gating.state = CLKS_ON;
6671 } else {
6672 /*
6673 * Clock gating work seems to be running in parallel
6674 * hence skip scaling work to avoid deadlock between
6675 * current scaling work and gating work.
6676 */
6677 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6678 return 0;
6679 }
6680 }
6681 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6682
Sahitya Tummala856b3482014-09-25 15:32:34 +03006683 if (*freq == UINT_MAX)
6684 err = ufshcd_scale_clks(hba, true);
6685 else if (*freq == 0)
6686 err = ufshcd_scale_clks(hba, false);
6687
Subhash Jadavani99a3bd32016-10-27 17:25:47 -07006688 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6689 if (release_clk_hold)
6690 __ufshcd_release(hba);
6691 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6692
Sahitya Tummala856b3482014-09-25 15:32:34 +03006693 return err;
6694}
6695
6696static int ufshcd_devfreq_get_dev_status(struct device *dev,
6697 struct devfreq_dev_status *stat)
6698{
6699 struct ufs_hba *hba = dev_get_drvdata(dev);
6700 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6701 unsigned long flags;
6702
6703 if (!ufshcd_is_clkscaling_enabled(hba))
6704 return -EINVAL;
6705
6706 memset(stat, 0, sizeof(*stat));
6707
6708 spin_lock_irqsave(hba->host->host_lock, flags);
6709 if (!scaling->window_start_t)
6710 goto start_window;
6711
6712 if (scaling->is_busy_started)
6713 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6714 scaling->busy_start_t));
6715
6716 stat->total_time = jiffies_to_usecs((long)jiffies -
6717 (long)scaling->window_start_t);
6718 stat->busy_time = scaling->tot_busy_t;
6719start_window:
6720 scaling->window_start_t = jiffies;
6721 scaling->tot_busy_t = 0;
6722
6723 if (hba->outstanding_reqs) {
6724 scaling->busy_start_t = ktime_get();
6725 scaling->is_busy_started = true;
6726 } else {
6727 scaling->busy_start_t = ktime_set(0, 0);
6728 scaling->is_busy_started = false;
6729 }
6730 spin_unlock_irqrestore(hba->host->host_lock, flags);
6731 return 0;
6732}
6733
6734static struct devfreq_dev_profile ufs_devfreq_profile = {
6735 .polling_ms = 100,
6736 .target = ufshcd_devfreq_target,
6737 .get_dev_status = ufshcd_devfreq_get_dev_status,
6738};
6739
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006740/**
6741 * ufshcd_init - Driver initialization routine
6742 * @hba: per-adapter instance
6743 * @mmio_base: base register address
6744 * @irq: Interrupt line of device
6745 * Returns 0 on success, non-zero value on failure
6746 */
6747int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6748{
6749 int err;
6750 struct Scsi_Host *host = hba->host;
6751 struct device *dev = hba->dev;
6752
6753 if (!mmio_base) {
6754 dev_err(hba->dev,
6755 "Invalid memory reference for mmio_base is NULL\n");
6756 err = -ENODEV;
6757 goto out_error;
6758 }
6759
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306760 hba->mmio_base = mmio_base;
6761 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306762
Potomski, MichalXe1928452017-02-23 09:05:30 +00006763 /* Set descriptor lengths to specification defaults */
6764 ufshcd_def_desc_sizes(hba);
6765
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006766 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006767 if (err)
6768 goto out_error;
6769
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306770 /* Read capabilities registers */
6771 ufshcd_hba_capabilities(hba);
6772
6773 /* Get UFS version supported by the controller */
6774 hba->ufs_version = ufshcd_get_ufs_version(hba);
6775
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306776 /* Get Interrupt bit mask per version */
6777 hba->intr_mask = ufshcd_get_intr_mask(hba);
6778
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006779 err = ufshcd_set_dma_mask(hba);
6780 if (err) {
6781 dev_err(hba->dev, "set dma mask failed\n");
6782 goto out_disable;
6783 }
6784
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306785 /* Allocate memory for host memory space */
6786 err = ufshcd_memory_alloc(hba);
6787 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306788 dev_err(hba->dev, "Memory allocation failed\n");
6789 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306790 }
6791
6792 /* Configure LRB */
6793 ufshcd_host_memory_configure(hba);
6794
6795 host->can_queue = hba->nutrs;
6796 host->cmd_per_lun = hba->nutrs;
6797 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006798 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306799 host->max_channel = UFSHCD_MAX_CHANNEL;
6800 host->unique_id = host->host_no;
6801 host->max_cmd_len = MAX_CDB_SIZE;
6802
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006803 hba->max_pwr_info.is_valid = false;
6804
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306805 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306806 init_waitqueue_head(&hba->tm_wq);
6807 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306808
6809 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306810 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306811 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306812
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306813 /* Initialize UIC command mutex */
6814 mutex_init(&hba->uic_cmd_mutex);
6815
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306816 /* Initialize mutex for device management commands */
6817 mutex_init(&hba->dev_cmd.lock);
6818
6819 /* Initialize device management tag acquire wait queue */
6820 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6821
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006822 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006823
6824 /*
6825 * In order to avoid any spurious interrupt immediately after
6826 * registering UFS controller interrupt handler, clear any pending UFS
6827 * interrupt status and disable all the UFS interrupts.
6828 */
6829 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6830 REG_INTERRUPT_STATUS);
6831 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6832 /*
6833 * Make sure that UFS interrupts are disabled and any pending interrupt
6834 * status is cleared before registering UFS interrupt handler.
6835 */
6836 mb();
6837
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306838 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006839 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306840 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306841 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006842 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006843 } else {
6844 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306845 }
6846
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306847 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306848 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306849 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006850 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306851 }
6852
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306853 /* Host controller enable */
6854 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306855 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306856 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306857 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306858 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306859
Sahitya Tummala856b3482014-09-25 15:32:34 +03006860 if (ufshcd_is_clkscaling_enabled(hba)) {
6861 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6862 "simple_ondemand", NULL);
6863 if (IS_ERR(hba->devfreq)) {
6864 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6865 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006866 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006867 goto out_remove_scsi_host;
6868 }
6869 /* Suspend devfreq until the UFS device is detected */
6870 devfreq_suspend_device(hba->devfreq);
6871 hba->clk_scaling.window_start_t = 0;
6872 }
6873
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306874 /* Hold auto suspend until async scan completes */
6875 pm_runtime_get_sync(dev);
6876
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006877 /*
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006878 * We are assuming that device wasn't put in sleep/power-down
6879 * state exclusively during the boot stage before kernel.
6880 * This assumption helps avoid doing link startup twice during
6881 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006882 */
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006883 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006884
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306885 async_schedule(ufshcd_async_scan, hba);
6886
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306887 return 0;
6888
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306889out_remove_scsi_host:
6890 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006891exit_gating:
6892 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306893out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006894 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006895 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306896out_error:
6897 return err;
6898}
6899EXPORT_SYMBOL_GPL(ufshcd_init);
6900
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306901MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6902MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306903MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306904MODULE_LICENSE("GPL");
6905MODULE_VERSION(UFSHCD_DRIVER_VERSION);