blob: f8b458fb258aa424705d26b46674f891125bf134 [file] [log] [blame]
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301/*
Vinayak Holikattie0eca632013-02-25 21:44:33 +05302 * Universal Flash Storage Host controller driver Core
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303 *
4 * This code is based on drivers/scsi/ufs/ufshcd.c
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305 * Copyright (C) 2011-2013 Samsung India Software Operations
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02006 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308 * Authors:
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053016 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053018 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053024 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
34 * this program.
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030035 *
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053038 */
39
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053040#include <linux/async.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030041#include <linux/devfreq.h>
Yaniv Gardib573d482016-03-10 17:37:09 +020042#include <linux/nls.h>
Yaniv Gardi54b879b2016-03-10 17:37:05 +020043#include <linux/of.h>
Vinayak Holikattie0eca632013-02-25 21:44:33 +053044#include "ufshcd.h"
Yaniv Gardic58ab7a2016-03-10 17:37:10 +020045#include "ufs_quirks.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053046#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053047
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053048#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
49 UTP_TASK_REQ_COMPL |\
50 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053051/* UIC command timeout, unit: ms */
52#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053053
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053054/* NOP OUT retries waiting for NOP IN response */
55#define NOP_OUT_RETRIES 10
56/* Timeout after 30 msecs if NOP OUT hangs without response */
57#define NOP_OUT_TIMEOUT 30 /* msecs */
58
Dolev Raviv68078d52013-07-30 00:35:58 +053059/* Query request retries */
60#define QUERY_REQ_RETRIES 10
61/* Query request timeout */
62#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020063/*
64 * Query request timeout for fDeviceInit flag
65 * fDeviceInit query response time for some devices is too large that default
66 * QUERY_REQ_TIMEOUT may not be enough for such devices.
67 */
68#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053069
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053070/* Task management command timeout */
71#define TM_CMD_TIMEOUT 100 /* msecs */
72
Yaniv Gardi64238fb2016-02-01 15:02:43 +020073/* maximum number of retries for a general UIC command */
74#define UFS_UIC_COMMAND_RETRIES 3
75
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030076/* maximum number of link-startup retries */
77#define DME_LINKSTARTUP_RETRIES 3
78
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020079/* Maximum retries for Hibern8 enter */
80#define UIC_HIBERN8_ENTER_RETRIES 3
81
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030082/* maximum number of reset retries before giving up */
83#define MAX_HOST_RESET_RETRIES 5
84
Dolev Raviv68078d52013-07-30 00:35:58 +053085/* Expose the flag value from utp_upiu_query.value */
86#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
87
Seungwon Jeon7d568652013-08-31 21:40:20 +053088/* Interrupt aggregation default timeout, unit: 40us */
89#define INT_AGGR_DEF_TO 0x02
90
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030091#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
92 ({ \
93 int _ret; \
94 if (_on) \
95 _ret = ufshcd_enable_vreg(_dev, _vreg); \
96 else \
97 _ret = ufshcd_disable_vreg(_dev, _vreg); \
98 _ret; \
99 })
100
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300101static u32 ufs_query_desc_max_size[] = {
102 QUERY_DESC_DEVICE_MAX_SIZE,
103 QUERY_DESC_CONFIGURAION_MAX_SIZE,
104 QUERY_DESC_UNIT_MAX_SIZE,
105 QUERY_DESC_RFU_MAX_SIZE,
106 QUERY_DESC_INTERCONNECT_MAX_SIZE,
107 QUERY_DESC_STRING_MAX_SIZE,
108 QUERY_DESC_RFU_MAX_SIZE,
Tomas Winkler1ce21792016-02-09 10:25:40 +0200109 QUERY_DESC_GEOMETRY_MAX_SIZE,
Subhash Jadavanida461ce2014-09-25 15:32:25 +0300110 QUERY_DESC_POWER_MAX_SIZE,
111 QUERY_DESC_RFU_MAX_SIZE,
112};
113
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530114enum {
115 UFSHCD_MAX_CHANNEL = 0,
116 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530117 UFSHCD_CMD_PER_LUN = 32,
118 UFSHCD_CAN_QUEUE = 32,
119};
120
121/* UFSHCD states */
122enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530123 UFSHCD_STATE_RESET,
124 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530125 UFSHCD_STATE_OPERATIONAL,
126};
127
128/* UFSHCD error handling flags */
129enum {
130 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530131};
132
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530133/* UFSHCD UIC layer error flags */
134enum {
135 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
136 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
137 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
138 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
139};
140
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530141/* Interrupt configuration options */
142enum {
143 UFSHCD_INT_DISABLE,
144 UFSHCD_INT_ENABLE,
145 UFSHCD_INT_CLEAR,
146};
147
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530148#define ufshcd_set_eh_in_progress(h) \
149 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
150#define ufshcd_eh_in_progress(h) \
151 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
152#define ufshcd_clear_eh_in_progress(h) \
153 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
154
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300155#define ufshcd_set_ufs_dev_active(h) \
156 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
157#define ufshcd_set_ufs_dev_sleep(h) \
158 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
159#define ufshcd_set_ufs_dev_poweroff(h) \
160 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
161#define ufshcd_is_ufs_dev_active(h) \
162 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
163#define ufshcd_is_ufs_dev_sleep(h) \
164 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
165#define ufshcd_is_ufs_dev_poweroff(h) \
166 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
167
168static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
169 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
170 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
171 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
172 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
173 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
174 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
175};
176
177static inline enum ufs_dev_pwr_mode
178ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
179{
180 return ufs_pm_lvl_states[lvl].dev_state;
181}
182
183static inline enum uic_link_state
184ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
185{
186 return ufs_pm_lvl_states[lvl].link_state;
187}
188
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530189static void ufshcd_tmc_handler(struct ufs_hba *hba);
190static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530191static int ufshcd_reset_and_restore(struct ufs_hba *hba);
192static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300193static void ufshcd_hba_exit(struct ufs_hba *hba);
194static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300195static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
196 bool skip_ref_clk);
197static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
198static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
199static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300200static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300201static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
202static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300203static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
204 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300205static int ufshcd_change_power_mode(struct ufs_hba *hba,
206 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200207static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
208{
209 return tag >= 0 && tag < hba->nutrs;
210}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300211
212static inline int ufshcd_enable_irq(struct ufs_hba *hba)
213{
214 int ret = 0;
215
216 if (!hba->is_irq_enabled) {
217 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
218 hba);
219 if (ret)
220 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
221 __func__, ret);
222 hba->is_irq_enabled = true;
223 }
224
225 return ret;
226}
227
228static inline void ufshcd_disable_irq(struct ufs_hba *hba)
229{
230 if (hba->is_irq_enabled) {
231 free_irq(hba->irq, hba);
232 hba->is_irq_enabled = false;
233 }
234}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530235
Yaniv Gardib573d482016-03-10 17:37:09 +0200236/* replace non-printable or non-ASCII characters with spaces */
237static inline void ufshcd_remove_non_printable(char *val)
238{
239 if (!val)
240 return;
241
242 if (*val < 0x20 || *val > 0x7e)
243 *val = ' ';
244}
245
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530246/*
247 * ufshcd_wait_for_register - wait for register value to change
248 * @hba - per-adapter interface
249 * @reg - mmio register offset
250 * @mask - mask to apply to read register value
251 * @val - wait condition
252 * @interval_us - polling interval in microsecs
253 * @timeout_ms - timeout in millisecs
Yaniv Gardi596585a2016-03-10 17:37:08 +0200254 * @can_sleep - perform sleep or just spin
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530255 *
256 * Returns -ETIMEDOUT on error, zero on success
257 */
Yaniv Gardi596585a2016-03-10 17:37:08 +0200258int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
259 u32 val, unsigned long interval_us,
260 unsigned long timeout_ms, bool can_sleep)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530261{
262 int err = 0;
263 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
264
265 /* ignore bits that we don't intend to wait on */
266 val = val & mask;
267
268 while ((ufshcd_readl(hba, reg) & mask) != val) {
Yaniv Gardi596585a2016-03-10 17:37:08 +0200269 if (can_sleep)
270 usleep_range(interval_us, interval_us + 50);
271 else
272 udelay(interval_us);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530273 if (time_after(jiffies, timeout)) {
274 if ((ufshcd_readl(hba, reg) & mask) != val)
275 err = -ETIMEDOUT;
276 break;
277 }
278 }
279
280 return err;
281}
282
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530283/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530284 * ufshcd_get_intr_mask - Get the interrupt bit mask
285 * @hba - Pointer to adapter instance
286 *
287 * Returns interrupt bit mask per version
288 */
289static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
290{
291 if (hba->ufs_version == UFSHCI_VERSION_10)
292 return INTERRUPT_MASK_ALL_VER_10;
293 else
294 return INTERRUPT_MASK_ALL_VER_11;
295}
296
297/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530298 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
299 * @hba - Pointer to adapter instance
300 *
301 * Returns UFSHCI version supported by the controller
302 */
303static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
304{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200305 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
306 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300307
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530308 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530309}
310
311/**
312 * ufshcd_is_device_present - Check if any device connected to
313 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300314 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530315 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530316 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530317 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300318static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530319{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300320 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
321 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530322}
323
324/**
325 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
326 * @lrb: pointer to local command reference block
327 *
328 * This function is used to get the OCS field from UTRD
329 * Returns the OCS field in the UTRD
330 */
331static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
332{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530333 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530334}
335
336/**
337 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
338 * @task_req_descp: pointer to utp_task_req_desc structure
339 *
340 * This function is used to get the OCS field from UTMRD
341 * Returns the OCS field in the UTMRD
342 */
343static inline int
344ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
345{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530346 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530347}
348
349/**
350 * ufshcd_get_tm_free_slot - get a free slot for task management request
351 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530352 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530353 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530354 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
355 * Returns 0 if free slot is not available, else return 1 with tag value
356 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530357 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530358static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530359{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530360 int tag;
361 bool ret = false;
362
363 if (!free_slot)
364 goto out;
365
366 do {
367 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
368 if (tag >= hba->nutmrs)
369 goto out;
370 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
371
372 *free_slot = tag;
373 ret = true;
374out:
375 return ret;
376}
377
378static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
379{
380 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530381}
382
383/**
384 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
385 * @hba: per adapter instance
386 * @pos: position of the bit to be cleared
387 */
388static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
389{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530390 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530391}
392
393/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200394 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
395 * @hba: per adapter instance
396 * @tag: position of the bit to be cleared
397 */
398static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
399{
400 __clear_bit(tag, &hba->outstanding_reqs);
401}
402
403/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530404 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
405 * @reg: Register value of host controller status
406 *
407 * Returns integer, 0 on Success and positive value if failed
408 */
409static inline int ufshcd_get_lists_status(u32 reg)
410{
411 /*
412 * The mask 0xFF is for the following HCS register bits
413 * Bit Description
414 * 0 Device Present
415 * 1 UTRLRDY
416 * 2 UTMRLRDY
417 * 3 UCRDY
Yaniv Gardi897efe62016-02-01 15:02:48 +0200418 * 4-7 reserved
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530419 */
Yaniv Gardi897efe62016-02-01 15:02:48 +0200420 return ((reg & 0xFF) >> 1) ^ 0x07;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530421}
422
423/**
424 * ufshcd_get_uic_cmd_result - Get the UIC command result
425 * @hba: Pointer to adapter instance
426 *
427 * This function gets the result of UIC command completion
428 * Returns 0 on success, non zero value on error
429 */
430static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
431{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530432 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530433 MASK_UIC_COMMAND_RESULT;
434}
435
436/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530437 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
438 * @hba: Pointer to adapter instance
439 *
440 * This function gets UIC command argument3
441 * Returns 0 on success, non zero value on error
442 */
443static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
444{
445 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
446}
447
448/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530449 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530450 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530451 */
452static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530453ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530454{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530455 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530456}
457
458/**
459 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
460 * @ucd_rsp_ptr: pointer to response UPIU
461 *
462 * This function gets the response status and scsi_status from response UPIU
463 * Returns the response result code.
464 */
465static inline int
466ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
467{
468 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
469}
470
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530471/*
472 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
473 * from response UPIU
474 * @ucd_rsp_ptr: pointer to response UPIU
475 *
476 * Return the data segment length.
477 */
478static inline unsigned int
479ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
480{
481 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
482 MASK_RSP_UPIU_DATA_SEG_LEN;
483}
484
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530485/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530486 * ufshcd_is_exception_event - Check if the device raised an exception event
487 * @ucd_rsp_ptr: pointer to response UPIU
488 *
489 * The function checks if the device raised an exception event indicated in
490 * the Device Information field of response UPIU.
491 *
492 * Returns true if exception is raised, false otherwise.
493 */
494static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
495{
496 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
497 MASK_RSP_EXCEPTION_EVENT ? true : false;
498}
499
500/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530501 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530502 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530503 */
504static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530505ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530506{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530507 ufshcd_writel(hba, INT_AGGR_ENABLE |
508 INT_AGGR_COUNTER_AND_TIMER_RESET,
509 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
510}
511
512/**
513 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
514 * @hba: per adapter instance
515 * @cnt: Interrupt aggregation counter threshold
516 * @tmout: Interrupt aggregation timeout value
517 */
518static inline void
519ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
520{
521 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
522 INT_AGGR_COUNTER_THLD_VAL(cnt) |
523 INT_AGGR_TIMEOUT_VAL(tmout),
524 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530525}
526
527/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300528 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
529 * @hba: per adapter instance
530 */
531static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
532{
533 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
534}
535
536/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530537 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
538 * When run-stop registers are set to 1, it indicates the
539 * host controller that it can process the requests
540 * @hba: per adapter instance
541 */
542static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
543{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530544 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
545 REG_UTP_TASK_REQ_LIST_RUN_STOP);
546 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
547 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530548}
549
550/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530551 * ufshcd_hba_start - Start controller initialization sequence
552 * @hba: per adapter instance
553 */
554static inline void ufshcd_hba_start(struct ufs_hba *hba)
555{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530556 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530557}
558
559/**
560 * ufshcd_is_hba_active - Get controller state
561 * @hba: per adapter instance
562 *
563 * Returns zero if controller is active, 1 otherwise
564 */
565static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
566{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530567 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530568}
569
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300570static void ufshcd_ungate_work(struct work_struct *work)
571{
572 int ret;
573 unsigned long flags;
574 struct ufs_hba *hba = container_of(work, struct ufs_hba,
575 clk_gating.ungate_work);
576
577 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
578
579 spin_lock_irqsave(hba->host->host_lock, flags);
580 if (hba->clk_gating.state == CLKS_ON) {
581 spin_unlock_irqrestore(hba->host->host_lock, flags);
582 goto unblock_reqs;
583 }
584
585 spin_unlock_irqrestore(hba->host->host_lock, flags);
586 ufshcd_setup_clocks(hba, true);
587
588 /* Exit from hibern8 */
589 if (ufshcd_can_hibern8_during_gating(hba)) {
590 /* Prevent gating in this path */
591 hba->clk_gating.is_suspended = true;
592 if (ufshcd_is_link_hibern8(hba)) {
593 ret = ufshcd_uic_hibern8_exit(hba);
594 if (ret)
595 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
596 __func__, ret);
597 else
598 ufshcd_set_link_active(hba);
599 }
600 hba->clk_gating.is_suspended = false;
601 }
602unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300603 if (ufshcd_is_clkscaling_enabled(hba))
604 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300605 scsi_unblock_requests(hba->host);
606}
607
608/**
609 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
610 * Also, exit from hibern8 mode and set the link as active.
611 * @hba: per adapter instance
612 * @async: This indicates whether caller should ungate clocks asynchronously.
613 */
614int ufshcd_hold(struct ufs_hba *hba, bool async)
615{
616 int rc = 0;
617 unsigned long flags;
618
619 if (!ufshcd_is_clkgating_allowed(hba))
620 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300621 spin_lock_irqsave(hba->host->host_lock, flags);
622 hba->clk_gating.active_reqs++;
623
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200624 if (ufshcd_eh_in_progress(hba)) {
625 spin_unlock_irqrestore(hba->host->host_lock, flags);
626 return 0;
627 }
628
Sahitya Tummala856b3482014-09-25 15:32:34 +0300629start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300630 switch (hba->clk_gating.state) {
631 case CLKS_ON:
632 break;
633 case REQ_CLKS_OFF:
634 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
635 hba->clk_gating.state = CLKS_ON;
636 break;
637 }
638 /*
639 * If we here, it means gating work is either done or
640 * currently running. Hence, fall through to cancel gating
641 * work and to enable clocks.
642 */
643 case CLKS_OFF:
644 scsi_block_requests(hba->host);
645 hba->clk_gating.state = REQ_CLKS_ON;
646 schedule_work(&hba->clk_gating.ungate_work);
647 /*
648 * fall through to check if we should wait for this
649 * work to be done or not.
650 */
651 case REQ_CLKS_ON:
652 if (async) {
653 rc = -EAGAIN;
654 hba->clk_gating.active_reqs--;
655 break;
656 }
657
658 spin_unlock_irqrestore(hba->host->host_lock, flags);
659 flush_work(&hba->clk_gating.ungate_work);
660 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300661 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300662 goto start;
663 default:
664 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
665 __func__, hba->clk_gating.state);
666 break;
667 }
668 spin_unlock_irqrestore(hba->host->host_lock, flags);
669out:
670 return rc;
671}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200672EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300673
674static void ufshcd_gate_work(struct work_struct *work)
675{
676 struct ufs_hba *hba = container_of(work, struct ufs_hba,
677 clk_gating.gate_work.work);
678 unsigned long flags;
679
680 spin_lock_irqsave(hba->host->host_lock, flags);
681 if (hba->clk_gating.is_suspended) {
682 hba->clk_gating.state = CLKS_ON;
683 goto rel_lock;
684 }
685
686 if (hba->clk_gating.active_reqs
687 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
688 || hba->lrb_in_use || hba->outstanding_tasks
689 || hba->active_uic_cmd || hba->uic_async_done)
690 goto rel_lock;
691
692 spin_unlock_irqrestore(hba->host->host_lock, flags);
693
694 /* put the link into hibern8 mode before turning off clocks */
695 if (ufshcd_can_hibern8_during_gating(hba)) {
696 if (ufshcd_uic_hibern8_enter(hba)) {
697 hba->clk_gating.state = CLKS_ON;
698 goto out;
699 }
700 ufshcd_set_link_hibern8(hba);
701 }
702
Sahitya Tummala856b3482014-09-25 15:32:34 +0300703 if (ufshcd_is_clkscaling_enabled(hba)) {
704 devfreq_suspend_device(hba->devfreq);
705 hba->clk_scaling.window_start_t = 0;
706 }
707
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300708 if (!ufshcd_is_link_active(hba))
709 ufshcd_setup_clocks(hba, false);
710 else
711 /* If link is active, device ref_clk can't be switched off */
712 __ufshcd_setup_clocks(hba, false, true);
713
714 /*
715 * In case you are here to cancel this work the gating state
716 * would be marked as REQ_CLKS_ON. In this case keep the state
717 * as REQ_CLKS_ON which would anyway imply that clocks are off
718 * and a request to turn them on is pending. By doing this way,
719 * we keep the state machine in tact and this would ultimately
720 * prevent from doing cancel work multiple times when there are
721 * new requests arriving before the current cancel work is done.
722 */
723 spin_lock_irqsave(hba->host->host_lock, flags);
724 if (hba->clk_gating.state == REQ_CLKS_OFF)
725 hba->clk_gating.state = CLKS_OFF;
726
727rel_lock:
728 spin_unlock_irqrestore(hba->host->host_lock, flags);
729out:
730 return;
731}
732
733/* host lock must be held before calling this variant */
734static void __ufshcd_release(struct ufs_hba *hba)
735{
736 if (!ufshcd_is_clkgating_allowed(hba))
737 return;
738
739 hba->clk_gating.active_reqs--;
740
741 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
742 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
743 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200744 || hba->active_uic_cmd || hba->uic_async_done
745 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300746 return;
747
748 hba->clk_gating.state = REQ_CLKS_OFF;
749 schedule_delayed_work(&hba->clk_gating.gate_work,
750 msecs_to_jiffies(hba->clk_gating.delay_ms));
751}
752
753void ufshcd_release(struct ufs_hba *hba)
754{
755 unsigned long flags;
756
757 spin_lock_irqsave(hba->host->host_lock, flags);
758 __ufshcd_release(hba);
759 spin_unlock_irqrestore(hba->host->host_lock, flags);
760}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200761EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300762
763static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
764 struct device_attribute *attr, char *buf)
765{
766 struct ufs_hba *hba = dev_get_drvdata(dev);
767
768 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
769}
770
771static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
772 struct device_attribute *attr, const char *buf, size_t count)
773{
774 struct ufs_hba *hba = dev_get_drvdata(dev);
775 unsigned long flags, value;
776
777 if (kstrtoul(buf, 0, &value))
778 return -EINVAL;
779
780 spin_lock_irqsave(hba->host->host_lock, flags);
781 hba->clk_gating.delay_ms = value;
782 spin_unlock_irqrestore(hba->host->host_lock, flags);
783 return count;
784}
785
786static void ufshcd_init_clk_gating(struct ufs_hba *hba)
787{
788 if (!ufshcd_is_clkgating_allowed(hba))
789 return;
790
791 hba->clk_gating.delay_ms = 150;
792 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
793 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
794
795 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
796 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
797 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
798 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
799 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
800 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
801 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
802}
803
804static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
805{
806 if (!ufshcd_is_clkgating_allowed(hba))
807 return;
808 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900809 cancel_work_sync(&hba->clk_gating.ungate_work);
810 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300811}
812
Sahitya Tummala856b3482014-09-25 15:32:34 +0300813/* Must be called with host lock acquired */
814static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
815{
816 if (!ufshcd_is_clkscaling_enabled(hba))
817 return;
818
819 if (!hba->clk_scaling.is_busy_started) {
820 hba->clk_scaling.busy_start_t = ktime_get();
821 hba->clk_scaling.is_busy_started = true;
822 }
823}
824
825static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
826{
827 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
828
829 if (!ufshcd_is_clkscaling_enabled(hba))
830 return;
831
832 if (!hba->outstanding_reqs && scaling->is_busy_started) {
833 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
834 scaling->busy_start_t));
835 scaling->busy_start_t = ktime_set(0, 0);
836 scaling->is_busy_started = false;
837 }
838}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530839/**
840 * ufshcd_send_command - Send SCSI or device management commands
841 * @hba: per adapter instance
842 * @task_tag: Task tag of the command
843 */
844static inline
845void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
846{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300847 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530848 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530849 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530850}
851
852/**
853 * ufshcd_copy_sense_data - Copy sense data in case of check condition
854 * @lrb - pointer to local reference block
855 */
856static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
857{
858 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530859 if (lrbp->sense_buffer &&
860 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530861 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530862 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530863 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530864 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
865 }
866}
867
868/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530869 * ufshcd_copy_query_response() - Copy the Query Response and the data
870 * descriptor
871 * @hba: per adapter instance
872 * @lrb - pointer to local reference block
873 */
874static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300875int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530876{
877 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
878
Dolev Raviv68078d52013-07-30 00:35:58 +0530879 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530880
Dolev Raviv68078d52013-07-30 00:35:58 +0530881 /* Get the descriptor */
882 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300883 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530884 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300885 u16 resp_len;
886 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530887
888 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300889 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530890 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300891 buf_len = be16_to_cpu(
892 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300893 if (likely(buf_len >= resp_len)) {
894 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
895 } else {
896 dev_warn(hba->dev,
897 "%s: Response size is bigger than buffer",
898 __func__);
899 return -EINVAL;
900 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530901 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300902
903 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530904}
905
906/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530907 * ufshcd_hba_capabilities - Read controller capabilities
908 * @hba: per adapter instance
909 */
910static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
911{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530912 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530913
914 /* nutrs and nutmrs are 0 based values */
915 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
916 hba->nutmrs =
917 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
918}
919
920/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530921 * ufshcd_ready_for_uic_cmd - Check if controller is ready
922 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530923 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530924 * Return true on success, else false
925 */
926static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
927{
928 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
929 return true;
930 else
931 return false;
932}
933
934/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530935 * ufshcd_get_upmcrs - Get the power mode change request status
936 * @hba: Pointer to adapter instance
937 *
938 * This function gets the UPMCRS field of HCS register
939 * Returns value of UPMCRS field
940 */
941static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
942{
943 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
944}
945
946/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530947 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
948 * @hba: per adapter instance
949 * @uic_cmd: UIC command
950 *
951 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530952 */
953static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530954ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530955{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530956 WARN_ON(hba->active_uic_cmd);
957
958 hba->active_uic_cmd = uic_cmd;
959
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530960 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530961 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
962 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
963 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530964
965 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530966 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530967 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530968}
969
970/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530971 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
972 * @hba: per adapter instance
973 * @uic_command: UIC command
974 *
975 * Must be called with mutex held.
976 * Returns 0 only if success.
977 */
978static int
979ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
980{
981 int ret;
982 unsigned long flags;
983
984 if (wait_for_completion_timeout(&uic_cmd->done,
985 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
986 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
987 else
988 ret = -ETIMEDOUT;
989
990 spin_lock_irqsave(hba->host->host_lock, flags);
991 hba->active_uic_cmd = NULL;
992 spin_unlock_irqrestore(hba->host->host_lock, flags);
993
994 return ret;
995}
996
997/**
998 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
999 * @hba: per adapter instance
1000 * @uic_cmd: UIC command
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001001 * @completion: initialize the completion only if this is set to true
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301002 *
1003 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001004 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301005 * Returns 0 only if success.
1006 */
1007static int
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001008__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd,
1009 bool completion)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301010{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301011 if (!ufshcd_ready_for_uic_cmd(hba)) {
1012 dev_err(hba->dev,
1013 "Controller not ready to accept UIC commands\n");
1014 return -EIO;
1015 }
1016
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001017 if (completion)
1018 init_completion(&uic_cmd->done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301019
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301020 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301021
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001022 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301023}
1024
1025/**
1026 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1027 * @hba: per adapter instance
1028 * @uic_cmd: UIC command
1029 *
1030 * Returns 0 only if success.
1031 */
1032static int
1033ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1034{
1035 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001036 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301037
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001038 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301039 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001040 ufshcd_add_delay_before_dme_cmd(hba);
1041
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001042 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02001043 ret = __ufshcd_send_uic_cmd(hba, uic_cmd, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001044 spin_unlock_irqrestore(hba->host->host_lock, flags);
1045 if (!ret)
1046 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1047
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301048 mutex_unlock(&hba->uic_cmd_mutex);
1049
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001050 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301051 return ret;
1052}
1053
1054/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301055 * ufshcd_map_sg - Map scatter-gather list to prdt
1056 * @lrbp - pointer to local reference block
1057 *
1058 * Returns 0 in case of success, non-zero value in case of failure
1059 */
1060static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1061{
1062 struct ufshcd_sg_entry *prd_table;
1063 struct scatterlist *sg;
1064 struct scsi_cmnd *cmd;
1065 int sg_segments;
1066 int i;
1067
1068 cmd = lrbp->cmd;
1069 sg_segments = scsi_dma_map(cmd);
1070 if (sg_segments < 0)
1071 return sg_segments;
1072
1073 if (sg_segments) {
1074 lrbp->utr_descriptor_ptr->prd_table_length =
1075 cpu_to_le16((u16) (sg_segments));
1076
1077 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1078
1079 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1080 prd_table[i].size =
1081 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1082 prd_table[i].base_addr =
1083 cpu_to_le32(lower_32_bits(sg->dma_address));
1084 prd_table[i].upper_addr =
1085 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001086 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301087 }
1088 } else {
1089 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1090 }
1091
1092 return 0;
1093}
1094
1095/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301096 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301097 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301098 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301099 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301100static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301101{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301102 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1103
1104 if (hba->ufs_version == UFSHCI_VERSION_10) {
1105 u32 rw;
1106 rw = set & INTERRUPT_MASK_RW_VER_10;
1107 set = rw | ((set ^ intrs) & intrs);
1108 } else {
1109 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301110 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301111
1112 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1113}
1114
1115/**
1116 * ufshcd_disable_intr - disable interrupts
1117 * @hba: per adapter instance
1118 * @intrs: interrupt bits
1119 */
1120static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1121{
1122 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1123
1124 if (hba->ufs_version == UFSHCI_VERSION_10) {
1125 u32 rw;
1126 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1127 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1128 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1129
1130 } else {
1131 set &= ~intrs;
1132 }
1133
1134 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301135}
1136
1137/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301138 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1139 * descriptor according to request
1140 * @lrbp: pointer to local reference block
1141 * @upiu_flags: flags required in the header
1142 * @cmd_dir: requests data direction
1143 */
1144static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
1145 u32 *upiu_flags, enum dma_data_direction cmd_dir)
1146{
1147 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1148 u32 data_direction;
1149 u32 dword_0;
1150
1151 if (cmd_dir == DMA_FROM_DEVICE) {
1152 data_direction = UTP_DEVICE_TO_HOST;
1153 *upiu_flags = UPIU_CMD_FLAGS_READ;
1154 } else if (cmd_dir == DMA_TO_DEVICE) {
1155 data_direction = UTP_HOST_TO_DEVICE;
1156 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1157 } else {
1158 data_direction = UTP_NO_DATA_TRANSFER;
1159 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1160 }
1161
1162 dword_0 = data_direction | (lrbp->command_type
1163 << UPIU_COMMAND_TYPE_OFFSET);
1164 if (lrbp->intr_cmd)
1165 dword_0 |= UTP_REQ_DESC_INT_CMD;
1166
1167 /* Transfer request descriptor header fields */
1168 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001169 /* dword_1 is reserved, hence it is set to 0 */
1170 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301171 /*
1172 * assigning invalid value for command status. Controller
1173 * updates OCS on command completion, with the command
1174 * status
1175 */
1176 req_desc->header.dword_2 =
1177 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001178 /* dword_3 is reserved, hence it is set to 0 */
1179 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001180
1181 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301182}
1183
1184/**
1185 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1186 * for scsi commands
1187 * @lrbp - local reference block pointer
1188 * @upiu_flags - flags
1189 */
1190static
1191void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1192{
1193 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001194 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301195
1196 /* command descriptor fields */
1197 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1198 UPIU_TRANSACTION_COMMAND, upiu_flags,
1199 lrbp->lun, lrbp->task_tag);
1200 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1201 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1202
1203 /* Total EHS length and Data segment length will be zero */
1204 ucd_req_ptr->header.dword_2 = 0;
1205
1206 ucd_req_ptr->sc.exp_data_transfer_len =
1207 cpu_to_be32(lrbp->cmd->sdb.length);
1208
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001209 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1210 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1211 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1212
1213 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301214}
1215
Dolev Raviv68078d52013-07-30 00:35:58 +05301216/**
1217 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1218 * for query requsts
1219 * @hba: UFS hba
1220 * @lrbp: local reference block pointer
1221 * @upiu_flags: flags
1222 */
1223static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1224 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1225{
1226 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1227 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301228 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301229 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1230
1231 /* Query request header */
1232 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1233 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1234 lrbp->lun, lrbp->task_tag);
1235 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1236 0, query->request.query_func, 0, 0);
1237
1238 /* Data segment length */
1239 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
1240 0, 0, len >> 8, (u8)len);
1241
1242 /* Copy the Query Request buffer as is */
1243 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1244 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301245
1246 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001247 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1248 memcpy(descp, query->descriptor, len);
1249
Yaniv Gardi51047262016-02-01 15:02:38 +02001250 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301251}
1252
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301253static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1254{
1255 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1256
1257 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1258
1259 /* command descriptor fields */
1260 ucd_req_ptr->header.dword_0 =
1261 UPIU_HEADER_DWORD(
1262 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001263 /* clear rest of the fields of basic header */
1264 ucd_req_ptr->header.dword_1 = 0;
1265 ucd_req_ptr->header.dword_2 = 0;
1266
1267 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301268}
1269
1270/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301271 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301272 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301273 * @lrb - pointer to local reference block
1274 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301275static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301276{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301277 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301278 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301279
1280 switch (lrbp->command_type) {
1281 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301282 if (likely(lrbp->cmd)) {
1283 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1284 lrbp->cmd->sc_data_direction);
1285 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301286 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301287 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301288 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301289 break;
1290 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301291 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301292 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1293 ufshcd_prepare_utp_query_req_upiu(
1294 hba, lrbp, upiu_flags);
1295 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301296 ufshcd_prepare_utp_nop_upiu(lrbp);
1297 else
1298 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301299 break;
1300 case UTP_CMD_TYPE_UFS:
1301 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301302 ret = -ENOTSUPP;
1303 dev_err(hba->dev, "%s: UFS native command are not supported\n",
1304 __func__);
1305 break;
1306 default:
1307 ret = -ENOTSUPP;
1308 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
1309 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301310 break;
1311 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301312
1313 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301314}
1315
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001316/*
1317 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1318 * @scsi_lun: scsi LUN id
1319 *
1320 * Returns UPIU LUN id
1321 */
1322static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1323{
1324 if (scsi_is_wlun(scsi_lun))
1325 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1326 | UFS_UPIU_WLUN_ID;
1327 else
1328 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1329}
1330
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301331/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001332 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1333 * @scsi_lun: UPIU W-LUN id
1334 *
1335 * Returns SCSI W-LUN id
1336 */
1337static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1338{
1339 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1340}
1341
1342/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301343 * ufshcd_queuecommand - main entry point for SCSI requests
1344 * @cmd: command from SCSI Midlayer
1345 * @done: call back function
1346 *
1347 * Returns 0 for success, non-zero in case of failure
1348 */
1349static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1350{
1351 struct ufshcd_lrb *lrbp;
1352 struct ufs_hba *hba;
1353 unsigned long flags;
1354 int tag;
1355 int err = 0;
1356
1357 hba = shost_priv(host);
1358
1359 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001360 if (!ufshcd_valid_tag(hba, tag)) {
1361 dev_err(hba->dev,
1362 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1363 __func__, tag, cmd, cmd->request);
1364 BUG();
1365 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301366
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301367 spin_lock_irqsave(hba->host->host_lock, flags);
1368 switch (hba->ufshcd_state) {
1369 case UFSHCD_STATE_OPERATIONAL:
1370 break;
1371 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301372 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301373 goto out_unlock;
1374 case UFSHCD_STATE_ERROR:
1375 set_host_byte(cmd, DID_ERROR);
1376 cmd->scsi_done(cmd);
1377 goto out_unlock;
1378 default:
1379 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1380 __func__, hba->ufshcd_state);
1381 set_host_byte(cmd, DID_BAD_TARGET);
1382 cmd->scsi_done(cmd);
1383 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301384 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001385
1386 /* if error handling is in progress, don't issue commands */
1387 if (ufshcd_eh_in_progress(hba)) {
1388 set_host_byte(cmd, DID_ERROR);
1389 cmd->scsi_done(cmd);
1390 goto out_unlock;
1391 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301392 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301393
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301394 /* acquire the tag to make sure device cmds don't use it */
1395 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1396 /*
1397 * Dev manage command in progress, requeue the command.
1398 * Requeuing the command helps in cases where the request *may*
1399 * find different tag instead of waiting for dev manage command
1400 * completion.
1401 */
1402 err = SCSI_MLQUEUE_HOST_BUSY;
1403 goto out;
1404 }
1405
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001406 err = ufshcd_hold(hba, true);
1407 if (err) {
1408 err = SCSI_MLQUEUE_HOST_BUSY;
1409 clear_bit_unlock(tag, &hba->lrb_in_use);
1410 goto out;
1411 }
1412 WARN_ON(hba->clk_gating.state != CLKS_ON);
1413
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301414 lrbp = &hba->lrb[tag];
1415
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301416 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301417 lrbp->cmd = cmd;
1418 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1419 lrbp->sense_buffer = cmd->sense_buffer;
1420 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001421 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001422 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301423 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1424
1425 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301426 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301427 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301428 if (err) {
1429 lrbp->cmd = NULL;
1430 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301431 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301432 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301433
1434 /* issue command to the controller */
1435 spin_lock_irqsave(hba->host->host_lock, flags);
1436 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301437out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301438 spin_unlock_irqrestore(hba->host->host_lock, flags);
1439out:
1440 return err;
1441}
1442
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301443static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1444 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1445{
1446 lrbp->cmd = NULL;
1447 lrbp->sense_bufflen = 0;
1448 lrbp->sense_buffer = NULL;
1449 lrbp->task_tag = tag;
1450 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
1451 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1452 lrbp->intr_cmd = true; /* No interrupt aggregation */
1453 hba->dev_cmd.type = cmd_type;
1454
1455 return ufshcd_compose_upiu(hba, lrbp);
1456}
1457
1458static int
1459ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1460{
1461 int err = 0;
1462 unsigned long flags;
1463 u32 mask = 1 << tag;
1464
1465 /* clear outstanding transaction before retry */
1466 spin_lock_irqsave(hba->host->host_lock, flags);
1467 ufshcd_utrl_clear(hba, tag);
1468 spin_unlock_irqrestore(hba->host->host_lock, flags);
1469
1470 /*
1471 * wait for for h/w to clear corresponding bit in door-bell.
1472 * max. wait is 1 sec.
1473 */
1474 err = ufshcd_wait_for_register(hba,
1475 REG_UTP_TRANSFER_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02001476 mask, ~mask, 1000, 1000, true);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301477
1478 return err;
1479}
1480
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001481static int
1482ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1483{
1484 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1485
1486 /* Get the UPIU response */
1487 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1488 UPIU_RSP_CODE_OFFSET;
1489 return query_res->response;
1490}
1491
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301492/**
1493 * ufshcd_dev_cmd_completion() - handles device management command responses
1494 * @hba: per adapter instance
1495 * @lrbp: pointer to local reference block
1496 */
1497static int
1498ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1499{
1500 int resp;
1501 int err = 0;
1502
1503 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1504
1505 switch (resp) {
1506 case UPIU_TRANSACTION_NOP_IN:
1507 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1508 err = -EINVAL;
1509 dev_err(hba->dev, "%s: unexpected response %x\n",
1510 __func__, resp);
1511 }
1512 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301513 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001514 err = ufshcd_check_query_response(hba, lrbp);
1515 if (!err)
1516 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301517 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301518 case UPIU_TRANSACTION_REJECT_UPIU:
1519 /* TODO: handle Reject UPIU Response */
1520 err = -EPERM;
1521 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1522 __func__);
1523 break;
1524 default:
1525 err = -EINVAL;
1526 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1527 __func__, resp);
1528 break;
1529 }
1530
1531 return err;
1532}
1533
1534static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1535 struct ufshcd_lrb *lrbp, int max_timeout)
1536{
1537 int err = 0;
1538 unsigned long time_left;
1539 unsigned long flags;
1540
1541 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1542 msecs_to_jiffies(max_timeout));
1543
1544 spin_lock_irqsave(hba->host->host_lock, flags);
1545 hba->dev_cmd.complete = NULL;
1546 if (likely(time_left)) {
1547 err = ufshcd_get_tr_ocs(lrbp);
1548 if (!err)
1549 err = ufshcd_dev_cmd_completion(hba, lrbp);
1550 }
1551 spin_unlock_irqrestore(hba->host->host_lock, flags);
1552
1553 if (!time_left) {
1554 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001555 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1556 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301557 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001558 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301559 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001560 /*
1561 * in case of an error, after clearing the doorbell,
1562 * we also need to clear the outstanding_request
1563 * field in hba
1564 */
1565 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301566 }
1567
1568 return err;
1569}
1570
1571/**
1572 * ufshcd_get_dev_cmd_tag - Get device management command tag
1573 * @hba: per-adapter instance
1574 * @tag: pointer to variable with available slot value
1575 *
1576 * Get a free slot and lock it until device management command
1577 * completes.
1578 *
1579 * Returns false if free slot is unavailable for locking, else
1580 * return true with tag value in @tag.
1581 */
1582static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1583{
1584 int tag;
1585 bool ret = false;
1586 unsigned long tmp;
1587
1588 if (!tag_out)
1589 goto out;
1590
1591 do {
1592 tmp = ~hba->lrb_in_use;
1593 tag = find_last_bit(&tmp, hba->nutrs);
1594 if (tag >= hba->nutrs)
1595 goto out;
1596 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1597
1598 *tag_out = tag;
1599 ret = true;
1600out:
1601 return ret;
1602}
1603
1604static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1605{
1606 clear_bit_unlock(tag, &hba->lrb_in_use);
1607}
1608
1609/**
1610 * ufshcd_exec_dev_cmd - API for sending device management requests
1611 * @hba - UFS hba
1612 * @cmd_type - specifies the type (NOP, Query...)
1613 * @timeout - time in seconds
1614 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301615 * NOTE: Since there is only one available tag for device management commands,
1616 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301617 */
1618static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1619 enum dev_cmd_type cmd_type, int timeout)
1620{
1621 struct ufshcd_lrb *lrbp;
1622 int err;
1623 int tag;
1624 struct completion wait;
1625 unsigned long flags;
1626
1627 /*
1628 * Get free slot, sleep if slots are unavailable.
1629 * Even though we use wait_event() which sleeps indefinitely,
1630 * the maximum wait time is bounded by SCSI request timeout.
1631 */
1632 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1633
1634 init_completion(&wait);
1635 lrbp = &hba->lrb[tag];
1636 WARN_ON(lrbp->cmd);
1637 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1638 if (unlikely(err))
1639 goto out_put_tag;
1640
1641 hba->dev_cmd.complete = &wait;
1642
Yaniv Gardie3dfdc52016-02-01 15:02:49 +02001643 /* Make sure descriptors are ready before ringing the doorbell */
1644 wmb();
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301645 spin_lock_irqsave(hba->host->host_lock, flags);
1646 ufshcd_send_command(hba, tag);
1647 spin_unlock_irqrestore(hba->host->host_lock, flags);
1648
1649 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1650
1651out_put_tag:
1652 ufshcd_put_dev_cmd_tag(hba, tag);
1653 wake_up(&hba->dev_cmd.tag_wq);
1654 return err;
1655}
1656
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301657/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001658 * ufshcd_init_query() - init the query response and request parameters
1659 * @hba: per-adapter instance
1660 * @request: address of the request pointer to be initialized
1661 * @response: address of the response pointer to be initialized
1662 * @opcode: operation to perform
1663 * @idn: flag idn to access
1664 * @index: LU number to access
1665 * @selector: query/flag/descriptor further identification
1666 */
1667static inline void ufshcd_init_query(struct ufs_hba *hba,
1668 struct ufs_query_req **request, struct ufs_query_res **response,
1669 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1670{
1671 *request = &hba->dev_cmd.query.request;
1672 *response = &hba->dev_cmd.query.response;
1673 memset(*request, 0, sizeof(struct ufs_query_req));
1674 memset(*response, 0, sizeof(struct ufs_query_res));
1675 (*request)->upiu_req.opcode = opcode;
1676 (*request)->upiu_req.idn = idn;
1677 (*request)->upiu_req.index = index;
1678 (*request)->upiu_req.selector = selector;
1679}
1680
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001681static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1682 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1683{
1684 int ret;
1685 int retries;
1686
1687 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1688 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1689 if (ret)
1690 dev_dbg(hba->dev,
1691 "%s: failed with error %d, retries %d\n",
1692 __func__, ret, retries);
1693 else
1694 break;
1695 }
1696
1697 if (ret)
1698 dev_err(hba->dev,
1699 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1700 __func__, opcode, idn, ret, retries);
1701 return ret;
1702}
1703
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001704/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301705 * ufshcd_query_flag() - API function for sending flag query requests
1706 * hba: per-adapter instance
1707 * query_opcode: flag query to perform
1708 * idn: flag idn to access
1709 * flag_res: the flag value after the query request completes
1710 *
1711 * Returns 0 for success, non-zero in case of failure
1712 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001713int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301714 enum flag_idn idn, bool *flag_res)
1715{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001716 struct ufs_query_req *request = NULL;
1717 struct ufs_query_res *response = NULL;
1718 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001719 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301720
1721 BUG_ON(!hba);
1722
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001723 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301724 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001725 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1726 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301727
1728 switch (opcode) {
1729 case UPIU_QUERY_OPCODE_SET_FLAG:
1730 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1731 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1732 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1733 break;
1734 case UPIU_QUERY_OPCODE_READ_FLAG:
1735 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1736 if (!flag_res) {
1737 /* No dummy reads */
1738 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1739 __func__);
1740 err = -EINVAL;
1741 goto out_unlock;
1742 }
1743 break;
1744 default:
1745 dev_err(hba->dev,
1746 "%s: Expected query flag opcode but got = %d\n",
1747 __func__, opcode);
1748 err = -EINVAL;
1749 goto out_unlock;
1750 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301751
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001752 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1753 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1754
1755 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301756
1757 if (err) {
1758 dev_err(hba->dev,
1759 "%s: Sending flag query for idn %d failed, err = %d\n",
1760 __func__, idn, err);
1761 goto out_unlock;
1762 }
1763
1764 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301765 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301766 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1767
1768out_unlock:
1769 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001770 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301771 return err;
1772}
1773
1774/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301775 * ufshcd_query_attr - API function for sending attribute requests
1776 * hba: per-adapter instance
1777 * opcode: attribute opcode
1778 * idn: attribute idn to access
1779 * index: index field
1780 * selector: selector field
1781 * attr_val: the attribute value after the query request completes
1782 *
1783 * Returns 0 for success, non-zero in case of failure
1784*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301785static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301786 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1787{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001788 struct ufs_query_req *request = NULL;
1789 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301790 int err;
1791
1792 BUG_ON(!hba);
1793
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001794 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301795 if (!attr_val) {
1796 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1797 __func__, opcode);
1798 err = -EINVAL;
1799 goto out;
1800 }
1801
1802 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001803 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1804 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301805
1806 switch (opcode) {
1807 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1808 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301809 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301810 break;
1811 case UPIU_QUERY_OPCODE_READ_ATTR:
1812 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1813 break;
1814 default:
1815 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1816 __func__, opcode);
1817 err = -EINVAL;
1818 goto out_unlock;
1819 }
1820
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001821 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301822
1823 if (err) {
1824 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1825 __func__, opcode, idn, err);
1826 goto out_unlock;
1827 }
1828
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301829 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301830
1831out_unlock:
1832 mutex_unlock(&hba->dev_cmd.lock);
1833out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001834 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301835 return err;
1836}
1837
1838/**
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02001839 * ufshcd_query_attr_retry() - API function for sending query
1840 * attribute with retries
1841 * @hba: per-adapter instance
1842 * @opcode: attribute opcode
1843 * @idn: attribute idn to access
1844 * @index: index field
1845 * @selector: selector field
1846 * @attr_val: the attribute value after the query request
1847 * completes
1848 *
1849 * Returns 0 for success, non-zero in case of failure
1850*/
1851static int ufshcd_query_attr_retry(struct ufs_hba *hba,
1852 enum query_opcode opcode, enum attr_idn idn, u8 index, u8 selector,
1853 u32 *attr_val)
1854{
1855 int ret = 0;
1856 u32 retries;
1857
1858 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1859 ret = ufshcd_query_attr(hba, opcode, idn, index,
1860 selector, attr_val);
1861 if (ret)
1862 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n",
1863 __func__, ret, retries);
1864 else
1865 break;
1866 }
1867
1868 if (ret)
1869 dev_err(hba->dev,
1870 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
1871 __func__, idn, ret, QUERY_REQ_RETRIES);
1872 return ret;
1873}
1874
1875/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001876 * ufshcd_query_descriptor - API function for sending descriptor requests
1877 * hba: per-adapter instance
1878 * opcode: attribute opcode
1879 * idn: attribute idn to access
1880 * index: index field
1881 * selector: selector field
1882 * desc_buf: the buffer that contains the descriptor
1883 * buf_len: length parameter passed to the device
1884 *
1885 * Returns 0 for success, non-zero in case of failure.
1886 * The buf_len parameter will contain, on return, the length parameter
1887 * received on the response.
1888 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001889static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001890 enum query_opcode opcode, enum desc_idn idn, u8 index,
1891 u8 selector, u8 *desc_buf, int *buf_len)
1892{
1893 struct ufs_query_req *request = NULL;
1894 struct ufs_query_res *response = NULL;
1895 int err;
1896
1897 BUG_ON(!hba);
1898
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001899 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001900 if (!desc_buf) {
1901 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1902 __func__, opcode);
1903 err = -EINVAL;
1904 goto out;
1905 }
1906
1907 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1908 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1909 __func__, *buf_len);
1910 err = -EINVAL;
1911 goto out;
1912 }
1913
1914 mutex_lock(&hba->dev_cmd.lock);
1915 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1916 selector);
1917 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001918 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001919
1920 switch (opcode) {
1921 case UPIU_QUERY_OPCODE_WRITE_DESC:
1922 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1923 break;
1924 case UPIU_QUERY_OPCODE_READ_DESC:
1925 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1926 break;
1927 default:
1928 dev_err(hba->dev,
1929 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1930 __func__, opcode);
1931 err = -EINVAL;
1932 goto out_unlock;
1933 }
1934
1935 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1936
1937 if (err) {
1938 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1939 __func__, opcode, idn, err);
1940 goto out_unlock;
1941 }
1942
1943 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001944 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001945
1946out_unlock:
1947 mutex_unlock(&hba->dev_cmd.lock);
1948out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001949 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001950 return err;
1951}
1952
1953/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001954 * ufshcd_read_desc_param - read the specified descriptor parameter
1955 * @hba: Pointer to adapter instance
1956 * @desc_id: descriptor idn value
1957 * @desc_index: descriptor index
1958 * @param_offset: offset of the parameter to read
1959 * @param_read_buf: pointer to buffer where parameter would be read
1960 * @param_size: sizeof(param_read_buf)
1961 *
1962 * Return 0 in case of success, non-zero otherwise
1963 */
1964static int ufshcd_read_desc_param(struct ufs_hba *hba,
1965 enum desc_idn desc_id,
1966 int desc_index,
1967 u32 param_offset,
1968 u8 *param_read_buf,
1969 u32 param_size)
1970{
1971 int ret;
1972 u8 *desc_buf;
1973 u32 buff_len;
1974 bool is_kmalloc = true;
1975
1976 /* safety checks */
1977 if (desc_id >= QUERY_DESC_IDN_MAX)
1978 return -EINVAL;
1979
1980 buff_len = ufs_query_desc_max_size[desc_id];
1981 if ((param_offset + param_size) > buff_len)
1982 return -EINVAL;
1983
1984 if (!param_offset && (param_size == buff_len)) {
1985 /* memory space already available to hold full descriptor */
1986 desc_buf = param_read_buf;
1987 is_kmalloc = false;
1988 } else {
1989 /* allocate memory to hold full descriptor */
1990 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1991 if (!desc_buf)
1992 return -ENOMEM;
1993 }
1994
1995 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1996 desc_id, desc_index, 0, desc_buf,
1997 &buff_len);
1998
1999 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
2000 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
2001 ufs_query_desc_max_size[desc_id])
2002 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
2003 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
2004 __func__, desc_id, param_offset, buff_len, ret);
2005 if (!ret)
2006 ret = -EINVAL;
2007
2008 goto out;
2009 }
2010
2011 if (is_kmalloc)
2012 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
2013out:
2014 if (is_kmalloc)
2015 kfree(desc_buf);
2016 return ret;
2017}
2018
2019static inline int ufshcd_read_desc(struct ufs_hba *hba,
2020 enum desc_idn desc_id,
2021 int desc_index,
2022 u8 *buf,
2023 u32 size)
2024{
2025 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
2026}
2027
2028static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
2029 u8 *buf,
2030 u32 size)
2031{
2032 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
2033}
2034
Yaniv Gardib573d482016-03-10 17:37:09 +02002035int ufshcd_read_device_desc(struct ufs_hba *hba, u8 *buf, u32 size)
2036{
2037 return ufshcd_read_desc(hba, QUERY_DESC_IDN_DEVICE, 0, buf, size);
2038}
2039EXPORT_SYMBOL(ufshcd_read_device_desc);
2040
2041/**
2042 * ufshcd_read_string_desc - read string descriptor
2043 * @hba: pointer to adapter instance
2044 * @desc_index: descriptor index
2045 * @buf: pointer to buffer where descriptor would be read
2046 * @size: size of buf
2047 * @ascii: if true convert from unicode to ascii characters
2048 *
2049 * Return 0 in case of success, non-zero otherwise
2050 */
2051int ufshcd_read_string_desc(struct ufs_hba *hba, int desc_index, u8 *buf,
2052 u32 size, bool ascii)
2053{
2054 int err = 0;
2055
2056 err = ufshcd_read_desc(hba,
2057 QUERY_DESC_IDN_STRING, desc_index, buf, size);
2058
2059 if (err) {
2060 dev_err(hba->dev, "%s: reading String Desc failed after %d retries. err = %d\n",
2061 __func__, QUERY_REQ_RETRIES, err);
2062 goto out;
2063 }
2064
2065 if (ascii) {
2066 int desc_len;
2067 int ascii_len;
2068 int i;
2069 char *buff_ascii;
2070
2071 desc_len = buf[0];
2072 /* remove header and divide by 2 to move from UTF16 to UTF8 */
2073 ascii_len = (desc_len - QUERY_DESC_HDR_SIZE) / 2 + 1;
2074 if (size < ascii_len + QUERY_DESC_HDR_SIZE) {
2075 dev_err(hba->dev, "%s: buffer allocated size is too small\n",
2076 __func__);
2077 err = -ENOMEM;
2078 goto out;
2079 }
2080
2081 buff_ascii = kmalloc(ascii_len, GFP_KERNEL);
2082 if (!buff_ascii) {
2083 err = -ENOMEM;
2084 goto out_free_buff;
2085 }
2086
2087 /*
2088 * the descriptor contains string in UTF16 format
2089 * we need to convert to utf-8 so it can be displayed
2090 */
2091 utf16s_to_utf8s((wchar_t *)&buf[QUERY_DESC_HDR_SIZE],
2092 desc_len - QUERY_DESC_HDR_SIZE,
2093 UTF16_BIG_ENDIAN, buff_ascii, ascii_len);
2094
2095 /* replace non-printable or non-ASCII characters with spaces */
2096 for (i = 0; i < ascii_len; i++)
2097 ufshcd_remove_non_printable(&buff_ascii[i]);
2098
2099 memset(buf + QUERY_DESC_HDR_SIZE, 0,
2100 size - QUERY_DESC_HDR_SIZE);
2101 memcpy(buf + QUERY_DESC_HDR_SIZE, buff_ascii, ascii_len);
2102 buf[QUERY_DESC_LENGTH_OFFSET] = ascii_len + QUERY_DESC_HDR_SIZE;
2103out_free_buff:
2104 kfree(buff_ascii);
2105 }
2106out:
2107 return err;
2108}
2109EXPORT_SYMBOL(ufshcd_read_string_desc);
2110
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002111/**
2112 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
2113 * @hba: Pointer to adapter instance
2114 * @lun: lun id
2115 * @param_offset: offset of the parameter to read
2116 * @param_read_buf: pointer to buffer where parameter would be read
2117 * @param_size: sizeof(param_read_buf)
2118 *
2119 * Return 0 in case of success, non-zero otherwise
2120 */
2121static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
2122 int lun,
2123 enum unit_desc_param param_offset,
2124 u8 *param_read_buf,
2125 u32 param_size)
2126{
2127 /*
2128 * Unit descriptors are only available for general purpose LUs (LUN id
2129 * from 0 to 7) and RPMB Well known LU.
2130 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002131 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002132 return -EOPNOTSUPP;
2133
2134 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2135 param_offset, param_read_buf, param_size);
2136}
2137
2138/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302139 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2140 * @hba: per adapter instance
2141 *
2142 * 1. Allocate DMA memory for Command Descriptor array
2143 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2144 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2145 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2146 * (UTMRDL)
2147 * 4. Allocate memory for local reference block(lrb).
2148 *
2149 * Returns 0 for success, non-zero in case of failure
2150 */
2151static int ufshcd_memory_alloc(struct ufs_hba *hba)
2152{
2153 size_t utmrdl_size, utrdl_size, ucdl_size;
2154
2155 /* Allocate memory for UTP command descriptors */
2156 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002157 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2158 ucdl_size,
2159 &hba->ucdl_dma_addr,
2160 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302161
2162 /*
2163 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2164 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2165 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2166 * be aligned to 128 bytes as well
2167 */
2168 if (!hba->ucdl_base_addr ||
2169 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302170 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302171 "Command Descriptor Memory allocation failed\n");
2172 goto out;
2173 }
2174
2175 /*
2176 * Allocate memory for UTP Transfer descriptors
2177 * UFSHCI requires 1024 byte alignment of UTRD
2178 */
2179 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002180 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2181 utrdl_size,
2182 &hba->utrdl_dma_addr,
2183 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302184 if (!hba->utrdl_base_addr ||
2185 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302186 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302187 "Transfer Descriptor Memory allocation failed\n");
2188 goto out;
2189 }
2190
2191 /*
2192 * Allocate memory for UTP Task Management descriptors
2193 * UFSHCI requires 1024 byte alignment of UTMRD
2194 */
2195 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002196 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2197 utmrdl_size,
2198 &hba->utmrdl_dma_addr,
2199 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302200 if (!hba->utmrdl_base_addr ||
2201 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302202 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302203 "Task Management Descriptor Memory allocation failed\n");
2204 goto out;
2205 }
2206
2207 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002208 hba->lrb = devm_kzalloc(hba->dev,
2209 hba->nutrs * sizeof(struct ufshcd_lrb),
2210 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302211 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302212 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302213 goto out;
2214 }
2215 return 0;
2216out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302217 return -ENOMEM;
2218}
2219
2220/**
2221 * ufshcd_host_memory_configure - configure local reference block with
2222 * memory offsets
2223 * @hba: per adapter instance
2224 *
2225 * Configure Host memory space
2226 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2227 * address.
2228 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2229 * and PRDT offset.
2230 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2231 * into local reference block.
2232 */
2233static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2234{
2235 struct utp_transfer_cmd_desc *cmd_descp;
2236 struct utp_transfer_req_desc *utrdlp;
2237 dma_addr_t cmd_desc_dma_addr;
2238 dma_addr_t cmd_desc_element_addr;
2239 u16 response_offset;
2240 u16 prdt_offset;
2241 int cmd_desc_size;
2242 int i;
2243
2244 utrdlp = hba->utrdl_base_addr;
2245 cmd_descp = hba->ucdl_base_addr;
2246
2247 response_offset =
2248 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2249 prdt_offset =
2250 offsetof(struct utp_transfer_cmd_desc, prd_table);
2251
2252 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2253 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2254
2255 for (i = 0; i < hba->nutrs; i++) {
2256 /* Configure UTRD with command descriptor base address */
2257 cmd_desc_element_addr =
2258 (cmd_desc_dma_addr + (cmd_desc_size * i));
2259 utrdlp[i].command_desc_base_addr_lo =
2260 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2261 utrdlp[i].command_desc_base_addr_hi =
2262 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2263
2264 /* Response upiu and prdt offset should be in double words */
2265 utrdlp[i].response_upiu_offset =
2266 cpu_to_le16((response_offset >> 2));
2267 utrdlp[i].prd_table_offset =
2268 cpu_to_le16((prdt_offset >> 2));
2269 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302270 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302271
2272 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302273 hba->lrb[i].ucd_req_ptr =
2274 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302275 hba->lrb[i].ucd_rsp_ptr =
2276 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2277 hba->lrb[i].ucd_prdt_ptr =
2278 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2279 }
2280}
2281
2282/**
2283 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2284 * @hba: per adapter instance
2285 *
2286 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2287 * in order to initialize the Unipro link startup procedure.
2288 * Once the Unipro links are up, the device connected to the controller
2289 * is detected.
2290 *
2291 * Returns 0 on success, non-zero value on failure
2292 */
2293static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2294{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302295 struct uic_command uic_cmd = {0};
2296 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302297
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302298 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2299
2300 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2301 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302302 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302303 "dme-link-startup: error code %d\n", ret);
2304 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302305}
2306
Yaniv Gardicad2e032015-03-31 17:37:14 +03002307static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2308{
2309 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2310 unsigned long min_sleep_time_us;
2311
2312 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2313 return;
2314
2315 /*
2316 * last_dme_cmd_tstamp will be 0 only for 1st call to
2317 * this function
2318 */
2319 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2320 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2321 } else {
2322 unsigned long delta =
2323 (unsigned long) ktime_to_us(
2324 ktime_sub(ktime_get(),
2325 hba->last_dme_cmd_tstamp));
2326
2327 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2328 min_sleep_time_us =
2329 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2330 else
2331 return; /* no more delay required */
2332 }
2333
2334 /* allow sleep for extra 50us if needed */
2335 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2336}
2337
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302338/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302339 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2340 * @hba: per adapter instance
2341 * @attr_sel: uic command argument1
2342 * @attr_set: attribute set type as uic command argument2
2343 * @mib_val: setting value as uic command argument3
2344 * @peer: indicate whether peer or local
2345 *
2346 * Returns 0 on success, non-zero value on failure
2347 */
2348int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2349 u8 attr_set, u32 mib_val, u8 peer)
2350{
2351 struct uic_command uic_cmd = {0};
2352 static const char *const action[] = {
2353 "dme-set",
2354 "dme-peer-set"
2355 };
2356 const char *set = action[!!peer];
2357 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002358 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302359
2360 uic_cmd.command = peer ?
2361 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2362 uic_cmd.argument1 = attr_sel;
2363 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2364 uic_cmd.argument3 = mib_val;
2365
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002366 do {
2367 /* for peer attributes we retry upon failure */
2368 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2369 if (ret)
2370 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2371 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2372 } while (ret && peer && --retries);
2373
2374 if (!retries)
2375 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2376 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2377 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302378
2379 return ret;
2380}
2381EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2382
2383/**
2384 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2385 * @hba: per adapter instance
2386 * @attr_sel: uic command argument1
2387 * @mib_val: the value of the attribute as returned by the UIC command
2388 * @peer: indicate whether peer or local
2389 *
2390 * Returns 0 on success, non-zero value on failure
2391 */
2392int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2393 u32 *mib_val, u8 peer)
2394{
2395 struct uic_command uic_cmd = {0};
2396 static const char *const action[] = {
2397 "dme-get",
2398 "dme-peer-get"
2399 };
2400 const char *get = action[!!peer];
2401 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002402 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002403 struct ufs_pa_layer_attr orig_pwr_info;
2404 struct ufs_pa_layer_attr temp_pwr_info;
2405 bool pwr_mode_change = false;
2406
2407 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2408 orig_pwr_info = hba->pwr_info;
2409 temp_pwr_info = orig_pwr_info;
2410
2411 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2412 orig_pwr_info.pwr_rx == FAST_MODE) {
2413 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2414 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2415 pwr_mode_change = true;
2416 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2417 orig_pwr_info.pwr_rx == SLOW_MODE) {
2418 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2419 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2420 pwr_mode_change = true;
2421 }
2422 if (pwr_mode_change) {
2423 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2424 if (ret)
2425 goto out;
2426 }
2427 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302428
2429 uic_cmd.command = peer ?
2430 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2431 uic_cmd.argument1 = attr_sel;
2432
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002433 do {
2434 /* for peer attributes we retry upon failure */
2435 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2436 if (ret)
2437 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2438 get, UIC_GET_ATTR_ID(attr_sel), ret);
2439 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302440
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002441 if (!retries)
2442 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2443 get, UIC_GET_ATTR_ID(attr_sel), retries);
2444
2445 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302446 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002447
2448 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2449 && pwr_mode_change)
2450 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302451out:
2452 return ret;
2453}
2454EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2455
2456/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002457 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2458 * state) and waits for it to take effect.
2459 *
2460 * @hba: per adapter instance
2461 * @cmd: UIC command to execute
2462 *
2463 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2464 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2465 * and device UniPro link and hence it's final completion would be indicated by
2466 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2467 * addition to normal UIC command completion Status (UCCS). This function only
2468 * returns after the relevant status bits indicate the completion.
2469 *
2470 * Returns 0 on success, non-zero value on failure
2471 */
2472static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2473{
2474 struct completion uic_async_done;
2475 unsigned long flags;
2476 u8 status;
2477 int ret;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002478 bool reenable_intr = false;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002479
2480 mutex_lock(&hba->uic_cmd_mutex);
2481 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002482 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002483
2484 spin_lock_irqsave(hba->host->host_lock, flags);
2485 hba->uic_async_done = &uic_async_done;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002486 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) {
2487 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL);
2488 /*
2489 * Make sure UIC command completion interrupt is disabled before
2490 * issuing UIC command.
2491 */
2492 wmb();
2493 reenable_intr = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002494 }
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002495 ret = __ufshcd_send_uic_cmd(hba, cmd, false);
2496 spin_unlock_irqrestore(hba->host->host_lock, flags);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002497 if (ret) {
2498 dev_err(hba->dev,
2499 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2500 cmd->command, cmd->argument3, ret);
2501 goto out;
2502 }
2503
2504 if (!wait_for_completion_timeout(hba->uic_async_done,
2505 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2506 dev_err(hba->dev,
2507 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2508 cmd->command, cmd->argument3);
2509 ret = -ETIMEDOUT;
2510 goto out;
2511 }
2512
2513 status = ufshcd_get_upmcrs(hba);
2514 if (status != PWR_LOCAL) {
2515 dev_err(hba->dev,
2516 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
2517 cmd->command, status);
2518 ret = (status != PWR_OK) ? status : -1;
2519 }
2520out:
2521 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002522 hba->active_uic_cmd = NULL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002523 hba->uic_async_done = NULL;
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02002524 if (reenable_intr)
2525 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002526 spin_unlock_irqrestore(hba->host->host_lock, flags);
2527 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002528
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002529 return ret;
2530}
2531
2532/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302533 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2534 * using DME_SET primitives.
2535 * @hba: per adapter instance
2536 * @mode: powr mode value
2537 *
2538 * Returns 0 on success, non-zero value on failure
2539 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302540static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302541{
2542 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002543 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302544
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002545 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2546 ret = ufshcd_dme_set(hba,
2547 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2548 if (ret) {
2549 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2550 __func__, ret);
2551 goto out;
2552 }
2553 }
2554
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302555 uic_cmd.command = UIC_CMD_DME_SET;
2556 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2557 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002558 ufshcd_hold(hba, false);
2559 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2560 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302561
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002562out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002563 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002564}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302565
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002566static int ufshcd_link_recovery(struct ufs_hba *hba)
2567{
2568 int ret;
2569 unsigned long flags;
2570
2571 spin_lock_irqsave(hba->host->host_lock, flags);
2572 hba->ufshcd_state = UFSHCD_STATE_RESET;
2573 ufshcd_set_eh_in_progress(hba);
2574 spin_unlock_irqrestore(hba->host->host_lock, flags);
2575
2576 ret = ufshcd_host_reset_and_restore(hba);
2577
2578 spin_lock_irqsave(hba->host->host_lock, flags);
2579 if (ret)
2580 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2581 ufshcd_clear_eh_in_progress(hba);
2582 spin_unlock_irqrestore(hba->host->host_lock, flags);
2583
2584 if (ret)
2585 dev_err(hba->dev, "%s: link recovery failed, err %d",
2586 __func__, ret);
2587
2588 return ret;
2589}
2590
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002591static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002592{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002593 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002594 struct uic_command uic_cmd = {0};
2595
2596 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002597 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002598
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002599 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002600 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2601 __func__, ret);
2602
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002603 /*
2604 * If link recovery fails then return error so that caller
2605 * don't retry the hibern8 enter again.
2606 */
2607 if (ufshcd_link_recovery(hba))
2608 ret = -ENOLINK;
2609 }
2610
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002611 return ret;
2612}
2613
2614static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2615{
2616 int ret = 0, retries;
2617
2618 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2619 ret = __ufshcd_uic_hibern8_enter(hba);
2620 if (!ret || ret == -ENOLINK)
2621 goto out;
2622 }
2623out:
2624 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002625}
2626
2627static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2628{
2629 struct uic_command uic_cmd = {0};
2630 int ret;
2631
2632 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2633 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302634 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002635 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2636 __func__, ret);
2637 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302638 }
2639
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302640 return ret;
2641}
2642
Yaniv Gardi50646362014-10-23 13:25:13 +03002643 /**
2644 * ufshcd_init_pwr_info - setting the POR (power on reset)
2645 * values in hba power info
2646 * @hba: per-adapter instance
2647 */
2648static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2649{
2650 hba->pwr_info.gear_rx = UFS_PWM_G1;
2651 hba->pwr_info.gear_tx = UFS_PWM_G1;
2652 hba->pwr_info.lane_rx = 1;
2653 hba->pwr_info.lane_tx = 1;
2654 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2655 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2656 hba->pwr_info.hs_rate = 0;
2657}
2658
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302659/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002660 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2661 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302662 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002663static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302664{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002665 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2666
2667 if (hba->max_pwr_info.is_valid)
2668 return 0;
2669
2670 pwr_info->pwr_tx = FASTAUTO_MODE;
2671 pwr_info->pwr_rx = FASTAUTO_MODE;
2672 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302673
2674 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002675 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2676 &pwr_info->lane_rx);
2677 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2678 &pwr_info->lane_tx);
2679
2680 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2681 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2682 __func__,
2683 pwr_info->lane_rx,
2684 pwr_info->lane_tx);
2685 return -EINVAL;
2686 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302687
2688 /*
2689 * First, get the maximum gears of HS speed.
2690 * If a zero value, it means there is no HSGEAR capability.
2691 * Then, get the maximum gears of PWM speed.
2692 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002693 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2694 if (!pwr_info->gear_rx) {
2695 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2696 &pwr_info->gear_rx);
2697 if (!pwr_info->gear_rx) {
2698 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2699 __func__, pwr_info->gear_rx);
2700 return -EINVAL;
2701 }
2702 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302703 }
2704
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002705 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2706 &pwr_info->gear_tx);
2707 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302708 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002709 &pwr_info->gear_tx);
2710 if (!pwr_info->gear_tx) {
2711 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2712 __func__, pwr_info->gear_tx);
2713 return -EINVAL;
2714 }
2715 pwr_info->pwr_tx = SLOWAUTO_MODE;
2716 }
2717
2718 hba->max_pwr_info.is_valid = true;
2719 return 0;
2720}
2721
2722static int ufshcd_change_power_mode(struct ufs_hba *hba,
2723 struct ufs_pa_layer_attr *pwr_mode)
2724{
2725 int ret;
2726
2727 /* if already configured to the requested pwr_mode */
2728 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2729 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2730 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2731 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2732 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2733 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2734 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2735 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2736 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302737 }
2738
2739 /*
2740 * Configure attributes for power mode change with below.
2741 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2742 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2743 * - PA_HSSERIES
2744 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002745 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2746 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2747 pwr_mode->lane_rx);
2748 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2749 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302750 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002751 else
2752 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302753
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002754 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2755 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2756 pwr_mode->lane_tx);
2757 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2758 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302759 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002760 else
2761 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302762
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002763 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2764 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2765 pwr_mode->pwr_rx == FAST_MODE ||
2766 pwr_mode->pwr_tx == FAST_MODE)
2767 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2768 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302769
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002770 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2771 | pwr_mode->pwr_tx);
2772
2773 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302774 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002775 "%s: power mode change failed %d\n", __func__, ret);
2776 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002777 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2778 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002779
2780 memcpy(&hba->pwr_info, pwr_mode,
2781 sizeof(struct ufs_pa_layer_attr));
2782 }
2783
2784 return ret;
2785}
2786
2787/**
2788 * ufshcd_config_pwr_mode - configure a new power mode
2789 * @hba: per-adapter instance
2790 * @desired_pwr_mode: desired power configuration
2791 */
2792static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2793 struct ufs_pa_layer_attr *desired_pwr_mode)
2794{
2795 struct ufs_pa_layer_attr final_params = { 0 };
2796 int ret;
2797
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002798 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2799 desired_pwr_mode, &final_params);
2800
2801 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002802 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2803
2804 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302805
2806 return ret;
2807}
2808
2809/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302810 * ufshcd_complete_dev_init() - checks device readiness
2811 * hba: per-adapter instance
2812 *
2813 * Set fDeviceInit flag and poll until device toggles it.
2814 */
2815static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2816{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002817 int i;
2818 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302819 bool flag_res = 1;
2820
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002821 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2822 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302823 if (err) {
2824 dev_err(hba->dev,
2825 "%s setting fDeviceInit flag failed with error %d\n",
2826 __func__, err);
2827 goto out;
2828 }
2829
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002830 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2831 for (i = 0; i < 1000 && !err && flag_res; i++)
2832 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2833 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2834
Dolev Raviv68078d52013-07-30 00:35:58 +05302835 if (err)
2836 dev_err(hba->dev,
2837 "%s reading fDeviceInit flag failed with error %d\n",
2838 __func__, err);
2839 else if (flag_res)
2840 dev_err(hba->dev,
2841 "%s fDeviceInit was not cleared by the device\n",
2842 __func__);
2843
2844out:
2845 return err;
2846}
2847
2848/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302849 * ufshcd_make_hba_operational - Make UFS controller operational
2850 * @hba: per adapter instance
2851 *
2852 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002853 * 1. Enable required interrupts
2854 * 2. Configure interrupt aggregation
Yaniv Gardi897efe62016-02-01 15:02:48 +02002855 * 3. Program UTRL and UTMRL base address
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002856 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302857 *
2858 * Returns 0 on success, non-zero value on failure
2859 */
2860static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2861{
2862 int err = 0;
2863 u32 reg;
2864
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302865 /* Enable required interrupts */
2866 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2867
2868 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002869 if (ufshcd_is_intr_aggr_allowed(hba))
2870 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2871 else
2872 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302873
2874 /* Configure UTRL and UTMRL base address registers */
2875 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2876 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2877 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2878 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2879 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2880 REG_UTP_TASK_REQ_LIST_BASE_L);
2881 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2882 REG_UTP_TASK_REQ_LIST_BASE_H);
2883
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302884 /*
Yaniv Gardi897efe62016-02-01 15:02:48 +02002885 * Make sure base address and interrupt setup are updated before
2886 * enabling the run/stop registers below.
2887 */
2888 wmb();
2889
2890 /*
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302891 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302892 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002893 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302894 if (!(ufshcd_get_lists_status(reg))) {
2895 ufshcd_enable_run_stop_reg(hba);
2896 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302897 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302898 "Host controller not ready to process requests");
2899 err = -EIO;
2900 goto out;
2901 }
2902
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302903out:
2904 return err;
2905}
2906
2907/**
Yaniv Gardi596585a2016-03-10 17:37:08 +02002908 * ufshcd_hba_stop - Send controller to reset state
2909 * @hba: per adapter instance
2910 * @can_sleep: perform sleep or just spin
2911 */
2912static inline void ufshcd_hba_stop(struct ufs_hba *hba, bool can_sleep)
2913{
2914 int err;
2915
2916 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE);
2917 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE,
2918 CONTROLLER_ENABLE, CONTROLLER_DISABLE,
2919 10, 1, can_sleep);
2920 if (err)
2921 dev_err(hba->dev, "%s: Controller disable failed\n", __func__);
2922}
2923
2924/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302925 * ufshcd_hba_enable - initialize the controller
2926 * @hba: per adapter instance
2927 *
2928 * The controller resets itself and controller firmware initialization
2929 * sequence kicks off. When controller is ready it will set
2930 * the Host Controller Enable bit to 1.
2931 *
2932 * Returns 0 on success, non-zero value on failure
2933 */
2934static int ufshcd_hba_enable(struct ufs_hba *hba)
2935{
2936 int retry;
2937
2938 /*
2939 * msleep of 1 and 5 used in this function might result in msleep(20),
2940 * but it was necessary to send the UFS FPGA to reset mode during
2941 * development and testing of this driver. msleep can be changed to
2942 * mdelay and retry count can be reduced based on the controller.
2943 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02002944 if (!ufshcd_is_hba_active(hba))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302945 /* change controller state to "reset state" */
Yaniv Gardi596585a2016-03-10 17:37:08 +02002946 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302947
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002948 /* UniPro link is disabled at this point */
2949 ufshcd_set_link_off(hba);
2950
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002951 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002952
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302953 /* start controller initialization sequence */
2954 ufshcd_hba_start(hba);
2955
2956 /*
2957 * To initialize a UFS host controller HCE bit must be set to 1.
2958 * During initialization the HCE bit value changes from 1->0->1.
2959 * When the host controller completes initialization sequence
2960 * it sets the value of HCE bit to 1. The same HCE bit is read back
2961 * to check if the controller has completed initialization sequence.
2962 * So without this delay the value HCE = 1, set in the previous
2963 * instruction might be read back.
2964 * This delay can be changed based on the controller.
2965 */
2966 msleep(1);
2967
2968 /* wait for the host controller to complete initialization */
2969 retry = 10;
2970 while (ufshcd_is_hba_active(hba)) {
2971 if (retry) {
2972 retry--;
2973 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302974 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302975 "Controller enable failed\n");
2976 return -EIO;
2977 }
2978 msleep(5);
2979 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002980
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002981 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002982 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002983
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002984 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002985
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302986 return 0;
2987}
2988
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002989static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
2990{
2991 int tx_lanes, i, err = 0;
2992
2993 if (!peer)
2994 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2995 &tx_lanes);
2996 else
2997 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2998 &tx_lanes);
2999 for (i = 0; i < tx_lanes; i++) {
3000 if (!peer)
3001 err = ufshcd_dme_set(hba,
3002 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3003 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3004 0);
3005 else
3006 err = ufshcd_dme_peer_set(hba,
3007 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
3008 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
3009 0);
3010 if (err) {
3011 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
3012 __func__, peer, i, err);
3013 break;
3014 }
3015 }
3016
3017 return err;
3018}
3019
3020static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
3021{
3022 return ufshcd_disable_tx_lcc(hba, true);
3023}
3024
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303025/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303026 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303027 * @hba: per adapter instance
3028 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303029 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303030 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303031static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303032{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303033 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003034 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303035
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003036 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003037 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303038
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003039 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003040
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003041 /* check if device is detected by inter-connect layer */
3042 if (!ret && !ufshcd_is_device_present(hba)) {
3043 dev_err(hba->dev, "%s: Device not present\n", __func__);
3044 ret = -ENXIO;
3045 goto out;
3046 }
3047
3048 /*
3049 * DME link lost indication is only received when link is up,
3050 * but we can't be sure if the link is up until link startup
3051 * succeeds. So reset the local Uni-Pro and try again.
3052 */
3053 if (ret && ufshcd_hba_enable(hba))
3054 goto out;
3055 } while (ret && retries--);
3056
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303057 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003058 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303059 goto out;
3060
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03003061 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
3062 ret = ufshcd_disable_device_tx_lcc(hba);
3063 if (ret)
3064 goto out;
3065 }
3066
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003067 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02003068 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
3069 if (ret)
3070 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003071
3072 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303073out:
3074 if (ret)
3075 dev_err(hba->dev, "link startup failed %d\n", ret);
3076 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303077}
3078
3079/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303080 * ufshcd_verify_dev_init() - Verify device initialization
3081 * @hba: per-adapter instance
3082 *
3083 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
3084 * device Transport Protocol (UTP) layer is ready after a reset.
3085 * If the UTP layer at the device side is not initialized, it may
3086 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
3087 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
3088 */
3089static int ufshcd_verify_dev_init(struct ufs_hba *hba)
3090{
3091 int err = 0;
3092 int retries;
3093
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003094 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303095 mutex_lock(&hba->dev_cmd.lock);
3096 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
3097 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
3098 NOP_OUT_TIMEOUT);
3099
3100 if (!err || err == -ETIMEDOUT)
3101 break;
3102
3103 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
3104 }
3105 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003106 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303107
3108 if (err)
3109 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
3110 return err;
3111}
3112
3113/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003114 * ufshcd_set_queue_depth - set lun queue depth
3115 * @sdev: pointer to SCSI device
3116 *
3117 * Read bLUQueueDepth value and activate scsi tagged command
3118 * queueing. For WLUN, queue depth is set to 1. For best-effort
3119 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
3120 * value that host can queue.
3121 */
3122static void ufshcd_set_queue_depth(struct scsi_device *sdev)
3123{
3124 int ret = 0;
3125 u8 lun_qdepth;
3126 struct ufs_hba *hba;
3127
3128 hba = shost_priv(sdev->host);
3129
3130 lun_qdepth = hba->nutrs;
3131 ret = ufshcd_read_unit_desc_param(hba,
3132 ufshcd_scsi_to_upiu_lun(sdev->lun),
3133 UNIT_DESC_PARAM_LU_Q_DEPTH,
3134 &lun_qdepth,
3135 sizeof(lun_qdepth));
3136
3137 /* Some WLUN doesn't support unit descriptor */
3138 if (ret == -EOPNOTSUPP)
3139 lun_qdepth = 1;
3140 else if (!lun_qdepth)
3141 /* eventually, we can figure out the real queue depth */
3142 lun_qdepth = hba->nutrs;
3143 else
3144 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
3145
3146 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
3147 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003148 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003149}
3150
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003151/*
3152 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3153 * @hba: per-adapter instance
3154 * @lun: UFS device lun id
3155 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3156 *
3157 * Returns 0 in case of success and b_lu_write_protect status would be returned
3158 * @b_lu_write_protect parameter.
3159 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3160 * Returns -EINVAL in case of invalid parameters passed to this function.
3161 */
3162static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3163 u8 lun,
3164 u8 *b_lu_write_protect)
3165{
3166 int ret;
3167
3168 if (!b_lu_write_protect)
3169 ret = -EINVAL;
3170 /*
3171 * According to UFS device spec, RPMB LU can't be write
3172 * protected so skip reading bLUWriteProtect parameter for
3173 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3174 */
3175 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3176 ret = -ENOTSUPP;
3177 else
3178 ret = ufshcd_read_unit_desc_param(hba,
3179 lun,
3180 UNIT_DESC_PARAM_LU_WR_PROTECT,
3181 b_lu_write_protect,
3182 sizeof(*b_lu_write_protect));
3183 return ret;
3184}
3185
3186/**
3187 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3188 * status
3189 * @hba: per-adapter instance
3190 * @sdev: pointer to SCSI device
3191 *
3192 */
3193static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3194 struct scsi_device *sdev)
3195{
3196 if (hba->dev_info.f_power_on_wp_en &&
3197 !hba->dev_info.is_lu_power_on_wp) {
3198 u8 b_lu_write_protect;
3199
3200 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3201 &b_lu_write_protect) &&
3202 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3203 hba->dev_info.is_lu_power_on_wp = true;
3204 }
3205}
3206
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003207/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303208 * ufshcd_slave_alloc - handle initial SCSI device configurations
3209 * @sdev: pointer to SCSI device
3210 *
3211 * Returns success
3212 */
3213static int ufshcd_slave_alloc(struct scsi_device *sdev)
3214{
3215 struct ufs_hba *hba;
3216
3217 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303218
3219 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3220 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303221
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303222 /* allow SCSI layer to restart the device in case of errors */
3223 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003224
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003225 /* REPORT SUPPORTED OPERATION CODES is not supported */
3226 sdev->no_report_opcodes = 1;
3227
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003228
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003229 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003230
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003231 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3232
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003233 return 0;
3234}
3235
3236/**
3237 * ufshcd_change_queue_depth - change queue depth
3238 * @sdev: pointer to SCSI device
3239 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003240 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003241 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003242 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003243static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003244{
3245 struct ufs_hba *hba = shost_priv(sdev->host);
3246
3247 if (depth > hba->nutrs)
3248 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003249 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303250}
3251
3252/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003253 * ufshcd_slave_configure - adjust SCSI device configurations
3254 * @sdev: pointer to SCSI device
3255 */
3256static int ufshcd_slave_configure(struct scsi_device *sdev)
3257{
3258 struct request_queue *q = sdev->request_queue;
3259
3260 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3261 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3262
3263 return 0;
3264}
3265
3266/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303267 * ufshcd_slave_destroy - remove SCSI device configurations
3268 * @sdev: pointer to SCSI device
3269 */
3270static void ufshcd_slave_destroy(struct scsi_device *sdev)
3271{
3272 struct ufs_hba *hba;
3273
3274 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003275 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003276 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3277 unsigned long flags;
3278
3279 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003280 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003281 spin_unlock_irqrestore(hba->host->host_lock, flags);
3282 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303283}
3284
3285/**
3286 * ufshcd_task_req_compl - handle task management request completion
3287 * @hba: per adapter instance
3288 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303289 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303290 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303291 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303292 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303293static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303294{
3295 struct utp_task_req_desc *task_req_descp;
3296 struct utp_upiu_task_rsp *task_rsp_upiup;
3297 unsigned long flags;
3298 int ocs_value;
3299 int task_result;
3300
3301 spin_lock_irqsave(hba->host->host_lock, flags);
3302
3303 /* Clear completed tasks from outstanding_tasks */
3304 __clear_bit(index, &hba->outstanding_tasks);
3305
3306 task_req_descp = hba->utmrdl_base_addr;
3307 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3308
3309 if (ocs_value == OCS_SUCCESS) {
3310 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3311 task_req_descp[index].task_rsp_upiu;
3312 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
3313 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303314 if (resp)
3315 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303316 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303317 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3318 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303319 }
3320 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303321
3322 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303323}
3324
3325/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303326 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3327 * @lrb: pointer to local reference block of completed command
3328 * @scsi_status: SCSI command status
3329 *
3330 * Returns value base on SCSI command status
3331 */
3332static inline int
3333ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3334{
3335 int result = 0;
3336
3337 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303338 case SAM_STAT_CHECK_CONDITION:
3339 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303340 case SAM_STAT_GOOD:
3341 result |= DID_OK << 16 |
3342 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303343 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303344 break;
3345 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303346 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303347 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303348 ufshcd_copy_sense_data(lrbp);
3349 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303350 break;
3351 default:
3352 result |= DID_ERROR << 16;
3353 break;
3354 } /* end of switch */
3355
3356 return result;
3357}
3358
3359/**
3360 * ufshcd_transfer_rsp_status - Get overall status of the response
3361 * @hba: per adapter instance
3362 * @lrb: pointer to local reference block of completed command
3363 *
3364 * Returns result of the command to notify SCSI midlayer
3365 */
3366static inline int
3367ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3368{
3369 int result = 0;
3370 int scsi_status;
3371 int ocs;
3372
3373 /* overall command status of utrd */
3374 ocs = ufshcd_get_tr_ocs(lrbp);
3375
3376 switch (ocs) {
3377 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303378 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303379
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303380 switch (result) {
3381 case UPIU_TRANSACTION_RESPONSE:
3382 /*
3383 * get the response UPIU result to extract
3384 * the SCSI command status
3385 */
3386 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3387
3388 /*
3389 * get the result based on SCSI status response
3390 * to notify the SCSI midlayer of the command status
3391 */
3392 scsi_status = result & MASK_SCSI_STATUS;
3393 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303394
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003395 /*
3396 * Currently we are only supporting BKOPs exception
3397 * events hence we can ignore BKOPs exception event
3398 * during power management callbacks. BKOPs exception
3399 * event is not expected to be raised in runtime suspend
3400 * callback as it allows the urgent bkops.
3401 * During system suspend, we are anyway forcefully
3402 * disabling the bkops and if urgent bkops is needed
3403 * it will be enabled on system resume. Long term
3404 * solution could be to abort the system suspend if
3405 * UFS device needs urgent BKOPs.
3406 */
3407 if (!hba->pm_op_in_progress &&
3408 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303409 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303410 break;
3411 case UPIU_TRANSACTION_REJECT_UPIU:
3412 /* TODO: handle Reject UPIU Response */
3413 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303414 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303415 "Reject UPIU not fully implemented\n");
3416 break;
3417 default:
3418 result = DID_ERROR << 16;
3419 dev_err(hba->dev,
3420 "Unexpected request response code = %x\n",
3421 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303422 break;
3423 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303424 break;
3425 case OCS_ABORTED:
3426 result |= DID_ABORT << 16;
3427 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303428 case OCS_INVALID_COMMAND_STATUS:
3429 result |= DID_REQUEUE << 16;
3430 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303431 case OCS_INVALID_CMD_TABLE_ATTR:
3432 case OCS_INVALID_PRDT_ATTR:
3433 case OCS_MISMATCH_DATA_BUF_SIZE:
3434 case OCS_MISMATCH_RESP_UPIU_SIZE:
3435 case OCS_PEER_COMM_FAILURE:
3436 case OCS_FATAL_ERROR:
3437 default:
3438 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303439 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303440 "OCS error from controller = %x\n", ocs);
3441 break;
3442 } /* end of switch */
3443
3444 return result;
3445}
3446
3447/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303448 * ufshcd_uic_cmd_compl - handle completion of uic command
3449 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303450 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303451 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303452static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303453{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303454 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303455 hba->active_uic_cmd->argument2 |=
3456 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303457 hba->active_uic_cmd->argument3 =
3458 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303459 complete(&hba->active_uic_cmd->done);
3460 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303461
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003462 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3463 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303464}
3465
3466/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303467 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3468 * @hba: per adapter instance
3469 */
3470static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3471{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303472 struct ufshcd_lrb *lrbp;
3473 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303474 unsigned long completed_reqs;
3475 u32 tr_doorbell;
3476 int result;
3477 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003478
3479 /* Resetting interrupt aggregation counters first and reading the
3480 * DOOR_BELL afterward allows us to handle all the completed requests.
3481 * In order to prevent other interrupts starvation the DB is read once
3482 * after reset. The down side of this solution is the possibility of
3483 * false interrupt if device completes another request after resetting
3484 * aggregation and before reading the DB.
3485 */
Yaniv Gardib8521902015-05-17 18:54:57 +03003486 if (ufshcd_is_intr_aggr_allowed(hba))
3487 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303488
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303489 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303490 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3491
Dolev Ravive9d501b2014-07-01 12:22:37 +03003492 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3493 lrbp = &hba->lrb[index];
3494 cmd = lrbp->cmd;
3495 if (cmd) {
3496 result = ufshcd_transfer_rsp_status(hba, lrbp);
3497 scsi_dma_unmap(cmd);
3498 cmd->result = result;
3499 /* Mark completed command as NULL in LRB */
3500 lrbp->cmd = NULL;
3501 clear_bit_unlock(index, &hba->lrb_in_use);
3502 /* Do not touch lrbp after scsi done */
3503 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003504 __ufshcd_release(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003505 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
3506 if (hba->dev_cmd.complete)
3507 complete(hba->dev_cmd.complete);
3508 }
3509 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303510
3511 /* clear corresponding bits of completed commands */
3512 hba->outstanding_reqs ^= completed_reqs;
3513
Sahitya Tummala856b3482014-09-25 15:32:34 +03003514 ufshcd_clk_scaling_update_busy(hba);
3515
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303516 /* we might have free'd some tags above */
3517 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303518}
3519
3520/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303521 * ufshcd_disable_ee - disable exception event
3522 * @hba: per-adapter instance
3523 * @mask: exception event to disable
3524 *
3525 * Disables exception event in the device so that the EVENT_ALERT
3526 * bit is not set.
3527 *
3528 * Returns zero on success, non-zero error value on failure.
3529 */
3530static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3531{
3532 int err = 0;
3533 u32 val;
3534
3535 if (!(hba->ee_ctrl_mask & mask))
3536 goto out;
3537
3538 val = hba->ee_ctrl_mask & ~mask;
3539 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003540 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303541 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3542 if (!err)
3543 hba->ee_ctrl_mask &= ~mask;
3544out:
3545 return err;
3546}
3547
3548/**
3549 * ufshcd_enable_ee - enable exception event
3550 * @hba: per-adapter instance
3551 * @mask: exception event to enable
3552 *
3553 * Enable corresponding exception event in the device to allow
3554 * device to alert host in critical scenarios.
3555 *
3556 * Returns zero on success, non-zero error value on failure.
3557 */
3558static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3559{
3560 int err = 0;
3561 u32 val;
3562
3563 if (hba->ee_ctrl_mask & mask)
3564 goto out;
3565
3566 val = hba->ee_ctrl_mask | mask;
3567 val &= 0xFFFF; /* 2 bytes */
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003568 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303569 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3570 if (!err)
3571 hba->ee_ctrl_mask |= mask;
3572out:
3573 return err;
3574}
3575
3576/**
3577 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3578 * @hba: per-adapter instance
3579 *
3580 * Allow device to manage background operations on its own. Enabling
3581 * this might lead to inconsistent latencies during normal data transfers
3582 * as the device is allowed to manage its own way of handling background
3583 * operations.
3584 *
3585 * Returns zero on success, non-zero on failure.
3586 */
3587static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3588{
3589 int err = 0;
3590
3591 if (hba->auto_bkops_enabled)
3592 goto out;
3593
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003594 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303595 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3596 if (err) {
3597 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3598 __func__, err);
3599 goto out;
3600 }
3601
3602 hba->auto_bkops_enabled = true;
3603
3604 /* No need of URGENT_BKOPS exception from the device */
3605 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3606 if (err)
3607 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3608 __func__, err);
3609out:
3610 return err;
3611}
3612
3613/**
3614 * ufshcd_disable_auto_bkops - block device in doing background operations
3615 * @hba: per-adapter instance
3616 *
3617 * Disabling background operations improves command response latency but
3618 * has drawback of device moving into critical state where the device is
3619 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3620 * host is idle so that BKOPS are managed effectively without any negative
3621 * impacts.
3622 *
3623 * Returns zero on success, non-zero on failure.
3624 */
3625static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3626{
3627 int err = 0;
3628
3629 if (!hba->auto_bkops_enabled)
3630 goto out;
3631
3632 /*
3633 * If host assisted BKOPs is to be enabled, make sure
3634 * urgent bkops exception is allowed.
3635 */
3636 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3637 if (err) {
3638 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3639 __func__, err);
3640 goto out;
3641 }
3642
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003643 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303644 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3645 if (err) {
3646 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3647 __func__, err);
3648 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3649 goto out;
3650 }
3651
3652 hba->auto_bkops_enabled = false;
3653out:
3654 return err;
3655}
3656
3657/**
3658 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3659 * @hba: per adapter instance
3660 *
3661 * After a device reset the device may toggle the BKOPS_EN flag
3662 * to default value. The s/w tracking variables should be updated
3663 * as well. Do this by forcing enable of auto bkops.
3664 */
3665static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3666{
3667 hba->auto_bkops_enabled = false;
3668 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3669 ufshcd_enable_auto_bkops(hba);
3670}
3671
3672static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3673{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003674 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303675 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3676}
3677
3678/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003679 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3680 * @hba: per-adapter instance
3681 * @status: bkops_status value
3682 *
3683 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3684 * flag in the device to permit background operations if the device
3685 * bkops_status is greater than or equal to "status" argument passed to
3686 * this function, disable otherwise.
3687 *
3688 * Returns 0 for success, non-zero in case of failure.
3689 *
3690 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3691 * to know whether auto bkops is enabled or disabled after this function
3692 * returns control to it.
3693 */
3694static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3695 enum bkops_status status)
3696{
3697 int err;
3698 u32 curr_status = 0;
3699
3700 err = ufshcd_get_bkops_status(hba, &curr_status);
3701 if (err) {
3702 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3703 __func__, err);
3704 goto out;
3705 } else if (curr_status > BKOPS_STATUS_MAX) {
3706 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3707 __func__, curr_status);
3708 err = -EINVAL;
3709 goto out;
3710 }
3711
3712 if (curr_status >= status)
3713 err = ufshcd_enable_auto_bkops(hba);
3714 else
3715 err = ufshcd_disable_auto_bkops(hba);
3716out:
3717 return err;
3718}
3719
3720/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303721 * ufshcd_urgent_bkops - handle urgent bkops exception event
3722 * @hba: per-adapter instance
3723 *
3724 * Enable fBackgroundOpsEn flag in the device to permit background
3725 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003726 *
3727 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3728 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303729 */
3730static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3731{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003732 return ufshcd_bkops_ctrl(hba, BKOPS_STATUS_PERF_IMPACT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303733}
3734
3735static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3736{
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02003737 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303738 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3739}
3740
3741/**
3742 * ufshcd_exception_event_handler - handle exceptions raised by device
3743 * @work: pointer to work data
3744 *
3745 * Read bExceptionEventStatus attribute from the device and handle the
3746 * exception event accordingly.
3747 */
3748static void ufshcd_exception_event_handler(struct work_struct *work)
3749{
3750 struct ufs_hba *hba;
3751 int err;
3752 u32 status = 0;
3753 hba = container_of(work, struct ufs_hba, eeh_work);
3754
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303755 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303756 err = ufshcd_get_ee_status(hba, &status);
3757 if (err) {
3758 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3759 __func__, err);
3760 goto out;
3761 }
3762
3763 status &= hba->ee_ctrl_mask;
3764 if (status & MASK_EE_URGENT_BKOPS) {
3765 err = ufshcd_urgent_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003766 if (err < 0)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303767 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3768 __func__, err);
3769 }
3770out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303771 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303772 return;
3773}
3774
3775/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303776 * ufshcd_err_handler - handle UFS errors that require s/w attention
3777 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303778 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303779static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303780{
3781 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303782 unsigned long flags;
3783 u32 err_xfer = 0;
3784 u32 err_tm = 0;
3785 int err = 0;
3786 int tag;
3787
3788 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303789
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303790 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003791 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303792
3793 spin_lock_irqsave(hba->host->host_lock, flags);
3794 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
3795 spin_unlock_irqrestore(hba->host->host_lock, flags);
3796 goto out;
3797 }
3798
3799 hba->ufshcd_state = UFSHCD_STATE_RESET;
3800 ufshcd_set_eh_in_progress(hba);
3801
3802 /* Complete requests that have door-bell cleared by h/w */
3803 ufshcd_transfer_req_compl(hba);
3804 ufshcd_tmc_handler(hba);
3805 spin_unlock_irqrestore(hba->host->host_lock, flags);
3806
3807 /* Clear pending transfer requests */
3808 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
3809 if (ufshcd_clear_cmd(hba, tag))
3810 err_xfer |= 1 << tag;
3811
3812 /* Clear pending task management requests */
3813 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
3814 if (ufshcd_clear_tm_cmd(hba, tag))
3815 err_tm |= 1 << tag;
3816
3817 /* Complete the requests that are cleared by s/w */
3818 spin_lock_irqsave(hba->host->host_lock, flags);
3819 ufshcd_transfer_req_compl(hba);
3820 ufshcd_tmc_handler(hba);
3821 spin_unlock_irqrestore(hba->host->host_lock, flags);
3822
3823 /* Fatal errors need reset */
3824 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
3825 ((hba->saved_err & UIC_ERROR) &&
3826 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
3827 err = ufshcd_reset_and_restore(hba);
3828 if (err) {
3829 dev_err(hba->dev, "%s: reset and restore failed\n",
3830 __func__);
3831 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3832 }
3833 /*
3834 * Inform scsi mid-layer that we did reset and allow to handle
3835 * Unit Attention properly.
3836 */
3837 scsi_report_bus_reset(hba->host, 0);
3838 hba->saved_err = 0;
3839 hba->saved_uic_err = 0;
3840 }
3841 ufshcd_clear_eh_in_progress(hba);
3842
3843out:
3844 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003845 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303846 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303847}
3848
3849/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303850 * ufshcd_update_uic_error - check and set fatal UIC error flags.
3851 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303852 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303853static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303854{
3855 u32 reg;
3856
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303857 /* PA_INIT_ERROR is fatal and needs UIC reset */
3858 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
3859 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
3860 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
3861
3862 /* UIC NL/TL/DME errors needs software retry */
3863 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
3864 if (reg)
3865 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
3866
3867 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
3868 if (reg)
3869 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
3870
3871 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
3872 if (reg)
3873 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
3874
3875 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
3876 __func__, hba->uic_error);
3877}
3878
3879/**
3880 * ufshcd_check_errors - Check for errors that need s/w attention
3881 * @hba: per-adapter instance
3882 */
3883static void ufshcd_check_errors(struct ufs_hba *hba)
3884{
3885 bool queue_eh_work = false;
3886
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303887 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303888 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303889
3890 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303891 hba->uic_error = 0;
3892 ufshcd_update_uic_error(hba);
3893 if (hba->uic_error)
3894 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303895 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303896
3897 if (queue_eh_work) {
3898 /* handle fatal errors only when link is functional */
3899 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
3900 /* block commands from scsi mid-layer */
3901 scsi_block_requests(hba->host);
3902
3903 /* transfer error masks to sticky bits */
3904 hba->saved_err |= hba->errors;
3905 hba->saved_uic_err |= hba->uic_error;
3906
3907 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3908 schedule_work(&hba->eh_work);
3909 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303910 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303911 /*
3912 * if (!queue_eh_work) -
3913 * Other errors are either non-fatal where host recovers
3914 * itself without s/w intervention or errors that will be
3915 * handled by the SCSI core layer.
3916 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303917}
3918
3919/**
3920 * ufshcd_tmc_handler - handle task management function completion
3921 * @hba: per adapter instance
3922 */
3923static void ufshcd_tmc_handler(struct ufs_hba *hba)
3924{
3925 u32 tm_doorbell;
3926
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303927 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303928 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303929 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303930}
3931
3932/**
3933 * ufshcd_sl_intr - Interrupt service routine
3934 * @hba: per adapter instance
3935 * @intr_status: contains interrupts generated by the controller
3936 */
3937static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
3938{
3939 hba->errors = UFSHCD_ERROR_MASK & intr_status;
3940 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303941 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303942
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303943 if (intr_status & UFSHCD_UIC_MASK)
3944 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303945
3946 if (intr_status & UTP_TASK_REQ_COMPL)
3947 ufshcd_tmc_handler(hba);
3948
3949 if (intr_status & UTP_TRANSFER_REQ_COMPL)
3950 ufshcd_transfer_req_compl(hba);
3951}
3952
3953/**
3954 * ufshcd_intr - Main interrupt service routine
3955 * @irq: irq number
3956 * @__hba: pointer to adapter instance
3957 *
3958 * Returns IRQ_HANDLED - If interrupt is valid
3959 * IRQ_NONE - If invalid interrupt
3960 */
3961static irqreturn_t ufshcd_intr(int irq, void *__hba)
3962{
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003963 u32 intr_status, enabled_intr_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303964 irqreturn_t retval = IRQ_NONE;
3965 struct ufs_hba *hba = __hba;
3966
3967 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303968 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003969 enabled_intr_status =
3970 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303971
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003972 if (intr_status)
Seungwon Jeon261ea452013-06-26 22:39:28 +05303973 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Yaniv Gardid75f7fe2016-02-01 15:02:47 +02003974
3975 if (enabled_intr_status) {
3976 ufshcd_sl_intr(hba, enabled_intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303977 retval = IRQ_HANDLED;
3978 }
3979 spin_unlock(hba->host->host_lock);
3980 return retval;
3981}
3982
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303983static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
3984{
3985 int err = 0;
3986 u32 mask = 1 << tag;
3987 unsigned long flags;
3988
3989 if (!test_bit(tag, &hba->outstanding_tasks))
3990 goto out;
3991
3992 spin_lock_irqsave(hba->host->host_lock, flags);
3993 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
3994 spin_unlock_irqrestore(hba->host->host_lock, flags);
3995
3996 /* poll for max. 1 sec to clear door bell register by h/w */
3997 err = ufshcd_wait_for_register(hba,
3998 REG_UTP_TASK_REQ_DOOR_BELL,
Yaniv Gardi596585a2016-03-10 17:37:08 +02003999 mask, 0, 1000, 1000, true);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304000out:
4001 return err;
4002}
4003
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304004/**
4005 * ufshcd_issue_tm_cmd - issues task management commands to controller
4006 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304007 * @lun_id: LUN ID to which TM command is sent
4008 * @task_id: task ID to which the TM command is applicable
4009 * @tm_function: task management function opcode
4010 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304011 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304012 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304013 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304014static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
4015 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304016{
4017 struct utp_task_req_desc *task_req_descp;
4018 struct utp_upiu_task_req *task_req_upiup;
4019 struct Scsi_Host *host;
4020 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304021 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304022 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304023 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304024
4025 host = hba->host;
4026
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304027 /*
4028 * Get free slot, sleep if slots are unavailable.
4029 * Even though we use wait_event() which sleeps indefinitely,
4030 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
4031 */
4032 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004033 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304034
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304035 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304036 task_req_descp = hba->utmrdl_base_addr;
4037 task_req_descp += free_slot;
4038
4039 /* Configure task request descriptor */
4040 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
4041 task_req_descp->header.dword_2 =
4042 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
4043
4044 /* Configure task request UPIU */
4045 task_req_upiup =
4046 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304047 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304048 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304049 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304050 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304051 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304052 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03004053 /*
4054 * The host shall provide the same value for LUN field in the basic
4055 * header and for Input Parameter.
4056 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304057 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
4058 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304059
4060 /* send command to the controller */
4061 __set_bit(free_slot, &hba->outstanding_tasks);
Yaniv Gardi897efe62016-02-01 15:02:48 +02004062
4063 /* Make sure descriptors are ready before ringing the task doorbell */
4064 wmb();
4065
Seungwon Jeonb873a2752013-06-26 22:39:26 +05304066 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304067
4068 spin_unlock_irqrestore(host->host_lock, flags);
4069
4070 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304071 err = wait_event_timeout(hba->tm_wq,
4072 test_bit(free_slot, &hba->tm_condition),
4073 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304074 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304075 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
4076 __func__, tm_function);
4077 if (ufshcd_clear_tm_cmd(hba, free_slot))
4078 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
4079 __func__, free_slot);
4080 err = -ETIMEDOUT;
4081 } else {
4082 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304083 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304084
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304085 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304086 ufshcd_put_tm_slot(hba, free_slot);
4087 wake_up(&hba->tm_tag_wq);
4088
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004089 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304090 return err;
4091}
4092
4093/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304094 * ufshcd_eh_device_reset_handler - device reset handler registered to
4095 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304096 * @cmd: SCSI command pointer
4097 *
4098 * Returns SUCCESS/FAILED
4099 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304100static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304101{
4102 struct Scsi_Host *host;
4103 struct ufs_hba *hba;
4104 unsigned int tag;
4105 u32 pos;
4106 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304107 u8 resp = 0xF;
4108 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304109 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304110
4111 host = cmd->device->host;
4112 hba = shost_priv(host);
4113 tag = cmd->request->tag;
4114
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304115 lrbp = &hba->lrb[tag];
4116 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
4117 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304118 if (!err)
4119 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304120 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304121 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304122
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304123 /* clear the commands that were pending for corresponding LUN */
4124 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
4125 if (hba->lrb[pos].lun == lrbp->lun) {
4126 err = ufshcd_clear_cmd(hba, pos);
4127 if (err)
4128 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304129 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304130 }
4131 spin_lock_irqsave(host->host_lock, flags);
4132 ufshcd_transfer_req_compl(hba);
4133 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304134out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304135 if (!err) {
4136 err = SUCCESS;
4137 } else {
4138 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4139 err = FAILED;
4140 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304141 return err;
4142}
4143
4144/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304145 * ufshcd_abort - abort a specific command
4146 * @cmd: SCSI command pointer
4147 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304148 * Abort the pending command in device by sending UFS_ABORT_TASK task management
4149 * command, and in host controller by clearing the door-bell register. There can
4150 * be race between controller sending the command to the device while abort is
4151 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
4152 * really issued and then try to abort it.
4153 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304154 * Returns SUCCESS/FAILED
4155 */
4156static int ufshcd_abort(struct scsi_cmnd *cmd)
4157{
4158 struct Scsi_Host *host;
4159 struct ufs_hba *hba;
4160 unsigned long flags;
4161 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304162 int err = 0;
4163 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304164 u8 resp = 0xF;
4165 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004166 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304167
4168 host = cmd->device->host;
4169 hba = shost_priv(host);
4170 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004171 if (!ufshcd_valid_tag(hba, tag)) {
4172 dev_err(hba->dev,
4173 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4174 __func__, tag, cmd, cmd->request);
4175 BUG();
4176 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304177
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004178 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004179 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004180 /* If command is already aborted/completed, return SUCCESS */
4181 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4182 dev_err(hba->dev,
4183 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4184 __func__, tag, hba->outstanding_reqs, reg);
4185 goto out;
4186 }
4187
Dolev Ravive9d501b2014-07-01 12:22:37 +03004188 if (!(reg & (1 << tag))) {
4189 dev_err(hba->dev,
4190 "%s: cmd was completed, but without a notifying intr, tag = %d",
4191 __func__, tag);
4192 }
4193
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304194 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304195 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4196 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4197 UFS_QUERY_TASK, &resp);
4198 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4199 /* cmd pending in the device */
4200 break;
4201 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304202 /*
4203 * cmd not pending in the device, check if it is
4204 * in transition.
4205 */
4206 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4207 if (reg & (1 << tag)) {
4208 /* sleep for max. 200us to stabilize */
4209 usleep_range(100, 200);
4210 continue;
4211 }
4212 /* command completed already */
4213 goto out;
4214 } else {
4215 if (!err)
4216 err = resp; /* service response error */
4217 goto out;
4218 }
4219 }
4220
4221 if (!poll_cnt) {
4222 err = -EBUSY;
4223 goto out;
4224 }
4225
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304226 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4227 UFS_ABORT_TASK, &resp);
4228 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304229 if (!err)
4230 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304231 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304232 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304233
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304234 err = ufshcd_clear_cmd(hba, tag);
4235 if (err)
4236 goto out;
4237
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304238 scsi_dma_unmap(cmd);
4239
4240 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004241 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304242 hba->lrb[tag].cmd = NULL;
4243 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304244
4245 clear_bit_unlock(tag, &hba->lrb_in_use);
4246 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004247
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304248out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304249 if (!err) {
4250 err = SUCCESS;
4251 } else {
4252 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4253 err = FAILED;
4254 }
4255
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004256 /*
4257 * This ufshcd_release() corresponds to the original scsi cmd that got
4258 * aborted here (as we won't get any IRQ for it).
4259 */
4260 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304261 return err;
4262}
4263
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304264/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304265 * ufshcd_host_reset_and_restore - reset and restore host controller
4266 * @hba: per-adapter instance
4267 *
4268 * Note that host controller reset may issue DME_RESET to
4269 * local and remote (device) Uni-Pro stack and the attributes
4270 * are reset to default state.
4271 *
4272 * Returns zero on success, non-zero on failure
4273 */
4274static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4275{
4276 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304277 unsigned long flags;
4278
4279 /* Reset the host controller */
4280 spin_lock_irqsave(hba->host->host_lock, flags);
Yaniv Gardi596585a2016-03-10 17:37:08 +02004281 ufshcd_hba_stop(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304282 spin_unlock_irqrestore(hba->host->host_lock, flags);
4283
4284 err = ufshcd_hba_enable(hba);
4285 if (err)
4286 goto out;
4287
4288 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004289 err = ufshcd_probe_hba(hba);
4290
4291 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304292 err = -EIO;
4293out:
4294 if (err)
4295 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4296
4297 return err;
4298}
4299
4300/**
4301 * ufshcd_reset_and_restore - reset and re-initialize host/device
4302 * @hba: per-adapter instance
4303 *
4304 * Reset and recover device, host and re-establish link. This
4305 * is helpful to recover the communication in fatal error conditions.
4306 *
4307 * Returns zero on success, non-zero on failure
4308 */
4309static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4310{
4311 int err = 0;
4312 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004313 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304314
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004315 do {
4316 err = ufshcd_host_reset_and_restore(hba);
4317 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304318
4319 /*
4320 * After reset the door-bell might be cleared, complete
4321 * outstanding requests in s/w here.
4322 */
4323 spin_lock_irqsave(hba->host->host_lock, flags);
4324 ufshcd_transfer_req_compl(hba);
4325 ufshcd_tmc_handler(hba);
4326 spin_unlock_irqrestore(hba->host->host_lock, flags);
4327
4328 return err;
4329}
4330
4331/**
4332 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4333 * @cmd - SCSI command pointer
4334 *
4335 * Returns SUCCESS/FAILED
4336 */
4337static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4338{
4339 int err;
4340 unsigned long flags;
4341 struct ufs_hba *hba;
4342
4343 hba = shost_priv(cmd->device->host);
4344
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004345 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304346 /*
4347 * Check if there is any race with fatal error handling.
4348 * If so, wait for it to complete. Even though fatal error
4349 * handling does reset and restore in some cases, don't assume
4350 * anything out of it. We are just avoiding race here.
4351 */
4352 do {
4353 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304354 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304355 hba->ufshcd_state == UFSHCD_STATE_RESET))
4356 break;
4357 spin_unlock_irqrestore(hba->host->host_lock, flags);
4358 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304359 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304360 } while (1);
4361
4362 hba->ufshcd_state = UFSHCD_STATE_RESET;
4363 ufshcd_set_eh_in_progress(hba);
4364 spin_unlock_irqrestore(hba->host->host_lock, flags);
4365
4366 err = ufshcd_reset_and_restore(hba);
4367
4368 spin_lock_irqsave(hba->host->host_lock, flags);
4369 if (!err) {
4370 err = SUCCESS;
4371 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4372 } else {
4373 err = FAILED;
4374 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4375 }
4376 ufshcd_clear_eh_in_progress(hba);
4377 spin_unlock_irqrestore(hba->host->host_lock, flags);
4378
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004379 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304380 return err;
4381}
4382
4383/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004384 * ufshcd_get_max_icc_level - calculate the ICC level
4385 * @sup_curr_uA: max. current supported by the regulator
4386 * @start_scan: row at the desc table to start scan from
4387 * @buff: power descriptor buffer
4388 *
4389 * Returns calculated max ICC level for specific regulator
4390 */
4391static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4392{
4393 int i;
4394 int curr_uA;
4395 u16 data;
4396 u16 unit;
4397
4398 for (i = start_scan; i >= 0; i--) {
4399 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4400 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4401 ATTR_ICC_LVL_UNIT_OFFSET;
4402 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4403 switch (unit) {
4404 case UFSHCD_NANO_AMP:
4405 curr_uA = curr_uA / 1000;
4406 break;
4407 case UFSHCD_MILI_AMP:
4408 curr_uA = curr_uA * 1000;
4409 break;
4410 case UFSHCD_AMP:
4411 curr_uA = curr_uA * 1000 * 1000;
4412 break;
4413 case UFSHCD_MICRO_AMP:
4414 default:
4415 break;
4416 }
4417 if (sup_curr_uA >= curr_uA)
4418 break;
4419 }
4420 if (i < 0) {
4421 i = 0;
4422 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4423 }
4424
4425 return (u32)i;
4426}
4427
4428/**
4429 * ufshcd_calc_icc_level - calculate the max ICC level
4430 * In case regulators are not initialized we'll return 0
4431 * @hba: per-adapter instance
4432 * @desc_buf: power descriptor buffer to extract ICC levels from.
4433 * @len: length of desc_buff
4434 *
4435 * Returns calculated ICC level
4436 */
4437static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4438 u8 *desc_buf, int len)
4439{
4440 u32 icc_level = 0;
4441
4442 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4443 !hba->vreg_info.vccq2) {
4444 dev_err(hba->dev,
4445 "%s: Regulator capability was not set, actvIccLevel=%d",
4446 __func__, icc_level);
4447 goto out;
4448 }
4449
4450 if (hba->vreg_info.vcc)
4451 icc_level = ufshcd_get_max_icc_level(
4452 hba->vreg_info.vcc->max_uA,
4453 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4454 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4455
4456 if (hba->vreg_info.vccq)
4457 icc_level = ufshcd_get_max_icc_level(
4458 hba->vreg_info.vccq->max_uA,
4459 icc_level,
4460 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4461
4462 if (hba->vreg_info.vccq2)
4463 icc_level = ufshcd_get_max_icc_level(
4464 hba->vreg_info.vccq2->max_uA,
4465 icc_level,
4466 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4467out:
4468 return icc_level;
4469}
4470
4471static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4472{
4473 int ret;
4474 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4475 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4476
4477 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4478 if (ret) {
4479 dev_err(hba->dev,
4480 "%s: Failed reading power descriptor.len = %d ret = %d",
4481 __func__, buff_len, ret);
4482 return;
4483 }
4484
4485 hba->init_prefetch_data.icc_level =
4486 ufshcd_find_max_sup_active_icc_level(hba,
4487 desc_buf, buff_len);
4488 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4489 __func__, hba->init_prefetch_data.icc_level);
4490
Yaniv Gardi5e86ae42016-02-01 15:02:50 +02004491 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4492 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4493 &hba->init_prefetch_data.icc_level);
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004494
4495 if (ret)
4496 dev_err(hba->dev,
4497 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4498 __func__, hba->init_prefetch_data.icc_level , ret);
4499
4500}
4501
4502/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004503 * ufshcd_scsi_add_wlus - Adds required W-LUs
4504 * @hba: per-adapter instance
4505 *
4506 * UFS device specification requires the UFS devices to support 4 well known
4507 * logical units:
4508 * "REPORT_LUNS" (address: 01h)
4509 * "UFS Device" (address: 50h)
4510 * "RPMB" (address: 44h)
4511 * "BOOT" (address: 30h)
4512 * UFS device's power management needs to be controlled by "POWER CONDITION"
4513 * field of SSU (START STOP UNIT) command. But this "power condition" field
4514 * will take effect only when its sent to "UFS device" well known logical unit
4515 * hence we require the scsi_device instance to represent this logical unit in
4516 * order for the UFS host driver to send the SSU command for power management.
4517
4518 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4519 * Block) LU so user space process can control this LU. User space may also
4520 * want to have access to BOOT LU.
4521
4522 * This function adds scsi device instances for each of all well known LUs
4523 * (except "REPORT LUNS" LU).
4524 *
4525 * Returns zero on success (all required W-LUs are added successfully),
4526 * non-zero error value on failure (if failed to add any of the required W-LU).
4527 */
4528static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4529{
4530 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004531 struct scsi_device *sdev_rpmb;
4532 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004533
4534 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4535 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4536 if (IS_ERR(hba->sdev_ufs_device)) {
4537 ret = PTR_ERR(hba->sdev_ufs_device);
4538 hba->sdev_ufs_device = NULL;
4539 goto out;
4540 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004541 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004542
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004543 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004544 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004545 if (IS_ERR(sdev_boot)) {
4546 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004547 goto remove_sdev_ufs_device;
4548 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004549 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004550
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004551 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004552 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004553 if (IS_ERR(sdev_rpmb)) {
4554 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004555 goto remove_sdev_boot;
4556 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004557 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004558 goto out;
4559
4560remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004561 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004562remove_sdev_ufs_device:
4563 scsi_remove_device(hba->sdev_ufs_device);
4564out:
4565 return ret;
4566}
4567
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004568static int ufs_get_device_info(struct ufs_hba *hba,
4569 struct ufs_device_info *card_data)
4570{
4571 int err;
4572 u8 model_index;
4573 u8 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE + 1] = {0};
4574 u8 desc_buf[QUERY_DESC_DEVICE_MAX_SIZE];
4575
4576 err = ufshcd_read_device_desc(hba, desc_buf,
4577 QUERY_DESC_DEVICE_MAX_SIZE);
4578 if (err) {
4579 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n",
4580 __func__, err);
4581 goto out;
4582 }
4583
4584 /*
4585 * getting vendor (manufacturerID) and Bank Index in big endian
4586 * format
4587 */
4588 card_data->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 |
4589 desc_buf[DEVICE_DESC_PARAM_MANF_ID + 1];
4590
4591 model_index = desc_buf[DEVICE_DESC_PARAM_PRDCT_NAME];
4592
4593 err = ufshcd_read_string_desc(hba, model_index, str_desc_buf,
4594 QUERY_DESC_STRING_MAX_SIZE, ASCII_STD);
4595 if (err) {
4596 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n",
4597 __func__, err);
4598 goto out;
4599 }
4600
4601 str_desc_buf[QUERY_DESC_STRING_MAX_SIZE] = '\0';
4602 strlcpy(card_data->model, (str_desc_buf + QUERY_DESC_HDR_SIZE),
4603 min_t(u8, str_desc_buf[QUERY_DESC_LENGTH_OFFSET],
4604 MAX_MODEL_LEN));
4605
4606 /* Null terminate the model string */
4607 card_data->model[MAX_MODEL_LEN] = '\0';
4608
4609out:
4610 return err;
4611}
4612
4613void ufs_advertise_fixup_device(struct ufs_hba *hba)
4614{
4615 int err;
4616 struct ufs_dev_fix *f;
4617 struct ufs_device_info card_data;
4618
4619 card_data.wmanufacturerid = 0;
4620
4621 err = ufs_get_device_info(hba, &card_data);
4622 if (err) {
4623 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n",
4624 __func__, err);
4625 return;
4626 }
4627
4628 for (f = ufs_fixups; f->quirk; f++) {
4629 if (((f->card.wmanufacturerid == card_data.wmanufacturerid) ||
4630 (f->card.wmanufacturerid == UFS_ANY_VENDOR)) &&
4631 (STR_PRFX_EQUAL(f->card.model, card_data.model) ||
4632 !strcmp(f->card.model, UFS_ANY_MODEL)))
4633 hba->dev_quirks |= f->quirk;
4634 }
4635}
4636
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004637/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004638 * ufshcd_probe_hba - probe hba to detect device and initialize
4639 * @hba: per-adapter instance
4640 *
4641 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304642 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004643static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304644{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304645 int ret;
4646
4647 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304648 if (ret)
4649 goto out;
4650
Yaniv Gardi50646362014-10-23 13:25:13 +03004651 ufshcd_init_pwr_info(hba);
4652
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004653 /* UniPro link is active now */
4654 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304655
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304656 ret = ufshcd_verify_dev_init(hba);
4657 if (ret)
4658 goto out;
4659
Dolev Raviv68078d52013-07-30 00:35:58 +05304660 ret = ufshcd_complete_dev_init(hba);
4661 if (ret)
4662 goto out;
4663
Yaniv Gardic58ab7a2016-03-10 17:37:10 +02004664 ufs_advertise_fixup_device(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004665 /* UFS device is also active now */
4666 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304667 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004668 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304669
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004670 if (ufshcd_get_max_pwr_mode(hba)) {
4671 dev_err(hba->dev,
4672 "%s: Failed getting max supported power mode\n",
4673 __func__);
4674 } else {
4675 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
4676 if (ret)
4677 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
4678 __func__, ret);
4679 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004680
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004681 /* set the state as operational after switching to desired gear */
4682 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004683 /*
4684 * If we are in error handling context or in power management callbacks
4685 * context, no need to scan the host
4686 */
4687 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4688 bool flag;
4689
4690 /* clear any previous UFS device information */
4691 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004692 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4693 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004694 hba->dev_info.f_power_on_wp_en = flag;
4695
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004696 if (!hba->is_init_prefetch)
4697 ufshcd_init_icc_levels(hba);
4698
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004699 /* Add required well known logical units to scsi mid layer */
4700 if (ufshcd_scsi_add_wlus(hba))
4701 goto out;
4702
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304703 scsi_scan_host(hba->host);
4704 pm_runtime_put_sync(hba->dev);
4705 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004706
4707 if (!hba->is_init_prefetch)
4708 hba->is_init_prefetch = true;
4709
Sahitya Tummala856b3482014-09-25 15:32:34 +03004710 /* Resume devfreq after UFS device is detected */
4711 if (ufshcd_is_clkscaling_enabled(hba))
4712 devfreq_resume_device(hba->devfreq);
4713
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304714out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004715 /*
4716 * If we failed to initialize the device or the device is not
4717 * present, turn off the power/clocks etc.
4718 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004719 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4720 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004721 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004722 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004723
4724 return ret;
4725}
4726
4727/**
4728 * ufshcd_async_scan - asynchronous execution for probing hba
4729 * @data: data pointer to pass to this function
4730 * @cookie: cookie data
4731 */
4732static void ufshcd_async_scan(void *data, async_cookie_t cookie)
4733{
4734 struct ufs_hba *hba = (struct ufs_hba *)data;
4735
4736 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304737}
4738
Yaniv Gardif550c652016-03-10 17:37:07 +02004739static enum blk_eh_timer_return ufshcd_eh_timed_out(struct scsi_cmnd *scmd)
4740{
4741 unsigned long flags;
4742 struct Scsi_Host *host;
4743 struct ufs_hba *hba;
4744 int index;
4745 bool found = false;
4746
4747 if (!scmd || !scmd->device || !scmd->device->host)
4748 return BLK_EH_NOT_HANDLED;
4749
4750 host = scmd->device->host;
4751 hba = shost_priv(host);
4752 if (!hba)
4753 return BLK_EH_NOT_HANDLED;
4754
4755 spin_lock_irqsave(host->host_lock, flags);
4756
4757 for_each_set_bit(index, &hba->outstanding_reqs, hba->nutrs) {
4758 if (hba->lrb[index].cmd == scmd) {
4759 found = true;
4760 break;
4761 }
4762 }
4763
4764 spin_unlock_irqrestore(host->host_lock, flags);
4765
4766 /*
4767 * Bypass SCSI error handling and reset the block layer timer if this
4768 * SCSI command was not actually dispatched to UFS driver, otherwise
4769 * let SCSI layer handle the error as usual.
4770 */
4771 return found ? BLK_EH_NOT_HANDLED : BLK_EH_RESET_TIMER;
4772}
4773
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304774static struct scsi_host_template ufshcd_driver_template = {
4775 .module = THIS_MODULE,
4776 .name = UFSHCD,
4777 .proc_name = UFSHCD,
4778 .queuecommand = ufshcd_queuecommand,
4779 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004780 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304781 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004782 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304783 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304784 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
4785 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Yaniv Gardif550c652016-03-10 17:37:07 +02004786 .eh_timed_out = ufshcd_eh_timed_out,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304787 .this_id = -1,
4788 .sg_tablesize = SG_ALL,
4789 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
4790 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004791 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01004792 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304793};
4794
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004795static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
4796 int ua)
4797{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004798 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004799
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004800 if (!vreg)
4801 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004802
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004803 ret = regulator_set_load(vreg->reg, ua);
4804 if (ret < 0) {
4805 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
4806 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004807 }
4808
4809 return ret;
4810}
4811
4812static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
4813 struct ufs_vreg *vreg)
4814{
4815 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
4816}
4817
4818static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
4819 struct ufs_vreg *vreg)
4820{
4821 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
4822}
4823
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004824static int ufshcd_config_vreg(struct device *dev,
4825 struct ufs_vreg *vreg, bool on)
4826{
4827 int ret = 0;
4828 struct regulator *reg = vreg->reg;
4829 const char *name = vreg->name;
4830 int min_uV, uA_load;
4831
4832 BUG_ON(!vreg);
4833
4834 if (regulator_count_voltages(reg) > 0) {
4835 min_uV = on ? vreg->min_uV : 0;
4836 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
4837 if (ret) {
4838 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
4839 __func__, name, ret);
4840 goto out;
4841 }
4842
4843 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004844 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
4845 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004846 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004847 }
4848out:
4849 return ret;
4850}
4851
4852static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
4853{
4854 int ret = 0;
4855
4856 if (!vreg || vreg->enabled)
4857 goto out;
4858
4859 ret = ufshcd_config_vreg(dev, vreg, true);
4860 if (!ret)
4861 ret = regulator_enable(vreg->reg);
4862
4863 if (!ret)
4864 vreg->enabled = true;
4865 else
4866 dev_err(dev, "%s: %s enable failed, err=%d\n",
4867 __func__, vreg->name, ret);
4868out:
4869 return ret;
4870}
4871
4872static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
4873{
4874 int ret = 0;
4875
4876 if (!vreg || !vreg->enabled)
4877 goto out;
4878
4879 ret = regulator_disable(vreg->reg);
4880
4881 if (!ret) {
4882 /* ignore errors on applying disable config */
4883 ufshcd_config_vreg(dev, vreg, false);
4884 vreg->enabled = false;
4885 } else {
4886 dev_err(dev, "%s: %s disable failed, err=%d\n",
4887 __func__, vreg->name, ret);
4888 }
4889out:
4890 return ret;
4891}
4892
4893static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
4894{
4895 int ret = 0;
4896 struct device *dev = hba->dev;
4897 struct ufs_vreg_info *info = &hba->vreg_info;
4898
4899 if (!info)
4900 goto out;
4901
4902 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
4903 if (ret)
4904 goto out;
4905
4906 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
4907 if (ret)
4908 goto out;
4909
4910 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
4911 if (ret)
4912 goto out;
4913
4914out:
4915 if (ret) {
4916 ufshcd_toggle_vreg(dev, info->vccq2, false);
4917 ufshcd_toggle_vreg(dev, info->vccq, false);
4918 ufshcd_toggle_vreg(dev, info->vcc, false);
4919 }
4920 return ret;
4921}
4922
Raviv Shvili6a771a62014-09-25 15:32:24 +03004923static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
4924{
4925 struct ufs_vreg_info *info = &hba->vreg_info;
4926
4927 if (info)
4928 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
4929
4930 return 0;
4931}
4932
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004933static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
4934{
4935 int ret = 0;
4936
4937 if (!vreg)
4938 goto out;
4939
4940 vreg->reg = devm_regulator_get(dev, vreg->name);
4941 if (IS_ERR(vreg->reg)) {
4942 ret = PTR_ERR(vreg->reg);
4943 dev_err(dev, "%s: %s get failed, err=%d\n",
4944 __func__, vreg->name, ret);
4945 }
4946out:
4947 return ret;
4948}
4949
4950static int ufshcd_init_vreg(struct ufs_hba *hba)
4951{
4952 int ret = 0;
4953 struct device *dev = hba->dev;
4954 struct ufs_vreg_info *info = &hba->vreg_info;
4955
4956 if (!info)
4957 goto out;
4958
4959 ret = ufshcd_get_vreg(dev, info->vcc);
4960 if (ret)
4961 goto out;
4962
4963 ret = ufshcd_get_vreg(dev, info->vccq);
4964 if (ret)
4965 goto out;
4966
4967 ret = ufshcd_get_vreg(dev, info->vccq2);
4968out:
4969 return ret;
4970}
4971
Raviv Shvili6a771a62014-09-25 15:32:24 +03004972static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
4973{
4974 struct ufs_vreg_info *info = &hba->vreg_info;
4975
4976 if (info)
4977 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
4978
4979 return 0;
4980}
4981
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004982static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
4983 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004984{
4985 int ret = 0;
4986 struct ufs_clk_info *clki;
4987 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004988 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004989
4990 if (!head || list_empty(head))
4991 goto out;
4992
4993 list_for_each_entry(clki, head, list) {
4994 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004995 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
4996 continue;
4997
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004998 if (on && !clki->enabled) {
4999 ret = clk_prepare_enable(clki->clk);
5000 if (ret) {
5001 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
5002 __func__, clki->name, ret);
5003 goto out;
5004 }
5005 } else if (!on && clki->enabled) {
5006 clk_disable_unprepare(clki->clk);
5007 }
5008 clki->enabled = on;
5009 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
5010 clki->name, on ? "en" : "dis");
5011 }
5012 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005013
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005014 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005015out:
5016 if (ret) {
5017 list_for_each_entry(clki, head, list) {
5018 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
5019 clk_disable_unprepare(clki->clk);
5020 }
Dolev Raviveda910e2014-10-23 13:25:16 +03005021 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005022 spin_lock_irqsave(hba->host->host_lock, flags);
5023 hba->clk_gating.state = CLKS_ON;
5024 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005025 }
5026 return ret;
5027}
5028
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005029static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
5030{
5031 return __ufshcd_setup_clocks(hba, on, false);
5032}
5033
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005034static int ufshcd_init_clocks(struct ufs_hba *hba)
5035{
5036 int ret = 0;
5037 struct ufs_clk_info *clki;
5038 struct device *dev = hba->dev;
5039 struct list_head *head = &hba->clk_list_head;
5040
5041 if (!head || list_empty(head))
5042 goto out;
5043
5044 list_for_each_entry(clki, head, list) {
5045 if (!clki->name)
5046 continue;
5047
5048 clki->clk = devm_clk_get(dev, clki->name);
5049 if (IS_ERR(clki->clk)) {
5050 ret = PTR_ERR(clki->clk);
5051 dev_err(dev, "%s: %s clk get failed, %d\n",
5052 __func__, clki->name, ret);
5053 goto out;
5054 }
5055
5056 if (clki->max_freq) {
5057 ret = clk_set_rate(clki->clk, clki->max_freq);
5058 if (ret) {
5059 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5060 __func__, clki->name,
5061 clki->max_freq, ret);
5062 goto out;
5063 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03005064 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005065 }
5066 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
5067 clki->name, clk_get_rate(clki->clk));
5068 }
5069out:
5070 return ret;
5071}
5072
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005073static int ufshcd_variant_hba_init(struct ufs_hba *hba)
5074{
5075 int err = 0;
5076
5077 if (!hba->vops)
5078 goto out;
5079
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005080 err = ufshcd_vops_init(hba);
5081 if (err)
5082 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005083
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005084 err = ufshcd_vops_setup_regulators(hba, true);
5085 if (err)
5086 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005087
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005088 goto out;
5089
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005090out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005091 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005092out:
5093 if (err)
5094 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005095 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005096 return err;
5097}
5098
5099static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
5100{
5101 if (!hba->vops)
5102 return;
5103
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005104 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005105
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005106 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005107
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005108 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005109}
5110
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005111static int ufshcd_hba_init(struct ufs_hba *hba)
5112{
5113 int err;
5114
Raviv Shvili6a771a62014-09-25 15:32:24 +03005115 /*
5116 * Handle host controller power separately from the UFS device power
5117 * rails as it will help controlling the UFS host controller power
5118 * collapse easily which is different than UFS device power collapse.
5119 * Also, enable the host controller power before we go ahead with rest
5120 * of the initialization here.
5121 */
5122 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005123 if (err)
5124 goto out;
5125
Raviv Shvili6a771a62014-09-25 15:32:24 +03005126 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005127 if (err)
5128 goto out;
5129
Raviv Shvili6a771a62014-09-25 15:32:24 +03005130 err = ufshcd_init_clocks(hba);
5131 if (err)
5132 goto out_disable_hba_vreg;
5133
5134 err = ufshcd_setup_clocks(hba, true);
5135 if (err)
5136 goto out_disable_hba_vreg;
5137
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005138 err = ufshcd_init_vreg(hba);
5139 if (err)
5140 goto out_disable_clks;
5141
5142 err = ufshcd_setup_vreg(hba, true);
5143 if (err)
5144 goto out_disable_clks;
5145
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005146 err = ufshcd_variant_hba_init(hba);
5147 if (err)
5148 goto out_disable_vreg;
5149
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005150 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005151 goto out;
5152
5153out_disable_vreg:
5154 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03005155out_disable_clks:
5156 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03005157out_disable_hba_vreg:
5158 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005159out:
5160 return err;
5161}
5162
5163static void ufshcd_hba_exit(struct ufs_hba *hba)
5164{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03005165 if (hba->is_powered) {
5166 ufshcd_variant_hba_exit(hba);
5167 ufshcd_setup_vreg(hba, false);
5168 ufshcd_setup_clocks(hba, false);
5169 ufshcd_setup_hba_vreg(hba, false);
5170 hba->is_powered = false;
5171 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005172}
5173
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005174static int
5175ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305176{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005177 unsigned char cmd[6] = {REQUEST_SENSE,
5178 0,
5179 0,
5180 0,
5181 SCSI_SENSE_BUFFERSIZE,
5182 0};
5183 char *buffer;
5184 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305185
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005186 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
5187 if (!buffer) {
5188 ret = -ENOMEM;
5189 goto out;
5190 }
5191
5192 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
5193 SCSI_SENSE_BUFFERSIZE, NULL,
5194 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
5195 if (ret)
5196 pr_err("%s: failed with err %d\n", __func__, ret);
5197
5198 kfree(buffer);
5199out:
5200 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305201}
5202
5203/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005204 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
5205 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305206 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005207 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305208 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005209 * Returns 0 if requested power mode is set successfully
5210 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305211 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005212static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
5213 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305214{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005215 unsigned char cmd[6] = { START_STOP };
5216 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005217 struct scsi_device *sdp;
5218 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005219 int ret;
5220
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005221 spin_lock_irqsave(hba->host->host_lock, flags);
5222 sdp = hba->sdev_ufs_device;
5223 if (sdp) {
5224 ret = scsi_device_get(sdp);
5225 if (!ret && !scsi_device_online(sdp)) {
5226 ret = -ENODEV;
5227 scsi_device_put(sdp);
5228 }
5229 } else {
5230 ret = -ENODEV;
5231 }
5232 spin_unlock_irqrestore(hba->host->host_lock, flags);
5233
5234 if (ret)
5235 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005236
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305237 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005238 * If scsi commands fail, the scsi mid-layer schedules scsi error-
5239 * handling, which would wait for host to be resumed. Since we know
5240 * we are functional while we are here, skip host resume in error
5241 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305242 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005243 hba->host->eh_noresume = 1;
5244 if (hba->wlun_dev_clr_ua) {
5245 ret = ufshcd_send_request_sense(hba, sdp);
5246 if (ret)
5247 goto out;
5248 /* Unit attention condition is cleared now */
5249 hba->wlun_dev_clr_ua = false;
5250 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305251
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005252 cmd[4] = pwr_mode << 4;
5253
5254 /*
5255 * Current function would be generally called from the power management
5256 * callbacks hence set the REQ_PM flag so that it doesn't resume the
5257 * already suspended childs.
5258 */
5259 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
5260 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
5261 if (ret) {
5262 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02005263 "START_STOP failed for power mode: %d, result %x\n",
5264 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005265 if (driver_byte(ret) & DRIVER_SENSE)
5266 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005267 }
5268
5269 if (!ret)
5270 hba->curr_dev_pwr_mode = pwr_mode;
5271out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005272 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005273 hba->host->eh_noresume = 0;
5274 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305275}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305276
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005277static int ufshcd_link_state_transition(struct ufs_hba *hba,
5278 enum uic_link_state req_link_state,
5279 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305280{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005281 int ret = 0;
5282
5283 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305284 return 0;
5285
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005286 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5287 ret = ufshcd_uic_hibern8_enter(hba);
5288 if (!ret)
5289 ufshcd_set_link_hibern8(hba);
5290 else
5291 goto out;
5292 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305293 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005294 * If autobkops is enabled, link can't be turned off because
5295 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305296 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005297 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5298 (!check_for_bkops || (check_for_bkops &&
5299 !hba->auto_bkops_enabled))) {
5300 /*
5301 * Change controller state to "reset state" which
5302 * should also put the link in off/reset state
5303 */
Yaniv Gardi596585a2016-03-10 17:37:08 +02005304 ufshcd_hba_stop(hba, true);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005305 /*
5306 * TODO: Check if we need any delay to make sure that
5307 * controller is reset
5308 */
5309 ufshcd_set_link_off(hba);
5310 }
5311
5312out:
5313 return ret;
5314}
5315
5316static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5317{
5318 /*
5319 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5320 * power.
5321 *
5322 * If UFS device and link is in OFF state, all power supplies (VCC,
5323 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5324 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5325 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5326 *
5327 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5328 * in low power state which would save some power.
5329 */
5330 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5331 !hba->dev_info.is_lu_power_on_wp) {
5332 ufshcd_setup_vreg(hba, false);
5333 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5334 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5335 if (!ufshcd_is_link_active(hba)) {
5336 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5337 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5338 }
5339 }
5340}
5341
5342static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5343{
5344 int ret = 0;
5345
5346 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5347 !hba->dev_info.is_lu_power_on_wp) {
5348 ret = ufshcd_setup_vreg(hba, true);
5349 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5350 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
5351 if (!ret && !ufshcd_is_link_active(hba)) {
5352 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5353 if (ret)
5354 goto vcc_disable;
5355 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5356 if (ret)
5357 goto vccq_lpm;
5358 }
5359 }
5360 goto out;
5361
5362vccq_lpm:
5363 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5364vcc_disable:
5365 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5366out:
5367 return ret;
5368}
5369
5370static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5371{
5372 if (ufshcd_is_link_off(hba))
5373 ufshcd_setup_hba_vreg(hba, false);
5374}
5375
5376static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5377{
5378 if (ufshcd_is_link_off(hba))
5379 ufshcd_setup_hba_vreg(hba, true);
5380}
5381
5382/**
5383 * ufshcd_suspend - helper function for suspend operations
5384 * @hba: per adapter instance
5385 * @pm_op: desired low power operation type
5386 *
5387 * This function will try to put the UFS device and link into low power
5388 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5389 * (System PM level).
5390 *
5391 * If this function is called during shutdown, it will make sure that
5392 * both UFS device and UFS link is powered off.
5393 *
5394 * NOTE: UFS device & link must be active before we enter in this function.
5395 *
5396 * Returns 0 for success and non-zero for failure
5397 */
5398static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5399{
5400 int ret = 0;
5401 enum ufs_pm_level pm_lvl;
5402 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5403 enum uic_link_state req_link_state;
5404
5405 hba->pm_op_in_progress = 1;
5406 if (!ufshcd_is_shutdown_pm(pm_op)) {
5407 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5408 hba->rpm_lvl : hba->spm_lvl;
5409 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5410 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5411 } else {
5412 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5413 req_link_state = UIC_LINK_OFF_STATE;
5414 }
5415
5416 /*
5417 * If we can't transition into any of the low power modes
5418 * just gate the clocks.
5419 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005420 ufshcd_hold(hba, false);
5421 hba->clk_gating.is_suspended = true;
5422
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005423 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5424 req_link_state == UIC_LINK_ACTIVE_STATE) {
5425 goto disable_clks;
5426 }
5427
5428 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5429 (req_link_state == hba->uic_link_state))
5430 goto out;
5431
5432 /* UFS device & link must be active before we enter in this function */
5433 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5434 ret = -EINVAL;
5435 goto out;
5436 }
5437
5438 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005439 if (ufshcd_can_autobkops_during_suspend(hba)) {
5440 /*
5441 * The device is idle with no requests in the queue,
5442 * allow background operations if bkops status shows
5443 * that performance might be impacted.
5444 */
5445 ret = ufshcd_urgent_bkops(hba);
5446 if (ret)
5447 goto enable_gating;
5448 } else {
5449 /* make sure that auto bkops is disabled */
5450 ufshcd_disable_auto_bkops(hba);
5451 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005452 }
5453
5454 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5455 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5456 !ufshcd_is_runtime_pm(pm_op))) {
5457 /* ensure that bkops is disabled */
5458 ufshcd_disable_auto_bkops(hba);
5459 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5460 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005461 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005462 }
5463
5464 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5465 if (ret)
5466 goto set_dev_active;
5467
5468 ufshcd_vreg_set_lpm(hba);
5469
5470disable_clks:
5471 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03005472 * The clock scaling needs access to controller registers. Hence, Wait
5473 * for pending clock scaling work to be done before clocks are
5474 * turned off.
5475 */
5476 if (ufshcd_is_clkscaling_enabled(hba)) {
5477 devfreq_suspend_device(hba->devfreq);
5478 hba->clk_scaling.window_start_t = 0;
5479 }
5480 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005481 * Call vendor specific suspend callback. As these callbacks may access
5482 * vendor specific host controller register space call them before the
5483 * host clocks are ON.
5484 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005485 ret = ufshcd_vops_suspend(hba, pm_op);
5486 if (ret)
5487 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005488
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005489 ret = ufshcd_vops_setup_clocks(hba, false);
5490 if (ret)
5491 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005492
5493 if (!ufshcd_is_link_active(hba))
5494 ufshcd_setup_clocks(hba, false);
5495 else
5496 /* If link is active, device ref_clk can't be switched off */
5497 __ufshcd_setup_clocks(hba, false, true);
5498
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005499 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005500 /*
5501 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005502 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005503 */
5504 ufshcd_disable_irq(hba);
5505 /* Put the host controller in low power mode if possible */
5506 ufshcd_hba_vreg_set_lpm(hba);
5507 goto out;
5508
5509vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005510 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005511set_link_active:
5512 ufshcd_vreg_set_hpm(hba);
5513 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5514 ufshcd_set_link_active(hba);
5515 else if (ufshcd_is_link_off(hba))
5516 ufshcd_host_reset_and_restore(hba);
5517set_dev_active:
5518 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5519 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005520enable_gating:
5521 hba->clk_gating.is_suspended = false;
5522 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005523out:
5524 hba->pm_op_in_progress = 0;
5525 return ret;
5526}
5527
5528/**
5529 * ufshcd_resume - helper function for resume operations
5530 * @hba: per adapter instance
5531 * @pm_op: runtime PM or system PM
5532 *
5533 * This function basically brings the UFS device, UniPro link and controller
5534 * to active state.
5535 *
5536 * Returns 0 for success and non-zero for failure
5537 */
5538static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5539{
5540 int ret;
5541 enum uic_link_state old_link_state;
5542
5543 hba->pm_op_in_progress = 1;
5544 old_link_state = hba->uic_link_state;
5545
5546 ufshcd_hba_vreg_set_hpm(hba);
5547 /* Make sure clocks are enabled before accessing controller */
5548 ret = ufshcd_setup_clocks(hba, true);
5549 if (ret)
5550 goto out;
5551
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005552 /* enable the host irq as host controller would be active soon */
5553 ret = ufshcd_enable_irq(hba);
5554 if (ret)
5555 goto disable_irq_and_vops_clks;
5556
5557 ret = ufshcd_vreg_set_hpm(hba);
5558 if (ret)
5559 goto disable_irq_and_vops_clks;
5560
5561 /*
5562 * Call vendor specific resume callback. As these callbacks may access
5563 * vendor specific host controller register space call them when the
5564 * host clocks are ON.
5565 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005566 ret = ufshcd_vops_resume(hba, pm_op);
5567 if (ret)
5568 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005569
5570 if (ufshcd_is_link_hibern8(hba)) {
5571 ret = ufshcd_uic_hibern8_exit(hba);
5572 if (!ret)
5573 ufshcd_set_link_active(hba);
5574 else
5575 goto vendor_suspend;
5576 } else if (ufshcd_is_link_off(hba)) {
5577 ret = ufshcd_host_reset_and_restore(hba);
5578 /*
5579 * ufshcd_host_reset_and_restore() should have already
5580 * set the link state as active
5581 */
5582 if (ret || !ufshcd_is_link_active(hba))
5583 goto vendor_suspend;
5584 }
5585
5586 if (!ufshcd_is_ufs_dev_active(hba)) {
5587 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
5588 if (ret)
5589 goto set_old_link_state;
5590 }
5591
Subhash Jadavani374a2462014-09-25 15:32:35 +03005592 /*
5593 * If BKOPs operations are urgently needed at this moment then
5594 * keep auto-bkops enabled or else disable it.
5595 */
5596 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005597 hba->clk_gating.is_suspended = false;
5598
Sahitya Tummala856b3482014-09-25 15:32:34 +03005599 if (ufshcd_is_clkscaling_enabled(hba))
5600 devfreq_resume_device(hba->devfreq);
5601
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005602 /* Schedule clock gating in case of no access to UFS device yet */
5603 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005604 goto out;
5605
5606set_old_link_state:
5607 ufshcd_link_state_transition(hba, old_link_state, 0);
5608vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005609 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005610disable_vreg:
5611 ufshcd_vreg_set_lpm(hba);
5612disable_irq_and_vops_clks:
5613 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005614 ufshcd_setup_clocks(hba, false);
5615out:
5616 hba->pm_op_in_progress = 0;
5617 return ret;
5618}
5619
5620/**
5621 * ufshcd_system_suspend - system suspend routine
5622 * @hba: per adapter instance
5623 * @pm_op: runtime PM or system PM
5624 *
5625 * Check the description of ufshcd_suspend() function for more details.
5626 *
5627 * Returns 0 for success and non-zero for failure
5628 */
5629int ufshcd_system_suspend(struct ufs_hba *hba)
5630{
5631 int ret = 0;
5632
5633 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03005634 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005635
5636 if (pm_runtime_suspended(hba->dev)) {
5637 if (hba->rpm_lvl == hba->spm_lvl)
5638 /*
5639 * There is possibility that device may still be in
5640 * active state during the runtime suspend.
5641 */
5642 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
5643 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
5644 goto out;
5645
5646 /*
5647 * UFS device and/or UFS link low power states during runtime
5648 * suspend seems to be different than what is expected during
5649 * system suspend. Hence runtime resume the devic & link and
5650 * let the system suspend low power states to take effect.
5651 * TODO: If resume takes longer time, we might have optimize
5652 * it in future by not resuming everything if possible.
5653 */
5654 ret = ufshcd_runtime_resume(hba);
5655 if (ret)
5656 goto out;
5657 }
5658
5659 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
5660out:
Dolev Ravive7850602014-09-25 15:32:36 +03005661 if (!ret)
5662 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005663 return ret;
5664}
5665EXPORT_SYMBOL(ufshcd_system_suspend);
5666
5667/**
5668 * ufshcd_system_resume - system resume routine
5669 * @hba: per adapter instance
5670 *
5671 * Returns 0 for success and non-zero for failure
5672 */
5673
5674int ufshcd_system_resume(struct ufs_hba *hba)
5675{
5676 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
5677 /*
5678 * Let the runtime resume take care of resuming
5679 * if runtime suspended.
5680 */
5681 return 0;
5682
5683 return ufshcd_resume(hba, UFS_SYSTEM_PM);
5684}
5685EXPORT_SYMBOL(ufshcd_system_resume);
5686
5687/**
5688 * ufshcd_runtime_suspend - runtime suspend routine
5689 * @hba: per adapter instance
5690 *
5691 * Check the description of ufshcd_suspend() function for more details.
5692 *
5693 * Returns 0 for success and non-zero for failure
5694 */
5695int ufshcd_runtime_suspend(struct ufs_hba *hba)
5696{
5697 if (!hba || !hba->is_powered)
5698 return 0;
5699
5700 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305701}
5702EXPORT_SYMBOL(ufshcd_runtime_suspend);
5703
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005704/**
5705 * ufshcd_runtime_resume - runtime resume routine
5706 * @hba: per adapter instance
5707 *
5708 * This function basically brings the UFS device, UniPro link and controller
5709 * to active state. Following operations are done in this function:
5710 *
5711 * 1. Turn on all the controller related clocks
5712 * 2. Bring the UniPro link out of Hibernate state
5713 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
5714 * to active state.
5715 * 4. If auto-bkops is enabled on the device, disable it.
5716 *
5717 * So following would be the possible power state after this function return
5718 * successfully:
5719 * S1: UFS device in Active state with VCC rail ON
5720 * UniPro link in Active state
5721 * All the UFS/UniPro controller clocks are ON
5722 *
5723 * Returns 0 for success and non-zero for failure
5724 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305725int ufshcd_runtime_resume(struct ufs_hba *hba)
5726{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005727 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305728 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005729 else
5730 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305731}
5732EXPORT_SYMBOL(ufshcd_runtime_resume);
5733
5734int ufshcd_runtime_idle(struct ufs_hba *hba)
5735{
5736 return 0;
5737}
5738EXPORT_SYMBOL(ufshcd_runtime_idle);
5739
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305740/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005741 * ufshcd_shutdown - shutdown routine
5742 * @hba: per adapter instance
5743 *
5744 * This function would power off both UFS device and UFS link.
5745 *
5746 * Returns 0 always to allow force shutdown even in case of errors.
5747 */
5748int ufshcd_shutdown(struct ufs_hba *hba)
5749{
5750 int ret = 0;
5751
5752 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
5753 goto out;
5754
5755 if (pm_runtime_suspended(hba->dev)) {
5756 ret = ufshcd_runtime_resume(hba);
5757 if (ret)
5758 goto out;
5759 }
5760
5761 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
5762out:
5763 if (ret)
5764 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
5765 /* allow force shutdown even in case of errors */
5766 return 0;
5767}
5768EXPORT_SYMBOL(ufshcd_shutdown);
5769
5770/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305771 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305772 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305773 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305774 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305775void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305776{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05305777 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305778 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305779 ufshcd_disable_intr(hba, hba->intr_mask);
Yaniv Gardi596585a2016-03-10 17:37:08 +02005780 ufshcd_hba_stop(hba, true);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305781
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305782 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005783
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005784 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005785 if (ufshcd_is_clkscaling_enabled(hba))
5786 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005787 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305788}
5789EXPORT_SYMBOL_GPL(ufshcd_remove);
5790
5791/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02005792 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
5793 * @hba: pointer to Host Bus Adapter (HBA)
5794 */
5795void ufshcd_dealloc_host(struct ufs_hba *hba)
5796{
5797 scsi_host_put(hba->host);
5798}
5799EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
5800
5801/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005802 * ufshcd_set_dma_mask - Set dma mask based on the controller
5803 * addressing capability
5804 * @hba: per adapter instance
5805 *
5806 * Returns 0 for success, non-zero for failure
5807 */
5808static int ufshcd_set_dma_mask(struct ufs_hba *hba)
5809{
5810 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
5811 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
5812 return 0;
5813 }
5814 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
5815}
5816
5817/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005818 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305819 * @dev: pointer to device handle
5820 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305821 * Returns 0 on success, non-zero value on failure
5822 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005823int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305824{
5825 struct Scsi_Host *host;
5826 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005827 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305828
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305829 if (!dev) {
5830 dev_err(dev,
5831 "Invalid memory reference for dev is NULL\n");
5832 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305833 goto out_error;
5834 }
5835
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305836 host = scsi_host_alloc(&ufshcd_driver_template,
5837 sizeof(struct ufs_hba));
5838 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305839 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305840 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305841 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305842 }
5843 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305844 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305845 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005846 *hba_handle = hba;
5847
5848out_error:
5849 return err;
5850}
5851EXPORT_SYMBOL(ufshcd_alloc_host);
5852
Sahitya Tummala856b3482014-09-25 15:32:34 +03005853static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
5854{
5855 int ret = 0;
5856 struct ufs_clk_info *clki;
5857 struct list_head *head = &hba->clk_list_head;
5858
5859 if (!head || list_empty(head))
5860 goto out;
5861
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005862 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
5863 if (ret)
5864 return ret;
5865
Sahitya Tummala856b3482014-09-25 15:32:34 +03005866 list_for_each_entry(clki, head, list) {
5867 if (!IS_ERR_OR_NULL(clki->clk)) {
5868 if (scale_up && clki->max_freq) {
5869 if (clki->curr_freq == clki->max_freq)
5870 continue;
5871 ret = clk_set_rate(clki->clk, clki->max_freq);
5872 if (ret) {
5873 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5874 __func__, clki->name,
5875 clki->max_freq, ret);
5876 break;
5877 }
5878 clki->curr_freq = clki->max_freq;
5879
5880 } else if (!scale_up && clki->min_freq) {
5881 if (clki->curr_freq == clki->min_freq)
5882 continue;
5883 ret = clk_set_rate(clki->clk, clki->min_freq);
5884 if (ret) {
5885 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5886 __func__, clki->name,
5887 clki->min_freq, ret);
5888 break;
5889 }
5890 clki->curr_freq = clki->min_freq;
5891 }
5892 }
5893 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
5894 clki->name, clk_get_rate(clki->clk));
5895 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005896
5897 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
5898
Sahitya Tummala856b3482014-09-25 15:32:34 +03005899out:
5900 return ret;
5901}
5902
5903static int ufshcd_devfreq_target(struct device *dev,
5904 unsigned long *freq, u32 flags)
5905{
5906 int err = 0;
5907 struct ufs_hba *hba = dev_get_drvdata(dev);
5908
5909 if (!ufshcd_is_clkscaling_enabled(hba))
5910 return -EINVAL;
5911
5912 if (*freq == UINT_MAX)
5913 err = ufshcd_scale_clks(hba, true);
5914 else if (*freq == 0)
5915 err = ufshcd_scale_clks(hba, false);
5916
5917 return err;
5918}
5919
5920static int ufshcd_devfreq_get_dev_status(struct device *dev,
5921 struct devfreq_dev_status *stat)
5922{
5923 struct ufs_hba *hba = dev_get_drvdata(dev);
5924 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
5925 unsigned long flags;
5926
5927 if (!ufshcd_is_clkscaling_enabled(hba))
5928 return -EINVAL;
5929
5930 memset(stat, 0, sizeof(*stat));
5931
5932 spin_lock_irqsave(hba->host->host_lock, flags);
5933 if (!scaling->window_start_t)
5934 goto start_window;
5935
5936 if (scaling->is_busy_started)
5937 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
5938 scaling->busy_start_t));
5939
5940 stat->total_time = jiffies_to_usecs((long)jiffies -
5941 (long)scaling->window_start_t);
5942 stat->busy_time = scaling->tot_busy_t;
5943start_window:
5944 scaling->window_start_t = jiffies;
5945 scaling->tot_busy_t = 0;
5946
5947 if (hba->outstanding_reqs) {
5948 scaling->busy_start_t = ktime_get();
5949 scaling->is_busy_started = true;
5950 } else {
5951 scaling->busy_start_t = ktime_set(0, 0);
5952 scaling->is_busy_started = false;
5953 }
5954 spin_unlock_irqrestore(hba->host->host_lock, flags);
5955 return 0;
5956}
5957
5958static struct devfreq_dev_profile ufs_devfreq_profile = {
5959 .polling_ms = 100,
5960 .target = ufshcd_devfreq_target,
5961 .get_dev_status = ufshcd_devfreq_get_dev_status,
5962};
5963
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005964/**
5965 * ufshcd_init - Driver initialization routine
5966 * @hba: per-adapter instance
5967 * @mmio_base: base register address
5968 * @irq: Interrupt line of device
5969 * Returns 0 on success, non-zero value on failure
5970 */
5971int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
5972{
5973 int err;
5974 struct Scsi_Host *host = hba->host;
5975 struct device *dev = hba->dev;
5976
5977 if (!mmio_base) {
5978 dev_err(hba->dev,
5979 "Invalid memory reference for mmio_base is NULL\n");
5980 err = -ENODEV;
5981 goto out_error;
5982 }
5983
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305984 hba->mmio_base = mmio_base;
5985 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305986
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005987 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005988 if (err)
5989 goto out_error;
5990
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305991 /* Read capabilities registers */
5992 ufshcd_hba_capabilities(hba);
5993
5994 /* Get UFS version supported by the controller */
5995 hba->ufs_version = ufshcd_get_ufs_version(hba);
5996
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305997 /* Get Interrupt bit mask per version */
5998 hba->intr_mask = ufshcd_get_intr_mask(hba);
5999
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09006000 err = ufshcd_set_dma_mask(hba);
6001 if (err) {
6002 dev_err(hba->dev, "set dma mask failed\n");
6003 goto out_disable;
6004 }
6005
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306006 /* Allocate memory for host memory space */
6007 err = ufshcd_memory_alloc(hba);
6008 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306009 dev_err(hba->dev, "Memory allocation failed\n");
6010 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306011 }
6012
6013 /* Configure LRB */
6014 ufshcd_host_memory_configure(hba);
6015
6016 host->can_queue = hba->nutrs;
6017 host->cmd_per_lun = hba->nutrs;
6018 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03006019 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306020 host->max_channel = UFSHCD_MAX_CHANNEL;
6021 host->unique_id = host->host_no;
6022 host->max_cmd_len = MAX_CDB_SIZE;
6023
Dolev Raviv7eb584d2014-09-25 15:32:31 +03006024 hba->max_pwr_info.is_valid = false;
6025
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306026 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05306027 init_waitqueue_head(&hba->tm_wq);
6028 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306029
6030 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05306031 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05306032 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306033
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306034 /* Initialize UIC command mutex */
6035 mutex_init(&hba->uic_cmd_mutex);
6036
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05306037 /* Initialize mutex for device management commands */
6038 mutex_init(&hba->dev_cmd.lock);
6039
6040 /* Initialize device management tag acquire wait queue */
6041 init_waitqueue_head(&hba->dev_cmd.tag_wq);
6042
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006043 ufshcd_init_clk_gating(hba);
Yaniv Gardi199ef132016-03-10 17:37:06 +02006044
6045 /*
6046 * In order to avoid any spurious interrupt immediately after
6047 * registering UFS controller interrupt handler, clear any pending UFS
6048 * interrupt status and disable all the UFS interrupts.
6049 */
6050 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS),
6051 REG_INTERRUPT_STATUS);
6052 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE);
6053 /*
6054 * Make sure that UFS interrupts are disabled and any pending interrupt
6055 * status is cleared before registering UFS interrupt handler.
6056 */
6057 mb();
6058
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306059 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09006060 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306061 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306062 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006063 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006064 } else {
6065 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306066 }
6067
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306068 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306069 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306070 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006071 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306072 }
6073
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306074 /* Host controller enable */
6075 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306076 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306077 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306078 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306079 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306080
Sahitya Tummala856b3482014-09-25 15:32:34 +03006081 if (ufshcd_is_clkscaling_enabled(hba)) {
6082 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
6083 "simple_ondemand", NULL);
6084 if (IS_ERR(hba->devfreq)) {
6085 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
6086 PTR_ERR(hba->devfreq));
6087 goto out_remove_scsi_host;
6088 }
6089 /* Suspend devfreq until the UFS device is detected */
6090 devfreq_suspend_device(hba->devfreq);
6091 hba->clk_scaling.window_start_t = 0;
6092 }
6093
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05306094 /* Hold auto suspend until async scan completes */
6095 pm_runtime_get_sync(dev);
6096
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006097 /*
6098 * The device-initialize-sequence hasn't been invoked yet.
6099 * Set the device to power-off state
6100 */
6101 ufshcd_set_ufs_dev_poweroff(hba);
6102
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05306103 async_schedule(ufshcd_async_scan, hba);
6104
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306105 return 0;
6106
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306107out_remove_scsi_host:
6108 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03006109exit_gating:
6110 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306111out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03006112 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306113 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03006114 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306115out_error:
6116 return err;
6117}
6118EXPORT_SYMBOL_GPL(ufshcd_init);
6119
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05306120MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
6121MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05306122MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306123MODULE_LICENSE("GPL");
6124MODULE_VERSION(UFSHCD_DRIVER_VERSION);