blob: 87da429ab7eb1e48650e5282008aaf0cbf7e8b3f [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>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053042
Vinayak Holikattie0eca632013-02-25 21:44:33 +053043#include "ufshcd.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053044#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053045
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053046#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
47 UTP_TASK_REQ_COMPL |\
48 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053049/* UIC command timeout, unit: ms */
50#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053051
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053052/* NOP OUT retries waiting for NOP IN response */
53#define NOP_OUT_RETRIES 10
54/* Timeout after 30 msecs if NOP OUT hangs without response */
55#define NOP_OUT_TIMEOUT 30 /* msecs */
56
Dolev Raviv68078d52013-07-30 00:35:58 +053057/* Query request retries */
58#define QUERY_REQ_RETRIES 10
59/* Query request timeout */
60#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020061/*
62 * Query request timeout for fDeviceInit flag
63 * fDeviceInit query response time for some devices is too large that default
64 * QUERY_REQ_TIMEOUT may not be enough for such devices.
65 */
66#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053067
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053068/* Task management command timeout */
69#define TM_CMD_TIMEOUT 100 /* msecs */
70
Yaniv Gardi64238fb2016-02-01 15:02:43 +020071/* maximum number of retries for a general UIC command */
72#define UFS_UIC_COMMAND_RETRIES 3
73
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030074/* maximum number of link-startup retries */
75#define DME_LINKSTARTUP_RETRIES 3
76
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020077/* Maximum retries for Hibern8 enter */
78#define UIC_HIBERN8_ENTER_RETRIES 3
79
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030080/* maximum number of reset retries before giving up */
81#define MAX_HOST_RESET_RETRIES 5
82
Dolev Raviv68078d52013-07-30 00:35:58 +053083/* Expose the flag value from utp_upiu_query.value */
84#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
85
Seungwon Jeon7d568652013-08-31 21:40:20 +053086/* Interrupt aggregation default timeout, unit: 40us */
87#define INT_AGGR_DEF_TO 0x02
88
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030089#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
90 ({ \
91 int _ret; \
92 if (_on) \
93 _ret = ufshcd_enable_vreg(_dev, _vreg); \
94 else \
95 _ret = ufshcd_disable_vreg(_dev, _vreg); \
96 _ret; \
97 })
98
Subhash Jadavanida461ce2014-09-25 15:32:25 +030099static u32 ufs_query_desc_max_size[] = {
100 QUERY_DESC_DEVICE_MAX_SIZE,
101 QUERY_DESC_CONFIGURAION_MAX_SIZE,
102 QUERY_DESC_UNIT_MAX_SIZE,
103 QUERY_DESC_RFU_MAX_SIZE,
104 QUERY_DESC_INTERCONNECT_MAX_SIZE,
105 QUERY_DESC_STRING_MAX_SIZE,
106 QUERY_DESC_RFU_MAX_SIZE,
107 QUERY_DESC_GEOMETRY_MAZ_SIZE,
108 QUERY_DESC_POWER_MAX_SIZE,
109 QUERY_DESC_RFU_MAX_SIZE,
110};
111
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530112enum {
113 UFSHCD_MAX_CHANNEL = 0,
114 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530115 UFSHCD_CMD_PER_LUN = 32,
116 UFSHCD_CAN_QUEUE = 32,
117};
118
119/* UFSHCD states */
120enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530121 UFSHCD_STATE_RESET,
122 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530123 UFSHCD_STATE_OPERATIONAL,
124};
125
126/* UFSHCD error handling flags */
127enum {
128 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530129};
130
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530131/* UFSHCD UIC layer error flags */
132enum {
133 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
134 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
135 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
136 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
137};
138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530139/* Interrupt configuration options */
140enum {
141 UFSHCD_INT_DISABLE,
142 UFSHCD_INT_ENABLE,
143 UFSHCD_INT_CLEAR,
144};
145
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530146#define ufshcd_set_eh_in_progress(h) \
147 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
148#define ufshcd_eh_in_progress(h) \
149 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
150#define ufshcd_clear_eh_in_progress(h) \
151 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
152
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300153#define ufshcd_set_ufs_dev_active(h) \
154 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
155#define ufshcd_set_ufs_dev_sleep(h) \
156 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
157#define ufshcd_set_ufs_dev_poweroff(h) \
158 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
159#define ufshcd_is_ufs_dev_active(h) \
160 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
161#define ufshcd_is_ufs_dev_sleep(h) \
162 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
163#define ufshcd_is_ufs_dev_poweroff(h) \
164 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
165
166static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
167 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
168 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
169 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
170 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
171 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
172 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
173};
174
175static inline enum ufs_dev_pwr_mode
176ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
177{
178 return ufs_pm_lvl_states[lvl].dev_state;
179}
180
181static inline enum uic_link_state
182ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
183{
184 return ufs_pm_lvl_states[lvl].link_state;
185}
186
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530187static void ufshcd_tmc_handler(struct ufs_hba *hba);
188static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530189static int ufshcd_reset_and_restore(struct ufs_hba *hba);
190static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300191static void ufshcd_hba_exit(struct ufs_hba *hba);
192static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300193static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
194 bool skip_ref_clk);
195static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
196static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
197static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300198static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300199static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
200static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300201static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
202 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300203static int ufshcd_change_power_mode(struct ufs_hba *hba,
204 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200205static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
206{
207 return tag >= 0 && tag < hba->nutrs;
208}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300209
210static inline int ufshcd_enable_irq(struct ufs_hba *hba)
211{
212 int ret = 0;
213
214 if (!hba->is_irq_enabled) {
215 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
216 hba);
217 if (ret)
218 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
219 __func__, ret);
220 hba->is_irq_enabled = true;
221 }
222
223 return ret;
224}
225
226static inline void ufshcd_disable_irq(struct ufs_hba *hba)
227{
228 if (hba->is_irq_enabled) {
229 free_irq(hba->irq, hba);
230 hba->is_irq_enabled = false;
231 }
232}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530233
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530234/*
235 * ufshcd_wait_for_register - wait for register value to change
236 * @hba - per-adapter interface
237 * @reg - mmio register offset
238 * @mask - mask to apply to read register value
239 * @val - wait condition
240 * @interval_us - polling interval in microsecs
241 * @timeout_ms - timeout in millisecs
242 *
243 * Returns -ETIMEDOUT on error, zero on success
244 */
245static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
246 u32 val, unsigned long interval_us, unsigned long timeout_ms)
247{
248 int err = 0;
249 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
250
251 /* ignore bits that we don't intend to wait on */
252 val = val & mask;
253
254 while ((ufshcd_readl(hba, reg) & mask) != val) {
255 /* wakeup within 50us of expiry */
256 usleep_range(interval_us, interval_us + 50);
257
258 if (time_after(jiffies, timeout)) {
259 if ((ufshcd_readl(hba, reg) & mask) != val)
260 err = -ETIMEDOUT;
261 break;
262 }
263 }
264
265 return err;
266}
267
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530268/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530269 * ufshcd_get_intr_mask - Get the interrupt bit mask
270 * @hba - Pointer to adapter instance
271 *
272 * Returns interrupt bit mask per version
273 */
274static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
275{
276 if (hba->ufs_version == UFSHCI_VERSION_10)
277 return INTERRUPT_MASK_ALL_VER_10;
278 else
279 return INTERRUPT_MASK_ALL_VER_11;
280}
281
282/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530283 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
284 * @hba - Pointer to adapter instance
285 *
286 * Returns UFSHCI version supported by the controller
287 */
288static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
289{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200290 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
291 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300292
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530293 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530294}
295
296/**
297 * ufshcd_is_device_present - Check if any device connected to
298 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300299 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530300 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530301 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530302 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300303static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530304{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300305 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
306 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530307}
308
309/**
310 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
311 * @lrb: pointer to local command reference block
312 *
313 * This function is used to get the OCS field from UTRD
314 * Returns the OCS field in the UTRD
315 */
316static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
317{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530318 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530319}
320
321/**
322 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
323 * @task_req_descp: pointer to utp_task_req_desc structure
324 *
325 * This function is used to get the OCS field from UTMRD
326 * Returns the OCS field in the UTMRD
327 */
328static inline int
329ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
330{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530331 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530332}
333
334/**
335 * ufshcd_get_tm_free_slot - get a free slot for task management request
336 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530337 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530338 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530339 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
340 * Returns 0 if free slot is not available, else return 1 with tag value
341 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530342 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530343static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530344{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530345 int tag;
346 bool ret = false;
347
348 if (!free_slot)
349 goto out;
350
351 do {
352 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
353 if (tag >= hba->nutmrs)
354 goto out;
355 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
356
357 *free_slot = tag;
358 ret = true;
359out:
360 return ret;
361}
362
363static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
364{
365 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530366}
367
368/**
369 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
370 * @hba: per adapter instance
371 * @pos: position of the bit to be cleared
372 */
373static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
374{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530375 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530376}
377
378/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200379 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
380 * @hba: per adapter instance
381 * @tag: position of the bit to be cleared
382 */
383static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
384{
385 __clear_bit(tag, &hba->outstanding_reqs);
386}
387
388/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530389 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
390 * @reg: Register value of host controller status
391 *
392 * Returns integer, 0 on Success and positive value if failed
393 */
394static inline int ufshcd_get_lists_status(u32 reg)
395{
396 /*
397 * The mask 0xFF is for the following HCS register bits
398 * Bit Description
399 * 0 Device Present
400 * 1 UTRLRDY
401 * 2 UTMRLRDY
402 * 3 UCRDY
403 * 4 HEI
404 * 5 DEI
405 * 6-7 reserved
406 */
407 return (((reg) & (0xFF)) >> 1) ^ (0x07);
408}
409
410/**
411 * ufshcd_get_uic_cmd_result - Get the UIC command result
412 * @hba: Pointer to adapter instance
413 *
414 * This function gets the result of UIC command completion
415 * Returns 0 on success, non zero value on error
416 */
417static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
418{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530419 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530420 MASK_UIC_COMMAND_RESULT;
421}
422
423/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530424 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
425 * @hba: Pointer to adapter instance
426 *
427 * This function gets UIC command argument3
428 * Returns 0 on success, non zero value on error
429 */
430static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
431{
432 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
433}
434
435/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530436 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530437 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530438 */
439static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530440ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530441{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530442 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530443}
444
445/**
446 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
447 * @ucd_rsp_ptr: pointer to response UPIU
448 *
449 * This function gets the response status and scsi_status from response UPIU
450 * Returns the response result code.
451 */
452static inline int
453ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
454{
455 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
456}
457
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530458/*
459 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
460 * from response UPIU
461 * @ucd_rsp_ptr: pointer to response UPIU
462 *
463 * Return the data segment length.
464 */
465static inline unsigned int
466ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
467{
468 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
469 MASK_RSP_UPIU_DATA_SEG_LEN;
470}
471
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530472/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530473 * ufshcd_is_exception_event - Check if the device raised an exception event
474 * @ucd_rsp_ptr: pointer to response UPIU
475 *
476 * The function checks if the device raised an exception event indicated in
477 * the Device Information field of response UPIU.
478 *
479 * Returns true if exception is raised, false otherwise.
480 */
481static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
482{
483 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
484 MASK_RSP_EXCEPTION_EVENT ? true : false;
485}
486
487/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530488 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530489 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530490 */
491static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530492ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530493{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530494 ufshcd_writel(hba, INT_AGGR_ENABLE |
495 INT_AGGR_COUNTER_AND_TIMER_RESET,
496 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
497}
498
499/**
500 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
501 * @hba: per adapter instance
502 * @cnt: Interrupt aggregation counter threshold
503 * @tmout: Interrupt aggregation timeout value
504 */
505static inline void
506ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
507{
508 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
509 INT_AGGR_COUNTER_THLD_VAL(cnt) |
510 INT_AGGR_TIMEOUT_VAL(tmout),
511 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530512}
513
514/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300515 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
516 * @hba: per adapter instance
517 */
518static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
519{
520 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
521}
522
523/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530524 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
525 * When run-stop registers are set to 1, it indicates the
526 * host controller that it can process the requests
527 * @hba: per adapter instance
528 */
529static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
530{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530531 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
532 REG_UTP_TASK_REQ_LIST_RUN_STOP);
533 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
534 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530535}
536
537/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530538 * ufshcd_hba_start - Start controller initialization sequence
539 * @hba: per adapter instance
540 */
541static inline void ufshcd_hba_start(struct ufs_hba *hba)
542{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530543 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530544}
545
546/**
547 * ufshcd_is_hba_active - Get controller state
548 * @hba: per adapter instance
549 *
550 * Returns zero if controller is active, 1 otherwise
551 */
552static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
553{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530554 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530555}
556
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300557static void ufshcd_ungate_work(struct work_struct *work)
558{
559 int ret;
560 unsigned long flags;
561 struct ufs_hba *hba = container_of(work, struct ufs_hba,
562 clk_gating.ungate_work);
563
564 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
565
566 spin_lock_irqsave(hba->host->host_lock, flags);
567 if (hba->clk_gating.state == CLKS_ON) {
568 spin_unlock_irqrestore(hba->host->host_lock, flags);
569 goto unblock_reqs;
570 }
571
572 spin_unlock_irqrestore(hba->host->host_lock, flags);
573 ufshcd_setup_clocks(hba, true);
574
575 /* Exit from hibern8 */
576 if (ufshcd_can_hibern8_during_gating(hba)) {
577 /* Prevent gating in this path */
578 hba->clk_gating.is_suspended = true;
579 if (ufshcd_is_link_hibern8(hba)) {
580 ret = ufshcd_uic_hibern8_exit(hba);
581 if (ret)
582 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
583 __func__, ret);
584 else
585 ufshcd_set_link_active(hba);
586 }
587 hba->clk_gating.is_suspended = false;
588 }
589unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300590 if (ufshcd_is_clkscaling_enabled(hba))
591 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300592 scsi_unblock_requests(hba->host);
593}
594
595/**
596 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
597 * Also, exit from hibern8 mode and set the link as active.
598 * @hba: per adapter instance
599 * @async: This indicates whether caller should ungate clocks asynchronously.
600 */
601int ufshcd_hold(struct ufs_hba *hba, bool async)
602{
603 int rc = 0;
604 unsigned long flags;
605
606 if (!ufshcd_is_clkgating_allowed(hba))
607 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300608 spin_lock_irqsave(hba->host->host_lock, flags);
609 hba->clk_gating.active_reqs++;
610
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200611 if (ufshcd_eh_in_progress(hba)) {
612 spin_unlock_irqrestore(hba->host->host_lock, flags);
613 return 0;
614 }
615
Sahitya Tummala856b3482014-09-25 15:32:34 +0300616start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300617 switch (hba->clk_gating.state) {
618 case CLKS_ON:
619 break;
620 case REQ_CLKS_OFF:
621 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
622 hba->clk_gating.state = CLKS_ON;
623 break;
624 }
625 /*
626 * If we here, it means gating work is either done or
627 * currently running. Hence, fall through to cancel gating
628 * work and to enable clocks.
629 */
630 case CLKS_OFF:
631 scsi_block_requests(hba->host);
632 hba->clk_gating.state = REQ_CLKS_ON;
633 schedule_work(&hba->clk_gating.ungate_work);
634 /*
635 * fall through to check if we should wait for this
636 * work to be done or not.
637 */
638 case REQ_CLKS_ON:
639 if (async) {
640 rc = -EAGAIN;
641 hba->clk_gating.active_reqs--;
642 break;
643 }
644
645 spin_unlock_irqrestore(hba->host->host_lock, flags);
646 flush_work(&hba->clk_gating.ungate_work);
647 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300648 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300649 goto start;
650 default:
651 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
652 __func__, hba->clk_gating.state);
653 break;
654 }
655 spin_unlock_irqrestore(hba->host->host_lock, flags);
656out:
657 return rc;
658}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200659EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300660
661static void ufshcd_gate_work(struct work_struct *work)
662{
663 struct ufs_hba *hba = container_of(work, struct ufs_hba,
664 clk_gating.gate_work.work);
665 unsigned long flags;
666
667 spin_lock_irqsave(hba->host->host_lock, flags);
668 if (hba->clk_gating.is_suspended) {
669 hba->clk_gating.state = CLKS_ON;
670 goto rel_lock;
671 }
672
673 if (hba->clk_gating.active_reqs
674 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
675 || hba->lrb_in_use || hba->outstanding_tasks
676 || hba->active_uic_cmd || hba->uic_async_done)
677 goto rel_lock;
678
679 spin_unlock_irqrestore(hba->host->host_lock, flags);
680
681 /* put the link into hibern8 mode before turning off clocks */
682 if (ufshcd_can_hibern8_during_gating(hba)) {
683 if (ufshcd_uic_hibern8_enter(hba)) {
684 hba->clk_gating.state = CLKS_ON;
685 goto out;
686 }
687 ufshcd_set_link_hibern8(hba);
688 }
689
Sahitya Tummala856b3482014-09-25 15:32:34 +0300690 if (ufshcd_is_clkscaling_enabled(hba)) {
691 devfreq_suspend_device(hba->devfreq);
692 hba->clk_scaling.window_start_t = 0;
693 }
694
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300695 if (!ufshcd_is_link_active(hba))
696 ufshcd_setup_clocks(hba, false);
697 else
698 /* If link is active, device ref_clk can't be switched off */
699 __ufshcd_setup_clocks(hba, false, true);
700
701 /*
702 * In case you are here to cancel this work the gating state
703 * would be marked as REQ_CLKS_ON. In this case keep the state
704 * as REQ_CLKS_ON which would anyway imply that clocks are off
705 * and a request to turn them on is pending. By doing this way,
706 * we keep the state machine in tact and this would ultimately
707 * prevent from doing cancel work multiple times when there are
708 * new requests arriving before the current cancel work is done.
709 */
710 spin_lock_irqsave(hba->host->host_lock, flags);
711 if (hba->clk_gating.state == REQ_CLKS_OFF)
712 hba->clk_gating.state = CLKS_OFF;
713
714rel_lock:
715 spin_unlock_irqrestore(hba->host->host_lock, flags);
716out:
717 return;
718}
719
720/* host lock must be held before calling this variant */
721static void __ufshcd_release(struct ufs_hba *hba)
722{
723 if (!ufshcd_is_clkgating_allowed(hba))
724 return;
725
726 hba->clk_gating.active_reqs--;
727
728 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
729 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
730 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200731 || hba->active_uic_cmd || hba->uic_async_done
732 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300733 return;
734
735 hba->clk_gating.state = REQ_CLKS_OFF;
736 schedule_delayed_work(&hba->clk_gating.gate_work,
737 msecs_to_jiffies(hba->clk_gating.delay_ms));
738}
739
740void ufshcd_release(struct ufs_hba *hba)
741{
742 unsigned long flags;
743
744 spin_lock_irqsave(hba->host->host_lock, flags);
745 __ufshcd_release(hba);
746 spin_unlock_irqrestore(hba->host->host_lock, flags);
747}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200748EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300749
750static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
751 struct device_attribute *attr, char *buf)
752{
753 struct ufs_hba *hba = dev_get_drvdata(dev);
754
755 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
756}
757
758static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
759 struct device_attribute *attr, const char *buf, size_t count)
760{
761 struct ufs_hba *hba = dev_get_drvdata(dev);
762 unsigned long flags, value;
763
764 if (kstrtoul(buf, 0, &value))
765 return -EINVAL;
766
767 spin_lock_irqsave(hba->host->host_lock, flags);
768 hba->clk_gating.delay_ms = value;
769 spin_unlock_irqrestore(hba->host->host_lock, flags);
770 return count;
771}
772
773static void ufshcd_init_clk_gating(struct ufs_hba *hba)
774{
775 if (!ufshcd_is_clkgating_allowed(hba))
776 return;
777
778 hba->clk_gating.delay_ms = 150;
779 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
780 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
781
782 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
783 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
784 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
785 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
786 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
787 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
788 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
789}
790
791static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
792{
793 if (!ufshcd_is_clkgating_allowed(hba))
794 return;
795 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900796 cancel_work_sync(&hba->clk_gating.ungate_work);
797 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300798}
799
Sahitya Tummala856b3482014-09-25 15:32:34 +0300800/* Must be called with host lock acquired */
801static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
802{
803 if (!ufshcd_is_clkscaling_enabled(hba))
804 return;
805
806 if (!hba->clk_scaling.is_busy_started) {
807 hba->clk_scaling.busy_start_t = ktime_get();
808 hba->clk_scaling.is_busy_started = true;
809 }
810}
811
812static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
813{
814 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
815
816 if (!ufshcd_is_clkscaling_enabled(hba))
817 return;
818
819 if (!hba->outstanding_reqs && scaling->is_busy_started) {
820 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
821 scaling->busy_start_t));
822 scaling->busy_start_t = ktime_set(0, 0);
823 scaling->is_busy_started = false;
824 }
825}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530826/**
827 * ufshcd_send_command - Send SCSI or device management commands
828 * @hba: per adapter instance
829 * @task_tag: Task tag of the command
830 */
831static inline
832void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
833{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300834 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530835 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530836 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530837}
838
839/**
840 * ufshcd_copy_sense_data - Copy sense data in case of check condition
841 * @lrb - pointer to local reference block
842 */
843static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
844{
845 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530846 if (lrbp->sense_buffer &&
847 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530848 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530849 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530850 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530851 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
852 }
853}
854
855/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530856 * ufshcd_copy_query_response() - Copy the Query Response and the data
857 * descriptor
858 * @hba: per adapter instance
859 * @lrb - pointer to local reference block
860 */
861static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300862int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530863{
864 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
865
Dolev Raviv68078d52013-07-30 00:35:58 +0530866 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530867
Dolev Raviv68078d52013-07-30 00:35:58 +0530868 /* Get the descriptor */
869 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300870 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530871 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300872 u16 resp_len;
873 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530874
875 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300876 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530877 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300878 buf_len = be16_to_cpu(
879 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300880 if (likely(buf_len >= resp_len)) {
881 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
882 } else {
883 dev_warn(hba->dev,
884 "%s: Response size is bigger than buffer",
885 __func__);
886 return -EINVAL;
887 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530888 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300889
890 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530891}
892
893/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530894 * ufshcd_hba_capabilities - Read controller capabilities
895 * @hba: per adapter instance
896 */
897static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
898{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530899 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530900
901 /* nutrs and nutmrs are 0 based values */
902 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
903 hba->nutmrs =
904 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
905}
906
907/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530908 * ufshcd_ready_for_uic_cmd - Check if controller is ready
909 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530910 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530911 * Return true on success, else false
912 */
913static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
914{
915 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
916 return true;
917 else
918 return false;
919}
920
921/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530922 * ufshcd_get_upmcrs - Get the power mode change request status
923 * @hba: Pointer to adapter instance
924 *
925 * This function gets the UPMCRS field of HCS register
926 * Returns value of UPMCRS field
927 */
928static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
929{
930 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
931}
932
933/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530934 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
935 * @hba: per adapter instance
936 * @uic_cmd: UIC command
937 *
938 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530939 */
940static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530941ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530942{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530943 WARN_ON(hba->active_uic_cmd);
944
945 hba->active_uic_cmd = uic_cmd;
946
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530947 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530948 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
949 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
950 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530951
952 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530953 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530954 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530955}
956
957/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530958 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
959 * @hba: per adapter instance
960 * @uic_command: UIC command
961 *
962 * Must be called with mutex held.
963 * Returns 0 only if success.
964 */
965static int
966ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
967{
968 int ret;
969 unsigned long flags;
970
971 if (wait_for_completion_timeout(&uic_cmd->done,
972 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
973 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
974 else
975 ret = -ETIMEDOUT;
976
977 spin_lock_irqsave(hba->host->host_lock, flags);
978 hba->active_uic_cmd = NULL;
979 spin_unlock_irqrestore(hba->host->host_lock, flags);
980
981 return ret;
982}
983
984/**
985 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
986 * @hba: per adapter instance
987 * @uic_cmd: UIC command
988 *
989 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300990 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530991 * Returns 0 only if success.
992 */
993static int
994__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
995{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530996 if (!ufshcd_ready_for_uic_cmd(hba)) {
997 dev_err(hba->dev,
998 "Controller not ready to accept UIC commands\n");
999 return -EIO;
1000 }
1001
1002 init_completion(&uic_cmd->done);
1003
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301004 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301005
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001006 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301007}
1008
1009/**
1010 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1011 * @hba: per adapter instance
1012 * @uic_cmd: UIC command
1013 *
1014 * Returns 0 only if success.
1015 */
1016static int
1017ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1018{
1019 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001020 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301021
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001022 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301023 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001024 ufshcd_add_delay_before_dme_cmd(hba);
1025
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001026 spin_lock_irqsave(hba->host->host_lock, flags);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301027 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001028 spin_unlock_irqrestore(hba->host->host_lock, flags);
1029 if (!ret)
1030 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1031
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301032 mutex_unlock(&hba->uic_cmd_mutex);
1033
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001034 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301035 return ret;
1036}
1037
1038/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301039 * ufshcd_map_sg - Map scatter-gather list to prdt
1040 * @lrbp - pointer to local reference block
1041 *
1042 * Returns 0 in case of success, non-zero value in case of failure
1043 */
1044static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1045{
1046 struct ufshcd_sg_entry *prd_table;
1047 struct scatterlist *sg;
1048 struct scsi_cmnd *cmd;
1049 int sg_segments;
1050 int i;
1051
1052 cmd = lrbp->cmd;
1053 sg_segments = scsi_dma_map(cmd);
1054 if (sg_segments < 0)
1055 return sg_segments;
1056
1057 if (sg_segments) {
1058 lrbp->utr_descriptor_ptr->prd_table_length =
1059 cpu_to_le16((u16) (sg_segments));
1060
1061 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1062
1063 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1064 prd_table[i].size =
1065 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1066 prd_table[i].base_addr =
1067 cpu_to_le32(lower_32_bits(sg->dma_address));
1068 prd_table[i].upper_addr =
1069 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001070 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301071 }
1072 } else {
1073 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1074 }
1075
1076 return 0;
1077}
1078
1079/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301080 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301081 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301082 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301083 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301084static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301085{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301086 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1087
1088 if (hba->ufs_version == UFSHCI_VERSION_10) {
1089 u32 rw;
1090 rw = set & INTERRUPT_MASK_RW_VER_10;
1091 set = rw | ((set ^ intrs) & intrs);
1092 } else {
1093 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301094 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301095
1096 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1097}
1098
1099/**
1100 * ufshcd_disable_intr - disable interrupts
1101 * @hba: per adapter instance
1102 * @intrs: interrupt bits
1103 */
1104static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1105{
1106 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1107
1108 if (hba->ufs_version == UFSHCI_VERSION_10) {
1109 u32 rw;
1110 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1111 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1112 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1113
1114 } else {
1115 set &= ~intrs;
1116 }
1117
1118 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301119}
1120
1121/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301122 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1123 * descriptor according to request
1124 * @lrbp: pointer to local reference block
1125 * @upiu_flags: flags required in the header
1126 * @cmd_dir: requests data direction
1127 */
1128static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
1129 u32 *upiu_flags, enum dma_data_direction cmd_dir)
1130{
1131 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1132 u32 data_direction;
1133 u32 dword_0;
1134
1135 if (cmd_dir == DMA_FROM_DEVICE) {
1136 data_direction = UTP_DEVICE_TO_HOST;
1137 *upiu_flags = UPIU_CMD_FLAGS_READ;
1138 } else if (cmd_dir == DMA_TO_DEVICE) {
1139 data_direction = UTP_HOST_TO_DEVICE;
1140 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1141 } else {
1142 data_direction = UTP_NO_DATA_TRANSFER;
1143 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1144 }
1145
1146 dword_0 = data_direction | (lrbp->command_type
1147 << UPIU_COMMAND_TYPE_OFFSET);
1148 if (lrbp->intr_cmd)
1149 dword_0 |= UTP_REQ_DESC_INT_CMD;
1150
1151 /* Transfer request descriptor header fields */
1152 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001153 /* dword_1 is reserved, hence it is set to 0 */
1154 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301155 /*
1156 * assigning invalid value for command status. Controller
1157 * updates OCS on command completion, with the command
1158 * status
1159 */
1160 req_desc->header.dword_2 =
1161 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001162 /* dword_3 is reserved, hence it is set to 0 */
1163 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001164
1165 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301166}
1167
1168/**
1169 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1170 * for scsi commands
1171 * @lrbp - local reference block pointer
1172 * @upiu_flags - flags
1173 */
1174static
1175void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1176{
1177 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001178 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301179
1180 /* command descriptor fields */
1181 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1182 UPIU_TRANSACTION_COMMAND, upiu_flags,
1183 lrbp->lun, lrbp->task_tag);
1184 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1185 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1186
1187 /* Total EHS length and Data segment length will be zero */
1188 ucd_req_ptr->header.dword_2 = 0;
1189
1190 ucd_req_ptr->sc.exp_data_transfer_len =
1191 cpu_to_be32(lrbp->cmd->sdb.length);
1192
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001193 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1194 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1195 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1196
1197 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301198}
1199
Dolev Raviv68078d52013-07-30 00:35:58 +05301200/**
1201 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1202 * for query requsts
1203 * @hba: UFS hba
1204 * @lrbp: local reference block pointer
1205 * @upiu_flags: flags
1206 */
1207static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1208 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1209{
1210 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1211 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301212 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301213 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1214
1215 /* Query request header */
1216 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1217 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1218 lrbp->lun, lrbp->task_tag);
1219 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1220 0, query->request.query_func, 0, 0);
1221
1222 /* Data segment length */
1223 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
1224 0, 0, len >> 8, (u8)len);
1225
1226 /* Copy the Query Request buffer as is */
1227 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1228 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301229
1230 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001231 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1232 memcpy(descp, query->descriptor, len);
1233
Yaniv Gardi51047262016-02-01 15:02:38 +02001234 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301235}
1236
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301237static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1238{
1239 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1240
1241 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1242
1243 /* command descriptor fields */
1244 ucd_req_ptr->header.dword_0 =
1245 UPIU_HEADER_DWORD(
1246 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001247 /* clear rest of the fields of basic header */
1248 ucd_req_ptr->header.dword_1 = 0;
1249 ucd_req_ptr->header.dword_2 = 0;
1250
1251 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301252}
1253
1254/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301255 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301256 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301257 * @lrb - pointer to local reference block
1258 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301259static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301260{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301261 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301262 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301263
1264 switch (lrbp->command_type) {
1265 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301266 if (likely(lrbp->cmd)) {
1267 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1268 lrbp->cmd->sc_data_direction);
1269 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301270 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301271 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301272 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301273 break;
1274 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301275 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301276 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1277 ufshcd_prepare_utp_query_req_upiu(
1278 hba, lrbp, upiu_flags);
1279 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301280 ufshcd_prepare_utp_nop_upiu(lrbp);
1281 else
1282 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301283 break;
1284 case UTP_CMD_TYPE_UFS:
1285 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301286 ret = -ENOTSUPP;
1287 dev_err(hba->dev, "%s: UFS native command are not supported\n",
1288 __func__);
1289 break;
1290 default:
1291 ret = -ENOTSUPP;
1292 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
1293 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301294 break;
1295 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301296
1297 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301298}
1299
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001300/*
1301 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1302 * @scsi_lun: scsi LUN id
1303 *
1304 * Returns UPIU LUN id
1305 */
1306static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1307{
1308 if (scsi_is_wlun(scsi_lun))
1309 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1310 | UFS_UPIU_WLUN_ID;
1311 else
1312 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1313}
1314
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301315/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001316 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1317 * @scsi_lun: UPIU W-LUN id
1318 *
1319 * Returns SCSI W-LUN id
1320 */
1321static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1322{
1323 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1324}
1325
1326/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301327 * ufshcd_queuecommand - main entry point for SCSI requests
1328 * @cmd: command from SCSI Midlayer
1329 * @done: call back function
1330 *
1331 * Returns 0 for success, non-zero in case of failure
1332 */
1333static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1334{
1335 struct ufshcd_lrb *lrbp;
1336 struct ufs_hba *hba;
1337 unsigned long flags;
1338 int tag;
1339 int err = 0;
1340
1341 hba = shost_priv(host);
1342
1343 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001344 if (!ufshcd_valid_tag(hba, tag)) {
1345 dev_err(hba->dev,
1346 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1347 __func__, tag, cmd, cmd->request);
1348 BUG();
1349 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301350
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301351 spin_lock_irqsave(hba->host->host_lock, flags);
1352 switch (hba->ufshcd_state) {
1353 case UFSHCD_STATE_OPERATIONAL:
1354 break;
1355 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301356 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301357 goto out_unlock;
1358 case UFSHCD_STATE_ERROR:
1359 set_host_byte(cmd, DID_ERROR);
1360 cmd->scsi_done(cmd);
1361 goto out_unlock;
1362 default:
1363 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1364 __func__, hba->ufshcd_state);
1365 set_host_byte(cmd, DID_BAD_TARGET);
1366 cmd->scsi_done(cmd);
1367 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301368 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001369
1370 /* if error handling is in progress, don't issue commands */
1371 if (ufshcd_eh_in_progress(hba)) {
1372 set_host_byte(cmd, DID_ERROR);
1373 cmd->scsi_done(cmd);
1374 goto out_unlock;
1375 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301376 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301377
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301378 /* acquire the tag to make sure device cmds don't use it */
1379 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1380 /*
1381 * Dev manage command in progress, requeue the command.
1382 * Requeuing the command helps in cases where the request *may*
1383 * find different tag instead of waiting for dev manage command
1384 * completion.
1385 */
1386 err = SCSI_MLQUEUE_HOST_BUSY;
1387 goto out;
1388 }
1389
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001390 err = ufshcd_hold(hba, true);
1391 if (err) {
1392 err = SCSI_MLQUEUE_HOST_BUSY;
1393 clear_bit_unlock(tag, &hba->lrb_in_use);
1394 goto out;
1395 }
1396 WARN_ON(hba->clk_gating.state != CLKS_ON);
1397
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301398 lrbp = &hba->lrb[tag];
1399
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301400 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301401 lrbp->cmd = cmd;
1402 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1403 lrbp->sense_buffer = cmd->sense_buffer;
1404 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001405 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001406 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301407 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1408
1409 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301410 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301411 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301412 if (err) {
1413 lrbp->cmd = NULL;
1414 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301415 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301416 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301417
1418 /* issue command to the controller */
1419 spin_lock_irqsave(hba->host->host_lock, flags);
1420 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301421out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301422 spin_unlock_irqrestore(hba->host->host_lock, flags);
1423out:
1424 return err;
1425}
1426
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301427static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1428 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1429{
1430 lrbp->cmd = NULL;
1431 lrbp->sense_bufflen = 0;
1432 lrbp->sense_buffer = NULL;
1433 lrbp->task_tag = tag;
1434 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
1435 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1436 lrbp->intr_cmd = true; /* No interrupt aggregation */
1437 hba->dev_cmd.type = cmd_type;
1438
1439 return ufshcd_compose_upiu(hba, lrbp);
1440}
1441
1442static int
1443ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1444{
1445 int err = 0;
1446 unsigned long flags;
1447 u32 mask = 1 << tag;
1448
1449 /* clear outstanding transaction before retry */
1450 spin_lock_irqsave(hba->host->host_lock, flags);
1451 ufshcd_utrl_clear(hba, tag);
1452 spin_unlock_irqrestore(hba->host->host_lock, flags);
1453
1454 /*
1455 * wait for for h/w to clear corresponding bit in door-bell.
1456 * max. wait is 1 sec.
1457 */
1458 err = ufshcd_wait_for_register(hba,
1459 REG_UTP_TRANSFER_REQ_DOOR_BELL,
1460 mask, ~mask, 1000, 1000);
1461
1462 return err;
1463}
1464
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001465static int
1466ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1467{
1468 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1469
1470 /* Get the UPIU response */
1471 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1472 UPIU_RSP_CODE_OFFSET;
1473 return query_res->response;
1474}
1475
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301476/**
1477 * ufshcd_dev_cmd_completion() - handles device management command responses
1478 * @hba: per adapter instance
1479 * @lrbp: pointer to local reference block
1480 */
1481static int
1482ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1483{
1484 int resp;
1485 int err = 0;
1486
1487 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1488
1489 switch (resp) {
1490 case UPIU_TRANSACTION_NOP_IN:
1491 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1492 err = -EINVAL;
1493 dev_err(hba->dev, "%s: unexpected response %x\n",
1494 __func__, resp);
1495 }
1496 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301497 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001498 err = ufshcd_check_query_response(hba, lrbp);
1499 if (!err)
1500 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301501 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301502 case UPIU_TRANSACTION_REJECT_UPIU:
1503 /* TODO: handle Reject UPIU Response */
1504 err = -EPERM;
1505 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1506 __func__);
1507 break;
1508 default:
1509 err = -EINVAL;
1510 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1511 __func__, resp);
1512 break;
1513 }
1514
1515 return err;
1516}
1517
1518static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1519 struct ufshcd_lrb *lrbp, int max_timeout)
1520{
1521 int err = 0;
1522 unsigned long time_left;
1523 unsigned long flags;
1524
1525 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1526 msecs_to_jiffies(max_timeout));
1527
1528 spin_lock_irqsave(hba->host->host_lock, flags);
1529 hba->dev_cmd.complete = NULL;
1530 if (likely(time_left)) {
1531 err = ufshcd_get_tr_ocs(lrbp);
1532 if (!err)
1533 err = ufshcd_dev_cmd_completion(hba, lrbp);
1534 }
1535 spin_unlock_irqrestore(hba->host->host_lock, flags);
1536
1537 if (!time_left) {
1538 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001539 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1540 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301541 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001542 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301543 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001544 /*
1545 * in case of an error, after clearing the doorbell,
1546 * we also need to clear the outstanding_request
1547 * field in hba
1548 */
1549 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301550 }
1551
1552 return err;
1553}
1554
1555/**
1556 * ufshcd_get_dev_cmd_tag - Get device management command tag
1557 * @hba: per-adapter instance
1558 * @tag: pointer to variable with available slot value
1559 *
1560 * Get a free slot and lock it until device management command
1561 * completes.
1562 *
1563 * Returns false if free slot is unavailable for locking, else
1564 * return true with tag value in @tag.
1565 */
1566static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1567{
1568 int tag;
1569 bool ret = false;
1570 unsigned long tmp;
1571
1572 if (!tag_out)
1573 goto out;
1574
1575 do {
1576 tmp = ~hba->lrb_in_use;
1577 tag = find_last_bit(&tmp, hba->nutrs);
1578 if (tag >= hba->nutrs)
1579 goto out;
1580 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1581
1582 *tag_out = tag;
1583 ret = true;
1584out:
1585 return ret;
1586}
1587
1588static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1589{
1590 clear_bit_unlock(tag, &hba->lrb_in_use);
1591}
1592
1593/**
1594 * ufshcd_exec_dev_cmd - API for sending device management requests
1595 * @hba - UFS hba
1596 * @cmd_type - specifies the type (NOP, Query...)
1597 * @timeout - time in seconds
1598 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301599 * NOTE: Since there is only one available tag for device management commands,
1600 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301601 */
1602static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1603 enum dev_cmd_type cmd_type, int timeout)
1604{
1605 struct ufshcd_lrb *lrbp;
1606 int err;
1607 int tag;
1608 struct completion wait;
1609 unsigned long flags;
1610
1611 /*
1612 * Get free slot, sleep if slots are unavailable.
1613 * Even though we use wait_event() which sleeps indefinitely,
1614 * the maximum wait time is bounded by SCSI request timeout.
1615 */
1616 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1617
1618 init_completion(&wait);
1619 lrbp = &hba->lrb[tag];
1620 WARN_ON(lrbp->cmd);
1621 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1622 if (unlikely(err))
1623 goto out_put_tag;
1624
1625 hba->dev_cmd.complete = &wait;
1626
1627 spin_lock_irqsave(hba->host->host_lock, flags);
1628 ufshcd_send_command(hba, tag);
1629 spin_unlock_irqrestore(hba->host->host_lock, flags);
1630
1631 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1632
1633out_put_tag:
1634 ufshcd_put_dev_cmd_tag(hba, tag);
1635 wake_up(&hba->dev_cmd.tag_wq);
1636 return err;
1637}
1638
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301639/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001640 * ufshcd_init_query() - init the query response and request parameters
1641 * @hba: per-adapter instance
1642 * @request: address of the request pointer to be initialized
1643 * @response: address of the response pointer to be initialized
1644 * @opcode: operation to perform
1645 * @idn: flag idn to access
1646 * @index: LU number to access
1647 * @selector: query/flag/descriptor further identification
1648 */
1649static inline void ufshcd_init_query(struct ufs_hba *hba,
1650 struct ufs_query_req **request, struct ufs_query_res **response,
1651 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1652{
1653 *request = &hba->dev_cmd.query.request;
1654 *response = &hba->dev_cmd.query.response;
1655 memset(*request, 0, sizeof(struct ufs_query_req));
1656 memset(*response, 0, sizeof(struct ufs_query_res));
1657 (*request)->upiu_req.opcode = opcode;
1658 (*request)->upiu_req.idn = idn;
1659 (*request)->upiu_req.index = index;
1660 (*request)->upiu_req.selector = selector;
1661}
1662
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001663static int ufshcd_query_flag_retry(struct ufs_hba *hba,
1664 enum query_opcode opcode, enum flag_idn idn, bool *flag_res)
1665{
1666 int ret;
1667 int retries;
1668
1669 for (retries = 0; retries < QUERY_REQ_RETRIES; retries++) {
1670 ret = ufshcd_query_flag(hba, opcode, idn, flag_res);
1671 if (ret)
1672 dev_dbg(hba->dev,
1673 "%s: failed with error %d, retries %d\n",
1674 __func__, ret, retries);
1675 else
1676 break;
1677 }
1678
1679 if (ret)
1680 dev_err(hba->dev,
1681 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
1682 __func__, opcode, idn, ret, retries);
1683 return ret;
1684}
1685
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001686/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301687 * ufshcd_query_flag() - API function for sending flag query requests
1688 * hba: per-adapter instance
1689 * query_opcode: flag query to perform
1690 * idn: flag idn to access
1691 * flag_res: the flag value after the query request completes
1692 *
1693 * Returns 0 for success, non-zero in case of failure
1694 */
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02001695int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
Dolev Raviv68078d52013-07-30 00:35:58 +05301696 enum flag_idn idn, bool *flag_res)
1697{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001698 struct ufs_query_req *request = NULL;
1699 struct ufs_query_res *response = NULL;
1700 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001701 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301702
1703 BUG_ON(!hba);
1704
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001705 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301706 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001707 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1708 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301709
1710 switch (opcode) {
1711 case UPIU_QUERY_OPCODE_SET_FLAG:
1712 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1713 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1714 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1715 break;
1716 case UPIU_QUERY_OPCODE_READ_FLAG:
1717 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1718 if (!flag_res) {
1719 /* No dummy reads */
1720 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1721 __func__);
1722 err = -EINVAL;
1723 goto out_unlock;
1724 }
1725 break;
1726 default:
1727 dev_err(hba->dev,
1728 "%s: Expected query flag opcode but got = %d\n",
1729 __func__, opcode);
1730 err = -EINVAL;
1731 goto out_unlock;
1732 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301733
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001734 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1735 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1736
1737 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301738
1739 if (err) {
1740 dev_err(hba->dev,
1741 "%s: Sending flag query for idn %d failed, err = %d\n",
1742 __func__, idn, err);
1743 goto out_unlock;
1744 }
1745
1746 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301747 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301748 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1749
1750out_unlock:
1751 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001752 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301753 return err;
1754}
1755
1756/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301757 * ufshcd_query_attr - API function for sending attribute requests
1758 * hba: per-adapter instance
1759 * opcode: attribute opcode
1760 * idn: attribute idn to access
1761 * index: index field
1762 * selector: selector field
1763 * attr_val: the attribute value after the query request completes
1764 *
1765 * Returns 0 for success, non-zero in case of failure
1766*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301767static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301768 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1769{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001770 struct ufs_query_req *request = NULL;
1771 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301772 int err;
1773
1774 BUG_ON(!hba);
1775
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001776 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301777 if (!attr_val) {
1778 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1779 __func__, opcode);
1780 err = -EINVAL;
1781 goto out;
1782 }
1783
1784 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001785 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1786 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301787
1788 switch (opcode) {
1789 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1790 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301791 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301792 break;
1793 case UPIU_QUERY_OPCODE_READ_ATTR:
1794 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1795 break;
1796 default:
1797 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1798 __func__, opcode);
1799 err = -EINVAL;
1800 goto out_unlock;
1801 }
1802
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001803 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301804
1805 if (err) {
1806 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1807 __func__, opcode, idn, err);
1808 goto out_unlock;
1809 }
1810
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301811 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301812
1813out_unlock:
1814 mutex_unlock(&hba->dev_cmd.lock);
1815out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001816 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301817 return err;
1818}
1819
1820/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001821 * ufshcd_query_descriptor - API function for sending descriptor requests
1822 * hba: per-adapter instance
1823 * opcode: attribute opcode
1824 * idn: attribute idn to access
1825 * index: index field
1826 * selector: selector field
1827 * desc_buf: the buffer that contains the descriptor
1828 * buf_len: length parameter passed to the device
1829 *
1830 * Returns 0 for success, non-zero in case of failure.
1831 * The buf_len parameter will contain, on return, the length parameter
1832 * received on the response.
1833 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001834static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001835 enum query_opcode opcode, enum desc_idn idn, u8 index,
1836 u8 selector, u8 *desc_buf, int *buf_len)
1837{
1838 struct ufs_query_req *request = NULL;
1839 struct ufs_query_res *response = NULL;
1840 int err;
1841
1842 BUG_ON(!hba);
1843
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001844 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001845 if (!desc_buf) {
1846 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1847 __func__, opcode);
1848 err = -EINVAL;
1849 goto out;
1850 }
1851
1852 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1853 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1854 __func__, *buf_len);
1855 err = -EINVAL;
1856 goto out;
1857 }
1858
1859 mutex_lock(&hba->dev_cmd.lock);
1860 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1861 selector);
1862 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001863 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001864
1865 switch (opcode) {
1866 case UPIU_QUERY_OPCODE_WRITE_DESC:
1867 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1868 break;
1869 case UPIU_QUERY_OPCODE_READ_DESC:
1870 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1871 break;
1872 default:
1873 dev_err(hba->dev,
1874 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1875 __func__, opcode);
1876 err = -EINVAL;
1877 goto out_unlock;
1878 }
1879
1880 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1881
1882 if (err) {
1883 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1884 __func__, opcode, idn, err);
1885 goto out_unlock;
1886 }
1887
1888 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001889 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001890
1891out_unlock:
1892 mutex_unlock(&hba->dev_cmd.lock);
1893out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001894 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001895 return err;
1896}
1897
1898/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001899 * ufshcd_read_desc_param - read the specified descriptor parameter
1900 * @hba: Pointer to adapter instance
1901 * @desc_id: descriptor idn value
1902 * @desc_index: descriptor index
1903 * @param_offset: offset of the parameter to read
1904 * @param_read_buf: pointer to buffer where parameter would be read
1905 * @param_size: sizeof(param_read_buf)
1906 *
1907 * Return 0 in case of success, non-zero otherwise
1908 */
1909static int ufshcd_read_desc_param(struct ufs_hba *hba,
1910 enum desc_idn desc_id,
1911 int desc_index,
1912 u32 param_offset,
1913 u8 *param_read_buf,
1914 u32 param_size)
1915{
1916 int ret;
1917 u8 *desc_buf;
1918 u32 buff_len;
1919 bool is_kmalloc = true;
1920
1921 /* safety checks */
1922 if (desc_id >= QUERY_DESC_IDN_MAX)
1923 return -EINVAL;
1924
1925 buff_len = ufs_query_desc_max_size[desc_id];
1926 if ((param_offset + param_size) > buff_len)
1927 return -EINVAL;
1928
1929 if (!param_offset && (param_size == buff_len)) {
1930 /* memory space already available to hold full descriptor */
1931 desc_buf = param_read_buf;
1932 is_kmalloc = false;
1933 } else {
1934 /* allocate memory to hold full descriptor */
1935 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1936 if (!desc_buf)
1937 return -ENOMEM;
1938 }
1939
1940 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1941 desc_id, desc_index, 0, desc_buf,
1942 &buff_len);
1943
1944 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
1945 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
1946 ufs_query_desc_max_size[desc_id])
1947 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
1948 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
1949 __func__, desc_id, param_offset, buff_len, ret);
1950 if (!ret)
1951 ret = -EINVAL;
1952
1953 goto out;
1954 }
1955
1956 if (is_kmalloc)
1957 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1958out:
1959 if (is_kmalloc)
1960 kfree(desc_buf);
1961 return ret;
1962}
1963
1964static inline int ufshcd_read_desc(struct ufs_hba *hba,
1965 enum desc_idn desc_id,
1966 int desc_index,
1967 u8 *buf,
1968 u32 size)
1969{
1970 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1971}
1972
1973static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
1974 u8 *buf,
1975 u32 size)
1976{
1977 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
1978}
1979
1980/**
1981 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
1982 * @hba: Pointer to adapter instance
1983 * @lun: lun id
1984 * @param_offset: offset of the parameter to read
1985 * @param_read_buf: pointer to buffer where parameter would be read
1986 * @param_size: sizeof(param_read_buf)
1987 *
1988 * Return 0 in case of success, non-zero otherwise
1989 */
1990static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
1991 int lun,
1992 enum unit_desc_param param_offset,
1993 u8 *param_read_buf,
1994 u32 param_size)
1995{
1996 /*
1997 * Unit descriptors are only available for general purpose LUs (LUN id
1998 * from 0 to 7) and RPMB Well known LU.
1999 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002000 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002001 return -EOPNOTSUPP;
2002
2003 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
2004 param_offset, param_read_buf, param_size);
2005}
2006
2007/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302008 * ufshcd_memory_alloc - allocate memory for host memory space data structures
2009 * @hba: per adapter instance
2010 *
2011 * 1. Allocate DMA memory for Command Descriptor array
2012 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
2013 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
2014 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
2015 * (UTMRDL)
2016 * 4. Allocate memory for local reference block(lrb).
2017 *
2018 * Returns 0 for success, non-zero in case of failure
2019 */
2020static int ufshcd_memory_alloc(struct ufs_hba *hba)
2021{
2022 size_t utmrdl_size, utrdl_size, ucdl_size;
2023
2024 /* Allocate memory for UTP command descriptors */
2025 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002026 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2027 ucdl_size,
2028 &hba->ucdl_dma_addr,
2029 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302030
2031 /*
2032 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2033 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2034 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2035 * be aligned to 128 bytes as well
2036 */
2037 if (!hba->ucdl_base_addr ||
2038 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302039 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302040 "Command Descriptor Memory allocation failed\n");
2041 goto out;
2042 }
2043
2044 /*
2045 * Allocate memory for UTP Transfer descriptors
2046 * UFSHCI requires 1024 byte alignment of UTRD
2047 */
2048 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002049 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2050 utrdl_size,
2051 &hba->utrdl_dma_addr,
2052 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302053 if (!hba->utrdl_base_addr ||
2054 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302055 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302056 "Transfer Descriptor Memory allocation failed\n");
2057 goto out;
2058 }
2059
2060 /*
2061 * Allocate memory for UTP Task Management descriptors
2062 * UFSHCI requires 1024 byte alignment of UTMRD
2063 */
2064 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002065 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2066 utmrdl_size,
2067 &hba->utmrdl_dma_addr,
2068 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302069 if (!hba->utmrdl_base_addr ||
2070 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302071 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302072 "Task Management Descriptor Memory allocation failed\n");
2073 goto out;
2074 }
2075
2076 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002077 hba->lrb = devm_kzalloc(hba->dev,
2078 hba->nutrs * sizeof(struct ufshcd_lrb),
2079 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302080 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302081 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302082 goto out;
2083 }
2084 return 0;
2085out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302086 return -ENOMEM;
2087}
2088
2089/**
2090 * ufshcd_host_memory_configure - configure local reference block with
2091 * memory offsets
2092 * @hba: per adapter instance
2093 *
2094 * Configure Host memory space
2095 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2096 * address.
2097 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2098 * and PRDT offset.
2099 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2100 * into local reference block.
2101 */
2102static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2103{
2104 struct utp_transfer_cmd_desc *cmd_descp;
2105 struct utp_transfer_req_desc *utrdlp;
2106 dma_addr_t cmd_desc_dma_addr;
2107 dma_addr_t cmd_desc_element_addr;
2108 u16 response_offset;
2109 u16 prdt_offset;
2110 int cmd_desc_size;
2111 int i;
2112
2113 utrdlp = hba->utrdl_base_addr;
2114 cmd_descp = hba->ucdl_base_addr;
2115
2116 response_offset =
2117 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2118 prdt_offset =
2119 offsetof(struct utp_transfer_cmd_desc, prd_table);
2120
2121 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2122 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2123
2124 for (i = 0; i < hba->nutrs; i++) {
2125 /* Configure UTRD with command descriptor base address */
2126 cmd_desc_element_addr =
2127 (cmd_desc_dma_addr + (cmd_desc_size * i));
2128 utrdlp[i].command_desc_base_addr_lo =
2129 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2130 utrdlp[i].command_desc_base_addr_hi =
2131 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2132
2133 /* Response upiu and prdt offset should be in double words */
2134 utrdlp[i].response_upiu_offset =
2135 cpu_to_le16((response_offset >> 2));
2136 utrdlp[i].prd_table_offset =
2137 cpu_to_le16((prdt_offset >> 2));
2138 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302139 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302140
2141 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302142 hba->lrb[i].ucd_req_ptr =
2143 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302144 hba->lrb[i].ucd_rsp_ptr =
2145 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2146 hba->lrb[i].ucd_prdt_ptr =
2147 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2148 }
2149}
2150
2151/**
2152 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2153 * @hba: per adapter instance
2154 *
2155 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2156 * in order to initialize the Unipro link startup procedure.
2157 * Once the Unipro links are up, the device connected to the controller
2158 * is detected.
2159 *
2160 * Returns 0 on success, non-zero value on failure
2161 */
2162static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2163{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302164 struct uic_command uic_cmd = {0};
2165 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302166
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302167 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2168
2169 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2170 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302171 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302172 "dme-link-startup: error code %d\n", ret);
2173 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302174}
2175
Yaniv Gardicad2e032015-03-31 17:37:14 +03002176static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2177{
2178 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2179 unsigned long min_sleep_time_us;
2180
2181 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2182 return;
2183
2184 /*
2185 * last_dme_cmd_tstamp will be 0 only for 1st call to
2186 * this function
2187 */
2188 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2189 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2190 } else {
2191 unsigned long delta =
2192 (unsigned long) ktime_to_us(
2193 ktime_sub(ktime_get(),
2194 hba->last_dme_cmd_tstamp));
2195
2196 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2197 min_sleep_time_us =
2198 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2199 else
2200 return; /* no more delay required */
2201 }
2202
2203 /* allow sleep for extra 50us if needed */
2204 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2205}
2206
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302207/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302208 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2209 * @hba: per adapter instance
2210 * @attr_sel: uic command argument1
2211 * @attr_set: attribute set type as uic command argument2
2212 * @mib_val: setting value as uic command argument3
2213 * @peer: indicate whether peer or local
2214 *
2215 * Returns 0 on success, non-zero value on failure
2216 */
2217int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2218 u8 attr_set, u32 mib_val, u8 peer)
2219{
2220 struct uic_command uic_cmd = {0};
2221 static const char *const action[] = {
2222 "dme-set",
2223 "dme-peer-set"
2224 };
2225 const char *set = action[!!peer];
2226 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002227 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302228
2229 uic_cmd.command = peer ?
2230 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2231 uic_cmd.argument1 = attr_sel;
2232 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2233 uic_cmd.argument3 = mib_val;
2234
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002235 do {
2236 /* for peer attributes we retry upon failure */
2237 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2238 if (ret)
2239 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2240 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2241 } while (ret && peer && --retries);
2242
2243 if (!retries)
2244 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2245 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2246 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302247
2248 return ret;
2249}
2250EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2251
2252/**
2253 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2254 * @hba: per adapter instance
2255 * @attr_sel: uic command argument1
2256 * @mib_val: the value of the attribute as returned by the UIC command
2257 * @peer: indicate whether peer or local
2258 *
2259 * Returns 0 on success, non-zero value on failure
2260 */
2261int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2262 u32 *mib_val, u8 peer)
2263{
2264 struct uic_command uic_cmd = {0};
2265 static const char *const action[] = {
2266 "dme-get",
2267 "dme-peer-get"
2268 };
2269 const char *get = action[!!peer];
2270 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002271 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002272 struct ufs_pa_layer_attr orig_pwr_info;
2273 struct ufs_pa_layer_attr temp_pwr_info;
2274 bool pwr_mode_change = false;
2275
2276 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2277 orig_pwr_info = hba->pwr_info;
2278 temp_pwr_info = orig_pwr_info;
2279
2280 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2281 orig_pwr_info.pwr_rx == FAST_MODE) {
2282 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2283 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2284 pwr_mode_change = true;
2285 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2286 orig_pwr_info.pwr_rx == SLOW_MODE) {
2287 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2288 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2289 pwr_mode_change = true;
2290 }
2291 if (pwr_mode_change) {
2292 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2293 if (ret)
2294 goto out;
2295 }
2296 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302297
2298 uic_cmd.command = peer ?
2299 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2300 uic_cmd.argument1 = attr_sel;
2301
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002302 do {
2303 /* for peer attributes we retry upon failure */
2304 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2305 if (ret)
2306 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2307 get, UIC_GET_ATTR_ID(attr_sel), ret);
2308 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302309
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002310 if (!retries)
2311 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2312 get, UIC_GET_ATTR_ID(attr_sel), retries);
2313
2314 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302315 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002316
2317 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2318 && pwr_mode_change)
2319 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302320out:
2321 return ret;
2322}
2323EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2324
2325/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002326 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2327 * state) and waits for it to take effect.
2328 *
2329 * @hba: per adapter instance
2330 * @cmd: UIC command to execute
2331 *
2332 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2333 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2334 * and device UniPro link and hence it's final completion would be indicated by
2335 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2336 * addition to normal UIC command completion Status (UCCS). This function only
2337 * returns after the relevant status bits indicate the completion.
2338 *
2339 * Returns 0 on success, non-zero value on failure
2340 */
2341static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2342{
2343 struct completion uic_async_done;
2344 unsigned long flags;
2345 u8 status;
2346 int ret;
2347
2348 mutex_lock(&hba->uic_cmd_mutex);
2349 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002350 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002351
2352 spin_lock_irqsave(hba->host->host_lock, flags);
2353 hba->uic_async_done = &uic_async_done;
2354 ret = __ufshcd_send_uic_cmd(hba, cmd);
2355 spin_unlock_irqrestore(hba->host->host_lock, flags);
2356 if (ret) {
2357 dev_err(hba->dev,
2358 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2359 cmd->command, cmd->argument3, ret);
2360 goto out;
2361 }
2362 ret = ufshcd_wait_for_uic_cmd(hba, cmd);
2363 if (ret) {
2364 dev_err(hba->dev,
2365 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2366 cmd->command, cmd->argument3, ret);
2367 goto out;
2368 }
2369
2370 if (!wait_for_completion_timeout(hba->uic_async_done,
2371 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2372 dev_err(hba->dev,
2373 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2374 cmd->command, cmd->argument3);
2375 ret = -ETIMEDOUT;
2376 goto out;
2377 }
2378
2379 status = ufshcd_get_upmcrs(hba);
2380 if (status != PWR_LOCAL) {
2381 dev_err(hba->dev,
2382 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
2383 cmd->command, status);
2384 ret = (status != PWR_OK) ? status : -1;
2385 }
2386out:
2387 spin_lock_irqsave(hba->host->host_lock, flags);
2388 hba->uic_async_done = NULL;
2389 spin_unlock_irqrestore(hba->host->host_lock, flags);
2390 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002391
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002392 return ret;
2393}
2394
2395/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302396 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2397 * using DME_SET primitives.
2398 * @hba: per adapter instance
2399 * @mode: powr mode value
2400 *
2401 * Returns 0 on success, non-zero value on failure
2402 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302403static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302404{
2405 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002406 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302407
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002408 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2409 ret = ufshcd_dme_set(hba,
2410 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2411 if (ret) {
2412 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2413 __func__, ret);
2414 goto out;
2415 }
2416 }
2417
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302418 uic_cmd.command = UIC_CMD_DME_SET;
2419 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2420 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002421 ufshcd_hold(hba, false);
2422 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2423 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302424
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002425out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002426 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002427}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302428
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002429static int ufshcd_link_recovery(struct ufs_hba *hba)
2430{
2431 int ret;
2432 unsigned long flags;
2433
2434 spin_lock_irqsave(hba->host->host_lock, flags);
2435 hba->ufshcd_state = UFSHCD_STATE_RESET;
2436 ufshcd_set_eh_in_progress(hba);
2437 spin_unlock_irqrestore(hba->host->host_lock, flags);
2438
2439 ret = ufshcd_host_reset_and_restore(hba);
2440
2441 spin_lock_irqsave(hba->host->host_lock, flags);
2442 if (ret)
2443 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2444 ufshcd_clear_eh_in_progress(hba);
2445 spin_unlock_irqrestore(hba->host->host_lock, flags);
2446
2447 if (ret)
2448 dev_err(hba->dev, "%s: link recovery failed, err %d",
2449 __func__, ret);
2450
2451 return ret;
2452}
2453
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002454static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002455{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002456 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002457 struct uic_command uic_cmd = {0};
2458
2459 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002460 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002461
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002462 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002463 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2464 __func__, ret);
2465
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002466 /*
2467 * If link recovery fails then return error so that caller
2468 * don't retry the hibern8 enter again.
2469 */
2470 if (ufshcd_link_recovery(hba))
2471 ret = -ENOLINK;
2472 }
2473
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002474 return ret;
2475}
2476
2477static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2478{
2479 int ret = 0, retries;
2480
2481 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2482 ret = __ufshcd_uic_hibern8_enter(hba);
2483 if (!ret || ret == -ENOLINK)
2484 goto out;
2485 }
2486out:
2487 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002488}
2489
2490static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2491{
2492 struct uic_command uic_cmd = {0};
2493 int ret;
2494
2495 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2496 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302497 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002498 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2499 __func__, ret);
2500 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302501 }
2502
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302503 return ret;
2504}
2505
Yaniv Gardi50646362014-10-23 13:25:13 +03002506 /**
2507 * ufshcd_init_pwr_info - setting the POR (power on reset)
2508 * values in hba power info
2509 * @hba: per-adapter instance
2510 */
2511static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2512{
2513 hba->pwr_info.gear_rx = UFS_PWM_G1;
2514 hba->pwr_info.gear_tx = UFS_PWM_G1;
2515 hba->pwr_info.lane_rx = 1;
2516 hba->pwr_info.lane_tx = 1;
2517 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2518 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2519 hba->pwr_info.hs_rate = 0;
2520}
2521
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302522/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002523 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2524 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302525 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002526static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302527{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002528 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2529
2530 if (hba->max_pwr_info.is_valid)
2531 return 0;
2532
2533 pwr_info->pwr_tx = FASTAUTO_MODE;
2534 pwr_info->pwr_rx = FASTAUTO_MODE;
2535 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302536
2537 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002538 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2539 &pwr_info->lane_rx);
2540 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2541 &pwr_info->lane_tx);
2542
2543 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2544 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2545 __func__,
2546 pwr_info->lane_rx,
2547 pwr_info->lane_tx);
2548 return -EINVAL;
2549 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302550
2551 /*
2552 * First, get the maximum gears of HS speed.
2553 * If a zero value, it means there is no HSGEAR capability.
2554 * Then, get the maximum gears of PWM speed.
2555 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002556 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2557 if (!pwr_info->gear_rx) {
2558 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2559 &pwr_info->gear_rx);
2560 if (!pwr_info->gear_rx) {
2561 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2562 __func__, pwr_info->gear_rx);
2563 return -EINVAL;
2564 }
2565 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302566 }
2567
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002568 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2569 &pwr_info->gear_tx);
2570 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302571 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002572 &pwr_info->gear_tx);
2573 if (!pwr_info->gear_tx) {
2574 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2575 __func__, pwr_info->gear_tx);
2576 return -EINVAL;
2577 }
2578 pwr_info->pwr_tx = SLOWAUTO_MODE;
2579 }
2580
2581 hba->max_pwr_info.is_valid = true;
2582 return 0;
2583}
2584
2585static int ufshcd_change_power_mode(struct ufs_hba *hba,
2586 struct ufs_pa_layer_attr *pwr_mode)
2587{
2588 int ret;
2589
2590 /* if already configured to the requested pwr_mode */
2591 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2592 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2593 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2594 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2595 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2596 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2597 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2598 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2599 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302600 }
2601
2602 /*
2603 * Configure attributes for power mode change with below.
2604 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2605 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2606 * - PA_HSSERIES
2607 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002608 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2609 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2610 pwr_mode->lane_rx);
2611 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2612 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302613 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002614 else
2615 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302616
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002617 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2618 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2619 pwr_mode->lane_tx);
2620 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2621 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302622 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002623 else
2624 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302625
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002626 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2627 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2628 pwr_mode->pwr_rx == FAST_MODE ||
2629 pwr_mode->pwr_tx == FAST_MODE)
2630 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2631 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302632
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002633 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2634 | pwr_mode->pwr_tx);
2635
2636 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302637 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002638 "%s: power mode change failed %d\n", __func__, ret);
2639 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002640 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2641 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002642
2643 memcpy(&hba->pwr_info, pwr_mode,
2644 sizeof(struct ufs_pa_layer_attr));
2645 }
2646
2647 return ret;
2648}
2649
2650/**
2651 * ufshcd_config_pwr_mode - configure a new power mode
2652 * @hba: per-adapter instance
2653 * @desired_pwr_mode: desired power configuration
2654 */
2655static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2656 struct ufs_pa_layer_attr *desired_pwr_mode)
2657{
2658 struct ufs_pa_layer_attr final_params = { 0 };
2659 int ret;
2660
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002661 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2662 desired_pwr_mode, &final_params);
2663
2664 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002665 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2666
2667 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302668
2669 return ret;
2670}
2671
2672/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302673 * ufshcd_complete_dev_init() - checks device readiness
2674 * hba: per-adapter instance
2675 *
2676 * Set fDeviceInit flag and poll until device toggles it.
2677 */
2678static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2679{
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002680 int i;
2681 int err;
Dolev Raviv68078d52013-07-30 00:35:58 +05302682 bool flag_res = 1;
2683
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002684 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2685 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
Dolev Raviv68078d52013-07-30 00:35:58 +05302686 if (err) {
2687 dev_err(hba->dev,
2688 "%s setting fDeviceInit flag failed with error %d\n",
2689 __func__, err);
2690 goto out;
2691 }
2692
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02002693 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
2694 for (i = 0; i < 1000 && !err && flag_res; i++)
2695 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
2696 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2697
Dolev Raviv68078d52013-07-30 00:35:58 +05302698 if (err)
2699 dev_err(hba->dev,
2700 "%s reading fDeviceInit flag failed with error %d\n",
2701 __func__, err);
2702 else if (flag_res)
2703 dev_err(hba->dev,
2704 "%s fDeviceInit was not cleared by the device\n",
2705 __func__);
2706
2707out:
2708 return err;
2709}
2710
2711/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302712 * ufshcd_make_hba_operational - Make UFS controller operational
2713 * @hba: per adapter instance
2714 *
2715 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002716 * 1. Enable required interrupts
2717 * 2. Configure interrupt aggregation
2718 * 3. Program UTRL and UTMRL base addres
2719 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302720 *
2721 * Returns 0 on success, non-zero value on failure
2722 */
2723static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2724{
2725 int err = 0;
2726 u32 reg;
2727
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302728 /* Enable required interrupts */
2729 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2730
2731 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002732 if (ufshcd_is_intr_aggr_allowed(hba))
2733 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2734 else
2735 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302736
2737 /* Configure UTRL and UTMRL base address registers */
2738 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2739 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2740 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2741 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2742 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2743 REG_UTP_TASK_REQ_LIST_BASE_L);
2744 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2745 REG_UTP_TASK_REQ_LIST_BASE_H);
2746
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302747 /*
2748 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
2749 * DEI, HEI bits must be 0
2750 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002751 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302752 if (!(ufshcd_get_lists_status(reg))) {
2753 ufshcd_enable_run_stop_reg(hba);
2754 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302755 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302756 "Host controller not ready to process requests");
2757 err = -EIO;
2758 goto out;
2759 }
2760
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302761out:
2762 return err;
2763}
2764
2765/**
2766 * ufshcd_hba_enable - initialize the controller
2767 * @hba: per adapter instance
2768 *
2769 * The controller resets itself and controller firmware initialization
2770 * sequence kicks off. When controller is ready it will set
2771 * the Host Controller Enable bit to 1.
2772 *
2773 * Returns 0 on success, non-zero value on failure
2774 */
2775static int ufshcd_hba_enable(struct ufs_hba *hba)
2776{
2777 int retry;
2778
2779 /*
2780 * msleep of 1 and 5 used in this function might result in msleep(20),
2781 * but it was necessary to send the UFS FPGA to reset mode during
2782 * development and testing of this driver. msleep can be changed to
2783 * mdelay and retry count can be reduced based on the controller.
2784 */
2785 if (!ufshcd_is_hba_active(hba)) {
2786
2787 /* change controller state to "reset state" */
2788 ufshcd_hba_stop(hba);
2789
2790 /*
2791 * This delay is based on the testing done with UFS host
2792 * controller FPGA. The delay can be changed based on the
2793 * host controller used.
2794 */
2795 msleep(5);
2796 }
2797
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002798 /* UniPro link is disabled at this point */
2799 ufshcd_set_link_off(hba);
2800
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002801 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002802
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302803 /* start controller initialization sequence */
2804 ufshcd_hba_start(hba);
2805
2806 /*
2807 * To initialize a UFS host controller HCE bit must be set to 1.
2808 * During initialization the HCE bit value changes from 1->0->1.
2809 * When the host controller completes initialization sequence
2810 * it sets the value of HCE bit to 1. The same HCE bit is read back
2811 * to check if the controller has completed initialization sequence.
2812 * So without this delay the value HCE = 1, set in the previous
2813 * instruction might be read back.
2814 * This delay can be changed based on the controller.
2815 */
2816 msleep(1);
2817
2818 /* wait for the host controller to complete initialization */
2819 retry = 10;
2820 while (ufshcd_is_hba_active(hba)) {
2821 if (retry) {
2822 retry--;
2823 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302824 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302825 "Controller enable failed\n");
2826 return -EIO;
2827 }
2828 msleep(5);
2829 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002830
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002831 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002832 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002833
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002834 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002835
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302836 return 0;
2837}
2838
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002839static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
2840{
2841 int tx_lanes, i, err = 0;
2842
2843 if (!peer)
2844 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2845 &tx_lanes);
2846 else
2847 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2848 &tx_lanes);
2849 for (i = 0; i < tx_lanes; i++) {
2850 if (!peer)
2851 err = ufshcd_dme_set(hba,
2852 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2853 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2854 0);
2855 else
2856 err = ufshcd_dme_peer_set(hba,
2857 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2858 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2859 0);
2860 if (err) {
2861 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
2862 __func__, peer, i, err);
2863 break;
2864 }
2865 }
2866
2867 return err;
2868}
2869
2870static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
2871{
2872 return ufshcd_disable_tx_lcc(hba, true);
2873}
2874
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302875/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302876 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302877 * @hba: per adapter instance
2878 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302879 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302880 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302881static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302882{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302883 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002884 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302885
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002886 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002887 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302888
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002889 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002890
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002891 /* check if device is detected by inter-connect layer */
2892 if (!ret && !ufshcd_is_device_present(hba)) {
2893 dev_err(hba->dev, "%s: Device not present\n", __func__);
2894 ret = -ENXIO;
2895 goto out;
2896 }
2897
2898 /*
2899 * DME link lost indication is only received when link is up,
2900 * but we can't be sure if the link is up until link startup
2901 * succeeds. So reset the local Uni-Pro and try again.
2902 */
2903 if (ret && ufshcd_hba_enable(hba))
2904 goto out;
2905 } while (ret && retries--);
2906
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302907 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002908 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302909 goto out;
2910
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002911 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
2912 ret = ufshcd_disable_device_tx_lcc(hba);
2913 if (ret)
2914 goto out;
2915 }
2916
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002917 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002918 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
2919 if (ret)
2920 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002921
2922 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302923out:
2924 if (ret)
2925 dev_err(hba->dev, "link startup failed %d\n", ret);
2926 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302927}
2928
2929/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302930 * ufshcd_verify_dev_init() - Verify device initialization
2931 * @hba: per-adapter instance
2932 *
2933 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
2934 * device Transport Protocol (UTP) layer is ready after a reset.
2935 * If the UTP layer at the device side is not initialized, it may
2936 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
2937 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
2938 */
2939static int ufshcd_verify_dev_init(struct ufs_hba *hba)
2940{
2941 int err = 0;
2942 int retries;
2943
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002944 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302945 mutex_lock(&hba->dev_cmd.lock);
2946 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
2947 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
2948 NOP_OUT_TIMEOUT);
2949
2950 if (!err || err == -ETIMEDOUT)
2951 break;
2952
2953 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2954 }
2955 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002956 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302957
2958 if (err)
2959 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2960 return err;
2961}
2962
2963/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002964 * ufshcd_set_queue_depth - set lun queue depth
2965 * @sdev: pointer to SCSI device
2966 *
2967 * Read bLUQueueDepth value and activate scsi tagged command
2968 * queueing. For WLUN, queue depth is set to 1. For best-effort
2969 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
2970 * value that host can queue.
2971 */
2972static void ufshcd_set_queue_depth(struct scsi_device *sdev)
2973{
2974 int ret = 0;
2975 u8 lun_qdepth;
2976 struct ufs_hba *hba;
2977
2978 hba = shost_priv(sdev->host);
2979
2980 lun_qdepth = hba->nutrs;
2981 ret = ufshcd_read_unit_desc_param(hba,
2982 ufshcd_scsi_to_upiu_lun(sdev->lun),
2983 UNIT_DESC_PARAM_LU_Q_DEPTH,
2984 &lun_qdepth,
2985 sizeof(lun_qdepth));
2986
2987 /* Some WLUN doesn't support unit descriptor */
2988 if (ret == -EOPNOTSUPP)
2989 lun_qdepth = 1;
2990 else if (!lun_qdepth)
2991 /* eventually, we can figure out the real queue depth */
2992 lun_qdepth = hba->nutrs;
2993 else
2994 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2995
2996 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2997 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002998 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002999}
3000
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003001/*
3002 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
3003 * @hba: per-adapter instance
3004 * @lun: UFS device lun id
3005 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
3006 *
3007 * Returns 0 in case of success and b_lu_write_protect status would be returned
3008 * @b_lu_write_protect parameter.
3009 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
3010 * Returns -EINVAL in case of invalid parameters passed to this function.
3011 */
3012static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3013 u8 lun,
3014 u8 *b_lu_write_protect)
3015{
3016 int ret;
3017
3018 if (!b_lu_write_protect)
3019 ret = -EINVAL;
3020 /*
3021 * According to UFS device spec, RPMB LU can't be write
3022 * protected so skip reading bLUWriteProtect parameter for
3023 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3024 */
3025 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3026 ret = -ENOTSUPP;
3027 else
3028 ret = ufshcd_read_unit_desc_param(hba,
3029 lun,
3030 UNIT_DESC_PARAM_LU_WR_PROTECT,
3031 b_lu_write_protect,
3032 sizeof(*b_lu_write_protect));
3033 return ret;
3034}
3035
3036/**
3037 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3038 * status
3039 * @hba: per-adapter instance
3040 * @sdev: pointer to SCSI device
3041 *
3042 */
3043static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3044 struct scsi_device *sdev)
3045{
3046 if (hba->dev_info.f_power_on_wp_en &&
3047 !hba->dev_info.is_lu_power_on_wp) {
3048 u8 b_lu_write_protect;
3049
3050 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3051 &b_lu_write_protect) &&
3052 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3053 hba->dev_info.is_lu_power_on_wp = true;
3054 }
3055}
3056
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003057/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303058 * ufshcd_slave_alloc - handle initial SCSI device configurations
3059 * @sdev: pointer to SCSI device
3060 *
3061 * Returns success
3062 */
3063static int ufshcd_slave_alloc(struct scsi_device *sdev)
3064{
3065 struct ufs_hba *hba;
3066
3067 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303068
3069 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3070 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303071
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303072 /* allow SCSI layer to restart the device in case of errors */
3073 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003074
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003075 /* REPORT SUPPORTED OPERATION CODES is not supported */
3076 sdev->no_report_opcodes = 1;
3077
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003078
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003079 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003080
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003081 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3082
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003083 return 0;
3084}
3085
3086/**
3087 * ufshcd_change_queue_depth - change queue depth
3088 * @sdev: pointer to SCSI device
3089 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003090 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003091 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003092 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003093static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003094{
3095 struct ufs_hba *hba = shost_priv(sdev->host);
3096
3097 if (depth > hba->nutrs)
3098 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003099 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303100}
3101
3102/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003103 * ufshcd_slave_configure - adjust SCSI device configurations
3104 * @sdev: pointer to SCSI device
3105 */
3106static int ufshcd_slave_configure(struct scsi_device *sdev)
3107{
3108 struct request_queue *q = sdev->request_queue;
3109
3110 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3111 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3112
3113 return 0;
3114}
3115
3116/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303117 * ufshcd_slave_destroy - remove SCSI device configurations
3118 * @sdev: pointer to SCSI device
3119 */
3120static void ufshcd_slave_destroy(struct scsi_device *sdev)
3121{
3122 struct ufs_hba *hba;
3123
3124 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003125 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003126 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3127 unsigned long flags;
3128
3129 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003130 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003131 spin_unlock_irqrestore(hba->host->host_lock, flags);
3132 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303133}
3134
3135/**
3136 * ufshcd_task_req_compl - handle task management request completion
3137 * @hba: per adapter instance
3138 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303139 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303140 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303141 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303142 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303143static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303144{
3145 struct utp_task_req_desc *task_req_descp;
3146 struct utp_upiu_task_rsp *task_rsp_upiup;
3147 unsigned long flags;
3148 int ocs_value;
3149 int task_result;
3150
3151 spin_lock_irqsave(hba->host->host_lock, flags);
3152
3153 /* Clear completed tasks from outstanding_tasks */
3154 __clear_bit(index, &hba->outstanding_tasks);
3155
3156 task_req_descp = hba->utmrdl_base_addr;
3157 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3158
3159 if (ocs_value == OCS_SUCCESS) {
3160 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3161 task_req_descp[index].task_rsp_upiu;
3162 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
3163 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303164 if (resp)
3165 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303166 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303167 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3168 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303169 }
3170 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303171
3172 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303173}
3174
3175/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303176 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3177 * @lrb: pointer to local reference block of completed command
3178 * @scsi_status: SCSI command status
3179 *
3180 * Returns value base on SCSI command status
3181 */
3182static inline int
3183ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3184{
3185 int result = 0;
3186
3187 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303188 case SAM_STAT_CHECK_CONDITION:
3189 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303190 case SAM_STAT_GOOD:
3191 result |= DID_OK << 16 |
3192 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303193 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303194 break;
3195 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303196 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303197 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303198 ufshcd_copy_sense_data(lrbp);
3199 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303200 break;
3201 default:
3202 result |= DID_ERROR << 16;
3203 break;
3204 } /* end of switch */
3205
3206 return result;
3207}
3208
3209/**
3210 * ufshcd_transfer_rsp_status - Get overall status of the response
3211 * @hba: per adapter instance
3212 * @lrb: pointer to local reference block of completed command
3213 *
3214 * Returns result of the command to notify SCSI midlayer
3215 */
3216static inline int
3217ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3218{
3219 int result = 0;
3220 int scsi_status;
3221 int ocs;
3222
3223 /* overall command status of utrd */
3224 ocs = ufshcd_get_tr_ocs(lrbp);
3225
3226 switch (ocs) {
3227 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303228 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303229
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303230 switch (result) {
3231 case UPIU_TRANSACTION_RESPONSE:
3232 /*
3233 * get the response UPIU result to extract
3234 * the SCSI command status
3235 */
3236 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3237
3238 /*
3239 * get the result based on SCSI status response
3240 * to notify the SCSI midlayer of the command status
3241 */
3242 scsi_status = result & MASK_SCSI_STATUS;
3243 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303244
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003245 /*
3246 * Currently we are only supporting BKOPs exception
3247 * events hence we can ignore BKOPs exception event
3248 * during power management callbacks. BKOPs exception
3249 * event is not expected to be raised in runtime suspend
3250 * callback as it allows the urgent bkops.
3251 * During system suspend, we are anyway forcefully
3252 * disabling the bkops and if urgent bkops is needed
3253 * it will be enabled on system resume. Long term
3254 * solution could be to abort the system suspend if
3255 * UFS device needs urgent BKOPs.
3256 */
3257 if (!hba->pm_op_in_progress &&
3258 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303259 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303260 break;
3261 case UPIU_TRANSACTION_REJECT_UPIU:
3262 /* TODO: handle Reject UPIU Response */
3263 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303264 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303265 "Reject UPIU not fully implemented\n");
3266 break;
3267 default:
3268 result = DID_ERROR << 16;
3269 dev_err(hba->dev,
3270 "Unexpected request response code = %x\n",
3271 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303272 break;
3273 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303274 break;
3275 case OCS_ABORTED:
3276 result |= DID_ABORT << 16;
3277 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303278 case OCS_INVALID_COMMAND_STATUS:
3279 result |= DID_REQUEUE << 16;
3280 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303281 case OCS_INVALID_CMD_TABLE_ATTR:
3282 case OCS_INVALID_PRDT_ATTR:
3283 case OCS_MISMATCH_DATA_BUF_SIZE:
3284 case OCS_MISMATCH_RESP_UPIU_SIZE:
3285 case OCS_PEER_COMM_FAILURE:
3286 case OCS_FATAL_ERROR:
3287 default:
3288 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303289 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303290 "OCS error from controller = %x\n", ocs);
3291 break;
3292 } /* end of switch */
3293
3294 return result;
3295}
3296
3297/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303298 * ufshcd_uic_cmd_compl - handle completion of uic command
3299 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303300 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303301 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303302static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303303{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303304 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303305 hba->active_uic_cmd->argument2 |=
3306 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303307 hba->active_uic_cmd->argument3 =
3308 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303309 complete(&hba->active_uic_cmd->done);
3310 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303311
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003312 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3313 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303314}
3315
3316/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303317 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3318 * @hba: per adapter instance
3319 */
3320static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3321{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303322 struct ufshcd_lrb *lrbp;
3323 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303324 unsigned long completed_reqs;
3325 u32 tr_doorbell;
3326 int result;
3327 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003328
3329 /* Resetting interrupt aggregation counters first and reading the
3330 * DOOR_BELL afterward allows us to handle all the completed requests.
3331 * In order to prevent other interrupts starvation the DB is read once
3332 * after reset. The down side of this solution is the possibility of
3333 * false interrupt if device completes another request after resetting
3334 * aggregation and before reading the DB.
3335 */
Yaniv Gardib8521902015-05-17 18:54:57 +03003336 if (ufshcd_is_intr_aggr_allowed(hba))
3337 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303338
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303339 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303340 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3341
Dolev Ravive9d501b2014-07-01 12:22:37 +03003342 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3343 lrbp = &hba->lrb[index];
3344 cmd = lrbp->cmd;
3345 if (cmd) {
3346 result = ufshcd_transfer_rsp_status(hba, lrbp);
3347 scsi_dma_unmap(cmd);
3348 cmd->result = result;
3349 /* Mark completed command as NULL in LRB */
3350 lrbp->cmd = NULL;
3351 clear_bit_unlock(index, &hba->lrb_in_use);
3352 /* Do not touch lrbp after scsi done */
3353 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003354 __ufshcd_release(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003355 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
3356 if (hba->dev_cmd.complete)
3357 complete(hba->dev_cmd.complete);
3358 }
3359 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303360
3361 /* clear corresponding bits of completed commands */
3362 hba->outstanding_reqs ^= completed_reqs;
3363
Sahitya Tummala856b3482014-09-25 15:32:34 +03003364 ufshcd_clk_scaling_update_busy(hba);
3365
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303366 /* we might have free'd some tags above */
3367 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303368}
3369
3370/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303371 * ufshcd_disable_ee - disable exception event
3372 * @hba: per-adapter instance
3373 * @mask: exception event to disable
3374 *
3375 * Disables exception event in the device so that the EVENT_ALERT
3376 * bit is not set.
3377 *
3378 * Returns zero on success, non-zero error value on failure.
3379 */
3380static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3381{
3382 int err = 0;
3383 u32 val;
3384
3385 if (!(hba->ee_ctrl_mask & mask))
3386 goto out;
3387
3388 val = hba->ee_ctrl_mask & ~mask;
3389 val &= 0xFFFF; /* 2 bytes */
3390 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3391 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3392 if (!err)
3393 hba->ee_ctrl_mask &= ~mask;
3394out:
3395 return err;
3396}
3397
3398/**
3399 * ufshcd_enable_ee - enable exception event
3400 * @hba: per-adapter instance
3401 * @mask: exception event to enable
3402 *
3403 * Enable corresponding exception event in the device to allow
3404 * device to alert host in critical scenarios.
3405 *
3406 * Returns zero on success, non-zero error value on failure.
3407 */
3408static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3409{
3410 int err = 0;
3411 u32 val;
3412
3413 if (hba->ee_ctrl_mask & mask)
3414 goto out;
3415
3416 val = hba->ee_ctrl_mask | mask;
3417 val &= 0xFFFF; /* 2 bytes */
3418 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3419 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3420 if (!err)
3421 hba->ee_ctrl_mask |= mask;
3422out:
3423 return err;
3424}
3425
3426/**
3427 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3428 * @hba: per-adapter instance
3429 *
3430 * Allow device to manage background operations on its own. Enabling
3431 * this might lead to inconsistent latencies during normal data transfers
3432 * as the device is allowed to manage its own way of handling background
3433 * operations.
3434 *
3435 * Returns zero on success, non-zero on failure.
3436 */
3437static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3438{
3439 int err = 0;
3440
3441 if (hba->auto_bkops_enabled)
3442 goto out;
3443
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003444 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303445 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3446 if (err) {
3447 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3448 __func__, err);
3449 goto out;
3450 }
3451
3452 hba->auto_bkops_enabled = true;
3453
3454 /* No need of URGENT_BKOPS exception from the device */
3455 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3456 if (err)
3457 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3458 __func__, err);
3459out:
3460 return err;
3461}
3462
3463/**
3464 * ufshcd_disable_auto_bkops - block device in doing background operations
3465 * @hba: per-adapter instance
3466 *
3467 * Disabling background operations improves command response latency but
3468 * has drawback of device moving into critical state where the device is
3469 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3470 * host is idle so that BKOPS are managed effectively without any negative
3471 * impacts.
3472 *
3473 * Returns zero on success, non-zero on failure.
3474 */
3475static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3476{
3477 int err = 0;
3478
3479 if (!hba->auto_bkops_enabled)
3480 goto out;
3481
3482 /*
3483 * If host assisted BKOPs is to be enabled, make sure
3484 * urgent bkops exception is allowed.
3485 */
3486 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3487 if (err) {
3488 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3489 __func__, err);
3490 goto out;
3491 }
3492
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02003493 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303494 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3495 if (err) {
3496 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3497 __func__, err);
3498 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3499 goto out;
3500 }
3501
3502 hba->auto_bkops_enabled = false;
3503out:
3504 return err;
3505}
3506
3507/**
3508 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3509 * @hba: per adapter instance
3510 *
3511 * After a device reset the device may toggle the BKOPS_EN flag
3512 * to default value. The s/w tracking variables should be updated
3513 * as well. Do this by forcing enable of auto bkops.
3514 */
3515static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3516{
3517 hba->auto_bkops_enabled = false;
3518 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3519 ufshcd_enable_auto_bkops(hba);
3520}
3521
3522static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3523{
3524 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3525 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3526}
3527
3528/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003529 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3530 * @hba: per-adapter instance
3531 * @status: bkops_status value
3532 *
3533 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3534 * flag in the device to permit background operations if the device
3535 * bkops_status is greater than or equal to "status" argument passed to
3536 * this function, disable otherwise.
3537 *
3538 * Returns 0 for success, non-zero in case of failure.
3539 *
3540 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3541 * to know whether auto bkops is enabled or disabled after this function
3542 * returns control to it.
3543 */
3544static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3545 enum bkops_status status)
3546{
3547 int err;
3548 u32 curr_status = 0;
3549
3550 err = ufshcd_get_bkops_status(hba, &curr_status);
3551 if (err) {
3552 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3553 __func__, err);
3554 goto out;
3555 } else if (curr_status > BKOPS_STATUS_MAX) {
3556 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3557 __func__, curr_status);
3558 err = -EINVAL;
3559 goto out;
3560 }
3561
3562 if (curr_status >= status)
3563 err = ufshcd_enable_auto_bkops(hba);
3564 else
3565 err = ufshcd_disable_auto_bkops(hba);
3566out:
3567 return err;
3568}
3569
3570/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303571 * ufshcd_urgent_bkops - handle urgent bkops exception event
3572 * @hba: per-adapter instance
3573 *
3574 * Enable fBackgroundOpsEn flag in the device to permit background
3575 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003576 *
3577 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3578 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303579 */
3580static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3581{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003582 return ufshcd_bkops_ctrl(hba, BKOPS_STATUS_PERF_IMPACT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303583}
3584
3585static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3586{
3587 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3588 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3589}
3590
3591/**
3592 * ufshcd_exception_event_handler - handle exceptions raised by device
3593 * @work: pointer to work data
3594 *
3595 * Read bExceptionEventStatus attribute from the device and handle the
3596 * exception event accordingly.
3597 */
3598static void ufshcd_exception_event_handler(struct work_struct *work)
3599{
3600 struct ufs_hba *hba;
3601 int err;
3602 u32 status = 0;
3603 hba = container_of(work, struct ufs_hba, eeh_work);
3604
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303605 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303606 err = ufshcd_get_ee_status(hba, &status);
3607 if (err) {
3608 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3609 __func__, err);
3610 goto out;
3611 }
3612
3613 status &= hba->ee_ctrl_mask;
3614 if (status & MASK_EE_URGENT_BKOPS) {
3615 err = ufshcd_urgent_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003616 if (err < 0)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303617 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3618 __func__, err);
3619 }
3620out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303621 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303622 return;
3623}
3624
3625/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303626 * ufshcd_err_handler - handle UFS errors that require s/w attention
3627 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303628 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303629static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303630{
3631 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303632 unsigned long flags;
3633 u32 err_xfer = 0;
3634 u32 err_tm = 0;
3635 int err = 0;
3636 int tag;
3637
3638 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303639
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303640 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003641 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303642
3643 spin_lock_irqsave(hba->host->host_lock, flags);
3644 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
3645 spin_unlock_irqrestore(hba->host->host_lock, flags);
3646 goto out;
3647 }
3648
3649 hba->ufshcd_state = UFSHCD_STATE_RESET;
3650 ufshcd_set_eh_in_progress(hba);
3651
3652 /* Complete requests that have door-bell cleared by h/w */
3653 ufshcd_transfer_req_compl(hba);
3654 ufshcd_tmc_handler(hba);
3655 spin_unlock_irqrestore(hba->host->host_lock, flags);
3656
3657 /* Clear pending transfer requests */
3658 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
3659 if (ufshcd_clear_cmd(hba, tag))
3660 err_xfer |= 1 << tag;
3661
3662 /* Clear pending task management requests */
3663 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
3664 if (ufshcd_clear_tm_cmd(hba, tag))
3665 err_tm |= 1 << tag;
3666
3667 /* Complete the requests that are cleared by s/w */
3668 spin_lock_irqsave(hba->host->host_lock, flags);
3669 ufshcd_transfer_req_compl(hba);
3670 ufshcd_tmc_handler(hba);
3671 spin_unlock_irqrestore(hba->host->host_lock, flags);
3672
3673 /* Fatal errors need reset */
3674 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
3675 ((hba->saved_err & UIC_ERROR) &&
3676 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
3677 err = ufshcd_reset_and_restore(hba);
3678 if (err) {
3679 dev_err(hba->dev, "%s: reset and restore failed\n",
3680 __func__);
3681 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3682 }
3683 /*
3684 * Inform scsi mid-layer that we did reset and allow to handle
3685 * Unit Attention properly.
3686 */
3687 scsi_report_bus_reset(hba->host, 0);
3688 hba->saved_err = 0;
3689 hba->saved_uic_err = 0;
3690 }
3691 ufshcd_clear_eh_in_progress(hba);
3692
3693out:
3694 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003695 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303696 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303697}
3698
3699/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303700 * ufshcd_update_uic_error - check and set fatal UIC error flags.
3701 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303702 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303703static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303704{
3705 u32 reg;
3706
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303707 /* PA_INIT_ERROR is fatal and needs UIC reset */
3708 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
3709 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
3710 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
3711
3712 /* UIC NL/TL/DME errors needs software retry */
3713 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
3714 if (reg)
3715 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
3716
3717 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
3718 if (reg)
3719 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
3720
3721 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
3722 if (reg)
3723 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
3724
3725 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
3726 __func__, hba->uic_error);
3727}
3728
3729/**
3730 * ufshcd_check_errors - Check for errors that need s/w attention
3731 * @hba: per-adapter instance
3732 */
3733static void ufshcd_check_errors(struct ufs_hba *hba)
3734{
3735 bool queue_eh_work = false;
3736
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303737 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303738 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303739
3740 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303741 hba->uic_error = 0;
3742 ufshcd_update_uic_error(hba);
3743 if (hba->uic_error)
3744 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303745 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303746
3747 if (queue_eh_work) {
3748 /* handle fatal errors only when link is functional */
3749 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
3750 /* block commands from scsi mid-layer */
3751 scsi_block_requests(hba->host);
3752
3753 /* transfer error masks to sticky bits */
3754 hba->saved_err |= hba->errors;
3755 hba->saved_uic_err |= hba->uic_error;
3756
3757 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3758 schedule_work(&hba->eh_work);
3759 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303760 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303761 /*
3762 * if (!queue_eh_work) -
3763 * Other errors are either non-fatal where host recovers
3764 * itself without s/w intervention or errors that will be
3765 * handled by the SCSI core layer.
3766 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303767}
3768
3769/**
3770 * ufshcd_tmc_handler - handle task management function completion
3771 * @hba: per adapter instance
3772 */
3773static void ufshcd_tmc_handler(struct ufs_hba *hba)
3774{
3775 u32 tm_doorbell;
3776
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303777 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303778 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303779 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303780}
3781
3782/**
3783 * ufshcd_sl_intr - Interrupt service routine
3784 * @hba: per adapter instance
3785 * @intr_status: contains interrupts generated by the controller
3786 */
3787static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
3788{
3789 hba->errors = UFSHCD_ERROR_MASK & intr_status;
3790 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303791 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303792
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303793 if (intr_status & UFSHCD_UIC_MASK)
3794 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303795
3796 if (intr_status & UTP_TASK_REQ_COMPL)
3797 ufshcd_tmc_handler(hba);
3798
3799 if (intr_status & UTP_TRANSFER_REQ_COMPL)
3800 ufshcd_transfer_req_compl(hba);
3801}
3802
3803/**
3804 * ufshcd_intr - Main interrupt service routine
3805 * @irq: irq number
3806 * @__hba: pointer to adapter instance
3807 *
3808 * Returns IRQ_HANDLED - If interrupt is valid
3809 * IRQ_NONE - If invalid interrupt
3810 */
3811static irqreturn_t ufshcd_intr(int irq, void *__hba)
3812{
3813 u32 intr_status;
3814 irqreturn_t retval = IRQ_NONE;
3815 struct ufs_hba *hba = __hba;
3816
3817 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303818 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303819
3820 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05303821 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303822 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303823 retval = IRQ_HANDLED;
3824 }
3825 spin_unlock(hba->host->host_lock);
3826 return retval;
3827}
3828
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303829static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
3830{
3831 int err = 0;
3832 u32 mask = 1 << tag;
3833 unsigned long flags;
3834
3835 if (!test_bit(tag, &hba->outstanding_tasks))
3836 goto out;
3837
3838 spin_lock_irqsave(hba->host->host_lock, flags);
3839 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
3840 spin_unlock_irqrestore(hba->host->host_lock, flags);
3841
3842 /* poll for max. 1 sec to clear door bell register by h/w */
3843 err = ufshcd_wait_for_register(hba,
3844 REG_UTP_TASK_REQ_DOOR_BELL,
3845 mask, 0, 1000, 1000);
3846out:
3847 return err;
3848}
3849
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303850/**
3851 * ufshcd_issue_tm_cmd - issues task management commands to controller
3852 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303853 * @lun_id: LUN ID to which TM command is sent
3854 * @task_id: task ID to which the TM command is applicable
3855 * @tm_function: task management function opcode
3856 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303857 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303858 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303859 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303860static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
3861 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303862{
3863 struct utp_task_req_desc *task_req_descp;
3864 struct utp_upiu_task_req *task_req_upiup;
3865 struct Scsi_Host *host;
3866 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303867 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303868 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303869 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303870
3871 host = hba->host;
3872
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303873 /*
3874 * Get free slot, sleep if slots are unavailable.
3875 * Even though we use wait_event() which sleeps indefinitely,
3876 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
3877 */
3878 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003879 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303880
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303881 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303882 task_req_descp = hba->utmrdl_base_addr;
3883 task_req_descp += free_slot;
3884
3885 /* Configure task request descriptor */
3886 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
3887 task_req_descp->header.dword_2 =
3888 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
3889
3890 /* Configure task request UPIU */
3891 task_req_upiup =
3892 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303893 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303894 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303895 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303896 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303897 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303898 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003899 /*
3900 * The host shall provide the same value for LUN field in the basic
3901 * header and for Input Parameter.
3902 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303903 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
3904 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303905
3906 /* send command to the controller */
3907 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303908 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303909
3910 spin_unlock_irqrestore(host->host_lock, flags);
3911
3912 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303913 err = wait_event_timeout(hba->tm_wq,
3914 test_bit(free_slot, &hba->tm_condition),
3915 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303916 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303917 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
3918 __func__, tm_function);
3919 if (ufshcd_clear_tm_cmd(hba, free_slot))
3920 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
3921 __func__, free_slot);
3922 err = -ETIMEDOUT;
3923 } else {
3924 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303925 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303926
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303927 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303928 ufshcd_put_tm_slot(hba, free_slot);
3929 wake_up(&hba->tm_tag_wq);
3930
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003931 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303932 return err;
3933}
3934
3935/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303936 * ufshcd_eh_device_reset_handler - device reset handler registered to
3937 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303938 * @cmd: SCSI command pointer
3939 *
3940 * Returns SUCCESS/FAILED
3941 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303942static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303943{
3944 struct Scsi_Host *host;
3945 struct ufs_hba *hba;
3946 unsigned int tag;
3947 u32 pos;
3948 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303949 u8 resp = 0xF;
3950 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303951 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303952
3953 host = cmd->device->host;
3954 hba = shost_priv(host);
3955 tag = cmd->request->tag;
3956
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303957 lrbp = &hba->lrb[tag];
3958 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
3959 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303960 if (!err)
3961 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303962 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303963 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303964
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303965 /* clear the commands that were pending for corresponding LUN */
3966 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
3967 if (hba->lrb[pos].lun == lrbp->lun) {
3968 err = ufshcd_clear_cmd(hba, pos);
3969 if (err)
3970 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303971 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303972 }
3973 spin_lock_irqsave(host->host_lock, flags);
3974 ufshcd_transfer_req_compl(hba);
3975 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303976out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303977 if (!err) {
3978 err = SUCCESS;
3979 } else {
3980 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3981 err = FAILED;
3982 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303983 return err;
3984}
3985
3986/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303987 * ufshcd_abort - abort a specific command
3988 * @cmd: SCSI command pointer
3989 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303990 * Abort the pending command in device by sending UFS_ABORT_TASK task management
3991 * command, and in host controller by clearing the door-bell register. There can
3992 * be race between controller sending the command to the device while abort is
3993 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
3994 * really issued and then try to abort it.
3995 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303996 * Returns SUCCESS/FAILED
3997 */
3998static int ufshcd_abort(struct scsi_cmnd *cmd)
3999{
4000 struct Scsi_Host *host;
4001 struct ufs_hba *hba;
4002 unsigned long flags;
4003 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304004 int err = 0;
4005 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304006 u8 resp = 0xF;
4007 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03004008 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304009
4010 host = cmd->device->host;
4011 hba = shost_priv(host);
4012 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004013 if (!ufshcd_valid_tag(hba, tag)) {
4014 dev_err(hba->dev,
4015 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4016 __func__, tag, cmd, cmd->request);
4017 BUG();
4018 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304019
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004020 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004021 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004022 /* If command is already aborted/completed, return SUCCESS */
4023 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4024 dev_err(hba->dev,
4025 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4026 __func__, tag, hba->outstanding_reqs, reg);
4027 goto out;
4028 }
4029
Dolev Ravive9d501b2014-07-01 12:22:37 +03004030 if (!(reg & (1 << tag))) {
4031 dev_err(hba->dev,
4032 "%s: cmd was completed, but without a notifying intr, tag = %d",
4033 __func__, tag);
4034 }
4035
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304036 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304037 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4038 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4039 UFS_QUERY_TASK, &resp);
4040 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4041 /* cmd pending in the device */
4042 break;
4043 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304044 /*
4045 * cmd not pending in the device, check if it is
4046 * in transition.
4047 */
4048 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4049 if (reg & (1 << tag)) {
4050 /* sleep for max. 200us to stabilize */
4051 usleep_range(100, 200);
4052 continue;
4053 }
4054 /* command completed already */
4055 goto out;
4056 } else {
4057 if (!err)
4058 err = resp; /* service response error */
4059 goto out;
4060 }
4061 }
4062
4063 if (!poll_cnt) {
4064 err = -EBUSY;
4065 goto out;
4066 }
4067
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304068 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4069 UFS_ABORT_TASK, &resp);
4070 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304071 if (!err)
4072 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304073 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304074 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304075
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304076 err = ufshcd_clear_cmd(hba, tag);
4077 if (err)
4078 goto out;
4079
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304080 scsi_dma_unmap(cmd);
4081
4082 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004083 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304084 hba->lrb[tag].cmd = NULL;
4085 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304086
4087 clear_bit_unlock(tag, &hba->lrb_in_use);
4088 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004089
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304090out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304091 if (!err) {
4092 err = SUCCESS;
4093 } else {
4094 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4095 err = FAILED;
4096 }
4097
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004098 /*
4099 * This ufshcd_release() corresponds to the original scsi cmd that got
4100 * aborted here (as we won't get any IRQ for it).
4101 */
4102 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304103 return err;
4104}
4105
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304106/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304107 * ufshcd_host_reset_and_restore - reset and restore host controller
4108 * @hba: per-adapter instance
4109 *
4110 * Note that host controller reset may issue DME_RESET to
4111 * local and remote (device) Uni-Pro stack and the attributes
4112 * are reset to default state.
4113 *
4114 * Returns zero on success, non-zero on failure
4115 */
4116static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4117{
4118 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304119 unsigned long flags;
4120
4121 /* Reset the host controller */
4122 spin_lock_irqsave(hba->host->host_lock, flags);
4123 ufshcd_hba_stop(hba);
4124 spin_unlock_irqrestore(hba->host->host_lock, flags);
4125
4126 err = ufshcd_hba_enable(hba);
4127 if (err)
4128 goto out;
4129
4130 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004131 err = ufshcd_probe_hba(hba);
4132
4133 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304134 err = -EIO;
4135out:
4136 if (err)
4137 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4138
4139 return err;
4140}
4141
4142/**
4143 * ufshcd_reset_and_restore - reset and re-initialize host/device
4144 * @hba: per-adapter instance
4145 *
4146 * Reset and recover device, host and re-establish link. This
4147 * is helpful to recover the communication in fatal error conditions.
4148 *
4149 * Returns zero on success, non-zero on failure
4150 */
4151static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4152{
4153 int err = 0;
4154 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004155 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304156
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004157 do {
4158 err = ufshcd_host_reset_and_restore(hba);
4159 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304160
4161 /*
4162 * After reset the door-bell might be cleared, complete
4163 * outstanding requests in s/w here.
4164 */
4165 spin_lock_irqsave(hba->host->host_lock, flags);
4166 ufshcd_transfer_req_compl(hba);
4167 ufshcd_tmc_handler(hba);
4168 spin_unlock_irqrestore(hba->host->host_lock, flags);
4169
4170 return err;
4171}
4172
4173/**
4174 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4175 * @cmd - SCSI command pointer
4176 *
4177 * Returns SUCCESS/FAILED
4178 */
4179static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4180{
4181 int err;
4182 unsigned long flags;
4183 struct ufs_hba *hba;
4184
4185 hba = shost_priv(cmd->device->host);
4186
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004187 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304188 /*
4189 * Check if there is any race with fatal error handling.
4190 * If so, wait for it to complete. Even though fatal error
4191 * handling does reset and restore in some cases, don't assume
4192 * anything out of it. We are just avoiding race here.
4193 */
4194 do {
4195 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304196 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304197 hba->ufshcd_state == UFSHCD_STATE_RESET))
4198 break;
4199 spin_unlock_irqrestore(hba->host->host_lock, flags);
4200 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304201 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304202 } while (1);
4203
4204 hba->ufshcd_state = UFSHCD_STATE_RESET;
4205 ufshcd_set_eh_in_progress(hba);
4206 spin_unlock_irqrestore(hba->host->host_lock, flags);
4207
4208 err = ufshcd_reset_and_restore(hba);
4209
4210 spin_lock_irqsave(hba->host->host_lock, flags);
4211 if (!err) {
4212 err = SUCCESS;
4213 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4214 } else {
4215 err = FAILED;
4216 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4217 }
4218 ufshcd_clear_eh_in_progress(hba);
4219 spin_unlock_irqrestore(hba->host->host_lock, flags);
4220
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004221 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304222 return err;
4223}
4224
4225/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004226 * ufshcd_get_max_icc_level - calculate the ICC level
4227 * @sup_curr_uA: max. current supported by the regulator
4228 * @start_scan: row at the desc table to start scan from
4229 * @buff: power descriptor buffer
4230 *
4231 * Returns calculated max ICC level for specific regulator
4232 */
4233static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4234{
4235 int i;
4236 int curr_uA;
4237 u16 data;
4238 u16 unit;
4239
4240 for (i = start_scan; i >= 0; i--) {
4241 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4242 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4243 ATTR_ICC_LVL_UNIT_OFFSET;
4244 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4245 switch (unit) {
4246 case UFSHCD_NANO_AMP:
4247 curr_uA = curr_uA / 1000;
4248 break;
4249 case UFSHCD_MILI_AMP:
4250 curr_uA = curr_uA * 1000;
4251 break;
4252 case UFSHCD_AMP:
4253 curr_uA = curr_uA * 1000 * 1000;
4254 break;
4255 case UFSHCD_MICRO_AMP:
4256 default:
4257 break;
4258 }
4259 if (sup_curr_uA >= curr_uA)
4260 break;
4261 }
4262 if (i < 0) {
4263 i = 0;
4264 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4265 }
4266
4267 return (u32)i;
4268}
4269
4270/**
4271 * ufshcd_calc_icc_level - calculate the max ICC level
4272 * In case regulators are not initialized we'll return 0
4273 * @hba: per-adapter instance
4274 * @desc_buf: power descriptor buffer to extract ICC levels from.
4275 * @len: length of desc_buff
4276 *
4277 * Returns calculated ICC level
4278 */
4279static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4280 u8 *desc_buf, int len)
4281{
4282 u32 icc_level = 0;
4283
4284 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4285 !hba->vreg_info.vccq2) {
4286 dev_err(hba->dev,
4287 "%s: Regulator capability was not set, actvIccLevel=%d",
4288 __func__, icc_level);
4289 goto out;
4290 }
4291
4292 if (hba->vreg_info.vcc)
4293 icc_level = ufshcd_get_max_icc_level(
4294 hba->vreg_info.vcc->max_uA,
4295 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4296 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4297
4298 if (hba->vreg_info.vccq)
4299 icc_level = ufshcd_get_max_icc_level(
4300 hba->vreg_info.vccq->max_uA,
4301 icc_level,
4302 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4303
4304 if (hba->vreg_info.vccq2)
4305 icc_level = ufshcd_get_max_icc_level(
4306 hba->vreg_info.vccq2->max_uA,
4307 icc_level,
4308 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4309out:
4310 return icc_level;
4311}
4312
4313static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4314{
4315 int ret;
4316 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4317 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4318
4319 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4320 if (ret) {
4321 dev_err(hba->dev,
4322 "%s: Failed reading power descriptor.len = %d ret = %d",
4323 __func__, buff_len, ret);
4324 return;
4325 }
4326
4327 hba->init_prefetch_data.icc_level =
4328 ufshcd_find_max_sup_active_icc_level(hba,
4329 desc_buf, buff_len);
4330 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4331 __func__, hba->init_prefetch_data.icc_level);
4332
4333 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4334 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4335 &hba->init_prefetch_data.icc_level);
4336
4337 if (ret)
4338 dev_err(hba->dev,
4339 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4340 __func__, hba->init_prefetch_data.icc_level , ret);
4341
4342}
4343
4344/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004345 * ufshcd_scsi_add_wlus - Adds required W-LUs
4346 * @hba: per-adapter instance
4347 *
4348 * UFS device specification requires the UFS devices to support 4 well known
4349 * logical units:
4350 * "REPORT_LUNS" (address: 01h)
4351 * "UFS Device" (address: 50h)
4352 * "RPMB" (address: 44h)
4353 * "BOOT" (address: 30h)
4354 * UFS device's power management needs to be controlled by "POWER CONDITION"
4355 * field of SSU (START STOP UNIT) command. But this "power condition" field
4356 * will take effect only when its sent to "UFS device" well known logical unit
4357 * hence we require the scsi_device instance to represent this logical unit in
4358 * order for the UFS host driver to send the SSU command for power management.
4359
4360 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4361 * Block) LU so user space process can control this LU. User space may also
4362 * want to have access to BOOT LU.
4363
4364 * This function adds scsi device instances for each of all well known LUs
4365 * (except "REPORT LUNS" LU).
4366 *
4367 * Returns zero on success (all required W-LUs are added successfully),
4368 * non-zero error value on failure (if failed to add any of the required W-LU).
4369 */
4370static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4371{
4372 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004373 struct scsi_device *sdev_rpmb;
4374 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004375
4376 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4377 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4378 if (IS_ERR(hba->sdev_ufs_device)) {
4379 ret = PTR_ERR(hba->sdev_ufs_device);
4380 hba->sdev_ufs_device = NULL;
4381 goto out;
4382 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004383 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004384
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004385 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004386 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004387 if (IS_ERR(sdev_boot)) {
4388 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004389 goto remove_sdev_ufs_device;
4390 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004391 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004392
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004393 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004394 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004395 if (IS_ERR(sdev_rpmb)) {
4396 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004397 goto remove_sdev_boot;
4398 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004399 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004400 goto out;
4401
4402remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004403 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004404remove_sdev_ufs_device:
4405 scsi_remove_device(hba->sdev_ufs_device);
4406out:
4407 return ret;
4408}
4409
4410/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004411 * ufshcd_probe_hba - probe hba to detect device and initialize
4412 * @hba: per-adapter instance
4413 *
4414 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304415 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004416static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304417{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304418 int ret;
4419
4420 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304421 if (ret)
4422 goto out;
4423
Yaniv Gardi50646362014-10-23 13:25:13 +03004424 ufshcd_init_pwr_info(hba);
4425
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004426 /* UniPro link is active now */
4427 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304428
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304429 ret = ufshcd_verify_dev_init(hba);
4430 if (ret)
4431 goto out;
4432
Dolev Raviv68078d52013-07-30 00:35:58 +05304433 ret = ufshcd_complete_dev_init(hba);
4434 if (ret)
4435 goto out;
4436
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004437 /* UFS device is also active now */
4438 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304439 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004440 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304441
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004442 if (ufshcd_get_max_pwr_mode(hba)) {
4443 dev_err(hba->dev,
4444 "%s: Failed getting max supported power mode\n",
4445 __func__);
4446 } else {
4447 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
4448 if (ret)
4449 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
4450 __func__, ret);
4451 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004452
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004453 /* set the state as operational after switching to desired gear */
4454 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004455 /*
4456 * If we are in error handling context or in power management callbacks
4457 * context, no need to scan the host
4458 */
4459 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4460 bool flag;
4461
4462 /* clear any previous UFS device information */
4463 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
Yaniv Gardidc3c8d32016-02-01 15:02:46 +02004464 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4465 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004466 hba->dev_info.f_power_on_wp_en = flag;
4467
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004468 if (!hba->is_init_prefetch)
4469 ufshcd_init_icc_levels(hba);
4470
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004471 /* Add required well known logical units to scsi mid layer */
4472 if (ufshcd_scsi_add_wlus(hba))
4473 goto out;
4474
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304475 scsi_scan_host(hba->host);
4476 pm_runtime_put_sync(hba->dev);
4477 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004478
4479 if (!hba->is_init_prefetch)
4480 hba->is_init_prefetch = true;
4481
Sahitya Tummala856b3482014-09-25 15:32:34 +03004482 /* Resume devfreq after UFS device is detected */
4483 if (ufshcd_is_clkscaling_enabled(hba))
4484 devfreq_resume_device(hba->devfreq);
4485
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304486out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004487 /*
4488 * If we failed to initialize the device or the device is not
4489 * present, turn off the power/clocks etc.
4490 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004491 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4492 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004493 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004494 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004495
4496 return ret;
4497}
4498
4499/**
4500 * ufshcd_async_scan - asynchronous execution for probing hba
4501 * @data: data pointer to pass to this function
4502 * @cookie: cookie data
4503 */
4504static void ufshcd_async_scan(void *data, async_cookie_t cookie)
4505{
4506 struct ufs_hba *hba = (struct ufs_hba *)data;
4507
4508 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304509}
4510
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304511static struct scsi_host_template ufshcd_driver_template = {
4512 .module = THIS_MODULE,
4513 .name = UFSHCD,
4514 .proc_name = UFSHCD,
4515 .queuecommand = ufshcd_queuecommand,
4516 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004517 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304518 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004519 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304520 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304521 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
4522 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304523 .this_id = -1,
4524 .sg_tablesize = SG_ALL,
4525 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
4526 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004527 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01004528 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304529};
4530
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004531static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
4532 int ua)
4533{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004534 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004535
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004536 if (!vreg)
4537 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004538
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004539 ret = regulator_set_load(vreg->reg, ua);
4540 if (ret < 0) {
4541 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
4542 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004543 }
4544
4545 return ret;
4546}
4547
4548static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
4549 struct ufs_vreg *vreg)
4550{
4551 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
4552}
4553
4554static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
4555 struct ufs_vreg *vreg)
4556{
4557 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
4558}
4559
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004560static int ufshcd_config_vreg(struct device *dev,
4561 struct ufs_vreg *vreg, bool on)
4562{
4563 int ret = 0;
4564 struct regulator *reg = vreg->reg;
4565 const char *name = vreg->name;
4566 int min_uV, uA_load;
4567
4568 BUG_ON(!vreg);
4569
4570 if (regulator_count_voltages(reg) > 0) {
4571 min_uV = on ? vreg->min_uV : 0;
4572 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
4573 if (ret) {
4574 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
4575 __func__, name, ret);
4576 goto out;
4577 }
4578
4579 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004580 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
4581 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004582 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004583 }
4584out:
4585 return ret;
4586}
4587
4588static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
4589{
4590 int ret = 0;
4591
4592 if (!vreg || vreg->enabled)
4593 goto out;
4594
4595 ret = ufshcd_config_vreg(dev, vreg, true);
4596 if (!ret)
4597 ret = regulator_enable(vreg->reg);
4598
4599 if (!ret)
4600 vreg->enabled = true;
4601 else
4602 dev_err(dev, "%s: %s enable failed, err=%d\n",
4603 __func__, vreg->name, ret);
4604out:
4605 return ret;
4606}
4607
4608static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
4609{
4610 int ret = 0;
4611
4612 if (!vreg || !vreg->enabled)
4613 goto out;
4614
4615 ret = regulator_disable(vreg->reg);
4616
4617 if (!ret) {
4618 /* ignore errors on applying disable config */
4619 ufshcd_config_vreg(dev, vreg, false);
4620 vreg->enabled = false;
4621 } else {
4622 dev_err(dev, "%s: %s disable failed, err=%d\n",
4623 __func__, vreg->name, ret);
4624 }
4625out:
4626 return ret;
4627}
4628
4629static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
4630{
4631 int ret = 0;
4632 struct device *dev = hba->dev;
4633 struct ufs_vreg_info *info = &hba->vreg_info;
4634
4635 if (!info)
4636 goto out;
4637
4638 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
4639 if (ret)
4640 goto out;
4641
4642 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
4643 if (ret)
4644 goto out;
4645
4646 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
4647 if (ret)
4648 goto out;
4649
4650out:
4651 if (ret) {
4652 ufshcd_toggle_vreg(dev, info->vccq2, false);
4653 ufshcd_toggle_vreg(dev, info->vccq, false);
4654 ufshcd_toggle_vreg(dev, info->vcc, false);
4655 }
4656 return ret;
4657}
4658
Raviv Shvili6a771a62014-09-25 15:32:24 +03004659static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
4660{
4661 struct ufs_vreg_info *info = &hba->vreg_info;
4662
4663 if (info)
4664 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
4665
4666 return 0;
4667}
4668
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004669static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
4670{
4671 int ret = 0;
4672
4673 if (!vreg)
4674 goto out;
4675
4676 vreg->reg = devm_regulator_get(dev, vreg->name);
4677 if (IS_ERR(vreg->reg)) {
4678 ret = PTR_ERR(vreg->reg);
4679 dev_err(dev, "%s: %s get failed, err=%d\n",
4680 __func__, vreg->name, ret);
4681 }
4682out:
4683 return ret;
4684}
4685
4686static int ufshcd_init_vreg(struct ufs_hba *hba)
4687{
4688 int ret = 0;
4689 struct device *dev = hba->dev;
4690 struct ufs_vreg_info *info = &hba->vreg_info;
4691
4692 if (!info)
4693 goto out;
4694
4695 ret = ufshcd_get_vreg(dev, info->vcc);
4696 if (ret)
4697 goto out;
4698
4699 ret = ufshcd_get_vreg(dev, info->vccq);
4700 if (ret)
4701 goto out;
4702
4703 ret = ufshcd_get_vreg(dev, info->vccq2);
4704out:
4705 return ret;
4706}
4707
Raviv Shvili6a771a62014-09-25 15:32:24 +03004708static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
4709{
4710 struct ufs_vreg_info *info = &hba->vreg_info;
4711
4712 if (info)
4713 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
4714
4715 return 0;
4716}
4717
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004718static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
4719 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004720{
4721 int ret = 0;
4722 struct ufs_clk_info *clki;
4723 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004724 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004725
4726 if (!head || list_empty(head))
4727 goto out;
4728
4729 list_for_each_entry(clki, head, list) {
4730 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004731 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
4732 continue;
4733
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004734 if (on && !clki->enabled) {
4735 ret = clk_prepare_enable(clki->clk);
4736 if (ret) {
4737 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
4738 __func__, clki->name, ret);
4739 goto out;
4740 }
4741 } else if (!on && clki->enabled) {
4742 clk_disable_unprepare(clki->clk);
4743 }
4744 clki->enabled = on;
4745 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
4746 clki->name, on ? "en" : "dis");
4747 }
4748 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004749
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004750 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004751out:
4752 if (ret) {
4753 list_for_each_entry(clki, head, list) {
4754 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
4755 clk_disable_unprepare(clki->clk);
4756 }
Dolev Raviveda910e2014-10-23 13:25:16 +03004757 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004758 spin_lock_irqsave(hba->host->host_lock, flags);
4759 hba->clk_gating.state = CLKS_ON;
4760 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004761 }
4762 return ret;
4763}
4764
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004765static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
4766{
4767 return __ufshcd_setup_clocks(hba, on, false);
4768}
4769
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004770static int ufshcd_init_clocks(struct ufs_hba *hba)
4771{
4772 int ret = 0;
4773 struct ufs_clk_info *clki;
4774 struct device *dev = hba->dev;
4775 struct list_head *head = &hba->clk_list_head;
4776
4777 if (!head || list_empty(head))
4778 goto out;
4779
4780 list_for_each_entry(clki, head, list) {
4781 if (!clki->name)
4782 continue;
4783
4784 clki->clk = devm_clk_get(dev, clki->name);
4785 if (IS_ERR(clki->clk)) {
4786 ret = PTR_ERR(clki->clk);
4787 dev_err(dev, "%s: %s clk get failed, %d\n",
4788 __func__, clki->name, ret);
4789 goto out;
4790 }
4791
4792 if (clki->max_freq) {
4793 ret = clk_set_rate(clki->clk, clki->max_freq);
4794 if (ret) {
4795 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
4796 __func__, clki->name,
4797 clki->max_freq, ret);
4798 goto out;
4799 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03004800 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004801 }
4802 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
4803 clki->name, clk_get_rate(clki->clk));
4804 }
4805out:
4806 return ret;
4807}
4808
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004809static int ufshcd_variant_hba_init(struct ufs_hba *hba)
4810{
4811 int err = 0;
4812
4813 if (!hba->vops)
4814 goto out;
4815
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004816 err = ufshcd_vops_init(hba);
4817 if (err)
4818 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004819
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004820 err = ufshcd_vops_setup_regulators(hba, true);
4821 if (err)
4822 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004823
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004824 goto out;
4825
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004826out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004827 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004828out:
4829 if (err)
4830 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004831 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004832 return err;
4833}
4834
4835static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
4836{
4837 if (!hba->vops)
4838 return;
4839
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004840 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004841
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004842 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004843
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004844 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004845}
4846
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004847static int ufshcd_hba_init(struct ufs_hba *hba)
4848{
4849 int err;
4850
Raviv Shvili6a771a62014-09-25 15:32:24 +03004851 /*
4852 * Handle host controller power separately from the UFS device power
4853 * rails as it will help controlling the UFS host controller power
4854 * collapse easily which is different than UFS device power collapse.
4855 * Also, enable the host controller power before we go ahead with rest
4856 * of the initialization here.
4857 */
4858 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004859 if (err)
4860 goto out;
4861
Raviv Shvili6a771a62014-09-25 15:32:24 +03004862 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004863 if (err)
4864 goto out;
4865
Raviv Shvili6a771a62014-09-25 15:32:24 +03004866 err = ufshcd_init_clocks(hba);
4867 if (err)
4868 goto out_disable_hba_vreg;
4869
4870 err = ufshcd_setup_clocks(hba, true);
4871 if (err)
4872 goto out_disable_hba_vreg;
4873
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004874 err = ufshcd_init_vreg(hba);
4875 if (err)
4876 goto out_disable_clks;
4877
4878 err = ufshcd_setup_vreg(hba, true);
4879 if (err)
4880 goto out_disable_clks;
4881
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004882 err = ufshcd_variant_hba_init(hba);
4883 if (err)
4884 goto out_disable_vreg;
4885
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004886 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004887 goto out;
4888
4889out_disable_vreg:
4890 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004891out_disable_clks:
4892 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03004893out_disable_hba_vreg:
4894 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004895out:
4896 return err;
4897}
4898
4899static void ufshcd_hba_exit(struct ufs_hba *hba)
4900{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004901 if (hba->is_powered) {
4902 ufshcd_variant_hba_exit(hba);
4903 ufshcd_setup_vreg(hba, false);
4904 ufshcd_setup_clocks(hba, false);
4905 ufshcd_setup_hba_vreg(hba, false);
4906 hba->is_powered = false;
4907 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004908}
4909
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004910static int
4911ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304912{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004913 unsigned char cmd[6] = {REQUEST_SENSE,
4914 0,
4915 0,
4916 0,
4917 SCSI_SENSE_BUFFERSIZE,
4918 0};
4919 char *buffer;
4920 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304921
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004922 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4923 if (!buffer) {
4924 ret = -ENOMEM;
4925 goto out;
4926 }
4927
4928 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
4929 SCSI_SENSE_BUFFERSIZE, NULL,
4930 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
4931 if (ret)
4932 pr_err("%s: failed with err %d\n", __func__, ret);
4933
4934 kfree(buffer);
4935out:
4936 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304937}
4938
4939/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004940 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
4941 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304942 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004943 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304944 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004945 * Returns 0 if requested power mode is set successfully
4946 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304947 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004948static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
4949 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304950{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004951 unsigned char cmd[6] = { START_STOP };
4952 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004953 struct scsi_device *sdp;
4954 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004955 int ret;
4956
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004957 spin_lock_irqsave(hba->host->host_lock, flags);
4958 sdp = hba->sdev_ufs_device;
4959 if (sdp) {
4960 ret = scsi_device_get(sdp);
4961 if (!ret && !scsi_device_online(sdp)) {
4962 ret = -ENODEV;
4963 scsi_device_put(sdp);
4964 }
4965 } else {
4966 ret = -ENODEV;
4967 }
4968 spin_unlock_irqrestore(hba->host->host_lock, flags);
4969
4970 if (ret)
4971 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004972
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304973 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004974 * If scsi commands fail, the scsi mid-layer schedules scsi error-
4975 * handling, which would wait for host to be resumed. Since we know
4976 * we are functional while we are here, skip host resume in error
4977 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304978 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004979 hba->host->eh_noresume = 1;
4980 if (hba->wlun_dev_clr_ua) {
4981 ret = ufshcd_send_request_sense(hba, sdp);
4982 if (ret)
4983 goto out;
4984 /* Unit attention condition is cleared now */
4985 hba->wlun_dev_clr_ua = false;
4986 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304987
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004988 cmd[4] = pwr_mode << 4;
4989
4990 /*
4991 * Current function would be generally called from the power management
4992 * callbacks hence set the REQ_PM flag so that it doesn't resume the
4993 * already suspended childs.
4994 */
4995 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
4996 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
4997 if (ret) {
4998 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02004999 "START_STOP failed for power mode: %d, result %x\n",
5000 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01005001 if (driver_byte(ret) & DRIVER_SENSE)
5002 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005003 }
5004
5005 if (!ret)
5006 hba->curr_dev_pwr_mode = pwr_mode;
5007out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03005008 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005009 hba->host->eh_noresume = 0;
5010 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305011}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305012
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005013static int ufshcd_link_state_transition(struct ufs_hba *hba,
5014 enum uic_link_state req_link_state,
5015 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305016{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005017 int ret = 0;
5018
5019 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305020 return 0;
5021
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005022 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5023 ret = ufshcd_uic_hibern8_enter(hba);
5024 if (!ret)
5025 ufshcd_set_link_hibern8(hba);
5026 else
5027 goto out;
5028 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305029 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005030 * If autobkops is enabled, link can't be turned off because
5031 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305032 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005033 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5034 (!check_for_bkops || (check_for_bkops &&
5035 !hba->auto_bkops_enabled))) {
5036 /*
5037 * Change controller state to "reset state" which
5038 * should also put the link in off/reset state
5039 */
5040 ufshcd_hba_stop(hba);
5041 /*
5042 * TODO: Check if we need any delay to make sure that
5043 * controller is reset
5044 */
5045 ufshcd_set_link_off(hba);
5046 }
5047
5048out:
5049 return ret;
5050}
5051
5052static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5053{
5054 /*
5055 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5056 * power.
5057 *
5058 * If UFS device and link is in OFF state, all power supplies (VCC,
5059 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5060 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5061 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5062 *
5063 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5064 * in low power state which would save some power.
5065 */
5066 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5067 !hba->dev_info.is_lu_power_on_wp) {
5068 ufshcd_setup_vreg(hba, false);
5069 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5070 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5071 if (!ufshcd_is_link_active(hba)) {
5072 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5073 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5074 }
5075 }
5076}
5077
5078static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5079{
5080 int ret = 0;
5081
5082 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5083 !hba->dev_info.is_lu_power_on_wp) {
5084 ret = ufshcd_setup_vreg(hba, true);
5085 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5086 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
5087 if (!ret && !ufshcd_is_link_active(hba)) {
5088 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5089 if (ret)
5090 goto vcc_disable;
5091 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5092 if (ret)
5093 goto vccq_lpm;
5094 }
5095 }
5096 goto out;
5097
5098vccq_lpm:
5099 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5100vcc_disable:
5101 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5102out:
5103 return ret;
5104}
5105
5106static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5107{
5108 if (ufshcd_is_link_off(hba))
5109 ufshcd_setup_hba_vreg(hba, false);
5110}
5111
5112static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5113{
5114 if (ufshcd_is_link_off(hba))
5115 ufshcd_setup_hba_vreg(hba, true);
5116}
5117
5118/**
5119 * ufshcd_suspend - helper function for suspend operations
5120 * @hba: per adapter instance
5121 * @pm_op: desired low power operation type
5122 *
5123 * This function will try to put the UFS device and link into low power
5124 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5125 * (System PM level).
5126 *
5127 * If this function is called during shutdown, it will make sure that
5128 * both UFS device and UFS link is powered off.
5129 *
5130 * NOTE: UFS device & link must be active before we enter in this function.
5131 *
5132 * Returns 0 for success and non-zero for failure
5133 */
5134static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5135{
5136 int ret = 0;
5137 enum ufs_pm_level pm_lvl;
5138 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5139 enum uic_link_state req_link_state;
5140
5141 hba->pm_op_in_progress = 1;
5142 if (!ufshcd_is_shutdown_pm(pm_op)) {
5143 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5144 hba->rpm_lvl : hba->spm_lvl;
5145 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5146 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5147 } else {
5148 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5149 req_link_state = UIC_LINK_OFF_STATE;
5150 }
5151
5152 /*
5153 * If we can't transition into any of the low power modes
5154 * just gate the clocks.
5155 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005156 ufshcd_hold(hba, false);
5157 hba->clk_gating.is_suspended = true;
5158
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005159 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5160 req_link_state == UIC_LINK_ACTIVE_STATE) {
5161 goto disable_clks;
5162 }
5163
5164 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5165 (req_link_state == hba->uic_link_state))
5166 goto out;
5167
5168 /* UFS device & link must be active before we enter in this function */
5169 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5170 ret = -EINVAL;
5171 goto out;
5172 }
5173
5174 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005175 if (ufshcd_can_autobkops_during_suspend(hba)) {
5176 /*
5177 * The device is idle with no requests in the queue,
5178 * allow background operations if bkops status shows
5179 * that performance might be impacted.
5180 */
5181 ret = ufshcd_urgent_bkops(hba);
5182 if (ret)
5183 goto enable_gating;
5184 } else {
5185 /* make sure that auto bkops is disabled */
5186 ufshcd_disable_auto_bkops(hba);
5187 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005188 }
5189
5190 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5191 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5192 !ufshcd_is_runtime_pm(pm_op))) {
5193 /* ensure that bkops is disabled */
5194 ufshcd_disable_auto_bkops(hba);
5195 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5196 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005197 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005198 }
5199
5200 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5201 if (ret)
5202 goto set_dev_active;
5203
5204 ufshcd_vreg_set_lpm(hba);
5205
5206disable_clks:
5207 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03005208 * The clock scaling needs access to controller registers. Hence, Wait
5209 * for pending clock scaling work to be done before clocks are
5210 * turned off.
5211 */
5212 if (ufshcd_is_clkscaling_enabled(hba)) {
5213 devfreq_suspend_device(hba->devfreq);
5214 hba->clk_scaling.window_start_t = 0;
5215 }
5216 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005217 * Call vendor specific suspend callback. As these callbacks may access
5218 * vendor specific host controller register space call them before the
5219 * host clocks are ON.
5220 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005221 ret = ufshcd_vops_suspend(hba, pm_op);
5222 if (ret)
5223 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005224
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005225 ret = ufshcd_vops_setup_clocks(hba, false);
5226 if (ret)
5227 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005228
5229 if (!ufshcd_is_link_active(hba))
5230 ufshcd_setup_clocks(hba, false);
5231 else
5232 /* If link is active, device ref_clk can't be switched off */
5233 __ufshcd_setup_clocks(hba, false, true);
5234
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005235 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005236 /*
5237 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005238 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005239 */
5240 ufshcd_disable_irq(hba);
5241 /* Put the host controller in low power mode if possible */
5242 ufshcd_hba_vreg_set_lpm(hba);
5243 goto out;
5244
5245vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005246 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005247set_link_active:
5248 ufshcd_vreg_set_hpm(hba);
5249 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5250 ufshcd_set_link_active(hba);
5251 else if (ufshcd_is_link_off(hba))
5252 ufshcd_host_reset_and_restore(hba);
5253set_dev_active:
5254 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5255 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005256enable_gating:
5257 hba->clk_gating.is_suspended = false;
5258 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005259out:
5260 hba->pm_op_in_progress = 0;
5261 return ret;
5262}
5263
5264/**
5265 * ufshcd_resume - helper function for resume operations
5266 * @hba: per adapter instance
5267 * @pm_op: runtime PM or system PM
5268 *
5269 * This function basically brings the UFS device, UniPro link and controller
5270 * to active state.
5271 *
5272 * Returns 0 for success and non-zero for failure
5273 */
5274static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5275{
5276 int ret;
5277 enum uic_link_state old_link_state;
5278
5279 hba->pm_op_in_progress = 1;
5280 old_link_state = hba->uic_link_state;
5281
5282 ufshcd_hba_vreg_set_hpm(hba);
5283 /* Make sure clocks are enabled before accessing controller */
5284 ret = ufshcd_setup_clocks(hba, true);
5285 if (ret)
5286 goto out;
5287
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005288 /* enable the host irq as host controller would be active soon */
5289 ret = ufshcd_enable_irq(hba);
5290 if (ret)
5291 goto disable_irq_and_vops_clks;
5292
5293 ret = ufshcd_vreg_set_hpm(hba);
5294 if (ret)
5295 goto disable_irq_and_vops_clks;
5296
5297 /*
5298 * Call vendor specific resume callback. As these callbacks may access
5299 * vendor specific host controller register space call them when the
5300 * host clocks are ON.
5301 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005302 ret = ufshcd_vops_resume(hba, pm_op);
5303 if (ret)
5304 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005305
5306 if (ufshcd_is_link_hibern8(hba)) {
5307 ret = ufshcd_uic_hibern8_exit(hba);
5308 if (!ret)
5309 ufshcd_set_link_active(hba);
5310 else
5311 goto vendor_suspend;
5312 } else if (ufshcd_is_link_off(hba)) {
5313 ret = ufshcd_host_reset_and_restore(hba);
5314 /*
5315 * ufshcd_host_reset_and_restore() should have already
5316 * set the link state as active
5317 */
5318 if (ret || !ufshcd_is_link_active(hba))
5319 goto vendor_suspend;
5320 }
5321
5322 if (!ufshcd_is_ufs_dev_active(hba)) {
5323 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
5324 if (ret)
5325 goto set_old_link_state;
5326 }
5327
Subhash Jadavani374a2462014-09-25 15:32:35 +03005328 /*
5329 * If BKOPs operations are urgently needed at this moment then
5330 * keep auto-bkops enabled or else disable it.
5331 */
5332 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005333 hba->clk_gating.is_suspended = false;
5334
Sahitya Tummala856b3482014-09-25 15:32:34 +03005335 if (ufshcd_is_clkscaling_enabled(hba))
5336 devfreq_resume_device(hba->devfreq);
5337
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005338 /* Schedule clock gating in case of no access to UFS device yet */
5339 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005340 goto out;
5341
5342set_old_link_state:
5343 ufshcd_link_state_transition(hba, old_link_state, 0);
5344vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005345 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005346disable_vreg:
5347 ufshcd_vreg_set_lpm(hba);
5348disable_irq_and_vops_clks:
5349 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005350 ufshcd_setup_clocks(hba, false);
5351out:
5352 hba->pm_op_in_progress = 0;
5353 return ret;
5354}
5355
5356/**
5357 * ufshcd_system_suspend - system suspend routine
5358 * @hba: per adapter instance
5359 * @pm_op: runtime PM or system PM
5360 *
5361 * Check the description of ufshcd_suspend() function for more details.
5362 *
5363 * Returns 0 for success and non-zero for failure
5364 */
5365int ufshcd_system_suspend(struct ufs_hba *hba)
5366{
5367 int ret = 0;
5368
5369 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03005370 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005371
5372 if (pm_runtime_suspended(hba->dev)) {
5373 if (hba->rpm_lvl == hba->spm_lvl)
5374 /*
5375 * There is possibility that device may still be in
5376 * active state during the runtime suspend.
5377 */
5378 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
5379 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
5380 goto out;
5381
5382 /*
5383 * UFS device and/or UFS link low power states during runtime
5384 * suspend seems to be different than what is expected during
5385 * system suspend. Hence runtime resume the devic & link and
5386 * let the system suspend low power states to take effect.
5387 * TODO: If resume takes longer time, we might have optimize
5388 * it in future by not resuming everything if possible.
5389 */
5390 ret = ufshcd_runtime_resume(hba);
5391 if (ret)
5392 goto out;
5393 }
5394
5395 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
5396out:
Dolev Ravive7850602014-09-25 15:32:36 +03005397 if (!ret)
5398 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005399 return ret;
5400}
5401EXPORT_SYMBOL(ufshcd_system_suspend);
5402
5403/**
5404 * ufshcd_system_resume - system resume routine
5405 * @hba: per adapter instance
5406 *
5407 * Returns 0 for success and non-zero for failure
5408 */
5409
5410int ufshcd_system_resume(struct ufs_hba *hba)
5411{
5412 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
5413 /*
5414 * Let the runtime resume take care of resuming
5415 * if runtime suspended.
5416 */
5417 return 0;
5418
5419 return ufshcd_resume(hba, UFS_SYSTEM_PM);
5420}
5421EXPORT_SYMBOL(ufshcd_system_resume);
5422
5423/**
5424 * ufshcd_runtime_suspend - runtime suspend routine
5425 * @hba: per adapter instance
5426 *
5427 * Check the description of ufshcd_suspend() function for more details.
5428 *
5429 * Returns 0 for success and non-zero for failure
5430 */
5431int ufshcd_runtime_suspend(struct ufs_hba *hba)
5432{
5433 if (!hba || !hba->is_powered)
5434 return 0;
5435
5436 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305437}
5438EXPORT_SYMBOL(ufshcd_runtime_suspend);
5439
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005440/**
5441 * ufshcd_runtime_resume - runtime resume routine
5442 * @hba: per adapter instance
5443 *
5444 * This function basically brings the UFS device, UniPro link and controller
5445 * to active state. Following operations are done in this function:
5446 *
5447 * 1. Turn on all the controller related clocks
5448 * 2. Bring the UniPro link out of Hibernate state
5449 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
5450 * to active state.
5451 * 4. If auto-bkops is enabled on the device, disable it.
5452 *
5453 * So following would be the possible power state after this function return
5454 * successfully:
5455 * S1: UFS device in Active state with VCC rail ON
5456 * UniPro link in Active state
5457 * All the UFS/UniPro controller clocks are ON
5458 *
5459 * Returns 0 for success and non-zero for failure
5460 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305461int ufshcd_runtime_resume(struct ufs_hba *hba)
5462{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005463 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305464 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005465 else
5466 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305467}
5468EXPORT_SYMBOL(ufshcd_runtime_resume);
5469
5470int ufshcd_runtime_idle(struct ufs_hba *hba)
5471{
5472 return 0;
5473}
5474EXPORT_SYMBOL(ufshcd_runtime_idle);
5475
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305476/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005477 * ufshcd_shutdown - shutdown routine
5478 * @hba: per adapter instance
5479 *
5480 * This function would power off both UFS device and UFS link.
5481 *
5482 * Returns 0 always to allow force shutdown even in case of errors.
5483 */
5484int ufshcd_shutdown(struct ufs_hba *hba)
5485{
5486 int ret = 0;
5487
5488 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
5489 goto out;
5490
5491 if (pm_runtime_suspended(hba->dev)) {
5492 ret = ufshcd_runtime_resume(hba);
5493 if (ret)
5494 goto out;
5495 }
5496
5497 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
5498out:
5499 if (ret)
5500 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
5501 /* allow force shutdown even in case of errors */
5502 return 0;
5503}
5504EXPORT_SYMBOL(ufshcd_shutdown);
5505
5506/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305507 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305508 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305509 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305510 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305511void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305512{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05305513 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305514 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305515 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305516 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305517
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305518 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005519
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005520 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005521 if (ufshcd_is_clkscaling_enabled(hba))
5522 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005523 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305524}
5525EXPORT_SYMBOL_GPL(ufshcd_remove);
5526
5527/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02005528 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
5529 * @hba: pointer to Host Bus Adapter (HBA)
5530 */
5531void ufshcd_dealloc_host(struct ufs_hba *hba)
5532{
5533 scsi_host_put(hba->host);
5534}
5535EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
5536
5537/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005538 * ufshcd_set_dma_mask - Set dma mask based on the controller
5539 * addressing capability
5540 * @hba: per adapter instance
5541 *
5542 * Returns 0 for success, non-zero for failure
5543 */
5544static int ufshcd_set_dma_mask(struct ufs_hba *hba)
5545{
5546 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
5547 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
5548 return 0;
5549 }
5550 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
5551}
5552
5553/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005554 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305555 * @dev: pointer to device handle
5556 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305557 * Returns 0 on success, non-zero value on failure
5558 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005559int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305560{
5561 struct Scsi_Host *host;
5562 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005563 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305564
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305565 if (!dev) {
5566 dev_err(dev,
5567 "Invalid memory reference for dev is NULL\n");
5568 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305569 goto out_error;
5570 }
5571
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305572 host = scsi_host_alloc(&ufshcd_driver_template,
5573 sizeof(struct ufs_hba));
5574 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305575 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305576 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305577 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305578 }
5579 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305580 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305581 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005582 *hba_handle = hba;
5583
5584out_error:
5585 return err;
5586}
5587EXPORT_SYMBOL(ufshcd_alloc_host);
5588
Sahitya Tummala856b3482014-09-25 15:32:34 +03005589static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
5590{
5591 int ret = 0;
5592 struct ufs_clk_info *clki;
5593 struct list_head *head = &hba->clk_list_head;
5594
5595 if (!head || list_empty(head))
5596 goto out;
5597
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005598 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
5599 if (ret)
5600 return ret;
5601
Sahitya Tummala856b3482014-09-25 15:32:34 +03005602 list_for_each_entry(clki, head, list) {
5603 if (!IS_ERR_OR_NULL(clki->clk)) {
5604 if (scale_up && clki->max_freq) {
5605 if (clki->curr_freq == clki->max_freq)
5606 continue;
5607 ret = clk_set_rate(clki->clk, clki->max_freq);
5608 if (ret) {
5609 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5610 __func__, clki->name,
5611 clki->max_freq, ret);
5612 break;
5613 }
5614 clki->curr_freq = clki->max_freq;
5615
5616 } else if (!scale_up && clki->min_freq) {
5617 if (clki->curr_freq == clki->min_freq)
5618 continue;
5619 ret = clk_set_rate(clki->clk, clki->min_freq);
5620 if (ret) {
5621 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5622 __func__, clki->name,
5623 clki->min_freq, ret);
5624 break;
5625 }
5626 clki->curr_freq = clki->min_freq;
5627 }
5628 }
5629 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
5630 clki->name, clk_get_rate(clki->clk));
5631 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005632
5633 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
5634
Sahitya Tummala856b3482014-09-25 15:32:34 +03005635out:
5636 return ret;
5637}
5638
5639static int ufshcd_devfreq_target(struct device *dev,
5640 unsigned long *freq, u32 flags)
5641{
5642 int err = 0;
5643 struct ufs_hba *hba = dev_get_drvdata(dev);
5644
5645 if (!ufshcd_is_clkscaling_enabled(hba))
5646 return -EINVAL;
5647
5648 if (*freq == UINT_MAX)
5649 err = ufshcd_scale_clks(hba, true);
5650 else if (*freq == 0)
5651 err = ufshcd_scale_clks(hba, false);
5652
5653 return err;
5654}
5655
5656static int ufshcd_devfreq_get_dev_status(struct device *dev,
5657 struct devfreq_dev_status *stat)
5658{
5659 struct ufs_hba *hba = dev_get_drvdata(dev);
5660 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
5661 unsigned long flags;
5662
5663 if (!ufshcd_is_clkscaling_enabled(hba))
5664 return -EINVAL;
5665
5666 memset(stat, 0, sizeof(*stat));
5667
5668 spin_lock_irqsave(hba->host->host_lock, flags);
5669 if (!scaling->window_start_t)
5670 goto start_window;
5671
5672 if (scaling->is_busy_started)
5673 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
5674 scaling->busy_start_t));
5675
5676 stat->total_time = jiffies_to_usecs((long)jiffies -
5677 (long)scaling->window_start_t);
5678 stat->busy_time = scaling->tot_busy_t;
5679start_window:
5680 scaling->window_start_t = jiffies;
5681 scaling->tot_busy_t = 0;
5682
5683 if (hba->outstanding_reqs) {
5684 scaling->busy_start_t = ktime_get();
5685 scaling->is_busy_started = true;
5686 } else {
5687 scaling->busy_start_t = ktime_set(0, 0);
5688 scaling->is_busy_started = false;
5689 }
5690 spin_unlock_irqrestore(hba->host->host_lock, flags);
5691 return 0;
5692}
5693
5694static struct devfreq_dev_profile ufs_devfreq_profile = {
5695 .polling_ms = 100,
5696 .target = ufshcd_devfreq_target,
5697 .get_dev_status = ufshcd_devfreq_get_dev_status,
5698};
5699
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005700/**
5701 * ufshcd_init - Driver initialization routine
5702 * @hba: per-adapter instance
5703 * @mmio_base: base register address
5704 * @irq: Interrupt line of device
5705 * Returns 0 on success, non-zero value on failure
5706 */
5707int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
5708{
5709 int err;
5710 struct Scsi_Host *host = hba->host;
5711 struct device *dev = hba->dev;
5712
5713 if (!mmio_base) {
5714 dev_err(hba->dev,
5715 "Invalid memory reference for mmio_base is NULL\n");
5716 err = -ENODEV;
5717 goto out_error;
5718 }
5719
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305720 hba->mmio_base = mmio_base;
5721 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305722
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005723 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005724 if (err)
5725 goto out_error;
5726
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305727 /* Read capabilities registers */
5728 ufshcd_hba_capabilities(hba);
5729
5730 /* Get UFS version supported by the controller */
5731 hba->ufs_version = ufshcd_get_ufs_version(hba);
5732
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305733 /* Get Interrupt bit mask per version */
5734 hba->intr_mask = ufshcd_get_intr_mask(hba);
5735
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005736 err = ufshcd_set_dma_mask(hba);
5737 if (err) {
5738 dev_err(hba->dev, "set dma mask failed\n");
5739 goto out_disable;
5740 }
5741
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305742 /* Allocate memory for host memory space */
5743 err = ufshcd_memory_alloc(hba);
5744 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305745 dev_err(hba->dev, "Memory allocation failed\n");
5746 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305747 }
5748
5749 /* Configure LRB */
5750 ufshcd_host_memory_configure(hba);
5751
5752 host->can_queue = hba->nutrs;
5753 host->cmd_per_lun = hba->nutrs;
5754 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005755 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305756 host->max_channel = UFSHCD_MAX_CHANNEL;
5757 host->unique_id = host->host_no;
5758 host->max_cmd_len = MAX_CDB_SIZE;
5759
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005760 hba->max_pwr_info.is_valid = false;
5761
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305762 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305763 init_waitqueue_head(&hba->tm_wq);
5764 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305765
5766 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305767 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305768 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305769
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305770 /* Initialize UIC command mutex */
5771 mutex_init(&hba->uic_cmd_mutex);
5772
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305773 /* Initialize mutex for device management commands */
5774 mutex_init(&hba->dev_cmd.lock);
5775
5776 /* Initialize device management tag acquire wait queue */
5777 init_waitqueue_head(&hba->dev_cmd.tag_wq);
5778
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005779 ufshcd_init_clk_gating(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305780 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09005781 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305782 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305783 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005784 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005785 } else {
5786 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305787 }
5788
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305789 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305790 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305791 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005792 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305793 }
5794
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305795 /* Host controller enable */
5796 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305797 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305798 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305799 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305800 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305801
Sahitya Tummala856b3482014-09-25 15:32:34 +03005802 if (ufshcd_is_clkscaling_enabled(hba)) {
5803 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
5804 "simple_ondemand", NULL);
5805 if (IS_ERR(hba->devfreq)) {
5806 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
5807 PTR_ERR(hba->devfreq));
5808 goto out_remove_scsi_host;
5809 }
5810 /* Suspend devfreq until the UFS device is detected */
5811 devfreq_suspend_device(hba->devfreq);
5812 hba->clk_scaling.window_start_t = 0;
5813 }
5814
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305815 /* Hold auto suspend until async scan completes */
5816 pm_runtime_get_sync(dev);
5817
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005818 /*
5819 * The device-initialize-sequence hasn't been invoked yet.
5820 * Set the device to power-off state
5821 */
5822 ufshcd_set_ufs_dev_poweroff(hba);
5823
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305824 async_schedule(ufshcd_async_scan, hba);
5825
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305826 return 0;
5827
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305828out_remove_scsi_host:
5829 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005830exit_gating:
5831 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305832out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005833 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305834 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005835 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305836out_error:
5837 return err;
5838}
5839EXPORT_SYMBOL_GPL(ufshcd_init);
5840
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305841MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
5842MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05305843MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305844MODULE_LICENSE("GPL");
5845MODULE_VERSION(UFSHCD_DRIVER_VERSION);