blob: 86a3110c6d75118b02fccf3b6a2a45a2db1994ed [file] [log] [blame]
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301/*
Vinayak Holikattie0eca632013-02-25 21:44:33 +05302 * Universal Flash Storage Host controller driver Core
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303 *
4 * This code is based on drivers/scsi/ufs/ufshcd.c
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305 * Copyright (C) 2011-2013 Samsung India Software Operations
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02006 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308 * Authors:
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053016 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053018 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053024 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
34 * this program.
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030035 *
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053038 */
39
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053040#include <linux/async.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030041#include <linux/devfreq.h>
Yaniv Gardib573d482016-03-10 17:37:09 +020042#include <linux/nls.h>
Yaniv Gardi54b879b2016-03-10 17:37:05 +020043#include <linux/of.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053044#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020045#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053046#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053047
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053048#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
49 UTP_TASK_REQ_COMPL |\
50 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053051/* UIC command timeout, unit: ms */
52#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053053
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053054/* NOP OUT retries waiting for NOP IN response */
55#define NOP_OUT_RETRIES 10
56/* Timeout after 30 msecs if NOP OUT hangs without response */
57#define NOP_OUT_TIMEOUT 30 /* msecs */
58
Dolev Raviv68078d52013-07-30 00:35:58 +053059/* Query request retries */
60#define QUERY_REQ_RETRIES 10
61/* Query request timeout */
62#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020063/*
64 * Query request timeout for fDeviceInit flag
65 * fDeviceInit query response time for some devices is too large that default
66 * QUERY_REQ_TIMEOUT may not be enough for such devices.
67 */
68#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053069
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053070/* Task management command timeout */
71#define TM_CMD_TIMEOUT 100 /* msecs */
72
Yaniv Gardi64238fb2016-02-01 15:02:43 +020073/* maximum number of retries for a general UIC command */
74#define UFS_UIC_COMMAND_RETRIES 3
75
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030076/* maximum number of link-startup retries */
77#define DME_LINKSTARTUP_RETRIES 3
78
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020079/* Maximum retries for Hibern8 enter */
80#define UIC_HIBERN8_ENTER_RETRIES 3
81
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030082/* maximum number of reset retries before giving up */
83#define MAX_HOST_RESET_RETRIES 5
84
Dolev Raviv68078d52013-07-30 00:35:58 +053085/* Expose the flag value from utp_upiu_query.value */
86#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
87
Seungwon Jeon7d568652013-08-31 21:40:20 +053088/* Interrupt aggregation default timeout, unit: 40us */
89#define INT_AGGR_DEF_TO 0x02
90
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030091#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
92 ({ \
93 int _ret; \
94 if (_on) \
95 _ret = ufshcd_enable_vreg(_dev, _vreg); \
96 else \
97 _ret = ufshcd_disable_vreg(_dev, _vreg); \
98 _ret; \
99 })
100
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530101enum {
102 UFSHCD_MAX_CHANNEL = 0,
103 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530104 UFSHCD_CMD_PER_LUN = 32,
105 UFSHCD_CAN_QUEUE = 32,
106};
107
108/* UFSHCD states */
109enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530110 UFSHCD_STATE_RESET,
111 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530112 UFSHCD_STATE_OPERATIONAL,
Zang Leiganga17bddc2017-04-04 19:32:20 +0000113 UFSHCD_STATE_EH_SCHEDULED,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530114};
115
116/* UFSHCD error handling flags */
117enum {
118 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530119};
120
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530121/* UFSHCD UIC layer error flags */
122enum {
123 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200124 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
125 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
126 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
127 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
128 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530129};
130
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530131/* Interrupt configuration options */
132enum {
133 UFSHCD_INT_DISABLE,
134 UFSHCD_INT_ENABLE,
135 UFSHCD_INT_CLEAR,
136};
137
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530138#define ufshcd_set_eh_in_progress(h) \
139 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
140#define ufshcd_eh_in_progress(h) \
141 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
142#define ufshcd_clear_eh_in_progress(h) \
143 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
144
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300145#define ufshcd_set_ufs_dev_active(h) \
146 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
147#define ufshcd_set_ufs_dev_sleep(h) \
148 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
149#define ufshcd_set_ufs_dev_poweroff(h) \
150 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
151#define ufshcd_is_ufs_dev_active(h) \
152 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
153#define ufshcd_is_ufs_dev_sleep(h) \
154 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
155#define ufshcd_is_ufs_dev_poweroff(h) \
156 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
157
158static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
159 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
160 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
161 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
162 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
163 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
164 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
165};
166
167static inline enum ufs_dev_pwr_mode
168ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
169{
170 return ufs_pm_lvl_states[lvl].dev_state;
171}
172
173static inline enum uic_link_state
174ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
175{
176 return ufs_pm_lvl_states[lvl].link_state;
177}
178
Subhash Jadavanibedc6292017-04-04 19:32:13 +0000179static struct ufs_dev_fix ufs_fixups[] = {
180 /* UFS cards deviations table */
181 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
182 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
183 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
184 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
185 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS),
186 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
187 UFS_DEVICE_NO_FASTAUTO),
188 UFS_FIX(UFS_VENDOR_SAMSUNG, UFS_ANY_MODEL,
189 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE),
190 UFS_FIX(UFS_VENDOR_TOSHIBA, UFS_ANY_MODEL,
191 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM),
192 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9C8KBADG",
193 UFS_DEVICE_QUIRK_PA_TACTIVATE),
194 UFS_FIX(UFS_VENDOR_TOSHIBA, "THGLF2G9D8KBADG",
195 UFS_DEVICE_QUIRK_PA_TACTIVATE),
196 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL, UFS_DEVICE_NO_VCCQ),
197 UFS_FIX(UFS_VENDOR_SKHYNIX, UFS_ANY_MODEL,
198 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME),
199
200 END_FIX
201};
202
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530203static void ufshcd_tmc_handler(struct ufs_hba *hba);
204static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530205static int ufshcd_reset_and_restore(struct ufs_hba *hba);
206static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300207static void ufshcd_hba_exit(struct ufs_hba *hba);
208static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300209static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
210 bool skip_ref_clk);
211static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200212static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300213static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
214static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300215static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300216static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
217static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300218static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
219 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300220static int ufshcd_change_power_mode(struct ufs_hba *hba,
221 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200222static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
223{
224 return tag >= 0 && tag < hba->nutrs;
225}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300226
227static inline int ufshcd_enable_irq(struct ufs_hba *hba)
228{
229 int ret = 0;
230
231 if (!hba->is_irq_enabled) {
232 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
233 hba);
234 if (ret)
235 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
236 __func__, ret);
237 hba->is_irq_enabled = true;
238 }
239
240 return ret;
241}
242
243static inline void ufshcd_disable_irq(struct ufs_hba *hba)
244{
245 if (hba->is_irq_enabled) {
246 free_irq(hba->irq, hba);
247 hba->is_irq_enabled = false;
248 }
249}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530250
Yaniv Gardib573d482016-03-10 17:37:09 +0200251/* replace non-printable or non-ASCII characters with spaces */
252static inline void ufshcd_remove_non_printable(char *val)
253{
254 if (!val)
255 return;
256
257 if (*val < 0x20 || *val > 0x7e)
258 *val = ' ';
259}
260
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530261/*
262 * ufshcd_wait_for_register - wait for register value to change
263 * @hba - per-adapter interface
264 * @reg - mmio register offset
265 * @mask - mask to apply to read register value
266 * @val - wait condition
267 * @interval_us - polling interval in microsecs
268 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200269 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530270 *
271 * Returns -ETIMEDOUT on error, zero on success
272 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200273int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
274 u32 val, unsigned long interval_us,
275 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530276{
277 int err = 0;
278 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
279
280 /* ignore bits that we don't intend to wait on */
281 val = val & mask;
282
283 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200284 if (can_sleep)
285 usleep_range(interval_us, interval_us + 50);
286 else
287 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530288 if (time_after(jiffies, timeout)) {
289 if ((ufshcd_readl(hba, reg) & mask) != val)
290 err = -ETIMEDOUT;
291 break;
292 }
293 }
294
295 return err;
296}
297
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530298/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530299 * ufshcd_get_intr_mask - Get the interrupt bit mask
300 * @hba - Pointer to adapter instance
301 *
302 * Returns interrupt bit mask per version
303 */
304static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
305{
306 if (hba->ufs_version == UFSHCI_VERSION_10)
307 return INTERRUPT_MASK_ALL_VER_10;
308 else
309 return INTERRUPT_MASK_ALL_VER_11;
310}
311
312/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530313 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
314 * @hba - Pointer to adapter instance
315 *
316 * Returns UFSHCI version supported by the controller
317 */
318static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
319{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200320 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
321 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300322
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530323 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530324}
325
326/**
327 * ufshcd_is_device_present - Check if any device connected to
328 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300329 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530330 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530331 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530332 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300333static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530334{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300335 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
336 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530337}
338
339/**
340 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
341 * @lrb: pointer to local command reference block
342 *
343 * This function is used to get the OCS field from UTRD
344 * Returns the OCS field in the UTRD
345 */
346static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
347{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530348 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530349}
350
351/**
352 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
353 * @task_req_descp: pointer to utp_task_req_desc structure
354 *
355 * This function is used to get the OCS field from UTMRD
356 * Returns the OCS field in the UTMRD
357 */
358static inline int
359ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
360{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530361 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530362}
363
364/**
365 * ufshcd_get_tm_free_slot - get a free slot for task management request
366 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530367 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530368 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530369 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
370 * Returns 0 if free slot is not available, else return 1 with tag value
371 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530372 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530373static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530374{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530375 int tag;
376 bool ret = false;
377
378 if (!free_slot)
379 goto out;
380
381 do {
382 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
383 if (tag >= hba->nutmrs)
384 goto out;
385 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
386
387 *free_slot = tag;
388 ret = true;
389out:
390 return ret;
391}
392
393static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
394{
395 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530396}
397
398/**
399 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
400 * @hba: per adapter instance
401 * @pos: position of the bit to be cleared
402 */
403static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
404{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530405 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530406}
407
408/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200409 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
410 * @hba: per adapter instance
411 * @tag: position of the bit to be cleared
412 */
413static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
414{
415 __clear_bit(tag, &hba->outstanding_reqs);
416}
417
418/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530419 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
420 * @reg: Register value of host controller status
421 *
422 * Returns integer, 0 on Success and positive value if failed
423 */
424static inline int ufshcd_get_lists_status(u32 reg)
425{
426 /*
427 * The mask 0xFF is for the following HCS register bits
428 * Bit Description
429 * 0 Device Present
430 * 1 UTRLRDY
431 * 2 UTMRLRDY
432 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200433 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530434 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200435 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530436}
437
438/**
439 * ufshcd_get_uic_cmd_result - Get the UIC command result
440 * @hba: Pointer to adapter instance
441 *
442 * This function gets the result of UIC command completion
443 * Returns 0 on success, non zero value on error
444 */
445static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
446{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530447 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530448 MASK_UIC_COMMAND_RESULT;
449}
450
451/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530452 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
453 * @hba: Pointer to adapter instance
454 *
455 * This function gets UIC command argument3
456 * Returns 0 on success, non zero value on error
457 */
458static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
459{
460 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
461}
462
463/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530464 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530465 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530466 */
467static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530468ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530469{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530470 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530471}
472
473/**
474 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
475 * @ucd_rsp_ptr: pointer to response UPIU
476 *
477 * This function gets the response status and scsi_status from response UPIU
478 * Returns the response result code.
479 */
480static inline int
481ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
482{
483 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
484}
485
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530486/*
487 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
488 * from response UPIU
489 * @ucd_rsp_ptr: pointer to response UPIU
490 *
491 * Return the data segment length.
492 */
493static inline unsigned int
494ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
495{
496 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
497 MASK_RSP_UPIU_DATA_SEG_LEN;
498}
499
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530500/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530501 * ufshcd_is_exception_event - Check if the device raised an exception event
502 * @ucd_rsp_ptr: pointer to response UPIU
503 *
504 * The function checks if the device raised an exception event indicated in
505 * the Device Information field of response UPIU.
506 *
507 * Returns true if exception is raised, false otherwise.
508 */
509static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
510{
511 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
512 MASK_RSP_EXCEPTION_EVENT ? true : false;
513}
514
515/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530516 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530517 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530518 */
519static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530520ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530521{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530522 ufshcd_writel(hba, INT_AGGR_ENABLE |
523 INT_AGGR_COUNTER_AND_TIMER_RESET,
524 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
525}
526
527/**
528 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
529 * @hba: per adapter instance
530 * @cnt: Interrupt aggregation counter threshold
531 * @tmout: Interrupt aggregation timeout value
532 */
533static inline void
534ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
535{
536 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
537 INT_AGGR_COUNTER_THLD_VAL(cnt) |
538 INT_AGGR_TIMEOUT_VAL(tmout),
539 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530540}
541
542/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300543 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
544 * @hba: per adapter instance
545 */
546static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
547{
548 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
549}
550
551/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530552 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
553 * When run-stop registers are set to 1, it indicates the
554 * host controller that it can process the requests
555 * @hba: per adapter instance
556 */
557static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
558{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530559 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
560 REG_UTP_TASK_REQ_LIST_RUN_STOP);
561 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
562 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530563}
564
565/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530566 * ufshcd_hba_start - Start controller initialization sequence
567 * @hba: per adapter instance
568 */
569static inline void ufshcd_hba_start(struct ufs_hba *hba)
570{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530571 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530572}
573
574/**
575 * ufshcd_is_hba_active - Get controller state
576 * @hba: per adapter instance
577 *
578 * Returns zero if controller is active, 1 otherwise
579 */
580static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
581{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530582 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530583}
584
Yaniv Gardi37113102016-03-10 17:37:16 +0200585u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
586{
587 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
588 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
589 (hba->ufs_version == UFSHCI_VERSION_11))
590 return UFS_UNIPRO_VER_1_41;
591 else
592 return UFS_UNIPRO_VER_1_6;
593}
594EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
595
596static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
597{
598 /*
599 * If both host and device support UniPro ver1.6 or later, PA layer
600 * parameters tuning happens during link startup itself.
601 *
602 * We can manually tune PA layer parameters if either host or device
603 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
604 * logic simple, we will only do manual tuning if local unipro version
605 * doesn't support ver1.6 or later.
606 */
607 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
608 return true;
609 else
610 return false;
611}
612
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300613static void ufshcd_ungate_work(struct work_struct *work)
614{
615 int ret;
616 unsigned long flags;
617 struct ufs_hba *hba = container_of(work, struct ufs_hba,
618 clk_gating.ungate_work);
619
620 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
621
622 spin_lock_irqsave(hba->host->host_lock, flags);
623 if (hba->clk_gating.state == CLKS_ON) {
624 spin_unlock_irqrestore(hba->host->host_lock, flags);
625 goto unblock_reqs;
626 }
627
628 spin_unlock_irqrestore(hba->host->host_lock, flags);
629 ufshcd_setup_clocks(hba, true);
630
631 /* Exit from hibern8 */
632 if (ufshcd_can_hibern8_during_gating(hba)) {
633 /* Prevent gating in this path */
634 hba->clk_gating.is_suspended = true;
635 if (ufshcd_is_link_hibern8(hba)) {
636 ret = ufshcd_uic_hibern8_exit(hba);
637 if (ret)
638 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
639 __func__, ret);
640 else
641 ufshcd_set_link_active(hba);
642 }
643 hba->clk_gating.is_suspended = false;
644 }
645unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300646 if (ufshcd_is_clkscaling_enabled(hba))
647 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300648 scsi_unblock_requests(hba->host);
649}
650
651/**
652 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
653 * Also, exit from hibern8 mode and set the link as active.
654 * @hba: per adapter instance
655 * @async: This indicates whether caller should ungate clocks asynchronously.
656 */
657int ufshcd_hold(struct ufs_hba *hba, bool async)
658{
659 int rc = 0;
660 unsigned long flags;
661
662 if (!ufshcd_is_clkgating_allowed(hba))
663 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300664 spin_lock_irqsave(hba->host->host_lock, flags);
665 hba->clk_gating.active_reqs++;
666
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200667 if (ufshcd_eh_in_progress(hba)) {
668 spin_unlock_irqrestore(hba->host->host_lock, flags);
669 return 0;
670 }
671
Sahitya Tummala856b3482014-09-25 15:32:34 +0300672start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300673 switch (hba->clk_gating.state) {
674 case CLKS_ON:
675 break;
676 case REQ_CLKS_OFF:
677 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
678 hba->clk_gating.state = CLKS_ON;
679 break;
680 }
681 /*
682 * If we here, it means gating work is either done or
683 * currently running. Hence, fall through to cancel gating
684 * work and to enable clocks.
685 */
686 case CLKS_OFF:
687 scsi_block_requests(hba->host);
688 hba->clk_gating.state = REQ_CLKS_ON;
689 schedule_work(&hba->clk_gating.ungate_work);
690 /*
691 * fall through to check if we should wait for this
692 * work to be done or not.
693 */
694 case REQ_CLKS_ON:
695 if (async) {
696 rc = -EAGAIN;
697 hba->clk_gating.active_reqs--;
698 break;
699 }
700
701 spin_unlock_irqrestore(hba->host->host_lock, flags);
702 flush_work(&hba->clk_gating.ungate_work);
703 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300704 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300705 goto start;
706 default:
707 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
708 __func__, hba->clk_gating.state);
709 break;
710 }
711 spin_unlock_irqrestore(hba->host->host_lock, flags);
712out:
713 return rc;
714}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200715EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300716
717static void ufshcd_gate_work(struct work_struct *work)
718{
719 struct ufs_hba *hba = container_of(work, struct ufs_hba,
720 clk_gating.gate_work.work);
721 unsigned long flags;
722
723 spin_lock_irqsave(hba->host->host_lock, flags);
724 if (hba->clk_gating.is_suspended) {
725 hba->clk_gating.state = CLKS_ON;
726 goto rel_lock;
727 }
728
729 if (hba->clk_gating.active_reqs
730 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
731 || hba->lrb_in_use || hba->outstanding_tasks
732 || hba->active_uic_cmd || hba->uic_async_done)
733 goto rel_lock;
734
735 spin_unlock_irqrestore(hba->host->host_lock, flags);
736
737 /* put the link into hibern8 mode before turning off clocks */
738 if (ufshcd_can_hibern8_during_gating(hba)) {
739 if (ufshcd_uic_hibern8_enter(hba)) {
740 hba->clk_gating.state = CLKS_ON;
741 goto out;
742 }
743 ufshcd_set_link_hibern8(hba);
744 }
745
Sahitya Tummala856b3482014-09-25 15:32:34 +0300746 if (ufshcd_is_clkscaling_enabled(hba)) {
747 devfreq_suspend_device(hba->devfreq);
748 hba->clk_scaling.window_start_t = 0;
749 }
750
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300751 if (!ufshcd_is_link_active(hba))
752 ufshcd_setup_clocks(hba, false);
753 else
754 /* If link is active, device ref_clk can't be switched off */
755 __ufshcd_setup_clocks(hba, false, true);
756
757 /*
758 * In case you are here to cancel this work the gating state
759 * would be marked as REQ_CLKS_ON. In this case keep the state
760 * as REQ_CLKS_ON which would anyway imply that clocks are off
761 * and a request to turn them on is pending. By doing this way,
762 * we keep the state machine in tact and this would ultimately
763 * prevent from doing cancel work multiple times when there are
764 * new requests arriving before the current cancel work is done.
765 */
766 spin_lock_irqsave(hba->host->host_lock, flags);
767 if (hba->clk_gating.state == REQ_CLKS_OFF)
768 hba->clk_gating.state = CLKS_OFF;
769
770rel_lock:
771 spin_unlock_irqrestore(hba->host->host_lock, flags);
772out:
773 return;
774}
775
776/* host lock must be held before calling this variant */
777static void __ufshcd_release(struct ufs_hba *hba)
778{
779 if (!ufshcd_is_clkgating_allowed(hba))
780 return;
781
782 hba->clk_gating.active_reqs--;
783
784 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
785 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
786 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200787 || hba->active_uic_cmd || hba->uic_async_done
788 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300789 return;
790
791 hba->clk_gating.state = REQ_CLKS_OFF;
792 schedule_delayed_work(&hba->clk_gating.gate_work,
793 msecs_to_jiffies(hba->clk_gating.delay_ms));
794}
795
796void ufshcd_release(struct ufs_hba *hba)
797{
798 unsigned long flags;
799
800 spin_lock_irqsave(hba->host->host_lock, flags);
801 __ufshcd_release(hba);
802 spin_unlock_irqrestore(hba->host->host_lock, flags);
803}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200804EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300805
806static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
807 struct device_attribute *attr, char *buf)
808{
809 struct ufs_hba *hba = dev_get_drvdata(dev);
810
811 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
812}
813
814static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
815 struct device_attribute *attr, const char *buf, size_t count)
816{
817 struct ufs_hba *hba = dev_get_drvdata(dev);
818 unsigned long flags, value;
819
820 if (kstrtoul(buf, 0, &value))
821 return -EINVAL;
822
823 spin_lock_irqsave(hba->host->host_lock, flags);
824 hba->clk_gating.delay_ms = value;
825 spin_unlock_irqrestore(hba->host->host_lock, flags);
826 return count;
827}
828
829static void ufshcd_init_clk_gating(struct ufs_hba *hba)
830{
831 if (!ufshcd_is_clkgating_allowed(hba))
832 return;
833
834 hba->clk_gating.delay_ms = 150;
835 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
836 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
837
838 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
839 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
840 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
841 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
842 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
843 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
844 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
845}
846
847static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
848{
849 if (!ufshcd_is_clkgating_allowed(hba))
850 return;
851 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900852 cancel_work_sync(&hba->clk_gating.ungate_work);
853 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300854}
855
Sahitya Tummala856b3482014-09-25 15:32:34 +0300856/* Must be called with host lock acquired */
857static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
858{
859 if (!ufshcd_is_clkscaling_enabled(hba))
860 return;
861
862 if (!hba->clk_scaling.is_busy_started) {
863 hba->clk_scaling.busy_start_t = ktime_get();
864 hba->clk_scaling.is_busy_started = true;
865 }
866}
867
868static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
869{
870 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
871
872 if (!ufshcd_is_clkscaling_enabled(hba))
873 return;
874
875 if (!hba->outstanding_reqs && scaling->is_busy_started) {
876 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
877 scaling->busy_start_t));
878 scaling->busy_start_t = ktime_set(0, 0);
879 scaling->is_busy_started = false;
880 }
881}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530882/**
883 * ufshcd_send_command - Send SCSI or device management commands
884 * @hba: per adapter instance
885 * @task_tag: Task tag of the command
886 */
887static inline
888void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
889{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300890 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530891 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530892 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530893}
894
895/**
896 * ufshcd_copy_sense_data - Copy sense data in case of check condition
897 * @lrb - pointer to local reference block
898 */
899static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
900{
901 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530902 if (lrbp->sense_buffer &&
903 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530904 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530905 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530906 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530907 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
908 }
909}
910
911/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530912 * ufshcd_copy_query_response() - Copy the Query Response and the data
913 * descriptor
914 * @hba: per adapter instance
915 * @lrb - pointer to local reference block
916 */
917static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300918int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530919{
920 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
921
Dolev Raviv68078d52013-07-30 00:35:58 +0530922 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530923
Dolev Raviv68078d52013-07-30 00:35:58 +0530924 /* Get the descriptor */
925 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300926 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530927 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300928 u16 resp_len;
929 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530930
931 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300932 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530933 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300934 buf_len = be16_to_cpu(
935 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300936 if (likely(buf_len >= resp_len)) {
937 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
938 } else {
939 dev_warn(hba->dev,
940 "%s: Response size is bigger than buffer",
941 __func__);
942 return -EINVAL;
943 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530944 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300945
946 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530947}
948
949/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530950 * ufshcd_hba_capabilities - Read controller capabilities
951 * @hba: per adapter instance
952 */
953static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
954{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530955 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530956
957 /* nutrs and nutmrs are 0 based values */
958 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
959 hba->nutmrs =
960 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
961}
962
963/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530964 * ufshcd_ready_for_uic_cmd - Check if controller is ready
965 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530966 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530967 * Return true on success, else false
968 */
969static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
970{
971 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
972 return true;
973 else
974 return false;
975}
976
977/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530978 * ufshcd_get_upmcrs - Get the power mode change request status
979 * @hba: Pointer to adapter instance
980 *
981 * This function gets the UPMCRS field of HCS register
982 * Returns value of UPMCRS field
983 */
984static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
985{
986 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
987}
988
989/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530990 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
991 * @hba: per adapter instance
992 * @uic_cmd: UIC command
993 *
994 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530995 */
996static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530997ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530998{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530999 WARN_ON(hba->active_uic_cmd);
1000
1001 hba->active_uic_cmd = uic_cmd;
1002
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301003 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301004 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1005 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1006 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301007
1008 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301009 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301010 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301011}
1012
1013/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301014 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1015 * @hba: per adapter instance
1016 * @uic_command: UIC command
1017 *
1018 * Must be called with mutex held.
1019 * Returns 0 only if success.
1020 */
1021static int
1022ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1023{
1024 int ret;
1025 unsigned long flags;
1026
1027 if (wait_for_completion_timeout(&uic_cmd->done,
1028 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1029 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1030 else
1031 ret = -ETIMEDOUT;
1032
1033 spin_lock_irqsave(hba->host->host_lock, flags);
1034 hba->active_uic_cmd = NULL;
1035 spin_unlock_irqrestore(hba->host->host_lock, flags);
1036
1037 return ret;
1038}
1039
1040/**
1041 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1042 * @hba: per adapter instance
1043 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001044 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301045 *
1046 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001047 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301048 * Returns 0 only if success.
1049 */
1050static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001051__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1052 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301053{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301054 if (!ufshcd_ready_for_uic_cmd(hba)) {
1055 dev_err(hba->dev,
1056 "Controller not ready to accept UIC commands\n");
1057 return -EIO;
1058 }
1059
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001060 if (completion)
1061 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301062
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301063 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301064
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001065 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301066}
1067
1068/**
1069 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1070 * @hba: per adapter instance
1071 * @uic_cmd: UIC command
1072 *
1073 * Returns 0 only if success.
1074 */
1075static int
1076ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1077{
1078 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001079 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301080
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001081 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301082 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001083 ufshcd_add_delay_before_dme_cmd(hba);
1084
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001085 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001086 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001087 spin_unlock_irqrestore(hba->host->host_lock, flags);
1088 if (!ret)
1089 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1090
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301091 mutex_unlock(&hba->uic_cmd_mutex);
1092
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001093 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301094 return ret;
1095}
1096
1097/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301098 * ufshcd_map_sg - Map scatter-gather list to prdt
1099 * @lrbp - pointer to local reference block
1100 *
1101 * Returns 0 in case of success, non-zero value in case of failure
1102 */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001103static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301104{
1105 struct ufshcd_sg_entry *prd_table;
1106 struct scatterlist *sg;
1107 struct scsi_cmnd *cmd;
1108 int sg_segments;
1109 int i;
1110
1111 cmd = lrbp->cmd;
1112 sg_segments = scsi_dma_map(cmd);
1113 if (sg_segments < 0)
1114 return sg_segments;
1115
1116 if (sg_segments) {
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001117 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN)
1118 lrbp->utr_descriptor_ptr->prd_table_length =
1119 cpu_to_le16((u16)(sg_segments *
1120 sizeof(struct ufshcd_sg_entry)));
1121 else
1122 lrbp->utr_descriptor_ptr->prd_table_length =
1123 cpu_to_le16((u16) (sg_segments));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301124
1125 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1126
1127 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1128 prd_table[i].size =
1129 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1130 prd_table[i].base_addr =
1131 cpu_to_le32(lower_32_bits(sg->dma_address));
1132 prd_table[i].upper_addr =
1133 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001134 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301135 }
1136 } else {
1137 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1138 }
1139
1140 return 0;
1141}
1142
1143/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301144 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301145 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301146 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301147 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301148static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301149{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301150 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1151
1152 if (hba->ufs_version == UFSHCI_VERSION_10) {
1153 u32 rw;
1154 rw = set & INTERRUPT_MASK_RW_VER_10;
1155 set = rw | ((set ^ intrs) & intrs);
1156 } else {
1157 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301158 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301159
1160 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1161}
1162
1163/**
1164 * ufshcd_disable_intr - disable interrupts
1165 * @hba: per adapter instance
1166 * @intrs: interrupt bits
1167 */
1168static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1169{
1170 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1171
1172 if (hba->ufs_version == UFSHCI_VERSION_10) {
1173 u32 rw;
1174 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1175 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1176 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1177
1178 } else {
1179 set &= ~intrs;
1180 }
1181
1182 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301183}
1184
1185/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301186 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1187 * descriptor according to request
1188 * @lrbp: pointer to local reference block
1189 * @upiu_flags: flags required in the header
1190 * @cmd_dir: requests data direction
1191 */
1192static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001193 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301194{
1195 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1196 u32 data_direction;
1197 u32 dword_0;
1198
1199 if (cmd_dir == DMA_FROM_DEVICE) {
1200 data_direction = UTP_DEVICE_TO_HOST;
1201 *upiu_flags = UPIU_CMD_FLAGS_READ;
1202 } else if (cmd_dir == DMA_TO_DEVICE) {
1203 data_direction = UTP_HOST_TO_DEVICE;
1204 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1205 } else {
1206 data_direction = UTP_NO_DATA_TRANSFER;
1207 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1208 }
1209
1210 dword_0 = data_direction | (lrbp->command_type
1211 << UPIU_COMMAND_TYPE_OFFSET);
1212 if (lrbp->intr_cmd)
1213 dword_0 |= UTP_REQ_DESC_INT_CMD;
1214
1215 /* Transfer request descriptor header fields */
1216 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001217 /* dword_1 is reserved, hence it is set to 0 */
1218 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301219 /*
1220 * assigning invalid value for command status. Controller
1221 * updates OCS on command completion, with the command
1222 * status
1223 */
1224 req_desc->header.dword_2 =
1225 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001226 /* dword_3 is reserved, hence it is set to 0 */
1227 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001228
1229 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301230}
1231
1232/**
1233 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1234 * for scsi commands
1235 * @lrbp - local reference block pointer
1236 * @upiu_flags - flags
1237 */
1238static
1239void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1240{
1241 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001242 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301243
1244 /* command descriptor fields */
1245 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1246 UPIU_TRANSACTION_COMMAND, upiu_flags,
1247 lrbp->lun, lrbp->task_tag);
1248 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1249 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1250
1251 /* Total EHS length and Data segment length will be zero */
1252 ucd_req_ptr->header.dword_2 = 0;
1253
1254 ucd_req_ptr->sc.exp_data_transfer_len =
1255 cpu_to_be32(lrbp->cmd->sdb.length);
1256
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001257 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1258 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1259 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1260
1261 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301262}
1263
Dolev Raviv68078d52013-07-30 00:35:58 +05301264/**
1265 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1266 * for query requsts
1267 * @hba: UFS hba
1268 * @lrbp: local reference block pointer
1269 * @upiu_flags: flags
1270 */
1271static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1272 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1273{
1274 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1275 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301276 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301277 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1278
1279 /* Query request header */
1280 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1281 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1282 lrbp->lun, lrbp->task_tag);
1283 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1284 0, query->request.query_func, 0, 0);
1285
Zang Leigang68612852016-08-25 17:39:19 +08001286 /* Data segment length only need for WRITE_DESC */
1287 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1288 ucd_req_ptr->header.dword_2 =
1289 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1290 else
1291 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301292
1293 /* Copy the Query Request buffer as is */
1294 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1295 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301296
1297 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001298 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1299 memcpy(descp, query->descriptor, len);
1300
Yaniv Gardi51047262016-02-01 15:02:38 +02001301 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301302}
1303
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301304static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1305{
1306 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1307
1308 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1309
1310 /* command descriptor fields */
1311 ucd_req_ptr->header.dword_0 =
1312 UPIU_HEADER_DWORD(
1313 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001314 /* clear rest of the fields of basic header */
1315 ucd_req_ptr->header.dword_1 = 0;
1316 ucd_req_ptr->header.dword_2 = 0;
1317
1318 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301319}
1320
1321/**
Joao Pinto300bb132016-05-11 12:21:27 +01001322 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1323 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301324 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301325 * @lrb - pointer to local reference block
1326 */
Joao Pinto300bb132016-05-11 12:21:27 +01001327static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301328{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301329 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301330 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301331
Joao Pinto300bb132016-05-11 12:21:27 +01001332 if (hba->ufs_version == UFSHCI_VERSION_20)
1333 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1334 else
1335 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1336
1337 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1338 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1339 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1340 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1341 ufshcd_prepare_utp_nop_upiu(lrbp);
1342 else
1343 ret = -EINVAL;
1344
1345 return ret;
1346}
1347
1348/**
1349 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1350 * for SCSI Purposes
1351 * @hba - per adapter instance
1352 * @lrb - pointer to local reference block
1353 */
1354static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1355{
1356 u32 upiu_flags;
1357 int ret = 0;
1358
1359 if (hba->ufs_version == UFSHCI_VERSION_20)
1360 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1361 else
1362 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1363
1364 if (likely(lrbp->cmd)) {
1365 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1366 lrbp->cmd->sc_data_direction);
1367 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1368 } else {
1369 ret = -EINVAL;
1370 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301371
1372 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301373}
1374
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001375/*
1376 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1377 * @scsi_lun: scsi LUN id
1378 *
1379 * Returns UPIU LUN id
1380 */
1381static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1382{
1383 if (scsi_is_wlun(scsi_lun))
1384 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1385 | UFS_UPIU_WLUN_ID;
1386 else
1387 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1388}
1389
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301390/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001391 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1392 * @scsi_lun: UPIU W-LUN id
1393 *
1394 * Returns SCSI W-LUN id
1395 */
1396static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1397{
1398 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1399}
1400
1401/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301402 * ufshcd_queuecommand - main entry point for SCSI requests
1403 * @cmd: command from SCSI Midlayer
1404 * @done: call back function
1405 *
1406 * Returns 0 for success, non-zero in case of failure
1407 */
1408static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1409{
1410 struct ufshcd_lrb *lrbp;
1411 struct ufs_hba *hba;
1412 unsigned long flags;
1413 int tag;
1414 int err = 0;
1415
1416 hba = shost_priv(host);
1417
1418 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001419 if (!ufshcd_valid_tag(hba, tag)) {
1420 dev_err(hba->dev,
1421 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1422 __func__, tag, cmd, cmd->request);
1423 BUG();
1424 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301425
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301426 spin_lock_irqsave(hba->host->host_lock, flags);
1427 switch (hba->ufshcd_state) {
1428 case UFSHCD_STATE_OPERATIONAL:
1429 break;
Zang Leiganga17bddc2017-04-04 19:32:20 +00001430 case UFSHCD_STATE_EH_SCHEDULED:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301431 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301432 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301433 goto out_unlock;
1434 case UFSHCD_STATE_ERROR:
1435 set_host_byte(cmd, DID_ERROR);
1436 cmd->scsi_done(cmd);
1437 goto out_unlock;
1438 default:
1439 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1440 __func__, hba->ufshcd_state);
1441 set_host_byte(cmd, DID_BAD_TARGET);
1442 cmd->scsi_done(cmd);
1443 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301444 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001445
1446 /* if error handling is in progress, don't issue commands */
1447 if (ufshcd_eh_in_progress(hba)) {
1448 set_host_byte(cmd, DID_ERROR);
1449 cmd->scsi_done(cmd);
1450 goto out_unlock;
1451 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301452 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301453
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301454 /* acquire the tag to make sure device cmds don't use it */
1455 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1456 /*
1457 * Dev manage command in progress, requeue the command.
1458 * Requeuing the command helps in cases where the request *may*
1459 * find different tag instead of waiting for dev manage command
1460 * completion.
1461 */
1462 err = SCSI_MLQUEUE_HOST_BUSY;
1463 goto out;
1464 }
1465
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001466 err = ufshcd_hold(hba, true);
1467 if (err) {
1468 err = SCSI_MLQUEUE_HOST_BUSY;
1469 clear_bit_unlock(tag, &hba->lrb_in_use);
1470 goto out;
1471 }
1472 WARN_ON(hba->clk_gating.state != CLKS_ON);
1473
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301474 lrbp = &hba->lrb[tag];
1475
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301476 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301477 lrbp->cmd = cmd;
1478 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1479 lrbp->sense_buffer = cmd->sense_buffer;
1480 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001481 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001482 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301483
Joao Pinto300bb132016-05-11 12:21:27 +01001484 ufshcd_comp_scsi_upiu(hba, lrbp);
1485
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00001486 err = ufshcd_map_sg(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301487 if (err) {
1488 lrbp->cmd = NULL;
1489 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301490 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301491 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301492
1493 /* issue command to the controller */
1494 spin_lock_irqsave(hba->host->host_lock, flags);
1495 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301496out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301497 spin_unlock_irqrestore(hba->host->host_lock, flags);
1498out:
1499 return err;
1500}
1501
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301502static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1503 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1504{
1505 lrbp->cmd = NULL;
1506 lrbp->sense_bufflen = 0;
1507 lrbp->sense_buffer = NULL;
1508 lrbp->task_tag = tag;
1509 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301510 lrbp->intr_cmd = true; /* No interrupt aggregation */
1511 hba->dev_cmd.type = cmd_type;
1512
Joao Pinto300bb132016-05-11 12:21:27 +01001513 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301514}
1515
1516static int
1517ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1518{
1519 int err = 0;
1520 unsigned long flags;
1521 u32 mask = 1 << tag;
1522
1523 /* clear outstanding transaction before retry */
1524 spin_lock_irqsave(hba->host->host_lock, flags);
1525 ufshcd_utrl_clear(hba, tag);
1526 spin_unlock_irqrestore(hba->host->host_lock, flags);
1527
1528 /*
1529 * wait for for h/w to clear corresponding bit in door-bell.
1530 * max. wait is 1 sec.
1531 */
1532 err = ufshcd_wait_for_register(hba,
1533 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001534 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301535
1536 return err;
1537}
1538
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001539static int
1540ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1541{
1542 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1543
1544 /* Get the UPIU response */
1545 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1546 UPIU_RSP_CODE_OFFSET;
1547 return query_res->response;
1548}
1549
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301550/**
1551 * ufshcd_dev_cmd_completion() - handles device management command responses
1552 * @hba: per adapter instance
1553 * @lrbp: pointer to local reference block
1554 */
1555static int
1556ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1557{
1558 int resp;
1559 int err = 0;
1560
1561 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1562
1563 switch (resp) {
1564 case UPIU_TRANSACTION_NOP_IN:
1565 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1566 err = -EINVAL;
1567 dev_err(hba->dev, "%s: unexpected response %x\n",
1568 __func__, resp);
1569 }
1570 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301571 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001572 err = ufshcd_check_query_response(hba, lrbp);
1573 if (!err)
1574 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301575 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301576 case UPIU_TRANSACTION_REJECT_UPIU:
1577 /* TODO: handle Reject UPIU Response */
1578 err = -EPERM;
1579 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1580 __func__);
1581 break;
1582 default:
1583 err = -EINVAL;
1584 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1585 __func__, resp);
1586 break;
1587 }
1588
1589 return err;
1590}
1591
1592static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1593 struct ufshcd_lrb *lrbp, int max_timeout)
1594{
1595 int err = 0;
1596 unsigned long time_left;
1597 unsigned long flags;
1598
1599 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1600 msecs_to_jiffies(max_timeout));
1601
1602 spin_lock_irqsave(hba->host->host_lock, flags);
1603 hba->dev_cmd.complete = NULL;
1604 if (likely(time_left)) {
1605 err = ufshcd_get_tr_ocs(lrbp);
1606 if (!err)
1607 err = ufshcd_dev_cmd_completion(hba, lrbp);
1608 }
1609 spin_unlock_irqrestore(hba->host->host_lock, flags);
1610
1611 if (!time_left) {
1612 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001613 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1614 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301615 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001616 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301617 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001618 /*
1619 * in case of an error, after clearing the doorbell,
1620 * we also need to clear the outstanding_request
1621 * field in hba
1622 */
1623 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301624 }
1625
1626 return err;
1627}
1628
1629/**
1630 * ufshcd_get_dev_cmd_tag - Get device management command tag
1631 * @hba: per-adapter instance
1632 * @tag: pointer to variable with available slot value
1633 *
1634 * Get a free slot and lock it until device management command
1635 * completes.
1636 *
1637 * Returns false if free slot is unavailable for locking, else
1638 * return true with tag value in @tag.
1639 */
1640static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1641{
1642 int tag;
1643 bool ret = false;
1644 unsigned long tmp;
1645
1646 if (!tag_out)
1647 goto out;
1648
1649 do {
1650 tmp = ~hba->lrb_in_use;
1651 tag = find_last_bit(&tmp, hba->nutrs);
1652 if (tag >= hba->nutrs)
1653 goto out;
1654 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1655
1656 *tag_out = tag;
1657 ret = true;
1658out:
1659 return ret;
1660}
1661
1662static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1663{
1664 clear_bit_unlock(tag, &hba->lrb_in_use);
1665}
1666
1667/**
1668 * ufshcd_exec_dev_cmd - API for sending device management requests
1669 * @hba - UFS hba
1670 * @cmd_type - specifies the type (NOP, Query...)
1671 * @timeout - time in seconds
1672 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301673 * NOTE: Since there is only one available tag for device management commands,
1674 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301675 */
1676static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1677 enum dev_cmd_type cmd_type, int timeout)
1678{
1679 struct ufshcd_lrb *lrbp;
1680 int err;
1681 int tag;
1682 struct completion wait;
1683 unsigned long flags;
1684
1685 /*
1686 * Get free slot, sleep if slots are unavailable.
1687 * Even though we use wait_event() which sleeps indefinitely,
1688 * the maximum wait time is bounded by SCSI request timeout.
1689 */
1690 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1691
1692 init_completion(&wait);
1693 lrbp = &hba->lrb[tag];
1694 WARN_ON(lrbp->cmd);
1695 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1696 if (unlikely(err))
1697 goto out_put_tag;
1698
1699 hba->dev_cmd.complete = &wait;
1700
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001701 /* Make sure descriptors are ready before ringing the doorbell */
1702 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301703 spin_lock_irqsave(hba->host->host_lock, flags);
1704 ufshcd_send_command(hba, tag);
1705 spin_unlock_irqrestore(hba->host->host_lock, flags);
1706
1707 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1708
1709out_put_tag:
1710 ufshcd_put_dev_cmd_tag(hba, tag);
1711 wake_up(&hba->dev_cmd.tag_wq);
1712 return err;
1713}
1714
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301715/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001716 * ufshcd_init_query() - init the query response and request parameters
1717 * @hba: per-adapter instance
1718 * @request: address of the request pointer to be initialized
1719 * @response: address of the response pointer to be initialized
1720 * @opcode: operation to perform
1721 * @idn: flag idn to access
1722 * @index: LU number to access
1723 * @selector: query/flag/descriptor further identification
1724 */
1725static inline void ufshcd_init_query(struct ufs_hba *hba,
1726 struct ufs_query_req **request, struct ufs_query_res **response,
1727 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1728{
1729 *request = &hba->dev_cmd.query.request;
1730 *response = &hba->dev_cmd.query.response;
1731 memset(*request, 0, sizeof(struct ufs_query_req));
1732 memset(*response, 0, sizeof(struct ufs_query_res));
1733 (*request)->upiu_req.opcode = opcode;
1734 (*request)->upiu_req.idn = idn;
1735 (*request)->upiu_req.index = index;
1736 (*request)->upiu_req.selector = selector;
1737}
1738
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001739static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1740 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1741{
1742 int ret;
1743 int retries;
1744
1745 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1746 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1747 if (ret)
1748 dev_dbg(hba->dev,
1749 "%s: failed with error %d, retries %d\n",
1750 __func__, ret, retries);
1751 else
1752 break;
1753 }
1754
1755 if (ret)
1756 dev_err(hba->dev,
1757 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1758 __func__, opcode, idn, ret, retries);
1759 return ret;
1760}
1761
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001762/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301763 * ufshcd_query_flag() - API function for sending flag query requests
1764 * hba: per-adapter instance
1765 * query_opcode: flag query to perform
1766 * idn: flag idn to access
1767 * flag_res: the flag value after the query request completes
1768 *
1769 * Returns 0 for success, non-zero in case of failure
1770 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001771int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301772 enum flag_idn idn, bool *flag_res)
1773{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001774 struct ufs_query_req *request = NULL;
1775 struct ufs_query_res *response = NULL;
1776 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001777 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301778
1779 BUG_ON(!hba);
1780
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001781 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301782 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001783 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1784 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301785
1786 switch (opcode) {
1787 case UPIU_QUERY_OPCODE_SET_FLAG:
1788 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1789 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1790 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1791 break;
1792 case UPIU_QUERY_OPCODE_READ_FLAG:
1793 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1794 if (!flag_res) {
1795 /* No dummy reads */
1796 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1797 __func__);
1798 err = -EINVAL;
1799 goto out_unlock;
1800 }
1801 break;
1802 default:
1803 dev_err(hba->dev,
1804 "%s: Expected query flag opcode but got = %d\n",
1805 __func__, opcode);
1806 err = -EINVAL;
1807 goto out_unlock;
1808 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301809
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001810 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1811 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1812
1813 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301814
1815 if (err) {
1816 dev_err(hba->dev,
1817 "%s: Sending flag query for idn %d failed, err = %d\n",
1818 __func__, idn, err);
1819 goto out_unlock;
1820 }
1821
1822 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301823 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301824 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1825
1826out_unlock:
1827 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001828 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301829 return err;
1830}
1831
1832/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301833 * ufshcd_query_attr - API function for sending attribute requests
1834 * hba: per-adapter instance
1835 * opcode: attribute opcode
1836 * idn: attribute idn to access
1837 * index: index field
1838 * selector: selector field
1839 * attr_val: the attribute value after the query request completes
1840 *
1841 * Returns 0 for success, non-zero in case of failure
1842*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301843static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301844 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1845{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001846 struct ufs_query_req *request = NULL;
1847 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301848 int err;
1849
1850 BUG_ON(!hba);
1851
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001852 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301853 if (!attr_val) {
1854 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1855 __func__, opcode);
1856 err = -EINVAL;
1857 goto out;
1858 }
1859
1860 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001861 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1862 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301863
1864 switch (opcode) {
1865 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1866 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301867 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301868 break;
1869 case UPIU_QUERY_OPCODE_READ_ATTR:
1870 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1871 break;
1872 default:
1873 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1874 __func__, opcode);
1875 err = -EINVAL;
1876 goto out_unlock;
1877 }
1878
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001879 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301880
1881 if (err) {
1882 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1883 __func__, opcode, idn, err);
1884 goto out_unlock;
1885 }
1886
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301887 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301888
1889out_unlock:
1890 mutex_unlock(&hba->dev_cmd.lock);
1891out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001892 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301893 return err;
1894}
1895
1896/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001897 * ufshcd_query_attr_retry() - API function for sending query
1898 * attribute with retries
1899 * @hba: per-adapter instance
1900 * @opcode: attribute opcode
1901 * @idn: attribute idn to access
1902 * @index: index field
1903 * @selector: selector field
1904 * @attr_val: the attribute value after the query request
1905 * completes
1906 *
1907 * Returns 0 for success, non-zero in case of failure
1908*/
1909static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1910 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1911 u32 *attr_val)
1912{
1913 int ret = 0;
1914 u32 retries;
1915
1916 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1917 ret = ufshcd_query_attr(hba, opcode, idn, index,
1918 selector, attr_val);
1919 if (ret)
1920 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1921 __func__, ret, retries);
1922 else
1923 break;
1924 }
1925
1926 if (ret)
1927 dev_err(hba->dev,
1928 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1929 __func__, idn, ret, QUERY_REQ_RETRIES);
1930 return ret;
1931}
1932
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001933static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001934 enum query_opcode opcode, enum desc_idn idn, u8 index,
1935 u8 selector, u8 *desc_buf, int *buf_len)
1936{
1937 struct ufs_query_req *request = NULL;
1938 struct ufs_query_res *response = NULL;
1939 int err;
1940
1941 BUG_ON(!hba);
1942
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001943 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001944 if (!desc_buf) {
1945 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1946 __func__, opcode);
1947 err = -EINVAL;
1948 goto out;
1949 }
1950
Potomski, MichalXe1928452017-02-23 09:05:30 +00001951 if (*buf_len < QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001952 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1953 __func__, *buf_len);
1954 err = -EINVAL;
1955 goto out;
1956 }
1957
1958 mutex_lock(&hba->dev_cmd.lock);
1959 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1960 selector);
1961 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001962 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001963
1964 switch (opcode) {
1965 case UPIU_QUERY_OPCODE_WRITE_DESC:
1966 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1967 break;
1968 case UPIU_QUERY_OPCODE_READ_DESC:
1969 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1970 break;
1971 default:
1972 dev_err(hba->dev,
1973 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1974 __func__, opcode);
1975 err = -EINVAL;
1976 goto out_unlock;
1977 }
1978
1979 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1980
1981 if (err) {
1982 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1983 __func__, opcode, idn, err);
1984 goto out_unlock;
1985 }
1986
1987 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001988 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001989
1990out_unlock:
1991 mutex_unlock(&hba->dev_cmd.lock);
1992out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001993 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001994 return err;
1995}
1996
1997/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001998 * ufshcd_query_descriptor_retry - API function for sending descriptor
1999 * requests
2000 * hba: per-adapter instance
2001 * opcode: attribute opcode
2002 * idn: attribute idn to access
2003 * index: index field
2004 * selector: selector field
2005 * desc_buf: the buffer that contains the descriptor
2006 * buf_len: length parameter passed to the device
2007 *
2008 * Returns 0 for success, non-zero in case of failure.
2009 * The buf_len parameter will contain, on return, the length parameter
2010 * received on the response.
2011 */
2012int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2013 enum query_opcode opcode, enum desc_idn idn, u8 index,
2014 u8 selector, u8 *desc_buf, int *buf_len)
2015{
2016 int err;
2017 int retries;
2018
2019 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2020 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2021 selector, desc_buf, buf_len);
2022 if (!err || err == -EINVAL)
2023 break;
2024 }
2025
2026 return err;
2027}
2028EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2029
2030/**
Potomski, MichalXe1928452017-02-23 09:05:30 +00002031 * ufshcd_read_desc_length - read the specified descriptor length from header
2032 * @hba: Pointer to adapter instance
2033 * @desc_id: descriptor idn value
2034 * @desc_index: descriptor index
2035 * @desc_length: pointer to variable to read the length of descriptor
2036 *
2037 * Return 0 in case of success, non-zero otherwise
2038 */
2039static int ufshcd_read_desc_length(struct ufs_hba *hba,
2040 enum desc_idn desc_id,
2041 int desc_index,
2042 int *desc_length)
2043{
2044 int ret;
2045 u8 header[QUERY_DESC_HDR_SIZE];
2046 int header_len = QUERY_DESC_HDR_SIZE;
2047
2048 if (desc_id >= QUERY_DESC_IDN_MAX)
2049 return -EINVAL;
2050
2051 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2052 desc_id, desc_index, 0, header,
2053 &header_len);
2054
2055 if (ret) {
2056 dev_err(hba->dev, "%s: Failed to get descriptor header id %d",
2057 __func__, desc_id);
2058 return ret;
2059 } else if (desc_id != header[QUERY_DESC_DESC_TYPE_OFFSET]) {
2060 dev_warn(hba->dev, "%s: descriptor header id %d and desc_id %d mismatch",
2061 __func__, header[QUERY_DESC_DESC_TYPE_OFFSET],
2062 desc_id);
2063 ret = -EINVAL;
2064 }
2065
2066 *desc_length = header[QUERY_DESC_LENGTH_OFFSET];
2067 return ret;
2068
2069}
2070
2071/**
2072 * ufshcd_map_desc_id_to_length - map descriptor IDN to its length
2073 * @hba: Pointer to adapter instance
2074 * @desc_id: descriptor idn value
2075 * @desc_len: mapped desc length (out)
2076 *
2077 * Return 0 in case of success, non-zero otherwise
2078 */
2079int ufshcd_map_desc_id_to_length(struct ufs_hba *hba,
2080 enum desc_idn desc_id, int *desc_len)
2081{
2082 switch (desc_id) {
2083 case QUERY_DESC_IDN_DEVICE:
2084 *desc_len = hba->desc_size.dev_desc;
2085 break;
2086 case QUERY_DESC_IDN_POWER:
2087 *desc_len = hba->desc_size.pwr_desc;
2088 break;
2089 case QUERY_DESC_IDN_GEOMETRY:
2090 *desc_len = hba->desc_size.geom_desc;
2091 break;
2092 case QUERY_DESC_IDN_CONFIGURATION:
2093 *desc_len = hba->desc_size.conf_desc;
2094 break;
2095 case QUERY_DESC_IDN_UNIT:
2096 *desc_len = hba->desc_size.unit_desc;
2097 break;
2098 case QUERY_DESC_IDN_INTERCONNECT:
2099 *desc_len = hba->desc_size.interc_desc;
2100 break;
2101 case QUERY_DESC_IDN_STRING:
2102 *desc_len = QUERY_DESC_MAX_SIZE;
2103 break;
2104 case QUERY_DESC_IDN_RFU_0:
2105 case QUERY_DESC_IDN_RFU_1:
2106 *desc_len = 0;
2107 break;
2108 default:
2109 *desc_len = 0;
2110 return -EINVAL;
2111 }
2112 return 0;
2113}
2114EXPORT_SYMBOL(ufshcd_map_desc_id_to_length);
2115
2116/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002117 * ufshcd_read_desc_param - read the specified descriptor parameter
2118 * @hba: Pointer to adapter instance
2119 * @desc_id: descriptor idn value
2120 * @desc_index: descriptor index
2121 * @param_offset: offset of the parameter to read
2122 * @param_read_buf: pointer to buffer where parameter would be read
2123 * @param_size: sizeof(param_read_buf)
2124 *
2125 * Return 0 in case of success, non-zero otherwise
2126 */
2127static int ufshcd_read_desc_param(struct ufs_hba *hba,
2128 enum desc_idn desc_id,
2129 int desc_index,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002130 u8 param_offset,
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002131 u8 *param_read_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002132 u8 param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002133{
2134 int ret;
2135 u8 *desc_buf;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002136 int buff_len;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002137 bool is_kmalloc = true;
2138
Potomski, MichalXe1928452017-02-23 09:05:30 +00002139 /* Safety check */
2140 if (desc_id >= QUERY_DESC_IDN_MAX || !param_size)
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002141 return -EINVAL;
2142
Potomski, MichalXe1928452017-02-23 09:05:30 +00002143 /* Get the max length of descriptor from structure filled up at probe
2144 * time.
2145 */
2146 ret = ufshcd_map_desc_id_to_length(hba, desc_id, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002147
Potomski, MichalXe1928452017-02-23 09:05:30 +00002148 /* Sanity checks */
2149 if (ret || !buff_len) {
2150 dev_err(hba->dev, "%s: Failed to get full descriptor length",
2151 __func__);
2152 return ret;
2153 }
2154
2155 /* Check whether we need temp memory */
2156 if (param_offset != 0 || param_size < buff_len) {
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002157 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2158 if (!desc_buf)
2159 return -ENOMEM;
Potomski, MichalXe1928452017-02-23 09:05:30 +00002160 } else {
2161 desc_buf = param_read_buf;
2162 is_kmalloc = false;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002163 }
2164
Potomski, MichalXe1928452017-02-23 09:05:30 +00002165 /* Request for full descriptor */
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002166 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
Potomski, MichalXe1928452017-02-23 09:05:30 +00002167 desc_id, desc_index, 0,
2168 desc_buf, &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002169
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002170 if (ret) {
2171 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2172 __func__, desc_id, desc_index, param_offset, ret);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002173 goto out;
2174 }
2175
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002176 /* Sanity check */
2177 if (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id) {
2178 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header",
2179 __func__, desc_buf[QUERY_DESC_DESC_TYPE_OFFSET]);
2180 ret = -EINVAL;
2181 goto out;
2182 }
2183
Potomski, MichalXe1928452017-02-23 09:05:30 +00002184 /* Check wherher we will not copy more data, than available */
2185 if (is_kmalloc && param_size > buff_len)
2186 param_size = buff_len;
Subhash Jadavanib0a12b42016-11-23 16:31:41 -08002187
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002188 if (is_kmalloc)
2189 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2190out:
2191 if (is_kmalloc)
2192 kfree(desc_buf);
2193 return ret;
2194}
2195
2196static inline int ufshcd_read_desc(struct ufs_hba *hba,
2197 enum desc_idn desc_id,
2198 int desc_index,
2199 u8 *buf,
2200 u32 size)
2201{
2202 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2203}
2204
2205static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2206 u8 *buf,
2207 u32 size)
2208{
2209 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2210}
2211
Yaniv Gardib573d482016-03-10 17:37:09 +02002212int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2213{
2214 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2215}
2216EXPORT_SYMBOL(ufshcd_read_device_desc);
2217
2218/**
2219 * ufshcd_read_string_desc - read string descriptor
2220 * @hba: pointer to adapter instance
2221 * @desc_index: descriptor index
2222 * @buf: pointer to buffer where descriptor would be read
2223 * @size: size of buf
2224 * @ascii: if true convert from unicode to ascii characters
2225 *
2226 * Return 0 in case of success, non-zero otherwise
2227 */
2228int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2229 u32 size, bool ascii)
2230{
2231 int err = 0;
2232
2233 err = ufshcd_read_desc(hba,
2234 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2235
2236 if (err) {
2237 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2238 __func__, QUERY_REQ_RETRIES, err);
2239 goto out;
2240 }
2241
2242 if (ascii) {
2243 int desc_len;
2244 int ascii_len;
2245 int i;
2246 char *buff_ascii;
2247
2248 desc_len = buf[0];
2249 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2250 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2251 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2252 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2253 __func__);
2254 err = -ENOMEM;
2255 goto out;
2256 }
2257
2258 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2259 if (!buff_ascii) {
2260 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002261 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002262 }
2263
2264 /*
2265 * the descriptor contains string in UTF16 format
2266 * we need to convert to utf-8 so it can be displayed
2267 */
2268 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2269 desc_len - QUERY_DESC_HDR_SIZE,
2270 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2271
2272 /* replace non-printable or non-ASCII characters with spaces */
2273 for (i = 0; i < ascii_len; i++)
2274 ufshcd_remove_non_printable(&buff_ascii[i]);
2275
2276 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2277 size - QUERY_DESC_HDR_SIZE);
2278 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2279 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002280 kfree(buff_ascii);
2281 }
2282out:
2283 return err;
2284}
2285EXPORT_SYMBOL(ufshcd_read_string_desc);
2286
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002287/**
2288 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2289 * @hba: Pointer to adapter instance
2290 * @lun: lun id
2291 * @param_offset: offset of the parameter to read
2292 * @param_read_buf: pointer to buffer where parameter would be read
2293 * @param_size: sizeof(param_read_buf)
2294 *
2295 * Return 0 in case of success, non-zero otherwise
2296 */
2297static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2298 int lun,
2299 enum unit_desc_param param_offset,
2300 u8 *param_read_buf,
2301 u32 param_size)
2302{
2303 /*
2304 * Unit descriptors are only available for general purpose LUs (LUN id
2305 * from 0 to 7) and RPMB Well known LU.
2306 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002307 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002308 return -EOPNOTSUPP;
2309
2310 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2311 param_offset, param_read_buf, param_size);
2312}
2313
2314/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302315 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2316 * @hba: per adapter instance
2317 *
2318 * 1. Allocate DMA memory for Command Descriptor array
2319 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2320 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2321 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2322 * (UTMRDL)
2323 * 4. Allocate memory for local reference block(lrb).
2324 *
2325 * Returns 0 for success, non-zero in case of failure
2326 */
2327static int ufshcd_memory_alloc(struct ufs_hba *hba)
2328{
2329 size_t utmrdl_size, utrdl_size, ucdl_size;
2330
2331 /* Allocate memory for UTP command descriptors */
2332 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002333 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2334 ucdl_size,
2335 &hba->ucdl_dma_addr,
2336 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302337
2338 /*
2339 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2340 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2341 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2342 * be aligned to 128 bytes as well
2343 */
2344 if (!hba->ucdl_base_addr ||
2345 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302346 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302347 "Command Descriptor Memory allocation failed\n");
2348 goto out;
2349 }
2350
2351 /*
2352 * Allocate memory for UTP Transfer descriptors
2353 * UFSHCI requires 1024 byte alignment of UTRD
2354 */
2355 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002356 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2357 utrdl_size,
2358 &hba->utrdl_dma_addr,
2359 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302360 if (!hba->utrdl_base_addr ||
2361 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302362 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302363 "Transfer Descriptor Memory allocation failed\n");
2364 goto out;
2365 }
2366
2367 /*
2368 * Allocate memory for UTP Task Management descriptors
2369 * UFSHCI requires 1024 byte alignment of UTMRD
2370 */
2371 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002372 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2373 utmrdl_size,
2374 &hba->utmrdl_dma_addr,
2375 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302376 if (!hba->utmrdl_base_addr ||
2377 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302378 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302379 "Task Management Descriptor Memory allocation failed\n");
2380 goto out;
2381 }
2382
2383 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002384 hba->lrb = devm_kzalloc(hba->dev,
2385 hba->nutrs * sizeof(struct ufshcd_lrb),
2386 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302387 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302388 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302389 goto out;
2390 }
2391 return 0;
2392out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302393 return -ENOMEM;
2394}
2395
2396/**
2397 * ufshcd_host_memory_configure - configure local reference block with
2398 * memory offsets
2399 * @hba: per adapter instance
2400 *
2401 * Configure Host memory space
2402 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2403 * address.
2404 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2405 * and PRDT offset.
2406 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2407 * into local reference block.
2408 */
2409static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2410{
2411 struct utp_transfer_cmd_desc *cmd_descp;
2412 struct utp_transfer_req_desc *utrdlp;
2413 dma_addr_t cmd_desc_dma_addr;
2414 dma_addr_t cmd_desc_element_addr;
2415 u16 response_offset;
2416 u16 prdt_offset;
2417 int cmd_desc_size;
2418 int i;
2419
2420 utrdlp = hba->utrdl_base_addr;
2421 cmd_descp = hba->ucdl_base_addr;
2422
2423 response_offset =
2424 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2425 prdt_offset =
2426 offsetof(struct utp_transfer_cmd_desc, prd_table);
2427
2428 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2429 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2430
2431 for (i = 0; i < hba->nutrs; i++) {
2432 /* Configure UTRD with command descriptor base address */
2433 cmd_desc_element_addr =
2434 (cmd_desc_dma_addr + (cmd_desc_size * i));
2435 utrdlp[i].command_desc_base_addr_lo =
2436 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2437 utrdlp[i].command_desc_base_addr_hi =
2438 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2439
2440 /* Response upiu and prdt offset should be in double words */
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002441 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) {
2442 utrdlp[i].response_upiu_offset =
2443 cpu_to_le16(response_offset);
2444 utrdlp[i].prd_table_offset =
2445 cpu_to_le16(prdt_offset);
2446 utrdlp[i].response_upiu_length =
2447 cpu_to_le16(ALIGNED_UPIU_SIZE);
2448 } else {
2449 utrdlp[i].response_upiu_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302450 cpu_to_le16((response_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002451 utrdlp[i].prd_table_offset =
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302452 cpu_to_le16((prdt_offset >> 2));
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002453 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302454 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Kiwoong Kim9b41ed72017-04-04 19:32:05 +00002455 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302456
2457 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302458 hba->lrb[i].ucd_req_ptr =
2459 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302460 hba->lrb[i].ucd_rsp_ptr =
2461 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2462 hba->lrb[i].ucd_prdt_ptr =
2463 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2464 }
2465}
2466
2467/**
2468 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2469 * @hba: per adapter instance
2470 *
2471 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2472 * in order to initialize the Unipro link startup procedure.
2473 * Once the Unipro links are up, the device connected to the controller
2474 * is detected.
2475 *
2476 * Returns 0 on success, non-zero value on failure
2477 */
2478static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2479{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302480 struct uic_command uic_cmd = {0};
2481 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302482
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302483 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2484
2485 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2486 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302487 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302488 "dme-link-startup: error code %d\n", ret);
2489 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302490}
2491
Yaniv Gardicad2e032015-03-31 17:37:14 +03002492static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2493{
2494 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2495 unsigned long min_sleep_time_us;
2496
2497 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2498 return;
2499
2500 /*
2501 * last_dme_cmd_tstamp will be 0 only for 1st call to
2502 * this function
2503 */
2504 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2505 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2506 } else {
2507 unsigned long delta =
2508 (unsigned long) ktime_to_us(
2509 ktime_sub(ktime_get(),
2510 hba->last_dme_cmd_tstamp));
2511
2512 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2513 min_sleep_time_us =
2514 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2515 else
2516 return; /* no more delay required */
2517 }
2518
2519 /* allow sleep for extra 50us if needed */
2520 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2521}
2522
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302523/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302524 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2525 * @hba: per adapter instance
2526 * @attr_sel: uic command argument1
2527 * @attr_set: attribute set type as uic command argument2
2528 * @mib_val: setting value as uic command argument3
2529 * @peer: indicate whether peer or local
2530 *
2531 * Returns 0 on success, non-zero value on failure
2532 */
2533int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2534 u8 attr_set, u32 mib_val, u8 peer)
2535{
2536 struct uic_command uic_cmd = {0};
2537 static const char *const action[] = {
2538 "dme-set",
2539 "dme-peer-set"
2540 };
2541 const char *set = action[!!peer];
2542 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002543 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302544
2545 uic_cmd.command = peer ?
2546 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2547 uic_cmd.argument1 = attr_sel;
2548 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2549 uic_cmd.argument3 = mib_val;
2550
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002551 do {
2552 /* for peer attributes we retry upon failure */
2553 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2554 if (ret)
2555 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2556 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2557 } while (ret && peer && --retries);
2558
2559 if (!retries)
2560 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2561 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2562 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302563
2564 return ret;
2565}
2566EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2567
2568/**
2569 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2570 * @hba: per adapter instance
2571 * @attr_sel: uic command argument1
2572 * @mib_val: the value of the attribute as returned by the UIC command
2573 * @peer: indicate whether peer or local
2574 *
2575 * Returns 0 on success, non-zero value on failure
2576 */
2577int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2578 u32 *mib_val, u8 peer)
2579{
2580 struct uic_command uic_cmd = {0};
2581 static const char *const action[] = {
2582 "dme-get",
2583 "dme-peer-get"
2584 };
2585 const char *get = action[!!peer];
2586 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002587 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002588 struct ufs_pa_layer_attr orig_pwr_info;
2589 struct ufs_pa_layer_attr temp_pwr_info;
2590 bool pwr_mode_change = false;
2591
2592 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2593 orig_pwr_info = hba->pwr_info;
2594 temp_pwr_info = orig_pwr_info;
2595
2596 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2597 orig_pwr_info.pwr_rx == FAST_MODE) {
2598 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2599 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2600 pwr_mode_change = true;
2601 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2602 orig_pwr_info.pwr_rx == SLOW_MODE) {
2603 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2604 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2605 pwr_mode_change = true;
2606 }
2607 if (pwr_mode_change) {
2608 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2609 if (ret)
2610 goto out;
2611 }
2612 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302613
2614 uic_cmd.command = peer ?
2615 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2616 uic_cmd.argument1 = attr_sel;
2617
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002618 do {
2619 /* for peer attributes we retry upon failure */
2620 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2621 if (ret)
2622 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2623 get, UIC_GET_ATTR_ID(attr_sel), ret);
2624 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302625
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002626 if (!retries)
2627 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2628 get, UIC_GET_ATTR_ID(attr_sel), retries);
2629
2630 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302631 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002632
2633 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2634 && pwr_mode_change)
2635 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302636out:
2637 return ret;
2638}
2639EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2640
2641/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002642 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2643 * state) and waits for it to take effect.
2644 *
2645 * @hba: per adapter instance
2646 * @cmd: UIC command to execute
2647 *
2648 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2649 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2650 * and device UniPro link and hence it's final completion would be indicated by
2651 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2652 * addition to normal UIC command completion Status (UCCS). This function only
2653 * returns after the relevant status bits indicate the completion.
2654 *
2655 * Returns 0 on success, non-zero value on failure
2656 */
2657static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2658{
2659 struct completion uic_async_done;
2660 unsigned long flags;
2661 u8 status;
2662 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002663 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002664
2665 mutex_lock(&hba->uic_cmd_mutex);
2666 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002667 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002668
2669 spin_lock_irqsave(hba->host->host_lock, flags);
2670 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002671 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2672 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2673 /*
2674 * Make sure UIC command completion interrupt is disabled before
2675 * issuing UIC command.
2676 */
2677 wmb();
2678 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002679 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002680 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2681 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002682 if (ret) {
2683 dev_err(hba->dev,
2684 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2685 cmd->command, cmd->argument3, ret);
2686 goto out;
2687 }
2688
2689 if (!wait_for_completion_timeout(hba->uic_async_done,
2690 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2691 dev_err(hba->dev,
2692 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2693 cmd->command, cmd->argument3);
2694 ret = -ETIMEDOUT;
2695 goto out;
2696 }
2697
2698 status = ufshcd_get_upmcrs(hba);
2699 if (status != PWR_LOCAL) {
2700 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002701 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002702 cmd->command, status);
2703 ret = (status != PWR_OK) ? status : -1;
2704 }
2705out:
2706 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002707 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002708 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002709 if (reenable_intr)
2710 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002711 spin_unlock_irqrestore(hba->host->host_lock, flags);
2712 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002713
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002714 return ret;
2715}
2716
2717/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302718 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2719 * using DME_SET primitives.
2720 * @hba: per adapter instance
2721 * @mode: powr mode value
2722 *
2723 * Returns 0 on success, non-zero value on failure
2724 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302725static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302726{
2727 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002728 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302729
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002730 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2731 ret = ufshcd_dme_set(hba,
2732 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2733 if (ret) {
2734 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2735 __func__, ret);
2736 goto out;
2737 }
2738 }
2739
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302740 uic_cmd.command = UIC_CMD_DME_SET;
2741 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2742 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002743 ufshcd_hold(hba, false);
2744 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2745 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302746
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002747out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002748 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002749}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302750
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002751static int ufshcd_link_recovery(struct ufs_hba *hba)
2752{
2753 int ret;
2754 unsigned long flags;
2755
2756 spin_lock_irqsave(hba->host->host_lock, flags);
2757 hba->ufshcd_state = UFSHCD_STATE_RESET;
2758 ufshcd_set_eh_in_progress(hba);
2759 spin_unlock_irqrestore(hba->host->host_lock, flags);
2760
2761 ret = ufshcd_host_reset_and_restore(hba);
2762
2763 spin_lock_irqsave(hba->host->host_lock, flags);
2764 if (ret)
2765 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2766 ufshcd_clear_eh_in_progress(hba);
2767 spin_unlock_irqrestore(hba->host->host_lock, flags);
2768
2769 if (ret)
2770 dev_err(hba->dev, "%s: link recovery failed, err %d",
2771 __func__, ret);
2772
2773 return ret;
2774}
2775
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002776static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002777{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002778 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002779 struct uic_command uic_cmd = {0};
2780
2781 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002782 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002783
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002784 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002785 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2786 __func__, ret);
2787
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002788 /*
2789 * If link recovery fails then return error so that caller
2790 * don't retry the hibern8 enter again.
2791 */
2792 if (ufshcd_link_recovery(hba))
2793 ret = -ENOLINK;
2794 }
2795
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002796 return ret;
2797}
2798
2799static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2800{
2801 int ret = 0, retries;
2802
2803 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2804 ret = __ufshcd_uic_hibern8_enter(hba);
2805 if (!ret || ret == -ENOLINK)
2806 goto out;
2807 }
2808out:
2809 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002810}
2811
2812static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2813{
2814 struct uic_command uic_cmd = {0};
2815 int ret;
2816
2817 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2818 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302819 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002820 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2821 __func__, ret);
2822 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302823 }
2824
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302825 return ret;
2826}
2827
Yaniv Gardi50646362014-10-23 13:25:13 +03002828 /**
2829 * ufshcd_init_pwr_info - setting the POR (power on reset)
2830 * values in hba power info
2831 * @hba: per-adapter instance
2832 */
2833static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2834{
2835 hba->pwr_info.gear_rx = UFS_PWM_G1;
2836 hba->pwr_info.gear_tx = UFS_PWM_G1;
2837 hba->pwr_info.lane_rx = 1;
2838 hba->pwr_info.lane_tx = 1;
2839 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2840 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2841 hba->pwr_info.hs_rate = 0;
2842}
2843
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302844/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002845 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2846 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302847 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002848static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302849{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002850 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2851
2852 if (hba->max_pwr_info.is_valid)
2853 return 0;
2854
2855 pwr_info->pwr_tx = FASTAUTO_MODE;
2856 pwr_info->pwr_rx = FASTAUTO_MODE;
2857 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302858
2859 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002860 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2861 &pwr_info->lane_rx);
2862 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2863 &pwr_info->lane_tx);
2864
2865 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2866 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2867 __func__,
2868 pwr_info->lane_rx,
2869 pwr_info->lane_tx);
2870 return -EINVAL;
2871 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302872
2873 /*
2874 * First, get the maximum gears of HS speed.
2875 * If a zero value, it means there is no HSGEAR capability.
2876 * Then, get the maximum gears of PWM speed.
2877 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002878 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2879 if (!pwr_info->gear_rx) {
2880 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2881 &pwr_info->gear_rx);
2882 if (!pwr_info->gear_rx) {
2883 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2884 __func__, pwr_info->gear_rx);
2885 return -EINVAL;
2886 }
2887 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302888 }
2889
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002890 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2891 &pwr_info->gear_tx);
2892 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302893 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002894 &pwr_info->gear_tx);
2895 if (!pwr_info->gear_tx) {
2896 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2897 __func__, pwr_info->gear_tx);
2898 return -EINVAL;
2899 }
2900 pwr_info->pwr_tx = SLOWAUTO_MODE;
2901 }
2902
2903 hba->max_pwr_info.is_valid = true;
2904 return 0;
2905}
2906
2907static int ufshcd_change_power_mode(struct ufs_hba *hba,
2908 struct ufs_pa_layer_attr *pwr_mode)
2909{
2910 int ret;
2911
2912 /* if already configured to the requested pwr_mode */
2913 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2914 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2915 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2916 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2917 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2918 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2919 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2920 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2921 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302922 }
2923
2924 /*
2925 * Configure attributes for power mode change with below.
2926 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2927 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2928 * - PA_HSSERIES
2929 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002930 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2931 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2932 pwr_mode->lane_rx);
2933 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2934 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302935 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002936 else
2937 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302938
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002939 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2940 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2941 pwr_mode->lane_tx);
2942 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2943 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302944 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002945 else
2946 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302947
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002948 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2949 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2950 pwr_mode->pwr_rx == FAST_MODE ||
2951 pwr_mode->pwr_tx == FAST_MODE)
2952 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2953 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302954
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002955 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2956 | pwr_mode->pwr_tx);
2957
2958 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302959 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002960 "%s: power mode change failed %d\n", __func__, ret);
2961 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002962 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2963 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002964
2965 memcpy(&hba->pwr_info, pwr_mode,
2966 sizeof(struct ufs_pa_layer_attr));
2967 }
2968
2969 return ret;
2970}
2971
2972/**
2973 * ufshcd_config_pwr_mode - configure a new power mode
2974 * @hba: per-adapter instance
2975 * @desired_pwr_mode: desired power configuration
2976 */
2977static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2978 struct ufs_pa_layer_attr *desired_pwr_mode)
2979{
2980 struct ufs_pa_layer_attr final_params = { 0 };
2981 int ret;
2982
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002983 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2984 desired_pwr_mode, &final_params);
2985
2986 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002987 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2988
2989 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302990
2991 return ret;
2992}
2993
2994/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302995 * ufshcd_complete_dev_init() - checks device readiness
2996 * hba: per-adapter instance
2997 *
2998 * Set fDeviceInit flag and poll until device toggles it.
2999 */
3000static int ufshcd_complete_dev_init(struct ufs_hba *hba)
3001{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003002 int i;
3003 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05303004 bool flag_res = 1;
3005
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003006 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3007 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05303008 if (err) {
3009 dev_err(hba->dev,
3010 "%s setting fDeviceInit flag failed with error %d\n",
3011 __func__, err);
3012 goto out;
3013 }
3014
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003015 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3016 for (i = 0; i < 1000 && !err && flag_res; i++)
3017 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
3018 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
3019
Dolev Raviv68078d52013-07-30 00:35:58 +05303020 if (err)
3021 dev_err(hba->dev,
3022 "%s reading fDeviceInit flag failed with error %d\n",
3023 __func__, err);
3024 else if (flag_res)
3025 dev_err(hba->dev,
3026 "%s fDeviceInit was not cleared by the device\n",
3027 __func__);
3028
3029out:
3030 return err;
3031}
3032
3033/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303034 * ufshcd_make_hba_operational - Make UFS controller operational
3035 * @hba: per adapter instance
3036 *
3037 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003038 * 1. Enable required interrupts
3039 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02003040 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003041 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303042 *
3043 * Returns 0 on success, non-zero value on failure
3044 */
3045static int ufshcd_make_hba_operational(struct ufs_hba *hba)
3046{
3047 int err = 0;
3048 u32 reg;
3049
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303050 /* Enable required interrupts */
3051 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
3052
3053 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03003054 if (ufshcd_is_intr_aggr_allowed(hba))
3055 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
3056 else
3057 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303058
3059 /* Configure UTRL and UTMRL base address registers */
3060 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
3061 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
3062 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
3063 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
3064 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
3065 REG_UTP_TASK_REQ_LIST_BASE_L);
3066 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
3067 REG_UTP_TASK_REQ_LIST_BASE_H);
3068
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303069 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02003070 * Make sure base address and interrupt setup are updated before
3071 * enabling the run/stop registers below.
3072 */
3073 wmb();
3074
3075 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303076 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303077 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003078 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303079 if (!(ufshcd_get_lists_status(reg))) {
3080 ufshcd_enable_run_stop_reg(hba);
3081 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303082 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303083 "Host controller not ready to process requests");
3084 err = -EIO;
3085 goto out;
3086 }
3087
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303088out:
3089 return err;
3090}
3091
3092/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02003093 * ufshcd_hba_stop - Send controller to reset state
3094 * @hba: per adapter instance
3095 * @can_sleep: perform sleep or just spin
3096 */
3097static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
3098{
3099 int err;
3100
3101 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
3102 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
3103 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
3104 10, 1, can_sleep);
3105 if (err)
3106 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3107}
3108
3109/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303110 * ufshcd_hba_enable - initialize the controller
3111 * @hba: per adapter instance
3112 *
3113 * The controller resets itself and controller firmware initialization
3114 * sequence kicks off. When controller is ready it will set
3115 * the Host Controller Enable bit to 1.
3116 *
3117 * Returns 0 on success, non-zero value on failure
3118 */
3119static int ufshcd_hba_enable(struct ufs_hba *hba)
3120{
3121 int retry;
3122
3123 /*
3124 * msleep of 1 and 5 used in this function might result in msleep(20),
3125 * but it was necessary to send the UFS FPGA to reset mode during
3126 * development and testing of this driver. msleep can be changed to
3127 * mdelay and retry count can be reduced based on the controller.
3128 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003129 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303130 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003131 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303132
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003133 /* UniPro link is disabled at this point */
3134 ufshcd_set_link_off(hba);
3135
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003136 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003137
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303138 /* start controller initialization sequence */
3139 ufshcd_hba_start(hba);
3140
3141 /*
3142 * To initialize a UFS host controller HCE bit must be set to 1.
3143 * During initialization the HCE bit value changes from 1->0->1.
3144 * When the host controller completes initialization sequence
3145 * it sets the value of HCE bit to 1. The same HCE bit is read back
3146 * to check if the controller has completed initialization sequence.
3147 * So without this delay the value HCE = 1, set in the previous
3148 * instruction might be read back.
3149 * This delay can be changed based on the controller.
3150 */
3151 msleep(1);
3152
3153 /* wait for the host controller to complete initialization */
3154 retry = 10;
3155 while (ufshcd_is_hba_active(hba)) {
3156 if (retry) {
3157 retry--;
3158 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303159 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303160 "Controller enable failed\n");
3161 return -EIO;
3162 }
3163 msleep(5);
3164 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003165
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003166 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003167 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003168
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003169 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003170
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303171 return 0;
3172}
3173
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003174static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3175{
3176 int tx_lanes, i, err = 0;
3177
3178 if (!peer)
3179 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3180 &tx_lanes);
3181 else
3182 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3183 &tx_lanes);
3184 for (i = 0; i < tx_lanes; i++) {
3185 if (!peer)
3186 err = ufshcd_dme_set(hba,
3187 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3188 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3189 0);
3190 else
3191 err = ufshcd_dme_peer_set(hba,
3192 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3193 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3194 0);
3195 if (err) {
3196 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3197 __func__, peer, i, err);
3198 break;
3199 }
3200 }
3201
3202 return err;
3203}
3204
3205static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3206{
3207 return ufshcd_disable_tx_lcc(hba, true);
3208}
3209
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303210/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303211 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303212 * @hba: per adapter instance
3213 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303214 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303215 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303216static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303217{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303218 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003219 int retries = DME_LINKSTARTUP_RETRIES;
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003220 bool link_startup_again = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303221
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003222 /*
3223 * If UFS device isn't active then we will have to issue link startup
3224 * 2 times to make sure the device state move to active.
3225 */
3226 if (!ufshcd_is_ufs_dev_active(hba))
3227 link_startup_again = true;
3228
3229link_startup:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003230 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003231 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303232
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003233 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003234
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003235 /* check if device is detected by inter-connect layer */
3236 if (!ret && !ufshcd_is_device_present(hba)) {
3237 dev_err(hba->dev, "%s: Device not present\n", __func__);
3238 ret = -ENXIO;
3239 goto out;
3240 }
3241
3242 /*
3243 * DME link lost indication is only received when link is up,
3244 * but we can't be sure if the link is up until link startup
3245 * succeeds. So reset the local Uni-Pro and try again.
3246 */
3247 if (ret && ufshcd_hba_enable(hba))
3248 goto out;
3249 } while (ret && retries--);
3250
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303251 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003252 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303253 goto out;
3254
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00003255 if (link_startup_again) {
3256 link_startup_again = false;
3257 retries = DME_LINKSTARTUP_RETRIES;
3258 goto link_startup;
3259 }
3260
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003261 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3262 ret = ufshcd_disable_device_tx_lcc(hba);
3263 if (ret)
3264 goto out;
3265 }
3266
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003267 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003268 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3269 if (ret)
3270 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003271
3272 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303273out:
3274 if (ret)
3275 dev_err(hba->dev, "link startup failed %d\n", ret);
3276 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303277}
3278
3279/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303280 * ufshcd_verify_dev_init() - Verify device initialization
3281 * @hba: per-adapter instance
3282 *
3283 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3284 * device Transport Protocol (UTP) layer is ready after a reset.
3285 * If the UTP layer at the device side is not initialized, it may
3286 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3287 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3288 */
3289static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3290{
3291 int err = 0;
3292 int retries;
3293
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003294 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303295 mutex_lock(&hba->dev_cmd.lock);
3296 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3297 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3298 NOP_OUT_TIMEOUT);
3299
3300 if (!err || err == -ETIMEDOUT)
3301 break;
3302
3303 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3304 }
3305 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003306 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303307
3308 if (err)
3309 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3310 return err;
3311}
3312
3313/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003314 * ufshcd_set_queue_depth - set lun queue depth
3315 * @sdev: pointer to SCSI device
3316 *
3317 * Read bLUQueueDepth value and activate scsi tagged command
3318 * queueing. For WLUN, queue depth is set to 1. For best-effort
3319 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3320 * value that host can queue.
3321 */
3322static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3323{
3324 int ret = 0;
3325 u8 lun_qdepth;
3326 struct ufs_hba *hba;
3327
3328 hba = shost_priv(sdev->host);
3329
3330 lun_qdepth = hba->nutrs;
3331 ret = ufshcd_read_unit_desc_param(hba,
3332 ufshcd_scsi_to_upiu_lun(sdev->lun),
3333 UNIT_DESC_PARAM_LU_Q_DEPTH,
3334 &lun_qdepth,
3335 sizeof(lun_qdepth));
3336
3337 /* Some WLUN doesn't support unit descriptor */
3338 if (ret == -EOPNOTSUPP)
3339 lun_qdepth = 1;
3340 else if (!lun_qdepth)
3341 /* eventually, we can figure out the real queue depth */
3342 lun_qdepth = hba->nutrs;
3343 else
3344 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3345
3346 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3347 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003348 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003349}
3350
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003351/*
3352 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3353 * @hba: per-adapter instance
3354 * @lun: UFS device lun id
3355 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3356 *
3357 * Returns 0 in case of success and b_lu_write_protect status would be returned
3358 * @b_lu_write_protect parameter.
3359 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3360 * Returns -EINVAL in case of invalid parameters passed to this function.
3361 */
3362static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3363 u8 lun,
3364 u8 *b_lu_write_protect)
3365{
3366 int ret;
3367
3368 if (!b_lu_write_protect)
3369 ret = -EINVAL;
3370 /*
3371 * According to UFS device spec, RPMB LU can't be write
3372 * protected so skip reading bLUWriteProtect parameter for
3373 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3374 */
3375 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3376 ret = -ENOTSUPP;
3377 else
3378 ret = ufshcd_read_unit_desc_param(hba,
3379 lun,
3380 UNIT_DESC_PARAM_LU_WR_PROTECT,
3381 b_lu_write_protect,
3382 sizeof(*b_lu_write_protect));
3383 return ret;
3384}
3385
3386/**
3387 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3388 * status
3389 * @hba: per-adapter instance
3390 * @sdev: pointer to SCSI device
3391 *
3392 */
3393static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3394 struct scsi_device *sdev)
3395{
3396 if (hba->dev_info.f_power_on_wp_en &&
3397 !hba->dev_info.is_lu_power_on_wp) {
3398 u8 b_lu_write_protect;
3399
3400 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3401 &b_lu_write_protect) &&
3402 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3403 hba->dev_info.is_lu_power_on_wp = true;
3404 }
3405}
3406
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003407/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303408 * ufshcd_slave_alloc - handle initial SCSI device configurations
3409 * @sdev: pointer to SCSI device
3410 *
3411 * Returns success
3412 */
3413static int ufshcd_slave_alloc(struct scsi_device *sdev)
3414{
3415 struct ufs_hba *hba;
3416
3417 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303418
3419 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3420 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303421
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303422 /* allow SCSI layer to restart the device in case of errors */
3423 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003424
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003425 /* REPORT SUPPORTED OPERATION CODES is not supported */
3426 sdev->no_report_opcodes = 1;
3427
Sujit Reddy Thumma9760af42018-01-24 09:52:35 +05303428 /* WRITE_SAME command is not supported */
3429 sdev->no_write_same = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003430
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003431 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003432
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003433 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3434
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003435 return 0;
3436}
3437
3438/**
3439 * ufshcd_change_queue_depth - change queue depth
3440 * @sdev: pointer to SCSI device
3441 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003442 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003443 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003444 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003445static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003446{
3447 struct ufs_hba *hba = shost_priv(sdev->host);
3448
3449 if (depth > hba->nutrs)
3450 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003451 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303452}
3453
3454/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003455 * ufshcd_slave_configure - adjust SCSI device configurations
3456 * @sdev: pointer to SCSI device
3457 */
3458static int ufshcd_slave_configure(struct scsi_device *sdev)
3459{
3460 struct request_queue *q = sdev->request_queue;
3461
3462 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3463 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3464
3465 return 0;
3466}
3467
3468/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303469 * ufshcd_slave_destroy - remove SCSI device configurations
3470 * @sdev: pointer to SCSI device
3471 */
3472static void ufshcd_slave_destroy(struct scsi_device *sdev)
3473{
3474 struct ufs_hba *hba;
3475
3476 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003477 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003478 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3479 unsigned long flags;
3480
3481 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003482 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003483 spin_unlock_irqrestore(hba->host->host_lock, flags);
3484 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303485}
3486
3487/**
3488 * ufshcd_task_req_compl - handle task management request completion
3489 * @hba: per adapter instance
3490 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303491 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303492 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303493 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303494 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303495static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303496{
3497 struct utp_task_req_desc *task_req_descp;
3498 struct utp_upiu_task_rsp *task_rsp_upiup;
3499 unsigned long flags;
3500 int ocs_value;
3501 int task_result;
3502
3503 spin_lock_irqsave(hba->host->host_lock, flags);
3504
3505 /* Clear completed tasks from outstanding_tasks */
3506 __clear_bit(index, &hba->outstanding_tasks);
3507
3508 task_req_descp = hba->utmrdl_base_addr;
3509 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3510
3511 if (ocs_value == OCS_SUCCESS) {
3512 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3513 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003514 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3515 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303516 if (resp)
3517 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303518 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303519 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3520 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303521 }
3522 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303523
3524 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303525}
3526
3527/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303528 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3529 * @lrb: pointer to local reference block of completed command
3530 * @scsi_status: SCSI command status
3531 *
3532 * Returns value base on SCSI command status
3533 */
3534static inline int
3535ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3536{
3537 int result = 0;
3538
3539 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303540 case SAM_STAT_CHECK_CONDITION:
3541 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303542 case SAM_STAT_GOOD:
3543 result |= DID_OK << 16 |
3544 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303545 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303546 break;
3547 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303548 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303549 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303550 ufshcd_copy_sense_data(lrbp);
3551 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303552 break;
3553 default:
3554 result |= DID_ERROR << 16;
3555 break;
3556 } /* end of switch */
3557
3558 return result;
3559}
3560
3561/**
3562 * ufshcd_transfer_rsp_status - Get overall status of the response
3563 * @hba: per adapter instance
3564 * @lrb: pointer to local reference block of completed command
3565 *
3566 * Returns result of the command to notify SCSI midlayer
3567 */
3568static inline int
3569ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3570{
3571 int result = 0;
3572 int scsi_status;
3573 int ocs;
3574
3575 /* overall command status of utrd */
3576 ocs = ufshcd_get_tr_ocs(lrbp);
3577
3578 switch (ocs) {
3579 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303580 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303581
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303582 switch (result) {
3583 case UPIU_TRANSACTION_RESPONSE:
3584 /*
3585 * get the response UPIU result to extract
3586 * the SCSI command status
3587 */
3588 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3589
3590 /*
3591 * get the result based on SCSI status response
3592 * to notify the SCSI midlayer of the command status
3593 */
3594 scsi_status = result & MASK_SCSI_STATUS;
3595 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303596
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003597 /*
3598 * Currently we are only supporting BKOPs exception
3599 * events hence we can ignore BKOPs exception event
3600 * during power management callbacks. BKOPs exception
3601 * event is not expected to be raised in runtime suspend
3602 * callback as it allows the urgent bkops.
3603 * During system suspend, we are anyway forcefully
3604 * disabling the bkops and if urgent bkops is needed
3605 * it will be enabled on system resume. Long term
3606 * solution could be to abort the system suspend if
3607 * UFS device needs urgent BKOPs.
3608 */
3609 if (!hba->pm_op_in_progress &&
3610 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303611 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303612 break;
3613 case UPIU_TRANSACTION_REJECT_UPIU:
3614 /* TODO: handle Reject UPIU Response */
3615 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303616 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303617 "Reject UPIU not fully implemented\n");
3618 break;
3619 default:
3620 result = DID_ERROR << 16;
3621 dev_err(hba->dev,
3622 "Unexpected request response code = %x\n",
3623 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303624 break;
3625 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303626 break;
3627 case OCS_ABORTED:
3628 result |= DID_ABORT << 16;
3629 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303630 case OCS_INVALID_COMMAND_STATUS:
3631 result |= DID_REQUEUE << 16;
3632 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303633 case OCS_INVALID_CMD_TABLE_ATTR:
3634 case OCS_INVALID_PRDT_ATTR:
3635 case OCS_MISMATCH_DATA_BUF_SIZE:
3636 case OCS_MISMATCH_RESP_UPIU_SIZE:
3637 case OCS_PEER_COMM_FAILURE:
3638 case OCS_FATAL_ERROR:
3639 default:
3640 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303641 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303642 "OCS error from controller = %x\n", ocs);
3643 break;
3644 } /* end of switch */
3645
3646 return result;
3647}
3648
3649/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303650 * ufshcd_uic_cmd_compl - handle completion of uic command
3651 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303652 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303653 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303654static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303655{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303656 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303657 hba->active_uic_cmd->argument2 |=
3658 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303659 hba->active_uic_cmd->argument3 =
3660 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303661 complete(&hba->active_uic_cmd->done);
3662 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303663
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003664 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3665 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303666}
3667
3668/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003669 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303670 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003671 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303672 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003673static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3674 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303675{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303676 struct ufshcd_lrb *lrbp;
3677 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303678 int result;
3679 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003680
Dolev Ravive9d501b2014-07-01 12:22:37 +03003681 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3682 lrbp = &hba->lrb[index];
3683 cmd = lrbp->cmd;
3684 if (cmd) {
3685 result = ufshcd_transfer_rsp_status(hba, lrbp);
3686 scsi_dma_unmap(cmd);
3687 cmd->result = result;
3688 /* Mark completed command as NULL in LRB */
3689 lrbp->cmd = NULL;
3690 clear_bit_unlock(index, &hba->lrb_in_use);
3691 /* Do not touch lrbp after scsi done */
3692 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003693 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003694 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3695 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003696 if (hba->dev_cmd.complete)
3697 complete(hba->dev_cmd.complete);
3698 }
3699 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303700
3701 /* clear corresponding bits of completed commands */
3702 hba->outstanding_reqs ^= completed_reqs;
3703
Sahitya Tummala856b3482014-09-25 15:32:34 +03003704 ufshcd_clk_scaling_update_busy(hba);
3705
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303706 /* we might have free'd some tags above */
3707 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303708}
3709
3710/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003711 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3712 * @hba: per adapter instance
3713 */
3714static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3715{
3716 unsigned long completed_reqs;
3717 u32 tr_doorbell;
3718
3719 /* Resetting interrupt aggregation counters first and reading the
3720 * DOOR_BELL afterward allows us to handle all the completed requests.
3721 * In order to prevent other interrupts starvation the DB is read once
3722 * after reset. The down side of this solution is the possibility of
3723 * false interrupt if device completes another request after resetting
3724 * aggregation and before reading the DB.
3725 */
3726 if (ufshcd_is_intr_aggr_allowed(hba))
3727 ufshcd_reset_intr_aggr(hba);
3728
3729 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3730 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3731
3732 __ufshcd_transfer_req_compl(hba, completed_reqs);
3733}
3734
3735/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303736 * ufshcd_disable_ee - disable exception event
3737 * @hba: per-adapter instance
3738 * @mask: exception event to disable
3739 *
3740 * Disables exception event in the device so that the EVENT_ALERT
3741 * bit is not set.
3742 *
3743 * Returns zero on success, non-zero error value on failure.
3744 */
3745static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3746{
3747 int err = 0;
3748 u32 val;
3749
3750 if (!(hba->ee_ctrl_mask & mask))
3751 goto out;
3752
3753 val = hba->ee_ctrl_mask & ~mask;
3754 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003755 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303756 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3757 if (!err)
3758 hba->ee_ctrl_mask &= ~mask;
3759out:
3760 return err;
3761}
3762
3763/**
3764 * ufshcd_enable_ee - enable exception event
3765 * @hba: per-adapter instance
3766 * @mask: exception event to enable
3767 *
3768 * Enable corresponding exception event in the device to allow
3769 * device to alert host in critical scenarios.
3770 *
3771 * Returns zero on success, non-zero error value on failure.
3772 */
3773static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3774{
3775 int err = 0;
3776 u32 val;
3777
3778 if (hba->ee_ctrl_mask & mask)
3779 goto out;
3780
3781 val = hba->ee_ctrl_mask | mask;
3782 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003783 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303784 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3785 if (!err)
3786 hba->ee_ctrl_mask |= mask;
3787out:
3788 return err;
3789}
3790
3791/**
3792 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3793 * @hba: per-adapter instance
3794 *
3795 * Allow device to manage background operations on its own. Enabling
3796 * this might lead to inconsistent latencies during normal data transfers
3797 * as the device is allowed to manage its own way of handling background
3798 * operations.
3799 *
3800 * Returns zero on success, non-zero on failure.
3801 */
3802static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3803{
3804 int err = 0;
3805
3806 if (hba->auto_bkops_enabled)
3807 goto out;
3808
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003809 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303810 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3811 if (err) {
3812 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3813 __func__, err);
3814 goto out;
3815 }
3816
3817 hba->auto_bkops_enabled = true;
3818
3819 /* No need of URGENT_BKOPS exception from the device */
3820 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3821 if (err)
3822 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3823 __func__, err);
3824out:
3825 return err;
3826}
3827
3828/**
3829 * ufshcd_disable_auto_bkops - block device in doing background operations
3830 * @hba: per-adapter instance
3831 *
3832 * Disabling background operations improves command response latency but
3833 * has drawback of device moving into critical state where the device is
3834 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3835 * host is idle so that BKOPS are managed effectively without any negative
3836 * impacts.
3837 *
3838 * Returns zero on success, non-zero on failure.
3839 */
3840static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3841{
3842 int err = 0;
3843
3844 if (!hba->auto_bkops_enabled)
3845 goto out;
3846
3847 /*
3848 * If host assisted BKOPs is to be enabled, make sure
3849 * urgent bkops exception is allowed.
3850 */
3851 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3852 if (err) {
3853 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3854 __func__, err);
3855 goto out;
3856 }
3857
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003858 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303859 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3860 if (err) {
3861 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3862 __func__, err);
3863 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3864 goto out;
3865 }
3866
3867 hba->auto_bkops_enabled = false;
3868out:
3869 return err;
3870}
3871
3872/**
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003873 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303874 * @hba: per adapter instance
3875 *
3876 * After a device reset the device may toggle the BKOPS_EN flag
3877 * to default value. The s/w tracking variables should be updated
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003878 * as well. This function would change the auto-bkops state based on
3879 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303880 */
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003881static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303882{
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08003883 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) {
3884 hba->auto_bkops_enabled = false;
3885 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3886 ufshcd_enable_auto_bkops(hba);
3887 } else {
3888 hba->auto_bkops_enabled = true;
3889 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS;
3890 ufshcd_disable_auto_bkops(hba);
3891 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303892}
3893
3894static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3895{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003896 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303897 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3898}
3899
3900/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003901 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3902 * @hba: per-adapter instance
3903 * @status: bkops_status value
3904 *
3905 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3906 * flag in the device to permit background operations if the device
3907 * bkops_status is greater than or equal to "status" argument passed to
3908 * this function, disable otherwise.
3909 *
3910 * Returns 0 for success, non-zero in case of failure.
3911 *
3912 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3913 * to know whether auto bkops is enabled or disabled after this function
3914 * returns control to it.
3915 */
3916static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3917 enum bkops_status status)
3918{
3919 int err;
3920 u32 curr_status = 0;
3921
3922 err = ufshcd_get_bkops_status(hba, &curr_status);
3923 if (err) {
3924 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3925 __func__, err);
3926 goto out;
3927 } else if (curr_status > BKOPS_STATUS_MAX) {
3928 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3929 __func__, curr_status);
3930 err = -EINVAL;
3931 goto out;
3932 }
3933
3934 if (curr_status >= status)
3935 err = ufshcd_enable_auto_bkops(hba);
3936 else
3937 err = ufshcd_disable_auto_bkops(hba);
3938out:
3939 return err;
3940}
3941
3942/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303943 * ufshcd_urgent_bkops - handle urgent bkops exception event
3944 * @hba: per-adapter instance
3945 *
3946 * Enable fBackgroundOpsEn flag in the device to permit background
3947 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003948 *
3949 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3950 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303951 */
3952static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3953{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003954 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303955}
3956
3957static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3958{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003959 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303960 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3961}
3962
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003963static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3964{
3965 int err;
3966 u32 curr_status = 0;
3967
3968 if (hba->is_urgent_bkops_lvl_checked)
3969 goto enable_auto_bkops;
3970
3971 err = ufshcd_get_bkops_status(hba, &curr_status);
3972 if (err) {
3973 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3974 __func__, err);
3975 goto out;
3976 }
3977
3978 /*
3979 * We are seeing that some devices are raising the urgent bkops
3980 * exception events even when BKOPS status doesn't indicate performace
3981 * impacted or critical. Handle these device by determining their urgent
3982 * bkops status at runtime.
3983 */
3984 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
3985 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
3986 __func__, curr_status);
3987 /* update the current status as the urgent bkops level */
3988 hba->urgent_bkops_lvl = curr_status;
3989 hba->is_urgent_bkops_lvl_checked = true;
3990 }
3991
3992enable_auto_bkops:
3993 err = ufshcd_enable_auto_bkops(hba);
3994out:
3995 if (err < 0)
3996 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3997 __func__, err);
3998}
3999
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304000/**
4001 * ufshcd_exception_event_handler - handle exceptions raised by device
4002 * @work: pointer to work data
4003 *
4004 * Read bExceptionEventStatus attribute from the device and handle the
4005 * exception event accordingly.
4006 */
4007static void ufshcd_exception_event_handler(struct work_struct *work)
4008{
4009 struct ufs_hba *hba;
4010 int err;
4011 u32 status = 0;
4012 hba = container_of(work, struct ufs_hba, eeh_work);
4013
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304014 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304015 err = ufshcd_get_ee_status(hba, &status);
4016 if (err) {
4017 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4018 __func__, err);
4019 goto out;
4020 }
4021
4022 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004023
4024 if (status & MASK_EE_URGENT_BKOPS)
4025 ufshcd_bkops_exception_event_handler(hba);
4026
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304027out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304028 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304029 return;
4030}
4031
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004032/* Complete requests that have door-bell cleared */
4033static void ufshcd_complete_requests(struct ufs_hba *hba)
4034{
4035 ufshcd_transfer_req_compl(hba);
4036 ufshcd_tmc_handler(hba);
4037}
4038
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304039/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004040 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4041 * to recover from the DL NAC errors or not.
4042 * @hba: per-adapter instance
4043 *
4044 * Returns true if error handling is required, false otherwise
4045 */
4046static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4047{
4048 unsigned long flags;
4049 bool err_handling = true;
4050
4051 spin_lock_irqsave(hba->host->host_lock, flags);
4052 /*
4053 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4054 * device fatal error and/or DL NAC & REPLAY timeout errors.
4055 */
4056 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4057 goto out;
4058
4059 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4060 ((hba->saved_err & UIC_ERROR) &&
4061 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4062 goto out;
4063
4064 if ((hba->saved_err & UIC_ERROR) &&
4065 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4066 int err;
4067 /*
4068 * wait for 50ms to see if we can get any other errors or not.
4069 */
4070 spin_unlock_irqrestore(hba->host->host_lock, flags);
4071 msleep(50);
4072 spin_lock_irqsave(hba->host->host_lock, flags);
4073
4074 /*
4075 * now check if we have got any other severe errors other than
4076 * DL NAC error?
4077 */
4078 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4079 ((hba->saved_err & UIC_ERROR) &&
4080 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4081 goto out;
4082
4083 /*
4084 * As DL NAC is the only error received so far, send out NOP
4085 * command to confirm if link is still active or not.
4086 * - If we don't get any response then do error recovery.
4087 * - If we get response then clear the DL NAC error bit.
4088 */
4089
4090 spin_unlock_irqrestore(hba->host->host_lock, flags);
4091 err = ufshcd_verify_dev_init(hba);
4092 spin_lock_irqsave(hba->host->host_lock, flags);
4093
4094 if (err)
4095 goto out;
4096
4097 /* Link seems to be alive hence ignore the DL NAC errors */
4098 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4099 hba->saved_err &= ~UIC_ERROR;
4100 /* clear NAC error */
4101 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4102 if (!hba->saved_uic_err) {
4103 err_handling = false;
4104 goto out;
4105 }
4106 }
4107out:
4108 spin_unlock_irqrestore(hba->host->host_lock, flags);
4109 return err_handling;
4110}
4111
4112/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304113 * ufshcd_err_handler - handle UFS errors that require s/w attention
4114 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304115 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304116static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304117{
4118 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304119 unsigned long flags;
4120 u32 err_xfer = 0;
4121 u32 err_tm = 0;
4122 int err = 0;
4123 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004124 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304125
4126 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304127
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304128 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004129 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304130
4131 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004132 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304133 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304134
4135 hba->ufshcd_state = UFSHCD_STATE_RESET;
4136 ufshcd_set_eh_in_progress(hba);
4137
4138 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004139 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004140
4141 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4142 bool ret;
4143
4144 spin_unlock_irqrestore(hba->host->host_lock, flags);
4145 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4146 ret = ufshcd_quirk_dl_nac_errors(hba);
4147 spin_lock_irqsave(hba->host->host_lock, flags);
4148 if (!ret)
4149 goto skip_err_handling;
4150 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004151 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4152 ((hba->saved_err & UIC_ERROR) &&
4153 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4154 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4155 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4156 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304157
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004158 /*
4159 * if host reset is required then skip clearing the pending
4160 * transfers forcefully because they will automatically get
4161 * cleared after link startup.
4162 */
4163 if (needs_reset)
4164 goto skip_pending_xfer_clear;
4165
4166 /* release lock as clear command might sleep */
4167 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304168 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004169 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4170 if (ufshcd_clear_cmd(hba, tag)) {
4171 err_xfer = true;
4172 goto lock_skip_pending_xfer_clear;
4173 }
4174 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304175
4176 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004177 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4178 if (ufshcd_clear_tm_cmd(hba, tag)) {
4179 err_tm = true;
4180 goto lock_skip_pending_xfer_clear;
4181 }
4182 }
4183
4184lock_skip_pending_xfer_clear:
4185 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304186
4187 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004188 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304189
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004190 if (err_xfer || err_tm)
4191 needs_reset = true;
4192
4193skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304194 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004195 if (needs_reset) {
4196 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4197
4198 /*
4199 * ufshcd_reset_and_restore() does the link reinitialization
4200 * which will need atleast one empty doorbell slot to send the
4201 * device management commands (NOP and query commands).
4202 * If there is no slot empty at this moment then free up last
4203 * slot forcefully.
4204 */
4205 if (hba->outstanding_reqs == max_doorbells)
4206 __ufshcd_transfer_req_compl(hba,
4207 (1UL << (hba->nutrs - 1)));
4208
4209 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304210 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004211 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304212 if (err) {
4213 dev_err(hba->dev, "%s: reset and restore failed\n",
4214 __func__);
4215 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4216 }
4217 /*
4218 * Inform scsi mid-layer that we did reset and allow to handle
4219 * Unit Attention properly.
4220 */
4221 scsi_report_bus_reset(hba->host, 0);
4222 hba->saved_err = 0;
4223 hba->saved_uic_err = 0;
4224 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004225
Yaniv Gardi583fa622016-03-10 17:37:13 +02004226skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004227 if (!needs_reset) {
4228 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4229 if (hba->saved_err || hba->saved_uic_err)
4230 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4231 __func__, hba->saved_err, hba->saved_uic_err);
4232 }
4233
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304234 ufshcd_clear_eh_in_progress(hba);
4235
4236out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004237 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304238 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004239 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304240 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304241}
4242
4243/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304244 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4245 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304246 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304247static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304248{
4249 u32 reg;
4250
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304251 /* PA_INIT_ERROR is fatal and needs UIC reset */
4252 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4253 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4254 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004255 else if (hba->dev_quirks &
4256 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4257 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4258 hba->uic_error |=
4259 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4260 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4261 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4262 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304263
4264 /* UIC NL/TL/DME errors needs software retry */
4265 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4266 if (reg)
4267 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4268
4269 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4270 if (reg)
4271 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4272
4273 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4274 if (reg)
4275 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4276
4277 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4278 __func__, hba->uic_error);
4279}
4280
4281/**
4282 * ufshcd_check_errors - Check for errors that need s/w attention
4283 * @hba: per-adapter instance
4284 */
4285static void ufshcd_check_errors(struct ufs_hba *hba)
4286{
4287 bool queue_eh_work = false;
4288
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304289 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304290 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304291
4292 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304293 hba->uic_error = 0;
4294 ufshcd_update_uic_error(hba);
4295 if (hba->uic_error)
4296 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304297 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304298
4299 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004300 /*
4301 * update the transfer error masks to sticky bits, let's do this
4302 * irrespective of current ufshcd_state.
4303 */
4304 hba->saved_err |= hba->errors;
4305 hba->saved_uic_err |= hba->uic_error;
4306
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304307 /* handle fatal errors only when link is functional */
4308 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4309 /* block commands from scsi mid-layer */
4310 scsi_block_requests(hba->host);
4311
Zang Leiganga17bddc2017-04-04 19:32:20 +00004312 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304313 schedule_work(&hba->eh_work);
4314 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304315 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304316 /*
4317 * if (!queue_eh_work) -
4318 * Other errors are either non-fatal where host recovers
4319 * itself without s/w intervention or errors that will be
4320 * handled by the SCSI core layer.
4321 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304322}
4323
4324/**
4325 * ufshcd_tmc_handler - handle task management function completion
4326 * @hba: per adapter instance
4327 */
4328static void ufshcd_tmc_handler(struct ufs_hba *hba)
4329{
4330 u32 tm_doorbell;
4331
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304332 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304333 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304334 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304335}
4336
4337/**
4338 * ufshcd_sl_intr - Interrupt service routine
4339 * @hba: per adapter instance
4340 * @intr_status: contains interrupts generated by the controller
4341 */
4342static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4343{
4344 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4345 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304346 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304347
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304348 if (intr_status & UFSHCD_UIC_MASK)
4349 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304350
4351 if (intr_status & UTP_TASK_REQ_COMPL)
4352 ufshcd_tmc_handler(hba);
4353
4354 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4355 ufshcd_transfer_req_compl(hba);
4356}
4357
4358/**
4359 * ufshcd_intr - Main interrupt service routine
4360 * @irq: irq number
4361 * @__hba: pointer to adapter instance
4362 *
4363 * Returns IRQ_HANDLED - If interrupt is valid
4364 * IRQ_NONE - If invalid interrupt
4365 */
4366static irqreturn_t ufshcd_intr(int irq, void *__hba)
4367{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004368 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304369 irqreturn_t retval = IRQ_NONE;
4370 struct ufs_hba *hba = __hba;
4371
4372 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304373 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004374 enabled_intr_status =
4375 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304376
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004377 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304378 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004379
4380 if (enabled_intr_status) {
4381 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304382 retval = IRQ_HANDLED;
4383 }
4384 spin_unlock(hba->host->host_lock);
4385 return retval;
4386}
4387
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304388static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4389{
4390 int err = 0;
4391 u32 mask = 1 << tag;
4392 unsigned long flags;
4393
4394 if (!test_bit(tag, &hba->outstanding_tasks))
4395 goto out;
4396
4397 spin_lock_irqsave(hba->host->host_lock, flags);
4398 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4399 spin_unlock_irqrestore(hba->host->host_lock, flags);
4400
4401 /* poll for max. 1 sec to clear door bell register by h/w */
4402 err = ufshcd_wait_for_register(hba,
4403 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004404 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304405out:
4406 return err;
4407}
4408
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304409/**
4410 * ufshcd_issue_tm_cmd - issues task management commands to controller
4411 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304412 * @lun_id: LUN ID to which TM command is sent
4413 * @task_id: task ID to which the TM command is applicable
4414 * @tm_function: task management function opcode
4415 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304416 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304417 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304418 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304419static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4420 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304421{
4422 struct utp_task_req_desc *task_req_descp;
4423 struct utp_upiu_task_req *task_req_upiup;
4424 struct Scsi_Host *host;
4425 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304426 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304427 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304428 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304429
4430 host = hba->host;
4431
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304432 /*
4433 * Get free slot, sleep if slots are unavailable.
4434 * Even though we use wait_event() which sleeps indefinitely,
4435 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4436 */
4437 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004438 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304439
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304440 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304441 task_req_descp = hba->utmrdl_base_addr;
4442 task_req_descp += free_slot;
4443
4444 /* Configure task request descriptor */
4445 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4446 task_req_descp->header.dword_2 =
4447 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4448
4449 /* Configure task request UPIU */
4450 task_req_upiup =
4451 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304452 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304453 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304454 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304455 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304456 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304457 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004458 /*
4459 * The host shall provide the same value for LUN field in the basic
4460 * header and for Input Parameter.
4461 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304462 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4463 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304464
4465 /* send command to the controller */
4466 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004467
4468 /* Make sure descriptors are ready before ringing the task doorbell */
4469 wmb();
4470
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304471 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304472
4473 spin_unlock_irqrestore(host->host_lock, flags);
4474
4475 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304476 err = wait_event_timeout(hba->tm_wq,
4477 test_bit(free_slot, &hba->tm_condition),
4478 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304479 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304480 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4481 __func__, tm_function);
4482 if (ufshcd_clear_tm_cmd(hba, free_slot))
4483 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4484 __func__, free_slot);
4485 err = -ETIMEDOUT;
4486 } else {
4487 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304488 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304489
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304490 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304491 ufshcd_put_tm_slot(hba, free_slot);
4492 wake_up(&hba->tm_tag_wq);
4493
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004494 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304495 return err;
4496}
4497
4498/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304499 * ufshcd_eh_device_reset_handler - device reset handler registered to
4500 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304501 * @cmd: SCSI command pointer
4502 *
4503 * Returns SUCCESS/FAILED
4504 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304505static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304506{
4507 struct Scsi_Host *host;
4508 struct ufs_hba *hba;
4509 unsigned int tag;
4510 u32 pos;
4511 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304512 u8 resp = 0xF;
4513 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304514 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304515
4516 host = cmd->device->host;
4517 hba = shost_priv(host);
4518 tag = cmd->request->tag;
4519
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304520 lrbp = &hba->lrb[tag];
4521 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4522 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304523 if (!err)
4524 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304525 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304526 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304527
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304528 /* clear the commands that were pending for corresponding LUN */
4529 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4530 if (hba->lrb[pos].lun == lrbp->lun) {
4531 err = ufshcd_clear_cmd(hba, pos);
4532 if (err)
4533 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304534 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304535 }
4536 spin_lock_irqsave(host->host_lock, flags);
4537 ufshcd_transfer_req_compl(hba);
4538 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304539out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304540 if (!err) {
4541 err = SUCCESS;
4542 } else {
4543 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4544 err = FAILED;
4545 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304546 return err;
4547}
4548
4549/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304550 * ufshcd_abort - abort a specific command
4551 * @cmd: SCSI command pointer
4552 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304553 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4554 * command, and in host controller by clearing the door-bell register. There can
4555 * be race between controller sending the command to the device while abort is
4556 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4557 * really issued and then try to abort it.
4558 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304559 * Returns SUCCESS/FAILED
4560 */
4561static int ufshcd_abort(struct scsi_cmnd *cmd)
4562{
4563 struct Scsi_Host *host;
4564 struct ufs_hba *hba;
4565 unsigned long flags;
4566 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304567 int err = 0;
4568 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304569 u8 resp = 0xF;
4570 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004571 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304572
4573 host = cmd->device->host;
4574 hba = shost_priv(host);
4575 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004576 if (!ufshcd_valid_tag(hba, tag)) {
4577 dev_err(hba->dev,
4578 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4579 __func__, tag, cmd, cmd->request);
4580 BUG();
4581 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304582
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004583 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004584 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004585 /* If command is already aborted/completed, return SUCCESS */
4586 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4587 dev_err(hba->dev,
4588 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4589 __func__, tag, hba->outstanding_reqs, reg);
4590 goto out;
4591 }
4592
Dolev Ravive9d501b2014-07-01 12:22:37 +03004593 if (!(reg & (1 << tag))) {
4594 dev_err(hba->dev,
4595 "%s: cmd was completed, but without a notifying intr, tag = %d",
4596 __func__, tag);
4597 }
4598
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304599 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304600 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4601 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4602 UFS_QUERY_TASK, &resp);
4603 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4604 /* cmd pending in the device */
4605 break;
4606 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304607 /*
4608 * cmd not pending in the device, check if it is
4609 * in transition.
4610 */
4611 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4612 if (reg & (1 << tag)) {
4613 /* sleep for max. 200us to stabilize */
4614 usleep_range(100, 200);
4615 continue;
4616 }
4617 /* command completed already */
4618 goto out;
4619 } else {
4620 if (!err)
4621 err = resp; /* service response error */
4622 goto out;
4623 }
4624 }
4625
4626 if (!poll_cnt) {
4627 err = -EBUSY;
4628 goto out;
4629 }
4630
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304631 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4632 UFS_ABORT_TASK, &resp);
4633 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304634 if (!err)
4635 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304636 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304637 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304638
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304639 err = ufshcd_clear_cmd(hba, tag);
4640 if (err)
4641 goto out;
4642
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304643 scsi_dma_unmap(cmd);
4644
4645 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004646 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304647 hba->lrb[tag].cmd = NULL;
4648 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304649
4650 clear_bit_unlock(tag, &hba->lrb_in_use);
4651 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004652
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304653out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304654 if (!err) {
4655 err = SUCCESS;
4656 } else {
4657 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4658 err = FAILED;
4659 }
4660
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004661 /*
4662 * This ufshcd_release() corresponds to the original scsi cmd that got
4663 * aborted here (as we won't get any IRQ for it).
4664 */
4665 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304666 return err;
4667}
4668
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304669/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304670 * ufshcd_host_reset_and_restore - reset and restore host controller
4671 * @hba: per-adapter instance
4672 *
4673 * Note that host controller reset may issue DME_RESET to
4674 * local and remote (device) Uni-Pro stack and the attributes
4675 * are reset to default state.
4676 *
4677 * Returns zero on success, non-zero on failure
4678 */
4679static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4680{
4681 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304682 unsigned long flags;
4683
4684 /* Reset the host controller */
4685 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004686 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304687 spin_unlock_irqrestore(hba->host->host_lock, flags);
4688
4689 err = ufshcd_hba_enable(hba);
4690 if (err)
4691 goto out;
4692
4693 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004694 err = ufshcd_probe_hba(hba);
4695
4696 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304697 err = -EIO;
4698out:
4699 if (err)
4700 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4701
4702 return err;
4703}
4704
4705/**
4706 * ufshcd_reset_and_restore - reset and re-initialize host/device
4707 * @hba: per-adapter instance
4708 *
4709 * Reset and recover device, host and re-establish link. This
4710 * is helpful to recover the communication in fatal error conditions.
4711 *
4712 * Returns zero on success, non-zero on failure
4713 */
4714static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4715{
4716 int err = 0;
4717 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004718 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304719
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004720 do {
4721 err = ufshcd_host_reset_and_restore(hba);
4722 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304723
4724 /*
4725 * After reset the door-bell might be cleared, complete
4726 * outstanding requests in s/w here.
4727 */
4728 spin_lock_irqsave(hba->host->host_lock, flags);
4729 ufshcd_transfer_req_compl(hba);
4730 ufshcd_tmc_handler(hba);
4731 spin_unlock_irqrestore(hba->host->host_lock, flags);
4732
4733 return err;
4734}
4735
4736/**
4737 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4738 * @cmd - SCSI command pointer
4739 *
4740 * Returns SUCCESS/FAILED
4741 */
4742static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4743{
4744 int err;
4745 unsigned long flags;
4746 struct ufs_hba *hba;
4747
4748 hba = shost_priv(cmd->device->host);
4749
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004750 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304751 /*
4752 * Check if there is any race with fatal error handling.
4753 * If so, wait for it to complete. Even though fatal error
4754 * handling does reset and restore in some cases, don't assume
4755 * anything out of it. We are just avoiding race here.
4756 */
4757 do {
4758 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304759 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304760 hba->ufshcd_state == UFSHCD_STATE_RESET))
4761 break;
4762 spin_unlock_irqrestore(hba->host->host_lock, flags);
4763 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304764 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304765 } while (1);
4766
4767 hba->ufshcd_state = UFSHCD_STATE_RESET;
4768 ufshcd_set_eh_in_progress(hba);
4769 spin_unlock_irqrestore(hba->host->host_lock, flags);
4770
4771 err = ufshcd_reset_and_restore(hba);
4772
4773 spin_lock_irqsave(hba->host->host_lock, flags);
4774 if (!err) {
4775 err = SUCCESS;
4776 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4777 } else {
4778 err = FAILED;
4779 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4780 }
4781 ufshcd_clear_eh_in_progress(hba);
4782 spin_unlock_irqrestore(hba->host->host_lock, flags);
4783
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004784 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304785 return err;
4786}
4787
4788/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004789 * ufshcd_get_max_icc_level - calculate the ICC level
4790 * @sup_curr_uA: max. current supported by the regulator
4791 * @start_scan: row at the desc table to start scan from
4792 * @buff: power descriptor buffer
4793 *
4794 * Returns calculated max ICC level for specific regulator
4795 */
4796static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4797{
4798 int i;
4799 int curr_uA;
4800 u16 data;
4801 u16 unit;
4802
4803 for (i = start_scan; i >= 0; i--) {
4804 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4805 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4806 ATTR_ICC_LVL_UNIT_OFFSET;
4807 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4808 switch (unit) {
4809 case UFSHCD_NANO_AMP:
4810 curr_uA = curr_uA / 1000;
4811 break;
4812 case UFSHCD_MILI_AMP:
4813 curr_uA = curr_uA * 1000;
4814 break;
4815 case UFSHCD_AMP:
4816 curr_uA = curr_uA * 1000 * 1000;
4817 break;
4818 case UFSHCD_MICRO_AMP:
4819 default:
4820 break;
4821 }
4822 if (sup_curr_uA >= curr_uA)
4823 break;
4824 }
4825 if (i < 0) {
4826 i = 0;
4827 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4828 }
4829
4830 return (u32)i;
4831}
4832
4833/**
4834 * ufshcd_calc_icc_level - calculate the max ICC level
4835 * In case regulators are not initialized we'll return 0
4836 * @hba: per-adapter instance
4837 * @desc_buf: power descriptor buffer to extract ICC levels from.
4838 * @len: length of desc_buff
4839 *
4840 * Returns calculated ICC level
4841 */
4842static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4843 u8 *desc_buf, int len)
4844{
4845 u32 icc_level = 0;
4846
4847 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4848 !hba->vreg_info.vccq2) {
4849 dev_err(hba->dev,
4850 "%s: Regulator capability was not set, actvIccLevel=%d",
4851 __func__, icc_level);
4852 goto out;
4853 }
4854
4855 if (hba->vreg_info.vcc)
4856 icc_level = ufshcd_get_max_icc_level(
4857 hba->vreg_info.vcc->max_uA,
4858 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4859 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4860
4861 if (hba->vreg_info.vccq)
4862 icc_level = ufshcd_get_max_icc_level(
4863 hba->vreg_info.vccq->max_uA,
4864 icc_level,
4865 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4866
4867 if (hba->vreg_info.vccq2)
4868 icc_level = ufshcd_get_max_icc_level(
4869 hba->vreg_info.vccq2->max_uA,
4870 icc_level,
4871 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4872out:
4873 return icc_level;
4874}
4875
4876static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4877{
4878 int ret;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004879 int buff_len = hba->desc_size.pwr_desc;
4880 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004881
4882 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4883 if (ret) {
4884 dev_err(hba->dev,
4885 "%s: Failed reading power descriptor.len = %d ret = %d",
4886 __func__, buff_len, ret);
4887 return;
4888 }
4889
4890 hba->init_prefetch_data.icc_level =
4891 ufshcd_find_max_sup_active_icc_level(hba,
4892 desc_buf, buff_len);
4893 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4894 __func__, hba->init_prefetch_data.icc_level);
4895
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004896 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4897 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4898 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004899
4900 if (ret)
4901 dev_err(hba->dev,
4902 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4903 __func__, hba->init_prefetch_data.icc_level , ret);
4904
4905}
4906
4907/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004908 * ufshcd_scsi_add_wlus - Adds required W-LUs
4909 * @hba: per-adapter instance
4910 *
4911 * UFS device specification requires the UFS devices to support 4 well known
4912 * logical units:
4913 * "REPORT_LUNS" (address: 01h)
4914 * "UFS Device" (address: 50h)
4915 * "RPMB" (address: 44h)
4916 * "BOOT" (address: 30h)
4917 * UFS device's power management needs to be controlled by "POWER CONDITION"
4918 * field of SSU (START STOP UNIT) command. But this "power condition" field
4919 * will take effect only when its sent to "UFS device" well known logical unit
4920 * hence we require the scsi_device instance to represent this logical unit in
4921 * order for the UFS host driver to send the SSU command for power management.
4922
4923 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4924 * Block) LU so user space process can control this LU. User space may also
4925 * want to have access to BOOT LU.
4926
4927 * This function adds scsi device instances for each of all well known LUs
4928 * (except "REPORT LUNS" LU).
4929 *
4930 * Returns zero on success (all required W-LUs are added successfully),
4931 * non-zero error value on failure (if failed to add any of the required W-LU).
4932 */
4933static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4934{
4935 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004936 struct scsi_device *sdev_rpmb;
4937 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004938
4939 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4940 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4941 if (IS_ERR(hba->sdev_ufs_device)) {
4942 ret = PTR_ERR(hba->sdev_ufs_device);
4943 hba->sdev_ufs_device = NULL;
4944 goto out;
4945 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004946 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004947
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004948 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004949 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004950 if (IS_ERR(sdev_boot)) {
4951 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004952 goto remove_sdev_ufs_device;
4953 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004954 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004955
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004956 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004957 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004958 if (IS_ERR(sdev_rpmb)) {
4959 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004960 goto remove_sdev_boot;
4961 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004962 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004963 goto out;
4964
4965remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004966 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004967remove_sdev_ufs_device:
4968 scsi_remove_device(hba->sdev_ufs_device);
4969out:
4970 return ret;
4971}
4972
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02004973static int ufs_get_device_desc(struct ufs_hba *hba,
4974 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004975{
4976 int err;
4977 u8 model_index;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004978 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
4979 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004980
Potomski, MichalXe1928452017-02-23 09:05:30 +00004981 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004982 if (err) {
4983 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4984 __func__, err);
4985 goto out;
4986 }
4987
4988 /*
4989 * getting vendor (manufacturerID) and Bank Index in big endian
4990 * format
4991 */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02004992 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004993 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4994
4995 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4996
4997 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00004998 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004999 if (err) {
5000 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5001 __func__, err);
5002 goto out;
5003 }
5004
Potomski, MichalXe1928452017-02-23 09:05:30 +00005005 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005006 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005007 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5008 MAX_MODEL_LEN));
5009
5010 /* Null terminate the model string */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005011 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005012
5013out:
5014 return err;
5015}
5016
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005017static void ufs_fixup_device_setup(struct ufs_hba *hba,
5018 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005019{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005020 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005021
5022 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005023 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
5024 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
5025 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005026 !strcmp(f->card.model, UFS_ANY_MODEL)))
5027 hba->dev_quirks |= f->quirk;
5028 }
5029}
5030
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005031/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005032 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5033 * @hba: per-adapter instance
5034 *
5035 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5036 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5037 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5038 * the hibern8 exit latency.
5039 *
5040 * Returns zero on success, non-zero error value on failure.
5041 */
5042static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5043{
5044 int ret = 0;
5045 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5046
5047 ret = ufshcd_dme_peer_get(hba,
5048 UIC_ARG_MIB_SEL(
5049 RX_MIN_ACTIVATETIME_CAPABILITY,
5050 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5051 &peer_rx_min_activatetime);
5052 if (ret)
5053 goto out;
5054
5055 /* make sure proper unit conversion is applied */
5056 tuned_pa_tactivate =
5057 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5058 / PA_TACTIVATE_TIME_UNIT_US);
5059 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5060 tuned_pa_tactivate);
5061
5062out:
5063 return ret;
5064}
5065
5066/**
5067 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5068 * @hba: per-adapter instance
5069 *
5070 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5071 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5072 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5073 * This optimal value can help reduce the hibern8 exit latency.
5074 *
5075 * Returns zero on success, non-zero error value on failure.
5076 */
5077static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5078{
5079 int ret = 0;
5080 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5081 u32 max_hibern8_time, tuned_pa_hibern8time;
5082
5083 ret = ufshcd_dme_get(hba,
5084 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5085 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5086 &local_tx_hibern8_time_cap);
5087 if (ret)
5088 goto out;
5089
5090 ret = ufshcd_dme_peer_get(hba,
5091 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5092 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5093 &peer_rx_hibern8_time_cap);
5094 if (ret)
5095 goto out;
5096
5097 max_hibern8_time = max(local_tx_hibern8_time_cap,
5098 peer_rx_hibern8_time_cap);
5099 /* make sure proper unit conversion is applied */
5100 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5101 / PA_HIBERN8_TIME_UNIT_US);
5102 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5103 tuned_pa_hibern8time);
5104out:
5105 return ret;
5106}
5107
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005108/**
5109 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5110 * less than device PA_TACTIVATE time.
5111 * @hba: per-adapter instance
5112 *
5113 * Some UFS devices require host PA_TACTIVATE to be lower than device
5114 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5115 * for such devices.
5116 *
5117 * Returns zero on success, non-zero error value on failure.
5118 */
5119static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5120{
5121 int ret = 0;
5122 u32 granularity, peer_granularity;
5123 u32 pa_tactivate, peer_pa_tactivate;
5124 u32 pa_tactivate_us, peer_pa_tactivate_us;
5125 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5126
5127 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5128 &granularity);
5129 if (ret)
5130 goto out;
5131
5132 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5133 &peer_granularity);
5134 if (ret)
5135 goto out;
5136
5137 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5138 (granularity > PA_GRANULARITY_MAX_VAL)) {
5139 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5140 __func__, granularity);
5141 return -EINVAL;
5142 }
5143
5144 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5145 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5146 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5147 __func__, peer_granularity);
5148 return -EINVAL;
5149 }
5150
5151 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5152 if (ret)
5153 goto out;
5154
5155 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5156 &peer_pa_tactivate);
5157 if (ret)
5158 goto out;
5159
5160 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5161 peer_pa_tactivate_us = peer_pa_tactivate *
5162 gran_to_us_table[peer_granularity - 1];
5163
5164 if (pa_tactivate_us > peer_pa_tactivate_us) {
5165 u32 new_peer_pa_tactivate;
5166
5167 new_peer_pa_tactivate = pa_tactivate_us /
5168 gran_to_us_table[peer_granularity - 1];
5169 new_peer_pa_tactivate++;
5170 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5171 new_peer_pa_tactivate);
5172 }
5173
5174out:
5175 return ret;
5176}
5177
Yaniv Gardi37113102016-03-10 17:37:16 +02005178static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5179{
5180 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5181 ufshcd_tune_pa_tactivate(hba);
5182 ufshcd_tune_pa_hibern8time(hba);
5183 }
5184
5185 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5186 /* set 1ms timeout for PA_TACTIVATE */
5187 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005188
5189 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5190 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavanibedc6292017-04-04 19:32:13 +00005191
5192 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005193}
5194
Potomski, MichalXe1928452017-02-23 09:05:30 +00005195static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
5196{
5197 int err;
5198
5199 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
5200 &hba->desc_size.dev_desc);
5201 if (err)
5202 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5203
5204 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
5205 &hba->desc_size.pwr_desc);
5206 if (err)
5207 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5208
5209 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
5210 &hba->desc_size.interc_desc);
5211 if (err)
5212 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5213
5214 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
5215 &hba->desc_size.conf_desc);
5216 if (err)
5217 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5218
5219 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
5220 &hba->desc_size.unit_desc);
5221 if (err)
5222 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5223
5224 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
5225 &hba->desc_size.geom_desc);
5226 if (err)
5227 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5228}
5229
5230static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
5231{
5232 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5233 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5234 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5235 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5236 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5237 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5238}
5239
Yaniv Gardi37113102016-03-10 17:37:16 +02005240/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005241 * ufshcd_probe_hba - probe hba to detect device and initialize
5242 * @hba: per-adapter instance
5243 *
5244 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305245 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005246static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305247{
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005248 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305249 int ret;
5250
5251 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305252 if (ret)
5253 goto out;
5254
Yaniv Gardi50646362014-10-23 13:25:13 +03005255 ufshcd_init_pwr_info(hba);
5256
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005257 /* set the default level for urgent bkops */
5258 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5259 hba->is_urgent_bkops_lvl_checked = false;
5260
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005261 /* UniPro link is active now */
5262 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305263
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305264 ret = ufshcd_verify_dev_init(hba);
5265 if (ret)
5266 goto out;
5267
Dolev Raviv68078d52013-07-30 00:35:58 +05305268 ret = ufshcd_complete_dev_init(hba);
5269 if (ret)
5270 goto out;
5271
Potomski, MichalXe1928452017-02-23 09:05:30 +00005272 /* Init check for device descriptor sizes */
5273 ufshcd_init_desc_sizes(hba);
5274
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005275 ret = ufs_get_device_desc(hba, &card);
5276 if (ret) {
5277 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5278 __func__, ret);
5279 goto out;
5280 }
5281
5282 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02005283 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005284
5285 ret = ufshcd_set_vccq_rail_unused(hba,
5286 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5287 if (ret)
5288 goto out;
5289
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005290 /* UFS device is also active now */
5291 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305292 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005293 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305294
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005295 if (ufshcd_get_max_pwr_mode(hba)) {
5296 dev_err(hba->dev,
5297 "%s: Failed getting max supported power mode\n",
5298 __func__);
5299 } else {
5300 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
5301 if (ret)
5302 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5303 __func__, ret);
5304 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005305
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005306 /* set the state as operational after switching to desired gear */
5307 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005308
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005309 /*
5310 * If we are in error handling context or in power management callbacks
5311 * context, no need to scan the host
5312 */
5313 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5314 bool flag;
5315
5316 /* clear any previous UFS device information */
5317 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005318 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5319 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005320 hba->dev_info.f_power_on_wp_en = flag;
5321
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005322 if (!hba->is_init_prefetch)
5323 ufshcd_init_icc_levels(hba);
5324
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005325 /* Add required well known logical units to scsi mid layer */
5326 if (ufshcd_scsi_add_wlus(hba))
5327 goto out;
5328
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305329 scsi_scan_host(hba->host);
5330 pm_runtime_put_sync(hba->dev);
5331 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005332
5333 if (!hba->is_init_prefetch)
5334 hba->is_init_prefetch = true;
5335
Sahitya Tummala856b3482014-09-25 15:32:34 +03005336 /* Resume devfreq after UFS device is detected */
5337 if (ufshcd_is_clkscaling_enabled(hba))
5338 devfreq_resume_device(hba->devfreq);
5339
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305340out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005341 /*
5342 * If we failed to initialize the device or the device is not
5343 * present, turn off the power/clocks etc.
5344 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005345 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5346 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005347 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005348 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005349
5350 return ret;
5351}
5352
5353/**
5354 * ufshcd_async_scan - asynchronous execution for probing hba
5355 * @data: data pointer to pass to this function
5356 * @cookie: cookie data
5357 */
5358static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5359{
5360 struct ufs_hba *hba = (struct ufs_hba *)data;
5361
5362 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305363}
5364
Yaniv Gardif550c652016-03-10 17:37:07 +02005365static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5366{
5367 unsigned long flags;
5368 struct Scsi_Host *host;
5369 struct ufs_hba *hba;
5370 int index;
5371 bool found = false;
5372
5373 if (!scmd || !scmd->device || !scmd->device->host)
5374 return BLK_EH_NOT_HANDLED;
5375
5376 host = scmd->device->host;
5377 hba = shost_priv(host);
5378 if (!hba)
5379 return BLK_EH_NOT_HANDLED;
5380
5381 spin_lock_irqsave(host->host_lock, flags);
5382
5383 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5384 if (hba->lrb[index].cmd == scmd) {
5385 found = true;
5386 break;
5387 }
5388 }
5389
5390 spin_unlock_irqrestore(host->host_lock, flags);
5391
5392 /*
5393 * Bypass SCSI error handling and reset the block layer timer if this
5394 * SCSI command was not actually dispatched to UFS driver, otherwise
5395 * let SCSI layer handle the error as usual.
5396 */
5397 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5398}
5399
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305400static struct scsi_host_template ufshcd_driver_template = {
5401 .module = THIS_MODULE,
5402 .name = UFSHCD,
5403 .proc_name = UFSHCD,
5404 .queuecommand = ufshcd_queuecommand,
5405 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005406 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305407 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005408 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305409 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305410 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5411 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005412 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305413 .this_id = -1,
5414 .sg_tablesize = SG_ALL,
5415 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5416 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005417 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005418 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305419};
5420
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005421static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5422 int ua)
5423{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005424 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005425
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005426 if (!vreg)
5427 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005428
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005429 ret = regulator_set_load(vreg->reg, ua);
5430 if (ret < 0) {
5431 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5432 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005433 }
5434
5435 return ret;
5436}
5437
5438static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5439 struct ufs_vreg *vreg)
5440{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005441 if (!vreg)
5442 return 0;
5443 else if (vreg->unused)
5444 return 0;
5445 else
5446 return ufshcd_config_vreg_load(hba->dev, vreg,
5447 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005448}
5449
5450static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5451 struct ufs_vreg *vreg)
5452{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005453 if (!vreg)
5454 return 0;
5455 else if (vreg->unused)
5456 return 0;
5457 else
5458 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005459}
5460
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005461static int ufshcd_config_vreg(struct device *dev,
5462 struct ufs_vreg *vreg, bool on)
5463{
5464 int ret = 0;
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005465 struct regulator *reg;
5466 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005467 int min_uV, uA_load;
5468
5469 BUG_ON(!vreg);
5470
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005471 reg = vreg->reg;
5472 name = vreg->name;
5473
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005474 if (regulator_count_voltages(reg) > 0) {
5475 min_uV = on ? vreg->min_uV : 0;
5476 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5477 if (ret) {
5478 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5479 __func__, name, ret);
5480 goto out;
5481 }
5482
5483 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005484 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5485 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005486 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005487 }
5488out:
5489 return ret;
5490}
5491
5492static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5493{
5494 int ret = 0;
5495
Yaniv Gardi60f01872016-03-10 17:37:11 +02005496 if (!vreg)
5497 goto out;
5498 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005499 goto out;
5500
5501 ret = ufshcd_config_vreg(dev, vreg, true);
5502 if (!ret)
5503 ret = regulator_enable(vreg->reg);
5504
5505 if (!ret)
5506 vreg->enabled = true;
5507 else
5508 dev_err(dev, "%s: %s enable failed, err=%d\n",
5509 __func__, vreg->name, ret);
5510out:
5511 return ret;
5512}
5513
5514static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5515{
5516 int ret = 0;
5517
Yaniv Gardi60f01872016-03-10 17:37:11 +02005518 if (!vreg)
5519 goto out;
5520 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005521 goto out;
5522
5523 ret = regulator_disable(vreg->reg);
5524
5525 if (!ret) {
5526 /* ignore errors on applying disable config */
5527 ufshcd_config_vreg(dev, vreg, false);
5528 vreg->enabled = false;
5529 } else {
5530 dev_err(dev, "%s: %s disable failed, err=%d\n",
5531 __func__, vreg->name, ret);
5532 }
5533out:
5534 return ret;
5535}
5536
5537static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5538{
5539 int ret = 0;
5540 struct device *dev = hba->dev;
5541 struct ufs_vreg_info *info = &hba->vreg_info;
5542
5543 if (!info)
5544 goto out;
5545
5546 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5547 if (ret)
5548 goto out;
5549
5550 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5551 if (ret)
5552 goto out;
5553
5554 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5555 if (ret)
5556 goto out;
5557
5558out:
5559 if (ret) {
5560 ufshcd_toggle_vreg(dev, info->vccq2, false);
5561 ufshcd_toggle_vreg(dev, info->vccq, false);
5562 ufshcd_toggle_vreg(dev, info->vcc, false);
5563 }
5564 return ret;
5565}
5566
Raviv Shvili6a771a62014-09-25 15:32:24 +03005567static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5568{
5569 struct ufs_vreg_info *info = &hba->vreg_info;
5570
5571 if (info)
5572 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5573
5574 return 0;
5575}
5576
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005577static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5578{
5579 int ret = 0;
5580
5581 if (!vreg)
5582 goto out;
5583
5584 vreg->reg = devm_regulator_get(dev, vreg->name);
5585 if (IS_ERR(vreg->reg)) {
5586 ret = PTR_ERR(vreg->reg);
5587 dev_err(dev, "%s: %s get failed, err=%d\n",
5588 __func__, vreg->name, ret);
5589 }
5590out:
5591 return ret;
5592}
5593
5594static int ufshcd_init_vreg(struct ufs_hba *hba)
5595{
5596 int ret = 0;
5597 struct device *dev = hba->dev;
5598 struct ufs_vreg_info *info = &hba->vreg_info;
5599
5600 if (!info)
5601 goto out;
5602
5603 ret = ufshcd_get_vreg(dev, info->vcc);
5604 if (ret)
5605 goto out;
5606
5607 ret = ufshcd_get_vreg(dev, info->vccq);
5608 if (ret)
5609 goto out;
5610
5611 ret = ufshcd_get_vreg(dev, info->vccq2);
5612out:
5613 return ret;
5614}
5615
Raviv Shvili6a771a62014-09-25 15:32:24 +03005616static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5617{
5618 struct ufs_vreg_info *info = &hba->vreg_info;
5619
5620 if (info)
5621 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5622
5623 return 0;
5624}
5625
Yaniv Gardi60f01872016-03-10 17:37:11 +02005626static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5627{
5628 int ret = 0;
5629 struct ufs_vreg_info *info = &hba->vreg_info;
5630
5631 if (!info)
5632 goto out;
5633 else if (!info->vccq)
5634 goto out;
5635
5636 if (unused) {
5637 /* shut off the rail here */
5638 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5639 /*
5640 * Mark this rail as no longer used, so it doesn't get enabled
5641 * later by mistake
5642 */
5643 if (!ret)
5644 info->vccq->unused = true;
5645 } else {
5646 /*
5647 * rail should have been already enabled hence just make sure
5648 * that unused flag is cleared.
5649 */
5650 info->vccq->unused = false;
5651 }
5652out:
5653 return ret;
5654}
5655
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005656static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5657 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005658{
5659 int ret = 0;
5660 struct ufs_clk_info *clki;
5661 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005662 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005663
5664 if (!head || list_empty(head))
5665 goto out;
5666
5667 list_for_each_entry(clki, head, list) {
5668 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005669 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5670 continue;
5671
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005672 if (on && !clki->enabled) {
5673 ret = clk_prepare_enable(clki->clk);
5674 if (ret) {
5675 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5676 __func__, clki->name, ret);
5677 goto out;
5678 }
5679 } else if (!on && clki->enabled) {
5680 clk_disable_unprepare(clki->clk);
5681 }
5682 clki->enabled = on;
5683 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5684 clki->name, on ? "en" : "dis");
5685 }
5686 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005687
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005688 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005689out:
5690 if (ret) {
5691 list_for_each_entry(clki, head, list) {
5692 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5693 clk_disable_unprepare(clki->clk);
5694 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005695 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005696 spin_lock_irqsave(hba->host->host_lock, flags);
5697 hba->clk_gating.state = CLKS_ON;
5698 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005699 }
5700 return ret;
5701}
5702
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005703static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5704{
5705 return __ufshcd_setup_clocks(hba, on, false);
5706}
5707
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005708static int ufshcd_init_clocks(struct ufs_hba *hba)
5709{
5710 int ret = 0;
5711 struct ufs_clk_info *clki;
5712 struct device *dev = hba->dev;
5713 struct list_head *head = &hba->clk_list_head;
5714
5715 if (!head || list_empty(head))
5716 goto out;
5717
5718 list_for_each_entry(clki, head, list) {
5719 if (!clki->name)
5720 continue;
5721
5722 clki->clk = devm_clk_get(dev, clki->name);
5723 if (IS_ERR(clki->clk)) {
5724 ret = PTR_ERR(clki->clk);
5725 dev_err(dev, "%s: %s clk get failed, %d\n",
5726 __func__, clki->name, ret);
5727 goto out;
5728 }
5729
5730 if (clki->max_freq) {
5731 ret = clk_set_rate(clki->clk, clki->max_freq);
5732 if (ret) {
5733 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5734 __func__, clki->name,
5735 clki->max_freq, ret);
5736 goto out;
5737 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005738 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005739 }
5740 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5741 clki->name, clk_get_rate(clki->clk));
5742 }
5743out:
5744 return ret;
5745}
5746
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005747static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5748{
5749 int err = 0;
5750
5751 if (!hba->vops)
5752 goto out;
5753
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005754 err = ufshcd_vops_init(hba);
5755 if (err)
5756 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005757
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005758 err = ufshcd_vops_setup_regulators(hba, true);
5759 if (err)
5760 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005761
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005762 goto out;
5763
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005764out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005765 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005766out:
5767 if (err)
5768 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005769 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005770 return err;
5771}
5772
5773static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5774{
5775 if (!hba->vops)
5776 return;
5777
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005778 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005779
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005780 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005781
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005782 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005783}
5784
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005785static int ufshcd_hba_init(struct ufs_hba *hba)
5786{
5787 int err;
5788
Raviv Shvili6a771a62014-09-25 15:32:24 +03005789 /*
5790 * Handle host controller power separately from the UFS device power
5791 * rails as it will help controlling the UFS host controller power
5792 * collapse easily which is different than UFS device power collapse.
5793 * Also, enable the host controller power before we go ahead with rest
5794 * of the initialization here.
5795 */
5796 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005797 if (err)
5798 goto out;
5799
Raviv Shvili6a771a62014-09-25 15:32:24 +03005800 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005801 if (err)
5802 goto out;
5803
Raviv Shvili6a771a62014-09-25 15:32:24 +03005804 err = ufshcd_init_clocks(hba);
5805 if (err)
5806 goto out_disable_hba_vreg;
5807
5808 err = ufshcd_setup_clocks(hba, true);
5809 if (err)
5810 goto out_disable_hba_vreg;
5811
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005812 err = ufshcd_init_vreg(hba);
5813 if (err)
5814 goto out_disable_clks;
5815
5816 err = ufshcd_setup_vreg(hba, true);
5817 if (err)
5818 goto out_disable_clks;
5819
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005820 err = ufshcd_variant_hba_init(hba);
5821 if (err)
5822 goto out_disable_vreg;
5823
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005824 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005825 goto out;
5826
5827out_disable_vreg:
5828 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005829out_disable_clks:
5830 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005831out_disable_hba_vreg:
5832 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005833out:
5834 return err;
5835}
5836
5837static void ufshcd_hba_exit(struct ufs_hba *hba)
5838{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005839 if (hba->is_powered) {
5840 ufshcd_variant_hba_exit(hba);
5841 ufshcd_setup_vreg(hba, false);
5842 ufshcd_setup_clocks(hba, false);
5843 ufshcd_setup_hba_vreg(hba, false);
5844 hba->is_powered = false;
5845 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005846}
5847
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005848static int
5849ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305850{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005851 unsigned char cmd[6] = {REQUEST_SENSE,
5852 0,
5853 0,
5854 0,
5855 SCSI_SENSE_BUFFERSIZE,
5856 0};
5857 char *buffer;
5858 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305859
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005860 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5861 if (!buffer) {
5862 ret = -ENOMEM;
5863 goto out;
5864 }
5865
5866 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
5867 SCSI_SENSE_BUFFERSIZE, NULL,
5868 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5869 if (ret)
5870 pr_err("%s: failed with err %d\n", __func__, ret);
5871
5872 kfree(buffer);
5873out:
5874 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305875}
5876
5877/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005878 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5879 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305880 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005881 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305882 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005883 * Returns 0 if requested power mode is set successfully
5884 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305885 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005886static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5887 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305888{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005889 unsigned char cmd[6] = { START_STOP };
5890 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005891 struct scsi_device *sdp;
5892 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005893 int ret;
5894
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005895 spin_lock_irqsave(hba->host->host_lock, flags);
5896 sdp = hba->sdev_ufs_device;
5897 if (sdp) {
5898 ret = scsi_device_get(sdp);
5899 if (!ret && !scsi_device_online(sdp)) {
5900 ret = -ENODEV;
5901 scsi_device_put(sdp);
5902 }
5903 } else {
5904 ret = -ENODEV;
5905 }
5906 spin_unlock_irqrestore(hba->host->host_lock, flags);
5907
5908 if (ret)
5909 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005910
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305911 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005912 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5913 * handling, which would wait for host to be resumed. Since we know
5914 * we are functional while we are here, skip host resume in error
5915 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305916 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005917 hba->host->eh_noresume = 1;
5918 if (hba->wlun_dev_clr_ua) {
5919 ret = ufshcd_send_request_sense(hba, sdp);
5920 if (ret)
5921 goto out;
5922 /* Unit attention condition is cleared now */
5923 hba->wlun_dev_clr_ua = false;
5924 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305925
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005926 cmd[4] = pwr_mode << 4;
5927
5928 /*
5929 * Current function would be generally called from the power management
5930 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5931 * already suspended childs.
5932 */
5933 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5934 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5935 if (ret) {
5936 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005937 "START_STOP failed for power mode: %d, result %x\n",
5938 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005939 if (driver_byte(ret) & DRIVER_SENSE)
5940 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005941 }
5942
5943 if (!ret)
5944 hba->curr_dev_pwr_mode = pwr_mode;
5945out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005946 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005947 hba->host->eh_noresume = 0;
5948 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305949}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305950
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005951static int ufshcd_link_state_transition(struct ufs_hba *hba,
5952 enum uic_link_state req_link_state,
5953 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305954{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005955 int ret = 0;
5956
5957 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305958 return 0;
5959
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005960 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5961 ret = ufshcd_uic_hibern8_enter(hba);
5962 if (!ret)
5963 ufshcd_set_link_hibern8(hba);
5964 else
5965 goto out;
5966 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305967 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005968 * If autobkops is enabled, link can't be turned off because
5969 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305970 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005971 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5972 (!check_for_bkops || (check_for_bkops &&
5973 !hba->auto_bkops_enabled))) {
5974 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005975 * Let's make sure that link is in low power mode, we are doing
5976 * this currently by putting the link in Hibern8. Otherway to
5977 * put the link in low power mode is to send the DME end point
5978 * to device and then send the DME reset command to local
5979 * unipro. But putting the link in hibern8 is much faster.
5980 */
5981 ret = ufshcd_uic_hibern8_enter(hba);
5982 if (ret)
5983 goto out;
5984 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005985 * Change controller state to "reset state" which
5986 * should also put the link in off/reset state
5987 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005988 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005989 /*
5990 * TODO: Check if we need any delay to make sure that
5991 * controller is reset
5992 */
5993 ufshcd_set_link_off(hba);
5994 }
5995
5996out:
5997 return ret;
5998}
5999
6000static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6001{
6002 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006003 * It seems some UFS devices may keep drawing more than sleep current
6004 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6005 * To avoid this situation, add 2ms delay before putting these UFS
6006 * rails in LPM mode.
6007 */
6008 if (!ufshcd_is_link_active(hba) &&
6009 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6010 usleep_range(2000, 2100);
6011
6012 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006013 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6014 * power.
6015 *
6016 * If UFS device and link is in OFF state, all power supplies (VCC,
6017 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6018 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6019 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6020 *
6021 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6022 * in low power state which would save some power.
6023 */
6024 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6025 !hba->dev_info.is_lu_power_on_wp) {
6026 ufshcd_setup_vreg(hba, false);
6027 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6028 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6029 if (!ufshcd_is_link_active(hba)) {
6030 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6031 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6032 }
6033 }
6034}
6035
6036static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6037{
6038 int ret = 0;
6039
6040 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6041 !hba->dev_info.is_lu_power_on_wp) {
6042 ret = ufshcd_setup_vreg(hba, true);
6043 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6044 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
6045 if (!ret && !ufshcd_is_link_active(hba)) {
6046 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6047 if (ret)
6048 goto vcc_disable;
6049 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6050 if (ret)
6051 goto vccq_lpm;
6052 }
6053 }
6054 goto out;
6055
6056vccq_lpm:
6057 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6058vcc_disable:
6059 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6060out:
6061 return ret;
6062}
6063
6064static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6065{
6066 if (ufshcd_is_link_off(hba))
6067 ufshcd_setup_hba_vreg(hba, false);
6068}
6069
6070static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6071{
6072 if (ufshcd_is_link_off(hba))
6073 ufshcd_setup_hba_vreg(hba, true);
6074}
6075
6076/**
6077 * ufshcd_suspend - helper function for suspend operations
6078 * @hba: per adapter instance
6079 * @pm_op: desired low power operation type
6080 *
6081 * This function will try to put the UFS device and link into low power
6082 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6083 * (System PM level).
6084 *
6085 * If this function is called during shutdown, it will make sure that
6086 * both UFS device and UFS link is powered off.
6087 *
6088 * NOTE: UFS device & link must be active before we enter in this function.
6089 *
6090 * Returns 0 for success and non-zero for failure
6091 */
6092static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6093{
6094 int ret = 0;
6095 enum ufs_pm_level pm_lvl;
6096 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6097 enum uic_link_state req_link_state;
6098
6099 hba->pm_op_in_progress = 1;
6100 if (!ufshcd_is_shutdown_pm(pm_op)) {
6101 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6102 hba->rpm_lvl : hba->spm_lvl;
6103 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6104 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6105 } else {
6106 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6107 req_link_state = UIC_LINK_OFF_STATE;
6108 }
6109
6110 /*
6111 * If we can't transition into any of the low power modes
6112 * just gate the clocks.
6113 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006114 ufshcd_hold(hba, false);
6115 hba->clk_gating.is_suspended = true;
6116
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006117 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6118 req_link_state == UIC_LINK_ACTIVE_STATE) {
6119 goto disable_clks;
6120 }
6121
6122 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6123 (req_link_state == hba->uic_link_state))
6124 goto out;
6125
6126 /* UFS device & link must be active before we enter in this function */
6127 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6128 ret = -EINVAL;
6129 goto out;
6130 }
6131
6132 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006133 if (ufshcd_can_autobkops_during_suspend(hba)) {
6134 /*
6135 * The device is idle with no requests in the queue,
6136 * allow background operations if bkops status shows
6137 * that performance might be impacted.
6138 */
6139 ret = ufshcd_urgent_bkops(hba);
6140 if (ret)
6141 goto enable_gating;
6142 } else {
6143 /* make sure that auto bkops is disabled */
6144 ufshcd_disable_auto_bkops(hba);
6145 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006146 }
6147
6148 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6149 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6150 !ufshcd_is_runtime_pm(pm_op))) {
6151 /* ensure that bkops is disabled */
6152 ufshcd_disable_auto_bkops(hba);
6153 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6154 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006155 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006156 }
6157
6158 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6159 if (ret)
6160 goto set_dev_active;
6161
6162 ufshcd_vreg_set_lpm(hba);
6163
6164disable_clks:
6165 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03006166 * The clock scaling needs access to controller registers. Hence, Wait
6167 * for pending clock scaling work to be done before clocks are
6168 * turned off.
6169 */
6170 if (ufshcd_is_clkscaling_enabled(hba)) {
6171 devfreq_suspend_device(hba->devfreq);
6172 hba->clk_scaling.window_start_t = 0;
6173 }
6174 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006175 * Call vendor specific suspend callback. As these callbacks may access
6176 * vendor specific host controller register space call them before the
6177 * host clocks are ON.
6178 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006179 ret = ufshcd_vops_suspend(hba, pm_op);
6180 if (ret)
6181 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006182
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006183 ret = ufshcd_vops_setup_clocks(hba, false);
6184 if (ret)
6185 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006186
6187 if (!ufshcd_is_link_active(hba))
6188 ufshcd_setup_clocks(hba, false);
6189 else
6190 /* If link is active, device ref_clk can't be switched off */
6191 __ufshcd_setup_clocks(hba, false, true);
6192
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006193 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006194 /*
6195 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006196 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006197 */
6198 ufshcd_disable_irq(hba);
6199 /* Put the host controller in low power mode if possible */
6200 ufshcd_hba_vreg_set_lpm(hba);
6201 goto out;
6202
6203vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006204 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006205set_link_active:
6206 ufshcd_vreg_set_hpm(hba);
6207 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6208 ufshcd_set_link_active(hba);
6209 else if (ufshcd_is_link_off(hba))
6210 ufshcd_host_reset_and_restore(hba);
6211set_dev_active:
6212 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6213 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006214enable_gating:
6215 hba->clk_gating.is_suspended = false;
6216 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006217out:
6218 hba->pm_op_in_progress = 0;
6219 return ret;
6220}
6221
6222/**
6223 * ufshcd_resume - helper function for resume operations
6224 * @hba: per adapter instance
6225 * @pm_op: runtime PM or system PM
6226 *
6227 * This function basically brings the UFS device, UniPro link and controller
6228 * to active state.
6229 *
6230 * Returns 0 for success and non-zero for failure
6231 */
6232static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6233{
6234 int ret;
6235 enum uic_link_state old_link_state;
6236
6237 hba->pm_op_in_progress = 1;
6238 old_link_state = hba->uic_link_state;
6239
6240 ufshcd_hba_vreg_set_hpm(hba);
6241 /* Make sure clocks are enabled before accessing controller */
6242 ret = ufshcd_setup_clocks(hba, true);
6243 if (ret)
6244 goto out;
6245
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006246 /* enable the host irq as host controller would be active soon */
6247 ret = ufshcd_enable_irq(hba);
6248 if (ret)
6249 goto disable_irq_and_vops_clks;
6250
6251 ret = ufshcd_vreg_set_hpm(hba);
6252 if (ret)
6253 goto disable_irq_and_vops_clks;
6254
6255 /*
6256 * Call vendor specific resume callback. As these callbacks may access
6257 * vendor specific host controller register space call them when the
6258 * host clocks are ON.
6259 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006260 ret = ufshcd_vops_resume(hba, pm_op);
6261 if (ret)
6262 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006263
6264 if (ufshcd_is_link_hibern8(hba)) {
6265 ret = ufshcd_uic_hibern8_exit(hba);
6266 if (!ret)
6267 ufshcd_set_link_active(hba);
6268 else
6269 goto vendor_suspend;
6270 } else if (ufshcd_is_link_off(hba)) {
6271 ret = ufshcd_host_reset_and_restore(hba);
6272 /*
6273 * ufshcd_host_reset_and_restore() should have already
6274 * set the link state as active
6275 */
6276 if (ret || !ufshcd_is_link_active(hba))
6277 goto vendor_suspend;
6278 }
6279
6280 if (!ufshcd_is_ufs_dev_active(hba)) {
6281 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6282 if (ret)
6283 goto set_old_link_state;
6284 }
6285
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08006286 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
6287 ufshcd_enable_auto_bkops(hba);
6288 else
6289 /*
6290 * If BKOPs operations are urgently needed at this moment then
6291 * keep auto-bkops enabled or else disable it.
6292 */
6293 ufshcd_urgent_bkops(hba);
6294
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006295 hba->clk_gating.is_suspended = false;
6296
Sahitya Tummala856b3482014-09-25 15:32:34 +03006297 if (ufshcd_is_clkscaling_enabled(hba))
6298 devfreq_resume_device(hba->devfreq);
6299
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006300 /* Schedule clock gating in case of no access to UFS device yet */
6301 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006302 goto out;
6303
6304set_old_link_state:
6305 ufshcd_link_state_transition(hba, old_link_state, 0);
6306vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006307 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006308disable_vreg:
6309 ufshcd_vreg_set_lpm(hba);
6310disable_irq_and_vops_clks:
6311 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006312 ufshcd_setup_clocks(hba, false);
6313out:
6314 hba->pm_op_in_progress = 0;
6315 return ret;
6316}
6317
6318/**
6319 * ufshcd_system_suspend - system suspend routine
6320 * @hba: per adapter instance
6321 * @pm_op: runtime PM or system PM
6322 *
6323 * Check the description of ufshcd_suspend() function for more details.
6324 *
6325 * Returns 0 for success and non-zero for failure
6326 */
6327int ufshcd_system_suspend(struct ufs_hba *hba)
6328{
6329 int ret = 0;
6330
6331 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006332 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006333
6334 if (pm_runtime_suspended(hba->dev)) {
6335 if (hba->rpm_lvl == hba->spm_lvl)
6336 /*
6337 * There is possibility that device may still be in
6338 * active state during the runtime suspend.
6339 */
6340 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6341 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6342 goto out;
6343
6344 /*
6345 * UFS device and/or UFS link low power states during runtime
6346 * suspend seems to be different than what is expected during
6347 * system suspend. Hence runtime resume the devic & link and
6348 * let the system suspend low power states to take effect.
6349 * TODO: If resume takes longer time, we might have optimize
6350 * it in future by not resuming everything if possible.
6351 */
6352 ret = ufshcd_runtime_resume(hba);
6353 if (ret)
6354 goto out;
6355 }
6356
6357 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6358out:
Dolev Ravive7850602014-09-25 15:32:36 +03006359 if (!ret)
6360 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006361 return ret;
6362}
6363EXPORT_SYMBOL(ufshcd_system_suspend);
6364
6365/**
6366 * ufshcd_system_resume - system resume routine
6367 * @hba: per adapter instance
6368 *
6369 * Returns 0 for success and non-zero for failure
6370 */
6371
6372int ufshcd_system_resume(struct ufs_hba *hba)
6373{
6374 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
6375 /*
6376 * Let the runtime resume take care of resuming
6377 * if runtime suspended.
6378 */
6379 return 0;
6380
6381 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6382}
6383EXPORT_SYMBOL(ufshcd_system_resume);
6384
6385/**
6386 * ufshcd_runtime_suspend - runtime suspend routine
6387 * @hba: per adapter instance
6388 *
6389 * Check the description of ufshcd_suspend() function for more details.
6390 *
6391 * Returns 0 for success and non-zero for failure
6392 */
6393int ufshcd_runtime_suspend(struct ufs_hba *hba)
6394{
6395 if (!hba || !hba->is_powered)
6396 return 0;
6397
6398 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306399}
6400EXPORT_SYMBOL(ufshcd_runtime_suspend);
6401
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006402/**
6403 * ufshcd_runtime_resume - runtime resume routine
6404 * @hba: per adapter instance
6405 *
6406 * This function basically brings the UFS device, UniPro link and controller
6407 * to active state. Following operations are done in this function:
6408 *
6409 * 1. Turn on all the controller related clocks
6410 * 2. Bring the UniPro link out of Hibernate state
6411 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6412 * to active state.
6413 * 4. If auto-bkops is enabled on the device, disable it.
6414 *
6415 * So following would be the possible power state after this function return
6416 * successfully:
6417 * S1: UFS device in Active state with VCC rail ON
6418 * UniPro link in Active state
6419 * All the UFS/UniPro controller clocks are ON
6420 *
6421 * Returns 0 for success and non-zero for failure
6422 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306423int ufshcd_runtime_resume(struct ufs_hba *hba)
6424{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006425 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306426 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006427 else
6428 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306429}
6430EXPORT_SYMBOL(ufshcd_runtime_resume);
6431
6432int ufshcd_runtime_idle(struct ufs_hba *hba)
6433{
6434 return 0;
6435}
6436EXPORT_SYMBOL(ufshcd_runtime_idle);
6437
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306438/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006439 * ufshcd_shutdown - shutdown routine
6440 * @hba: per adapter instance
6441 *
6442 * This function would power off both UFS device and UFS link.
6443 *
6444 * Returns 0 always to allow force shutdown even in case of errors.
6445 */
6446int ufshcd_shutdown(struct ufs_hba *hba)
6447{
6448 int ret = 0;
6449
6450 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6451 goto out;
6452
6453 if (pm_runtime_suspended(hba->dev)) {
6454 ret = ufshcd_runtime_resume(hba);
6455 if (ret)
6456 goto out;
6457 }
6458
6459 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6460out:
6461 if (ret)
6462 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6463 /* allow force shutdown even in case of errors */
6464 return 0;
6465}
6466EXPORT_SYMBOL(ufshcd_shutdown);
6467
6468/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306469 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306470 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306471 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306472 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306473void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306474{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306475 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306476 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306477 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006478 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306479
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306480 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006481
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006482 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006483 if (ufshcd_is_clkscaling_enabled(hba))
6484 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006485 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306486}
6487EXPORT_SYMBOL_GPL(ufshcd_remove);
6488
6489/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006490 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6491 * @hba: pointer to Host Bus Adapter (HBA)
6492 */
6493void ufshcd_dealloc_host(struct ufs_hba *hba)
6494{
6495 scsi_host_put(hba->host);
6496}
6497EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6498
6499/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006500 * ufshcd_set_dma_mask - Set dma mask based on the controller
6501 * addressing capability
6502 * @hba: per adapter instance
6503 *
6504 * Returns 0 for success, non-zero for failure
6505 */
6506static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6507{
6508 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6509 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6510 return 0;
6511 }
6512 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6513}
6514
6515/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006516 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306517 * @dev: pointer to device handle
6518 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306519 * Returns 0 on success, non-zero value on failure
6520 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006521int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306522{
6523 struct Scsi_Host *host;
6524 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006525 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306526
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306527 if (!dev) {
6528 dev_err(dev,
6529 "Invalid memory reference for dev is NULL\n");
6530 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306531 goto out_error;
6532 }
6533
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306534 host = scsi_host_alloc(&ufshcd_driver_template,
6535 sizeof(struct ufs_hba));
6536 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306537 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306538 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306539 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306540 }
6541 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306542 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306543 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006544 *hba_handle = hba;
6545
6546out_error:
6547 return err;
6548}
6549EXPORT_SYMBOL(ufshcd_alloc_host);
6550
Sahitya Tummala856b3482014-09-25 15:32:34 +03006551static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6552{
6553 int ret = 0;
6554 struct ufs_clk_info *clki;
6555 struct list_head *head = &hba->clk_list_head;
6556
6557 if (!head || list_empty(head))
6558 goto out;
6559
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006560 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6561 if (ret)
6562 return ret;
6563
Sahitya Tummala856b3482014-09-25 15:32:34 +03006564 list_for_each_entry(clki, head, list) {
6565 if (!IS_ERR_OR_NULL(clki->clk)) {
6566 if (scale_up && clki->max_freq) {
6567 if (clki->curr_freq == clki->max_freq)
6568 continue;
6569 ret = clk_set_rate(clki->clk, clki->max_freq);
6570 if (ret) {
6571 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6572 __func__, clki->name,
6573 clki->max_freq, ret);
6574 break;
6575 }
6576 clki->curr_freq = clki->max_freq;
6577
6578 } else if (!scale_up && clki->min_freq) {
6579 if (clki->curr_freq == clki->min_freq)
6580 continue;
6581 ret = clk_set_rate(clki->clk, clki->min_freq);
6582 if (ret) {
6583 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6584 __func__, clki->name,
6585 clki->min_freq, ret);
6586 break;
6587 }
6588 clki->curr_freq = clki->min_freq;
6589 }
6590 }
6591 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6592 clki->name, clk_get_rate(clki->clk));
6593 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006594
6595 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6596
Sahitya Tummala856b3482014-09-25 15:32:34 +03006597out:
6598 return ret;
6599}
6600
6601static int ufshcd_devfreq_target(struct device *dev,
6602 unsigned long *freq, u32 flags)
6603{
6604 int err = 0;
6605 struct ufs_hba *hba = dev_get_drvdata(dev);
6606
6607 if (!ufshcd_is_clkscaling_enabled(hba))
6608 return -EINVAL;
6609
6610 if (*freq == UINT_MAX)
6611 err = ufshcd_scale_clks(hba, true);
6612 else if (*freq == 0)
6613 err = ufshcd_scale_clks(hba, false);
6614
6615 return err;
6616}
6617
6618static int ufshcd_devfreq_get_dev_status(struct device *dev,
6619 struct devfreq_dev_status *stat)
6620{
6621 struct ufs_hba *hba = dev_get_drvdata(dev);
6622 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6623 unsigned long flags;
6624
6625 if (!ufshcd_is_clkscaling_enabled(hba))
6626 return -EINVAL;
6627
6628 memset(stat, 0, sizeof(*stat));
6629
6630 spin_lock_irqsave(hba->host->host_lock, flags);
6631 if (!scaling->window_start_t)
6632 goto start_window;
6633
6634 if (scaling->is_busy_started)
6635 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6636 scaling->busy_start_t));
6637
6638 stat->total_time = jiffies_to_usecs((long)jiffies -
6639 (long)scaling->window_start_t);
6640 stat->busy_time = scaling->tot_busy_t;
6641start_window:
6642 scaling->window_start_t = jiffies;
6643 scaling->tot_busy_t = 0;
6644
6645 if (hba->outstanding_reqs) {
6646 scaling->busy_start_t = ktime_get();
6647 scaling->is_busy_started = true;
6648 } else {
6649 scaling->busy_start_t = ktime_set(0, 0);
6650 scaling->is_busy_started = false;
6651 }
6652 spin_unlock_irqrestore(hba->host->host_lock, flags);
6653 return 0;
6654}
6655
6656static struct devfreq_dev_profile ufs_devfreq_profile = {
6657 .polling_ms = 100,
6658 .target = ufshcd_devfreq_target,
6659 .get_dev_status = ufshcd_devfreq_get_dev_status,
6660};
6661
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006662/**
6663 * ufshcd_init - Driver initialization routine
6664 * @hba: per-adapter instance
6665 * @mmio_base: base register address
6666 * @irq: Interrupt line of device
6667 * Returns 0 on success, non-zero value on failure
6668 */
6669int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6670{
6671 int err;
6672 struct Scsi_Host *host = hba->host;
6673 struct device *dev = hba->dev;
6674
6675 if (!mmio_base) {
6676 dev_err(hba->dev,
6677 "Invalid memory reference for mmio_base is NULL\n");
6678 err = -ENODEV;
6679 goto out_error;
6680 }
6681
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306682 hba->mmio_base = mmio_base;
6683 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306684
Potomski, MichalXe1928452017-02-23 09:05:30 +00006685 /* Set descriptor lengths to specification defaults */
6686 ufshcd_def_desc_sizes(hba);
6687
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006688 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006689 if (err)
6690 goto out_error;
6691
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306692 /* Read capabilities registers */
6693 ufshcd_hba_capabilities(hba);
6694
6695 /* Get UFS version supported by the controller */
6696 hba->ufs_version = ufshcd_get_ufs_version(hba);
6697
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306698 /* Get Interrupt bit mask per version */
6699 hba->intr_mask = ufshcd_get_intr_mask(hba);
6700
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006701 err = ufshcd_set_dma_mask(hba);
6702 if (err) {
6703 dev_err(hba->dev, "set dma mask failed\n");
6704 goto out_disable;
6705 }
6706
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306707 /* Allocate memory for host memory space */
6708 err = ufshcd_memory_alloc(hba);
6709 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306710 dev_err(hba->dev, "Memory allocation failed\n");
6711 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306712 }
6713
6714 /* Configure LRB */
6715 ufshcd_host_memory_configure(hba);
6716
6717 host->can_queue = hba->nutrs;
6718 host->cmd_per_lun = hba->nutrs;
6719 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006720 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306721 host->max_channel = UFSHCD_MAX_CHANNEL;
6722 host->unique_id = host->host_no;
6723 host->max_cmd_len = MAX_CDB_SIZE;
6724
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006725 hba->max_pwr_info.is_valid = false;
6726
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306727 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306728 init_waitqueue_head(&hba->tm_wq);
6729 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306730
6731 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306732 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306733 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306734
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306735 /* Initialize UIC command mutex */
6736 mutex_init(&hba->uic_cmd_mutex);
6737
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306738 /* Initialize mutex for device management commands */
6739 mutex_init(&hba->dev_cmd.lock);
6740
6741 /* Initialize device management tag acquire wait queue */
6742 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6743
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006744 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006745
6746 /*
6747 * In order to avoid any spurious interrupt immediately after
6748 * registering UFS controller interrupt handler, clear any pending UFS
6749 * interrupt status and disable all the UFS interrupts.
6750 */
6751 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6752 REG_INTERRUPT_STATUS);
6753 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6754 /*
6755 * Make sure that UFS interrupts are disabled and any pending interrupt
6756 * status is cleared before registering UFS interrupt handler.
6757 */
6758 mb();
6759
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306760 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006761 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306762 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306763 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006764 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006765 } else {
6766 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306767 }
6768
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306769 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306770 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306771 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006772 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306773 }
6774
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306775 /* Host controller enable */
6776 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306777 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306778 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306779 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306780 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306781
Sahitya Tummala856b3482014-09-25 15:32:34 +03006782 if (ufshcd_is_clkscaling_enabled(hba)) {
6783 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6784 "simple_ondemand", NULL);
6785 if (IS_ERR(hba->devfreq)) {
6786 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6787 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006788 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006789 goto out_remove_scsi_host;
6790 }
6791 /* Suspend devfreq until the UFS device is detected */
6792 devfreq_suspend_device(hba->devfreq);
6793 hba->clk_scaling.window_start_t = 0;
6794 }
6795
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306796 /* Hold auto suspend until async scan completes */
6797 pm_runtime_get_sync(dev);
6798
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006799 /*
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006800 * We are assuming that device wasn't put in sleep/power-down
6801 * state exclusively during the boot stage before kernel.
6802 * This assumption helps avoid doing link startup twice during
6803 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006804 */
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006805 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006806
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306807 async_schedule(ufshcd_async_scan, hba);
6808
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306809 return 0;
6810
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306811out_remove_scsi_host:
6812 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006813exit_gating:
6814 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306815out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006816 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306817 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006818 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306819out_error:
6820 return err;
6821}
6822EXPORT_SYMBOL_GPL(ufshcd_init);
6823
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306824MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6825MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306826MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306827MODULE_LICENSE("GPL");
6828MODULE_VERSION(UFSHCD_DRIVER_VERSION);