blob: f857086ce2fa986a690b59012000443d67b5dc3c [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);
Maya Erez62413ba2018-05-03 16:37:16 +05304015 scsi_block_requests(hba->host);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304016 err = ufshcd_get_ee_status(hba, &status);
4017 if (err) {
4018 dev_err(hba->dev, "%s: failed to get exception status %d\n",
4019 __func__, err);
4020 goto out;
4021 }
4022
4023 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02004024
4025 if (status & MASK_EE_URGENT_BKOPS)
4026 ufshcd_bkops_exception_event_handler(hba);
4027
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304028out:
Maya Erez62413ba2018-05-03 16:37:16 +05304029 scsi_unblock_requests(hba->host);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304030 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304031 return;
4032}
4033
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004034/* Complete requests that have door-bell cleared */
4035static void ufshcd_complete_requests(struct ufs_hba *hba)
4036{
4037 ufshcd_transfer_req_compl(hba);
4038 ufshcd_tmc_handler(hba);
4039}
4040
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304041/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02004042 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4043 * to recover from the DL NAC errors or not.
4044 * @hba: per-adapter instance
4045 *
4046 * Returns true if error handling is required, false otherwise
4047 */
4048static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
4049{
4050 unsigned long flags;
4051 bool err_handling = true;
4052
4053 spin_lock_irqsave(hba->host->host_lock, flags);
4054 /*
4055 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4056 * device fatal error and/or DL NAC & REPLAY timeout errors.
4057 */
4058 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
4059 goto out;
4060
4061 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
4062 ((hba->saved_err & UIC_ERROR) &&
4063 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
4064 goto out;
4065
4066 if ((hba->saved_err & UIC_ERROR) &&
4067 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
4068 int err;
4069 /*
4070 * wait for 50ms to see if we can get any other errors or not.
4071 */
4072 spin_unlock_irqrestore(hba->host->host_lock, flags);
4073 msleep(50);
4074 spin_lock_irqsave(hba->host->host_lock, flags);
4075
4076 /*
4077 * now check if we have got any other severe errors other than
4078 * DL NAC error?
4079 */
4080 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4081 ((hba->saved_err & UIC_ERROR) &&
4082 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
4083 goto out;
4084
4085 /*
4086 * As DL NAC is the only error received so far, send out NOP
4087 * command to confirm if link is still active or not.
4088 * - If we don't get any response then do error recovery.
4089 * - If we get response then clear the DL NAC error bit.
4090 */
4091
4092 spin_unlock_irqrestore(hba->host->host_lock, flags);
4093 err = ufshcd_verify_dev_init(hba);
4094 spin_lock_irqsave(hba->host->host_lock, flags);
4095
4096 if (err)
4097 goto out;
4098
4099 /* Link seems to be alive hence ignore the DL NAC errors */
4100 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
4101 hba->saved_err &= ~UIC_ERROR;
4102 /* clear NAC error */
4103 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4104 if (!hba->saved_uic_err) {
4105 err_handling = false;
4106 goto out;
4107 }
4108 }
4109out:
4110 spin_unlock_irqrestore(hba->host->host_lock, flags);
4111 return err_handling;
4112}
4113
4114/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304115 * ufshcd_err_handler - handle UFS errors that require s/w attention
4116 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304117 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304118static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304119{
4120 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304121 unsigned long flags;
4122 u32 err_xfer = 0;
4123 u32 err_tm = 0;
4124 int err = 0;
4125 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004126 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304127
4128 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304129
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304130 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004131 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304132
4133 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004134 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304135 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304136
4137 hba->ufshcd_state = UFSHCD_STATE_RESET;
4138 ufshcd_set_eh_in_progress(hba);
4139
4140 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004141 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004142
4143 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4144 bool ret;
4145
4146 spin_unlock_irqrestore(hba->host->host_lock, flags);
4147 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4148 ret = ufshcd_quirk_dl_nac_errors(hba);
4149 spin_lock_irqsave(hba->host->host_lock, flags);
4150 if (!ret)
4151 goto skip_err_handling;
4152 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004153 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4154 ((hba->saved_err & UIC_ERROR) &&
4155 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4156 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4157 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4158 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304159
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004160 /*
4161 * if host reset is required then skip clearing the pending
4162 * transfers forcefully because they will automatically get
4163 * cleared after link startup.
4164 */
4165 if (needs_reset)
4166 goto skip_pending_xfer_clear;
4167
4168 /* release lock as clear command might sleep */
4169 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304170 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004171 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4172 if (ufshcd_clear_cmd(hba, tag)) {
4173 err_xfer = true;
4174 goto lock_skip_pending_xfer_clear;
4175 }
4176 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304177
4178 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004179 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4180 if (ufshcd_clear_tm_cmd(hba, tag)) {
4181 err_tm = true;
4182 goto lock_skip_pending_xfer_clear;
4183 }
4184 }
4185
4186lock_skip_pending_xfer_clear:
4187 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304188
4189 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004190 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304191
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004192 if (err_xfer || err_tm)
4193 needs_reset = true;
4194
4195skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304196 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004197 if (needs_reset) {
4198 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4199
4200 /*
4201 * ufshcd_reset_and_restore() does the link reinitialization
4202 * which will need atleast one empty doorbell slot to send the
4203 * device management commands (NOP and query commands).
4204 * If there is no slot empty at this moment then free up last
4205 * slot forcefully.
4206 */
4207 if (hba->outstanding_reqs == max_doorbells)
4208 __ufshcd_transfer_req_compl(hba,
4209 (1UL << (hba->nutrs - 1)));
4210
4211 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304212 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004213 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304214 if (err) {
4215 dev_err(hba->dev, "%s: reset and restore failed\n",
4216 __func__);
4217 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4218 }
4219 /*
4220 * Inform scsi mid-layer that we did reset and allow to handle
4221 * Unit Attention properly.
4222 */
4223 scsi_report_bus_reset(hba->host, 0);
4224 hba->saved_err = 0;
4225 hba->saved_uic_err = 0;
4226 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004227
Yaniv Gardi583fa622016-03-10 17:37:13 +02004228skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004229 if (!needs_reset) {
4230 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4231 if (hba->saved_err || hba->saved_uic_err)
4232 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4233 __func__, hba->saved_err, hba->saved_uic_err);
4234 }
4235
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304236 ufshcd_clear_eh_in_progress(hba);
4237
4238out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004239 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304240 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004241 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304242 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304243}
4244
4245/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304246 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4247 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304248 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304249static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304250{
4251 u32 reg;
4252
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304253 /* PA_INIT_ERROR is fatal and needs UIC reset */
4254 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4255 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4256 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004257 else if (hba->dev_quirks &
4258 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4259 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4260 hba->uic_error |=
4261 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4262 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4263 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4264 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304265
4266 /* UIC NL/TL/DME errors needs software retry */
4267 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4268 if (reg)
4269 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4270
4271 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4272 if (reg)
4273 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4274
4275 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4276 if (reg)
4277 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4278
4279 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4280 __func__, hba->uic_error);
4281}
4282
4283/**
4284 * ufshcd_check_errors - Check for errors that need s/w attention
4285 * @hba: per-adapter instance
4286 */
4287static void ufshcd_check_errors(struct ufs_hba *hba)
4288{
4289 bool queue_eh_work = false;
4290
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304291 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304292 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304293
4294 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304295 hba->uic_error = 0;
4296 ufshcd_update_uic_error(hba);
4297 if (hba->uic_error)
4298 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304299 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304300
4301 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004302 /*
4303 * update the transfer error masks to sticky bits, let's do this
4304 * irrespective of current ufshcd_state.
4305 */
4306 hba->saved_err |= hba->errors;
4307 hba->saved_uic_err |= hba->uic_error;
4308
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304309 /* handle fatal errors only when link is functional */
4310 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4311 /* block commands from scsi mid-layer */
4312 scsi_block_requests(hba->host);
4313
Zang Leiganga17bddc2017-04-04 19:32:20 +00004314 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304315 schedule_work(&hba->eh_work);
4316 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304317 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304318 /*
4319 * if (!queue_eh_work) -
4320 * Other errors are either non-fatal where host recovers
4321 * itself without s/w intervention or errors that will be
4322 * handled by the SCSI core layer.
4323 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304324}
4325
4326/**
4327 * ufshcd_tmc_handler - handle task management function completion
4328 * @hba: per adapter instance
4329 */
4330static void ufshcd_tmc_handler(struct ufs_hba *hba)
4331{
4332 u32 tm_doorbell;
4333
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304334 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304335 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304336 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304337}
4338
4339/**
4340 * ufshcd_sl_intr - Interrupt service routine
4341 * @hba: per adapter instance
4342 * @intr_status: contains interrupts generated by the controller
4343 */
4344static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4345{
4346 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4347 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304348 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304349
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304350 if (intr_status & UFSHCD_UIC_MASK)
4351 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304352
4353 if (intr_status & UTP_TASK_REQ_COMPL)
4354 ufshcd_tmc_handler(hba);
4355
4356 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4357 ufshcd_transfer_req_compl(hba);
4358}
4359
4360/**
4361 * ufshcd_intr - Main interrupt service routine
4362 * @irq: irq number
4363 * @__hba: pointer to adapter instance
4364 *
4365 * Returns IRQ_HANDLED - If interrupt is valid
4366 * IRQ_NONE - If invalid interrupt
4367 */
4368static irqreturn_t ufshcd_intr(int irq, void *__hba)
4369{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004370 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304371 irqreturn_t retval = IRQ_NONE;
4372 struct ufs_hba *hba = __hba;
4373
4374 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304375 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004376 enabled_intr_status =
4377 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304378
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004379 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304380 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004381
4382 if (enabled_intr_status) {
4383 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304384 retval = IRQ_HANDLED;
4385 }
4386 spin_unlock(hba->host->host_lock);
4387 return retval;
4388}
4389
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304390static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4391{
4392 int err = 0;
4393 u32 mask = 1 << tag;
4394 unsigned long flags;
4395
4396 if (!test_bit(tag, &hba->outstanding_tasks))
4397 goto out;
4398
4399 spin_lock_irqsave(hba->host->host_lock, flags);
4400 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4401 spin_unlock_irqrestore(hba->host->host_lock, flags);
4402
4403 /* poll for max. 1 sec to clear door bell register by h/w */
4404 err = ufshcd_wait_for_register(hba,
4405 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004406 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304407out:
4408 return err;
4409}
4410
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304411/**
4412 * ufshcd_issue_tm_cmd - issues task management commands to controller
4413 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304414 * @lun_id: LUN ID to which TM command is sent
4415 * @task_id: task ID to which the TM command is applicable
4416 * @tm_function: task management function opcode
4417 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304418 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304419 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304420 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304421static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4422 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304423{
4424 struct utp_task_req_desc *task_req_descp;
4425 struct utp_upiu_task_req *task_req_upiup;
4426 struct Scsi_Host *host;
4427 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304428 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304429 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304430 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304431
4432 host = hba->host;
4433
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304434 /*
4435 * Get free slot, sleep if slots are unavailable.
4436 * Even though we use wait_event() which sleeps indefinitely,
4437 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4438 */
4439 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004440 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304441
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304442 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304443 task_req_descp = hba->utmrdl_base_addr;
4444 task_req_descp += free_slot;
4445
4446 /* Configure task request descriptor */
4447 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4448 task_req_descp->header.dword_2 =
4449 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4450
4451 /* Configure task request UPIU */
4452 task_req_upiup =
4453 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304454 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304455 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304456 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304457 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304458 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304459 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004460 /*
4461 * The host shall provide the same value for LUN field in the basic
4462 * header and for Input Parameter.
4463 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304464 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4465 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304466
4467 /* send command to the controller */
4468 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004469
4470 /* Make sure descriptors are ready before ringing the task doorbell */
4471 wmb();
4472
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304473 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304474
4475 spin_unlock_irqrestore(host->host_lock, flags);
4476
4477 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304478 err = wait_event_timeout(hba->tm_wq,
4479 test_bit(free_slot, &hba->tm_condition),
4480 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304481 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304482 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4483 __func__, tm_function);
4484 if (ufshcd_clear_tm_cmd(hba, free_slot))
4485 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4486 __func__, free_slot);
4487 err = -ETIMEDOUT;
4488 } else {
4489 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304490 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304491
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304492 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304493 ufshcd_put_tm_slot(hba, free_slot);
4494 wake_up(&hba->tm_tag_wq);
4495
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004496 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304497 return err;
4498}
4499
4500/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304501 * ufshcd_eh_device_reset_handler - device reset handler registered to
4502 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304503 * @cmd: SCSI command pointer
4504 *
4505 * Returns SUCCESS/FAILED
4506 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304507static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304508{
4509 struct Scsi_Host *host;
4510 struct ufs_hba *hba;
4511 unsigned int tag;
4512 u32 pos;
4513 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304514 u8 resp = 0xF;
4515 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304516 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304517
4518 host = cmd->device->host;
4519 hba = shost_priv(host);
4520 tag = cmd->request->tag;
4521
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304522 lrbp = &hba->lrb[tag];
4523 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4524 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304525 if (!err)
4526 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304527 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304528 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304529
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304530 /* clear the commands that were pending for corresponding LUN */
4531 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4532 if (hba->lrb[pos].lun == lrbp->lun) {
4533 err = ufshcd_clear_cmd(hba, pos);
4534 if (err)
4535 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304536 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304537 }
4538 spin_lock_irqsave(host->host_lock, flags);
4539 ufshcd_transfer_req_compl(hba);
4540 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304541out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304542 if (!err) {
4543 err = SUCCESS;
4544 } else {
4545 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4546 err = FAILED;
4547 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304548 return err;
4549}
4550
4551/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304552 * ufshcd_abort - abort a specific command
4553 * @cmd: SCSI command pointer
4554 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304555 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4556 * command, and in host controller by clearing the door-bell register. There can
4557 * be race between controller sending the command to the device while abort is
4558 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4559 * really issued and then try to abort it.
4560 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304561 * Returns SUCCESS/FAILED
4562 */
4563static int ufshcd_abort(struct scsi_cmnd *cmd)
4564{
4565 struct Scsi_Host *host;
4566 struct ufs_hba *hba;
4567 unsigned long flags;
4568 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304569 int err = 0;
4570 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304571 u8 resp = 0xF;
4572 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004573 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304574
4575 host = cmd->device->host;
4576 hba = shost_priv(host);
4577 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004578 if (!ufshcd_valid_tag(hba, tag)) {
4579 dev_err(hba->dev,
4580 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4581 __func__, tag, cmd, cmd->request);
4582 BUG();
4583 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304584
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004585 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004586 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004587 /* If command is already aborted/completed, return SUCCESS */
4588 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4589 dev_err(hba->dev,
4590 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4591 __func__, tag, hba->outstanding_reqs, reg);
4592 goto out;
4593 }
4594
Dolev Ravive9d501b2014-07-01 12:22:37 +03004595 if (!(reg & (1 << tag))) {
4596 dev_err(hba->dev,
4597 "%s: cmd was completed, but without a notifying intr, tag = %d",
4598 __func__, tag);
4599 }
4600
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304601 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304602 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4603 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4604 UFS_QUERY_TASK, &resp);
4605 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4606 /* cmd pending in the device */
4607 break;
4608 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304609 /*
4610 * cmd not pending in the device, check if it is
4611 * in transition.
4612 */
4613 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4614 if (reg & (1 << tag)) {
4615 /* sleep for max. 200us to stabilize */
4616 usleep_range(100, 200);
4617 continue;
4618 }
4619 /* command completed already */
4620 goto out;
4621 } else {
4622 if (!err)
4623 err = resp; /* service response error */
4624 goto out;
4625 }
4626 }
4627
4628 if (!poll_cnt) {
4629 err = -EBUSY;
4630 goto out;
4631 }
4632
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304633 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4634 UFS_ABORT_TASK, &resp);
4635 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304636 if (!err)
4637 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304638 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304639 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304640
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304641 err = ufshcd_clear_cmd(hba, tag);
4642 if (err)
4643 goto out;
4644
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304645 scsi_dma_unmap(cmd);
4646
4647 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004648 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304649 hba->lrb[tag].cmd = NULL;
4650 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304651
4652 clear_bit_unlock(tag, &hba->lrb_in_use);
4653 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004654
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304655out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304656 if (!err) {
4657 err = SUCCESS;
4658 } else {
4659 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4660 err = FAILED;
4661 }
4662
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004663 /*
4664 * This ufshcd_release() corresponds to the original scsi cmd that got
4665 * aborted here (as we won't get any IRQ for it).
4666 */
4667 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304668 return err;
4669}
4670
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304671/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304672 * ufshcd_host_reset_and_restore - reset and restore host controller
4673 * @hba: per-adapter instance
4674 *
4675 * Note that host controller reset may issue DME_RESET to
4676 * local and remote (device) Uni-Pro stack and the attributes
4677 * are reset to default state.
4678 *
4679 * Returns zero on success, non-zero on failure
4680 */
4681static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4682{
4683 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304684 unsigned long flags;
4685
4686 /* Reset the host controller */
4687 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004688 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304689 spin_unlock_irqrestore(hba->host->host_lock, flags);
4690
4691 err = ufshcd_hba_enable(hba);
4692 if (err)
4693 goto out;
4694
4695 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004696 err = ufshcd_probe_hba(hba);
4697
4698 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304699 err = -EIO;
4700out:
4701 if (err)
4702 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4703
4704 return err;
4705}
4706
4707/**
4708 * ufshcd_reset_and_restore - reset and re-initialize host/device
4709 * @hba: per-adapter instance
4710 *
4711 * Reset and recover device, host and re-establish link. This
4712 * is helpful to recover the communication in fatal error conditions.
4713 *
4714 * Returns zero on success, non-zero on failure
4715 */
4716static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4717{
4718 int err = 0;
4719 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004720 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304721
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004722 do {
4723 err = ufshcd_host_reset_and_restore(hba);
4724 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304725
4726 /*
4727 * After reset the door-bell might be cleared, complete
4728 * outstanding requests in s/w here.
4729 */
4730 spin_lock_irqsave(hba->host->host_lock, flags);
4731 ufshcd_transfer_req_compl(hba);
4732 ufshcd_tmc_handler(hba);
4733 spin_unlock_irqrestore(hba->host->host_lock, flags);
4734
4735 return err;
4736}
4737
4738/**
4739 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4740 * @cmd - SCSI command pointer
4741 *
4742 * Returns SUCCESS/FAILED
4743 */
4744static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4745{
4746 int err;
4747 unsigned long flags;
4748 struct ufs_hba *hba;
4749
4750 hba = shost_priv(cmd->device->host);
4751
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004752 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304753 /*
4754 * Check if there is any race with fatal error handling.
4755 * If so, wait for it to complete. Even though fatal error
4756 * handling does reset and restore in some cases, don't assume
4757 * anything out of it. We are just avoiding race here.
4758 */
4759 do {
4760 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304761 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304762 hba->ufshcd_state == UFSHCD_STATE_RESET))
4763 break;
4764 spin_unlock_irqrestore(hba->host->host_lock, flags);
4765 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304766 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304767 } while (1);
4768
4769 hba->ufshcd_state = UFSHCD_STATE_RESET;
4770 ufshcd_set_eh_in_progress(hba);
4771 spin_unlock_irqrestore(hba->host->host_lock, flags);
4772
4773 err = ufshcd_reset_and_restore(hba);
4774
4775 spin_lock_irqsave(hba->host->host_lock, flags);
4776 if (!err) {
4777 err = SUCCESS;
4778 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4779 } else {
4780 err = FAILED;
4781 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4782 }
4783 ufshcd_clear_eh_in_progress(hba);
4784 spin_unlock_irqrestore(hba->host->host_lock, flags);
4785
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004786 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304787 return err;
4788}
4789
4790/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004791 * ufshcd_get_max_icc_level - calculate the ICC level
4792 * @sup_curr_uA: max. current supported by the regulator
4793 * @start_scan: row at the desc table to start scan from
4794 * @buff: power descriptor buffer
4795 *
4796 * Returns calculated max ICC level for specific regulator
4797 */
4798static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4799{
4800 int i;
4801 int curr_uA;
4802 u16 data;
4803 u16 unit;
4804
4805 for (i = start_scan; i >= 0; i--) {
4806 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4807 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4808 ATTR_ICC_LVL_UNIT_OFFSET;
4809 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4810 switch (unit) {
4811 case UFSHCD_NANO_AMP:
4812 curr_uA = curr_uA / 1000;
4813 break;
4814 case UFSHCD_MILI_AMP:
4815 curr_uA = curr_uA * 1000;
4816 break;
4817 case UFSHCD_AMP:
4818 curr_uA = curr_uA * 1000 * 1000;
4819 break;
4820 case UFSHCD_MICRO_AMP:
4821 default:
4822 break;
4823 }
4824 if (sup_curr_uA >= curr_uA)
4825 break;
4826 }
4827 if (i < 0) {
4828 i = 0;
4829 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4830 }
4831
4832 return (u32)i;
4833}
4834
4835/**
4836 * ufshcd_calc_icc_level - calculate the max ICC level
4837 * In case regulators are not initialized we'll return 0
4838 * @hba: per-adapter instance
4839 * @desc_buf: power descriptor buffer to extract ICC levels from.
4840 * @len: length of desc_buff
4841 *
4842 * Returns calculated ICC level
4843 */
4844static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4845 u8 *desc_buf, int len)
4846{
4847 u32 icc_level = 0;
4848
4849 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4850 !hba->vreg_info.vccq2) {
4851 dev_err(hba->dev,
4852 "%s: Regulator capability was not set, actvIccLevel=%d",
4853 __func__, icc_level);
4854 goto out;
4855 }
4856
4857 if (hba->vreg_info.vcc)
4858 icc_level = ufshcd_get_max_icc_level(
4859 hba->vreg_info.vcc->max_uA,
4860 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4861 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4862
4863 if (hba->vreg_info.vccq)
4864 icc_level = ufshcd_get_max_icc_level(
4865 hba->vreg_info.vccq->max_uA,
4866 icc_level,
4867 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4868
4869 if (hba->vreg_info.vccq2)
4870 icc_level = ufshcd_get_max_icc_level(
4871 hba->vreg_info.vccq2->max_uA,
4872 icc_level,
4873 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4874out:
4875 return icc_level;
4876}
4877
4878static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4879{
4880 int ret;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004881 int buff_len = hba->desc_size.pwr_desc;
4882 u8 desc_buf[hba->desc_size.pwr_desc];
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004883
4884 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4885 if (ret) {
4886 dev_err(hba->dev,
4887 "%s: Failed reading power descriptor.len = %d ret = %d",
4888 __func__, buff_len, ret);
4889 return;
4890 }
4891
4892 hba->init_prefetch_data.icc_level =
4893 ufshcd_find_max_sup_active_icc_level(hba,
4894 desc_buf, buff_len);
4895 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4896 __func__, hba->init_prefetch_data.icc_level);
4897
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004898 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4899 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4900 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004901
4902 if (ret)
4903 dev_err(hba->dev,
4904 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4905 __func__, hba->init_prefetch_data.icc_level , ret);
4906
4907}
4908
4909/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004910 * ufshcd_scsi_add_wlus - Adds required W-LUs
4911 * @hba: per-adapter instance
4912 *
4913 * UFS device specification requires the UFS devices to support 4 well known
4914 * logical units:
4915 * "REPORT_LUNS" (address: 01h)
4916 * "UFS Device" (address: 50h)
4917 * "RPMB" (address: 44h)
4918 * "BOOT" (address: 30h)
4919 * UFS device's power management needs to be controlled by "POWER CONDITION"
4920 * field of SSU (START STOP UNIT) command. But this "power condition" field
4921 * will take effect only when its sent to "UFS device" well known logical unit
4922 * hence we require the scsi_device instance to represent this logical unit in
4923 * order for the UFS host driver to send the SSU command for power management.
4924
4925 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4926 * Block) LU so user space process can control this LU. User space may also
4927 * want to have access to BOOT LU.
4928
4929 * This function adds scsi device instances for each of all well known LUs
4930 * (except "REPORT LUNS" LU).
4931 *
4932 * Returns zero on success (all required W-LUs are added successfully),
4933 * non-zero error value on failure (if failed to add any of the required W-LU).
4934 */
4935static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4936{
4937 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004938 struct scsi_device *sdev_rpmb;
4939 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004940
4941 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4942 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4943 if (IS_ERR(hba->sdev_ufs_device)) {
4944 ret = PTR_ERR(hba->sdev_ufs_device);
4945 hba->sdev_ufs_device = NULL;
4946 goto out;
4947 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004948 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004949
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004950 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004951 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004952 if (IS_ERR(sdev_boot)) {
4953 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004954 goto remove_sdev_ufs_device;
4955 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004956 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004957
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004958 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004959 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004960 if (IS_ERR(sdev_rpmb)) {
4961 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004962 goto remove_sdev_boot;
4963 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004964 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004965 goto out;
4966
4967remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004968 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004969remove_sdev_ufs_device:
4970 scsi_remove_device(hba->sdev_ufs_device);
4971out:
4972 return ret;
4973}
4974
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02004975static int ufs_get_device_desc(struct ufs_hba *hba,
4976 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004977{
4978 int err;
4979 u8 model_index;
Potomski, MichalXe1928452017-02-23 09:05:30 +00004980 u8 str_desc_buf[QUERY_DESC_MAX_SIZE + 1] = {0};
4981 u8 desc_buf[hba->desc_size.dev_desc];
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004982
Potomski, MichalXe1928452017-02-23 09:05:30 +00004983 err = ufshcd_read_device_desc(hba, desc_buf, hba->desc_size.dev_desc);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004984 if (err) {
4985 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4986 __func__, err);
4987 goto out;
4988 }
4989
4990 /*
4991 * getting vendor (manufacturerID) and Bank Index in big endian
4992 * format
4993 */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02004994 dev_desc->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004995 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4996
4997 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4998
4999 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
Potomski, MichalXe1928452017-02-23 09:05:30 +00005000 QUERY_DESC_MAX_SIZE, ASCII_STD);
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005001 if (err) {
5002 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
5003 __func__, err);
5004 goto out;
5005 }
5006
Potomski, MichalXe1928452017-02-23 09:05:30 +00005007 str_desc_buf[QUERY_DESC_MAX_SIZE] = '\0';
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005008 strlcpy(dev_desc->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005009 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
5010 MAX_MODEL_LEN));
5011
5012 /* Null terminate the model string */
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005013 dev_desc->model[MAX_MODEL_LEN] = '\0';
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005014
5015out:
5016 return err;
5017}
5018
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005019static void ufs_fixup_device_setup(struct ufs_hba *hba,
5020 struct ufs_dev_desc *dev_desc)
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005021{
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005022 struct ufs_dev_fix *f;
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005023
5024 for (f = ufs_fixups; f->quirk; f++) {
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005025 if ((f->card.wmanufacturerid == dev_desc->wmanufacturerid ||
5026 f->card.wmanufacturerid == UFS_ANY_VENDOR) &&
5027 (STR_PRFX_EQUAL(f->card.model, dev_desc->model) ||
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005028 !strcmp(f->card.model, UFS_ANY_MODEL)))
5029 hba->dev_quirks |= f->quirk;
5030 }
5031}
5032
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005033/**
Yaniv Gardi37113102016-03-10 17:37:16 +02005034 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
5035 * @hba: per-adapter instance
5036 *
5037 * PA_TActivate parameter can be tuned manually if UniPro version is less than
5038 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
5039 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
5040 * the hibern8 exit latency.
5041 *
5042 * Returns zero on success, non-zero error value on failure.
5043 */
5044static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
5045{
5046 int ret = 0;
5047 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
5048
5049 ret = ufshcd_dme_peer_get(hba,
5050 UIC_ARG_MIB_SEL(
5051 RX_MIN_ACTIVATETIME_CAPABILITY,
5052 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5053 &peer_rx_min_activatetime);
5054 if (ret)
5055 goto out;
5056
5057 /* make sure proper unit conversion is applied */
5058 tuned_pa_tactivate =
5059 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
5060 / PA_TACTIVATE_TIME_UNIT_US);
5061 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5062 tuned_pa_tactivate);
5063
5064out:
5065 return ret;
5066}
5067
5068/**
5069 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
5070 * @hba: per-adapter instance
5071 *
5072 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
5073 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
5074 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
5075 * This optimal value can help reduce the hibern8 exit latency.
5076 *
5077 * Returns zero on success, non-zero error value on failure.
5078 */
5079static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
5080{
5081 int ret = 0;
5082 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
5083 u32 max_hibern8_time, tuned_pa_hibern8time;
5084
5085 ret = ufshcd_dme_get(hba,
5086 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
5087 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
5088 &local_tx_hibern8_time_cap);
5089 if (ret)
5090 goto out;
5091
5092 ret = ufshcd_dme_peer_get(hba,
5093 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
5094 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
5095 &peer_rx_hibern8_time_cap);
5096 if (ret)
5097 goto out;
5098
5099 max_hibern8_time = max(local_tx_hibern8_time_cap,
5100 peer_rx_hibern8_time_cap);
5101 /* make sure proper unit conversion is applied */
5102 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
5103 / PA_HIBERN8_TIME_UNIT_US);
5104 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
5105 tuned_pa_hibern8time);
5106out:
5107 return ret;
5108}
5109
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005110/**
5111 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
5112 * less than device PA_TACTIVATE time.
5113 * @hba: per-adapter instance
5114 *
5115 * Some UFS devices require host PA_TACTIVATE to be lower than device
5116 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
5117 * for such devices.
5118 *
5119 * Returns zero on success, non-zero error value on failure.
5120 */
5121static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba)
5122{
5123 int ret = 0;
5124 u32 granularity, peer_granularity;
5125 u32 pa_tactivate, peer_pa_tactivate;
5126 u32 pa_tactivate_us, peer_pa_tactivate_us;
5127 u8 gran_to_us_table[] = {1, 4, 8, 16, 32, 100};
5128
5129 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5130 &granularity);
5131 if (ret)
5132 goto out;
5133
5134 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY),
5135 &peer_granularity);
5136 if (ret)
5137 goto out;
5138
5139 if ((granularity < PA_GRANULARITY_MIN_VAL) ||
5140 (granularity > PA_GRANULARITY_MAX_VAL)) {
5141 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d",
5142 __func__, granularity);
5143 return -EINVAL;
5144 }
5145
5146 if ((peer_granularity < PA_GRANULARITY_MIN_VAL) ||
5147 (peer_granularity > PA_GRANULARITY_MAX_VAL)) {
5148 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d",
5149 __func__, peer_granularity);
5150 return -EINVAL;
5151 }
5152
5153 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate);
5154 if (ret)
5155 goto out;
5156
5157 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE),
5158 &peer_pa_tactivate);
5159 if (ret)
5160 goto out;
5161
5162 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1];
5163 peer_pa_tactivate_us = peer_pa_tactivate *
5164 gran_to_us_table[peer_granularity - 1];
5165
5166 if (pa_tactivate_us > peer_pa_tactivate_us) {
5167 u32 new_peer_pa_tactivate;
5168
5169 new_peer_pa_tactivate = pa_tactivate_us /
5170 gran_to_us_table[peer_granularity - 1];
5171 new_peer_pa_tactivate++;
5172 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
5173 new_peer_pa_tactivate);
5174 }
5175
5176out:
5177 return ret;
5178}
5179
Yaniv Gardi37113102016-03-10 17:37:16 +02005180static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
5181{
5182 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
5183 ufshcd_tune_pa_tactivate(hba);
5184 ufshcd_tune_pa_hibern8time(hba);
5185 }
5186
5187 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5188 /* set 1ms timeout for PA_TACTIVATE */
5189 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
Subhash Jadavani3a87bcd2017-04-04 19:32:07 +00005190
5191 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE)
5192 ufshcd_quirk_tune_host_pa_tactivate(hba);
Subhash Jadavanibedc6292017-04-04 19:32:13 +00005193
5194 ufshcd_vops_apply_dev_quirks(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005195}
5196
Potomski, MichalXe1928452017-02-23 09:05:30 +00005197static void ufshcd_init_desc_sizes(struct ufs_hba *hba)
5198{
5199 int err;
5200
5201 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_DEVICE, 0,
5202 &hba->desc_size.dev_desc);
5203 if (err)
5204 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5205
5206 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_POWER, 0,
5207 &hba->desc_size.pwr_desc);
5208 if (err)
5209 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5210
5211 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_INTERCONNECT, 0,
5212 &hba->desc_size.interc_desc);
5213 if (err)
5214 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5215
5216 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_CONFIGURATION, 0,
5217 &hba->desc_size.conf_desc);
5218 if (err)
5219 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5220
5221 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_UNIT, 0,
5222 &hba->desc_size.unit_desc);
5223 if (err)
5224 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5225
5226 err = ufshcd_read_desc_length(hba, QUERY_DESC_IDN_GEOMETRY, 0,
5227 &hba->desc_size.geom_desc);
5228 if (err)
5229 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5230}
5231
5232static void ufshcd_def_desc_sizes(struct ufs_hba *hba)
5233{
5234 hba->desc_size.dev_desc = QUERY_DESC_DEVICE_DEF_SIZE;
5235 hba->desc_size.pwr_desc = QUERY_DESC_POWER_DEF_SIZE;
5236 hba->desc_size.interc_desc = QUERY_DESC_INTERCONNECT_DEF_SIZE;
5237 hba->desc_size.conf_desc = QUERY_DESC_CONFIGURATION_DEF_SIZE;
5238 hba->desc_size.unit_desc = QUERY_DESC_UNIT_DEF_SIZE;
5239 hba->desc_size.geom_desc = QUERY_DESC_GEOMETRY_DEF_SIZE;
5240}
5241
Yaniv Gardi37113102016-03-10 17:37:16 +02005242/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005243 * ufshcd_probe_hba - probe hba to detect device and initialize
5244 * @hba: per-adapter instance
5245 *
5246 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305247 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005248static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305249{
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005250 struct ufs_dev_desc card = {0};
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305251 int ret;
5252
5253 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305254 if (ret)
5255 goto out;
5256
Yaniv Gardi50646362014-10-23 13:25:13 +03005257 ufshcd_init_pwr_info(hba);
5258
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005259 /* set the default level for urgent bkops */
5260 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5261 hba->is_urgent_bkops_lvl_checked = false;
5262
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005263 /* UniPro link is active now */
5264 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305265
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305266 ret = ufshcd_verify_dev_init(hba);
5267 if (ret)
5268 goto out;
5269
Dolev Raviv68078d52013-07-30 00:35:58 +05305270 ret = ufshcd_complete_dev_init(hba);
5271 if (ret)
5272 goto out;
5273
Potomski, MichalXe1928452017-02-23 09:05:30 +00005274 /* Init check for device descriptor sizes */
5275 ufshcd_init_desc_sizes(hba);
5276
Tomas Winklerbe4d66d2017-01-05 10:45:12 +02005277 ret = ufs_get_device_desc(hba, &card);
5278 if (ret) {
5279 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
5280 __func__, ret);
5281 goto out;
5282 }
5283
5284 ufs_fixup_device_setup(hba, &card);
Yaniv Gardi37113102016-03-10 17:37:16 +02005285 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005286
5287 ret = ufshcd_set_vccq_rail_unused(hba,
5288 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5289 if (ret)
5290 goto out;
5291
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005292 /* UFS device is also active now */
5293 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305294 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005295 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305296
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005297 if (ufshcd_get_max_pwr_mode(hba)) {
5298 dev_err(hba->dev,
5299 "%s: Failed getting max supported power mode\n",
5300 __func__);
5301 } else {
5302 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
5303 if (ret)
5304 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5305 __func__, ret);
5306 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005307
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005308 /* set the state as operational after switching to desired gear */
5309 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Potomski, MichalXe1928452017-02-23 09:05:30 +00005310
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005311 /*
5312 * If we are in error handling context or in power management callbacks
5313 * context, no need to scan the host
5314 */
5315 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5316 bool flag;
5317
5318 /* clear any previous UFS device information */
5319 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005320 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5321 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005322 hba->dev_info.f_power_on_wp_en = flag;
5323
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005324 if (!hba->is_init_prefetch)
5325 ufshcd_init_icc_levels(hba);
5326
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005327 /* Add required well known logical units to scsi mid layer */
5328 if (ufshcd_scsi_add_wlus(hba))
5329 goto out;
5330
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305331 scsi_scan_host(hba->host);
5332 pm_runtime_put_sync(hba->dev);
5333 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005334
5335 if (!hba->is_init_prefetch)
5336 hba->is_init_prefetch = true;
5337
Sahitya Tummala856b3482014-09-25 15:32:34 +03005338 /* Resume devfreq after UFS device is detected */
5339 if (ufshcd_is_clkscaling_enabled(hba))
5340 devfreq_resume_device(hba->devfreq);
5341
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305342out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005343 /*
5344 * If we failed to initialize the device or the device is not
5345 * present, turn off the power/clocks etc.
5346 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005347 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5348 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005349 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005350 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005351
5352 return ret;
5353}
5354
5355/**
5356 * ufshcd_async_scan - asynchronous execution for probing hba
5357 * @data: data pointer to pass to this function
5358 * @cookie: cookie data
5359 */
5360static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5361{
5362 struct ufs_hba *hba = (struct ufs_hba *)data;
5363
5364 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305365}
5366
Yaniv Gardif550c652016-03-10 17:37:07 +02005367static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5368{
5369 unsigned long flags;
5370 struct Scsi_Host *host;
5371 struct ufs_hba *hba;
5372 int index;
5373 bool found = false;
5374
5375 if (!scmd || !scmd->device || !scmd->device->host)
5376 return BLK_EH_NOT_HANDLED;
5377
5378 host = scmd->device->host;
5379 hba = shost_priv(host);
5380 if (!hba)
5381 return BLK_EH_NOT_HANDLED;
5382
5383 spin_lock_irqsave(host->host_lock, flags);
5384
5385 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5386 if (hba->lrb[index].cmd == scmd) {
5387 found = true;
5388 break;
5389 }
5390 }
5391
5392 spin_unlock_irqrestore(host->host_lock, flags);
5393
5394 /*
5395 * Bypass SCSI error handling and reset the block layer timer if this
5396 * SCSI command was not actually dispatched to UFS driver, otherwise
5397 * let SCSI layer handle the error as usual.
5398 */
5399 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5400}
5401
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305402static struct scsi_host_template ufshcd_driver_template = {
5403 .module = THIS_MODULE,
5404 .name = UFSHCD,
5405 .proc_name = UFSHCD,
5406 .queuecommand = ufshcd_queuecommand,
5407 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005408 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305409 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005410 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305411 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305412 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5413 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005414 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305415 .this_id = -1,
5416 .sg_tablesize = SG_ALL,
5417 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5418 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005419 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005420 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305421};
5422
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005423static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5424 int ua)
5425{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005426 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005427
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005428 if (!vreg)
5429 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005430
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005431 ret = regulator_set_load(vreg->reg, ua);
5432 if (ret < 0) {
5433 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5434 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005435 }
5436
5437 return ret;
5438}
5439
5440static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5441 struct ufs_vreg *vreg)
5442{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005443 if (!vreg)
5444 return 0;
5445 else if (vreg->unused)
5446 return 0;
5447 else
5448 return ufshcd_config_vreg_load(hba->dev, vreg,
5449 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005450}
5451
5452static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5453 struct ufs_vreg *vreg)
5454{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005455 if (!vreg)
5456 return 0;
5457 else if (vreg->unused)
5458 return 0;
5459 else
5460 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005461}
5462
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005463static int ufshcd_config_vreg(struct device *dev,
5464 struct ufs_vreg *vreg, bool on)
5465{
5466 int ret = 0;
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005467 struct regulator *reg;
5468 const char *name;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005469 int min_uV, uA_load;
5470
5471 BUG_ON(!vreg);
5472
Gustavo A. R. Silvaa248dc62017-11-20 08:12:29 -06005473 reg = vreg->reg;
5474 name = vreg->name;
5475
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005476 if (regulator_count_voltages(reg) > 0) {
5477 min_uV = on ? vreg->min_uV : 0;
5478 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5479 if (ret) {
5480 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5481 __func__, name, ret);
5482 goto out;
5483 }
5484
5485 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005486 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5487 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005488 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005489 }
5490out:
5491 return ret;
5492}
5493
5494static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5495{
5496 int ret = 0;
5497
Yaniv Gardi60f01872016-03-10 17:37:11 +02005498 if (!vreg)
5499 goto out;
5500 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005501 goto out;
5502
5503 ret = ufshcd_config_vreg(dev, vreg, true);
5504 if (!ret)
5505 ret = regulator_enable(vreg->reg);
5506
5507 if (!ret)
5508 vreg->enabled = true;
5509 else
5510 dev_err(dev, "%s: %s enable failed, err=%d\n",
5511 __func__, vreg->name, ret);
5512out:
5513 return ret;
5514}
5515
5516static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5517{
5518 int ret = 0;
5519
Yaniv Gardi60f01872016-03-10 17:37:11 +02005520 if (!vreg)
5521 goto out;
5522 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005523 goto out;
5524
5525 ret = regulator_disable(vreg->reg);
5526
5527 if (!ret) {
5528 /* ignore errors on applying disable config */
5529 ufshcd_config_vreg(dev, vreg, false);
5530 vreg->enabled = false;
5531 } else {
5532 dev_err(dev, "%s: %s disable failed, err=%d\n",
5533 __func__, vreg->name, ret);
5534 }
5535out:
5536 return ret;
5537}
5538
5539static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5540{
5541 int ret = 0;
5542 struct device *dev = hba->dev;
5543 struct ufs_vreg_info *info = &hba->vreg_info;
5544
5545 if (!info)
5546 goto out;
5547
5548 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5549 if (ret)
5550 goto out;
5551
5552 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5553 if (ret)
5554 goto out;
5555
5556 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5557 if (ret)
5558 goto out;
5559
5560out:
5561 if (ret) {
5562 ufshcd_toggle_vreg(dev, info->vccq2, false);
5563 ufshcd_toggle_vreg(dev, info->vccq, false);
5564 ufshcd_toggle_vreg(dev, info->vcc, false);
5565 }
5566 return ret;
5567}
5568
Raviv Shvili6a771a62014-09-25 15:32:24 +03005569static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5570{
5571 struct ufs_vreg_info *info = &hba->vreg_info;
5572
5573 if (info)
5574 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5575
5576 return 0;
5577}
5578
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005579static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5580{
5581 int ret = 0;
5582
5583 if (!vreg)
5584 goto out;
5585
5586 vreg->reg = devm_regulator_get(dev, vreg->name);
5587 if (IS_ERR(vreg->reg)) {
5588 ret = PTR_ERR(vreg->reg);
5589 dev_err(dev, "%s: %s get failed, err=%d\n",
5590 __func__, vreg->name, ret);
5591 }
5592out:
5593 return ret;
5594}
5595
5596static int ufshcd_init_vreg(struct ufs_hba *hba)
5597{
5598 int ret = 0;
5599 struct device *dev = hba->dev;
5600 struct ufs_vreg_info *info = &hba->vreg_info;
5601
5602 if (!info)
5603 goto out;
5604
5605 ret = ufshcd_get_vreg(dev, info->vcc);
5606 if (ret)
5607 goto out;
5608
5609 ret = ufshcd_get_vreg(dev, info->vccq);
5610 if (ret)
5611 goto out;
5612
5613 ret = ufshcd_get_vreg(dev, info->vccq2);
5614out:
5615 return ret;
5616}
5617
Raviv Shvili6a771a62014-09-25 15:32:24 +03005618static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5619{
5620 struct ufs_vreg_info *info = &hba->vreg_info;
5621
5622 if (info)
5623 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5624
5625 return 0;
5626}
5627
Yaniv Gardi60f01872016-03-10 17:37:11 +02005628static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5629{
5630 int ret = 0;
5631 struct ufs_vreg_info *info = &hba->vreg_info;
5632
5633 if (!info)
5634 goto out;
5635 else if (!info->vccq)
5636 goto out;
5637
5638 if (unused) {
5639 /* shut off the rail here */
5640 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5641 /*
5642 * Mark this rail as no longer used, so it doesn't get enabled
5643 * later by mistake
5644 */
5645 if (!ret)
5646 info->vccq->unused = true;
5647 } else {
5648 /*
5649 * rail should have been already enabled hence just make sure
5650 * that unused flag is cleared.
5651 */
5652 info->vccq->unused = false;
5653 }
5654out:
5655 return ret;
5656}
5657
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005658static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5659 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005660{
5661 int ret = 0;
5662 struct ufs_clk_info *clki;
5663 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005664 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005665
5666 if (!head || list_empty(head))
5667 goto out;
5668
5669 list_for_each_entry(clki, head, list) {
5670 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005671 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5672 continue;
5673
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005674 if (on && !clki->enabled) {
5675 ret = clk_prepare_enable(clki->clk);
5676 if (ret) {
5677 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5678 __func__, clki->name, ret);
5679 goto out;
5680 }
5681 } else if (!on && clki->enabled) {
5682 clk_disable_unprepare(clki->clk);
5683 }
5684 clki->enabled = on;
5685 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5686 clki->name, on ? "en" : "dis");
5687 }
5688 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005689
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005690 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005691out:
5692 if (ret) {
5693 list_for_each_entry(clki, head, list) {
5694 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5695 clk_disable_unprepare(clki->clk);
5696 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005697 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005698 spin_lock_irqsave(hba->host->host_lock, flags);
5699 hba->clk_gating.state = CLKS_ON;
5700 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005701 }
5702 return ret;
5703}
5704
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005705static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5706{
5707 return __ufshcd_setup_clocks(hba, on, false);
5708}
5709
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005710static int ufshcd_init_clocks(struct ufs_hba *hba)
5711{
5712 int ret = 0;
5713 struct ufs_clk_info *clki;
5714 struct device *dev = hba->dev;
5715 struct list_head *head = &hba->clk_list_head;
5716
5717 if (!head || list_empty(head))
5718 goto out;
5719
5720 list_for_each_entry(clki, head, list) {
5721 if (!clki->name)
5722 continue;
5723
5724 clki->clk = devm_clk_get(dev, clki->name);
5725 if (IS_ERR(clki->clk)) {
5726 ret = PTR_ERR(clki->clk);
5727 dev_err(dev, "%s: %s clk get failed, %d\n",
5728 __func__, clki->name, ret);
5729 goto out;
5730 }
5731
5732 if (clki->max_freq) {
5733 ret = clk_set_rate(clki->clk, clki->max_freq);
5734 if (ret) {
5735 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5736 __func__, clki->name,
5737 clki->max_freq, ret);
5738 goto out;
5739 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005740 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005741 }
5742 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5743 clki->name, clk_get_rate(clki->clk));
5744 }
5745out:
5746 return ret;
5747}
5748
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005749static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5750{
5751 int err = 0;
5752
5753 if (!hba->vops)
5754 goto out;
5755
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005756 err = ufshcd_vops_init(hba);
5757 if (err)
5758 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005759
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005760 err = ufshcd_vops_setup_regulators(hba, true);
5761 if (err)
5762 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005763
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005764 goto out;
5765
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005766out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005767 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005768out:
5769 if (err)
5770 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005771 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005772 return err;
5773}
5774
5775static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5776{
5777 if (!hba->vops)
5778 return;
5779
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005780 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005781
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005782 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005783
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005784 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005785}
5786
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005787static int ufshcd_hba_init(struct ufs_hba *hba)
5788{
5789 int err;
5790
Raviv Shvili6a771a62014-09-25 15:32:24 +03005791 /*
5792 * Handle host controller power separately from the UFS device power
5793 * rails as it will help controlling the UFS host controller power
5794 * collapse easily which is different than UFS device power collapse.
5795 * Also, enable the host controller power before we go ahead with rest
5796 * of the initialization here.
5797 */
5798 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005799 if (err)
5800 goto out;
5801
Raviv Shvili6a771a62014-09-25 15:32:24 +03005802 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005803 if (err)
5804 goto out;
5805
Raviv Shvili6a771a62014-09-25 15:32:24 +03005806 err = ufshcd_init_clocks(hba);
5807 if (err)
5808 goto out_disable_hba_vreg;
5809
5810 err = ufshcd_setup_clocks(hba, true);
5811 if (err)
5812 goto out_disable_hba_vreg;
5813
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005814 err = ufshcd_init_vreg(hba);
5815 if (err)
5816 goto out_disable_clks;
5817
5818 err = ufshcd_setup_vreg(hba, true);
5819 if (err)
5820 goto out_disable_clks;
5821
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005822 err = ufshcd_variant_hba_init(hba);
5823 if (err)
5824 goto out_disable_vreg;
5825
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005826 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005827 goto out;
5828
5829out_disable_vreg:
5830 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005831out_disable_clks:
5832 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005833out_disable_hba_vreg:
5834 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005835out:
5836 return err;
5837}
5838
5839static void ufshcd_hba_exit(struct ufs_hba *hba)
5840{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005841 if (hba->is_powered) {
5842 ufshcd_variant_hba_exit(hba);
5843 ufshcd_setup_vreg(hba, false);
5844 ufshcd_setup_clocks(hba, false);
5845 ufshcd_setup_hba_vreg(hba, false);
5846 hba->is_powered = false;
5847 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005848}
5849
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005850static int
5851ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305852{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005853 unsigned char cmd[6] = {REQUEST_SENSE,
5854 0,
5855 0,
5856 0,
5857 SCSI_SENSE_BUFFERSIZE,
5858 0};
5859 char *buffer;
5860 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305861
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005862 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5863 if (!buffer) {
5864 ret = -ENOMEM;
5865 goto out;
5866 }
5867
5868 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
5869 SCSI_SENSE_BUFFERSIZE, NULL,
5870 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5871 if (ret)
5872 pr_err("%s: failed with err %d\n", __func__, ret);
5873
5874 kfree(buffer);
5875out:
5876 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305877}
5878
5879/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005880 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5881 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305882 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005883 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305884 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005885 * Returns 0 if requested power mode is set successfully
5886 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305887 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005888static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5889 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305890{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005891 unsigned char cmd[6] = { START_STOP };
5892 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005893 struct scsi_device *sdp;
5894 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005895 int ret;
5896
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005897 spin_lock_irqsave(hba->host->host_lock, flags);
5898 sdp = hba->sdev_ufs_device;
5899 if (sdp) {
5900 ret = scsi_device_get(sdp);
5901 if (!ret && !scsi_device_online(sdp)) {
5902 ret = -ENODEV;
5903 scsi_device_put(sdp);
5904 }
5905 } else {
5906 ret = -ENODEV;
5907 }
5908 spin_unlock_irqrestore(hba->host->host_lock, flags);
5909
5910 if (ret)
5911 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005912
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305913 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005914 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5915 * handling, which would wait for host to be resumed. Since we know
5916 * we are functional while we are here, skip host resume in error
5917 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305918 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005919 hba->host->eh_noresume = 1;
5920 if (hba->wlun_dev_clr_ua) {
5921 ret = ufshcd_send_request_sense(hba, sdp);
5922 if (ret)
5923 goto out;
5924 /* Unit attention condition is cleared now */
5925 hba->wlun_dev_clr_ua = false;
5926 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305927
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005928 cmd[4] = pwr_mode << 4;
5929
5930 /*
5931 * Current function would be generally called from the power management
5932 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5933 * already suspended childs.
5934 */
5935 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5936 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5937 if (ret) {
5938 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005939 "START_STOP failed for power mode: %d, result %x\n",
5940 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005941 if (driver_byte(ret) & DRIVER_SENSE)
5942 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005943 }
5944
5945 if (!ret)
5946 hba->curr_dev_pwr_mode = pwr_mode;
5947out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005948 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005949 hba->host->eh_noresume = 0;
5950 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305951}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305952
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005953static int ufshcd_link_state_transition(struct ufs_hba *hba,
5954 enum uic_link_state req_link_state,
5955 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305956{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005957 int ret = 0;
5958
5959 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305960 return 0;
5961
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005962 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5963 ret = ufshcd_uic_hibern8_enter(hba);
5964 if (!ret)
5965 ufshcd_set_link_hibern8(hba);
5966 else
5967 goto out;
5968 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305969 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005970 * If autobkops is enabled, link can't be turned off because
5971 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305972 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005973 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5974 (!check_for_bkops || (check_for_bkops &&
5975 !hba->auto_bkops_enabled))) {
5976 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005977 * Let's make sure that link is in low power mode, we are doing
5978 * this currently by putting the link in Hibern8. Otherway to
5979 * put the link in low power mode is to send the DME end point
5980 * to device and then send the DME reset command to local
5981 * unipro. But putting the link in hibern8 is much faster.
5982 */
5983 ret = ufshcd_uic_hibern8_enter(hba);
5984 if (ret)
5985 goto out;
5986 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005987 * Change controller state to "reset state" which
5988 * should also put the link in off/reset state
5989 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005990 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005991 /*
5992 * TODO: Check if we need any delay to make sure that
5993 * controller is reset
5994 */
5995 ufshcd_set_link_off(hba);
5996 }
5997
5998out:
5999 return ret;
6000}
6001
6002static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
6003{
6004 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02006005 * It seems some UFS devices may keep drawing more than sleep current
6006 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
6007 * To avoid this situation, add 2ms delay before putting these UFS
6008 * rails in LPM mode.
6009 */
6010 if (!ufshcd_is_link_active(hba) &&
6011 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
6012 usleep_range(2000, 2100);
6013
6014 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006015 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
6016 * power.
6017 *
6018 * If UFS device and link is in OFF state, all power supplies (VCC,
6019 * VCCQ, VCCQ2) can be turned off if power on write protect is not
6020 * required. If UFS link is inactive (Hibern8 or OFF state) and device
6021 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
6022 *
6023 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
6024 * in low power state which would save some power.
6025 */
6026 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6027 !hba->dev_info.is_lu_power_on_wp) {
6028 ufshcd_setup_vreg(hba, false);
6029 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6030 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6031 if (!ufshcd_is_link_active(hba)) {
6032 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6033 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
6034 }
6035 }
6036}
6037
6038static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
6039{
6040 int ret = 0;
6041
6042 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
6043 !hba->dev_info.is_lu_power_on_wp) {
6044 ret = ufshcd_setup_vreg(hba, true);
6045 } else if (!ufshcd_is_ufs_dev_active(hba)) {
6046 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
6047 if (!ret && !ufshcd_is_link_active(hba)) {
6048 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
6049 if (ret)
6050 goto vcc_disable;
6051 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
6052 if (ret)
6053 goto vccq_lpm;
6054 }
6055 }
6056 goto out;
6057
6058vccq_lpm:
6059 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
6060vcc_disable:
6061 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
6062out:
6063 return ret;
6064}
6065
6066static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
6067{
6068 if (ufshcd_is_link_off(hba))
6069 ufshcd_setup_hba_vreg(hba, false);
6070}
6071
6072static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
6073{
6074 if (ufshcd_is_link_off(hba))
6075 ufshcd_setup_hba_vreg(hba, true);
6076}
6077
6078/**
6079 * ufshcd_suspend - helper function for suspend operations
6080 * @hba: per adapter instance
6081 * @pm_op: desired low power operation type
6082 *
6083 * This function will try to put the UFS device and link into low power
6084 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
6085 * (System PM level).
6086 *
6087 * If this function is called during shutdown, it will make sure that
6088 * both UFS device and UFS link is powered off.
6089 *
6090 * NOTE: UFS device & link must be active before we enter in this function.
6091 *
6092 * Returns 0 for success and non-zero for failure
6093 */
6094static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6095{
6096 int ret = 0;
6097 enum ufs_pm_level pm_lvl;
6098 enum ufs_dev_pwr_mode req_dev_pwr_mode;
6099 enum uic_link_state req_link_state;
6100
6101 hba->pm_op_in_progress = 1;
6102 if (!ufshcd_is_shutdown_pm(pm_op)) {
6103 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
6104 hba->rpm_lvl : hba->spm_lvl;
6105 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
6106 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
6107 } else {
6108 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
6109 req_link_state = UIC_LINK_OFF_STATE;
6110 }
6111
6112 /*
6113 * If we can't transition into any of the low power modes
6114 * just gate the clocks.
6115 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006116 ufshcd_hold(hba, false);
6117 hba->clk_gating.is_suspended = true;
6118
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006119 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
6120 req_link_state == UIC_LINK_ACTIVE_STATE) {
6121 goto disable_clks;
6122 }
6123
6124 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
6125 (req_link_state == hba->uic_link_state))
6126 goto out;
6127
6128 /* UFS device & link must be active before we enter in this function */
6129 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
6130 ret = -EINVAL;
6131 goto out;
6132 }
6133
6134 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03006135 if (ufshcd_can_autobkops_during_suspend(hba)) {
6136 /*
6137 * The device is idle with no requests in the queue,
6138 * allow background operations if bkops status shows
6139 * that performance might be impacted.
6140 */
6141 ret = ufshcd_urgent_bkops(hba);
6142 if (ret)
6143 goto enable_gating;
6144 } else {
6145 /* make sure that auto bkops is disabled */
6146 ufshcd_disable_auto_bkops(hba);
6147 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006148 }
6149
6150 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
6151 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
6152 !ufshcd_is_runtime_pm(pm_op))) {
6153 /* ensure that bkops is disabled */
6154 ufshcd_disable_auto_bkops(hba);
6155 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
6156 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006157 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006158 }
6159
6160 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
6161 if (ret)
6162 goto set_dev_active;
6163
6164 ufshcd_vreg_set_lpm(hba);
6165
6166disable_clks:
6167 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03006168 * The clock scaling needs access to controller registers. Hence, Wait
6169 * for pending clock scaling work to be done before clocks are
6170 * turned off.
6171 */
6172 if (ufshcd_is_clkscaling_enabled(hba)) {
6173 devfreq_suspend_device(hba->devfreq);
6174 hba->clk_scaling.window_start_t = 0;
6175 }
6176 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006177 * Call vendor specific suspend callback. As these callbacks may access
6178 * vendor specific host controller register space call them before the
6179 * host clocks are ON.
6180 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006181 ret = ufshcd_vops_suspend(hba, pm_op);
6182 if (ret)
6183 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006184
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006185 ret = ufshcd_vops_setup_clocks(hba, false);
6186 if (ret)
6187 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006188
6189 if (!ufshcd_is_link_active(hba))
6190 ufshcd_setup_clocks(hba, false);
6191 else
6192 /* If link is active, device ref_clk can't be switched off */
6193 __ufshcd_setup_clocks(hba, false, true);
6194
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006195 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006196 /*
6197 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006198 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006199 */
6200 ufshcd_disable_irq(hba);
6201 /* Put the host controller in low power mode if possible */
6202 ufshcd_hba_vreg_set_lpm(hba);
6203 goto out;
6204
6205vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006206 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006207set_link_active:
6208 ufshcd_vreg_set_hpm(hba);
6209 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
6210 ufshcd_set_link_active(hba);
6211 else if (ufshcd_is_link_off(hba))
6212 ufshcd_host_reset_and_restore(hba);
6213set_dev_active:
6214 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
6215 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006216enable_gating:
6217 hba->clk_gating.is_suspended = false;
6218 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006219out:
6220 hba->pm_op_in_progress = 0;
6221 return ret;
6222}
6223
6224/**
6225 * ufshcd_resume - helper function for resume operations
6226 * @hba: per adapter instance
6227 * @pm_op: runtime PM or system PM
6228 *
6229 * This function basically brings the UFS device, UniPro link and controller
6230 * to active state.
6231 *
6232 * Returns 0 for success and non-zero for failure
6233 */
6234static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
6235{
6236 int ret;
6237 enum uic_link_state old_link_state;
6238
6239 hba->pm_op_in_progress = 1;
6240 old_link_state = hba->uic_link_state;
6241
6242 ufshcd_hba_vreg_set_hpm(hba);
6243 /* Make sure clocks are enabled before accessing controller */
6244 ret = ufshcd_setup_clocks(hba, true);
6245 if (ret)
6246 goto out;
6247
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006248 /* enable the host irq as host controller would be active soon */
6249 ret = ufshcd_enable_irq(hba);
6250 if (ret)
6251 goto disable_irq_and_vops_clks;
6252
6253 ret = ufshcd_vreg_set_hpm(hba);
6254 if (ret)
6255 goto disable_irq_and_vops_clks;
6256
6257 /*
6258 * Call vendor specific resume callback. As these callbacks may access
6259 * vendor specific host controller register space call them when the
6260 * host clocks are ON.
6261 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006262 ret = ufshcd_vops_resume(hba, pm_op);
6263 if (ret)
6264 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006265
6266 if (ufshcd_is_link_hibern8(hba)) {
6267 ret = ufshcd_uic_hibern8_exit(hba);
6268 if (!ret)
6269 ufshcd_set_link_active(hba);
6270 else
6271 goto vendor_suspend;
6272 } else if (ufshcd_is_link_off(hba)) {
6273 ret = ufshcd_host_reset_and_restore(hba);
6274 /*
6275 * ufshcd_host_reset_and_restore() should have already
6276 * set the link state as active
6277 */
6278 if (ret || !ufshcd_is_link_active(hba))
6279 goto vendor_suspend;
6280 }
6281
6282 if (!ufshcd_is_ufs_dev_active(hba)) {
6283 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6284 if (ret)
6285 goto set_old_link_state;
6286 }
6287
subhashj@codeaurora.orgcfb09f02016-12-22 18:41:22 -08006288 if (ufshcd_keep_autobkops_enabled_except_suspend(hba))
6289 ufshcd_enable_auto_bkops(hba);
6290 else
6291 /*
6292 * If BKOPs operations are urgently needed at this moment then
6293 * keep auto-bkops enabled or else disable it.
6294 */
6295 ufshcd_urgent_bkops(hba);
6296
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006297 hba->clk_gating.is_suspended = false;
6298
Sahitya Tummala856b3482014-09-25 15:32:34 +03006299 if (ufshcd_is_clkscaling_enabled(hba))
6300 devfreq_resume_device(hba->devfreq);
6301
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006302 /* Schedule clock gating in case of no access to UFS device yet */
6303 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006304 goto out;
6305
6306set_old_link_state:
6307 ufshcd_link_state_transition(hba, old_link_state, 0);
6308vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006309 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006310disable_vreg:
6311 ufshcd_vreg_set_lpm(hba);
6312disable_irq_and_vops_clks:
6313 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006314 ufshcd_setup_clocks(hba, false);
6315out:
6316 hba->pm_op_in_progress = 0;
6317 return ret;
6318}
6319
6320/**
6321 * ufshcd_system_suspend - system suspend routine
6322 * @hba: per adapter instance
6323 * @pm_op: runtime PM or system PM
6324 *
6325 * Check the description of ufshcd_suspend() function for more details.
6326 *
6327 * Returns 0 for success and non-zero for failure
6328 */
6329int ufshcd_system_suspend(struct ufs_hba *hba)
6330{
6331 int ret = 0;
6332
6333 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006334 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006335
6336 if (pm_runtime_suspended(hba->dev)) {
6337 if (hba->rpm_lvl == hba->spm_lvl)
6338 /*
6339 * There is possibility that device may still be in
6340 * active state during the runtime suspend.
6341 */
6342 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6343 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6344 goto out;
6345
6346 /*
6347 * UFS device and/or UFS link low power states during runtime
6348 * suspend seems to be different than what is expected during
6349 * system suspend. Hence runtime resume the devic & link and
6350 * let the system suspend low power states to take effect.
6351 * TODO: If resume takes longer time, we might have optimize
6352 * it in future by not resuming everything if possible.
6353 */
6354 ret = ufshcd_runtime_resume(hba);
6355 if (ret)
6356 goto out;
6357 }
6358
6359 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6360out:
Dolev Ravive7850602014-09-25 15:32:36 +03006361 if (!ret)
6362 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006363 return ret;
6364}
6365EXPORT_SYMBOL(ufshcd_system_suspend);
6366
6367/**
6368 * ufshcd_system_resume - system resume routine
6369 * @hba: per adapter instance
6370 *
6371 * Returns 0 for success and non-zero for failure
6372 */
6373
6374int ufshcd_system_resume(struct ufs_hba *hba)
6375{
6376 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
6377 /*
6378 * Let the runtime resume take care of resuming
6379 * if runtime suspended.
6380 */
6381 return 0;
6382
6383 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6384}
6385EXPORT_SYMBOL(ufshcd_system_resume);
6386
6387/**
6388 * ufshcd_runtime_suspend - runtime suspend routine
6389 * @hba: per adapter instance
6390 *
6391 * Check the description of ufshcd_suspend() function for more details.
6392 *
6393 * Returns 0 for success and non-zero for failure
6394 */
6395int ufshcd_runtime_suspend(struct ufs_hba *hba)
6396{
6397 if (!hba || !hba->is_powered)
6398 return 0;
6399
6400 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306401}
6402EXPORT_SYMBOL(ufshcd_runtime_suspend);
6403
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006404/**
6405 * ufshcd_runtime_resume - runtime resume routine
6406 * @hba: per adapter instance
6407 *
6408 * This function basically brings the UFS device, UniPro link and controller
6409 * to active state. Following operations are done in this function:
6410 *
6411 * 1. Turn on all the controller related clocks
6412 * 2. Bring the UniPro link out of Hibernate state
6413 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6414 * to active state.
6415 * 4. If auto-bkops is enabled on the device, disable it.
6416 *
6417 * So following would be the possible power state after this function return
6418 * successfully:
6419 * S1: UFS device in Active state with VCC rail ON
6420 * UniPro link in Active state
6421 * All the UFS/UniPro controller clocks are ON
6422 *
6423 * Returns 0 for success and non-zero for failure
6424 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306425int ufshcd_runtime_resume(struct ufs_hba *hba)
6426{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006427 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306428 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006429 else
6430 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306431}
6432EXPORT_SYMBOL(ufshcd_runtime_resume);
6433
6434int ufshcd_runtime_idle(struct ufs_hba *hba)
6435{
6436 return 0;
6437}
6438EXPORT_SYMBOL(ufshcd_runtime_idle);
6439
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306440/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006441 * ufshcd_shutdown - shutdown routine
6442 * @hba: per adapter instance
6443 *
6444 * This function would power off both UFS device and UFS link.
6445 *
6446 * Returns 0 always to allow force shutdown even in case of errors.
6447 */
6448int ufshcd_shutdown(struct ufs_hba *hba)
6449{
6450 int ret = 0;
6451
6452 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6453 goto out;
6454
6455 if (pm_runtime_suspended(hba->dev)) {
6456 ret = ufshcd_runtime_resume(hba);
6457 if (ret)
6458 goto out;
6459 }
6460
6461 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6462out:
6463 if (ret)
6464 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6465 /* allow force shutdown even in case of errors */
6466 return 0;
6467}
6468EXPORT_SYMBOL(ufshcd_shutdown);
6469
6470/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306471 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306472 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306473 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306474 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306475void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306476{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306477 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306478 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306479 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006480 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306481
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306482 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006483
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006484 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006485 if (ufshcd_is_clkscaling_enabled(hba))
6486 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006487 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306488}
6489EXPORT_SYMBOL_GPL(ufshcd_remove);
6490
6491/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006492 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6493 * @hba: pointer to Host Bus Adapter (HBA)
6494 */
6495void ufshcd_dealloc_host(struct ufs_hba *hba)
6496{
6497 scsi_host_put(hba->host);
6498}
6499EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6500
6501/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006502 * ufshcd_set_dma_mask - Set dma mask based on the controller
6503 * addressing capability
6504 * @hba: per adapter instance
6505 *
6506 * Returns 0 for success, non-zero for failure
6507 */
6508static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6509{
6510 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6511 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6512 return 0;
6513 }
6514 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6515}
6516
6517/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006518 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306519 * @dev: pointer to device handle
6520 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306521 * Returns 0 on success, non-zero value on failure
6522 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006523int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306524{
6525 struct Scsi_Host *host;
6526 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006527 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306528
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306529 if (!dev) {
6530 dev_err(dev,
6531 "Invalid memory reference for dev is NULL\n");
6532 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306533 goto out_error;
6534 }
6535
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306536 host = scsi_host_alloc(&ufshcd_driver_template,
6537 sizeof(struct ufs_hba));
6538 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306539 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306540 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306541 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306542 }
6543 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306544 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306545 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006546 *hba_handle = hba;
6547
6548out_error:
6549 return err;
6550}
6551EXPORT_SYMBOL(ufshcd_alloc_host);
6552
Sahitya Tummala856b3482014-09-25 15:32:34 +03006553static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6554{
6555 int ret = 0;
6556 struct ufs_clk_info *clki;
6557 struct list_head *head = &hba->clk_list_head;
6558
6559 if (!head || list_empty(head))
6560 goto out;
6561
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006562 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6563 if (ret)
6564 return ret;
6565
Sahitya Tummala856b3482014-09-25 15:32:34 +03006566 list_for_each_entry(clki, head, list) {
6567 if (!IS_ERR_OR_NULL(clki->clk)) {
6568 if (scale_up && clki->max_freq) {
6569 if (clki->curr_freq == clki->max_freq)
6570 continue;
6571 ret = clk_set_rate(clki->clk, clki->max_freq);
6572 if (ret) {
6573 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6574 __func__, clki->name,
6575 clki->max_freq, ret);
6576 break;
6577 }
6578 clki->curr_freq = clki->max_freq;
6579
6580 } else if (!scale_up && clki->min_freq) {
6581 if (clki->curr_freq == clki->min_freq)
6582 continue;
6583 ret = clk_set_rate(clki->clk, clki->min_freq);
6584 if (ret) {
6585 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6586 __func__, clki->name,
6587 clki->min_freq, ret);
6588 break;
6589 }
6590 clki->curr_freq = clki->min_freq;
6591 }
6592 }
6593 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6594 clki->name, clk_get_rate(clki->clk));
6595 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006596
6597 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6598
Sahitya Tummala856b3482014-09-25 15:32:34 +03006599out:
6600 return ret;
6601}
6602
6603static int ufshcd_devfreq_target(struct device *dev,
6604 unsigned long *freq, u32 flags)
6605{
6606 int err = 0;
6607 struct ufs_hba *hba = dev_get_drvdata(dev);
6608
6609 if (!ufshcd_is_clkscaling_enabled(hba))
6610 return -EINVAL;
6611
6612 if (*freq == UINT_MAX)
6613 err = ufshcd_scale_clks(hba, true);
6614 else if (*freq == 0)
6615 err = ufshcd_scale_clks(hba, false);
6616
6617 return err;
6618}
6619
6620static int ufshcd_devfreq_get_dev_status(struct device *dev,
6621 struct devfreq_dev_status *stat)
6622{
6623 struct ufs_hba *hba = dev_get_drvdata(dev);
6624 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6625 unsigned long flags;
6626
6627 if (!ufshcd_is_clkscaling_enabled(hba))
6628 return -EINVAL;
6629
6630 memset(stat, 0, sizeof(*stat));
6631
6632 spin_lock_irqsave(hba->host->host_lock, flags);
6633 if (!scaling->window_start_t)
6634 goto start_window;
6635
6636 if (scaling->is_busy_started)
6637 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6638 scaling->busy_start_t));
6639
6640 stat->total_time = jiffies_to_usecs((long)jiffies -
6641 (long)scaling->window_start_t);
6642 stat->busy_time = scaling->tot_busy_t;
6643start_window:
6644 scaling->window_start_t = jiffies;
6645 scaling->tot_busy_t = 0;
6646
6647 if (hba->outstanding_reqs) {
6648 scaling->busy_start_t = ktime_get();
6649 scaling->is_busy_started = true;
6650 } else {
6651 scaling->busy_start_t = ktime_set(0, 0);
6652 scaling->is_busy_started = false;
6653 }
6654 spin_unlock_irqrestore(hba->host->host_lock, flags);
6655 return 0;
6656}
6657
6658static struct devfreq_dev_profile ufs_devfreq_profile = {
6659 .polling_ms = 100,
6660 .target = ufshcd_devfreq_target,
6661 .get_dev_status = ufshcd_devfreq_get_dev_status,
6662};
6663
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006664/**
6665 * ufshcd_init - Driver initialization routine
6666 * @hba: per-adapter instance
6667 * @mmio_base: base register address
6668 * @irq: Interrupt line of device
6669 * Returns 0 on success, non-zero value on failure
6670 */
6671int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6672{
6673 int err;
6674 struct Scsi_Host *host = hba->host;
6675 struct device *dev = hba->dev;
6676
6677 if (!mmio_base) {
6678 dev_err(hba->dev,
6679 "Invalid memory reference for mmio_base is NULL\n");
6680 err = -ENODEV;
6681 goto out_error;
6682 }
6683
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306684 hba->mmio_base = mmio_base;
6685 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306686
Potomski, MichalXe1928452017-02-23 09:05:30 +00006687 /* Set descriptor lengths to specification defaults */
6688 ufshcd_def_desc_sizes(hba);
6689
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006690 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006691 if (err)
6692 goto out_error;
6693
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306694 /* Read capabilities registers */
6695 ufshcd_hba_capabilities(hba);
6696
6697 /* Get UFS version supported by the controller */
6698 hba->ufs_version = ufshcd_get_ufs_version(hba);
6699
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306700 /* Get Interrupt bit mask per version */
6701 hba->intr_mask = ufshcd_get_intr_mask(hba);
6702
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006703 err = ufshcd_set_dma_mask(hba);
6704 if (err) {
6705 dev_err(hba->dev, "set dma mask failed\n");
6706 goto out_disable;
6707 }
6708
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306709 /* Allocate memory for host memory space */
6710 err = ufshcd_memory_alloc(hba);
6711 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306712 dev_err(hba->dev, "Memory allocation failed\n");
6713 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306714 }
6715
6716 /* Configure LRB */
6717 ufshcd_host_memory_configure(hba);
6718
6719 host->can_queue = hba->nutrs;
6720 host->cmd_per_lun = hba->nutrs;
6721 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006722 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306723 host->max_channel = UFSHCD_MAX_CHANNEL;
6724 host->unique_id = host->host_no;
6725 host->max_cmd_len = MAX_CDB_SIZE;
6726
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006727 hba->max_pwr_info.is_valid = false;
6728
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306729 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306730 init_waitqueue_head(&hba->tm_wq);
6731 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306732
6733 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306734 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306735 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306736
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306737 /* Initialize UIC command mutex */
6738 mutex_init(&hba->uic_cmd_mutex);
6739
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306740 /* Initialize mutex for device management commands */
6741 mutex_init(&hba->dev_cmd.lock);
6742
6743 /* Initialize device management tag acquire wait queue */
6744 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6745
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006746 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006747
6748 /*
6749 * In order to avoid any spurious interrupt immediately after
6750 * registering UFS controller interrupt handler, clear any pending UFS
6751 * interrupt status and disable all the UFS interrupts.
6752 */
6753 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6754 REG_INTERRUPT_STATUS);
6755 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6756 /*
6757 * Make sure that UFS interrupts are disabled and any pending interrupt
6758 * status is cleared before registering UFS interrupt handler.
6759 */
6760 mb();
6761
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306762 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006763 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306764 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306765 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006766 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006767 } else {
6768 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306769 }
6770
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306771 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306772 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306773 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006774 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306775 }
6776
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306777 /* Host controller enable */
6778 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306779 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306780 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306781 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306782 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306783
Sahitya Tummala856b3482014-09-25 15:32:34 +03006784 if (ufshcd_is_clkscaling_enabled(hba)) {
6785 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6786 "simple_ondemand", NULL);
6787 if (IS_ERR(hba->devfreq)) {
6788 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6789 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006790 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006791 goto out_remove_scsi_host;
6792 }
6793 /* Suspend devfreq until the UFS device is detected */
6794 devfreq_suspend_device(hba->devfreq);
6795 hba->clk_scaling.window_start_t = 0;
6796 }
6797
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306798 /* Hold auto suspend until async scan completes */
6799 pm_runtime_get_sync(dev);
6800
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006801 /*
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006802 * We are assuming that device wasn't put in sleep/power-down
6803 * state exclusively during the boot stage before kernel.
6804 * This assumption helps avoid doing link startup twice during
6805 * ufshcd_probe_hba().
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006806 */
subhashj@codeaurora.orgc5fc9462017-04-04 19:32:20 +00006807 ufshcd_set_ufs_dev_active(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006808
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306809 async_schedule(ufshcd_async_scan, hba);
6810
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306811 return 0;
6812
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306813out_remove_scsi_host:
6814 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006815exit_gating:
6816 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306817out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006818 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306819 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006820 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306821out_error:
6822 return err;
6823}
6824EXPORT_SYMBOL_GPL(ufshcd_init);
6825
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306826MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6827MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306828MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306829MODULE_LICENSE("GPL");
6830MODULE_VERSION(UFSHCD_DRIVER_VERSION);