blob: 03533f05b6c0d5c44acdb7159d5a06e5673ca996 [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 */
61
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053062/* Task management command timeout */
63#define TM_CMD_TIMEOUT 100 /* msecs */
64
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030065/* maximum number of link-startup retries */
66#define DME_LINKSTARTUP_RETRIES 3
67
68/* maximum number of reset retries before giving up */
69#define MAX_HOST_RESET_RETRIES 5
70
Dolev Raviv68078d52013-07-30 00:35:58 +053071/* Expose the flag value from utp_upiu_query.value */
72#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
73
Seungwon Jeon7d568652013-08-31 21:40:20 +053074/* Interrupt aggregation default timeout, unit: 40us */
75#define INT_AGGR_DEF_TO 0x02
76
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030077#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
78 ({ \
79 int _ret; \
80 if (_on) \
81 _ret = ufshcd_enable_vreg(_dev, _vreg); \
82 else \
83 _ret = ufshcd_disable_vreg(_dev, _vreg); \
84 _ret; \
85 })
86
Subhash Jadavanida461ce2014-09-25 15:32:25 +030087static u32 ufs_query_desc_max_size[] = {
88 QUERY_DESC_DEVICE_MAX_SIZE,
89 QUERY_DESC_CONFIGURAION_MAX_SIZE,
90 QUERY_DESC_UNIT_MAX_SIZE,
91 QUERY_DESC_RFU_MAX_SIZE,
92 QUERY_DESC_INTERCONNECT_MAX_SIZE,
93 QUERY_DESC_STRING_MAX_SIZE,
94 QUERY_DESC_RFU_MAX_SIZE,
95 QUERY_DESC_GEOMETRY_MAZ_SIZE,
96 QUERY_DESC_POWER_MAX_SIZE,
97 QUERY_DESC_RFU_MAX_SIZE,
98};
99
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530100enum {
101 UFSHCD_MAX_CHANNEL = 0,
102 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530103 UFSHCD_CMD_PER_LUN = 32,
104 UFSHCD_CAN_QUEUE = 32,
105};
106
107/* UFSHCD states */
108enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530109 UFSHCD_STATE_RESET,
110 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530111 UFSHCD_STATE_OPERATIONAL,
112};
113
114/* UFSHCD error handling flags */
115enum {
116 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530117};
118
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530119/* UFSHCD UIC layer error flags */
120enum {
121 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
122 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
123 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
124 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
125};
126
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530127/* Interrupt configuration options */
128enum {
129 UFSHCD_INT_DISABLE,
130 UFSHCD_INT_ENABLE,
131 UFSHCD_INT_CLEAR,
132};
133
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530134#define ufshcd_set_eh_in_progress(h) \
135 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
136#define ufshcd_eh_in_progress(h) \
137 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
138#define ufshcd_clear_eh_in_progress(h) \
139 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
140
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300141#define ufshcd_set_ufs_dev_active(h) \
142 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
143#define ufshcd_set_ufs_dev_sleep(h) \
144 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
145#define ufshcd_set_ufs_dev_poweroff(h) \
146 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
147#define ufshcd_is_ufs_dev_active(h) \
148 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
149#define ufshcd_is_ufs_dev_sleep(h) \
150 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
151#define ufshcd_is_ufs_dev_poweroff(h) \
152 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
153
154static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
155 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
156 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
157 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
158 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
159 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
160 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
161};
162
163static inline enum ufs_dev_pwr_mode
164ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
165{
166 return ufs_pm_lvl_states[lvl].dev_state;
167}
168
169static inline enum uic_link_state
170ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
171{
172 return ufs_pm_lvl_states[lvl].link_state;
173}
174
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530175static void ufshcd_tmc_handler(struct ufs_hba *hba);
176static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530177static int ufshcd_reset_and_restore(struct ufs_hba *hba);
178static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300179static void ufshcd_hba_exit(struct ufs_hba *hba);
180static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300181static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
182 bool skip_ref_clk);
183static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
184static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
185static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300186static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300187static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
188static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300189static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
190 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300191static int ufshcd_change_power_mode(struct ufs_hba *hba,
192 struct ufs_pa_layer_attr *pwr_mode);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300193
194static inline int ufshcd_enable_irq(struct ufs_hba *hba)
195{
196 int ret = 0;
197
198 if (!hba->is_irq_enabled) {
199 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
200 hba);
201 if (ret)
202 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
203 __func__, ret);
204 hba->is_irq_enabled = true;
205 }
206
207 return ret;
208}
209
210static inline void ufshcd_disable_irq(struct ufs_hba *hba)
211{
212 if (hba->is_irq_enabled) {
213 free_irq(hba->irq, hba);
214 hba->is_irq_enabled = false;
215 }
216}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530217
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530218/*
219 * ufshcd_wait_for_register - wait for register value to change
220 * @hba - per-adapter interface
221 * @reg - mmio register offset
222 * @mask - mask to apply to read register value
223 * @val - wait condition
224 * @interval_us - polling interval in microsecs
225 * @timeout_ms - timeout in millisecs
226 *
227 * Returns -ETIMEDOUT on error, zero on success
228 */
229static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
230 u32 val, unsigned long interval_us, unsigned long timeout_ms)
231{
232 int err = 0;
233 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
234
235 /* ignore bits that we don't intend to wait on */
236 val = val & mask;
237
238 while ((ufshcd_readl(hba, reg) & mask) != val) {
239 /* wakeup within 50us of expiry */
240 usleep_range(interval_us, interval_us + 50);
241
242 if (time_after(jiffies, timeout)) {
243 if ((ufshcd_readl(hba, reg) & mask) != val)
244 err = -ETIMEDOUT;
245 break;
246 }
247 }
248
249 return err;
250}
251
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530252/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530253 * ufshcd_get_intr_mask - Get the interrupt bit mask
254 * @hba - Pointer to adapter instance
255 *
256 * Returns interrupt bit mask per version
257 */
258static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
259{
260 if (hba->ufs_version == UFSHCI_VERSION_10)
261 return INTERRUPT_MASK_ALL_VER_10;
262 else
263 return INTERRUPT_MASK_ALL_VER_11;
264}
265
266/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530267 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
268 * @hba - Pointer to adapter instance
269 *
270 * Returns UFSHCI version supported by the controller
271 */
272static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
273{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200274 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
275 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300276
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530277 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530278}
279
280/**
281 * ufshcd_is_device_present - Check if any device connected to
282 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300283 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530284 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530285 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530286 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300287static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530288{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300289 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
290 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530291}
292
293/**
294 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
295 * @lrb: pointer to local command reference block
296 *
297 * This function is used to get the OCS field from UTRD
298 * Returns the OCS field in the UTRD
299 */
300static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
301{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530302 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530303}
304
305/**
306 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
307 * @task_req_descp: pointer to utp_task_req_desc structure
308 *
309 * This function is used to get the OCS field from UTMRD
310 * Returns the OCS field in the UTMRD
311 */
312static inline int
313ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
314{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530315 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530316}
317
318/**
319 * ufshcd_get_tm_free_slot - get a free slot for task management request
320 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530321 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530322 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530323 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
324 * Returns 0 if free slot is not available, else return 1 with tag value
325 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530326 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530327static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530328{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530329 int tag;
330 bool ret = false;
331
332 if (!free_slot)
333 goto out;
334
335 do {
336 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
337 if (tag >= hba->nutmrs)
338 goto out;
339 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
340
341 *free_slot = tag;
342 ret = true;
343out:
344 return ret;
345}
346
347static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
348{
349 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530350}
351
352/**
353 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
354 * @hba: per adapter instance
355 * @pos: position of the bit to be cleared
356 */
357static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
358{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530359 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530360}
361
362/**
363 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
364 * @reg: Register value of host controller status
365 *
366 * Returns integer, 0 on Success and positive value if failed
367 */
368static inline int ufshcd_get_lists_status(u32 reg)
369{
370 /*
371 * The mask 0xFF is for the following HCS register bits
372 * Bit Description
373 * 0 Device Present
374 * 1 UTRLRDY
375 * 2 UTMRLRDY
376 * 3 UCRDY
377 * 4 HEI
378 * 5 DEI
379 * 6-7 reserved
380 */
381 return (((reg) & (0xFF)) >> 1) ^ (0x07);
382}
383
384/**
385 * ufshcd_get_uic_cmd_result - Get the UIC command result
386 * @hba: Pointer to adapter instance
387 *
388 * This function gets the result of UIC command completion
389 * Returns 0 on success, non zero value on error
390 */
391static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
392{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530393 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530394 MASK_UIC_COMMAND_RESULT;
395}
396
397/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530398 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
399 * @hba: Pointer to adapter instance
400 *
401 * This function gets UIC command argument3
402 * Returns 0 on success, non zero value on error
403 */
404static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
405{
406 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
407}
408
409/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530410 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530411 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530412 */
413static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530414ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530415{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530416 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530417}
418
419/**
420 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
421 * @ucd_rsp_ptr: pointer to response UPIU
422 *
423 * This function gets the response status and scsi_status from response UPIU
424 * Returns the response result code.
425 */
426static inline int
427ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
428{
429 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
430}
431
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530432/*
433 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
434 * from response UPIU
435 * @ucd_rsp_ptr: pointer to response UPIU
436 *
437 * Return the data segment length.
438 */
439static inline unsigned int
440ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
441{
442 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
443 MASK_RSP_UPIU_DATA_SEG_LEN;
444}
445
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530446/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530447 * ufshcd_is_exception_event - Check if the device raised an exception event
448 * @ucd_rsp_ptr: pointer to response UPIU
449 *
450 * The function checks if the device raised an exception event indicated in
451 * the Device Information field of response UPIU.
452 *
453 * Returns true if exception is raised, false otherwise.
454 */
455static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
456{
457 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
458 MASK_RSP_EXCEPTION_EVENT ? true : false;
459}
460
461/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530462 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530463 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530464 */
465static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530466ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530467{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530468 ufshcd_writel(hba, INT_AGGR_ENABLE |
469 INT_AGGR_COUNTER_AND_TIMER_RESET,
470 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
471}
472
473/**
474 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
475 * @hba: per adapter instance
476 * @cnt: Interrupt aggregation counter threshold
477 * @tmout: Interrupt aggregation timeout value
478 */
479static inline void
480ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
481{
482 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
483 INT_AGGR_COUNTER_THLD_VAL(cnt) |
484 INT_AGGR_TIMEOUT_VAL(tmout),
485 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530486}
487
488/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300489 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
490 * @hba: per adapter instance
491 */
492static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
493{
494 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
495}
496
497/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530498 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
499 * When run-stop registers are set to 1, it indicates the
500 * host controller that it can process the requests
501 * @hba: per adapter instance
502 */
503static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
504{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530505 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
506 REG_UTP_TASK_REQ_LIST_RUN_STOP);
507 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
508 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530509}
510
511/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530512 * ufshcd_hba_start - Start controller initialization sequence
513 * @hba: per adapter instance
514 */
515static inline void ufshcd_hba_start(struct ufs_hba *hba)
516{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530517 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530518}
519
520/**
521 * ufshcd_is_hba_active - Get controller state
522 * @hba: per adapter instance
523 *
524 * Returns zero if controller is active, 1 otherwise
525 */
526static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
527{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530528 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530529}
530
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300531static void ufshcd_ungate_work(struct work_struct *work)
532{
533 int ret;
534 unsigned long flags;
535 struct ufs_hba *hba = container_of(work, struct ufs_hba,
536 clk_gating.ungate_work);
537
538 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
539
540 spin_lock_irqsave(hba->host->host_lock, flags);
541 if (hba->clk_gating.state == CLKS_ON) {
542 spin_unlock_irqrestore(hba->host->host_lock, flags);
543 goto unblock_reqs;
544 }
545
546 spin_unlock_irqrestore(hba->host->host_lock, flags);
547 ufshcd_setup_clocks(hba, true);
548
549 /* Exit from hibern8 */
550 if (ufshcd_can_hibern8_during_gating(hba)) {
551 /* Prevent gating in this path */
552 hba->clk_gating.is_suspended = true;
553 if (ufshcd_is_link_hibern8(hba)) {
554 ret = ufshcd_uic_hibern8_exit(hba);
555 if (ret)
556 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
557 __func__, ret);
558 else
559 ufshcd_set_link_active(hba);
560 }
561 hba->clk_gating.is_suspended = false;
562 }
563unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300564 if (ufshcd_is_clkscaling_enabled(hba))
565 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300566 scsi_unblock_requests(hba->host);
567}
568
569/**
570 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
571 * Also, exit from hibern8 mode and set the link as active.
572 * @hba: per adapter instance
573 * @async: This indicates whether caller should ungate clocks asynchronously.
574 */
575int ufshcd_hold(struct ufs_hba *hba, bool async)
576{
577 int rc = 0;
578 unsigned long flags;
579
580 if (!ufshcd_is_clkgating_allowed(hba))
581 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300582 spin_lock_irqsave(hba->host->host_lock, flags);
583 hba->clk_gating.active_reqs++;
584
Sahitya Tummala856b3482014-09-25 15:32:34 +0300585start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300586 switch (hba->clk_gating.state) {
587 case CLKS_ON:
588 break;
589 case REQ_CLKS_OFF:
590 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
591 hba->clk_gating.state = CLKS_ON;
592 break;
593 }
594 /*
595 * If we here, it means gating work is either done or
596 * currently running. Hence, fall through to cancel gating
597 * work and to enable clocks.
598 */
599 case CLKS_OFF:
600 scsi_block_requests(hba->host);
601 hba->clk_gating.state = REQ_CLKS_ON;
602 schedule_work(&hba->clk_gating.ungate_work);
603 /*
604 * fall through to check if we should wait for this
605 * work to be done or not.
606 */
607 case REQ_CLKS_ON:
608 if (async) {
609 rc = -EAGAIN;
610 hba->clk_gating.active_reqs--;
611 break;
612 }
613
614 spin_unlock_irqrestore(hba->host->host_lock, flags);
615 flush_work(&hba->clk_gating.ungate_work);
616 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300617 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300618 goto start;
619 default:
620 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
621 __func__, hba->clk_gating.state);
622 break;
623 }
624 spin_unlock_irqrestore(hba->host->host_lock, flags);
625out:
626 return rc;
627}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200628EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300629
630static void ufshcd_gate_work(struct work_struct *work)
631{
632 struct ufs_hba *hba = container_of(work, struct ufs_hba,
633 clk_gating.gate_work.work);
634 unsigned long flags;
635
636 spin_lock_irqsave(hba->host->host_lock, flags);
637 if (hba->clk_gating.is_suspended) {
638 hba->clk_gating.state = CLKS_ON;
639 goto rel_lock;
640 }
641
642 if (hba->clk_gating.active_reqs
643 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
644 || hba->lrb_in_use || hba->outstanding_tasks
645 || hba->active_uic_cmd || hba->uic_async_done)
646 goto rel_lock;
647
648 spin_unlock_irqrestore(hba->host->host_lock, flags);
649
650 /* put the link into hibern8 mode before turning off clocks */
651 if (ufshcd_can_hibern8_during_gating(hba)) {
652 if (ufshcd_uic_hibern8_enter(hba)) {
653 hba->clk_gating.state = CLKS_ON;
654 goto out;
655 }
656 ufshcd_set_link_hibern8(hba);
657 }
658
Sahitya Tummala856b3482014-09-25 15:32:34 +0300659 if (ufshcd_is_clkscaling_enabled(hba)) {
660 devfreq_suspend_device(hba->devfreq);
661 hba->clk_scaling.window_start_t = 0;
662 }
663
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300664 if (!ufshcd_is_link_active(hba))
665 ufshcd_setup_clocks(hba, false);
666 else
667 /* If link is active, device ref_clk can't be switched off */
668 __ufshcd_setup_clocks(hba, false, true);
669
670 /*
671 * In case you are here to cancel this work the gating state
672 * would be marked as REQ_CLKS_ON. In this case keep the state
673 * as REQ_CLKS_ON which would anyway imply that clocks are off
674 * and a request to turn them on is pending. By doing this way,
675 * we keep the state machine in tact and this would ultimately
676 * prevent from doing cancel work multiple times when there are
677 * new requests arriving before the current cancel work is done.
678 */
679 spin_lock_irqsave(hba->host->host_lock, flags);
680 if (hba->clk_gating.state == REQ_CLKS_OFF)
681 hba->clk_gating.state = CLKS_OFF;
682
683rel_lock:
684 spin_unlock_irqrestore(hba->host->host_lock, flags);
685out:
686 return;
687}
688
689/* host lock must be held before calling this variant */
690static void __ufshcd_release(struct ufs_hba *hba)
691{
692 if (!ufshcd_is_clkgating_allowed(hba))
693 return;
694
695 hba->clk_gating.active_reqs--;
696
697 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
698 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
699 || hba->lrb_in_use || hba->outstanding_tasks
700 || hba->active_uic_cmd || hba->uic_async_done)
701 return;
702
703 hba->clk_gating.state = REQ_CLKS_OFF;
704 schedule_delayed_work(&hba->clk_gating.gate_work,
705 msecs_to_jiffies(hba->clk_gating.delay_ms));
706}
707
708void ufshcd_release(struct ufs_hba *hba)
709{
710 unsigned long flags;
711
712 spin_lock_irqsave(hba->host->host_lock, flags);
713 __ufshcd_release(hba);
714 spin_unlock_irqrestore(hba->host->host_lock, flags);
715}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200716EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300717
718static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
719 struct device_attribute *attr, char *buf)
720{
721 struct ufs_hba *hba = dev_get_drvdata(dev);
722
723 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
724}
725
726static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
727 struct device_attribute *attr, const char *buf, size_t count)
728{
729 struct ufs_hba *hba = dev_get_drvdata(dev);
730 unsigned long flags, value;
731
732 if (kstrtoul(buf, 0, &value))
733 return -EINVAL;
734
735 spin_lock_irqsave(hba->host->host_lock, flags);
736 hba->clk_gating.delay_ms = value;
737 spin_unlock_irqrestore(hba->host->host_lock, flags);
738 return count;
739}
740
741static void ufshcd_init_clk_gating(struct ufs_hba *hba)
742{
743 if (!ufshcd_is_clkgating_allowed(hba))
744 return;
745
746 hba->clk_gating.delay_ms = 150;
747 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
748 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
749
750 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
751 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
752 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
753 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
754 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
755 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
756 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
757}
758
759static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
760{
761 if (!ufshcd_is_clkgating_allowed(hba))
762 return;
763 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900764 cancel_work_sync(&hba->clk_gating.ungate_work);
765 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300766}
767
Sahitya Tummala856b3482014-09-25 15:32:34 +0300768/* Must be called with host lock acquired */
769static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
770{
771 if (!ufshcd_is_clkscaling_enabled(hba))
772 return;
773
774 if (!hba->clk_scaling.is_busy_started) {
775 hba->clk_scaling.busy_start_t = ktime_get();
776 hba->clk_scaling.is_busy_started = true;
777 }
778}
779
780static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
781{
782 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
783
784 if (!ufshcd_is_clkscaling_enabled(hba))
785 return;
786
787 if (!hba->outstanding_reqs && scaling->is_busy_started) {
788 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
789 scaling->busy_start_t));
790 scaling->busy_start_t = ktime_set(0, 0);
791 scaling->is_busy_started = false;
792 }
793}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530794/**
795 * ufshcd_send_command - Send SCSI or device management commands
796 * @hba: per adapter instance
797 * @task_tag: Task tag of the command
798 */
799static inline
800void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
801{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300802 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530803 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530804 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530805}
806
807/**
808 * ufshcd_copy_sense_data - Copy sense data in case of check condition
809 * @lrb - pointer to local reference block
810 */
811static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
812{
813 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530814 if (lrbp->sense_buffer &&
815 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530816 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530817 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530818 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530819 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
820 }
821}
822
823/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530824 * ufshcd_copy_query_response() - Copy the Query Response and the data
825 * descriptor
826 * @hba: per adapter instance
827 * @lrb - pointer to local reference block
828 */
829static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300830int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530831{
832 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
833
Dolev Raviv68078d52013-07-30 00:35:58 +0530834 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530835
Dolev Raviv68078d52013-07-30 00:35:58 +0530836 /* Get the descriptor */
837 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300838 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530839 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300840 u16 resp_len;
841 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530842
843 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300844 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530845 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300846 buf_len = be16_to_cpu(
847 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300848 if (likely(buf_len >= resp_len)) {
849 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
850 } else {
851 dev_warn(hba->dev,
852 "%s: Response size is bigger than buffer",
853 __func__);
854 return -EINVAL;
855 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530856 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300857
858 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530859}
860
861/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530862 * ufshcd_hba_capabilities - Read controller capabilities
863 * @hba: per adapter instance
864 */
865static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
866{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530867 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530868
869 /* nutrs and nutmrs are 0 based values */
870 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
871 hba->nutmrs =
872 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
873}
874
875/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530876 * ufshcd_ready_for_uic_cmd - Check if controller is ready
877 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530878 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530879 * Return true on success, else false
880 */
881static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
882{
883 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
884 return true;
885 else
886 return false;
887}
888
889/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530890 * ufshcd_get_upmcrs - Get the power mode change request status
891 * @hba: Pointer to adapter instance
892 *
893 * This function gets the UPMCRS field of HCS register
894 * Returns value of UPMCRS field
895 */
896static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
897{
898 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
899}
900
901/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530902 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
903 * @hba: per adapter instance
904 * @uic_cmd: UIC command
905 *
906 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530907 */
908static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530909ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530910{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530911 WARN_ON(hba->active_uic_cmd);
912
913 hba->active_uic_cmd = uic_cmd;
914
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530915 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530916 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
917 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
918 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530919
920 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530921 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530922 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530923}
924
925/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530926 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
927 * @hba: per adapter instance
928 * @uic_command: UIC command
929 *
930 * Must be called with mutex held.
931 * Returns 0 only if success.
932 */
933static int
934ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
935{
936 int ret;
937 unsigned long flags;
938
939 if (wait_for_completion_timeout(&uic_cmd->done,
940 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
941 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
942 else
943 ret = -ETIMEDOUT;
944
945 spin_lock_irqsave(hba->host->host_lock, flags);
946 hba->active_uic_cmd = NULL;
947 spin_unlock_irqrestore(hba->host->host_lock, flags);
948
949 return ret;
950}
951
952/**
953 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
954 * @hba: per adapter instance
955 * @uic_cmd: UIC command
956 *
957 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300958 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530959 * Returns 0 only if success.
960 */
961static int
962__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
963{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530964 if (!ufshcd_ready_for_uic_cmd(hba)) {
965 dev_err(hba->dev,
966 "Controller not ready to accept UIC commands\n");
967 return -EIO;
968 }
969
970 init_completion(&uic_cmd->done);
971
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530972 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530973
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300974 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530975}
976
977/**
978 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
979 * @hba: per adapter instance
980 * @uic_cmd: UIC command
981 *
982 * Returns 0 only if success.
983 */
984static int
985ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
986{
987 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300988 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530989
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300990 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530991 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300992 ufshcd_add_delay_before_dme_cmd(hba);
993
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300994 spin_lock_irqsave(hba->host->host_lock, flags);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530995 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300996 spin_unlock_irqrestore(hba->host->host_lock, flags);
997 if (!ret)
998 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
999
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301000 mutex_unlock(&hba->uic_cmd_mutex);
1001
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001002 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301003 return ret;
1004}
1005
1006/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301007 * ufshcd_map_sg - Map scatter-gather list to prdt
1008 * @lrbp - pointer to local reference block
1009 *
1010 * Returns 0 in case of success, non-zero value in case of failure
1011 */
1012static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1013{
1014 struct ufshcd_sg_entry *prd_table;
1015 struct scatterlist *sg;
1016 struct scsi_cmnd *cmd;
1017 int sg_segments;
1018 int i;
1019
1020 cmd = lrbp->cmd;
1021 sg_segments = scsi_dma_map(cmd);
1022 if (sg_segments < 0)
1023 return sg_segments;
1024
1025 if (sg_segments) {
1026 lrbp->utr_descriptor_ptr->prd_table_length =
1027 cpu_to_le16((u16) (sg_segments));
1028
1029 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1030
1031 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1032 prd_table[i].size =
1033 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1034 prd_table[i].base_addr =
1035 cpu_to_le32(lower_32_bits(sg->dma_address));
1036 prd_table[i].upper_addr =
1037 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001038 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301039 }
1040 } else {
1041 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1042 }
1043
1044 return 0;
1045}
1046
1047/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301048 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301049 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301050 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301051 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301052static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301053{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301054 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1055
1056 if (hba->ufs_version == UFSHCI_VERSION_10) {
1057 u32 rw;
1058 rw = set & INTERRUPT_MASK_RW_VER_10;
1059 set = rw | ((set ^ intrs) & intrs);
1060 } else {
1061 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301062 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301063
1064 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1065}
1066
1067/**
1068 * ufshcd_disable_intr - disable interrupts
1069 * @hba: per adapter instance
1070 * @intrs: interrupt bits
1071 */
1072static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1073{
1074 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1075
1076 if (hba->ufs_version == UFSHCI_VERSION_10) {
1077 u32 rw;
1078 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1079 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1080 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1081
1082 } else {
1083 set &= ~intrs;
1084 }
1085
1086 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301087}
1088
1089/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301090 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1091 * descriptor according to request
1092 * @lrbp: pointer to local reference block
1093 * @upiu_flags: flags required in the header
1094 * @cmd_dir: requests data direction
1095 */
1096static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
1097 u32 *upiu_flags, enum dma_data_direction cmd_dir)
1098{
1099 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1100 u32 data_direction;
1101 u32 dword_0;
1102
1103 if (cmd_dir == DMA_FROM_DEVICE) {
1104 data_direction = UTP_DEVICE_TO_HOST;
1105 *upiu_flags = UPIU_CMD_FLAGS_READ;
1106 } else if (cmd_dir == DMA_TO_DEVICE) {
1107 data_direction = UTP_HOST_TO_DEVICE;
1108 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1109 } else {
1110 data_direction = UTP_NO_DATA_TRANSFER;
1111 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1112 }
1113
1114 dword_0 = data_direction | (lrbp->command_type
1115 << UPIU_COMMAND_TYPE_OFFSET);
1116 if (lrbp->intr_cmd)
1117 dword_0 |= UTP_REQ_DESC_INT_CMD;
1118
1119 /* Transfer request descriptor header fields */
1120 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001121 /* dword_1 is reserved, hence it is set to 0 */
1122 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301123 /*
1124 * assigning invalid value for command status. Controller
1125 * updates OCS on command completion, with the command
1126 * status
1127 */
1128 req_desc->header.dword_2 =
1129 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001130 /* dword_3 is reserved, hence it is set to 0 */
1131 req_desc->header.dword_3 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301132}
1133
1134/**
1135 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1136 * for scsi commands
1137 * @lrbp - local reference block pointer
1138 * @upiu_flags - flags
1139 */
1140static
1141void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1142{
1143 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001144 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301145
1146 /* command descriptor fields */
1147 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1148 UPIU_TRANSACTION_COMMAND, upiu_flags,
1149 lrbp->lun, lrbp->task_tag);
1150 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1151 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1152
1153 /* Total EHS length and Data segment length will be zero */
1154 ucd_req_ptr->header.dword_2 = 0;
1155
1156 ucd_req_ptr->sc.exp_data_transfer_len =
1157 cpu_to_be32(lrbp->cmd->sdb.length);
1158
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001159 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1160 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1161 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1162
1163 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301164}
1165
Dolev Raviv68078d52013-07-30 00:35:58 +05301166/**
1167 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1168 * for query requsts
1169 * @hba: UFS hba
1170 * @lrbp: local reference block pointer
1171 * @upiu_flags: flags
1172 */
1173static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1174 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1175{
1176 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1177 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301178 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301179 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1180
1181 /* Query request header */
1182 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1183 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1184 lrbp->lun, lrbp->task_tag);
1185 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1186 0, query->request.query_func, 0, 0);
1187
1188 /* Data segment length */
1189 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
1190 0, 0, len >> 8, (u8)len);
1191
1192 /* Copy the Query Request buffer as is */
1193 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1194 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301195
1196 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001197 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1198 memcpy(descp, query->descriptor, len);
1199
Dolev Raviv68078d52013-07-30 00:35:58 +05301200}
1201
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301202static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1203{
1204 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1205
1206 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1207
1208 /* command descriptor fields */
1209 ucd_req_ptr->header.dword_0 =
1210 UPIU_HEADER_DWORD(
1211 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
1212}
1213
1214/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301215 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301216 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301217 * @lrb - pointer to local reference block
1218 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301219static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301220{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301221 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301222 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301223
1224 switch (lrbp->command_type) {
1225 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301226 if (likely(lrbp->cmd)) {
1227 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1228 lrbp->cmd->sc_data_direction);
1229 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301230 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301231 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301232 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301233 break;
1234 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301235 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301236 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1237 ufshcd_prepare_utp_query_req_upiu(
1238 hba, lrbp, upiu_flags);
1239 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301240 ufshcd_prepare_utp_nop_upiu(lrbp);
1241 else
1242 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301243 break;
1244 case UTP_CMD_TYPE_UFS:
1245 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301246 ret = -ENOTSUPP;
1247 dev_err(hba->dev, "%s: UFS native command are not supported\n",
1248 __func__);
1249 break;
1250 default:
1251 ret = -ENOTSUPP;
1252 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
1253 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301254 break;
1255 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301256
1257 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301258}
1259
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001260/*
1261 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1262 * @scsi_lun: scsi LUN id
1263 *
1264 * Returns UPIU LUN id
1265 */
1266static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1267{
1268 if (scsi_is_wlun(scsi_lun))
1269 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1270 | UFS_UPIU_WLUN_ID;
1271 else
1272 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1273}
1274
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301275/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001276 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1277 * @scsi_lun: UPIU W-LUN id
1278 *
1279 * Returns SCSI W-LUN id
1280 */
1281static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1282{
1283 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1284}
1285
1286/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301287 * ufshcd_queuecommand - main entry point for SCSI requests
1288 * @cmd: command from SCSI Midlayer
1289 * @done: call back function
1290 *
1291 * Returns 0 for success, non-zero in case of failure
1292 */
1293static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1294{
1295 struct ufshcd_lrb *lrbp;
1296 struct ufs_hba *hba;
1297 unsigned long flags;
1298 int tag;
1299 int err = 0;
1300
1301 hba = shost_priv(host);
1302
1303 tag = cmd->request->tag;
1304
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301305 spin_lock_irqsave(hba->host->host_lock, flags);
1306 switch (hba->ufshcd_state) {
1307 case UFSHCD_STATE_OPERATIONAL:
1308 break;
1309 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301310 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301311 goto out_unlock;
1312 case UFSHCD_STATE_ERROR:
1313 set_host_byte(cmd, DID_ERROR);
1314 cmd->scsi_done(cmd);
1315 goto out_unlock;
1316 default:
1317 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1318 __func__, hba->ufshcd_state);
1319 set_host_byte(cmd, DID_BAD_TARGET);
1320 cmd->scsi_done(cmd);
1321 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301322 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301323 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301324
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301325 /* acquire the tag to make sure device cmds don't use it */
1326 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1327 /*
1328 * Dev manage command in progress, requeue the command.
1329 * Requeuing the command helps in cases where the request *may*
1330 * find different tag instead of waiting for dev manage command
1331 * completion.
1332 */
1333 err = SCSI_MLQUEUE_HOST_BUSY;
1334 goto out;
1335 }
1336
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001337 err = ufshcd_hold(hba, true);
1338 if (err) {
1339 err = SCSI_MLQUEUE_HOST_BUSY;
1340 clear_bit_unlock(tag, &hba->lrb_in_use);
1341 goto out;
1342 }
1343 WARN_ON(hba->clk_gating.state != CLKS_ON);
1344
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301345 lrbp = &hba->lrb[tag];
1346
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301347 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301348 lrbp->cmd = cmd;
1349 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1350 lrbp->sense_buffer = cmd->sense_buffer;
1351 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001352 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001353 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301354 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1355
1356 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301357 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301358 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301359 if (err) {
1360 lrbp->cmd = NULL;
1361 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301362 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301363 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301364
1365 /* issue command to the controller */
1366 spin_lock_irqsave(hba->host->host_lock, flags);
1367 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301368out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301369 spin_unlock_irqrestore(hba->host->host_lock, flags);
1370out:
1371 return err;
1372}
1373
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301374static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1375 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1376{
1377 lrbp->cmd = NULL;
1378 lrbp->sense_bufflen = 0;
1379 lrbp->sense_buffer = NULL;
1380 lrbp->task_tag = tag;
1381 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
1382 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1383 lrbp->intr_cmd = true; /* No interrupt aggregation */
1384 hba->dev_cmd.type = cmd_type;
1385
1386 return ufshcd_compose_upiu(hba, lrbp);
1387}
1388
1389static int
1390ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1391{
1392 int err = 0;
1393 unsigned long flags;
1394 u32 mask = 1 << tag;
1395
1396 /* clear outstanding transaction before retry */
1397 spin_lock_irqsave(hba->host->host_lock, flags);
1398 ufshcd_utrl_clear(hba, tag);
1399 spin_unlock_irqrestore(hba->host->host_lock, flags);
1400
1401 /*
1402 * wait for for h/w to clear corresponding bit in door-bell.
1403 * max. wait is 1 sec.
1404 */
1405 err = ufshcd_wait_for_register(hba,
1406 REG_UTP_TRANSFER_REQ_DOOR_BELL,
1407 mask, ~mask, 1000, 1000);
1408
1409 return err;
1410}
1411
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001412static int
1413ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1414{
1415 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1416
1417 /* Get the UPIU response */
1418 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1419 UPIU_RSP_CODE_OFFSET;
1420 return query_res->response;
1421}
1422
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301423/**
1424 * ufshcd_dev_cmd_completion() - handles device management command responses
1425 * @hba: per adapter instance
1426 * @lrbp: pointer to local reference block
1427 */
1428static int
1429ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1430{
1431 int resp;
1432 int err = 0;
1433
1434 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1435
1436 switch (resp) {
1437 case UPIU_TRANSACTION_NOP_IN:
1438 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1439 err = -EINVAL;
1440 dev_err(hba->dev, "%s: unexpected response %x\n",
1441 __func__, resp);
1442 }
1443 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301444 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001445 err = ufshcd_check_query_response(hba, lrbp);
1446 if (!err)
1447 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301448 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301449 case UPIU_TRANSACTION_REJECT_UPIU:
1450 /* TODO: handle Reject UPIU Response */
1451 err = -EPERM;
1452 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1453 __func__);
1454 break;
1455 default:
1456 err = -EINVAL;
1457 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1458 __func__, resp);
1459 break;
1460 }
1461
1462 return err;
1463}
1464
1465static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1466 struct ufshcd_lrb *lrbp, int max_timeout)
1467{
1468 int err = 0;
1469 unsigned long time_left;
1470 unsigned long flags;
1471
1472 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1473 msecs_to_jiffies(max_timeout));
1474
1475 spin_lock_irqsave(hba->host->host_lock, flags);
1476 hba->dev_cmd.complete = NULL;
1477 if (likely(time_left)) {
1478 err = ufshcd_get_tr_ocs(lrbp);
1479 if (!err)
1480 err = ufshcd_dev_cmd_completion(hba, lrbp);
1481 }
1482 spin_unlock_irqrestore(hba->host->host_lock, flags);
1483
1484 if (!time_left) {
1485 err = -ETIMEDOUT;
1486 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
1487 /* sucessfully cleared the command, retry if needed */
1488 err = -EAGAIN;
1489 }
1490
1491 return err;
1492}
1493
1494/**
1495 * ufshcd_get_dev_cmd_tag - Get device management command tag
1496 * @hba: per-adapter instance
1497 * @tag: pointer to variable with available slot value
1498 *
1499 * Get a free slot and lock it until device management command
1500 * completes.
1501 *
1502 * Returns false if free slot is unavailable for locking, else
1503 * return true with tag value in @tag.
1504 */
1505static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1506{
1507 int tag;
1508 bool ret = false;
1509 unsigned long tmp;
1510
1511 if (!tag_out)
1512 goto out;
1513
1514 do {
1515 tmp = ~hba->lrb_in_use;
1516 tag = find_last_bit(&tmp, hba->nutrs);
1517 if (tag >= hba->nutrs)
1518 goto out;
1519 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1520
1521 *tag_out = tag;
1522 ret = true;
1523out:
1524 return ret;
1525}
1526
1527static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1528{
1529 clear_bit_unlock(tag, &hba->lrb_in_use);
1530}
1531
1532/**
1533 * ufshcd_exec_dev_cmd - API for sending device management requests
1534 * @hba - UFS hba
1535 * @cmd_type - specifies the type (NOP, Query...)
1536 * @timeout - time in seconds
1537 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301538 * NOTE: Since there is only one available tag for device management commands,
1539 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301540 */
1541static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1542 enum dev_cmd_type cmd_type, int timeout)
1543{
1544 struct ufshcd_lrb *lrbp;
1545 int err;
1546 int tag;
1547 struct completion wait;
1548 unsigned long flags;
1549
1550 /*
1551 * Get free slot, sleep if slots are unavailable.
1552 * Even though we use wait_event() which sleeps indefinitely,
1553 * the maximum wait time is bounded by SCSI request timeout.
1554 */
1555 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1556
1557 init_completion(&wait);
1558 lrbp = &hba->lrb[tag];
1559 WARN_ON(lrbp->cmd);
1560 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1561 if (unlikely(err))
1562 goto out_put_tag;
1563
1564 hba->dev_cmd.complete = &wait;
1565
1566 spin_lock_irqsave(hba->host->host_lock, flags);
1567 ufshcd_send_command(hba, tag);
1568 spin_unlock_irqrestore(hba->host->host_lock, flags);
1569
1570 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1571
1572out_put_tag:
1573 ufshcd_put_dev_cmd_tag(hba, tag);
1574 wake_up(&hba->dev_cmd.tag_wq);
1575 return err;
1576}
1577
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301578/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001579 * ufshcd_init_query() - init the query response and request parameters
1580 * @hba: per-adapter instance
1581 * @request: address of the request pointer to be initialized
1582 * @response: address of the response pointer to be initialized
1583 * @opcode: operation to perform
1584 * @idn: flag idn to access
1585 * @index: LU number to access
1586 * @selector: query/flag/descriptor further identification
1587 */
1588static inline void ufshcd_init_query(struct ufs_hba *hba,
1589 struct ufs_query_req **request, struct ufs_query_res **response,
1590 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1591{
1592 *request = &hba->dev_cmd.query.request;
1593 *response = &hba->dev_cmd.query.response;
1594 memset(*request, 0, sizeof(struct ufs_query_req));
1595 memset(*response, 0, sizeof(struct ufs_query_res));
1596 (*request)->upiu_req.opcode = opcode;
1597 (*request)->upiu_req.idn = idn;
1598 (*request)->upiu_req.index = index;
1599 (*request)->upiu_req.selector = selector;
1600}
1601
1602/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301603 * ufshcd_query_flag() - API function for sending flag query requests
1604 * hba: per-adapter instance
1605 * query_opcode: flag query to perform
1606 * idn: flag idn to access
1607 * flag_res: the flag value after the query request completes
1608 *
1609 * Returns 0 for success, non-zero in case of failure
1610 */
1611static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1612 enum flag_idn idn, bool *flag_res)
1613{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001614 struct ufs_query_req *request = NULL;
1615 struct ufs_query_res *response = NULL;
1616 int err, index = 0, selector = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301617
1618 BUG_ON(!hba);
1619
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001620 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301621 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001622 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1623 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301624
1625 switch (opcode) {
1626 case UPIU_QUERY_OPCODE_SET_FLAG:
1627 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1628 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1629 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1630 break;
1631 case UPIU_QUERY_OPCODE_READ_FLAG:
1632 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1633 if (!flag_res) {
1634 /* No dummy reads */
1635 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1636 __func__);
1637 err = -EINVAL;
1638 goto out_unlock;
1639 }
1640 break;
1641 default:
1642 dev_err(hba->dev,
1643 "%s: Expected query flag opcode but got = %d\n",
1644 __func__, opcode);
1645 err = -EINVAL;
1646 goto out_unlock;
1647 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301648
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001649 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Dolev Raviv68078d52013-07-30 00:35:58 +05301650
1651 if (err) {
1652 dev_err(hba->dev,
1653 "%s: Sending flag query for idn %d failed, err = %d\n",
1654 __func__, idn, err);
1655 goto out_unlock;
1656 }
1657
1658 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301659 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301660 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1661
1662out_unlock:
1663 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001664 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301665 return err;
1666}
1667
1668/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301669 * ufshcd_query_attr - API function for sending attribute requests
1670 * hba: per-adapter instance
1671 * opcode: attribute opcode
1672 * idn: attribute idn to access
1673 * index: index field
1674 * selector: selector field
1675 * attr_val: the attribute value after the query request completes
1676 *
1677 * Returns 0 for success, non-zero in case of failure
1678*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301679static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301680 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1681{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001682 struct ufs_query_req *request = NULL;
1683 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301684 int err;
1685
1686 BUG_ON(!hba);
1687
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001688 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301689 if (!attr_val) {
1690 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1691 __func__, opcode);
1692 err = -EINVAL;
1693 goto out;
1694 }
1695
1696 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001697 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1698 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301699
1700 switch (opcode) {
1701 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1702 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301703 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301704 break;
1705 case UPIU_QUERY_OPCODE_READ_ATTR:
1706 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1707 break;
1708 default:
1709 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1710 __func__, opcode);
1711 err = -EINVAL;
1712 goto out_unlock;
1713 }
1714
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001715 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301716
1717 if (err) {
1718 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1719 __func__, opcode, idn, err);
1720 goto out_unlock;
1721 }
1722
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301723 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301724
1725out_unlock:
1726 mutex_unlock(&hba->dev_cmd.lock);
1727out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001728 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301729 return err;
1730}
1731
1732/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001733 * ufshcd_query_descriptor - API function for sending descriptor requests
1734 * hba: per-adapter instance
1735 * opcode: attribute opcode
1736 * idn: attribute idn to access
1737 * index: index field
1738 * selector: selector field
1739 * desc_buf: the buffer that contains the descriptor
1740 * buf_len: length parameter passed to the device
1741 *
1742 * Returns 0 for success, non-zero in case of failure.
1743 * The buf_len parameter will contain, on return, the length parameter
1744 * received on the response.
1745 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001746static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001747 enum query_opcode opcode, enum desc_idn idn, u8 index,
1748 u8 selector, u8 *desc_buf, int *buf_len)
1749{
1750 struct ufs_query_req *request = NULL;
1751 struct ufs_query_res *response = NULL;
1752 int err;
1753
1754 BUG_ON(!hba);
1755
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001756 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001757 if (!desc_buf) {
1758 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1759 __func__, opcode);
1760 err = -EINVAL;
1761 goto out;
1762 }
1763
1764 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1765 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1766 __func__, *buf_len);
1767 err = -EINVAL;
1768 goto out;
1769 }
1770
1771 mutex_lock(&hba->dev_cmd.lock);
1772 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1773 selector);
1774 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001775 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001776
1777 switch (opcode) {
1778 case UPIU_QUERY_OPCODE_WRITE_DESC:
1779 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1780 break;
1781 case UPIU_QUERY_OPCODE_READ_DESC:
1782 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1783 break;
1784 default:
1785 dev_err(hba->dev,
1786 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1787 __func__, opcode);
1788 err = -EINVAL;
1789 goto out_unlock;
1790 }
1791
1792 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1793
1794 if (err) {
1795 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1796 __func__, opcode, idn, err);
1797 goto out_unlock;
1798 }
1799
1800 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001801 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001802
1803out_unlock:
1804 mutex_unlock(&hba->dev_cmd.lock);
1805out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001806 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001807 return err;
1808}
1809
1810/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001811 * ufshcd_read_desc_param - read the specified descriptor parameter
1812 * @hba: Pointer to adapter instance
1813 * @desc_id: descriptor idn value
1814 * @desc_index: descriptor index
1815 * @param_offset: offset of the parameter to read
1816 * @param_read_buf: pointer to buffer where parameter would be read
1817 * @param_size: sizeof(param_read_buf)
1818 *
1819 * Return 0 in case of success, non-zero otherwise
1820 */
1821static int ufshcd_read_desc_param(struct ufs_hba *hba,
1822 enum desc_idn desc_id,
1823 int desc_index,
1824 u32 param_offset,
1825 u8 *param_read_buf,
1826 u32 param_size)
1827{
1828 int ret;
1829 u8 *desc_buf;
1830 u32 buff_len;
1831 bool is_kmalloc = true;
1832
1833 /* safety checks */
1834 if (desc_id >= QUERY_DESC_IDN_MAX)
1835 return -EINVAL;
1836
1837 buff_len = ufs_query_desc_max_size[desc_id];
1838 if ((param_offset + param_size) > buff_len)
1839 return -EINVAL;
1840
1841 if (!param_offset && (param_size == buff_len)) {
1842 /* memory space already available to hold full descriptor */
1843 desc_buf = param_read_buf;
1844 is_kmalloc = false;
1845 } else {
1846 /* allocate memory to hold full descriptor */
1847 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1848 if (!desc_buf)
1849 return -ENOMEM;
1850 }
1851
1852 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1853 desc_id, desc_index, 0, desc_buf,
1854 &buff_len);
1855
1856 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
1857 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
1858 ufs_query_desc_max_size[desc_id])
1859 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
1860 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
1861 __func__, desc_id, param_offset, buff_len, ret);
1862 if (!ret)
1863 ret = -EINVAL;
1864
1865 goto out;
1866 }
1867
1868 if (is_kmalloc)
1869 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1870out:
1871 if (is_kmalloc)
1872 kfree(desc_buf);
1873 return ret;
1874}
1875
1876static inline int ufshcd_read_desc(struct ufs_hba *hba,
1877 enum desc_idn desc_id,
1878 int desc_index,
1879 u8 *buf,
1880 u32 size)
1881{
1882 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1883}
1884
1885static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
1886 u8 *buf,
1887 u32 size)
1888{
1889 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
1890}
1891
1892/**
1893 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
1894 * @hba: Pointer to adapter instance
1895 * @lun: lun id
1896 * @param_offset: offset of the parameter to read
1897 * @param_read_buf: pointer to buffer where parameter would be read
1898 * @param_size: sizeof(param_read_buf)
1899 *
1900 * Return 0 in case of success, non-zero otherwise
1901 */
1902static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
1903 int lun,
1904 enum unit_desc_param param_offset,
1905 u8 *param_read_buf,
1906 u32 param_size)
1907{
1908 /*
1909 * Unit descriptors are only available for general purpose LUs (LUN id
1910 * from 0 to 7) and RPMB Well known LU.
1911 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001912 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001913 return -EOPNOTSUPP;
1914
1915 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
1916 param_offset, param_read_buf, param_size);
1917}
1918
1919/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301920 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1921 * @hba: per adapter instance
1922 *
1923 * 1. Allocate DMA memory for Command Descriptor array
1924 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1925 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1926 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1927 * (UTMRDL)
1928 * 4. Allocate memory for local reference block(lrb).
1929 *
1930 * Returns 0 for success, non-zero in case of failure
1931 */
1932static int ufshcd_memory_alloc(struct ufs_hba *hba)
1933{
1934 size_t utmrdl_size, utrdl_size, ucdl_size;
1935
1936 /* Allocate memory for UTP command descriptors */
1937 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001938 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
1939 ucdl_size,
1940 &hba->ucdl_dma_addr,
1941 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301942
1943 /*
1944 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1945 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1946 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1947 * be aligned to 128 bytes as well
1948 */
1949 if (!hba->ucdl_base_addr ||
1950 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301951 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301952 "Command Descriptor Memory allocation failed\n");
1953 goto out;
1954 }
1955
1956 /*
1957 * Allocate memory for UTP Transfer descriptors
1958 * UFSHCI requires 1024 byte alignment of UTRD
1959 */
1960 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001961 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
1962 utrdl_size,
1963 &hba->utrdl_dma_addr,
1964 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301965 if (!hba->utrdl_base_addr ||
1966 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301967 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301968 "Transfer Descriptor Memory allocation failed\n");
1969 goto out;
1970 }
1971
1972 /*
1973 * Allocate memory for UTP Task Management descriptors
1974 * UFSHCI requires 1024 byte alignment of UTMRD
1975 */
1976 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09001977 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
1978 utmrdl_size,
1979 &hba->utmrdl_dma_addr,
1980 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301981 if (!hba->utmrdl_base_addr ||
1982 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301983 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301984 "Task Management Descriptor Memory allocation failed\n");
1985 goto out;
1986 }
1987
1988 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09001989 hba->lrb = devm_kzalloc(hba->dev,
1990 hba->nutrs * sizeof(struct ufshcd_lrb),
1991 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301992 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301993 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301994 goto out;
1995 }
1996 return 0;
1997out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301998 return -ENOMEM;
1999}
2000
2001/**
2002 * ufshcd_host_memory_configure - configure local reference block with
2003 * memory offsets
2004 * @hba: per adapter instance
2005 *
2006 * Configure Host memory space
2007 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2008 * address.
2009 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2010 * and PRDT offset.
2011 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2012 * into local reference block.
2013 */
2014static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2015{
2016 struct utp_transfer_cmd_desc *cmd_descp;
2017 struct utp_transfer_req_desc *utrdlp;
2018 dma_addr_t cmd_desc_dma_addr;
2019 dma_addr_t cmd_desc_element_addr;
2020 u16 response_offset;
2021 u16 prdt_offset;
2022 int cmd_desc_size;
2023 int i;
2024
2025 utrdlp = hba->utrdl_base_addr;
2026 cmd_descp = hba->ucdl_base_addr;
2027
2028 response_offset =
2029 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2030 prdt_offset =
2031 offsetof(struct utp_transfer_cmd_desc, prd_table);
2032
2033 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2034 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2035
2036 for (i = 0; i < hba->nutrs; i++) {
2037 /* Configure UTRD with command descriptor base address */
2038 cmd_desc_element_addr =
2039 (cmd_desc_dma_addr + (cmd_desc_size * i));
2040 utrdlp[i].command_desc_base_addr_lo =
2041 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2042 utrdlp[i].command_desc_base_addr_hi =
2043 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2044
2045 /* Response upiu and prdt offset should be in double words */
2046 utrdlp[i].response_upiu_offset =
2047 cpu_to_le16((response_offset >> 2));
2048 utrdlp[i].prd_table_offset =
2049 cpu_to_le16((prdt_offset >> 2));
2050 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302051 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302052
2053 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302054 hba->lrb[i].ucd_req_ptr =
2055 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302056 hba->lrb[i].ucd_rsp_ptr =
2057 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2058 hba->lrb[i].ucd_prdt_ptr =
2059 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2060 }
2061}
2062
2063/**
2064 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2065 * @hba: per adapter instance
2066 *
2067 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2068 * in order to initialize the Unipro link startup procedure.
2069 * Once the Unipro links are up, the device connected to the controller
2070 * is detected.
2071 *
2072 * Returns 0 on success, non-zero value on failure
2073 */
2074static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2075{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302076 struct uic_command uic_cmd = {0};
2077 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302078
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302079 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2080
2081 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2082 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302083 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302084 "dme-link-startup: error code %d\n", ret);
2085 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302086}
2087
Yaniv Gardicad2e032015-03-31 17:37:14 +03002088static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2089{
2090 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2091 unsigned long min_sleep_time_us;
2092
2093 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2094 return;
2095
2096 /*
2097 * last_dme_cmd_tstamp will be 0 only for 1st call to
2098 * this function
2099 */
2100 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2101 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2102 } else {
2103 unsigned long delta =
2104 (unsigned long) ktime_to_us(
2105 ktime_sub(ktime_get(),
2106 hba->last_dme_cmd_tstamp));
2107
2108 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2109 min_sleep_time_us =
2110 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2111 else
2112 return; /* no more delay required */
2113 }
2114
2115 /* allow sleep for extra 50us if needed */
2116 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2117}
2118
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302119/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302120 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2121 * @hba: per adapter instance
2122 * @attr_sel: uic command argument1
2123 * @attr_set: attribute set type as uic command argument2
2124 * @mib_val: setting value as uic command argument3
2125 * @peer: indicate whether peer or local
2126 *
2127 * Returns 0 on success, non-zero value on failure
2128 */
2129int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2130 u8 attr_set, u32 mib_val, u8 peer)
2131{
2132 struct uic_command uic_cmd = {0};
2133 static const char *const action[] = {
2134 "dme-set",
2135 "dme-peer-set"
2136 };
2137 const char *set = action[!!peer];
2138 int ret;
2139
2140 uic_cmd.command = peer ?
2141 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2142 uic_cmd.argument1 = attr_sel;
2143 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2144 uic_cmd.argument3 = mib_val;
2145
2146 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2147 if (ret)
2148 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2149 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2150
2151 return ret;
2152}
2153EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2154
2155/**
2156 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2157 * @hba: per adapter instance
2158 * @attr_sel: uic command argument1
2159 * @mib_val: the value of the attribute as returned by the UIC command
2160 * @peer: indicate whether peer or local
2161 *
2162 * Returns 0 on success, non-zero value on failure
2163 */
2164int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2165 u32 *mib_val, u8 peer)
2166{
2167 struct uic_command uic_cmd = {0};
2168 static const char *const action[] = {
2169 "dme-get",
2170 "dme-peer-get"
2171 };
2172 const char *get = action[!!peer];
2173 int ret;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002174 struct ufs_pa_layer_attr orig_pwr_info;
2175 struct ufs_pa_layer_attr temp_pwr_info;
2176 bool pwr_mode_change = false;
2177
2178 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2179 orig_pwr_info = hba->pwr_info;
2180 temp_pwr_info = orig_pwr_info;
2181
2182 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2183 orig_pwr_info.pwr_rx == FAST_MODE) {
2184 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2185 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2186 pwr_mode_change = true;
2187 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2188 orig_pwr_info.pwr_rx == SLOW_MODE) {
2189 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2190 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2191 pwr_mode_change = true;
2192 }
2193 if (pwr_mode_change) {
2194 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2195 if (ret)
2196 goto out;
2197 }
2198 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302199
2200 uic_cmd.command = peer ?
2201 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2202 uic_cmd.argument1 = attr_sel;
2203
2204 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2205 if (ret) {
2206 dev_err(hba->dev, "%s: attr-id 0x%x error code %d\n",
2207 get, UIC_GET_ATTR_ID(attr_sel), ret);
2208 goto out;
2209 }
2210
2211 if (mib_val)
2212 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002213
2214 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2215 && pwr_mode_change)
2216 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302217out:
2218 return ret;
2219}
2220EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2221
2222/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002223 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2224 * state) and waits for it to take effect.
2225 *
2226 * @hba: per adapter instance
2227 * @cmd: UIC command to execute
2228 *
2229 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2230 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2231 * and device UniPro link and hence it's final completion would be indicated by
2232 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2233 * addition to normal UIC command completion Status (UCCS). This function only
2234 * returns after the relevant status bits indicate the completion.
2235 *
2236 * Returns 0 on success, non-zero value on failure
2237 */
2238static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2239{
2240 struct completion uic_async_done;
2241 unsigned long flags;
2242 u8 status;
2243 int ret;
2244
2245 mutex_lock(&hba->uic_cmd_mutex);
2246 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002247 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002248
2249 spin_lock_irqsave(hba->host->host_lock, flags);
2250 hba->uic_async_done = &uic_async_done;
2251 ret = __ufshcd_send_uic_cmd(hba, cmd);
2252 spin_unlock_irqrestore(hba->host->host_lock, flags);
2253 if (ret) {
2254 dev_err(hba->dev,
2255 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2256 cmd->command, cmd->argument3, ret);
2257 goto out;
2258 }
2259 ret = ufshcd_wait_for_uic_cmd(hba, cmd);
2260 if (ret) {
2261 dev_err(hba->dev,
2262 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2263 cmd->command, cmd->argument3, ret);
2264 goto out;
2265 }
2266
2267 if (!wait_for_completion_timeout(hba->uic_async_done,
2268 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2269 dev_err(hba->dev,
2270 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2271 cmd->command, cmd->argument3);
2272 ret = -ETIMEDOUT;
2273 goto out;
2274 }
2275
2276 status = ufshcd_get_upmcrs(hba);
2277 if (status != PWR_LOCAL) {
2278 dev_err(hba->dev,
2279 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
2280 cmd->command, status);
2281 ret = (status != PWR_OK) ? status : -1;
2282 }
2283out:
2284 spin_lock_irqsave(hba->host->host_lock, flags);
2285 hba->uic_async_done = NULL;
2286 spin_unlock_irqrestore(hba->host->host_lock, flags);
2287 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002288
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002289 return ret;
2290}
2291
2292/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302293 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2294 * using DME_SET primitives.
2295 * @hba: per adapter instance
2296 * @mode: powr mode value
2297 *
2298 * Returns 0 on success, non-zero value on failure
2299 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302300static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302301{
2302 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002303 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302304
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002305 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2306 ret = ufshcd_dme_set(hba,
2307 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2308 if (ret) {
2309 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2310 __func__, ret);
2311 goto out;
2312 }
2313 }
2314
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302315 uic_cmd.command = UIC_CMD_DME_SET;
2316 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2317 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002318 ufshcd_hold(hba, false);
2319 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2320 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302321
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002322out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002323 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002324}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302325
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002326static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2327{
2328 struct uic_command uic_cmd = {0};
2329
2330 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
2331
2332 return ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2333}
2334
2335static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2336{
2337 struct uic_command uic_cmd = {0};
2338 int ret;
2339
2340 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2341 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302342 if (ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002343 ufshcd_set_link_off(hba);
2344 ret = ufshcd_host_reset_and_restore(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302345 }
2346
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302347 return ret;
2348}
2349
Yaniv Gardi50646362014-10-23 13:25:13 +03002350 /**
2351 * ufshcd_init_pwr_info - setting the POR (power on reset)
2352 * values in hba power info
2353 * @hba: per-adapter instance
2354 */
2355static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2356{
2357 hba->pwr_info.gear_rx = UFS_PWM_G1;
2358 hba->pwr_info.gear_tx = UFS_PWM_G1;
2359 hba->pwr_info.lane_rx = 1;
2360 hba->pwr_info.lane_tx = 1;
2361 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2362 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2363 hba->pwr_info.hs_rate = 0;
2364}
2365
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302366/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002367 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2368 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302369 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002370static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302371{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002372 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2373
2374 if (hba->max_pwr_info.is_valid)
2375 return 0;
2376
2377 pwr_info->pwr_tx = FASTAUTO_MODE;
2378 pwr_info->pwr_rx = FASTAUTO_MODE;
2379 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302380
2381 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002382 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2383 &pwr_info->lane_rx);
2384 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2385 &pwr_info->lane_tx);
2386
2387 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2388 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2389 __func__,
2390 pwr_info->lane_rx,
2391 pwr_info->lane_tx);
2392 return -EINVAL;
2393 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302394
2395 /*
2396 * First, get the maximum gears of HS speed.
2397 * If a zero value, it means there is no HSGEAR capability.
2398 * Then, get the maximum gears of PWM speed.
2399 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002400 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2401 if (!pwr_info->gear_rx) {
2402 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2403 &pwr_info->gear_rx);
2404 if (!pwr_info->gear_rx) {
2405 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2406 __func__, pwr_info->gear_rx);
2407 return -EINVAL;
2408 }
2409 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302410 }
2411
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002412 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2413 &pwr_info->gear_tx);
2414 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302415 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002416 &pwr_info->gear_tx);
2417 if (!pwr_info->gear_tx) {
2418 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2419 __func__, pwr_info->gear_tx);
2420 return -EINVAL;
2421 }
2422 pwr_info->pwr_tx = SLOWAUTO_MODE;
2423 }
2424
2425 hba->max_pwr_info.is_valid = true;
2426 return 0;
2427}
2428
2429static int ufshcd_change_power_mode(struct ufs_hba *hba,
2430 struct ufs_pa_layer_attr *pwr_mode)
2431{
2432 int ret;
2433
2434 /* if already configured to the requested pwr_mode */
2435 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2436 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2437 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2438 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2439 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2440 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2441 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2442 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2443 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302444 }
2445
2446 /*
2447 * Configure attributes for power mode change with below.
2448 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2449 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2450 * - PA_HSSERIES
2451 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002452 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2453 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2454 pwr_mode->lane_rx);
2455 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2456 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302457 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002458 else
2459 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302460
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002461 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2462 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2463 pwr_mode->lane_tx);
2464 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2465 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302466 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002467 else
2468 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302469
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002470 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2471 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2472 pwr_mode->pwr_rx == FAST_MODE ||
2473 pwr_mode->pwr_tx == FAST_MODE)
2474 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2475 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302476
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002477 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2478 | pwr_mode->pwr_tx);
2479
2480 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302481 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002482 "%s: power mode change failed %d\n", __func__, ret);
2483 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002484 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2485 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002486
2487 memcpy(&hba->pwr_info, pwr_mode,
2488 sizeof(struct ufs_pa_layer_attr));
2489 }
2490
2491 return ret;
2492}
2493
2494/**
2495 * ufshcd_config_pwr_mode - configure a new power mode
2496 * @hba: per-adapter instance
2497 * @desired_pwr_mode: desired power configuration
2498 */
2499static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2500 struct ufs_pa_layer_attr *desired_pwr_mode)
2501{
2502 struct ufs_pa_layer_attr final_params = { 0 };
2503 int ret;
2504
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002505 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2506 desired_pwr_mode, &final_params);
2507
2508 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002509 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2510
2511 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302512
2513 return ret;
2514}
2515
2516/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302517 * ufshcd_complete_dev_init() - checks device readiness
2518 * hba: per-adapter instance
2519 *
2520 * Set fDeviceInit flag and poll until device toggles it.
2521 */
2522static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2523{
2524 int i, retries, err = 0;
2525 bool flag_res = 1;
2526
2527 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2528 /* Set the fDeviceInit flag */
2529 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2530 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
2531 if (!err || err == -ETIMEDOUT)
2532 break;
2533 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2534 }
2535 if (err) {
2536 dev_err(hba->dev,
2537 "%s setting fDeviceInit flag failed with error %d\n",
2538 __func__, err);
2539 goto out;
2540 }
2541
2542 /* poll for max. 100 iterations for fDeviceInit flag to clear */
2543 for (i = 0; i < 100 && !err && flag_res; i++) {
2544 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2545 err = ufshcd_query_flag(hba,
2546 UPIU_QUERY_OPCODE_READ_FLAG,
2547 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2548 if (!err || err == -ETIMEDOUT)
2549 break;
2550 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
2551 err);
2552 }
2553 }
2554 if (err)
2555 dev_err(hba->dev,
2556 "%s reading fDeviceInit flag failed with error %d\n",
2557 __func__, err);
2558 else if (flag_res)
2559 dev_err(hba->dev,
2560 "%s fDeviceInit was not cleared by the device\n",
2561 __func__);
2562
2563out:
2564 return err;
2565}
2566
2567/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302568 * ufshcd_make_hba_operational - Make UFS controller operational
2569 * @hba: per adapter instance
2570 *
2571 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002572 * 1. Enable required interrupts
2573 * 2. Configure interrupt aggregation
2574 * 3. Program UTRL and UTMRL base addres
2575 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302576 *
2577 * Returns 0 on success, non-zero value on failure
2578 */
2579static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2580{
2581 int err = 0;
2582 u32 reg;
2583
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302584 /* Enable required interrupts */
2585 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2586
2587 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002588 if (ufshcd_is_intr_aggr_allowed(hba))
2589 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2590 else
2591 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302592
2593 /* Configure UTRL and UTMRL base address registers */
2594 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2595 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2596 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2597 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2598 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2599 REG_UTP_TASK_REQ_LIST_BASE_L);
2600 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2601 REG_UTP_TASK_REQ_LIST_BASE_H);
2602
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302603 /*
2604 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
2605 * DEI, HEI bits must be 0
2606 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002607 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302608 if (!(ufshcd_get_lists_status(reg))) {
2609 ufshcd_enable_run_stop_reg(hba);
2610 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302611 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302612 "Host controller not ready to process requests");
2613 err = -EIO;
2614 goto out;
2615 }
2616
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302617out:
2618 return err;
2619}
2620
2621/**
2622 * ufshcd_hba_enable - initialize the controller
2623 * @hba: per adapter instance
2624 *
2625 * The controller resets itself and controller firmware initialization
2626 * sequence kicks off. When controller is ready it will set
2627 * the Host Controller Enable bit to 1.
2628 *
2629 * Returns 0 on success, non-zero value on failure
2630 */
2631static int ufshcd_hba_enable(struct ufs_hba *hba)
2632{
2633 int retry;
2634
2635 /*
2636 * msleep of 1 and 5 used in this function might result in msleep(20),
2637 * but it was necessary to send the UFS FPGA to reset mode during
2638 * development and testing of this driver. msleep can be changed to
2639 * mdelay and retry count can be reduced based on the controller.
2640 */
2641 if (!ufshcd_is_hba_active(hba)) {
2642
2643 /* change controller state to "reset state" */
2644 ufshcd_hba_stop(hba);
2645
2646 /*
2647 * This delay is based on the testing done with UFS host
2648 * controller FPGA. The delay can be changed based on the
2649 * host controller used.
2650 */
2651 msleep(5);
2652 }
2653
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002654 /* UniPro link is disabled at this point */
2655 ufshcd_set_link_off(hba);
2656
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002657 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002658
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302659 /* start controller initialization sequence */
2660 ufshcd_hba_start(hba);
2661
2662 /*
2663 * To initialize a UFS host controller HCE bit must be set to 1.
2664 * During initialization the HCE bit value changes from 1->0->1.
2665 * When the host controller completes initialization sequence
2666 * it sets the value of HCE bit to 1. The same HCE bit is read back
2667 * to check if the controller has completed initialization sequence.
2668 * So without this delay the value HCE = 1, set in the previous
2669 * instruction might be read back.
2670 * This delay can be changed based on the controller.
2671 */
2672 msleep(1);
2673
2674 /* wait for the host controller to complete initialization */
2675 retry = 10;
2676 while (ufshcd_is_hba_active(hba)) {
2677 if (retry) {
2678 retry--;
2679 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302680 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302681 "Controller enable failed\n");
2682 return -EIO;
2683 }
2684 msleep(5);
2685 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002686
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002687 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002688 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002689
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002690 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002691
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302692 return 0;
2693}
2694
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002695static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
2696{
2697 int tx_lanes, i, err = 0;
2698
2699 if (!peer)
2700 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2701 &tx_lanes);
2702 else
2703 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2704 &tx_lanes);
2705 for (i = 0; i < tx_lanes; i++) {
2706 if (!peer)
2707 err = ufshcd_dme_set(hba,
2708 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2709 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2710 0);
2711 else
2712 err = ufshcd_dme_peer_set(hba,
2713 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2714 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2715 0);
2716 if (err) {
2717 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
2718 __func__, peer, i, err);
2719 break;
2720 }
2721 }
2722
2723 return err;
2724}
2725
2726static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
2727{
2728 return ufshcd_disable_tx_lcc(hba, true);
2729}
2730
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302731/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302732 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302733 * @hba: per adapter instance
2734 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302735 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302736 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302737static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302738{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302739 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002740 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302741
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002742 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002743 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302744
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002745 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002746
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002747 /* check if device is detected by inter-connect layer */
2748 if (!ret && !ufshcd_is_device_present(hba)) {
2749 dev_err(hba->dev, "%s: Device not present\n", __func__);
2750 ret = -ENXIO;
2751 goto out;
2752 }
2753
2754 /*
2755 * DME link lost indication is only received when link is up,
2756 * but we can't be sure if the link is up until link startup
2757 * succeeds. So reset the local Uni-Pro and try again.
2758 */
2759 if (ret && ufshcd_hba_enable(hba))
2760 goto out;
2761 } while (ret && retries--);
2762
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302763 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002764 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302765 goto out;
2766
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002767 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
2768 ret = ufshcd_disable_device_tx_lcc(hba);
2769 if (ret)
2770 goto out;
2771 }
2772
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002773 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002774 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
2775 if (ret)
2776 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002777
2778 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302779out:
2780 if (ret)
2781 dev_err(hba->dev, "link startup failed %d\n", ret);
2782 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302783}
2784
2785/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302786 * ufshcd_verify_dev_init() - Verify device initialization
2787 * @hba: per-adapter instance
2788 *
2789 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
2790 * device Transport Protocol (UTP) layer is ready after a reset.
2791 * If the UTP layer at the device side is not initialized, it may
2792 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
2793 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
2794 */
2795static int ufshcd_verify_dev_init(struct ufs_hba *hba)
2796{
2797 int err = 0;
2798 int retries;
2799
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002800 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302801 mutex_lock(&hba->dev_cmd.lock);
2802 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
2803 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
2804 NOP_OUT_TIMEOUT);
2805
2806 if (!err || err == -ETIMEDOUT)
2807 break;
2808
2809 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2810 }
2811 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002812 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302813
2814 if (err)
2815 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2816 return err;
2817}
2818
2819/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002820 * ufshcd_set_queue_depth - set lun queue depth
2821 * @sdev: pointer to SCSI device
2822 *
2823 * Read bLUQueueDepth value and activate scsi tagged command
2824 * queueing. For WLUN, queue depth is set to 1. For best-effort
2825 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
2826 * value that host can queue.
2827 */
2828static void ufshcd_set_queue_depth(struct scsi_device *sdev)
2829{
2830 int ret = 0;
2831 u8 lun_qdepth;
2832 struct ufs_hba *hba;
2833
2834 hba = shost_priv(sdev->host);
2835
2836 lun_qdepth = hba->nutrs;
2837 ret = ufshcd_read_unit_desc_param(hba,
2838 ufshcd_scsi_to_upiu_lun(sdev->lun),
2839 UNIT_DESC_PARAM_LU_Q_DEPTH,
2840 &lun_qdepth,
2841 sizeof(lun_qdepth));
2842
2843 /* Some WLUN doesn't support unit descriptor */
2844 if (ret == -EOPNOTSUPP)
2845 lun_qdepth = 1;
2846 else if (!lun_qdepth)
2847 /* eventually, we can figure out the real queue depth */
2848 lun_qdepth = hba->nutrs;
2849 else
2850 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2851
2852 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2853 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002854 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002855}
2856
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002857/*
2858 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
2859 * @hba: per-adapter instance
2860 * @lun: UFS device lun id
2861 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
2862 *
2863 * Returns 0 in case of success and b_lu_write_protect status would be returned
2864 * @b_lu_write_protect parameter.
2865 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
2866 * Returns -EINVAL in case of invalid parameters passed to this function.
2867 */
2868static int ufshcd_get_lu_wp(struct ufs_hba *hba,
2869 u8 lun,
2870 u8 *b_lu_write_protect)
2871{
2872 int ret;
2873
2874 if (!b_lu_write_protect)
2875 ret = -EINVAL;
2876 /*
2877 * According to UFS device spec, RPMB LU can't be write
2878 * protected so skip reading bLUWriteProtect parameter for
2879 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
2880 */
2881 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
2882 ret = -ENOTSUPP;
2883 else
2884 ret = ufshcd_read_unit_desc_param(hba,
2885 lun,
2886 UNIT_DESC_PARAM_LU_WR_PROTECT,
2887 b_lu_write_protect,
2888 sizeof(*b_lu_write_protect));
2889 return ret;
2890}
2891
2892/**
2893 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
2894 * status
2895 * @hba: per-adapter instance
2896 * @sdev: pointer to SCSI device
2897 *
2898 */
2899static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
2900 struct scsi_device *sdev)
2901{
2902 if (hba->dev_info.f_power_on_wp_en &&
2903 !hba->dev_info.is_lu_power_on_wp) {
2904 u8 b_lu_write_protect;
2905
2906 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
2907 &b_lu_write_protect) &&
2908 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
2909 hba->dev_info.is_lu_power_on_wp = true;
2910 }
2911}
2912
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002913/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302914 * ufshcd_slave_alloc - handle initial SCSI device configurations
2915 * @sdev: pointer to SCSI device
2916 *
2917 * Returns success
2918 */
2919static int ufshcd_slave_alloc(struct scsi_device *sdev)
2920{
2921 struct ufs_hba *hba;
2922
2923 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302924
2925 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
2926 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302927
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302928 /* allow SCSI layer to restart the device in case of errors */
2929 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002930
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03002931 /* REPORT SUPPORTED OPERATION CODES is not supported */
2932 sdev->no_report_opcodes = 1;
2933
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002934
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002935 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002936
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002937 ufshcd_get_lu_power_on_wp_status(hba, sdev);
2938
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002939 return 0;
2940}
2941
2942/**
2943 * ufshcd_change_queue_depth - change queue depth
2944 * @sdev: pointer to SCSI device
2945 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002946 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002947 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002948 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002949static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002950{
2951 struct ufs_hba *hba = shost_priv(sdev->host);
2952
2953 if (depth > hba->nutrs)
2954 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002955 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302956}
2957
2958/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09002959 * ufshcd_slave_configure - adjust SCSI device configurations
2960 * @sdev: pointer to SCSI device
2961 */
2962static int ufshcd_slave_configure(struct scsi_device *sdev)
2963{
2964 struct request_queue *q = sdev->request_queue;
2965
2966 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
2967 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
2968
2969 return 0;
2970}
2971
2972/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302973 * ufshcd_slave_destroy - remove SCSI device configurations
2974 * @sdev: pointer to SCSI device
2975 */
2976static void ufshcd_slave_destroy(struct scsi_device *sdev)
2977{
2978 struct ufs_hba *hba;
2979
2980 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002981 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03002982 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
2983 unsigned long flags;
2984
2985 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002986 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03002987 spin_unlock_irqrestore(hba->host->host_lock, flags);
2988 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302989}
2990
2991/**
2992 * ufshcd_task_req_compl - handle task management request completion
2993 * @hba: per adapter instance
2994 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302995 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302996 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302997 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302998 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302999static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303000{
3001 struct utp_task_req_desc *task_req_descp;
3002 struct utp_upiu_task_rsp *task_rsp_upiup;
3003 unsigned long flags;
3004 int ocs_value;
3005 int task_result;
3006
3007 spin_lock_irqsave(hba->host->host_lock, flags);
3008
3009 /* Clear completed tasks from outstanding_tasks */
3010 __clear_bit(index, &hba->outstanding_tasks);
3011
3012 task_req_descp = hba->utmrdl_base_addr;
3013 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3014
3015 if (ocs_value == OCS_SUCCESS) {
3016 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3017 task_req_descp[index].task_rsp_upiu;
3018 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
3019 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303020 if (resp)
3021 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303022 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303023 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3024 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303025 }
3026 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303027
3028 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303029}
3030
3031/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303032 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3033 * @lrb: pointer to local reference block of completed command
3034 * @scsi_status: SCSI command status
3035 *
3036 * Returns value base on SCSI command status
3037 */
3038static inline int
3039ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3040{
3041 int result = 0;
3042
3043 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303044 case SAM_STAT_CHECK_CONDITION:
3045 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303046 case SAM_STAT_GOOD:
3047 result |= DID_OK << 16 |
3048 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303049 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303050 break;
3051 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303052 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303053 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303054 ufshcd_copy_sense_data(lrbp);
3055 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303056 break;
3057 default:
3058 result |= DID_ERROR << 16;
3059 break;
3060 } /* end of switch */
3061
3062 return result;
3063}
3064
3065/**
3066 * ufshcd_transfer_rsp_status - Get overall status of the response
3067 * @hba: per adapter instance
3068 * @lrb: pointer to local reference block of completed command
3069 *
3070 * Returns result of the command to notify SCSI midlayer
3071 */
3072static inline int
3073ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3074{
3075 int result = 0;
3076 int scsi_status;
3077 int ocs;
3078
3079 /* overall command status of utrd */
3080 ocs = ufshcd_get_tr_ocs(lrbp);
3081
3082 switch (ocs) {
3083 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303084 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303085
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303086 switch (result) {
3087 case UPIU_TRANSACTION_RESPONSE:
3088 /*
3089 * get the response UPIU result to extract
3090 * the SCSI command status
3091 */
3092 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3093
3094 /*
3095 * get the result based on SCSI status response
3096 * to notify the SCSI midlayer of the command status
3097 */
3098 scsi_status = result & MASK_SCSI_STATUS;
3099 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303100
3101 if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
3102 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303103 break;
3104 case UPIU_TRANSACTION_REJECT_UPIU:
3105 /* TODO: handle Reject UPIU Response */
3106 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303107 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303108 "Reject UPIU not fully implemented\n");
3109 break;
3110 default:
3111 result = DID_ERROR << 16;
3112 dev_err(hba->dev,
3113 "Unexpected request response code = %x\n",
3114 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303115 break;
3116 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303117 break;
3118 case OCS_ABORTED:
3119 result |= DID_ABORT << 16;
3120 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303121 case OCS_INVALID_COMMAND_STATUS:
3122 result |= DID_REQUEUE << 16;
3123 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303124 case OCS_INVALID_CMD_TABLE_ATTR:
3125 case OCS_INVALID_PRDT_ATTR:
3126 case OCS_MISMATCH_DATA_BUF_SIZE:
3127 case OCS_MISMATCH_RESP_UPIU_SIZE:
3128 case OCS_PEER_COMM_FAILURE:
3129 case OCS_FATAL_ERROR:
3130 default:
3131 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303132 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303133 "OCS error from controller = %x\n", ocs);
3134 break;
3135 } /* end of switch */
3136
3137 return result;
3138}
3139
3140/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303141 * ufshcd_uic_cmd_compl - handle completion of uic command
3142 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303143 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303144 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303145static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303146{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303147 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303148 hba->active_uic_cmd->argument2 |=
3149 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303150 hba->active_uic_cmd->argument3 =
3151 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303152 complete(&hba->active_uic_cmd->done);
3153 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303154
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003155 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3156 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303157}
3158
3159/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303160 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3161 * @hba: per adapter instance
3162 */
3163static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3164{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303165 struct ufshcd_lrb *lrbp;
3166 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303167 unsigned long completed_reqs;
3168 u32 tr_doorbell;
3169 int result;
3170 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003171
3172 /* Resetting interrupt aggregation counters first and reading the
3173 * DOOR_BELL afterward allows us to handle all the completed requests.
3174 * In order to prevent other interrupts starvation the DB is read once
3175 * after reset. The down side of this solution is the possibility of
3176 * false interrupt if device completes another request after resetting
3177 * aggregation and before reading the DB.
3178 */
Yaniv Gardib8521902015-05-17 18:54:57 +03003179 if (ufshcd_is_intr_aggr_allowed(hba))
3180 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303181
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303182 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303183 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3184
Dolev Ravive9d501b2014-07-01 12:22:37 +03003185 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3186 lrbp = &hba->lrb[index];
3187 cmd = lrbp->cmd;
3188 if (cmd) {
3189 result = ufshcd_transfer_rsp_status(hba, lrbp);
3190 scsi_dma_unmap(cmd);
3191 cmd->result = result;
3192 /* Mark completed command as NULL in LRB */
3193 lrbp->cmd = NULL;
3194 clear_bit_unlock(index, &hba->lrb_in_use);
3195 /* Do not touch lrbp after scsi done */
3196 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003197 __ufshcd_release(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003198 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
3199 if (hba->dev_cmd.complete)
3200 complete(hba->dev_cmd.complete);
3201 }
3202 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303203
3204 /* clear corresponding bits of completed commands */
3205 hba->outstanding_reqs ^= completed_reqs;
3206
Sahitya Tummala856b3482014-09-25 15:32:34 +03003207 ufshcd_clk_scaling_update_busy(hba);
3208
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303209 /* we might have free'd some tags above */
3210 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303211}
3212
3213/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303214 * ufshcd_disable_ee - disable exception event
3215 * @hba: per-adapter instance
3216 * @mask: exception event to disable
3217 *
3218 * Disables exception event in the device so that the EVENT_ALERT
3219 * bit is not set.
3220 *
3221 * Returns zero on success, non-zero error value on failure.
3222 */
3223static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3224{
3225 int err = 0;
3226 u32 val;
3227
3228 if (!(hba->ee_ctrl_mask & mask))
3229 goto out;
3230
3231 val = hba->ee_ctrl_mask & ~mask;
3232 val &= 0xFFFF; /* 2 bytes */
3233 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3234 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3235 if (!err)
3236 hba->ee_ctrl_mask &= ~mask;
3237out:
3238 return err;
3239}
3240
3241/**
3242 * ufshcd_enable_ee - enable exception event
3243 * @hba: per-adapter instance
3244 * @mask: exception event to enable
3245 *
3246 * Enable corresponding exception event in the device to allow
3247 * device to alert host in critical scenarios.
3248 *
3249 * Returns zero on success, non-zero error value on failure.
3250 */
3251static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3252{
3253 int err = 0;
3254 u32 val;
3255
3256 if (hba->ee_ctrl_mask & mask)
3257 goto out;
3258
3259 val = hba->ee_ctrl_mask | mask;
3260 val &= 0xFFFF; /* 2 bytes */
3261 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3262 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3263 if (!err)
3264 hba->ee_ctrl_mask |= mask;
3265out:
3266 return err;
3267}
3268
3269/**
3270 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3271 * @hba: per-adapter instance
3272 *
3273 * Allow device to manage background operations on its own. Enabling
3274 * this might lead to inconsistent latencies during normal data transfers
3275 * as the device is allowed to manage its own way of handling background
3276 * operations.
3277 *
3278 * Returns zero on success, non-zero on failure.
3279 */
3280static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3281{
3282 int err = 0;
3283
3284 if (hba->auto_bkops_enabled)
3285 goto out;
3286
3287 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3288 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3289 if (err) {
3290 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3291 __func__, err);
3292 goto out;
3293 }
3294
3295 hba->auto_bkops_enabled = true;
3296
3297 /* No need of URGENT_BKOPS exception from the device */
3298 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3299 if (err)
3300 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3301 __func__, err);
3302out:
3303 return err;
3304}
3305
3306/**
3307 * ufshcd_disable_auto_bkops - block device in doing background operations
3308 * @hba: per-adapter instance
3309 *
3310 * Disabling background operations improves command response latency but
3311 * has drawback of device moving into critical state where the device is
3312 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3313 * host is idle so that BKOPS are managed effectively without any negative
3314 * impacts.
3315 *
3316 * Returns zero on success, non-zero on failure.
3317 */
3318static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3319{
3320 int err = 0;
3321
3322 if (!hba->auto_bkops_enabled)
3323 goto out;
3324
3325 /*
3326 * If host assisted BKOPs is to be enabled, make sure
3327 * urgent bkops exception is allowed.
3328 */
3329 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3330 if (err) {
3331 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3332 __func__, err);
3333 goto out;
3334 }
3335
3336 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
3337 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3338 if (err) {
3339 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3340 __func__, err);
3341 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3342 goto out;
3343 }
3344
3345 hba->auto_bkops_enabled = false;
3346out:
3347 return err;
3348}
3349
3350/**
3351 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3352 * @hba: per adapter instance
3353 *
3354 * After a device reset the device may toggle the BKOPS_EN flag
3355 * to default value. The s/w tracking variables should be updated
3356 * as well. Do this by forcing enable of auto bkops.
3357 */
3358static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3359{
3360 hba->auto_bkops_enabled = false;
3361 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3362 ufshcd_enable_auto_bkops(hba);
3363}
3364
3365static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3366{
3367 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3368 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3369}
3370
3371/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003372 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3373 * @hba: per-adapter instance
3374 * @status: bkops_status value
3375 *
3376 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3377 * flag in the device to permit background operations if the device
3378 * bkops_status is greater than or equal to "status" argument passed to
3379 * this function, disable otherwise.
3380 *
3381 * Returns 0 for success, non-zero in case of failure.
3382 *
3383 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3384 * to know whether auto bkops is enabled or disabled after this function
3385 * returns control to it.
3386 */
3387static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3388 enum bkops_status status)
3389{
3390 int err;
3391 u32 curr_status = 0;
3392
3393 err = ufshcd_get_bkops_status(hba, &curr_status);
3394 if (err) {
3395 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3396 __func__, err);
3397 goto out;
3398 } else if (curr_status > BKOPS_STATUS_MAX) {
3399 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3400 __func__, curr_status);
3401 err = -EINVAL;
3402 goto out;
3403 }
3404
3405 if (curr_status >= status)
3406 err = ufshcd_enable_auto_bkops(hba);
3407 else
3408 err = ufshcd_disable_auto_bkops(hba);
3409out:
3410 return err;
3411}
3412
3413/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303414 * ufshcd_urgent_bkops - handle urgent bkops exception event
3415 * @hba: per-adapter instance
3416 *
3417 * Enable fBackgroundOpsEn flag in the device to permit background
3418 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003419 *
3420 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3421 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303422 */
3423static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3424{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003425 return ufshcd_bkops_ctrl(hba, BKOPS_STATUS_PERF_IMPACT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303426}
3427
3428static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3429{
3430 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3431 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3432}
3433
3434/**
3435 * ufshcd_exception_event_handler - handle exceptions raised by device
3436 * @work: pointer to work data
3437 *
3438 * Read bExceptionEventStatus attribute from the device and handle the
3439 * exception event accordingly.
3440 */
3441static void ufshcd_exception_event_handler(struct work_struct *work)
3442{
3443 struct ufs_hba *hba;
3444 int err;
3445 u32 status = 0;
3446 hba = container_of(work, struct ufs_hba, eeh_work);
3447
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303448 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303449 err = ufshcd_get_ee_status(hba, &status);
3450 if (err) {
3451 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3452 __func__, err);
3453 goto out;
3454 }
3455
3456 status &= hba->ee_ctrl_mask;
3457 if (status & MASK_EE_URGENT_BKOPS) {
3458 err = ufshcd_urgent_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003459 if (err < 0)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303460 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3461 __func__, err);
3462 }
3463out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303464 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303465 return;
3466}
3467
3468/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303469 * ufshcd_err_handler - handle UFS errors that require s/w attention
3470 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303471 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303472static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303473{
3474 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303475 unsigned long flags;
3476 u32 err_xfer = 0;
3477 u32 err_tm = 0;
3478 int err = 0;
3479 int tag;
3480
3481 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303482
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303483 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003484 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303485
3486 spin_lock_irqsave(hba->host->host_lock, flags);
3487 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
3488 spin_unlock_irqrestore(hba->host->host_lock, flags);
3489 goto out;
3490 }
3491
3492 hba->ufshcd_state = UFSHCD_STATE_RESET;
3493 ufshcd_set_eh_in_progress(hba);
3494
3495 /* Complete requests that have door-bell cleared by h/w */
3496 ufshcd_transfer_req_compl(hba);
3497 ufshcd_tmc_handler(hba);
3498 spin_unlock_irqrestore(hba->host->host_lock, flags);
3499
3500 /* Clear pending transfer requests */
3501 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
3502 if (ufshcd_clear_cmd(hba, tag))
3503 err_xfer |= 1 << tag;
3504
3505 /* Clear pending task management requests */
3506 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
3507 if (ufshcd_clear_tm_cmd(hba, tag))
3508 err_tm |= 1 << tag;
3509
3510 /* Complete the requests that are cleared by s/w */
3511 spin_lock_irqsave(hba->host->host_lock, flags);
3512 ufshcd_transfer_req_compl(hba);
3513 ufshcd_tmc_handler(hba);
3514 spin_unlock_irqrestore(hba->host->host_lock, flags);
3515
3516 /* Fatal errors need reset */
3517 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
3518 ((hba->saved_err & UIC_ERROR) &&
3519 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
3520 err = ufshcd_reset_and_restore(hba);
3521 if (err) {
3522 dev_err(hba->dev, "%s: reset and restore failed\n",
3523 __func__);
3524 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3525 }
3526 /*
3527 * Inform scsi mid-layer that we did reset and allow to handle
3528 * Unit Attention properly.
3529 */
3530 scsi_report_bus_reset(hba->host, 0);
3531 hba->saved_err = 0;
3532 hba->saved_uic_err = 0;
3533 }
3534 ufshcd_clear_eh_in_progress(hba);
3535
3536out:
3537 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003538 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303539 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303540}
3541
3542/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303543 * ufshcd_update_uic_error - check and set fatal UIC error flags.
3544 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303545 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303546static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303547{
3548 u32 reg;
3549
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303550 /* PA_INIT_ERROR is fatal and needs UIC reset */
3551 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
3552 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
3553 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
3554
3555 /* UIC NL/TL/DME errors needs software retry */
3556 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
3557 if (reg)
3558 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
3559
3560 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
3561 if (reg)
3562 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
3563
3564 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
3565 if (reg)
3566 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
3567
3568 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
3569 __func__, hba->uic_error);
3570}
3571
3572/**
3573 * ufshcd_check_errors - Check for errors that need s/w attention
3574 * @hba: per-adapter instance
3575 */
3576static void ufshcd_check_errors(struct ufs_hba *hba)
3577{
3578 bool queue_eh_work = false;
3579
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303580 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303581 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303582
3583 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303584 hba->uic_error = 0;
3585 ufshcd_update_uic_error(hba);
3586 if (hba->uic_error)
3587 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303588 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303589
3590 if (queue_eh_work) {
3591 /* handle fatal errors only when link is functional */
3592 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
3593 /* block commands from scsi mid-layer */
3594 scsi_block_requests(hba->host);
3595
3596 /* transfer error masks to sticky bits */
3597 hba->saved_err |= hba->errors;
3598 hba->saved_uic_err |= hba->uic_error;
3599
3600 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3601 schedule_work(&hba->eh_work);
3602 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303603 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303604 /*
3605 * if (!queue_eh_work) -
3606 * Other errors are either non-fatal where host recovers
3607 * itself without s/w intervention or errors that will be
3608 * handled by the SCSI core layer.
3609 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303610}
3611
3612/**
3613 * ufshcd_tmc_handler - handle task management function completion
3614 * @hba: per adapter instance
3615 */
3616static void ufshcd_tmc_handler(struct ufs_hba *hba)
3617{
3618 u32 tm_doorbell;
3619
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303620 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303621 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303622 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303623}
3624
3625/**
3626 * ufshcd_sl_intr - Interrupt service routine
3627 * @hba: per adapter instance
3628 * @intr_status: contains interrupts generated by the controller
3629 */
3630static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
3631{
3632 hba->errors = UFSHCD_ERROR_MASK & intr_status;
3633 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303634 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303635
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303636 if (intr_status & UFSHCD_UIC_MASK)
3637 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303638
3639 if (intr_status & UTP_TASK_REQ_COMPL)
3640 ufshcd_tmc_handler(hba);
3641
3642 if (intr_status & UTP_TRANSFER_REQ_COMPL)
3643 ufshcd_transfer_req_compl(hba);
3644}
3645
3646/**
3647 * ufshcd_intr - Main interrupt service routine
3648 * @irq: irq number
3649 * @__hba: pointer to adapter instance
3650 *
3651 * Returns IRQ_HANDLED - If interrupt is valid
3652 * IRQ_NONE - If invalid interrupt
3653 */
3654static irqreturn_t ufshcd_intr(int irq, void *__hba)
3655{
3656 u32 intr_status;
3657 irqreturn_t retval = IRQ_NONE;
3658 struct ufs_hba *hba = __hba;
3659
3660 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303661 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303662
3663 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05303664 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303665 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303666 retval = IRQ_HANDLED;
3667 }
3668 spin_unlock(hba->host->host_lock);
3669 return retval;
3670}
3671
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303672static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
3673{
3674 int err = 0;
3675 u32 mask = 1 << tag;
3676 unsigned long flags;
3677
3678 if (!test_bit(tag, &hba->outstanding_tasks))
3679 goto out;
3680
3681 spin_lock_irqsave(hba->host->host_lock, flags);
3682 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
3683 spin_unlock_irqrestore(hba->host->host_lock, flags);
3684
3685 /* poll for max. 1 sec to clear door bell register by h/w */
3686 err = ufshcd_wait_for_register(hba,
3687 REG_UTP_TASK_REQ_DOOR_BELL,
3688 mask, 0, 1000, 1000);
3689out:
3690 return err;
3691}
3692
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303693/**
3694 * ufshcd_issue_tm_cmd - issues task management commands to controller
3695 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303696 * @lun_id: LUN ID to which TM command is sent
3697 * @task_id: task ID to which the TM command is applicable
3698 * @tm_function: task management function opcode
3699 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303700 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303701 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303702 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303703static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
3704 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303705{
3706 struct utp_task_req_desc *task_req_descp;
3707 struct utp_upiu_task_req *task_req_upiup;
3708 struct Scsi_Host *host;
3709 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303710 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303711 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303712 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303713
3714 host = hba->host;
3715
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303716 /*
3717 * Get free slot, sleep if slots are unavailable.
3718 * Even though we use wait_event() which sleeps indefinitely,
3719 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
3720 */
3721 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003722 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303723
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303724 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303725 task_req_descp = hba->utmrdl_base_addr;
3726 task_req_descp += free_slot;
3727
3728 /* Configure task request descriptor */
3729 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
3730 task_req_descp->header.dword_2 =
3731 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
3732
3733 /* Configure task request UPIU */
3734 task_req_upiup =
3735 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303736 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303737 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303738 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303739 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303740 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303741 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003742 /*
3743 * The host shall provide the same value for LUN field in the basic
3744 * header and for Input Parameter.
3745 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303746 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
3747 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303748
3749 /* send command to the controller */
3750 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303751 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303752
3753 spin_unlock_irqrestore(host->host_lock, flags);
3754
3755 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303756 err = wait_event_timeout(hba->tm_wq,
3757 test_bit(free_slot, &hba->tm_condition),
3758 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303759 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303760 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
3761 __func__, tm_function);
3762 if (ufshcd_clear_tm_cmd(hba, free_slot))
3763 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
3764 __func__, free_slot);
3765 err = -ETIMEDOUT;
3766 } else {
3767 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303768 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303769
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303770 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303771 ufshcd_put_tm_slot(hba, free_slot);
3772 wake_up(&hba->tm_tag_wq);
3773
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003774 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303775 return err;
3776}
3777
3778/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303779 * ufshcd_eh_device_reset_handler - device reset handler registered to
3780 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303781 * @cmd: SCSI command pointer
3782 *
3783 * Returns SUCCESS/FAILED
3784 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303785static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303786{
3787 struct Scsi_Host *host;
3788 struct ufs_hba *hba;
3789 unsigned int tag;
3790 u32 pos;
3791 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303792 u8 resp = 0xF;
3793 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303794 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303795
3796 host = cmd->device->host;
3797 hba = shost_priv(host);
3798 tag = cmd->request->tag;
3799
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303800 lrbp = &hba->lrb[tag];
3801 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
3802 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303803 if (!err)
3804 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303805 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303806 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303807
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303808 /* clear the commands that were pending for corresponding LUN */
3809 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
3810 if (hba->lrb[pos].lun == lrbp->lun) {
3811 err = ufshcd_clear_cmd(hba, pos);
3812 if (err)
3813 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303814 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303815 }
3816 spin_lock_irqsave(host->host_lock, flags);
3817 ufshcd_transfer_req_compl(hba);
3818 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303819out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303820 if (!err) {
3821 err = SUCCESS;
3822 } else {
3823 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3824 err = FAILED;
3825 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303826 return err;
3827}
3828
3829/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303830 * ufshcd_abort - abort a specific command
3831 * @cmd: SCSI command pointer
3832 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303833 * Abort the pending command in device by sending UFS_ABORT_TASK task management
3834 * command, and in host controller by clearing the door-bell register. There can
3835 * be race between controller sending the command to the device while abort is
3836 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
3837 * really issued and then try to abort it.
3838 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303839 * Returns SUCCESS/FAILED
3840 */
3841static int ufshcd_abort(struct scsi_cmnd *cmd)
3842{
3843 struct Scsi_Host *host;
3844 struct ufs_hba *hba;
3845 unsigned long flags;
3846 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303847 int err = 0;
3848 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303849 u8 resp = 0xF;
3850 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003851 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303852
3853 host = cmd->device->host;
3854 hba = shost_priv(host);
3855 tag = cmd->request->tag;
3856
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003857 ufshcd_hold(hba, false);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303858 /* If command is already aborted/completed, return SUCCESS */
3859 if (!(test_bit(tag, &hba->outstanding_reqs)))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303860 goto out;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303861
Dolev Ravive9d501b2014-07-01 12:22:37 +03003862 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3863 if (!(reg & (1 << tag))) {
3864 dev_err(hba->dev,
3865 "%s: cmd was completed, but without a notifying intr, tag = %d",
3866 __func__, tag);
3867 }
3868
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303869 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303870 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
3871 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3872 UFS_QUERY_TASK, &resp);
3873 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
3874 /* cmd pending in the device */
3875 break;
3876 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303877 /*
3878 * cmd not pending in the device, check if it is
3879 * in transition.
3880 */
3881 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3882 if (reg & (1 << tag)) {
3883 /* sleep for max. 200us to stabilize */
3884 usleep_range(100, 200);
3885 continue;
3886 }
3887 /* command completed already */
3888 goto out;
3889 } else {
3890 if (!err)
3891 err = resp; /* service response error */
3892 goto out;
3893 }
3894 }
3895
3896 if (!poll_cnt) {
3897 err = -EBUSY;
3898 goto out;
3899 }
3900
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303901 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3902 UFS_ABORT_TASK, &resp);
3903 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303904 if (!err)
3905 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303906 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303907 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303908
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303909 err = ufshcd_clear_cmd(hba, tag);
3910 if (err)
3911 goto out;
3912
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303913 scsi_dma_unmap(cmd);
3914
3915 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303916 __clear_bit(tag, &hba->outstanding_reqs);
3917 hba->lrb[tag].cmd = NULL;
3918 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303919
3920 clear_bit_unlock(tag, &hba->lrb_in_use);
3921 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003922
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303923out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303924 if (!err) {
3925 err = SUCCESS;
3926 } else {
3927 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3928 err = FAILED;
3929 }
3930
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003931 /*
3932 * This ufshcd_release() corresponds to the original scsi cmd that got
3933 * aborted here (as we won't get any IRQ for it).
3934 */
3935 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303936 return err;
3937}
3938
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303939/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303940 * ufshcd_host_reset_and_restore - reset and restore host controller
3941 * @hba: per-adapter instance
3942 *
3943 * Note that host controller reset may issue DME_RESET to
3944 * local and remote (device) Uni-Pro stack and the attributes
3945 * are reset to default state.
3946 *
3947 * Returns zero on success, non-zero on failure
3948 */
3949static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
3950{
3951 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303952 unsigned long flags;
3953
3954 /* Reset the host controller */
3955 spin_lock_irqsave(hba->host->host_lock, flags);
3956 ufshcd_hba_stop(hba);
3957 spin_unlock_irqrestore(hba->host->host_lock, flags);
3958
3959 err = ufshcd_hba_enable(hba);
3960 if (err)
3961 goto out;
3962
3963 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003964 err = ufshcd_probe_hba(hba);
3965
3966 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303967 err = -EIO;
3968out:
3969 if (err)
3970 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
3971
3972 return err;
3973}
3974
3975/**
3976 * ufshcd_reset_and_restore - reset and re-initialize host/device
3977 * @hba: per-adapter instance
3978 *
3979 * Reset and recover device, host and re-establish link. This
3980 * is helpful to recover the communication in fatal error conditions.
3981 *
3982 * Returns zero on success, non-zero on failure
3983 */
3984static int ufshcd_reset_and_restore(struct ufs_hba *hba)
3985{
3986 int err = 0;
3987 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003988 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303989
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003990 do {
3991 err = ufshcd_host_reset_and_restore(hba);
3992 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303993
3994 /*
3995 * After reset the door-bell might be cleared, complete
3996 * outstanding requests in s/w here.
3997 */
3998 spin_lock_irqsave(hba->host->host_lock, flags);
3999 ufshcd_transfer_req_compl(hba);
4000 ufshcd_tmc_handler(hba);
4001 spin_unlock_irqrestore(hba->host->host_lock, flags);
4002
4003 return err;
4004}
4005
4006/**
4007 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4008 * @cmd - SCSI command pointer
4009 *
4010 * Returns SUCCESS/FAILED
4011 */
4012static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4013{
4014 int err;
4015 unsigned long flags;
4016 struct ufs_hba *hba;
4017
4018 hba = shost_priv(cmd->device->host);
4019
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004020 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304021 /*
4022 * Check if there is any race with fatal error handling.
4023 * If so, wait for it to complete. Even though fatal error
4024 * handling does reset and restore in some cases, don't assume
4025 * anything out of it. We are just avoiding race here.
4026 */
4027 do {
4028 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304029 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304030 hba->ufshcd_state == UFSHCD_STATE_RESET))
4031 break;
4032 spin_unlock_irqrestore(hba->host->host_lock, flags);
4033 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304034 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304035 } while (1);
4036
4037 hba->ufshcd_state = UFSHCD_STATE_RESET;
4038 ufshcd_set_eh_in_progress(hba);
4039 spin_unlock_irqrestore(hba->host->host_lock, flags);
4040
4041 err = ufshcd_reset_and_restore(hba);
4042
4043 spin_lock_irqsave(hba->host->host_lock, flags);
4044 if (!err) {
4045 err = SUCCESS;
4046 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4047 } else {
4048 err = FAILED;
4049 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4050 }
4051 ufshcd_clear_eh_in_progress(hba);
4052 spin_unlock_irqrestore(hba->host->host_lock, flags);
4053
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004054 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304055 return err;
4056}
4057
4058/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004059 * ufshcd_get_max_icc_level - calculate the ICC level
4060 * @sup_curr_uA: max. current supported by the regulator
4061 * @start_scan: row at the desc table to start scan from
4062 * @buff: power descriptor buffer
4063 *
4064 * Returns calculated max ICC level for specific regulator
4065 */
4066static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4067{
4068 int i;
4069 int curr_uA;
4070 u16 data;
4071 u16 unit;
4072
4073 for (i = start_scan; i >= 0; i--) {
4074 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4075 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4076 ATTR_ICC_LVL_UNIT_OFFSET;
4077 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4078 switch (unit) {
4079 case UFSHCD_NANO_AMP:
4080 curr_uA = curr_uA / 1000;
4081 break;
4082 case UFSHCD_MILI_AMP:
4083 curr_uA = curr_uA * 1000;
4084 break;
4085 case UFSHCD_AMP:
4086 curr_uA = curr_uA * 1000 * 1000;
4087 break;
4088 case UFSHCD_MICRO_AMP:
4089 default:
4090 break;
4091 }
4092 if (sup_curr_uA >= curr_uA)
4093 break;
4094 }
4095 if (i < 0) {
4096 i = 0;
4097 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4098 }
4099
4100 return (u32)i;
4101}
4102
4103/**
4104 * ufshcd_calc_icc_level - calculate the max ICC level
4105 * In case regulators are not initialized we'll return 0
4106 * @hba: per-adapter instance
4107 * @desc_buf: power descriptor buffer to extract ICC levels from.
4108 * @len: length of desc_buff
4109 *
4110 * Returns calculated ICC level
4111 */
4112static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4113 u8 *desc_buf, int len)
4114{
4115 u32 icc_level = 0;
4116
4117 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4118 !hba->vreg_info.vccq2) {
4119 dev_err(hba->dev,
4120 "%s: Regulator capability was not set, actvIccLevel=%d",
4121 __func__, icc_level);
4122 goto out;
4123 }
4124
4125 if (hba->vreg_info.vcc)
4126 icc_level = ufshcd_get_max_icc_level(
4127 hba->vreg_info.vcc->max_uA,
4128 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4129 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4130
4131 if (hba->vreg_info.vccq)
4132 icc_level = ufshcd_get_max_icc_level(
4133 hba->vreg_info.vccq->max_uA,
4134 icc_level,
4135 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4136
4137 if (hba->vreg_info.vccq2)
4138 icc_level = ufshcd_get_max_icc_level(
4139 hba->vreg_info.vccq2->max_uA,
4140 icc_level,
4141 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4142out:
4143 return icc_level;
4144}
4145
4146static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4147{
4148 int ret;
4149 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4150 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4151
4152 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4153 if (ret) {
4154 dev_err(hba->dev,
4155 "%s: Failed reading power descriptor.len = %d ret = %d",
4156 __func__, buff_len, ret);
4157 return;
4158 }
4159
4160 hba->init_prefetch_data.icc_level =
4161 ufshcd_find_max_sup_active_icc_level(hba,
4162 desc_buf, buff_len);
4163 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4164 __func__, hba->init_prefetch_data.icc_level);
4165
4166 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4167 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4168 &hba->init_prefetch_data.icc_level);
4169
4170 if (ret)
4171 dev_err(hba->dev,
4172 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4173 __func__, hba->init_prefetch_data.icc_level , ret);
4174
4175}
4176
4177/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004178 * ufshcd_scsi_add_wlus - Adds required W-LUs
4179 * @hba: per-adapter instance
4180 *
4181 * UFS device specification requires the UFS devices to support 4 well known
4182 * logical units:
4183 * "REPORT_LUNS" (address: 01h)
4184 * "UFS Device" (address: 50h)
4185 * "RPMB" (address: 44h)
4186 * "BOOT" (address: 30h)
4187 * UFS device's power management needs to be controlled by "POWER CONDITION"
4188 * field of SSU (START STOP UNIT) command. But this "power condition" field
4189 * will take effect only when its sent to "UFS device" well known logical unit
4190 * hence we require the scsi_device instance to represent this logical unit in
4191 * order for the UFS host driver to send the SSU command for power management.
4192
4193 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4194 * Block) LU so user space process can control this LU. User space may also
4195 * want to have access to BOOT LU.
4196
4197 * This function adds scsi device instances for each of all well known LUs
4198 * (except "REPORT LUNS" LU).
4199 *
4200 * Returns zero on success (all required W-LUs are added successfully),
4201 * non-zero error value on failure (if failed to add any of the required W-LU).
4202 */
4203static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4204{
4205 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004206 struct scsi_device *sdev_rpmb;
4207 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004208
4209 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4210 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4211 if (IS_ERR(hba->sdev_ufs_device)) {
4212 ret = PTR_ERR(hba->sdev_ufs_device);
4213 hba->sdev_ufs_device = NULL;
4214 goto out;
4215 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004216 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004217
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004218 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004219 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004220 if (IS_ERR(sdev_boot)) {
4221 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004222 goto remove_sdev_ufs_device;
4223 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004224 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004225
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004226 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004227 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004228 if (IS_ERR(sdev_rpmb)) {
4229 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004230 goto remove_sdev_boot;
4231 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004232 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004233 goto out;
4234
4235remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004236 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004237remove_sdev_ufs_device:
4238 scsi_remove_device(hba->sdev_ufs_device);
4239out:
4240 return ret;
4241}
4242
4243/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004244 * ufshcd_probe_hba - probe hba to detect device and initialize
4245 * @hba: per-adapter instance
4246 *
4247 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304248 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004249static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304250{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304251 int ret;
4252
4253 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304254 if (ret)
4255 goto out;
4256
Yaniv Gardi50646362014-10-23 13:25:13 +03004257 ufshcd_init_pwr_info(hba);
4258
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004259 /* UniPro link is active now */
4260 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304261
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304262 ret = ufshcd_verify_dev_init(hba);
4263 if (ret)
4264 goto out;
4265
Dolev Raviv68078d52013-07-30 00:35:58 +05304266 ret = ufshcd_complete_dev_init(hba);
4267 if (ret)
4268 goto out;
4269
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004270 /* UFS device is also active now */
4271 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304272 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304273 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004274 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304275
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004276 if (ufshcd_get_max_pwr_mode(hba)) {
4277 dev_err(hba->dev,
4278 "%s: Failed getting max supported power mode\n",
4279 __func__);
4280 } else {
4281 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
4282 if (ret)
4283 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
4284 __func__, ret);
4285 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004286
4287 /*
4288 * If we are in error handling context or in power management callbacks
4289 * context, no need to scan the host
4290 */
4291 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4292 bool flag;
4293
4294 /* clear any previous UFS device information */
4295 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
4296 if (!ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4297 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
4298 hba->dev_info.f_power_on_wp_en = flag;
4299
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004300 if (!hba->is_init_prefetch)
4301 ufshcd_init_icc_levels(hba);
4302
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004303 /* Add required well known logical units to scsi mid layer */
4304 if (ufshcd_scsi_add_wlus(hba))
4305 goto out;
4306
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304307 scsi_scan_host(hba->host);
4308 pm_runtime_put_sync(hba->dev);
4309 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004310
4311 if (!hba->is_init_prefetch)
4312 hba->is_init_prefetch = true;
4313
Sahitya Tummala856b3482014-09-25 15:32:34 +03004314 /* Resume devfreq after UFS device is detected */
4315 if (ufshcd_is_clkscaling_enabled(hba))
4316 devfreq_resume_device(hba->devfreq);
4317
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304318out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004319 /*
4320 * If we failed to initialize the device or the device is not
4321 * present, turn off the power/clocks etc.
4322 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004323 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4324 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004325 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004326 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004327
4328 return ret;
4329}
4330
4331/**
4332 * ufshcd_async_scan - asynchronous execution for probing hba
4333 * @data: data pointer to pass to this function
4334 * @cookie: cookie data
4335 */
4336static void ufshcd_async_scan(void *data, async_cookie_t cookie)
4337{
4338 struct ufs_hba *hba = (struct ufs_hba *)data;
4339
4340 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304341}
4342
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304343static struct scsi_host_template ufshcd_driver_template = {
4344 .module = THIS_MODULE,
4345 .name = UFSHCD,
4346 .proc_name = UFSHCD,
4347 .queuecommand = ufshcd_queuecommand,
4348 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004349 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304350 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004351 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304352 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304353 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
4354 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304355 .this_id = -1,
4356 .sg_tablesize = SG_ALL,
4357 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
4358 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004359 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01004360 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304361};
4362
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004363static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
4364 int ua)
4365{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004366 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004367
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004368 if (!vreg)
4369 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004370
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004371 ret = regulator_set_load(vreg->reg, ua);
4372 if (ret < 0) {
4373 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
4374 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004375 }
4376
4377 return ret;
4378}
4379
4380static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
4381 struct ufs_vreg *vreg)
4382{
4383 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
4384}
4385
4386static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
4387 struct ufs_vreg *vreg)
4388{
4389 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
4390}
4391
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004392static int ufshcd_config_vreg(struct device *dev,
4393 struct ufs_vreg *vreg, bool on)
4394{
4395 int ret = 0;
4396 struct regulator *reg = vreg->reg;
4397 const char *name = vreg->name;
4398 int min_uV, uA_load;
4399
4400 BUG_ON(!vreg);
4401
4402 if (regulator_count_voltages(reg) > 0) {
4403 min_uV = on ? vreg->min_uV : 0;
4404 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
4405 if (ret) {
4406 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
4407 __func__, name, ret);
4408 goto out;
4409 }
4410
4411 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004412 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
4413 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004414 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004415 }
4416out:
4417 return ret;
4418}
4419
4420static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
4421{
4422 int ret = 0;
4423
4424 if (!vreg || vreg->enabled)
4425 goto out;
4426
4427 ret = ufshcd_config_vreg(dev, vreg, true);
4428 if (!ret)
4429 ret = regulator_enable(vreg->reg);
4430
4431 if (!ret)
4432 vreg->enabled = true;
4433 else
4434 dev_err(dev, "%s: %s enable failed, err=%d\n",
4435 __func__, vreg->name, ret);
4436out:
4437 return ret;
4438}
4439
4440static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
4441{
4442 int ret = 0;
4443
4444 if (!vreg || !vreg->enabled)
4445 goto out;
4446
4447 ret = regulator_disable(vreg->reg);
4448
4449 if (!ret) {
4450 /* ignore errors on applying disable config */
4451 ufshcd_config_vreg(dev, vreg, false);
4452 vreg->enabled = false;
4453 } else {
4454 dev_err(dev, "%s: %s disable failed, err=%d\n",
4455 __func__, vreg->name, ret);
4456 }
4457out:
4458 return ret;
4459}
4460
4461static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
4462{
4463 int ret = 0;
4464 struct device *dev = hba->dev;
4465 struct ufs_vreg_info *info = &hba->vreg_info;
4466
4467 if (!info)
4468 goto out;
4469
4470 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
4471 if (ret)
4472 goto out;
4473
4474 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
4475 if (ret)
4476 goto out;
4477
4478 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
4479 if (ret)
4480 goto out;
4481
4482out:
4483 if (ret) {
4484 ufshcd_toggle_vreg(dev, info->vccq2, false);
4485 ufshcd_toggle_vreg(dev, info->vccq, false);
4486 ufshcd_toggle_vreg(dev, info->vcc, false);
4487 }
4488 return ret;
4489}
4490
Raviv Shvili6a771a62014-09-25 15:32:24 +03004491static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
4492{
4493 struct ufs_vreg_info *info = &hba->vreg_info;
4494
4495 if (info)
4496 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
4497
4498 return 0;
4499}
4500
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004501static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
4502{
4503 int ret = 0;
4504
4505 if (!vreg)
4506 goto out;
4507
4508 vreg->reg = devm_regulator_get(dev, vreg->name);
4509 if (IS_ERR(vreg->reg)) {
4510 ret = PTR_ERR(vreg->reg);
4511 dev_err(dev, "%s: %s get failed, err=%d\n",
4512 __func__, vreg->name, ret);
4513 }
4514out:
4515 return ret;
4516}
4517
4518static int ufshcd_init_vreg(struct ufs_hba *hba)
4519{
4520 int ret = 0;
4521 struct device *dev = hba->dev;
4522 struct ufs_vreg_info *info = &hba->vreg_info;
4523
4524 if (!info)
4525 goto out;
4526
4527 ret = ufshcd_get_vreg(dev, info->vcc);
4528 if (ret)
4529 goto out;
4530
4531 ret = ufshcd_get_vreg(dev, info->vccq);
4532 if (ret)
4533 goto out;
4534
4535 ret = ufshcd_get_vreg(dev, info->vccq2);
4536out:
4537 return ret;
4538}
4539
Raviv Shvili6a771a62014-09-25 15:32:24 +03004540static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
4541{
4542 struct ufs_vreg_info *info = &hba->vreg_info;
4543
4544 if (info)
4545 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
4546
4547 return 0;
4548}
4549
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004550static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
4551 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004552{
4553 int ret = 0;
4554 struct ufs_clk_info *clki;
4555 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004556 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004557
4558 if (!head || list_empty(head))
4559 goto out;
4560
4561 list_for_each_entry(clki, head, list) {
4562 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004563 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
4564 continue;
4565
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004566 if (on && !clki->enabled) {
4567 ret = clk_prepare_enable(clki->clk);
4568 if (ret) {
4569 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
4570 __func__, clki->name, ret);
4571 goto out;
4572 }
4573 } else if (!on && clki->enabled) {
4574 clk_disable_unprepare(clki->clk);
4575 }
4576 clki->enabled = on;
4577 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
4578 clki->name, on ? "en" : "dis");
4579 }
4580 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004581
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004582 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004583out:
4584 if (ret) {
4585 list_for_each_entry(clki, head, list) {
4586 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
4587 clk_disable_unprepare(clki->clk);
4588 }
Dolev Raviveda910e2014-10-23 13:25:16 +03004589 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004590 spin_lock_irqsave(hba->host->host_lock, flags);
4591 hba->clk_gating.state = CLKS_ON;
4592 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004593 }
4594 return ret;
4595}
4596
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004597static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
4598{
4599 return __ufshcd_setup_clocks(hba, on, false);
4600}
4601
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004602static int ufshcd_init_clocks(struct ufs_hba *hba)
4603{
4604 int ret = 0;
4605 struct ufs_clk_info *clki;
4606 struct device *dev = hba->dev;
4607 struct list_head *head = &hba->clk_list_head;
4608
4609 if (!head || list_empty(head))
4610 goto out;
4611
4612 list_for_each_entry(clki, head, list) {
4613 if (!clki->name)
4614 continue;
4615
4616 clki->clk = devm_clk_get(dev, clki->name);
4617 if (IS_ERR(clki->clk)) {
4618 ret = PTR_ERR(clki->clk);
4619 dev_err(dev, "%s: %s clk get failed, %d\n",
4620 __func__, clki->name, ret);
4621 goto out;
4622 }
4623
4624 if (clki->max_freq) {
4625 ret = clk_set_rate(clki->clk, clki->max_freq);
4626 if (ret) {
4627 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
4628 __func__, clki->name,
4629 clki->max_freq, ret);
4630 goto out;
4631 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03004632 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004633 }
4634 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
4635 clki->name, clk_get_rate(clki->clk));
4636 }
4637out:
4638 return ret;
4639}
4640
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004641static int ufshcd_variant_hba_init(struct ufs_hba *hba)
4642{
4643 int err = 0;
4644
4645 if (!hba->vops)
4646 goto out;
4647
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004648 err = ufshcd_vops_init(hba);
4649 if (err)
4650 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004651
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004652 err = ufshcd_vops_setup_regulators(hba, true);
4653 if (err)
4654 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004655
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004656 goto out;
4657
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004658out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004659 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004660out:
4661 if (err)
4662 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004663 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004664 return err;
4665}
4666
4667static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
4668{
4669 if (!hba->vops)
4670 return;
4671
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004672 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004673
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004674 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004675
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004676 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004677}
4678
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004679static int ufshcd_hba_init(struct ufs_hba *hba)
4680{
4681 int err;
4682
Raviv Shvili6a771a62014-09-25 15:32:24 +03004683 /*
4684 * Handle host controller power separately from the UFS device power
4685 * rails as it will help controlling the UFS host controller power
4686 * collapse easily which is different than UFS device power collapse.
4687 * Also, enable the host controller power before we go ahead with rest
4688 * of the initialization here.
4689 */
4690 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004691 if (err)
4692 goto out;
4693
Raviv Shvili6a771a62014-09-25 15:32:24 +03004694 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004695 if (err)
4696 goto out;
4697
Raviv Shvili6a771a62014-09-25 15:32:24 +03004698 err = ufshcd_init_clocks(hba);
4699 if (err)
4700 goto out_disable_hba_vreg;
4701
4702 err = ufshcd_setup_clocks(hba, true);
4703 if (err)
4704 goto out_disable_hba_vreg;
4705
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004706 err = ufshcd_init_vreg(hba);
4707 if (err)
4708 goto out_disable_clks;
4709
4710 err = ufshcd_setup_vreg(hba, true);
4711 if (err)
4712 goto out_disable_clks;
4713
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004714 err = ufshcd_variant_hba_init(hba);
4715 if (err)
4716 goto out_disable_vreg;
4717
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004718 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004719 goto out;
4720
4721out_disable_vreg:
4722 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004723out_disable_clks:
4724 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03004725out_disable_hba_vreg:
4726 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004727out:
4728 return err;
4729}
4730
4731static void ufshcd_hba_exit(struct ufs_hba *hba)
4732{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004733 if (hba->is_powered) {
4734 ufshcd_variant_hba_exit(hba);
4735 ufshcd_setup_vreg(hba, false);
4736 ufshcd_setup_clocks(hba, false);
4737 ufshcd_setup_hba_vreg(hba, false);
4738 hba->is_powered = false;
4739 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004740}
4741
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004742static int
4743ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304744{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004745 unsigned char cmd[6] = {REQUEST_SENSE,
4746 0,
4747 0,
4748 0,
4749 SCSI_SENSE_BUFFERSIZE,
4750 0};
4751 char *buffer;
4752 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304753
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004754 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4755 if (!buffer) {
4756 ret = -ENOMEM;
4757 goto out;
4758 }
4759
4760 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
4761 SCSI_SENSE_BUFFERSIZE, NULL,
4762 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
4763 if (ret)
4764 pr_err("%s: failed with err %d\n", __func__, ret);
4765
4766 kfree(buffer);
4767out:
4768 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304769}
4770
4771/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004772 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
4773 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304774 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004775 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304776 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004777 * Returns 0 if requested power mode is set successfully
4778 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304779 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004780static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
4781 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304782{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004783 unsigned char cmd[6] = { START_STOP };
4784 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004785 struct scsi_device *sdp;
4786 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004787 int ret;
4788
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004789 spin_lock_irqsave(hba->host->host_lock, flags);
4790 sdp = hba->sdev_ufs_device;
4791 if (sdp) {
4792 ret = scsi_device_get(sdp);
4793 if (!ret && !scsi_device_online(sdp)) {
4794 ret = -ENODEV;
4795 scsi_device_put(sdp);
4796 }
4797 } else {
4798 ret = -ENODEV;
4799 }
4800 spin_unlock_irqrestore(hba->host->host_lock, flags);
4801
4802 if (ret)
4803 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004804
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304805 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004806 * If scsi commands fail, the scsi mid-layer schedules scsi error-
4807 * handling, which would wait for host to be resumed. Since we know
4808 * we are functional while we are here, skip host resume in error
4809 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304810 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004811 hba->host->eh_noresume = 1;
4812 if (hba->wlun_dev_clr_ua) {
4813 ret = ufshcd_send_request_sense(hba, sdp);
4814 if (ret)
4815 goto out;
4816 /* Unit attention condition is cleared now */
4817 hba->wlun_dev_clr_ua = false;
4818 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304819
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004820 cmd[4] = pwr_mode << 4;
4821
4822 /*
4823 * Current function would be generally called from the power management
4824 * callbacks hence set the REQ_PM flag so that it doesn't resume the
4825 * already suspended childs.
4826 */
4827 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
4828 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
4829 if (ret) {
4830 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02004831 "START_STOP failed for power mode: %d, result %x\n",
4832 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01004833 if (driver_byte(ret) & DRIVER_SENSE)
4834 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004835 }
4836
4837 if (!ret)
4838 hba->curr_dev_pwr_mode = pwr_mode;
4839out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004840 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004841 hba->host->eh_noresume = 0;
4842 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304843}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304844
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004845static int ufshcd_link_state_transition(struct ufs_hba *hba,
4846 enum uic_link_state req_link_state,
4847 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304848{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004849 int ret = 0;
4850
4851 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304852 return 0;
4853
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004854 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
4855 ret = ufshcd_uic_hibern8_enter(hba);
4856 if (!ret)
4857 ufshcd_set_link_hibern8(hba);
4858 else
4859 goto out;
4860 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304861 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004862 * If autobkops is enabled, link can't be turned off because
4863 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304864 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004865 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
4866 (!check_for_bkops || (check_for_bkops &&
4867 !hba->auto_bkops_enabled))) {
4868 /*
4869 * Change controller state to "reset state" which
4870 * should also put the link in off/reset state
4871 */
4872 ufshcd_hba_stop(hba);
4873 /*
4874 * TODO: Check if we need any delay to make sure that
4875 * controller is reset
4876 */
4877 ufshcd_set_link_off(hba);
4878 }
4879
4880out:
4881 return ret;
4882}
4883
4884static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
4885{
4886 /*
4887 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
4888 * power.
4889 *
4890 * If UFS device and link is in OFF state, all power supplies (VCC,
4891 * VCCQ, VCCQ2) can be turned off if power on write protect is not
4892 * required. If UFS link is inactive (Hibern8 or OFF state) and device
4893 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
4894 *
4895 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
4896 * in low power state which would save some power.
4897 */
4898 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
4899 !hba->dev_info.is_lu_power_on_wp) {
4900 ufshcd_setup_vreg(hba, false);
4901 } else if (!ufshcd_is_ufs_dev_active(hba)) {
4902 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
4903 if (!ufshcd_is_link_active(hba)) {
4904 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
4905 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
4906 }
4907 }
4908}
4909
4910static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
4911{
4912 int ret = 0;
4913
4914 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
4915 !hba->dev_info.is_lu_power_on_wp) {
4916 ret = ufshcd_setup_vreg(hba, true);
4917 } else if (!ufshcd_is_ufs_dev_active(hba)) {
4918 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
4919 if (!ret && !ufshcd_is_link_active(hba)) {
4920 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
4921 if (ret)
4922 goto vcc_disable;
4923 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
4924 if (ret)
4925 goto vccq_lpm;
4926 }
4927 }
4928 goto out;
4929
4930vccq_lpm:
4931 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
4932vcc_disable:
4933 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
4934out:
4935 return ret;
4936}
4937
4938static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
4939{
4940 if (ufshcd_is_link_off(hba))
4941 ufshcd_setup_hba_vreg(hba, false);
4942}
4943
4944static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
4945{
4946 if (ufshcd_is_link_off(hba))
4947 ufshcd_setup_hba_vreg(hba, true);
4948}
4949
4950/**
4951 * ufshcd_suspend - helper function for suspend operations
4952 * @hba: per adapter instance
4953 * @pm_op: desired low power operation type
4954 *
4955 * This function will try to put the UFS device and link into low power
4956 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
4957 * (System PM level).
4958 *
4959 * If this function is called during shutdown, it will make sure that
4960 * both UFS device and UFS link is powered off.
4961 *
4962 * NOTE: UFS device & link must be active before we enter in this function.
4963 *
4964 * Returns 0 for success and non-zero for failure
4965 */
4966static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
4967{
4968 int ret = 0;
4969 enum ufs_pm_level pm_lvl;
4970 enum ufs_dev_pwr_mode req_dev_pwr_mode;
4971 enum uic_link_state req_link_state;
4972
4973 hba->pm_op_in_progress = 1;
4974 if (!ufshcd_is_shutdown_pm(pm_op)) {
4975 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
4976 hba->rpm_lvl : hba->spm_lvl;
4977 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
4978 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
4979 } else {
4980 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
4981 req_link_state = UIC_LINK_OFF_STATE;
4982 }
4983
4984 /*
4985 * If we can't transition into any of the low power modes
4986 * just gate the clocks.
4987 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004988 ufshcd_hold(hba, false);
4989 hba->clk_gating.is_suspended = true;
4990
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004991 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
4992 req_link_state == UIC_LINK_ACTIVE_STATE) {
4993 goto disable_clks;
4994 }
4995
4996 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
4997 (req_link_state == hba->uic_link_state))
4998 goto out;
4999
5000 /* UFS device & link must be active before we enter in this function */
5001 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5002 ret = -EINVAL;
5003 goto out;
5004 }
5005
5006 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005007 if (ufshcd_can_autobkops_during_suspend(hba)) {
5008 /*
5009 * The device is idle with no requests in the queue,
5010 * allow background operations if bkops status shows
5011 * that performance might be impacted.
5012 */
5013 ret = ufshcd_urgent_bkops(hba);
5014 if (ret)
5015 goto enable_gating;
5016 } else {
5017 /* make sure that auto bkops is disabled */
5018 ufshcd_disable_auto_bkops(hba);
5019 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005020 }
5021
5022 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5023 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5024 !ufshcd_is_runtime_pm(pm_op))) {
5025 /* ensure that bkops is disabled */
5026 ufshcd_disable_auto_bkops(hba);
5027 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5028 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005029 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005030 }
5031
5032 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5033 if (ret)
5034 goto set_dev_active;
5035
5036 ufshcd_vreg_set_lpm(hba);
5037
5038disable_clks:
5039 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03005040 * The clock scaling needs access to controller registers. Hence, Wait
5041 * for pending clock scaling work to be done before clocks are
5042 * turned off.
5043 */
5044 if (ufshcd_is_clkscaling_enabled(hba)) {
5045 devfreq_suspend_device(hba->devfreq);
5046 hba->clk_scaling.window_start_t = 0;
5047 }
5048 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005049 * Call vendor specific suspend callback. As these callbacks may access
5050 * vendor specific host controller register space call them before the
5051 * host clocks are ON.
5052 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005053 ret = ufshcd_vops_suspend(hba, pm_op);
5054 if (ret)
5055 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005056
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005057 ret = ufshcd_vops_setup_clocks(hba, false);
5058 if (ret)
5059 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005060
5061 if (!ufshcd_is_link_active(hba))
5062 ufshcd_setup_clocks(hba, false);
5063 else
5064 /* If link is active, device ref_clk can't be switched off */
5065 __ufshcd_setup_clocks(hba, false, true);
5066
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005067 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005068 /*
5069 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005070 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005071 */
5072 ufshcd_disable_irq(hba);
5073 /* Put the host controller in low power mode if possible */
5074 ufshcd_hba_vreg_set_lpm(hba);
5075 goto out;
5076
5077vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005078 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005079set_link_active:
5080 ufshcd_vreg_set_hpm(hba);
5081 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5082 ufshcd_set_link_active(hba);
5083 else if (ufshcd_is_link_off(hba))
5084 ufshcd_host_reset_and_restore(hba);
5085set_dev_active:
5086 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5087 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005088enable_gating:
5089 hba->clk_gating.is_suspended = false;
5090 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005091out:
5092 hba->pm_op_in_progress = 0;
5093 return ret;
5094}
5095
5096/**
5097 * ufshcd_resume - helper function for resume operations
5098 * @hba: per adapter instance
5099 * @pm_op: runtime PM or system PM
5100 *
5101 * This function basically brings the UFS device, UniPro link and controller
5102 * to active state.
5103 *
5104 * Returns 0 for success and non-zero for failure
5105 */
5106static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5107{
5108 int ret;
5109 enum uic_link_state old_link_state;
5110
5111 hba->pm_op_in_progress = 1;
5112 old_link_state = hba->uic_link_state;
5113
5114 ufshcd_hba_vreg_set_hpm(hba);
5115 /* Make sure clocks are enabled before accessing controller */
5116 ret = ufshcd_setup_clocks(hba, true);
5117 if (ret)
5118 goto out;
5119
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005120 /* enable the host irq as host controller would be active soon */
5121 ret = ufshcd_enable_irq(hba);
5122 if (ret)
5123 goto disable_irq_and_vops_clks;
5124
5125 ret = ufshcd_vreg_set_hpm(hba);
5126 if (ret)
5127 goto disable_irq_and_vops_clks;
5128
5129 /*
5130 * Call vendor specific resume callback. As these callbacks may access
5131 * vendor specific host controller register space call them when the
5132 * host clocks are ON.
5133 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005134 ret = ufshcd_vops_resume(hba, pm_op);
5135 if (ret)
5136 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005137
5138 if (ufshcd_is_link_hibern8(hba)) {
5139 ret = ufshcd_uic_hibern8_exit(hba);
5140 if (!ret)
5141 ufshcd_set_link_active(hba);
5142 else
5143 goto vendor_suspend;
5144 } else if (ufshcd_is_link_off(hba)) {
5145 ret = ufshcd_host_reset_and_restore(hba);
5146 /*
5147 * ufshcd_host_reset_and_restore() should have already
5148 * set the link state as active
5149 */
5150 if (ret || !ufshcd_is_link_active(hba))
5151 goto vendor_suspend;
5152 }
5153
5154 if (!ufshcd_is_ufs_dev_active(hba)) {
5155 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
5156 if (ret)
5157 goto set_old_link_state;
5158 }
5159
Subhash Jadavani374a2462014-09-25 15:32:35 +03005160 /*
5161 * If BKOPs operations are urgently needed at this moment then
5162 * keep auto-bkops enabled or else disable it.
5163 */
5164 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005165 hba->clk_gating.is_suspended = false;
5166
Sahitya Tummala856b3482014-09-25 15:32:34 +03005167 if (ufshcd_is_clkscaling_enabled(hba))
5168 devfreq_resume_device(hba->devfreq);
5169
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005170 /* Schedule clock gating in case of no access to UFS device yet */
5171 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005172 goto out;
5173
5174set_old_link_state:
5175 ufshcd_link_state_transition(hba, old_link_state, 0);
5176vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005177 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005178disable_vreg:
5179 ufshcd_vreg_set_lpm(hba);
5180disable_irq_and_vops_clks:
5181 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005182 ufshcd_setup_clocks(hba, false);
5183out:
5184 hba->pm_op_in_progress = 0;
5185 return ret;
5186}
5187
5188/**
5189 * ufshcd_system_suspend - system suspend routine
5190 * @hba: per adapter instance
5191 * @pm_op: runtime PM or system PM
5192 *
5193 * Check the description of ufshcd_suspend() function for more details.
5194 *
5195 * Returns 0 for success and non-zero for failure
5196 */
5197int ufshcd_system_suspend(struct ufs_hba *hba)
5198{
5199 int ret = 0;
5200
5201 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03005202 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005203
5204 if (pm_runtime_suspended(hba->dev)) {
5205 if (hba->rpm_lvl == hba->spm_lvl)
5206 /*
5207 * There is possibility that device may still be in
5208 * active state during the runtime suspend.
5209 */
5210 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
5211 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
5212 goto out;
5213
5214 /*
5215 * UFS device and/or UFS link low power states during runtime
5216 * suspend seems to be different than what is expected during
5217 * system suspend. Hence runtime resume the devic & link and
5218 * let the system suspend low power states to take effect.
5219 * TODO: If resume takes longer time, we might have optimize
5220 * it in future by not resuming everything if possible.
5221 */
5222 ret = ufshcd_runtime_resume(hba);
5223 if (ret)
5224 goto out;
5225 }
5226
5227 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
5228out:
Dolev Ravive7850602014-09-25 15:32:36 +03005229 if (!ret)
5230 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005231 return ret;
5232}
5233EXPORT_SYMBOL(ufshcd_system_suspend);
5234
5235/**
5236 * ufshcd_system_resume - system resume routine
5237 * @hba: per adapter instance
5238 *
5239 * Returns 0 for success and non-zero for failure
5240 */
5241
5242int ufshcd_system_resume(struct ufs_hba *hba)
5243{
5244 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
5245 /*
5246 * Let the runtime resume take care of resuming
5247 * if runtime suspended.
5248 */
5249 return 0;
5250
5251 return ufshcd_resume(hba, UFS_SYSTEM_PM);
5252}
5253EXPORT_SYMBOL(ufshcd_system_resume);
5254
5255/**
5256 * ufshcd_runtime_suspend - runtime suspend routine
5257 * @hba: per adapter instance
5258 *
5259 * Check the description of ufshcd_suspend() function for more details.
5260 *
5261 * Returns 0 for success and non-zero for failure
5262 */
5263int ufshcd_runtime_suspend(struct ufs_hba *hba)
5264{
5265 if (!hba || !hba->is_powered)
5266 return 0;
5267
5268 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305269}
5270EXPORT_SYMBOL(ufshcd_runtime_suspend);
5271
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005272/**
5273 * ufshcd_runtime_resume - runtime resume routine
5274 * @hba: per adapter instance
5275 *
5276 * This function basically brings the UFS device, UniPro link and controller
5277 * to active state. Following operations are done in this function:
5278 *
5279 * 1. Turn on all the controller related clocks
5280 * 2. Bring the UniPro link out of Hibernate state
5281 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
5282 * to active state.
5283 * 4. If auto-bkops is enabled on the device, disable it.
5284 *
5285 * So following would be the possible power state after this function return
5286 * successfully:
5287 * S1: UFS device in Active state with VCC rail ON
5288 * UniPro link in Active state
5289 * All the UFS/UniPro controller clocks are ON
5290 *
5291 * Returns 0 for success and non-zero for failure
5292 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305293int ufshcd_runtime_resume(struct ufs_hba *hba)
5294{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005295 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305296 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005297 else
5298 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305299}
5300EXPORT_SYMBOL(ufshcd_runtime_resume);
5301
5302int ufshcd_runtime_idle(struct ufs_hba *hba)
5303{
5304 return 0;
5305}
5306EXPORT_SYMBOL(ufshcd_runtime_idle);
5307
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305308/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005309 * ufshcd_shutdown - shutdown routine
5310 * @hba: per adapter instance
5311 *
5312 * This function would power off both UFS device and UFS link.
5313 *
5314 * Returns 0 always to allow force shutdown even in case of errors.
5315 */
5316int ufshcd_shutdown(struct ufs_hba *hba)
5317{
5318 int ret = 0;
5319
5320 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
5321 goto out;
5322
5323 if (pm_runtime_suspended(hba->dev)) {
5324 ret = ufshcd_runtime_resume(hba);
5325 if (ret)
5326 goto out;
5327 }
5328
5329 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
5330out:
5331 if (ret)
5332 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
5333 /* allow force shutdown even in case of errors */
5334 return 0;
5335}
5336EXPORT_SYMBOL(ufshcd_shutdown);
5337
5338/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305339 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305340 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305341 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305342 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305343void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305344{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05305345 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305346 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305347 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305348 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305349
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305350 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005351
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005352 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005353 if (ufshcd_is_clkscaling_enabled(hba))
5354 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005355 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305356}
5357EXPORT_SYMBOL_GPL(ufshcd_remove);
5358
5359/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02005360 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
5361 * @hba: pointer to Host Bus Adapter (HBA)
5362 */
5363void ufshcd_dealloc_host(struct ufs_hba *hba)
5364{
5365 scsi_host_put(hba->host);
5366}
5367EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
5368
5369/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005370 * ufshcd_set_dma_mask - Set dma mask based on the controller
5371 * addressing capability
5372 * @hba: per adapter instance
5373 *
5374 * Returns 0 for success, non-zero for failure
5375 */
5376static int ufshcd_set_dma_mask(struct ufs_hba *hba)
5377{
5378 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
5379 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
5380 return 0;
5381 }
5382 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
5383}
5384
5385/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005386 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305387 * @dev: pointer to device handle
5388 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305389 * Returns 0 on success, non-zero value on failure
5390 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005391int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305392{
5393 struct Scsi_Host *host;
5394 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005395 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305396
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305397 if (!dev) {
5398 dev_err(dev,
5399 "Invalid memory reference for dev is NULL\n");
5400 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305401 goto out_error;
5402 }
5403
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305404 host = scsi_host_alloc(&ufshcd_driver_template,
5405 sizeof(struct ufs_hba));
5406 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305407 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305408 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305409 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305410 }
5411 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305412 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305413 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005414 *hba_handle = hba;
5415
5416out_error:
5417 return err;
5418}
5419EXPORT_SYMBOL(ufshcd_alloc_host);
5420
Sahitya Tummala856b3482014-09-25 15:32:34 +03005421static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
5422{
5423 int ret = 0;
5424 struct ufs_clk_info *clki;
5425 struct list_head *head = &hba->clk_list_head;
5426
5427 if (!head || list_empty(head))
5428 goto out;
5429
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005430 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
5431 if (ret)
5432 return ret;
5433
Sahitya Tummala856b3482014-09-25 15:32:34 +03005434 list_for_each_entry(clki, head, list) {
5435 if (!IS_ERR_OR_NULL(clki->clk)) {
5436 if (scale_up && clki->max_freq) {
5437 if (clki->curr_freq == clki->max_freq)
5438 continue;
5439 ret = clk_set_rate(clki->clk, clki->max_freq);
5440 if (ret) {
5441 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5442 __func__, clki->name,
5443 clki->max_freq, ret);
5444 break;
5445 }
5446 clki->curr_freq = clki->max_freq;
5447
5448 } else if (!scale_up && clki->min_freq) {
5449 if (clki->curr_freq == clki->min_freq)
5450 continue;
5451 ret = clk_set_rate(clki->clk, clki->min_freq);
5452 if (ret) {
5453 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5454 __func__, clki->name,
5455 clki->min_freq, ret);
5456 break;
5457 }
5458 clki->curr_freq = clki->min_freq;
5459 }
5460 }
5461 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
5462 clki->name, clk_get_rate(clki->clk));
5463 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005464
5465 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
5466
Sahitya Tummala856b3482014-09-25 15:32:34 +03005467out:
5468 return ret;
5469}
5470
5471static int ufshcd_devfreq_target(struct device *dev,
5472 unsigned long *freq, u32 flags)
5473{
5474 int err = 0;
5475 struct ufs_hba *hba = dev_get_drvdata(dev);
5476
5477 if (!ufshcd_is_clkscaling_enabled(hba))
5478 return -EINVAL;
5479
5480 if (*freq == UINT_MAX)
5481 err = ufshcd_scale_clks(hba, true);
5482 else if (*freq == 0)
5483 err = ufshcd_scale_clks(hba, false);
5484
5485 return err;
5486}
5487
5488static int ufshcd_devfreq_get_dev_status(struct device *dev,
5489 struct devfreq_dev_status *stat)
5490{
5491 struct ufs_hba *hba = dev_get_drvdata(dev);
5492 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
5493 unsigned long flags;
5494
5495 if (!ufshcd_is_clkscaling_enabled(hba))
5496 return -EINVAL;
5497
5498 memset(stat, 0, sizeof(*stat));
5499
5500 spin_lock_irqsave(hba->host->host_lock, flags);
5501 if (!scaling->window_start_t)
5502 goto start_window;
5503
5504 if (scaling->is_busy_started)
5505 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
5506 scaling->busy_start_t));
5507
5508 stat->total_time = jiffies_to_usecs((long)jiffies -
5509 (long)scaling->window_start_t);
5510 stat->busy_time = scaling->tot_busy_t;
5511start_window:
5512 scaling->window_start_t = jiffies;
5513 scaling->tot_busy_t = 0;
5514
5515 if (hba->outstanding_reqs) {
5516 scaling->busy_start_t = ktime_get();
5517 scaling->is_busy_started = true;
5518 } else {
5519 scaling->busy_start_t = ktime_set(0, 0);
5520 scaling->is_busy_started = false;
5521 }
5522 spin_unlock_irqrestore(hba->host->host_lock, flags);
5523 return 0;
5524}
5525
5526static struct devfreq_dev_profile ufs_devfreq_profile = {
5527 .polling_ms = 100,
5528 .target = ufshcd_devfreq_target,
5529 .get_dev_status = ufshcd_devfreq_get_dev_status,
5530};
5531
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005532/**
5533 * ufshcd_init - Driver initialization routine
5534 * @hba: per-adapter instance
5535 * @mmio_base: base register address
5536 * @irq: Interrupt line of device
5537 * Returns 0 on success, non-zero value on failure
5538 */
5539int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
5540{
5541 int err;
5542 struct Scsi_Host *host = hba->host;
5543 struct device *dev = hba->dev;
5544
5545 if (!mmio_base) {
5546 dev_err(hba->dev,
5547 "Invalid memory reference for mmio_base is NULL\n");
5548 err = -ENODEV;
5549 goto out_error;
5550 }
5551
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305552 hba->mmio_base = mmio_base;
5553 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305554
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005555 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005556 if (err)
5557 goto out_error;
5558
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305559 /* Read capabilities registers */
5560 ufshcd_hba_capabilities(hba);
5561
5562 /* Get UFS version supported by the controller */
5563 hba->ufs_version = ufshcd_get_ufs_version(hba);
5564
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305565 /* Get Interrupt bit mask per version */
5566 hba->intr_mask = ufshcd_get_intr_mask(hba);
5567
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005568 err = ufshcd_set_dma_mask(hba);
5569 if (err) {
5570 dev_err(hba->dev, "set dma mask failed\n");
5571 goto out_disable;
5572 }
5573
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305574 /* Allocate memory for host memory space */
5575 err = ufshcd_memory_alloc(hba);
5576 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305577 dev_err(hba->dev, "Memory allocation failed\n");
5578 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305579 }
5580
5581 /* Configure LRB */
5582 ufshcd_host_memory_configure(hba);
5583
5584 host->can_queue = hba->nutrs;
5585 host->cmd_per_lun = hba->nutrs;
5586 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005587 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305588 host->max_channel = UFSHCD_MAX_CHANNEL;
5589 host->unique_id = host->host_no;
5590 host->max_cmd_len = MAX_CDB_SIZE;
5591
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005592 hba->max_pwr_info.is_valid = false;
5593
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305594 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305595 init_waitqueue_head(&hba->tm_wq);
5596 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305597
5598 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305599 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305600 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305601
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305602 /* Initialize UIC command mutex */
5603 mutex_init(&hba->uic_cmd_mutex);
5604
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305605 /* Initialize mutex for device management commands */
5606 mutex_init(&hba->dev_cmd.lock);
5607
5608 /* Initialize device management tag acquire wait queue */
5609 init_waitqueue_head(&hba->dev_cmd.tag_wq);
5610
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005611 ufshcd_init_clk_gating(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305612 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09005613 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305614 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305615 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005616 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005617 } else {
5618 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305619 }
5620
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305621 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305622 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305623 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005624 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305625 }
5626
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305627 /* Host controller enable */
5628 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305629 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305630 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305631 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305632 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305633
Sahitya Tummala856b3482014-09-25 15:32:34 +03005634 if (ufshcd_is_clkscaling_enabled(hba)) {
5635 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
5636 "simple_ondemand", NULL);
5637 if (IS_ERR(hba->devfreq)) {
5638 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
5639 PTR_ERR(hba->devfreq));
5640 goto out_remove_scsi_host;
5641 }
5642 /* Suspend devfreq until the UFS device is detected */
5643 devfreq_suspend_device(hba->devfreq);
5644 hba->clk_scaling.window_start_t = 0;
5645 }
5646
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305647 /* Hold auto suspend until async scan completes */
5648 pm_runtime_get_sync(dev);
5649
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005650 /*
5651 * The device-initialize-sequence hasn't been invoked yet.
5652 * Set the device to power-off state
5653 */
5654 ufshcd_set_ufs_dev_poweroff(hba);
5655
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305656 async_schedule(ufshcd_async_scan, hba);
5657
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305658 return 0;
5659
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305660out_remove_scsi_host:
5661 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005662exit_gating:
5663 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305664out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005665 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305666 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005667 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305668out_error:
5669 return err;
5670}
5671EXPORT_SYMBOL_GPL(ufshcd_init);
5672
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305673MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
5674MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05305675MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305676MODULE_LICENSE("GPL");
5677MODULE_VERSION(UFSHCD_DRIVER_VERSION);