blob: d0f6f2c51baba5c5f3a3757e1241b94e4806672d [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:
676 break;
677 case REQ_CLKS_OFF:
678 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
679 hba->clk_gating.state = CLKS_ON;
680 break;
681 }
682 /*
683 * If we here, it means gating work is either done or
684 * currently running. Hence, fall through to cancel gating
685 * work and to enable clocks.
686 */
687 case CLKS_OFF:
688 scsi_block_requests(hba->host);
689 hba->clk_gating.state = REQ_CLKS_ON;
690 schedule_work(&hba->clk_gating.ungate_work);
691 /*
692 * fall through to check if we should wait for this
693 * work to be done or not.
694 */
695 case REQ_CLKS_ON:
696 if (async) {
697 rc = -EAGAIN;
698 hba->clk_gating.active_reqs--;
699 break;
700 }
701
702 spin_unlock_irqrestore(hba->host->host_lock, flags);
703 flush_work(&hba->clk_gating.ungate_work);
704 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300705 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300706 goto start;
707 default:
708 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
709 __func__, hba->clk_gating.state);
710 break;
711 }
712 spin_unlock_irqrestore(hba->host->host_lock, flags);
713out:
714 return rc;
715}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200716EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300717
718static void ufshcd_gate_work(struct work_struct *work)
719{
720 struct ufs_hba *hba = container_of(work, struct ufs_hba,
721 clk_gating.gate_work.work);
722 unsigned long flags;
723
724 spin_lock_irqsave(hba->host->host_lock, flags);
725 if (hba->clk_gating.is_suspended) {
726 hba->clk_gating.state = CLKS_ON;
727 goto rel_lock;
728 }
729
730 if (hba->clk_gating.active_reqs
731 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
732 || hba->lrb_in_use || hba->outstanding_tasks
733 || hba->active_uic_cmd || hba->uic_async_done)
734 goto rel_lock;
735
736 spin_unlock_irqrestore(hba->host->host_lock, flags);
737
738 /* put the link into hibern8 mode before turning off clocks */
739 if (ufshcd_can_hibern8_during_gating(hba)) {
740 if (ufshcd_uic_hibern8_enter(hba)) {
741 hba->clk_gating.state = CLKS_ON;
742 goto out;
743 }
744 ufshcd_set_link_hibern8(hba);
745 }
746
Sahitya Tummala856b3482014-09-25 15:32:34 +0300747 if (ufshcd_is_clkscaling_enabled(hba)) {
748 devfreq_suspend_device(hba->devfreq);
749 hba->clk_scaling.window_start_t = 0;
750 }
751
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300752 if (!ufshcd_is_link_active(hba))
753 ufshcd_setup_clocks(hba, false);
754 else
755 /* If link is active, device ref_clk can't be switched off */
756 __ufshcd_setup_clocks(hba, false, true);
757
758 /*
759 * In case you are here to cancel this work the gating state
760 * would be marked as REQ_CLKS_ON. In this case keep the state
761 * as REQ_CLKS_ON which would anyway imply that clocks are off
762 * and a request to turn them on is pending. By doing this way,
763 * we keep the state machine in tact and this would ultimately
764 * prevent from doing cancel work multiple times when there are
765 * new requests arriving before the current cancel work is done.
766 */
767 spin_lock_irqsave(hba->host->host_lock, flags);
768 if (hba->clk_gating.state == REQ_CLKS_OFF)
769 hba->clk_gating.state = CLKS_OFF;
770
771rel_lock:
772 spin_unlock_irqrestore(hba->host->host_lock, flags);
773out:
774 return;
775}
776
777/* host lock must be held before calling this variant */
778static void __ufshcd_release(struct ufs_hba *hba)
779{
780 if (!ufshcd_is_clkgating_allowed(hba))
781 return;
782
783 hba->clk_gating.active_reqs--;
784
785 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
786 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
787 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200788 || hba->active_uic_cmd || hba->uic_async_done
789 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300790 return;
791
792 hba->clk_gating.state = REQ_CLKS_OFF;
793 schedule_delayed_work(&hba->clk_gating.gate_work,
794 msecs_to_jiffies(hba->clk_gating.delay_ms));
795}
796
797void ufshcd_release(struct ufs_hba *hba)
798{
799 unsigned long flags;
800
801 spin_lock_irqsave(hba->host->host_lock, flags);
802 __ufshcd_release(hba);
803 spin_unlock_irqrestore(hba->host->host_lock, flags);
804}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200805EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300806
807static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
808 struct device_attribute *attr, char *buf)
809{
810 struct ufs_hba *hba = dev_get_drvdata(dev);
811
812 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
813}
814
815static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
816 struct device_attribute *attr, const char *buf, size_t count)
817{
818 struct ufs_hba *hba = dev_get_drvdata(dev);
819 unsigned long flags, value;
820
821 if (kstrtoul(buf, 0, &value))
822 return -EINVAL;
823
824 spin_lock_irqsave(hba->host->host_lock, flags);
825 hba->clk_gating.delay_ms = value;
826 spin_unlock_irqrestore(hba->host->host_lock, flags);
827 return count;
828}
829
830static void ufshcd_init_clk_gating(struct ufs_hba *hba)
831{
832 if (!ufshcd_is_clkgating_allowed(hba))
833 return;
834
835 hba->clk_gating.delay_ms = 150;
836 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
837 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
838
839 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
840 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
841 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
842 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
843 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
844 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
845 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
846}
847
848static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
849{
850 if (!ufshcd_is_clkgating_allowed(hba))
851 return;
852 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900853 cancel_work_sync(&hba->clk_gating.ungate_work);
854 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300855}
856
Sahitya Tummala856b3482014-09-25 15:32:34 +0300857/* Must be called with host lock acquired */
858static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
859{
860 if (!ufshcd_is_clkscaling_enabled(hba))
861 return;
862
863 if (!hba->clk_scaling.is_busy_started) {
864 hba->clk_scaling.busy_start_t = ktime_get();
865 hba->clk_scaling.is_busy_started = true;
866 }
867}
868
869static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
870{
871 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
872
873 if (!ufshcd_is_clkscaling_enabled(hba))
874 return;
875
876 if (!hba->outstanding_reqs && scaling->is_busy_started) {
877 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
878 scaling->busy_start_t));
879 scaling->busy_start_t = ktime_set(0, 0);
880 scaling->is_busy_started = false;
881 }
882}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530883/**
884 * ufshcd_send_command - Send SCSI or device management commands
885 * @hba: per adapter instance
886 * @task_tag: Task tag of the command
887 */
888static inline
889void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
890{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300891 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530892 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530893 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530894}
895
896/**
897 * ufshcd_copy_sense_data - Copy sense data in case of check condition
898 * @lrb - pointer to local reference block
899 */
900static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
901{
902 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530903 if (lrbp->sense_buffer &&
904 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530905 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530906 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530907 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530908 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
909 }
910}
911
912/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530913 * ufshcd_copy_query_response() - Copy the Query Response and the data
914 * descriptor
915 * @hba: per adapter instance
916 * @lrb - pointer to local reference block
917 */
918static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300919int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530920{
921 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
922
Dolev Raviv68078d52013-07-30 00:35:58 +0530923 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530924
Dolev Raviv68078d52013-07-30 00:35:58 +0530925 /* Get the descriptor */
926 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300927 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530928 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300929 u16 resp_len;
930 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530931
932 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300933 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530934 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300935 buf_len = be16_to_cpu(
936 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300937 if (likely(buf_len >= resp_len)) {
938 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
939 } else {
940 dev_warn(hba->dev,
941 "%s: Response size is bigger than buffer",
942 __func__);
943 return -EINVAL;
944 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530945 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300946
947 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530948}
949
950/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530951 * ufshcd_hba_capabilities - Read controller capabilities
952 * @hba: per adapter instance
953 */
954static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
955{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530956 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530957
958 /* nutrs and nutmrs are 0 based values */
959 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
960 hba->nutmrs =
961 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
962}
963
964/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530965 * ufshcd_ready_for_uic_cmd - Check if controller is ready
966 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530967 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530968 * Return true on success, else false
969 */
970static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
971{
972 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
973 return true;
974 else
975 return false;
976}
977
978/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530979 * ufshcd_get_upmcrs - Get the power mode change request status
980 * @hba: Pointer to adapter instance
981 *
982 * This function gets the UPMCRS field of HCS register
983 * Returns value of UPMCRS field
984 */
985static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
986{
987 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
988}
989
990/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530991 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
992 * @hba: per adapter instance
993 * @uic_cmd: UIC command
994 *
995 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530996 */
997static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530998ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530999{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301000 WARN_ON(hba->active_uic_cmd);
1001
1002 hba->active_uic_cmd = uic_cmd;
1003
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301004 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301005 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1006 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1007 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301008
1009 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301010 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301011 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301012}
1013
1014/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301015 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1016 * @hba: per adapter instance
1017 * @uic_command: UIC command
1018 *
1019 * Must be called with mutex held.
1020 * Returns 0 only if success.
1021 */
1022static int
1023ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1024{
1025 int ret;
1026 unsigned long flags;
1027
1028 if (wait_for_completion_timeout(&uic_cmd->done,
1029 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1030 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1031 else
1032 ret = -ETIMEDOUT;
1033
1034 spin_lock_irqsave(hba->host->host_lock, flags);
1035 hba->active_uic_cmd = NULL;
1036 spin_unlock_irqrestore(hba->host->host_lock, flags);
1037
1038 return ret;
1039}
1040
1041/**
1042 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1043 * @hba: per adapter instance
1044 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001045 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301046 *
1047 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001048 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301049 * Returns 0 only if success.
1050 */
1051static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001052__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1053 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301054{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301055 if (!ufshcd_ready_for_uic_cmd(hba)) {
1056 dev_err(hba->dev,
1057 "Controller not ready to accept UIC commands\n");
1058 return -EIO;
1059 }
1060
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001061 if (completion)
1062 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301063
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301064 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301065
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001066 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301067}
1068
1069/**
1070 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1071 * @hba: per adapter instance
1072 * @uic_cmd: UIC command
1073 *
1074 * Returns 0 only if success.
1075 */
1076static int
1077ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1078{
1079 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001080 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301081
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001082 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301083 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001084 ufshcd_add_delay_before_dme_cmd(hba);
1085
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001086 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001087 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001088 spin_unlock_irqrestore(hba->host->host_lock, flags);
1089 if (!ret)
1090 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1091
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301092 mutex_unlock(&hba->uic_cmd_mutex);
1093
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001094 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301095 return ret;
1096}
1097
1098/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301099 * ufshcd_map_sg - Map scatter-gather list to prdt
1100 * @lrbp - pointer to local reference block
1101 *
1102 * Returns 0 in case of success, non-zero value in case of failure
1103 */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001104static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301105{
1106 struct ufshcd_sg_entry *prd_table;
1107 struct scatterlist *sg;
1108 struct scsi_cmnd *cmd;
1109 int sg_segments;
1110 int i;
1111
1112 cmd = lrbp->cmd;
1113 sg_segments = scsi_dma_map(cmd);
1114 if (sg_segments < 0)
1115 return sg_segments;
1116
1117 if (sg_segments) {
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001118 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1119 lrbp->utr_descriptor_ptr->prd_table_length =
1120 cpu_to_le16((u16)(sg_segments *
1121 sizeof(struct ufshcd_sg_entry)));
1122 else
1123 lrbp->utr_descriptor_ptr->prd_table_length =
1124 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301125
1126 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1127
1128 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1129 prd_table[i].size =
1130 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1131 prd_table[i].base_addr =
1132 cpu_to_le32(lower_32_bits(sg->dma_address));
1133 prd_table[i].upper_addr =
1134 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001135 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301136 }
1137 } else {
1138 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1139 }
1140
1141 return 0;
1142}
1143
1144/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301145 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301146 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301147 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301148 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301149static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301150{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301151 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1152
1153 if (hba->ufs_version == UFSHCI_VERSION_10) {
1154 u32 rw;
1155 rw = set & INTERRUPT_MASK_RW_VER_10;
1156 set = rw | ((set ^ intrs) & intrs);
1157 } else {
1158 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301159 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301160
1161 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1162}
1163
1164/**
1165 * ufshcd_disable_intr - disable interrupts
1166 * @hba: per adapter instance
1167 * @intrs: interrupt bits
1168 */
1169static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1170{
1171 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1172
1173 if (hba->ufs_version == UFSHCI_VERSION_10) {
1174 u32 rw;
1175 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1176 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1177 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1178
1179 } else {
1180 set &= ~intrs;
1181 }
1182
1183 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301184}
1185
1186/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301187 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1188 * descriptor according to request
1189 * @lrbp: pointer to local reference block
1190 * @upiu_flags: flags required in the header
1191 * @cmd_dir: requests data direction
1192 */
1193static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001194 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301195{
1196 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1197 u32 data_direction;
1198 u32 dword_0;
1199
1200 if (cmd_dir == DMA_FROM_DEVICE) {
1201 data_direction = UTP_DEVICE_TO_HOST;
1202 *upiu_flags = UPIU_CMD_FLAGS_READ;
1203 } else if (cmd_dir == DMA_TO_DEVICE) {
1204 data_direction = UTP_HOST_TO_DEVICE;
1205 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1206 } else {
1207 data_direction = UTP_NO_DATA_TRANSFER;
1208 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1209 }
1210
1211 dword_0 = data_direction | (lrbp->command_type
1212 << UPIU_COMMAND_TYPE_OFFSET);
1213 if (lrbp->intr_cmd)
1214 dword_0 |= UTP_REQ_DESC_INT_CMD;
1215
1216 /* Transfer request descriptor header fields */
1217 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001218 /* dword_1 is reserved, hence it is set to 0 */
1219 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301220 /*
1221 * assigning invalid value for command status. Controller
1222 * updates OCS on command completion, with the command
1223 * status
1224 */
1225 req_desc->header.dword_2 =
1226 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001227 /* dword_3 is reserved, hence it is set to 0 */
1228 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001229
1230 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301231}
1232
1233/**
1234 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1235 * for scsi commands
1236 * @lrbp - local reference block pointer
1237 * @upiu_flags - flags
1238 */
1239static
1240void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1241{
1242 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001243 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301244
1245 /* command descriptor fields */
1246 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1247 UPIU_TRANSACTION_COMMAND, upiu_flags,
1248 lrbp->lun, lrbp->task_tag);
1249 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1250 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1251
1252 /* Total EHS length and Data segment length will be zero */
1253 ucd_req_ptr->header.dword_2 = 0;
1254
1255 ucd_req_ptr->sc.exp_data_transfer_len =
1256 cpu_to_be32(lrbp->cmd->sdb.length);
1257
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001258 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1259 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1260 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1261
1262 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301263}
1264
Dolev Raviv68078d52013-07-30 00:35:58 +05301265/**
1266 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1267 * for query requsts
1268 * @hba: UFS hba
1269 * @lrbp: local reference block pointer
1270 * @upiu_flags: flags
1271 */
1272static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1273 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1274{
1275 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1276 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301277 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301278 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1279
1280 /* Query request header */
1281 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1282 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1283 lrbp->lun, lrbp->task_tag);
1284 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1285 0, query->request.query_func, 0, 0);
1286
Zang Leigang68612852016-08-25 17:39:19 +08001287 /* Data segment length only need for WRITE_DESC */
1288 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1289 ucd_req_ptr->header.dword_2 =
1290 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1291 else
1292 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301293
1294 /* Copy the Query Request buffer as is */
1295 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1296 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301297
1298 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001299 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1300 memcpy(descp, query->descriptor, len);
1301
Yaniv Gardi51047262016-02-01 15:02:38 +02001302 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301303}
1304
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301305static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1306{
1307 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1308
1309 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1310
1311 /* command descriptor fields */
1312 ucd_req_ptr->header.dword_0 =
1313 UPIU_HEADER_DWORD(
1314 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001315 /* clear rest of the fields of basic header */
1316 ucd_req_ptr->header.dword_1 = 0;
1317 ucd_req_ptr->header.dword_2 = 0;
1318
1319 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301320}
1321
1322/**
Joao Pinto300bb132016-05-11 12:21:27 +01001323 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1324 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301325 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301326 * @lrb - pointer to local reference block
1327 */
Joao Pinto300bb132016-05-11 12:21:27 +01001328static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301329{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301330 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301331 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301332
Joao Pinto300bb132016-05-11 12:21:27 +01001333 if (hba->ufs_version == UFSHCI_VERSION_20)
1334 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1335 else
1336 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1337
1338 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1339 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1340 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1341 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1342 ufshcd_prepare_utp_nop_upiu(lrbp);
1343 else
1344 ret = -EINVAL;
1345
1346 return ret;
1347}
1348
1349/**
1350 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1351 * for SCSI Purposes
1352 * @hba - per adapter instance
1353 * @lrb - pointer to local reference block
1354 */
1355static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1356{
1357 u32 upiu_flags;
1358 int ret = 0;
1359
1360 if (hba->ufs_version == UFSHCI_VERSION_20)
1361 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1362 else
1363 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1364
1365 if (likely(lrbp->cmd)) {
1366 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1367 lrbp->cmd->sc_data_direction);
1368 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1369 } else {
1370 ret = -EINVAL;
1371 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301372
1373 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301374}
1375
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001376/*
1377 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1378 * @scsi_lun: scsi LUN id
1379 *
1380 * Returns UPIU LUN id
1381 */
1382static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1383{
1384 if (scsi_is_wlun(scsi_lun))
1385 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1386 | UFS_UPIU_WLUN_ID;
1387 else
1388 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1389}
1390
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301391/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001392 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1393 * @scsi_lun: UPIU W-LUN id
1394 *
1395 * Returns SCSI W-LUN id
1396 */
1397static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1398{
1399 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1400}
1401
1402/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301403 * ufshcd_queuecommand - main entry point for SCSI requests
1404 * @cmd: command from SCSI Midlayer
1405 * @done: call back function
1406 *
1407 * Returns 0 for success, non-zero in case of failure
1408 */
1409static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1410{
1411 struct ufshcd_lrb *lrbp;
1412 struct ufs_hba *hba;
1413 unsigned long flags;
1414 int tag;
1415 int err = 0;
1416
1417 hba = shost_priv(host);
1418
1419 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001420 if (!ufshcd_valid_tag(hba, tag)) {
1421 dev_err(hba->dev,
1422 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1423 __func__, tag, cmd, cmd->request);
1424 BUG();
1425 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301426
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301427 spin_lock_irqsave(hba->host->host_lock, flags);
1428 switch (hba->ufshcd_state) {
1429 case UFSHCD_STATE_OPERATIONAL:
1430 break;
Zang Leiganga17bddc2017-04-04 19:32:20 +00001431 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301432 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301433 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301434 goto out_unlock;
1435 case UFSHCD_STATE_ERROR:
1436 set_host_byte(cmd, DID_ERROR);
1437 cmd->scsi_done(cmd);
1438 goto out_unlock;
1439 default:
1440 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1441 __func__, hba->ufshcd_state);
1442 set_host_byte(cmd, DID_BAD_TARGET);
1443 cmd->scsi_done(cmd);
1444 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301445 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001446
1447 /* if error handling is in progress, don't issue commands */
1448 if (ufshcd_eh_in_progress(hba)) {
1449 set_host_byte(cmd, DID_ERROR);
1450 cmd->scsi_done(cmd);
1451 goto out_unlock;
1452 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301453 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301454
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301455 /* acquire the tag to make sure device cmds don't use it */
1456 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1457 /*
1458 * Dev manage command in progress, requeue the command.
1459 * Requeuing the command helps in cases where the request *may*
1460 * find different tag instead of waiting for dev manage command
1461 * completion.
1462 */
1463 err = SCSI_MLQUEUE_HOST_BUSY;
1464 goto out;
1465 }
1466
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001467 err = ufshcd_hold(hba, true);
1468 if (err) {
1469 err = SCSI_MLQUEUE_HOST_BUSY;
1470 clear_bit_unlock(tag, &hba->lrb_in_use);
1471 goto out;
1472 }
Mohan Srinivasane2d88782016-12-14 15:55:36 -08001473
1474 /* IO svc time latency histogram */
1475 if (hba != NULL && cmd->request != NULL) {
1476 if (hba->latency_hist_enabled &&
1477 (cmd->request->cmd_type == REQ_TYPE_FS)) {
1478 cmd->request->lat_hist_io_start = ktime_get();
1479 cmd->request->lat_hist_enabled = 1;
1480 } else
1481 cmd->request->lat_hist_enabled = 0;
1482 }
1483
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001484 WARN_ON(hba->clk_gating.state != CLKS_ON);
1485
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301486 lrbp = &hba->lrb[tag];
1487
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301488 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301489 lrbp->cmd = cmd;
1490 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1491 lrbp->sense_buffer = cmd->sense_buffer;
1492 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001493 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001494 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301495
Joao Pinto300bb132016-05-11 12:21:27 +01001496 ufshcd_comp_scsi_upiu(hba, lrbp);
1497
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001498 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301499 if (err) {
1500 lrbp->cmd = NULL;
1501 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301502 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301503 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301504
1505 /* issue command to the controller */
1506 spin_lock_irqsave(hba->host->host_lock, flags);
1507 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301508out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301509 spin_unlock_irqrestore(hba->host->host_lock, flags);
1510out:
1511 return err;
1512}
1513
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301514static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1515 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1516{
1517 lrbp->cmd = NULL;
1518 lrbp->sense_bufflen = 0;
1519 lrbp->sense_buffer = NULL;
1520 lrbp->task_tag = tag;
1521 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301522 lrbp->intr_cmd = true; /* No interrupt aggregation */
1523 hba->dev_cmd.type = cmd_type;
1524
Joao Pinto300bb132016-05-11 12:21:27 +01001525 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301526}
1527
1528static int
1529ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1530{
1531 int err = 0;
1532 unsigned long flags;
1533 u32 mask = 1 << tag;
1534
1535 /* clear outstanding transaction before retry */
1536 spin_lock_irqsave(hba->host->host_lock, flags);
1537 ufshcd_utrl_clear(hba, tag);
1538 spin_unlock_irqrestore(hba->host->host_lock, flags);
1539
1540 /*
1541 * wait for for h/w to clear corresponding bit in door-bell.
1542 * max. wait is 1 sec.
1543 */
1544 err = ufshcd_wait_for_register(hba,
1545 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001546 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301547
1548 return err;
1549}
1550
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001551static int
1552ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1553{
1554 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1555
1556 /* Get the UPIU response */
1557 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1558 UPIU_RSP_CODE_OFFSET;
1559 return query_res->response;
1560}
1561
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301562/**
1563 * ufshcd_dev_cmd_completion() - handles device management command responses
1564 * @hba: per adapter instance
1565 * @lrbp: pointer to local reference block
1566 */
1567static int
1568ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1569{
1570 int resp;
1571 int err = 0;
1572
1573 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1574
1575 switch (resp) {
1576 case UPIU_TRANSACTION_NOP_IN:
1577 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1578 err = -EINVAL;
1579 dev_err(hba->dev, "%s: unexpected response %x\n",
1580 __func__, resp);
1581 }
1582 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301583 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001584 err = ufshcd_check_query_response(hba, lrbp);
1585 if (!err)
1586 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301587 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301588 case UPIU_TRANSACTION_REJECT_UPIU:
1589 /* TODO: handle Reject UPIU Response */
1590 err = -EPERM;
1591 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1592 __func__);
1593 break;
1594 default:
1595 err = -EINVAL;
1596 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1597 __func__, resp);
1598 break;
1599 }
1600
1601 return err;
1602}
1603
1604static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1605 struct ufshcd_lrb *lrbp, int max_timeout)
1606{
1607 int err = 0;
1608 unsigned long time_left;
1609 unsigned long flags;
1610
1611 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1612 msecs_to_jiffies(max_timeout));
1613
1614 spin_lock_irqsave(hba->host->host_lock, flags);
1615 hba->dev_cmd.complete = NULL;
1616 if (likely(time_left)) {
1617 err = ufshcd_get_tr_ocs(lrbp);
1618 if (!err)
1619 err = ufshcd_dev_cmd_completion(hba, lrbp);
1620 }
1621 spin_unlock_irqrestore(hba->host->host_lock, flags);
1622
1623 if (!time_left) {
1624 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001625 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1626 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301627 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001628 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301629 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001630 /*
1631 * in case of an error, after clearing the doorbell,
1632 * we also need to clear the outstanding_request
1633 * field in hba
1634 */
1635 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301636 }
1637
1638 return err;
1639}
1640
1641/**
1642 * ufshcd_get_dev_cmd_tag - Get device management command tag
1643 * @hba: per-adapter instance
1644 * @tag: pointer to variable with available slot value
1645 *
1646 * Get a free slot and lock it until device management command
1647 * completes.
1648 *
1649 * Returns false if free slot is unavailable for locking, else
1650 * return true with tag value in @tag.
1651 */
1652static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1653{
1654 int tag;
1655 bool ret = false;
1656 unsigned long tmp;
1657
1658 if (!tag_out)
1659 goto out;
1660
1661 do {
1662 tmp = ~hba->lrb_in_use;
1663 tag = find_last_bit(&tmp, hba->nutrs);
1664 if (tag >= hba->nutrs)
1665 goto out;
1666 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1667
1668 *tag_out = tag;
1669 ret = true;
1670out:
1671 return ret;
1672}
1673
1674static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1675{
1676 clear_bit_unlock(tag, &hba->lrb_in_use);
1677}
1678
1679/**
1680 * ufshcd_exec_dev_cmd - API for sending device management requests
1681 * @hba - UFS hba
1682 * @cmd_type - specifies the type (NOP, Query...)
1683 * @timeout - time in seconds
1684 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301685 * NOTE: Since there is only one available tag for device management commands,
1686 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301687 */
1688static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1689 enum dev_cmd_type cmd_type, int timeout)
1690{
1691 struct ufshcd_lrb *lrbp;
1692 int err;
1693 int tag;
1694 struct completion wait;
1695 unsigned long flags;
1696
1697 /*
1698 * Get free slot, sleep if slots are unavailable.
1699 * Even though we use wait_event() which sleeps indefinitely,
1700 * the maximum wait time is bounded by SCSI request timeout.
1701 */
1702 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1703
1704 init_completion(&wait);
1705 lrbp = &hba->lrb[tag];
1706 WARN_ON(lrbp->cmd);
1707 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1708 if (unlikely(err))
1709 goto out_put_tag;
1710
1711 hba->dev_cmd.complete = &wait;
1712
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001713 /* Make sure descriptors are ready before ringing the doorbell */
1714 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301715 spin_lock_irqsave(hba->host->host_lock, flags);
1716 ufshcd_send_command(hba, tag);
1717 spin_unlock_irqrestore(hba->host->host_lock, flags);
1718
1719 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1720
1721out_put_tag:
1722 ufshcd_put_dev_cmd_tag(hba, tag);
1723 wake_up(&hba->dev_cmd.tag_wq);
1724 return err;
1725}
1726
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301727/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001728 * ufshcd_init_query() - init the query response and request parameters
1729 * @hba: per-adapter instance
1730 * @request: address of the request pointer to be initialized
1731 * @response: address of the response pointer to be initialized
1732 * @opcode: operation to perform
1733 * @idn: flag idn to access
1734 * @index: LU number to access
1735 * @selector: query/flag/descriptor further identification
1736 */
1737static inline void ufshcd_init_query(struct ufs_hba *hba,
1738 struct ufs_query_req **request, struct ufs_query_res **response,
1739 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1740{
1741 *request = &hba->dev_cmd.query.request;
1742 *response = &hba->dev_cmd.query.response;
1743 memset(*request, 0, sizeof(struct ufs_query_req));
1744 memset(*response, 0, sizeof(struct ufs_query_res));
1745 (*request)->upiu_req.opcode = opcode;
1746 (*request)->upiu_req.idn = idn;
1747 (*request)->upiu_req.index = index;
1748 (*request)->upiu_req.selector = selector;
1749}
1750
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001751static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1752 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1753{
1754 int ret;
1755 int retries;
1756
1757 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1758 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1759 if (ret)
1760 dev_dbg(hba->dev,
1761 "%s: failed with error %d, retries %d\n",
1762 __func__, ret, retries);
1763 else
1764 break;
1765 }
1766
1767 if (ret)
1768 dev_err(hba->dev,
1769 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1770 __func__, opcode, idn, ret, retries);
1771 return ret;
1772}
1773
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001774/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301775 * ufshcd_query_flag() - API function for sending flag query requests
1776 * hba: per-adapter instance
1777 * query_opcode: flag query to perform
1778 * idn: flag idn to access
1779 * flag_res: the flag value after the query request completes
1780 *
1781 * Returns 0 for success, non-zero in case of failure
1782 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001783int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301784 enum flag_idn idn, bool *flag_res)
1785{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001786 struct ufs_query_req *request = NULL;
1787 struct ufs_query_res *response = NULL;
1788 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001789 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301790
1791 BUG_ON(!hba);
1792
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001793 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301794 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001795 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1796 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301797
1798 switch (opcode) {
1799 case UPIU_QUERY_OPCODE_SET_FLAG:
1800 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1801 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1802 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1803 break;
1804 case UPIU_QUERY_OPCODE_READ_FLAG:
1805 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1806 if (!flag_res) {
1807 /* No dummy reads */
1808 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1809 __func__);
1810 err = -EINVAL;
1811 goto out_unlock;
1812 }
1813 break;
1814 default:
1815 dev_err(hba->dev,
1816 "%s: Expected query flag opcode but got = %d\n",
1817 __func__, opcode);
1818 err = -EINVAL;
1819 goto out_unlock;
1820 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301821
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001822 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1823 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1824
1825 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301826
1827 if (err) {
1828 dev_err(hba->dev,
1829 "%s: Sending flag query for idn %d failed, err = %d\n",
1830 __func__, idn, err);
1831 goto out_unlock;
1832 }
1833
1834 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301835 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301836 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1837
1838out_unlock:
1839 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001840 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301841 return err;
1842}
1843
1844/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301845 * ufshcd_query_attr - API function for sending attribute requests
1846 * hba: per-adapter instance
1847 * opcode: attribute opcode
1848 * idn: attribute idn to access
1849 * index: index field
1850 * selector: selector field
1851 * attr_val: the attribute value after the query request completes
1852 *
1853 * Returns 0 for success, non-zero in case of failure
1854*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301855static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301856 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1857{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001858 struct ufs_query_req *request = NULL;
1859 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301860 int err;
1861
1862 BUG_ON(!hba);
1863
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001864 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301865 if (!attr_val) {
1866 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1867 __func__, opcode);
1868 err = -EINVAL;
1869 goto out;
1870 }
1871
1872 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001873 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1874 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301875
1876 switch (opcode) {
1877 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1878 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301879 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301880 break;
1881 case UPIU_QUERY_OPCODE_READ_ATTR:
1882 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1883 break;
1884 default:
1885 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1886 __func__, opcode);
1887 err = -EINVAL;
1888 goto out_unlock;
1889 }
1890
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001891 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301892
1893 if (err) {
1894 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1895 __func__, opcode, idn, err);
1896 goto out_unlock;
1897 }
1898
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301899 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301900
1901out_unlock:
1902 mutex_unlock(&hba->dev_cmd.lock);
1903out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001904 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301905 return err;
1906}
1907
1908/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001909 * ufshcd_query_attr_retry() - API function for sending query
1910 * attribute with retries
1911 * @hba: per-adapter instance
1912 * @opcode: attribute opcode
1913 * @idn: attribute idn to access
1914 * @index: index field
1915 * @selector: selector field
1916 * @attr_val: the attribute value after the query request
1917 * completes
1918 *
1919 * Returns 0 for success, non-zero in case of failure
1920*/
1921static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1922 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1923 u32 *attr_val)
1924{
1925 int ret = 0;
1926 u32 retries;
1927
1928 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1929 ret = ufshcd_query_attr(hba, opcode, idn, index,
1930 selector, attr_val);
1931 if (ret)
1932 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1933 __func__, ret, retries);
1934 else
1935 break;
1936 }
1937
1938 if (ret)
1939 dev_err(hba->dev,
1940 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1941 __func__, idn, ret, QUERY_REQ_RETRIES);
1942 return ret;
1943}
1944
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001945static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001946 enum query_opcode opcode, enum desc_idn idn, u8 index,
1947 u8 selector, u8 *desc_buf, int *buf_len)
1948{
1949 struct ufs_query_req *request = NULL;
1950 struct ufs_query_res *response = NULL;
1951 int err;
1952
1953 BUG_ON(!hba);
1954
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001955 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001956 if (!desc_buf) {
1957 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1958 __func__, opcode);
1959 err = -EINVAL;
1960 goto out;
1961 }
1962
Potomski, MichalXe1928452017-02-23 09:05:30 +00001963 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001964 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1965 __func__, *buf_len);
1966 err = -EINVAL;
1967 goto out;
1968 }
1969
1970 mutex_lock(&hba->dev_cmd.lock);
1971 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1972 selector);
1973 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001974 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001975
1976 switch (opcode) {
1977 case UPIU_QUERY_OPCODE_WRITE_DESC:
1978 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1979 break;
1980 case UPIU_QUERY_OPCODE_READ_DESC:
1981 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1982 break;
1983 default:
1984 dev_err(hba->dev,
1985 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1986 __func__, opcode);
1987 err = -EINVAL;
1988 goto out_unlock;
1989 }
1990
1991 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1992
1993 if (err) {
1994 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1995 __func__, opcode, idn, err);
1996 goto out_unlock;
1997 }
1998
1999 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03002000 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002001
2002out_unlock:
2003 mutex_unlock(&hba->dev_cmd.lock);
2004out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002005 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03002006 return err;
2007}
2008
2009/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002010 * ufshcd_query_descriptor_retry - API function for sending descriptor
2011 * requests
2012 * hba: per-adapter instance
2013 * opcode: attribute opcode
2014 * idn: attribute idn to access
2015 * index: index field
2016 * selector: selector field
2017 * desc_buf: the buffer that contains the descriptor
2018 * buf_len: length parameter passed to the device
2019 *
2020 * Returns 0 for success, non-zero in case of failure.
2021 * The buf_len parameter will contain, on return, the length parameter
2022 * received on the response.
2023 */
2024int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2025 enum query_opcode opcode, enum desc_idn idn, u8 index,
2026 u8 selector, u8 *desc_buf, int *buf_len)
2027{
2028 int err;
2029 int retries;
2030
2031 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2032 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2033 selector, desc_buf, buf_len);
2034 if (!err || err == -EINVAL)
2035 break;
2036 }
2037
2038 return err;
2039}
2040EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2041
2042/**
Potomski, MichalXe1928452017-02-23 09:05:30 +00002043 * ufshcd_read_desc_length - read the specified descriptor length from header
2044 * @hba: Pointer to adapter instance
2045 * @desc_id: descriptor idn value
2046 * @desc_index: descriptor index
2047 * @desc_length: pointer to variable to read the length of descriptor
2048 *
2049 * Return 0 in case of success, non-zero otherwise
2050 */
2051static int ufshcd_read_desc_length(struct ufs_hba *hba,
2052 enum desc_idn desc_id,
2053 int desc_index,
2054 int *desc_length)
2055{
2056 int ret;
2057 u8 header[QUERY_DESC_HDR_SIZE];
2058 int header_len = QUERY_DESC_HDR_SIZE;
2059
2060 if (desc_id >= QUERY_DESC_IDN_MAX)
2061 return -EINVAL;
2062
2063 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2064 desc_id, desc_index, 0, header,
2065 &header_len);
2066
2067 if (ret) {
2068 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2069 __func__, desc_id);
2070 return ret;
2071 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
2072 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
2073 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
2074 desc_id);
2075 ret = -EINVAL;
2076 }
2077
2078 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
2079 return ret;
2080
2081}
2082
2083/**
2084 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
2085 * @hba: Pointer to adapter instance
2086 * @desc_id: descriptor idn value
2087 * @desc_len: mapped desc length (out)
2088 *
2089 * Return 0 in case of success, non-zero otherwise
2090 */
2091int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
2092 enum desc_idn desc_id, int *desc_len)
2093{
2094 switch (desc_id) {
2095 case QUERY_DESC_IDN_DEVICE:
2096 *desc_len = hba->desc_size.dev_desc;
2097 break;
2098 case QUERY_DESC_IDN_POWER:
2099 *desc_len = hba->desc_size.pwr_desc;
2100 break;
2101 case QUERY_DESC_IDN_GEOMETRY:
2102 *desc_len = hba->desc_size.geom_desc;
2103 break;
2104 case QUERY_DESC_IDN_CONFIGURATION:
2105 *desc_len = hba->desc_size.conf_desc;
2106 break;
2107 case QUERY_DESC_IDN_UNIT:
2108 *desc_len = hba->desc_size.unit_desc;
2109 break;
2110 case QUERY_DESC_IDN_INTERCONNECT:
2111 *desc_len = hba->desc_size.interc_desc;
2112 break;
2113 case QUERY_DESC_IDN_STRING:
2114 *desc_len = QUERY_DESC_MAX_SIZE;
2115 break;
2116 case QUERY_DESC_IDN_RFU_0:
2117 case QUERY_DESC_IDN_RFU_1:
2118 *desc_len = 0;
2119 break;
2120 default:
2121 *desc_len = 0;
2122 return -EINVAL;
2123 }
2124 return 0;
2125}
2126EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
2127
2128/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002129 * ufshcd_read_desc_param - read the specified descriptor parameter
2130 * @hba: Pointer to adapter instance
2131 * @desc_id: descriptor idn value
2132 * @desc_index: descriptor index
2133 * @param_offset: offset of the parameter to read
2134 * @param_read_buf: pointer to buffer where parameter would be read
2135 * @param_size: sizeof(param_read_buf)
2136 *
2137 * Return 0 in case of success, non-zero otherwise
2138 */
2139static int ufshcd_read_desc_param(struct ufs_hba *hba,
2140 enum desc_idn desc_id,
2141 int desc_index,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002142 u8 param_offset,
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002143 u8 *param_read_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002144 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002145{
2146 int ret;
2147 u8 *desc_buf;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002148 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002149 bool is_kmalloc = true;
2150
Potomski, MichalXe1928452017-02-23 09:05:30 +00002151 /* Safety check */
2152 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002153 return -EINVAL;
2154
Potomski, MichalXe1928452017-02-23 09:05:30 +00002155 /* Get the max length of descriptor from structure filled up at probe
2156 * time.
2157 */
2158 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002159
Potomski, MichalXe1928452017-02-23 09:05:30 +00002160 /* Sanity checks */
2161 if (ret || !buff_len) {
2162 dev_err(hba->dev, "%s: Failed to get full descriptor length",
2163 __func__);
2164 return ret;
2165 }
2166
2167 /* Check whether we need temp memory */
2168 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002169 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2170 if (!desc_buf)
2171 return -ENOMEM;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002172 } else {
2173 desc_buf = param_read_buf;
2174 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002175 }
2176
Potomski, MichalXe1928452017-02-23 09:05:30 +00002177 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002178 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002179 desc_id, desc_index, 0,
2180 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002181
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002182 if (ret) {
2183 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2184 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002185 goto out;
2186 }
2187
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002188 /* Sanity check */
2189 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2190 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2191 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2192 ret = -EINVAL;
2193 goto out;
2194 }
2195
Potomski, MichalXe1928452017-02-23 09:05:30 +00002196 /* Check wherher we will not copy more data, than available */
2197 if (is_kmalloc && param_size > buff_len)
2198 param_size = buff_len;
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002199
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002200 if (is_kmalloc)
2201 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2202out:
2203 if (is_kmalloc)
2204 kfree(desc_buf);
2205 return ret;
2206}
2207
2208static inline int ufshcd_read_desc(struct ufs_hba *hba,
2209 enum desc_idn desc_id,
2210 int desc_index,
2211 u8 *buf,
2212 u32 size)
2213{
2214 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2215}
2216
2217static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2218 u8 *buf,
2219 u32 size)
2220{
2221 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2222}
2223
Yaniv Gardib573d482016-03-10 17:37:09 +02002224int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2225{
2226 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2227}
2228EXPORT_SYMBOL(ufshcd_read_device_desc);
2229
2230/**
2231 * ufshcd_read_string_desc - read string descriptor
2232 * @hba: pointer to adapter instance
2233 * @desc_index: descriptor index
2234 * @buf: pointer to buffer where descriptor would be read
2235 * @size: size of buf
2236 * @ascii: if true convert from unicode to ascii characters
2237 *
2238 * Return 0 in case of success, non-zero otherwise
2239 */
2240int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2241 u32 size, bool ascii)
2242{
2243 int err = 0;
2244
2245 err = ufshcd_read_desc(hba,
2246 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2247
2248 if (err) {
2249 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2250 __func__, QUERY_REQ_RETRIES, err);
2251 goto out;
2252 }
2253
2254 if (ascii) {
2255 int desc_len;
2256 int ascii_len;
2257 int i;
2258 char *buff_ascii;
2259
2260 desc_len = buf[0];
2261 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2262 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2263 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2264 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2265 __func__);
2266 err = -ENOMEM;
2267 goto out;
2268 }
2269
2270 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2271 if (!buff_ascii) {
2272 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002273 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002274 }
2275
2276 /*
2277 * the descriptor contains string in UTF16 format
2278 * we need to convert to utf-8 so it can be displayed
2279 */
2280 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2281 desc_len - QUERY_DESC_HDR_SIZE,
2282 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2283
2284 /* replace non-printable or non-ASCII characters with spaces */
2285 for (i = 0; i < ascii_len; i++)
2286 ufshcd_remove_non_printable(&buff_ascii[i]);
2287
2288 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2289 size - QUERY_DESC_HDR_SIZE);
2290 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2291 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002292 kfree(buff_ascii);
2293 }
2294out:
2295 return err;
2296}
2297EXPORT_SYMBOL(ufshcd_read_string_desc);
2298
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002299/**
2300 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2301 * @hba: Pointer to adapter instance
2302 * @lun: lun id
2303 * @param_offset: offset of the parameter to read
2304 * @param_read_buf: pointer to buffer where parameter would be read
2305 * @param_size: sizeof(param_read_buf)
2306 *
2307 * Return 0 in case of success, non-zero otherwise
2308 */
2309static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2310 int lun,
2311 enum unit_desc_param param_offset,
2312 u8 *param_read_buf,
2313 u32 param_size)
2314{
2315 /*
2316 * Unit descriptors are only available for general purpose LUs (LUN id
2317 * from 0 to 7) and RPMB Well known LU.
2318 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002319 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002320 return -EOPNOTSUPP;
2321
2322 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2323 param_offset, param_read_buf, param_size);
2324}
2325
2326/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302327 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2328 * @hba: per adapter instance
2329 *
2330 * 1. Allocate DMA memory for Command Descriptor array
2331 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2332 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2333 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2334 * (UTMRDL)
2335 * 4. Allocate memory for local reference block(lrb).
2336 *
2337 * Returns 0 for success, non-zero in case of failure
2338 */
2339static int ufshcd_memory_alloc(struct ufs_hba *hba)
2340{
2341 size_t utmrdl_size, utrdl_size, ucdl_size;
2342
2343 /* Allocate memory for UTP command descriptors */
2344 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002345 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2346 ucdl_size,
2347 &hba->ucdl_dma_addr,
2348 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302349
2350 /*
2351 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2352 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2353 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2354 * be aligned to 128 bytes as well
2355 */
2356 if (!hba->ucdl_base_addr ||
2357 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302358 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302359 "Command Descriptor Memory allocation failed\n");
2360 goto out;
2361 }
2362
2363 /*
2364 * Allocate memory for UTP Transfer descriptors
2365 * UFSHCI requires 1024 byte alignment of UTRD
2366 */
2367 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002368 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2369 utrdl_size,
2370 &hba->utrdl_dma_addr,
2371 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302372 if (!hba->utrdl_base_addr ||
2373 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302374 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302375 "Transfer Descriptor Memory allocation failed\n");
2376 goto out;
2377 }
2378
2379 /*
2380 * Allocate memory for UTP Task Management descriptors
2381 * UFSHCI requires 1024 byte alignment of UTMRD
2382 */
2383 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002384 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2385 utmrdl_size,
2386 &hba->utmrdl_dma_addr,
2387 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302388 if (!hba->utmrdl_base_addr ||
2389 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302390 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302391 "Task Management Descriptor Memory allocation failed\n");
2392 goto out;
2393 }
2394
2395 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002396 hba->lrb = devm_kzalloc(hba->dev,
2397 hba->nutrs * sizeof(struct ufshcd_lrb),
2398 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302399 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302400 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302401 goto out;
2402 }
2403 return 0;
2404out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302405 return -ENOMEM;
2406}
2407
2408/**
2409 * ufshcd_host_memory_configure - configure local reference block with
2410 * memory offsets
2411 * @hba: per adapter instance
2412 *
2413 * Configure Host memory space
2414 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2415 * address.
2416 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2417 * and PRDT offset.
2418 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2419 * into local reference block.
2420 */
2421static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2422{
2423 struct utp_transfer_cmd_desc *cmd_descp;
2424 struct utp_transfer_req_desc *utrdlp;
2425 dma_addr_t cmd_desc_dma_addr;
2426 dma_addr_t cmd_desc_element_addr;
2427 u16 response_offset;
2428 u16 prdt_offset;
2429 int cmd_desc_size;
2430 int i;
2431
2432 utrdlp = hba->utrdl_base_addr;
2433 cmd_descp = hba->ucdl_base_addr;
2434
2435 response_offset =
2436 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2437 prdt_offset =
2438 offsetof(struct utp_transfer_cmd_desc, prd_table);
2439
2440 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2441 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2442
2443 for (i = 0; i < hba->nutrs; i++) {
2444 /* Configure UTRD with command descriptor base address */
2445 cmd_desc_element_addr =
2446 (cmd_desc_dma_addr + (cmd_desc_size * i));
2447 utrdlp[i].command_desc_base_addr_lo =
2448 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2449 utrdlp[i].command_desc_base_addr_hi =
2450 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2451
2452 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002453 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2454 utrdlp[i].response_upiu_offset =
2455 cpu_to_le16(response_offset);
2456 utrdlp[i].prd_table_offset =
2457 cpu_to_le16(prdt_offset);
2458 utrdlp[i].response_upiu_length =
2459 cpu_to_le16(ALIGNED_UPIU_SIZE);
2460 } else {
2461 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302462 cpu_to_le16((response_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002463 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302464 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002465 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302466 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002467 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302468
2469 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302470 hba->lrb[i].ucd_req_ptr =
2471 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302472 hba->lrb[i].ucd_rsp_ptr =
2473 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2474 hba->lrb[i].ucd_prdt_ptr =
2475 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2476 }
2477}
2478
2479/**
2480 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2481 * @hba: per adapter instance
2482 *
2483 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2484 * in order to initialize the Unipro link startup procedure.
2485 * Once the Unipro links are up, the device connected to the controller
2486 * is detected.
2487 *
2488 * Returns 0 on success, non-zero value on failure
2489 */
2490static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2491{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302492 struct uic_command uic_cmd = {0};
2493 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302494
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302495 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2496
2497 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2498 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302499 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302500 "dme-link-startup: error code %d\n", ret);
2501 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302502}
2503
Yaniv Gardicad2e032015-03-31 17:37:14 +03002504static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2505{
2506 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2507 unsigned long min_sleep_time_us;
2508
2509 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2510 return;
2511
2512 /*
2513 * last_dme_cmd_tstamp will be 0 only for 1st call to
2514 * this function
2515 */
2516 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2517 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2518 } else {
2519 unsigned long delta =
2520 (unsigned long) ktime_to_us(
2521 ktime_sub(ktime_get(),
2522 hba->last_dme_cmd_tstamp));
2523
2524 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2525 min_sleep_time_us =
2526 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2527 else
2528 return; /* no more delay required */
2529 }
2530
2531 /* allow sleep for extra 50us if needed */
2532 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2533}
2534
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302535/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302536 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2537 * @hba: per adapter instance
2538 * @attr_sel: uic command argument1
2539 * @attr_set: attribute set type as uic command argument2
2540 * @mib_val: setting value as uic command argument3
2541 * @peer: indicate whether peer or local
2542 *
2543 * Returns 0 on success, non-zero value on failure
2544 */
2545int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2546 u8 attr_set, u32 mib_val, u8 peer)
2547{
2548 struct uic_command uic_cmd = {0};
2549 static const char *const action[] = {
2550 "dme-set",
2551 "dme-peer-set"
2552 };
2553 const char *set = action[!!peer];
2554 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002555 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302556
2557 uic_cmd.command = peer ?
2558 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2559 uic_cmd.argument1 = attr_sel;
2560 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2561 uic_cmd.argument3 = mib_val;
2562
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002563 do {
2564 /* for peer attributes we retry upon failure */
2565 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2566 if (ret)
2567 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2568 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2569 } while (ret && peer && --retries);
2570
2571 if (!retries)
2572 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2573 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2574 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302575
2576 return ret;
2577}
2578EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2579
2580/**
2581 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2582 * @hba: per adapter instance
2583 * @attr_sel: uic command argument1
2584 * @mib_val: the value of the attribute as returned by the UIC command
2585 * @peer: indicate whether peer or local
2586 *
2587 * Returns 0 on success, non-zero value on failure
2588 */
2589int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2590 u32 *mib_val, u8 peer)
2591{
2592 struct uic_command uic_cmd = {0};
2593 static const char *const action[] = {
2594 "dme-get",
2595 "dme-peer-get"
2596 };
2597 const char *get = action[!!peer];
2598 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002599 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002600 struct ufs_pa_layer_attr orig_pwr_info;
2601 struct ufs_pa_layer_attr temp_pwr_info;
2602 bool pwr_mode_change = false;
2603
2604 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2605 orig_pwr_info = hba->pwr_info;
2606 temp_pwr_info = orig_pwr_info;
2607
2608 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2609 orig_pwr_info.pwr_rx == FAST_MODE) {
2610 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2611 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2612 pwr_mode_change = true;
2613 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2614 orig_pwr_info.pwr_rx == SLOW_MODE) {
2615 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2616 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2617 pwr_mode_change = true;
2618 }
2619 if (pwr_mode_change) {
2620 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2621 if (ret)
2622 goto out;
2623 }
2624 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302625
2626 uic_cmd.command = peer ?
2627 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2628 uic_cmd.argument1 = attr_sel;
2629
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002630 do {
2631 /* for peer attributes we retry upon failure */
2632 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2633 if (ret)
2634 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2635 get, UIC_GET_ATTR_ID(attr_sel), ret);
2636 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302637
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002638 if (!retries)
2639 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2640 get, UIC_GET_ATTR_ID(attr_sel), retries);
2641
2642 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302643 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002644
2645 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2646 && pwr_mode_change)
2647 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302648out:
2649 return ret;
2650}
2651EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2652
2653/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002654 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2655 * state) and waits for it to take effect.
2656 *
2657 * @hba: per adapter instance
2658 * @cmd: UIC command to execute
2659 *
2660 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2661 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2662 * and device UniPro link and hence it's final completion would be indicated by
2663 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2664 * addition to normal UIC command completion Status (UCCS). This function only
2665 * returns after the relevant status bits indicate the completion.
2666 *
2667 * Returns 0 on success, non-zero value on failure
2668 */
2669static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2670{
2671 struct completion uic_async_done;
2672 unsigned long flags;
2673 u8 status;
2674 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002675 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002676
2677 mutex_lock(&hba->uic_cmd_mutex);
2678 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002679 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002680
2681 spin_lock_irqsave(hba->host->host_lock, flags);
2682 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002683 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2684 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2685 /*
2686 * Make sure UIC command completion interrupt is disabled before
2687 * issuing UIC command.
2688 */
2689 wmb();
2690 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002691 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002692 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2693 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002694 if (ret) {
2695 dev_err(hba->dev,
2696 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2697 cmd->command, cmd->argument3, ret);
2698 goto out;
2699 }
2700
2701 if (!wait_for_completion_timeout(hba->uic_async_done,
2702 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2703 dev_err(hba->dev,
2704 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2705 cmd->command, cmd->argument3);
2706 ret = -ETIMEDOUT;
2707 goto out;
2708 }
2709
2710 status = ufshcd_get_upmcrs(hba);
2711 if (status != PWR_LOCAL) {
2712 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002713 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002714 cmd->command, status);
2715 ret = (status != PWR_OK) ? status : -1;
2716 }
2717out:
2718 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002719 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002720 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002721 if (reenable_intr)
2722 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002723 spin_unlock_irqrestore(hba->host->host_lock, flags);
2724 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002725
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002726 return ret;
2727}
2728
2729/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302730 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2731 * using DME_SET primitives.
2732 * @hba: per adapter instance
2733 * @mode: powr mode value
2734 *
2735 * Returns 0 on success, non-zero value on failure
2736 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302737static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302738{
2739 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002740 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302741
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002742 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2743 ret = ufshcd_dme_set(hba,
2744 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2745 if (ret) {
2746 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2747 __func__, ret);
2748 goto out;
2749 }
2750 }
2751
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302752 uic_cmd.command = UIC_CMD_DME_SET;
2753 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2754 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002755 ufshcd_hold(hba, false);
2756 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2757 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302758
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002759out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002760 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002761}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302762
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002763static int ufshcd_link_recovery(struct ufs_hba *hba)
2764{
2765 int ret;
2766 unsigned long flags;
2767
2768 spin_lock_irqsave(hba->host->host_lock, flags);
2769 hba->ufshcd_state = UFSHCD_STATE_RESET;
2770 ufshcd_set_eh_in_progress(hba);
2771 spin_unlock_irqrestore(hba->host->host_lock, flags);
2772
2773 ret = ufshcd_host_reset_and_restore(hba);
2774
2775 spin_lock_irqsave(hba->host->host_lock, flags);
2776 if (ret)
2777 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2778 ufshcd_clear_eh_in_progress(hba);
2779 spin_unlock_irqrestore(hba->host->host_lock, flags);
2780
2781 if (ret)
2782 dev_err(hba->dev, "%s: link recovery failed, err %d",
2783 __func__, ret);
2784
2785 return ret;
2786}
2787
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002788static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002789{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002790 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002791 struct uic_command uic_cmd = {0};
2792
2793 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002794 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002795
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002796 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002797 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2798 __func__, ret);
2799
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002800 /*
2801 * If link recovery fails then return error so that caller
2802 * don't retry the hibern8 enter again.
2803 */
2804 if (ufshcd_link_recovery(hba))
2805 ret = -ENOLINK;
2806 }
2807
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002808 return ret;
2809}
2810
2811static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2812{
2813 int ret = 0, retries;
2814
2815 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2816 ret = __ufshcd_uic_hibern8_enter(hba);
2817 if (!ret || ret == -ENOLINK)
2818 goto out;
2819 }
2820out:
2821 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002822}
2823
2824static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2825{
2826 struct uic_command uic_cmd = {0};
2827 int ret;
2828
2829 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2830 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302831 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002832 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2833 __func__, ret);
2834 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302835 }
2836
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302837 return ret;
2838}
2839
Yaniv Gardi50646362014-10-23 13:25:13 +03002840 /**
2841 * ufshcd_init_pwr_info - setting the POR (power on reset)
2842 * values in hba power info
2843 * @hba: per-adapter instance
2844 */
2845static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2846{
2847 hba->pwr_info.gear_rx = UFS_PWM_G1;
2848 hba->pwr_info.gear_tx = UFS_PWM_G1;
2849 hba->pwr_info.lane_rx = 1;
2850 hba->pwr_info.lane_tx = 1;
2851 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2852 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2853 hba->pwr_info.hs_rate = 0;
2854}
2855
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302856/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002857 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2858 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302859 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002860static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302861{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002862 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2863
2864 if (hba->max_pwr_info.is_valid)
2865 return 0;
2866
2867 pwr_info->pwr_tx = FASTAUTO_MODE;
2868 pwr_info->pwr_rx = FASTAUTO_MODE;
2869 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302870
2871 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002872 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2873 &pwr_info->lane_rx);
2874 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2875 &pwr_info->lane_tx);
2876
2877 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2878 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2879 __func__,
2880 pwr_info->lane_rx,
2881 pwr_info->lane_tx);
2882 return -EINVAL;
2883 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302884
2885 /*
2886 * First, get the maximum gears of HS speed.
2887 * If a zero value, it means there is no HSGEAR capability.
2888 * Then, get the maximum gears of PWM speed.
2889 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002890 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2891 if (!pwr_info->gear_rx) {
2892 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2893 &pwr_info->gear_rx);
2894 if (!pwr_info->gear_rx) {
2895 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2896 __func__, pwr_info->gear_rx);
2897 return -EINVAL;
2898 }
2899 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302900 }
2901
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002902 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2903 &pwr_info->gear_tx);
2904 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302905 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002906 &pwr_info->gear_tx);
2907 if (!pwr_info->gear_tx) {
2908 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2909 __func__, pwr_info->gear_tx);
2910 return -EINVAL;
2911 }
2912 pwr_info->pwr_tx = SLOWAUTO_MODE;
2913 }
2914
2915 hba->max_pwr_info.is_valid = true;
2916 return 0;
2917}
2918
2919static int ufshcd_change_power_mode(struct ufs_hba *hba,
2920 struct ufs_pa_layer_attr *pwr_mode)
2921{
2922 int ret;
2923
2924 /* if already configured to the requested pwr_mode */
2925 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2926 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2927 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2928 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2929 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2930 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2931 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2932 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2933 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302934 }
2935
2936 /*
2937 * Configure attributes for power mode change with below.
2938 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2939 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2940 * - PA_HSSERIES
2941 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002942 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2943 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2944 pwr_mode->lane_rx);
2945 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2946 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302947 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002948 else
2949 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302950
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002951 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2952 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2953 pwr_mode->lane_tx);
2954 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2955 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302956 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002957 else
2958 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302959
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002960 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2961 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2962 pwr_mode->pwr_rx == FAST_MODE ||
2963 pwr_mode->pwr_tx == FAST_MODE)
2964 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2965 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302966
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002967 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2968 | pwr_mode->pwr_tx);
2969
2970 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302971 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002972 "%s: power mode change failed %d\n", __func__, ret);
2973 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002974 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2975 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002976
2977 memcpy(&hba->pwr_info, pwr_mode,
2978 sizeof(struct ufs_pa_layer_attr));
2979 }
2980
2981 return ret;
2982}
2983
2984/**
2985 * ufshcd_config_pwr_mode - configure a new power mode
2986 * @hba: per-adapter instance
2987 * @desired_pwr_mode: desired power configuration
2988 */
2989static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2990 struct ufs_pa_layer_attr *desired_pwr_mode)
2991{
2992 struct ufs_pa_layer_attr final_params = { 0 };
2993 int ret;
2994
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002995 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2996 desired_pwr_mode, &final_params);
2997
2998 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002999 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
3000
3001 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303002
3003 return ret;
3004}
3005
3006/**
Dolev Raviv68078d52013-07-30 00:35:58 +05303007 * ufshcd_complete_dev_init() - checks device readiness
3008 * hba: per-adapter instance
3009 *
3010 * Set fDeviceInit flag and poll until device toggles it.
3011 */
3012static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3013{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003014 int i;
3015 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303016 bool flag_res = 1;
3017
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003018 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3019 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303020 if (err) {
3021 dev_err(hba->dev,
3022 "%s setting fDeviceInit flag failed with error %d\n",
3023 __func__, err);
3024 goto out;
3025 }
3026
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003027 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3028 for (i = 0; i < 1000 && !err && flag_res; i++)
3029 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3030 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3031
Dolev Raviv68078d52013-07-30 00:35:58 +05303032 if (err)
3033 dev_err(hba->dev,
3034 "%s reading fDeviceInit flag failed with error %d\n",
3035 __func__, err);
3036 else if (flag_res)
3037 dev_err(hba->dev,
3038 "%s fDeviceInit was not cleared by the device\n",
3039 __func__);
3040
3041out:
3042 return err;
3043}
3044
3045/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303046 * ufshcd_make_hba_operational - Make UFS controller operational
3047 * @hba: per adapter instance
3048 *
3049 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003050 * 1. Enable required interrupts
3051 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003052 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003053 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303054 *
3055 * Returns 0 on success, non-zero value on failure
3056 */
3057static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3058{
3059 int err = 0;
3060 u32 reg;
3061
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303062 /* Enable required interrupts */
3063 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3064
3065 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003066 if (ufshcd_is_intr_aggr_allowed(hba))
3067 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3068 else
3069 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303070
3071 /* Configure UTRL and UTMRL base address registers */
3072 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3073 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3074 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3075 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3076 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3077 REG_UTP_TASK_REQ_LIST_BASE_L);
3078 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3079 REG_UTP_TASK_REQ_LIST_BASE_H);
3080
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303081 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003082 * Make sure base address and interrupt setup are updated before
3083 * enabling the run/stop registers below.
3084 */
3085 wmb();
3086
3087 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303088 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303089 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003090 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303091 if (!(ufshcd_get_lists_status(reg))) {
3092 ufshcd_enable_run_stop_reg(hba);
3093 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303094 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303095 "Host controller not ready to process requests");
3096 err = -EIO;
3097 goto out;
3098 }
3099
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303100out:
3101 return err;
3102}
3103
3104/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003105 * ufshcd_hba_stop - Send controller to reset state
3106 * @hba: per adapter instance
3107 * @can_sleep: perform sleep or just spin
3108 */
3109static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3110{
3111 int err;
3112
3113 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3114 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3115 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3116 10, 1, can_sleep);
3117 if (err)
3118 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3119}
3120
3121/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303122 * ufshcd_hba_enable - initialize the controller
3123 * @hba: per adapter instance
3124 *
3125 * The controller resets itself and controller firmware initialization
3126 * sequence kicks off. When controller is ready it will set
3127 * the Host Controller Enable bit to 1.
3128 *
3129 * Returns 0 on success, non-zero value on failure
3130 */
3131static int ufshcd_hba_enable(struct ufs_hba *hba)
3132{
3133 int retry;
3134
3135 /*
3136 * msleep of 1 and 5 used in this function might result in msleep(20),
3137 * but it was necessary to send the UFS FPGA to reset mode during
3138 * development and testing of this driver. msleep can be changed to
3139 * mdelay and retry count can be reduced based on the controller.
3140 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003141 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303142 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003143 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303144
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003145 /* UniPro link is disabled at this point */
3146 ufshcd_set_link_off(hba);
3147
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003148 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003149
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303150 /* start controller initialization sequence */
3151 ufshcd_hba_start(hba);
3152
3153 /*
3154 * To initialize a UFS host controller HCE bit must be set to 1.
3155 * During initialization the HCE bit value changes from 1->0->1.
3156 * When the host controller completes initialization sequence
3157 * it sets the value of HCE bit to 1. The same HCE bit is read back
3158 * to check if the controller has completed initialization sequence.
3159 * So without this delay the value HCE = 1, set in the previous
3160 * instruction might be read back.
3161 * This delay can be changed based on the controller.
3162 */
3163 msleep(1);
3164
3165 /* wait for the host controller to complete initialization */
3166 retry = 10;
3167 while (ufshcd_is_hba_active(hba)) {
3168 if (retry) {
3169 retry--;
3170 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303171 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303172 "Controller enable failed\n");
3173 return -EIO;
3174 }
3175 msleep(5);
3176 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003177
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003178 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003179 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003180
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003181 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003182
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303183 return 0;
3184}
3185
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003186static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3187{
3188 int tx_lanes, i, err = 0;
3189
3190 if (!peer)
3191 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3192 &tx_lanes);
3193 else
3194 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3195 &tx_lanes);
3196 for (i = 0; i < tx_lanes; i++) {
3197 if (!peer)
3198 err = ufshcd_dme_set(hba,
3199 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3200 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3201 0);
3202 else
3203 err = ufshcd_dme_peer_set(hba,
3204 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3205 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3206 0);
3207 if (err) {
3208 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3209 __func__, peer, i, err);
3210 break;
3211 }
3212 }
3213
3214 return err;
3215}
3216
3217static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3218{
3219 return ufshcd_disable_tx_lcc(hba, true);
3220}
3221
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303222/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303223 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303224 * @hba: per adapter instance
3225 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303226 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303227 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303228static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303229{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303230 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003231 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003232 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303233
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003234 /*
3235 * If UFS device isn't active then we will have to issue link startup
3236 * 2 times to make sure the device state move to active.
3237 */
3238 if (!ufshcd_is_ufs_dev_active(hba))
3239 link_startup_again = true;
3240
3241link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003242 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003243 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303244
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003245 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003246
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003247 /* check if device is detected by inter-connect layer */
3248 if (!ret && !ufshcd_is_device_present(hba)) {
3249 dev_err(hba->dev, "%s: Device not present\n", __func__);
3250 ret = -ENXIO;
3251 goto out;
3252 }
3253
3254 /*
3255 * DME link lost indication is only received when link is up,
3256 * but we can't be sure if the link is up until link startup
3257 * succeeds. So reset the local Uni-Pro and try again.
3258 */
3259 if (ret && ufshcd_hba_enable(hba))
3260 goto out;
3261 } while (ret && retries--);
3262
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303263 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003264 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303265 goto out;
3266
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003267 if (link_startup_again) {
3268 link_startup_again = false;
3269 retries = DME_LINKSTARTUP_RETRIES;
3270 goto link_startup;
3271 }
3272
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003273 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3274 ret = ufshcd_disable_device_tx_lcc(hba);
3275 if (ret)
3276 goto out;
3277 }
3278
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003279 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003280 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3281 if (ret)
3282 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003283
3284 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303285out:
3286 if (ret)
3287 dev_err(hba->dev, "link startup failed %d\n", ret);
3288 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303289}
3290
3291/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303292 * ufshcd_verify_dev_init() - Verify device initialization
3293 * @hba: per-adapter instance
3294 *
3295 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3296 * device Transport Protocol (UTP) layer is ready after a reset.
3297 * If the UTP layer at the device side is not initialized, it may
3298 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3299 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3300 */
3301static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3302{
3303 int err = 0;
3304 int retries;
3305
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003306 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303307 mutex_lock(&hba->dev_cmd.lock);
3308 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3309 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3310 NOP_OUT_TIMEOUT);
3311
3312 if (!err || err == -ETIMEDOUT)
3313 break;
3314
3315 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3316 }
3317 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003318 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303319
3320 if (err)
3321 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3322 return err;
3323}
3324
3325/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003326 * ufshcd_set_queue_depth - set lun queue depth
3327 * @sdev: pointer to SCSI device
3328 *
3329 * Read bLUQueueDepth value and activate scsi tagged command
3330 * queueing. For WLUN, queue depth is set to 1. For best-effort
3331 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3332 * value that host can queue.
3333 */
3334static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3335{
3336 int ret = 0;
3337 u8 lun_qdepth;
3338 struct ufs_hba *hba;
3339
3340 hba = shost_priv(sdev->host);
3341
3342 lun_qdepth = hba->nutrs;
3343 ret = ufshcd_read_unit_desc_param(hba,
3344 ufshcd_scsi_to_upiu_lun(sdev->lun),
3345 UNIT_DESC_PARAM_LU_Q_DEPTH,
3346 &lun_qdepth,
3347 sizeof(lun_qdepth));
3348
3349 /* Some WLUN doesn't support unit descriptor */
3350 if (ret == -EOPNOTSUPP)
3351 lun_qdepth = 1;
3352 else if (!lun_qdepth)
3353 /* eventually, we can figure out the real queue depth */
3354 lun_qdepth = hba->nutrs;
3355 else
3356 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3357
3358 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3359 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003360 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003361}
3362
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003363/*
3364 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3365 * @hba: per-adapter instance
3366 * @lun: UFS device lun id
3367 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3368 *
3369 * Returns 0 in case of success and b_lu_write_protect status would be returned
3370 * @b_lu_write_protect parameter.
3371 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3372 * Returns -EINVAL in case of invalid parameters passed to this function.
3373 */
3374static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3375 u8 lun,
3376 u8 *b_lu_write_protect)
3377{
3378 int ret;
3379
3380 if (!b_lu_write_protect)
3381 ret = -EINVAL;
3382 /*
3383 * According to UFS device spec, RPMB LU can't be write
3384 * protected so skip reading bLUWriteProtect parameter for
3385 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3386 */
3387 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3388 ret = -ENOTSUPP;
3389 else
3390 ret = ufshcd_read_unit_desc_param(hba,
3391 lun,
3392 UNIT_DESC_PARAM_LU_WR_PROTECT,
3393 b_lu_write_protect,
3394 sizeof(*b_lu_write_protect));
3395 return ret;
3396}
3397
3398/**
3399 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3400 * status
3401 * @hba: per-adapter instance
3402 * @sdev: pointer to SCSI device
3403 *
3404 */
3405static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3406 struct scsi_device *sdev)
3407{
3408 if (hba->dev_info.f_power_on_wp_en &&
3409 !hba->dev_info.is_lu_power_on_wp) {
3410 u8 b_lu_write_protect;
3411
3412 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3413 &b_lu_write_protect) &&
3414 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3415 hba->dev_info.is_lu_power_on_wp = true;
3416 }
3417}
3418
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003419/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303420 * ufshcd_slave_alloc - handle initial SCSI device configurations
3421 * @sdev: pointer to SCSI device
3422 *
3423 * Returns success
3424 */
3425static int ufshcd_slave_alloc(struct scsi_device *sdev)
3426{
3427 struct ufs_hba *hba;
3428
3429 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303430
3431 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3432 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303433
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303434 /* allow SCSI layer to restart the device in case of errors */
3435 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003436
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003437 /* REPORT SUPPORTED OPERATION CODES is not supported */
3438 sdev->no_report_opcodes = 1;
3439
Sujit Reddy Thumma9760af42018-01-24 09:52:35 +05303440 /* WRITE_SAME command is not supported */
3441 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003442
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003443 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003444
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003445 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3446
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003447 return 0;
3448}
3449
3450/**
3451 * ufshcd_change_queue_depth - change queue depth
3452 * @sdev: pointer to SCSI device
3453 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003454 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003455 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003456 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003457static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003458{
3459 struct ufs_hba *hba = shost_priv(sdev->host);
3460
3461 if (depth > hba->nutrs)
3462 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003463 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303464}
3465
3466/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003467 * ufshcd_slave_configure - adjust SCSI device configurations
3468 * @sdev: pointer to SCSI device
3469 */
3470static int ufshcd_slave_configure(struct scsi_device *sdev)
3471{
3472 struct request_queue *q = sdev->request_queue;
3473
3474 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3475 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3476
3477 return 0;
3478}
3479
3480/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303481 * ufshcd_slave_destroy - remove SCSI device configurations
3482 * @sdev: pointer to SCSI device
3483 */
3484static void ufshcd_slave_destroy(struct scsi_device *sdev)
3485{
3486 struct ufs_hba *hba;
3487
3488 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003489 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003490 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3491 unsigned long flags;
3492
3493 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003494 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003495 spin_unlock_irqrestore(hba->host->host_lock, flags);
3496 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303497}
3498
3499/**
3500 * ufshcd_task_req_compl - handle task management request completion
3501 * @hba: per adapter instance
3502 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303503 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303504 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303505 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303506 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303507static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303508{
3509 struct utp_task_req_desc *task_req_descp;
3510 struct utp_upiu_task_rsp *task_rsp_upiup;
3511 unsigned long flags;
3512 int ocs_value;
3513 int task_result;
3514
3515 spin_lock_irqsave(hba->host->host_lock, flags);
3516
3517 /* Clear completed tasks from outstanding_tasks */
3518 __clear_bit(index, &hba->outstanding_tasks);
3519
3520 task_req_descp = hba->utmrdl_base_addr;
3521 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3522
3523 if (ocs_value == OCS_SUCCESS) {
3524 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3525 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003526 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3527 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303528 if (resp)
3529 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303530 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303531 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3532 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303533 }
3534 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303535
3536 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303537}
3538
3539/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303540 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3541 * @lrb: pointer to local reference block of completed command
3542 * @scsi_status: SCSI command status
3543 *
3544 * Returns value base on SCSI command status
3545 */
3546static inline int
3547ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3548{
3549 int result = 0;
3550
3551 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303552 case SAM_STAT_CHECK_CONDITION:
3553 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303554 case SAM_STAT_GOOD:
3555 result |= DID_OK << 16 |
3556 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303557 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303558 break;
3559 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303560 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303561 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303562 ufshcd_copy_sense_data(lrbp);
3563 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303564 break;
3565 default:
3566 result |= DID_ERROR << 16;
3567 break;
3568 } /* end of switch */
3569
3570 return result;
3571}
3572
3573/**
3574 * ufshcd_transfer_rsp_status - Get overall status of the response
3575 * @hba: per adapter instance
3576 * @lrb: pointer to local reference block of completed command
3577 *
3578 * Returns result of the command to notify SCSI midlayer
3579 */
3580static inline int
3581ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3582{
3583 int result = 0;
3584 int scsi_status;
3585 int ocs;
3586
3587 /* overall command status of utrd */
3588 ocs = ufshcd_get_tr_ocs(lrbp);
3589
3590 switch (ocs) {
3591 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303592 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303593
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303594 switch (result) {
3595 case UPIU_TRANSACTION_RESPONSE:
3596 /*
3597 * get the response UPIU result to extract
3598 * the SCSI command status
3599 */
3600 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3601
3602 /*
3603 * get the result based on SCSI status response
3604 * to notify the SCSI midlayer of the command status
3605 */
3606 scsi_status = result & MASK_SCSI_STATUS;
3607 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303608
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003609 /*
3610 * Currently we are only supporting BKOPs exception
3611 * events hence we can ignore BKOPs exception event
3612 * during power management callbacks. BKOPs exception
3613 * event is not expected to be raised in runtime suspend
3614 * callback as it allows the urgent bkops.
3615 * During system suspend, we are anyway forcefully
3616 * disabling the bkops and if urgent bkops is needed
3617 * it will be enabled on system resume. Long term
3618 * solution could be to abort the system suspend if
3619 * UFS device needs urgent BKOPs.
3620 */
3621 if (!hba->pm_op_in_progress &&
3622 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303623 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303624 break;
3625 case UPIU_TRANSACTION_REJECT_UPIU:
3626 /* TODO: handle Reject UPIU Response */
3627 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303628 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303629 "Reject UPIU not fully implemented\n");
3630 break;
3631 default:
3632 result = DID_ERROR << 16;
3633 dev_err(hba->dev,
3634 "Unexpected request response code = %x\n",
3635 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303636 break;
3637 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303638 break;
3639 case OCS_ABORTED:
3640 result |= DID_ABORT << 16;
3641 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303642 case OCS_INVALID_COMMAND_STATUS:
3643 result |= DID_REQUEUE << 16;
3644 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303645 case OCS_INVALID_CMD_TABLE_ATTR:
3646 case OCS_INVALID_PRDT_ATTR:
3647 case OCS_MISMATCH_DATA_BUF_SIZE:
3648 case OCS_MISMATCH_RESP_UPIU_SIZE:
3649 case OCS_PEER_COMM_FAILURE:
3650 case OCS_FATAL_ERROR:
3651 default:
3652 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303653 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303654 "OCS error from controller = %x\n", ocs);
3655 break;
3656 } /* end of switch */
3657
3658 return result;
3659}
3660
3661/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303662 * ufshcd_uic_cmd_compl - handle completion of uic command
3663 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303664 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303665 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303666static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303667{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303668 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303669 hba->active_uic_cmd->argument2 |=
3670 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303671 hba->active_uic_cmd->argument3 =
3672 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303673 complete(&hba->active_uic_cmd->done);
3674 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303675
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003676 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3677 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303678}
3679
3680/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003681 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303682 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003683 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303684 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003685static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3686 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303687{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303688 struct ufshcd_lrb *lrbp;
3689 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303690 int result;
3691 int index;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003692 struct request *req;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003693
Dolev Ravive9d501b2014-07-01 12:22:37 +03003694 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3695 lrbp = &hba->lrb[index];
3696 cmd = lrbp->cmd;
3697 if (cmd) {
3698 result = ufshcd_transfer_rsp_status(hba, lrbp);
3699 scsi_dma_unmap(cmd);
3700 cmd->result = result;
3701 /* Mark completed command as NULL in LRB */
3702 lrbp->cmd = NULL;
3703 clear_bit_unlock(index, &hba->lrb_in_use);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003704 req = cmd->request;
3705 if (req) {
3706 /* Update IO svc time latency histogram */
3707 if (req->lat_hist_enabled) {
3708 ktime_t completion;
3709 u_int64_t delta_us;
3710
3711 completion = ktime_get();
3712 delta_us = ktime_us_delta(completion,
3713 req->lat_hist_io_start);
Hyojun Kim11537d52017-12-21 09:57:41 -08003714 blk_update_latency_hist(
3715 (rq_data_dir(req) == READ) ?
3716 &hba->io_lat_read :
3717 &hba->io_lat_write, delta_us);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08003718 }
3719 }
Dolev Ravive9d501b2014-07-01 12:22:37 +03003720 /* Do not touch lrbp after scsi done */
3721 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003722 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003723 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3724 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003725 if (hba->dev_cmd.complete)
3726 complete(hba->dev_cmd.complete);
3727 }
3728 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303729
3730 /* clear corresponding bits of completed commands */
3731 hba->outstanding_reqs ^= completed_reqs;
3732
Sahitya Tummala856b3482014-09-25 15:32:34 +03003733 ufshcd_clk_scaling_update_busy(hba);
3734
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303735 /* we might have free'd some tags above */
3736 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303737}
3738
3739/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003740 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3741 * @hba: per adapter instance
3742 */
3743static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3744{
3745 unsigned long completed_reqs;
3746 u32 tr_doorbell;
3747
3748 /* Resetting interrupt aggregation counters first and reading the
3749 * DOOR_BELL afterward allows us to handle all the completed requests.
3750 * In order to prevent other interrupts starvation the DB is read once
3751 * after reset. The down side of this solution is the possibility of
3752 * false interrupt if device completes another request after resetting
3753 * aggregation and before reading the DB.
3754 */
3755 if (ufshcd_is_intr_aggr_allowed(hba))
3756 ufshcd_reset_intr_aggr(hba);
3757
3758 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3759 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3760
3761 __ufshcd_transfer_req_compl(hba, completed_reqs);
3762}
3763
3764/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303765 * ufshcd_disable_ee - disable exception event
3766 * @hba: per-adapter instance
3767 * @mask: exception event to disable
3768 *
3769 * Disables exception event in the device so that the EVENT_ALERT
3770 * bit is not set.
3771 *
3772 * Returns zero on success, non-zero error value on failure.
3773 */
3774static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3775{
3776 int err = 0;
3777 u32 val;
3778
3779 if (!(hba->ee_ctrl_mask & mask))
3780 goto out;
3781
3782 val = hba->ee_ctrl_mask & ~mask;
3783 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003784 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303785 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3786 if (!err)
3787 hba->ee_ctrl_mask &= ~mask;
3788out:
3789 return err;
3790}
3791
3792/**
3793 * ufshcd_enable_ee - enable exception event
3794 * @hba: per-adapter instance
3795 * @mask: exception event to enable
3796 *
3797 * Enable corresponding exception event in the device to allow
3798 * device to alert host in critical scenarios.
3799 *
3800 * Returns zero on success, non-zero error value on failure.
3801 */
3802static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3803{
3804 int err = 0;
3805 u32 val;
3806
3807 if (hba->ee_ctrl_mask & mask)
3808 goto out;
3809
3810 val = hba->ee_ctrl_mask | mask;
3811 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003812 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303813 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3814 if (!err)
3815 hba->ee_ctrl_mask |= mask;
3816out:
3817 return err;
3818}
3819
3820/**
3821 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3822 * @hba: per-adapter instance
3823 *
3824 * Allow device to manage background operations on its own. Enabling
3825 * this might lead to inconsistent latencies during normal data transfers
3826 * as the device is allowed to manage its own way of handling background
3827 * operations.
3828 *
3829 * Returns zero on success, non-zero on failure.
3830 */
3831static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3832{
3833 int err = 0;
3834
3835 if (hba->auto_bkops_enabled)
3836 goto out;
3837
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003838 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303839 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3840 if (err) {
3841 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3842 __func__, err);
3843 goto out;
3844 }
3845
3846 hba->auto_bkops_enabled = true;
3847
3848 /* No need of URGENT_BKOPS exception from the device */
3849 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3850 if (err)
3851 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3852 __func__, err);
3853out:
3854 return err;
3855}
3856
3857/**
3858 * ufshcd_disable_auto_bkops - block device in doing background operations
3859 * @hba: per-adapter instance
3860 *
3861 * Disabling background operations improves command response latency but
3862 * has drawback of device moving into critical state where the device is
3863 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3864 * host is idle so that BKOPS are managed effectively without any negative
3865 * impacts.
3866 *
3867 * Returns zero on success, non-zero on failure.
3868 */
3869static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3870{
3871 int err = 0;
3872
3873 if (!hba->auto_bkops_enabled)
3874 goto out;
3875
3876 /*
3877 * If host assisted BKOPs is to be enabled, make sure
3878 * urgent bkops exception is allowed.
3879 */
3880 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3881 if (err) {
3882 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3883 __func__, err);
3884 goto out;
3885 }
3886
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003887 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303888 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3889 if (err) {
3890 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3891 __func__, err);
3892 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3893 goto out;
3894 }
3895
3896 hba->auto_bkops_enabled = false;
3897out:
3898 return err;
3899}
3900
3901/**
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003902 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303903 * @hba: per adapter instance
3904 *
3905 * After a device reset the device may toggle the BKOPS_EN flag
3906 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003907 * as well. This function would change the auto-bkops state based on
3908 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303909 */
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003910static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303911{
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003912 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
3913 hba->auto_bkops_enabled = false;
3914 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3915 ufshcd_enable_auto_bkops(hba);
3916 } else {
3917 hba->auto_bkops_enabled = true;
3918 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
3919 ufshcd_disable_auto_bkops(hba);
3920 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303921}
3922
3923static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3924{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003925 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303926 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3927}
3928
3929/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003930 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3931 * @hba: per-adapter instance
3932 * @status: bkops_status value
3933 *
3934 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3935 * flag in the device to permit background operations if the device
3936 * bkops_status is greater than or equal to "status" argument passed to
3937 * this function, disable otherwise.
3938 *
3939 * Returns 0 for success, non-zero in case of failure.
3940 *
3941 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3942 * to know whether auto bkops is enabled or disabled after this function
3943 * returns control to it.
3944 */
3945static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3946 enum bkops_status status)
3947{
3948 int err;
3949 u32 curr_status = 0;
3950
3951 err = ufshcd_get_bkops_status(hba, &curr_status);
3952 if (err) {
3953 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3954 __func__, err);
3955 goto out;
3956 } else if (curr_status > BKOPS_STATUS_MAX) {
3957 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3958 __func__, curr_status);
3959 err = -EINVAL;
3960 goto out;
3961 }
3962
3963 if (curr_status >= status)
3964 err = ufshcd_enable_auto_bkops(hba);
3965 else
3966 err = ufshcd_disable_auto_bkops(hba);
3967out:
3968 return err;
3969}
3970
3971/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303972 * ufshcd_urgent_bkops - handle urgent bkops exception event
3973 * @hba: per-adapter instance
3974 *
3975 * Enable fBackgroundOpsEn flag in the device to permit background
3976 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003977 *
3978 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3979 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303980 */
3981static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3982{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003983 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303984}
3985
3986static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3987{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003988 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303989 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3990}
3991
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003992static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3993{
3994 int err;
3995 u32 curr_status = 0;
3996
3997 if (hba->is_urgent_bkops_lvl_checked)
3998 goto enable_auto_bkops;
3999
4000 err = ufshcd_get_bkops_status(hba, &curr_status);
4001 if (err) {
4002 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
4003 __func__, err);
4004 goto out;
4005 }
4006
4007 /*
4008 * We are seeing that some devices are raising the urgent bkops
4009 * exception events even when BKOPS status doesn't indicate performace
4010 * impacted or critical. Handle these device by determining their urgent
4011 * bkops status at runtime.
4012 */
4013 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
4014 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4015 __func__, curr_status);
4016 /* update the current status as the urgent bkops level */
4017 hba->urgent_bkops_lvl = curr_status;
4018 hba->is_urgent_bkops_lvl_checked = true;
4019 }
4020
4021enable_auto_bkops:
4022 err = ufshcd_enable_auto_bkops(hba);
4023out:
4024 if (err < 0)
4025 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
4026 __func__, err);
4027}
4028
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304029/**
4030 * ufshcd_exception_event_handler - handle exceptions raised by device
4031 * @work: pointer to work data
4032 *
4033 * Read bExceptionEventStatus attribute from the device and handle the
4034 * exception event accordingly.
4035 */
4036static void ufshcd_exception_event_handler(struct work_struct *work)
4037{
4038 struct ufs_hba *hba;
4039 int err;
4040 u32 status = 0;
4041 hba = container_of(work, struct ufs_hba, eeh_work);
4042
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304043 pm_runtime_get_sync(hba->dev);
Maya Erez62413ba2018-05-03 16:37:16 +05304044 scsi_block_requests(hba->host);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304045 err = ufshcd_get_ee_status(hba, &status);
4046 if (err) {
4047 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4048 __func__, err);
4049 goto out;
4050 }
4051
4052 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004053
4054 if (status & MASK_EE_URGENT_BKOPS)
4055 ufshcd_bkops_exception_event_handler(hba);
4056
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304057out:
Maya Erez62413ba2018-05-03 16:37:16 +05304058 scsi_unblock_requests(hba->host);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304059 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304060 return;
4061}
4062
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004063/* Complete requests that have door-bell cleared */
4064static void ufshcd_complete_requests(struct ufs_hba *hba)
4065{
4066 ufshcd_transfer_req_compl(hba);
4067 ufshcd_tmc_handler(hba);
4068}
4069
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304070/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004071 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4072 * to recover from the DL NAC errors or not.
4073 * @hba: per-adapter instance
4074 *
4075 * Returns true if error handling is required, false otherwise
4076 */
4077static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4078{
4079 unsigned long flags;
4080 bool err_handling = true;
4081
4082 spin_lock_irqsave(hba->host->host_lock, flags);
4083 /*
4084 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4085 * device fatal error and/or DL NAC & REPLAY timeout errors.
4086 */
4087 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4088 goto out;
4089
4090 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4091 ((hba->saved_err & UIC_ERROR) &&
4092 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4093 goto out;
4094
4095 if ((hba->saved_err & UIC_ERROR) &&
4096 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4097 int err;
4098 /*
4099 * wait for 50ms to see if we can get any other errors or not.
4100 */
4101 spin_unlock_irqrestore(hba->host->host_lock, flags);
4102 msleep(50);
4103 spin_lock_irqsave(hba->host->host_lock, flags);
4104
4105 /*
4106 * now check if we have got any other severe errors other than
4107 * DL NAC error?
4108 */
4109 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4110 ((hba->saved_err & UIC_ERROR) &&
4111 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4112 goto out;
4113
4114 /*
4115 * As DL NAC is the only error received so far, send out NOP
4116 * command to confirm if link is still active or not.
4117 * - If we don't get any response then do error recovery.
4118 * - If we get response then clear the DL NAC error bit.
4119 */
4120
4121 spin_unlock_irqrestore(hba->host->host_lock, flags);
4122 err = ufshcd_verify_dev_init(hba);
4123 spin_lock_irqsave(hba->host->host_lock, flags);
4124
4125 if (err)
4126 goto out;
4127
4128 /* Link seems to be alive hence ignore the DL NAC errors */
4129 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4130 hba->saved_err &= ~UIC_ERROR;
4131 /* clear NAC error */
4132 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4133 if (!hba->saved_uic_err) {
4134 err_handling = false;
4135 goto out;
4136 }
4137 }
4138out:
4139 spin_unlock_irqrestore(hba->host->host_lock, flags);
4140 return err_handling;
4141}
4142
4143/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304144 * ufshcd_err_handler - handle UFS errors that require s/w attention
4145 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304146 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304147static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304148{
4149 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304150 unsigned long flags;
4151 u32 err_xfer = 0;
4152 u32 err_tm = 0;
4153 int err = 0;
4154 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004155 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304156
4157 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304158
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304159 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004160 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304161
4162 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004163 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304164 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304165
4166 hba->ufshcd_state = UFSHCD_STATE_RESET;
4167 ufshcd_set_eh_in_progress(hba);
4168
4169 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004170 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004171
4172 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4173 bool ret;
4174
4175 spin_unlock_irqrestore(hba->host->host_lock, flags);
4176 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4177 ret = ufshcd_quirk_dl_nac_errors(hba);
4178 spin_lock_irqsave(hba->host->host_lock, flags);
4179 if (!ret)
4180 goto skip_err_handling;
4181 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004182 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4183 ((hba->saved_err & UIC_ERROR) &&
4184 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4185 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4186 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4187 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304188
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004189 /*
4190 * if host reset is required then skip clearing the pending
4191 * transfers forcefully because they will automatically get
4192 * cleared after link startup.
4193 */
4194 if (needs_reset)
4195 goto skip_pending_xfer_clear;
4196
4197 /* release lock as clear command might sleep */
4198 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304199 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004200 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4201 if (ufshcd_clear_cmd(hba, tag)) {
4202 err_xfer = true;
4203 goto lock_skip_pending_xfer_clear;
4204 }
4205 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304206
4207 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004208 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4209 if (ufshcd_clear_tm_cmd(hba, tag)) {
4210 err_tm = true;
4211 goto lock_skip_pending_xfer_clear;
4212 }
4213 }
4214
4215lock_skip_pending_xfer_clear:
4216 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304217
4218 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004219 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304220
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004221 if (err_xfer || err_tm)
4222 needs_reset = true;
4223
4224skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304225 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004226 if (needs_reset) {
4227 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4228
4229 /*
4230 * ufshcd_reset_and_restore() does the link reinitialization
4231 * which will need atleast one empty doorbell slot to send the
4232 * device management commands (NOP and query commands).
4233 * If there is no slot empty at this moment then free up last
4234 * slot forcefully.
4235 */
4236 if (hba->outstanding_reqs == max_doorbells)
4237 __ufshcd_transfer_req_compl(hba,
4238 (1UL << (hba->nutrs - 1)));
4239
4240 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304241 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004242 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304243 if (err) {
4244 dev_err(hba->dev, "%s: reset and restore failed\n",
4245 __func__);
4246 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4247 }
4248 /*
4249 * Inform scsi mid-layer that we did reset and allow to handle
4250 * Unit Attention properly.
4251 */
4252 scsi_report_bus_reset(hba->host, 0);
4253 hba->saved_err = 0;
4254 hba->saved_uic_err = 0;
4255 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004256
Yaniv Gardi583fa622016-03-10 17:37:13 +02004257skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004258 if (!needs_reset) {
4259 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4260 if (hba->saved_err || hba->saved_uic_err)
4261 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4262 __func__, hba->saved_err, hba->saved_uic_err);
4263 }
4264
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304265 ufshcd_clear_eh_in_progress(hba);
4266
4267out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004268 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304269 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004270 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304271 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304272}
4273
4274/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304275 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4276 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304277 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304278static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304279{
4280 u32 reg;
4281
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304282 /* PA_INIT_ERROR is fatal and needs UIC reset */
4283 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4284 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4285 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004286 else if (hba->dev_quirks &
4287 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4288 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4289 hba->uic_error |=
4290 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4291 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4292 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4293 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304294
4295 /* UIC NL/TL/DME errors needs software retry */
4296 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4297 if (reg)
4298 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4299
4300 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4301 if (reg)
4302 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4303
4304 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4305 if (reg)
4306 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4307
4308 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4309 __func__, hba->uic_error);
4310}
4311
4312/**
4313 * ufshcd_check_errors - Check for errors that need s/w attention
4314 * @hba: per-adapter instance
4315 */
4316static void ufshcd_check_errors(struct ufs_hba *hba)
4317{
4318 bool queue_eh_work = false;
4319
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304320 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304321 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304322
4323 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304324 hba->uic_error = 0;
4325 ufshcd_update_uic_error(hba);
4326 if (hba->uic_error)
4327 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304328 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304329
4330 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004331 /*
4332 * update the transfer error masks to sticky bits, let's do this
4333 * irrespective of current ufshcd_state.
4334 */
4335 hba->saved_err |= hba->errors;
4336 hba->saved_uic_err |= hba->uic_error;
4337
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304338 /* handle fatal errors only when link is functional */
4339 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4340 /* block commands from scsi mid-layer */
4341 scsi_block_requests(hba->host);
4342
Zang Leiganga17bddc2017-04-04 19:32:20 +00004343 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304344 schedule_work(&hba->eh_work);
4345 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304346 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304347 /*
4348 * if (!queue_eh_work) -
4349 * Other errors are either non-fatal where host recovers
4350 * itself without s/w intervention or errors that will be
4351 * handled by the SCSI core layer.
4352 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304353}
4354
4355/**
4356 * ufshcd_tmc_handler - handle task management function completion
4357 * @hba: per adapter instance
4358 */
4359static void ufshcd_tmc_handler(struct ufs_hba *hba)
4360{
4361 u32 tm_doorbell;
4362
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304363 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304364 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304365 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304366}
4367
4368/**
4369 * ufshcd_sl_intr - Interrupt service routine
4370 * @hba: per adapter instance
4371 * @intr_status: contains interrupts generated by the controller
4372 */
4373static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4374{
4375 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4376 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304377 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304378
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304379 if (intr_status & UFSHCD_UIC_MASK)
4380 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304381
4382 if (intr_status & UTP_TASK_REQ_COMPL)
4383 ufshcd_tmc_handler(hba);
4384
4385 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4386 ufshcd_transfer_req_compl(hba);
4387}
4388
4389/**
4390 * ufshcd_intr - Main interrupt service routine
4391 * @irq: irq number
4392 * @__hba: pointer to adapter instance
4393 *
4394 * Returns IRQ_HANDLED - If interrupt is valid
4395 * IRQ_NONE - If invalid interrupt
4396 */
4397static irqreturn_t ufshcd_intr(int irq, void *__hba)
4398{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004399 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304400 irqreturn_t retval = IRQ_NONE;
4401 struct ufs_hba *hba = __hba;
4402
4403 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304404 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004405 enabled_intr_status =
4406 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304407
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004408 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304409 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004410
4411 if (enabled_intr_status) {
4412 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304413 retval = IRQ_HANDLED;
4414 }
4415 spin_unlock(hba->host->host_lock);
4416 return retval;
4417}
4418
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304419static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4420{
4421 int err = 0;
4422 u32 mask = 1 << tag;
4423 unsigned long flags;
4424
4425 if (!test_bit(tag, &hba->outstanding_tasks))
4426 goto out;
4427
4428 spin_lock_irqsave(hba->host->host_lock, flags);
4429 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4430 spin_unlock_irqrestore(hba->host->host_lock, flags);
4431
4432 /* poll for max. 1 sec to clear door bell register by h/w */
4433 err = ufshcd_wait_for_register(hba,
4434 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004435 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304436out:
4437 return err;
4438}
4439
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304440/**
4441 * ufshcd_issue_tm_cmd - issues task management commands to controller
4442 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304443 * @lun_id: LUN ID to which TM command is sent
4444 * @task_id: task ID to which the TM command is applicable
4445 * @tm_function: task management function opcode
4446 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304447 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304448 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304449 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304450static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4451 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304452{
4453 struct utp_task_req_desc *task_req_descp;
4454 struct utp_upiu_task_req *task_req_upiup;
4455 struct Scsi_Host *host;
4456 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304457 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304458 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304459 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304460
4461 host = hba->host;
4462
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304463 /*
4464 * Get free slot, sleep if slots are unavailable.
4465 * Even though we use wait_event() which sleeps indefinitely,
4466 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4467 */
4468 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004469 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304470
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304471 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304472 task_req_descp = hba->utmrdl_base_addr;
4473 task_req_descp += free_slot;
4474
4475 /* Configure task request descriptor */
4476 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4477 task_req_descp->header.dword_2 =
4478 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4479
4480 /* Configure task request UPIU */
4481 task_req_upiup =
4482 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304483 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304484 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304485 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304486 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304487 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304488 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004489 /*
4490 * The host shall provide the same value for LUN field in the basic
4491 * header and for Input Parameter.
4492 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304493 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4494 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304495
4496 /* send command to the controller */
4497 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004498
4499 /* Make sure descriptors are ready before ringing the task doorbell */
4500 wmb();
4501
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304502 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304503
4504 spin_unlock_irqrestore(host->host_lock, flags);
4505
4506 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304507 err = wait_event_timeout(hba->tm_wq,
4508 test_bit(free_slot, &hba->tm_condition),
4509 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304510 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304511 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4512 __func__, tm_function);
4513 if (ufshcd_clear_tm_cmd(hba, free_slot))
4514 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4515 __func__, free_slot);
4516 err = -ETIMEDOUT;
4517 } else {
4518 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304519 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304520
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304521 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304522 ufshcd_put_tm_slot(hba, free_slot);
4523 wake_up(&hba->tm_tag_wq);
4524
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004525 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304526 return err;
4527}
4528
4529/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304530 * ufshcd_eh_device_reset_handler - device reset handler registered to
4531 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304532 * @cmd: SCSI command pointer
4533 *
4534 * Returns SUCCESS/FAILED
4535 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304536static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304537{
4538 struct Scsi_Host *host;
4539 struct ufs_hba *hba;
4540 unsigned int tag;
4541 u32 pos;
4542 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304543 u8 resp = 0xF;
4544 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304545 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304546
4547 host = cmd->device->host;
4548 hba = shost_priv(host);
4549 tag = cmd->request->tag;
4550
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304551 lrbp = &hba->lrb[tag];
4552 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4553 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304554 if (!err)
4555 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304556 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304557 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304558
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304559 /* clear the commands that were pending for corresponding LUN */
4560 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4561 if (hba->lrb[pos].lun == lrbp->lun) {
4562 err = ufshcd_clear_cmd(hba, pos);
4563 if (err)
4564 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304565 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304566 }
4567 spin_lock_irqsave(host->host_lock, flags);
4568 ufshcd_transfer_req_compl(hba);
4569 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304570out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304571 if (!err) {
4572 err = SUCCESS;
4573 } else {
4574 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4575 err = FAILED;
4576 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304577 return err;
4578}
4579
4580/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304581 * ufshcd_abort - abort a specific command
4582 * @cmd: SCSI command pointer
4583 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304584 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4585 * command, and in host controller by clearing the door-bell register. There can
4586 * be race between controller sending the command to the device while abort is
4587 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4588 * really issued and then try to abort it.
4589 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304590 * Returns SUCCESS/FAILED
4591 */
4592static int ufshcd_abort(struct scsi_cmnd *cmd)
4593{
4594 struct Scsi_Host *host;
4595 struct ufs_hba *hba;
4596 unsigned long flags;
4597 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304598 int err = 0;
4599 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304600 u8 resp = 0xF;
4601 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004602 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304603
4604 host = cmd->device->host;
4605 hba = shost_priv(host);
4606 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004607 if (!ufshcd_valid_tag(hba, tag)) {
4608 dev_err(hba->dev,
4609 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4610 __func__, tag, cmd, cmd->request);
4611 BUG();
4612 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304613
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004614 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004615 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004616 /* If command is already aborted/completed, return SUCCESS */
4617 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4618 dev_err(hba->dev,
4619 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4620 __func__, tag, hba->outstanding_reqs, reg);
4621 goto out;
4622 }
4623
Dolev Ravive9d501b2014-07-01 12:22:37 +03004624 if (!(reg & (1 << tag))) {
4625 dev_err(hba->dev,
4626 "%s: cmd was completed, but without a notifying intr, tag = %d",
4627 __func__, tag);
4628 }
4629
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304630 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304631 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4632 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4633 UFS_QUERY_TASK, &resp);
4634 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4635 /* cmd pending in the device */
4636 break;
4637 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304638 /*
4639 * cmd not pending in the device, check if it is
4640 * in transition.
4641 */
4642 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4643 if (reg & (1 << tag)) {
4644 /* sleep for max. 200us to stabilize */
4645 usleep_range(100, 200);
4646 continue;
4647 }
4648 /* command completed already */
4649 goto out;
4650 } else {
4651 if (!err)
4652 err = resp; /* service response error */
4653 goto out;
4654 }
4655 }
4656
4657 if (!poll_cnt) {
4658 err = -EBUSY;
4659 goto out;
4660 }
4661
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304662 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4663 UFS_ABORT_TASK, &resp);
4664 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304665 if (!err)
4666 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304667 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304668 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304669
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304670 err = ufshcd_clear_cmd(hba, tag);
4671 if (err)
4672 goto out;
4673
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304674 scsi_dma_unmap(cmd);
4675
4676 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004677 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304678 hba->lrb[tag].cmd = NULL;
4679 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304680
4681 clear_bit_unlock(tag, &hba->lrb_in_use);
4682 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004683
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304684out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304685 if (!err) {
4686 err = SUCCESS;
4687 } else {
4688 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4689 err = FAILED;
4690 }
4691
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004692 /*
4693 * This ufshcd_release() corresponds to the original scsi cmd that got
4694 * aborted here (as we won't get any IRQ for it).
4695 */
4696 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304697 return err;
4698}
4699
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304700/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304701 * ufshcd_host_reset_and_restore - reset and restore host controller
4702 * @hba: per-adapter instance
4703 *
4704 * Note that host controller reset may issue DME_RESET to
4705 * local and remote (device) Uni-Pro stack and the attributes
4706 * are reset to default state.
4707 *
4708 * Returns zero on success, non-zero on failure
4709 */
4710static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4711{
4712 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304713 unsigned long flags;
4714
4715 /* Reset the host controller */
4716 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004717 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304718 spin_unlock_irqrestore(hba->host->host_lock, flags);
4719
4720 err = ufshcd_hba_enable(hba);
4721 if (err)
4722 goto out;
4723
4724 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004725 err = ufshcd_probe_hba(hba);
4726
4727 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304728 err = -EIO;
4729out:
4730 if (err)
4731 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4732
4733 return err;
4734}
4735
4736/**
4737 * ufshcd_reset_and_restore - reset and re-initialize host/device
4738 * @hba: per-adapter instance
4739 *
4740 * Reset and recover device, host and re-establish link. This
4741 * is helpful to recover the communication in fatal error conditions.
4742 *
4743 * Returns zero on success, non-zero on failure
4744 */
4745static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4746{
4747 int err = 0;
4748 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004749 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304750
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004751 do {
4752 err = ufshcd_host_reset_and_restore(hba);
4753 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304754
4755 /*
4756 * After reset the door-bell might be cleared, complete
4757 * outstanding requests in s/w here.
4758 */
4759 spin_lock_irqsave(hba->host->host_lock, flags);
4760 ufshcd_transfer_req_compl(hba);
4761 ufshcd_tmc_handler(hba);
4762 spin_unlock_irqrestore(hba->host->host_lock, flags);
4763
4764 return err;
4765}
4766
4767/**
4768 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4769 * @cmd - SCSI command pointer
4770 *
4771 * Returns SUCCESS/FAILED
4772 */
4773static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4774{
4775 int err;
4776 unsigned long flags;
4777 struct ufs_hba *hba;
4778
4779 hba = shost_priv(cmd->device->host);
4780
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004781 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304782 /*
4783 * Check if there is any race with fatal error handling.
4784 * If so, wait for it to complete. Even though fatal error
4785 * handling does reset and restore in some cases, don't assume
4786 * anything out of it. We are just avoiding race here.
4787 */
4788 do {
4789 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304790 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304791 hba->ufshcd_state == UFSHCD_STATE_RESET))
4792 break;
4793 spin_unlock_irqrestore(hba->host->host_lock, flags);
4794 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304795 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304796 } while (1);
4797
4798 hba->ufshcd_state = UFSHCD_STATE_RESET;
4799 ufshcd_set_eh_in_progress(hba);
4800 spin_unlock_irqrestore(hba->host->host_lock, flags);
4801
4802 err = ufshcd_reset_and_restore(hba);
4803
4804 spin_lock_irqsave(hba->host->host_lock, flags);
4805 if (!err) {
4806 err = SUCCESS;
4807 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4808 } else {
4809 err = FAILED;
4810 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4811 }
4812 ufshcd_clear_eh_in_progress(hba);
4813 spin_unlock_irqrestore(hba->host->host_lock, flags);
4814
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004815 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304816 return err;
4817}
4818
4819/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004820 * ufshcd_get_max_icc_level - calculate the ICC level
4821 * @sup_curr_uA: max. current supported by the regulator
4822 * @start_scan: row at the desc table to start scan from
4823 * @buff: power descriptor buffer
4824 *
4825 * Returns calculated max ICC level for specific regulator
4826 */
4827static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4828{
4829 int i;
4830 int curr_uA;
4831 u16 data;
4832 u16 unit;
4833
4834 for (i = start_scan; i >= 0; i--) {
4835 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4836 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4837 ATTR_ICC_LVL_UNIT_OFFSET;
4838 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4839 switch (unit) {
4840 case UFSHCD_NANO_AMP:
4841 curr_uA = curr_uA / 1000;
4842 break;
4843 case UFSHCD_MILI_AMP:
4844 curr_uA = curr_uA * 1000;
4845 break;
4846 case UFSHCD_AMP:
4847 curr_uA = curr_uA * 1000 * 1000;
4848 break;
4849 case UFSHCD_MICRO_AMP:
4850 default:
4851 break;
4852 }
4853 if (sup_curr_uA >= curr_uA)
4854 break;
4855 }
4856 if (i < 0) {
4857 i = 0;
4858 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4859 }
4860
4861 return (u32)i;
4862}
4863
4864/**
4865 * ufshcd_calc_icc_level - calculate the max ICC level
4866 * In case regulators are not initialized we'll return 0
4867 * @hba: per-adapter instance
4868 * @desc_buf: power descriptor buffer to extract ICC levels from.
4869 * @len: length of desc_buff
4870 *
4871 * Returns calculated ICC level
4872 */
4873static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4874 u8 *desc_buf, int len)
4875{
4876 u32 icc_level = 0;
4877
4878 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4879 !hba->vreg_info.vccq2) {
4880 dev_err(hba->dev,
4881 "%s: Regulator capability was not set, actvIccLevel=%d",
4882 __func__, icc_level);
4883 goto out;
4884 }
4885
4886 if (hba->vreg_info.vcc)
4887 icc_level = ufshcd_get_max_icc_level(
4888 hba->vreg_info.vcc->max_uA,
4889 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4890 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4891
4892 if (hba->vreg_info.vccq)
4893 icc_level = ufshcd_get_max_icc_level(
4894 hba->vreg_info.vccq->max_uA,
4895 icc_level,
4896 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4897
4898 if (hba->vreg_info.vccq2)
4899 icc_level = ufshcd_get_max_icc_level(
4900 hba->vreg_info.vccq2->max_uA,
4901 icc_level,
4902 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4903out:
4904 return icc_level;
4905}
4906
4907static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4908{
4909 int ret;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004910 int buff_len = hba->desc_size.pwr_desc;
4911 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004912
4913 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4914 if (ret) {
4915 dev_err(hba->dev,
4916 "%s: Failed reading power descriptor.len = %d ret = %d",
4917 __func__, buff_len, ret);
4918 return;
4919 }
4920
4921 hba->init_prefetch_data.icc_level =
4922 ufshcd_find_max_sup_active_icc_level(hba,
4923 desc_buf, buff_len);
4924 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4925 __func__, hba->init_prefetch_data.icc_level);
4926
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004927 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4928 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4929 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004930
4931 if (ret)
4932 dev_err(hba->dev,
4933 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4934 __func__, hba->init_prefetch_data.icc_level , ret);
4935
4936}
4937
4938/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004939 * ufshcd_scsi_add_wlus - Adds required W-LUs
4940 * @hba: per-adapter instance
4941 *
4942 * UFS device specification requires the UFS devices to support 4 well known
4943 * logical units:
4944 * "REPORT_LUNS" (address: 01h)
4945 * "UFS Device" (address: 50h)
4946 * "RPMB" (address: 44h)
4947 * "BOOT" (address: 30h)
4948 * UFS device's power management needs to be controlled by "POWER CONDITION"
4949 * field of SSU (START STOP UNIT) command. But this "power condition" field
4950 * will take effect only when its sent to "UFS device" well known logical unit
4951 * hence we require the scsi_device instance to represent this logical unit in
4952 * order for the UFS host driver to send the SSU command for power management.
4953
4954 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4955 * Block) LU so user space process can control this LU. User space may also
4956 * want to have access to BOOT LU.
4957
4958 * This function adds scsi device instances for each of all well known LUs
4959 * (except "REPORT LUNS" LU).
4960 *
4961 * Returns zero on success (all required W-LUs are added successfully),
4962 * non-zero error value on failure (if failed to add any of the required W-LU).
4963 */
4964static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4965{
4966 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004967 struct scsi_device *sdev_rpmb;
4968 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004969
4970 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4971 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4972 if (IS_ERR(hba->sdev_ufs_device)) {
4973 ret = PTR_ERR(hba->sdev_ufs_device);
4974 hba->sdev_ufs_device = NULL;
4975 goto out;
4976 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004977 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004978
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004979 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004980 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004981 if (IS_ERR(sdev_boot)) {
4982 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004983 goto remove_sdev_ufs_device;
4984 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004985 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004986
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004987 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004988 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004989 if (IS_ERR(sdev_rpmb)) {
4990 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004991 goto remove_sdev_boot;
4992 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004993 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004994 goto out;
4995
4996remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004997 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004998remove_sdev_ufs_device:
4999 scsi_remove_device(hba->sdev_ufs_device);
5000out:
5001 return ret;
5002}
5003
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005004static int ufs_get_device_desc(struct ufs_hba *hba,
5005 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005006{
5007 int err;
5008 u8 model_index;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005009 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
5010 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005011
Potomski, MichalXe1928452017-02-23 09:05:30 +00005012 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005013 if (err) {
5014 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
5015 __func__, err);
5016 goto out;
5017 }
5018
5019 /*
5020 * getting vendor (manufacturerID) and Bank Index in big endian
5021 * format
5022 */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005023 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005024 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
5025
5026 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
5027
5028 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00005029 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005030 if (err) {
5031 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5032 __func__, err);
5033 goto out;
5034 }
5035
Potomski, MichalXe1928452017-02-23 09:05:30 +00005036 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005037 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005038 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5039 MAX_MODEL_LEN));
5040
5041 /* Null terminate the model string */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005042 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005043
5044out:
5045 return err;
5046}
5047
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005048static void ufs_fixup_device_setup(struct ufs_hba *hba,
5049 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005050{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005051 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005052
5053 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005054 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
5055 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
5056 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005057 !strcmp(f->card.model, UFS_ANY_MODEL)))
5058 hba->dev_quirks |= f->quirk;
5059 }
5060}
5061
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005062/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005063 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5064 * @hba: per-adapter instance
5065 *
5066 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5067 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5068 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5069 * the hibern8 exit latency.
5070 *
5071 * Returns zero on success, non-zero error value on failure.
5072 */
5073static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5074{
5075 int ret = 0;
5076 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5077
5078 ret = ufshcd_dme_peer_get(hba,
5079 UIC_ARG_MIB_SEL(
5080 RX_MIN_ACTIVATETIME_CAPABILITY,
5081 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5082 &peer_rx_min_activatetime);
5083 if (ret)
5084 goto out;
5085
5086 /* make sure proper unit conversion is applied */
5087 tuned_pa_tactivate =
5088 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5089 / PA_TACTIVATE_TIME_UNIT_US);
5090 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5091 tuned_pa_tactivate);
5092
5093out:
5094 return ret;
5095}
5096
5097/**
5098 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5099 * @hba: per-adapter instance
5100 *
5101 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5102 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5103 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5104 * This optimal value can help reduce the hibern8 exit latency.
5105 *
5106 * Returns zero on success, non-zero error value on failure.
5107 */
5108static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5109{
5110 int ret = 0;
5111 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5112 u32 max_hibern8_time, tuned_pa_hibern8time;
5113
5114 ret = ufshcd_dme_get(hba,
5115 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5116 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5117 &local_tx_hibern8_time_cap);
5118 if (ret)
5119 goto out;
5120
5121 ret = ufshcd_dme_peer_get(hba,
5122 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5123 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5124 &peer_rx_hibern8_time_cap);
5125 if (ret)
5126 goto out;
5127
5128 max_hibern8_time = max(local_tx_hibern8_time_cap,
5129 peer_rx_hibern8_time_cap);
5130 /* make sure proper unit conversion is applied */
5131 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5132 / PA_HIBERN8_TIME_UNIT_US);
5133 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5134 tuned_pa_hibern8time);
5135out:
5136 return ret;
5137}
5138
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005139/**
5140 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5141 * less than device PA_TACTIVATE time.
5142 * @hba: per-adapter instance
5143 *
5144 * Some UFS devices require host PA_TACTIVATE to be lower than device
5145 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5146 * for such devices.
5147 *
5148 * Returns zero on success, non-zero error value on failure.
5149 */
5150static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5151{
5152 int ret = 0;
5153 u32 granularity, peer_granularity;
5154 u32 pa_tactivate, peer_pa_tactivate;
5155 u32 pa_tactivate_us, peer_pa_tactivate_us;
5156 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5157
5158 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5159 &granularity);
5160 if (ret)
5161 goto out;
5162
5163 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5164 &peer_granularity);
5165 if (ret)
5166 goto out;
5167
5168 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5169 (granularity > PA_GRANULARITY_MAX_VAL)) {
5170 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5171 __func__, granularity);
5172 return -EINVAL;
5173 }
5174
5175 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5176 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5177 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5178 __func__, peer_granularity);
5179 return -EINVAL;
5180 }
5181
5182 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5183 if (ret)
5184 goto out;
5185
5186 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5187 &peer_pa_tactivate);
5188 if (ret)
5189 goto out;
5190
5191 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5192 peer_pa_tactivate_us = peer_pa_tactivate *
5193 gran_to_us_table[peer_granularity - 1];
5194
5195 if (pa_tactivate_us > peer_pa_tactivate_us) {
5196 u32 new_peer_pa_tactivate;
5197
5198 new_peer_pa_tactivate = pa_tactivate_us /
5199 gran_to_us_table[peer_granularity - 1];
5200 new_peer_pa_tactivate++;
5201 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5202 new_peer_pa_tactivate);
5203 }
5204
5205out:
5206 return ret;
5207}
5208
Yaniv Gardi37113102016-03-10 17:37:16 +02005209static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5210{
5211 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5212 ufshcd_tune_pa_tactivate(hba);
5213 ufshcd_tune_pa_hibern8time(hba);
5214 }
5215
5216 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5217 /* set 1ms timeout for PA_TACTIVATE */
5218 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005219
5220 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5221 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavanibedc6292017-04-04 19:32:13 +00005222
5223 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005224}
5225
Potomski, MichalXe1928452017-02-23 09:05:30 +00005226static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
5227{
5228 int err;
5229
5230 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
5231 &hba->desc_size.dev_desc);
5232 if (err)
5233 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5234
5235 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
5236 &hba->desc_size.pwr_desc);
5237 if (err)
5238 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5239
5240 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
5241 &hba->desc_size.interc_desc);
5242 if (err)
5243 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5244
5245 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
5246 &hba->desc_size.conf_desc);
5247 if (err)
5248 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5249
5250 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
5251 &hba->desc_size.unit_desc);
5252 if (err)
5253 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5254
5255 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
5256 &hba->desc_size.geom_desc);
5257 if (err)
5258 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5259}
5260
5261static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
5262{
5263 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5264 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5265 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5266 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5267 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5268 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5269}
5270
Yaniv Gardi37113102016-03-10 17:37:16 +02005271/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005272 * ufshcd_probe_hba - probe hba to detect device and initialize
5273 * @hba: per-adapter instance
5274 *
5275 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305276 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005277static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305278{
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005279 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305280 int ret;
5281
5282 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305283 if (ret)
5284 goto out;
5285
Yaniv Gardi50646362014-10-23 13:25:13 +03005286 ufshcd_init_pwr_info(hba);
5287
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005288 /* set the default level for urgent bkops */
5289 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5290 hba->is_urgent_bkops_lvl_checked = false;
5291
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005292 /* UniPro link is active now */
5293 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305294
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305295 ret = ufshcd_verify_dev_init(hba);
5296 if (ret)
5297 goto out;
5298
Dolev Raviv68078d52013-07-30 00:35:58 +05305299 ret = ufshcd_complete_dev_init(hba);
5300 if (ret)
5301 goto out;
5302
Potomski, MichalXe1928452017-02-23 09:05:30 +00005303 /* Init check for device descriptor sizes */
5304 ufshcd_init_desc_sizes(hba);
5305
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005306 ret = ufs_get_device_desc(hba, &card);
5307 if (ret) {
5308 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5309 __func__, ret);
5310 goto out;
5311 }
5312
5313 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02005314 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005315
5316 ret = ufshcd_set_vccq_rail_unused(hba,
5317 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5318 if (ret)
5319 goto out;
5320
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005321 /* UFS device is also active now */
5322 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305323 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005324 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305325
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005326 if (ufshcd_get_max_pwr_mode(hba)) {
5327 dev_err(hba->dev,
5328 "%s: Failed getting max supported power mode\n",
5329 __func__);
5330 } else {
5331 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
5332 if (ret)
5333 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5334 __func__, ret);
5335 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005336
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005337 /* set the state as operational after switching to desired gear */
5338 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005339
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005340 /*
5341 * If we are in error handling context or in power management callbacks
5342 * context, no need to scan the host
5343 */
5344 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5345 bool flag;
5346
5347 /* clear any previous UFS device information */
5348 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005349 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5350 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005351 hba->dev_info.f_power_on_wp_en = flag;
5352
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005353 if (!hba->is_init_prefetch)
5354 ufshcd_init_icc_levels(hba);
5355
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005356 /* Add required well known logical units to scsi mid layer */
5357 if (ufshcd_scsi_add_wlus(hba))
5358 goto out;
5359
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305360 scsi_scan_host(hba->host);
5361 pm_runtime_put_sync(hba->dev);
5362 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005363
5364 if (!hba->is_init_prefetch)
5365 hba->is_init_prefetch = true;
5366
Sahitya Tummala856b3482014-09-25 15:32:34 +03005367 /* Resume devfreq after UFS device is detected */
5368 if (ufshcd_is_clkscaling_enabled(hba))
5369 devfreq_resume_device(hba->devfreq);
5370
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305371out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005372 /*
5373 * If we failed to initialize the device or the device is not
5374 * present, turn off the power/clocks etc.
5375 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005376 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5377 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005378 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005379 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005380
5381 return ret;
5382}
5383
5384/**
5385 * ufshcd_async_scan - asynchronous execution for probing hba
5386 * @data: data pointer to pass to this function
5387 * @cookie: cookie data
5388 */
5389static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5390{
5391 struct ufs_hba *hba = (struct ufs_hba *)data;
5392
5393 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305394}
5395
Yaniv Gardif550c652016-03-10 17:37:07 +02005396static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5397{
5398 unsigned long flags;
5399 struct Scsi_Host *host;
5400 struct ufs_hba *hba;
5401 int index;
5402 bool found = false;
5403
5404 if (!scmd || !scmd->device || !scmd->device->host)
5405 return BLK_EH_NOT_HANDLED;
5406
5407 host = scmd->device->host;
5408 hba = shost_priv(host);
5409 if (!hba)
5410 return BLK_EH_NOT_HANDLED;
5411
5412 spin_lock_irqsave(host->host_lock, flags);
5413
5414 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5415 if (hba->lrb[index].cmd == scmd) {
5416 found = true;
5417 break;
5418 }
5419 }
5420
5421 spin_unlock_irqrestore(host->host_lock, flags);
5422
5423 /*
5424 * Bypass SCSI error handling and reset the block layer timer if this
5425 * SCSI command was not actually dispatched to UFS driver, otherwise
5426 * let SCSI layer handle the error as usual.
5427 */
5428 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5429}
5430
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305431static struct scsi_host_template ufshcd_driver_template = {
5432 .module = THIS_MODULE,
5433 .name = UFSHCD,
5434 .proc_name = UFSHCD,
5435 .queuecommand = ufshcd_queuecommand,
5436 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005437 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305438 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005439 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305440 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305441 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5442 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005443 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305444 .this_id = -1,
5445 .sg_tablesize = SG_ALL,
5446 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5447 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005448 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005449 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305450};
5451
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005452static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5453 int ua)
5454{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005455 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005456
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005457 if (!vreg)
5458 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005459
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005460 ret = regulator_set_load(vreg->reg, ua);
5461 if (ret < 0) {
5462 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5463 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005464 }
5465
5466 return ret;
5467}
5468
5469static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5470 struct ufs_vreg *vreg)
5471{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005472 if (!vreg)
5473 return 0;
5474 else if (vreg->unused)
5475 return 0;
5476 else
5477 return ufshcd_config_vreg_load(hba->dev, vreg,
5478 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005479}
5480
5481static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5482 struct ufs_vreg *vreg)
5483{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005484 if (!vreg)
5485 return 0;
5486 else if (vreg->unused)
5487 return 0;
5488 else
5489 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005490}
5491
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005492static int ufshcd_config_vreg(struct device *dev,
5493 struct ufs_vreg *vreg, bool on)
5494{
5495 int ret = 0;
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005496 struct regulator *reg;
5497 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005498 int min_uV, uA_load;
5499
5500 BUG_ON(!vreg);
5501
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005502 reg = vreg->reg;
5503 name = vreg->name;
5504
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005505 if (regulator_count_voltages(reg) > 0) {
5506 min_uV = on ? vreg->min_uV : 0;
5507 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5508 if (ret) {
5509 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5510 __func__, name, ret);
5511 goto out;
5512 }
5513
5514 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005515 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5516 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005517 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005518 }
5519out:
5520 return ret;
5521}
5522
5523static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5524{
5525 int ret = 0;
5526
Yaniv Gardi60f01872016-03-10 17:37:11 +02005527 if (!vreg)
5528 goto out;
5529 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005530 goto out;
5531
5532 ret = ufshcd_config_vreg(dev, vreg, true);
5533 if (!ret)
5534 ret = regulator_enable(vreg->reg);
5535
5536 if (!ret)
5537 vreg->enabled = true;
5538 else
5539 dev_err(dev, "%s: %s enable failed, err=%d\n",
5540 __func__, vreg->name, ret);
5541out:
5542 return ret;
5543}
5544
5545static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5546{
5547 int ret = 0;
5548
Yaniv Gardi60f01872016-03-10 17:37:11 +02005549 if (!vreg)
5550 goto out;
5551 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005552 goto out;
5553
5554 ret = regulator_disable(vreg->reg);
5555
5556 if (!ret) {
5557 /* ignore errors on applying disable config */
5558 ufshcd_config_vreg(dev, vreg, false);
5559 vreg->enabled = false;
5560 } else {
5561 dev_err(dev, "%s: %s disable failed, err=%d\n",
5562 __func__, vreg->name, ret);
5563 }
5564out:
5565 return ret;
5566}
5567
5568static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5569{
5570 int ret = 0;
5571 struct device *dev = hba->dev;
5572 struct ufs_vreg_info *info = &hba->vreg_info;
5573
5574 if (!info)
5575 goto out;
5576
5577 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5578 if (ret)
5579 goto out;
5580
5581 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5582 if (ret)
5583 goto out;
5584
5585 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5586 if (ret)
5587 goto out;
5588
5589out:
5590 if (ret) {
5591 ufshcd_toggle_vreg(dev, info->vccq2, false);
5592 ufshcd_toggle_vreg(dev, info->vccq, false);
5593 ufshcd_toggle_vreg(dev, info->vcc, false);
5594 }
5595 return ret;
5596}
5597
Raviv Shvili6a771a62014-09-25 15:32:24 +03005598static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5599{
5600 struct ufs_vreg_info *info = &hba->vreg_info;
5601
5602 if (info)
5603 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5604
5605 return 0;
5606}
5607
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005608static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5609{
5610 int ret = 0;
5611
5612 if (!vreg)
5613 goto out;
5614
5615 vreg->reg = devm_regulator_get(dev, vreg->name);
5616 if (IS_ERR(vreg->reg)) {
5617 ret = PTR_ERR(vreg->reg);
5618 dev_err(dev, "%s: %s get failed, err=%d\n",
5619 __func__, vreg->name, ret);
5620 }
5621out:
5622 return ret;
5623}
5624
5625static int ufshcd_init_vreg(struct ufs_hba *hba)
5626{
5627 int ret = 0;
5628 struct device *dev = hba->dev;
5629 struct ufs_vreg_info *info = &hba->vreg_info;
5630
5631 if (!info)
5632 goto out;
5633
5634 ret = ufshcd_get_vreg(dev, info->vcc);
5635 if (ret)
5636 goto out;
5637
5638 ret = ufshcd_get_vreg(dev, info->vccq);
5639 if (ret)
5640 goto out;
5641
5642 ret = ufshcd_get_vreg(dev, info->vccq2);
5643out:
5644 return ret;
5645}
5646
Raviv Shvili6a771a62014-09-25 15:32:24 +03005647static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5648{
5649 struct ufs_vreg_info *info = &hba->vreg_info;
5650
5651 if (info)
5652 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5653
5654 return 0;
5655}
5656
Yaniv Gardi60f01872016-03-10 17:37:11 +02005657static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5658{
5659 int ret = 0;
5660 struct ufs_vreg_info *info = &hba->vreg_info;
5661
5662 if (!info)
5663 goto out;
5664 else if (!info->vccq)
5665 goto out;
5666
5667 if (unused) {
5668 /* shut off the rail here */
5669 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5670 /*
5671 * Mark this rail as no longer used, so it doesn't get enabled
5672 * later by mistake
5673 */
5674 if (!ret)
5675 info->vccq->unused = true;
5676 } else {
5677 /*
5678 * rail should have been already enabled hence just make sure
5679 * that unused flag is cleared.
5680 */
5681 info->vccq->unused = false;
5682 }
5683out:
5684 return ret;
5685}
5686
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005687static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5688 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005689{
5690 int ret = 0;
5691 struct ufs_clk_info *clki;
5692 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005693 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005694
5695 if (!head || list_empty(head))
5696 goto out;
5697
5698 list_for_each_entry(clki, head, list) {
5699 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005700 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5701 continue;
5702
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005703 if (on && !clki->enabled) {
5704 ret = clk_prepare_enable(clki->clk);
5705 if (ret) {
5706 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5707 __func__, clki->name, ret);
5708 goto out;
5709 }
5710 } else if (!on && clki->enabled) {
5711 clk_disable_unprepare(clki->clk);
5712 }
5713 clki->enabled = on;
5714 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5715 clki->name, on ? "en" : "dis");
5716 }
5717 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005718
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005719 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005720out:
5721 if (ret) {
5722 list_for_each_entry(clki, head, list) {
5723 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5724 clk_disable_unprepare(clki->clk);
5725 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005726 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005727 spin_lock_irqsave(hba->host->host_lock, flags);
5728 hba->clk_gating.state = CLKS_ON;
5729 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005730 }
5731 return ret;
5732}
5733
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005734static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5735{
5736 return __ufshcd_setup_clocks(hba, on, false);
5737}
5738
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005739static int ufshcd_init_clocks(struct ufs_hba *hba)
5740{
5741 int ret = 0;
5742 struct ufs_clk_info *clki;
5743 struct device *dev = hba->dev;
5744 struct list_head *head = &hba->clk_list_head;
5745
5746 if (!head || list_empty(head))
5747 goto out;
5748
5749 list_for_each_entry(clki, head, list) {
5750 if (!clki->name)
5751 continue;
5752
5753 clki->clk = devm_clk_get(dev, clki->name);
5754 if (IS_ERR(clki->clk)) {
5755 ret = PTR_ERR(clki->clk);
5756 dev_err(dev, "%s: %s clk get failed, %d\n",
5757 __func__, clki->name, ret);
5758 goto out;
5759 }
5760
5761 if (clki->max_freq) {
5762 ret = clk_set_rate(clki->clk, clki->max_freq);
5763 if (ret) {
5764 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5765 __func__, clki->name,
5766 clki->max_freq, ret);
5767 goto out;
5768 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005769 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005770 }
5771 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5772 clki->name, clk_get_rate(clki->clk));
5773 }
5774out:
5775 return ret;
5776}
5777
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005778static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5779{
5780 int err = 0;
5781
5782 if (!hba->vops)
5783 goto out;
5784
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005785 err = ufshcd_vops_init(hba);
5786 if (err)
5787 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005788
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005789 err = ufshcd_vops_setup_regulators(hba, true);
5790 if (err)
5791 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005792
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005793 goto out;
5794
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005795out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005796 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005797out:
5798 if (err)
5799 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005800 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005801 return err;
5802}
5803
5804static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5805{
5806 if (!hba->vops)
5807 return;
5808
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005809 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005810
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005811 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005812
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005813 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005814}
5815
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005816static int ufshcd_hba_init(struct ufs_hba *hba)
5817{
5818 int err;
5819
Raviv Shvili6a771a62014-09-25 15:32:24 +03005820 /*
5821 * Handle host controller power separately from the UFS device power
5822 * rails as it will help controlling the UFS host controller power
5823 * collapse easily which is different than UFS device power collapse.
5824 * Also, enable the host controller power before we go ahead with rest
5825 * of the initialization here.
5826 */
5827 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005828 if (err)
5829 goto out;
5830
Raviv Shvili6a771a62014-09-25 15:32:24 +03005831 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005832 if (err)
5833 goto out;
5834
Raviv Shvili6a771a62014-09-25 15:32:24 +03005835 err = ufshcd_init_clocks(hba);
5836 if (err)
5837 goto out_disable_hba_vreg;
5838
5839 err = ufshcd_setup_clocks(hba, true);
5840 if (err)
5841 goto out_disable_hba_vreg;
5842
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005843 err = ufshcd_init_vreg(hba);
5844 if (err)
5845 goto out_disable_clks;
5846
5847 err = ufshcd_setup_vreg(hba, true);
5848 if (err)
5849 goto out_disable_clks;
5850
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005851 err = ufshcd_variant_hba_init(hba);
5852 if (err)
5853 goto out_disable_vreg;
5854
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005855 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005856 goto out;
5857
5858out_disable_vreg:
5859 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005860out_disable_clks:
5861 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005862out_disable_hba_vreg:
5863 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005864out:
5865 return err;
5866}
5867
5868static void ufshcd_hba_exit(struct ufs_hba *hba)
5869{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005870 if (hba->is_powered) {
5871 ufshcd_variant_hba_exit(hba);
5872 ufshcd_setup_vreg(hba, false);
5873 ufshcd_setup_clocks(hba, false);
5874 ufshcd_setup_hba_vreg(hba, false);
5875 hba->is_powered = false;
5876 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005877}
5878
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005879static int
5880ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305881{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005882 unsigned char cmd[6] = {REQUEST_SENSE,
5883 0,
5884 0,
5885 0,
5886 SCSI_SENSE_BUFFERSIZE,
5887 0};
5888 char *buffer;
5889 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305890
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005891 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5892 if (!buffer) {
5893 ret = -ENOMEM;
5894 goto out;
5895 }
5896
5897 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
5898 SCSI_SENSE_BUFFERSIZE, NULL,
5899 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5900 if (ret)
5901 pr_err("%s: failed with err %d\n", __func__, ret);
5902
5903 kfree(buffer);
5904out:
5905 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305906}
5907
5908/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005909 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5910 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305911 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005912 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305913 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005914 * Returns 0 if requested power mode is set successfully
5915 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305916 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005917static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5918 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305919{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005920 unsigned char cmd[6] = { START_STOP };
5921 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005922 struct scsi_device *sdp;
5923 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005924 int ret;
5925
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005926 spin_lock_irqsave(hba->host->host_lock, flags);
5927 sdp = hba->sdev_ufs_device;
5928 if (sdp) {
5929 ret = scsi_device_get(sdp);
5930 if (!ret && !scsi_device_online(sdp)) {
5931 ret = -ENODEV;
5932 scsi_device_put(sdp);
5933 }
5934 } else {
5935 ret = -ENODEV;
5936 }
5937 spin_unlock_irqrestore(hba->host->host_lock, flags);
5938
5939 if (ret)
5940 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005941
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305942 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005943 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5944 * handling, which would wait for host to be resumed. Since we know
5945 * we are functional while we are here, skip host resume in error
5946 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305947 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005948 hba->host->eh_noresume = 1;
5949 if (hba->wlun_dev_clr_ua) {
5950 ret = ufshcd_send_request_sense(hba, sdp);
5951 if (ret)
5952 goto out;
5953 /* Unit attention condition is cleared now */
5954 hba->wlun_dev_clr_ua = false;
5955 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305956
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005957 cmd[4] = pwr_mode << 4;
5958
5959 /*
5960 * Current function would be generally called from the power management
5961 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5962 * already suspended childs.
5963 */
5964 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5965 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5966 if (ret) {
5967 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005968 "START_STOP failed for power mode: %d, result %x\n",
5969 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005970 if (driver_byte(ret) & DRIVER_SENSE)
5971 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005972 }
5973
5974 if (!ret)
5975 hba->curr_dev_pwr_mode = pwr_mode;
5976out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005977 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005978 hba->host->eh_noresume = 0;
5979 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305980}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305981
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005982static int ufshcd_link_state_transition(struct ufs_hba *hba,
5983 enum uic_link_state req_link_state,
5984 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305985{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005986 int ret = 0;
5987
5988 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305989 return 0;
5990
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005991 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5992 ret = ufshcd_uic_hibern8_enter(hba);
5993 if (!ret)
5994 ufshcd_set_link_hibern8(hba);
5995 else
5996 goto out;
5997 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305998 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005999 * If autobkops is enabled, link can't be turned off because
6000 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306001 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006002 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
6003 (!check_for_bkops || (check_for_bkops &&
6004 !hba->auto_bkops_enabled))) {
6005 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02006006 * Let's make sure that link is in low power mode, we are doing
6007 * this currently by putting the link in Hibern8. Otherway to
6008 * put the link in low power mode is to send the DME end point
6009 * to device and then send the DME reset command to local
6010 * unipro. But putting the link in hibern8 is much faster.
6011 */
6012 ret = ufshcd_uic_hibern8_enter(hba);
6013 if (ret)
6014 goto out;
6015 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006016 * Change controller state to "reset state" which
6017 * should also put the link in off/reset state
6018 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02006019 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006020 /*
6021 * TODO: Check if we need any delay to make sure that
6022 * controller is reset
6023 */
6024 ufshcd_set_link_off(hba);
6025 }
6026
6027out:
6028 return ret;
6029}
6030
6031static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6032{
6033 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006034 * It seems some UFS devices may keep drawing more than sleep current
6035 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6036 * To avoid this situation, add 2ms delay before putting these UFS
6037 * rails in LPM mode.
6038 */
6039 if (!ufshcd_is_link_active(hba) &&
6040 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6041 usleep_range(2000, 2100);
6042
6043 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006044 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6045 * power.
6046 *
6047 * If UFS device and link is in OFF state, all power supplies (VCC,
6048 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6049 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6050 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6051 *
6052 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6053 * in low power state which would save some power.
6054 */
6055 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6056 !hba->dev_info.is_lu_power_on_wp) {
6057 ufshcd_setup_vreg(hba, false);
6058 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6059 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6060 if (!ufshcd_is_link_active(hba)) {
6061 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6062 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6063 }
6064 }
6065}
6066
6067static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6068{
6069 int ret = 0;
6070
6071 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6072 !hba->dev_info.is_lu_power_on_wp) {
6073 ret = ufshcd_setup_vreg(hba, true);
6074 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6075 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
6076 if (!ret && !ufshcd_is_link_active(hba)) {
6077 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6078 if (ret)
6079 goto vcc_disable;
6080 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6081 if (ret)
6082 goto vccq_lpm;
6083 }
6084 }
6085 goto out;
6086
6087vccq_lpm:
6088 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6089vcc_disable:
6090 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6091out:
6092 return ret;
6093}
6094
6095static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6096{
6097 if (ufshcd_is_link_off(hba))
6098 ufshcd_setup_hba_vreg(hba, false);
6099}
6100
6101static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6102{
6103 if (ufshcd_is_link_off(hba))
6104 ufshcd_setup_hba_vreg(hba, true);
6105}
6106
6107/**
6108 * ufshcd_suspend - helper function for suspend operations
6109 * @hba: per adapter instance
6110 * @pm_op: desired low power operation type
6111 *
6112 * This function will try to put the UFS device and link into low power
6113 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6114 * (System PM level).
6115 *
6116 * If this function is called during shutdown, it will make sure that
6117 * both UFS device and UFS link is powered off.
6118 *
6119 * NOTE: UFS device & link must be active before we enter in this function.
6120 *
6121 * Returns 0 for success and non-zero for failure
6122 */
6123static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6124{
6125 int ret = 0;
6126 enum ufs_pm_level pm_lvl;
6127 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6128 enum uic_link_state req_link_state;
6129
6130 hba->pm_op_in_progress = 1;
6131 if (!ufshcd_is_shutdown_pm(pm_op)) {
6132 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6133 hba->rpm_lvl : hba->spm_lvl;
6134 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6135 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6136 } else {
6137 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6138 req_link_state = UIC_LINK_OFF_STATE;
6139 }
6140
6141 /*
6142 * If we can't transition into any of the low power modes
6143 * just gate the clocks.
6144 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006145 ufshcd_hold(hba, false);
6146 hba->clk_gating.is_suspended = true;
6147
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006148 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6149 req_link_state == UIC_LINK_ACTIVE_STATE) {
6150 goto disable_clks;
6151 }
6152
6153 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6154 (req_link_state == hba->uic_link_state))
6155 goto out;
6156
6157 /* UFS device & link must be active before we enter in this function */
6158 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6159 ret = -EINVAL;
6160 goto out;
6161 }
6162
6163 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006164 if (ufshcd_can_autobkops_during_suspend(hba)) {
6165 /*
6166 * The device is idle with no requests in the queue,
6167 * allow background operations if bkops status shows
6168 * that performance might be impacted.
6169 */
6170 ret = ufshcd_urgent_bkops(hba);
6171 if (ret)
6172 goto enable_gating;
6173 } else {
6174 /* make sure that auto bkops is disabled */
6175 ufshcd_disable_auto_bkops(hba);
6176 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006177 }
6178
6179 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6180 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6181 !ufshcd_is_runtime_pm(pm_op))) {
6182 /* ensure that bkops is disabled */
6183 ufshcd_disable_auto_bkops(hba);
6184 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6185 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006186 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006187 }
6188
6189 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6190 if (ret)
6191 goto set_dev_active;
6192
6193 ufshcd_vreg_set_lpm(hba);
6194
6195disable_clks:
6196 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03006197 * The clock scaling needs access to controller registers. Hence, Wait
6198 * for pending clock scaling work to be done before clocks are
6199 * turned off.
6200 */
6201 if (ufshcd_is_clkscaling_enabled(hba)) {
6202 devfreq_suspend_device(hba->devfreq);
6203 hba->clk_scaling.window_start_t = 0;
6204 }
6205 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006206 * Call vendor specific suspend callback. As these callbacks may access
6207 * vendor specific host controller register space call them before the
6208 * host clocks are ON.
6209 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006210 ret = ufshcd_vops_suspend(hba, pm_op);
6211 if (ret)
6212 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006213
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006214 ret = ufshcd_vops_setup_clocks(hba, false);
6215 if (ret)
6216 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006217
6218 if (!ufshcd_is_link_active(hba))
6219 ufshcd_setup_clocks(hba, false);
6220 else
6221 /* If link is active, device ref_clk can't be switched off */
6222 __ufshcd_setup_clocks(hba, false, true);
6223
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006224 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006225 /*
6226 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006227 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006228 */
6229 ufshcd_disable_irq(hba);
6230 /* Put the host controller in low power mode if possible */
6231 ufshcd_hba_vreg_set_lpm(hba);
6232 goto out;
6233
6234vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006235 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006236set_link_active:
6237 ufshcd_vreg_set_hpm(hba);
6238 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6239 ufshcd_set_link_active(hba);
6240 else if (ufshcd_is_link_off(hba))
6241 ufshcd_host_reset_and_restore(hba);
6242set_dev_active:
6243 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6244 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006245enable_gating:
6246 hba->clk_gating.is_suspended = false;
6247 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006248out:
6249 hba->pm_op_in_progress = 0;
6250 return ret;
6251}
6252
6253/**
6254 * ufshcd_resume - helper function for resume operations
6255 * @hba: per adapter instance
6256 * @pm_op: runtime PM or system PM
6257 *
6258 * This function basically brings the UFS device, UniPro link and controller
6259 * to active state.
6260 *
6261 * Returns 0 for success and non-zero for failure
6262 */
6263static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6264{
6265 int ret;
6266 enum uic_link_state old_link_state;
6267
6268 hba->pm_op_in_progress = 1;
6269 old_link_state = hba->uic_link_state;
6270
6271 ufshcd_hba_vreg_set_hpm(hba);
6272 /* Make sure clocks are enabled before accessing controller */
6273 ret = ufshcd_setup_clocks(hba, true);
6274 if (ret)
6275 goto out;
6276
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006277 /* enable the host irq as host controller would be active soon */
6278 ret = ufshcd_enable_irq(hba);
6279 if (ret)
6280 goto disable_irq_and_vops_clks;
6281
6282 ret = ufshcd_vreg_set_hpm(hba);
6283 if (ret)
6284 goto disable_irq_and_vops_clks;
6285
6286 /*
6287 * Call vendor specific resume callback. As these callbacks may access
6288 * vendor specific host controller register space call them when the
6289 * host clocks are ON.
6290 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006291 ret = ufshcd_vops_resume(hba, pm_op);
6292 if (ret)
6293 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006294
6295 if (ufshcd_is_link_hibern8(hba)) {
6296 ret = ufshcd_uic_hibern8_exit(hba);
6297 if (!ret)
6298 ufshcd_set_link_active(hba);
6299 else
6300 goto vendor_suspend;
6301 } else if (ufshcd_is_link_off(hba)) {
6302 ret = ufshcd_host_reset_and_restore(hba);
6303 /*
6304 * ufshcd_host_reset_and_restore() should have already
6305 * set the link state as active
6306 */
6307 if (ret || !ufshcd_is_link_active(hba))
6308 goto vendor_suspend;
6309 }
6310
6311 if (!ufshcd_is_ufs_dev_active(hba)) {
6312 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6313 if (ret)
6314 goto set_old_link_state;
6315 }
6316
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08006317 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
6318 ufshcd_enable_auto_bkops(hba);
6319 else
6320 /*
6321 * If BKOPs operations are urgently needed at this moment then
6322 * keep auto-bkops enabled or else disable it.
6323 */
6324 ufshcd_urgent_bkops(hba);
6325
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006326 hba->clk_gating.is_suspended = false;
6327
Sahitya Tummala856b3482014-09-25 15:32:34 +03006328 if (ufshcd_is_clkscaling_enabled(hba))
6329 devfreq_resume_device(hba->devfreq);
6330
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006331 /* Schedule clock gating in case of no access to UFS device yet */
6332 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006333 goto out;
6334
6335set_old_link_state:
6336 ufshcd_link_state_transition(hba, old_link_state, 0);
6337vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006338 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006339disable_vreg:
6340 ufshcd_vreg_set_lpm(hba);
6341disable_irq_and_vops_clks:
6342 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006343 ufshcd_setup_clocks(hba, false);
6344out:
6345 hba->pm_op_in_progress = 0;
6346 return ret;
6347}
6348
6349/**
6350 * ufshcd_system_suspend - system suspend routine
6351 * @hba: per adapter instance
6352 * @pm_op: runtime PM or system PM
6353 *
6354 * Check the description of ufshcd_suspend() function for more details.
6355 *
6356 * Returns 0 for success and non-zero for failure
6357 */
6358int ufshcd_system_suspend(struct ufs_hba *hba)
6359{
6360 int ret = 0;
6361
6362 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006363 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006364
6365 if (pm_runtime_suspended(hba->dev)) {
6366 if (hba->rpm_lvl == hba->spm_lvl)
6367 /*
6368 * There is possibility that device may still be in
6369 * active state during the runtime suspend.
6370 */
6371 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6372 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6373 goto out;
6374
6375 /*
6376 * UFS device and/or UFS link low power states during runtime
6377 * suspend seems to be different than what is expected during
6378 * system suspend. Hence runtime resume the devic & link and
6379 * let the system suspend low power states to take effect.
6380 * TODO: If resume takes longer time, we might have optimize
6381 * it in future by not resuming everything if possible.
6382 */
6383 ret = ufshcd_runtime_resume(hba);
6384 if (ret)
6385 goto out;
6386 }
6387
6388 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6389out:
Dolev Ravive7850602014-09-25 15:32:36 +03006390 if (!ret)
6391 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006392 return ret;
6393}
6394EXPORT_SYMBOL(ufshcd_system_suspend);
6395
6396/**
6397 * ufshcd_system_resume - system resume routine
6398 * @hba: per adapter instance
6399 *
6400 * Returns 0 for success and non-zero for failure
6401 */
6402
6403int ufshcd_system_resume(struct ufs_hba *hba)
6404{
6405 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
6406 /*
6407 * Let the runtime resume take care of resuming
6408 * if runtime suspended.
6409 */
6410 return 0;
6411
6412 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6413}
6414EXPORT_SYMBOL(ufshcd_system_resume);
6415
6416/**
6417 * ufshcd_runtime_suspend - runtime suspend routine
6418 * @hba: per adapter instance
6419 *
6420 * Check the description of ufshcd_suspend() function for more details.
6421 *
6422 * Returns 0 for success and non-zero for failure
6423 */
6424int ufshcd_runtime_suspend(struct ufs_hba *hba)
6425{
6426 if (!hba || !hba->is_powered)
6427 return 0;
6428
6429 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306430}
6431EXPORT_SYMBOL(ufshcd_runtime_suspend);
6432
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006433/**
6434 * ufshcd_runtime_resume - runtime resume routine
6435 * @hba: per adapter instance
6436 *
6437 * This function basically brings the UFS device, UniPro link and controller
6438 * to active state. Following operations are done in this function:
6439 *
6440 * 1. Turn on all the controller related clocks
6441 * 2. Bring the UniPro link out of Hibernate state
6442 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6443 * to active state.
6444 * 4. If auto-bkops is enabled on the device, disable it.
6445 *
6446 * So following would be the possible power state after this function return
6447 * successfully:
6448 * S1: UFS device in Active state with VCC rail ON
6449 * UniPro link in Active state
6450 * All the UFS/UniPro controller clocks are ON
6451 *
6452 * Returns 0 for success and non-zero for failure
6453 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306454int ufshcd_runtime_resume(struct ufs_hba *hba)
6455{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006456 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306457 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006458 else
6459 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306460}
6461EXPORT_SYMBOL(ufshcd_runtime_resume);
6462
6463int ufshcd_runtime_idle(struct ufs_hba *hba)
6464{
6465 return 0;
6466}
6467EXPORT_SYMBOL(ufshcd_runtime_idle);
6468
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306469/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006470 * ufshcd_shutdown - shutdown routine
6471 * @hba: per adapter instance
6472 *
6473 * This function would power off both UFS device and UFS link.
6474 *
6475 * Returns 0 always to allow force shutdown even in case of errors.
6476 */
6477int ufshcd_shutdown(struct ufs_hba *hba)
6478{
6479 int ret = 0;
6480
6481 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6482 goto out;
6483
6484 if (pm_runtime_suspended(hba->dev)) {
6485 ret = ufshcd_runtime_resume(hba);
6486 if (ret)
6487 goto out;
6488 }
6489
6490 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6491out:
6492 if (ret)
6493 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6494 /* allow force shutdown even in case of errors */
6495 return 0;
6496}
6497EXPORT_SYMBOL(ufshcd_shutdown);
6498
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006499/*
6500 * Values permitted 0, 1, 2.
6501 * 0 -> Disable IO latency histograms (default)
6502 * 1 -> Enable IO latency histograms
6503 * 2 -> Zero out IO latency histograms
6504 */
6505static ssize_t
6506latency_hist_store(struct device *dev, struct device_attribute *attr,
6507 const char *buf, size_t count)
6508{
6509 struct ufs_hba *hba = dev_get_drvdata(dev);
6510 long value;
6511
6512 if (kstrtol(buf, 0, &value))
6513 return -EINVAL;
Hyojun Kim11537d52017-12-21 09:57:41 -08006514 if (value == BLK_IO_LAT_HIST_ZERO) {
6515 memset(&hba->io_lat_read, 0, sizeof(hba->io_lat_read));
6516 memset(&hba->io_lat_write, 0, sizeof(hba->io_lat_write));
6517 } else if (value == BLK_IO_LAT_HIST_ENABLE ||
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006518 value == BLK_IO_LAT_HIST_DISABLE)
6519 hba->latency_hist_enabled = value;
6520 return count;
6521}
6522
6523ssize_t
6524latency_hist_show(struct device *dev, struct device_attribute *attr,
6525 char *buf)
6526{
6527 struct ufs_hba *hba = dev_get_drvdata(dev);
Hyojun Kim11537d52017-12-21 09:57:41 -08006528 size_t written_bytes;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006529
Hyojun Kim11537d52017-12-21 09:57:41 -08006530 written_bytes = blk_latency_hist_show("Read", &hba->io_lat_read,
6531 buf, PAGE_SIZE);
6532 written_bytes += blk_latency_hist_show("Write", &hba->io_lat_write,
6533 buf + written_bytes, PAGE_SIZE - written_bytes);
6534
6535 return written_bytes;
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006536}
6537
6538static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR,
6539 latency_hist_show, latency_hist_store);
6540
6541static void
6542ufshcd_init_latency_hist(struct ufs_hba *hba)
6543{
6544 if (device_create_file(hba->dev, &dev_attr_latency_hist))
6545 dev_err(hba->dev, "Failed to create latency_hist sysfs entry\n");
6546}
6547
6548static void
6549ufshcd_exit_latency_hist(struct ufs_hba *hba)
6550{
6551 device_create_file(hba->dev, &dev_attr_latency_hist);
6552}
6553
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006554/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306555 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306556 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306557 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306558 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306559void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306560{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306561 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306562 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306563 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006564 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306565
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306566 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006567
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006568 ufshcd_exit_clk_gating(hba);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006569 ufshcd_exit_latency_hist(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006570 if (ufshcd_is_clkscaling_enabled(hba))
6571 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006572 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306573}
6574EXPORT_SYMBOL_GPL(ufshcd_remove);
6575
6576/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006577 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6578 * @hba: pointer to Host Bus Adapter (HBA)
6579 */
6580void ufshcd_dealloc_host(struct ufs_hba *hba)
6581{
6582 scsi_host_put(hba->host);
6583}
6584EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6585
6586/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006587 * ufshcd_set_dma_mask - Set dma mask based on the controller
6588 * addressing capability
6589 * @hba: per adapter instance
6590 *
6591 * Returns 0 for success, non-zero for failure
6592 */
6593static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6594{
6595 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6596 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6597 return 0;
6598 }
6599 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6600}
6601
6602/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006603 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306604 * @dev: pointer to device handle
6605 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306606 * Returns 0 on success, non-zero value on failure
6607 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006608int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306609{
6610 struct Scsi_Host *host;
6611 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006612 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306613
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306614 if (!dev) {
6615 dev_err(dev,
6616 "Invalid memory reference for dev is NULL\n");
6617 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306618 goto out_error;
6619 }
6620
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306621 host = scsi_host_alloc(&ufshcd_driver_template,
6622 sizeof(struct ufs_hba));
6623 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306624 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306625 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306626 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306627 }
6628 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306629 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306630 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006631 *hba_handle = hba;
6632
6633out_error:
6634 return err;
6635}
6636EXPORT_SYMBOL(ufshcd_alloc_host);
6637
Sahitya Tummala856b3482014-09-25 15:32:34 +03006638static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6639{
6640 int ret = 0;
6641 struct ufs_clk_info *clki;
6642 struct list_head *head = &hba->clk_list_head;
6643
6644 if (!head || list_empty(head))
6645 goto out;
6646
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006647 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6648 if (ret)
6649 return ret;
6650
Sahitya Tummala856b3482014-09-25 15:32:34 +03006651 list_for_each_entry(clki, head, list) {
6652 if (!IS_ERR_OR_NULL(clki->clk)) {
6653 if (scale_up && clki->max_freq) {
6654 if (clki->curr_freq == clki->max_freq)
6655 continue;
6656 ret = clk_set_rate(clki->clk, clki->max_freq);
6657 if (ret) {
6658 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6659 __func__, clki->name,
6660 clki->max_freq, ret);
6661 break;
6662 }
6663 clki->curr_freq = clki->max_freq;
6664
6665 } else if (!scale_up && clki->min_freq) {
6666 if (clki->curr_freq == clki->min_freq)
6667 continue;
6668 ret = clk_set_rate(clki->clk, clki->min_freq);
6669 if (ret) {
6670 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6671 __func__, clki->name,
6672 clki->min_freq, ret);
6673 break;
6674 }
6675 clki->curr_freq = clki->min_freq;
6676 }
6677 }
6678 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6679 clki->name, clk_get_rate(clki->clk));
6680 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006681
6682 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6683
Sahitya Tummala856b3482014-09-25 15:32:34 +03006684out:
6685 return ret;
6686}
6687
6688static int ufshcd_devfreq_target(struct device *dev,
6689 unsigned long *freq, u32 flags)
6690{
6691 int err = 0;
6692 struct ufs_hba *hba = dev_get_drvdata(dev);
6693
6694 if (!ufshcd_is_clkscaling_enabled(hba))
6695 return -EINVAL;
6696
6697 if (*freq == UINT_MAX)
6698 err = ufshcd_scale_clks(hba, true);
6699 else if (*freq == 0)
6700 err = ufshcd_scale_clks(hba, false);
6701
6702 return err;
6703}
6704
6705static int ufshcd_devfreq_get_dev_status(struct device *dev,
6706 struct devfreq_dev_status *stat)
6707{
6708 struct ufs_hba *hba = dev_get_drvdata(dev);
6709 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6710 unsigned long flags;
6711
6712 if (!ufshcd_is_clkscaling_enabled(hba))
6713 return -EINVAL;
6714
6715 memset(stat, 0, sizeof(*stat));
6716
6717 spin_lock_irqsave(hba->host->host_lock, flags);
6718 if (!scaling->window_start_t)
6719 goto start_window;
6720
6721 if (scaling->is_busy_started)
6722 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6723 scaling->busy_start_t));
6724
6725 stat->total_time = jiffies_to_usecs((long)jiffies -
6726 (long)scaling->window_start_t);
6727 stat->busy_time = scaling->tot_busy_t;
6728start_window:
6729 scaling->window_start_t = jiffies;
6730 scaling->tot_busy_t = 0;
6731
6732 if (hba->outstanding_reqs) {
6733 scaling->busy_start_t = ktime_get();
6734 scaling->is_busy_started = true;
6735 } else {
6736 scaling->busy_start_t = ktime_set(0, 0);
6737 scaling->is_busy_started = false;
6738 }
6739 spin_unlock_irqrestore(hba->host->host_lock, flags);
6740 return 0;
6741}
6742
6743static struct devfreq_dev_profile ufs_devfreq_profile = {
6744 .polling_ms = 100,
6745 .target = ufshcd_devfreq_target,
6746 .get_dev_status = ufshcd_devfreq_get_dev_status,
6747};
6748
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006749/**
6750 * ufshcd_init - Driver initialization routine
6751 * @hba: per-adapter instance
6752 * @mmio_base: base register address
6753 * @irq: Interrupt line of device
6754 * Returns 0 on success, non-zero value on failure
6755 */
6756int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6757{
6758 int err;
6759 struct Scsi_Host *host = hba->host;
6760 struct device *dev = hba->dev;
6761
6762 if (!mmio_base) {
6763 dev_err(hba->dev,
6764 "Invalid memory reference for mmio_base is NULL\n");
6765 err = -ENODEV;
6766 goto out_error;
6767 }
6768
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306769 hba->mmio_base = mmio_base;
6770 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306771
Potomski, MichalXe1928452017-02-23 09:05:30 +00006772 /* Set descriptor lengths to specification defaults */
6773 ufshcd_def_desc_sizes(hba);
6774
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006775 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006776 if (err)
6777 goto out_error;
6778
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306779 /* Read capabilities registers */
6780 ufshcd_hba_capabilities(hba);
6781
6782 /* Get UFS version supported by the controller */
6783 hba->ufs_version = ufshcd_get_ufs_version(hba);
6784
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306785 /* Get Interrupt bit mask per version */
6786 hba->intr_mask = ufshcd_get_intr_mask(hba);
6787
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006788 err = ufshcd_set_dma_mask(hba);
6789 if (err) {
6790 dev_err(hba->dev, "set dma mask failed\n");
6791 goto out_disable;
6792 }
6793
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306794 /* Allocate memory for host memory space */
6795 err = ufshcd_memory_alloc(hba);
6796 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306797 dev_err(hba->dev, "Memory allocation failed\n");
6798 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306799 }
6800
6801 /* Configure LRB */
6802 ufshcd_host_memory_configure(hba);
6803
6804 host->can_queue = hba->nutrs;
6805 host->cmd_per_lun = hba->nutrs;
6806 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006807 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306808 host->max_channel = UFSHCD_MAX_CHANNEL;
6809 host->unique_id = host->host_no;
6810 host->max_cmd_len = MAX_CDB_SIZE;
6811
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006812 hba->max_pwr_info.is_valid = false;
6813
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306814 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306815 init_waitqueue_head(&hba->tm_wq);
6816 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306817
6818 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306819 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306820 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306821
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306822 /* Initialize UIC command mutex */
6823 mutex_init(&hba->uic_cmd_mutex);
6824
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306825 /* Initialize mutex for device management commands */
6826 mutex_init(&hba->dev_cmd.lock);
6827
6828 /* Initialize device management tag acquire wait queue */
6829 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6830
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006831 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006832
6833 /*
6834 * In order to avoid any spurious interrupt immediately after
6835 * registering UFS controller interrupt handler, clear any pending UFS
6836 * interrupt status and disable all the UFS interrupts.
6837 */
6838 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6839 REG_INTERRUPT_STATUS);
6840 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6841 /*
6842 * Make sure that UFS interrupts are disabled and any pending interrupt
6843 * status is cleared before registering UFS interrupt handler.
6844 */
6845 mb();
6846
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306847 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006848 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306849 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306850 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006851 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006852 } else {
6853 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306854 }
6855
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306856 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306857 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306858 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006859 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306860 }
6861
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306862 /* Host controller enable */
6863 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306864 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306865 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306866 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306867 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306868
Sahitya Tummala856b3482014-09-25 15:32:34 +03006869 if (ufshcd_is_clkscaling_enabled(hba)) {
6870 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6871 "simple_ondemand", NULL);
6872 if (IS_ERR(hba->devfreq)) {
6873 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6874 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006875 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006876 goto out_remove_scsi_host;
6877 }
6878 /* Suspend devfreq until the UFS device is detected */
6879 devfreq_suspend_device(hba->devfreq);
6880 hba->clk_scaling.window_start_t = 0;
6881 }
6882
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306883 /* Hold auto suspend until async scan completes */
6884 pm_runtime_get_sync(dev);
6885
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006886 ufshcd_init_latency_hist(hba);
6887
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006888 /*
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006889 * We are assuming that device wasn't put in sleep/power-down
6890 * state exclusively during the boot stage before kernel.
6891 * This assumption helps avoid doing link startup twice during
6892 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006893 */
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006894 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006895
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306896 async_schedule(ufshcd_async_scan, hba);
6897
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306898 return 0;
6899
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306900out_remove_scsi_host:
6901 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006902exit_gating:
6903 ufshcd_exit_clk_gating(hba);
Mohan Srinivasane2d88782016-12-14 15:55:36 -08006904 ufshcd_exit_latency_hist(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306905out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006906 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306907 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006908 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306909out_error:
6910 return err;
6911}
6912EXPORT_SYMBOL_GPL(ufshcd_init);
6913
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306914MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6915MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306916MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306917MODULE_LICENSE("GPL");
6918MODULE_VERSION(UFSHCD_DRIVER_VERSION);