blob: 1581d03e9912ee826c8cb2e1900ae927008b8f92 [file] [log] [blame]
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301/*
Vinayak Holikattie0eca632013-02-25 21:44:33 +05302 * Universal Flash Storage Host controller driver Core
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303 *
4 * This code is based on drivers/scsi/ufs/ufshcd.c
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305 * Copyright (C) 2011-2013 Samsung India Software Operations
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02006 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308 * Authors:
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053016 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053018 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053024 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
34 * this program.
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030035 *
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053038 */
39
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053040#include <linux/async.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030041#include <linux/devfreq.h>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053042
Vinayak Holikattie0eca632013-02-25 21:44:33 +053043#include "ufshcd.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053044#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053045
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053046#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
47 UTP_TASK_REQ_COMPL |\
48 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053049/* UIC command timeout, unit: ms */
50#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053051
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053052/* NOP OUT retries waiting for NOP IN response */
53#define NOP_OUT_RETRIES 10
54/* Timeout after 30 msecs if NOP OUT hangs without response */
55#define NOP_OUT_TIMEOUT 30 /* msecs */
56
Dolev Raviv68078d52013-07-30 00:35:58 +053057/* Query request retries */
58#define QUERY_REQ_RETRIES 10
59/* Query request timeout */
60#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020061/*
62 * Query request timeout for fDeviceInit flag
63 * fDeviceInit query response time for some devices is too large that default
64 * QUERY_REQ_TIMEOUT may not be enough for such devices.
65 */
66#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053067
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053068/* Task management command timeout */
69#define TM_CMD_TIMEOUT 100 /* msecs */
70
Yaniv Gardi64238fb2016-02-01 15:02:43 +020071/* maximum number of retries for a general UIC command */
72#define UFS_UIC_COMMAND_RETRIES 3
73
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030074/* maximum number of link-startup retries */
75#define DME_LINKSTARTUP_RETRIES 3
76
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020077/* Maximum retries for Hibern8 enter */
78#define UIC_HIBERN8_ENTER_RETRIES 3
79
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030080/* maximum number of reset retries before giving up */
81#define MAX_HOST_RESET_RETRIES 5
82
Dolev Raviv68078d52013-07-30 00:35:58 +053083/* Expose the flag value from utp_upiu_query.value */
84#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
85
Seungwon Jeon7d568652013-08-31 21:40:20 +053086/* Interrupt aggregation default timeout, unit: 40us */
87#define INT_AGGR_DEF_TO 0x02
88
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030089#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
90 ({ \
91 int _ret; \
92 if (_on) \
93 _ret = ufshcd_enable_vreg(_dev, _vreg); \
94 else \
95 _ret = ufshcd_disable_vreg(_dev, _vreg); \
96 _ret; \
97 })
98
Subhash Jadavanida461ce2014-09-25 15:32:25 +030099static u32 ufs_query_desc_max_size[] = {
100 QUERY_DESC_DEVICE_MAX_SIZE,
101 QUERY_DESC_CONFIGURAION_MAX_SIZE,
102 QUERY_DESC_UNIT_MAX_SIZE,
103 QUERY_DESC_RFU_MAX_SIZE,
104 QUERY_DESC_INTERCONNECT_MAX_SIZE,
105 QUERY_DESC_STRING_MAX_SIZE,
106 QUERY_DESC_RFU_MAX_SIZE,
107 QUERY_DESC_GEOMETRY_MAZ_SIZE,
108 QUERY_DESC_POWER_MAX_SIZE,
109 QUERY_DESC_RFU_MAX_SIZE,
110};
111
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530112enum {
113 UFSHCD_MAX_CHANNEL = 0,
114 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530115 UFSHCD_CMD_PER_LUN = 32,
116 UFSHCD_CAN_QUEUE = 32,
117};
118
119/* UFSHCD states */
120enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530121 UFSHCD_STATE_RESET,
122 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530123 UFSHCD_STATE_OPERATIONAL,
124};
125
126/* UFSHCD error handling flags */
127enum {
128 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530129};
130
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530131/* UFSHCD UIC layer error flags */
132enum {
133 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
134 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
135 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
136 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
137};
138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530139/* Interrupt configuration options */
140enum {
141 UFSHCD_INT_DISABLE,
142 UFSHCD_INT_ENABLE,
143 UFSHCD_INT_CLEAR,
144};
145
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530146#define ufshcd_set_eh_in_progress(h) \
147 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
148#define ufshcd_eh_in_progress(h) \
149 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
150#define ufshcd_clear_eh_in_progress(h) \
151 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
152
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300153#define ufshcd_set_ufs_dev_active(h) \
154 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
155#define ufshcd_set_ufs_dev_sleep(h) \
156 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
157#define ufshcd_set_ufs_dev_poweroff(h) \
158 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
159#define ufshcd_is_ufs_dev_active(h) \
160 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
161#define ufshcd_is_ufs_dev_sleep(h) \
162 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
163#define ufshcd_is_ufs_dev_poweroff(h) \
164 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
165
166static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
167 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
168 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
169 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
170 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
171 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
172 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
173};
174
175static inline enum ufs_dev_pwr_mode
176ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
177{
178 return ufs_pm_lvl_states[lvl].dev_state;
179}
180
181static inline enum uic_link_state
182ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
183{
184 return ufs_pm_lvl_states[lvl].link_state;
185}
186
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530187static void ufshcd_tmc_handler(struct ufs_hba *hba);
188static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530189static int ufshcd_reset_and_restore(struct ufs_hba *hba);
190static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300191static void ufshcd_hba_exit(struct ufs_hba *hba);
192static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300193static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
194 bool skip_ref_clk);
195static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
196static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
197static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300198static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300199static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
200static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300201static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
202 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300203static int ufshcd_change_power_mode(struct ufs_hba *hba,
204 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200205static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
206{
207 return tag >= 0 && tag < hba->nutrs;
208}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300209
210static inline int ufshcd_enable_irq(struct ufs_hba *hba)
211{
212 int ret = 0;
213
214 if (!hba->is_irq_enabled) {
215 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
216 hba);
217 if (ret)
218 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
219 __func__, ret);
220 hba->is_irq_enabled = true;
221 }
222
223 return ret;
224}
225
226static inline void ufshcd_disable_irq(struct ufs_hba *hba)
227{
228 if (hba->is_irq_enabled) {
229 free_irq(hba->irq, hba);
230 hba->is_irq_enabled = false;
231 }
232}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530233
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530234/*
235 * ufshcd_wait_for_register - wait for register value to change
236 * @hba - per-adapter interface
237 * @reg - mmio register offset
238 * @mask - mask to apply to read register value
239 * @val - wait condition
240 * @interval_us - polling interval in microsecs
241 * @timeout_ms - timeout in millisecs
242 *
243 * Returns -ETIMEDOUT on error, zero on success
244 */
245static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
246 u32 val, unsigned long interval_us, unsigned long timeout_ms)
247{
248 int err = 0;
249 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
250
251 /* ignore bits that we don't intend to wait on */
252 val = val & mask;
253
254 while ((ufshcd_readl(hba, reg) & mask) != val) {
255 /* wakeup within 50us of expiry */
256 usleep_range(interval_us, interval_us + 50);
257
258 if (time_after(jiffies, timeout)) {
259 if ((ufshcd_readl(hba, reg) & mask) != val)
260 err = -ETIMEDOUT;
261 break;
262 }
263 }
264
265 return err;
266}
267
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530268/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530269 * ufshcd_get_intr_mask - Get the interrupt bit mask
270 * @hba - Pointer to adapter instance
271 *
272 * Returns interrupt bit mask per version
273 */
274static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
275{
276 if (hba->ufs_version == UFSHCI_VERSION_10)
277 return INTERRUPT_MASK_ALL_VER_10;
278 else
279 return INTERRUPT_MASK_ALL_VER_11;
280}
281
282/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530283 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
284 * @hba - Pointer to adapter instance
285 *
286 * Returns UFSHCI version supported by the controller
287 */
288static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
289{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200290 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
291 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300292
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530293 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530294}
295
296/**
297 * ufshcd_is_device_present - Check if any device connected to
298 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300299 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530300 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530301 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530302 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300303static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530304{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300305 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
306 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530307}
308
309/**
310 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
311 * @lrb: pointer to local command reference block
312 *
313 * This function is used to get the OCS field from UTRD
314 * Returns the OCS field in the UTRD
315 */
316static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
317{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530318 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530319}
320
321/**
322 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
323 * @task_req_descp: pointer to utp_task_req_desc structure
324 *
325 * This function is used to get the OCS field from UTMRD
326 * Returns the OCS field in the UTMRD
327 */
328static inline int
329ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
330{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530331 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530332}
333
334/**
335 * ufshcd_get_tm_free_slot - get a free slot for task management request
336 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530337 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530338 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530339 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
340 * Returns 0 if free slot is not available, else return 1 with tag value
341 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530342 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530343static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530344{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530345 int tag;
346 bool ret = false;
347
348 if (!free_slot)
349 goto out;
350
351 do {
352 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
353 if (tag >= hba->nutmrs)
354 goto out;
355 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
356
357 *free_slot = tag;
358 ret = true;
359out:
360 return ret;
361}
362
363static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
364{
365 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530366}
367
368/**
369 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
370 * @hba: per adapter instance
371 * @pos: position of the bit to be cleared
372 */
373static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
374{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530375 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530376}
377
378/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200379 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
380 * @hba: per adapter instance
381 * @tag: position of the bit to be cleared
382 */
383static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
384{
385 __clear_bit(tag, &hba->outstanding_reqs);
386}
387
388/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530389 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
390 * @reg: Register value of host controller status
391 *
392 * Returns integer, 0 on Success and positive value if failed
393 */
394static inline int ufshcd_get_lists_status(u32 reg)
395{
396 /*
397 * The mask 0xFF is for the following HCS register bits
398 * Bit Description
399 * 0 Device Present
400 * 1 UTRLRDY
401 * 2 UTMRLRDY
402 * 3 UCRDY
403 * 4 HEI
404 * 5 DEI
405 * 6-7 reserved
406 */
407 return (((reg) & (0xFF)) >> 1) ^ (0x07);
408}
409
410/**
411 * ufshcd_get_uic_cmd_result - Get the UIC command result
412 * @hba: Pointer to adapter instance
413 *
414 * This function gets the result of UIC command completion
415 * Returns 0 on success, non zero value on error
416 */
417static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
418{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530419 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530420 MASK_UIC_COMMAND_RESULT;
421}
422
423/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530424 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
425 * @hba: Pointer to adapter instance
426 *
427 * This function gets UIC command argument3
428 * Returns 0 on success, non zero value on error
429 */
430static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
431{
432 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
433}
434
435/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530436 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530437 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530438 */
439static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530440ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530441{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530442 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530443}
444
445/**
446 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
447 * @ucd_rsp_ptr: pointer to response UPIU
448 *
449 * This function gets the response status and scsi_status from response UPIU
450 * Returns the response result code.
451 */
452static inline int
453ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
454{
455 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
456}
457
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530458/*
459 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
460 * from response UPIU
461 * @ucd_rsp_ptr: pointer to response UPIU
462 *
463 * Return the data segment length.
464 */
465static inline unsigned int
466ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
467{
468 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
469 MASK_RSP_UPIU_DATA_SEG_LEN;
470}
471
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530472/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530473 * ufshcd_is_exception_event - Check if the device raised an exception event
474 * @ucd_rsp_ptr: pointer to response UPIU
475 *
476 * The function checks if the device raised an exception event indicated in
477 * the Device Information field of response UPIU.
478 *
479 * Returns true if exception is raised, false otherwise.
480 */
481static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
482{
483 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
484 MASK_RSP_EXCEPTION_EVENT ? true : false;
485}
486
487/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530488 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530489 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530490 */
491static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530492ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530493{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530494 ufshcd_writel(hba, INT_AGGR_ENABLE |
495 INT_AGGR_COUNTER_AND_TIMER_RESET,
496 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
497}
498
499/**
500 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
501 * @hba: per adapter instance
502 * @cnt: Interrupt aggregation counter threshold
503 * @tmout: Interrupt aggregation timeout value
504 */
505static inline void
506ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
507{
508 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
509 INT_AGGR_COUNTER_THLD_VAL(cnt) |
510 INT_AGGR_TIMEOUT_VAL(tmout),
511 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530512}
513
514/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300515 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
516 * @hba: per adapter instance
517 */
518static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
519{
520 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
521}
522
523/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530524 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
525 * When run-stop registers are set to 1, it indicates the
526 * host controller that it can process the requests
527 * @hba: per adapter instance
528 */
529static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
530{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530531 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
532 REG_UTP_TASK_REQ_LIST_RUN_STOP);
533 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
534 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530535}
536
537/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530538 * ufshcd_hba_start - Start controller initialization sequence
539 * @hba: per adapter instance
540 */
541static inline void ufshcd_hba_start(struct ufs_hba *hba)
542{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530543 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530544}
545
546/**
547 * ufshcd_is_hba_active - Get controller state
548 * @hba: per adapter instance
549 *
550 * Returns zero if controller is active, 1 otherwise
551 */
552static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
553{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530554 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530555}
556
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300557static void ufshcd_ungate_work(struct work_struct *work)
558{
559 int ret;
560 unsigned long flags;
561 struct ufs_hba *hba = container_of(work, struct ufs_hba,
562 clk_gating.ungate_work);
563
564 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
565
566 spin_lock_irqsave(hba->host->host_lock, flags);
567 if (hba->clk_gating.state == CLKS_ON) {
568 spin_unlock_irqrestore(hba->host->host_lock, flags);
569 goto unblock_reqs;
570 }
571
572 spin_unlock_irqrestore(hba->host->host_lock, flags);
573 ufshcd_setup_clocks(hba, true);
574
575 /* Exit from hibern8 */
576 if (ufshcd_can_hibern8_during_gating(hba)) {
577 /* Prevent gating in this path */
578 hba->clk_gating.is_suspended = true;
579 if (ufshcd_is_link_hibern8(hba)) {
580 ret = ufshcd_uic_hibern8_exit(hba);
581 if (ret)
582 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
583 __func__, ret);
584 else
585 ufshcd_set_link_active(hba);
586 }
587 hba->clk_gating.is_suspended = false;
588 }
589unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300590 if (ufshcd_is_clkscaling_enabled(hba))
591 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300592 scsi_unblock_requests(hba->host);
593}
594
595/**
596 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
597 * Also, exit from hibern8 mode and set the link as active.
598 * @hba: per adapter instance
599 * @async: This indicates whether caller should ungate clocks asynchronously.
600 */
601int ufshcd_hold(struct ufs_hba *hba, bool async)
602{
603 int rc = 0;
604 unsigned long flags;
605
606 if (!ufshcd_is_clkgating_allowed(hba))
607 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300608 spin_lock_irqsave(hba->host->host_lock, flags);
609 hba->clk_gating.active_reqs++;
610
Sahitya Tummala856b3482014-09-25 15:32:34 +0300611start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300612 switch (hba->clk_gating.state) {
613 case CLKS_ON:
614 break;
615 case REQ_CLKS_OFF:
616 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
617 hba->clk_gating.state = CLKS_ON;
618 break;
619 }
620 /*
621 * If we here, it means gating work is either done or
622 * currently running. Hence, fall through to cancel gating
623 * work and to enable clocks.
624 */
625 case CLKS_OFF:
626 scsi_block_requests(hba->host);
627 hba->clk_gating.state = REQ_CLKS_ON;
628 schedule_work(&hba->clk_gating.ungate_work);
629 /*
630 * fall through to check if we should wait for this
631 * work to be done or not.
632 */
633 case REQ_CLKS_ON:
634 if (async) {
635 rc = -EAGAIN;
636 hba->clk_gating.active_reqs--;
637 break;
638 }
639
640 spin_unlock_irqrestore(hba->host->host_lock, flags);
641 flush_work(&hba->clk_gating.ungate_work);
642 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300643 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300644 goto start;
645 default:
646 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
647 __func__, hba->clk_gating.state);
648 break;
649 }
650 spin_unlock_irqrestore(hba->host->host_lock, flags);
651out:
652 return rc;
653}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200654EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300655
656static void ufshcd_gate_work(struct work_struct *work)
657{
658 struct ufs_hba *hba = container_of(work, struct ufs_hba,
659 clk_gating.gate_work.work);
660 unsigned long flags;
661
662 spin_lock_irqsave(hba->host->host_lock, flags);
663 if (hba->clk_gating.is_suspended) {
664 hba->clk_gating.state = CLKS_ON;
665 goto rel_lock;
666 }
667
668 if (hba->clk_gating.active_reqs
669 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
670 || hba->lrb_in_use || hba->outstanding_tasks
671 || hba->active_uic_cmd || hba->uic_async_done)
672 goto rel_lock;
673
674 spin_unlock_irqrestore(hba->host->host_lock, flags);
675
676 /* put the link into hibern8 mode before turning off clocks */
677 if (ufshcd_can_hibern8_during_gating(hba)) {
678 if (ufshcd_uic_hibern8_enter(hba)) {
679 hba->clk_gating.state = CLKS_ON;
680 goto out;
681 }
682 ufshcd_set_link_hibern8(hba);
683 }
684
Sahitya Tummala856b3482014-09-25 15:32:34 +0300685 if (ufshcd_is_clkscaling_enabled(hba)) {
686 devfreq_suspend_device(hba->devfreq);
687 hba->clk_scaling.window_start_t = 0;
688 }
689
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300690 if (!ufshcd_is_link_active(hba))
691 ufshcd_setup_clocks(hba, false);
692 else
693 /* If link is active, device ref_clk can't be switched off */
694 __ufshcd_setup_clocks(hba, false, true);
695
696 /*
697 * In case you are here to cancel this work the gating state
698 * would be marked as REQ_CLKS_ON. In this case keep the state
699 * as REQ_CLKS_ON which would anyway imply that clocks are off
700 * and a request to turn them on is pending. By doing this way,
701 * we keep the state machine in tact and this would ultimately
702 * prevent from doing cancel work multiple times when there are
703 * new requests arriving before the current cancel work is done.
704 */
705 spin_lock_irqsave(hba->host->host_lock, flags);
706 if (hba->clk_gating.state == REQ_CLKS_OFF)
707 hba->clk_gating.state = CLKS_OFF;
708
709rel_lock:
710 spin_unlock_irqrestore(hba->host->host_lock, flags);
711out:
712 return;
713}
714
715/* host lock must be held before calling this variant */
716static void __ufshcd_release(struct ufs_hba *hba)
717{
718 if (!ufshcd_is_clkgating_allowed(hba))
719 return;
720
721 hba->clk_gating.active_reqs--;
722
723 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
724 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
725 || hba->lrb_in_use || hba->outstanding_tasks
726 || hba->active_uic_cmd || hba->uic_async_done)
727 return;
728
729 hba->clk_gating.state = REQ_CLKS_OFF;
730 schedule_delayed_work(&hba->clk_gating.gate_work,
731 msecs_to_jiffies(hba->clk_gating.delay_ms));
732}
733
734void ufshcd_release(struct ufs_hba *hba)
735{
736 unsigned long flags;
737
738 spin_lock_irqsave(hba->host->host_lock, flags);
739 __ufshcd_release(hba);
740 spin_unlock_irqrestore(hba->host->host_lock, flags);
741}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200742EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300743
744static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
745 struct device_attribute *attr, char *buf)
746{
747 struct ufs_hba *hba = dev_get_drvdata(dev);
748
749 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
750}
751
752static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
753 struct device_attribute *attr, const char *buf, size_t count)
754{
755 struct ufs_hba *hba = dev_get_drvdata(dev);
756 unsigned long flags, value;
757
758 if (kstrtoul(buf, 0, &value))
759 return -EINVAL;
760
761 spin_lock_irqsave(hba->host->host_lock, flags);
762 hba->clk_gating.delay_ms = value;
763 spin_unlock_irqrestore(hba->host->host_lock, flags);
764 return count;
765}
766
767static void ufshcd_init_clk_gating(struct ufs_hba *hba)
768{
769 if (!ufshcd_is_clkgating_allowed(hba))
770 return;
771
772 hba->clk_gating.delay_ms = 150;
773 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
774 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
775
776 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
777 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
778 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
779 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
780 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
781 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
782 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
783}
784
785static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
786{
787 if (!ufshcd_is_clkgating_allowed(hba))
788 return;
789 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900790 cancel_work_sync(&hba->clk_gating.ungate_work);
791 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300792}
793
Sahitya Tummala856b3482014-09-25 15:32:34 +0300794/* Must be called with host lock acquired */
795static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
796{
797 if (!ufshcd_is_clkscaling_enabled(hba))
798 return;
799
800 if (!hba->clk_scaling.is_busy_started) {
801 hba->clk_scaling.busy_start_t = ktime_get();
802 hba->clk_scaling.is_busy_started = true;
803 }
804}
805
806static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
807{
808 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
809
810 if (!ufshcd_is_clkscaling_enabled(hba))
811 return;
812
813 if (!hba->outstanding_reqs && scaling->is_busy_started) {
814 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
815 scaling->busy_start_t));
816 scaling->busy_start_t = ktime_set(0, 0);
817 scaling->is_busy_started = false;
818 }
819}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530820/**
821 * ufshcd_send_command - Send SCSI or device management commands
822 * @hba: per adapter instance
823 * @task_tag: Task tag of the command
824 */
825static inline
826void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
827{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300828 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530829 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530830 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530831}
832
833/**
834 * ufshcd_copy_sense_data - Copy sense data in case of check condition
835 * @lrb - pointer to local reference block
836 */
837static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
838{
839 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530840 if (lrbp->sense_buffer &&
841 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530842 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530843 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530844 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530845 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
846 }
847}
848
849/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530850 * ufshcd_copy_query_response() - Copy the Query Response and the data
851 * descriptor
852 * @hba: per adapter instance
853 * @lrb - pointer to local reference block
854 */
855static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300856int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530857{
858 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
859
Dolev Raviv68078d52013-07-30 00:35:58 +0530860 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530861
Dolev Raviv68078d52013-07-30 00:35:58 +0530862 /* Get the descriptor */
863 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300864 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530865 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300866 u16 resp_len;
867 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530868
869 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300870 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530871 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300872 buf_len = be16_to_cpu(
873 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300874 if (likely(buf_len >= resp_len)) {
875 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
876 } else {
877 dev_warn(hba->dev,
878 "%s: Response size is bigger than buffer",
879 __func__);
880 return -EINVAL;
881 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530882 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300883
884 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530885}
886
887/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530888 * ufshcd_hba_capabilities - Read controller capabilities
889 * @hba: per adapter instance
890 */
891static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
892{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530893 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530894
895 /* nutrs and nutmrs are 0 based values */
896 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
897 hba->nutmrs =
898 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
899}
900
901/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530902 * ufshcd_ready_for_uic_cmd - Check if controller is ready
903 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530904 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530905 * Return true on success, else false
906 */
907static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
908{
909 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
910 return true;
911 else
912 return false;
913}
914
915/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530916 * ufshcd_get_upmcrs - Get the power mode change request status
917 * @hba: Pointer to adapter instance
918 *
919 * This function gets the UPMCRS field of HCS register
920 * Returns value of UPMCRS field
921 */
922static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
923{
924 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
925}
926
927/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530928 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
929 * @hba: per adapter instance
930 * @uic_cmd: UIC command
931 *
932 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530933 */
934static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530935ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530936{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530937 WARN_ON(hba->active_uic_cmd);
938
939 hba->active_uic_cmd = uic_cmd;
940
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530941 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530942 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
943 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
944 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530945
946 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530947 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530948 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530949}
950
951/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530952 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
953 * @hba: per adapter instance
954 * @uic_command: UIC command
955 *
956 * Must be called with mutex held.
957 * Returns 0 only if success.
958 */
959static int
960ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
961{
962 int ret;
963 unsigned long flags;
964
965 if (wait_for_completion_timeout(&uic_cmd->done,
966 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
967 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
968 else
969 ret = -ETIMEDOUT;
970
971 spin_lock_irqsave(hba->host->host_lock, flags);
972 hba->active_uic_cmd = NULL;
973 spin_unlock_irqrestore(hba->host->host_lock, flags);
974
975 return ret;
976}
977
978/**
979 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
980 * @hba: per adapter instance
981 * @uic_cmd: UIC command
982 *
983 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300984 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530985 * Returns 0 only if success.
986 */
987static int
988__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
989{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530990 if (!ufshcd_ready_for_uic_cmd(hba)) {
991 dev_err(hba->dev,
992 "Controller not ready to accept UIC commands\n");
993 return -EIO;
994 }
995
996 init_completion(&uic_cmd->done);
997
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530998 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530999
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001000 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301001}
1002
1003/**
1004 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1005 * @hba: per adapter instance
1006 * @uic_cmd: UIC command
1007 *
1008 * Returns 0 only if success.
1009 */
1010static int
1011ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1012{
1013 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001014 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301015
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001016 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301017 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001018 ufshcd_add_delay_before_dme_cmd(hba);
1019
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001020 spin_lock_irqsave(hba->host->host_lock, flags);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301021 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001022 spin_unlock_irqrestore(hba->host->host_lock, flags);
1023 if (!ret)
1024 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1025
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301026 mutex_unlock(&hba->uic_cmd_mutex);
1027
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001028 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301029 return ret;
1030}
1031
1032/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301033 * ufshcd_map_sg - Map scatter-gather list to prdt
1034 * @lrbp - pointer to local reference block
1035 *
1036 * Returns 0 in case of success, non-zero value in case of failure
1037 */
1038static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1039{
1040 struct ufshcd_sg_entry *prd_table;
1041 struct scatterlist *sg;
1042 struct scsi_cmnd *cmd;
1043 int sg_segments;
1044 int i;
1045
1046 cmd = lrbp->cmd;
1047 sg_segments = scsi_dma_map(cmd);
1048 if (sg_segments < 0)
1049 return sg_segments;
1050
1051 if (sg_segments) {
1052 lrbp->utr_descriptor_ptr->prd_table_length =
1053 cpu_to_le16((u16) (sg_segments));
1054
1055 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1056
1057 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1058 prd_table[i].size =
1059 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1060 prd_table[i].base_addr =
1061 cpu_to_le32(lower_32_bits(sg->dma_address));
1062 prd_table[i].upper_addr =
1063 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001064 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301065 }
1066 } else {
1067 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1068 }
1069
1070 return 0;
1071}
1072
1073/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301074 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301075 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301076 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301077 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301078static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301079{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301080 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1081
1082 if (hba->ufs_version == UFSHCI_VERSION_10) {
1083 u32 rw;
1084 rw = set & INTERRUPT_MASK_RW_VER_10;
1085 set = rw | ((set ^ intrs) & intrs);
1086 } else {
1087 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301088 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301089
1090 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1091}
1092
1093/**
1094 * ufshcd_disable_intr - disable interrupts
1095 * @hba: per adapter instance
1096 * @intrs: interrupt bits
1097 */
1098static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1099{
1100 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1101
1102 if (hba->ufs_version == UFSHCI_VERSION_10) {
1103 u32 rw;
1104 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1105 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1106 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1107
1108 } else {
1109 set &= ~intrs;
1110 }
1111
1112 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301113}
1114
1115/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301116 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1117 * descriptor according to request
1118 * @lrbp: pointer to local reference block
1119 * @upiu_flags: flags required in the header
1120 * @cmd_dir: requests data direction
1121 */
1122static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
1123 u32 *upiu_flags, enum dma_data_direction cmd_dir)
1124{
1125 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1126 u32 data_direction;
1127 u32 dword_0;
1128
1129 if (cmd_dir == DMA_FROM_DEVICE) {
1130 data_direction = UTP_DEVICE_TO_HOST;
1131 *upiu_flags = UPIU_CMD_FLAGS_READ;
1132 } else if (cmd_dir == DMA_TO_DEVICE) {
1133 data_direction = UTP_HOST_TO_DEVICE;
1134 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1135 } else {
1136 data_direction = UTP_NO_DATA_TRANSFER;
1137 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1138 }
1139
1140 dword_0 = data_direction | (lrbp->command_type
1141 << UPIU_COMMAND_TYPE_OFFSET);
1142 if (lrbp->intr_cmd)
1143 dword_0 |= UTP_REQ_DESC_INT_CMD;
1144
1145 /* Transfer request descriptor header fields */
1146 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001147 /* dword_1 is reserved, hence it is set to 0 */
1148 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301149 /*
1150 * assigning invalid value for command status. Controller
1151 * updates OCS on command completion, with the command
1152 * status
1153 */
1154 req_desc->header.dword_2 =
1155 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001156 /* dword_3 is reserved, hence it is set to 0 */
1157 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001158
1159 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301160}
1161
1162/**
1163 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1164 * for scsi commands
1165 * @lrbp - local reference block pointer
1166 * @upiu_flags - flags
1167 */
1168static
1169void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1170{
1171 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001172 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301173
1174 /* command descriptor fields */
1175 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1176 UPIU_TRANSACTION_COMMAND, upiu_flags,
1177 lrbp->lun, lrbp->task_tag);
1178 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1179 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1180
1181 /* Total EHS length and Data segment length will be zero */
1182 ucd_req_ptr->header.dword_2 = 0;
1183
1184 ucd_req_ptr->sc.exp_data_transfer_len =
1185 cpu_to_be32(lrbp->cmd->sdb.length);
1186
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001187 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1188 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1189 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1190
1191 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301192}
1193
Dolev Raviv68078d52013-07-30 00:35:58 +05301194/**
1195 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1196 * for query requsts
1197 * @hba: UFS hba
1198 * @lrbp: local reference block pointer
1199 * @upiu_flags: flags
1200 */
1201static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1202 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1203{
1204 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1205 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301206 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301207 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1208
1209 /* Query request header */
1210 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1211 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1212 lrbp->lun, lrbp->task_tag);
1213 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1214 0, query->request.query_func, 0, 0);
1215
1216 /* Data segment length */
1217 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
1218 0, 0, len >> 8, (u8)len);
1219
1220 /* Copy the Query Request buffer as is */
1221 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1222 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301223
1224 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001225 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1226 memcpy(descp, query->descriptor, len);
1227
Yaniv Gardi51047262016-02-01 15:02:38 +02001228 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301229}
1230
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301231static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1232{
1233 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1234
1235 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1236
1237 /* command descriptor fields */
1238 ucd_req_ptr->header.dword_0 =
1239 UPIU_HEADER_DWORD(
1240 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001241 /* clear rest of the fields of basic header */
1242 ucd_req_ptr->header.dword_1 = 0;
1243 ucd_req_ptr->header.dword_2 = 0;
1244
1245 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301246}
1247
1248/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301249 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301250 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301251 * @lrb - pointer to local reference block
1252 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301253static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301254{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301255 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301256 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301257
1258 switch (lrbp->command_type) {
1259 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301260 if (likely(lrbp->cmd)) {
1261 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1262 lrbp->cmd->sc_data_direction);
1263 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301264 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301265 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301266 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301267 break;
1268 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301269 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301270 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1271 ufshcd_prepare_utp_query_req_upiu(
1272 hba, lrbp, upiu_flags);
1273 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301274 ufshcd_prepare_utp_nop_upiu(lrbp);
1275 else
1276 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301277 break;
1278 case UTP_CMD_TYPE_UFS:
1279 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301280 ret = -ENOTSUPP;
1281 dev_err(hba->dev, "%s: UFS native command are not supported\n",
1282 __func__);
1283 break;
1284 default:
1285 ret = -ENOTSUPP;
1286 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
1287 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301288 break;
1289 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301290
1291 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301292}
1293
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001294/*
1295 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1296 * @scsi_lun: scsi LUN id
1297 *
1298 * Returns UPIU LUN id
1299 */
1300static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1301{
1302 if (scsi_is_wlun(scsi_lun))
1303 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1304 | UFS_UPIU_WLUN_ID;
1305 else
1306 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1307}
1308
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301309/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001310 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1311 * @scsi_lun: UPIU W-LUN id
1312 *
1313 * Returns SCSI W-LUN id
1314 */
1315static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1316{
1317 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1318}
1319
1320/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301321 * ufshcd_queuecommand - main entry point for SCSI requests
1322 * @cmd: command from SCSI Midlayer
1323 * @done: call back function
1324 *
1325 * Returns 0 for success, non-zero in case of failure
1326 */
1327static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1328{
1329 struct ufshcd_lrb *lrbp;
1330 struct ufs_hba *hba;
1331 unsigned long flags;
1332 int tag;
1333 int err = 0;
1334
1335 hba = shost_priv(host);
1336
1337 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001338 if (!ufshcd_valid_tag(hba, tag)) {
1339 dev_err(hba->dev,
1340 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1341 __func__, tag, cmd, cmd->request);
1342 BUG();
1343 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301344
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301345 spin_lock_irqsave(hba->host->host_lock, flags);
1346 switch (hba->ufshcd_state) {
1347 case UFSHCD_STATE_OPERATIONAL:
1348 break;
1349 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301350 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301351 goto out_unlock;
1352 case UFSHCD_STATE_ERROR:
1353 set_host_byte(cmd, DID_ERROR);
1354 cmd->scsi_done(cmd);
1355 goto out_unlock;
1356 default:
1357 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1358 __func__, hba->ufshcd_state);
1359 set_host_byte(cmd, DID_BAD_TARGET);
1360 cmd->scsi_done(cmd);
1361 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301362 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301363 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301364
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301365 /* acquire the tag to make sure device cmds don't use it */
1366 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1367 /*
1368 * Dev manage command in progress, requeue the command.
1369 * Requeuing the command helps in cases where the request *may*
1370 * find different tag instead of waiting for dev manage command
1371 * completion.
1372 */
1373 err = SCSI_MLQUEUE_HOST_BUSY;
1374 goto out;
1375 }
1376
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001377 err = ufshcd_hold(hba, true);
1378 if (err) {
1379 err = SCSI_MLQUEUE_HOST_BUSY;
1380 clear_bit_unlock(tag, &hba->lrb_in_use);
1381 goto out;
1382 }
1383 WARN_ON(hba->clk_gating.state != CLKS_ON);
1384
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301385 lrbp = &hba->lrb[tag];
1386
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301387 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301388 lrbp->cmd = cmd;
1389 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1390 lrbp->sense_buffer = cmd->sense_buffer;
1391 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001392 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001393 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301394 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1395
1396 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301397 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301398 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301399 if (err) {
1400 lrbp->cmd = NULL;
1401 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301402 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301403 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301404
1405 /* issue command to the controller */
1406 spin_lock_irqsave(hba->host->host_lock, flags);
1407 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301408out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301409 spin_unlock_irqrestore(hba->host->host_lock, flags);
1410out:
1411 return err;
1412}
1413
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301414static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1415 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1416{
1417 lrbp->cmd = NULL;
1418 lrbp->sense_bufflen = 0;
1419 lrbp->sense_buffer = NULL;
1420 lrbp->task_tag = tag;
1421 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
1422 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1423 lrbp->intr_cmd = true; /* No interrupt aggregation */
1424 hba->dev_cmd.type = cmd_type;
1425
1426 return ufshcd_compose_upiu(hba, lrbp);
1427}
1428
1429static int
1430ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1431{
1432 int err = 0;
1433 unsigned long flags;
1434 u32 mask = 1 << tag;
1435
1436 /* clear outstanding transaction before retry */
1437 spin_lock_irqsave(hba->host->host_lock, flags);
1438 ufshcd_utrl_clear(hba, tag);
1439 spin_unlock_irqrestore(hba->host->host_lock, flags);
1440
1441 /*
1442 * wait for for h/w to clear corresponding bit in door-bell.
1443 * max. wait is 1 sec.
1444 */
1445 err = ufshcd_wait_for_register(hba,
1446 REG_UTP_TRANSFER_REQ_DOOR_BELL,
1447 mask, ~mask, 1000, 1000);
1448
1449 return err;
1450}
1451
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001452static int
1453ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1454{
1455 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1456
1457 /* Get the UPIU response */
1458 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1459 UPIU_RSP_CODE_OFFSET;
1460 return query_res->response;
1461}
1462
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301463/**
1464 * ufshcd_dev_cmd_completion() - handles device management command responses
1465 * @hba: per adapter instance
1466 * @lrbp: pointer to local reference block
1467 */
1468static int
1469ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1470{
1471 int resp;
1472 int err = 0;
1473
1474 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1475
1476 switch (resp) {
1477 case UPIU_TRANSACTION_NOP_IN:
1478 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1479 err = -EINVAL;
1480 dev_err(hba->dev, "%s: unexpected response %x\n",
1481 __func__, resp);
1482 }
1483 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301484 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001485 err = ufshcd_check_query_response(hba, lrbp);
1486 if (!err)
1487 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301488 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301489 case UPIU_TRANSACTION_REJECT_UPIU:
1490 /* TODO: handle Reject UPIU Response */
1491 err = -EPERM;
1492 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1493 __func__);
1494 break;
1495 default:
1496 err = -EINVAL;
1497 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1498 __func__, resp);
1499 break;
1500 }
1501
1502 return err;
1503}
1504
1505static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1506 struct ufshcd_lrb *lrbp, int max_timeout)
1507{
1508 int err = 0;
1509 unsigned long time_left;
1510 unsigned long flags;
1511
1512 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1513 msecs_to_jiffies(max_timeout));
1514
1515 spin_lock_irqsave(hba->host->host_lock, flags);
1516 hba->dev_cmd.complete = NULL;
1517 if (likely(time_left)) {
1518 err = ufshcd_get_tr_ocs(lrbp);
1519 if (!err)
1520 err = ufshcd_dev_cmd_completion(hba, lrbp);
1521 }
1522 spin_unlock_irqrestore(hba->host->host_lock, flags);
1523
1524 if (!time_left) {
1525 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001526 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1527 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301528 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001529 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301530 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001531 /*
1532 * in case of an error, after clearing the doorbell,
1533 * we also need to clear the outstanding_request
1534 * field in hba
1535 */
1536 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301537 }
1538
1539 return err;
1540}
1541
1542/**
1543 * ufshcd_get_dev_cmd_tag - Get device management command tag
1544 * @hba: per-adapter instance
1545 * @tag: pointer to variable with available slot value
1546 *
1547 * Get a free slot and lock it until device management command
1548 * completes.
1549 *
1550 * Returns false if free slot is unavailable for locking, else
1551 * return true with tag value in @tag.
1552 */
1553static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1554{
1555 int tag;
1556 bool ret = false;
1557 unsigned long tmp;
1558
1559 if (!tag_out)
1560 goto out;
1561
1562 do {
1563 tmp = ~hba->lrb_in_use;
1564 tag = find_last_bit(&tmp, hba->nutrs);
1565 if (tag >= hba->nutrs)
1566 goto out;
1567 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1568
1569 *tag_out = tag;
1570 ret = true;
1571out:
1572 return ret;
1573}
1574
1575static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1576{
1577 clear_bit_unlock(tag, &hba->lrb_in_use);
1578}
1579
1580/**
1581 * ufshcd_exec_dev_cmd - API for sending device management requests
1582 * @hba - UFS hba
1583 * @cmd_type - specifies the type (NOP, Query...)
1584 * @timeout - time in seconds
1585 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301586 * NOTE: Since there is only one available tag for device management commands,
1587 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301588 */
1589static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1590 enum dev_cmd_type cmd_type, int timeout)
1591{
1592 struct ufshcd_lrb *lrbp;
1593 int err;
1594 int tag;
1595 struct completion wait;
1596 unsigned long flags;
1597
1598 /*
1599 * Get free slot, sleep if slots are unavailable.
1600 * Even though we use wait_event() which sleeps indefinitely,
1601 * the maximum wait time is bounded by SCSI request timeout.
1602 */
1603 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1604
1605 init_completion(&wait);
1606 lrbp = &hba->lrb[tag];
1607 WARN_ON(lrbp->cmd);
1608 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1609 if (unlikely(err))
1610 goto out_put_tag;
1611
1612 hba->dev_cmd.complete = &wait;
1613
1614 spin_lock_irqsave(hba->host->host_lock, flags);
1615 ufshcd_send_command(hba, tag);
1616 spin_unlock_irqrestore(hba->host->host_lock, flags);
1617
1618 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1619
1620out_put_tag:
1621 ufshcd_put_dev_cmd_tag(hba, tag);
1622 wake_up(&hba->dev_cmd.tag_wq);
1623 return err;
1624}
1625
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301626/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001627 * ufshcd_init_query() - init the query response and request parameters
1628 * @hba: per-adapter instance
1629 * @request: address of the request pointer to be initialized
1630 * @response: address of the response pointer to be initialized
1631 * @opcode: operation to perform
1632 * @idn: flag idn to access
1633 * @index: LU number to access
1634 * @selector: query/flag/descriptor further identification
1635 */
1636static inline void ufshcd_init_query(struct ufs_hba *hba,
1637 struct ufs_query_req **request, struct ufs_query_res **response,
1638 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1639{
1640 *request = &hba->dev_cmd.query.request;
1641 *response = &hba->dev_cmd.query.response;
1642 memset(*request, 0, sizeof(struct ufs_query_req));
1643 memset(*response, 0, sizeof(struct ufs_query_res));
1644 (*request)->upiu_req.opcode = opcode;
1645 (*request)->upiu_req.idn = idn;
1646 (*request)->upiu_req.index = index;
1647 (*request)->upiu_req.selector = selector;
1648}
1649
1650/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301651 * ufshcd_query_flag() - API function for sending flag query requests
1652 * hba: per-adapter instance
1653 * query_opcode: flag query to perform
1654 * idn: flag idn to access
1655 * flag_res: the flag value after the query request completes
1656 *
1657 * Returns 0 for success, non-zero in case of failure
1658 */
1659static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1660 enum flag_idn idn, bool *flag_res)
1661{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001662 struct ufs_query_req *request = NULL;
1663 struct ufs_query_res *response = NULL;
1664 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001665 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301666
1667 BUG_ON(!hba);
1668
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001669 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301670 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001671 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1672 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301673
1674 switch (opcode) {
1675 case UPIU_QUERY_OPCODE_SET_FLAG:
1676 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1677 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1678 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1679 break;
1680 case UPIU_QUERY_OPCODE_READ_FLAG:
1681 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1682 if (!flag_res) {
1683 /* No dummy reads */
1684 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1685 __func__);
1686 err = -EINVAL;
1687 goto out_unlock;
1688 }
1689 break;
1690 default:
1691 dev_err(hba->dev,
1692 "%s: Expected query flag opcode but got = %d\n",
1693 __func__, opcode);
1694 err = -EINVAL;
1695 goto out_unlock;
1696 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301697
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001698 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1699 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1700
1701 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301702
1703 if (err) {
1704 dev_err(hba->dev,
1705 "%s: Sending flag query for idn %d failed, err = %d\n",
1706 __func__, idn, err);
1707 goto out_unlock;
1708 }
1709
1710 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301711 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301712 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1713
1714out_unlock:
1715 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001716 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301717 return err;
1718}
1719
1720/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301721 * ufshcd_query_attr - API function for sending attribute requests
1722 * hba: per-adapter instance
1723 * opcode: attribute opcode
1724 * idn: attribute idn to access
1725 * index: index field
1726 * selector: selector field
1727 * attr_val: the attribute value after the query request completes
1728 *
1729 * Returns 0 for success, non-zero in case of failure
1730*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301731static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301732 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1733{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001734 struct ufs_query_req *request = NULL;
1735 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301736 int err;
1737
1738 BUG_ON(!hba);
1739
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001740 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301741 if (!attr_val) {
1742 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1743 __func__, opcode);
1744 err = -EINVAL;
1745 goto out;
1746 }
1747
1748 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001749 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1750 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301751
1752 switch (opcode) {
1753 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1754 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301755 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301756 break;
1757 case UPIU_QUERY_OPCODE_READ_ATTR:
1758 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1759 break;
1760 default:
1761 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1762 __func__, opcode);
1763 err = -EINVAL;
1764 goto out_unlock;
1765 }
1766
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001767 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301768
1769 if (err) {
1770 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1771 __func__, opcode, idn, err);
1772 goto out_unlock;
1773 }
1774
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301775 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301776
1777out_unlock:
1778 mutex_unlock(&hba->dev_cmd.lock);
1779out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001780 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301781 return err;
1782}
1783
1784/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001785 * ufshcd_query_descriptor - API function for sending descriptor requests
1786 * hba: per-adapter instance
1787 * opcode: attribute opcode
1788 * idn: attribute idn to access
1789 * index: index field
1790 * selector: selector field
1791 * desc_buf: the buffer that contains the descriptor
1792 * buf_len: length parameter passed to the device
1793 *
1794 * Returns 0 for success, non-zero in case of failure.
1795 * The buf_len parameter will contain, on return, the length parameter
1796 * received on the response.
1797 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001798static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001799 enum query_opcode opcode, enum desc_idn idn, u8 index,
1800 u8 selector, u8 *desc_buf, int *buf_len)
1801{
1802 struct ufs_query_req *request = NULL;
1803 struct ufs_query_res *response = NULL;
1804 int err;
1805
1806 BUG_ON(!hba);
1807
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001808 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001809 if (!desc_buf) {
1810 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1811 __func__, opcode);
1812 err = -EINVAL;
1813 goto out;
1814 }
1815
1816 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1817 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1818 __func__, *buf_len);
1819 err = -EINVAL;
1820 goto out;
1821 }
1822
1823 mutex_lock(&hba->dev_cmd.lock);
1824 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1825 selector);
1826 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001827 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001828
1829 switch (opcode) {
1830 case UPIU_QUERY_OPCODE_WRITE_DESC:
1831 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1832 break;
1833 case UPIU_QUERY_OPCODE_READ_DESC:
1834 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1835 break;
1836 default:
1837 dev_err(hba->dev,
1838 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1839 __func__, opcode);
1840 err = -EINVAL;
1841 goto out_unlock;
1842 }
1843
1844 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1845
1846 if (err) {
1847 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1848 __func__, opcode, idn, err);
1849 goto out_unlock;
1850 }
1851
1852 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001853 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001854
1855out_unlock:
1856 mutex_unlock(&hba->dev_cmd.lock);
1857out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001858 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001859 return err;
1860}
1861
1862/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001863 * ufshcd_read_desc_param - read the specified descriptor parameter
1864 * @hba: Pointer to adapter instance
1865 * @desc_id: descriptor idn value
1866 * @desc_index: descriptor index
1867 * @param_offset: offset of the parameter to read
1868 * @param_read_buf: pointer to buffer where parameter would be read
1869 * @param_size: sizeof(param_read_buf)
1870 *
1871 * Return 0 in case of success, non-zero otherwise
1872 */
1873static int ufshcd_read_desc_param(struct ufs_hba *hba,
1874 enum desc_idn desc_id,
1875 int desc_index,
1876 u32 param_offset,
1877 u8 *param_read_buf,
1878 u32 param_size)
1879{
1880 int ret;
1881 u8 *desc_buf;
1882 u32 buff_len;
1883 bool is_kmalloc = true;
1884
1885 /* safety checks */
1886 if (desc_id >= QUERY_DESC_IDN_MAX)
1887 return -EINVAL;
1888
1889 buff_len = ufs_query_desc_max_size[desc_id];
1890 if ((param_offset + param_size) > buff_len)
1891 return -EINVAL;
1892
1893 if (!param_offset && (param_size == buff_len)) {
1894 /* memory space already available to hold full descriptor */
1895 desc_buf = param_read_buf;
1896 is_kmalloc = false;
1897 } else {
1898 /* allocate memory to hold full descriptor */
1899 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1900 if (!desc_buf)
1901 return -ENOMEM;
1902 }
1903
1904 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1905 desc_id, desc_index, 0, desc_buf,
1906 &buff_len);
1907
1908 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
1909 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
1910 ufs_query_desc_max_size[desc_id])
1911 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
1912 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
1913 __func__, desc_id, param_offset, buff_len, ret);
1914 if (!ret)
1915 ret = -EINVAL;
1916
1917 goto out;
1918 }
1919
1920 if (is_kmalloc)
1921 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1922out:
1923 if (is_kmalloc)
1924 kfree(desc_buf);
1925 return ret;
1926}
1927
1928static inline int ufshcd_read_desc(struct ufs_hba *hba,
1929 enum desc_idn desc_id,
1930 int desc_index,
1931 u8 *buf,
1932 u32 size)
1933{
1934 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1935}
1936
1937static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
1938 u8 *buf,
1939 u32 size)
1940{
1941 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
1942}
1943
1944/**
1945 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
1946 * @hba: Pointer to adapter instance
1947 * @lun: lun id
1948 * @param_offset: offset of the parameter to read
1949 * @param_read_buf: pointer to buffer where parameter would be read
1950 * @param_size: sizeof(param_read_buf)
1951 *
1952 * Return 0 in case of success, non-zero otherwise
1953 */
1954static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
1955 int lun,
1956 enum unit_desc_param param_offset,
1957 u8 *param_read_buf,
1958 u32 param_size)
1959{
1960 /*
1961 * Unit descriptors are only available for general purpose LUs (LUN id
1962 * from 0 to 7) and RPMB Well known LU.
1963 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001964 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001965 return -EOPNOTSUPP;
1966
1967 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
1968 param_offset, param_read_buf, param_size);
1969}
1970
1971/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301972 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1973 * @hba: per adapter instance
1974 *
1975 * 1. Allocate DMA memory for Command Descriptor array
1976 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1977 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1978 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1979 * (UTMRDL)
1980 * 4. Allocate memory for local reference block(lrb).
1981 *
1982 * Returns 0 for success, non-zero in case of failure
1983 */
1984static int ufshcd_memory_alloc(struct ufs_hba *hba)
1985{
1986 size_t utmrdl_size, utrdl_size, ucdl_size;
1987
1988 /* Allocate memory for UTP command descriptors */
1989 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001990 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
1991 ucdl_size,
1992 &hba->ucdl_dma_addr,
1993 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301994
1995 /*
1996 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1997 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1998 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1999 * be aligned to 128 bytes as well
2000 */
2001 if (!hba->ucdl_base_addr ||
2002 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302003 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302004 "Command Descriptor Memory allocation failed\n");
2005 goto out;
2006 }
2007
2008 /*
2009 * Allocate memory for UTP Transfer descriptors
2010 * UFSHCI requires 1024 byte alignment of UTRD
2011 */
2012 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002013 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2014 utrdl_size,
2015 &hba->utrdl_dma_addr,
2016 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302017 if (!hba->utrdl_base_addr ||
2018 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302019 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302020 "Transfer Descriptor Memory allocation failed\n");
2021 goto out;
2022 }
2023
2024 /*
2025 * Allocate memory for UTP Task Management descriptors
2026 * UFSHCI requires 1024 byte alignment of UTMRD
2027 */
2028 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002029 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2030 utmrdl_size,
2031 &hba->utmrdl_dma_addr,
2032 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302033 if (!hba->utmrdl_base_addr ||
2034 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302035 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302036 "Task Management Descriptor Memory allocation failed\n");
2037 goto out;
2038 }
2039
2040 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002041 hba->lrb = devm_kzalloc(hba->dev,
2042 hba->nutrs * sizeof(struct ufshcd_lrb),
2043 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302044 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302045 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302046 goto out;
2047 }
2048 return 0;
2049out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302050 return -ENOMEM;
2051}
2052
2053/**
2054 * ufshcd_host_memory_configure - configure local reference block with
2055 * memory offsets
2056 * @hba: per adapter instance
2057 *
2058 * Configure Host memory space
2059 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2060 * address.
2061 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2062 * and PRDT offset.
2063 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2064 * into local reference block.
2065 */
2066static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2067{
2068 struct utp_transfer_cmd_desc *cmd_descp;
2069 struct utp_transfer_req_desc *utrdlp;
2070 dma_addr_t cmd_desc_dma_addr;
2071 dma_addr_t cmd_desc_element_addr;
2072 u16 response_offset;
2073 u16 prdt_offset;
2074 int cmd_desc_size;
2075 int i;
2076
2077 utrdlp = hba->utrdl_base_addr;
2078 cmd_descp = hba->ucdl_base_addr;
2079
2080 response_offset =
2081 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2082 prdt_offset =
2083 offsetof(struct utp_transfer_cmd_desc, prd_table);
2084
2085 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2086 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2087
2088 for (i = 0; i < hba->nutrs; i++) {
2089 /* Configure UTRD with command descriptor base address */
2090 cmd_desc_element_addr =
2091 (cmd_desc_dma_addr + (cmd_desc_size * i));
2092 utrdlp[i].command_desc_base_addr_lo =
2093 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2094 utrdlp[i].command_desc_base_addr_hi =
2095 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2096
2097 /* Response upiu and prdt offset should be in double words */
2098 utrdlp[i].response_upiu_offset =
2099 cpu_to_le16((response_offset >> 2));
2100 utrdlp[i].prd_table_offset =
2101 cpu_to_le16((prdt_offset >> 2));
2102 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302103 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302104
2105 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302106 hba->lrb[i].ucd_req_ptr =
2107 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302108 hba->lrb[i].ucd_rsp_ptr =
2109 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2110 hba->lrb[i].ucd_prdt_ptr =
2111 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2112 }
2113}
2114
2115/**
2116 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2117 * @hba: per adapter instance
2118 *
2119 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2120 * in order to initialize the Unipro link startup procedure.
2121 * Once the Unipro links are up, the device connected to the controller
2122 * is detected.
2123 *
2124 * Returns 0 on success, non-zero value on failure
2125 */
2126static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2127{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302128 struct uic_command uic_cmd = {0};
2129 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302130
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302131 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2132
2133 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2134 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302135 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302136 "dme-link-startup: error code %d\n", ret);
2137 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302138}
2139
Yaniv Gardicad2e032015-03-31 17:37:14 +03002140static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2141{
2142 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2143 unsigned long min_sleep_time_us;
2144
2145 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2146 return;
2147
2148 /*
2149 * last_dme_cmd_tstamp will be 0 only for 1st call to
2150 * this function
2151 */
2152 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2153 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2154 } else {
2155 unsigned long delta =
2156 (unsigned long) ktime_to_us(
2157 ktime_sub(ktime_get(),
2158 hba->last_dme_cmd_tstamp));
2159
2160 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2161 min_sleep_time_us =
2162 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2163 else
2164 return; /* no more delay required */
2165 }
2166
2167 /* allow sleep for extra 50us if needed */
2168 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2169}
2170
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302171/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302172 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2173 * @hba: per adapter instance
2174 * @attr_sel: uic command argument1
2175 * @attr_set: attribute set type as uic command argument2
2176 * @mib_val: setting value as uic command argument3
2177 * @peer: indicate whether peer or local
2178 *
2179 * Returns 0 on success, non-zero value on failure
2180 */
2181int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2182 u8 attr_set, u32 mib_val, u8 peer)
2183{
2184 struct uic_command uic_cmd = {0};
2185 static const char *const action[] = {
2186 "dme-set",
2187 "dme-peer-set"
2188 };
2189 const char *set = action[!!peer];
2190 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002191 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302192
2193 uic_cmd.command = peer ?
2194 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2195 uic_cmd.argument1 = attr_sel;
2196 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2197 uic_cmd.argument3 = mib_val;
2198
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002199 do {
2200 /* for peer attributes we retry upon failure */
2201 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2202 if (ret)
2203 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2204 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2205 } while (ret && peer && --retries);
2206
2207 if (!retries)
2208 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2209 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2210 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302211
2212 return ret;
2213}
2214EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2215
2216/**
2217 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2218 * @hba: per adapter instance
2219 * @attr_sel: uic command argument1
2220 * @mib_val: the value of the attribute as returned by the UIC command
2221 * @peer: indicate whether peer or local
2222 *
2223 * Returns 0 on success, non-zero value on failure
2224 */
2225int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2226 u32 *mib_val, u8 peer)
2227{
2228 struct uic_command uic_cmd = {0};
2229 static const char *const action[] = {
2230 "dme-get",
2231 "dme-peer-get"
2232 };
2233 const char *get = action[!!peer];
2234 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002235 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002236 struct ufs_pa_layer_attr orig_pwr_info;
2237 struct ufs_pa_layer_attr temp_pwr_info;
2238 bool pwr_mode_change = false;
2239
2240 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2241 orig_pwr_info = hba->pwr_info;
2242 temp_pwr_info = orig_pwr_info;
2243
2244 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2245 orig_pwr_info.pwr_rx == FAST_MODE) {
2246 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2247 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2248 pwr_mode_change = true;
2249 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2250 orig_pwr_info.pwr_rx == SLOW_MODE) {
2251 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2252 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2253 pwr_mode_change = true;
2254 }
2255 if (pwr_mode_change) {
2256 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2257 if (ret)
2258 goto out;
2259 }
2260 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302261
2262 uic_cmd.command = peer ?
2263 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2264 uic_cmd.argument1 = attr_sel;
2265
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002266 do {
2267 /* for peer attributes we retry upon failure */
2268 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2269 if (ret)
2270 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2271 get, UIC_GET_ATTR_ID(attr_sel), ret);
2272 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302273
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002274 if (!retries)
2275 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2276 get, UIC_GET_ATTR_ID(attr_sel), retries);
2277
2278 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302279 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002280
2281 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2282 && pwr_mode_change)
2283 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302284out:
2285 return ret;
2286}
2287EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2288
2289/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002290 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2291 * state) and waits for it to take effect.
2292 *
2293 * @hba: per adapter instance
2294 * @cmd: UIC command to execute
2295 *
2296 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2297 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2298 * and device UniPro link and hence it's final completion would be indicated by
2299 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2300 * addition to normal UIC command completion Status (UCCS). This function only
2301 * returns after the relevant status bits indicate the completion.
2302 *
2303 * Returns 0 on success, non-zero value on failure
2304 */
2305static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2306{
2307 struct completion uic_async_done;
2308 unsigned long flags;
2309 u8 status;
2310 int ret;
2311
2312 mutex_lock(&hba->uic_cmd_mutex);
2313 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002314 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002315
2316 spin_lock_irqsave(hba->host->host_lock, flags);
2317 hba->uic_async_done = &uic_async_done;
2318 ret = __ufshcd_send_uic_cmd(hba, cmd);
2319 spin_unlock_irqrestore(hba->host->host_lock, flags);
2320 if (ret) {
2321 dev_err(hba->dev,
2322 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2323 cmd->command, cmd->argument3, ret);
2324 goto out;
2325 }
2326 ret = ufshcd_wait_for_uic_cmd(hba, cmd);
2327 if (ret) {
2328 dev_err(hba->dev,
2329 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2330 cmd->command, cmd->argument3, ret);
2331 goto out;
2332 }
2333
2334 if (!wait_for_completion_timeout(hba->uic_async_done,
2335 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2336 dev_err(hba->dev,
2337 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2338 cmd->command, cmd->argument3);
2339 ret = -ETIMEDOUT;
2340 goto out;
2341 }
2342
2343 status = ufshcd_get_upmcrs(hba);
2344 if (status != PWR_LOCAL) {
2345 dev_err(hba->dev,
2346 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
2347 cmd->command, status);
2348 ret = (status != PWR_OK) ? status : -1;
2349 }
2350out:
2351 spin_lock_irqsave(hba->host->host_lock, flags);
2352 hba->uic_async_done = NULL;
2353 spin_unlock_irqrestore(hba->host->host_lock, flags);
2354 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002355
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002356 return ret;
2357}
2358
2359/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302360 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2361 * using DME_SET primitives.
2362 * @hba: per adapter instance
2363 * @mode: powr mode value
2364 *
2365 * Returns 0 on success, non-zero value on failure
2366 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302367static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302368{
2369 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002370 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302371
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002372 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2373 ret = ufshcd_dme_set(hba,
2374 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2375 if (ret) {
2376 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2377 __func__, ret);
2378 goto out;
2379 }
2380 }
2381
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302382 uic_cmd.command = UIC_CMD_DME_SET;
2383 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2384 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002385 ufshcd_hold(hba, false);
2386 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2387 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302388
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002389out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002390 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002391}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302392
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002393static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002394{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002395 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002396 struct uic_command uic_cmd = {0};
2397
2398 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002399 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002400
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002401 if (ret)
2402 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2403 __func__, ret);
2404
2405 return ret;
2406}
2407
2408static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2409{
2410 int ret = 0, retries;
2411
2412 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2413 ret = __ufshcd_uic_hibern8_enter(hba);
2414 if (!ret || ret == -ENOLINK)
2415 goto out;
2416 }
2417out:
2418 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002419}
2420
2421static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2422{
2423 struct uic_command uic_cmd = {0};
2424 int ret;
2425
2426 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2427 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302428 if (ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002429 ufshcd_set_link_off(hba);
2430 ret = ufshcd_host_reset_and_restore(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302431 }
2432
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302433 return ret;
2434}
2435
Yaniv Gardi50646362014-10-23 13:25:13 +03002436 /**
2437 * ufshcd_init_pwr_info - setting the POR (power on reset)
2438 * values in hba power info
2439 * @hba: per-adapter instance
2440 */
2441static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2442{
2443 hba->pwr_info.gear_rx = UFS_PWM_G1;
2444 hba->pwr_info.gear_tx = UFS_PWM_G1;
2445 hba->pwr_info.lane_rx = 1;
2446 hba->pwr_info.lane_tx = 1;
2447 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2448 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2449 hba->pwr_info.hs_rate = 0;
2450}
2451
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302452/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002453 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2454 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302455 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002456static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302457{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002458 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2459
2460 if (hba->max_pwr_info.is_valid)
2461 return 0;
2462
2463 pwr_info->pwr_tx = FASTAUTO_MODE;
2464 pwr_info->pwr_rx = FASTAUTO_MODE;
2465 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302466
2467 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002468 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2469 &pwr_info->lane_rx);
2470 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2471 &pwr_info->lane_tx);
2472
2473 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2474 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2475 __func__,
2476 pwr_info->lane_rx,
2477 pwr_info->lane_tx);
2478 return -EINVAL;
2479 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302480
2481 /*
2482 * First, get the maximum gears of HS speed.
2483 * If a zero value, it means there is no HSGEAR capability.
2484 * Then, get the maximum gears of PWM speed.
2485 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002486 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2487 if (!pwr_info->gear_rx) {
2488 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2489 &pwr_info->gear_rx);
2490 if (!pwr_info->gear_rx) {
2491 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2492 __func__, pwr_info->gear_rx);
2493 return -EINVAL;
2494 }
2495 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302496 }
2497
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002498 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2499 &pwr_info->gear_tx);
2500 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302501 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002502 &pwr_info->gear_tx);
2503 if (!pwr_info->gear_tx) {
2504 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2505 __func__, pwr_info->gear_tx);
2506 return -EINVAL;
2507 }
2508 pwr_info->pwr_tx = SLOWAUTO_MODE;
2509 }
2510
2511 hba->max_pwr_info.is_valid = true;
2512 return 0;
2513}
2514
2515static int ufshcd_change_power_mode(struct ufs_hba *hba,
2516 struct ufs_pa_layer_attr *pwr_mode)
2517{
2518 int ret;
2519
2520 /* if already configured to the requested pwr_mode */
2521 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2522 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2523 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2524 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2525 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2526 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2527 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2528 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2529 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302530 }
2531
2532 /*
2533 * Configure attributes for power mode change with below.
2534 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2535 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2536 * - PA_HSSERIES
2537 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002538 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2539 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2540 pwr_mode->lane_rx);
2541 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2542 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302543 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002544 else
2545 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302546
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002547 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2548 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2549 pwr_mode->lane_tx);
2550 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2551 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302552 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002553 else
2554 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302555
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002556 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2557 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2558 pwr_mode->pwr_rx == FAST_MODE ||
2559 pwr_mode->pwr_tx == FAST_MODE)
2560 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2561 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302562
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002563 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2564 | pwr_mode->pwr_tx);
2565
2566 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302567 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002568 "%s: power mode change failed %d\n", __func__, ret);
2569 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002570 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2571 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002572
2573 memcpy(&hba->pwr_info, pwr_mode,
2574 sizeof(struct ufs_pa_layer_attr));
2575 }
2576
2577 return ret;
2578}
2579
2580/**
2581 * ufshcd_config_pwr_mode - configure a new power mode
2582 * @hba: per-adapter instance
2583 * @desired_pwr_mode: desired power configuration
2584 */
2585static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2586 struct ufs_pa_layer_attr *desired_pwr_mode)
2587{
2588 struct ufs_pa_layer_attr final_params = { 0 };
2589 int ret;
2590
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002591 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2592 desired_pwr_mode, &final_params);
2593
2594 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002595 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2596
2597 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302598
2599 return ret;
2600}
2601
2602/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302603 * ufshcd_complete_dev_init() - checks device readiness
2604 * hba: per-adapter instance
2605 *
2606 * Set fDeviceInit flag and poll until device toggles it.
2607 */
2608static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2609{
2610 int i, retries, err = 0;
2611 bool flag_res = 1;
2612
2613 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2614 /* Set the fDeviceInit flag */
2615 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2616 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
2617 if (!err || err == -ETIMEDOUT)
2618 break;
2619 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2620 }
2621 if (err) {
2622 dev_err(hba->dev,
2623 "%s setting fDeviceInit flag failed with error %d\n",
2624 __func__, err);
2625 goto out;
2626 }
2627
2628 /* poll for max. 100 iterations for fDeviceInit flag to clear */
2629 for (i = 0; i < 100 && !err && flag_res; i++) {
2630 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2631 err = ufshcd_query_flag(hba,
2632 UPIU_QUERY_OPCODE_READ_FLAG,
2633 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2634 if (!err || err == -ETIMEDOUT)
2635 break;
2636 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
2637 err);
2638 }
2639 }
2640 if (err)
2641 dev_err(hba->dev,
2642 "%s reading fDeviceInit flag failed with error %d\n",
2643 __func__, err);
2644 else if (flag_res)
2645 dev_err(hba->dev,
2646 "%s fDeviceInit was not cleared by the device\n",
2647 __func__);
2648
2649out:
2650 return err;
2651}
2652
2653/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302654 * ufshcd_make_hba_operational - Make UFS controller operational
2655 * @hba: per adapter instance
2656 *
2657 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002658 * 1. Enable required interrupts
2659 * 2. Configure interrupt aggregation
2660 * 3. Program UTRL and UTMRL base addres
2661 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302662 *
2663 * Returns 0 on success, non-zero value on failure
2664 */
2665static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2666{
2667 int err = 0;
2668 u32 reg;
2669
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302670 /* Enable required interrupts */
2671 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2672
2673 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002674 if (ufshcd_is_intr_aggr_allowed(hba))
2675 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2676 else
2677 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302678
2679 /* Configure UTRL and UTMRL base address registers */
2680 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2681 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2682 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2683 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2684 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2685 REG_UTP_TASK_REQ_LIST_BASE_L);
2686 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2687 REG_UTP_TASK_REQ_LIST_BASE_H);
2688
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302689 /*
2690 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
2691 * DEI, HEI bits must be 0
2692 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002693 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302694 if (!(ufshcd_get_lists_status(reg))) {
2695 ufshcd_enable_run_stop_reg(hba);
2696 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302697 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302698 "Host controller not ready to process requests");
2699 err = -EIO;
2700 goto out;
2701 }
2702
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302703out:
2704 return err;
2705}
2706
2707/**
2708 * ufshcd_hba_enable - initialize the controller
2709 * @hba: per adapter instance
2710 *
2711 * The controller resets itself and controller firmware initialization
2712 * sequence kicks off. When controller is ready it will set
2713 * the Host Controller Enable bit to 1.
2714 *
2715 * Returns 0 on success, non-zero value on failure
2716 */
2717static int ufshcd_hba_enable(struct ufs_hba *hba)
2718{
2719 int retry;
2720
2721 /*
2722 * msleep of 1 and 5 used in this function might result in msleep(20),
2723 * but it was necessary to send the UFS FPGA to reset mode during
2724 * development and testing of this driver. msleep can be changed to
2725 * mdelay and retry count can be reduced based on the controller.
2726 */
2727 if (!ufshcd_is_hba_active(hba)) {
2728
2729 /* change controller state to "reset state" */
2730 ufshcd_hba_stop(hba);
2731
2732 /*
2733 * This delay is based on the testing done with UFS host
2734 * controller FPGA. The delay can be changed based on the
2735 * host controller used.
2736 */
2737 msleep(5);
2738 }
2739
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002740 /* UniPro link is disabled at this point */
2741 ufshcd_set_link_off(hba);
2742
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002743 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002744
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302745 /* start controller initialization sequence */
2746 ufshcd_hba_start(hba);
2747
2748 /*
2749 * To initialize a UFS host controller HCE bit must be set to 1.
2750 * During initialization the HCE bit value changes from 1->0->1.
2751 * When the host controller completes initialization sequence
2752 * it sets the value of HCE bit to 1. The same HCE bit is read back
2753 * to check if the controller has completed initialization sequence.
2754 * So without this delay the value HCE = 1, set in the previous
2755 * instruction might be read back.
2756 * This delay can be changed based on the controller.
2757 */
2758 msleep(1);
2759
2760 /* wait for the host controller to complete initialization */
2761 retry = 10;
2762 while (ufshcd_is_hba_active(hba)) {
2763 if (retry) {
2764 retry--;
2765 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302766 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302767 "Controller enable failed\n");
2768 return -EIO;
2769 }
2770 msleep(5);
2771 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002772
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002773 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002774 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002775
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002776 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002777
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302778 return 0;
2779}
2780
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002781static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
2782{
2783 int tx_lanes, i, err = 0;
2784
2785 if (!peer)
2786 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2787 &tx_lanes);
2788 else
2789 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2790 &tx_lanes);
2791 for (i = 0; i < tx_lanes; i++) {
2792 if (!peer)
2793 err = ufshcd_dme_set(hba,
2794 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2795 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2796 0);
2797 else
2798 err = ufshcd_dme_peer_set(hba,
2799 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2800 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2801 0);
2802 if (err) {
2803 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
2804 __func__, peer, i, err);
2805 break;
2806 }
2807 }
2808
2809 return err;
2810}
2811
2812static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
2813{
2814 return ufshcd_disable_tx_lcc(hba, true);
2815}
2816
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302817/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302818 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302819 * @hba: per adapter instance
2820 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302821 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302822 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302823static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302824{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302825 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002826 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302827
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002828 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002829 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302830
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002831 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002832
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002833 /* check if device is detected by inter-connect layer */
2834 if (!ret && !ufshcd_is_device_present(hba)) {
2835 dev_err(hba->dev, "%s: Device not present\n", __func__);
2836 ret = -ENXIO;
2837 goto out;
2838 }
2839
2840 /*
2841 * DME link lost indication is only received when link is up,
2842 * but we can't be sure if the link is up until link startup
2843 * succeeds. So reset the local Uni-Pro and try again.
2844 */
2845 if (ret && ufshcd_hba_enable(hba))
2846 goto out;
2847 } while (ret && retries--);
2848
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302849 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002850 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302851 goto out;
2852
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002853 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
2854 ret = ufshcd_disable_device_tx_lcc(hba);
2855 if (ret)
2856 goto out;
2857 }
2858
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002859 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002860 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
2861 if (ret)
2862 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002863
2864 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302865out:
2866 if (ret)
2867 dev_err(hba->dev, "link startup failed %d\n", ret);
2868 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302869}
2870
2871/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302872 * ufshcd_verify_dev_init() - Verify device initialization
2873 * @hba: per-adapter instance
2874 *
2875 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
2876 * device Transport Protocol (UTP) layer is ready after a reset.
2877 * If the UTP layer at the device side is not initialized, it may
2878 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
2879 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
2880 */
2881static int ufshcd_verify_dev_init(struct ufs_hba *hba)
2882{
2883 int err = 0;
2884 int retries;
2885
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002886 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302887 mutex_lock(&hba->dev_cmd.lock);
2888 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
2889 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
2890 NOP_OUT_TIMEOUT);
2891
2892 if (!err || err == -ETIMEDOUT)
2893 break;
2894
2895 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2896 }
2897 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002898 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302899
2900 if (err)
2901 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2902 return err;
2903}
2904
2905/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002906 * ufshcd_set_queue_depth - set lun queue depth
2907 * @sdev: pointer to SCSI device
2908 *
2909 * Read bLUQueueDepth value and activate scsi tagged command
2910 * queueing. For WLUN, queue depth is set to 1. For best-effort
2911 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
2912 * value that host can queue.
2913 */
2914static void ufshcd_set_queue_depth(struct scsi_device *sdev)
2915{
2916 int ret = 0;
2917 u8 lun_qdepth;
2918 struct ufs_hba *hba;
2919
2920 hba = shost_priv(sdev->host);
2921
2922 lun_qdepth = hba->nutrs;
2923 ret = ufshcd_read_unit_desc_param(hba,
2924 ufshcd_scsi_to_upiu_lun(sdev->lun),
2925 UNIT_DESC_PARAM_LU_Q_DEPTH,
2926 &lun_qdepth,
2927 sizeof(lun_qdepth));
2928
2929 /* Some WLUN doesn't support unit descriptor */
2930 if (ret == -EOPNOTSUPP)
2931 lun_qdepth = 1;
2932 else if (!lun_qdepth)
2933 /* eventually, we can figure out the real queue depth */
2934 lun_qdepth = hba->nutrs;
2935 else
2936 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2937
2938 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2939 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002940 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002941}
2942
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002943/*
2944 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
2945 * @hba: per-adapter instance
2946 * @lun: UFS device lun id
2947 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
2948 *
2949 * Returns 0 in case of success and b_lu_write_protect status would be returned
2950 * @b_lu_write_protect parameter.
2951 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
2952 * Returns -EINVAL in case of invalid parameters passed to this function.
2953 */
2954static int ufshcd_get_lu_wp(struct ufs_hba *hba,
2955 u8 lun,
2956 u8 *b_lu_write_protect)
2957{
2958 int ret;
2959
2960 if (!b_lu_write_protect)
2961 ret = -EINVAL;
2962 /*
2963 * According to UFS device spec, RPMB LU can't be write
2964 * protected so skip reading bLUWriteProtect parameter for
2965 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
2966 */
2967 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
2968 ret = -ENOTSUPP;
2969 else
2970 ret = ufshcd_read_unit_desc_param(hba,
2971 lun,
2972 UNIT_DESC_PARAM_LU_WR_PROTECT,
2973 b_lu_write_protect,
2974 sizeof(*b_lu_write_protect));
2975 return ret;
2976}
2977
2978/**
2979 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
2980 * status
2981 * @hba: per-adapter instance
2982 * @sdev: pointer to SCSI device
2983 *
2984 */
2985static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
2986 struct scsi_device *sdev)
2987{
2988 if (hba->dev_info.f_power_on_wp_en &&
2989 !hba->dev_info.is_lu_power_on_wp) {
2990 u8 b_lu_write_protect;
2991
2992 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
2993 &b_lu_write_protect) &&
2994 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
2995 hba->dev_info.is_lu_power_on_wp = true;
2996 }
2997}
2998
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002999/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303000 * ufshcd_slave_alloc - handle initial SCSI device configurations
3001 * @sdev: pointer to SCSI device
3002 *
3003 * Returns success
3004 */
3005static int ufshcd_slave_alloc(struct scsi_device *sdev)
3006{
3007 struct ufs_hba *hba;
3008
3009 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303010
3011 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3012 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303013
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303014 /* allow SCSI layer to restart the device in case of errors */
3015 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003016
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003017 /* REPORT SUPPORTED OPERATION CODES is not supported */
3018 sdev->no_report_opcodes = 1;
3019
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003020
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003021 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003022
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003023 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3024
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003025 return 0;
3026}
3027
3028/**
3029 * ufshcd_change_queue_depth - change queue depth
3030 * @sdev: pointer to SCSI device
3031 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003032 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003033 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003034 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003035static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003036{
3037 struct ufs_hba *hba = shost_priv(sdev->host);
3038
3039 if (depth > hba->nutrs)
3040 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003041 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303042}
3043
3044/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003045 * ufshcd_slave_configure - adjust SCSI device configurations
3046 * @sdev: pointer to SCSI device
3047 */
3048static int ufshcd_slave_configure(struct scsi_device *sdev)
3049{
3050 struct request_queue *q = sdev->request_queue;
3051
3052 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3053 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3054
3055 return 0;
3056}
3057
3058/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303059 * ufshcd_slave_destroy - remove SCSI device configurations
3060 * @sdev: pointer to SCSI device
3061 */
3062static void ufshcd_slave_destroy(struct scsi_device *sdev)
3063{
3064 struct ufs_hba *hba;
3065
3066 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003067 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003068 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3069 unsigned long flags;
3070
3071 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003072 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003073 spin_unlock_irqrestore(hba->host->host_lock, flags);
3074 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303075}
3076
3077/**
3078 * ufshcd_task_req_compl - handle task management request completion
3079 * @hba: per adapter instance
3080 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303081 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303082 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303083 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303084 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303085static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303086{
3087 struct utp_task_req_desc *task_req_descp;
3088 struct utp_upiu_task_rsp *task_rsp_upiup;
3089 unsigned long flags;
3090 int ocs_value;
3091 int task_result;
3092
3093 spin_lock_irqsave(hba->host->host_lock, flags);
3094
3095 /* Clear completed tasks from outstanding_tasks */
3096 __clear_bit(index, &hba->outstanding_tasks);
3097
3098 task_req_descp = hba->utmrdl_base_addr;
3099 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3100
3101 if (ocs_value == OCS_SUCCESS) {
3102 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3103 task_req_descp[index].task_rsp_upiu;
3104 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
3105 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303106 if (resp)
3107 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303108 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303109 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3110 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303111 }
3112 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303113
3114 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303115}
3116
3117/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303118 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3119 * @lrb: pointer to local reference block of completed command
3120 * @scsi_status: SCSI command status
3121 *
3122 * Returns value base on SCSI command status
3123 */
3124static inline int
3125ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3126{
3127 int result = 0;
3128
3129 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303130 case SAM_STAT_CHECK_CONDITION:
3131 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303132 case SAM_STAT_GOOD:
3133 result |= DID_OK << 16 |
3134 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303135 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303136 break;
3137 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303138 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303139 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303140 ufshcd_copy_sense_data(lrbp);
3141 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303142 break;
3143 default:
3144 result |= DID_ERROR << 16;
3145 break;
3146 } /* end of switch */
3147
3148 return result;
3149}
3150
3151/**
3152 * ufshcd_transfer_rsp_status - Get overall status of the response
3153 * @hba: per adapter instance
3154 * @lrb: pointer to local reference block of completed command
3155 *
3156 * Returns result of the command to notify SCSI midlayer
3157 */
3158static inline int
3159ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3160{
3161 int result = 0;
3162 int scsi_status;
3163 int ocs;
3164
3165 /* overall command status of utrd */
3166 ocs = ufshcd_get_tr_ocs(lrbp);
3167
3168 switch (ocs) {
3169 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303170 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303171
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303172 switch (result) {
3173 case UPIU_TRANSACTION_RESPONSE:
3174 /*
3175 * get the response UPIU result to extract
3176 * the SCSI command status
3177 */
3178 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3179
3180 /*
3181 * get the result based on SCSI status response
3182 * to notify the SCSI midlayer of the command status
3183 */
3184 scsi_status = result & MASK_SCSI_STATUS;
3185 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303186
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003187 /*
3188 * Currently we are only supporting BKOPs exception
3189 * events hence we can ignore BKOPs exception event
3190 * during power management callbacks. BKOPs exception
3191 * event is not expected to be raised in runtime suspend
3192 * callback as it allows the urgent bkops.
3193 * During system suspend, we are anyway forcefully
3194 * disabling the bkops and if urgent bkops is needed
3195 * it will be enabled on system resume. Long term
3196 * solution could be to abort the system suspend if
3197 * UFS device needs urgent BKOPs.
3198 */
3199 if (!hba->pm_op_in_progress &&
3200 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303201 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303202 break;
3203 case UPIU_TRANSACTION_REJECT_UPIU:
3204 /* TODO: handle Reject UPIU Response */
3205 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303206 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303207 "Reject UPIU not fully implemented\n");
3208 break;
3209 default:
3210 result = DID_ERROR << 16;
3211 dev_err(hba->dev,
3212 "Unexpected request response code = %x\n",
3213 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303214 break;
3215 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303216 break;
3217 case OCS_ABORTED:
3218 result |= DID_ABORT << 16;
3219 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303220 case OCS_INVALID_COMMAND_STATUS:
3221 result |= DID_REQUEUE << 16;
3222 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303223 case OCS_INVALID_CMD_TABLE_ATTR:
3224 case OCS_INVALID_PRDT_ATTR:
3225 case OCS_MISMATCH_DATA_BUF_SIZE:
3226 case OCS_MISMATCH_RESP_UPIU_SIZE:
3227 case OCS_PEER_COMM_FAILURE:
3228 case OCS_FATAL_ERROR:
3229 default:
3230 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303231 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303232 "OCS error from controller = %x\n", ocs);
3233 break;
3234 } /* end of switch */
3235
3236 return result;
3237}
3238
3239/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303240 * ufshcd_uic_cmd_compl - handle completion of uic command
3241 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303242 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303243 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303244static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303245{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303246 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303247 hba->active_uic_cmd->argument2 |=
3248 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303249 hba->active_uic_cmd->argument3 =
3250 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303251 complete(&hba->active_uic_cmd->done);
3252 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303253
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003254 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3255 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303256}
3257
3258/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303259 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3260 * @hba: per adapter instance
3261 */
3262static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3263{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303264 struct ufshcd_lrb *lrbp;
3265 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303266 unsigned long completed_reqs;
3267 u32 tr_doorbell;
3268 int result;
3269 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003270
3271 /* Resetting interrupt aggregation counters first and reading the
3272 * DOOR_BELL afterward allows us to handle all the completed requests.
3273 * In order to prevent other interrupts starvation the DB is read once
3274 * after reset. The down side of this solution is the possibility of
3275 * false interrupt if device completes another request after resetting
3276 * aggregation and before reading the DB.
3277 */
Yaniv Gardib8521902015-05-17 18:54:57 +03003278 if (ufshcd_is_intr_aggr_allowed(hba))
3279 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303280
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303281 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303282 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3283
Dolev Ravive9d501b2014-07-01 12:22:37 +03003284 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3285 lrbp = &hba->lrb[index];
3286 cmd = lrbp->cmd;
3287 if (cmd) {
3288 result = ufshcd_transfer_rsp_status(hba, lrbp);
3289 scsi_dma_unmap(cmd);
3290 cmd->result = result;
3291 /* Mark completed command as NULL in LRB */
3292 lrbp->cmd = NULL;
3293 clear_bit_unlock(index, &hba->lrb_in_use);
3294 /* Do not touch lrbp after scsi done */
3295 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003296 __ufshcd_release(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003297 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
3298 if (hba->dev_cmd.complete)
3299 complete(hba->dev_cmd.complete);
3300 }
3301 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303302
3303 /* clear corresponding bits of completed commands */
3304 hba->outstanding_reqs ^= completed_reqs;
3305
Sahitya Tummala856b3482014-09-25 15:32:34 +03003306 ufshcd_clk_scaling_update_busy(hba);
3307
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303308 /* we might have free'd some tags above */
3309 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303310}
3311
3312/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303313 * ufshcd_disable_ee - disable exception event
3314 * @hba: per-adapter instance
3315 * @mask: exception event to disable
3316 *
3317 * Disables exception event in the device so that the EVENT_ALERT
3318 * bit is not set.
3319 *
3320 * Returns zero on success, non-zero error value on failure.
3321 */
3322static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3323{
3324 int err = 0;
3325 u32 val;
3326
3327 if (!(hba->ee_ctrl_mask & mask))
3328 goto out;
3329
3330 val = hba->ee_ctrl_mask & ~mask;
3331 val &= 0xFFFF; /* 2 bytes */
3332 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3333 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3334 if (!err)
3335 hba->ee_ctrl_mask &= ~mask;
3336out:
3337 return err;
3338}
3339
3340/**
3341 * ufshcd_enable_ee - enable exception event
3342 * @hba: per-adapter instance
3343 * @mask: exception event to enable
3344 *
3345 * Enable corresponding exception event in the device to allow
3346 * device to alert host in critical scenarios.
3347 *
3348 * Returns zero on success, non-zero error value on failure.
3349 */
3350static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3351{
3352 int err = 0;
3353 u32 val;
3354
3355 if (hba->ee_ctrl_mask & mask)
3356 goto out;
3357
3358 val = hba->ee_ctrl_mask | mask;
3359 val &= 0xFFFF; /* 2 bytes */
3360 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3361 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3362 if (!err)
3363 hba->ee_ctrl_mask |= mask;
3364out:
3365 return err;
3366}
3367
3368/**
3369 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3370 * @hba: per-adapter instance
3371 *
3372 * Allow device to manage background operations on its own. Enabling
3373 * this might lead to inconsistent latencies during normal data transfers
3374 * as the device is allowed to manage its own way of handling background
3375 * operations.
3376 *
3377 * Returns zero on success, non-zero on failure.
3378 */
3379static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3380{
3381 int err = 0;
3382
3383 if (hba->auto_bkops_enabled)
3384 goto out;
3385
3386 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3387 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3388 if (err) {
3389 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3390 __func__, err);
3391 goto out;
3392 }
3393
3394 hba->auto_bkops_enabled = true;
3395
3396 /* No need of URGENT_BKOPS exception from the device */
3397 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3398 if (err)
3399 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3400 __func__, err);
3401out:
3402 return err;
3403}
3404
3405/**
3406 * ufshcd_disable_auto_bkops - block device in doing background operations
3407 * @hba: per-adapter instance
3408 *
3409 * Disabling background operations improves command response latency but
3410 * has drawback of device moving into critical state where the device is
3411 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3412 * host is idle so that BKOPS are managed effectively without any negative
3413 * impacts.
3414 *
3415 * Returns zero on success, non-zero on failure.
3416 */
3417static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3418{
3419 int err = 0;
3420
3421 if (!hba->auto_bkops_enabled)
3422 goto out;
3423
3424 /*
3425 * If host assisted BKOPs is to be enabled, make sure
3426 * urgent bkops exception is allowed.
3427 */
3428 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3429 if (err) {
3430 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3431 __func__, err);
3432 goto out;
3433 }
3434
3435 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
3436 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3437 if (err) {
3438 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3439 __func__, err);
3440 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3441 goto out;
3442 }
3443
3444 hba->auto_bkops_enabled = false;
3445out:
3446 return err;
3447}
3448
3449/**
3450 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3451 * @hba: per adapter instance
3452 *
3453 * After a device reset the device may toggle the BKOPS_EN flag
3454 * to default value. The s/w tracking variables should be updated
3455 * as well. Do this by forcing enable of auto bkops.
3456 */
3457static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3458{
3459 hba->auto_bkops_enabled = false;
3460 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3461 ufshcd_enable_auto_bkops(hba);
3462}
3463
3464static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3465{
3466 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3467 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3468}
3469
3470/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003471 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3472 * @hba: per-adapter instance
3473 * @status: bkops_status value
3474 *
3475 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3476 * flag in the device to permit background operations if the device
3477 * bkops_status is greater than or equal to "status" argument passed to
3478 * this function, disable otherwise.
3479 *
3480 * Returns 0 for success, non-zero in case of failure.
3481 *
3482 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3483 * to know whether auto bkops is enabled or disabled after this function
3484 * returns control to it.
3485 */
3486static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3487 enum bkops_status status)
3488{
3489 int err;
3490 u32 curr_status = 0;
3491
3492 err = ufshcd_get_bkops_status(hba, &curr_status);
3493 if (err) {
3494 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3495 __func__, err);
3496 goto out;
3497 } else if (curr_status > BKOPS_STATUS_MAX) {
3498 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3499 __func__, curr_status);
3500 err = -EINVAL;
3501 goto out;
3502 }
3503
3504 if (curr_status >= status)
3505 err = ufshcd_enable_auto_bkops(hba);
3506 else
3507 err = ufshcd_disable_auto_bkops(hba);
3508out:
3509 return err;
3510}
3511
3512/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303513 * ufshcd_urgent_bkops - handle urgent bkops exception event
3514 * @hba: per-adapter instance
3515 *
3516 * Enable fBackgroundOpsEn flag in the device to permit background
3517 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003518 *
3519 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3520 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303521 */
3522static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3523{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003524 return ufshcd_bkops_ctrl(hba, BKOPS_STATUS_PERF_IMPACT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303525}
3526
3527static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3528{
3529 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3530 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3531}
3532
3533/**
3534 * ufshcd_exception_event_handler - handle exceptions raised by device
3535 * @work: pointer to work data
3536 *
3537 * Read bExceptionEventStatus attribute from the device and handle the
3538 * exception event accordingly.
3539 */
3540static void ufshcd_exception_event_handler(struct work_struct *work)
3541{
3542 struct ufs_hba *hba;
3543 int err;
3544 u32 status = 0;
3545 hba = container_of(work, struct ufs_hba, eeh_work);
3546
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303547 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303548 err = ufshcd_get_ee_status(hba, &status);
3549 if (err) {
3550 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3551 __func__, err);
3552 goto out;
3553 }
3554
3555 status &= hba->ee_ctrl_mask;
3556 if (status & MASK_EE_URGENT_BKOPS) {
3557 err = ufshcd_urgent_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003558 if (err < 0)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303559 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3560 __func__, err);
3561 }
3562out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303563 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303564 return;
3565}
3566
3567/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303568 * ufshcd_err_handler - handle UFS errors that require s/w attention
3569 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303570 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303571static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303572{
3573 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303574 unsigned long flags;
3575 u32 err_xfer = 0;
3576 u32 err_tm = 0;
3577 int err = 0;
3578 int tag;
3579
3580 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303581
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303582 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003583 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303584
3585 spin_lock_irqsave(hba->host->host_lock, flags);
3586 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
3587 spin_unlock_irqrestore(hba->host->host_lock, flags);
3588 goto out;
3589 }
3590
3591 hba->ufshcd_state = UFSHCD_STATE_RESET;
3592 ufshcd_set_eh_in_progress(hba);
3593
3594 /* Complete requests that have door-bell cleared by h/w */
3595 ufshcd_transfer_req_compl(hba);
3596 ufshcd_tmc_handler(hba);
3597 spin_unlock_irqrestore(hba->host->host_lock, flags);
3598
3599 /* Clear pending transfer requests */
3600 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
3601 if (ufshcd_clear_cmd(hba, tag))
3602 err_xfer |= 1 << tag;
3603
3604 /* Clear pending task management requests */
3605 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
3606 if (ufshcd_clear_tm_cmd(hba, tag))
3607 err_tm |= 1 << tag;
3608
3609 /* Complete the requests that are cleared by s/w */
3610 spin_lock_irqsave(hba->host->host_lock, flags);
3611 ufshcd_transfer_req_compl(hba);
3612 ufshcd_tmc_handler(hba);
3613 spin_unlock_irqrestore(hba->host->host_lock, flags);
3614
3615 /* Fatal errors need reset */
3616 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
3617 ((hba->saved_err & UIC_ERROR) &&
3618 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
3619 err = ufshcd_reset_and_restore(hba);
3620 if (err) {
3621 dev_err(hba->dev, "%s: reset and restore failed\n",
3622 __func__);
3623 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3624 }
3625 /*
3626 * Inform scsi mid-layer that we did reset and allow to handle
3627 * Unit Attention properly.
3628 */
3629 scsi_report_bus_reset(hba->host, 0);
3630 hba->saved_err = 0;
3631 hba->saved_uic_err = 0;
3632 }
3633 ufshcd_clear_eh_in_progress(hba);
3634
3635out:
3636 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003637 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303638 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303639}
3640
3641/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303642 * ufshcd_update_uic_error - check and set fatal UIC error flags.
3643 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303644 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303645static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303646{
3647 u32 reg;
3648
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303649 /* PA_INIT_ERROR is fatal and needs UIC reset */
3650 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
3651 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
3652 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
3653
3654 /* UIC NL/TL/DME errors needs software retry */
3655 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
3656 if (reg)
3657 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
3658
3659 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
3660 if (reg)
3661 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
3662
3663 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
3664 if (reg)
3665 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
3666
3667 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
3668 __func__, hba->uic_error);
3669}
3670
3671/**
3672 * ufshcd_check_errors - Check for errors that need s/w attention
3673 * @hba: per-adapter instance
3674 */
3675static void ufshcd_check_errors(struct ufs_hba *hba)
3676{
3677 bool queue_eh_work = false;
3678
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303679 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303680 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303681
3682 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303683 hba->uic_error = 0;
3684 ufshcd_update_uic_error(hba);
3685 if (hba->uic_error)
3686 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303687 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303688
3689 if (queue_eh_work) {
3690 /* handle fatal errors only when link is functional */
3691 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
3692 /* block commands from scsi mid-layer */
3693 scsi_block_requests(hba->host);
3694
3695 /* transfer error masks to sticky bits */
3696 hba->saved_err |= hba->errors;
3697 hba->saved_uic_err |= hba->uic_error;
3698
3699 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3700 schedule_work(&hba->eh_work);
3701 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303702 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303703 /*
3704 * if (!queue_eh_work) -
3705 * Other errors are either non-fatal where host recovers
3706 * itself without s/w intervention or errors that will be
3707 * handled by the SCSI core layer.
3708 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303709}
3710
3711/**
3712 * ufshcd_tmc_handler - handle task management function completion
3713 * @hba: per adapter instance
3714 */
3715static void ufshcd_tmc_handler(struct ufs_hba *hba)
3716{
3717 u32 tm_doorbell;
3718
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303719 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303720 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303721 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303722}
3723
3724/**
3725 * ufshcd_sl_intr - Interrupt service routine
3726 * @hba: per adapter instance
3727 * @intr_status: contains interrupts generated by the controller
3728 */
3729static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
3730{
3731 hba->errors = UFSHCD_ERROR_MASK & intr_status;
3732 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303733 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303734
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303735 if (intr_status & UFSHCD_UIC_MASK)
3736 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303737
3738 if (intr_status & UTP_TASK_REQ_COMPL)
3739 ufshcd_tmc_handler(hba);
3740
3741 if (intr_status & UTP_TRANSFER_REQ_COMPL)
3742 ufshcd_transfer_req_compl(hba);
3743}
3744
3745/**
3746 * ufshcd_intr - Main interrupt service routine
3747 * @irq: irq number
3748 * @__hba: pointer to adapter instance
3749 *
3750 * Returns IRQ_HANDLED - If interrupt is valid
3751 * IRQ_NONE - If invalid interrupt
3752 */
3753static irqreturn_t ufshcd_intr(int irq, void *__hba)
3754{
3755 u32 intr_status;
3756 irqreturn_t retval = IRQ_NONE;
3757 struct ufs_hba *hba = __hba;
3758
3759 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303760 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303761
3762 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05303763 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303764 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303765 retval = IRQ_HANDLED;
3766 }
3767 spin_unlock(hba->host->host_lock);
3768 return retval;
3769}
3770
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303771static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
3772{
3773 int err = 0;
3774 u32 mask = 1 << tag;
3775 unsigned long flags;
3776
3777 if (!test_bit(tag, &hba->outstanding_tasks))
3778 goto out;
3779
3780 spin_lock_irqsave(hba->host->host_lock, flags);
3781 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
3782 spin_unlock_irqrestore(hba->host->host_lock, flags);
3783
3784 /* poll for max. 1 sec to clear door bell register by h/w */
3785 err = ufshcd_wait_for_register(hba,
3786 REG_UTP_TASK_REQ_DOOR_BELL,
3787 mask, 0, 1000, 1000);
3788out:
3789 return err;
3790}
3791
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303792/**
3793 * ufshcd_issue_tm_cmd - issues task management commands to controller
3794 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303795 * @lun_id: LUN ID to which TM command is sent
3796 * @task_id: task ID to which the TM command is applicable
3797 * @tm_function: task management function opcode
3798 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303799 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303800 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303801 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303802static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
3803 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303804{
3805 struct utp_task_req_desc *task_req_descp;
3806 struct utp_upiu_task_req *task_req_upiup;
3807 struct Scsi_Host *host;
3808 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303809 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303810 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303811 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303812
3813 host = hba->host;
3814
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303815 /*
3816 * Get free slot, sleep if slots are unavailable.
3817 * Even though we use wait_event() which sleeps indefinitely,
3818 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
3819 */
3820 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003821 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303822
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303823 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303824 task_req_descp = hba->utmrdl_base_addr;
3825 task_req_descp += free_slot;
3826
3827 /* Configure task request descriptor */
3828 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
3829 task_req_descp->header.dword_2 =
3830 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
3831
3832 /* Configure task request UPIU */
3833 task_req_upiup =
3834 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303835 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303836 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303837 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303838 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303839 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303840 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003841 /*
3842 * The host shall provide the same value for LUN field in the basic
3843 * header and for Input Parameter.
3844 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303845 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
3846 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303847
3848 /* send command to the controller */
3849 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303850 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303851
3852 spin_unlock_irqrestore(host->host_lock, flags);
3853
3854 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303855 err = wait_event_timeout(hba->tm_wq,
3856 test_bit(free_slot, &hba->tm_condition),
3857 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303858 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303859 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
3860 __func__, tm_function);
3861 if (ufshcd_clear_tm_cmd(hba, free_slot))
3862 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
3863 __func__, free_slot);
3864 err = -ETIMEDOUT;
3865 } else {
3866 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303867 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303868
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303869 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303870 ufshcd_put_tm_slot(hba, free_slot);
3871 wake_up(&hba->tm_tag_wq);
3872
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003873 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303874 return err;
3875}
3876
3877/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303878 * ufshcd_eh_device_reset_handler - device reset handler registered to
3879 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303880 * @cmd: SCSI command pointer
3881 *
3882 * Returns SUCCESS/FAILED
3883 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303884static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303885{
3886 struct Scsi_Host *host;
3887 struct ufs_hba *hba;
3888 unsigned int tag;
3889 u32 pos;
3890 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303891 u8 resp = 0xF;
3892 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303893 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303894
3895 host = cmd->device->host;
3896 hba = shost_priv(host);
3897 tag = cmd->request->tag;
3898
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303899 lrbp = &hba->lrb[tag];
3900 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
3901 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303902 if (!err)
3903 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303904 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303905 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303906
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303907 /* clear the commands that were pending for corresponding LUN */
3908 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
3909 if (hba->lrb[pos].lun == lrbp->lun) {
3910 err = ufshcd_clear_cmd(hba, pos);
3911 if (err)
3912 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303913 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303914 }
3915 spin_lock_irqsave(host->host_lock, flags);
3916 ufshcd_transfer_req_compl(hba);
3917 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303918out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303919 if (!err) {
3920 err = SUCCESS;
3921 } else {
3922 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3923 err = FAILED;
3924 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303925 return err;
3926}
3927
3928/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303929 * ufshcd_abort - abort a specific command
3930 * @cmd: SCSI command pointer
3931 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303932 * Abort the pending command in device by sending UFS_ABORT_TASK task management
3933 * command, and in host controller by clearing the door-bell register. There can
3934 * be race between controller sending the command to the device while abort is
3935 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
3936 * really issued and then try to abort it.
3937 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303938 * Returns SUCCESS/FAILED
3939 */
3940static int ufshcd_abort(struct scsi_cmnd *cmd)
3941{
3942 struct Scsi_Host *host;
3943 struct ufs_hba *hba;
3944 unsigned long flags;
3945 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303946 int err = 0;
3947 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303948 u8 resp = 0xF;
3949 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003950 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303951
3952 host = cmd->device->host;
3953 hba = shost_priv(host);
3954 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02003955 if (!ufshcd_valid_tag(hba, tag)) {
3956 dev_err(hba->dev,
3957 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
3958 __func__, tag, cmd, cmd->request);
3959 BUG();
3960 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303961
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003962 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003963 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02003964 /* If command is already aborted/completed, return SUCCESS */
3965 if (!(test_bit(tag, &hba->outstanding_reqs))) {
3966 dev_err(hba->dev,
3967 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
3968 __func__, tag, hba->outstanding_reqs, reg);
3969 goto out;
3970 }
3971
Dolev Ravive9d501b2014-07-01 12:22:37 +03003972 if (!(reg & (1 << tag))) {
3973 dev_err(hba->dev,
3974 "%s: cmd was completed, but without a notifying intr, tag = %d",
3975 __func__, tag);
3976 }
3977
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303978 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303979 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
3980 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3981 UFS_QUERY_TASK, &resp);
3982 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
3983 /* cmd pending in the device */
3984 break;
3985 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303986 /*
3987 * cmd not pending in the device, check if it is
3988 * in transition.
3989 */
3990 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3991 if (reg & (1 << tag)) {
3992 /* sleep for max. 200us to stabilize */
3993 usleep_range(100, 200);
3994 continue;
3995 }
3996 /* command completed already */
3997 goto out;
3998 } else {
3999 if (!err)
4000 err = resp; /* service response error */
4001 goto out;
4002 }
4003 }
4004
4005 if (!poll_cnt) {
4006 err = -EBUSY;
4007 goto out;
4008 }
4009
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304010 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4011 UFS_ABORT_TASK, &resp);
4012 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304013 if (!err)
4014 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304015 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304016 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304017
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304018 err = ufshcd_clear_cmd(hba, tag);
4019 if (err)
4020 goto out;
4021
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304022 scsi_dma_unmap(cmd);
4023
4024 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004025 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304026 hba->lrb[tag].cmd = NULL;
4027 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304028
4029 clear_bit_unlock(tag, &hba->lrb_in_use);
4030 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004031
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304032out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304033 if (!err) {
4034 err = SUCCESS;
4035 } else {
4036 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4037 err = FAILED;
4038 }
4039
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004040 /*
4041 * This ufshcd_release() corresponds to the original scsi cmd that got
4042 * aborted here (as we won't get any IRQ for it).
4043 */
4044 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304045 return err;
4046}
4047
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304048/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304049 * ufshcd_host_reset_and_restore - reset and restore host controller
4050 * @hba: per-adapter instance
4051 *
4052 * Note that host controller reset may issue DME_RESET to
4053 * local and remote (device) Uni-Pro stack and the attributes
4054 * are reset to default state.
4055 *
4056 * Returns zero on success, non-zero on failure
4057 */
4058static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4059{
4060 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304061 unsigned long flags;
4062
4063 /* Reset the host controller */
4064 spin_lock_irqsave(hba->host->host_lock, flags);
4065 ufshcd_hba_stop(hba);
4066 spin_unlock_irqrestore(hba->host->host_lock, flags);
4067
4068 err = ufshcd_hba_enable(hba);
4069 if (err)
4070 goto out;
4071
4072 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004073 err = ufshcd_probe_hba(hba);
4074
4075 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304076 err = -EIO;
4077out:
4078 if (err)
4079 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4080
4081 return err;
4082}
4083
4084/**
4085 * ufshcd_reset_and_restore - reset and re-initialize host/device
4086 * @hba: per-adapter instance
4087 *
4088 * Reset and recover device, host and re-establish link. This
4089 * is helpful to recover the communication in fatal error conditions.
4090 *
4091 * Returns zero on success, non-zero on failure
4092 */
4093static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4094{
4095 int err = 0;
4096 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004097 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304098
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004099 do {
4100 err = ufshcd_host_reset_and_restore(hba);
4101 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304102
4103 /*
4104 * After reset the door-bell might be cleared, complete
4105 * outstanding requests in s/w here.
4106 */
4107 spin_lock_irqsave(hba->host->host_lock, flags);
4108 ufshcd_transfer_req_compl(hba);
4109 ufshcd_tmc_handler(hba);
4110 spin_unlock_irqrestore(hba->host->host_lock, flags);
4111
4112 return err;
4113}
4114
4115/**
4116 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4117 * @cmd - SCSI command pointer
4118 *
4119 * Returns SUCCESS/FAILED
4120 */
4121static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4122{
4123 int err;
4124 unsigned long flags;
4125 struct ufs_hba *hba;
4126
4127 hba = shost_priv(cmd->device->host);
4128
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004129 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304130 /*
4131 * Check if there is any race with fatal error handling.
4132 * If so, wait for it to complete. Even though fatal error
4133 * handling does reset and restore in some cases, don't assume
4134 * anything out of it. We are just avoiding race here.
4135 */
4136 do {
4137 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304138 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304139 hba->ufshcd_state == UFSHCD_STATE_RESET))
4140 break;
4141 spin_unlock_irqrestore(hba->host->host_lock, flags);
4142 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304143 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304144 } while (1);
4145
4146 hba->ufshcd_state = UFSHCD_STATE_RESET;
4147 ufshcd_set_eh_in_progress(hba);
4148 spin_unlock_irqrestore(hba->host->host_lock, flags);
4149
4150 err = ufshcd_reset_and_restore(hba);
4151
4152 spin_lock_irqsave(hba->host->host_lock, flags);
4153 if (!err) {
4154 err = SUCCESS;
4155 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4156 } else {
4157 err = FAILED;
4158 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4159 }
4160 ufshcd_clear_eh_in_progress(hba);
4161 spin_unlock_irqrestore(hba->host->host_lock, flags);
4162
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004163 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304164 return err;
4165}
4166
4167/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004168 * ufshcd_get_max_icc_level - calculate the ICC level
4169 * @sup_curr_uA: max. current supported by the regulator
4170 * @start_scan: row at the desc table to start scan from
4171 * @buff: power descriptor buffer
4172 *
4173 * Returns calculated max ICC level for specific regulator
4174 */
4175static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4176{
4177 int i;
4178 int curr_uA;
4179 u16 data;
4180 u16 unit;
4181
4182 for (i = start_scan; i >= 0; i--) {
4183 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4184 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4185 ATTR_ICC_LVL_UNIT_OFFSET;
4186 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4187 switch (unit) {
4188 case UFSHCD_NANO_AMP:
4189 curr_uA = curr_uA / 1000;
4190 break;
4191 case UFSHCD_MILI_AMP:
4192 curr_uA = curr_uA * 1000;
4193 break;
4194 case UFSHCD_AMP:
4195 curr_uA = curr_uA * 1000 * 1000;
4196 break;
4197 case UFSHCD_MICRO_AMP:
4198 default:
4199 break;
4200 }
4201 if (sup_curr_uA >= curr_uA)
4202 break;
4203 }
4204 if (i < 0) {
4205 i = 0;
4206 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4207 }
4208
4209 return (u32)i;
4210}
4211
4212/**
4213 * ufshcd_calc_icc_level - calculate the max ICC level
4214 * In case regulators are not initialized we'll return 0
4215 * @hba: per-adapter instance
4216 * @desc_buf: power descriptor buffer to extract ICC levels from.
4217 * @len: length of desc_buff
4218 *
4219 * Returns calculated ICC level
4220 */
4221static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4222 u8 *desc_buf, int len)
4223{
4224 u32 icc_level = 0;
4225
4226 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4227 !hba->vreg_info.vccq2) {
4228 dev_err(hba->dev,
4229 "%s: Regulator capability was not set, actvIccLevel=%d",
4230 __func__, icc_level);
4231 goto out;
4232 }
4233
4234 if (hba->vreg_info.vcc)
4235 icc_level = ufshcd_get_max_icc_level(
4236 hba->vreg_info.vcc->max_uA,
4237 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4238 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4239
4240 if (hba->vreg_info.vccq)
4241 icc_level = ufshcd_get_max_icc_level(
4242 hba->vreg_info.vccq->max_uA,
4243 icc_level,
4244 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4245
4246 if (hba->vreg_info.vccq2)
4247 icc_level = ufshcd_get_max_icc_level(
4248 hba->vreg_info.vccq2->max_uA,
4249 icc_level,
4250 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4251out:
4252 return icc_level;
4253}
4254
4255static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4256{
4257 int ret;
4258 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4259 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4260
4261 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4262 if (ret) {
4263 dev_err(hba->dev,
4264 "%s: Failed reading power descriptor.len = %d ret = %d",
4265 __func__, buff_len, ret);
4266 return;
4267 }
4268
4269 hba->init_prefetch_data.icc_level =
4270 ufshcd_find_max_sup_active_icc_level(hba,
4271 desc_buf, buff_len);
4272 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4273 __func__, hba->init_prefetch_data.icc_level);
4274
4275 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4276 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4277 &hba->init_prefetch_data.icc_level);
4278
4279 if (ret)
4280 dev_err(hba->dev,
4281 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4282 __func__, hba->init_prefetch_data.icc_level , ret);
4283
4284}
4285
4286/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004287 * ufshcd_scsi_add_wlus - Adds required W-LUs
4288 * @hba: per-adapter instance
4289 *
4290 * UFS device specification requires the UFS devices to support 4 well known
4291 * logical units:
4292 * "REPORT_LUNS" (address: 01h)
4293 * "UFS Device" (address: 50h)
4294 * "RPMB" (address: 44h)
4295 * "BOOT" (address: 30h)
4296 * UFS device's power management needs to be controlled by "POWER CONDITION"
4297 * field of SSU (START STOP UNIT) command. But this "power condition" field
4298 * will take effect only when its sent to "UFS device" well known logical unit
4299 * hence we require the scsi_device instance to represent this logical unit in
4300 * order for the UFS host driver to send the SSU command for power management.
4301
4302 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4303 * Block) LU so user space process can control this LU. User space may also
4304 * want to have access to BOOT LU.
4305
4306 * This function adds scsi device instances for each of all well known LUs
4307 * (except "REPORT LUNS" LU).
4308 *
4309 * Returns zero on success (all required W-LUs are added successfully),
4310 * non-zero error value on failure (if failed to add any of the required W-LU).
4311 */
4312static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4313{
4314 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004315 struct scsi_device *sdev_rpmb;
4316 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004317
4318 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4319 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4320 if (IS_ERR(hba->sdev_ufs_device)) {
4321 ret = PTR_ERR(hba->sdev_ufs_device);
4322 hba->sdev_ufs_device = NULL;
4323 goto out;
4324 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004325 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004326
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004327 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004328 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004329 if (IS_ERR(sdev_boot)) {
4330 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004331 goto remove_sdev_ufs_device;
4332 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004333 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004334
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004335 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004336 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004337 if (IS_ERR(sdev_rpmb)) {
4338 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004339 goto remove_sdev_boot;
4340 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004341 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004342 goto out;
4343
4344remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004345 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004346remove_sdev_ufs_device:
4347 scsi_remove_device(hba->sdev_ufs_device);
4348out:
4349 return ret;
4350}
4351
4352/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004353 * ufshcd_probe_hba - probe hba to detect device and initialize
4354 * @hba: per-adapter instance
4355 *
4356 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304357 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004358static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304359{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304360 int ret;
4361
4362 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304363 if (ret)
4364 goto out;
4365
Yaniv Gardi50646362014-10-23 13:25:13 +03004366 ufshcd_init_pwr_info(hba);
4367
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004368 /* UniPro link is active now */
4369 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304370
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304371 ret = ufshcd_verify_dev_init(hba);
4372 if (ret)
4373 goto out;
4374
Dolev Raviv68078d52013-07-30 00:35:58 +05304375 ret = ufshcd_complete_dev_init(hba);
4376 if (ret)
4377 goto out;
4378
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004379 /* UFS device is also active now */
4380 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304381 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304382 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004383 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304384
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004385 if (ufshcd_get_max_pwr_mode(hba)) {
4386 dev_err(hba->dev,
4387 "%s: Failed getting max supported power mode\n",
4388 __func__);
4389 } else {
4390 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
4391 if (ret)
4392 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
4393 __func__, ret);
4394 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004395
4396 /*
4397 * If we are in error handling context or in power management callbacks
4398 * context, no need to scan the host
4399 */
4400 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4401 bool flag;
4402
4403 /* clear any previous UFS device information */
4404 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
4405 if (!ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4406 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
4407 hba->dev_info.f_power_on_wp_en = flag;
4408
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004409 if (!hba->is_init_prefetch)
4410 ufshcd_init_icc_levels(hba);
4411
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004412 /* Add required well known logical units to scsi mid layer */
4413 if (ufshcd_scsi_add_wlus(hba))
4414 goto out;
4415
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304416 scsi_scan_host(hba->host);
4417 pm_runtime_put_sync(hba->dev);
4418 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004419
4420 if (!hba->is_init_prefetch)
4421 hba->is_init_prefetch = true;
4422
Sahitya Tummala856b3482014-09-25 15:32:34 +03004423 /* Resume devfreq after UFS device is detected */
4424 if (ufshcd_is_clkscaling_enabled(hba))
4425 devfreq_resume_device(hba->devfreq);
4426
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304427out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004428 /*
4429 * If we failed to initialize the device or the device is not
4430 * present, turn off the power/clocks etc.
4431 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004432 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4433 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004434 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004435 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004436
4437 return ret;
4438}
4439
4440/**
4441 * ufshcd_async_scan - asynchronous execution for probing hba
4442 * @data: data pointer to pass to this function
4443 * @cookie: cookie data
4444 */
4445static void ufshcd_async_scan(void *data, async_cookie_t cookie)
4446{
4447 struct ufs_hba *hba = (struct ufs_hba *)data;
4448
4449 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304450}
4451
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304452static struct scsi_host_template ufshcd_driver_template = {
4453 .module = THIS_MODULE,
4454 .name = UFSHCD,
4455 .proc_name = UFSHCD,
4456 .queuecommand = ufshcd_queuecommand,
4457 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004458 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304459 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004460 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304461 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304462 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
4463 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304464 .this_id = -1,
4465 .sg_tablesize = SG_ALL,
4466 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
4467 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004468 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01004469 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304470};
4471
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004472static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
4473 int ua)
4474{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004475 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004476
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004477 if (!vreg)
4478 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004479
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004480 ret = regulator_set_load(vreg->reg, ua);
4481 if (ret < 0) {
4482 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
4483 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004484 }
4485
4486 return ret;
4487}
4488
4489static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
4490 struct ufs_vreg *vreg)
4491{
4492 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
4493}
4494
4495static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
4496 struct ufs_vreg *vreg)
4497{
4498 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
4499}
4500
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004501static int ufshcd_config_vreg(struct device *dev,
4502 struct ufs_vreg *vreg, bool on)
4503{
4504 int ret = 0;
4505 struct regulator *reg = vreg->reg;
4506 const char *name = vreg->name;
4507 int min_uV, uA_load;
4508
4509 BUG_ON(!vreg);
4510
4511 if (regulator_count_voltages(reg) > 0) {
4512 min_uV = on ? vreg->min_uV : 0;
4513 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
4514 if (ret) {
4515 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
4516 __func__, name, ret);
4517 goto out;
4518 }
4519
4520 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004521 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
4522 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004523 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004524 }
4525out:
4526 return ret;
4527}
4528
4529static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
4530{
4531 int ret = 0;
4532
4533 if (!vreg || vreg->enabled)
4534 goto out;
4535
4536 ret = ufshcd_config_vreg(dev, vreg, true);
4537 if (!ret)
4538 ret = regulator_enable(vreg->reg);
4539
4540 if (!ret)
4541 vreg->enabled = true;
4542 else
4543 dev_err(dev, "%s: %s enable failed, err=%d\n",
4544 __func__, vreg->name, ret);
4545out:
4546 return ret;
4547}
4548
4549static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
4550{
4551 int ret = 0;
4552
4553 if (!vreg || !vreg->enabled)
4554 goto out;
4555
4556 ret = regulator_disable(vreg->reg);
4557
4558 if (!ret) {
4559 /* ignore errors on applying disable config */
4560 ufshcd_config_vreg(dev, vreg, false);
4561 vreg->enabled = false;
4562 } else {
4563 dev_err(dev, "%s: %s disable failed, err=%d\n",
4564 __func__, vreg->name, ret);
4565 }
4566out:
4567 return ret;
4568}
4569
4570static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
4571{
4572 int ret = 0;
4573 struct device *dev = hba->dev;
4574 struct ufs_vreg_info *info = &hba->vreg_info;
4575
4576 if (!info)
4577 goto out;
4578
4579 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
4580 if (ret)
4581 goto out;
4582
4583 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
4584 if (ret)
4585 goto out;
4586
4587 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
4588 if (ret)
4589 goto out;
4590
4591out:
4592 if (ret) {
4593 ufshcd_toggle_vreg(dev, info->vccq2, false);
4594 ufshcd_toggle_vreg(dev, info->vccq, false);
4595 ufshcd_toggle_vreg(dev, info->vcc, false);
4596 }
4597 return ret;
4598}
4599
Raviv Shvili6a771a62014-09-25 15:32:24 +03004600static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
4601{
4602 struct ufs_vreg_info *info = &hba->vreg_info;
4603
4604 if (info)
4605 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
4606
4607 return 0;
4608}
4609
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004610static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
4611{
4612 int ret = 0;
4613
4614 if (!vreg)
4615 goto out;
4616
4617 vreg->reg = devm_regulator_get(dev, vreg->name);
4618 if (IS_ERR(vreg->reg)) {
4619 ret = PTR_ERR(vreg->reg);
4620 dev_err(dev, "%s: %s get failed, err=%d\n",
4621 __func__, vreg->name, ret);
4622 }
4623out:
4624 return ret;
4625}
4626
4627static int ufshcd_init_vreg(struct ufs_hba *hba)
4628{
4629 int ret = 0;
4630 struct device *dev = hba->dev;
4631 struct ufs_vreg_info *info = &hba->vreg_info;
4632
4633 if (!info)
4634 goto out;
4635
4636 ret = ufshcd_get_vreg(dev, info->vcc);
4637 if (ret)
4638 goto out;
4639
4640 ret = ufshcd_get_vreg(dev, info->vccq);
4641 if (ret)
4642 goto out;
4643
4644 ret = ufshcd_get_vreg(dev, info->vccq2);
4645out:
4646 return ret;
4647}
4648
Raviv Shvili6a771a62014-09-25 15:32:24 +03004649static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
4650{
4651 struct ufs_vreg_info *info = &hba->vreg_info;
4652
4653 if (info)
4654 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
4655
4656 return 0;
4657}
4658
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004659static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
4660 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004661{
4662 int ret = 0;
4663 struct ufs_clk_info *clki;
4664 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004665 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004666
4667 if (!head || list_empty(head))
4668 goto out;
4669
4670 list_for_each_entry(clki, head, list) {
4671 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004672 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
4673 continue;
4674
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004675 if (on && !clki->enabled) {
4676 ret = clk_prepare_enable(clki->clk);
4677 if (ret) {
4678 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
4679 __func__, clki->name, ret);
4680 goto out;
4681 }
4682 } else if (!on && clki->enabled) {
4683 clk_disable_unprepare(clki->clk);
4684 }
4685 clki->enabled = on;
4686 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
4687 clki->name, on ? "en" : "dis");
4688 }
4689 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004690
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004691 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004692out:
4693 if (ret) {
4694 list_for_each_entry(clki, head, list) {
4695 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
4696 clk_disable_unprepare(clki->clk);
4697 }
Dolev Raviveda910e2014-10-23 13:25:16 +03004698 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004699 spin_lock_irqsave(hba->host->host_lock, flags);
4700 hba->clk_gating.state = CLKS_ON;
4701 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004702 }
4703 return ret;
4704}
4705
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004706static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
4707{
4708 return __ufshcd_setup_clocks(hba, on, false);
4709}
4710
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004711static int ufshcd_init_clocks(struct ufs_hba *hba)
4712{
4713 int ret = 0;
4714 struct ufs_clk_info *clki;
4715 struct device *dev = hba->dev;
4716 struct list_head *head = &hba->clk_list_head;
4717
4718 if (!head || list_empty(head))
4719 goto out;
4720
4721 list_for_each_entry(clki, head, list) {
4722 if (!clki->name)
4723 continue;
4724
4725 clki->clk = devm_clk_get(dev, clki->name);
4726 if (IS_ERR(clki->clk)) {
4727 ret = PTR_ERR(clki->clk);
4728 dev_err(dev, "%s: %s clk get failed, %d\n",
4729 __func__, clki->name, ret);
4730 goto out;
4731 }
4732
4733 if (clki->max_freq) {
4734 ret = clk_set_rate(clki->clk, clki->max_freq);
4735 if (ret) {
4736 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
4737 __func__, clki->name,
4738 clki->max_freq, ret);
4739 goto out;
4740 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03004741 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004742 }
4743 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
4744 clki->name, clk_get_rate(clki->clk));
4745 }
4746out:
4747 return ret;
4748}
4749
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004750static int ufshcd_variant_hba_init(struct ufs_hba *hba)
4751{
4752 int err = 0;
4753
4754 if (!hba->vops)
4755 goto out;
4756
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004757 err = ufshcd_vops_init(hba);
4758 if (err)
4759 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004760
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004761 err = ufshcd_vops_setup_regulators(hba, true);
4762 if (err)
4763 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004764
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004765 goto out;
4766
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004767out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004768 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004769out:
4770 if (err)
4771 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004772 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004773 return err;
4774}
4775
4776static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
4777{
4778 if (!hba->vops)
4779 return;
4780
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004781 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004782
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004783 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004784
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004785 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004786}
4787
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004788static int ufshcd_hba_init(struct ufs_hba *hba)
4789{
4790 int err;
4791
Raviv Shvili6a771a62014-09-25 15:32:24 +03004792 /*
4793 * Handle host controller power separately from the UFS device power
4794 * rails as it will help controlling the UFS host controller power
4795 * collapse easily which is different than UFS device power collapse.
4796 * Also, enable the host controller power before we go ahead with rest
4797 * of the initialization here.
4798 */
4799 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004800 if (err)
4801 goto out;
4802
Raviv Shvili6a771a62014-09-25 15:32:24 +03004803 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004804 if (err)
4805 goto out;
4806
Raviv Shvili6a771a62014-09-25 15:32:24 +03004807 err = ufshcd_init_clocks(hba);
4808 if (err)
4809 goto out_disable_hba_vreg;
4810
4811 err = ufshcd_setup_clocks(hba, true);
4812 if (err)
4813 goto out_disable_hba_vreg;
4814
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004815 err = ufshcd_init_vreg(hba);
4816 if (err)
4817 goto out_disable_clks;
4818
4819 err = ufshcd_setup_vreg(hba, true);
4820 if (err)
4821 goto out_disable_clks;
4822
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004823 err = ufshcd_variant_hba_init(hba);
4824 if (err)
4825 goto out_disable_vreg;
4826
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004827 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004828 goto out;
4829
4830out_disable_vreg:
4831 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004832out_disable_clks:
4833 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03004834out_disable_hba_vreg:
4835 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004836out:
4837 return err;
4838}
4839
4840static void ufshcd_hba_exit(struct ufs_hba *hba)
4841{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004842 if (hba->is_powered) {
4843 ufshcd_variant_hba_exit(hba);
4844 ufshcd_setup_vreg(hba, false);
4845 ufshcd_setup_clocks(hba, false);
4846 ufshcd_setup_hba_vreg(hba, false);
4847 hba->is_powered = false;
4848 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004849}
4850
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004851static int
4852ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304853{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004854 unsigned char cmd[6] = {REQUEST_SENSE,
4855 0,
4856 0,
4857 0,
4858 SCSI_SENSE_BUFFERSIZE,
4859 0};
4860 char *buffer;
4861 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304862
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004863 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4864 if (!buffer) {
4865 ret = -ENOMEM;
4866 goto out;
4867 }
4868
4869 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
4870 SCSI_SENSE_BUFFERSIZE, NULL,
4871 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
4872 if (ret)
4873 pr_err("%s: failed with err %d\n", __func__, ret);
4874
4875 kfree(buffer);
4876out:
4877 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304878}
4879
4880/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004881 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
4882 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304883 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004884 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304885 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004886 * Returns 0 if requested power mode is set successfully
4887 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304888 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004889static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
4890 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304891{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004892 unsigned char cmd[6] = { START_STOP };
4893 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004894 struct scsi_device *sdp;
4895 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004896 int ret;
4897
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004898 spin_lock_irqsave(hba->host->host_lock, flags);
4899 sdp = hba->sdev_ufs_device;
4900 if (sdp) {
4901 ret = scsi_device_get(sdp);
4902 if (!ret && !scsi_device_online(sdp)) {
4903 ret = -ENODEV;
4904 scsi_device_put(sdp);
4905 }
4906 } else {
4907 ret = -ENODEV;
4908 }
4909 spin_unlock_irqrestore(hba->host->host_lock, flags);
4910
4911 if (ret)
4912 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004913
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304914 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004915 * If scsi commands fail, the scsi mid-layer schedules scsi error-
4916 * handling, which would wait for host to be resumed. Since we know
4917 * we are functional while we are here, skip host resume in error
4918 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304919 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004920 hba->host->eh_noresume = 1;
4921 if (hba->wlun_dev_clr_ua) {
4922 ret = ufshcd_send_request_sense(hba, sdp);
4923 if (ret)
4924 goto out;
4925 /* Unit attention condition is cleared now */
4926 hba->wlun_dev_clr_ua = false;
4927 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304928
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004929 cmd[4] = pwr_mode << 4;
4930
4931 /*
4932 * Current function would be generally called from the power management
4933 * callbacks hence set the REQ_PM flag so that it doesn't resume the
4934 * already suspended childs.
4935 */
4936 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
4937 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
4938 if (ret) {
4939 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02004940 "START_STOP failed for power mode: %d, result %x\n",
4941 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01004942 if (driver_byte(ret) & DRIVER_SENSE)
4943 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004944 }
4945
4946 if (!ret)
4947 hba->curr_dev_pwr_mode = pwr_mode;
4948out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004949 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004950 hba->host->eh_noresume = 0;
4951 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304952}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304953
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004954static int ufshcd_link_state_transition(struct ufs_hba *hba,
4955 enum uic_link_state req_link_state,
4956 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304957{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004958 int ret = 0;
4959
4960 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304961 return 0;
4962
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004963 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
4964 ret = ufshcd_uic_hibern8_enter(hba);
4965 if (!ret)
4966 ufshcd_set_link_hibern8(hba);
4967 else
4968 goto out;
4969 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304970 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004971 * If autobkops is enabled, link can't be turned off because
4972 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304973 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004974 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
4975 (!check_for_bkops || (check_for_bkops &&
4976 !hba->auto_bkops_enabled))) {
4977 /*
4978 * Change controller state to "reset state" which
4979 * should also put the link in off/reset state
4980 */
4981 ufshcd_hba_stop(hba);
4982 /*
4983 * TODO: Check if we need any delay to make sure that
4984 * controller is reset
4985 */
4986 ufshcd_set_link_off(hba);
4987 }
4988
4989out:
4990 return ret;
4991}
4992
4993static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
4994{
4995 /*
4996 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
4997 * power.
4998 *
4999 * If UFS device and link is in OFF state, all power supplies (VCC,
5000 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5001 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5002 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5003 *
5004 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5005 * in low power state which would save some power.
5006 */
5007 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5008 !hba->dev_info.is_lu_power_on_wp) {
5009 ufshcd_setup_vreg(hba, false);
5010 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5011 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5012 if (!ufshcd_is_link_active(hba)) {
5013 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5014 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5015 }
5016 }
5017}
5018
5019static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5020{
5021 int ret = 0;
5022
5023 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5024 !hba->dev_info.is_lu_power_on_wp) {
5025 ret = ufshcd_setup_vreg(hba, true);
5026 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5027 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
5028 if (!ret && !ufshcd_is_link_active(hba)) {
5029 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5030 if (ret)
5031 goto vcc_disable;
5032 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5033 if (ret)
5034 goto vccq_lpm;
5035 }
5036 }
5037 goto out;
5038
5039vccq_lpm:
5040 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5041vcc_disable:
5042 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5043out:
5044 return ret;
5045}
5046
5047static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5048{
5049 if (ufshcd_is_link_off(hba))
5050 ufshcd_setup_hba_vreg(hba, false);
5051}
5052
5053static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5054{
5055 if (ufshcd_is_link_off(hba))
5056 ufshcd_setup_hba_vreg(hba, true);
5057}
5058
5059/**
5060 * ufshcd_suspend - helper function for suspend operations
5061 * @hba: per adapter instance
5062 * @pm_op: desired low power operation type
5063 *
5064 * This function will try to put the UFS device and link into low power
5065 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5066 * (System PM level).
5067 *
5068 * If this function is called during shutdown, it will make sure that
5069 * both UFS device and UFS link is powered off.
5070 *
5071 * NOTE: UFS device & link must be active before we enter in this function.
5072 *
5073 * Returns 0 for success and non-zero for failure
5074 */
5075static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5076{
5077 int ret = 0;
5078 enum ufs_pm_level pm_lvl;
5079 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5080 enum uic_link_state req_link_state;
5081
5082 hba->pm_op_in_progress = 1;
5083 if (!ufshcd_is_shutdown_pm(pm_op)) {
5084 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5085 hba->rpm_lvl : hba->spm_lvl;
5086 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5087 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5088 } else {
5089 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5090 req_link_state = UIC_LINK_OFF_STATE;
5091 }
5092
5093 /*
5094 * If we can't transition into any of the low power modes
5095 * just gate the clocks.
5096 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005097 ufshcd_hold(hba, false);
5098 hba->clk_gating.is_suspended = true;
5099
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005100 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5101 req_link_state == UIC_LINK_ACTIVE_STATE) {
5102 goto disable_clks;
5103 }
5104
5105 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5106 (req_link_state == hba->uic_link_state))
5107 goto out;
5108
5109 /* UFS device & link must be active before we enter in this function */
5110 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5111 ret = -EINVAL;
5112 goto out;
5113 }
5114
5115 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005116 if (ufshcd_can_autobkops_during_suspend(hba)) {
5117 /*
5118 * The device is idle with no requests in the queue,
5119 * allow background operations if bkops status shows
5120 * that performance might be impacted.
5121 */
5122 ret = ufshcd_urgent_bkops(hba);
5123 if (ret)
5124 goto enable_gating;
5125 } else {
5126 /* make sure that auto bkops is disabled */
5127 ufshcd_disable_auto_bkops(hba);
5128 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005129 }
5130
5131 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5132 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5133 !ufshcd_is_runtime_pm(pm_op))) {
5134 /* ensure that bkops is disabled */
5135 ufshcd_disable_auto_bkops(hba);
5136 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5137 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005138 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005139 }
5140
5141 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5142 if (ret)
5143 goto set_dev_active;
5144
5145 ufshcd_vreg_set_lpm(hba);
5146
5147disable_clks:
5148 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03005149 * The clock scaling needs access to controller registers. Hence, Wait
5150 * for pending clock scaling work to be done before clocks are
5151 * turned off.
5152 */
5153 if (ufshcd_is_clkscaling_enabled(hba)) {
5154 devfreq_suspend_device(hba->devfreq);
5155 hba->clk_scaling.window_start_t = 0;
5156 }
5157 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005158 * Call vendor specific suspend callback. As these callbacks may access
5159 * vendor specific host controller register space call them before the
5160 * host clocks are ON.
5161 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005162 ret = ufshcd_vops_suspend(hba, pm_op);
5163 if (ret)
5164 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005165
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005166 ret = ufshcd_vops_setup_clocks(hba, false);
5167 if (ret)
5168 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005169
5170 if (!ufshcd_is_link_active(hba))
5171 ufshcd_setup_clocks(hba, false);
5172 else
5173 /* If link is active, device ref_clk can't be switched off */
5174 __ufshcd_setup_clocks(hba, false, true);
5175
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005176 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005177 /*
5178 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005179 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005180 */
5181 ufshcd_disable_irq(hba);
5182 /* Put the host controller in low power mode if possible */
5183 ufshcd_hba_vreg_set_lpm(hba);
5184 goto out;
5185
5186vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005187 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005188set_link_active:
5189 ufshcd_vreg_set_hpm(hba);
5190 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5191 ufshcd_set_link_active(hba);
5192 else if (ufshcd_is_link_off(hba))
5193 ufshcd_host_reset_and_restore(hba);
5194set_dev_active:
5195 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5196 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005197enable_gating:
5198 hba->clk_gating.is_suspended = false;
5199 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005200out:
5201 hba->pm_op_in_progress = 0;
5202 return ret;
5203}
5204
5205/**
5206 * ufshcd_resume - helper function for resume operations
5207 * @hba: per adapter instance
5208 * @pm_op: runtime PM or system PM
5209 *
5210 * This function basically brings the UFS device, UniPro link and controller
5211 * to active state.
5212 *
5213 * Returns 0 for success and non-zero for failure
5214 */
5215static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5216{
5217 int ret;
5218 enum uic_link_state old_link_state;
5219
5220 hba->pm_op_in_progress = 1;
5221 old_link_state = hba->uic_link_state;
5222
5223 ufshcd_hba_vreg_set_hpm(hba);
5224 /* Make sure clocks are enabled before accessing controller */
5225 ret = ufshcd_setup_clocks(hba, true);
5226 if (ret)
5227 goto out;
5228
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005229 /* enable the host irq as host controller would be active soon */
5230 ret = ufshcd_enable_irq(hba);
5231 if (ret)
5232 goto disable_irq_and_vops_clks;
5233
5234 ret = ufshcd_vreg_set_hpm(hba);
5235 if (ret)
5236 goto disable_irq_and_vops_clks;
5237
5238 /*
5239 * Call vendor specific resume callback. As these callbacks may access
5240 * vendor specific host controller register space call them when the
5241 * host clocks are ON.
5242 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005243 ret = ufshcd_vops_resume(hba, pm_op);
5244 if (ret)
5245 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005246
5247 if (ufshcd_is_link_hibern8(hba)) {
5248 ret = ufshcd_uic_hibern8_exit(hba);
5249 if (!ret)
5250 ufshcd_set_link_active(hba);
5251 else
5252 goto vendor_suspend;
5253 } else if (ufshcd_is_link_off(hba)) {
5254 ret = ufshcd_host_reset_and_restore(hba);
5255 /*
5256 * ufshcd_host_reset_and_restore() should have already
5257 * set the link state as active
5258 */
5259 if (ret || !ufshcd_is_link_active(hba))
5260 goto vendor_suspend;
5261 }
5262
5263 if (!ufshcd_is_ufs_dev_active(hba)) {
5264 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
5265 if (ret)
5266 goto set_old_link_state;
5267 }
5268
Subhash Jadavani374a2462014-09-25 15:32:35 +03005269 /*
5270 * If BKOPs operations are urgently needed at this moment then
5271 * keep auto-bkops enabled or else disable it.
5272 */
5273 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005274 hba->clk_gating.is_suspended = false;
5275
Sahitya Tummala856b3482014-09-25 15:32:34 +03005276 if (ufshcd_is_clkscaling_enabled(hba))
5277 devfreq_resume_device(hba->devfreq);
5278
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005279 /* Schedule clock gating in case of no access to UFS device yet */
5280 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005281 goto out;
5282
5283set_old_link_state:
5284 ufshcd_link_state_transition(hba, old_link_state, 0);
5285vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005286 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005287disable_vreg:
5288 ufshcd_vreg_set_lpm(hba);
5289disable_irq_and_vops_clks:
5290 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005291 ufshcd_setup_clocks(hba, false);
5292out:
5293 hba->pm_op_in_progress = 0;
5294 return ret;
5295}
5296
5297/**
5298 * ufshcd_system_suspend - system suspend routine
5299 * @hba: per adapter instance
5300 * @pm_op: runtime PM or system PM
5301 *
5302 * Check the description of ufshcd_suspend() function for more details.
5303 *
5304 * Returns 0 for success and non-zero for failure
5305 */
5306int ufshcd_system_suspend(struct ufs_hba *hba)
5307{
5308 int ret = 0;
5309
5310 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03005311 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005312
5313 if (pm_runtime_suspended(hba->dev)) {
5314 if (hba->rpm_lvl == hba->spm_lvl)
5315 /*
5316 * There is possibility that device may still be in
5317 * active state during the runtime suspend.
5318 */
5319 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
5320 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
5321 goto out;
5322
5323 /*
5324 * UFS device and/or UFS link low power states during runtime
5325 * suspend seems to be different than what is expected during
5326 * system suspend. Hence runtime resume the devic & link and
5327 * let the system suspend low power states to take effect.
5328 * TODO: If resume takes longer time, we might have optimize
5329 * it in future by not resuming everything if possible.
5330 */
5331 ret = ufshcd_runtime_resume(hba);
5332 if (ret)
5333 goto out;
5334 }
5335
5336 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
5337out:
Dolev Ravive7850602014-09-25 15:32:36 +03005338 if (!ret)
5339 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005340 return ret;
5341}
5342EXPORT_SYMBOL(ufshcd_system_suspend);
5343
5344/**
5345 * ufshcd_system_resume - system resume routine
5346 * @hba: per adapter instance
5347 *
5348 * Returns 0 for success and non-zero for failure
5349 */
5350
5351int ufshcd_system_resume(struct ufs_hba *hba)
5352{
5353 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
5354 /*
5355 * Let the runtime resume take care of resuming
5356 * if runtime suspended.
5357 */
5358 return 0;
5359
5360 return ufshcd_resume(hba, UFS_SYSTEM_PM);
5361}
5362EXPORT_SYMBOL(ufshcd_system_resume);
5363
5364/**
5365 * ufshcd_runtime_suspend - runtime suspend routine
5366 * @hba: per adapter instance
5367 *
5368 * Check the description of ufshcd_suspend() function for more details.
5369 *
5370 * Returns 0 for success and non-zero for failure
5371 */
5372int ufshcd_runtime_suspend(struct ufs_hba *hba)
5373{
5374 if (!hba || !hba->is_powered)
5375 return 0;
5376
5377 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305378}
5379EXPORT_SYMBOL(ufshcd_runtime_suspend);
5380
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005381/**
5382 * ufshcd_runtime_resume - runtime resume routine
5383 * @hba: per adapter instance
5384 *
5385 * This function basically brings the UFS device, UniPro link and controller
5386 * to active state. Following operations are done in this function:
5387 *
5388 * 1. Turn on all the controller related clocks
5389 * 2. Bring the UniPro link out of Hibernate state
5390 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
5391 * to active state.
5392 * 4. If auto-bkops is enabled on the device, disable it.
5393 *
5394 * So following would be the possible power state after this function return
5395 * successfully:
5396 * S1: UFS device in Active state with VCC rail ON
5397 * UniPro link in Active state
5398 * All the UFS/UniPro controller clocks are ON
5399 *
5400 * Returns 0 for success and non-zero for failure
5401 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305402int ufshcd_runtime_resume(struct ufs_hba *hba)
5403{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005404 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305405 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005406 else
5407 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305408}
5409EXPORT_SYMBOL(ufshcd_runtime_resume);
5410
5411int ufshcd_runtime_idle(struct ufs_hba *hba)
5412{
5413 return 0;
5414}
5415EXPORT_SYMBOL(ufshcd_runtime_idle);
5416
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305417/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005418 * ufshcd_shutdown - shutdown routine
5419 * @hba: per adapter instance
5420 *
5421 * This function would power off both UFS device and UFS link.
5422 *
5423 * Returns 0 always to allow force shutdown even in case of errors.
5424 */
5425int ufshcd_shutdown(struct ufs_hba *hba)
5426{
5427 int ret = 0;
5428
5429 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
5430 goto out;
5431
5432 if (pm_runtime_suspended(hba->dev)) {
5433 ret = ufshcd_runtime_resume(hba);
5434 if (ret)
5435 goto out;
5436 }
5437
5438 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
5439out:
5440 if (ret)
5441 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
5442 /* allow force shutdown even in case of errors */
5443 return 0;
5444}
5445EXPORT_SYMBOL(ufshcd_shutdown);
5446
5447/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305448 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305449 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305450 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305451 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305452void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305453{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05305454 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305455 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305456 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305457 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305458
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305459 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005460
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005461 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005462 if (ufshcd_is_clkscaling_enabled(hba))
5463 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005464 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305465}
5466EXPORT_SYMBOL_GPL(ufshcd_remove);
5467
5468/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02005469 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
5470 * @hba: pointer to Host Bus Adapter (HBA)
5471 */
5472void ufshcd_dealloc_host(struct ufs_hba *hba)
5473{
5474 scsi_host_put(hba->host);
5475}
5476EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
5477
5478/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005479 * ufshcd_set_dma_mask - Set dma mask based on the controller
5480 * addressing capability
5481 * @hba: per adapter instance
5482 *
5483 * Returns 0 for success, non-zero for failure
5484 */
5485static int ufshcd_set_dma_mask(struct ufs_hba *hba)
5486{
5487 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
5488 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
5489 return 0;
5490 }
5491 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
5492}
5493
5494/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005495 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305496 * @dev: pointer to device handle
5497 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305498 * Returns 0 on success, non-zero value on failure
5499 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005500int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305501{
5502 struct Scsi_Host *host;
5503 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005504 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305505
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305506 if (!dev) {
5507 dev_err(dev,
5508 "Invalid memory reference for dev is NULL\n");
5509 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305510 goto out_error;
5511 }
5512
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305513 host = scsi_host_alloc(&ufshcd_driver_template,
5514 sizeof(struct ufs_hba));
5515 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305516 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305517 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305518 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305519 }
5520 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305521 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305522 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005523 *hba_handle = hba;
5524
5525out_error:
5526 return err;
5527}
5528EXPORT_SYMBOL(ufshcd_alloc_host);
5529
Sahitya Tummala856b3482014-09-25 15:32:34 +03005530static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
5531{
5532 int ret = 0;
5533 struct ufs_clk_info *clki;
5534 struct list_head *head = &hba->clk_list_head;
5535
5536 if (!head || list_empty(head))
5537 goto out;
5538
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005539 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
5540 if (ret)
5541 return ret;
5542
Sahitya Tummala856b3482014-09-25 15:32:34 +03005543 list_for_each_entry(clki, head, list) {
5544 if (!IS_ERR_OR_NULL(clki->clk)) {
5545 if (scale_up && clki->max_freq) {
5546 if (clki->curr_freq == clki->max_freq)
5547 continue;
5548 ret = clk_set_rate(clki->clk, clki->max_freq);
5549 if (ret) {
5550 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5551 __func__, clki->name,
5552 clki->max_freq, ret);
5553 break;
5554 }
5555 clki->curr_freq = clki->max_freq;
5556
5557 } else if (!scale_up && clki->min_freq) {
5558 if (clki->curr_freq == clki->min_freq)
5559 continue;
5560 ret = clk_set_rate(clki->clk, clki->min_freq);
5561 if (ret) {
5562 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5563 __func__, clki->name,
5564 clki->min_freq, ret);
5565 break;
5566 }
5567 clki->curr_freq = clki->min_freq;
5568 }
5569 }
5570 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
5571 clki->name, clk_get_rate(clki->clk));
5572 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005573
5574 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
5575
Sahitya Tummala856b3482014-09-25 15:32:34 +03005576out:
5577 return ret;
5578}
5579
5580static int ufshcd_devfreq_target(struct device *dev,
5581 unsigned long *freq, u32 flags)
5582{
5583 int err = 0;
5584 struct ufs_hba *hba = dev_get_drvdata(dev);
5585
5586 if (!ufshcd_is_clkscaling_enabled(hba))
5587 return -EINVAL;
5588
5589 if (*freq == UINT_MAX)
5590 err = ufshcd_scale_clks(hba, true);
5591 else if (*freq == 0)
5592 err = ufshcd_scale_clks(hba, false);
5593
5594 return err;
5595}
5596
5597static int ufshcd_devfreq_get_dev_status(struct device *dev,
5598 struct devfreq_dev_status *stat)
5599{
5600 struct ufs_hba *hba = dev_get_drvdata(dev);
5601 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
5602 unsigned long flags;
5603
5604 if (!ufshcd_is_clkscaling_enabled(hba))
5605 return -EINVAL;
5606
5607 memset(stat, 0, sizeof(*stat));
5608
5609 spin_lock_irqsave(hba->host->host_lock, flags);
5610 if (!scaling->window_start_t)
5611 goto start_window;
5612
5613 if (scaling->is_busy_started)
5614 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
5615 scaling->busy_start_t));
5616
5617 stat->total_time = jiffies_to_usecs((long)jiffies -
5618 (long)scaling->window_start_t);
5619 stat->busy_time = scaling->tot_busy_t;
5620start_window:
5621 scaling->window_start_t = jiffies;
5622 scaling->tot_busy_t = 0;
5623
5624 if (hba->outstanding_reqs) {
5625 scaling->busy_start_t = ktime_get();
5626 scaling->is_busy_started = true;
5627 } else {
5628 scaling->busy_start_t = ktime_set(0, 0);
5629 scaling->is_busy_started = false;
5630 }
5631 spin_unlock_irqrestore(hba->host->host_lock, flags);
5632 return 0;
5633}
5634
5635static struct devfreq_dev_profile ufs_devfreq_profile = {
5636 .polling_ms = 100,
5637 .target = ufshcd_devfreq_target,
5638 .get_dev_status = ufshcd_devfreq_get_dev_status,
5639};
5640
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005641/**
5642 * ufshcd_init - Driver initialization routine
5643 * @hba: per-adapter instance
5644 * @mmio_base: base register address
5645 * @irq: Interrupt line of device
5646 * Returns 0 on success, non-zero value on failure
5647 */
5648int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
5649{
5650 int err;
5651 struct Scsi_Host *host = hba->host;
5652 struct device *dev = hba->dev;
5653
5654 if (!mmio_base) {
5655 dev_err(hba->dev,
5656 "Invalid memory reference for mmio_base is NULL\n");
5657 err = -ENODEV;
5658 goto out_error;
5659 }
5660
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305661 hba->mmio_base = mmio_base;
5662 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305663
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005664 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005665 if (err)
5666 goto out_error;
5667
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305668 /* Read capabilities registers */
5669 ufshcd_hba_capabilities(hba);
5670
5671 /* Get UFS version supported by the controller */
5672 hba->ufs_version = ufshcd_get_ufs_version(hba);
5673
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305674 /* Get Interrupt bit mask per version */
5675 hba->intr_mask = ufshcd_get_intr_mask(hba);
5676
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005677 err = ufshcd_set_dma_mask(hba);
5678 if (err) {
5679 dev_err(hba->dev, "set dma mask failed\n");
5680 goto out_disable;
5681 }
5682
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305683 /* Allocate memory for host memory space */
5684 err = ufshcd_memory_alloc(hba);
5685 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305686 dev_err(hba->dev, "Memory allocation failed\n");
5687 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305688 }
5689
5690 /* Configure LRB */
5691 ufshcd_host_memory_configure(hba);
5692
5693 host->can_queue = hba->nutrs;
5694 host->cmd_per_lun = hba->nutrs;
5695 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005696 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305697 host->max_channel = UFSHCD_MAX_CHANNEL;
5698 host->unique_id = host->host_no;
5699 host->max_cmd_len = MAX_CDB_SIZE;
5700
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005701 hba->max_pwr_info.is_valid = false;
5702
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305703 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305704 init_waitqueue_head(&hba->tm_wq);
5705 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305706
5707 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305708 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305709 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305710
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305711 /* Initialize UIC command mutex */
5712 mutex_init(&hba->uic_cmd_mutex);
5713
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305714 /* Initialize mutex for device management commands */
5715 mutex_init(&hba->dev_cmd.lock);
5716
5717 /* Initialize device management tag acquire wait queue */
5718 init_waitqueue_head(&hba->dev_cmd.tag_wq);
5719
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005720 ufshcd_init_clk_gating(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305721 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09005722 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305723 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305724 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005725 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005726 } else {
5727 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305728 }
5729
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305730 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305731 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305732 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005733 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305734 }
5735
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305736 /* Host controller enable */
5737 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305738 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305739 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305740 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305741 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305742
Sahitya Tummala856b3482014-09-25 15:32:34 +03005743 if (ufshcd_is_clkscaling_enabled(hba)) {
5744 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
5745 "simple_ondemand", NULL);
5746 if (IS_ERR(hba->devfreq)) {
5747 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
5748 PTR_ERR(hba->devfreq));
5749 goto out_remove_scsi_host;
5750 }
5751 /* Suspend devfreq until the UFS device is detected */
5752 devfreq_suspend_device(hba->devfreq);
5753 hba->clk_scaling.window_start_t = 0;
5754 }
5755
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305756 /* Hold auto suspend until async scan completes */
5757 pm_runtime_get_sync(dev);
5758
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005759 /*
5760 * The device-initialize-sequence hasn't been invoked yet.
5761 * Set the device to power-off state
5762 */
5763 ufshcd_set_ufs_dev_poweroff(hba);
5764
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305765 async_schedule(ufshcd_async_scan, hba);
5766
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305767 return 0;
5768
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305769out_remove_scsi_host:
5770 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005771exit_gating:
5772 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305773out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005774 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305775 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005776 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305777out_error:
5778 return err;
5779}
5780EXPORT_SYMBOL_GPL(ufshcd_init);
5781
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305782MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
5783MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05305784MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305785MODULE_LICENSE("GPL");
5786MODULE_VERSION(UFSHCD_DRIVER_VERSION);