blob: d2930fb8b44690cb482231bd3348a79ddc6628da [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
Gilad Bronerdcea0bf2016-10-17 17:09:48 -070048#define UFSHCD_REQ_SENSE_SIZE 18
49
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053050#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
51 UTP_TASK_REQ_COMPL |\
52 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053053/* UIC command timeout, unit: ms */
54#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053055
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053056/* NOP OUT retries waiting for NOP IN response */
57#define NOP_OUT_RETRIES 10
58/* Timeout after 30 msecs if NOP OUT hangs without response */
59#define NOP_OUT_TIMEOUT 30 /* msecs */
60
Dolev Raviv68078d52013-07-30 00:35:58 +053061/* Query request retries */
62#define QUERY_REQ_RETRIES 10
63/* Query request timeout */
64#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020065/*
66 * Query request timeout for fDeviceInit flag
67 * fDeviceInit query response time for some devices is too large that default
68 * QUERY_REQ_TIMEOUT may not be enough for such devices.
69 */
70#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053071
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053072/* Task management command timeout */
73#define TM_CMD_TIMEOUT 100 /* msecs */
74
Yaniv Gardi64238fb2016-02-01 15:02:43 +020075/* maximum number of retries for a general UIC command */
76#define UFS_UIC_COMMAND_RETRIES 3
77
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030078/* maximum number of link-startup retries */
79#define DME_LINKSTARTUP_RETRIES 3
80
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020081/* Maximum retries for Hibern8 enter */
82#define UIC_HIBERN8_ENTER_RETRIES 3
83
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030084/* maximum number of reset retries before giving up */
85#define MAX_HOST_RESET_RETRIES 5
86
Dolev Raviv68078d52013-07-30 00:35:58 +053087/* Expose the flag value from utp_upiu_query.value */
88#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
89
Seungwon Jeon7d568652013-08-31 21:40:20 +053090/* Interrupt aggregation default timeout, unit: 40us */
91#define INT_AGGR_DEF_TO 0x02
92
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030093#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
94 ({ \
95 int _ret; \
96 if (_on) \
97 _ret = ufshcd_enable_vreg(_dev, _vreg); \
98 else \
99 _ret = ufshcd_disable_vreg(_dev, _vreg); \
100 _ret; \
101 })
102
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300103static u32 ufs_query_desc_max_size[] = {
104 QUERY_DESC_DEVICE_MAX_SIZE,
105 QUERY_DESC_CONFIGURAION_MAX_SIZE,
106 QUERY_DESC_UNIT_MAX_SIZE,
107 QUERY_DESC_RFU_MAX_SIZE,
108 QUERY_DESC_INTERCONNECT_MAX_SIZE,
109 QUERY_DESC_STRING_MAX_SIZE,
110 QUERY_DESC_RFU_MAX_SIZE,
Tomas Winkler1ce21792016-02-09 10:25:40 +0200111 QUERY_DESC_GEOMETRY_MAX_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300112 QUERY_DESC_POWER_MAX_SIZE,
113 QUERY_DESC_RFU_MAX_SIZE,
114};
115
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530116enum {
117 UFSHCD_MAX_CHANNEL = 0,
118 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530119 UFSHCD_CMD_PER_LUN = 32,
120 UFSHCD_CAN_QUEUE = 32,
121};
122
123/* UFSHCD states */
124enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530125 UFSHCD_STATE_RESET,
126 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530127 UFSHCD_STATE_OPERATIONAL,
128};
129
130/* UFSHCD error handling flags */
131enum {
132 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530133};
134
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530135/* UFSHCD UIC layer error flags */
136enum {
137 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +0200138 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR = (1 << 1), /* Data link layer error */
139 UFSHCD_UIC_DL_TCx_REPLAY_ERROR = (1 << 2), /* Data link layer error */
140 UFSHCD_UIC_NL_ERROR = (1 << 3), /* Network layer error */
141 UFSHCD_UIC_TL_ERROR = (1 << 4), /* Transport Layer error */
142 UFSHCD_UIC_DME_ERROR = (1 << 5), /* DME error */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530143};
144
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530145/* Interrupt configuration options */
146enum {
147 UFSHCD_INT_DISABLE,
148 UFSHCD_INT_ENABLE,
149 UFSHCD_INT_CLEAR,
150};
151
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530152#define ufshcd_set_eh_in_progress(h) \
153 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
154#define ufshcd_eh_in_progress(h) \
155 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
156#define ufshcd_clear_eh_in_progress(h) \
157 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
158
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300159#define ufshcd_set_ufs_dev_active(h) \
160 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
161#define ufshcd_set_ufs_dev_sleep(h) \
162 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
163#define ufshcd_set_ufs_dev_poweroff(h) \
164 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
165#define ufshcd_is_ufs_dev_active(h) \
166 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
167#define ufshcd_is_ufs_dev_sleep(h) \
168 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
169#define ufshcd_is_ufs_dev_poweroff(h) \
170 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
171
172static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
173 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
174 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
175 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
176 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
177 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
178 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
179};
180
181static inline enum ufs_dev_pwr_mode
182ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
183{
184 return ufs_pm_lvl_states[lvl].dev_state;
185}
186
187static inline enum uic_link_state
188ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
189{
190 return ufs_pm_lvl_states[lvl].link_state;
191}
192
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530193static void ufshcd_tmc_handler(struct ufs_hba *hba);
194static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530195static int ufshcd_reset_and_restore(struct ufs_hba *hba);
196static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300197static void ufshcd_hba_exit(struct ufs_hba *hba);
198static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300199static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
200 bool skip_ref_clk);
201static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
Yaniv Gardi60f01872016-03-10 17:37:11 +0200202static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300203static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
204static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300205static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300206static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
207static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300208static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
209 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300210static int ufshcd_change_power_mode(struct ufs_hba *hba,
211 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200212static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
213{
214 return tag >= 0 && tag < hba->nutrs;
215}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300216
217static inline int ufshcd_enable_irq(struct ufs_hba *hba)
218{
219 int ret = 0;
220
221 if (!hba->is_irq_enabled) {
222 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
223 hba);
224 if (ret)
225 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
226 __func__, ret);
227 hba->is_irq_enabled = true;
228 }
229
230 return ret;
231}
232
233static inline void ufshcd_disable_irq(struct ufs_hba *hba)
234{
235 if (hba->is_irq_enabled) {
236 free_irq(hba->irq, hba);
237 hba->is_irq_enabled = false;
238 }
239}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530240
Yaniv Gardib573d482016-03-10 17:37:09 +0200241/* replace non-printable or non-ASCII characters with spaces */
242static inline void ufshcd_remove_non_printable(char *val)
243{
244 if (!val)
245 return;
246
247 if (*val < 0x20 || *val > 0x7e)
248 *val = ' ';
249}
250
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530251/*
252 * ufshcd_wait_for_register - wait for register value to change
253 * @hba - per-adapter interface
254 * @reg - mmio register offset
255 * @mask - mask to apply to read register value
256 * @val - wait condition
257 * @interval_us - polling interval in microsecs
258 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200259 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530260 *
261 * Returns -ETIMEDOUT on error, zero on success
262 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200263int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
264 u32 val, unsigned long interval_us,
265 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530266{
267 int err = 0;
268 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
269
270 /* ignore bits that we don't intend to wait on */
271 val = val & mask;
272
273 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200274 if (can_sleep)
275 usleep_range(interval_us, interval_us + 50);
276 else
277 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530278 if (time_after(jiffies, timeout)) {
279 if ((ufshcd_readl(hba, reg) & mask) != val)
280 err = -ETIMEDOUT;
281 break;
282 }
283 }
284
285 return err;
286}
287
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530288/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530289 * ufshcd_get_intr_mask - Get the interrupt bit mask
290 * @hba - Pointer to adapter instance
291 *
292 * Returns interrupt bit mask per version
293 */
294static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
295{
296 if (hba->ufs_version == UFSHCI_VERSION_10)
297 return INTERRUPT_MASK_ALL_VER_10;
298 else
299 return INTERRUPT_MASK_ALL_VER_11;
300}
301
302/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530303 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
304 * @hba - Pointer to adapter instance
305 *
306 * Returns UFSHCI version supported by the controller
307 */
308static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
309{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200310 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
311 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300312
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530313 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530314}
315
316/**
317 * ufshcd_is_device_present - Check if any device connected to
318 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300319 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530320 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530321 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530322 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300323static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530324{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300325 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
326 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530327}
328
329/**
330 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
331 * @lrb: pointer to local command reference block
332 *
333 * This function is used to get the OCS field from UTRD
334 * Returns the OCS field in the UTRD
335 */
336static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
337{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530338 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530339}
340
341/**
342 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
343 * @task_req_descp: pointer to utp_task_req_desc structure
344 *
345 * This function is used to get the OCS field from UTMRD
346 * Returns the OCS field in the UTMRD
347 */
348static inline int
349ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
350{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530351 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530352}
353
354/**
355 * ufshcd_get_tm_free_slot - get a free slot for task management request
356 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530357 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530358 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530359 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
360 * Returns 0 if free slot is not available, else return 1 with tag value
361 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530362 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530363static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530364{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530365 int tag;
366 bool ret = false;
367
368 if (!free_slot)
369 goto out;
370
371 do {
372 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
373 if (tag >= hba->nutmrs)
374 goto out;
375 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
376
377 *free_slot = tag;
378 ret = true;
379out:
380 return ret;
381}
382
383static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
384{
385 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530386}
387
388/**
389 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
390 * @hba: per adapter instance
391 * @pos: position of the bit to be cleared
392 */
393static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
394{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530395 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530396}
397
398/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200399 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
400 * @hba: per adapter instance
401 * @tag: position of the bit to be cleared
402 */
403static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
404{
405 __clear_bit(tag, &hba->outstanding_reqs);
406}
407
408/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530409 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
410 * @reg: Register value of host controller status
411 *
412 * Returns integer, 0 on Success and positive value if failed
413 */
414static inline int ufshcd_get_lists_status(u32 reg)
415{
416 /*
417 * The mask 0xFF is for the following HCS register bits
418 * Bit Description
419 * 0 Device Present
420 * 1 UTRLRDY
421 * 2 UTMRLRDY
422 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200423 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530424 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200425 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530426}
427
428/**
429 * ufshcd_get_uic_cmd_result - Get the UIC command result
430 * @hba: Pointer to adapter instance
431 *
432 * This function gets the result of UIC command completion
433 * Returns 0 on success, non zero value on error
434 */
435static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
436{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530437 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530438 MASK_UIC_COMMAND_RESULT;
439}
440
441/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530442 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
443 * @hba: Pointer to adapter instance
444 *
445 * This function gets UIC command argument3
446 * Returns 0 on success, non zero value on error
447 */
448static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
449{
450 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
451}
452
453/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530454 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530455 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530456 */
457static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530458ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530459{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530460 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530461}
462
463/**
464 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
465 * @ucd_rsp_ptr: pointer to response UPIU
466 *
467 * This function gets the response status and scsi_status from response UPIU
468 * Returns the response result code.
469 */
470static inline int
471ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
472{
473 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
474}
475
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530476/*
477 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
478 * from response UPIU
479 * @ucd_rsp_ptr: pointer to response UPIU
480 *
481 * Return the data segment length.
482 */
483static inline unsigned int
484ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
485{
486 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
487 MASK_RSP_UPIU_DATA_SEG_LEN;
488}
489
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530490/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530491 * ufshcd_is_exception_event - Check if the device raised an exception event
492 * @ucd_rsp_ptr: pointer to response UPIU
493 *
494 * The function checks if the device raised an exception event indicated in
495 * the Device Information field of response UPIU.
496 *
497 * Returns true if exception is raised, false otherwise.
498 */
499static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
500{
501 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
502 MASK_RSP_EXCEPTION_EVENT ? true : false;
503}
504
505/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530506 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530507 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530508 */
509static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530510ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530511{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530512 ufshcd_writel(hba, INT_AGGR_ENABLE |
513 INT_AGGR_COUNTER_AND_TIMER_RESET,
514 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
515}
516
517/**
518 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
519 * @hba: per adapter instance
520 * @cnt: Interrupt aggregation counter threshold
521 * @tmout: Interrupt aggregation timeout value
522 */
523static inline void
524ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
525{
526 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
527 INT_AGGR_COUNTER_THLD_VAL(cnt) |
528 INT_AGGR_TIMEOUT_VAL(tmout),
529 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530530}
531
532/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300533 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
534 * @hba: per adapter instance
535 */
536static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
537{
538 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
539}
540
541/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530542 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
543 * When run-stop registers are set to 1, it indicates the
544 * host controller that it can process the requests
545 * @hba: per adapter instance
546 */
547static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
548{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530549 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
550 REG_UTP_TASK_REQ_LIST_RUN_STOP);
551 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
552 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530553}
554
555/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530556 * ufshcd_hba_start - Start controller initialization sequence
557 * @hba: per adapter instance
558 */
559static inline void ufshcd_hba_start(struct ufs_hba *hba)
560{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530561 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530562}
563
564/**
565 * ufshcd_is_hba_active - Get controller state
566 * @hba: per adapter instance
567 *
568 * Returns zero if controller is active, 1 otherwise
569 */
570static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
571{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530572 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530573}
574
Yaniv Gardi37113102016-03-10 17:37:16 +0200575u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba)
576{
577 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
578 if ((hba->ufs_version == UFSHCI_VERSION_10) ||
579 (hba->ufs_version == UFSHCI_VERSION_11))
580 return UFS_UNIPRO_VER_1_41;
581 else
582 return UFS_UNIPRO_VER_1_6;
583}
584EXPORT_SYMBOL(ufshcd_get_local_unipro_ver);
585
586static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba)
587{
588 /*
589 * If both host and device support UniPro ver1.6 or later, PA layer
590 * parameters tuning happens during link startup itself.
591 *
592 * We can manually tune PA layer parameters if either host or device
593 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
594 * logic simple, we will only do manual tuning if local unipro version
595 * doesn't support ver1.6 or later.
596 */
597 if (ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6)
598 return true;
599 else
600 return false;
601}
602
Gilad Bronera5082532016-10-17 17:10:00 -0700603static void ufshcd_suspend_clkscaling(struct ufs_hba *hba)
604{
605 if (ufshcd_is_clkscaling_enabled(hba)) {
606 devfreq_suspend_device(hba->devfreq);
607 hba->clk_scaling.window_start_t = 0;
608 }
609}
610
611static void ufshcd_resume_clkscaling(struct ufs_hba *hba)
612{
613 if (ufshcd_is_clkscaling_enabled(hba))
614 devfreq_resume_device(hba->devfreq);
615}
616
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300617static void ufshcd_ungate_work(struct work_struct *work)
618{
619 int ret;
620 unsigned long flags;
621 struct ufs_hba *hba = container_of(work, struct ufs_hba,
622 clk_gating.ungate_work);
623
624 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
625
626 spin_lock_irqsave(hba->host->host_lock, flags);
627 if (hba->clk_gating.state == CLKS_ON) {
628 spin_unlock_irqrestore(hba->host->host_lock, flags);
629 goto unblock_reqs;
630 }
631
632 spin_unlock_irqrestore(hba->host->host_lock, flags);
633 ufshcd_setup_clocks(hba, true);
634
635 /* Exit from hibern8 */
636 if (ufshcd_can_hibern8_during_gating(hba)) {
637 /* Prevent gating in this path */
638 hba->clk_gating.is_suspended = true;
639 if (ufshcd_is_link_hibern8(hba)) {
640 ret = ufshcd_uic_hibern8_exit(hba);
641 if (ret)
642 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
643 __func__, ret);
644 else
645 ufshcd_set_link_active(hba);
646 }
647 hba->clk_gating.is_suspended = false;
648 }
649unblock_reqs:
Gilad Bronera5082532016-10-17 17:10:00 -0700650 ufshcd_resume_clkscaling(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300651 scsi_unblock_requests(hba->host);
652}
653
654/**
655 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
656 * Also, exit from hibern8 mode and set the link as active.
657 * @hba: per adapter instance
658 * @async: This indicates whether caller should ungate clocks asynchronously.
659 */
660int ufshcd_hold(struct ufs_hba *hba, bool async)
661{
662 int rc = 0;
663 unsigned long flags;
664
665 if (!ufshcd_is_clkgating_allowed(hba))
666 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300667 spin_lock_irqsave(hba->host->host_lock, flags);
668 hba->clk_gating.active_reqs++;
669
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200670 if (ufshcd_eh_in_progress(hba)) {
671 spin_unlock_irqrestore(hba->host->host_lock, flags);
672 return 0;
673 }
674
Sahitya Tummala856b3482014-09-25 15:32:34 +0300675start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300676 switch (hba->clk_gating.state) {
677 case CLKS_ON:
678 break;
679 case REQ_CLKS_OFF:
680 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
681 hba->clk_gating.state = CLKS_ON;
682 break;
683 }
684 /*
685 * If we here, it means gating work is either done or
686 * currently running. Hence, fall through to cancel gating
687 * work and to enable clocks.
688 */
689 case CLKS_OFF:
690 scsi_block_requests(hba->host);
691 hba->clk_gating.state = REQ_CLKS_ON;
692 schedule_work(&hba->clk_gating.ungate_work);
693 /*
694 * fall through to check if we should wait for this
695 * work to be done or not.
696 */
697 case REQ_CLKS_ON:
698 if (async) {
699 rc = -EAGAIN;
700 hba->clk_gating.active_reqs--;
701 break;
702 }
703
704 spin_unlock_irqrestore(hba->host->host_lock, flags);
705 flush_work(&hba->clk_gating.ungate_work);
706 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300707 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300708 goto start;
709 default:
710 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
711 __func__, hba->clk_gating.state);
712 break;
713 }
714 spin_unlock_irqrestore(hba->host->host_lock, flags);
715out:
716 return rc;
717}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200718EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300719
720static void ufshcd_gate_work(struct work_struct *work)
721{
722 struct ufs_hba *hba = container_of(work, struct ufs_hba,
723 clk_gating.gate_work.work);
724 unsigned long flags;
725
726 spin_lock_irqsave(hba->host->host_lock, flags);
727 if (hba->clk_gating.is_suspended) {
728 hba->clk_gating.state = CLKS_ON;
729 goto rel_lock;
730 }
731
732 if (hba->clk_gating.active_reqs
733 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
734 || hba->lrb_in_use || hba->outstanding_tasks
735 || hba->active_uic_cmd || hba->uic_async_done)
736 goto rel_lock;
737
738 spin_unlock_irqrestore(hba->host->host_lock, flags);
739
740 /* put the link into hibern8 mode before turning off clocks */
741 if (ufshcd_can_hibern8_during_gating(hba)) {
742 if (ufshcd_uic_hibern8_enter(hba)) {
743 hba->clk_gating.state = CLKS_ON;
744 goto out;
745 }
746 ufshcd_set_link_hibern8(hba);
747 }
748
Gilad Bronera5082532016-10-17 17:10:00 -0700749 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +0300750
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);
Gilad Bronerad1a1b92016-10-17 17:09:36 -0700893 /* Make sure that doorbell is committed immediately */
894 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530895}
896
897/**
898 * ufshcd_copy_sense_data - Copy sense data in case of check condition
899 * @lrb - pointer to local reference block
900 */
901static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
902{
903 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530904 if (lrbp->sense_buffer &&
905 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Yaniv Gardie3ce73d2016-10-17 17:09:24 -0700906 int len_to_copy;
907
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530908 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Yaniv Gardie3ce73d2016-10-17 17:09:24 -0700909 len_to_copy = min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH, len);
910
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530911 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530912 lrbp->ucd_rsp_ptr->sr.sense_data,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -0700913 min_t(int, len_to_copy, UFSHCD_REQ_SENSE_SIZE));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530914 }
915}
916
917/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530918 * ufshcd_copy_query_response() - Copy the Query Response and the data
919 * descriptor
920 * @hba: per adapter instance
921 * @lrb - pointer to local reference block
922 */
923static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300924int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530925{
926 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
927
Dolev Raviv68078d52013-07-30 00:35:58 +0530928 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530929
Dolev Raviv68078d52013-07-30 00:35:58 +0530930 /* Get the descriptor */
931 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300932 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530933 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300934 u16 resp_len;
935 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530936
937 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300938 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530939 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300940 buf_len = be16_to_cpu(
941 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300942 if (likely(buf_len >= resp_len)) {
943 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
944 } else {
945 dev_warn(hba->dev,
946 "%s: Response size is bigger than buffer",
947 __func__);
948 return -EINVAL;
949 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530950 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300951
952 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530953}
954
955/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530956 * ufshcd_hba_capabilities - Read controller capabilities
957 * @hba: per adapter instance
958 */
959static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
960{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530961 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530962
963 /* nutrs and nutmrs are 0 based values */
964 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
965 hba->nutmrs =
966 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
967}
968
969/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530970 * ufshcd_ready_for_uic_cmd - Check if controller is ready
971 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530972 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530973 * Return true on success, else false
974 */
975static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
976{
977 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
978 return true;
979 else
980 return false;
981}
982
983/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530984 * ufshcd_get_upmcrs - Get the power mode change request status
985 * @hba: Pointer to adapter instance
986 *
987 * This function gets the UPMCRS field of HCS register
988 * Returns value of UPMCRS field
989 */
990static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
991{
992 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
993}
994
995/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530996 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
997 * @hba: per adapter instance
998 * @uic_cmd: UIC command
999 *
1000 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301001 */
1002static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301003ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301004{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301005 WARN_ON(hba->active_uic_cmd);
1006
1007 hba->active_uic_cmd = uic_cmd;
1008
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301009 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301010 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
1011 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
1012 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301013
1014 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301015 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301016 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301017}
1018
1019/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301020 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1021 * @hba: per adapter instance
1022 * @uic_command: UIC command
1023 *
1024 * Must be called with mutex held.
1025 * Returns 0 only if success.
1026 */
1027static int
1028ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1029{
1030 int ret;
1031 unsigned long flags;
1032
1033 if (wait_for_completion_timeout(&uic_cmd->done,
1034 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
1035 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
1036 else
1037 ret = -ETIMEDOUT;
1038
1039 spin_lock_irqsave(hba->host->host_lock, flags);
1040 hba->active_uic_cmd = NULL;
1041 spin_unlock_irqrestore(hba->host->host_lock, flags);
1042
1043 return ret;
1044}
1045
1046/**
1047 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1048 * @hba: per adapter instance
1049 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001050 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301051 *
1052 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001053 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301054 * Returns 0 only if success.
1055 */
1056static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001057__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1058 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301059{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301060 if (!ufshcd_ready_for_uic_cmd(hba)) {
1061 dev_err(hba->dev,
1062 "Controller not ready to accept UIC commands\n");
1063 return -EIO;
1064 }
1065
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001066 if (completion)
1067 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301068
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301069 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301070
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001071 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301072}
1073
1074/**
1075 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1076 * @hba: per adapter instance
1077 * @uic_cmd: UIC command
1078 *
1079 * Returns 0 only if success.
1080 */
1081static int
1082ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1083{
1084 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001085 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301086
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001087 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301088 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001089 ufshcd_add_delay_before_dme_cmd(hba);
1090
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001091 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001092 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001093 spin_unlock_irqrestore(hba->host->host_lock, flags);
1094 if (!ret)
1095 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1096
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301097 mutex_unlock(&hba->uic_cmd_mutex);
1098
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001099 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301100 return ret;
1101}
1102
1103/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301104 * ufshcd_map_sg - Map scatter-gather list to prdt
1105 * @lrbp - pointer to local reference block
1106 *
1107 * Returns 0 in case of success, non-zero value in case of failure
1108 */
1109static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1110{
1111 struct ufshcd_sg_entry *prd_table;
1112 struct scatterlist *sg;
1113 struct scsi_cmnd *cmd;
1114 int sg_segments;
1115 int i;
1116
1117 cmd = lrbp->cmd;
1118 sg_segments = scsi_dma_map(cmd);
1119 if (sg_segments < 0)
1120 return sg_segments;
1121
1122 if (sg_segments) {
1123 lrbp->utr_descriptor_ptr->prd_table_length =
1124 cpu_to_le16((u16) (sg_segments));
1125
1126 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1127
1128 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1129 prd_table[i].size =
1130 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1131 prd_table[i].base_addr =
1132 cpu_to_le32(lower_32_bits(sg->dma_address));
1133 prd_table[i].upper_addr =
1134 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001135 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301136 }
1137 } else {
1138 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1139 }
1140
1141 return 0;
1142}
1143
1144/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301145 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301146 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301147 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301148 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301149static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301150{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301151 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1152
1153 if (hba->ufs_version == UFSHCI_VERSION_10) {
1154 u32 rw;
1155 rw = set & INTERRUPT_MASK_RW_VER_10;
1156 set = rw | ((set ^ intrs) & intrs);
1157 } else {
1158 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301159 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301160
1161 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1162}
1163
1164/**
1165 * ufshcd_disable_intr - disable interrupts
1166 * @hba: per adapter instance
1167 * @intrs: interrupt bits
1168 */
1169static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1170{
1171 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1172
1173 if (hba->ufs_version == UFSHCI_VERSION_10) {
1174 u32 rw;
1175 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1176 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1177 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1178
1179 } else {
1180 set &= ~intrs;
1181 }
1182
1183 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301184}
1185
1186/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301187 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1188 * descriptor according to request
1189 * @lrbp: pointer to local reference block
1190 * @upiu_flags: flags required in the header
1191 * @cmd_dir: requests data direction
1192 */
1193static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
Joao Pinto300bb132016-05-11 12:21:27 +01001194 u32 *upiu_flags, enum dma_data_direction cmd_dir)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301195{
1196 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1197 u32 data_direction;
1198 u32 dword_0;
1199
1200 if (cmd_dir == DMA_FROM_DEVICE) {
1201 data_direction = UTP_DEVICE_TO_HOST;
1202 *upiu_flags = UPIU_CMD_FLAGS_READ;
1203 } else if (cmd_dir == DMA_TO_DEVICE) {
1204 data_direction = UTP_HOST_TO_DEVICE;
1205 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1206 } else {
1207 data_direction = UTP_NO_DATA_TRANSFER;
1208 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1209 }
1210
1211 dword_0 = data_direction | (lrbp->command_type
1212 << UPIU_COMMAND_TYPE_OFFSET);
1213 if (lrbp->intr_cmd)
1214 dword_0 |= UTP_REQ_DESC_INT_CMD;
1215
1216 /* Transfer request descriptor header fields */
1217 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001218 /* dword_1 is reserved, hence it is set to 0 */
1219 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301220 /*
1221 * assigning invalid value for command status. Controller
1222 * updates OCS on command completion, with the command
1223 * status
1224 */
1225 req_desc->header.dword_2 =
1226 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001227 /* dword_3 is reserved, hence it is set to 0 */
1228 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001229
1230 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301231}
1232
1233/**
1234 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1235 * for scsi commands
1236 * @lrbp - local reference block pointer
1237 * @upiu_flags - flags
1238 */
1239static
1240void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1241{
1242 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001243 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301244
1245 /* command descriptor fields */
1246 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1247 UPIU_TRANSACTION_COMMAND, upiu_flags,
1248 lrbp->lun, lrbp->task_tag);
1249 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1250 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1251
1252 /* Total EHS length and Data segment length will be zero */
1253 ucd_req_ptr->header.dword_2 = 0;
1254
1255 ucd_req_ptr->sc.exp_data_transfer_len =
1256 cpu_to_be32(lrbp->cmd->sdb.length);
1257
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001258 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1259 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1260 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1261
1262 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301263}
1264
Dolev Raviv68078d52013-07-30 00:35:58 +05301265/**
1266 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1267 * for query requsts
1268 * @hba: UFS hba
1269 * @lrbp: local reference block pointer
1270 * @upiu_flags: flags
1271 */
1272static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1273 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1274{
1275 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1276 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301277 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301278 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1279
1280 /* Query request header */
1281 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1282 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1283 lrbp->lun, lrbp->task_tag);
1284 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1285 0, query->request.query_func, 0, 0);
1286
Zang Leigang68612852016-08-25 17:39:19 +08001287 /* Data segment length only need for WRITE_DESC */
1288 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1289 ucd_req_ptr->header.dword_2 =
1290 UPIU_HEADER_DWORD(0, 0, (len >> 8), (u8)len);
1291 else
1292 ucd_req_ptr->header.dword_2 = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301293
1294 /* Copy the Query Request buffer as is */
1295 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1296 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301297
1298 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001299 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1300 memcpy(descp, query->descriptor, len);
1301
Yaniv Gardi51047262016-02-01 15:02:38 +02001302 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301303}
1304
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301305static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1306{
1307 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1308
1309 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1310
1311 /* command descriptor fields */
1312 ucd_req_ptr->header.dword_0 =
1313 UPIU_HEADER_DWORD(
1314 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001315 /* clear rest of the fields of basic header */
1316 ucd_req_ptr->header.dword_1 = 0;
1317 ucd_req_ptr->header.dword_2 = 0;
1318
1319 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301320}
1321
1322/**
Joao Pinto300bb132016-05-11 12:21:27 +01001323 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
1324 * for Device Management Purposes
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301325 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301326 * @lrb - pointer to local reference block
1327 */
Joao Pinto300bb132016-05-11 12:21:27 +01001328static int ufshcd_comp_devman_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301329{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301330 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301331 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301332
Joao Pinto300bb132016-05-11 12:21:27 +01001333 if (hba->ufs_version == UFSHCI_VERSION_20)
1334 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1335 else
1336 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1337
1338 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
1339 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1340 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags);
1341 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
1342 ufshcd_prepare_utp_nop_upiu(lrbp);
1343 else
1344 ret = -EINVAL;
1345
1346 return ret;
1347}
1348
1349/**
1350 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
1351 * for SCSI Purposes
1352 * @hba - per adapter instance
1353 * @lrb - pointer to local reference block
1354 */
1355static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1356{
1357 u32 upiu_flags;
1358 int ret = 0;
1359
1360 if (hba->ufs_version == UFSHCI_VERSION_20)
1361 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE;
1362 else
1363 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1364
1365 if (likely(lrbp->cmd)) {
1366 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1367 lrbp->cmd->sc_data_direction);
1368 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
1369 } else {
1370 ret = -EINVAL;
1371 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301372
1373 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301374}
1375
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001376/*
1377 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1378 * @scsi_lun: scsi LUN id
1379 *
1380 * Returns UPIU LUN id
1381 */
1382static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1383{
1384 if (scsi_is_wlun(scsi_lun))
1385 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1386 | UFS_UPIU_WLUN_ID;
1387 else
1388 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1389}
1390
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301391/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001392 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1393 * @scsi_lun: UPIU W-LUN id
1394 *
1395 * Returns SCSI W-LUN id
1396 */
1397static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1398{
1399 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1400}
1401
1402/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301403 * ufshcd_queuecommand - main entry point for SCSI requests
1404 * @cmd: command from SCSI Midlayer
1405 * @done: call back function
1406 *
1407 * Returns 0 for success, non-zero in case of failure
1408 */
1409static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1410{
1411 struct ufshcd_lrb *lrbp;
1412 struct ufs_hba *hba;
1413 unsigned long flags;
1414 int tag;
1415 int err = 0;
1416
1417 hba = shost_priv(host);
1418
1419 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001420 if (!ufshcd_valid_tag(hba, tag)) {
1421 dev_err(hba->dev,
1422 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1423 __func__, tag, cmd, cmd->request);
1424 BUG();
1425 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301426
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301427 spin_lock_irqsave(hba->host->host_lock, flags);
1428 switch (hba->ufshcd_state) {
1429 case UFSHCD_STATE_OPERATIONAL:
1430 break;
1431 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;
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07001478 lrbp->sense_bufflen = UFSHCD_REQ_SENSE_SIZE;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301479 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
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301486 err = ufshcd_map_sg(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 }
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001492 /* Make sure descriptors are ready before ringing the doorbell */
1493 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301494
1495 /* issue command to the controller */
1496 spin_lock_irqsave(hba->host->host_lock, flags);
1497 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301498out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301499 spin_unlock_irqrestore(hba->host->host_lock, flags);
1500out:
1501 return err;
1502}
1503
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301504static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1505 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1506{
1507 lrbp->cmd = NULL;
1508 lrbp->sense_bufflen = 0;
1509 lrbp->sense_buffer = NULL;
1510 lrbp->task_tag = tag;
1511 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301512 lrbp->intr_cmd = true; /* No interrupt aggregation */
1513 hba->dev_cmd.type = cmd_type;
1514
Joao Pinto300bb132016-05-11 12:21:27 +01001515 return ufshcd_comp_devman_upiu(hba, lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301516}
1517
1518static int
1519ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1520{
1521 int err = 0;
1522 unsigned long flags;
1523 u32 mask = 1 << tag;
1524
1525 /* clear outstanding transaction before retry */
1526 spin_lock_irqsave(hba->host->host_lock, flags);
1527 ufshcd_utrl_clear(hba, tag);
1528 spin_unlock_irqrestore(hba->host->host_lock, flags);
1529
1530 /*
1531 * wait for for h/w to clear corresponding bit in door-bell.
1532 * max. wait is 1 sec.
1533 */
1534 err = ufshcd_wait_for_register(hba,
1535 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001536 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301537
1538 return err;
1539}
1540
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001541static int
1542ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1543{
1544 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1545
1546 /* Get the UPIU response */
1547 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1548 UPIU_RSP_CODE_OFFSET;
1549 return query_res->response;
1550}
1551
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301552/**
1553 * ufshcd_dev_cmd_completion() - handles device management command responses
1554 * @hba: per adapter instance
1555 * @lrbp: pointer to local reference block
1556 */
1557static int
1558ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1559{
1560 int resp;
1561 int err = 0;
1562
1563 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1564
1565 switch (resp) {
1566 case UPIU_TRANSACTION_NOP_IN:
1567 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1568 err = -EINVAL;
1569 dev_err(hba->dev, "%s: unexpected response %x\n",
1570 __func__, resp);
1571 }
1572 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301573 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001574 err = ufshcd_check_query_response(hba, lrbp);
1575 if (!err)
1576 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301577 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301578 case UPIU_TRANSACTION_REJECT_UPIU:
1579 /* TODO: handle Reject UPIU Response */
1580 err = -EPERM;
1581 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1582 __func__);
1583 break;
1584 default:
1585 err = -EINVAL;
1586 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1587 __func__, resp);
1588 break;
1589 }
1590
1591 return err;
1592}
1593
1594static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1595 struct ufshcd_lrb *lrbp, int max_timeout)
1596{
1597 int err = 0;
1598 unsigned long time_left;
1599 unsigned long flags;
1600
1601 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1602 msecs_to_jiffies(max_timeout));
1603
Gilad Bronerad1a1b92016-10-17 17:09:36 -07001604 /* Make sure descriptors are ready before ringing the doorbell */
1605 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301606 spin_lock_irqsave(hba->host->host_lock, flags);
1607 hba->dev_cmd.complete = NULL;
1608 if (likely(time_left)) {
1609 err = ufshcd_get_tr_ocs(lrbp);
1610 if (!err)
1611 err = ufshcd_dev_cmd_completion(hba, lrbp);
1612 }
1613 spin_unlock_irqrestore(hba->host->host_lock, flags);
1614
1615 if (!time_left) {
1616 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001617 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1618 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301619 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001620 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301621 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001622 /*
1623 * in case of an error, after clearing the doorbell,
1624 * we also need to clear the outstanding_request
1625 * field in hba
1626 */
1627 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301628 }
1629
1630 return err;
1631}
1632
1633/**
1634 * ufshcd_get_dev_cmd_tag - Get device management command tag
1635 * @hba: per-adapter instance
1636 * @tag: pointer to variable with available slot value
1637 *
1638 * Get a free slot and lock it until device management command
1639 * completes.
1640 *
1641 * Returns false if free slot is unavailable for locking, else
1642 * return true with tag value in @tag.
1643 */
1644static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1645{
1646 int tag;
1647 bool ret = false;
1648 unsigned long tmp;
1649
1650 if (!tag_out)
1651 goto out;
1652
1653 do {
1654 tmp = ~hba->lrb_in_use;
1655 tag = find_last_bit(&tmp, hba->nutrs);
1656 if (tag >= hba->nutrs)
1657 goto out;
1658 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1659
1660 *tag_out = tag;
1661 ret = true;
1662out:
1663 return ret;
1664}
1665
1666static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1667{
1668 clear_bit_unlock(tag, &hba->lrb_in_use);
1669}
1670
1671/**
1672 * ufshcd_exec_dev_cmd - API for sending device management requests
1673 * @hba - UFS hba
1674 * @cmd_type - specifies the type (NOP, Query...)
1675 * @timeout - time in seconds
1676 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301677 * NOTE: Since there is only one available tag for device management commands,
1678 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301679 */
1680static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1681 enum dev_cmd_type cmd_type, int timeout)
1682{
1683 struct ufshcd_lrb *lrbp;
1684 int err;
1685 int tag;
1686 struct completion wait;
1687 unsigned long flags;
1688
1689 /*
1690 * Get free slot, sleep if slots are unavailable.
1691 * Even though we use wait_event() which sleeps indefinitely,
1692 * the maximum wait time is bounded by SCSI request timeout.
1693 */
1694 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1695
1696 init_completion(&wait);
1697 lrbp = &hba->lrb[tag];
1698 WARN_ON(lrbp->cmd);
1699 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1700 if (unlikely(err))
1701 goto out_put_tag;
1702
1703 hba->dev_cmd.complete = &wait;
1704
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001705 /* Make sure descriptors are ready before ringing the doorbell */
1706 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301707 spin_lock_irqsave(hba->host->host_lock, flags);
1708 ufshcd_send_command(hba, tag);
1709 spin_unlock_irqrestore(hba->host->host_lock, flags);
1710
1711 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1712
1713out_put_tag:
1714 ufshcd_put_dev_cmd_tag(hba, tag);
1715 wake_up(&hba->dev_cmd.tag_wq);
1716 return err;
1717}
1718
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301719/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001720 * ufshcd_init_query() - init the query response and request parameters
1721 * @hba: per-adapter instance
1722 * @request: address of the request pointer to be initialized
1723 * @response: address of the response pointer to be initialized
1724 * @opcode: operation to perform
1725 * @idn: flag idn to access
1726 * @index: LU number to access
1727 * @selector: query/flag/descriptor further identification
1728 */
1729static inline void ufshcd_init_query(struct ufs_hba *hba,
1730 struct ufs_query_req **request, struct ufs_query_res **response,
1731 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1732{
1733 *request = &hba->dev_cmd.query.request;
1734 *response = &hba->dev_cmd.query.response;
1735 memset(*request, 0, sizeof(struct ufs_query_req));
1736 memset(*response, 0, sizeof(struct ufs_query_res));
1737 (*request)->upiu_req.opcode = opcode;
1738 (*request)->upiu_req.idn = idn;
1739 (*request)->upiu_req.index = index;
1740 (*request)->upiu_req.selector = selector;
1741}
1742
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001743static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1744 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1745{
1746 int ret;
1747 int retries;
1748
1749 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1750 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1751 if (ret)
1752 dev_dbg(hba->dev,
1753 "%s: failed with error %d, retries %d\n",
1754 __func__, ret, retries);
1755 else
1756 break;
1757 }
1758
1759 if (ret)
1760 dev_err(hba->dev,
1761 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1762 __func__, opcode, idn, ret, retries);
1763 return ret;
1764}
1765
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001766/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301767 * ufshcd_query_flag() - API function for sending flag query requests
1768 * hba: per-adapter instance
1769 * query_opcode: flag query to perform
1770 * idn: flag idn to access
1771 * flag_res: the flag value after the query request completes
1772 *
1773 * Returns 0 for success, non-zero in case of failure
1774 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001775int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301776 enum flag_idn idn, bool *flag_res)
1777{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001778 struct ufs_query_req *request = NULL;
1779 struct ufs_query_res *response = NULL;
1780 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001781 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301782
1783 BUG_ON(!hba);
1784
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001785 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301786 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001787 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1788 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301789
1790 switch (opcode) {
1791 case UPIU_QUERY_OPCODE_SET_FLAG:
1792 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1793 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1794 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1795 break;
1796 case UPIU_QUERY_OPCODE_READ_FLAG:
1797 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1798 if (!flag_res) {
1799 /* No dummy reads */
1800 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1801 __func__);
1802 err = -EINVAL;
1803 goto out_unlock;
1804 }
1805 break;
1806 default:
1807 dev_err(hba->dev,
1808 "%s: Expected query flag opcode but got = %d\n",
1809 __func__, opcode);
1810 err = -EINVAL;
1811 goto out_unlock;
1812 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301813
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001814 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1815 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1816
1817 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301818
1819 if (err) {
1820 dev_err(hba->dev,
1821 "%s: Sending flag query for idn %d failed, err = %d\n",
1822 __func__, idn, err);
1823 goto out_unlock;
1824 }
1825
1826 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301827 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301828 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1829
1830out_unlock:
1831 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001832 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301833 return err;
1834}
1835
1836/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301837 * ufshcd_query_attr - API function for sending attribute requests
1838 * hba: per-adapter instance
1839 * opcode: attribute opcode
1840 * idn: attribute idn to access
1841 * index: index field
1842 * selector: selector field
1843 * attr_val: the attribute value after the query request completes
1844 *
1845 * Returns 0 for success, non-zero in case of failure
1846*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301847static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301848 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1849{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001850 struct ufs_query_req *request = NULL;
1851 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301852 int err;
1853
1854 BUG_ON(!hba);
1855
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001856 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301857 if (!attr_val) {
1858 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1859 __func__, opcode);
1860 err = -EINVAL;
1861 goto out;
1862 }
1863
1864 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001865 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1866 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301867
1868 switch (opcode) {
1869 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1870 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301871 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301872 break;
1873 case UPIU_QUERY_OPCODE_READ_ATTR:
1874 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1875 break;
1876 default:
1877 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1878 __func__, opcode);
1879 err = -EINVAL;
1880 goto out_unlock;
1881 }
1882
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001883 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301884
1885 if (err) {
1886 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1887 __func__, opcode, idn, err);
1888 goto out_unlock;
1889 }
1890
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301891 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301892
1893out_unlock:
1894 mutex_unlock(&hba->dev_cmd.lock);
1895out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001896 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301897 return err;
1898}
1899
1900/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001901 * ufshcd_query_attr_retry() - API function for sending query
1902 * attribute with retries
1903 * @hba: per-adapter instance
1904 * @opcode: attribute opcode
1905 * @idn: attribute idn to access
1906 * @index: index field
1907 * @selector: selector field
1908 * @attr_val: the attribute value after the query request
1909 * completes
1910 *
1911 * Returns 0 for success, non-zero in case of failure
1912*/
1913static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1914 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1915 u32 *attr_val)
1916{
1917 int ret = 0;
1918 u32 retries;
1919
1920 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1921 ret = ufshcd_query_attr(hba, opcode, idn, index,
1922 selector, attr_val);
1923 if (ret)
1924 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1925 __func__, ret, retries);
1926 else
1927 break;
1928 }
1929
1930 if (ret)
1931 dev_err(hba->dev,
1932 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1933 __func__, idn, ret, QUERY_REQ_RETRIES);
1934 return ret;
1935}
1936
Yaniv Gardia70e91b2016-03-10 17:37:14 +02001937static int __ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001938 enum query_opcode opcode, enum desc_idn idn, u8 index,
1939 u8 selector, u8 *desc_buf, int *buf_len)
1940{
1941 struct ufs_query_req *request = NULL;
1942 struct ufs_query_res *response = NULL;
1943 int err;
1944
1945 BUG_ON(!hba);
1946
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001947 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001948 if (!desc_buf) {
1949 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1950 __func__, opcode);
1951 err = -EINVAL;
1952 goto out;
1953 }
1954
1955 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1956 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1957 __func__, *buf_len);
1958 err = -EINVAL;
1959 goto out;
1960 }
1961
1962 mutex_lock(&hba->dev_cmd.lock);
1963 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1964 selector);
1965 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001966 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001967
1968 switch (opcode) {
1969 case UPIU_QUERY_OPCODE_WRITE_DESC:
1970 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1971 break;
1972 case UPIU_QUERY_OPCODE_READ_DESC:
1973 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1974 break;
1975 default:
1976 dev_err(hba->dev,
1977 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1978 __func__, opcode);
1979 err = -EINVAL;
1980 goto out_unlock;
1981 }
1982
1983 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1984
1985 if (err) {
1986 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1987 __func__, opcode, idn, err);
1988 goto out_unlock;
1989 }
1990
1991 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001992 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001993
1994out_unlock:
1995 mutex_unlock(&hba->dev_cmd.lock);
1996out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001997 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001998 return err;
1999}
2000
2001/**
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002002 * ufshcd_query_descriptor_retry - API function for sending descriptor
2003 * requests
2004 * hba: per-adapter instance
2005 * opcode: attribute opcode
2006 * idn: attribute idn to access
2007 * index: index field
2008 * selector: selector field
2009 * desc_buf: the buffer that contains the descriptor
2010 * buf_len: length parameter passed to the device
2011 *
2012 * Returns 0 for success, non-zero in case of failure.
2013 * The buf_len parameter will contain, on return, the length parameter
2014 * received on the response.
2015 */
2016int ufshcd_query_descriptor_retry(struct ufs_hba *hba,
2017 enum query_opcode opcode, enum desc_idn idn, u8 index,
2018 u8 selector, u8 *desc_buf, int *buf_len)
2019{
2020 int err;
2021 int retries;
2022
2023 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2024 err = __ufshcd_query_descriptor(hba, opcode, idn, index,
2025 selector, desc_buf, buf_len);
2026 if (!err || err == -EINVAL)
2027 break;
2028 }
2029
2030 return err;
2031}
2032EXPORT_SYMBOL(ufshcd_query_descriptor_retry);
2033
2034/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002035 * ufshcd_read_desc_param - read the specified descriptor parameter
2036 * @hba: Pointer to adapter instance
2037 * @desc_id: descriptor idn value
2038 * @desc_index: descriptor index
2039 * @param_offset: offset of the parameter to read
2040 * @param_read_buf: pointer to buffer where parameter would be read
2041 * @param_size: sizeof(param_read_buf)
2042 *
2043 * Return 0 in case of success, non-zero otherwise
2044 */
2045static int ufshcd_read_desc_param(struct ufs_hba *hba,
2046 enum desc_idn desc_id,
2047 int desc_index,
2048 u32 param_offset,
2049 u8 *param_read_buf,
2050 u32 param_size)
2051{
2052 int ret;
2053 u8 *desc_buf;
2054 u32 buff_len;
2055 bool is_kmalloc = true;
2056
2057 /* safety checks */
2058 if (desc_id >= QUERY_DESC_IDN_MAX)
2059 return -EINVAL;
2060
2061 buff_len = ufs_query_desc_max_size[desc_id];
2062 if ((param_offset + param_size) > buff_len)
2063 return -EINVAL;
2064
2065 if (!param_offset && (param_size == buff_len)) {
2066 /* memory space already available to hold full descriptor */
2067 desc_buf = param_read_buf;
2068 is_kmalloc = false;
2069 } else {
2070 /* allocate memory to hold full descriptor */
2071 desc_buf = kmalloc(buff_len, GFP_KERNEL);
2072 if (!desc_buf)
2073 return -ENOMEM;
2074 }
2075
Yaniv Gardia70e91b2016-03-10 17:37:14 +02002076 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC,
2077 desc_id, desc_index, 0, desc_buf,
2078 &buff_len);
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002079
2080 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
2081 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
2082 ufs_query_desc_max_size[desc_id])
2083 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
2084 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
2085 __func__, desc_id, param_offset, buff_len, ret);
2086 if (!ret)
2087 ret = -EINVAL;
2088
2089 goto out;
2090 }
2091
2092 if (is_kmalloc)
2093 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2094out:
2095 if (is_kmalloc)
2096 kfree(desc_buf);
2097 return ret;
2098}
2099
2100static inline int ufshcd_read_desc(struct ufs_hba *hba,
2101 enum desc_idn desc_id,
2102 int desc_index,
2103 u8 *buf,
2104 u32 size)
2105{
2106 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2107}
2108
2109static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2110 u8 *buf,
2111 u32 size)
2112{
2113 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2114}
2115
Yaniv Gardib573d482016-03-10 17:37:09 +02002116int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2117{
2118 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2119}
2120EXPORT_SYMBOL(ufshcd_read_device_desc);
2121
2122/**
2123 * ufshcd_read_string_desc - read string descriptor
2124 * @hba: pointer to adapter instance
2125 * @desc_index: descriptor index
2126 * @buf: pointer to buffer where descriptor would be read
2127 * @size: size of buf
2128 * @ascii: if true convert from unicode to ascii characters
2129 *
2130 * Return 0 in case of success, non-zero otherwise
2131 */
2132int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2133 u32 size, bool ascii)
2134{
2135 int err = 0;
2136
2137 err = ufshcd_read_desc(hba,
2138 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2139
2140 if (err) {
2141 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2142 __func__, QUERY_REQ_RETRIES, err);
2143 goto out;
2144 }
2145
2146 if (ascii) {
2147 int desc_len;
2148 int ascii_len;
2149 int i;
2150 char *buff_ascii;
2151
2152 desc_len = buf[0];
2153 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2154 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2155 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2156 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2157 __func__);
2158 err = -ENOMEM;
2159 goto out;
2160 }
2161
2162 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2163 if (!buff_ascii) {
2164 err = -ENOMEM;
Tiezhu Yangfcbefc32016-06-25 12:35:22 +08002165 goto out;
Yaniv Gardib573d482016-03-10 17:37:09 +02002166 }
2167
2168 /*
2169 * the descriptor contains string in UTF16 format
2170 * we need to convert to utf-8 so it can be displayed
2171 */
2172 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2173 desc_len - QUERY_DESC_HDR_SIZE,
2174 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2175
2176 /* replace non-printable or non-ASCII characters with spaces */
2177 for (i = 0; i < ascii_len; i++)
2178 ufshcd_remove_non_printable(&buff_ascii[i]);
2179
2180 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2181 size - QUERY_DESC_HDR_SIZE);
2182 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2183 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
Yaniv Gardib573d482016-03-10 17:37:09 +02002184 kfree(buff_ascii);
2185 }
2186out:
2187 return err;
2188}
2189EXPORT_SYMBOL(ufshcd_read_string_desc);
2190
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002191/**
2192 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2193 * @hba: Pointer to adapter instance
2194 * @lun: lun id
2195 * @param_offset: offset of the parameter to read
2196 * @param_read_buf: pointer to buffer where parameter would be read
2197 * @param_size: sizeof(param_read_buf)
2198 *
2199 * Return 0 in case of success, non-zero otherwise
2200 */
2201static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2202 int lun,
2203 enum unit_desc_param param_offset,
2204 u8 *param_read_buf,
2205 u32 param_size)
2206{
2207 /*
2208 * Unit descriptors are only available for general purpose LUs (LUN id
2209 * from 0 to 7) and RPMB Well known LU.
2210 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002211 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002212 return -EOPNOTSUPP;
2213
2214 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2215 param_offset, param_read_buf, param_size);
2216}
2217
2218/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302219 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2220 * @hba: per adapter instance
2221 *
2222 * 1. Allocate DMA memory for Command Descriptor array
2223 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2224 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2225 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2226 * (UTMRDL)
2227 * 4. Allocate memory for local reference block(lrb).
2228 *
2229 * Returns 0 for success, non-zero in case of failure
2230 */
2231static int ufshcd_memory_alloc(struct ufs_hba *hba)
2232{
2233 size_t utmrdl_size, utrdl_size, ucdl_size;
2234
2235 /* Allocate memory for UTP command descriptors */
2236 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002237 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2238 ucdl_size,
2239 &hba->ucdl_dma_addr,
2240 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302241
2242 /*
2243 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2244 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2245 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2246 * be aligned to 128 bytes as well
2247 */
2248 if (!hba->ucdl_base_addr ||
2249 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302250 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302251 "Command Descriptor Memory allocation failed\n");
2252 goto out;
2253 }
2254
2255 /*
2256 * Allocate memory for UTP Transfer descriptors
2257 * UFSHCI requires 1024 byte alignment of UTRD
2258 */
2259 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002260 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2261 utrdl_size,
2262 &hba->utrdl_dma_addr,
2263 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302264 if (!hba->utrdl_base_addr ||
2265 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302266 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302267 "Transfer Descriptor Memory allocation failed\n");
2268 goto out;
2269 }
2270
2271 /*
2272 * Allocate memory for UTP Task Management descriptors
2273 * UFSHCI requires 1024 byte alignment of UTMRD
2274 */
2275 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002276 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2277 utmrdl_size,
2278 &hba->utmrdl_dma_addr,
2279 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302280 if (!hba->utmrdl_base_addr ||
2281 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302282 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302283 "Task Management Descriptor Memory allocation failed\n");
2284 goto out;
2285 }
2286
2287 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002288 hba->lrb = devm_kzalloc(hba->dev,
2289 hba->nutrs * sizeof(struct ufshcd_lrb),
2290 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302291 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302292 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302293 goto out;
2294 }
2295 return 0;
2296out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302297 return -ENOMEM;
2298}
2299
2300/**
2301 * ufshcd_host_memory_configure - configure local reference block with
2302 * memory offsets
2303 * @hba: per adapter instance
2304 *
2305 * Configure Host memory space
2306 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2307 * address.
2308 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2309 * and PRDT offset.
2310 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2311 * into local reference block.
2312 */
2313static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2314{
2315 struct utp_transfer_cmd_desc *cmd_descp;
2316 struct utp_transfer_req_desc *utrdlp;
2317 dma_addr_t cmd_desc_dma_addr;
2318 dma_addr_t cmd_desc_element_addr;
2319 u16 response_offset;
2320 u16 prdt_offset;
2321 int cmd_desc_size;
2322 int i;
2323
2324 utrdlp = hba->utrdl_base_addr;
2325 cmd_descp = hba->ucdl_base_addr;
2326
2327 response_offset =
2328 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2329 prdt_offset =
2330 offsetof(struct utp_transfer_cmd_desc, prd_table);
2331
2332 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2333 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2334
2335 for (i = 0; i < hba->nutrs; i++) {
2336 /* Configure UTRD with command descriptor base address */
2337 cmd_desc_element_addr =
2338 (cmd_desc_dma_addr + (cmd_desc_size * i));
2339 utrdlp[i].command_desc_base_addr_lo =
2340 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2341 utrdlp[i].command_desc_base_addr_hi =
2342 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2343
2344 /* Response upiu and prdt offset should be in double words */
2345 utrdlp[i].response_upiu_offset =
2346 cpu_to_le16((response_offset >> 2));
2347 utrdlp[i].prd_table_offset =
2348 cpu_to_le16((prdt_offset >> 2));
2349 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302350 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302351
2352 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302353 hba->lrb[i].ucd_req_ptr =
2354 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302355 hba->lrb[i].ucd_rsp_ptr =
2356 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2357 hba->lrb[i].ucd_prdt_ptr =
2358 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2359 }
2360}
2361
2362/**
2363 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2364 * @hba: per adapter instance
2365 *
2366 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2367 * in order to initialize the Unipro link startup procedure.
2368 * Once the Unipro links are up, the device connected to the controller
2369 * is detected.
2370 *
2371 * Returns 0 on success, non-zero value on failure
2372 */
2373static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2374{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302375 struct uic_command uic_cmd = {0};
2376 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302377
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302378 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2379
2380 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2381 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302382 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302383 "dme-link-startup: error code %d\n", ret);
2384 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302385}
2386
Yaniv Gardicad2e032015-03-31 17:37:14 +03002387static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2388{
2389 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2390 unsigned long min_sleep_time_us;
2391
2392 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2393 return;
2394
2395 /*
2396 * last_dme_cmd_tstamp will be 0 only for 1st call to
2397 * this function
2398 */
2399 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2400 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2401 } else {
2402 unsigned long delta =
2403 (unsigned long) ktime_to_us(
2404 ktime_sub(ktime_get(),
2405 hba->last_dme_cmd_tstamp));
2406
2407 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2408 min_sleep_time_us =
2409 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2410 else
2411 return; /* no more delay required */
2412 }
2413
2414 /* allow sleep for extra 50us if needed */
2415 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2416}
2417
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302418/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302419 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2420 * @hba: per adapter instance
2421 * @attr_sel: uic command argument1
2422 * @attr_set: attribute set type as uic command argument2
2423 * @mib_val: setting value as uic command argument3
2424 * @peer: indicate whether peer or local
2425 *
2426 * Returns 0 on success, non-zero value on failure
2427 */
2428int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2429 u8 attr_set, u32 mib_val, u8 peer)
2430{
2431 struct uic_command uic_cmd = {0};
2432 static const char *const action[] = {
2433 "dme-set",
2434 "dme-peer-set"
2435 };
2436 const char *set = action[!!peer];
2437 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002438 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302439
2440 uic_cmd.command = peer ?
2441 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2442 uic_cmd.argument1 = attr_sel;
2443 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2444 uic_cmd.argument3 = mib_val;
2445
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002446 do {
2447 /* for peer attributes we retry upon failure */
2448 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2449 if (ret)
2450 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2451 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2452 } while (ret && peer && --retries);
2453
2454 if (!retries)
2455 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2456 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2457 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302458
2459 return ret;
2460}
2461EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2462
2463/**
2464 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2465 * @hba: per adapter instance
2466 * @attr_sel: uic command argument1
2467 * @mib_val: the value of the attribute as returned by the UIC command
2468 * @peer: indicate whether peer or local
2469 *
2470 * Returns 0 on success, non-zero value on failure
2471 */
2472int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2473 u32 *mib_val, u8 peer)
2474{
2475 struct uic_command uic_cmd = {0};
2476 static const char *const action[] = {
2477 "dme-get",
2478 "dme-peer-get"
2479 };
2480 const char *get = action[!!peer];
2481 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002482 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002483 struct ufs_pa_layer_attr orig_pwr_info;
2484 struct ufs_pa_layer_attr temp_pwr_info;
2485 bool pwr_mode_change = false;
2486
2487 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2488 orig_pwr_info = hba->pwr_info;
2489 temp_pwr_info = orig_pwr_info;
2490
2491 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2492 orig_pwr_info.pwr_rx == FAST_MODE) {
2493 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2494 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2495 pwr_mode_change = true;
2496 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2497 orig_pwr_info.pwr_rx == SLOW_MODE) {
2498 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2499 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2500 pwr_mode_change = true;
2501 }
2502 if (pwr_mode_change) {
2503 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2504 if (ret)
2505 goto out;
2506 }
2507 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302508
2509 uic_cmd.command = peer ?
2510 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2511 uic_cmd.argument1 = attr_sel;
2512
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002513 do {
2514 /* for peer attributes we retry upon failure */
2515 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2516 if (ret)
2517 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2518 get, UIC_GET_ATTR_ID(attr_sel), ret);
2519 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302520
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002521 if (!retries)
2522 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2523 get, UIC_GET_ATTR_ID(attr_sel), retries);
2524
2525 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302526 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002527
2528 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2529 && pwr_mode_change)
2530 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302531out:
2532 return ret;
2533}
2534EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2535
2536/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002537 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2538 * state) and waits for it to take effect.
2539 *
2540 * @hba: per adapter instance
2541 * @cmd: UIC command to execute
2542 *
2543 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2544 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2545 * and device UniPro link and hence it's final completion would be indicated by
2546 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2547 * addition to normal UIC command completion Status (UCCS). This function only
2548 * returns after the relevant status bits indicate the completion.
2549 *
2550 * Returns 0 on success, non-zero value on failure
2551 */
2552static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2553{
2554 struct completion uic_async_done;
2555 unsigned long flags;
2556 u8 status;
2557 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002558 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002559
2560 mutex_lock(&hba->uic_cmd_mutex);
2561 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002562 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002563
2564 spin_lock_irqsave(hba->host->host_lock, flags);
2565 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002566 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2567 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2568 /*
2569 * Make sure UIC command completion interrupt is disabled before
2570 * issuing UIC command.
2571 */
2572 wmb();
2573 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002574 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002575 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2576 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002577 if (ret) {
2578 dev_err(hba->dev,
2579 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2580 cmd->command, cmd->argument3, ret);
2581 goto out;
2582 }
2583
2584 if (!wait_for_completion_timeout(hba->uic_async_done,
2585 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2586 dev_err(hba->dev,
2587 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2588 cmd->command, cmd->argument3);
2589 ret = -ETIMEDOUT;
2590 goto out;
2591 }
2592
2593 status = ufshcd_get_upmcrs(hba);
2594 if (status != PWR_LOCAL) {
2595 dev_err(hba->dev,
Kiwoong Kim73615422016-09-08 16:50:02 +09002596 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002597 cmd->command, status);
2598 ret = (status != PWR_OK) ? status : -1;
2599 }
2600out:
2601 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002602 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002603 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002604 if (reenable_intr)
2605 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002606 spin_unlock_irqrestore(hba->host->host_lock, flags);
2607 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002608
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002609 return ret;
2610}
2611
2612/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302613 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2614 * using DME_SET primitives.
2615 * @hba: per adapter instance
2616 * @mode: powr mode value
2617 *
2618 * Returns 0 on success, non-zero value on failure
2619 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302620static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302621{
2622 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002623 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302624
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002625 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2626 ret = ufshcd_dme_set(hba,
2627 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2628 if (ret) {
2629 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2630 __func__, ret);
2631 goto out;
2632 }
2633 }
2634
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302635 uic_cmd.command = UIC_CMD_DME_SET;
2636 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2637 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002638 ufshcd_hold(hba, false);
2639 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2640 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302641
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002642out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002643 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002644}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302645
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002646static int ufshcd_link_recovery(struct ufs_hba *hba)
2647{
2648 int ret;
2649 unsigned long flags;
2650
2651 spin_lock_irqsave(hba->host->host_lock, flags);
2652 hba->ufshcd_state = UFSHCD_STATE_RESET;
2653 ufshcd_set_eh_in_progress(hba);
2654 spin_unlock_irqrestore(hba->host->host_lock, flags);
2655
2656 ret = ufshcd_host_reset_and_restore(hba);
2657
2658 spin_lock_irqsave(hba->host->host_lock, flags);
2659 if (ret)
2660 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2661 ufshcd_clear_eh_in_progress(hba);
2662 spin_unlock_irqrestore(hba->host->host_lock, flags);
2663
2664 if (ret)
2665 dev_err(hba->dev, "%s: link recovery failed, err %d",
2666 __func__, ret);
2667
2668 return ret;
2669}
2670
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002671static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002672{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002673 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002674 struct uic_command uic_cmd = {0};
2675
2676 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002677 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002678
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002679 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002680 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2681 __func__, ret);
2682
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002683 /*
2684 * If link recovery fails then return error so that caller
2685 * don't retry the hibern8 enter again.
2686 */
2687 if (ufshcd_link_recovery(hba))
2688 ret = -ENOLINK;
2689 }
2690
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002691 return ret;
2692}
2693
2694static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2695{
2696 int ret = 0, retries;
2697
2698 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2699 ret = __ufshcd_uic_hibern8_enter(hba);
2700 if (!ret || ret == -ENOLINK)
2701 goto out;
2702 }
2703out:
2704 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002705}
2706
2707static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2708{
2709 struct uic_command uic_cmd = {0};
2710 int ret;
2711
2712 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2713 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302714 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002715 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2716 __func__, ret);
2717 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302718 }
2719
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302720 return ret;
2721}
2722
Yaniv Gardi50646362014-10-23 13:25:13 +03002723 /**
2724 * ufshcd_init_pwr_info - setting the POR (power on reset)
2725 * values in hba power info
2726 * @hba: per-adapter instance
2727 */
2728static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2729{
2730 hba->pwr_info.gear_rx = UFS_PWM_G1;
2731 hba->pwr_info.gear_tx = UFS_PWM_G1;
2732 hba->pwr_info.lane_rx = 1;
2733 hba->pwr_info.lane_tx = 1;
2734 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2735 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2736 hba->pwr_info.hs_rate = 0;
2737}
2738
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302739/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002740 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2741 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302742 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002743static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302744{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002745 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2746
2747 if (hba->max_pwr_info.is_valid)
2748 return 0;
2749
2750 pwr_info->pwr_tx = FASTAUTO_MODE;
2751 pwr_info->pwr_rx = FASTAUTO_MODE;
2752 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302753
2754 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002755 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2756 &pwr_info->lane_rx);
2757 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2758 &pwr_info->lane_tx);
2759
2760 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2761 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2762 __func__,
2763 pwr_info->lane_rx,
2764 pwr_info->lane_tx);
2765 return -EINVAL;
2766 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302767
2768 /*
2769 * First, get the maximum gears of HS speed.
2770 * If a zero value, it means there is no HSGEAR capability.
2771 * Then, get the maximum gears of PWM speed.
2772 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002773 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2774 if (!pwr_info->gear_rx) {
2775 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2776 &pwr_info->gear_rx);
2777 if (!pwr_info->gear_rx) {
2778 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2779 __func__, pwr_info->gear_rx);
2780 return -EINVAL;
2781 }
2782 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302783 }
2784
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002785 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2786 &pwr_info->gear_tx);
2787 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302788 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002789 &pwr_info->gear_tx);
2790 if (!pwr_info->gear_tx) {
2791 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2792 __func__, pwr_info->gear_tx);
2793 return -EINVAL;
2794 }
2795 pwr_info->pwr_tx = SLOWAUTO_MODE;
2796 }
2797
2798 hba->max_pwr_info.is_valid = true;
2799 return 0;
2800}
2801
2802static int ufshcd_change_power_mode(struct ufs_hba *hba,
2803 struct ufs_pa_layer_attr *pwr_mode)
2804{
2805 int ret;
2806
2807 /* if already configured to the requested pwr_mode */
2808 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2809 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2810 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2811 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2812 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2813 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2814 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2815 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2816 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302817 }
2818
2819 /*
2820 * Configure attributes for power mode change with below.
2821 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2822 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2823 * - PA_HSSERIES
2824 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002825 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2826 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2827 pwr_mode->lane_rx);
2828 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2829 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302830 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002831 else
2832 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302833
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002834 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2835 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2836 pwr_mode->lane_tx);
2837 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2838 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302839 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002840 else
2841 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302842
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002843 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2844 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2845 pwr_mode->pwr_rx == FAST_MODE ||
2846 pwr_mode->pwr_tx == FAST_MODE)
2847 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2848 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302849
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002850 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2851 | pwr_mode->pwr_tx);
2852
2853 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302854 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002855 "%s: power mode change failed %d\n", __func__, ret);
2856 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002857 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2858 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002859
2860 memcpy(&hba->pwr_info, pwr_mode,
2861 sizeof(struct ufs_pa_layer_attr));
2862 }
2863
2864 return ret;
2865}
2866
2867/**
2868 * ufshcd_config_pwr_mode - configure a new power mode
2869 * @hba: per-adapter instance
2870 * @desired_pwr_mode: desired power configuration
2871 */
2872static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2873 struct ufs_pa_layer_attr *desired_pwr_mode)
2874{
2875 struct ufs_pa_layer_attr final_params = { 0 };
2876 int ret;
2877
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002878 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2879 desired_pwr_mode, &final_params);
2880
2881 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002882 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2883
2884 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302885
2886 return ret;
2887}
2888
2889/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302890 * ufshcd_complete_dev_init() - checks device readiness
2891 * hba: per-adapter instance
2892 *
2893 * Set fDeviceInit flag and poll until device toggles it.
2894 */
2895static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2896{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002897 int i;
2898 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302899 bool flag_res = 1;
2900
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002901 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2902 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302903 if (err) {
2904 dev_err(hba->dev,
2905 "%s setting fDeviceInit flag failed with error %d\n",
2906 __func__, err);
2907 goto out;
2908 }
2909
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002910 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2911 for (i = 0; i < 1000 && !err && flag_res; i++)
2912 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2913 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2914
Dolev Raviv68078d52013-07-30 00:35:58 +05302915 if (err)
2916 dev_err(hba->dev,
2917 "%s reading fDeviceInit flag failed with error %d\n",
2918 __func__, err);
2919 else if (flag_res)
2920 dev_err(hba->dev,
2921 "%s fDeviceInit was not cleared by the device\n",
2922 __func__);
2923
2924out:
2925 return err;
2926}
2927
2928/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302929 * ufshcd_make_hba_operational - Make UFS controller operational
2930 * @hba: per adapter instance
2931 *
2932 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002933 * 1. Enable required interrupts
2934 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02002935 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002936 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302937 *
2938 * Returns 0 on success, non-zero value on failure
2939 */
2940static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2941{
2942 int err = 0;
2943 u32 reg;
2944
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302945 /* Enable required interrupts */
2946 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2947
2948 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002949 if (ufshcd_is_intr_aggr_allowed(hba))
2950 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2951 else
2952 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302953
2954 /* Configure UTRL and UTMRL base address registers */
2955 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2956 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2957 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2958 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2959 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2960 REG_UTP_TASK_REQ_LIST_BASE_L);
2961 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2962 REG_UTP_TASK_REQ_LIST_BASE_H);
2963
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302964 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02002965 * Make sure base address and interrupt setup are updated before
2966 * enabling the run/stop registers below.
2967 */
2968 wmb();
2969
2970 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302971 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302972 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002973 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302974 if (!(ufshcd_get_lists_status(reg))) {
2975 ufshcd_enable_run_stop_reg(hba);
2976 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302977 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302978 "Host controller not ready to process requests");
2979 err = -EIO;
2980 goto out;
2981 }
2982
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302983out:
2984 return err;
2985}
2986
2987/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02002988 * ufshcd_hba_stop - Send controller to reset state
2989 * @hba: per adapter instance
2990 * @can_sleep: perform sleep or just spin
2991 */
2992static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
2993{
2994 int err;
2995
2996 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
2997 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
2998 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
2999 10, 1, can_sleep);
3000 if (err)
3001 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
3002}
3003
3004/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303005 * ufshcd_hba_enable - initialize the controller
3006 * @hba: per adapter instance
3007 *
3008 * The controller resets itself and controller firmware initialization
3009 * sequence kicks off. When controller is ready it will set
3010 * the Host Controller Enable bit to 1.
3011 *
3012 * Returns 0 on success, non-zero value on failure
3013 */
3014static int ufshcd_hba_enable(struct ufs_hba *hba)
3015{
3016 int retry;
3017
3018 /*
3019 * msleep of 1 and 5 used in this function might result in msleep(20),
3020 * but it was necessary to send the UFS FPGA to reset mode during
3021 * development and testing of this driver. msleep can be changed to
3022 * mdelay and retry count can be reduced based on the controller.
3023 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003024 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303025 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02003026 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303027
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003028 /* UniPro link is disabled at this point */
3029 ufshcd_set_link_off(hba);
3030
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003031 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003032
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303033 /* start controller initialization sequence */
3034 ufshcd_hba_start(hba);
3035
3036 /*
3037 * To initialize a UFS host controller HCE bit must be set to 1.
3038 * During initialization the HCE bit value changes from 1->0->1.
3039 * When the host controller completes initialization sequence
3040 * it sets the value of HCE bit to 1. The same HCE bit is read back
3041 * to check if the controller has completed initialization sequence.
3042 * So without this delay the value HCE = 1, set in the previous
3043 * instruction might be read back.
3044 * This delay can be changed based on the controller.
3045 */
3046 msleep(1);
3047
3048 /* wait for the host controller to complete initialization */
3049 retry = 10;
3050 while (ufshcd_is_hba_active(hba)) {
3051 if (retry) {
3052 retry--;
3053 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303054 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303055 "Controller enable failed\n");
3056 return -EIO;
3057 }
3058 msleep(5);
3059 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003060
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003061 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003062 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003063
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003064 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003065
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303066 return 0;
3067}
3068
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003069static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
3070{
3071 int tx_lanes, i, err = 0;
3072
3073 if (!peer)
3074 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3075 &tx_lanes);
3076 else
3077 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
3078 &tx_lanes);
3079 for (i = 0; i < tx_lanes; i++) {
3080 if (!peer)
3081 err = ufshcd_dme_set(hba,
3082 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3083 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3084 0);
3085 else
3086 err = ufshcd_dme_peer_set(hba,
3087 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3088 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3089 0);
3090 if (err) {
3091 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3092 __func__, peer, i, err);
3093 break;
3094 }
3095 }
3096
3097 return err;
3098}
3099
3100static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3101{
3102 return ufshcd_disable_tx_lcc(hba, true);
3103}
3104
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303105/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303106 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303107 * @hba: per adapter instance
3108 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303109 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303110 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303111static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303112{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303113 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003114 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303115
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003116 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003117 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303118
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003119 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003120
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003121 /* check if device is detected by inter-connect layer */
3122 if (!ret && !ufshcd_is_device_present(hba)) {
3123 dev_err(hba->dev, "%s: Device not present\n", __func__);
3124 ret = -ENXIO;
3125 goto out;
3126 }
3127
3128 /*
3129 * DME link lost indication is only received when link is up,
3130 * but we can't be sure if the link is up until link startup
3131 * succeeds. So reset the local Uni-Pro and try again.
3132 */
3133 if (ret && ufshcd_hba_enable(hba))
3134 goto out;
3135 } while (ret && retries--);
3136
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303137 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003138 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303139 goto out;
3140
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003141 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3142 ret = ufshcd_disable_device_tx_lcc(hba);
3143 if (ret)
3144 goto out;
3145 }
3146
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003147 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003148 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3149 if (ret)
3150 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003151
3152 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303153out:
3154 if (ret)
3155 dev_err(hba->dev, "link startup failed %d\n", ret);
3156 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303157}
3158
3159/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303160 * ufshcd_verify_dev_init() - Verify device initialization
3161 * @hba: per-adapter instance
3162 *
3163 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3164 * device Transport Protocol (UTP) layer is ready after a reset.
3165 * If the UTP layer at the device side is not initialized, it may
3166 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3167 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3168 */
3169static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3170{
3171 int err = 0;
3172 int retries;
3173
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003174 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303175 mutex_lock(&hba->dev_cmd.lock);
3176 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3177 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3178 NOP_OUT_TIMEOUT);
3179
3180 if (!err || err == -ETIMEDOUT)
3181 break;
3182
3183 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3184 }
3185 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003186 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303187
3188 if (err)
3189 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3190 return err;
3191}
3192
3193/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003194 * ufshcd_set_queue_depth - set lun queue depth
3195 * @sdev: pointer to SCSI device
3196 *
3197 * Read bLUQueueDepth value and activate scsi tagged command
3198 * queueing. For WLUN, queue depth is set to 1. For best-effort
3199 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3200 * value that host can queue.
3201 */
3202static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3203{
3204 int ret = 0;
3205 u8 lun_qdepth;
3206 struct ufs_hba *hba;
3207
3208 hba = shost_priv(sdev->host);
3209
3210 lun_qdepth = hba->nutrs;
3211 ret = ufshcd_read_unit_desc_param(hba,
3212 ufshcd_scsi_to_upiu_lun(sdev->lun),
3213 UNIT_DESC_PARAM_LU_Q_DEPTH,
3214 &lun_qdepth,
3215 sizeof(lun_qdepth));
3216
3217 /* Some WLUN doesn't support unit descriptor */
3218 if (ret == -EOPNOTSUPP)
3219 lun_qdepth = 1;
3220 else if (!lun_qdepth)
3221 /* eventually, we can figure out the real queue depth */
3222 lun_qdepth = hba->nutrs;
3223 else
3224 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3225
3226 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3227 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003228 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003229}
3230
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003231/*
3232 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3233 * @hba: per-adapter instance
3234 * @lun: UFS device lun id
3235 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3236 *
3237 * Returns 0 in case of success and b_lu_write_protect status would be returned
3238 * @b_lu_write_protect parameter.
3239 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3240 * Returns -EINVAL in case of invalid parameters passed to this function.
3241 */
3242static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3243 u8 lun,
3244 u8 *b_lu_write_protect)
3245{
3246 int ret;
3247
3248 if (!b_lu_write_protect)
3249 ret = -EINVAL;
3250 /*
3251 * According to UFS device spec, RPMB LU can't be write
3252 * protected so skip reading bLUWriteProtect parameter for
3253 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3254 */
3255 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3256 ret = -ENOTSUPP;
3257 else
3258 ret = ufshcd_read_unit_desc_param(hba,
3259 lun,
3260 UNIT_DESC_PARAM_LU_WR_PROTECT,
3261 b_lu_write_protect,
3262 sizeof(*b_lu_write_protect));
3263 return ret;
3264}
3265
3266/**
3267 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3268 * status
3269 * @hba: per-adapter instance
3270 * @sdev: pointer to SCSI device
3271 *
3272 */
3273static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3274 struct scsi_device *sdev)
3275{
3276 if (hba->dev_info.f_power_on_wp_en &&
3277 !hba->dev_info.is_lu_power_on_wp) {
3278 u8 b_lu_write_protect;
3279
3280 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3281 &b_lu_write_protect) &&
3282 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3283 hba->dev_info.is_lu_power_on_wp = true;
3284 }
3285}
3286
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003287/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303288 * ufshcd_slave_alloc - handle initial SCSI device configurations
3289 * @sdev: pointer to SCSI device
3290 *
3291 * Returns success
3292 */
3293static int ufshcd_slave_alloc(struct scsi_device *sdev)
3294{
3295 struct ufs_hba *hba;
3296
3297 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303298
3299 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3300 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303301
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303302 /* allow SCSI layer to restart the device in case of errors */
3303 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003304
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003305 /* REPORT SUPPORTED OPERATION CODES is not supported */
3306 sdev->no_report_opcodes = 1;
3307
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003308
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003309 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003310
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003311 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3312
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003313 return 0;
3314}
3315
3316/**
3317 * ufshcd_change_queue_depth - change queue depth
3318 * @sdev: pointer to SCSI device
3319 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003320 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003321 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003322 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003323static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003324{
3325 struct ufs_hba *hba = shost_priv(sdev->host);
3326
3327 if (depth > hba->nutrs)
3328 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003329 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303330}
3331
3332/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003333 * ufshcd_slave_configure - adjust SCSI device configurations
3334 * @sdev: pointer to SCSI device
3335 */
3336static int ufshcd_slave_configure(struct scsi_device *sdev)
3337{
3338 struct request_queue *q = sdev->request_queue;
3339
3340 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3341 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3342
3343 return 0;
3344}
3345
3346/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303347 * ufshcd_slave_destroy - remove SCSI device configurations
3348 * @sdev: pointer to SCSI device
3349 */
3350static void ufshcd_slave_destroy(struct scsi_device *sdev)
3351{
3352 struct ufs_hba *hba;
3353
3354 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003355 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003356 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3357 unsigned long flags;
3358
3359 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003360 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003361 spin_unlock_irqrestore(hba->host->host_lock, flags);
3362 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303363}
3364
3365/**
3366 * ufshcd_task_req_compl - handle task management request completion
3367 * @hba: per adapter instance
3368 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303369 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303370 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303371 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303372 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303373static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303374{
3375 struct utp_task_req_desc *task_req_descp;
3376 struct utp_upiu_task_rsp *task_rsp_upiup;
3377 unsigned long flags;
3378 int ocs_value;
3379 int task_result;
3380
3381 spin_lock_irqsave(hba->host->host_lock, flags);
3382
3383 /* Clear completed tasks from outstanding_tasks */
3384 __clear_bit(index, &hba->outstanding_tasks);
3385
3386 task_req_descp = hba->utmrdl_base_addr;
3387 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3388
3389 if (ocs_value == OCS_SUCCESS) {
3390 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3391 task_req_descp[index].task_rsp_upiu;
Kiwoong Kim8794ee02016-09-09 08:22:22 +09003392 task_result = be32_to_cpu(task_rsp_upiup->output_param1);
3393 task_result = task_result & MASK_TM_SERVICE_RESP;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303394 if (resp)
3395 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303396 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303397 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3398 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303399 }
3400 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303401
3402 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303403}
3404
3405/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303406 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3407 * @lrb: pointer to local reference block of completed command
3408 * @scsi_status: SCSI command status
3409 *
3410 * Returns value base on SCSI command status
3411 */
3412static inline int
3413ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3414{
3415 int result = 0;
3416
3417 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303418 case SAM_STAT_CHECK_CONDITION:
3419 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303420 case SAM_STAT_GOOD:
3421 result |= DID_OK << 16 |
3422 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303423 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303424 break;
3425 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303426 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303427 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303428 ufshcd_copy_sense_data(lrbp);
3429 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303430 break;
3431 default:
3432 result |= DID_ERROR << 16;
3433 break;
3434 } /* end of switch */
3435
3436 return result;
3437}
3438
3439/**
3440 * ufshcd_transfer_rsp_status - Get overall status of the response
3441 * @hba: per adapter instance
3442 * @lrb: pointer to local reference block of completed command
3443 *
3444 * Returns result of the command to notify SCSI midlayer
3445 */
3446static inline int
3447ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3448{
3449 int result = 0;
3450 int scsi_status;
3451 int ocs;
3452
3453 /* overall command status of utrd */
3454 ocs = ufshcd_get_tr_ocs(lrbp);
3455
3456 switch (ocs) {
3457 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303458 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303459
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303460 switch (result) {
3461 case UPIU_TRANSACTION_RESPONSE:
3462 /*
3463 * get the response UPIU result to extract
3464 * the SCSI command status
3465 */
3466 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3467
3468 /*
3469 * get the result based on SCSI status response
3470 * to notify the SCSI midlayer of the command status
3471 */
3472 scsi_status = result & MASK_SCSI_STATUS;
3473 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303474
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003475 /*
3476 * Currently we are only supporting BKOPs exception
3477 * events hence we can ignore BKOPs exception event
3478 * during power management callbacks. BKOPs exception
3479 * event is not expected to be raised in runtime suspend
3480 * callback as it allows the urgent bkops.
3481 * During system suspend, we are anyway forcefully
3482 * disabling the bkops and if urgent bkops is needed
3483 * it will be enabled on system resume. Long term
3484 * solution could be to abort the system suspend if
3485 * UFS device needs urgent BKOPs.
3486 */
3487 if (!hba->pm_op_in_progress &&
3488 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303489 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303490 break;
3491 case UPIU_TRANSACTION_REJECT_UPIU:
3492 /* TODO: handle Reject UPIU Response */
3493 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303494 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303495 "Reject UPIU not fully implemented\n");
3496 break;
3497 default:
3498 result = DID_ERROR << 16;
3499 dev_err(hba->dev,
3500 "Unexpected request response code = %x\n",
3501 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303502 break;
3503 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303504 break;
3505 case OCS_ABORTED:
3506 result |= DID_ABORT << 16;
3507 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303508 case OCS_INVALID_COMMAND_STATUS:
3509 result |= DID_REQUEUE << 16;
3510 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303511 case OCS_INVALID_CMD_TABLE_ATTR:
3512 case OCS_INVALID_PRDT_ATTR:
3513 case OCS_MISMATCH_DATA_BUF_SIZE:
3514 case OCS_MISMATCH_RESP_UPIU_SIZE:
3515 case OCS_PEER_COMM_FAILURE:
3516 case OCS_FATAL_ERROR:
3517 default:
3518 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303519 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303520 "OCS error from controller = %x\n", ocs);
3521 break;
3522 } /* end of switch */
3523
3524 return result;
3525}
3526
3527/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303528 * ufshcd_uic_cmd_compl - handle completion of uic command
3529 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303530 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303531 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303532static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303533{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303534 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303535 hba->active_uic_cmd->argument2 |=
3536 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303537 hba->active_uic_cmd->argument3 =
3538 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303539 complete(&hba->active_uic_cmd->done);
3540 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303541
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003542 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3543 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303544}
3545
3546/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003547 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303548 * @hba: per adapter instance
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003549 * @completed_reqs: requests to complete
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303550 */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003551static void __ufshcd_transfer_req_compl(struct ufs_hba *hba,
3552 unsigned long completed_reqs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303553{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303554 struct ufshcd_lrb *lrbp;
3555 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303556 int result;
3557 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003558
Dolev Ravive9d501b2014-07-01 12:22:37 +03003559 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3560 lrbp = &hba->lrb[index];
3561 cmd = lrbp->cmd;
3562 if (cmd) {
3563 result = ufshcd_transfer_rsp_status(hba, lrbp);
3564 scsi_dma_unmap(cmd);
3565 cmd->result = result;
3566 /* Mark completed command as NULL in LRB */
3567 lrbp->cmd = NULL;
3568 clear_bit_unlock(index, &hba->lrb_in_use);
3569 /* Do not touch lrbp after scsi done */
3570 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003571 __ufshcd_release(hba);
Joao Pinto300bb132016-05-11 12:21:27 +01003572 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE ||
3573 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) {
Dolev Ravive9d501b2014-07-01 12:22:37 +03003574 if (hba->dev_cmd.complete)
3575 complete(hba->dev_cmd.complete);
3576 }
3577 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303578
3579 /* clear corresponding bits of completed commands */
3580 hba->outstanding_reqs ^= completed_reqs;
3581
Sahitya Tummala856b3482014-09-25 15:32:34 +03003582 ufshcd_clk_scaling_update_busy(hba);
3583
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303584 /* we might have free'd some tags above */
3585 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303586}
3587
3588/**
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003589 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3590 * @hba: per adapter instance
3591 */
3592static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3593{
3594 unsigned long completed_reqs;
3595 u32 tr_doorbell;
3596
3597 /* Resetting interrupt aggregation counters first and reading the
3598 * DOOR_BELL afterward allows us to handle all the completed requests.
3599 * In order to prevent other interrupts starvation the DB is read once
3600 * after reset. The down side of this solution is the possibility of
3601 * false interrupt if device completes another request after resetting
3602 * aggregation and before reading the DB.
3603 */
3604 if (ufshcd_is_intr_aggr_allowed(hba))
3605 ufshcd_reset_intr_aggr(hba);
3606
3607 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3608 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3609
3610 __ufshcd_transfer_req_compl(hba, completed_reqs);
3611}
3612
3613/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303614 * ufshcd_disable_ee - disable exception event
3615 * @hba: per-adapter instance
3616 * @mask: exception event to disable
3617 *
3618 * Disables exception event in the device so that the EVENT_ALERT
3619 * bit is not set.
3620 *
3621 * Returns zero on success, non-zero error value on failure.
3622 */
3623static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3624{
3625 int err = 0;
3626 u32 val;
3627
3628 if (!(hba->ee_ctrl_mask & mask))
3629 goto out;
3630
3631 val = hba->ee_ctrl_mask & ~mask;
3632 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003633 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303634 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3635 if (!err)
3636 hba->ee_ctrl_mask &= ~mask;
3637out:
3638 return err;
3639}
3640
3641/**
3642 * ufshcd_enable_ee - enable exception event
3643 * @hba: per-adapter instance
3644 * @mask: exception event to enable
3645 *
3646 * Enable corresponding exception event in the device to allow
3647 * device to alert host in critical scenarios.
3648 *
3649 * Returns zero on success, non-zero error value on failure.
3650 */
3651static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3652{
3653 int err = 0;
3654 u32 val;
3655
3656 if (hba->ee_ctrl_mask & mask)
3657 goto out;
3658
3659 val = hba->ee_ctrl_mask | mask;
3660 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003661 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303662 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3663 if (!err)
3664 hba->ee_ctrl_mask |= mask;
3665out:
3666 return err;
3667}
3668
3669/**
3670 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3671 * @hba: per-adapter instance
3672 *
3673 * Allow device to manage background operations on its own. Enabling
3674 * this might lead to inconsistent latencies during normal data transfers
3675 * as the device is allowed to manage its own way of handling background
3676 * operations.
3677 *
3678 * Returns zero on success, non-zero on failure.
3679 */
3680static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3681{
3682 int err = 0;
3683
3684 if (hba->auto_bkops_enabled)
3685 goto out;
3686
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003687 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303688 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3689 if (err) {
3690 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3691 __func__, err);
3692 goto out;
3693 }
3694
3695 hba->auto_bkops_enabled = true;
3696
3697 /* No need of URGENT_BKOPS exception from the device */
3698 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3699 if (err)
3700 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3701 __func__, err);
3702out:
3703 return err;
3704}
3705
3706/**
3707 * ufshcd_disable_auto_bkops - block device in doing background operations
3708 * @hba: per-adapter instance
3709 *
3710 * Disabling background operations improves command response latency but
3711 * has drawback of device moving into critical state where the device is
3712 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3713 * host is idle so that BKOPS are managed effectively without any negative
3714 * impacts.
3715 *
3716 * Returns zero on success, non-zero on failure.
3717 */
3718static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3719{
3720 int err = 0;
3721
3722 if (!hba->auto_bkops_enabled)
3723 goto out;
3724
3725 /*
3726 * If host assisted BKOPs is to be enabled, make sure
3727 * urgent bkops exception is allowed.
3728 */
3729 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3730 if (err) {
3731 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3732 __func__, err);
3733 goto out;
3734 }
3735
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003736 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303737 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3738 if (err) {
3739 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3740 __func__, err);
3741 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3742 goto out;
3743 }
3744
3745 hba->auto_bkops_enabled = false;
3746out:
3747 return err;
3748}
3749
3750/**
3751 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3752 * @hba: per adapter instance
3753 *
3754 * After a device reset the device may toggle the BKOPS_EN flag
3755 * to default value. The s/w tracking variables should be updated
3756 * as well. Do this by forcing enable of auto bkops.
3757 */
3758static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3759{
3760 hba->auto_bkops_enabled = false;
3761 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3762 ufshcd_enable_auto_bkops(hba);
3763}
3764
3765static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3766{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003767 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303768 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3769}
3770
3771/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003772 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3773 * @hba: per-adapter instance
3774 * @status: bkops_status value
3775 *
3776 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3777 * flag in the device to permit background operations if the device
3778 * bkops_status is greater than or equal to "status" argument passed to
3779 * this function, disable otherwise.
3780 *
3781 * Returns 0 for success, non-zero in case of failure.
3782 *
3783 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3784 * to know whether auto bkops is enabled or disabled after this function
3785 * returns control to it.
3786 */
3787static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3788 enum bkops_status status)
3789{
3790 int err;
3791 u32 curr_status = 0;
3792
3793 err = ufshcd_get_bkops_status(hba, &curr_status);
3794 if (err) {
3795 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3796 __func__, err);
3797 goto out;
3798 } else if (curr_status > BKOPS_STATUS_MAX) {
3799 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3800 __func__, curr_status);
3801 err = -EINVAL;
3802 goto out;
3803 }
3804
3805 if (curr_status >= status)
3806 err = ufshcd_enable_auto_bkops(hba);
3807 else
3808 err = ufshcd_disable_auto_bkops(hba);
3809out:
3810 return err;
3811}
3812
3813/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303814 * ufshcd_urgent_bkops - handle urgent bkops exception event
3815 * @hba: per-adapter instance
3816 *
3817 * Enable fBackgroundOpsEn flag in the device to permit background
3818 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003819 *
3820 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3821 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303822 */
3823static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3824{
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003825 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303826}
3827
3828static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3829{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003830 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303831 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3832}
3833
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003834static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba)
3835{
3836 int err;
3837 u32 curr_status = 0;
3838
3839 if (hba->is_urgent_bkops_lvl_checked)
3840 goto enable_auto_bkops;
3841
3842 err = ufshcd_get_bkops_status(hba, &curr_status);
3843 if (err) {
3844 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3845 __func__, err);
3846 goto out;
3847 }
3848
3849 /*
3850 * We are seeing that some devices are raising the urgent bkops
3851 * exception events even when BKOPS status doesn't indicate performace
3852 * impacted or critical. Handle these device by determining their urgent
3853 * bkops status at runtime.
3854 */
3855 if (curr_status < BKOPS_STATUS_PERF_IMPACT) {
3856 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n",
3857 __func__, curr_status);
3858 /* update the current status as the urgent bkops level */
3859 hba->urgent_bkops_lvl = curr_status;
3860 hba->is_urgent_bkops_lvl_checked = true;
3861 }
3862
3863enable_auto_bkops:
3864 err = ufshcd_enable_auto_bkops(hba);
3865out:
3866 if (err < 0)
3867 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3868 __func__, err);
3869}
3870
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303871/**
3872 * ufshcd_exception_event_handler - handle exceptions raised by device
3873 * @work: pointer to work data
3874 *
3875 * Read bExceptionEventStatus attribute from the device and handle the
3876 * exception event accordingly.
3877 */
3878static void ufshcd_exception_event_handler(struct work_struct *work)
3879{
3880 struct ufs_hba *hba;
3881 int err;
3882 u32 status = 0;
3883 hba = container_of(work, struct ufs_hba, eeh_work);
3884
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303885 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303886 err = ufshcd_get_ee_status(hba, &status);
3887 if (err) {
3888 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3889 __func__, err);
3890 goto out;
3891 }
3892
3893 status &= hba->ee_ctrl_mask;
Yaniv Gardiafdfff52016-03-10 17:37:15 +02003894
3895 if (status & MASK_EE_URGENT_BKOPS)
3896 ufshcd_bkops_exception_event_handler(hba);
3897
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303898out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303899 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303900 return;
3901}
3902
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003903/* Complete requests that have door-bell cleared */
3904static void ufshcd_complete_requests(struct ufs_hba *hba)
3905{
3906 ufshcd_transfer_req_compl(hba);
3907 ufshcd_tmc_handler(hba);
3908}
3909
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303910/**
Yaniv Gardi583fa622016-03-10 17:37:13 +02003911 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
3912 * to recover from the DL NAC errors or not.
3913 * @hba: per-adapter instance
3914 *
3915 * Returns true if error handling is required, false otherwise
3916 */
3917static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba)
3918{
3919 unsigned long flags;
3920 bool err_handling = true;
3921
3922 spin_lock_irqsave(hba->host->host_lock, flags);
3923 /*
3924 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
3925 * device fatal error and/or DL NAC & REPLAY timeout errors.
3926 */
3927 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR))
3928 goto out;
3929
3930 if ((hba->saved_err & DEVICE_FATAL_ERROR) ||
3931 ((hba->saved_err & UIC_ERROR) &&
3932 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR)))
3933 goto out;
3934
3935 if ((hba->saved_err & UIC_ERROR) &&
3936 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) {
3937 int err;
3938 /*
3939 * wait for 50ms to see if we can get any other errors or not.
3940 */
3941 spin_unlock_irqrestore(hba->host->host_lock, flags);
3942 msleep(50);
3943 spin_lock_irqsave(hba->host->host_lock, flags);
3944
3945 /*
3946 * now check if we have got any other severe errors other than
3947 * DL NAC error?
3948 */
3949 if ((hba->saved_err & INT_FATAL_ERRORS) ||
3950 ((hba->saved_err & UIC_ERROR) &&
3951 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)))
3952 goto out;
3953
3954 /*
3955 * As DL NAC is the only error received so far, send out NOP
3956 * command to confirm if link is still active or not.
3957 * - If we don't get any response then do error recovery.
3958 * - If we get response then clear the DL NAC error bit.
3959 */
3960
3961 spin_unlock_irqrestore(hba->host->host_lock, flags);
3962 err = ufshcd_verify_dev_init(hba);
3963 spin_lock_irqsave(hba->host->host_lock, flags);
3964
3965 if (err)
3966 goto out;
3967
3968 /* Link seems to be alive hence ignore the DL NAC errors */
3969 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)
3970 hba->saved_err &= ~UIC_ERROR;
3971 /* clear NAC error */
3972 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
3973 if (!hba->saved_uic_err) {
3974 err_handling = false;
3975 goto out;
3976 }
3977 }
3978out:
3979 spin_unlock_irqrestore(hba->host->host_lock, flags);
3980 return err_handling;
3981}
3982
3983/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303984 * ufshcd_err_handler - handle UFS errors that require s/w attention
3985 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303986 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303987static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303988{
3989 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303990 unsigned long flags;
3991 u32 err_xfer = 0;
3992 u32 err_tm = 0;
3993 int err = 0;
3994 int tag;
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02003995 bool needs_reset = false;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303996
3997 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303998
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303999 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004000 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304001
4002 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004003 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304004 goto out;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304005
4006 hba->ufshcd_state = UFSHCD_STATE_RESET;
4007 ufshcd_set_eh_in_progress(hba);
4008
4009 /* Complete requests that have door-bell cleared by h/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004010 ufshcd_complete_requests(hba);
Yaniv Gardi583fa622016-03-10 17:37:13 +02004011
4012 if (hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4013 bool ret;
4014
4015 spin_unlock_irqrestore(hba->host->host_lock, flags);
4016 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
4017 ret = ufshcd_quirk_dl_nac_errors(hba);
4018 spin_lock_irqsave(hba->host->host_lock, flags);
4019 if (!ret)
4020 goto skip_err_handling;
4021 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004022 if ((hba->saved_err & INT_FATAL_ERRORS) ||
4023 ((hba->saved_err & UIC_ERROR) &&
4024 (hba->saved_uic_err & (UFSHCD_UIC_DL_PA_INIT_ERROR |
4025 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR |
4026 UFSHCD_UIC_DL_TCx_REPLAY_ERROR))))
4027 needs_reset = true;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304028
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004029 /*
4030 * if host reset is required then skip clearing the pending
4031 * transfers forcefully because they will automatically get
4032 * cleared after link startup.
4033 */
4034 if (needs_reset)
4035 goto skip_pending_xfer_clear;
4036
4037 /* release lock as clear command might sleep */
4038 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304039 /* Clear pending transfer requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004040 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) {
4041 if (ufshcd_clear_cmd(hba, tag)) {
4042 err_xfer = true;
4043 goto lock_skip_pending_xfer_clear;
4044 }
4045 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304046
4047 /* Clear pending task management requests */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004048 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) {
4049 if (ufshcd_clear_tm_cmd(hba, tag)) {
4050 err_tm = true;
4051 goto lock_skip_pending_xfer_clear;
4052 }
4053 }
4054
4055lock_skip_pending_xfer_clear:
4056 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304057
4058 /* Complete the requests that are cleared by s/w */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004059 ufshcd_complete_requests(hba);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304060
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004061 if (err_xfer || err_tm)
4062 needs_reset = true;
4063
4064skip_pending_xfer_clear:
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304065 /* Fatal errors need reset */
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004066 if (needs_reset) {
4067 unsigned long max_doorbells = (1UL << hba->nutrs) - 1;
4068
4069 /*
4070 * ufshcd_reset_and_restore() does the link reinitialization
4071 * which will need atleast one empty doorbell slot to send the
4072 * device management commands (NOP and query commands).
4073 * If there is no slot empty at this moment then free up last
4074 * slot forcefully.
4075 */
4076 if (hba->outstanding_reqs == max_doorbells)
4077 __ufshcd_transfer_req_compl(hba,
4078 (1UL << (hba->nutrs - 1)));
4079
4080 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304081 err = ufshcd_reset_and_restore(hba);
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004082 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304083 if (err) {
4084 dev_err(hba->dev, "%s: reset and restore failed\n",
4085 __func__);
4086 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4087 }
4088 /*
4089 * Inform scsi mid-layer that we did reset and allow to handle
4090 * Unit Attention properly.
4091 */
4092 scsi_report_bus_reset(hba->host, 0);
4093 hba->saved_err = 0;
4094 hba->saved_uic_err = 0;
4095 }
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004096
Yaniv Gardi583fa622016-03-10 17:37:13 +02004097skip_err_handling:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004098 if (!needs_reset) {
4099 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4100 if (hba->saved_err || hba->saved_uic_err)
4101 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
4102 __func__, hba->saved_err, hba->saved_uic_err);
4103 }
4104
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304105 ufshcd_clear_eh_in_progress(hba);
4106
4107out:
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004108 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304109 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004110 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05304111 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304112}
4113
4114/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304115 * ufshcd_update_uic_error - check and set fatal UIC error flags.
4116 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304117 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304118static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304119{
4120 u32 reg;
4121
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304122 /* PA_INIT_ERROR is fatal and needs UIC reset */
4123 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
4124 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
4125 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
Yaniv Gardi583fa622016-03-10 17:37:13 +02004126 else if (hba->dev_quirks &
4127 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) {
4128 if (reg & UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED)
4129 hba->uic_error |=
4130 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR;
4131 else if (reg & UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT)
4132 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR;
4133 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304134
4135 /* UIC NL/TL/DME errors needs software retry */
4136 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
4137 if (reg)
4138 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
4139
4140 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
4141 if (reg)
4142 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
4143
4144 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
4145 if (reg)
4146 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
4147
4148 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
4149 __func__, hba->uic_error);
4150}
4151
4152/**
4153 * ufshcd_check_errors - Check for errors that need s/w attention
4154 * @hba: per-adapter instance
4155 */
4156static void ufshcd_check_errors(struct ufs_hba *hba)
4157{
4158 bool queue_eh_work = false;
4159
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304160 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304161 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304162
4163 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304164 hba->uic_error = 0;
4165 ufshcd_update_uic_error(hba);
4166 if (hba->uic_error)
4167 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304168 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304169
4170 if (queue_eh_work) {
Yaniv Gardi9a47ec72016-03-10 17:37:12 +02004171 /*
4172 * update the transfer error masks to sticky bits, let's do this
4173 * irrespective of current ufshcd_state.
4174 */
4175 hba->saved_err |= hba->errors;
4176 hba->saved_uic_err |= hba->uic_error;
4177
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304178 /* handle fatal errors only when link is functional */
4179 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
4180 /* block commands from scsi mid-layer */
4181 scsi_block_requests(hba->host);
4182
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304183 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4184 schedule_work(&hba->eh_work);
4185 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304186 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304187 /*
4188 * if (!queue_eh_work) -
4189 * Other errors are either non-fatal where host recovers
4190 * itself without s/w intervention or errors that will be
4191 * handled by the SCSI core layer.
4192 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304193}
4194
4195/**
4196 * ufshcd_tmc_handler - handle task management function completion
4197 * @hba: per adapter instance
4198 */
4199static void ufshcd_tmc_handler(struct ufs_hba *hba)
4200{
4201 u32 tm_doorbell;
4202
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304203 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304204 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304205 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304206}
4207
4208/**
4209 * ufshcd_sl_intr - Interrupt service routine
4210 * @hba: per adapter instance
4211 * @intr_status: contains interrupts generated by the controller
4212 */
4213static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
4214{
4215 hba->errors = UFSHCD_ERROR_MASK & intr_status;
4216 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304217 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304218
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05304219 if (intr_status & UFSHCD_UIC_MASK)
4220 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304221
4222 if (intr_status & UTP_TASK_REQ_COMPL)
4223 ufshcd_tmc_handler(hba);
4224
4225 if (intr_status & UTP_TRANSFER_REQ_COMPL)
4226 ufshcd_transfer_req_compl(hba);
4227}
4228
4229/**
4230 * ufshcd_intr - Main interrupt service routine
4231 * @irq: irq number
4232 * @__hba: pointer to adapter instance
4233 *
4234 * Returns IRQ_HANDLED - If interrupt is valid
4235 * IRQ_NONE - If invalid interrupt
4236 */
4237static irqreturn_t ufshcd_intr(int irq, void *__hba)
4238{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004239 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304240 irqreturn_t retval = IRQ_NONE;
4241 struct ufs_hba *hba = __hba;
4242
4243 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304244 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004245 enabled_intr_status =
4246 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304247
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004248 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05304249 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02004250
4251 if (enabled_intr_status) {
4252 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304253 retval = IRQ_HANDLED;
4254 }
4255 spin_unlock(hba->host->host_lock);
4256 return retval;
4257}
4258
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304259static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
4260{
4261 int err = 0;
4262 u32 mask = 1 << tag;
4263 unsigned long flags;
4264
4265 if (!test_bit(tag, &hba->outstanding_tasks))
4266 goto out;
4267
4268 spin_lock_irqsave(hba->host->host_lock, flags);
4269 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
4270 spin_unlock_irqrestore(hba->host->host_lock, flags);
4271
4272 /* poll for max. 1 sec to clear door bell register by h/w */
4273 err = ufshcd_wait_for_register(hba,
4274 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02004275 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304276out:
4277 return err;
4278}
4279
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304280/**
4281 * ufshcd_issue_tm_cmd - issues task management commands to controller
4282 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304283 * @lun_id: LUN ID to which TM command is sent
4284 * @task_id: task ID to which the TM command is applicable
4285 * @tm_function: task management function opcode
4286 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304287 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304288 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304289 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304290static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4291 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304292{
4293 struct utp_task_req_desc *task_req_descp;
4294 struct utp_upiu_task_req *task_req_upiup;
4295 struct Scsi_Host *host;
4296 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304297 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304298 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304299 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304300
4301 host = hba->host;
4302
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304303 /*
4304 * Get free slot, sleep if slots are unavailable.
4305 * Even though we use wait_event() which sleeps indefinitely,
4306 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4307 */
4308 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004309 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304310
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304311 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304312 task_req_descp = hba->utmrdl_base_addr;
4313 task_req_descp += free_slot;
4314
4315 /* Configure task request descriptor */
4316 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4317 task_req_descp->header.dword_2 =
4318 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4319
4320 /* Configure task request UPIU */
4321 task_req_upiup =
4322 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304323 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304324 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304325 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304326 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304327 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304328 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004329 /*
4330 * The host shall provide the same value for LUN field in the basic
4331 * header and for Input Parameter.
4332 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304333 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4334 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304335
4336 /* send command to the controller */
4337 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004338
4339 /* Make sure descriptors are ready before ringing the task doorbell */
4340 wmb();
4341
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304342 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Gilad Bronerad1a1b92016-10-17 17:09:36 -07004343 /* Make sure that doorbell is committed immediately */
4344 wmb();
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304345
4346 spin_unlock_irqrestore(host->host_lock, flags);
4347
4348 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304349 err = wait_event_timeout(hba->tm_wq,
4350 test_bit(free_slot, &hba->tm_condition),
4351 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304352 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304353 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4354 __func__, tm_function);
4355 if (ufshcd_clear_tm_cmd(hba, free_slot))
4356 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4357 __func__, free_slot);
4358 err = -ETIMEDOUT;
4359 } else {
4360 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304361 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304362
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304363 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304364 ufshcd_put_tm_slot(hba, free_slot);
4365 wake_up(&hba->tm_tag_wq);
4366
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004367 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304368 return err;
4369}
4370
4371/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304372 * ufshcd_eh_device_reset_handler - device reset handler registered to
4373 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304374 * @cmd: SCSI command pointer
4375 *
4376 * Returns SUCCESS/FAILED
4377 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304378static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304379{
4380 struct Scsi_Host *host;
4381 struct ufs_hba *hba;
4382 unsigned int tag;
4383 u32 pos;
4384 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304385 u8 resp = 0xF;
4386 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304387 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304388
4389 host = cmd->device->host;
4390 hba = shost_priv(host);
4391 tag = cmd->request->tag;
4392
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304393 lrbp = &hba->lrb[tag];
4394 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4395 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304396 if (!err)
4397 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304398 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304399 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304400
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304401 /* clear the commands that were pending for corresponding LUN */
4402 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4403 if (hba->lrb[pos].lun == lrbp->lun) {
4404 err = ufshcd_clear_cmd(hba, pos);
4405 if (err)
4406 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304407 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304408 }
4409 spin_lock_irqsave(host->host_lock, flags);
4410 ufshcd_transfer_req_compl(hba);
4411 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304412out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304413 if (!err) {
4414 err = SUCCESS;
4415 } else {
4416 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4417 err = FAILED;
4418 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304419 return err;
4420}
4421
4422/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304423 * ufshcd_abort - abort a specific command
4424 * @cmd: SCSI command pointer
4425 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304426 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4427 * command, and in host controller by clearing the door-bell register. There can
4428 * be race between controller sending the command to the device while abort is
4429 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4430 * really issued and then try to abort it.
4431 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304432 * Returns SUCCESS/FAILED
4433 */
4434static int ufshcd_abort(struct scsi_cmnd *cmd)
4435{
4436 struct Scsi_Host *host;
4437 struct ufs_hba *hba;
4438 unsigned long flags;
4439 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304440 int err = 0;
4441 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304442 u8 resp = 0xF;
4443 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004444 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304445
4446 host = cmd->device->host;
4447 hba = shost_priv(host);
4448 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004449 if (!ufshcd_valid_tag(hba, tag)) {
4450 dev_err(hba->dev,
4451 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4452 __func__, tag, cmd, cmd->request);
4453 BUG();
4454 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304455
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004456 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004457 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004458 /* If command is already aborted/completed, return SUCCESS */
4459 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4460 dev_err(hba->dev,
4461 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4462 __func__, tag, hba->outstanding_reqs, reg);
4463 goto out;
4464 }
4465
Dolev Ravive9d501b2014-07-01 12:22:37 +03004466 if (!(reg & (1 << tag))) {
4467 dev_err(hba->dev,
4468 "%s: cmd was completed, but without a notifying intr, tag = %d",
4469 __func__, tag);
4470 }
4471
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304472 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304473 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4474 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4475 UFS_QUERY_TASK, &resp);
4476 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4477 /* cmd pending in the device */
4478 break;
4479 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304480 /*
4481 * cmd not pending in the device, check if it is
4482 * in transition.
4483 */
4484 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4485 if (reg & (1 << tag)) {
4486 /* sleep for max. 200us to stabilize */
4487 usleep_range(100, 200);
4488 continue;
4489 }
4490 /* command completed already */
4491 goto out;
4492 } else {
4493 if (!err)
4494 err = resp; /* service response error */
4495 goto out;
4496 }
4497 }
4498
4499 if (!poll_cnt) {
4500 err = -EBUSY;
4501 goto out;
4502 }
4503
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304504 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4505 UFS_ABORT_TASK, &resp);
4506 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304507 if (!err)
4508 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304509 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304510 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304511
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304512 err = ufshcd_clear_cmd(hba, tag);
4513 if (err)
4514 goto out;
4515
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304516 scsi_dma_unmap(cmd);
4517
4518 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004519 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304520 hba->lrb[tag].cmd = NULL;
4521 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304522
4523 clear_bit_unlock(tag, &hba->lrb_in_use);
4524 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004525
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304526out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304527 if (!err) {
4528 err = SUCCESS;
4529 } else {
4530 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4531 err = FAILED;
4532 }
4533
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004534 /*
4535 * This ufshcd_release() corresponds to the original scsi cmd that got
4536 * aborted here (as we won't get any IRQ for it).
4537 */
4538 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304539 return err;
4540}
4541
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304542/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304543 * ufshcd_host_reset_and_restore - reset and restore host controller
4544 * @hba: per-adapter instance
4545 *
4546 * Note that host controller reset may issue DME_RESET to
4547 * local and remote (device) Uni-Pro stack and the attributes
4548 * are reset to default state.
4549 *
4550 * Returns zero on success, non-zero on failure
4551 */
4552static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4553{
4554 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304555 unsigned long flags;
4556
4557 /* Reset the host controller */
4558 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004559 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304560 spin_unlock_irqrestore(hba->host->host_lock, flags);
4561
4562 err = ufshcd_hba_enable(hba);
4563 if (err)
4564 goto out;
4565
4566 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004567 err = ufshcd_probe_hba(hba);
4568
4569 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304570 err = -EIO;
4571out:
4572 if (err)
4573 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4574
4575 return err;
4576}
4577
4578/**
4579 * ufshcd_reset_and_restore - reset and re-initialize host/device
4580 * @hba: per-adapter instance
4581 *
4582 * Reset and recover device, host and re-establish link. This
4583 * is helpful to recover the communication in fatal error conditions.
4584 *
4585 * Returns zero on success, non-zero on failure
4586 */
4587static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4588{
4589 int err = 0;
4590 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004591 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304592
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004593 do {
4594 err = ufshcd_host_reset_and_restore(hba);
4595 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304596
4597 /*
4598 * After reset the door-bell might be cleared, complete
4599 * outstanding requests in s/w here.
4600 */
4601 spin_lock_irqsave(hba->host->host_lock, flags);
4602 ufshcd_transfer_req_compl(hba);
4603 ufshcd_tmc_handler(hba);
4604 spin_unlock_irqrestore(hba->host->host_lock, flags);
4605
4606 return err;
4607}
4608
4609/**
4610 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4611 * @cmd - SCSI command pointer
4612 *
4613 * Returns SUCCESS/FAILED
4614 */
4615static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4616{
4617 int err;
4618 unsigned long flags;
4619 struct ufs_hba *hba;
4620
4621 hba = shost_priv(cmd->device->host);
4622
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004623 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304624 /*
4625 * Check if there is any race with fatal error handling.
4626 * If so, wait for it to complete. Even though fatal error
4627 * handling does reset and restore in some cases, don't assume
4628 * anything out of it. We are just avoiding race here.
4629 */
4630 do {
4631 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304632 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304633 hba->ufshcd_state == UFSHCD_STATE_RESET))
4634 break;
4635 spin_unlock_irqrestore(hba->host->host_lock, flags);
4636 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304637 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304638 } while (1);
4639
4640 hba->ufshcd_state = UFSHCD_STATE_RESET;
4641 ufshcd_set_eh_in_progress(hba);
4642 spin_unlock_irqrestore(hba->host->host_lock, flags);
4643
4644 err = ufshcd_reset_and_restore(hba);
4645
4646 spin_lock_irqsave(hba->host->host_lock, flags);
4647 if (!err) {
4648 err = SUCCESS;
4649 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4650 } else {
4651 err = FAILED;
4652 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4653 }
4654 ufshcd_clear_eh_in_progress(hba);
4655 spin_unlock_irqrestore(hba->host->host_lock, flags);
4656
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004657 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304658 return err;
4659}
4660
4661/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004662 * ufshcd_get_max_icc_level - calculate the ICC level
4663 * @sup_curr_uA: max. current supported by the regulator
4664 * @start_scan: row at the desc table to start scan from
4665 * @buff: power descriptor buffer
4666 *
4667 * Returns calculated max ICC level for specific regulator
4668 */
4669static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4670{
4671 int i;
4672 int curr_uA;
4673 u16 data;
4674 u16 unit;
4675
4676 for (i = start_scan; i >= 0; i--) {
4677 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4678 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4679 ATTR_ICC_LVL_UNIT_OFFSET;
4680 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4681 switch (unit) {
4682 case UFSHCD_NANO_AMP:
4683 curr_uA = curr_uA / 1000;
4684 break;
4685 case UFSHCD_MILI_AMP:
4686 curr_uA = curr_uA * 1000;
4687 break;
4688 case UFSHCD_AMP:
4689 curr_uA = curr_uA * 1000 * 1000;
4690 break;
4691 case UFSHCD_MICRO_AMP:
4692 default:
4693 break;
4694 }
4695 if (sup_curr_uA >= curr_uA)
4696 break;
4697 }
4698 if (i < 0) {
4699 i = 0;
4700 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4701 }
4702
4703 return (u32)i;
4704}
4705
4706/**
4707 * ufshcd_calc_icc_level - calculate the max ICC level
4708 * In case regulators are not initialized we'll return 0
4709 * @hba: per-adapter instance
4710 * @desc_buf: power descriptor buffer to extract ICC levels from.
4711 * @len: length of desc_buff
4712 *
4713 * Returns calculated ICC level
4714 */
4715static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4716 u8 *desc_buf, int len)
4717{
4718 u32 icc_level = 0;
4719
4720 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4721 !hba->vreg_info.vccq2) {
4722 dev_err(hba->dev,
4723 "%s: Regulator capability was not set, actvIccLevel=%d",
4724 __func__, icc_level);
4725 goto out;
4726 }
4727
4728 if (hba->vreg_info.vcc)
4729 icc_level = ufshcd_get_max_icc_level(
4730 hba->vreg_info.vcc->max_uA,
4731 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4732 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4733
4734 if (hba->vreg_info.vccq)
4735 icc_level = ufshcd_get_max_icc_level(
4736 hba->vreg_info.vccq->max_uA,
4737 icc_level,
4738 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4739
4740 if (hba->vreg_info.vccq2)
4741 icc_level = ufshcd_get_max_icc_level(
4742 hba->vreg_info.vccq2->max_uA,
4743 icc_level,
4744 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4745out:
4746 return icc_level;
4747}
4748
4749static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4750{
4751 int ret;
4752 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4753 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4754
4755 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4756 if (ret) {
4757 dev_err(hba->dev,
4758 "%s: Failed reading power descriptor.len = %d ret = %d",
4759 __func__, buff_len, ret);
4760 return;
4761 }
4762
4763 hba->init_prefetch_data.icc_level =
4764 ufshcd_find_max_sup_active_icc_level(hba,
4765 desc_buf, buff_len);
4766 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4767 __func__, hba->init_prefetch_data.icc_level);
4768
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004769 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4770 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4771 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004772
4773 if (ret)
4774 dev_err(hba->dev,
4775 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4776 __func__, hba->init_prefetch_data.icc_level , ret);
4777
4778}
4779
4780/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004781 * ufshcd_scsi_add_wlus - Adds required W-LUs
4782 * @hba: per-adapter instance
4783 *
4784 * UFS device specification requires the UFS devices to support 4 well known
4785 * logical units:
4786 * "REPORT_LUNS" (address: 01h)
4787 * "UFS Device" (address: 50h)
4788 * "RPMB" (address: 44h)
4789 * "BOOT" (address: 30h)
4790 * UFS device's power management needs to be controlled by "POWER CONDITION"
4791 * field of SSU (START STOP UNIT) command. But this "power condition" field
4792 * will take effect only when its sent to "UFS device" well known logical unit
4793 * hence we require the scsi_device instance to represent this logical unit in
4794 * order for the UFS host driver to send the SSU command for power management.
4795
4796 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4797 * Block) LU so user space process can control this LU. User space may also
4798 * want to have access to BOOT LU.
4799
4800 * This function adds scsi device instances for each of all well known LUs
4801 * (except "REPORT LUNS" LU).
4802 *
4803 * Returns zero on success (all required W-LUs are added successfully),
4804 * non-zero error value on failure (if failed to add any of the required W-LU).
4805 */
4806static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4807{
4808 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004809 struct scsi_device *sdev_rpmb;
4810 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004811
4812 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4813 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4814 if (IS_ERR(hba->sdev_ufs_device)) {
4815 ret = PTR_ERR(hba->sdev_ufs_device);
4816 hba->sdev_ufs_device = NULL;
4817 goto out;
4818 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004819 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004820
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004821 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004822 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004823 if (IS_ERR(sdev_boot)) {
4824 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004825 goto remove_sdev_ufs_device;
4826 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004827 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004828
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004829 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004830 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004831 if (IS_ERR(sdev_rpmb)) {
4832 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004833 goto remove_sdev_boot;
4834 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004835 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004836 goto out;
4837
4838remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004839 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004840remove_sdev_ufs_device:
4841 scsi_remove_device(hba->sdev_ufs_device);
4842out:
4843 return ret;
4844}
4845
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004846static int ufs_get_device_info(struct ufs_hba *hba,
4847 struct ufs_device_info *card_data)
4848{
4849 int err;
4850 u8 model_index;
4851 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
4852 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
4853
4854 err = ufshcd_read_device_desc(hba, desc_buf,
4855 QUERY_DESC_DEVICE_MAX_SIZE);
4856 if (err) {
4857 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4858 __func__, err);
4859 goto out;
4860 }
4861
4862 /*
4863 * getting vendor (manufacturerID) and Bank Index in big endian
4864 * format
4865 */
4866 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
4867 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4868
4869 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4870
4871 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
4872 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
4873 if (err) {
4874 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
4875 __func__, err);
4876 goto out;
4877 }
4878
4879 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
4880 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
4881 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
4882 MAX_MODEL_LEN));
4883
4884 /* Null terminate the model string */
4885 card_data->model[MAX_MODEL_LEN] = '\0';
4886
4887out:
4888 return err;
4889}
4890
4891void ufs_advertise_fixup_device(struct ufs_hba *hba)
4892{
4893 int err;
4894 struct ufs_dev_fix *f;
4895 struct ufs_device_info card_data;
4896
4897 card_data.wmanufacturerid = 0;
4898
4899 err = ufs_get_device_info(hba, &card_data);
4900 if (err) {
4901 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
4902 __func__, err);
4903 return;
4904 }
4905
4906 for (f = ufs_fixups; f->quirk; f++) {
4907 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
4908 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
4909 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
4910 !strcmp(f->card.model, UFS_ANY_MODEL)))
4911 hba->dev_quirks |= f->quirk;
4912 }
4913}
4914
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004915/**
Yaniv Gardi37113102016-03-10 17:37:16 +02004916 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
4917 * @hba: per-adapter instance
4918 *
4919 * PA_TActivate parameter can be tuned manually if UniPro version is less than
4920 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
4921 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
4922 * the hibern8 exit latency.
4923 *
4924 * Returns zero on success, non-zero error value on failure.
4925 */
4926static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba)
4927{
4928 int ret = 0;
4929 u32 peer_rx_min_activatetime = 0, tuned_pa_tactivate;
4930
4931 ret = ufshcd_dme_peer_get(hba,
4932 UIC_ARG_MIB_SEL(
4933 RX_MIN_ACTIVATETIME_CAPABILITY,
4934 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
4935 &peer_rx_min_activatetime);
4936 if (ret)
4937 goto out;
4938
4939 /* make sure proper unit conversion is applied */
4940 tuned_pa_tactivate =
4941 ((peer_rx_min_activatetime * RX_MIN_ACTIVATETIME_UNIT_US)
4942 / PA_TACTIVATE_TIME_UNIT_US);
4943 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE),
4944 tuned_pa_tactivate);
4945
4946out:
4947 return ret;
4948}
4949
4950/**
4951 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
4952 * @hba: per-adapter instance
4953 *
4954 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
4955 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
4956 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
4957 * This optimal value can help reduce the hibern8 exit latency.
4958 *
4959 * Returns zero on success, non-zero error value on failure.
4960 */
4961static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba)
4962{
4963 int ret = 0;
4964 u32 local_tx_hibern8_time_cap = 0, peer_rx_hibern8_time_cap = 0;
4965 u32 max_hibern8_time, tuned_pa_hibern8time;
4966
4967 ret = ufshcd_dme_get(hba,
4968 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY,
4969 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
4970 &local_tx_hibern8_time_cap);
4971 if (ret)
4972 goto out;
4973
4974 ret = ufshcd_dme_peer_get(hba,
4975 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY,
4976 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
4977 &peer_rx_hibern8_time_cap);
4978 if (ret)
4979 goto out;
4980
4981 max_hibern8_time = max(local_tx_hibern8_time_cap,
4982 peer_rx_hibern8_time_cap);
4983 /* make sure proper unit conversion is applied */
4984 tuned_pa_hibern8time = ((max_hibern8_time * HIBERN8TIME_UNIT_US)
4985 / PA_HIBERN8_TIME_UNIT_US);
4986 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME),
4987 tuned_pa_hibern8time);
4988out:
4989 return ret;
4990}
4991
4992static void ufshcd_tune_unipro_params(struct ufs_hba *hba)
4993{
4994 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) {
4995 ufshcd_tune_pa_tactivate(hba);
4996 ufshcd_tune_pa_hibern8time(hba);
4997 }
4998
4999 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE)
5000 /* set 1ms timeout for PA_TACTIVATE */
5001 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10);
5002}
5003
5004/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005005 * ufshcd_probe_hba - probe hba to detect device and initialize
5006 * @hba: per-adapter instance
5007 *
5008 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305009 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005010static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305011{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305012 int ret;
5013
5014 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305015 if (ret)
5016 goto out;
5017
Yaniv Gardi50646362014-10-23 13:25:13 +03005018 ufshcd_init_pwr_info(hba);
5019
Yaniv Gardiafdfff52016-03-10 17:37:15 +02005020 /* set the default level for urgent bkops */
5021 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT;
5022 hba->is_urgent_bkops_lvl_checked = false;
5023
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005024 /* UniPro link is active now */
5025 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05305026
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305027 ret = ufshcd_verify_dev_init(hba);
5028 if (ret)
5029 goto out;
5030
Dolev Raviv68078d52013-07-30 00:35:58 +05305031 ret = ufshcd_complete_dev_init(hba);
5032 if (ret)
5033 goto out;
5034
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02005035 ufs_advertise_fixup_device(hba);
Yaniv Gardi37113102016-03-10 17:37:16 +02005036 ufshcd_tune_unipro_params(hba);
Yaniv Gardi60f01872016-03-10 17:37:11 +02005037
5038 ret = ufshcd_set_vccq_rail_unused(hba,
5039 (hba->dev_quirks & UFS_DEVICE_NO_VCCQ) ? true : false);
5040 if (ret)
5041 goto out;
5042
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005043 /* UFS device is also active now */
5044 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305045 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005046 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305047
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005048 if (ufshcd_get_max_pwr_mode(hba)) {
5049 dev_err(hba->dev,
5050 "%s: Failed getting max supported power mode\n",
5051 __func__);
5052 } else {
5053 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005054 if (ret) {
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005055 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
5056 __func__, ret);
Dov Levenglick8643ae62016-10-17 17:10:14 -07005057 goto out;
5058 }
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005059 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005060
Yaniv Gardi53c12d02016-02-01 15:02:45 +02005061 /* set the state as operational after switching to desired gear */
5062 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005063 /*
5064 * If we are in error handling context or in power management callbacks
5065 * context, no need to scan the host
5066 */
5067 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5068 bool flag;
5069
5070 /* clear any previous UFS device information */
5071 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02005072 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
5073 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005074 hba->dev_info.f_power_on_wp_en = flag;
5075
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005076 if (!hba->is_init_prefetch)
5077 ufshcd_init_icc_levels(hba);
5078
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005079 /* Add required well known logical units to scsi mid layer */
5080 if (ufshcd_scsi_add_wlus(hba))
5081 goto out;
5082
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305083 scsi_scan_host(hba->host);
5084 pm_runtime_put_sync(hba->dev);
5085 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03005086
5087 if (!hba->is_init_prefetch)
5088 hba->is_init_prefetch = true;
5089
Sahitya Tummala856b3482014-09-25 15:32:34 +03005090 /* Resume devfreq after UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07005091 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005092
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305093out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005094 /*
5095 * If we failed to initialize the device or the device is not
5096 * present, turn off the power/clocks etc.
5097 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005098 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
5099 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005100 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005101 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005102
5103 return ret;
5104}
5105
5106/**
5107 * ufshcd_async_scan - asynchronous execution for probing hba
5108 * @data: data pointer to pass to this function
5109 * @cookie: cookie data
5110 */
5111static void ufshcd_async_scan(void *data, async_cookie_t cookie)
5112{
5113 struct ufs_hba *hba = (struct ufs_hba *)data;
5114
5115 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305116}
5117
Yaniv Gardif550c652016-03-10 17:37:07 +02005118static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
5119{
5120 unsigned long flags;
5121 struct Scsi_Host *host;
5122 struct ufs_hba *hba;
5123 int index;
5124 bool found = false;
5125
5126 if (!scmd || !scmd->device || !scmd->device->host)
5127 return BLK_EH_NOT_HANDLED;
5128
5129 host = scmd->device->host;
5130 hba = shost_priv(host);
5131 if (!hba)
5132 return BLK_EH_NOT_HANDLED;
5133
5134 spin_lock_irqsave(host->host_lock, flags);
5135
5136 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
5137 if (hba->lrb[index].cmd == scmd) {
5138 found = true;
5139 break;
5140 }
5141 }
5142
5143 spin_unlock_irqrestore(host->host_lock, flags);
5144
5145 /*
5146 * Bypass SCSI error handling and reset the block layer timer if this
5147 * SCSI command was not actually dispatched to UFS driver, otherwise
5148 * let SCSI layer handle the error as usual.
5149 */
5150 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
5151}
5152
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305153static struct scsi_host_template ufshcd_driver_template = {
5154 .module = THIS_MODULE,
5155 .name = UFSHCD,
5156 .proc_name = UFSHCD,
5157 .queuecommand = ufshcd_queuecommand,
5158 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09005159 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305160 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03005161 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305162 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05305163 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
5164 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02005165 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305166 .this_id = -1,
5167 .sg_tablesize = SG_ALL,
5168 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
5169 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005170 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01005171 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305172};
5173
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005174static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
5175 int ua)
5176{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005177 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005178
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005179 if (!vreg)
5180 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005181
Bjorn Andersson7b16a072015-02-11 19:35:28 -08005182 ret = regulator_set_load(vreg->reg, ua);
5183 if (ret < 0) {
5184 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
5185 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005186 }
5187
5188 return ret;
5189}
5190
5191static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
5192 struct ufs_vreg *vreg)
5193{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005194 if (!vreg)
5195 return 0;
5196 else if (vreg->unused)
5197 return 0;
5198 else
5199 return ufshcd_config_vreg_load(hba->dev, vreg,
5200 UFS_VREG_LPM_LOAD_UA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005201}
5202
5203static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
5204 struct ufs_vreg *vreg)
5205{
Yaniv Gardi60f01872016-03-10 17:37:11 +02005206 if (!vreg)
5207 return 0;
5208 else if (vreg->unused)
5209 return 0;
5210 else
5211 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005212}
5213
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005214static int ufshcd_config_vreg(struct device *dev,
5215 struct ufs_vreg *vreg, bool on)
5216{
5217 int ret = 0;
5218 struct regulator *reg = vreg->reg;
5219 const char *name = vreg->name;
5220 int min_uV, uA_load;
5221
5222 BUG_ON(!vreg);
5223
5224 if (regulator_count_voltages(reg) > 0) {
5225 min_uV = on ? vreg->min_uV : 0;
5226 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
5227 if (ret) {
5228 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
5229 __func__, name, ret);
5230 goto out;
5231 }
5232
5233 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005234 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
5235 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005236 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005237 }
5238out:
5239 return ret;
5240}
5241
5242static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
5243{
5244 int ret = 0;
5245
Yaniv Gardi60f01872016-03-10 17:37:11 +02005246 if (!vreg)
5247 goto out;
5248 else if (vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005249 goto out;
5250
5251 ret = ufshcd_config_vreg(dev, vreg, true);
5252 if (!ret)
5253 ret = regulator_enable(vreg->reg);
5254
5255 if (!ret)
5256 vreg->enabled = true;
5257 else
5258 dev_err(dev, "%s: %s enable failed, err=%d\n",
5259 __func__, vreg->name, ret);
5260out:
5261 return ret;
5262}
5263
5264static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
5265{
5266 int ret = 0;
5267
Yaniv Gardi60f01872016-03-10 17:37:11 +02005268 if (!vreg)
5269 goto out;
5270 else if (!vreg->enabled || vreg->unused)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005271 goto out;
5272
5273 ret = regulator_disable(vreg->reg);
5274
5275 if (!ret) {
5276 /* ignore errors on applying disable config */
5277 ufshcd_config_vreg(dev, vreg, false);
5278 vreg->enabled = false;
5279 } else {
5280 dev_err(dev, "%s: %s disable failed, err=%d\n",
5281 __func__, vreg->name, ret);
5282 }
5283out:
5284 return ret;
5285}
5286
5287static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
5288{
5289 int ret = 0;
5290 struct device *dev = hba->dev;
5291 struct ufs_vreg_info *info = &hba->vreg_info;
5292
5293 if (!info)
5294 goto out;
5295
5296 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
5297 if (ret)
5298 goto out;
5299
5300 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
5301 if (ret)
5302 goto out;
5303
5304 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
5305 if (ret)
5306 goto out;
5307
5308out:
5309 if (ret) {
5310 ufshcd_toggle_vreg(dev, info->vccq2, false);
5311 ufshcd_toggle_vreg(dev, info->vccq, false);
5312 ufshcd_toggle_vreg(dev, info->vcc, false);
5313 }
5314 return ret;
5315}
5316
Raviv Shvili6a771a62014-09-25 15:32:24 +03005317static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
5318{
5319 struct ufs_vreg_info *info = &hba->vreg_info;
5320
5321 if (info)
5322 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
5323
5324 return 0;
5325}
5326
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005327static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
5328{
5329 int ret = 0;
5330
5331 if (!vreg)
5332 goto out;
5333
5334 vreg->reg = devm_regulator_get(dev, vreg->name);
5335 if (IS_ERR(vreg->reg)) {
5336 ret = PTR_ERR(vreg->reg);
5337 dev_err(dev, "%s: %s get failed, err=%d\n",
5338 __func__, vreg->name, ret);
5339 }
5340out:
5341 return ret;
5342}
5343
5344static int ufshcd_init_vreg(struct ufs_hba *hba)
5345{
5346 int ret = 0;
5347 struct device *dev = hba->dev;
5348 struct ufs_vreg_info *info = &hba->vreg_info;
5349
5350 if (!info)
5351 goto out;
5352
5353 ret = ufshcd_get_vreg(dev, info->vcc);
5354 if (ret)
5355 goto out;
5356
5357 ret = ufshcd_get_vreg(dev, info->vccq);
5358 if (ret)
5359 goto out;
5360
5361 ret = ufshcd_get_vreg(dev, info->vccq2);
5362out:
5363 return ret;
5364}
5365
Raviv Shvili6a771a62014-09-25 15:32:24 +03005366static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
5367{
5368 struct ufs_vreg_info *info = &hba->vreg_info;
5369
5370 if (info)
5371 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
5372
5373 return 0;
5374}
5375
Yaniv Gardi60f01872016-03-10 17:37:11 +02005376static int ufshcd_set_vccq_rail_unused(struct ufs_hba *hba, bool unused)
5377{
5378 int ret = 0;
5379 struct ufs_vreg_info *info = &hba->vreg_info;
5380
5381 if (!info)
5382 goto out;
5383 else if (!info->vccq)
5384 goto out;
5385
5386 if (unused) {
5387 /* shut off the rail here */
5388 ret = ufshcd_toggle_vreg(hba->dev, info->vccq, false);
5389 /*
5390 * Mark this rail as no longer used, so it doesn't get enabled
5391 * later by mistake
5392 */
5393 if (!ret)
5394 info->vccq->unused = true;
5395 } else {
5396 /*
5397 * rail should have been already enabled hence just make sure
5398 * that unused flag is cleared.
5399 */
5400 info->vccq->unused = false;
5401 }
5402out:
5403 return ret;
5404}
5405
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005406static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
5407 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005408{
5409 int ret = 0;
5410 struct ufs_clk_info *clki;
5411 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005412 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005413
5414 if (!head || list_empty(head))
5415 goto out;
5416
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005417 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE);
5418 if (ret)
5419 return ret;
5420
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005421 list_for_each_entry(clki, head, list) {
5422 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005423 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
5424 continue;
5425
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005426 if (on && !clki->enabled) {
5427 ret = clk_prepare_enable(clki->clk);
5428 if (ret) {
5429 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5430 __func__, clki->name, ret);
5431 goto out;
5432 }
5433 } else if (!on && clki->enabled) {
5434 clk_disable_unprepare(clki->clk);
5435 }
5436 clki->enabled = on;
5437 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5438 clki->name, on ? "en" : "dis");
5439 }
5440 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005441
Subhash Jadavani1e879e82016-10-06 21:48:22 -07005442 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE);
5443 if (ret)
5444 return ret;
5445
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005446out:
5447 if (ret) {
5448 list_for_each_entry(clki, head, list) {
5449 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5450 clk_disable_unprepare(clki->clk);
5451 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005452 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005453 spin_lock_irqsave(hba->host->host_lock, flags);
5454 hba->clk_gating.state = CLKS_ON;
5455 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005456 }
5457 return ret;
5458}
5459
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005460static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5461{
5462 return __ufshcd_setup_clocks(hba, on, false);
5463}
5464
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005465static int ufshcd_init_clocks(struct ufs_hba *hba)
5466{
5467 int ret = 0;
5468 struct ufs_clk_info *clki;
5469 struct device *dev = hba->dev;
5470 struct list_head *head = &hba->clk_list_head;
5471
5472 if (!head || list_empty(head))
5473 goto out;
5474
5475 list_for_each_entry(clki, head, list) {
5476 if (!clki->name)
5477 continue;
5478
5479 clki->clk = devm_clk_get(dev, clki->name);
5480 if (IS_ERR(clki->clk)) {
5481 ret = PTR_ERR(clki->clk);
5482 dev_err(dev, "%s: %s clk get failed, %d\n",
5483 __func__, clki->name, ret);
5484 goto out;
5485 }
5486
5487 if (clki->max_freq) {
5488 ret = clk_set_rate(clki->clk, clki->max_freq);
5489 if (ret) {
5490 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5491 __func__, clki->name,
5492 clki->max_freq, ret);
5493 goto out;
5494 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005495 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005496 }
5497 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5498 clki->name, clk_get_rate(clki->clk));
5499 }
5500out:
5501 return ret;
5502}
5503
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005504static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5505{
5506 int err = 0;
5507
5508 if (!hba->vops)
5509 goto out;
5510
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005511 err = ufshcd_vops_init(hba);
5512 if (err)
5513 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005514
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005515 err = ufshcd_vops_setup_regulators(hba, true);
5516 if (err)
5517 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005518
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005519 goto out;
5520
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005521out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005522 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005523out:
5524 if (err)
5525 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005526 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005527 return err;
5528}
5529
5530static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5531{
5532 if (!hba->vops)
5533 return;
5534
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005535 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005536
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005537 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005538}
5539
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005540static int ufshcd_hba_init(struct ufs_hba *hba)
5541{
5542 int err;
5543
Raviv Shvili6a771a62014-09-25 15:32:24 +03005544 /*
5545 * Handle host controller power separately from the UFS device power
5546 * rails as it will help controlling the UFS host controller power
5547 * collapse easily which is different than UFS device power collapse.
5548 * Also, enable the host controller power before we go ahead with rest
5549 * of the initialization here.
5550 */
5551 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005552 if (err)
5553 goto out;
5554
Raviv Shvili6a771a62014-09-25 15:32:24 +03005555 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005556 if (err)
5557 goto out;
5558
Raviv Shvili6a771a62014-09-25 15:32:24 +03005559 err = ufshcd_init_clocks(hba);
5560 if (err)
5561 goto out_disable_hba_vreg;
5562
5563 err = ufshcd_setup_clocks(hba, true);
5564 if (err)
5565 goto out_disable_hba_vreg;
5566
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005567 err = ufshcd_init_vreg(hba);
5568 if (err)
5569 goto out_disable_clks;
5570
5571 err = ufshcd_setup_vreg(hba, true);
5572 if (err)
5573 goto out_disable_clks;
5574
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005575 err = ufshcd_variant_hba_init(hba);
5576 if (err)
5577 goto out_disable_vreg;
5578
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005579 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005580 goto out;
5581
5582out_disable_vreg:
5583 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005584out_disable_clks:
5585 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005586out_disable_hba_vreg:
5587 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005588out:
5589 return err;
5590}
5591
5592static void ufshcd_hba_exit(struct ufs_hba *hba)
5593{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005594 if (hba->is_powered) {
5595 ufshcd_variant_hba_exit(hba);
5596 ufshcd_setup_vreg(hba, false);
Gilad Bronera5082532016-10-17 17:10:00 -07005597 ufshcd_suspend_clkscaling(hba);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005598 ufshcd_setup_clocks(hba, false);
5599 ufshcd_setup_hba_vreg(hba, false);
5600 hba->is_powered = false;
5601 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005602}
5603
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005604static int
5605ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305606{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005607 unsigned char cmd[6] = {REQUEST_SENSE,
5608 0,
5609 0,
5610 0,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005611 UFSHCD_REQ_SENSE_SIZE,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005612 0};
5613 char *buffer;
5614 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305615
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005616 buffer = kzalloc(UFSHCD_REQ_SENSE_SIZE, GFP_KERNEL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005617 if (!buffer) {
5618 ret = -ENOMEM;
5619 goto out;
5620 }
5621
5622 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
Gilad Bronerdcea0bf2016-10-17 17:09:48 -07005623 UFSHCD_REQ_SENSE_SIZE, NULL,
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005624 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5625 if (ret)
5626 pr_err("%s: failed with err %d\n", __func__, ret);
5627
5628 kfree(buffer);
5629out:
5630 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305631}
5632
5633/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005634 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5635 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305636 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005637 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305638 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005639 * Returns 0 if requested power mode is set successfully
5640 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305641 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005642static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5643 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305644{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005645 unsigned char cmd[6] = { START_STOP };
5646 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005647 struct scsi_device *sdp;
5648 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005649 int ret;
5650
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005651 spin_lock_irqsave(hba->host->host_lock, flags);
5652 sdp = hba->sdev_ufs_device;
5653 if (sdp) {
5654 ret = scsi_device_get(sdp);
5655 if (!ret && !scsi_device_online(sdp)) {
5656 ret = -ENODEV;
5657 scsi_device_put(sdp);
5658 }
5659 } else {
5660 ret = -ENODEV;
5661 }
5662 spin_unlock_irqrestore(hba->host->host_lock, flags);
5663
5664 if (ret)
5665 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005666
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305667 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005668 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5669 * handling, which would wait for host to be resumed. Since we know
5670 * we are functional while we are here, skip host resume in error
5671 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305672 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005673 hba->host->eh_noresume = 1;
5674 if (hba->wlun_dev_clr_ua) {
5675 ret = ufshcd_send_request_sense(hba, sdp);
5676 if (ret)
5677 goto out;
5678 /* Unit attention condition is cleared now */
5679 hba->wlun_dev_clr_ua = false;
5680 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305681
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005682 cmd[4] = pwr_mode << 4;
5683
5684 /*
5685 * Current function would be generally called from the power management
5686 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5687 * already suspended childs.
5688 */
5689 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5690 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5691 if (ret) {
5692 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005693 "START_STOP failed for power mode: %d, result %x\n",
5694 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005695 if (driver_byte(ret) & DRIVER_SENSE)
5696 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005697 }
5698
5699 if (!ret)
5700 hba->curr_dev_pwr_mode = pwr_mode;
5701out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005702 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005703 hba->host->eh_noresume = 0;
5704 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305705}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305706
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005707static int ufshcd_link_state_transition(struct ufs_hba *hba,
5708 enum uic_link_state req_link_state,
5709 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305710{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005711 int ret = 0;
5712
5713 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305714 return 0;
5715
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005716 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5717 ret = ufshcd_uic_hibern8_enter(hba);
5718 if (!ret)
5719 ufshcd_set_link_hibern8(hba);
5720 else
5721 goto out;
5722 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305723 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005724 * If autobkops is enabled, link can't be turned off because
5725 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305726 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005727 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5728 (!check_for_bkops || (check_for_bkops &&
5729 !hba->auto_bkops_enabled))) {
5730 /*
Yaniv Gardif3099fb2016-03-10 17:37:17 +02005731 * Let's make sure that link is in low power mode, we are doing
5732 * this currently by putting the link in Hibern8. Otherway to
5733 * put the link in low power mode is to send the DME end point
5734 * to device and then send the DME reset command to local
5735 * unipro. But putting the link in hibern8 is much faster.
5736 */
5737 ret = ufshcd_uic_hibern8_enter(hba);
5738 if (ret)
5739 goto out;
5740 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005741 * Change controller state to "reset state" which
5742 * should also put the link in off/reset state
5743 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005744 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005745 /*
5746 * TODO: Check if we need any delay to make sure that
5747 * controller is reset
5748 */
5749 ufshcd_set_link_off(hba);
5750 }
5751
5752out:
5753 return ret;
5754}
5755
5756static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5757{
5758 /*
Yaniv Gardib799fdf2016-03-10 17:37:18 +02005759 * It seems some UFS devices may keep drawing more than sleep current
5760 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
5761 * To avoid this situation, add 2ms delay before putting these UFS
5762 * rails in LPM mode.
5763 */
5764 if (!ufshcd_is_link_active(hba) &&
5765 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM)
5766 usleep_range(2000, 2100);
5767
5768 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005769 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5770 * power.
5771 *
5772 * If UFS device and link is in OFF state, all power supplies (VCC,
5773 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5774 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5775 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5776 *
5777 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5778 * in low power state which would save some power.
5779 */
5780 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5781 !hba->dev_info.is_lu_power_on_wp) {
5782 ufshcd_setup_vreg(hba, false);
5783 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5784 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5785 if (!ufshcd_is_link_active(hba)) {
5786 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5787 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5788 }
5789 }
5790}
5791
5792static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5793{
5794 int ret = 0;
5795
5796 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5797 !hba->dev_info.is_lu_power_on_wp) {
5798 ret = ufshcd_setup_vreg(hba, true);
5799 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5800 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
5801 if (!ret && !ufshcd_is_link_active(hba)) {
5802 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5803 if (ret)
5804 goto vcc_disable;
5805 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5806 if (ret)
5807 goto vccq_lpm;
5808 }
5809 }
5810 goto out;
5811
5812vccq_lpm:
5813 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5814vcc_disable:
5815 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5816out:
5817 return ret;
5818}
5819
5820static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5821{
5822 if (ufshcd_is_link_off(hba))
5823 ufshcd_setup_hba_vreg(hba, false);
5824}
5825
5826static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5827{
5828 if (ufshcd_is_link_off(hba))
5829 ufshcd_setup_hba_vreg(hba, true);
5830}
5831
5832/**
5833 * ufshcd_suspend - helper function for suspend operations
5834 * @hba: per adapter instance
5835 * @pm_op: desired low power operation type
5836 *
5837 * This function will try to put the UFS device and link into low power
5838 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5839 * (System PM level).
5840 *
5841 * If this function is called during shutdown, it will make sure that
5842 * both UFS device and UFS link is powered off.
5843 *
5844 * NOTE: UFS device & link must be active before we enter in this function.
5845 *
5846 * Returns 0 for success and non-zero for failure
5847 */
5848static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5849{
5850 int ret = 0;
5851 enum ufs_pm_level pm_lvl;
5852 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5853 enum uic_link_state req_link_state;
5854
5855 hba->pm_op_in_progress = 1;
5856 if (!ufshcd_is_shutdown_pm(pm_op)) {
5857 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5858 hba->rpm_lvl : hba->spm_lvl;
5859 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5860 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5861 } else {
5862 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5863 req_link_state = UIC_LINK_OFF_STATE;
5864 }
5865
5866 /*
5867 * If we can't transition into any of the low power modes
5868 * just gate the clocks.
5869 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005870 ufshcd_hold(hba, false);
5871 hba->clk_gating.is_suspended = true;
5872
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005873 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5874 req_link_state == UIC_LINK_ACTIVE_STATE) {
5875 goto disable_clks;
5876 }
5877
5878 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5879 (req_link_state == hba->uic_link_state))
5880 goto out;
5881
5882 /* UFS device & link must be active before we enter in this function */
5883 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5884 ret = -EINVAL;
5885 goto out;
5886 }
5887
5888 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005889 if (ufshcd_can_autobkops_during_suspend(hba)) {
5890 /*
5891 * The device is idle with no requests in the queue,
5892 * allow background operations if bkops status shows
5893 * that performance might be impacted.
5894 */
5895 ret = ufshcd_urgent_bkops(hba);
5896 if (ret)
5897 goto enable_gating;
5898 } else {
5899 /* make sure that auto bkops is disabled */
5900 ufshcd_disable_auto_bkops(hba);
5901 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005902 }
5903
5904 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5905 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5906 !ufshcd_is_runtime_pm(pm_op))) {
5907 /* ensure that bkops is disabled */
5908 ufshcd_disable_auto_bkops(hba);
5909 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5910 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005911 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005912 }
5913
5914 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5915 if (ret)
5916 goto set_dev_active;
5917
5918 ufshcd_vreg_set_lpm(hba);
5919
5920disable_clks:
5921 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03005922 * The clock scaling needs access to controller registers. Hence, Wait
5923 * for pending clock scaling work to be done before clocks are
5924 * turned off.
5925 */
Gilad Bronera5082532016-10-17 17:10:00 -07005926 ufshcd_suspend_clkscaling(hba);
5927
Sahitya Tummala856b3482014-09-25 15:32:34 +03005928 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005929 * Call vendor specific suspend callback. As these callbacks may access
5930 * vendor specific host controller register space call them before the
5931 * host clocks are ON.
5932 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005933 ret = ufshcd_vops_suspend(hba, pm_op);
5934 if (ret)
5935 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005936
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005937 if (!ufshcd_is_link_active(hba))
5938 ufshcd_setup_clocks(hba, false);
5939 else
5940 /* If link is active, device ref_clk can't be switched off */
5941 __ufshcd_setup_clocks(hba, false, true);
5942
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005943 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005944 /*
5945 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005946 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005947 */
5948 ufshcd_disable_irq(hba);
5949 /* Put the host controller in low power mode if possible */
5950 ufshcd_hba_vreg_set_lpm(hba);
5951 goto out;
5952
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005953set_link_active:
Gilad Bronera5082532016-10-17 17:10:00 -07005954 ufshcd_resume_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005955 ufshcd_vreg_set_hpm(hba);
5956 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5957 ufshcd_set_link_active(hba);
5958 else if (ufshcd_is_link_off(hba))
5959 ufshcd_host_reset_and_restore(hba);
5960set_dev_active:
5961 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5962 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005963enable_gating:
5964 hba->clk_gating.is_suspended = false;
5965 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005966out:
5967 hba->pm_op_in_progress = 0;
5968 return ret;
5969}
5970
5971/**
5972 * ufshcd_resume - helper function for resume operations
5973 * @hba: per adapter instance
5974 * @pm_op: runtime PM or system PM
5975 *
5976 * This function basically brings the UFS device, UniPro link and controller
5977 * to active state.
5978 *
5979 * Returns 0 for success and non-zero for failure
5980 */
5981static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5982{
5983 int ret;
5984 enum uic_link_state old_link_state;
5985
5986 hba->pm_op_in_progress = 1;
5987 old_link_state = hba->uic_link_state;
5988
5989 ufshcd_hba_vreg_set_hpm(hba);
5990 /* Make sure clocks are enabled before accessing controller */
5991 ret = ufshcd_setup_clocks(hba, true);
5992 if (ret)
5993 goto out;
5994
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005995 /* enable the host irq as host controller would be active soon */
5996 ret = ufshcd_enable_irq(hba);
5997 if (ret)
5998 goto disable_irq_and_vops_clks;
5999
6000 ret = ufshcd_vreg_set_hpm(hba);
6001 if (ret)
6002 goto disable_irq_and_vops_clks;
6003
6004 /*
6005 * Call vendor specific resume callback. As these callbacks may access
6006 * vendor specific host controller register space call them when the
6007 * host clocks are ON.
6008 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006009 ret = ufshcd_vops_resume(hba, pm_op);
6010 if (ret)
6011 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006012
6013 if (ufshcd_is_link_hibern8(hba)) {
6014 ret = ufshcd_uic_hibern8_exit(hba);
6015 if (!ret)
6016 ufshcd_set_link_active(hba);
6017 else
6018 goto vendor_suspend;
6019 } else if (ufshcd_is_link_off(hba)) {
6020 ret = ufshcd_host_reset_and_restore(hba);
6021 /*
6022 * ufshcd_host_reset_and_restore() should have already
6023 * set the link state as active
6024 */
6025 if (ret || !ufshcd_is_link_active(hba))
6026 goto vendor_suspend;
6027 }
6028
6029 if (!ufshcd_is_ufs_dev_active(hba)) {
6030 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
6031 if (ret)
6032 goto set_old_link_state;
6033 }
6034
Subhash Jadavani374a2462014-09-25 15:32:35 +03006035 /*
6036 * If BKOPs operations are urgently needed at this moment then
6037 * keep auto-bkops enabled or else disable it.
6038 */
6039 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006040 hba->clk_gating.is_suspended = false;
6041
Gilad Bronera5082532016-10-17 17:10:00 -07006042 ufshcd_resume_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006043
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006044 /* Schedule clock gating in case of no access to UFS device yet */
6045 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006046 goto out;
6047
6048set_old_link_state:
6049 ufshcd_link_state_transition(hba, old_link_state, 0);
6050vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02006051 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006052disable_vreg:
6053 ufshcd_vreg_set_lpm(hba);
6054disable_irq_and_vops_clks:
6055 ufshcd_disable_irq(hba);
Gilad Bronera5082532016-10-17 17:10:00 -07006056 ufshcd_suspend_clkscaling(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006057 ufshcd_setup_clocks(hba, false);
6058out:
6059 hba->pm_op_in_progress = 0;
6060 return ret;
6061}
6062
6063/**
6064 * ufshcd_system_suspend - system suspend routine
6065 * @hba: per adapter instance
6066 * @pm_op: runtime PM or system PM
6067 *
6068 * Check the description of ufshcd_suspend() function for more details.
6069 *
6070 * Returns 0 for success and non-zero for failure
6071 */
6072int ufshcd_system_suspend(struct ufs_hba *hba)
6073{
6074 int ret = 0;
6075
6076 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03006077 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006078
6079 if (pm_runtime_suspended(hba->dev)) {
6080 if (hba->rpm_lvl == hba->spm_lvl)
6081 /*
6082 * There is possibility that device may still be in
6083 * active state during the runtime suspend.
6084 */
6085 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
6086 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
6087 goto out;
6088
6089 /*
6090 * UFS device and/or UFS link low power states during runtime
6091 * suspend seems to be different than what is expected during
6092 * system suspend. Hence runtime resume the devic & link and
6093 * let the system suspend low power states to take effect.
6094 * TODO: If resume takes longer time, we might have optimize
6095 * it in future by not resuming everything if possible.
6096 */
6097 ret = ufshcd_runtime_resume(hba);
6098 if (ret)
6099 goto out;
6100 }
6101
6102 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
6103out:
Dolev Ravive7850602014-09-25 15:32:36 +03006104 if (!ret)
6105 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006106 return ret;
6107}
6108EXPORT_SYMBOL(ufshcd_system_suspend);
6109
6110/**
6111 * ufshcd_system_resume - system resume routine
6112 * @hba: per adapter instance
6113 *
6114 * Returns 0 for success and non-zero for failure
6115 */
6116
6117int ufshcd_system_resume(struct ufs_hba *hba)
6118{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006119 if (!hba)
6120 return -EINVAL;
6121
6122 if (!hba->is_powered || pm_runtime_suspended(hba->dev))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006123 /*
6124 * Let the runtime resume take care of resuming
6125 * if runtime suspended.
6126 */
6127 return 0;
6128
6129 return ufshcd_resume(hba, UFS_SYSTEM_PM);
6130}
6131EXPORT_SYMBOL(ufshcd_system_resume);
6132
6133/**
6134 * ufshcd_runtime_suspend - runtime suspend routine
6135 * @hba: per adapter instance
6136 *
6137 * Check the description of ufshcd_suspend() function for more details.
6138 *
6139 * Returns 0 for success and non-zero for failure
6140 */
6141int ufshcd_runtime_suspend(struct ufs_hba *hba)
6142{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006143 if (!hba)
6144 return -EINVAL;
6145
6146 if (!hba->is_powered)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006147 return 0;
6148
6149 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306150}
6151EXPORT_SYMBOL(ufshcd_runtime_suspend);
6152
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006153/**
6154 * ufshcd_runtime_resume - runtime resume routine
6155 * @hba: per adapter instance
6156 *
6157 * This function basically brings the UFS device, UniPro link and controller
6158 * to active state. Following operations are done in this function:
6159 *
6160 * 1. Turn on all the controller related clocks
6161 * 2. Bring the UniPro link out of Hibernate state
6162 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
6163 * to active state.
6164 * 4. If auto-bkops is enabled on the device, disable it.
6165 *
6166 * So following would be the possible power state after this function return
6167 * successfully:
6168 * S1: UFS device in Active state with VCC rail ON
6169 * UniPro link in Active state
6170 * All the UFS/UniPro controller clocks are ON
6171 *
6172 * Returns 0 for success and non-zero for failure
6173 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306174int ufshcd_runtime_resume(struct ufs_hba *hba)
6175{
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006176 if (!hba)
6177 return -EINVAL;
6178
6179 if (!hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306180 return 0;
Yaniv Gardie3ce73d2016-10-17 17:09:24 -07006181
6182 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306183}
6184EXPORT_SYMBOL(ufshcd_runtime_resume);
6185
6186int ufshcd_runtime_idle(struct ufs_hba *hba)
6187{
6188 return 0;
6189}
6190EXPORT_SYMBOL(ufshcd_runtime_idle);
6191
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306192/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006193 * ufshcd_shutdown - shutdown routine
6194 * @hba: per adapter instance
6195 *
6196 * This function would power off both UFS device and UFS link.
6197 *
6198 * Returns 0 always to allow force shutdown even in case of errors.
6199 */
6200int ufshcd_shutdown(struct ufs_hba *hba)
6201{
6202 int ret = 0;
6203
6204 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
6205 goto out;
6206
6207 if (pm_runtime_suspended(hba->dev)) {
6208 ret = ufshcd_runtime_resume(hba);
6209 if (ret)
6210 goto out;
6211 }
6212
6213 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
6214out:
6215 if (ret)
6216 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
6217 /* allow force shutdown even in case of errors */
6218 return 0;
6219}
6220EXPORT_SYMBOL(ufshcd_shutdown);
6221
6222/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306223 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306224 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306225 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306226 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306227void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306228{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05306229 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306230 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306231 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02006232 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306233
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306234 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006235
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006236 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006237 if (ufshcd_is_clkscaling_enabled(hba))
6238 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006239 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306240}
6241EXPORT_SYMBOL_GPL(ufshcd_remove);
6242
6243/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02006244 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
6245 * @hba: pointer to Host Bus Adapter (HBA)
6246 */
6247void ufshcd_dealloc_host(struct ufs_hba *hba)
6248{
6249 scsi_host_put(hba->host);
6250}
6251EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
6252
6253/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006254 * ufshcd_set_dma_mask - Set dma mask based on the controller
6255 * addressing capability
6256 * @hba: per adapter instance
6257 *
6258 * Returns 0 for success, non-zero for failure
6259 */
6260static int ufshcd_set_dma_mask(struct ufs_hba *hba)
6261{
6262 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
6263 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
6264 return 0;
6265 }
6266 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
6267}
6268
6269/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006270 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306271 * @dev: pointer to device handle
6272 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306273 * Returns 0 on success, non-zero value on failure
6274 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006275int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306276{
6277 struct Scsi_Host *host;
6278 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006279 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306280
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306281 if (!dev) {
6282 dev_err(dev,
6283 "Invalid memory reference for dev is NULL\n");
6284 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306285 goto out_error;
6286 }
6287
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306288 host = scsi_host_alloc(&ufshcd_driver_template,
6289 sizeof(struct ufs_hba));
6290 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306291 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306292 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306293 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306294 }
6295 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306296 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306297 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006298 *hba_handle = hba;
6299
6300out_error:
6301 return err;
6302}
6303EXPORT_SYMBOL(ufshcd_alloc_host);
6304
Sahitya Tummala856b3482014-09-25 15:32:34 +03006305static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
6306{
6307 int ret = 0;
6308 struct ufs_clk_info *clki;
6309 struct list_head *head = &hba->clk_list_head;
6310
6311 if (!head || list_empty(head))
6312 goto out;
6313
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006314 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
6315 if (ret)
6316 return ret;
6317
Sahitya Tummala856b3482014-09-25 15:32:34 +03006318 list_for_each_entry(clki, head, list) {
6319 if (!IS_ERR_OR_NULL(clki->clk)) {
6320 if (scale_up && clki->max_freq) {
6321 if (clki->curr_freq == clki->max_freq)
6322 continue;
6323 ret = clk_set_rate(clki->clk, clki->max_freq);
6324 if (ret) {
6325 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6326 __func__, clki->name,
6327 clki->max_freq, ret);
6328 break;
6329 }
6330 clki->curr_freq = clki->max_freq;
6331
6332 } else if (!scale_up && clki->min_freq) {
6333 if (clki->curr_freq == clki->min_freq)
6334 continue;
6335 ret = clk_set_rate(clki->clk, clki->min_freq);
6336 if (ret) {
6337 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
6338 __func__, clki->name,
6339 clki->min_freq, ret);
6340 break;
6341 }
6342 clki->curr_freq = clki->min_freq;
6343 }
6344 }
6345 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
6346 clki->name, clk_get_rate(clki->clk));
6347 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02006348
6349 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
6350
Sahitya Tummala856b3482014-09-25 15:32:34 +03006351out:
6352 return ret;
6353}
6354
6355static int ufshcd_devfreq_target(struct device *dev,
6356 unsigned long *freq, u32 flags)
6357{
6358 int err = 0;
6359 struct ufs_hba *hba = dev_get_drvdata(dev);
6360
6361 if (!ufshcd_is_clkscaling_enabled(hba))
6362 return -EINVAL;
6363
6364 if (*freq == UINT_MAX)
6365 err = ufshcd_scale_clks(hba, true);
6366 else if (*freq == 0)
6367 err = ufshcd_scale_clks(hba, false);
6368
6369 return err;
6370}
6371
6372static int ufshcd_devfreq_get_dev_status(struct device *dev,
6373 struct devfreq_dev_status *stat)
6374{
6375 struct ufs_hba *hba = dev_get_drvdata(dev);
6376 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
6377 unsigned long flags;
6378
6379 if (!ufshcd_is_clkscaling_enabled(hba))
6380 return -EINVAL;
6381
6382 memset(stat, 0, sizeof(*stat));
6383
6384 spin_lock_irqsave(hba->host->host_lock, flags);
6385 if (!scaling->window_start_t)
6386 goto start_window;
6387
6388 if (scaling->is_busy_started)
6389 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
6390 scaling->busy_start_t));
6391
6392 stat->total_time = jiffies_to_usecs((long)jiffies -
6393 (long)scaling->window_start_t);
6394 stat->busy_time = scaling->tot_busy_t;
6395start_window:
6396 scaling->window_start_t = jiffies;
6397 scaling->tot_busy_t = 0;
6398
6399 if (hba->outstanding_reqs) {
6400 scaling->busy_start_t = ktime_get();
6401 scaling->is_busy_started = true;
6402 } else {
6403 scaling->busy_start_t = ktime_set(0, 0);
6404 scaling->is_busy_started = false;
6405 }
6406 spin_unlock_irqrestore(hba->host->host_lock, flags);
6407 return 0;
6408}
6409
6410static struct devfreq_dev_profile ufs_devfreq_profile = {
6411 .polling_ms = 100,
6412 .target = ufshcd_devfreq_target,
6413 .get_dev_status = ufshcd_devfreq_get_dev_status,
6414};
6415
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006416/**
6417 * ufshcd_init - Driver initialization routine
6418 * @hba: per-adapter instance
6419 * @mmio_base: base register address
6420 * @irq: Interrupt line of device
6421 * Returns 0 on success, non-zero value on failure
6422 */
6423int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
6424{
6425 int err;
6426 struct Scsi_Host *host = hba->host;
6427 struct device *dev = hba->dev;
6428
6429 if (!mmio_base) {
6430 dev_err(hba->dev,
6431 "Invalid memory reference for mmio_base is NULL\n");
6432 err = -ENODEV;
6433 goto out_error;
6434 }
6435
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306436 hba->mmio_base = mmio_base;
6437 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306438
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006439 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006440 if (err)
6441 goto out_error;
6442
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306443 /* Read capabilities registers */
6444 ufshcd_hba_capabilities(hba);
6445
6446 /* Get UFS version supported by the controller */
6447 hba->ufs_version = ufshcd_get_ufs_version(hba);
6448
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05306449 /* Get Interrupt bit mask per version */
6450 hba->intr_mask = ufshcd_get_intr_mask(hba);
6451
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006452 err = ufshcd_set_dma_mask(hba);
6453 if (err) {
6454 dev_err(hba->dev, "set dma mask failed\n");
6455 goto out_disable;
6456 }
6457
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306458 /* Allocate memory for host memory space */
6459 err = ufshcd_memory_alloc(hba);
6460 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306461 dev_err(hba->dev, "Memory allocation failed\n");
6462 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306463 }
6464
6465 /* Configure LRB */
6466 ufshcd_host_memory_configure(hba);
6467
6468 host->can_queue = hba->nutrs;
6469 host->cmd_per_lun = hba->nutrs;
6470 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006471 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306472 host->max_channel = UFSHCD_MAX_CHANNEL;
6473 host->unique_id = host->host_no;
6474 host->max_cmd_len = MAX_CDB_SIZE;
6475
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006476 hba->max_pwr_info.is_valid = false;
6477
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306478 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306479 init_waitqueue_head(&hba->tm_wq);
6480 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306481
6482 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306483 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306484 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306485
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306486 /* Initialize UIC command mutex */
6487 mutex_init(&hba->uic_cmd_mutex);
6488
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306489 /* Initialize mutex for device management commands */
6490 mutex_init(&hba->dev_cmd.lock);
6491
6492 /* Initialize device management tag acquire wait queue */
6493 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6494
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006495 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006496
6497 /*
6498 * In order to avoid any spurious interrupt immediately after
6499 * registering UFS controller interrupt handler, clear any pending UFS
6500 * interrupt status and disable all the UFS interrupts.
6501 */
6502 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6503 REG_INTERRUPT_STATUS);
6504 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6505 /*
6506 * Make sure that UFS interrupts are disabled and any pending interrupt
6507 * status is cleared before registering UFS interrupt handler.
6508 */
6509 mb();
6510
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306511 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006512 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306513 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306514 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006515 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006516 } else {
6517 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306518 }
6519
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306520 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306521 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306522 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006523 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306524 }
6525
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306526 /* Host controller enable */
6527 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306528 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306529 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306530 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306531 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306532
Sahitya Tummala856b3482014-09-25 15:32:34 +03006533 if (ufshcd_is_clkscaling_enabled(hba)) {
6534 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6535 "simple_ondemand", NULL);
6536 if (IS_ERR(hba->devfreq)) {
6537 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6538 PTR_ERR(hba->devfreq));
Wei Yongjun73811c92016-09-28 14:49:42 +00006539 err = PTR_ERR(hba->devfreq);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006540 goto out_remove_scsi_host;
6541 }
6542 /* Suspend devfreq until the UFS device is detected */
Gilad Bronera5082532016-10-17 17:10:00 -07006543 ufshcd_suspend_clkscaling(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03006544 }
6545
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306546 /* Hold auto suspend until async scan completes */
6547 pm_runtime_get_sync(dev);
6548
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006549 /*
6550 * The device-initialize-sequence hasn't been invoked yet.
6551 * Set the device to power-off state
6552 */
6553 ufshcd_set_ufs_dev_poweroff(hba);
6554
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306555 async_schedule(ufshcd_async_scan, hba);
6556
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306557 return 0;
6558
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306559out_remove_scsi_host:
6560 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006561exit_gating:
6562 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306563out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006564 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306565 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006566 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306567out_error:
6568 return err;
6569}
6570EXPORT_SYMBOL_GPL(ufshcd_init);
6571
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306572MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6573MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306574MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306575MODULE_LICENSE("GPL");
6576MODULE_VERSION(UFSHCD_DRIVER_VERSION);