blob: 0e41d4c541a3fdc791f35d9979450b5933323967 [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>
Mohan Srinivasane2d88782016-12-14 15:55:36 -080044#include <linux/blkdev.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053045#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020046#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053047#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053048
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053049#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
50 UTP_TASK_REQ_COMPL |\
51 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053052/* UIC command timeout, unit: ms */
53#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053054
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053055/* NOP OUT retries waiting for NOP IN response */
56#define NOP_OUT_RETRIES 10
57/* Timeout after 30 msecs if NOP OUT hangs without response */
58#define NOP_OUT_TIMEOUT 30 /* msecs */
59
Dolev Raviv68078d52013-07-30 00:35:58 +053060/* Query request retries */
61#define QUERY_REQ_RETRIES 10
62/* Query request timeout */
63#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020064/*
65 * Query request timeout for fDeviceInit flag
66 * fDeviceInit query response time for some devices is too large that default
67 * QUERY_REQ_TIMEOUT may not be enough for such devices.
68 */
69#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053070
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053071/* Task management command timeout */
72#define TM_CMD_TIMEOUT 100 /* msecs */
73
Yaniv Gardi64238fb2016-02-01 15:02:43 +020074/* maximum number of retries for a general UIC command */
75#define UFS_UIC_COMMAND_RETRIES 3
76
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030077/* maximum number of link-startup retries */
78#define DME_LINKSTARTUP_RETRIES 3
79
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020080/* Maximum retries for Hibern8 enter */
81#define UIC_HIBERN8_ENTER_RETRIES 3
82
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030083/* maximum number of reset retries before giving up */
84#define MAX_HOST_RESET_RETRIES 5
85
Dolev Raviv68078d52013-07-30 00:35:58 +053086/* Expose the flag value from utp_upiu_query.value */
87#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
88
Seungwon Jeon7d568652013-08-31 21:40:20 +053089/* Interrupt aggregation default timeout, unit: 40us */
90#define INT_AGGR_DEF_TO 0x02
91
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030092#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
93 ({ \
94 int _ret; \
95 if (_on) \
96 _ret = ufshcd_enable_vreg(_dev, _vreg); \
97 else \
98 _ret = ufshcd_disable_vreg(_dev, _vreg); \
99 _ret; \
100 })
101
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530102enum {
103 UFSHCD_MAX_CHANNEL = 0,
104 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530105 UFSHCD_CMD_PER_LUN = 32,
106 UFSHCD_CAN_QUEUE = 32,
107};
108
109/* UFSHCD states */
110enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530111 UFSHCD_STATE_RESET,
112 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530113 UFSHCD_STATE_OPERATIONAL,
Zang Leiganga17bddc2017-04-04 19:32:20 +0000114 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530115};
116
117/* UFSHCD error handling flags */
118enum {
119 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530120};
121
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530122/* UFSHCD UIC layer error flags */
123enum {
124 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200125 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
126 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
127 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
128 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
129 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530130};
131
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530132/* Interrupt configuration options */
133enum {
134 UFSHCD_INT_DISABLE,
135 UFSHCD_INT_ENABLE,
136 UFSHCD_INT_CLEAR,
137};
138
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530139#define ufshcd_set_eh_in_progress(h) \
140 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
141#define ufshcd_eh_in_progress(h) \
142 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
143#define ufshcd_clear_eh_in_progress(h) \
144 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
145
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300146#define ufshcd_set_ufs_dev_active(h) \
147 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
148#define ufshcd_set_ufs_dev_sleep(h) \
149 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
150#define ufshcd_set_ufs_dev_poweroff(h) \
151 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
152#define ufshcd_is_ufs_dev_active(h) \
153 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
154#define ufshcd_is_ufs_dev_sleep(h) \
155 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
156#define ufshcd_is_ufs_dev_poweroff(h) \
157 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
158
159static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
160 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
161 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
162 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
163 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
164 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
165 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
166};
167
168static inline enum ufs_dev_pwr_mode
169ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
170{
171 return ufs_pm_lvl_states[lvl].dev_state;
172}
173
174static inline enum uic_link_state
175ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
176{
177 return ufs_pm_lvl_states[lvl].link_state;
178}
179
Subhash Jadavanibedc6292017-04-04 19:32:13 +0000180static struct ufs_dev_fix ufs_fixups[] = {
181 /* UFS cards deviations table */
182 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
183 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
184 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
185 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
186 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
187 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
188 UFS_DEVICE_NO_FASTAUTO),
189 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
190 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
191 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
192 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
193 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
194 UFS_DEVICE_QUIRK_PA_TACTIVATE),
195 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
196 UFS_DEVICE_QUIRK_PA_TACTIVATE),
197 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
198 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
199 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
200
201 END_FIX
202};
203
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530204static void ufshcd_tmc_handler(struct ufs_hba *hba);
205static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530206static int ufshcd_reset_and_restore(struct ufs_hba *hba);
207static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300208static void ufshcd_hba_exit(struct ufs_hba *hba);
209static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300210static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
211 bool skip_ref_clk);
212static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200213static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300214static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
215static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300216static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300217static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
218static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300219static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
220 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300221static int ufshcd_change_power_mode(struct ufs_hba *hba,
222 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200223static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
224{
225 return tag >= 0 && tag < hba->nutrs;
226}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300227
228static inline int ufshcd_enable_irq(struct ufs_hba *hba)
229{
230 int ret = 0;
231
232 if (!hba->is_irq_enabled) {
233 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
234 hba);
235 if (ret)
236 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
237 __func__, ret);
238 hba->is_irq_enabled = true;
239 }
240
241 return ret;
242}
243
244static inline void ufshcd_disable_irq(struct ufs_hba *hba)
245{
246 if (hba->is_irq_enabled) {
247 free_irq(hba->irq, hba);
248 hba->is_irq_enabled = false;
249 }
250}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530251
Yaniv Gardib573d482016-03-10 17:37:09 +0200252/* replace non-printable or non-ASCII characters with spaces */
253static inline void ufshcd_remove_non_printable(char *val)
254{
255 if (!val)
256 return;
257
258 if (*val < 0x20 || *val > 0x7e)
259 *val = ' ';
260}
261
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530262/*
263 * ufshcd_wait_for_register - wait for register value to change
264 * @hba - per-adapter interface
265 * @reg - mmio register offset
266 * @mask - mask to apply to read register value
267 * @val - wait condition
268 * @interval_us - polling interval in microsecs
269 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200270 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530271 *
272 * Returns -ETIMEDOUT on error, zero on success
273 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200274int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
275 u32 val, unsigned long interval_us,
276 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530277{
278 int err = 0;
279 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
280
281 /* ignore bits that we don't intend to wait on */
282 val = val & mask;
283
284 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200285 if (can_sleep)
286 usleep_range(interval_us, interval_us + 50);
287 else
288 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530289 if (time_after(jiffies, timeout)) {
290 if ((ufshcd_readl(hba, reg) & mask) != val)
291 err = -ETIMEDOUT;
292 break;
293 }
294 }
295
296 return err;
297}
298
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530299/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530300 * ufshcd_get_intr_mask - Get the interrupt bit mask
301 * @hba - Pointer to adapter instance
302 *
303 * Returns interrupt bit mask per version
304 */
305static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
306{
307 if (hba->ufs_version == UFSHCI_VERSION_10)
308 return INTERRUPT_MASK_ALL_VER_10;
309 else
310 return INTERRUPT_MASK_ALL_VER_11;
311}
312
313/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530314 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
315 * @hba - Pointer to adapter instance
316 *
317 * Returns UFSHCI version supported by the controller
318 */
319static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
320{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200321 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
322 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300323
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530324 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530325}
326
327/**
328 * ufshcd_is_device_present - Check if any device connected to
329 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300330 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530331 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530332 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530333 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300334static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530335{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300336 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
337 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530338}
339
340/**
341 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
342 * @lrb: pointer to local command reference block
343 *
344 * This function is used to get the OCS field from UTRD
345 * Returns the OCS field in the UTRD
346 */
347static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
348{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530349 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530350}
351
352/**
353 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
354 * @task_req_descp: pointer to utp_task_req_desc structure
355 *
356 * This function is used to get the OCS field from UTMRD
357 * Returns the OCS field in the UTMRD
358 */
359static inline int
360ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
361{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530362 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530363}
364
365/**
366 * ufshcd_get_tm_free_slot - get a free slot for task management request
367 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530368 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530369 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530370 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
371 * Returns 0 if free slot is not available, else return 1 with tag value
372 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530373 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530374static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530375{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530376 int tag;
377 bool ret = false;
378
379 if (!free_slot)
380 goto out;
381
382 do {
383 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
384 if (tag >= hba->nutmrs)
385 goto out;
386 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
387
388 *free_slot = tag;
389 ret = true;
390out:
391 return ret;
392}
393
394static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
395{
396 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530397}
398
399/**
400 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
401 * @hba: per adapter instance
402 * @pos: position of the bit to be cleared
403 */
404static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
405{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530406 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530407}
408
409/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200410 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
411 * @hba: per adapter instance
412 * @tag: position of the bit to be cleared
413 */
414static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
415{
416 __clear_bit(tag, &hba->outstanding_reqs);
417}
418
419/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530420 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
421 * @reg: Register value of host controller status
422 *
423 * Returns integer, 0 on Success and positive value if failed
424 */
425static inline int ufshcd_get_lists_status(u32 reg)
426{
427 /*
428 * The mask 0xFF is for the following HCS register bits
429 * Bit Description
430 * 0 Device Present
431 * 1 UTRLRDY
432 * 2 UTMRLRDY
433 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200434 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530435 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200436 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530437}
438
439/**
440 * ufshcd_get_uic_cmd_result - Get the UIC command result
441 * @hba: Pointer to adapter instance
442 *
443 * This function gets the result of UIC command completion
444 * Returns 0 on success, non zero value on error
445 */
446static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
447{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530448 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530449 MASK_UIC_COMMAND_RESULT;
450}
451
452/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530453 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
454 * @hba: Pointer to adapter instance
455 *
456 * This function gets UIC command argument3
457 * Returns 0 on success, non zero value on error
458 */
459static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
460{
461 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
462}
463
464/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530465 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530466 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530467 */
468static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530469ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530470{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530471 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530472}
473
474/**
475 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
476 * @ucd_rsp_ptr: pointer to response UPIU
477 *
478 * This function gets the response status and scsi_status from response UPIU
479 * Returns the response result code.
480 */
481static inline int
482ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
483{
484 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
485}
486
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530487/*
488 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
489 * from response UPIU
490 * @ucd_rsp_ptr: pointer to response UPIU
491 *
492 * Return the data segment length.
493 */
494static inline unsigned int
495ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
496{
497 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
498 MASK_RSP_UPIU_DATA_SEG_LEN;
499}
500
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530501/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530502 * ufshcd_is_exception_event - Check if the device raised an exception event
503 * @ucd_rsp_ptr: pointer to response UPIU
504 *
505 * The function checks if the device raised an exception event indicated in
506 * the Device Information field of response UPIU.
507 *
508 * Returns true if exception is raised, false otherwise.
509 */
510static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
511{
512 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
513 MASK_RSP_EXCEPTION_EVENT ? true : false;
514}
515
516/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530517 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530518 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530519 */
520static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530521ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530522{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530523 ufshcd_writel(hba, INT_AGGR_ENABLE |
524 INT_AGGR_COUNTER_AND_TIMER_RESET,
525 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
526}
527
528/**
529 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
530 * @hba: per adapter instance
531 * @cnt: Interrupt aggregation counter threshold
532 * @tmout: Interrupt aggregation timeout value
533 */
534static inline void
535ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
536{
537 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
538 INT_AGGR_COUNTER_THLD_VAL(cnt) |
539 INT_AGGR_TIMEOUT_VAL(tmout),
540 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530541}
542
543/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300544 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
545 * @hba: per adapter instance
546 */
547static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
548{
549 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
550}
551
552/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530553 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
554 * When run-stop registers are set to 1, it indicates the
555 * host controller that it can process the requests
556 * @hba: per adapter instance
557 */
558static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
559{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530560 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
561 REG_UTP_TASK_REQ_LIST_RUN_STOP);
562 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
563 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530564}
565
566/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530567 * ufshcd_hba_start - Start controller initialization sequence
568 * @hba: per adapter instance
569 */
570static inline void ufshcd_hba_start(struct ufs_hba *hba)
571{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530572 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530573}
574
575/**
576 * ufshcd_is_hba_active - Get controller state
577 * @hba: per adapter instance
578 *
579 * Returns zero if controller is active, 1 otherwise
580 */
581static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
582{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530583 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530584}
585
Yaniv Gardi37113102016-03-10 17:37:16 +0200586u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
587{
588 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
589 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
590 (hba->ufs_version == UFSHCI_VERSION_11))
591 return UFS_UNIPRO_VER_1_41;
592 else
593 return UFS_UNIPRO_VER_1_6;
594}
595EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
596
597static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
598{
599 /*
600 * If both host and device support UniPro ver1.6 or later, PA layer
601 * parameters tuning happens during link startup itself.
602 *
603 * We can manually tune PA layer parameters if either host or device
604 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
605 * logic simple, we will only do manual tuning if local unipro version
606 * doesn't support ver1.6 or later.
607 */
608 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
609 return true;
610 else
611 return false;
612}
613
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300614static void ufshcd_ungate_work(struct work_struct *work)
615{
616 int ret;
617 unsigned long flags;
618 struct ufs_hba *hba = container_of(work, struct ufs_hba,
619 clk_gating.ungate_work);
620
621 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
622
623 spin_lock_irqsave(hba->host->host_lock, flags);
624 if (hba->clk_gating.state == CLKS_ON) {
625 spin_unlock_irqrestore(hba->host->host_lock, flags);
626 goto unblock_reqs;
627 }
628
629 spin_unlock_irqrestore(hba->host->host_lock, flags);
630 ufshcd_setup_clocks(hba, true);
631
632 /* Exit from hibern8 */
633 if (ufshcd_can_hibern8_during_gating(hba)) {
634 /* Prevent gating in this path */
635 hba->clk_gating.is_suspended = true;
636 if (ufshcd_is_link_hibern8(hba)) {
637 ret = ufshcd_uic_hibern8_exit(hba);
638 if (ret)
639 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
640 __func__, ret);
641 else
642 ufshcd_set_link_active(hba);
643 }
644 hba->clk_gating.is_suspended = false;
645 }
646unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300647 if (ufshcd_is_clkscaling_enabled(hba))
648 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300649 scsi_unblock_requests(hba->host);
650}
651
652/**
653 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
654 * Also, exit from hibern8 mode and set the link as active.
655 * @hba: per adapter instance
656 * @async: This indicates whether caller should ungate clocks asynchronously.
657 */
658int ufshcd_hold(struct ufs_hba *hba, bool async)
659{
660 int rc = 0;
661 unsigned long flags;
662
663 if (!ufshcd_is_clkgating_allowed(hba))
664 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300665 spin_lock_irqsave(hba->host->host_lock, flags);
666 hba->clk_gating.active_reqs++;
667
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200668 if (ufshcd_eh_in_progress(hba)) {
669 spin_unlock_irqrestore(hba->host->host_lock, flags);
670 return 0;
671 }
672
Sahitya Tummala856b3482014-09-25 15:32:34 +0300673start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300674 switch (hba->clk_gating.state) {
675 case CLKS_ON:
Venkat Gopalakrishnan0967f4d2016-10-17 17:10:53 -0700676 /*
677 * Wait for the ungate work to complete if in progress.
678 * Though the clocks may be in ON state, the link could
679 * still be in hibner8 state if hibern8 is allowed
680 * during clock gating.
681 * Make sure we exit hibern8 state also in addition to
682 * clocks being ON.
683 */
684 if (ufshcd_can_hibern8_during_gating(hba) &&
685 ufshcd_is_link_hibern8(hba)) {
686 spin_unlock_irqrestore(hba->host->host_lock, flags);
687 flush_work(&hba->clk_gating.ungate_work);
688 spin_lock_irqsave(hba->host->host_lock, flags);
689 goto start;
690 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300691 break;
692 case REQ_CLKS_OFF:
693 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
694 hba->clk_gating.state = CLKS_ON;
695 break;
696 }
697 /*
698 * If we here, it means gating work is either done or
699 * currently running. Hence, fall through to cancel gating
700 * work and to enable clocks.
701 */
702 case CLKS_OFF:
703 scsi_block_requests(hba->host);
704 hba->clk_gating.state = REQ_CLKS_ON;
705 schedule_work(&hba->clk_gating.ungate_work);
706 /*
707 * fall through to check if we should wait for this
708 * work to be done or not.
709 */
710 case REQ_CLKS_ON:
711 if (async) {
712 rc = -EAGAIN;
713 hba->clk_gating.active_reqs--;
714 break;
715 }
716
717 spin_unlock_irqrestore(hba->host->host_lock, flags);
718 flush_work(&hba->clk_gating.ungate_work);
719 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300720 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300721 goto start;
722 default:
723 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
724 __func__, hba->clk_gating.state);
725 break;
726 }
727 spin_unlock_irqrestore(hba->host->host_lock, flags);
728out:
729 return rc;
730}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200731EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300732
733static void ufshcd_gate_work(struct work_struct *work)
734{
735 struct ufs_hba *hba = container_of(work, struct ufs_hba,
736 clk_gating.gate_work.work);
737 unsigned long flags;
738
739 spin_lock_irqsave(hba->host->host_lock, flags);
740 if (hba->clk_gating.is_suspended) {
741 hba->clk_gating.state = CLKS_ON;
742 goto rel_lock;
743 }
744
745 if (hba->clk_gating.active_reqs
746 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
747 || hba->lrb_in_use || hba->outstanding_tasks
748 || hba->active_uic_cmd || hba->uic_async_done)
749 goto rel_lock;
750
751 spin_unlock_irqrestore(hba->host->host_lock, flags);
752
753 /* put the link into hibern8 mode before turning off clocks */
754 if (ufshcd_can_hibern8_during_gating(hba)) {
755 if (ufshcd_uic_hibern8_enter(hba)) {
756 hba->clk_gating.state = CLKS_ON;
757 goto out;
758 }
759 ufshcd_set_link_hibern8(hba);
760 }
761
Sahitya Tummala856b3482014-09-25 15:32:34 +0300762 if (ufshcd_is_clkscaling_enabled(hba)) {
763 devfreq_suspend_device(hba->devfreq);
764 hba->clk_scaling.window_start_t = 0;
765 }
766
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300767 if (!ufshcd_is_link_active(hba))
768 ufshcd_setup_clocks(hba, false);
769 else
770 /* If link is active, device ref_clk can't be switched off */
771 __ufshcd_setup_clocks(hba, false, true);
772
773 /*
774 * In case you are here to cancel this work the gating state
775 * would be marked as REQ_CLKS_ON. In this case keep the state
776 * as REQ_CLKS_ON which would anyway imply that clocks are off
777 * and a request to turn them on is pending. By doing this way,
778 * we keep the state machine in tact and this would ultimately
779 * prevent from doing cancel work multiple times when there are
780 * new requests arriving before the current cancel work is done.
781 */
782 spin_lock_irqsave(hba->host->host_lock, flags);
783 if (hba->clk_gating.state == REQ_CLKS_OFF)
784 hba->clk_gating.state = CLKS_OFF;
785
786rel_lock:
787 spin_unlock_irqrestore(hba->host->host_lock, flags);
788out:
789 return;
790}
791
792/* host lock must be held before calling this variant */
793static void __ufshcd_release(struct ufs_hba *hba)
794{
795 if (!ufshcd_is_clkgating_allowed(hba))
796 return;
797
798 hba->clk_gating.active_reqs--;
799
800 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
801 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
802 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200803 || hba->active_uic_cmd || hba->uic_async_done
804 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300805 return;
806
807 hba->clk_gating.state = REQ_CLKS_OFF;
808 schedule_delayed_work(&hba->clk_gating.gate_work,
809 msecs_to_jiffies(hba->clk_gating.delay_ms));
810}
811
812void ufshcd_release(struct ufs_hba *hba)
813{
814 unsigned long flags;
815
816 spin_lock_irqsave(hba->host->host_lock, flags);
817 __ufshcd_release(hba);
818 spin_unlock_irqrestore(hba->host->host_lock, flags);
819}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200820EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300821
822static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
823 struct device_attribute *attr, char *buf)
824{
825 struct ufs_hba *hba = dev_get_drvdata(dev);
826
827 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
828}
829
830static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
831 struct device_attribute *attr, const char *buf, size_t count)
832{
833 struct ufs_hba *hba = dev_get_drvdata(dev);
834 unsigned long flags, value;
835
836 if (kstrtoul(buf, 0, &value))
837 return -EINVAL;
838
839 spin_lock_irqsave(hba->host->host_lock, flags);
840 hba->clk_gating.delay_ms = value;
841 spin_unlock_irqrestore(hba->host->host_lock, flags);
842 return count;
843}
844
845static void ufshcd_init_clk_gating(struct ufs_hba *hba)
846{
847 if (!ufshcd_is_clkgating_allowed(hba))
848 return;
849
850 hba->clk_gating.delay_ms = 150;
851 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
852 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
853
854 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
855 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
856 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
857 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
858 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
859 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
860 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
861}
862
863static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
864{
865 if (!ufshcd_is_clkgating_allowed(hba))
866 return;
867 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900868 cancel_work_sync(&hba->clk_gating.ungate_work);
869 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300870}
871
Sahitya Tummala856b3482014-09-25 15:32:34 +0300872/* Must be called with host lock acquired */
873static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
874{
875 if (!ufshcd_is_clkscaling_enabled(hba))
876 return;
877
878 if (!hba->clk_scaling.is_busy_started) {
879 hba->clk_scaling.busy_start_t = ktime_get();
880 hba->clk_scaling.is_busy_started = true;
881 }
882}
883
884static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
885{
886 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
887
888 if (!ufshcd_is_clkscaling_enabled(hba))
889 return;
890
891 if (!hba->outstanding_reqs && scaling->is_busy_started) {
892 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
893 scaling->busy_start_t));
894 scaling->busy_start_t = ktime_set(0, 0);
895 scaling->is_busy_started = false;
896 }
897}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530898/**
899 * ufshcd_send_command - Send SCSI or device management commands
900 * @hba: per adapter instance
901 * @task_tag: Task tag of the command
902 */
903static inline
904void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
905{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300906 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530907 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530908 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530909}
910
911/**
912 * ufshcd_copy_sense_data - Copy sense data in case of check condition
913 * @lrb - pointer to local reference block
914 */
915static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
916{
917 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530918 if (lrbp->sense_buffer &&
919 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardi675d9192016-10-17 17:09:24 -0700920 int len_to_copy;
921
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530922 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardi675d9192016-10-17 17:09:24 -0700923 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
924
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530925 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530926 lrbp->ucd_rsp_ptr->sr.sense_data,
Yaniv Gardi675d9192016-10-17 17:09:24 -0700927 min_t(int, len_to_copy, SCSI_SENSE_BUFFERSIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530928 }
929}
930
931/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530932 * ufshcd_copy_query_response() - Copy the Query Response and the data
933 * descriptor
934 * @hba: per adapter instance
935 * @lrb - pointer to local reference block
936 */
937static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300938int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530939{
940 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
941
Dolev Raviv68078d52013-07-30 00:35:58 +0530942 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530943
Dolev Raviv68078d52013-07-30 00:35:58 +0530944 /* Get the descriptor */
945 if (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
Joao Pinto300bb132016-05-11 12:21:27 +01001352 if (hba->ufs_version == UFSHCI_VERSION_20)
1353 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1354 else
1355 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1356
1357 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1358 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1359 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1360 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1361 ufshcd_prepare_utp_nop_upiu(lrbp);
1362 else
1363 ret = -EINVAL;
1364
1365 return ret;
1366}
1367
1368/**
1369 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1370 * for SCSI Purposes
1371 * @hba - per adapter instance
1372 * @lrb - pointer to local reference block
1373 */
1374static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1375{
1376 u32 upiu_flags;
1377 int ret = 0;
1378
1379 if (hba->ufs_version == UFSHCI_VERSION_20)
1380 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1381 else
1382 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1383
1384 if (likely(lrbp->cmd)) {
1385 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1386 lrbp->cmd->sc_data_direction);
1387 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1388 } else {
1389 ret = -EINVAL;
1390 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301391
1392 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301393}
1394
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001395/*
1396 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1397 * @scsi_lun: scsi LUN id
1398 *
1399 * Returns UPIU LUN id
1400 */
1401static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1402{
1403 if (scsi_is_wlun(scsi_lun))
1404 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1405 | UFS_UPIU_WLUN_ID;
1406 else
1407 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1408}
1409
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301410/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001411 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1412 * @scsi_lun: UPIU W-LUN id
1413 *
1414 * Returns SCSI W-LUN id
1415 */
1416static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1417{
1418 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1419}
1420
1421/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301422 * ufshcd_queuecommand - main entry point for SCSI requests
1423 * @cmd: command from SCSI Midlayer
1424 * @done: call back function
1425 *
1426 * Returns 0 for success, non-zero in case of failure
1427 */
1428static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1429{
1430 struct ufshcd_lrb *lrbp;
1431 struct ufs_hba *hba;
1432 unsigned long flags;
1433 int tag;
1434 int err = 0;
1435
1436 hba = shost_priv(host);
1437
1438 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001439 if (!ufshcd_valid_tag(hba, tag)) {
1440 dev_err(hba->dev,
1441 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1442 __func__, tag, cmd, cmd->request);
1443 BUG();
1444 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301445
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301446 spin_lock_irqsave(hba->host->host_lock, flags);
1447 switch (hba->ufshcd_state) {
1448 case UFSHCD_STATE_OPERATIONAL:
1449 break;
Zang Leiganga17bddc2017-04-04 19:32:20 +00001450 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301451 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301452 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301453 goto out_unlock;
1454 case UFSHCD_STATE_ERROR:
1455 set_host_byte(cmd, DID_ERROR);
1456 cmd->scsi_done(cmd);
1457 goto out_unlock;
1458 default:
1459 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1460 __func__, hba->ufshcd_state);
1461 set_host_byte(cmd, DID_BAD_TARGET);
1462 cmd->scsi_done(cmd);
1463 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301464 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001465
1466 /* if error handling is in progress, don't issue commands */
1467 if (ufshcd_eh_in_progress(hba)) {
1468 set_host_byte(cmd, DID_ERROR);
1469 cmd->scsi_done(cmd);
1470 goto out_unlock;
1471 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301472 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301473
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301474 /* acquire the tag to make sure device cmds don't use it */
1475 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1476 /*
1477 * Dev manage command in progress, requeue the command.
1478 * Requeuing the command helps in cases where the request *may*
1479 * find different tag instead of waiting for dev manage command
1480 * completion.
1481 */
1482 err = SCSI_MLQUEUE_HOST_BUSY;
1483 goto out;
1484 }
1485
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001486 err = ufshcd_hold(hba, true);
1487 if (err) {
1488 err = SCSI_MLQUEUE_HOST_BUSY;
1489 clear_bit_unlock(tag, &hba->lrb_in_use);
1490 goto out;
1491 }
Mohan Srinivasane2d88782016-12-14 15:55:36 -08001492
1493 /* IO svc time latency histogram */
1494 if (hba != NULL && cmd->request != NULL) {
1495 if (hba->latency_hist_enabled &&
1496 (cmd->request->cmd_type == REQ_TYPE_FS)) {
1497 cmd->request->lat_hist_io_start = ktime_get();
1498 cmd->request->lat_hist_enabled = 1;
1499 } else
1500 cmd->request->lat_hist_enabled = 0;
1501 }
1502
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001503 WARN_ON(hba->clk_gating.state != CLKS_ON);
1504
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301505 lrbp = &hba->lrb[tag];
1506
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301507 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301508 lrbp->cmd = cmd;
1509 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1510 lrbp->sense_buffer = cmd->sense_buffer;
1511 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001512 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001513 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301514
Joao Pinto300bb132016-05-11 12:21:27 +01001515 ufshcd_comp_scsi_upiu(hba, lrbp);
1516
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001517 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301518 if (err) {
1519 lrbp->cmd = NULL;
1520 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301521 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301522 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301523
1524 /* issue command to the controller */
1525 spin_lock_irqsave(hba->host->host_lock, flags);
1526 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301527out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301528 spin_unlock_irqrestore(hba->host->host_lock, flags);
1529out:
1530 return err;
1531}
1532
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301533static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1534 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1535{
1536 lrbp->cmd = NULL;
1537 lrbp->sense_bufflen = 0;
1538 lrbp->sense_buffer = NULL;
1539 lrbp->task_tag = tag;
1540 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301541 lrbp->intr_cmd = true; /* No interrupt aggregation */
1542 hba->dev_cmd.type = cmd_type;
1543
Joao Pinto300bb132016-05-11 12:21:27 +01001544 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301545}
1546
1547static int
1548ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1549{
1550 int err = 0;
1551 unsigned long flags;
1552 u32 mask = 1 << tag;
1553
1554 /* clear outstanding transaction before retry */
1555 spin_lock_irqsave(hba->host->host_lock, flags);
1556 ufshcd_utrl_clear(hba, tag);
1557 spin_unlock_irqrestore(hba->host->host_lock, flags);
1558
1559 /*
1560 * wait for for h/w to clear corresponding bit in door-bell.
1561 * max. wait is 1 sec.
1562 */
1563 err = ufshcd_wait_for_register(hba,
1564 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001565 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301566
1567 return err;
1568}
1569
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001570static int
1571ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1572{
1573 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1574
1575 /* Get the UPIU response */
1576 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1577 UPIU_RSP_CODE_OFFSET;
1578 return query_res->response;
1579}
1580
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301581/**
1582 * ufshcd_dev_cmd_completion() - handles device management command responses
1583 * @hba: per adapter instance
1584 * @lrbp: pointer to local reference block
1585 */
1586static int
1587ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1588{
1589 int resp;
1590 int err = 0;
1591
1592 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1593
1594 switch (resp) {
1595 case UPIU_TRANSACTION_NOP_IN:
1596 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1597 err = -EINVAL;
1598 dev_err(hba->dev, "%s: unexpected response %x\n",
1599 __func__, resp);
1600 }
1601 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301602 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001603 err = ufshcd_check_query_response(hba, lrbp);
1604 if (!err)
1605 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301606 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301607 case UPIU_TRANSACTION_REJECT_UPIU:
1608 /* TODO: handle Reject UPIU Response */
1609 err = -EPERM;
1610 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1611 __func__);
1612 break;
1613 default:
1614 err = -EINVAL;
1615 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1616 __func__, resp);
1617 break;
1618 }
1619
1620 return err;
1621}
1622
1623static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1624 struct ufshcd_lrb *lrbp, int max_timeout)
1625{
1626 int err = 0;
1627 unsigned long time_left;
1628 unsigned long flags;
1629
1630 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1631 msecs_to_jiffies(max_timeout));
1632
1633 spin_lock_irqsave(hba->host->host_lock, flags);
1634 hba->dev_cmd.complete = NULL;
1635 if (likely(time_left)) {
1636 err = ufshcd_get_tr_ocs(lrbp);
1637 if (!err)
1638 err = ufshcd_dev_cmd_completion(hba, lrbp);
1639 }
1640 spin_unlock_irqrestore(hba->host->host_lock, flags);
1641
1642 if (!time_left) {
1643 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001644 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1645 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301646 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001647 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301648 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001649 /*
1650 * in case of an error, after clearing the doorbell,
1651 * we also need to clear the outstanding_request
1652 * field in hba
1653 */
1654 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301655 }
1656
1657 return err;
1658}
1659
1660/**
1661 * ufshcd_get_dev_cmd_tag - Get device management command tag
1662 * @hba: per-adapter instance
1663 * @tag: pointer to variable with available slot value
1664 *
1665 * Get a free slot and lock it until device management command
1666 * completes.
1667 *
1668 * Returns false if free slot is unavailable for locking, else
1669 * return true with tag value in @tag.
1670 */
1671static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1672{
1673 int tag;
1674 bool ret = false;
1675 unsigned long tmp;
1676
1677 if (!tag_out)
1678 goto out;
1679
1680 do {
1681 tmp = ~hba->lrb_in_use;
1682 tag = find_last_bit(&tmp, hba->nutrs);
1683 if (tag >= hba->nutrs)
1684 goto out;
1685 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1686
1687 *tag_out = tag;
1688 ret = true;
1689out:
1690 return ret;
1691}
1692
1693static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1694{
1695 clear_bit_unlock(tag, &hba->lrb_in_use);
1696}
1697
1698/**
1699 * ufshcd_exec_dev_cmd - API for sending device management requests
1700 * @hba - UFS hba
1701 * @cmd_type - specifies the type (NOP, Query...)
1702 * @timeout - time in seconds
1703 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301704 * NOTE: Since there is only one available tag for device management commands,
1705 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301706 */
1707static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1708 enum dev_cmd_type cmd_type, int timeout)
1709{
1710 struct ufshcd_lrb *lrbp;
1711 int err;
1712 int tag;
1713 struct completion wait;
1714 unsigned long flags;
1715
1716 /*
1717 * Get free slot, sleep if slots are unavailable.
1718 * Even though we use wait_event() which sleeps indefinitely,
1719 * the maximum wait time is bounded by SCSI request timeout.
1720 */
1721 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1722
1723 init_completion(&wait);
1724 lrbp = &hba->lrb[tag];
1725 WARN_ON(lrbp->cmd);
1726 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1727 if (unlikely(err))
1728 goto out_put_tag;
1729
1730 hba->dev_cmd.complete = &wait;
1731
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001732 /* Make sure descriptors are ready before ringing the doorbell */
1733 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301734 spin_lock_irqsave(hba->host->host_lock, flags);
1735 ufshcd_send_command(hba, tag);
1736 spin_unlock_irqrestore(hba->host->host_lock, flags);
1737
1738 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1739
1740out_put_tag:
1741 ufshcd_put_dev_cmd_tag(hba, tag);
1742 wake_up(&hba->dev_cmd.tag_wq);
1743 return err;
1744}
1745
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301746/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001747 * ufshcd_init_query() - init the query response and request parameters
1748 * @hba: per-adapter instance
1749 * @request: address of the request pointer to be initialized
1750 * @response: address of the response pointer to be initialized
1751 * @opcode: operation to perform
1752 * @idn: flag idn to access
1753 * @index: LU number to access
1754 * @selector: query/flag/descriptor further identification
1755 */
1756static inline void ufshcd_init_query(struct ufs_hba *hba,
1757 struct ufs_query_req **request, struct ufs_query_res **response,
1758 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1759{
1760 *request = &hba->dev_cmd.query.request;
1761 *response = &hba->dev_cmd.query.response;
1762 memset(*request, 0, sizeof(struct ufs_query_req));
1763 memset(*response, 0, sizeof(struct ufs_query_res));
1764 (*request)->upiu_req.opcode = opcode;
1765 (*request)->upiu_req.idn = idn;
1766 (*request)->upiu_req.index = index;
1767 (*request)->upiu_req.selector = selector;
1768}
1769
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001770static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1771 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1772{
1773 int ret;
1774 int retries;
1775
1776 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1777 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1778 if (ret)
1779 dev_dbg(hba->dev,
1780 "%s: failed with error %d, retries %d\n",
1781 __func__, ret, retries);
1782 else
1783 break;
1784 }
1785
1786 if (ret)
1787 dev_err(hba->dev,
1788 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1789 __func__, opcode, idn, ret, retries);
1790 return ret;
1791}
1792
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001793/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301794 * ufshcd_query_flag() - API function for sending flag query requests
1795 * hba: per-adapter instance
1796 * query_opcode: flag query to perform
1797 * idn: flag idn to access
1798 * flag_res: the flag value after the query request completes
1799 *
1800 * Returns 0 for success, non-zero in case of failure
1801 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001802int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301803 enum flag_idn idn, bool *flag_res)
1804{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001805 struct ufs_query_req *request = NULL;
1806 struct ufs_query_res *response = NULL;
1807 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001808 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301809
1810 BUG_ON(!hba);
1811
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001812 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301813 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001814 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1815 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301816
1817 switch (opcode) {
1818 case UPIU_QUERY_OPCODE_SET_FLAG:
1819 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1820 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1821 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1822 break;
1823 case UPIU_QUERY_OPCODE_READ_FLAG:
1824 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1825 if (!flag_res) {
1826 /* No dummy reads */
1827 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1828 __func__);
1829 err = -EINVAL;
1830 goto out_unlock;
1831 }
1832 break;
1833 default:
1834 dev_err(hba->dev,
1835 "%s: Expected query flag opcode but got = %d\n",
1836 __func__, opcode);
1837 err = -EINVAL;
1838 goto out_unlock;
1839 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301840
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001841 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1842 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1843
1844 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301845
1846 if (err) {
1847 dev_err(hba->dev,
1848 "%s: Sending flag query for idn %d failed, err = %d\n",
1849 __func__, idn, err);
1850 goto out_unlock;
1851 }
1852
1853 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301854 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301855 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1856
1857out_unlock:
1858 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001859 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301860 return err;
1861}
1862
1863/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301864 * ufshcd_query_attr - API function for sending attribute requests
1865 * hba: per-adapter instance
1866 * opcode: attribute opcode
1867 * idn: attribute idn to access
1868 * index: index field
1869 * selector: selector field
1870 * attr_val: the attribute value after the query request completes
1871 *
1872 * Returns 0 for success, non-zero in case of failure
1873*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301874static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301875 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1876{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001877 struct ufs_query_req *request = NULL;
1878 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301879 int err;
1880
1881 BUG_ON(!hba);
1882
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001883 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301884 if (!attr_val) {
1885 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1886 __func__, opcode);
1887 err = -EINVAL;
1888 goto out;
1889 }
1890
1891 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001892 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1893 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301894
1895 switch (opcode) {
1896 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1897 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301898 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301899 break;
1900 case UPIU_QUERY_OPCODE_READ_ATTR:
1901 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1902 break;
1903 default:
1904 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1905 __func__, opcode);
1906 err = -EINVAL;
1907 goto out_unlock;
1908 }
1909
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001910 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301911
1912 if (err) {
1913 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1914 __func__, opcode, idn, err);
1915 goto out_unlock;
1916 }
1917
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301918 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301919
1920out_unlock:
1921 mutex_unlock(&hba->dev_cmd.lock);
1922out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001923 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301924 return err;
1925}
1926
1927/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001928 * ufshcd_query_attr_retry() - API function for sending query
1929 * attribute with retries
1930 * @hba: per-adapter instance
1931 * @opcode: attribute opcode
1932 * @idn: attribute idn to access
1933 * @index: index field
1934 * @selector: selector field
1935 * @attr_val: the attribute value after the query request
1936 * completes
1937 *
1938 * Returns 0 for success, non-zero in case of failure
1939*/
1940static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1941 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1942 u32 *attr_val)
1943{
1944 int ret = 0;
1945 u32 retries;
1946
1947 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1948 ret = ufshcd_query_attr(hba, opcode, idn, index,
1949 selector, attr_val);
1950 if (ret)
1951 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1952 __func__, ret, retries);
1953 else
1954 break;
1955 }
1956
1957 if (ret)
1958 dev_err(hba->dev,
1959 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1960 __func__, idn, ret, QUERY_REQ_RETRIES);
1961 return ret;
1962}
1963
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001964static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001965 enum query_opcode opcode, enum desc_idn idn, u8 index,
1966 u8 selector, u8 *desc_buf, int *buf_len)
1967{
1968 struct ufs_query_req *request = NULL;
1969 struct ufs_query_res *response = NULL;
1970 int err;
1971
1972 BUG_ON(!hba);
1973
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001974 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001975 if (!desc_buf) {
1976 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1977 __func__, opcode);
1978 err = -EINVAL;
1979 goto out;
1980 }
1981
Potomski, MichalXe1928452017-02-23 09:05:30 +00001982 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001983 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1984 __func__, *buf_len);
1985 err = -EINVAL;
1986 goto out;
1987 }
1988
1989 mutex_lock(&hba->dev_cmd.lock);
1990 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1991 selector);
1992 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001993 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001994
1995 switch (opcode) {
1996 case UPIU_QUERY_OPCODE_WRITE_DESC:
1997 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1998 break;
1999 case UPIU_QUERY_OPCODE_READ_DESC:
2000 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
2001 break;
2002 default:
2003 dev_err(hba->dev,
2004 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2005 __func__, opcode);
2006 err = -EINVAL;
2007 goto out_unlock;
2008 }
2009
2010 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
2011
2012 if (err) {
2013 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
2014 __func__, opcode, idn, err);
2015 goto out_unlock;
2016 }
2017
2018 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002019 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002020
2021out_unlock:
2022 mutex_unlock(&hba->dev_cmd.lock);
2023out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002024 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002025 return err;
2026}
2027
2028/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002029 * ufshcd_query_descriptor_retry - API function for sending descriptor
2030 * requests
2031 * hba: per-adapter instance
2032 * opcode: attribute opcode
2033 * idn: attribute idn to access
2034 * index: index field
2035 * selector: selector field
2036 * desc_buf: the buffer that contains the descriptor
2037 * buf_len: length parameter passed to the device
2038 *
2039 * Returns 0 for success, non-zero in case of failure.
2040 * The buf_len parameter will contain, on return, the length parameter
2041 * received on the response.
2042 */
2043int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2044 enum query_opcode opcode, enum desc_idn idn, u8 index,
2045 u8 selector, u8 *desc_buf, int *buf_len)
2046{
2047 int err;
2048 int retries;
2049
2050 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2051 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2052 selector, desc_buf, buf_len);
2053 if (!err || err == -EINVAL)
2054 break;
2055 }
2056
2057 return err;
2058}
2059EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2060
2061/**
Potomski, MichalXe1928452017-02-23 09:05:30 +00002062 * ufshcd_read_desc_length - read the specified descriptor length from header
2063 * @hba: Pointer to adapter instance
2064 * @desc_id: descriptor idn value
2065 * @desc_index: descriptor index
2066 * @desc_length: pointer to variable to read the length of descriptor
2067 *
2068 * Return 0 in case of success, non-zero otherwise
2069 */
2070static int ufshcd_read_desc_length(struct ufs_hba *hba,
2071 enum desc_idn desc_id,
2072 int desc_index,
2073 int *desc_length)
2074{
2075 int ret;
2076 u8 header[QUERY_DESC_HDR_SIZE];
2077 int header_len = QUERY_DESC_HDR_SIZE;
2078
2079 if (desc_id >= QUERY_DESC_IDN_MAX)
2080 return -EINVAL;
2081
2082 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2083 desc_id, desc_index, 0, header,
2084 &header_len);
2085
2086 if (ret) {
2087 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2088 __func__, desc_id);
2089 return ret;
2090 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
2091 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
2092 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
2093 desc_id);
2094 ret = -EINVAL;
2095 }
2096
2097 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
2098 return ret;
2099
2100}
2101
2102/**
2103 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
2104 * @hba: Pointer to adapter instance
2105 * @desc_id: descriptor idn value
2106 * @desc_len: mapped desc length (out)
2107 *
2108 * Return 0 in case of success, non-zero otherwise
2109 */
2110int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
2111 enum desc_idn desc_id, int *desc_len)
2112{
2113 switch (desc_id) {
2114 case QUERY_DESC_IDN_DEVICE:
2115 *desc_len = hba->desc_size.dev_desc;
2116 break;
2117 case QUERY_DESC_IDN_POWER:
2118 *desc_len = hba->desc_size.pwr_desc;
2119 break;
2120 case QUERY_DESC_IDN_GEOMETRY:
2121 *desc_len = hba->desc_size.geom_desc;
2122 break;
2123 case QUERY_DESC_IDN_CONFIGURATION:
2124 *desc_len = hba->desc_size.conf_desc;
2125 break;
2126 case QUERY_DESC_IDN_UNIT:
2127 *desc_len = hba->desc_size.unit_desc;
2128 break;
2129 case QUERY_DESC_IDN_INTERCONNECT:
2130 *desc_len = hba->desc_size.interc_desc;
2131 break;
2132 case QUERY_DESC_IDN_STRING:
2133 *desc_len = QUERY_DESC_MAX_SIZE;
2134 break;
2135 case QUERY_DESC_IDN_RFU_0:
2136 case QUERY_DESC_IDN_RFU_1:
2137 *desc_len = 0;
2138 break;
2139 default:
2140 *desc_len = 0;
2141 return -EINVAL;
2142 }
2143 return 0;
2144}
2145EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
2146
2147/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002148 * ufshcd_read_desc_param - read the specified descriptor parameter
2149 * @hba: Pointer to adapter instance
2150 * @desc_id: descriptor idn value
2151 * @desc_index: descriptor index
2152 * @param_offset: offset of the parameter to read
2153 * @param_read_buf: pointer to buffer where parameter would be read
2154 * @param_size: sizeof(param_read_buf)
2155 *
2156 * Return 0 in case of success, non-zero otherwise
2157 */
2158static int ufshcd_read_desc_param(struct ufs_hba *hba,
2159 enum desc_idn desc_id,
2160 int desc_index,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002161 u8 param_offset,
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002162 u8 *param_read_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002163 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002164{
2165 int ret;
2166 u8 *desc_buf;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002167 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002168 bool is_kmalloc = true;
2169
Potomski, MichalXe1928452017-02-23 09:05:30 +00002170 /* Safety check */
2171 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002172 return -EINVAL;
2173
Potomski, MichalXe1928452017-02-23 09:05:30 +00002174 /* Get the max length of descriptor from structure filled up at probe
2175 * time.
2176 */
2177 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002178
Potomski, MichalXe1928452017-02-23 09:05:30 +00002179 /* Sanity checks */
2180 if (ret || !buff_len) {
2181 dev_err(hba->dev, "%s: Failed to get full descriptor length",
2182 __func__);
2183 return ret;
2184 }
2185
2186 /* Check whether we need temp memory */
2187 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002188 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2189 if (!desc_buf)
2190 return -ENOMEM;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002191 } else {
2192 desc_buf = param_read_buf;
2193 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002194 }
2195
Potomski, MichalXe1928452017-02-23 09:05:30 +00002196 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002197 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002198 desc_id, desc_index, 0,
2199 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002200
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002201 if (ret) {
2202 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2203 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002204 goto out;
2205 }
2206
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002207 /* Sanity check */
2208 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2209 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2210 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2211 ret = -EINVAL;
2212 goto out;
2213 }
2214
Potomski, MichalXe1928452017-02-23 09:05:30 +00002215 /* Check wherher we will not copy more data, than available */
2216 if (is_kmalloc && param_size > buff_len)
2217 param_size = buff_len;
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002218
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002219 if (is_kmalloc)
2220 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2221out:
2222 if (is_kmalloc)
2223 kfree(desc_buf);
2224 return ret;
2225}
2226
2227static inline int ufshcd_read_desc(struct ufs_hba *hba,
2228 enum desc_idn desc_id,
2229 int desc_index,
2230 u8 *buf,
2231 u32 size)
2232{
2233 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2234}
2235
2236static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2237 u8 *buf,
2238 u32 size)
2239{
2240 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2241}
2242
Yaniv Gardib573d482016-03-10 17:37:09 +02002243int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2244{
2245 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2246}
2247EXPORT_SYMBOL(ufshcd_read_device_desc);
2248
2249/**
2250 * ufshcd_read_string_desc - read string descriptor
2251 * @hba: pointer to adapter instance
2252 * @desc_index: descriptor index
2253 * @buf: pointer to buffer where descriptor would be read
2254 * @size: size of buf
2255 * @ascii: if true convert from unicode to ascii characters
2256 *
2257 * Return 0 in case of success, non-zero otherwise
2258 */
2259int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2260 u32 size, bool ascii)
2261{
2262 int err = 0;
2263
2264 err = ufshcd_read_desc(hba,
2265 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2266
2267 if (err) {
2268 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2269 __func__, QUERY_REQ_RETRIES, err);
2270 goto out;
2271 }
2272
2273 if (ascii) {
2274 int desc_len;
2275 int ascii_len;
2276 int i;
2277 char *buff_ascii;
2278
2279 desc_len = buf[0];
2280 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2281 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2282 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2283 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2284 __func__);
2285 err = -ENOMEM;
2286 goto out;
2287 }
2288
2289 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2290 if (!buff_ascii) {
2291 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002292 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002293 }
2294
2295 /*
2296 * the descriptor contains string in UTF16 format
2297 * we need to convert to utf-8 so it can be displayed
2298 */
2299 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2300 desc_len - QUERY_DESC_HDR_SIZE,
2301 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2302
2303 /* replace non-printable or non-ASCII characters with spaces */
2304 for (i = 0; i < ascii_len; i++)
2305 ufshcd_remove_non_printable(&buff_ascii[i]);
2306
2307 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2308 size - QUERY_DESC_HDR_SIZE);
2309 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2310 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002311 kfree(buff_ascii);
2312 }
2313out:
2314 return err;
2315}
2316EXPORT_SYMBOL(ufshcd_read_string_desc);
2317
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002318/**
2319 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2320 * @hba: Pointer to adapter instance
2321 * @lun: lun id
2322 * @param_offset: offset of the parameter to read
2323 * @param_read_buf: pointer to buffer where parameter would be read
2324 * @param_size: sizeof(param_read_buf)
2325 *
2326 * Return 0 in case of success, non-zero otherwise
2327 */
2328static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2329 int lun,
2330 enum unit_desc_param param_offset,
2331 u8 *param_read_buf,
2332 u32 param_size)
2333{
2334 /*
2335 * Unit descriptors are only available for general purpose LUs (LUN id
2336 * from 0 to 7) and RPMB Well known LU.
2337 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002338 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002339 return -EOPNOTSUPP;
2340
2341 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2342 param_offset, param_read_buf, param_size);
2343}
2344
2345/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302346 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2347 * @hba: per adapter instance
2348 *
2349 * 1. Allocate DMA memory for Command Descriptor array
2350 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2351 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2352 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2353 * (UTMRDL)
2354 * 4. Allocate memory for local reference block(lrb).
2355 *
2356 * Returns 0 for success, non-zero in case of failure
2357 */
2358static int ufshcd_memory_alloc(struct ufs_hba *hba)
2359{
2360 size_t utmrdl_size, utrdl_size, ucdl_size;
2361
2362 /* Allocate memory for UTP command descriptors */
2363 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002364 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2365 ucdl_size,
2366 &hba->ucdl_dma_addr,
2367 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302368
2369 /*
2370 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2371 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2372 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2373 * be aligned to 128 bytes as well
2374 */
2375 if (!hba->ucdl_base_addr ||
2376 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302377 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302378 "Command Descriptor Memory allocation failed\n");
2379 goto out;
2380 }
2381
2382 /*
2383 * Allocate memory for UTP Transfer descriptors
2384 * UFSHCI requires 1024 byte alignment of UTRD
2385 */
2386 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002387 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2388 utrdl_size,
2389 &hba->utrdl_dma_addr,
2390 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302391 if (!hba->utrdl_base_addr ||
2392 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302393 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302394 "Transfer Descriptor Memory allocation failed\n");
2395 goto out;
2396 }
2397
2398 /*
2399 * Allocate memory for UTP Task Management descriptors
2400 * UFSHCI requires 1024 byte alignment of UTMRD
2401 */
2402 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002403 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2404 utmrdl_size,
2405 &hba->utmrdl_dma_addr,
2406 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302407 if (!hba->utmrdl_base_addr ||
2408 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302409 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302410 "Task Management Descriptor Memory allocation failed\n");
2411 goto out;
2412 }
2413
2414 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002415 hba->lrb = devm_kzalloc(hba->dev,
2416 hba->nutrs * sizeof(struct ufshcd_lrb),
2417 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302418 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302419 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302420 goto out;
2421 }
2422 return 0;
2423out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302424 return -ENOMEM;
2425}
2426
2427/**
2428 * ufshcd_host_memory_configure - configure local reference block with
2429 * memory offsets
2430 * @hba: per adapter instance
2431 *
2432 * Configure Host memory space
2433 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2434 * address.
2435 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2436 * and PRDT offset.
2437 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2438 * into local reference block.
2439 */
2440static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2441{
2442 struct utp_transfer_cmd_desc *cmd_descp;
2443 struct utp_transfer_req_desc *utrdlp;
2444 dma_addr_t cmd_desc_dma_addr;
2445 dma_addr_t cmd_desc_element_addr;
2446 u16 response_offset;
2447 u16 prdt_offset;
2448 int cmd_desc_size;
2449 int i;
2450
2451 utrdlp = hba->utrdl_base_addr;
2452 cmd_descp = hba->ucdl_base_addr;
2453
2454 response_offset =
2455 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2456 prdt_offset =
2457 offsetof(struct utp_transfer_cmd_desc, prd_table);
2458
2459 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2460 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2461
2462 for (i = 0; i < hba->nutrs; i++) {
2463 /* Configure UTRD with command descriptor base address */
2464 cmd_desc_element_addr =
2465 (cmd_desc_dma_addr + (cmd_desc_size * i));
2466 utrdlp[i].command_desc_base_addr_lo =
2467 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2468 utrdlp[i].command_desc_base_addr_hi =
2469 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2470
2471 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002472 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2473 utrdlp[i].response_upiu_offset =
2474 cpu_to_le16(response_offset);
2475 utrdlp[i].prd_table_offset =
2476 cpu_to_le16(prdt_offset);
2477 utrdlp[i].response_upiu_length =
2478 cpu_to_le16(ALIGNED_UPIU_SIZE);
2479 } else {
2480 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302481 cpu_to_le16((response_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002482 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302483 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002484 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302485 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002486 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302487
2488 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302489 hba->lrb[i].ucd_req_ptr =
2490 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302491 hba->lrb[i].ucd_rsp_ptr =
2492 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2493 hba->lrb[i].ucd_prdt_ptr =
2494 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2495 }
2496}
2497
2498/**
2499 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2500 * @hba: per adapter instance
2501 *
2502 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2503 * in order to initialize the Unipro link startup procedure.
2504 * Once the Unipro links are up, the device connected to the controller
2505 * is detected.
2506 *
2507 * Returns 0 on success, non-zero value on failure
2508 */
2509static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2510{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302511 struct uic_command uic_cmd = {0};
2512 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302513
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302514 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2515
2516 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2517 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302518 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302519 "dme-link-startup: error code %d\n", ret);
2520 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302521}
2522
Yaniv Gardicad2e032015-03-31 17:37:14 +03002523static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2524{
2525 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2526 unsigned long min_sleep_time_us;
2527
2528 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2529 return;
2530
2531 /*
2532 * last_dme_cmd_tstamp will be 0 only for 1st call to
2533 * this function
2534 */
2535 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2536 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2537 } else {
2538 unsigned long delta =
2539 (unsigned long) ktime_to_us(
2540 ktime_sub(ktime_get(),
2541 hba->last_dme_cmd_tstamp));
2542
2543 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2544 min_sleep_time_us =
2545 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2546 else
2547 return; /* no more delay required */
2548 }
2549
2550 /* allow sleep for extra 50us if needed */
2551 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2552}
2553
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302554/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302555 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2556 * @hba: per adapter instance
2557 * @attr_sel: uic command argument1
2558 * @attr_set: attribute set type as uic command argument2
2559 * @mib_val: setting value as uic command argument3
2560 * @peer: indicate whether peer or local
2561 *
2562 * Returns 0 on success, non-zero value on failure
2563 */
2564int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2565 u8 attr_set, u32 mib_val, u8 peer)
2566{
2567 struct uic_command uic_cmd = {0};
2568 static const char *const action[] = {
2569 "dme-set",
2570 "dme-peer-set"
2571 };
2572 const char *set = action[!!peer];
2573 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002574 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302575
2576 uic_cmd.command = peer ?
2577 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2578 uic_cmd.argument1 = attr_sel;
2579 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2580 uic_cmd.argument3 = mib_val;
2581
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002582 do {
2583 /* for peer attributes we retry upon failure */
2584 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2585 if (ret)
2586 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2587 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2588 } while (ret && peer && --retries);
2589
2590 if (!retries)
2591 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2592 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2593 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302594
2595 return ret;
2596}
2597EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2598
2599/**
2600 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2601 * @hba: per adapter instance
2602 * @attr_sel: uic command argument1
2603 * @mib_val: the value of the attribute as returned by the UIC command
2604 * @peer: indicate whether peer or local
2605 *
2606 * Returns 0 on success, non-zero value on failure
2607 */
2608int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2609 u32 *mib_val, u8 peer)
2610{
2611 struct uic_command uic_cmd = {0};
2612 static const char *const action[] = {
2613 "dme-get",
2614 "dme-peer-get"
2615 };
2616 const char *get = action[!!peer];
2617 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002618 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002619 struct ufs_pa_layer_attr orig_pwr_info;
2620 struct ufs_pa_layer_attr temp_pwr_info;
2621 bool pwr_mode_change = false;
2622
2623 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2624 orig_pwr_info = hba->pwr_info;
2625 temp_pwr_info = orig_pwr_info;
2626
2627 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2628 orig_pwr_info.pwr_rx == FAST_MODE) {
2629 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2630 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2631 pwr_mode_change = true;
2632 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2633 orig_pwr_info.pwr_rx == SLOW_MODE) {
2634 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2635 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2636 pwr_mode_change = true;
2637 }
2638 if (pwr_mode_change) {
2639 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2640 if (ret)
2641 goto out;
2642 }
2643 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302644
2645 uic_cmd.command = peer ?
2646 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2647 uic_cmd.argument1 = attr_sel;
2648
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002649 do {
2650 /* for peer attributes we retry upon failure */
2651 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2652 if (ret)
2653 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2654 get, UIC_GET_ATTR_ID(attr_sel), ret);
2655 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302656
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002657 if (!retries)
2658 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2659 get, UIC_GET_ATTR_ID(attr_sel), retries);
2660
2661 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302662 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002663
2664 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2665 && pwr_mode_change)
2666 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302667out:
2668 return ret;
2669}
2670EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2671
2672/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002673 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2674 * state) and waits for it to take effect.
2675 *
2676 * @hba: per adapter instance
2677 * @cmd: UIC command to execute
2678 *
2679 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2680 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2681 * and device UniPro link and hence it's final completion would be indicated by
2682 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2683 * addition to normal UIC command completion Status (UCCS). This function only
2684 * returns after the relevant status bits indicate the completion.
2685 *
2686 * Returns 0 on success, non-zero value on failure
2687 */
2688static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2689{
2690 struct completion uic_async_done;
2691 unsigned long flags;
2692 u8 status;
2693 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002694 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002695
2696 mutex_lock(&hba->uic_cmd_mutex);
2697 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002698 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002699
2700 spin_lock_irqsave(hba->host->host_lock, flags);
2701 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002702 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2703 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2704 /*
2705 * Make sure UIC command completion interrupt is disabled before
2706 * issuing UIC command.
2707 */
2708 wmb();
2709 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002710 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002711 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2712 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002713 if (ret) {
2714 dev_err(hba->dev,
2715 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2716 cmd->command, cmd->argument3, ret);
2717 goto out;
2718 }
2719
2720 if (!wait_for_completion_timeout(hba->uic_async_done,
2721 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2722 dev_err(hba->dev,
2723 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2724 cmd->command, cmd->argument3);
2725 ret = -ETIMEDOUT;
2726 goto out;
2727 }
2728
2729 status = ufshcd_get_upmcrs(hba);
2730 if (status != PWR_LOCAL) {
2731 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002732 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002733 cmd->command, status);
2734 ret = (status != PWR_OK) ? status : -1;
2735 }
2736out:
2737 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002738 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002739 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002740 if (reenable_intr)
2741 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002742 spin_unlock_irqrestore(hba->host->host_lock, flags);
2743 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002744
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002745 return ret;
2746}
2747
2748/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302749 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2750 * using DME_SET primitives.
2751 * @hba: per adapter instance
2752 * @mode: powr mode value
2753 *
2754 * Returns 0 on success, non-zero value on failure
2755 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302756static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302757{
2758 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002759 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302760
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002761 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2762 ret = ufshcd_dme_set(hba,
2763 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2764 if (ret) {
2765 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2766 __func__, ret);
2767 goto out;
2768 }
2769 }
2770
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302771 uic_cmd.command = UIC_CMD_DME_SET;
2772 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2773 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002774 ufshcd_hold(hba, false);
2775 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2776 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302777
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002778out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002779 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002780}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302781
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002782static int ufshcd_link_recovery(struct ufs_hba *hba)
2783{
2784 int ret;
2785 unsigned long flags;
2786
2787 spin_lock_irqsave(hba->host->host_lock, flags);
2788 hba->ufshcd_state = UFSHCD_STATE_RESET;
2789 ufshcd_set_eh_in_progress(hba);
2790 spin_unlock_irqrestore(hba->host->host_lock, flags);
2791
2792 ret = ufshcd_host_reset_and_restore(hba);
2793
2794 spin_lock_irqsave(hba->host->host_lock, flags);
2795 if (ret)
2796 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2797 ufshcd_clear_eh_in_progress(hba);
2798 spin_unlock_irqrestore(hba->host->host_lock, flags);
2799
2800 if (ret)
2801 dev_err(hba->dev, "%s: link recovery failed, err %d",
2802 __func__, ret);
2803
2804 return ret;
2805}
2806
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002807static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002808{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002809 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002810 struct uic_command uic_cmd = {0};
2811
2812 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002813 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002814
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002815 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002816 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2817 __func__, ret);
2818
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002819 /*
2820 * If link recovery fails then return error so that caller
2821 * don't retry the hibern8 enter again.
2822 */
2823 if (ufshcd_link_recovery(hba))
2824 ret = -ENOLINK;
2825 }
2826
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002827 return ret;
2828}
2829
2830static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2831{
2832 int ret = 0, retries;
2833
2834 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2835 ret = __ufshcd_uic_hibern8_enter(hba);
2836 if (!ret || ret == -ENOLINK)
2837 goto out;
2838 }
2839out:
2840 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002841}
2842
2843static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2844{
2845 struct uic_command uic_cmd = {0};
2846 int ret;
2847
2848 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2849 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302850 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002851 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2852 __func__, ret);
2853 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302854 }
2855
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302856 return ret;
2857}
2858
Yaniv Gardi50646362014-10-23 13:25:13 +03002859 /**
2860 * ufshcd_init_pwr_info - setting the POR (power on reset)
2861 * values in hba power info
2862 * @hba: per-adapter instance
2863 */
2864static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2865{
2866 hba->pwr_info.gear_rx = UFS_PWM_G1;
2867 hba->pwr_info.gear_tx = UFS_PWM_G1;
2868 hba->pwr_info.lane_rx = 1;
2869 hba->pwr_info.lane_tx = 1;
2870 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2871 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2872 hba->pwr_info.hs_rate = 0;
2873}
2874
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302875/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002876 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2877 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302878 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002879static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302880{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002881 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2882
2883 if (hba->max_pwr_info.is_valid)
2884 return 0;
2885
2886 pwr_info->pwr_tx = FASTAUTO_MODE;
2887 pwr_info->pwr_rx = FASTAUTO_MODE;
2888 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302889
2890 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002891 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2892 &pwr_info->lane_rx);
2893 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2894 &pwr_info->lane_tx);
2895
2896 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2897 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2898 __func__,
2899 pwr_info->lane_rx,
2900 pwr_info->lane_tx);
2901 return -EINVAL;
2902 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302903
2904 /*
2905 * First, get the maximum gears of HS speed.
2906 * If a zero value, it means there is no HSGEAR capability.
2907 * Then, get the maximum gears of PWM speed.
2908 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002909 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2910 if (!pwr_info->gear_rx) {
2911 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2912 &pwr_info->gear_rx);
2913 if (!pwr_info->gear_rx) {
2914 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2915 __func__, pwr_info->gear_rx);
2916 return -EINVAL;
2917 }
2918 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302919 }
2920
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002921 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2922 &pwr_info->gear_tx);
2923 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302924 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002925 &pwr_info->gear_tx);
2926 if (!pwr_info->gear_tx) {
2927 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2928 __func__, pwr_info->gear_tx);
2929 return -EINVAL;
2930 }
2931 pwr_info->pwr_tx = SLOWAUTO_MODE;
2932 }
2933
2934 hba->max_pwr_info.is_valid = true;
2935 return 0;
2936}
2937
2938static int ufshcd_change_power_mode(struct ufs_hba *hba,
2939 struct ufs_pa_layer_attr *pwr_mode)
2940{
2941 int ret;
2942
2943 /* if already configured to the requested pwr_mode */
2944 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2945 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2946 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2947 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2948 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2949 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2950 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2951 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2952 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302953 }
2954
2955 /*
2956 * Configure attributes for power mode change with below.
2957 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2958 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2959 * - PA_HSSERIES
2960 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002961 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2962 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2963 pwr_mode->lane_rx);
2964 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2965 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302966 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002967 else
2968 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302969
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002970 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2971 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2972 pwr_mode->lane_tx);
2973 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2974 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302975 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002976 else
2977 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302978
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002979 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2980 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2981 pwr_mode->pwr_rx == FAST_MODE ||
2982 pwr_mode->pwr_tx == FAST_MODE)
2983 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2984 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302985
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002986 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2987 | pwr_mode->pwr_tx);
2988
2989 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302990 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002991 "%s: power mode change failed %d\n", __func__, ret);
2992 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002993 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2994 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002995
2996 memcpy(&hba->pwr_info, pwr_mode,
2997 sizeof(struct ufs_pa_layer_attr));
2998 }
2999
3000 return ret;
3001}
3002
3003/**
3004 * ufshcd_config_pwr_mode - configure a new power mode
3005 * @hba: per-adapter instance
3006 * @desired_pwr_mode: desired power configuration
3007 */
3008static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
3009 struct ufs_pa_layer_attr *desired_pwr_mode)
3010{
3011 struct ufs_pa_layer_attr final_params = { 0 };
3012 int ret;
3013
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003014 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
3015 desired_pwr_mode, &final_params);
3016
3017 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03003018 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3019
3020 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303021
3022 return ret;
3023}
3024
3025/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303026 * ufshcd_complete_dev_init() - checks device readiness
3027 * hba: per-adapter instance
3028 *
3029 * Set fDeviceInit flag and poll until device toggles it.
3030 */
3031static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3032{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003033 int i;
3034 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303035 bool flag_res = 1;
3036
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003037 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3038 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303039 if (err) {
3040 dev_err(hba->dev,
3041 "%s setting fDeviceInit flag failed with error %d\n",
3042 __func__, err);
3043 goto out;
3044 }
3045
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003046 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3047 for (i = 0; i < 1000 && !err && flag_res; i++)
3048 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3049 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3050
Dolev Raviv68078d52013-07-30 00:35:58 +05303051 if (err)
3052 dev_err(hba->dev,
3053 "%s reading fDeviceInit flag failed with error %d\n",
3054 __func__, err);
3055 else if (flag_res)
3056 dev_err(hba->dev,
3057 "%s fDeviceInit was not cleared by the device\n",
3058 __func__);
3059
3060out:
3061 return err;
3062}
3063
3064/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303065 * ufshcd_make_hba_operational - Make UFS controller operational
3066 * @hba: per adapter instance
3067 *
3068 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003069 * 1. Enable required interrupts
3070 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003071 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003072 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303073 *
3074 * Returns 0 on success, non-zero value on failure
3075 */
3076static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3077{
3078 int err = 0;
3079 u32 reg;
3080
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303081 /* Enable required interrupts */
3082 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3083
3084 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003085 if (ufshcd_is_intr_aggr_allowed(hba))
3086 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3087 else
3088 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303089
3090 /* Configure UTRL and UTMRL base address registers */
3091 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3092 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3093 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3094 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3095 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3096 REG_UTP_TASK_REQ_LIST_BASE_L);
3097 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3098 REG_UTP_TASK_REQ_LIST_BASE_H);
3099
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303100 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003101 * Make sure base address and interrupt setup are updated before
3102 * enabling the run/stop registers below.
3103 */
3104 wmb();
3105
3106 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303107 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303108 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003109 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303110 if (!(ufshcd_get_lists_status(reg))) {
3111 ufshcd_enable_run_stop_reg(hba);
3112 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303113 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303114 "Host controller not ready to process requests");
3115 err = -EIO;
3116 goto out;
3117 }
3118
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303119out:
3120 return err;
3121}
3122
3123/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003124 * ufshcd_hba_stop - Send controller to reset state
3125 * @hba: per adapter instance
3126 * @can_sleep: perform sleep or just spin
3127 */
3128static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3129{
3130 int err;
3131
3132 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3133 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3134 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3135 10, 1, can_sleep);
3136 if (err)
3137 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3138}
3139
3140/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303141 * ufshcd_hba_enable - initialize the controller
3142 * @hba: per adapter instance
3143 *
3144 * The controller resets itself and controller firmware initialization
3145 * sequence kicks off. When controller is ready it will set
3146 * the Host Controller Enable bit to 1.
3147 *
3148 * Returns 0 on success, non-zero value on failure
3149 */
3150static int ufshcd_hba_enable(struct ufs_hba *hba)
3151{
3152 int retry;
3153
3154 /*
3155 * msleep of 1 and 5 used in this function might result in msleep(20),
3156 * but it was necessary to send the UFS FPGA to reset mode during
3157 * development and testing of this driver. msleep can be changed to
3158 * mdelay and retry count can be reduced based on the controller.
3159 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003160 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303161 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003162 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303163
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003164 /* UniPro link is disabled at this point */
3165 ufshcd_set_link_off(hba);
3166
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003167 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003168
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303169 /* start controller initialization sequence */
3170 ufshcd_hba_start(hba);
3171
3172 /*
3173 * To initialize a UFS host controller HCE bit must be set to 1.
3174 * During initialization the HCE bit value changes from 1->0->1.
3175 * When the host controller completes initialization sequence
3176 * it sets the value of HCE bit to 1. The same HCE bit is read back
3177 * to check if the controller has completed initialization sequence.
3178 * So without this delay the value HCE = 1, set in the previous
3179 * instruction might be read back.
3180 * This delay can be changed based on the controller.
3181 */
3182 msleep(1);
3183
3184 /* wait for the host controller to complete initialization */
3185 retry = 10;
3186 while (ufshcd_is_hba_active(hba)) {
3187 if (retry) {
3188 retry--;
3189 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303190 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303191 "Controller enable failed\n");
3192 return -EIO;
3193 }
3194 msleep(5);
3195 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003196
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003197 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003198 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003199
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003200 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003201
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303202 return 0;
3203}
3204
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003205static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3206{
3207 int tx_lanes, i, err = 0;
3208
3209 if (!peer)
3210 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3211 &tx_lanes);
3212 else
3213 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3214 &tx_lanes);
3215 for (i = 0; i < tx_lanes; i++) {
3216 if (!peer)
3217 err = ufshcd_dme_set(hba,
3218 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3219 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3220 0);
3221 else
3222 err = ufshcd_dme_peer_set(hba,
3223 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3224 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3225 0);
3226 if (err) {
3227 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3228 __func__, peer, i, err);
3229 break;
3230 }
3231 }
3232
3233 return err;
3234}
3235
3236static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3237{
3238 return ufshcd_disable_tx_lcc(hba, true);
3239}
3240
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303241/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303242 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303243 * @hba: per adapter instance
3244 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303245 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303246 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303247static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303248{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303249 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003250 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003251 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303252
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003253 /*
3254 * If UFS device isn't active then we will have to issue link startup
3255 * 2 times to make sure the device state move to active.
3256 */
3257 if (!ufshcd_is_ufs_dev_active(hba))
3258 link_startup_again = true;
3259
3260link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003261 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003262 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303263
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003264 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003265
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003266 /* check if device is detected by inter-connect layer */
3267 if (!ret && !ufshcd_is_device_present(hba)) {
3268 dev_err(hba->dev, "%s: Device not present\n", __func__);
3269 ret = -ENXIO;
3270 goto out;
3271 }
3272
3273 /*
3274 * DME link lost indication is only received when link is up,
3275 * but we can't be sure if the link is up until link startup
3276 * succeeds. So reset the local Uni-Pro and try again.
3277 */
3278 if (ret && ufshcd_hba_enable(hba))
3279 goto out;
3280 } while (ret && retries--);
3281
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303282 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003283 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303284 goto out;
3285
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003286 if (link_startup_again) {
3287 link_startup_again = false;
3288 retries = DME_LINKSTARTUP_RETRIES;
3289 goto link_startup;
3290 }
3291
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003292 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3293 ret = ufshcd_disable_device_tx_lcc(hba);
3294 if (ret)
3295 goto out;
3296 }
3297
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003298 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003299 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3300 if (ret)
3301 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003302
3303 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303304out:
3305 if (ret)
3306 dev_err(hba->dev, "link startup failed %d\n", ret);
3307 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303308}
3309
3310/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303311 * ufshcd_verify_dev_init() - Verify device initialization
3312 * @hba: per-adapter instance
3313 *
3314 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3315 * device Transport Protocol (UTP) layer is ready after a reset.
3316 * If the UTP layer at the device side is not initialized, it may
3317 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3318 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3319 */
3320static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3321{
3322 int err = 0;
3323 int retries;
3324
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003325 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303326 mutex_lock(&hba->dev_cmd.lock);
3327 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3328 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3329 NOP_OUT_TIMEOUT);
3330
3331 if (!err || err == -ETIMEDOUT)
3332 break;
3333
3334 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3335 }
3336 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003337 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303338
3339 if (err)
3340 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3341 return err;
3342}
3343
3344/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003345 * ufshcd_set_queue_depth - set lun queue depth
3346 * @sdev: pointer to SCSI device
3347 *
3348 * Read bLUQueueDepth value and activate scsi tagged command
3349 * queueing. For WLUN, queue depth is set to 1. For best-effort
3350 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3351 * value that host can queue.
3352 */
3353static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3354{
3355 int ret = 0;
3356 u8 lun_qdepth;
3357 struct ufs_hba *hba;
3358
3359 hba = shost_priv(sdev->host);
3360
3361 lun_qdepth = hba->nutrs;
3362 ret = ufshcd_read_unit_desc_param(hba,
3363 ufshcd_scsi_to_upiu_lun(sdev->lun),
3364 UNIT_DESC_PARAM_LU_Q_DEPTH,
3365 &lun_qdepth,
3366 sizeof(lun_qdepth));
3367
3368 /* Some WLUN doesn't support unit descriptor */
3369 if (ret == -EOPNOTSUPP)
3370 lun_qdepth = 1;
3371 else if (!lun_qdepth)
3372 /* eventually, we can figure out the real queue depth */
3373 lun_qdepth = hba->nutrs;
3374 else
3375 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3376
3377 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3378 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003379 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003380}
3381
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003382/*
3383 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3384 * @hba: per-adapter instance
3385 * @lun: UFS device lun id
3386 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3387 *
3388 * Returns 0 in case of success and b_lu_write_protect status would be returned
3389 * @b_lu_write_protect parameter.
3390 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3391 * Returns -EINVAL in case of invalid parameters passed to this function.
3392 */
3393static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3394 u8 lun,
3395 u8 *b_lu_write_protect)
3396{
3397 int ret;
3398
3399 if (!b_lu_write_protect)
3400 ret = -EINVAL;
3401 /*
3402 * According to UFS device spec, RPMB LU can't be write
3403 * protected so skip reading bLUWriteProtect parameter for
3404 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3405 */
3406 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3407 ret = -ENOTSUPP;
3408 else
3409 ret = ufshcd_read_unit_desc_param(hba,
3410 lun,
3411 UNIT_DESC_PARAM_LU_WR_PROTECT,
3412 b_lu_write_protect,
3413 sizeof(*b_lu_write_protect));
3414 return ret;
3415}
3416
3417/**
3418 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3419 * status
3420 * @hba: per-adapter instance
3421 * @sdev: pointer to SCSI device
3422 *
3423 */
3424static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3425 struct scsi_device *sdev)
3426{
3427 if (hba->dev_info.f_power_on_wp_en &&
3428 !hba->dev_info.is_lu_power_on_wp) {
3429 u8 b_lu_write_protect;
3430
3431 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3432 &b_lu_write_protect) &&
3433 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3434 hba->dev_info.is_lu_power_on_wp = true;
3435 }
3436}
3437
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003438/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303439 * ufshcd_slave_alloc - handle initial SCSI device configurations
3440 * @sdev: pointer to SCSI device
3441 *
3442 * Returns success
3443 */
3444static int ufshcd_slave_alloc(struct scsi_device *sdev)
3445{
3446 struct ufs_hba *hba;
3447
3448 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303449
3450 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3451 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303452
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303453 /* allow SCSI layer to restart the device in case of errors */
3454 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003455
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003456 /* REPORT SUPPORTED OPERATION CODES is not supported */
3457 sdev->no_report_opcodes = 1;
3458
Sujit Reddy Thumma9760af42018-01-24 09:52:35 +05303459 /* WRITE_SAME command is not supported */
3460 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003461
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003462 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003463
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003464 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3465
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003466 return 0;
3467}
3468
3469/**
3470 * ufshcd_change_queue_depth - change queue depth
3471 * @sdev: pointer to SCSI device
3472 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003473 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003474 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003475 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003476static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003477{
3478 struct ufs_hba *hba = shost_priv(sdev->host);
3479
3480 if (depth > hba->nutrs)
3481 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003482 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303483}
3484
3485/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003486 * ufshcd_slave_configure - adjust SCSI device configurations
3487 * @sdev: pointer to SCSI device
3488 */
3489static int ufshcd_slave_configure(struct scsi_device *sdev)
3490{
3491 struct request_queue *q = sdev->request_queue;
3492
3493 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3494 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3495
3496 return 0;
3497}
3498
3499/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303500 * ufshcd_slave_destroy - remove SCSI device configurations
3501 * @sdev: pointer to SCSI device
3502 */
3503static void ufshcd_slave_destroy(struct scsi_device *sdev)
3504{
3505 struct ufs_hba *hba;
3506
3507 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003508 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003509 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3510 unsigned long flags;
3511
3512 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003513 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003514 spin_unlock_irqrestore(hba->host->host_lock, flags);
3515 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303516}
3517
3518/**
3519 * ufshcd_task_req_compl - handle task management request completion
3520 * @hba: per adapter instance
3521 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303522 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303523 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303524 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303525 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303526static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303527{
3528 struct utp_task_req_desc *task_req_descp;
3529 struct utp_upiu_task_rsp *task_rsp_upiup;
3530 unsigned long flags;
3531 int ocs_value;
3532 int task_result;
3533
3534 spin_lock_irqsave(hba->host->host_lock, flags);
3535
3536 /* Clear completed tasks from outstanding_tasks */
3537 __clear_bit(index, &hba->outstanding_tasks);
3538
3539 task_req_descp = hba->utmrdl_base_addr;
3540 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3541
3542 if (ocs_value == OCS_SUCCESS) {
3543 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3544 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003545 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3546 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303547 if (resp)
3548 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303549 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303550 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3551 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303552 }
3553 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303554
3555 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303556}
3557
3558/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303559 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3560 * @lrb: pointer to local reference block of completed command
3561 * @scsi_status: SCSI command status
3562 *
3563 * Returns value base on SCSI command status
3564 */
3565static inline int
3566ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3567{
3568 int result = 0;
3569
3570 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303571 case SAM_STAT_CHECK_CONDITION:
3572 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303573 case SAM_STAT_GOOD:
3574 result |= DID_OK << 16 |
3575 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303576 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303577 break;
3578 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303579 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303580 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303581 ufshcd_copy_sense_data(lrbp);
3582 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303583 break;
3584 default:
3585 result |= DID_ERROR << 16;
3586 break;
3587 } /* end of switch */
3588
3589 return result;
3590}
3591
3592/**
3593 * ufshcd_transfer_rsp_status - Get overall status of the response
3594 * @hba: per adapter instance
3595 * @lrb: pointer to local reference block of completed command
3596 *
3597 * Returns result of the command to notify SCSI midlayer
3598 */
3599static inline int
3600ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3601{
3602 int result = 0;
3603 int scsi_status;
3604 int ocs;
3605
3606 /* overall command status of utrd */
3607 ocs = ufshcd_get_tr_ocs(lrbp);
3608
3609 switch (ocs) {
3610 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303611 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303612
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303613 switch (result) {
3614 case UPIU_TRANSACTION_RESPONSE:
3615 /*
3616 * get the response UPIU result to extract
3617 * the SCSI command status
3618 */
3619 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3620
3621 /*
3622 * get the result based on SCSI status response
3623 * to notify the SCSI midlayer of the command status
3624 */
3625 scsi_status = result & MASK_SCSI_STATUS;
3626 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303627
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003628 /*
3629 * Currently we are only supporting BKOPs exception
3630 * events hence we can ignore BKOPs exception event
3631 * during power management callbacks. BKOPs exception
3632 * event is not expected to be raised in runtime suspend
3633 * callback as it allows the urgent bkops.
3634 * During system suspend, we are anyway forcefully
3635 * disabling the bkops and if urgent bkops is needed
3636 * it will be enabled on system resume. Long term
3637 * solution could be to abort the system suspend if
3638 * UFS device needs urgent BKOPs.
3639 */
3640 if (!hba->pm_op_in_progress &&
3641 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303642 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303643 break;
3644 case UPIU_TRANSACTION_REJECT_UPIU:
3645 /* TODO: handle Reject UPIU Response */
3646 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303647 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303648 "Reject UPIU not fully implemented\n");
3649 break;
3650 default:
3651 result = DID_ERROR << 16;
3652 dev_err(hba->dev,
3653 "Unexpected request response code = %x\n",
3654 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303655 break;
3656 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303657 break;
3658 case OCS_ABORTED:
3659 result |= DID_ABORT << 16;
3660 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303661 case OCS_INVALID_COMMAND_STATUS:
3662 result |= DID_REQUEUE << 16;
3663 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303664 case OCS_INVALID_CMD_TABLE_ATTR:
3665 case OCS_INVALID_PRDT_ATTR:
3666 case OCS_MISMATCH_DATA_BUF_SIZE:
3667 case OCS_MISMATCH_RESP_UPIU_SIZE:
3668 case OCS_PEER_COMM_FAILURE:
3669 case OCS_FATAL_ERROR:
3670 default:
3671 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303672 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303673 "OCS error from controller = %x\n", ocs);
3674 break;
3675 } /* end of switch */
3676
3677 return result;
3678}
3679
3680/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303681 * ufshcd_uic_cmd_compl - handle completion of uic command
3682 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303683 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303684 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303685static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303686{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303687 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303688 hba->active_uic_cmd->argument2 |=
3689 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303690 hba->active_uic_cmd->argument3 =
3691 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303692 complete(&hba->active_uic_cmd->done);
3693 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303694
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003695 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3696 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303697}
3698
3699/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003700 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303701 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003702 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303703 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003704static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3705 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303706{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303707 struct ufshcd_lrb *lrbp;
3708 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303709 int result;
3710 int index;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003711 struct request *req;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003712
Dolev Ravive9d501b2014-07-01 12:22:37 +03003713 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3714 lrbp = &hba->lrb[index];
3715 cmd = lrbp->cmd;
3716 if (cmd) {
3717 result = ufshcd_transfer_rsp_status(hba, lrbp);
3718 scsi_dma_unmap(cmd);
3719 cmd->result = result;
3720 /* Mark completed command as NULL in LRB */
3721 lrbp->cmd = NULL;
3722 clear_bit_unlock(index, &hba->lrb_in_use);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003723 req = cmd->request;
3724 if (req) {
3725 /* Update IO svc time latency histogram */
3726 if (req->lat_hist_enabled) {
3727 ktime_t completion;
3728 u_int64_t delta_us;
3729
3730 completion = ktime_get();
3731 delta_us = ktime_us_delta(completion,
3732 req->lat_hist_io_start);
Hyojun Kim11537d52017-12-21 09:57:41 -08003733 blk_update_latency_hist(
3734 (rq_data_dir(req) == READ) ?
3735 &hba->io_lat_read :
3736 &hba->io_lat_write, delta_us);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003737 }
3738 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03003739 /* Do not touch lrbp after scsi done */
3740 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003741 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003742 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3743 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003744 if (hba->dev_cmd.complete)
3745 complete(hba->dev_cmd.complete);
3746 }
3747 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303748
3749 /* clear corresponding bits of completed commands */
3750 hba->outstanding_reqs ^= completed_reqs;
3751
Sahitya Tummala856b3482014-09-25 15:32:34 +03003752 ufshcd_clk_scaling_update_busy(hba);
3753
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303754 /* we might have free'd some tags above */
3755 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303756}
3757
3758/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003759 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3760 * @hba: per adapter instance
3761 */
3762static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3763{
3764 unsigned long completed_reqs;
3765 u32 tr_doorbell;
3766
3767 /* Resetting interrupt aggregation counters first and reading the
3768 * DOOR_BELL afterward allows us to handle all the completed requests.
3769 * In order to prevent other interrupts starvation the DB is read once
3770 * after reset. The down side of this solution is the possibility of
3771 * false interrupt if device completes another request after resetting
3772 * aggregation and before reading the DB.
3773 */
3774 if (ufshcd_is_intr_aggr_allowed(hba))
3775 ufshcd_reset_intr_aggr(hba);
3776
3777 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3778 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3779
3780 __ufshcd_transfer_req_compl(hba, completed_reqs);
3781}
3782
3783/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303784 * ufshcd_disable_ee - disable exception event
3785 * @hba: per-adapter instance
3786 * @mask: exception event to disable
3787 *
3788 * Disables exception event in the device so that the EVENT_ALERT
3789 * bit is not set.
3790 *
3791 * Returns zero on success, non-zero error value on failure.
3792 */
3793static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3794{
3795 int err = 0;
3796 u32 val;
3797
3798 if (!(hba->ee_ctrl_mask & mask))
3799 goto out;
3800
3801 val = hba->ee_ctrl_mask & ~mask;
3802 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003803 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303804 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3805 if (!err)
3806 hba->ee_ctrl_mask &= ~mask;
3807out:
3808 return err;
3809}
3810
3811/**
3812 * ufshcd_enable_ee - enable exception event
3813 * @hba: per-adapter instance
3814 * @mask: exception event to enable
3815 *
3816 * Enable corresponding exception event in the device to allow
3817 * device to alert host in critical scenarios.
3818 *
3819 * Returns zero on success, non-zero error value on failure.
3820 */
3821static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3822{
3823 int err = 0;
3824 u32 val;
3825
3826 if (hba->ee_ctrl_mask & mask)
3827 goto out;
3828
3829 val = hba->ee_ctrl_mask | mask;
3830 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003831 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303832 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3833 if (!err)
3834 hba->ee_ctrl_mask |= mask;
3835out:
3836 return err;
3837}
3838
3839/**
3840 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3841 * @hba: per-adapter instance
3842 *
3843 * Allow device to manage background operations on its own. Enabling
3844 * this might lead to inconsistent latencies during normal data transfers
3845 * as the device is allowed to manage its own way of handling background
3846 * operations.
3847 *
3848 * Returns zero on success, non-zero on failure.
3849 */
3850static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3851{
3852 int err = 0;
3853
3854 if (hba->auto_bkops_enabled)
3855 goto out;
3856
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003857 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303858 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3859 if (err) {
3860 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3861 __func__, err);
3862 goto out;
3863 }
3864
3865 hba->auto_bkops_enabled = true;
3866
3867 /* No need of URGENT_BKOPS exception from the device */
3868 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3869 if (err)
3870 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3871 __func__, err);
3872out:
3873 return err;
3874}
3875
3876/**
3877 * ufshcd_disable_auto_bkops - block device in doing background operations
3878 * @hba: per-adapter instance
3879 *
3880 * Disabling background operations improves command response latency but
3881 * has drawback of device moving into critical state where the device is
3882 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3883 * host is idle so that BKOPS are managed effectively without any negative
3884 * impacts.
3885 *
3886 * Returns zero on success, non-zero on failure.
3887 */
3888static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3889{
3890 int err = 0;
3891
3892 if (!hba->auto_bkops_enabled)
3893 goto out;
3894
3895 /*
3896 * If host assisted BKOPs is to be enabled, make sure
3897 * urgent bkops exception is allowed.
3898 */
3899 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3900 if (err) {
3901 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3902 __func__, err);
3903 goto out;
3904 }
3905
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003906 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303907 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3908 if (err) {
3909 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3910 __func__, err);
3911 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3912 goto out;
3913 }
3914
3915 hba->auto_bkops_enabled = false;
3916out:
3917 return err;
3918}
3919
3920/**
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003921 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303922 * @hba: per adapter instance
3923 *
3924 * After a device reset the device may toggle the BKOPS_EN flag
3925 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003926 * as well. This function would change the auto-bkops state based on
3927 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303928 */
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003929static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303930{
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003931 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
3932 hba->auto_bkops_enabled = false;
3933 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3934 ufshcd_enable_auto_bkops(hba);
3935 } else {
3936 hba->auto_bkops_enabled = true;
3937 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
3938 ufshcd_disable_auto_bkops(hba);
3939 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303940}
3941
3942static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3943{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003944 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303945 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3946}
3947
3948/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003949 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3950 * @hba: per-adapter instance
3951 * @status: bkops_status value
3952 *
3953 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3954 * flag in the device to permit background operations if the device
3955 * bkops_status is greater than or equal to "status" argument passed to
3956 * this function, disable otherwise.
3957 *
3958 * Returns 0 for success, non-zero in case of failure.
3959 *
3960 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3961 * to know whether auto bkops is enabled or disabled after this function
3962 * returns control to it.
3963 */
3964static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3965 enum bkops_status status)
3966{
3967 int err;
3968 u32 curr_status = 0;
3969
3970 err = ufshcd_get_bkops_status(hba, &curr_status);
3971 if (err) {
3972 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3973 __func__, err);
3974 goto out;
3975 } else if (curr_status > BKOPS_STATUS_MAX) {
3976 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3977 __func__, curr_status);
3978 err = -EINVAL;
3979 goto out;
3980 }
3981
3982 if (curr_status >= status)
3983 err = ufshcd_enable_auto_bkops(hba);
3984 else
3985 err = ufshcd_disable_auto_bkops(hba);
3986out:
3987 return err;
3988}
3989
3990/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303991 * ufshcd_urgent_bkops - handle urgent bkops exception event
3992 * @hba: per-adapter instance
3993 *
3994 * Enable fBackgroundOpsEn flag in the device to permit background
3995 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003996 *
3997 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3998 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303999 */
4000static int ufshcd_urgent_bkops(struct ufs_hba *hba)
4001{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004002 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304003}
4004
4005static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
4006{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004007 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304008 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
4009}
4010
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004011static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
4012{
4013 int err;
4014 u32 curr_status = 0;
4015
4016 if (hba->is_urgent_bkops_lvl_checked)
4017 goto enable_auto_bkops;
4018
4019 err = ufshcd_get_bkops_status(hba, &curr_status);
4020 if (err) {
4021 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4022 __func__, err);
4023 goto out;
4024 }
4025
4026 /*
4027 * We are seeing that some devices are raising the urgent bkops
4028 * exception events even when BKOPS status doesn't indicate performace
4029 * impacted or critical. Handle these device by determining their urgent
4030 * bkops status at runtime.
4031 */
4032 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4033 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4034 __func__, curr_status);
4035 /* update the current status as the urgent bkops level */
4036 hba->urgent_bkops_lvl = curr_status;
4037 hba->is_urgent_bkops_lvl_checked = true;
4038 }
4039
4040enable_auto_bkops:
4041 err = ufshcd_enable_auto_bkops(hba);
4042out:
4043 if (err < 0)
4044 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4045 __func__, err);
4046}
4047
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304048/**
4049 * ufshcd_exception_event_handler - handle exceptions raised by device
4050 * @work: pointer to work data
4051 *
4052 * Read bExceptionEventStatus attribute from the device and handle the
4053 * exception event accordingly.
4054 */
4055static void ufshcd_exception_event_handler(struct work_struct *work)
4056{
4057 struct ufs_hba *hba;
4058 int err;
4059 u32 status = 0;
4060 hba = container_of(work, struct ufs_hba, eeh_work);
4061
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304062 pm_runtime_get_sync(hba->dev);
Maya Erez62413ba2018-05-03 16:37:16 +05304063 scsi_block_requests(hba->host);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304064 err = ufshcd_get_ee_status(hba, &status);
4065 if (err) {
4066 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4067 __func__, err);
4068 goto out;
4069 }
4070
4071 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004072
4073 if (status & MASK_EE_URGENT_BKOPS)
4074 ufshcd_bkops_exception_event_handler(hba);
4075
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304076out:
Maya Erez62413ba2018-05-03 16:37:16 +05304077 scsi_unblock_requests(hba->host);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304078 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304079 return;
4080}
4081
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004082/* Complete requests that have door-bell cleared */
4083static void ufshcd_complete_requests(struct ufs_hba *hba)
4084{
4085 ufshcd_transfer_req_compl(hba);
4086 ufshcd_tmc_handler(hba);
4087}
4088
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304089/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004090 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4091 * to recover from the DL NAC errors or not.
4092 * @hba: per-adapter instance
4093 *
4094 * Returns true if error handling is required, false otherwise
4095 */
4096static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4097{
4098 unsigned long flags;
4099 bool err_handling = true;
4100
4101 spin_lock_irqsave(hba->host->host_lock, flags);
4102 /*
4103 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4104 * device fatal error and/or DL NAC & REPLAY timeout errors.
4105 */
4106 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4107 goto out;
4108
4109 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4110 ((hba->saved_err & UIC_ERROR) &&
4111 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4112 goto out;
4113
4114 if ((hba->saved_err & UIC_ERROR) &&
4115 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4116 int err;
4117 /*
4118 * wait for 50ms to see if we can get any other errors or not.
4119 */
4120 spin_unlock_irqrestore(hba->host->host_lock, flags);
4121 msleep(50);
4122 spin_lock_irqsave(hba->host->host_lock, flags);
4123
4124 /*
4125 * now check if we have got any other severe errors other than
4126 * DL NAC error?
4127 */
4128 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4129 ((hba->saved_err & UIC_ERROR) &&
4130 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4131 goto out;
4132
4133 /*
4134 * As DL NAC is the only error received so far, send out NOP
4135 * command to confirm if link is still active or not.
4136 * - If we don't get any response then do error recovery.
4137 * - If we get response then clear the DL NAC error bit.
4138 */
4139
4140 spin_unlock_irqrestore(hba->host->host_lock, flags);
4141 err = ufshcd_verify_dev_init(hba);
4142 spin_lock_irqsave(hba->host->host_lock, flags);
4143
4144 if (err)
4145 goto out;
4146
4147 /* Link seems to be alive hence ignore the DL NAC errors */
4148 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4149 hba->saved_err &= ~UIC_ERROR;
4150 /* clear NAC error */
4151 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4152 if (!hba->saved_uic_err) {
4153 err_handling = false;
4154 goto out;
4155 }
4156 }
4157out:
4158 spin_unlock_irqrestore(hba->host->host_lock, flags);
4159 return err_handling;
4160}
4161
4162/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304163 * ufshcd_err_handler - handle UFS errors that require s/w attention
4164 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304165 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304166static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304167{
4168 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304169 unsigned long flags;
4170 u32 err_xfer = 0;
4171 u32 err_tm = 0;
4172 int err = 0;
4173 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004174 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304175
4176 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304177
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304178 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004179 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304180
4181 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004182 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304183 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304184
4185 hba->ufshcd_state = UFSHCD_STATE_RESET;
4186 ufshcd_set_eh_in_progress(hba);
4187
4188 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004189 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004190
4191 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4192 bool ret;
4193
4194 spin_unlock_irqrestore(hba->host->host_lock, flags);
4195 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4196 ret = ufshcd_quirk_dl_nac_errors(hba);
4197 spin_lock_irqsave(hba->host->host_lock, flags);
4198 if (!ret)
4199 goto skip_err_handling;
4200 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004201 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4202 ((hba->saved_err & UIC_ERROR) &&
4203 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4204 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4205 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4206 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304207
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004208 /*
4209 * if host reset is required then skip clearing the pending
4210 * transfers forcefully because they will automatically get
4211 * cleared after link startup.
4212 */
4213 if (needs_reset)
4214 goto skip_pending_xfer_clear;
4215
4216 /* release lock as clear command might sleep */
4217 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304218 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004219 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4220 if (ufshcd_clear_cmd(hba, tag)) {
4221 err_xfer = true;
4222 goto lock_skip_pending_xfer_clear;
4223 }
4224 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304225
4226 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004227 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4228 if (ufshcd_clear_tm_cmd(hba, tag)) {
4229 err_tm = true;
4230 goto lock_skip_pending_xfer_clear;
4231 }
4232 }
4233
4234lock_skip_pending_xfer_clear:
4235 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304236
4237 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004238 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304239
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004240 if (err_xfer || err_tm)
4241 needs_reset = true;
4242
4243skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304244 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004245 if (needs_reset) {
4246 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4247
4248 /*
4249 * ufshcd_reset_and_restore() does the link reinitialization
4250 * which will need atleast one empty doorbell slot to send the
4251 * device management commands (NOP and query commands).
4252 * If there is no slot empty at this moment then free up last
4253 * slot forcefully.
4254 */
4255 if (hba->outstanding_reqs == max_doorbells)
4256 __ufshcd_transfer_req_compl(hba,
4257 (1UL << (hba->nutrs - 1)));
4258
4259 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304260 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004261 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304262 if (err) {
4263 dev_err(hba->dev, "%s: reset and restore failed\n",
4264 __func__);
4265 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4266 }
4267 /*
4268 * Inform scsi mid-layer that we did reset and allow to handle
4269 * Unit Attention properly.
4270 */
4271 scsi_report_bus_reset(hba->host, 0);
4272 hba->saved_err = 0;
4273 hba->saved_uic_err = 0;
4274 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004275
Yaniv Gardi583fa622016-03-10 17:37:13 +02004276skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004277 if (!needs_reset) {
4278 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4279 if (hba->saved_err || hba->saved_uic_err)
4280 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4281 __func__, hba->saved_err, hba->saved_uic_err);
4282 }
4283
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304284 ufshcd_clear_eh_in_progress(hba);
4285
4286out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004287 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304288 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004289 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304290 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304291}
4292
4293/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304294 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4295 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304296 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304297static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304298{
4299 u32 reg;
4300
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304301 /* PA_INIT_ERROR is fatal and needs UIC reset */
4302 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4303 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4304 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004305 else if (hba->dev_quirks &
4306 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4307 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4308 hba->uic_error |=
4309 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4310 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4311 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4312 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304313
4314 /* UIC NL/TL/DME errors needs software retry */
4315 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4316 if (reg)
4317 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4318
4319 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4320 if (reg)
4321 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4322
4323 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4324 if (reg)
4325 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4326
4327 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4328 __func__, hba->uic_error);
4329}
4330
4331/**
4332 * ufshcd_check_errors - Check for errors that need s/w attention
4333 * @hba: per-adapter instance
4334 */
4335static void ufshcd_check_errors(struct ufs_hba *hba)
4336{
4337 bool queue_eh_work = false;
4338
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304339 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304340 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304341
4342 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304343 hba->uic_error = 0;
4344 ufshcd_update_uic_error(hba);
4345 if (hba->uic_error)
4346 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304347 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304348
4349 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004350 /*
4351 * update the transfer error masks to sticky bits, let's do this
4352 * irrespective of current ufshcd_state.
4353 */
4354 hba->saved_err |= hba->errors;
4355 hba->saved_uic_err |= hba->uic_error;
4356
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304357 /* handle fatal errors only when link is functional */
4358 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4359 /* block commands from scsi mid-layer */
4360 scsi_block_requests(hba->host);
4361
Zang Leiganga17bddc2017-04-04 19:32:20 +00004362 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304363 schedule_work(&hba->eh_work);
4364 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304365 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304366 /*
4367 * if (!queue_eh_work) -
4368 * Other errors are either non-fatal where host recovers
4369 * itself without s/w intervention or errors that will be
4370 * handled by the SCSI core layer.
4371 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304372}
4373
4374/**
4375 * ufshcd_tmc_handler - handle task management function completion
4376 * @hba: per adapter instance
4377 */
4378static void ufshcd_tmc_handler(struct ufs_hba *hba)
4379{
4380 u32 tm_doorbell;
4381
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304382 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304383 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304384 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304385}
4386
4387/**
4388 * ufshcd_sl_intr - Interrupt service routine
4389 * @hba: per adapter instance
4390 * @intr_status: contains interrupts generated by the controller
4391 */
4392static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4393{
4394 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4395 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304396 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304397
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304398 if (intr_status & UFSHCD_UIC_MASK)
4399 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304400
4401 if (intr_status & UTP_TASK_REQ_COMPL)
4402 ufshcd_tmc_handler(hba);
4403
4404 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4405 ufshcd_transfer_req_compl(hba);
4406}
4407
4408/**
4409 * ufshcd_intr - Main interrupt service routine
4410 * @irq: irq number
4411 * @__hba: pointer to adapter instance
4412 *
4413 * Returns IRQ_HANDLED - If interrupt is valid
4414 * IRQ_NONE - If invalid interrupt
4415 */
4416static irqreturn_t ufshcd_intr(int irq, void *__hba)
4417{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004418 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304419 irqreturn_t retval = IRQ_NONE;
4420 struct ufs_hba *hba = __hba;
4421
4422 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304423 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004424 enabled_intr_status =
4425 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304426
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004427 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304428 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004429
4430 if (enabled_intr_status) {
4431 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432 retval = IRQ_HANDLED;
4433 }
4434 spin_unlock(hba->host->host_lock);
4435 return retval;
4436}
4437
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304438static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4439{
4440 int err = 0;
4441 u32 mask = 1 << tag;
4442 unsigned long flags;
4443
4444 if (!test_bit(tag, &hba->outstanding_tasks))
4445 goto out;
4446
4447 spin_lock_irqsave(hba->host->host_lock, flags);
4448 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4449 spin_unlock_irqrestore(hba->host->host_lock, flags);
4450
4451 /* poll for max. 1 sec to clear door bell register by h/w */
4452 err = ufshcd_wait_for_register(hba,
4453 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004454 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304455out:
4456 return err;
4457}
4458
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304459/**
4460 * ufshcd_issue_tm_cmd - issues task management commands to controller
4461 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304462 * @lun_id: LUN ID to which TM command is sent
4463 * @task_id: task ID to which the TM command is applicable
4464 * @tm_function: task management function opcode
4465 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304466 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304467 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304468 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304469static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4470 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304471{
4472 struct utp_task_req_desc *task_req_descp;
4473 struct utp_upiu_task_req *task_req_upiup;
4474 struct Scsi_Host *host;
4475 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304476 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304477 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304478 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304479
4480 host = hba->host;
4481
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304482 /*
4483 * Get free slot, sleep if slots are unavailable.
4484 * Even though we use wait_event() which sleeps indefinitely,
4485 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4486 */
4487 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004488 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304489
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304490 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304491 task_req_descp = hba->utmrdl_base_addr;
4492 task_req_descp += free_slot;
4493
4494 /* Configure task request descriptor */
4495 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4496 task_req_descp->header.dword_2 =
4497 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4498
4499 /* Configure task request UPIU */
4500 task_req_upiup =
4501 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304502 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304503 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304504 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304505 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304506 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304507 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004508 /*
4509 * The host shall provide the same value for LUN field in the basic
4510 * header and for Input Parameter.
4511 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304512 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4513 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304514
4515 /* send command to the controller */
4516 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004517
4518 /* Make sure descriptors are ready before ringing the task doorbell */
4519 wmb();
4520
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304521 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304522
4523 spin_unlock_irqrestore(host->host_lock, flags);
4524
4525 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304526 err = wait_event_timeout(hba->tm_wq,
4527 test_bit(free_slot, &hba->tm_condition),
4528 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304529 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304530 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4531 __func__, tm_function);
4532 if (ufshcd_clear_tm_cmd(hba, free_slot))
4533 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4534 __func__, free_slot);
4535 err = -ETIMEDOUT;
4536 } else {
4537 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304538 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304539
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304540 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304541 ufshcd_put_tm_slot(hba, free_slot);
4542 wake_up(&hba->tm_tag_wq);
4543
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004544 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304545 return err;
4546}
4547
4548/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304549 * ufshcd_eh_device_reset_handler - device reset handler registered to
4550 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304551 * @cmd: SCSI command pointer
4552 *
4553 * Returns SUCCESS/FAILED
4554 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304555static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304556{
4557 struct Scsi_Host *host;
4558 struct ufs_hba *hba;
4559 unsigned int tag;
4560 u32 pos;
4561 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304562 u8 resp = 0xF;
4563 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304564 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304565
4566 host = cmd->device->host;
4567 hba = shost_priv(host);
4568 tag = cmd->request->tag;
4569
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304570 lrbp = &hba->lrb[tag];
4571 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4572 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304573 if (!err)
4574 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304575 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304576 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304577
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304578 /* clear the commands that were pending for corresponding LUN */
4579 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4580 if (hba->lrb[pos].lun == lrbp->lun) {
4581 err = ufshcd_clear_cmd(hba, pos);
4582 if (err)
4583 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304584 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304585 }
4586 spin_lock_irqsave(host->host_lock, flags);
4587 ufshcd_transfer_req_compl(hba);
4588 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304589out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304590 if (!err) {
4591 err = SUCCESS;
4592 } else {
4593 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4594 err = FAILED;
4595 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304596 return err;
4597}
4598
4599/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304600 * ufshcd_abort - abort a specific command
4601 * @cmd: SCSI command pointer
4602 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304603 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4604 * command, and in host controller by clearing the door-bell register. There can
4605 * be race between controller sending the command to the device while abort is
4606 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4607 * really issued and then try to abort it.
4608 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304609 * Returns SUCCESS/FAILED
4610 */
4611static int ufshcd_abort(struct scsi_cmnd *cmd)
4612{
4613 struct Scsi_Host *host;
4614 struct ufs_hba *hba;
4615 unsigned long flags;
4616 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304617 int err = 0;
4618 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304619 u8 resp = 0xF;
4620 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004621 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304622
4623 host = cmd->device->host;
4624 hba = shost_priv(host);
4625 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004626 if (!ufshcd_valid_tag(hba, tag)) {
4627 dev_err(hba->dev,
4628 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4629 __func__, tag, cmd, cmd->request);
4630 BUG();
4631 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304632
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004633 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004634 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004635 /* If command is already aborted/completed, return SUCCESS */
4636 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4637 dev_err(hba->dev,
4638 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4639 __func__, tag, hba->outstanding_reqs, reg);
4640 goto out;
4641 }
4642
Dolev Ravive9d501b2014-07-01 12:22:37 +03004643 if (!(reg & (1 << tag))) {
4644 dev_err(hba->dev,
4645 "%s: cmd was completed, but without a notifying intr, tag = %d",
4646 __func__, tag);
4647 }
4648
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304649 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304650 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4651 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4652 UFS_QUERY_TASK, &resp);
4653 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4654 /* cmd pending in the device */
4655 break;
4656 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304657 /*
4658 * cmd not pending in the device, check if it is
4659 * in transition.
4660 */
4661 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4662 if (reg & (1 << tag)) {
4663 /* sleep for max. 200us to stabilize */
4664 usleep_range(100, 200);
4665 continue;
4666 }
4667 /* command completed already */
4668 goto out;
4669 } else {
4670 if (!err)
4671 err = resp; /* service response error */
4672 goto out;
4673 }
4674 }
4675
4676 if (!poll_cnt) {
4677 err = -EBUSY;
4678 goto out;
4679 }
4680
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304681 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4682 UFS_ABORT_TASK, &resp);
4683 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304684 if (!err)
4685 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304686 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304687 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304688
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304689 err = ufshcd_clear_cmd(hba, tag);
4690 if (err)
4691 goto out;
4692
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304693 scsi_dma_unmap(cmd);
4694
4695 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004696 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304697 hba->lrb[tag].cmd = NULL;
4698 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304699
4700 clear_bit_unlock(tag, &hba->lrb_in_use);
4701 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004702
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304703out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304704 if (!err) {
4705 err = SUCCESS;
4706 } else {
4707 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4708 err = FAILED;
4709 }
4710
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004711 /*
4712 * This ufshcd_release() corresponds to the original scsi cmd that got
4713 * aborted here (as we won't get any IRQ for it).
4714 */
4715 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304716 return err;
4717}
4718
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304719/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304720 * ufshcd_host_reset_and_restore - reset and restore host controller
4721 * @hba: per-adapter instance
4722 *
4723 * Note that host controller reset may issue DME_RESET to
4724 * local and remote (device) Uni-Pro stack and the attributes
4725 * are reset to default state.
4726 *
4727 * Returns zero on success, non-zero on failure
4728 */
4729static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4730{
4731 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304732 unsigned long flags;
4733
4734 /* Reset the host controller */
4735 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004736 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304737 spin_unlock_irqrestore(hba->host->host_lock, flags);
4738
4739 err = ufshcd_hba_enable(hba);
4740 if (err)
4741 goto out;
4742
4743 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004744 err = ufshcd_probe_hba(hba);
4745
4746 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304747 err = -EIO;
4748out:
4749 if (err)
4750 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4751
4752 return err;
4753}
4754
4755/**
4756 * ufshcd_reset_and_restore - reset and re-initialize host/device
4757 * @hba: per-adapter instance
4758 *
4759 * Reset and recover device, host and re-establish link. This
4760 * is helpful to recover the communication in fatal error conditions.
4761 *
4762 * Returns zero on success, non-zero on failure
4763 */
4764static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4765{
4766 int err = 0;
4767 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004768 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304769
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004770 do {
4771 err = ufshcd_host_reset_and_restore(hba);
4772 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304773
4774 /*
4775 * After reset the door-bell might be cleared, complete
4776 * outstanding requests in s/w here.
4777 */
4778 spin_lock_irqsave(hba->host->host_lock, flags);
4779 ufshcd_transfer_req_compl(hba);
4780 ufshcd_tmc_handler(hba);
4781 spin_unlock_irqrestore(hba->host->host_lock, flags);
4782
4783 return err;
4784}
4785
4786/**
4787 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4788 * @cmd - SCSI command pointer
4789 *
4790 * Returns SUCCESS/FAILED
4791 */
4792static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4793{
4794 int err;
4795 unsigned long flags;
4796 struct ufs_hba *hba;
4797
4798 hba = shost_priv(cmd->device->host);
4799
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004800 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304801 /*
4802 * Check if there is any race with fatal error handling.
4803 * If so, wait for it to complete. Even though fatal error
4804 * handling does reset and restore in some cases, don't assume
4805 * anything out of it. We are just avoiding race here.
4806 */
4807 do {
4808 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304809 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304810 hba->ufshcd_state == UFSHCD_STATE_RESET))
4811 break;
4812 spin_unlock_irqrestore(hba->host->host_lock, flags);
4813 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304814 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304815 } while (1);
4816
4817 hba->ufshcd_state = UFSHCD_STATE_RESET;
4818 ufshcd_set_eh_in_progress(hba);
4819 spin_unlock_irqrestore(hba->host->host_lock, flags);
4820
4821 err = ufshcd_reset_and_restore(hba);
4822
4823 spin_lock_irqsave(hba->host->host_lock, flags);
4824 if (!err) {
4825 err = SUCCESS;
4826 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4827 } else {
4828 err = FAILED;
4829 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4830 }
4831 ufshcd_clear_eh_in_progress(hba);
4832 spin_unlock_irqrestore(hba->host->host_lock, flags);
4833
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004834 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304835 return err;
4836}
4837
4838/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004839 * ufshcd_get_max_icc_level - calculate the ICC level
4840 * @sup_curr_uA: max. current supported by the regulator
4841 * @start_scan: row at the desc table to start scan from
4842 * @buff: power descriptor buffer
4843 *
4844 * Returns calculated max ICC level for specific regulator
4845 */
4846static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4847{
4848 int i;
4849 int curr_uA;
4850 u16 data;
4851 u16 unit;
4852
4853 for (i = start_scan; i >= 0; i--) {
4854 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4855 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4856 ATTR_ICC_LVL_UNIT_OFFSET;
4857 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4858 switch (unit) {
4859 case UFSHCD_NANO_AMP:
4860 curr_uA = curr_uA / 1000;
4861 break;
4862 case UFSHCD_MILI_AMP:
4863 curr_uA = curr_uA * 1000;
4864 break;
4865 case UFSHCD_AMP:
4866 curr_uA = curr_uA * 1000 * 1000;
4867 break;
4868 case UFSHCD_MICRO_AMP:
4869 default:
4870 break;
4871 }
4872 if (sup_curr_uA >= curr_uA)
4873 break;
4874 }
4875 if (i < 0) {
4876 i = 0;
4877 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4878 }
4879
4880 return (u32)i;
4881}
4882
4883/**
4884 * ufshcd_calc_icc_level - calculate the max ICC level
4885 * In case regulators are not initialized we'll return 0
4886 * @hba: per-adapter instance
4887 * @desc_buf: power descriptor buffer to extract ICC levels from.
4888 * @len: length of desc_buff
4889 *
4890 * Returns calculated ICC level
4891 */
4892static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4893 u8 *desc_buf, int len)
4894{
4895 u32 icc_level = 0;
4896
4897 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4898 !hba->vreg_info.vccq2) {
4899 dev_err(hba->dev,
4900 "%s: Regulator capability was not set, actvIccLevel=%d",
4901 __func__, icc_level);
4902 goto out;
4903 }
4904
4905 if (hba->vreg_info.vcc)
4906 icc_level = ufshcd_get_max_icc_level(
4907 hba->vreg_info.vcc->max_uA,
4908 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4909 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4910
4911 if (hba->vreg_info.vccq)
4912 icc_level = ufshcd_get_max_icc_level(
4913 hba->vreg_info.vccq->max_uA,
4914 icc_level,
4915 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4916
4917 if (hba->vreg_info.vccq2)
4918 icc_level = ufshcd_get_max_icc_level(
4919 hba->vreg_info.vccq2->max_uA,
4920 icc_level,
4921 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4922out:
4923 return icc_level;
4924}
4925
4926static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4927{
4928 int ret;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004929 int buff_len = hba->desc_size.pwr_desc;
4930 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004931
4932 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4933 if (ret) {
4934 dev_err(hba->dev,
4935 "%s: Failed reading power descriptor.len = %d ret = %d",
4936 __func__, buff_len, ret);
4937 return;
4938 }
4939
4940 hba->init_prefetch_data.icc_level =
4941 ufshcd_find_max_sup_active_icc_level(hba,
4942 desc_buf, buff_len);
4943 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4944 __func__, hba->init_prefetch_data.icc_level);
4945
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004946 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4947 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4948 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004949
4950 if (ret)
4951 dev_err(hba->dev,
4952 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4953 __func__, hba->init_prefetch_data.icc_level , ret);
4954
4955}
4956
4957/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004958 * ufshcd_scsi_add_wlus - Adds required W-LUs
4959 * @hba: per-adapter instance
4960 *
4961 * UFS device specification requires the UFS devices to support 4 well known
4962 * logical units:
4963 * "REPORT_LUNS" (address: 01h)
4964 * "UFS Device" (address: 50h)
4965 * "RPMB" (address: 44h)
4966 * "BOOT" (address: 30h)
4967 * UFS device's power management needs to be controlled by "POWER CONDITION"
4968 * field of SSU (START STOP UNIT) command. But this "power condition" field
4969 * will take effect only when its sent to "UFS device" well known logical unit
4970 * hence we require the scsi_device instance to represent this logical unit in
4971 * order for the UFS host driver to send the SSU command for power management.
4972
4973 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4974 * Block) LU so user space process can control this LU. User space may also
4975 * want to have access to BOOT LU.
4976
4977 * This function adds scsi device instances for each of all well known LUs
4978 * (except "REPORT LUNS" LU).
4979 *
4980 * Returns zero on success (all required W-LUs are added successfully),
4981 * non-zero error value on failure (if failed to add any of the required W-LU).
4982 */
4983static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4984{
4985 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004986 struct scsi_device *sdev_rpmb;
4987 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004988
4989 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4990 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4991 if (IS_ERR(hba->sdev_ufs_device)) {
4992 ret = PTR_ERR(hba->sdev_ufs_device);
4993 hba->sdev_ufs_device = NULL;
4994 goto out;
4995 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004996 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004997
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004998 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004999 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005000 if (IS_ERR(sdev_boot)) {
5001 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005002 goto remove_sdev_ufs_device;
5003 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005004 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005005
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005006 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005007 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005008 if (IS_ERR(sdev_rpmb)) {
5009 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005010 goto remove_sdev_boot;
5011 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005012 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005013 goto out;
5014
5015remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005016 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005017remove_sdev_ufs_device:
5018 scsi_remove_device(hba->sdev_ufs_device);
5019out:
5020 return ret;
5021}
5022
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005023static int ufs_get_device_desc(struct ufs_hba *hba,
5024 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005025{
5026 int err;
5027 u8 model_index;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005028 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
5029 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005030
Potomski, MichalXe1928452017-02-23 09:05:30 +00005031 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005032 if (err) {
5033 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
5034 __func__, err);
5035 goto out;
5036 }
5037
5038 /*
5039 * getting vendor (manufacturerID) and Bank Index in big endian
5040 * format
5041 */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005042 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005043 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
5044
5045 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
5046
5047 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00005048 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005049 if (err) {
5050 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5051 __func__, err);
5052 goto out;
5053 }
5054
Potomski, MichalXe1928452017-02-23 09:05:30 +00005055 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005056 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005057 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5058 MAX_MODEL_LEN));
5059
5060 /* Null terminate the model string */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005061 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005062
5063out:
5064 return err;
5065}
5066
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005067static void ufs_fixup_device_setup(struct ufs_hba *hba,
5068 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005069{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005070 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005071
5072 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005073 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
5074 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
5075 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005076 !strcmp(f->card.model, UFS_ANY_MODEL)))
5077 hba->dev_quirks |= f->quirk;
5078 }
5079}
5080
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005081/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005082 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5083 * @hba: per-adapter instance
5084 *
5085 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5086 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5087 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5088 * the hibern8 exit latency.
5089 *
5090 * Returns zero on success, non-zero error value on failure.
5091 */
5092static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5093{
5094 int ret = 0;
5095 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5096
5097 ret = ufshcd_dme_peer_get(hba,
5098 UIC_ARG_MIB_SEL(
5099 RX_MIN_ACTIVATETIME_CAPABILITY,
5100 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5101 &peer_rx_min_activatetime);
5102 if (ret)
5103 goto out;
5104
5105 /* make sure proper unit conversion is applied */
5106 tuned_pa_tactivate =
5107 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5108 / PA_TACTIVATE_TIME_UNIT_US);
5109 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5110 tuned_pa_tactivate);
5111
5112out:
5113 return ret;
5114}
5115
5116/**
5117 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5118 * @hba: per-adapter instance
5119 *
5120 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5121 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5122 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5123 * This optimal value can help reduce the hibern8 exit latency.
5124 *
5125 * Returns zero on success, non-zero error value on failure.
5126 */
5127static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5128{
5129 int ret = 0;
5130 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5131 u32 max_hibern8_time, tuned_pa_hibern8time;
5132
5133 ret = ufshcd_dme_get(hba,
5134 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5135 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5136 &local_tx_hibern8_time_cap);
5137 if (ret)
5138 goto out;
5139
5140 ret = ufshcd_dme_peer_get(hba,
5141 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5142 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5143 &peer_rx_hibern8_time_cap);
5144 if (ret)
5145 goto out;
5146
5147 max_hibern8_time = max(local_tx_hibern8_time_cap,
5148 peer_rx_hibern8_time_cap);
5149 /* make sure proper unit conversion is applied */
5150 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5151 / PA_HIBERN8_TIME_UNIT_US);
5152 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5153 tuned_pa_hibern8time);
5154out:
5155 return ret;
5156}
5157
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005158/**
5159 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5160 * less than device PA_TACTIVATE time.
5161 * @hba: per-adapter instance
5162 *
5163 * Some UFS devices require host PA_TACTIVATE to be lower than device
5164 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5165 * for such devices.
5166 *
5167 * Returns zero on success, non-zero error value on failure.
5168 */
5169static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5170{
5171 int ret = 0;
5172 u32 granularity, peer_granularity;
5173 u32 pa_tactivate, peer_pa_tactivate;
5174 u32 pa_tactivate_us, peer_pa_tactivate_us;
5175 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5176
5177 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5178 &granularity);
5179 if (ret)
5180 goto out;
5181
5182 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5183 &peer_granularity);
5184 if (ret)
5185 goto out;
5186
5187 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5188 (granularity > PA_GRANULARITY_MAX_VAL)) {
5189 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5190 __func__, granularity);
5191 return -EINVAL;
5192 }
5193
5194 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5195 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5196 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5197 __func__, peer_granularity);
5198 return -EINVAL;
5199 }
5200
5201 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5202 if (ret)
5203 goto out;
5204
5205 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5206 &peer_pa_tactivate);
5207 if (ret)
5208 goto out;
5209
5210 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5211 peer_pa_tactivate_us = peer_pa_tactivate *
5212 gran_to_us_table[peer_granularity - 1];
5213
5214 if (pa_tactivate_us > peer_pa_tactivate_us) {
5215 u32 new_peer_pa_tactivate;
5216
5217 new_peer_pa_tactivate = pa_tactivate_us /
5218 gran_to_us_table[peer_granularity - 1];
5219 new_peer_pa_tactivate++;
5220 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5221 new_peer_pa_tactivate);
5222 }
5223
5224out:
5225 return ret;
5226}
5227
Yaniv Gardi37113102016-03-10 17:37:16 +02005228static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5229{
5230 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5231 ufshcd_tune_pa_tactivate(hba);
5232 ufshcd_tune_pa_hibern8time(hba);
5233 }
5234
5235 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5236 /* set 1ms timeout for PA_TACTIVATE */
5237 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005238
5239 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5240 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavanibedc6292017-04-04 19:32:13 +00005241
5242 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005243}
5244
Potomski, MichalXe1928452017-02-23 09:05:30 +00005245static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
5246{
5247 int err;
5248
5249 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
5250 &hba->desc_size.dev_desc);
5251 if (err)
5252 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5253
5254 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
5255 &hba->desc_size.pwr_desc);
5256 if (err)
5257 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5258
5259 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
5260 &hba->desc_size.interc_desc);
5261 if (err)
5262 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5263
5264 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
5265 &hba->desc_size.conf_desc);
5266 if (err)
5267 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5268
5269 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
5270 &hba->desc_size.unit_desc);
5271 if (err)
5272 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5273
5274 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
5275 &hba->desc_size.geom_desc);
5276 if (err)
5277 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5278}
5279
5280static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
5281{
5282 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5283 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5284 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5285 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5286 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5287 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5288}
5289
Yaniv Gardi37113102016-03-10 17:37:16 +02005290/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005291 * ufshcd_probe_hba - probe hba to detect device and initialize
5292 * @hba: per-adapter instance
5293 *
5294 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305295 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005296static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305297{
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005298 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305299 int ret;
5300
5301 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305302 if (ret)
5303 goto out;
5304
Yaniv Gardi50646362014-10-23 13:25:13 +03005305 ufshcd_init_pwr_info(hba);
5306
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005307 /* set the default level for urgent bkops */
5308 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5309 hba->is_urgent_bkops_lvl_checked = false;
5310
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005311 /* UniPro link is active now */
5312 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305313
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305314 ret = ufshcd_verify_dev_init(hba);
5315 if (ret)
5316 goto out;
5317
Dolev Raviv68078d52013-07-30 00:35:58 +05305318 ret = ufshcd_complete_dev_init(hba);
5319 if (ret)
5320 goto out;
5321
Potomski, MichalXe1928452017-02-23 09:05:30 +00005322 /* Init check for device descriptor sizes */
5323 ufshcd_init_desc_sizes(hba);
5324
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005325 ret = ufs_get_device_desc(hba, &card);
5326 if (ret) {
5327 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5328 __func__, ret);
5329 goto out;
5330 }
5331
5332 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02005333 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005334
5335 ret = ufshcd_set_vccq_rail_unused(hba,
5336 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5337 if (ret)
5338 goto out;
5339
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005340 /* UFS device is also active now */
5341 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305342 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005343 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305344
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005345 if (ufshcd_get_max_pwr_mode(hba)) {
5346 dev_err(hba->dev,
5347 "%s: Failed getting max supported power mode\n",
5348 __func__);
5349 } else {
5350 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
5351 if (ret)
5352 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5353 __func__, ret);
5354 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005355
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005356 /* set the state as operational after switching to desired gear */
5357 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005358
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005359 /*
5360 * If we are in error handling context or in power management callbacks
5361 * context, no need to scan the host
5362 */
5363 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5364 bool flag;
5365
5366 /* clear any previous UFS device information */
5367 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005368 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5369 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005370 hba->dev_info.f_power_on_wp_en = flag;
5371
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005372 if (!hba->is_init_prefetch)
5373 ufshcd_init_icc_levels(hba);
5374
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005375 /* Add required well known logical units to scsi mid layer */
5376 if (ufshcd_scsi_add_wlus(hba))
5377 goto out;
5378
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305379 scsi_scan_host(hba->host);
5380 pm_runtime_put_sync(hba->dev);
5381 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005382
5383 if (!hba->is_init_prefetch)
5384 hba->is_init_prefetch = true;
5385
Sahitya Tummala856b3482014-09-25 15:32:34 +03005386 /* Resume devfreq after UFS device is detected */
5387 if (ufshcd_is_clkscaling_enabled(hba))
5388 devfreq_resume_device(hba->devfreq);
5389
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305390out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005391 /*
5392 * If we failed to initialize the device or the device is not
5393 * present, turn off the power/clocks etc.
5394 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005395 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5396 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005397 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005398 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005399
5400 return ret;
5401}
5402
5403/**
5404 * ufshcd_async_scan - asynchronous execution for probing hba
5405 * @data: data pointer to pass to this function
5406 * @cookie: cookie data
5407 */
5408static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5409{
5410 struct ufs_hba *hba = (struct ufs_hba *)data;
5411
5412 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305413}
5414
Yaniv Gardif550c652016-03-10 17:37:07 +02005415static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5416{
5417 unsigned long flags;
5418 struct Scsi_Host *host;
5419 struct ufs_hba *hba;
5420 int index;
5421 bool found = false;
5422
5423 if (!scmd || !scmd->device || !scmd->device->host)
5424 return BLK_EH_NOT_HANDLED;
5425
5426 host = scmd->device->host;
5427 hba = shost_priv(host);
5428 if (!hba)
5429 return BLK_EH_NOT_HANDLED;
5430
5431 spin_lock_irqsave(host->host_lock, flags);
5432
5433 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5434 if (hba->lrb[index].cmd == scmd) {
5435 found = true;
5436 break;
5437 }
5438 }
5439
5440 spin_unlock_irqrestore(host->host_lock, flags);
5441
5442 /*
5443 * Bypass SCSI error handling and reset the block layer timer if this
5444 * SCSI command was not actually dispatched to UFS driver, otherwise
5445 * let SCSI layer handle the error as usual.
5446 */
5447 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5448}
5449
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305450static struct scsi_host_template ufshcd_driver_template = {
5451 .module = THIS_MODULE,
5452 .name = UFSHCD,
5453 .proc_name = UFSHCD,
5454 .queuecommand = ufshcd_queuecommand,
5455 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005456 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305457 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005458 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305459 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305460 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5461 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005462 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305463 .this_id = -1,
5464 .sg_tablesize = SG_ALL,
5465 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5466 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005467 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005468 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305469};
5470
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005471static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5472 int ua)
5473{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005474 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005475
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005476 if (!vreg)
5477 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005478
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005479 ret = regulator_set_load(vreg->reg, ua);
5480 if (ret < 0) {
5481 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5482 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005483 }
5484
5485 return ret;
5486}
5487
5488static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5489 struct ufs_vreg *vreg)
5490{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005491 if (!vreg)
5492 return 0;
5493 else if (vreg->unused)
5494 return 0;
5495 else
5496 return ufshcd_config_vreg_load(hba->dev, vreg,
5497 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005498}
5499
5500static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5501 struct ufs_vreg *vreg)
5502{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005503 if (!vreg)
5504 return 0;
5505 else if (vreg->unused)
5506 return 0;
5507 else
5508 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005509}
5510
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005511static int ufshcd_config_vreg(struct device *dev,
5512 struct ufs_vreg *vreg, bool on)
5513{
5514 int ret = 0;
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005515 struct regulator *reg;
5516 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005517 int min_uV, uA_load;
5518
5519 BUG_ON(!vreg);
5520
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005521 reg = vreg->reg;
5522 name = vreg->name;
5523
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005524 if (regulator_count_voltages(reg) > 0) {
5525 min_uV = on ? vreg->min_uV : 0;
5526 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5527 if (ret) {
5528 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5529 __func__, name, ret);
5530 goto out;
5531 }
5532
5533 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005534 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5535 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005536 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005537 }
5538out:
5539 return ret;
5540}
5541
5542static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5543{
5544 int ret = 0;
5545
Yaniv Gardi60f01872016-03-10 17:37:11 +02005546 if (!vreg)
5547 goto out;
5548 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005549 goto out;
5550
5551 ret = ufshcd_config_vreg(dev, vreg, true);
5552 if (!ret)
5553 ret = regulator_enable(vreg->reg);
5554
5555 if (!ret)
5556 vreg->enabled = true;
5557 else
5558 dev_err(dev, "%s: %s enable failed, err=%d\n",
5559 __func__, vreg->name, ret);
5560out:
5561 return ret;
5562}
5563
5564static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5565{
5566 int ret = 0;
5567
Yaniv Gardi60f01872016-03-10 17:37:11 +02005568 if (!vreg)
5569 goto out;
5570 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005571 goto out;
5572
5573 ret = regulator_disable(vreg->reg);
5574
5575 if (!ret) {
5576 /* ignore errors on applying disable config */
5577 ufshcd_config_vreg(dev, vreg, false);
5578 vreg->enabled = false;
5579 } else {
5580 dev_err(dev, "%s: %s disable failed, err=%d\n",
5581 __func__, vreg->name, ret);
5582 }
5583out:
5584 return ret;
5585}
5586
5587static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5588{
5589 int ret = 0;
5590 struct device *dev = hba->dev;
5591 struct ufs_vreg_info *info = &hba->vreg_info;
5592
5593 if (!info)
5594 goto out;
5595
5596 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5597 if (ret)
5598 goto out;
5599
5600 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5601 if (ret)
5602 goto out;
5603
5604 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5605 if (ret)
5606 goto out;
5607
5608out:
5609 if (ret) {
5610 ufshcd_toggle_vreg(dev, info->vccq2, false);
5611 ufshcd_toggle_vreg(dev, info->vccq, false);
5612 ufshcd_toggle_vreg(dev, info->vcc, false);
5613 }
5614 return ret;
5615}
5616
Raviv Shvili6a771a62014-09-25 15:32:24 +03005617static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5618{
5619 struct ufs_vreg_info *info = &hba->vreg_info;
5620
5621 if (info)
5622 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5623
5624 return 0;
5625}
5626
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005627static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5628{
5629 int ret = 0;
5630
5631 if (!vreg)
5632 goto out;
5633
5634 vreg->reg = devm_regulator_get(dev, vreg->name);
5635 if (IS_ERR(vreg->reg)) {
5636 ret = PTR_ERR(vreg->reg);
5637 dev_err(dev, "%s: %s get failed, err=%d\n",
5638 __func__, vreg->name, ret);
5639 }
5640out:
5641 return ret;
5642}
5643
5644static int ufshcd_init_vreg(struct ufs_hba *hba)
5645{
5646 int ret = 0;
5647 struct device *dev = hba->dev;
5648 struct ufs_vreg_info *info = &hba->vreg_info;
5649
5650 if (!info)
5651 goto out;
5652
5653 ret = ufshcd_get_vreg(dev, info->vcc);
5654 if (ret)
5655 goto out;
5656
5657 ret = ufshcd_get_vreg(dev, info->vccq);
5658 if (ret)
5659 goto out;
5660
5661 ret = ufshcd_get_vreg(dev, info->vccq2);
5662out:
5663 return ret;
5664}
5665
Raviv Shvili6a771a62014-09-25 15:32:24 +03005666static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5667{
5668 struct ufs_vreg_info *info = &hba->vreg_info;
5669
5670 if (info)
5671 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5672
5673 return 0;
5674}
5675
Yaniv Gardi60f01872016-03-10 17:37:11 +02005676static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5677{
5678 int ret = 0;
5679 struct ufs_vreg_info *info = &hba->vreg_info;
5680
5681 if (!info)
5682 goto out;
5683 else if (!info->vccq)
5684 goto out;
5685
5686 if (unused) {
5687 /* shut off the rail here */
5688 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5689 /*
5690 * Mark this rail as no longer used, so it doesn't get enabled
5691 * later by mistake
5692 */
5693 if (!ret)
5694 info->vccq->unused = true;
5695 } else {
5696 /*
5697 * rail should have been already enabled hence just make sure
5698 * that unused flag is cleared.
5699 */
5700 info->vccq->unused = false;
5701 }
5702out:
5703 return ret;
5704}
5705
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005706static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5707 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005708{
5709 int ret = 0;
5710 struct ufs_clk_info *clki;
5711 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005712 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005713
5714 if (!head || list_empty(head))
5715 goto out;
5716
5717 list_for_each_entry(clki, head, list) {
5718 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005719 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5720 continue;
5721
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005722 if (on && !clki->enabled) {
5723 ret = clk_prepare_enable(clki->clk);
5724 if (ret) {
5725 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5726 __func__, clki->name, ret);
5727 goto out;
5728 }
5729 } else if (!on && clki->enabled) {
5730 clk_disable_unprepare(clki->clk);
5731 }
5732 clki->enabled = on;
5733 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5734 clki->name, on ? "en" : "dis");
5735 }
5736 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005737
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005738 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005739out:
5740 if (ret) {
5741 list_for_each_entry(clki, head, list) {
5742 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5743 clk_disable_unprepare(clki->clk);
5744 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005745 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005746 spin_lock_irqsave(hba->host->host_lock, flags);
5747 hba->clk_gating.state = CLKS_ON;
5748 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005749 }
5750 return ret;
5751}
5752
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005753static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5754{
5755 return __ufshcd_setup_clocks(hba, on, false);
5756}
5757
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005758static int ufshcd_init_clocks(struct ufs_hba *hba)
5759{
5760 int ret = 0;
5761 struct ufs_clk_info *clki;
5762 struct device *dev = hba->dev;
5763 struct list_head *head = &hba->clk_list_head;
5764
5765 if (!head || list_empty(head))
5766 goto out;
5767
5768 list_for_each_entry(clki, head, list) {
5769 if (!clki->name)
5770 continue;
5771
5772 clki->clk = devm_clk_get(dev, clki->name);
5773 if (IS_ERR(clki->clk)) {
5774 ret = PTR_ERR(clki->clk);
5775 dev_err(dev, "%s: %s clk get failed, %d\n",
5776 __func__, clki->name, ret);
5777 goto out;
5778 }
5779
5780 if (clki->max_freq) {
5781 ret = clk_set_rate(clki->clk, clki->max_freq);
5782 if (ret) {
5783 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5784 __func__, clki->name,
5785 clki->max_freq, ret);
5786 goto out;
5787 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005788 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005789 }
5790 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5791 clki->name, clk_get_rate(clki->clk));
5792 }
5793out:
5794 return ret;
5795}
5796
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005797static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5798{
5799 int err = 0;
5800
5801 if (!hba->vops)
5802 goto out;
5803
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005804 err = ufshcd_vops_init(hba);
5805 if (err)
5806 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005807
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005808 err = ufshcd_vops_setup_regulators(hba, true);
5809 if (err)
5810 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005811
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005812 goto out;
5813
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005814out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005815 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005816out:
5817 if (err)
5818 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005819 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005820 return err;
5821}
5822
5823static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5824{
5825 if (!hba->vops)
5826 return;
5827
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005828 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005829
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005830 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005831
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005832 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005833}
5834
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005835static int ufshcd_hba_init(struct ufs_hba *hba)
5836{
5837 int err;
5838
Raviv Shvili6a771a62014-09-25 15:32:24 +03005839 /*
5840 * Handle host controller power separately from the UFS device power
5841 * rails as it will help controlling the UFS host controller power
5842 * collapse easily which is different than UFS device power collapse.
5843 * Also, enable the host controller power before we go ahead with rest
5844 * of the initialization here.
5845 */
5846 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005847 if (err)
5848 goto out;
5849
Raviv Shvili6a771a62014-09-25 15:32:24 +03005850 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005851 if (err)
5852 goto out;
5853
Raviv Shvili6a771a62014-09-25 15:32:24 +03005854 err = ufshcd_init_clocks(hba);
5855 if (err)
5856 goto out_disable_hba_vreg;
5857
5858 err = ufshcd_setup_clocks(hba, true);
5859 if (err)
5860 goto out_disable_hba_vreg;
5861
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005862 err = ufshcd_init_vreg(hba);
5863 if (err)
5864 goto out_disable_clks;
5865
5866 err = ufshcd_setup_vreg(hba, true);
5867 if (err)
5868 goto out_disable_clks;
5869
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005870 err = ufshcd_variant_hba_init(hba);
5871 if (err)
5872 goto out_disable_vreg;
5873
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005874 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005875 goto out;
5876
5877out_disable_vreg:
5878 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005879out_disable_clks:
5880 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005881out_disable_hba_vreg:
5882 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005883out:
5884 return err;
5885}
5886
5887static void ufshcd_hba_exit(struct ufs_hba *hba)
5888{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005889 if (hba->is_powered) {
5890 ufshcd_variant_hba_exit(hba);
5891 ufshcd_setup_vreg(hba, false);
5892 ufshcd_setup_clocks(hba, false);
5893 ufshcd_setup_hba_vreg(hba, false);
5894 hba->is_powered = false;
5895 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005896}
5897
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005898static int
5899ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305900{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005901 unsigned char cmd[6] = {REQUEST_SENSE,
5902 0,
5903 0,
5904 0,
5905 SCSI_SENSE_BUFFERSIZE,
5906 0};
5907 char *buffer;
5908 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305909
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005910 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5911 if (!buffer) {
5912 ret = -ENOMEM;
5913 goto out;
5914 }
5915
5916 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
5917 SCSI_SENSE_BUFFERSIZE, NULL,
5918 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5919 if (ret)
5920 pr_err("%s: failed with err %d\n", __func__, ret);
5921
5922 kfree(buffer);
5923out:
5924 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305925}
5926
5927/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005928 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5929 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305930 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005931 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305932 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005933 * Returns 0 if requested power mode is set successfully
5934 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305935 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005936static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5937 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305938{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005939 unsigned char cmd[6] = { START_STOP };
5940 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005941 struct scsi_device *sdp;
5942 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005943 int ret;
5944
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005945 spin_lock_irqsave(hba->host->host_lock, flags);
5946 sdp = hba->sdev_ufs_device;
5947 if (sdp) {
5948 ret = scsi_device_get(sdp);
5949 if (!ret && !scsi_device_online(sdp)) {
5950 ret = -ENODEV;
5951 scsi_device_put(sdp);
5952 }
5953 } else {
5954 ret = -ENODEV;
5955 }
5956 spin_unlock_irqrestore(hba->host->host_lock, flags);
5957
5958 if (ret)
5959 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005960
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305961 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005962 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5963 * handling, which would wait for host to be resumed. Since we know
5964 * we are functional while we are here, skip host resume in error
5965 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305966 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005967 hba->host->eh_noresume = 1;
5968 if (hba->wlun_dev_clr_ua) {
5969 ret = ufshcd_send_request_sense(hba, sdp);
5970 if (ret)
5971 goto out;
5972 /* Unit attention condition is cleared now */
5973 hba->wlun_dev_clr_ua = false;
5974 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305975
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005976 cmd[4] = pwr_mode << 4;
5977
5978 /*
5979 * Current function would be generally called from the power management
5980 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5981 * already suspended childs.
5982 */
5983 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5984 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5985 if (ret) {
5986 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005987 "START_STOP failed for power mode: %d, result %x\n",
5988 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005989 if (driver_byte(ret) & DRIVER_SENSE)
5990 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005991 }
5992
5993 if (!ret)
5994 hba->curr_dev_pwr_mode = pwr_mode;
5995out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005996 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005997 hba->host->eh_noresume = 0;
5998 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305999}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306000
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006001static int ufshcd_link_state_transition(struct ufs_hba *hba,
6002 enum uic_link_state req_link_state,
6003 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306004{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006005 int ret = 0;
6006
6007 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306008 return 0;
6009
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006010 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
6011 ret = ufshcd_uic_hibern8_enter(hba);
6012 if (!ret)
6013 ufshcd_set_link_hibern8(hba);
6014 else
6015 goto out;
6016 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306017 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006018 * If autobkops is enabled, link can't be turned off because
6019 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306020 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006021 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
6022 (!check_for_bkops || (check_for_bkops &&
6023 !hba->auto_bkops_enabled))) {
6024 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02006025 * Let's make sure that link is in low power mode, we are doing
6026 * this currently by putting the link in Hibern8. Otherway to
6027 * put the link in low power mode is to send the DME end point
6028 * to device and then send the DME reset command to local
6029 * unipro. But putting the link in hibern8 is much faster.
6030 */
6031 ret = ufshcd_uic_hibern8_enter(hba);
6032 if (ret)
6033 goto out;
6034 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006035 * Change controller state to "reset state" which
6036 * should also put the link in off/reset state
6037 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02006038 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006039 /*
6040 * TODO: Check if we need any delay to make sure that
6041 * controller is reset
6042 */
6043 ufshcd_set_link_off(hba);
6044 }
6045
6046out:
6047 return ret;
6048}
6049
6050static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6051{
6052 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006053 * It seems some UFS devices may keep drawing more than sleep current
6054 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6055 * To avoid this situation, add 2ms delay before putting these UFS
6056 * rails in LPM mode.
6057 */
6058 if (!ufshcd_is_link_active(hba) &&
6059 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6060 usleep_range(2000, 2100);
6061
6062 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006063 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6064 * power.
6065 *
6066 * If UFS device and link is in OFF state, all power supplies (VCC,
6067 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6068 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6069 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6070 *
6071 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6072 * in low power state which would save some power.
6073 */
6074 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6075 !hba->dev_info.is_lu_power_on_wp) {
6076 ufshcd_setup_vreg(hba, false);
6077 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6078 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6079 if (!ufshcd_is_link_active(hba)) {
6080 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6081 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6082 }
6083 }
6084}
6085
6086static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6087{
6088 int ret = 0;
6089
6090 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6091 !hba->dev_info.is_lu_power_on_wp) {
6092 ret = ufshcd_setup_vreg(hba, true);
6093 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6094 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
6095 if (!ret && !ufshcd_is_link_active(hba)) {
6096 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6097 if (ret)
6098 goto vcc_disable;
6099 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6100 if (ret)
6101 goto vccq_lpm;
6102 }
6103 }
6104 goto out;
6105
6106vccq_lpm:
6107 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6108vcc_disable:
6109 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6110out:
6111 return ret;
6112}
6113
6114static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6115{
6116 if (ufshcd_is_link_off(hba))
6117 ufshcd_setup_hba_vreg(hba, false);
6118}
6119
6120static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6121{
6122 if (ufshcd_is_link_off(hba))
6123 ufshcd_setup_hba_vreg(hba, true);
6124}
6125
6126/**
6127 * ufshcd_suspend - helper function for suspend operations
6128 * @hba: per adapter instance
6129 * @pm_op: desired low power operation type
6130 *
6131 * This function will try to put the UFS device and link into low power
6132 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6133 * (System PM level).
6134 *
6135 * If this function is called during shutdown, it will make sure that
6136 * both UFS device and UFS link is powered off.
6137 *
6138 * NOTE: UFS device & link must be active before we enter in this function.
6139 *
6140 * Returns 0 for success and non-zero for failure
6141 */
6142static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6143{
6144 int ret = 0;
6145 enum ufs_pm_level pm_lvl;
6146 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6147 enum uic_link_state req_link_state;
6148
6149 hba->pm_op_in_progress = 1;
6150 if (!ufshcd_is_shutdown_pm(pm_op)) {
6151 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6152 hba->rpm_lvl : hba->spm_lvl;
6153 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6154 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6155 } else {
6156 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6157 req_link_state = UIC_LINK_OFF_STATE;
6158 }
6159
6160 /*
6161 * If we can't transition into any of the low power modes
6162 * just gate the clocks.
6163 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006164 ufshcd_hold(hba, false);
6165 hba->clk_gating.is_suspended = true;
6166
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006167 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6168 req_link_state == UIC_LINK_ACTIVE_STATE) {
6169 goto disable_clks;
6170 }
6171
6172 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6173 (req_link_state == hba->uic_link_state))
6174 goto out;
6175
6176 /* UFS device & link must be active before we enter in this function */
6177 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6178 ret = -EINVAL;
6179 goto out;
6180 }
6181
6182 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006183 if (ufshcd_can_autobkops_during_suspend(hba)) {
6184 /*
6185 * The device is idle with no requests in the queue,
6186 * allow background operations if bkops status shows
6187 * that performance might be impacted.
6188 */
6189 ret = ufshcd_urgent_bkops(hba);
6190 if (ret)
6191 goto enable_gating;
6192 } else {
6193 /* make sure that auto bkops is disabled */
6194 ufshcd_disable_auto_bkops(hba);
6195 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006196 }
6197
6198 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6199 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6200 !ufshcd_is_runtime_pm(pm_op))) {
6201 /* ensure that bkops is disabled */
6202 ufshcd_disable_auto_bkops(hba);
6203 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6204 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006205 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006206 }
6207
6208 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6209 if (ret)
6210 goto set_dev_active;
6211
6212 ufshcd_vreg_set_lpm(hba);
6213
6214disable_clks:
6215 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03006216 * The clock scaling needs access to controller registers. Hence, Wait
6217 * for pending clock scaling work to be done before clocks are
6218 * turned off.
6219 */
6220 if (ufshcd_is_clkscaling_enabled(hba)) {
6221 devfreq_suspend_device(hba->devfreq);
6222 hba->clk_scaling.window_start_t = 0;
6223 }
6224 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006225 * Call vendor specific suspend callback. As these callbacks may access
6226 * vendor specific host controller register space call them before the
6227 * host clocks are ON.
6228 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006229 ret = ufshcd_vops_suspend(hba, pm_op);
6230 if (ret)
6231 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006232
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006233 ret = ufshcd_vops_setup_clocks(hba, false);
6234 if (ret)
6235 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006236
6237 if (!ufshcd_is_link_active(hba))
6238 ufshcd_setup_clocks(hba, false);
6239 else
6240 /* If link is active, device ref_clk can't be switched off */
6241 __ufshcd_setup_clocks(hba, false, true);
6242
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006243 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006244 /*
6245 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006246 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006247 */
6248 ufshcd_disable_irq(hba);
6249 /* Put the host controller in low power mode if possible */
6250 ufshcd_hba_vreg_set_lpm(hba);
6251 goto out;
6252
6253vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006254 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006255set_link_active:
6256 ufshcd_vreg_set_hpm(hba);
6257 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6258 ufshcd_set_link_active(hba);
6259 else if (ufshcd_is_link_off(hba))
6260 ufshcd_host_reset_and_restore(hba);
6261set_dev_active:
6262 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6263 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006264enable_gating:
6265 hba->clk_gating.is_suspended = false;
6266 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006267out:
6268 hba->pm_op_in_progress = 0;
6269 return ret;
6270}
6271
6272/**
6273 * ufshcd_resume - helper function for resume operations
6274 * @hba: per adapter instance
6275 * @pm_op: runtime PM or system PM
6276 *
6277 * This function basically brings the UFS device, UniPro link and controller
6278 * to active state.
6279 *
6280 * Returns 0 for success and non-zero for failure
6281 */
6282static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6283{
6284 int ret;
6285 enum uic_link_state old_link_state;
6286
6287 hba->pm_op_in_progress = 1;
6288 old_link_state = hba->uic_link_state;
6289
6290 ufshcd_hba_vreg_set_hpm(hba);
6291 /* Make sure clocks are enabled before accessing controller */
6292 ret = ufshcd_setup_clocks(hba, true);
6293 if (ret)
6294 goto out;
6295
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006296 /* enable the host irq as host controller would be active soon */
6297 ret = ufshcd_enable_irq(hba);
6298 if (ret)
6299 goto disable_irq_and_vops_clks;
6300
6301 ret = ufshcd_vreg_set_hpm(hba);
6302 if (ret)
6303 goto disable_irq_and_vops_clks;
6304
6305 /*
6306 * Call vendor specific resume callback. As these callbacks may access
6307 * vendor specific host controller register space call them when the
6308 * host clocks are ON.
6309 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006310 ret = ufshcd_vops_resume(hba, pm_op);
6311 if (ret)
6312 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006313
6314 if (ufshcd_is_link_hibern8(hba)) {
6315 ret = ufshcd_uic_hibern8_exit(hba);
6316 if (!ret)
6317 ufshcd_set_link_active(hba);
6318 else
6319 goto vendor_suspend;
6320 } else if (ufshcd_is_link_off(hba)) {
6321 ret = ufshcd_host_reset_and_restore(hba);
6322 /*
6323 * ufshcd_host_reset_and_restore() should have already
6324 * set the link state as active
6325 */
6326 if (ret || !ufshcd_is_link_active(hba))
6327 goto vendor_suspend;
6328 }
6329
6330 if (!ufshcd_is_ufs_dev_active(hba)) {
6331 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6332 if (ret)
6333 goto set_old_link_state;
6334 }
6335
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08006336 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
6337 ufshcd_enable_auto_bkops(hba);
6338 else
6339 /*
6340 * If BKOPs operations are urgently needed at this moment then
6341 * keep auto-bkops enabled or else disable it.
6342 */
6343 ufshcd_urgent_bkops(hba);
6344
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006345 hba->clk_gating.is_suspended = false;
6346
Sahitya Tummala856b3482014-09-25 15:32:34 +03006347 if (ufshcd_is_clkscaling_enabled(hba))
6348 devfreq_resume_device(hba->devfreq);
6349
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006350 /* Schedule clock gating in case of no access to UFS device yet */
6351 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006352 goto out;
6353
6354set_old_link_state:
6355 ufshcd_link_state_transition(hba, old_link_state, 0);
6356vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006357 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006358disable_vreg:
6359 ufshcd_vreg_set_lpm(hba);
6360disable_irq_and_vops_clks:
6361 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006362 ufshcd_setup_clocks(hba, false);
6363out:
6364 hba->pm_op_in_progress = 0;
6365 return ret;
6366}
6367
6368/**
6369 * ufshcd_system_suspend - system suspend routine
6370 * @hba: per adapter instance
6371 * @pm_op: runtime PM or system PM
6372 *
6373 * Check the description of ufshcd_suspend() function for more details.
6374 *
6375 * Returns 0 for success and non-zero for failure
6376 */
6377int ufshcd_system_suspend(struct ufs_hba *hba)
6378{
6379 int ret = 0;
6380
6381 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006382 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006383
6384 if (pm_runtime_suspended(hba->dev)) {
6385 if (hba->rpm_lvl == hba->spm_lvl)
6386 /*
6387 * There is possibility that device may still be in
6388 * active state during the runtime suspend.
6389 */
6390 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6391 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6392 goto out;
6393
6394 /*
6395 * UFS device and/or UFS link low power states during runtime
6396 * suspend seems to be different than what is expected during
6397 * system suspend. Hence runtime resume the devic & link and
6398 * let the system suspend low power states to take effect.
6399 * TODO: If resume takes longer time, we might have optimize
6400 * it in future by not resuming everything if possible.
6401 */
6402 ret = ufshcd_runtime_resume(hba);
6403 if (ret)
6404 goto out;
6405 }
6406
6407 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6408out:
Dolev Ravive7850602014-09-25 15:32:36 +03006409 if (!ret)
6410 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006411 return ret;
6412}
6413EXPORT_SYMBOL(ufshcd_system_suspend);
6414
6415/**
6416 * ufshcd_system_resume - system resume routine
6417 * @hba: per adapter instance
6418 *
6419 * Returns 0 for success and non-zero for failure
6420 */
6421
6422int ufshcd_system_resume(struct ufs_hba *hba)
6423{
Yaniv Gardi675d9192016-10-17 17:09:24 -07006424 if (!hba)
6425 return -EINVAL;
6426
6427 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006428 /*
6429 * Let the runtime resume take care of resuming
6430 * if runtime suspended.
6431 */
6432 return 0;
6433
6434 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6435}
6436EXPORT_SYMBOL(ufshcd_system_resume);
6437
6438/**
6439 * ufshcd_runtime_suspend - runtime suspend routine
6440 * @hba: per adapter instance
6441 *
6442 * Check the description of ufshcd_suspend() function for more details.
6443 *
6444 * Returns 0 for success and non-zero for failure
6445 */
6446int ufshcd_runtime_suspend(struct ufs_hba *hba)
6447{
Yaniv Gardi675d9192016-10-17 17:09:24 -07006448 if (!hba)
6449 return -EINVAL;
6450
6451 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006452 return 0;
6453
6454 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306455}
6456EXPORT_SYMBOL(ufshcd_runtime_suspend);
6457
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006458/**
6459 * ufshcd_runtime_resume - runtime resume routine
6460 * @hba: per adapter instance
6461 *
6462 * This function basically brings the UFS device, UniPro link and controller
6463 * to active state. Following operations are done in this function:
6464 *
6465 * 1. Turn on all the controller related clocks
6466 * 2. Bring the UniPro link out of Hibernate state
6467 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6468 * to active state.
6469 * 4. If auto-bkops is enabled on the device, disable it.
6470 *
6471 * So following would be the possible power state after this function return
6472 * successfully:
6473 * S1: UFS device in Active state with VCC rail ON
6474 * UniPro link in Active state
6475 * All the UFS/UniPro controller clocks are ON
6476 *
6477 * Returns 0 for success and non-zero for failure
6478 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306479int ufshcd_runtime_resume(struct ufs_hba *hba)
6480{
Yaniv Gardi675d9192016-10-17 17:09:24 -07006481 if (!hba)
6482 return -EINVAL;
6483
6484 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306485 return 0;
Yaniv Gardi675d9192016-10-17 17:09:24 -07006486
6487 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306488}
6489EXPORT_SYMBOL(ufshcd_runtime_resume);
6490
6491int ufshcd_runtime_idle(struct ufs_hba *hba)
6492{
6493 return 0;
6494}
6495EXPORT_SYMBOL(ufshcd_runtime_idle);
6496
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306497/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006498 * ufshcd_shutdown - shutdown routine
6499 * @hba: per adapter instance
6500 *
6501 * This function would power off both UFS device and UFS link.
6502 *
6503 * Returns 0 always to allow force shutdown even in case of errors.
6504 */
6505int ufshcd_shutdown(struct ufs_hba *hba)
6506{
6507 int ret = 0;
6508
6509 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6510 goto out;
6511
6512 if (pm_runtime_suspended(hba->dev)) {
6513 ret = ufshcd_runtime_resume(hba);
6514 if (ret)
6515 goto out;
6516 }
6517
6518 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6519out:
6520 if (ret)
6521 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6522 /* allow force shutdown even in case of errors */
6523 return 0;
6524}
6525EXPORT_SYMBOL(ufshcd_shutdown);
6526
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006527/*
6528 * Values permitted 0, 1, 2.
6529 * 0 -> Disable IO latency histograms (default)
6530 * 1 -> Enable IO latency histograms
6531 * 2 -> Zero out IO latency histograms
6532 */
6533static ssize_t
6534latency_hist_store(struct device *dev, struct device_attribute *attr,
6535 const char *buf, size_t count)
6536{
6537 struct ufs_hba *hba = dev_get_drvdata(dev);
6538 long value;
6539
6540 if (kstrtol(buf, 0, &value))
6541 return -EINVAL;
Hyojun Kim11537d52017-12-21 09:57:41 -08006542 if (value == BLK_IO_LAT_HIST_ZERO) {
6543 memset(&hba->io_lat_read, 0, sizeof(hba->io_lat_read));
6544 memset(&hba->io_lat_write, 0, sizeof(hba->io_lat_write));
6545 } else if (value == BLK_IO_LAT_HIST_ENABLE ||
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006546 value == BLK_IO_LAT_HIST_DISABLE)
6547 hba->latency_hist_enabled = value;
6548 return count;
6549}
6550
6551ssize_t
6552latency_hist_show(struct device *dev, struct device_attribute *attr,
6553 char *buf)
6554{
6555 struct ufs_hba *hba = dev_get_drvdata(dev);
Hyojun Kim11537d52017-12-21 09:57:41 -08006556 size_t written_bytes;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006557
Hyojun Kim11537d52017-12-21 09:57:41 -08006558 written_bytes = blk_latency_hist_show("Read", &hba->io_lat_read,
6559 buf, PAGE_SIZE);
6560 written_bytes += blk_latency_hist_show("Write", &hba->io_lat_write,
6561 buf + written_bytes, PAGE_SIZE - written_bytes);
6562
6563 return written_bytes;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006564}
6565
6566static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR,
6567 latency_hist_show, latency_hist_store);
6568
6569static void
6570ufshcd_init_latency_hist(struct ufs_hba *hba)
6571{
6572 if (device_create_file(hba->dev, &dev_attr_latency_hist))
6573 dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n");
6574}
6575
6576static void
6577ufshcd_exit_latency_hist(struct ufs_hba *hba)
6578{
6579 device_create_file(hba->dev, &dev_attr_latency_hist);
6580}
6581
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006582/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306583 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306584 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306585 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306586 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306587void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306588{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306589 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306590 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306591 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006592 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306593
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006594 ufshcd_exit_clk_gating(hba);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006595 ufshcd_exit_latency_hist(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006596 if (ufshcd_is_clkscaling_enabled(hba))
6597 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006598 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306599}
6600EXPORT_SYMBOL_GPL(ufshcd_remove);
6601
6602/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006603 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6604 * @hba: pointer to Host Bus Adapter (HBA)
6605 */
6606void ufshcd_dealloc_host(struct ufs_hba *hba)
6607{
6608 scsi_host_put(hba->host);
6609}
6610EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6611
6612/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006613 * ufshcd_set_dma_mask - Set dma mask based on the controller
6614 * addressing capability
6615 * @hba: per adapter instance
6616 *
6617 * Returns 0 for success, non-zero for failure
6618 */
6619static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6620{
6621 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6622 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6623 return 0;
6624 }
6625 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6626}
6627
6628/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006629 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306630 * @dev: pointer to device handle
6631 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306632 * Returns 0 on success, non-zero value on failure
6633 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006634int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306635{
6636 struct Scsi_Host *host;
6637 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006638 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306639
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306640 if (!dev) {
6641 dev_err(dev,
6642 "Invalid memory reference for dev is NULL\n");
6643 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306644 goto out_error;
6645 }
6646
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306647 host = scsi_host_alloc(&ufshcd_driver_template,
6648 sizeof(struct ufs_hba));
6649 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306650 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306651 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306652 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306653 }
6654 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306655 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306656 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006657 *hba_handle = hba;
6658
6659out_error:
6660 return err;
6661}
6662EXPORT_SYMBOL(ufshcd_alloc_host);
6663
Sahitya Tummala856b3482014-09-25 15:32:34 +03006664static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6665{
6666 int ret = 0;
6667 struct ufs_clk_info *clki;
6668 struct list_head *head = &hba->clk_list_head;
6669
6670 if (!head || list_empty(head))
6671 goto out;
6672
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006673 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6674 if (ret)
6675 return ret;
6676
Sahitya Tummala856b3482014-09-25 15:32:34 +03006677 list_for_each_entry(clki, head, list) {
6678 if (!IS_ERR_OR_NULL(clki->clk)) {
6679 if (scale_up && clki->max_freq) {
6680 if (clki->curr_freq == clki->max_freq)
6681 continue;
6682 ret = clk_set_rate(clki->clk, clki->max_freq);
6683 if (ret) {
6684 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6685 __func__, clki->name,
6686 clki->max_freq, ret);
6687 break;
6688 }
6689 clki->curr_freq = clki->max_freq;
6690
6691 } else if (!scale_up && clki->min_freq) {
6692 if (clki->curr_freq == clki->min_freq)
6693 continue;
6694 ret = clk_set_rate(clki->clk, clki->min_freq);
6695 if (ret) {
6696 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6697 __func__, clki->name,
6698 clki->min_freq, ret);
6699 break;
6700 }
6701 clki->curr_freq = clki->min_freq;
6702 }
6703 }
6704 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6705 clki->name, clk_get_rate(clki->clk));
6706 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006707
6708 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6709
Sahitya Tummala856b3482014-09-25 15:32:34 +03006710out:
6711 return ret;
6712}
6713
6714static int ufshcd_devfreq_target(struct device *dev,
6715 unsigned long *freq, u32 flags)
6716{
6717 int err = 0;
6718 struct ufs_hba *hba = dev_get_drvdata(dev);
Subhash Jadavani99a3bd32016-10-27 17:25:47 -07006719 bool release_clk_hold = false;
6720 unsigned long irq_flags;
Sahitya Tummala856b3482014-09-25 15:32:34 +03006721
6722 if (!ufshcd_is_clkscaling_enabled(hba))
6723 return -EINVAL;
6724
Subhash Jadavani99a3bd32016-10-27 17:25:47 -07006725 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6726 if (ufshcd_eh_in_progress(hba)) {
6727 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6728 return 0;
6729 }
6730
6731 if (ufshcd_is_clkgating_allowed(hba) &&
6732 (hba->clk_gating.state != CLKS_ON)) {
6733 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
6734 /* hold the vote until the scaling work is completed */
6735 hba->clk_gating.active_reqs++;
6736 release_clk_hold = true;
6737 hba->clk_gating.state = CLKS_ON;
6738 } else {
6739 /*
6740 * Clock gating work seems to be running in parallel
6741 * hence skip scaling work to avoid deadlock between
6742 * current scaling work and gating work.
6743 */
6744 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6745 return 0;
6746 }
6747 }
6748 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6749
Sahitya Tummala856b3482014-09-25 15:32:34 +03006750 if (*freq == UINT_MAX)
6751 err = ufshcd_scale_clks(hba, true);
6752 else if (*freq == 0)
6753 err = ufshcd_scale_clks(hba, false);
6754
Subhash Jadavani99a3bd32016-10-27 17:25:47 -07006755 spin_lock_irqsave(hba->host->host_lock, irq_flags);
6756 if (release_clk_hold)
6757 __ufshcd_release(hba);
6758 spin_unlock_irqrestore(hba->host->host_lock, irq_flags);
6759
Sahitya Tummala856b3482014-09-25 15:32:34 +03006760 return err;
6761}
6762
6763static int ufshcd_devfreq_get_dev_status(struct device *dev,
6764 struct devfreq_dev_status *stat)
6765{
6766 struct ufs_hba *hba = dev_get_drvdata(dev);
6767 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6768 unsigned long flags;
6769
6770 if (!ufshcd_is_clkscaling_enabled(hba))
6771 return -EINVAL;
6772
6773 memset(stat, 0, sizeof(*stat));
6774
6775 spin_lock_irqsave(hba->host->host_lock, flags);
6776 if (!scaling->window_start_t)
6777 goto start_window;
6778
6779 if (scaling->is_busy_started)
6780 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6781 scaling->busy_start_t));
6782
6783 stat->total_time = jiffies_to_usecs((long)jiffies -
6784 (long)scaling->window_start_t);
6785 stat->busy_time = scaling->tot_busy_t;
6786start_window:
6787 scaling->window_start_t = jiffies;
6788 scaling->tot_busy_t = 0;
6789
6790 if (hba->outstanding_reqs) {
6791 scaling->busy_start_t = ktime_get();
6792 scaling->is_busy_started = true;
6793 } else {
6794 scaling->busy_start_t = ktime_set(0, 0);
6795 scaling->is_busy_started = false;
6796 }
6797 spin_unlock_irqrestore(hba->host->host_lock, flags);
6798 return 0;
6799}
6800
6801static struct devfreq_dev_profile ufs_devfreq_profile = {
6802 .polling_ms = 100,
6803 .target = ufshcd_devfreq_target,
6804 .get_dev_status = ufshcd_devfreq_get_dev_status,
6805};
6806
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006807/**
6808 * ufshcd_init - Driver initialization routine
6809 * @hba: per-adapter instance
6810 * @mmio_base: base register address
6811 * @irq: Interrupt line of device
6812 * Returns 0 on success, non-zero value on failure
6813 */
6814int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6815{
6816 int err;
6817 struct Scsi_Host *host = hba->host;
6818 struct device *dev = hba->dev;
6819
6820 if (!mmio_base) {
6821 dev_err(hba->dev,
6822 "Invalid memory reference for mmio_base is NULL\n");
6823 err = -ENODEV;
6824 goto out_error;
6825 }
6826
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306827 hba->mmio_base = mmio_base;
6828 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306829
Potomski, MichalXe1928452017-02-23 09:05:30 +00006830 /* Set descriptor lengths to specification defaults */
6831 ufshcd_def_desc_sizes(hba);
6832
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006833 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006834 if (err)
6835 goto out_error;
6836
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306837 /* Read capabilities registers */
6838 ufshcd_hba_capabilities(hba);
6839
6840 /* Get UFS version supported by the controller */
6841 hba->ufs_version = ufshcd_get_ufs_version(hba);
6842
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306843 /* Get Interrupt bit mask per version */
6844 hba->intr_mask = ufshcd_get_intr_mask(hba);
6845
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006846 err = ufshcd_set_dma_mask(hba);
6847 if (err) {
6848 dev_err(hba->dev, "set dma mask failed\n");
6849 goto out_disable;
6850 }
6851
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306852 /* Allocate memory for host memory space */
6853 err = ufshcd_memory_alloc(hba);
6854 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306855 dev_err(hba->dev, "Memory allocation failed\n");
6856 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306857 }
6858
6859 /* Configure LRB */
6860 ufshcd_host_memory_configure(hba);
6861
6862 host->can_queue = hba->nutrs;
6863 host->cmd_per_lun = hba->nutrs;
6864 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006865 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306866 host->max_channel = UFSHCD_MAX_CHANNEL;
6867 host->unique_id = host->host_no;
6868 host->max_cmd_len = MAX_CDB_SIZE;
6869
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006870 hba->max_pwr_info.is_valid = false;
6871
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306872 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306873 init_waitqueue_head(&hba->tm_wq);
6874 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306875
6876 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306877 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306878 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306879
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306880 /* Initialize UIC command mutex */
6881 mutex_init(&hba->uic_cmd_mutex);
6882
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306883 /* Initialize mutex for device management commands */
6884 mutex_init(&hba->dev_cmd.lock);
6885
6886 /* Initialize device management tag acquire wait queue */
6887 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6888
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006889 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006890
6891 /*
6892 * In order to avoid any spurious interrupt immediately after
6893 * registering UFS controller interrupt handler, clear any pending UFS
6894 * interrupt status and disable all the UFS interrupts.
6895 */
6896 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6897 REG_INTERRUPT_STATUS);
6898 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6899 /*
6900 * Make sure that UFS interrupts are disabled and any pending interrupt
6901 * status is cleared before registering UFS interrupt handler.
6902 */
6903 mb();
6904
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306905 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006906 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306907 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306908 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006909 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006910 } else {
6911 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306912 }
6913
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306914 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306915 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306916 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006917 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306918 }
6919
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306920 /* Host controller enable */
6921 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306922 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306923 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306924 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306925 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306926
Sahitya Tummala856b3482014-09-25 15:32:34 +03006927 if (ufshcd_is_clkscaling_enabled(hba)) {
6928 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6929 "simple_ondemand", NULL);
6930 if (IS_ERR(hba->devfreq)) {
6931 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6932 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006933 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006934 goto out_remove_scsi_host;
6935 }
6936 /* Suspend devfreq until the UFS device is detected */
6937 devfreq_suspend_device(hba->devfreq);
6938 hba->clk_scaling.window_start_t = 0;
6939 }
6940
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306941 /* Hold auto suspend until async scan completes */
6942 pm_runtime_get_sync(dev);
6943
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006944 ufshcd_init_latency_hist(hba);
6945
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006946 /*
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006947 * We are assuming that device wasn't put in sleep/power-down
6948 * state exclusively during the boot stage before kernel.
6949 * This assumption helps avoid doing link startup twice during
6950 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006951 */
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006952 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006953
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306954 async_schedule(ufshcd_async_scan, hba);
6955
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306956 return 0;
6957
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306958out_remove_scsi_host:
6959 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006960exit_gating:
6961 ufshcd_exit_clk_gating(hba);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006962 ufshcd_exit_latency_hist(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306963out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006964 hba->is_irq_enabled = false;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006965 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306966out_error:
6967 return err;
6968}
6969EXPORT_SYMBOL_GPL(ufshcd_init);
6970
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306971MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6972MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306973MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306974MODULE_LICENSE("GPL");
6975MODULE_VERSION(UFSHCD_DRIVER_VERSION);