blob: da08a40c4152eff845ba2ada0418c39436a061b5 [file] [log] [blame]
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301/*
Vinayak Holikattie0eca632013-02-25 21:44:33 +05302 * Universal Flash Storage Host controller driver Core
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303 *
4 * This code is based on drivers/scsi/ufs/ufshcd.c
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305 * Copyright (C) 2011-2013 Samsung India Software Operations
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02006 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308 * Authors:
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053016 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053018 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053024 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
34 * this program.
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030035 *
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053038 */
39
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053040#include <linux/async.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030041#include <linux/devfreq.h>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053042
Vinayak Holikattie0eca632013-02-25 21:44:33 +053043#include "ufshcd.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053044#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053045
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053046#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
47 UTP_TASK_REQ_COMPL |\
48 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053049/* UIC command timeout, unit: ms */
50#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053051
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053052/* NOP OUT retries waiting for NOP IN response */
53#define NOP_OUT_RETRIES 10
54/* Timeout after 30 msecs if NOP OUT hangs without response */
55#define NOP_OUT_TIMEOUT 30 /* msecs */
56
Dolev Raviv68078d52013-07-30 00:35:58 +053057/* Query request retries */
58#define QUERY_REQ_RETRIES 10
59/* Query request timeout */
60#define QUERY_REQ_TIMEOUT 30 /* msec */
Yaniv Gardie5ad4062016-02-01 15:02:41 +020061/*
62 * Query request timeout for fDeviceInit flag
63 * fDeviceInit query response time for some devices is too large that default
64 * QUERY_REQ_TIMEOUT may not be enough for such devices.
65 */
66#define QUERY_FDEVICEINIT_REQ_TIMEOUT 600 /* msec */
Dolev Raviv68078d52013-07-30 00:35:58 +053067
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053068/* Task management command timeout */
69#define TM_CMD_TIMEOUT 100 /* msecs */
70
Yaniv Gardi64238fb2016-02-01 15:02:43 +020071/* maximum number of retries for a general UIC command */
72#define UFS_UIC_COMMAND_RETRIES 3
73
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030074/* maximum number of link-startup retries */
75#define DME_LINKSTARTUP_RETRIES 3
76
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +020077/* Maximum retries for Hibern8 enter */
78#define UIC_HIBERN8_ENTER_RETRIES 3
79
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030080/* maximum number of reset retries before giving up */
81#define MAX_HOST_RESET_RETRIES 5
82
Dolev Raviv68078d52013-07-30 00:35:58 +053083/* Expose the flag value from utp_upiu_query.value */
84#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
85
Seungwon Jeon7d568652013-08-31 21:40:20 +053086/* Interrupt aggregation default timeout, unit: 40us */
87#define INT_AGGR_DEF_TO 0x02
88
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030089#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
90 ({ \
91 int _ret; \
92 if (_on) \
93 _ret = ufshcd_enable_vreg(_dev, _vreg); \
94 else \
95 _ret = ufshcd_disable_vreg(_dev, _vreg); \
96 _ret; \
97 })
98
Subhash Jadavanida461ce2014-09-25 15:32:25 +030099static u32 ufs_query_desc_max_size[] = {
100 QUERY_DESC_DEVICE_MAX_SIZE,
101 QUERY_DESC_CONFIGURAION_MAX_SIZE,
102 QUERY_DESC_UNIT_MAX_SIZE,
103 QUERY_DESC_RFU_MAX_SIZE,
104 QUERY_DESC_INTERCONNECT_MAX_SIZE,
105 QUERY_DESC_STRING_MAX_SIZE,
106 QUERY_DESC_RFU_MAX_SIZE,
107 QUERY_DESC_GEOMETRY_MAZ_SIZE,
108 QUERY_DESC_POWER_MAX_SIZE,
109 QUERY_DESC_RFU_MAX_SIZE,
110};
111
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530112enum {
113 UFSHCD_MAX_CHANNEL = 0,
114 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530115 UFSHCD_CMD_PER_LUN = 32,
116 UFSHCD_CAN_QUEUE = 32,
117};
118
119/* UFSHCD states */
120enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530121 UFSHCD_STATE_RESET,
122 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530123 UFSHCD_STATE_OPERATIONAL,
124};
125
126/* UFSHCD error handling flags */
127enum {
128 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530129};
130
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530131/* UFSHCD UIC layer error flags */
132enum {
133 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
134 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
135 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
136 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
137};
138
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530139/* Interrupt configuration options */
140enum {
141 UFSHCD_INT_DISABLE,
142 UFSHCD_INT_ENABLE,
143 UFSHCD_INT_CLEAR,
144};
145
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530146#define ufshcd_set_eh_in_progress(h) \
147 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
148#define ufshcd_eh_in_progress(h) \
149 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
150#define ufshcd_clear_eh_in_progress(h) \
151 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
152
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300153#define ufshcd_set_ufs_dev_active(h) \
154 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
155#define ufshcd_set_ufs_dev_sleep(h) \
156 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
157#define ufshcd_set_ufs_dev_poweroff(h) \
158 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
159#define ufshcd_is_ufs_dev_active(h) \
160 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
161#define ufshcd_is_ufs_dev_sleep(h) \
162 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
163#define ufshcd_is_ufs_dev_poweroff(h) \
164 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
165
166static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
167 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
168 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
169 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
170 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
171 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
172 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
173};
174
175static inline enum ufs_dev_pwr_mode
176ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
177{
178 return ufs_pm_lvl_states[lvl].dev_state;
179}
180
181static inline enum uic_link_state
182ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
183{
184 return ufs_pm_lvl_states[lvl].link_state;
185}
186
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530187static void ufshcd_tmc_handler(struct ufs_hba *hba);
188static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530189static int ufshcd_reset_and_restore(struct ufs_hba *hba);
190static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300191static void ufshcd_hba_exit(struct ufs_hba *hba);
192static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300193static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
194 bool skip_ref_clk);
195static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
196static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
197static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Yaniv Gardicad2e032015-03-31 17:37:14 +0300198static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300199static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
200static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300201static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
202 struct ufs_pa_layer_attr *desired_pwr_mode);
Yaniv Gardi874237f2015-05-17 18:55:03 +0300203static int ufshcd_change_power_mode(struct ufs_hba *hba,
204 struct ufs_pa_layer_attr *pwr_mode);
Yaniv Gardi14497322016-02-01 15:02:39 +0200205static inline bool ufshcd_valid_tag(struct ufs_hba *hba, int tag)
206{
207 return tag >= 0 && tag < hba->nutrs;
208}
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300209
210static inline int ufshcd_enable_irq(struct ufs_hba *hba)
211{
212 int ret = 0;
213
214 if (!hba->is_irq_enabled) {
215 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
216 hba);
217 if (ret)
218 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
219 __func__, ret);
220 hba->is_irq_enabled = true;
221 }
222
223 return ret;
224}
225
226static inline void ufshcd_disable_irq(struct ufs_hba *hba)
227{
228 if (hba->is_irq_enabled) {
229 free_irq(hba->irq, hba);
230 hba->is_irq_enabled = false;
231 }
232}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530233
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530234/*
235 * ufshcd_wait_for_register - wait for register value to change
236 * @hba - per-adapter interface
237 * @reg - mmio register offset
238 * @mask - mask to apply to read register value
239 * @val - wait condition
240 * @interval_us - polling interval in microsecs
241 * @timeout_ms - timeout in millisecs
242 *
243 * Returns -ETIMEDOUT on error, zero on success
244 */
245static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
246 u32 val, unsigned long interval_us, unsigned long timeout_ms)
247{
248 int err = 0;
249 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
250
251 /* ignore bits that we don't intend to wait on */
252 val = val & mask;
253
254 while ((ufshcd_readl(hba, reg) & mask) != val) {
255 /* wakeup within 50us of expiry */
256 usleep_range(interval_us, interval_us + 50);
257
258 if (time_after(jiffies, timeout)) {
259 if ((ufshcd_readl(hba, reg) & mask) != val)
260 err = -ETIMEDOUT;
261 break;
262 }
263 }
264
265 return err;
266}
267
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530268/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530269 * ufshcd_get_intr_mask - Get the interrupt bit mask
270 * @hba - Pointer to adapter instance
271 *
272 * Returns interrupt bit mask per version
273 */
274static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
275{
276 if (hba->ufs_version == UFSHCI_VERSION_10)
277 return INTERRUPT_MASK_ALL_VER_10;
278 else
279 return INTERRUPT_MASK_ALL_VER_11;
280}
281
282/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530283 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
284 * @hba - Pointer to adapter instance
285 *
286 * Returns UFSHCI version supported by the controller
287 */
288static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
289{
Yaniv Gardi0263bcd2015-10-28 13:15:48 +0200290 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION)
291 return ufshcd_vops_get_ufs_hci_version(hba);
Yaniv Gardi9949e702015-05-17 18:55:05 +0300292
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530293 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530294}
295
296/**
297 * ufshcd_is_device_present - Check if any device connected to
298 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300299 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530300 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530301 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530302 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300303static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530304{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300305 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
306 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530307}
308
309/**
310 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
311 * @lrb: pointer to local command reference block
312 *
313 * This function is used to get the OCS field from UTRD
314 * Returns the OCS field in the UTRD
315 */
316static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
317{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530318 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530319}
320
321/**
322 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
323 * @task_req_descp: pointer to utp_task_req_desc structure
324 *
325 * This function is used to get the OCS field from UTMRD
326 * Returns the OCS field in the UTMRD
327 */
328static inline int
329ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
330{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530331 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530332}
333
334/**
335 * ufshcd_get_tm_free_slot - get a free slot for task management request
336 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530337 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530338 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530339 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
340 * Returns 0 if free slot is not available, else return 1 with tag value
341 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530342 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530343static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530344{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530345 int tag;
346 bool ret = false;
347
348 if (!free_slot)
349 goto out;
350
351 do {
352 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
353 if (tag >= hba->nutmrs)
354 goto out;
355 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
356
357 *free_slot = tag;
358 ret = true;
359out:
360 return ret;
361}
362
363static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
364{
365 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530366}
367
368/**
369 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
370 * @hba: per adapter instance
371 * @pos: position of the bit to be cleared
372 */
373static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
374{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530375 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530376}
377
378/**
Yaniv Gardia48353f2016-02-01 15:02:40 +0200379 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
380 * @hba: per adapter instance
381 * @tag: position of the bit to be cleared
382 */
383static inline void ufshcd_outstanding_req_clear(struct ufs_hba *hba, int tag)
384{
385 __clear_bit(tag, &hba->outstanding_reqs);
386}
387
388/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530389 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
390 * @reg: Register value of host controller status
391 *
392 * Returns integer, 0 on Success and positive value if failed
393 */
394static inline int ufshcd_get_lists_status(u32 reg)
395{
396 /*
397 * The mask 0xFF is for the following HCS register bits
398 * Bit Description
399 * 0 Device Present
400 * 1 UTRLRDY
401 * 2 UTMRLRDY
402 * 3 UCRDY
403 * 4 HEI
404 * 5 DEI
405 * 6-7 reserved
406 */
407 return (((reg) & (0xFF)) >> 1) ^ (0x07);
408}
409
410/**
411 * ufshcd_get_uic_cmd_result - Get the UIC command result
412 * @hba: Pointer to adapter instance
413 *
414 * This function gets the result of UIC command completion
415 * Returns 0 on success, non zero value on error
416 */
417static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
418{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530419 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530420 MASK_UIC_COMMAND_RESULT;
421}
422
423/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530424 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
425 * @hba: Pointer to adapter instance
426 *
427 * This function gets UIC command argument3
428 * Returns 0 on success, non zero value on error
429 */
430static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
431{
432 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
433}
434
435/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530436 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530437 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530438 */
439static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530440ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530441{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530442 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530443}
444
445/**
446 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
447 * @ucd_rsp_ptr: pointer to response UPIU
448 *
449 * This function gets the response status and scsi_status from response UPIU
450 * Returns the response result code.
451 */
452static inline int
453ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
454{
455 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
456}
457
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530458/*
459 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
460 * from response UPIU
461 * @ucd_rsp_ptr: pointer to response UPIU
462 *
463 * Return the data segment length.
464 */
465static inline unsigned int
466ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
467{
468 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
469 MASK_RSP_UPIU_DATA_SEG_LEN;
470}
471
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530472/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530473 * ufshcd_is_exception_event - Check if the device raised an exception event
474 * @ucd_rsp_ptr: pointer to response UPIU
475 *
476 * The function checks if the device raised an exception event indicated in
477 * the Device Information field of response UPIU.
478 *
479 * Returns true if exception is raised, false otherwise.
480 */
481static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
482{
483 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
484 MASK_RSP_EXCEPTION_EVENT ? true : false;
485}
486
487/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530488 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530489 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530490 */
491static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530492ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530493{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530494 ufshcd_writel(hba, INT_AGGR_ENABLE |
495 INT_AGGR_COUNTER_AND_TIMER_RESET,
496 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
497}
498
499/**
500 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
501 * @hba: per adapter instance
502 * @cnt: Interrupt aggregation counter threshold
503 * @tmout: Interrupt aggregation timeout value
504 */
505static inline void
506ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
507{
508 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
509 INT_AGGR_COUNTER_THLD_VAL(cnt) |
510 INT_AGGR_TIMEOUT_VAL(tmout),
511 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530512}
513
514/**
Yaniv Gardib8521902015-05-17 18:54:57 +0300515 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
516 * @hba: per adapter instance
517 */
518static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba)
519{
520 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
521}
522
523/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530524 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
525 * When run-stop registers are set to 1, it indicates the
526 * host controller that it can process the requests
527 * @hba: per adapter instance
528 */
529static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
530{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530531 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
532 REG_UTP_TASK_REQ_LIST_RUN_STOP);
533 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
534 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530535}
536
537/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530538 * ufshcd_hba_start - Start controller initialization sequence
539 * @hba: per adapter instance
540 */
541static inline void ufshcd_hba_start(struct ufs_hba *hba)
542{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530543 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530544}
545
546/**
547 * ufshcd_is_hba_active - Get controller state
548 * @hba: per adapter instance
549 *
550 * Returns zero if controller is active, 1 otherwise
551 */
552static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
553{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530554 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530555}
556
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300557static void ufshcd_ungate_work(struct work_struct *work)
558{
559 int ret;
560 unsigned long flags;
561 struct ufs_hba *hba = container_of(work, struct ufs_hba,
562 clk_gating.ungate_work);
563
564 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
565
566 spin_lock_irqsave(hba->host->host_lock, flags);
567 if (hba->clk_gating.state == CLKS_ON) {
568 spin_unlock_irqrestore(hba->host->host_lock, flags);
569 goto unblock_reqs;
570 }
571
572 spin_unlock_irqrestore(hba->host->host_lock, flags);
573 ufshcd_setup_clocks(hba, true);
574
575 /* Exit from hibern8 */
576 if (ufshcd_can_hibern8_during_gating(hba)) {
577 /* Prevent gating in this path */
578 hba->clk_gating.is_suspended = true;
579 if (ufshcd_is_link_hibern8(hba)) {
580 ret = ufshcd_uic_hibern8_exit(hba);
581 if (ret)
582 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
583 __func__, ret);
584 else
585 ufshcd_set_link_active(hba);
586 }
587 hba->clk_gating.is_suspended = false;
588 }
589unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300590 if (ufshcd_is_clkscaling_enabled(hba))
591 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300592 scsi_unblock_requests(hba->host);
593}
594
595/**
596 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
597 * Also, exit from hibern8 mode and set the link as active.
598 * @hba: per adapter instance
599 * @async: This indicates whether caller should ungate clocks asynchronously.
600 */
601int ufshcd_hold(struct ufs_hba *hba, bool async)
602{
603 int rc = 0;
604 unsigned long flags;
605
606 if (!ufshcd_is_clkgating_allowed(hba))
607 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300608 spin_lock_irqsave(hba->host->host_lock, flags);
609 hba->clk_gating.active_reqs++;
610
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200611 if (ufshcd_eh_in_progress(hba)) {
612 spin_unlock_irqrestore(hba->host->host_lock, flags);
613 return 0;
614 }
615
Sahitya Tummala856b3482014-09-25 15:32:34 +0300616start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300617 switch (hba->clk_gating.state) {
618 case CLKS_ON:
619 break;
620 case REQ_CLKS_OFF:
621 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
622 hba->clk_gating.state = CLKS_ON;
623 break;
624 }
625 /*
626 * If we here, it means gating work is either done or
627 * currently running. Hence, fall through to cancel gating
628 * work and to enable clocks.
629 */
630 case CLKS_OFF:
631 scsi_block_requests(hba->host);
632 hba->clk_gating.state = REQ_CLKS_ON;
633 schedule_work(&hba->clk_gating.ungate_work);
634 /*
635 * fall through to check if we should wait for this
636 * work to be done or not.
637 */
638 case REQ_CLKS_ON:
639 if (async) {
640 rc = -EAGAIN;
641 hba->clk_gating.active_reqs--;
642 break;
643 }
644
645 spin_unlock_irqrestore(hba->host->host_lock, flags);
646 flush_work(&hba->clk_gating.ungate_work);
647 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300648 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300649 goto start;
650 default:
651 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
652 __func__, hba->clk_gating.state);
653 break;
654 }
655 spin_unlock_irqrestore(hba->host->host_lock, flags);
656out:
657 return rc;
658}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200659EXPORT_SYMBOL_GPL(ufshcd_hold);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300660
661static void ufshcd_gate_work(struct work_struct *work)
662{
663 struct ufs_hba *hba = container_of(work, struct ufs_hba,
664 clk_gating.gate_work.work);
665 unsigned long flags;
666
667 spin_lock_irqsave(hba->host->host_lock, flags);
668 if (hba->clk_gating.is_suspended) {
669 hba->clk_gating.state = CLKS_ON;
670 goto rel_lock;
671 }
672
673 if (hba->clk_gating.active_reqs
674 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
675 || hba->lrb_in_use || hba->outstanding_tasks
676 || hba->active_uic_cmd || hba->uic_async_done)
677 goto rel_lock;
678
679 spin_unlock_irqrestore(hba->host->host_lock, flags);
680
681 /* put the link into hibern8 mode before turning off clocks */
682 if (ufshcd_can_hibern8_during_gating(hba)) {
683 if (ufshcd_uic_hibern8_enter(hba)) {
684 hba->clk_gating.state = CLKS_ON;
685 goto out;
686 }
687 ufshcd_set_link_hibern8(hba);
688 }
689
Sahitya Tummala856b3482014-09-25 15:32:34 +0300690 if (ufshcd_is_clkscaling_enabled(hba)) {
691 devfreq_suspend_device(hba->devfreq);
692 hba->clk_scaling.window_start_t = 0;
693 }
694
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300695 if (!ufshcd_is_link_active(hba))
696 ufshcd_setup_clocks(hba, false);
697 else
698 /* If link is active, device ref_clk can't be switched off */
699 __ufshcd_setup_clocks(hba, false, true);
700
701 /*
702 * In case you are here to cancel this work the gating state
703 * would be marked as REQ_CLKS_ON. In this case keep the state
704 * as REQ_CLKS_ON which would anyway imply that clocks are off
705 * and a request to turn them on is pending. By doing this way,
706 * we keep the state machine in tact and this would ultimately
707 * prevent from doing cancel work multiple times when there are
708 * new requests arriving before the current cancel work is done.
709 */
710 spin_lock_irqsave(hba->host->host_lock, flags);
711 if (hba->clk_gating.state == REQ_CLKS_OFF)
712 hba->clk_gating.state = CLKS_OFF;
713
714rel_lock:
715 spin_unlock_irqrestore(hba->host->host_lock, flags);
716out:
717 return;
718}
719
720/* host lock must be held before calling this variant */
721static void __ufshcd_release(struct ufs_hba *hba)
722{
723 if (!ufshcd_is_clkgating_allowed(hba))
724 return;
725
726 hba->clk_gating.active_reqs--;
727
728 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
729 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
730 || hba->lrb_in_use || hba->outstanding_tasks
Yaniv Gardi53c12d02016-02-01 15:02:45 +0200731 || hba->active_uic_cmd || hba->uic_async_done
732 || ufshcd_eh_in_progress(hba))
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300733 return;
734
735 hba->clk_gating.state = REQ_CLKS_OFF;
736 schedule_delayed_work(&hba->clk_gating.gate_work,
737 msecs_to_jiffies(hba->clk_gating.delay_ms));
738}
739
740void ufshcd_release(struct ufs_hba *hba)
741{
742 unsigned long flags;
743
744 spin_lock_irqsave(hba->host->host_lock, flags);
745 __ufshcd_release(hba);
746 spin_unlock_irqrestore(hba->host->host_lock, flags);
747}
Yaniv Gardi6e3fd442015-10-28 13:15:50 +0200748EXPORT_SYMBOL_GPL(ufshcd_release);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300749
750static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
751 struct device_attribute *attr, char *buf)
752{
753 struct ufs_hba *hba = dev_get_drvdata(dev);
754
755 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
756}
757
758static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
759 struct device_attribute *attr, const char *buf, size_t count)
760{
761 struct ufs_hba *hba = dev_get_drvdata(dev);
762 unsigned long flags, value;
763
764 if (kstrtoul(buf, 0, &value))
765 return -EINVAL;
766
767 spin_lock_irqsave(hba->host->host_lock, flags);
768 hba->clk_gating.delay_ms = value;
769 spin_unlock_irqrestore(hba->host->host_lock, flags);
770 return count;
771}
772
773static void ufshcd_init_clk_gating(struct ufs_hba *hba)
774{
775 if (!ufshcd_is_clkgating_allowed(hba))
776 return;
777
778 hba->clk_gating.delay_ms = 150;
779 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
780 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
781
782 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
783 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
784 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
785 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
786 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
787 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
788 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
789}
790
791static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
792{
793 if (!ufshcd_is_clkgating_allowed(hba))
794 return;
795 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
Akinobu Mita97cd6802014-11-24 14:24:18 +0900796 cancel_work_sync(&hba->clk_gating.ungate_work);
797 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300798}
799
Sahitya Tummala856b3482014-09-25 15:32:34 +0300800/* Must be called with host lock acquired */
801static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
802{
803 if (!ufshcd_is_clkscaling_enabled(hba))
804 return;
805
806 if (!hba->clk_scaling.is_busy_started) {
807 hba->clk_scaling.busy_start_t = ktime_get();
808 hba->clk_scaling.is_busy_started = true;
809 }
810}
811
812static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
813{
814 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
815
816 if (!ufshcd_is_clkscaling_enabled(hba))
817 return;
818
819 if (!hba->outstanding_reqs && scaling->is_busy_started) {
820 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
821 scaling->busy_start_t));
822 scaling->busy_start_t = ktime_set(0, 0);
823 scaling->is_busy_started = false;
824 }
825}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530826/**
827 * ufshcd_send_command - Send SCSI or device management commands
828 * @hba: per adapter instance
829 * @task_tag: Task tag of the command
830 */
831static inline
832void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
833{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300834 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530835 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530836 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530837}
838
839/**
840 * ufshcd_copy_sense_data - Copy sense data in case of check condition
841 * @lrb - pointer to local reference block
842 */
843static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
844{
845 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530846 if (lrbp->sense_buffer &&
847 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530848 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530849 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530850 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530851 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
852 }
853}
854
855/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530856 * ufshcd_copy_query_response() - Copy the Query Response and the data
857 * descriptor
858 * @hba: per adapter instance
859 * @lrb - pointer to local reference block
860 */
861static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300862int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530863{
864 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
865
Dolev Raviv68078d52013-07-30 00:35:58 +0530866 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530867
Dolev Raviv68078d52013-07-30 00:35:58 +0530868 /* Get the descriptor */
869 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300870 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530871 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300872 u16 resp_len;
873 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530874
875 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300876 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530877 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300878 buf_len = be16_to_cpu(
879 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300880 if (likely(buf_len >= resp_len)) {
881 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
882 } else {
883 dev_warn(hba->dev,
884 "%s: Response size is bigger than buffer",
885 __func__);
886 return -EINVAL;
887 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530888 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300889
890 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530891}
892
893/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530894 * ufshcd_hba_capabilities - Read controller capabilities
895 * @hba: per adapter instance
896 */
897static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
898{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530899 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530900
901 /* nutrs and nutmrs are 0 based values */
902 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
903 hba->nutmrs =
904 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
905}
906
907/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530908 * ufshcd_ready_for_uic_cmd - Check if controller is ready
909 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530910 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530911 * Return true on success, else false
912 */
913static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
914{
915 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
916 return true;
917 else
918 return false;
919}
920
921/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530922 * ufshcd_get_upmcrs - Get the power mode change request status
923 * @hba: Pointer to adapter instance
924 *
925 * This function gets the UPMCRS field of HCS register
926 * Returns value of UPMCRS field
927 */
928static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
929{
930 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
931}
932
933/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530934 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
935 * @hba: per adapter instance
936 * @uic_cmd: UIC command
937 *
938 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530939 */
940static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530941ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530942{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530943 WARN_ON(hba->active_uic_cmd);
944
945 hba->active_uic_cmd = uic_cmd;
946
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530947 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530948 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
949 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
950 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530951
952 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530953 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530954 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530955}
956
957/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530958 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
959 * @hba: per adapter instance
960 * @uic_command: UIC command
961 *
962 * Must be called with mutex held.
963 * Returns 0 only if success.
964 */
965static int
966ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
967{
968 int ret;
969 unsigned long flags;
970
971 if (wait_for_completion_timeout(&uic_cmd->done,
972 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
973 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
974 else
975 ret = -ETIMEDOUT;
976
977 spin_lock_irqsave(hba->host->host_lock, flags);
978 hba->active_uic_cmd = NULL;
979 spin_unlock_irqrestore(hba->host->host_lock, flags);
980
981 return ret;
982}
983
984/**
985 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
986 * @hba: per adapter instance
987 * @uic_cmd: UIC command
988 *
989 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300990 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530991 * Returns 0 only if success.
992 */
993static int
994__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
995{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530996 if (!ufshcd_ready_for_uic_cmd(hba)) {
997 dev_err(hba->dev,
998 "Controller not ready to accept UIC commands\n");
999 return -EIO;
1000 }
1001
1002 init_completion(&uic_cmd->done);
1003
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301004 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301005
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001006 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301007}
1008
1009/**
1010 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1011 * @hba: per adapter instance
1012 * @uic_cmd: UIC command
1013 *
1014 * Returns 0 only if success.
1015 */
1016static int
1017ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
1018{
1019 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001020 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301021
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001022 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301023 mutex_lock(&hba->uic_cmd_mutex);
Yaniv Gardicad2e032015-03-31 17:37:14 +03001024 ufshcd_add_delay_before_dme_cmd(hba);
1025
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001026 spin_lock_irqsave(hba->host->host_lock, flags);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301027 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03001028 spin_unlock_irqrestore(hba->host->host_lock, flags);
1029 if (!ret)
1030 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
1031
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301032 mutex_unlock(&hba->uic_cmd_mutex);
1033
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001034 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301035 return ret;
1036}
1037
1038/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301039 * ufshcd_map_sg - Map scatter-gather list to prdt
1040 * @lrbp - pointer to local reference block
1041 *
1042 * Returns 0 in case of success, non-zero value in case of failure
1043 */
1044static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
1045{
1046 struct ufshcd_sg_entry *prd_table;
1047 struct scatterlist *sg;
1048 struct scsi_cmnd *cmd;
1049 int sg_segments;
1050 int i;
1051
1052 cmd = lrbp->cmd;
1053 sg_segments = scsi_dma_map(cmd);
1054 if (sg_segments < 0)
1055 return sg_segments;
1056
1057 if (sg_segments) {
1058 lrbp->utr_descriptor_ptr->prd_table_length =
1059 cpu_to_le16((u16) (sg_segments));
1060
1061 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1062
1063 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1064 prd_table[i].size =
1065 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1066 prd_table[i].base_addr =
1067 cpu_to_le32(lower_32_bits(sg->dma_address));
1068 prd_table[i].upper_addr =
1069 cpu_to_le32(upper_32_bits(sg->dma_address));
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001070 prd_table[i].reserved = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301071 }
1072 } else {
1073 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1074 }
1075
1076 return 0;
1077}
1078
1079/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301080 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301081 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301082 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301083 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301084static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301085{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301086 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1087
1088 if (hba->ufs_version == UFSHCI_VERSION_10) {
1089 u32 rw;
1090 rw = set & INTERRUPT_MASK_RW_VER_10;
1091 set = rw | ((set ^ intrs) & intrs);
1092 } else {
1093 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301094 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301095
1096 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1097}
1098
1099/**
1100 * ufshcd_disable_intr - disable interrupts
1101 * @hba: per adapter instance
1102 * @intrs: interrupt bits
1103 */
1104static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1105{
1106 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1107
1108 if (hba->ufs_version == UFSHCI_VERSION_10) {
1109 u32 rw;
1110 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1111 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1112 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1113
1114 } else {
1115 set &= ~intrs;
1116 }
1117
1118 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301119}
1120
1121/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301122 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1123 * descriptor according to request
1124 * @lrbp: pointer to local reference block
1125 * @upiu_flags: flags required in the header
1126 * @cmd_dir: requests data direction
1127 */
1128static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
1129 u32 *upiu_flags, enum dma_data_direction cmd_dir)
1130{
1131 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1132 u32 data_direction;
1133 u32 dword_0;
1134
1135 if (cmd_dir == DMA_FROM_DEVICE) {
1136 data_direction = UTP_DEVICE_TO_HOST;
1137 *upiu_flags = UPIU_CMD_FLAGS_READ;
1138 } else if (cmd_dir == DMA_TO_DEVICE) {
1139 data_direction = UTP_HOST_TO_DEVICE;
1140 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1141 } else {
1142 data_direction = UTP_NO_DATA_TRANSFER;
1143 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1144 }
1145
1146 dword_0 = data_direction | (lrbp->command_type
1147 << UPIU_COMMAND_TYPE_OFFSET);
1148 if (lrbp->intr_cmd)
1149 dword_0 |= UTP_REQ_DESC_INT_CMD;
1150
1151 /* Transfer request descriptor header fields */
1152 req_desc->header.dword_0 = cpu_to_le32(dword_0);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001153 /* dword_1 is reserved, hence it is set to 0 */
1154 req_desc->header.dword_1 = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301155 /*
1156 * assigning invalid value for command status. Controller
1157 * updates OCS on command completion, with the command
1158 * status
1159 */
1160 req_desc->header.dword_2 =
1161 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001162 /* dword_3 is reserved, hence it is set to 0 */
1163 req_desc->header.dword_3 = 0;
Yaniv Gardi51047262016-02-01 15:02:38 +02001164
1165 req_desc->prd_table_length = 0;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301166}
1167
1168/**
1169 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1170 * for scsi commands
1171 * @lrbp - local reference block pointer
1172 * @upiu_flags - flags
1173 */
1174static
1175void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1176{
1177 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001178 unsigned short cdb_len;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301179
1180 /* command descriptor fields */
1181 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1182 UPIU_TRANSACTION_COMMAND, upiu_flags,
1183 lrbp->lun, lrbp->task_tag);
1184 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1185 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1186
1187 /* Total EHS length and Data segment length will be zero */
1188 ucd_req_ptr->header.dword_2 = 0;
1189
1190 ucd_req_ptr->sc.exp_data_transfer_len =
1191 cpu_to_be32(lrbp->cmd->sdb.length);
1192
Yaniv Gardi52ac95f2016-02-01 15:02:37 +02001193 cdb_len = min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE);
1194 memset(ucd_req_ptr->sc.cdb, 0, MAX_CDB_SIZE);
1195 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd, cdb_len);
1196
1197 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301198}
1199
Dolev Raviv68078d52013-07-30 00:35:58 +05301200/**
1201 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1202 * for query requsts
1203 * @hba: UFS hba
1204 * @lrbp: local reference block pointer
1205 * @upiu_flags: flags
1206 */
1207static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1208 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1209{
1210 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1211 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301212 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301213 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1214
1215 /* Query request header */
1216 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1217 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1218 lrbp->lun, lrbp->task_tag);
1219 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1220 0, query->request.query_func, 0, 0);
1221
1222 /* Data segment length */
1223 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
1224 0, 0, len >> 8, (u8)len);
1225
1226 /* Copy the Query Request buffer as is */
1227 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1228 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301229
1230 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001231 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1232 memcpy(descp, query->descriptor, len);
1233
Yaniv Gardi51047262016-02-01 15:02:38 +02001234 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Dolev Raviv68078d52013-07-30 00:35:58 +05301235}
1236
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301237static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1238{
1239 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1240
1241 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1242
1243 /* command descriptor fields */
1244 ucd_req_ptr->header.dword_0 =
1245 UPIU_HEADER_DWORD(
1246 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
Yaniv Gardi51047262016-02-01 15:02:38 +02001247 /* clear rest of the fields of basic header */
1248 ucd_req_ptr->header.dword_1 = 0;
1249 ucd_req_ptr->header.dword_2 = 0;
1250
1251 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp));
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301252}
1253
1254/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301255 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301256 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301257 * @lrb - pointer to local reference block
1258 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301259static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301260{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301261 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301262 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301263
1264 switch (lrbp->command_type) {
1265 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301266 if (likely(lrbp->cmd)) {
1267 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1268 lrbp->cmd->sc_data_direction);
1269 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301270 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301271 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301272 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301273 break;
1274 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301275 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301276 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1277 ufshcd_prepare_utp_query_req_upiu(
1278 hba, lrbp, upiu_flags);
1279 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301280 ufshcd_prepare_utp_nop_upiu(lrbp);
1281 else
1282 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301283 break;
1284 case UTP_CMD_TYPE_UFS:
1285 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301286 ret = -ENOTSUPP;
1287 dev_err(hba->dev, "%s: UFS native command are not supported\n",
1288 __func__);
1289 break;
1290 default:
1291 ret = -ENOTSUPP;
1292 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
1293 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301294 break;
1295 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301296
1297 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301298}
1299
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001300/*
1301 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1302 * @scsi_lun: scsi LUN id
1303 *
1304 * Returns UPIU LUN id
1305 */
1306static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1307{
1308 if (scsi_is_wlun(scsi_lun))
1309 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1310 | UFS_UPIU_WLUN_ID;
1311 else
1312 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1313}
1314
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301315/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001316 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1317 * @scsi_lun: UPIU W-LUN id
1318 *
1319 * Returns SCSI W-LUN id
1320 */
1321static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1322{
1323 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1324}
1325
1326/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301327 * ufshcd_queuecommand - main entry point for SCSI requests
1328 * @cmd: command from SCSI Midlayer
1329 * @done: call back function
1330 *
1331 * Returns 0 for success, non-zero in case of failure
1332 */
1333static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1334{
1335 struct ufshcd_lrb *lrbp;
1336 struct ufs_hba *hba;
1337 unsigned long flags;
1338 int tag;
1339 int err = 0;
1340
1341 hba = shost_priv(host);
1342
1343 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02001344 if (!ufshcd_valid_tag(hba, tag)) {
1345 dev_err(hba->dev,
1346 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
1347 __func__, tag, cmd, cmd->request);
1348 BUG();
1349 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301350
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301351 spin_lock_irqsave(hba->host->host_lock, flags);
1352 switch (hba->ufshcd_state) {
1353 case UFSHCD_STATE_OPERATIONAL:
1354 break;
1355 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301356 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301357 goto out_unlock;
1358 case UFSHCD_STATE_ERROR:
1359 set_host_byte(cmd, DID_ERROR);
1360 cmd->scsi_done(cmd);
1361 goto out_unlock;
1362 default:
1363 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1364 __func__, hba->ufshcd_state);
1365 set_host_byte(cmd, DID_BAD_TARGET);
1366 cmd->scsi_done(cmd);
1367 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301368 }
Yaniv Gardi53c12d02016-02-01 15:02:45 +02001369
1370 /* if error handling is in progress, don't issue commands */
1371 if (ufshcd_eh_in_progress(hba)) {
1372 set_host_byte(cmd, DID_ERROR);
1373 cmd->scsi_done(cmd);
1374 goto out_unlock;
1375 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301376 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301377
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301378 /* acquire the tag to make sure device cmds don't use it */
1379 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1380 /*
1381 * Dev manage command in progress, requeue the command.
1382 * Requeuing the command helps in cases where the request *may*
1383 * find different tag instead of waiting for dev manage command
1384 * completion.
1385 */
1386 err = SCSI_MLQUEUE_HOST_BUSY;
1387 goto out;
1388 }
1389
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001390 err = ufshcd_hold(hba, true);
1391 if (err) {
1392 err = SCSI_MLQUEUE_HOST_BUSY;
1393 clear_bit_unlock(tag, &hba->lrb_in_use);
1394 goto out;
1395 }
1396 WARN_ON(hba->clk_gating.state != CLKS_ON);
1397
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301398 lrbp = &hba->lrb[tag];
1399
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301400 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301401 lrbp->cmd = cmd;
1402 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1403 lrbp->sense_buffer = cmd->sense_buffer;
1404 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001405 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Yaniv Gardib8521902015-05-17 18:54:57 +03001406 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba) ? true : false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301407 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1408
1409 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301410 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301411 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301412 if (err) {
1413 lrbp->cmd = NULL;
1414 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301415 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301416 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301417
1418 /* issue command to the controller */
1419 spin_lock_irqsave(hba->host->host_lock, flags);
1420 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301421out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301422 spin_unlock_irqrestore(hba->host->host_lock, flags);
1423out:
1424 return err;
1425}
1426
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301427static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1428 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1429{
1430 lrbp->cmd = NULL;
1431 lrbp->sense_bufflen = 0;
1432 lrbp->sense_buffer = NULL;
1433 lrbp->task_tag = tag;
1434 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
1435 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1436 lrbp->intr_cmd = true; /* No interrupt aggregation */
1437 hba->dev_cmd.type = cmd_type;
1438
1439 return ufshcd_compose_upiu(hba, lrbp);
1440}
1441
1442static int
1443ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1444{
1445 int err = 0;
1446 unsigned long flags;
1447 u32 mask = 1 << tag;
1448
1449 /* clear outstanding transaction before retry */
1450 spin_lock_irqsave(hba->host->host_lock, flags);
1451 ufshcd_utrl_clear(hba, tag);
1452 spin_unlock_irqrestore(hba->host->host_lock, flags);
1453
1454 /*
1455 * wait for for h/w to clear corresponding bit in door-bell.
1456 * max. wait is 1 sec.
1457 */
1458 err = ufshcd_wait_for_register(hba,
1459 REG_UTP_TRANSFER_REQ_DOOR_BELL,
1460 mask, ~mask, 1000, 1000);
1461
1462 return err;
1463}
1464
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001465static int
1466ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1467{
1468 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1469
1470 /* Get the UPIU response */
1471 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1472 UPIU_RSP_CODE_OFFSET;
1473 return query_res->response;
1474}
1475
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301476/**
1477 * ufshcd_dev_cmd_completion() - handles device management command responses
1478 * @hba: per adapter instance
1479 * @lrbp: pointer to local reference block
1480 */
1481static int
1482ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1483{
1484 int resp;
1485 int err = 0;
1486
1487 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1488
1489 switch (resp) {
1490 case UPIU_TRANSACTION_NOP_IN:
1491 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1492 err = -EINVAL;
1493 dev_err(hba->dev, "%s: unexpected response %x\n",
1494 __func__, resp);
1495 }
1496 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301497 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001498 err = ufshcd_check_query_response(hba, lrbp);
1499 if (!err)
1500 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301501 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301502 case UPIU_TRANSACTION_REJECT_UPIU:
1503 /* TODO: handle Reject UPIU Response */
1504 err = -EPERM;
1505 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1506 __func__);
1507 break;
1508 default:
1509 err = -EINVAL;
1510 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1511 __func__, resp);
1512 break;
1513 }
1514
1515 return err;
1516}
1517
1518static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1519 struct ufshcd_lrb *lrbp, int max_timeout)
1520{
1521 int err = 0;
1522 unsigned long time_left;
1523 unsigned long flags;
1524
1525 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1526 msecs_to_jiffies(max_timeout));
1527
1528 spin_lock_irqsave(hba->host->host_lock, flags);
1529 hba->dev_cmd.complete = NULL;
1530 if (likely(time_left)) {
1531 err = ufshcd_get_tr_ocs(lrbp);
1532 if (!err)
1533 err = ufshcd_dev_cmd_completion(hba, lrbp);
1534 }
1535 spin_unlock_irqrestore(hba->host->host_lock, flags);
1536
1537 if (!time_left) {
1538 err = -ETIMEDOUT;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001539 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n",
1540 __func__, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301541 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
Yaniv Gardia48353f2016-02-01 15:02:40 +02001542 /* successfully cleared the command, retry if needed */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301543 err = -EAGAIN;
Yaniv Gardia48353f2016-02-01 15:02:40 +02001544 /*
1545 * in case of an error, after clearing the doorbell,
1546 * we also need to clear the outstanding_request
1547 * field in hba
1548 */
1549 ufshcd_outstanding_req_clear(hba, lrbp->task_tag);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301550 }
1551
1552 return err;
1553}
1554
1555/**
1556 * ufshcd_get_dev_cmd_tag - Get device management command tag
1557 * @hba: per-adapter instance
1558 * @tag: pointer to variable with available slot value
1559 *
1560 * Get a free slot and lock it until device management command
1561 * completes.
1562 *
1563 * Returns false if free slot is unavailable for locking, else
1564 * return true with tag value in @tag.
1565 */
1566static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1567{
1568 int tag;
1569 bool ret = false;
1570 unsigned long tmp;
1571
1572 if (!tag_out)
1573 goto out;
1574
1575 do {
1576 tmp = ~hba->lrb_in_use;
1577 tag = find_last_bit(&tmp, hba->nutrs);
1578 if (tag >= hba->nutrs)
1579 goto out;
1580 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1581
1582 *tag_out = tag;
1583 ret = true;
1584out:
1585 return ret;
1586}
1587
1588static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1589{
1590 clear_bit_unlock(tag, &hba->lrb_in_use);
1591}
1592
1593/**
1594 * ufshcd_exec_dev_cmd - API for sending device management requests
1595 * @hba - UFS hba
1596 * @cmd_type - specifies the type (NOP, Query...)
1597 * @timeout - time in seconds
1598 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301599 * NOTE: Since there is only one available tag for device management commands,
1600 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301601 */
1602static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1603 enum dev_cmd_type cmd_type, int timeout)
1604{
1605 struct ufshcd_lrb *lrbp;
1606 int err;
1607 int tag;
1608 struct completion wait;
1609 unsigned long flags;
1610
1611 /*
1612 * Get free slot, sleep if slots are unavailable.
1613 * Even though we use wait_event() which sleeps indefinitely,
1614 * the maximum wait time is bounded by SCSI request timeout.
1615 */
1616 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1617
1618 init_completion(&wait);
1619 lrbp = &hba->lrb[tag];
1620 WARN_ON(lrbp->cmd);
1621 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1622 if (unlikely(err))
1623 goto out_put_tag;
1624
1625 hba->dev_cmd.complete = &wait;
1626
1627 spin_lock_irqsave(hba->host->host_lock, flags);
1628 ufshcd_send_command(hba, tag);
1629 spin_unlock_irqrestore(hba->host->host_lock, flags);
1630
1631 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1632
1633out_put_tag:
1634 ufshcd_put_dev_cmd_tag(hba, tag);
1635 wake_up(&hba->dev_cmd.tag_wq);
1636 return err;
1637}
1638
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301639/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001640 * ufshcd_init_query() - init the query response and request parameters
1641 * @hba: per-adapter instance
1642 * @request: address of the request pointer to be initialized
1643 * @response: address of the response pointer to be initialized
1644 * @opcode: operation to perform
1645 * @idn: flag idn to access
1646 * @index: LU number to access
1647 * @selector: query/flag/descriptor further identification
1648 */
1649static inline void ufshcd_init_query(struct ufs_hba *hba,
1650 struct ufs_query_req **request, struct ufs_query_res **response,
1651 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1652{
1653 *request = &hba->dev_cmd.query.request;
1654 *response = &hba->dev_cmd.query.response;
1655 memset(*request, 0, sizeof(struct ufs_query_req));
1656 memset(*response, 0, sizeof(struct ufs_query_res));
1657 (*request)->upiu_req.opcode = opcode;
1658 (*request)->upiu_req.idn = idn;
1659 (*request)->upiu_req.index = index;
1660 (*request)->upiu_req.selector = selector;
1661}
1662
1663/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301664 * ufshcd_query_flag() - API function for sending flag query requests
1665 * hba: per-adapter instance
1666 * query_opcode: flag query to perform
1667 * idn: flag idn to access
1668 * flag_res: the flag value after the query request completes
1669 *
1670 * Returns 0 for success, non-zero in case of failure
1671 */
1672static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1673 enum flag_idn idn, bool *flag_res)
1674{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001675 struct ufs_query_req *request = NULL;
1676 struct ufs_query_res *response = NULL;
1677 int err, index = 0, selector = 0;
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001678 int timeout = QUERY_REQ_TIMEOUT;
Dolev Raviv68078d52013-07-30 00:35:58 +05301679
1680 BUG_ON(!hba);
1681
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001682 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301683 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001684 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1685 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301686
1687 switch (opcode) {
1688 case UPIU_QUERY_OPCODE_SET_FLAG:
1689 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1690 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1691 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1692 break;
1693 case UPIU_QUERY_OPCODE_READ_FLAG:
1694 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1695 if (!flag_res) {
1696 /* No dummy reads */
1697 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1698 __func__);
1699 err = -EINVAL;
1700 goto out_unlock;
1701 }
1702 break;
1703 default:
1704 dev_err(hba->dev,
1705 "%s: Expected query flag opcode but got = %d\n",
1706 __func__, opcode);
1707 err = -EINVAL;
1708 goto out_unlock;
1709 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301710
Yaniv Gardie5ad4062016-02-01 15:02:41 +02001711 if (idn == QUERY_FLAG_IDN_FDEVICEINIT)
1712 timeout = QUERY_FDEVICEINIT_REQ_TIMEOUT;
1713
1714 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout);
Dolev Raviv68078d52013-07-30 00:35:58 +05301715
1716 if (err) {
1717 dev_err(hba->dev,
1718 "%s: Sending flag query for idn %d failed, err = %d\n",
1719 __func__, idn, err);
1720 goto out_unlock;
1721 }
1722
1723 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301724 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301725 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1726
1727out_unlock:
1728 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001729 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301730 return err;
1731}
1732
1733/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301734 * ufshcd_query_attr - API function for sending attribute requests
1735 * hba: per-adapter instance
1736 * opcode: attribute opcode
1737 * idn: attribute idn to access
1738 * index: index field
1739 * selector: selector field
1740 * attr_val: the attribute value after the query request completes
1741 *
1742 * Returns 0 for success, non-zero in case of failure
1743*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301744static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301745 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1746{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001747 struct ufs_query_req *request = NULL;
1748 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301749 int err;
1750
1751 BUG_ON(!hba);
1752
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001753 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301754 if (!attr_val) {
1755 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1756 __func__, opcode);
1757 err = -EINVAL;
1758 goto out;
1759 }
1760
1761 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001762 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1763 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301764
1765 switch (opcode) {
1766 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1767 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301768 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301769 break;
1770 case UPIU_QUERY_OPCODE_READ_ATTR:
1771 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1772 break;
1773 default:
1774 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1775 __func__, opcode);
1776 err = -EINVAL;
1777 goto out_unlock;
1778 }
1779
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001780 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301781
1782 if (err) {
1783 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1784 __func__, opcode, idn, err);
1785 goto out_unlock;
1786 }
1787
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301788 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301789
1790out_unlock:
1791 mutex_unlock(&hba->dev_cmd.lock);
1792out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001793 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301794 return err;
1795}
1796
1797/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001798 * ufshcd_query_descriptor - API function for sending descriptor requests
1799 * hba: per-adapter instance
1800 * opcode: attribute opcode
1801 * idn: attribute idn to access
1802 * index: index field
1803 * selector: selector field
1804 * desc_buf: the buffer that contains the descriptor
1805 * buf_len: length parameter passed to the device
1806 *
1807 * Returns 0 for success, non-zero in case of failure.
1808 * The buf_len parameter will contain, on return, the length parameter
1809 * received on the response.
1810 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001811static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001812 enum query_opcode opcode, enum desc_idn idn, u8 index,
1813 u8 selector, u8 *desc_buf, int *buf_len)
1814{
1815 struct ufs_query_req *request = NULL;
1816 struct ufs_query_res *response = NULL;
1817 int err;
1818
1819 BUG_ON(!hba);
1820
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001821 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001822 if (!desc_buf) {
1823 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1824 __func__, opcode);
1825 err = -EINVAL;
1826 goto out;
1827 }
1828
1829 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1830 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1831 __func__, *buf_len);
1832 err = -EINVAL;
1833 goto out;
1834 }
1835
1836 mutex_lock(&hba->dev_cmd.lock);
1837 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1838 selector);
1839 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001840 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001841
1842 switch (opcode) {
1843 case UPIU_QUERY_OPCODE_WRITE_DESC:
1844 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1845 break;
1846 case UPIU_QUERY_OPCODE_READ_DESC:
1847 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1848 break;
1849 default:
1850 dev_err(hba->dev,
1851 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1852 __func__, opcode);
1853 err = -EINVAL;
1854 goto out_unlock;
1855 }
1856
1857 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1858
1859 if (err) {
1860 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1861 __func__, opcode, idn, err);
1862 goto out_unlock;
1863 }
1864
1865 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001866 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001867
1868out_unlock:
1869 mutex_unlock(&hba->dev_cmd.lock);
1870out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001871 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001872 return err;
1873}
1874
1875/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001876 * ufshcd_read_desc_param - read the specified descriptor parameter
1877 * @hba: Pointer to adapter instance
1878 * @desc_id: descriptor idn value
1879 * @desc_index: descriptor index
1880 * @param_offset: offset of the parameter to read
1881 * @param_read_buf: pointer to buffer where parameter would be read
1882 * @param_size: sizeof(param_read_buf)
1883 *
1884 * Return 0 in case of success, non-zero otherwise
1885 */
1886static int ufshcd_read_desc_param(struct ufs_hba *hba,
1887 enum desc_idn desc_id,
1888 int desc_index,
1889 u32 param_offset,
1890 u8 *param_read_buf,
1891 u32 param_size)
1892{
1893 int ret;
1894 u8 *desc_buf;
1895 u32 buff_len;
1896 bool is_kmalloc = true;
1897
1898 /* safety checks */
1899 if (desc_id >= QUERY_DESC_IDN_MAX)
1900 return -EINVAL;
1901
1902 buff_len = ufs_query_desc_max_size[desc_id];
1903 if ((param_offset + param_size) > buff_len)
1904 return -EINVAL;
1905
1906 if (!param_offset && (param_size == buff_len)) {
1907 /* memory space already available to hold full descriptor */
1908 desc_buf = param_read_buf;
1909 is_kmalloc = false;
1910 } else {
1911 /* allocate memory to hold full descriptor */
1912 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1913 if (!desc_buf)
1914 return -ENOMEM;
1915 }
1916
1917 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1918 desc_id, desc_index, 0, desc_buf,
1919 &buff_len);
1920
1921 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
1922 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
1923 ufs_query_desc_max_size[desc_id])
1924 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
1925 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
1926 __func__, desc_id, param_offset, buff_len, ret);
1927 if (!ret)
1928 ret = -EINVAL;
1929
1930 goto out;
1931 }
1932
1933 if (is_kmalloc)
1934 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1935out:
1936 if (is_kmalloc)
1937 kfree(desc_buf);
1938 return ret;
1939}
1940
1941static inline int ufshcd_read_desc(struct ufs_hba *hba,
1942 enum desc_idn desc_id,
1943 int desc_index,
1944 u8 *buf,
1945 u32 size)
1946{
1947 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1948}
1949
1950static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
1951 u8 *buf,
1952 u32 size)
1953{
1954 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
1955}
1956
1957/**
1958 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
1959 * @hba: Pointer to adapter instance
1960 * @lun: lun id
1961 * @param_offset: offset of the parameter to read
1962 * @param_read_buf: pointer to buffer where parameter would be read
1963 * @param_size: sizeof(param_read_buf)
1964 *
1965 * Return 0 in case of success, non-zero otherwise
1966 */
1967static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
1968 int lun,
1969 enum unit_desc_param param_offset,
1970 u8 *param_read_buf,
1971 u32 param_size)
1972{
1973 /*
1974 * Unit descriptors are only available for general purpose LUs (LUN id
1975 * from 0 to 7) and RPMB Well known LU.
1976 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001977 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001978 return -EOPNOTSUPP;
1979
1980 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
1981 param_offset, param_read_buf, param_size);
1982}
1983
1984/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301985 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1986 * @hba: per adapter instance
1987 *
1988 * 1. Allocate DMA memory for Command Descriptor array
1989 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1990 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1991 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1992 * (UTMRDL)
1993 * 4. Allocate memory for local reference block(lrb).
1994 *
1995 * Returns 0 for success, non-zero in case of failure
1996 */
1997static int ufshcd_memory_alloc(struct ufs_hba *hba)
1998{
1999 size_t utmrdl_size, utrdl_size, ucdl_size;
2000
2001 /* Allocate memory for UTP command descriptors */
2002 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002003 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
2004 ucdl_size,
2005 &hba->ucdl_dma_addr,
2006 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302007
2008 /*
2009 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
2010 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
2011 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
2012 * be aligned to 128 bytes as well
2013 */
2014 if (!hba->ucdl_base_addr ||
2015 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302016 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302017 "Command Descriptor Memory allocation failed\n");
2018 goto out;
2019 }
2020
2021 /*
2022 * Allocate memory for UTP Transfer descriptors
2023 * UFSHCI requires 1024 byte alignment of UTRD
2024 */
2025 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09002026 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
2027 utrdl_size,
2028 &hba->utrdl_dma_addr,
2029 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302030 if (!hba->utrdl_base_addr ||
2031 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302032 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302033 "Transfer Descriptor Memory allocation failed\n");
2034 goto out;
2035 }
2036
2037 /*
2038 * Allocate memory for UTP Task Management descriptors
2039 * UFSHCI requires 1024 byte alignment of UTMRD
2040 */
2041 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09002042 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
2043 utmrdl_size,
2044 &hba->utmrdl_dma_addr,
2045 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302046 if (!hba->utmrdl_base_addr ||
2047 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302048 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302049 "Task Management Descriptor Memory allocation failed\n");
2050 goto out;
2051 }
2052
2053 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09002054 hba->lrb = devm_kzalloc(hba->dev,
2055 hba->nutrs * sizeof(struct ufshcd_lrb),
2056 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302057 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302058 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302059 goto out;
2060 }
2061 return 0;
2062out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302063 return -ENOMEM;
2064}
2065
2066/**
2067 * ufshcd_host_memory_configure - configure local reference block with
2068 * memory offsets
2069 * @hba: per adapter instance
2070 *
2071 * Configure Host memory space
2072 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
2073 * address.
2074 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
2075 * and PRDT offset.
2076 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
2077 * into local reference block.
2078 */
2079static void ufshcd_host_memory_configure(struct ufs_hba *hba)
2080{
2081 struct utp_transfer_cmd_desc *cmd_descp;
2082 struct utp_transfer_req_desc *utrdlp;
2083 dma_addr_t cmd_desc_dma_addr;
2084 dma_addr_t cmd_desc_element_addr;
2085 u16 response_offset;
2086 u16 prdt_offset;
2087 int cmd_desc_size;
2088 int i;
2089
2090 utrdlp = hba->utrdl_base_addr;
2091 cmd_descp = hba->ucdl_base_addr;
2092
2093 response_offset =
2094 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2095 prdt_offset =
2096 offsetof(struct utp_transfer_cmd_desc, prd_table);
2097
2098 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2099 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2100
2101 for (i = 0; i < hba->nutrs; i++) {
2102 /* Configure UTRD with command descriptor base address */
2103 cmd_desc_element_addr =
2104 (cmd_desc_dma_addr + (cmd_desc_size * i));
2105 utrdlp[i].command_desc_base_addr_lo =
2106 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2107 utrdlp[i].command_desc_base_addr_hi =
2108 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2109
2110 /* Response upiu and prdt offset should be in double words */
2111 utrdlp[i].response_upiu_offset =
2112 cpu_to_le16((response_offset >> 2));
2113 utrdlp[i].prd_table_offset =
2114 cpu_to_le16((prdt_offset >> 2));
2115 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302116 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302117
2118 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302119 hba->lrb[i].ucd_req_ptr =
2120 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302121 hba->lrb[i].ucd_rsp_ptr =
2122 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2123 hba->lrb[i].ucd_prdt_ptr =
2124 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2125 }
2126}
2127
2128/**
2129 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2130 * @hba: per adapter instance
2131 *
2132 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2133 * in order to initialize the Unipro link startup procedure.
2134 * Once the Unipro links are up, the device connected to the controller
2135 * is detected.
2136 *
2137 * Returns 0 on success, non-zero value on failure
2138 */
2139static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2140{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302141 struct uic_command uic_cmd = {0};
2142 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302143
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302144 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2145
2146 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2147 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302148 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302149 "dme-link-startup: error code %d\n", ret);
2150 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302151}
2152
Yaniv Gardicad2e032015-03-31 17:37:14 +03002153static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba)
2154{
2155 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
2156 unsigned long min_sleep_time_us;
2157
2158 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS))
2159 return;
2160
2161 /*
2162 * last_dme_cmd_tstamp will be 0 only for 1st call to
2163 * this function
2164 */
2165 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) {
2166 min_sleep_time_us = MIN_DELAY_BEFORE_DME_CMDS_US;
2167 } else {
2168 unsigned long delta =
2169 (unsigned long) ktime_to_us(
2170 ktime_sub(ktime_get(),
2171 hba->last_dme_cmd_tstamp));
2172
2173 if (delta < MIN_DELAY_BEFORE_DME_CMDS_US)
2174 min_sleep_time_us =
2175 MIN_DELAY_BEFORE_DME_CMDS_US - delta;
2176 else
2177 return; /* no more delay required */
2178 }
2179
2180 /* allow sleep for extra 50us if needed */
2181 usleep_range(min_sleep_time_us, min_sleep_time_us + 50);
2182}
2183
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302184/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302185 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2186 * @hba: per adapter instance
2187 * @attr_sel: uic command argument1
2188 * @attr_set: attribute set type as uic command argument2
2189 * @mib_val: setting value as uic command argument3
2190 * @peer: indicate whether peer or local
2191 *
2192 * Returns 0 on success, non-zero value on failure
2193 */
2194int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2195 u8 attr_set, u32 mib_val, u8 peer)
2196{
2197 struct uic_command uic_cmd = {0};
2198 static const char *const action[] = {
2199 "dme-set",
2200 "dme-peer-set"
2201 };
2202 const char *set = action[!!peer];
2203 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002204 int retries = UFS_UIC_COMMAND_RETRIES;
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302205
2206 uic_cmd.command = peer ?
2207 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2208 uic_cmd.argument1 = attr_sel;
2209 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2210 uic_cmd.argument3 = mib_val;
2211
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002212 do {
2213 /* for peer attributes we retry upon failure */
2214 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2215 if (ret)
2216 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2217 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2218 } while (ret && peer && --retries);
2219
2220 if (!retries)
2221 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
2222 set, UIC_GET_ATTR_ID(attr_sel), mib_val,
2223 retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302224
2225 return ret;
2226}
2227EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2228
2229/**
2230 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2231 * @hba: per adapter instance
2232 * @attr_sel: uic command argument1
2233 * @mib_val: the value of the attribute as returned by the UIC command
2234 * @peer: indicate whether peer or local
2235 *
2236 * Returns 0 on success, non-zero value on failure
2237 */
2238int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2239 u32 *mib_val, u8 peer)
2240{
2241 struct uic_command uic_cmd = {0};
2242 static const char *const action[] = {
2243 "dme-get",
2244 "dme-peer-get"
2245 };
2246 const char *get = action[!!peer];
2247 int ret;
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002248 int retries = UFS_UIC_COMMAND_RETRIES;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002249 struct ufs_pa_layer_attr orig_pwr_info;
2250 struct ufs_pa_layer_attr temp_pwr_info;
2251 bool pwr_mode_change = false;
2252
2253 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) {
2254 orig_pwr_info = hba->pwr_info;
2255 temp_pwr_info = orig_pwr_info;
2256
2257 if (orig_pwr_info.pwr_tx == FAST_MODE ||
2258 orig_pwr_info.pwr_rx == FAST_MODE) {
2259 temp_pwr_info.pwr_tx = FASTAUTO_MODE;
2260 temp_pwr_info.pwr_rx = FASTAUTO_MODE;
2261 pwr_mode_change = true;
2262 } else if (orig_pwr_info.pwr_tx == SLOW_MODE ||
2263 orig_pwr_info.pwr_rx == SLOW_MODE) {
2264 temp_pwr_info.pwr_tx = SLOWAUTO_MODE;
2265 temp_pwr_info.pwr_rx = SLOWAUTO_MODE;
2266 pwr_mode_change = true;
2267 }
2268 if (pwr_mode_change) {
2269 ret = ufshcd_change_power_mode(hba, &temp_pwr_info);
2270 if (ret)
2271 goto out;
2272 }
2273 }
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302274
2275 uic_cmd.command = peer ?
2276 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2277 uic_cmd.argument1 = attr_sel;
2278
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002279 do {
2280 /* for peer attributes we retry upon failure */
2281 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2282 if (ret)
2283 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n",
2284 get, UIC_GET_ATTR_ID(attr_sel), ret);
2285 } while (ret && peer && --retries);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302286
Yaniv Gardi64238fb2016-02-01 15:02:43 +02002287 if (!retries)
2288 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n",
2289 get, UIC_GET_ATTR_ID(attr_sel), retries);
2290
2291 if (mib_val && !ret)
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302292 *mib_val = uic_cmd.argument3;
Yaniv Gardi874237f2015-05-17 18:55:03 +03002293
2294 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)
2295 && pwr_mode_change)
2296 ufshcd_change_power_mode(hba, &orig_pwr_info);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302297out:
2298 return ret;
2299}
2300EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2301
2302/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002303 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2304 * state) and waits for it to take effect.
2305 *
2306 * @hba: per adapter instance
2307 * @cmd: UIC command to execute
2308 *
2309 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2310 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2311 * and device UniPro link and hence it's final completion would be indicated by
2312 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2313 * addition to normal UIC command completion Status (UCCS). This function only
2314 * returns after the relevant status bits indicate the completion.
2315 *
2316 * Returns 0 on success, non-zero value on failure
2317 */
2318static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2319{
2320 struct completion uic_async_done;
2321 unsigned long flags;
2322 u8 status;
2323 int ret;
2324
2325 mutex_lock(&hba->uic_cmd_mutex);
2326 init_completion(&uic_async_done);
Yaniv Gardicad2e032015-03-31 17:37:14 +03002327 ufshcd_add_delay_before_dme_cmd(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002328
2329 spin_lock_irqsave(hba->host->host_lock, flags);
2330 hba->uic_async_done = &uic_async_done;
2331 ret = __ufshcd_send_uic_cmd(hba, cmd);
2332 spin_unlock_irqrestore(hba->host->host_lock, flags);
2333 if (ret) {
2334 dev_err(hba->dev,
2335 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2336 cmd->command, cmd->argument3, ret);
2337 goto out;
2338 }
2339 ret = ufshcd_wait_for_uic_cmd(hba, cmd);
2340 if (ret) {
2341 dev_err(hba->dev,
2342 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2343 cmd->command, cmd->argument3, ret);
2344 goto out;
2345 }
2346
2347 if (!wait_for_completion_timeout(hba->uic_async_done,
2348 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2349 dev_err(hba->dev,
2350 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2351 cmd->command, cmd->argument3);
2352 ret = -ETIMEDOUT;
2353 goto out;
2354 }
2355
2356 status = ufshcd_get_upmcrs(hba);
2357 if (status != PWR_LOCAL) {
2358 dev_err(hba->dev,
2359 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
2360 cmd->command, status);
2361 ret = (status != PWR_OK) ? status : -1;
2362 }
2363out:
2364 spin_lock_irqsave(hba->host->host_lock, flags);
2365 hba->uic_async_done = NULL;
2366 spin_unlock_irqrestore(hba->host->host_lock, flags);
2367 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002368
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002369 return ret;
2370}
2371
2372/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302373 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2374 * using DME_SET primitives.
2375 * @hba: per adapter instance
2376 * @mode: powr mode value
2377 *
2378 * Returns 0 on success, non-zero value on failure
2379 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302380static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302381{
2382 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002383 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302384
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002385 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) {
2386 ret = ufshcd_dme_set(hba,
2387 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP, 0), 1);
2388 if (ret) {
2389 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
2390 __func__, ret);
2391 goto out;
2392 }
2393 }
2394
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302395 uic_cmd.command = UIC_CMD_DME_SET;
2396 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2397 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002398 ufshcd_hold(hba, false);
2399 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2400 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302401
Yaniv Gardic3a2f9e2015-05-17 18:55:01 +03002402out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002403 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002404}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302405
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002406static int ufshcd_link_recovery(struct ufs_hba *hba)
2407{
2408 int ret;
2409 unsigned long flags;
2410
2411 spin_lock_irqsave(hba->host->host_lock, flags);
2412 hba->ufshcd_state = UFSHCD_STATE_RESET;
2413 ufshcd_set_eh_in_progress(hba);
2414 spin_unlock_irqrestore(hba->host->host_lock, flags);
2415
2416 ret = ufshcd_host_reset_and_restore(hba);
2417
2418 spin_lock_irqsave(hba->host->host_lock, flags);
2419 if (ret)
2420 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2421 ufshcd_clear_eh_in_progress(hba);
2422 spin_unlock_irqrestore(hba->host->host_lock, flags);
2423
2424 if (ret)
2425 dev_err(hba->dev, "%s: link recovery failed, err %d",
2426 __func__, ret);
2427
2428 return ret;
2429}
2430
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002431static int __ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002432{
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002433 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002434 struct uic_command uic_cmd = {0};
2435
2436 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002437 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002438
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002439 if (ret) {
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002440 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n",
2441 __func__, ret);
2442
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002443 /*
2444 * If link recovery fails then return error so that caller
2445 * don't retry the hibern8 enter again.
2446 */
2447 if (ufshcd_link_recovery(hba))
2448 ret = -ENOLINK;
2449 }
2450
Yaniv Gardi87d0b4a2016-02-01 15:02:44 +02002451 return ret;
2452}
2453
2454static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2455{
2456 int ret = 0, retries;
2457
2458 for (retries = UIC_HIBERN8_ENTER_RETRIES; retries > 0; retries--) {
2459 ret = __ufshcd_uic_hibern8_enter(hba);
2460 if (!ret || ret == -ENOLINK)
2461 goto out;
2462 }
2463out:
2464 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002465}
2466
2467static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2468{
2469 struct uic_command uic_cmd = {0};
2470 int ret;
2471
2472 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2473 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302474 if (ret) {
Yaniv Gardi53c12d02016-02-01 15:02:45 +02002475 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n",
2476 __func__, ret);
2477 ret = ufshcd_link_recovery(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302478 }
2479
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302480 return ret;
2481}
2482
Yaniv Gardi50646362014-10-23 13:25:13 +03002483 /**
2484 * ufshcd_init_pwr_info - setting the POR (power on reset)
2485 * values in hba power info
2486 * @hba: per-adapter instance
2487 */
2488static void ufshcd_init_pwr_info(struct ufs_hba *hba)
2489{
2490 hba->pwr_info.gear_rx = UFS_PWM_G1;
2491 hba->pwr_info.gear_tx = UFS_PWM_G1;
2492 hba->pwr_info.lane_rx = 1;
2493 hba->pwr_info.lane_tx = 1;
2494 hba->pwr_info.pwr_rx = SLOWAUTO_MODE;
2495 hba->pwr_info.pwr_tx = SLOWAUTO_MODE;
2496 hba->pwr_info.hs_rate = 0;
2497}
2498
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302499/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002500 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2501 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302502 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002503static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302504{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002505 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2506
2507 if (hba->max_pwr_info.is_valid)
2508 return 0;
2509
2510 pwr_info->pwr_tx = FASTAUTO_MODE;
2511 pwr_info->pwr_rx = FASTAUTO_MODE;
2512 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302513
2514 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002515 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2516 &pwr_info->lane_rx);
2517 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2518 &pwr_info->lane_tx);
2519
2520 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2521 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2522 __func__,
2523 pwr_info->lane_rx,
2524 pwr_info->lane_tx);
2525 return -EINVAL;
2526 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302527
2528 /*
2529 * First, get the maximum gears of HS speed.
2530 * If a zero value, it means there is no HSGEAR capability.
2531 * Then, get the maximum gears of PWM speed.
2532 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002533 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2534 if (!pwr_info->gear_rx) {
2535 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2536 &pwr_info->gear_rx);
2537 if (!pwr_info->gear_rx) {
2538 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2539 __func__, pwr_info->gear_rx);
2540 return -EINVAL;
2541 }
2542 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302543 }
2544
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002545 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2546 &pwr_info->gear_tx);
2547 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302548 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002549 &pwr_info->gear_tx);
2550 if (!pwr_info->gear_tx) {
2551 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2552 __func__, pwr_info->gear_tx);
2553 return -EINVAL;
2554 }
2555 pwr_info->pwr_tx = SLOWAUTO_MODE;
2556 }
2557
2558 hba->max_pwr_info.is_valid = true;
2559 return 0;
2560}
2561
2562static int ufshcd_change_power_mode(struct ufs_hba *hba,
2563 struct ufs_pa_layer_attr *pwr_mode)
2564{
2565 int ret;
2566
2567 /* if already configured to the requested pwr_mode */
2568 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2569 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2570 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2571 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2572 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2573 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2574 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2575 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2576 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302577 }
2578
2579 /*
2580 * Configure attributes for power mode change with below.
2581 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2582 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2583 * - PA_HSSERIES
2584 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002585 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2586 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2587 pwr_mode->lane_rx);
2588 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2589 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302590 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002591 else
2592 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302593
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002594 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2595 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2596 pwr_mode->lane_tx);
2597 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2598 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302599 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002600 else
2601 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302602
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002603 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2604 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2605 pwr_mode->pwr_rx == FAST_MODE ||
2606 pwr_mode->pwr_tx == FAST_MODE)
2607 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2608 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302609
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002610 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2611 | pwr_mode->pwr_tx);
2612
2613 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302614 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002615 "%s: power mode change failed %d\n", __func__, ret);
2616 } else {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002617 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL,
2618 pwr_mode);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002619
2620 memcpy(&hba->pwr_info, pwr_mode,
2621 sizeof(struct ufs_pa_layer_attr));
2622 }
2623
2624 return ret;
2625}
2626
2627/**
2628 * ufshcd_config_pwr_mode - configure a new power mode
2629 * @hba: per-adapter instance
2630 * @desired_pwr_mode: desired power configuration
2631 */
2632static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2633 struct ufs_pa_layer_attr *desired_pwr_mode)
2634{
2635 struct ufs_pa_layer_attr final_params = { 0 };
2636 int ret;
2637
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002638 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE,
2639 desired_pwr_mode, &final_params);
2640
2641 if (ret)
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002642 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2643
2644 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302645
2646 return ret;
2647}
2648
2649/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302650 * ufshcd_complete_dev_init() - checks device readiness
2651 * hba: per-adapter instance
2652 *
2653 * Set fDeviceInit flag and poll until device toggles it.
2654 */
2655static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2656{
2657 int i, retries, err = 0;
2658 bool flag_res = 1;
2659
2660 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2661 /* Set the fDeviceInit flag */
2662 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2663 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
2664 if (!err || err == -ETIMEDOUT)
2665 break;
2666 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2667 }
2668 if (err) {
2669 dev_err(hba->dev,
2670 "%s setting fDeviceInit flag failed with error %d\n",
2671 __func__, err);
2672 goto out;
2673 }
2674
2675 /* poll for max. 100 iterations for fDeviceInit flag to clear */
2676 for (i = 0; i < 100 && !err && flag_res; i++) {
2677 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2678 err = ufshcd_query_flag(hba,
2679 UPIU_QUERY_OPCODE_READ_FLAG,
2680 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2681 if (!err || err == -ETIMEDOUT)
2682 break;
2683 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
2684 err);
2685 }
2686 }
2687 if (err)
2688 dev_err(hba->dev,
2689 "%s reading fDeviceInit flag failed with error %d\n",
2690 __func__, err);
2691 else if (flag_res)
2692 dev_err(hba->dev,
2693 "%s fDeviceInit was not cleared by the device\n",
2694 __func__);
2695
2696out:
2697 return err;
2698}
2699
2700/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302701 * ufshcd_make_hba_operational - Make UFS controller operational
2702 * @hba: per adapter instance
2703 *
2704 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002705 * 1. Enable required interrupts
2706 * 2. Configure interrupt aggregation
2707 * 3. Program UTRL and UTMRL base addres
2708 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302709 *
2710 * Returns 0 on success, non-zero value on failure
2711 */
2712static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2713{
2714 int err = 0;
2715 u32 reg;
2716
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302717 /* Enable required interrupts */
2718 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2719
2720 /* Configure interrupt aggregation */
Yaniv Gardib8521902015-05-17 18:54:57 +03002721 if (ufshcd_is_intr_aggr_allowed(hba))
2722 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
2723 else
2724 ufshcd_disable_intr_aggr(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302725
2726 /* Configure UTRL and UTMRL base address registers */
2727 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2728 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2729 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2730 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2731 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2732 REG_UTP_TASK_REQ_LIST_BASE_L);
2733 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2734 REG_UTP_TASK_REQ_LIST_BASE_H);
2735
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302736 /*
2737 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
2738 * DEI, HEI bits must be 0
2739 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002740 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302741 if (!(ufshcd_get_lists_status(reg))) {
2742 ufshcd_enable_run_stop_reg(hba);
2743 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302744 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302745 "Host controller not ready to process requests");
2746 err = -EIO;
2747 goto out;
2748 }
2749
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302750out:
2751 return err;
2752}
2753
2754/**
2755 * ufshcd_hba_enable - initialize the controller
2756 * @hba: per adapter instance
2757 *
2758 * The controller resets itself and controller firmware initialization
2759 * sequence kicks off. When controller is ready it will set
2760 * the Host Controller Enable bit to 1.
2761 *
2762 * Returns 0 on success, non-zero value on failure
2763 */
2764static int ufshcd_hba_enable(struct ufs_hba *hba)
2765{
2766 int retry;
2767
2768 /*
2769 * msleep of 1 and 5 used in this function might result in msleep(20),
2770 * but it was necessary to send the UFS FPGA to reset mode during
2771 * development and testing of this driver. msleep can be changed to
2772 * mdelay and retry count can be reduced based on the controller.
2773 */
2774 if (!ufshcd_is_hba_active(hba)) {
2775
2776 /* change controller state to "reset state" */
2777 ufshcd_hba_stop(hba);
2778
2779 /*
2780 * This delay is based on the testing done with UFS host
2781 * controller FPGA. The delay can be changed based on the
2782 * host controller used.
2783 */
2784 msleep(5);
2785 }
2786
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002787 /* UniPro link is disabled at this point */
2788 ufshcd_set_link_off(hba);
2789
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002790 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002791
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302792 /* start controller initialization sequence */
2793 ufshcd_hba_start(hba);
2794
2795 /*
2796 * To initialize a UFS host controller HCE bit must be set to 1.
2797 * During initialization the HCE bit value changes from 1->0->1.
2798 * When the host controller completes initialization sequence
2799 * it sets the value of HCE bit to 1. The same HCE bit is read back
2800 * to check if the controller has completed initialization sequence.
2801 * So without this delay the value HCE = 1, set in the previous
2802 * instruction might be read back.
2803 * This delay can be changed based on the controller.
2804 */
2805 msleep(1);
2806
2807 /* wait for the host controller to complete initialization */
2808 retry = 10;
2809 while (ufshcd_is_hba_active(hba)) {
2810 if (retry) {
2811 retry--;
2812 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302813 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302814 "Controller enable failed\n");
2815 return -EIO;
2816 }
2817 msleep(5);
2818 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002819
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002820 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002821 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002822
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002823 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002824
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302825 return 0;
2826}
2827
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002828static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer)
2829{
2830 int tx_lanes, i, err = 0;
2831
2832 if (!peer)
2833 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2834 &tx_lanes);
2835 else
2836 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2837 &tx_lanes);
2838 for (i = 0; i < tx_lanes; i++) {
2839 if (!peer)
2840 err = ufshcd_dme_set(hba,
2841 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2842 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2843 0);
2844 else
2845 err = ufshcd_dme_peer_set(hba,
2846 UIC_ARG_MIB_SEL(TX_LCC_ENABLE,
2847 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i)),
2848 0);
2849 if (err) {
2850 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
2851 __func__, peer, i, err);
2852 break;
2853 }
2854 }
2855
2856 return err;
2857}
2858
2859static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba)
2860{
2861 return ufshcd_disable_tx_lcc(hba, true);
2862}
2863
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302864/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302865 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302866 * @hba: per adapter instance
2867 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302868 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302869 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302870static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302871{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302872 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002873 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302874
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002875 do {
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002876 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302877
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002878 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002879
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002880 /* check if device is detected by inter-connect layer */
2881 if (!ret && !ufshcd_is_device_present(hba)) {
2882 dev_err(hba->dev, "%s: Device not present\n", __func__);
2883 ret = -ENXIO;
2884 goto out;
2885 }
2886
2887 /*
2888 * DME link lost indication is only received when link is up,
2889 * but we can't be sure if the link is up until link startup
2890 * succeeds. So reset the local Uni-Pro and try again.
2891 */
2892 if (ret && ufshcd_hba_enable(hba))
2893 goto out;
2894 } while (ret && retries--);
2895
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302896 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002897 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302898 goto out;
2899
Yaniv Gardi7ca38cf2015-05-17 18:54:59 +03002900 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) {
2901 ret = ufshcd_disable_device_tx_lcc(hba);
2902 if (ret)
2903 goto out;
2904 }
2905
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002906 /* Include any host controller configuration via UIC commands */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02002907 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE);
2908 if (ret)
2909 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002910
2911 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302912out:
2913 if (ret)
2914 dev_err(hba->dev, "link startup failed %d\n", ret);
2915 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302916}
2917
2918/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302919 * ufshcd_verify_dev_init() - Verify device initialization
2920 * @hba: per-adapter instance
2921 *
2922 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
2923 * device Transport Protocol (UTP) layer is ready after a reset.
2924 * If the UTP layer at the device side is not initialized, it may
2925 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
2926 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
2927 */
2928static int ufshcd_verify_dev_init(struct ufs_hba *hba)
2929{
2930 int err = 0;
2931 int retries;
2932
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002933 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302934 mutex_lock(&hba->dev_cmd.lock);
2935 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
2936 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
2937 NOP_OUT_TIMEOUT);
2938
2939 if (!err || err == -ETIMEDOUT)
2940 break;
2941
2942 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2943 }
2944 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002945 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302946
2947 if (err)
2948 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2949 return err;
2950}
2951
2952/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002953 * ufshcd_set_queue_depth - set lun queue depth
2954 * @sdev: pointer to SCSI device
2955 *
2956 * Read bLUQueueDepth value and activate scsi tagged command
2957 * queueing. For WLUN, queue depth is set to 1. For best-effort
2958 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
2959 * value that host can queue.
2960 */
2961static void ufshcd_set_queue_depth(struct scsi_device *sdev)
2962{
2963 int ret = 0;
2964 u8 lun_qdepth;
2965 struct ufs_hba *hba;
2966
2967 hba = shost_priv(sdev->host);
2968
2969 lun_qdepth = hba->nutrs;
2970 ret = ufshcd_read_unit_desc_param(hba,
2971 ufshcd_scsi_to_upiu_lun(sdev->lun),
2972 UNIT_DESC_PARAM_LU_Q_DEPTH,
2973 &lun_qdepth,
2974 sizeof(lun_qdepth));
2975
2976 /* Some WLUN doesn't support unit descriptor */
2977 if (ret == -EOPNOTSUPP)
2978 lun_qdepth = 1;
2979 else if (!lun_qdepth)
2980 /* eventually, we can figure out the real queue depth */
2981 lun_qdepth = hba->nutrs;
2982 else
2983 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2984
2985 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2986 __func__, lun_qdepth);
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002987 scsi_change_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002988}
2989
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002990/*
2991 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
2992 * @hba: per-adapter instance
2993 * @lun: UFS device lun id
2994 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
2995 *
2996 * Returns 0 in case of success and b_lu_write_protect status would be returned
2997 * @b_lu_write_protect parameter.
2998 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
2999 * Returns -EINVAL in case of invalid parameters passed to this function.
3000 */
3001static int ufshcd_get_lu_wp(struct ufs_hba *hba,
3002 u8 lun,
3003 u8 *b_lu_write_protect)
3004{
3005 int ret;
3006
3007 if (!b_lu_write_protect)
3008 ret = -EINVAL;
3009 /*
3010 * According to UFS device spec, RPMB LU can't be write
3011 * protected so skip reading bLUWriteProtect parameter for
3012 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
3013 */
3014 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
3015 ret = -ENOTSUPP;
3016 else
3017 ret = ufshcd_read_unit_desc_param(hba,
3018 lun,
3019 UNIT_DESC_PARAM_LU_WR_PROTECT,
3020 b_lu_write_protect,
3021 sizeof(*b_lu_write_protect));
3022 return ret;
3023}
3024
3025/**
3026 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
3027 * status
3028 * @hba: per-adapter instance
3029 * @sdev: pointer to SCSI device
3030 *
3031 */
3032static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
3033 struct scsi_device *sdev)
3034{
3035 if (hba->dev_info.f_power_on_wp_en &&
3036 !hba->dev_info.is_lu_power_on_wp) {
3037 u8 b_lu_write_protect;
3038
3039 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
3040 &b_lu_write_protect) &&
3041 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
3042 hba->dev_info.is_lu_power_on_wp = true;
3043 }
3044}
3045
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003046/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303047 * ufshcd_slave_alloc - handle initial SCSI device configurations
3048 * @sdev: pointer to SCSI device
3049 *
3050 * Returns success
3051 */
3052static int ufshcd_slave_alloc(struct scsi_device *sdev)
3053{
3054 struct ufs_hba *hba;
3055
3056 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303057
3058 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
3059 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303060
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303061 /* allow SCSI layer to restart the device in case of errors */
3062 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003063
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03003064 /* REPORT SUPPORTED OPERATION CODES is not supported */
3065 sdev->no_report_opcodes = 1;
3066
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003067
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003068 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003069
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003070 ufshcd_get_lu_power_on_wp_status(hba, sdev);
3071
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003072 return 0;
3073}
3074
3075/**
3076 * ufshcd_change_queue_depth - change queue depth
3077 * @sdev: pointer to SCSI device
3078 * @depth: required depth to set
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003079 *
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003080 * Change queue depth and make sure the max. limits are not crossed.
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003081 */
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003082static int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003083{
3084 struct ufs_hba *hba = shost_priv(sdev->host);
3085
3086 if (depth > hba->nutrs)
3087 depth = hba->nutrs;
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01003088 return scsi_change_queue_depth(sdev, depth);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303089}
3090
3091/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003092 * ufshcd_slave_configure - adjust SCSI device configurations
3093 * @sdev: pointer to SCSI device
3094 */
3095static int ufshcd_slave_configure(struct scsi_device *sdev)
3096{
3097 struct request_queue *q = sdev->request_queue;
3098
3099 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
3100 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
3101
3102 return 0;
3103}
3104
3105/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303106 * ufshcd_slave_destroy - remove SCSI device configurations
3107 * @sdev: pointer to SCSI device
3108 */
3109static void ufshcd_slave_destroy(struct scsi_device *sdev)
3110{
3111 struct ufs_hba *hba;
3112
3113 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003114 /* Drop the reference as it won't be needed anymore */
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003115 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) {
3116 unsigned long flags;
3117
3118 spin_lock_irqsave(hba->host->host_lock, flags);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003119 hba->sdev_ufs_device = NULL;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03003120 spin_unlock_irqrestore(hba->host->host_lock, flags);
3121 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303122}
3123
3124/**
3125 * ufshcd_task_req_compl - handle task management request completion
3126 * @hba: per adapter instance
3127 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303128 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303129 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303130 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303131 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303132static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303133{
3134 struct utp_task_req_desc *task_req_descp;
3135 struct utp_upiu_task_rsp *task_rsp_upiup;
3136 unsigned long flags;
3137 int ocs_value;
3138 int task_result;
3139
3140 spin_lock_irqsave(hba->host->host_lock, flags);
3141
3142 /* Clear completed tasks from outstanding_tasks */
3143 __clear_bit(index, &hba->outstanding_tasks);
3144
3145 task_req_descp = hba->utmrdl_base_addr;
3146 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
3147
3148 if (ocs_value == OCS_SUCCESS) {
3149 task_rsp_upiup = (struct utp_upiu_task_rsp *)
3150 task_req_descp[index].task_rsp_upiu;
3151 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
3152 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303153 if (resp)
3154 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303155 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303156 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
3157 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303158 }
3159 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303160
3161 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303162}
3163
3164/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303165 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
3166 * @lrb: pointer to local reference block of completed command
3167 * @scsi_status: SCSI command status
3168 *
3169 * Returns value base on SCSI command status
3170 */
3171static inline int
3172ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
3173{
3174 int result = 0;
3175
3176 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303177 case SAM_STAT_CHECK_CONDITION:
3178 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303179 case SAM_STAT_GOOD:
3180 result |= DID_OK << 16 |
3181 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303182 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303183 break;
3184 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303185 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303186 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05303187 ufshcd_copy_sense_data(lrbp);
3188 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303189 break;
3190 default:
3191 result |= DID_ERROR << 16;
3192 break;
3193 } /* end of switch */
3194
3195 return result;
3196}
3197
3198/**
3199 * ufshcd_transfer_rsp_status - Get overall status of the response
3200 * @hba: per adapter instance
3201 * @lrb: pointer to local reference block of completed command
3202 *
3203 * Returns result of the command to notify SCSI midlayer
3204 */
3205static inline int
3206ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
3207{
3208 int result = 0;
3209 int scsi_status;
3210 int ocs;
3211
3212 /* overall command status of utrd */
3213 ocs = ufshcd_get_tr_ocs(lrbp);
3214
3215 switch (ocs) {
3216 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303217 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303218
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303219 switch (result) {
3220 case UPIU_TRANSACTION_RESPONSE:
3221 /*
3222 * get the response UPIU result to extract
3223 * the SCSI command status
3224 */
3225 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
3226
3227 /*
3228 * get the result based on SCSI status response
3229 * to notify the SCSI midlayer of the command status
3230 */
3231 scsi_status = result & MASK_SCSI_STATUS;
3232 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303233
Yaniv Gardif05ac2e2016-02-01 15:02:42 +02003234 /*
3235 * Currently we are only supporting BKOPs exception
3236 * events hence we can ignore BKOPs exception event
3237 * during power management callbacks. BKOPs exception
3238 * event is not expected to be raised in runtime suspend
3239 * callback as it allows the urgent bkops.
3240 * During system suspend, we are anyway forcefully
3241 * disabling the bkops and if urgent bkops is needed
3242 * it will be enabled on system resume. Long term
3243 * solution could be to abort the system suspend if
3244 * UFS device needs urgent BKOPs.
3245 */
3246 if (!hba->pm_op_in_progress &&
3247 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303248 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303249 break;
3250 case UPIU_TRANSACTION_REJECT_UPIU:
3251 /* TODO: handle Reject UPIU Response */
3252 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303253 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303254 "Reject UPIU not fully implemented\n");
3255 break;
3256 default:
3257 result = DID_ERROR << 16;
3258 dev_err(hba->dev,
3259 "Unexpected request response code = %x\n",
3260 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303261 break;
3262 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303263 break;
3264 case OCS_ABORTED:
3265 result |= DID_ABORT << 16;
3266 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303267 case OCS_INVALID_COMMAND_STATUS:
3268 result |= DID_REQUEUE << 16;
3269 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303270 case OCS_INVALID_CMD_TABLE_ATTR:
3271 case OCS_INVALID_PRDT_ATTR:
3272 case OCS_MISMATCH_DATA_BUF_SIZE:
3273 case OCS_MISMATCH_RESP_UPIU_SIZE:
3274 case OCS_PEER_COMM_FAILURE:
3275 case OCS_FATAL_ERROR:
3276 default:
3277 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303278 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303279 "OCS error from controller = %x\n", ocs);
3280 break;
3281 } /* end of switch */
3282
3283 return result;
3284}
3285
3286/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303287 * ufshcd_uic_cmd_compl - handle completion of uic command
3288 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303289 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303290 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303291static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303292{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303293 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303294 hba->active_uic_cmd->argument2 |=
3295 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05303296 hba->active_uic_cmd->argument3 =
3297 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303298 complete(&hba->active_uic_cmd->done);
3299 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303300
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003301 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3302 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303303}
3304
3305/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303306 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3307 * @hba: per adapter instance
3308 */
3309static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3310{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303311 struct ufshcd_lrb *lrbp;
3312 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303313 unsigned long completed_reqs;
3314 u32 tr_doorbell;
3315 int result;
3316 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003317
3318 /* Resetting interrupt aggregation counters first and reading the
3319 * DOOR_BELL afterward allows us to handle all the completed requests.
3320 * In order to prevent other interrupts starvation the DB is read once
3321 * after reset. The down side of this solution is the possibility of
3322 * false interrupt if device completes another request after resetting
3323 * aggregation and before reading the DB.
3324 */
Yaniv Gardib8521902015-05-17 18:54:57 +03003325 if (ufshcd_is_intr_aggr_allowed(hba))
3326 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303327
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303328 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303329 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3330
Dolev Ravive9d501b2014-07-01 12:22:37 +03003331 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3332 lrbp = &hba->lrb[index];
3333 cmd = lrbp->cmd;
3334 if (cmd) {
3335 result = ufshcd_transfer_rsp_status(hba, lrbp);
3336 scsi_dma_unmap(cmd);
3337 cmd->result = result;
3338 /* Mark completed command as NULL in LRB */
3339 lrbp->cmd = NULL;
3340 clear_bit_unlock(index, &hba->lrb_in_use);
3341 /* Do not touch lrbp after scsi done */
3342 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003343 __ufshcd_release(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003344 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
3345 if (hba->dev_cmd.complete)
3346 complete(hba->dev_cmd.complete);
3347 }
3348 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303349
3350 /* clear corresponding bits of completed commands */
3351 hba->outstanding_reqs ^= completed_reqs;
3352
Sahitya Tummala856b3482014-09-25 15:32:34 +03003353 ufshcd_clk_scaling_update_busy(hba);
3354
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303355 /* we might have free'd some tags above */
3356 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303357}
3358
3359/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303360 * ufshcd_disable_ee - disable exception event
3361 * @hba: per-adapter instance
3362 * @mask: exception event to disable
3363 *
3364 * Disables exception event in the device so that the EVENT_ALERT
3365 * bit is not set.
3366 *
3367 * Returns zero on success, non-zero error value on failure.
3368 */
3369static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3370{
3371 int err = 0;
3372 u32 val;
3373
3374 if (!(hba->ee_ctrl_mask & mask))
3375 goto out;
3376
3377 val = hba->ee_ctrl_mask & ~mask;
3378 val &= 0xFFFF; /* 2 bytes */
3379 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3380 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3381 if (!err)
3382 hba->ee_ctrl_mask &= ~mask;
3383out:
3384 return err;
3385}
3386
3387/**
3388 * ufshcd_enable_ee - enable exception event
3389 * @hba: per-adapter instance
3390 * @mask: exception event to enable
3391 *
3392 * Enable corresponding exception event in the device to allow
3393 * device to alert host in critical scenarios.
3394 *
3395 * Returns zero on success, non-zero error value on failure.
3396 */
3397static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3398{
3399 int err = 0;
3400 u32 val;
3401
3402 if (hba->ee_ctrl_mask & mask)
3403 goto out;
3404
3405 val = hba->ee_ctrl_mask | mask;
3406 val &= 0xFFFF; /* 2 bytes */
3407 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3408 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3409 if (!err)
3410 hba->ee_ctrl_mask |= mask;
3411out:
3412 return err;
3413}
3414
3415/**
3416 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3417 * @hba: per-adapter instance
3418 *
3419 * Allow device to manage background operations on its own. Enabling
3420 * this might lead to inconsistent latencies during normal data transfers
3421 * as the device is allowed to manage its own way of handling background
3422 * operations.
3423 *
3424 * Returns zero on success, non-zero on failure.
3425 */
3426static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3427{
3428 int err = 0;
3429
3430 if (hba->auto_bkops_enabled)
3431 goto out;
3432
3433 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3434 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3435 if (err) {
3436 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3437 __func__, err);
3438 goto out;
3439 }
3440
3441 hba->auto_bkops_enabled = true;
3442
3443 /* No need of URGENT_BKOPS exception from the device */
3444 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3445 if (err)
3446 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3447 __func__, err);
3448out:
3449 return err;
3450}
3451
3452/**
3453 * ufshcd_disable_auto_bkops - block device in doing background operations
3454 * @hba: per-adapter instance
3455 *
3456 * Disabling background operations improves command response latency but
3457 * has drawback of device moving into critical state where the device is
3458 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3459 * host is idle so that BKOPS are managed effectively without any negative
3460 * impacts.
3461 *
3462 * Returns zero on success, non-zero on failure.
3463 */
3464static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3465{
3466 int err = 0;
3467
3468 if (!hba->auto_bkops_enabled)
3469 goto out;
3470
3471 /*
3472 * If host assisted BKOPs is to be enabled, make sure
3473 * urgent bkops exception is allowed.
3474 */
3475 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3476 if (err) {
3477 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3478 __func__, err);
3479 goto out;
3480 }
3481
3482 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
3483 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3484 if (err) {
3485 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3486 __func__, err);
3487 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3488 goto out;
3489 }
3490
3491 hba->auto_bkops_enabled = false;
3492out:
3493 return err;
3494}
3495
3496/**
3497 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3498 * @hba: per adapter instance
3499 *
3500 * After a device reset the device may toggle the BKOPS_EN flag
3501 * to default value. The s/w tracking variables should be updated
3502 * as well. Do this by forcing enable of auto bkops.
3503 */
3504static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3505{
3506 hba->auto_bkops_enabled = false;
3507 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3508 ufshcd_enable_auto_bkops(hba);
3509}
3510
3511static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3512{
3513 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3514 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3515}
3516
3517/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003518 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3519 * @hba: per-adapter instance
3520 * @status: bkops_status value
3521 *
3522 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3523 * flag in the device to permit background operations if the device
3524 * bkops_status is greater than or equal to "status" argument passed to
3525 * this function, disable otherwise.
3526 *
3527 * Returns 0 for success, non-zero in case of failure.
3528 *
3529 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3530 * to know whether auto bkops is enabled or disabled after this function
3531 * returns control to it.
3532 */
3533static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3534 enum bkops_status status)
3535{
3536 int err;
3537 u32 curr_status = 0;
3538
3539 err = ufshcd_get_bkops_status(hba, &curr_status);
3540 if (err) {
3541 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3542 __func__, err);
3543 goto out;
3544 } else if (curr_status > BKOPS_STATUS_MAX) {
3545 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3546 __func__, curr_status);
3547 err = -EINVAL;
3548 goto out;
3549 }
3550
3551 if (curr_status >= status)
3552 err = ufshcd_enable_auto_bkops(hba);
3553 else
3554 err = ufshcd_disable_auto_bkops(hba);
3555out:
3556 return err;
3557}
3558
3559/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303560 * ufshcd_urgent_bkops - handle urgent bkops exception event
3561 * @hba: per-adapter instance
3562 *
3563 * Enable fBackgroundOpsEn flag in the device to permit background
3564 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003565 *
3566 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3567 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303568 */
3569static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3570{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003571 return ufshcd_bkops_ctrl(hba, BKOPS_STATUS_PERF_IMPACT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303572}
3573
3574static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3575{
3576 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3577 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3578}
3579
3580/**
3581 * ufshcd_exception_event_handler - handle exceptions raised by device
3582 * @work: pointer to work data
3583 *
3584 * Read bExceptionEventStatus attribute from the device and handle the
3585 * exception event accordingly.
3586 */
3587static void ufshcd_exception_event_handler(struct work_struct *work)
3588{
3589 struct ufs_hba *hba;
3590 int err;
3591 u32 status = 0;
3592 hba = container_of(work, struct ufs_hba, eeh_work);
3593
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303594 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303595 err = ufshcd_get_ee_status(hba, &status);
3596 if (err) {
3597 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3598 __func__, err);
3599 goto out;
3600 }
3601
3602 status &= hba->ee_ctrl_mask;
3603 if (status & MASK_EE_URGENT_BKOPS) {
3604 err = ufshcd_urgent_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003605 if (err < 0)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303606 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3607 __func__, err);
3608 }
3609out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303610 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303611 return;
3612}
3613
3614/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303615 * ufshcd_err_handler - handle UFS errors that require s/w attention
3616 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303617 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303618static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303619{
3620 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303621 unsigned long flags;
3622 u32 err_xfer = 0;
3623 u32 err_tm = 0;
3624 int err = 0;
3625 int tag;
3626
3627 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303628
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303629 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003630 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303631
3632 spin_lock_irqsave(hba->host->host_lock, flags);
3633 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
3634 spin_unlock_irqrestore(hba->host->host_lock, flags);
3635 goto out;
3636 }
3637
3638 hba->ufshcd_state = UFSHCD_STATE_RESET;
3639 ufshcd_set_eh_in_progress(hba);
3640
3641 /* Complete requests that have door-bell cleared by h/w */
3642 ufshcd_transfer_req_compl(hba);
3643 ufshcd_tmc_handler(hba);
3644 spin_unlock_irqrestore(hba->host->host_lock, flags);
3645
3646 /* Clear pending transfer requests */
3647 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
3648 if (ufshcd_clear_cmd(hba, tag))
3649 err_xfer |= 1 << tag;
3650
3651 /* Clear pending task management requests */
3652 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
3653 if (ufshcd_clear_tm_cmd(hba, tag))
3654 err_tm |= 1 << tag;
3655
3656 /* Complete the requests that are cleared by s/w */
3657 spin_lock_irqsave(hba->host->host_lock, flags);
3658 ufshcd_transfer_req_compl(hba);
3659 ufshcd_tmc_handler(hba);
3660 spin_unlock_irqrestore(hba->host->host_lock, flags);
3661
3662 /* Fatal errors need reset */
3663 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
3664 ((hba->saved_err & UIC_ERROR) &&
3665 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
3666 err = ufshcd_reset_and_restore(hba);
3667 if (err) {
3668 dev_err(hba->dev, "%s: reset and restore failed\n",
3669 __func__);
3670 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3671 }
3672 /*
3673 * Inform scsi mid-layer that we did reset and allow to handle
3674 * Unit Attention properly.
3675 */
3676 scsi_report_bus_reset(hba->host, 0);
3677 hba->saved_err = 0;
3678 hba->saved_uic_err = 0;
3679 }
3680 ufshcd_clear_eh_in_progress(hba);
3681
3682out:
3683 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003684 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303685 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303686}
3687
3688/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303689 * ufshcd_update_uic_error - check and set fatal UIC error flags.
3690 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303691 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303692static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303693{
3694 u32 reg;
3695
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303696 /* PA_INIT_ERROR is fatal and needs UIC reset */
3697 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
3698 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
3699 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
3700
3701 /* UIC NL/TL/DME errors needs software retry */
3702 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
3703 if (reg)
3704 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
3705
3706 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
3707 if (reg)
3708 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
3709
3710 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
3711 if (reg)
3712 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
3713
3714 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
3715 __func__, hba->uic_error);
3716}
3717
3718/**
3719 * ufshcd_check_errors - Check for errors that need s/w attention
3720 * @hba: per-adapter instance
3721 */
3722static void ufshcd_check_errors(struct ufs_hba *hba)
3723{
3724 bool queue_eh_work = false;
3725
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303726 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303727 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303728
3729 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303730 hba->uic_error = 0;
3731 ufshcd_update_uic_error(hba);
3732 if (hba->uic_error)
3733 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303734 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303735
3736 if (queue_eh_work) {
3737 /* handle fatal errors only when link is functional */
3738 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
3739 /* block commands from scsi mid-layer */
3740 scsi_block_requests(hba->host);
3741
3742 /* transfer error masks to sticky bits */
3743 hba->saved_err |= hba->errors;
3744 hba->saved_uic_err |= hba->uic_error;
3745
3746 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3747 schedule_work(&hba->eh_work);
3748 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303749 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303750 /*
3751 * if (!queue_eh_work) -
3752 * Other errors are either non-fatal where host recovers
3753 * itself without s/w intervention or errors that will be
3754 * handled by the SCSI core layer.
3755 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303756}
3757
3758/**
3759 * ufshcd_tmc_handler - handle task management function completion
3760 * @hba: per adapter instance
3761 */
3762static void ufshcd_tmc_handler(struct ufs_hba *hba)
3763{
3764 u32 tm_doorbell;
3765
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303766 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303767 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303768 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303769}
3770
3771/**
3772 * ufshcd_sl_intr - Interrupt service routine
3773 * @hba: per adapter instance
3774 * @intr_status: contains interrupts generated by the controller
3775 */
3776static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
3777{
3778 hba->errors = UFSHCD_ERROR_MASK & intr_status;
3779 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303780 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303781
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303782 if (intr_status & UFSHCD_UIC_MASK)
3783 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303784
3785 if (intr_status & UTP_TASK_REQ_COMPL)
3786 ufshcd_tmc_handler(hba);
3787
3788 if (intr_status & UTP_TRANSFER_REQ_COMPL)
3789 ufshcd_transfer_req_compl(hba);
3790}
3791
3792/**
3793 * ufshcd_intr - Main interrupt service routine
3794 * @irq: irq number
3795 * @__hba: pointer to adapter instance
3796 *
3797 * Returns IRQ_HANDLED - If interrupt is valid
3798 * IRQ_NONE - If invalid interrupt
3799 */
3800static irqreturn_t ufshcd_intr(int irq, void *__hba)
3801{
3802 u32 intr_status;
3803 irqreturn_t retval = IRQ_NONE;
3804 struct ufs_hba *hba = __hba;
3805
3806 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303807 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303808
3809 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05303810 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303811 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303812 retval = IRQ_HANDLED;
3813 }
3814 spin_unlock(hba->host->host_lock);
3815 return retval;
3816}
3817
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303818static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
3819{
3820 int err = 0;
3821 u32 mask = 1 << tag;
3822 unsigned long flags;
3823
3824 if (!test_bit(tag, &hba->outstanding_tasks))
3825 goto out;
3826
3827 spin_lock_irqsave(hba->host->host_lock, flags);
3828 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
3829 spin_unlock_irqrestore(hba->host->host_lock, flags);
3830
3831 /* poll for max. 1 sec to clear door bell register by h/w */
3832 err = ufshcd_wait_for_register(hba,
3833 REG_UTP_TASK_REQ_DOOR_BELL,
3834 mask, 0, 1000, 1000);
3835out:
3836 return err;
3837}
3838
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303839/**
3840 * ufshcd_issue_tm_cmd - issues task management commands to controller
3841 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303842 * @lun_id: LUN ID to which TM command is sent
3843 * @task_id: task ID to which the TM command is applicable
3844 * @tm_function: task management function opcode
3845 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303846 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303847 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303848 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303849static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
3850 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303851{
3852 struct utp_task_req_desc *task_req_descp;
3853 struct utp_upiu_task_req *task_req_upiup;
3854 struct Scsi_Host *host;
3855 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303856 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303857 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303858 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303859
3860 host = hba->host;
3861
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303862 /*
3863 * Get free slot, sleep if slots are unavailable.
3864 * Even though we use wait_event() which sleeps indefinitely,
3865 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
3866 */
3867 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003868 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303869
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303870 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303871 task_req_descp = hba->utmrdl_base_addr;
3872 task_req_descp += free_slot;
3873
3874 /* Configure task request descriptor */
3875 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
3876 task_req_descp->header.dword_2 =
3877 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
3878
3879 /* Configure task request UPIU */
3880 task_req_upiup =
3881 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303882 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303883 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303884 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303885 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303886 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303887 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003888 /*
3889 * The host shall provide the same value for LUN field in the basic
3890 * header and for Input Parameter.
3891 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303892 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
3893 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303894
3895 /* send command to the controller */
3896 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303897 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303898
3899 spin_unlock_irqrestore(host->host_lock, flags);
3900
3901 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303902 err = wait_event_timeout(hba->tm_wq,
3903 test_bit(free_slot, &hba->tm_condition),
3904 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303905 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303906 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
3907 __func__, tm_function);
3908 if (ufshcd_clear_tm_cmd(hba, free_slot))
3909 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
3910 __func__, free_slot);
3911 err = -ETIMEDOUT;
3912 } else {
3913 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303914 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303915
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303916 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303917 ufshcd_put_tm_slot(hba, free_slot);
3918 wake_up(&hba->tm_tag_wq);
3919
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003920 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303921 return err;
3922}
3923
3924/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303925 * ufshcd_eh_device_reset_handler - device reset handler registered to
3926 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303927 * @cmd: SCSI command pointer
3928 *
3929 * Returns SUCCESS/FAILED
3930 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303931static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303932{
3933 struct Scsi_Host *host;
3934 struct ufs_hba *hba;
3935 unsigned int tag;
3936 u32 pos;
3937 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303938 u8 resp = 0xF;
3939 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303940 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303941
3942 host = cmd->device->host;
3943 hba = shost_priv(host);
3944 tag = cmd->request->tag;
3945
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303946 lrbp = &hba->lrb[tag];
3947 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
3948 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303949 if (!err)
3950 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303951 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303952 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303953
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303954 /* clear the commands that were pending for corresponding LUN */
3955 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
3956 if (hba->lrb[pos].lun == lrbp->lun) {
3957 err = ufshcd_clear_cmd(hba, pos);
3958 if (err)
3959 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303960 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303961 }
3962 spin_lock_irqsave(host->host_lock, flags);
3963 ufshcd_transfer_req_compl(hba);
3964 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303965out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303966 if (!err) {
3967 err = SUCCESS;
3968 } else {
3969 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3970 err = FAILED;
3971 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303972 return err;
3973}
3974
3975/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303976 * ufshcd_abort - abort a specific command
3977 * @cmd: SCSI command pointer
3978 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303979 * Abort the pending command in device by sending UFS_ABORT_TASK task management
3980 * command, and in host controller by clearing the door-bell register. There can
3981 * be race between controller sending the command to the device while abort is
3982 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
3983 * really issued and then try to abort it.
3984 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303985 * Returns SUCCESS/FAILED
3986 */
3987static int ufshcd_abort(struct scsi_cmnd *cmd)
3988{
3989 struct Scsi_Host *host;
3990 struct ufs_hba *hba;
3991 unsigned long flags;
3992 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303993 int err = 0;
3994 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303995 u8 resp = 0xF;
3996 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003997 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303998
3999 host = cmd->device->host;
4000 hba = shost_priv(host);
4001 tag = cmd->request->tag;
Yaniv Gardi14497322016-02-01 15:02:39 +02004002 if (!ufshcd_valid_tag(hba, tag)) {
4003 dev_err(hba->dev,
4004 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
4005 __func__, tag, cmd, cmd->request);
4006 BUG();
4007 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304008
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004009 ufshcd_hold(hba, false);
Dolev Ravive9d501b2014-07-01 12:22:37 +03004010 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Yaniv Gardi14497322016-02-01 15:02:39 +02004011 /* If command is already aborted/completed, return SUCCESS */
4012 if (!(test_bit(tag, &hba->outstanding_reqs))) {
4013 dev_err(hba->dev,
4014 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
4015 __func__, tag, hba->outstanding_reqs, reg);
4016 goto out;
4017 }
4018
Dolev Ravive9d501b2014-07-01 12:22:37 +03004019 if (!(reg & (1 << tag))) {
4020 dev_err(hba->dev,
4021 "%s: cmd was completed, but without a notifying intr, tag = %d",
4022 __func__, tag);
4023 }
4024
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304025 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304026 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
4027 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4028 UFS_QUERY_TASK, &resp);
4029 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
4030 /* cmd pending in the device */
4031 break;
4032 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304033 /*
4034 * cmd not pending in the device, check if it is
4035 * in transition.
4036 */
4037 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
4038 if (reg & (1 << tag)) {
4039 /* sleep for max. 200us to stabilize */
4040 usleep_range(100, 200);
4041 continue;
4042 }
4043 /* command completed already */
4044 goto out;
4045 } else {
4046 if (!err)
4047 err = resp; /* service response error */
4048 goto out;
4049 }
4050 }
4051
4052 if (!poll_cnt) {
4053 err = -EBUSY;
4054 goto out;
4055 }
4056
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304057 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
4058 UFS_ABORT_TASK, &resp);
4059 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304060 if (!err)
4061 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304062 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05304063 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304064
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304065 err = ufshcd_clear_cmd(hba, tag);
4066 if (err)
4067 goto out;
4068
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304069 scsi_dma_unmap(cmd);
4070
4071 spin_lock_irqsave(host->host_lock, flags);
Yaniv Gardia48353f2016-02-01 15:02:40 +02004072 ufshcd_outstanding_req_clear(hba, tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304073 hba->lrb[tag].cmd = NULL;
4074 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304075
4076 clear_bit_unlock(tag, &hba->lrb_in_use);
4077 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004078
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304079out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05304080 if (!err) {
4081 err = SUCCESS;
4082 } else {
4083 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
4084 err = FAILED;
4085 }
4086
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004087 /*
4088 * This ufshcd_release() corresponds to the original scsi cmd that got
4089 * aborted here (as we won't get any IRQ for it).
4090 */
4091 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304092 return err;
4093}
4094
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304095/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304096 * ufshcd_host_reset_and_restore - reset and restore host controller
4097 * @hba: per-adapter instance
4098 *
4099 * Note that host controller reset may issue DME_RESET to
4100 * local and remote (device) Uni-Pro stack and the attributes
4101 * are reset to default state.
4102 *
4103 * Returns zero on success, non-zero on failure
4104 */
4105static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
4106{
4107 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304108 unsigned long flags;
4109
4110 /* Reset the host controller */
4111 spin_lock_irqsave(hba->host->host_lock, flags);
4112 ufshcd_hba_stop(hba);
4113 spin_unlock_irqrestore(hba->host->host_lock, flags);
4114
4115 err = ufshcd_hba_enable(hba);
4116 if (err)
4117 goto out;
4118
4119 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004120 err = ufshcd_probe_hba(hba);
4121
4122 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304123 err = -EIO;
4124out:
4125 if (err)
4126 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
4127
4128 return err;
4129}
4130
4131/**
4132 * ufshcd_reset_and_restore - reset and re-initialize host/device
4133 * @hba: per-adapter instance
4134 *
4135 * Reset and recover device, host and re-establish link. This
4136 * is helpful to recover the communication in fatal error conditions.
4137 *
4138 * Returns zero on success, non-zero on failure
4139 */
4140static int ufshcd_reset_and_restore(struct ufs_hba *hba)
4141{
4142 int err = 0;
4143 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004144 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304145
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004146 do {
4147 err = ufshcd_host_reset_and_restore(hba);
4148 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304149
4150 /*
4151 * After reset the door-bell might be cleared, complete
4152 * outstanding requests in s/w here.
4153 */
4154 spin_lock_irqsave(hba->host->host_lock, flags);
4155 ufshcd_transfer_req_compl(hba);
4156 ufshcd_tmc_handler(hba);
4157 spin_unlock_irqrestore(hba->host->host_lock, flags);
4158
4159 return err;
4160}
4161
4162/**
4163 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
4164 * @cmd - SCSI command pointer
4165 *
4166 * Returns SUCCESS/FAILED
4167 */
4168static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
4169{
4170 int err;
4171 unsigned long flags;
4172 struct ufs_hba *hba;
4173
4174 hba = shost_priv(cmd->device->host);
4175
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004176 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304177 /*
4178 * Check if there is any race with fatal error handling.
4179 * If so, wait for it to complete. Even though fatal error
4180 * handling does reset and restore in some cases, don't assume
4181 * anything out of it. We are just avoiding race here.
4182 */
4183 do {
4184 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304185 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304186 hba->ufshcd_state == UFSHCD_STATE_RESET))
4187 break;
4188 spin_unlock_irqrestore(hba->host->host_lock, flags);
4189 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05304190 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304191 } while (1);
4192
4193 hba->ufshcd_state = UFSHCD_STATE_RESET;
4194 ufshcd_set_eh_in_progress(hba);
4195 spin_unlock_irqrestore(hba->host->host_lock, flags);
4196
4197 err = ufshcd_reset_and_restore(hba);
4198
4199 spin_lock_irqsave(hba->host->host_lock, flags);
4200 if (!err) {
4201 err = SUCCESS;
4202 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
4203 } else {
4204 err = FAILED;
4205 hba->ufshcd_state = UFSHCD_STATE_ERROR;
4206 }
4207 ufshcd_clear_eh_in_progress(hba);
4208 spin_unlock_irqrestore(hba->host->host_lock, flags);
4209
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004210 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304211 return err;
4212}
4213
4214/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004215 * ufshcd_get_max_icc_level - calculate the ICC level
4216 * @sup_curr_uA: max. current supported by the regulator
4217 * @start_scan: row at the desc table to start scan from
4218 * @buff: power descriptor buffer
4219 *
4220 * Returns calculated max ICC level for specific regulator
4221 */
4222static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
4223{
4224 int i;
4225 int curr_uA;
4226 u16 data;
4227 u16 unit;
4228
4229 for (i = start_scan; i >= 0; i--) {
4230 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
4231 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
4232 ATTR_ICC_LVL_UNIT_OFFSET;
4233 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
4234 switch (unit) {
4235 case UFSHCD_NANO_AMP:
4236 curr_uA = curr_uA / 1000;
4237 break;
4238 case UFSHCD_MILI_AMP:
4239 curr_uA = curr_uA * 1000;
4240 break;
4241 case UFSHCD_AMP:
4242 curr_uA = curr_uA * 1000 * 1000;
4243 break;
4244 case UFSHCD_MICRO_AMP:
4245 default:
4246 break;
4247 }
4248 if (sup_curr_uA >= curr_uA)
4249 break;
4250 }
4251 if (i < 0) {
4252 i = 0;
4253 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
4254 }
4255
4256 return (u32)i;
4257}
4258
4259/**
4260 * ufshcd_calc_icc_level - calculate the max ICC level
4261 * In case regulators are not initialized we'll return 0
4262 * @hba: per-adapter instance
4263 * @desc_buf: power descriptor buffer to extract ICC levels from.
4264 * @len: length of desc_buff
4265 *
4266 * Returns calculated ICC level
4267 */
4268static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
4269 u8 *desc_buf, int len)
4270{
4271 u32 icc_level = 0;
4272
4273 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
4274 !hba->vreg_info.vccq2) {
4275 dev_err(hba->dev,
4276 "%s: Regulator capability was not set, actvIccLevel=%d",
4277 __func__, icc_level);
4278 goto out;
4279 }
4280
4281 if (hba->vreg_info.vcc)
4282 icc_level = ufshcd_get_max_icc_level(
4283 hba->vreg_info.vcc->max_uA,
4284 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
4285 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
4286
4287 if (hba->vreg_info.vccq)
4288 icc_level = ufshcd_get_max_icc_level(
4289 hba->vreg_info.vccq->max_uA,
4290 icc_level,
4291 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
4292
4293 if (hba->vreg_info.vccq2)
4294 icc_level = ufshcd_get_max_icc_level(
4295 hba->vreg_info.vccq2->max_uA,
4296 icc_level,
4297 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
4298out:
4299 return icc_level;
4300}
4301
4302static void ufshcd_init_icc_levels(struct ufs_hba *hba)
4303{
4304 int ret;
4305 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
4306 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
4307
4308 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
4309 if (ret) {
4310 dev_err(hba->dev,
4311 "%s: Failed reading power descriptor.len = %d ret = %d",
4312 __func__, buff_len, ret);
4313 return;
4314 }
4315
4316 hba->init_prefetch_data.icc_level =
4317 ufshcd_find_max_sup_active_icc_level(hba,
4318 desc_buf, buff_len);
4319 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4320 __func__, hba->init_prefetch_data.icc_level);
4321
4322 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4323 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4324 &hba->init_prefetch_data.icc_level);
4325
4326 if (ret)
4327 dev_err(hba->dev,
4328 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4329 __func__, hba->init_prefetch_data.icc_level , ret);
4330
4331}
4332
4333/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004334 * ufshcd_scsi_add_wlus - Adds required W-LUs
4335 * @hba: per-adapter instance
4336 *
4337 * UFS device specification requires the UFS devices to support 4 well known
4338 * logical units:
4339 * "REPORT_LUNS" (address: 01h)
4340 * "UFS Device" (address: 50h)
4341 * "RPMB" (address: 44h)
4342 * "BOOT" (address: 30h)
4343 * UFS device's power management needs to be controlled by "POWER CONDITION"
4344 * field of SSU (START STOP UNIT) command. But this "power condition" field
4345 * will take effect only when its sent to "UFS device" well known logical unit
4346 * hence we require the scsi_device instance to represent this logical unit in
4347 * order for the UFS host driver to send the SSU command for power management.
4348
4349 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4350 * Block) LU so user space process can control this LU. User space may also
4351 * want to have access to BOOT LU.
4352
4353 * This function adds scsi device instances for each of all well known LUs
4354 * (except "REPORT LUNS" LU).
4355 *
4356 * Returns zero on success (all required W-LUs are added successfully),
4357 * non-zero error value on failure (if failed to add any of the required W-LU).
4358 */
4359static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4360{
4361 int ret = 0;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004362 struct scsi_device *sdev_rpmb;
4363 struct scsi_device *sdev_boot;
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004364
4365 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4366 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4367 if (IS_ERR(hba->sdev_ufs_device)) {
4368 ret = PTR_ERR(hba->sdev_ufs_device);
4369 hba->sdev_ufs_device = NULL;
4370 goto out;
4371 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004372 scsi_device_put(hba->sdev_ufs_device);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004373
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004374 sdev_boot = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004375 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004376 if (IS_ERR(sdev_boot)) {
4377 ret = PTR_ERR(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004378 goto remove_sdev_ufs_device;
4379 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004380 scsi_device_put(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004381
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004382 sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004383 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004384 if (IS_ERR(sdev_rpmb)) {
4385 ret = PTR_ERR(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004386 goto remove_sdev_boot;
4387 }
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004388 scsi_device_put(sdev_rpmb);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004389 goto out;
4390
4391remove_sdev_boot:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004392 scsi_remove_device(sdev_boot);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004393remove_sdev_ufs_device:
4394 scsi_remove_device(hba->sdev_ufs_device);
4395out:
4396 return ret;
4397}
4398
4399/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004400 * ufshcd_probe_hba - probe hba to detect device and initialize
4401 * @hba: per-adapter instance
4402 *
4403 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304404 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004405static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304406{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304407 int ret;
4408
4409 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304410 if (ret)
4411 goto out;
4412
Yaniv Gardi50646362014-10-23 13:25:13 +03004413 ufshcd_init_pwr_info(hba);
4414
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004415 /* UniPro link is active now */
4416 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304417
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304418 ret = ufshcd_verify_dev_init(hba);
4419 if (ret)
4420 goto out;
4421
Dolev Raviv68078d52013-07-30 00:35:58 +05304422 ret = ufshcd_complete_dev_init(hba);
4423 if (ret)
4424 goto out;
4425
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004426 /* UFS device is also active now */
4427 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304428 ufshcd_force_reset_auto_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004429 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304430
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004431 if (ufshcd_get_max_pwr_mode(hba)) {
4432 dev_err(hba->dev,
4433 "%s: Failed getting max supported power mode\n",
4434 __func__);
4435 } else {
4436 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
4437 if (ret)
4438 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
4439 __func__, ret);
4440 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004441
Yaniv Gardi53c12d02016-02-01 15:02:45 +02004442 /* set the state as operational after switching to desired gear */
4443 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004444 /*
4445 * If we are in error handling context or in power management callbacks
4446 * context, no need to scan the host
4447 */
4448 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4449 bool flag;
4450
4451 /* clear any previous UFS device information */
4452 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
4453 if (!ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4454 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
4455 hba->dev_info.f_power_on_wp_en = flag;
4456
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004457 if (!hba->is_init_prefetch)
4458 ufshcd_init_icc_levels(hba);
4459
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004460 /* Add required well known logical units to scsi mid layer */
4461 if (ufshcd_scsi_add_wlus(hba))
4462 goto out;
4463
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304464 scsi_scan_host(hba->host);
4465 pm_runtime_put_sync(hba->dev);
4466 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004467
4468 if (!hba->is_init_prefetch)
4469 hba->is_init_prefetch = true;
4470
Sahitya Tummala856b3482014-09-25 15:32:34 +03004471 /* Resume devfreq after UFS device is detected */
4472 if (ufshcd_is_clkscaling_enabled(hba))
4473 devfreq_resume_device(hba->devfreq);
4474
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304475out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004476 /*
4477 * If we failed to initialize the device or the device is not
4478 * present, turn off the power/clocks etc.
4479 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004480 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4481 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004482 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004483 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004484
4485 return ret;
4486}
4487
4488/**
4489 * ufshcd_async_scan - asynchronous execution for probing hba
4490 * @data: data pointer to pass to this function
4491 * @cookie: cookie data
4492 */
4493static void ufshcd_async_scan(void *data, async_cookie_t cookie)
4494{
4495 struct ufs_hba *hba = (struct ufs_hba *)data;
4496
4497 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304498}
4499
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304500static struct scsi_host_template ufshcd_driver_template = {
4501 .module = THIS_MODULE,
4502 .name = UFSHCD,
4503 .proc_name = UFSHCD,
4504 .queuecommand = ufshcd_queuecommand,
4505 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004506 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304507 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004508 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304509 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304510 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
4511 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304512 .this_id = -1,
4513 .sg_tablesize = SG_ALL,
4514 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
4515 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004516 .max_host_blocked = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01004517 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304518};
4519
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004520static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
4521 int ua)
4522{
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004523 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004524
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004525 if (!vreg)
4526 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004527
Bjorn Andersson7b16a072015-02-11 19:35:28 -08004528 ret = regulator_set_load(vreg->reg, ua);
4529 if (ret < 0) {
4530 dev_err(dev, "%s: %s set load (ua=%d) failed, err=%d\n",
4531 __func__, vreg->name, ua, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004532 }
4533
4534 return ret;
4535}
4536
4537static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
4538 struct ufs_vreg *vreg)
4539{
4540 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
4541}
4542
4543static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
4544 struct ufs_vreg *vreg)
4545{
4546 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
4547}
4548
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004549static int ufshcd_config_vreg(struct device *dev,
4550 struct ufs_vreg *vreg, bool on)
4551{
4552 int ret = 0;
4553 struct regulator *reg = vreg->reg;
4554 const char *name = vreg->name;
4555 int min_uV, uA_load;
4556
4557 BUG_ON(!vreg);
4558
4559 if (regulator_count_voltages(reg) > 0) {
4560 min_uV = on ? vreg->min_uV : 0;
4561 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
4562 if (ret) {
4563 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
4564 __func__, name, ret);
4565 goto out;
4566 }
4567
4568 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004569 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
4570 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004571 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004572 }
4573out:
4574 return ret;
4575}
4576
4577static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
4578{
4579 int ret = 0;
4580
4581 if (!vreg || vreg->enabled)
4582 goto out;
4583
4584 ret = ufshcd_config_vreg(dev, vreg, true);
4585 if (!ret)
4586 ret = regulator_enable(vreg->reg);
4587
4588 if (!ret)
4589 vreg->enabled = true;
4590 else
4591 dev_err(dev, "%s: %s enable failed, err=%d\n",
4592 __func__, vreg->name, ret);
4593out:
4594 return ret;
4595}
4596
4597static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
4598{
4599 int ret = 0;
4600
4601 if (!vreg || !vreg->enabled)
4602 goto out;
4603
4604 ret = regulator_disable(vreg->reg);
4605
4606 if (!ret) {
4607 /* ignore errors on applying disable config */
4608 ufshcd_config_vreg(dev, vreg, false);
4609 vreg->enabled = false;
4610 } else {
4611 dev_err(dev, "%s: %s disable failed, err=%d\n",
4612 __func__, vreg->name, ret);
4613 }
4614out:
4615 return ret;
4616}
4617
4618static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
4619{
4620 int ret = 0;
4621 struct device *dev = hba->dev;
4622 struct ufs_vreg_info *info = &hba->vreg_info;
4623
4624 if (!info)
4625 goto out;
4626
4627 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
4628 if (ret)
4629 goto out;
4630
4631 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
4632 if (ret)
4633 goto out;
4634
4635 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
4636 if (ret)
4637 goto out;
4638
4639out:
4640 if (ret) {
4641 ufshcd_toggle_vreg(dev, info->vccq2, false);
4642 ufshcd_toggle_vreg(dev, info->vccq, false);
4643 ufshcd_toggle_vreg(dev, info->vcc, false);
4644 }
4645 return ret;
4646}
4647
Raviv Shvili6a771a62014-09-25 15:32:24 +03004648static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
4649{
4650 struct ufs_vreg_info *info = &hba->vreg_info;
4651
4652 if (info)
4653 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
4654
4655 return 0;
4656}
4657
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004658static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
4659{
4660 int ret = 0;
4661
4662 if (!vreg)
4663 goto out;
4664
4665 vreg->reg = devm_regulator_get(dev, vreg->name);
4666 if (IS_ERR(vreg->reg)) {
4667 ret = PTR_ERR(vreg->reg);
4668 dev_err(dev, "%s: %s get failed, err=%d\n",
4669 __func__, vreg->name, ret);
4670 }
4671out:
4672 return ret;
4673}
4674
4675static int ufshcd_init_vreg(struct ufs_hba *hba)
4676{
4677 int ret = 0;
4678 struct device *dev = hba->dev;
4679 struct ufs_vreg_info *info = &hba->vreg_info;
4680
4681 if (!info)
4682 goto out;
4683
4684 ret = ufshcd_get_vreg(dev, info->vcc);
4685 if (ret)
4686 goto out;
4687
4688 ret = ufshcd_get_vreg(dev, info->vccq);
4689 if (ret)
4690 goto out;
4691
4692 ret = ufshcd_get_vreg(dev, info->vccq2);
4693out:
4694 return ret;
4695}
4696
Raviv Shvili6a771a62014-09-25 15:32:24 +03004697static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
4698{
4699 struct ufs_vreg_info *info = &hba->vreg_info;
4700
4701 if (info)
4702 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
4703
4704 return 0;
4705}
4706
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004707static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
4708 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004709{
4710 int ret = 0;
4711 struct ufs_clk_info *clki;
4712 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004713 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004714
4715 if (!head || list_empty(head))
4716 goto out;
4717
4718 list_for_each_entry(clki, head, list) {
4719 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004720 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
4721 continue;
4722
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004723 if (on && !clki->enabled) {
4724 ret = clk_prepare_enable(clki->clk);
4725 if (ret) {
4726 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
4727 __func__, clki->name, ret);
4728 goto out;
4729 }
4730 } else if (!on && clki->enabled) {
4731 clk_disable_unprepare(clki->clk);
4732 }
4733 clki->enabled = on;
4734 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
4735 clki->name, on ? "en" : "dis");
4736 }
4737 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004738
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004739 ret = ufshcd_vops_setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004740out:
4741 if (ret) {
4742 list_for_each_entry(clki, head, list) {
4743 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
4744 clk_disable_unprepare(clki->clk);
4745 }
Dolev Raviveda910e2014-10-23 13:25:16 +03004746 } else if (on) {
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004747 spin_lock_irqsave(hba->host->host_lock, flags);
4748 hba->clk_gating.state = CLKS_ON;
4749 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004750 }
4751 return ret;
4752}
4753
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004754static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
4755{
4756 return __ufshcd_setup_clocks(hba, on, false);
4757}
4758
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004759static int ufshcd_init_clocks(struct ufs_hba *hba)
4760{
4761 int ret = 0;
4762 struct ufs_clk_info *clki;
4763 struct device *dev = hba->dev;
4764 struct list_head *head = &hba->clk_list_head;
4765
4766 if (!head || list_empty(head))
4767 goto out;
4768
4769 list_for_each_entry(clki, head, list) {
4770 if (!clki->name)
4771 continue;
4772
4773 clki->clk = devm_clk_get(dev, clki->name);
4774 if (IS_ERR(clki->clk)) {
4775 ret = PTR_ERR(clki->clk);
4776 dev_err(dev, "%s: %s clk get failed, %d\n",
4777 __func__, clki->name, ret);
4778 goto out;
4779 }
4780
4781 if (clki->max_freq) {
4782 ret = clk_set_rate(clki->clk, clki->max_freq);
4783 if (ret) {
4784 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
4785 __func__, clki->name,
4786 clki->max_freq, ret);
4787 goto out;
4788 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03004789 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004790 }
4791 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
4792 clki->name, clk_get_rate(clki->clk));
4793 }
4794out:
4795 return ret;
4796}
4797
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004798static int ufshcd_variant_hba_init(struct ufs_hba *hba)
4799{
4800 int err = 0;
4801
4802 if (!hba->vops)
4803 goto out;
4804
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004805 err = ufshcd_vops_init(hba);
4806 if (err)
4807 goto out;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004808
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004809 err = ufshcd_vops_setup_regulators(hba, true);
4810 if (err)
4811 goto out_exit;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004812
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004813 goto out;
4814
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004815out_exit:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004816 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004817out:
4818 if (err)
4819 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004820 __func__, ufshcd_get_var_name(hba), err);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004821 return err;
4822}
4823
4824static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
4825{
4826 if (!hba->vops)
4827 return;
4828
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004829 ufshcd_vops_setup_clocks(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004830
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004831 ufshcd_vops_setup_regulators(hba, false);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004832
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02004833 ufshcd_vops_exit(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004834}
4835
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004836static int ufshcd_hba_init(struct ufs_hba *hba)
4837{
4838 int err;
4839
Raviv Shvili6a771a62014-09-25 15:32:24 +03004840 /*
4841 * Handle host controller power separately from the UFS device power
4842 * rails as it will help controlling the UFS host controller power
4843 * collapse easily which is different than UFS device power collapse.
4844 * Also, enable the host controller power before we go ahead with rest
4845 * of the initialization here.
4846 */
4847 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004848 if (err)
4849 goto out;
4850
Raviv Shvili6a771a62014-09-25 15:32:24 +03004851 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004852 if (err)
4853 goto out;
4854
Raviv Shvili6a771a62014-09-25 15:32:24 +03004855 err = ufshcd_init_clocks(hba);
4856 if (err)
4857 goto out_disable_hba_vreg;
4858
4859 err = ufshcd_setup_clocks(hba, true);
4860 if (err)
4861 goto out_disable_hba_vreg;
4862
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004863 err = ufshcd_init_vreg(hba);
4864 if (err)
4865 goto out_disable_clks;
4866
4867 err = ufshcd_setup_vreg(hba, true);
4868 if (err)
4869 goto out_disable_clks;
4870
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004871 err = ufshcd_variant_hba_init(hba);
4872 if (err)
4873 goto out_disable_vreg;
4874
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004875 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004876 goto out;
4877
4878out_disable_vreg:
4879 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004880out_disable_clks:
4881 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03004882out_disable_hba_vreg:
4883 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004884out:
4885 return err;
4886}
4887
4888static void ufshcd_hba_exit(struct ufs_hba *hba)
4889{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004890 if (hba->is_powered) {
4891 ufshcd_variant_hba_exit(hba);
4892 ufshcd_setup_vreg(hba, false);
4893 ufshcd_setup_clocks(hba, false);
4894 ufshcd_setup_hba_vreg(hba, false);
4895 hba->is_powered = false;
4896 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004897}
4898
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004899static int
4900ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304901{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004902 unsigned char cmd[6] = {REQUEST_SENSE,
4903 0,
4904 0,
4905 0,
4906 SCSI_SENSE_BUFFERSIZE,
4907 0};
4908 char *buffer;
4909 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304910
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004911 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4912 if (!buffer) {
4913 ret = -ENOMEM;
4914 goto out;
4915 }
4916
4917 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
4918 SCSI_SENSE_BUFFERSIZE, NULL,
4919 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
4920 if (ret)
4921 pr_err("%s: failed with err %d\n", __func__, ret);
4922
4923 kfree(buffer);
4924out:
4925 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304926}
4927
4928/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004929 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
4930 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304931 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004932 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304933 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004934 * Returns 0 if requested power mode is set successfully
4935 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304936 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004937static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
4938 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304939{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004940 unsigned char cmd[6] = { START_STOP };
4941 struct scsi_sense_hdr sshdr;
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004942 struct scsi_device *sdp;
4943 unsigned long flags;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004944 int ret;
4945
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004946 spin_lock_irqsave(hba->host->host_lock, flags);
4947 sdp = hba->sdev_ufs_device;
4948 if (sdp) {
4949 ret = scsi_device_get(sdp);
4950 if (!ret && !scsi_device_online(sdp)) {
4951 ret = -ENODEV;
4952 scsi_device_put(sdp);
4953 }
4954 } else {
4955 ret = -ENODEV;
4956 }
4957 spin_unlock_irqrestore(hba->host->host_lock, flags);
4958
4959 if (ret)
4960 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004961
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304962 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004963 * If scsi commands fail, the scsi mid-layer schedules scsi error-
4964 * handling, which would wait for host to be resumed. Since we know
4965 * we are functional while we are here, skip host resume in error
4966 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304967 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004968 hba->host->eh_noresume = 1;
4969 if (hba->wlun_dev_clr_ua) {
4970 ret = ufshcd_send_request_sense(hba, sdp);
4971 if (ret)
4972 goto out;
4973 /* Unit attention condition is cleared now */
4974 hba->wlun_dev_clr_ua = false;
4975 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304976
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004977 cmd[4] = pwr_mode << 4;
4978
4979 /*
4980 * Current function would be generally called from the power management
4981 * callbacks hence set the REQ_PM flag so that it doesn't resume the
4982 * already suspended childs.
4983 */
4984 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
4985 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
4986 if (ret) {
4987 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02004988 "START_STOP failed for power mode: %d, result %x\n",
4989 pwr_mode, ret);
Hannes Reinecke21045512015-01-08 07:43:46 +01004990 if (driver_byte(ret) & DRIVER_SENSE)
4991 scsi_print_sense_hdr(sdp, NULL, &sshdr);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004992 }
4993
4994 if (!ret)
4995 hba->curr_dev_pwr_mode = pwr_mode;
4996out:
Akinobu Mita7c48bfd2014-10-23 13:25:12 +03004997 scsi_device_put(sdp);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004998 hba->host->eh_noresume = 0;
4999 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305000}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305001
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005002static int ufshcd_link_state_transition(struct ufs_hba *hba,
5003 enum uic_link_state req_link_state,
5004 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305005{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005006 int ret = 0;
5007
5008 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305009 return 0;
5010
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005011 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
5012 ret = ufshcd_uic_hibern8_enter(hba);
5013 if (!ret)
5014 ufshcd_set_link_hibern8(hba);
5015 else
5016 goto out;
5017 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305018 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005019 * If autobkops is enabled, link can't be turned off because
5020 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305021 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005022 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
5023 (!check_for_bkops || (check_for_bkops &&
5024 !hba->auto_bkops_enabled))) {
5025 /*
5026 * Change controller state to "reset state" which
5027 * should also put the link in off/reset state
5028 */
5029 ufshcd_hba_stop(hba);
5030 /*
5031 * TODO: Check if we need any delay to make sure that
5032 * controller is reset
5033 */
5034 ufshcd_set_link_off(hba);
5035 }
5036
5037out:
5038 return ret;
5039}
5040
5041static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
5042{
5043 /*
5044 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
5045 * power.
5046 *
5047 * If UFS device and link is in OFF state, all power supplies (VCC,
5048 * VCCQ, VCCQ2) can be turned off if power on write protect is not
5049 * required. If UFS link is inactive (Hibern8 or OFF state) and device
5050 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
5051 *
5052 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
5053 * in low power state which would save some power.
5054 */
5055 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5056 !hba->dev_info.is_lu_power_on_wp) {
5057 ufshcd_setup_vreg(hba, false);
5058 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5059 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5060 if (!ufshcd_is_link_active(hba)) {
5061 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5062 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
5063 }
5064 }
5065}
5066
5067static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
5068{
5069 int ret = 0;
5070
5071 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
5072 !hba->dev_info.is_lu_power_on_wp) {
5073 ret = ufshcd_setup_vreg(hba, true);
5074 } else if (!ufshcd_is_ufs_dev_active(hba)) {
5075 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
5076 if (!ret && !ufshcd_is_link_active(hba)) {
5077 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
5078 if (ret)
5079 goto vcc_disable;
5080 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
5081 if (ret)
5082 goto vccq_lpm;
5083 }
5084 }
5085 goto out;
5086
5087vccq_lpm:
5088 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
5089vcc_disable:
5090 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
5091out:
5092 return ret;
5093}
5094
5095static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
5096{
5097 if (ufshcd_is_link_off(hba))
5098 ufshcd_setup_hba_vreg(hba, false);
5099}
5100
5101static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
5102{
5103 if (ufshcd_is_link_off(hba))
5104 ufshcd_setup_hba_vreg(hba, true);
5105}
5106
5107/**
5108 * ufshcd_suspend - helper function for suspend operations
5109 * @hba: per adapter instance
5110 * @pm_op: desired low power operation type
5111 *
5112 * This function will try to put the UFS device and link into low power
5113 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
5114 * (System PM level).
5115 *
5116 * If this function is called during shutdown, it will make sure that
5117 * both UFS device and UFS link is powered off.
5118 *
5119 * NOTE: UFS device & link must be active before we enter in this function.
5120 *
5121 * Returns 0 for success and non-zero for failure
5122 */
5123static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5124{
5125 int ret = 0;
5126 enum ufs_pm_level pm_lvl;
5127 enum ufs_dev_pwr_mode req_dev_pwr_mode;
5128 enum uic_link_state req_link_state;
5129
5130 hba->pm_op_in_progress = 1;
5131 if (!ufshcd_is_shutdown_pm(pm_op)) {
5132 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
5133 hba->rpm_lvl : hba->spm_lvl;
5134 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
5135 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
5136 } else {
5137 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
5138 req_link_state = UIC_LINK_OFF_STATE;
5139 }
5140
5141 /*
5142 * If we can't transition into any of the low power modes
5143 * just gate the clocks.
5144 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005145 ufshcd_hold(hba, false);
5146 hba->clk_gating.is_suspended = true;
5147
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005148 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
5149 req_link_state == UIC_LINK_ACTIVE_STATE) {
5150 goto disable_clks;
5151 }
5152
5153 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
5154 (req_link_state == hba->uic_link_state))
5155 goto out;
5156
5157 /* UFS device & link must be active before we enter in this function */
5158 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
5159 ret = -EINVAL;
5160 goto out;
5161 }
5162
5163 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03005164 if (ufshcd_can_autobkops_during_suspend(hba)) {
5165 /*
5166 * The device is idle with no requests in the queue,
5167 * allow background operations if bkops status shows
5168 * that performance might be impacted.
5169 */
5170 ret = ufshcd_urgent_bkops(hba);
5171 if (ret)
5172 goto enable_gating;
5173 } else {
5174 /* make sure that auto bkops is disabled */
5175 ufshcd_disable_auto_bkops(hba);
5176 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005177 }
5178
5179 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
5180 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
5181 !ufshcd_is_runtime_pm(pm_op))) {
5182 /* ensure that bkops is disabled */
5183 ufshcd_disable_auto_bkops(hba);
5184 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
5185 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005186 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005187 }
5188
5189 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
5190 if (ret)
5191 goto set_dev_active;
5192
5193 ufshcd_vreg_set_lpm(hba);
5194
5195disable_clks:
5196 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03005197 * The clock scaling needs access to controller registers. Hence, Wait
5198 * for pending clock scaling work to be done before clocks are
5199 * turned off.
5200 */
5201 if (ufshcd_is_clkscaling_enabled(hba)) {
5202 devfreq_suspend_device(hba->devfreq);
5203 hba->clk_scaling.window_start_t = 0;
5204 }
5205 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005206 * Call vendor specific suspend callback. As these callbacks may access
5207 * vendor specific host controller register space call them before the
5208 * host clocks are ON.
5209 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005210 ret = ufshcd_vops_suspend(hba, pm_op);
5211 if (ret)
5212 goto set_link_active;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005213
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005214 ret = ufshcd_vops_setup_clocks(hba, false);
5215 if (ret)
5216 goto vops_resume;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005217
5218 if (!ufshcd_is_link_active(hba))
5219 ufshcd_setup_clocks(hba, false);
5220 else
5221 /* If link is active, device ref_clk can't be switched off */
5222 __ufshcd_setup_clocks(hba, false, true);
5223
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005224 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005225 /*
5226 * Disable the host irq as host controller as there won't be any
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005227 * host controller transaction expected till resume.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005228 */
5229 ufshcd_disable_irq(hba);
5230 /* Put the host controller in low power mode if possible */
5231 ufshcd_hba_vreg_set_lpm(hba);
5232 goto out;
5233
5234vops_resume:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005235 ufshcd_vops_resume(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005236set_link_active:
5237 ufshcd_vreg_set_hpm(hba);
5238 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
5239 ufshcd_set_link_active(hba);
5240 else if (ufshcd_is_link_off(hba))
5241 ufshcd_host_reset_and_restore(hba);
5242set_dev_active:
5243 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
5244 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005245enable_gating:
5246 hba->clk_gating.is_suspended = false;
5247 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005248out:
5249 hba->pm_op_in_progress = 0;
5250 return ret;
5251}
5252
5253/**
5254 * ufshcd_resume - helper function for resume operations
5255 * @hba: per adapter instance
5256 * @pm_op: runtime PM or system PM
5257 *
5258 * This function basically brings the UFS device, UniPro link and controller
5259 * to active state.
5260 *
5261 * Returns 0 for success and non-zero for failure
5262 */
5263static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
5264{
5265 int ret;
5266 enum uic_link_state old_link_state;
5267
5268 hba->pm_op_in_progress = 1;
5269 old_link_state = hba->uic_link_state;
5270
5271 ufshcd_hba_vreg_set_hpm(hba);
5272 /* Make sure clocks are enabled before accessing controller */
5273 ret = ufshcd_setup_clocks(hba, true);
5274 if (ret)
5275 goto out;
5276
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005277 /* enable the host irq as host controller would be active soon */
5278 ret = ufshcd_enable_irq(hba);
5279 if (ret)
5280 goto disable_irq_and_vops_clks;
5281
5282 ret = ufshcd_vreg_set_hpm(hba);
5283 if (ret)
5284 goto disable_irq_and_vops_clks;
5285
5286 /*
5287 * Call vendor specific resume callback. As these callbacks may access
5288 * vendor specific host controller register space call them when the
5289 * host clocks are ON.
5290 */
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005291 ret = ufshcd_vops_resume(hba, pm_op);
5292 if (ret)
5293 goto disable_vreg;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005294
5295 if (ufshcd_is_link_hibern8(hba)) {
5296 ret = ufshcd_uic_hibern8_exit(hba);
5297 if (!ret)
5298 ufshcd_set_link_active(hba);
5299 else
5300 goto vendor_suspend;
5301 } else if (ufshcd_is_link_off(hba)) {
5302 ret = ufshcd_host_reset_and_restore(hba);
5303 /*
5304 * ufshcd_host_reset_and_restore() should have already
5305 * set the link state as active
5306 */
5307 if (ret || !ufshcd_is_link_active(hba))
5308 goto vendor_suspend;
5309 }
5310
5311 if (!ufshcd_is_ufs_dev_active(hba)) {
5312 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
5313 if (ret)
5314 goto set_old_link_state;
5315 }
5316
Subhash Jadavani374a2462014-09-25 15:32:35 +03005317 /*
5318 * If BKOPs operations are urgently needed at this moment then
5319 * keep auto-bkops enabled or else disable it.
5320 */
5321 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005322 hba->clk_gating.is_suspended = false;
5323
Sahitya Tummala856b3482014-09-25 15:32:34 +03005324 if (ufshcd_is_clkscaling_enabled(hba))
5325 devfreq_resume_device(hba->devfreq);
5326
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005327 /* Schedule clock gating in case of no access to UFS device yet */
5328 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005329 goto out;
5330
5331set_old_link_state:
5332 ufshcd_link_state_transition(hba, old_link_state, 0);
5333vendor_suspend:
Yaniv Gardi0263bcd2015-10-28 13:15:48 +02005334 ufshcd_vops_suspend(hba, pm_op);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005335disable_vreg:
5336 ufshcd_vreg_set_lpm(hba);
5337disable_irq_and_vops_clks:
5338 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005339 ufshcd_setup_clocks(hba, false);
5340out:
5341 hba->pm_op_in_progress = 0;
5342 return ret;
5343}
5344
5345/**
5346 * ufshcd_system_suspend - system suspend routine
5347 * @hba: per adapter instance
5348 * @pm_op: runtime PM or system PM
5349 *
5350 * Check the description of ufshcd_suspend() function for more details.
5351 *
5352 * Returns 0 for success and non-zero for failure
5353 */
5354int ufshcd_system_suspend(struct ufs_hba *hba)
5355{
5356 int ret = 0;
5357
5358 if (!hba || !hba->is_powered)
Dolev Raviv233b5942014-10-23 13:25:14 +03005359 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005360
5361 if (pm_runtime_suspended(hba->dev)) {
5362 if (hba->rpm_lvl == hba->spm_lvl)
5363 /*
5364 * There is possibility that device may still be in
5365 * active state during the runtime suspend.
5366 */
5367 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
5368 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
5369 goto out;
5370
5371 /*
5372 * UFS device and/or UFS link low power states during runtime
5373 * suspend seems to be different than what is expected during
5374 * system suspend. Hence runtime resume the devic & link and
5375 * let the system suspend low power states to take effect.
5376 * TODO: If resume takes longer time, we might have optimize
5377 * it in future by not resuming everything if possible.
5378 */
5379 ret = ufshcd_runtime_resume(hba);
5380 if (ret)
5381 goto out;
5382 }
5383
5384 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
5385out:
Dolev Ravive7850602014-09-25 15:32:36 +03005386 if (!ret)
5387 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005388 return ret;
5389}
5390EXPORT_SYMBOL(ufshcd_system_suspend);
5391
5392/**
5393 * ufshcd_system_resume - system resume routine
5394 * @hba: per adapter instance
5395 *
5396 * Returns 0 for success and non-zero for failure
5397 */
5398
5399int ufshcd_system_resume(struct ufs_hba *hba)
5400{
5401 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
5402 /*
5403 * Let the runtime resume take care of resuming
5404 * if runtime suspended.
5405 */
5406 return 0;
5407
5408 return ufshcd_resume(hba, UFS_SYSTEM_PM);
5409}
5410EXPORT_SYMBOL(ufshcd_system_resume);
5411
5412/**
5413 * ufshcd_runtime_suspend - runtime suspend routine
5414 * @hba: per adapter instance
5415 *
5416 * Check the description of ufshcd_suspend() function for more details.
5417 *
5418 * Returns 0 for success and non-zero for failure
5419 */
5420int ufshcd_runtime_suspend(struct ufs_hba *hba)
5421{
5422 if (!hba || !hba->is_powered)
5423 return 0;
5424
5425 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305426}
5427EXPORT_SYMBOL(ufshcd_runtime_suspend);
5428
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005429/**
5430 * ufshcd_runtime_resume - runtime resume routine
5431 * @hba: per adapter instance
5432 *
5433 * This function basically brings the UFS device, UniPro link and controller
5434 * to active state. Following operations are done in this function:
5435 *
5436 * 1. Turn on all the controller related clocks
5437 * 2. Bring the UniPro link out of Hibernate state
5438 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
5439 * to active state.
5440 * 4. If auto-bkops is enabled on the device, disable it.
5441 *
5442 * So following would be the possible power state after this function return
5443 * successfully:
5444 * S1: UFS device in Active state with VCC rail ON
5445 * UniPro link in Active state
5446 * All the UFS/UniPro controller clocks are ON
5447 *
5448 * Returns 0 for success and non-zero for failure
5449 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305450int ufshcd_runtime_resume(struct ufs_hba *hba)
5451{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005452 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305453 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005454 else
5455 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305456}
5457EXPORT_SYMBOL(ufshcd_runtime_resume);
5458
5459int ufshcd_runtime_idle(struct ufs_hba *hba)
5460{
5461 return 0;
5462}
5463EXPORT_SYMBOL(ufshcd_runtime_idle);
5464
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305465/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005466 * ufshcd_shutdown - shutdown routine
5467 * @hba: per adapter instance
5468 *
5469 * This function would power off both UFS device and UFS link.
5470 *
5471 * Returns 0 always to allow force shutdown even in case of errors.
5472 */
5473int ufshcd_shutdown(struct ufs_hba *hba)
5474{
5475 int ret = 0;
5476
5477 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
5478 goto out;
5479
5480 if (pm_runtime_suspended(hba->dev)) {
5481 ret = ufshcd_runtime_resume(hba);
5482 if (ret)
5483 goto out;
5484 }
5485
5486 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
5487out:
5488 if (ret)
5489 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
5490 /* allow force shutdown even in case of errors */
5491 return 0;
5492}
5493EXPORT_SYMBOL(ufshcd_shutdown);
5494
5495/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305496 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305497 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305498 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305499 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305500void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305501{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05305502 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305503 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305504 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305505 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305506
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305507 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005508
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005509 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005510 if (ufshcd_is_clkscaling_enabled(hba))
5511 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005512 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305513}
5514EXPORT_SYMBOL_GPL(ufshcd_remove);
5515
5516/**
Yaniv Gardi47555a52015-10-28 13:15:49 +02005517 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
5518 * @hba: pointer to Host Bus Adapter (HBA)
5519 */
5520void ufshcd_dealloc_host(struct ufs_hba *hba)
5521{
5522 scsi_host_put(hba->host);
5523}
5524EXPORT_SYMBOL_GPL(ufshcd_dealloc_host);
5525
5526/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005527 * ufshcd_set_dma_mask - Set dma mask based on the controller
5528 * addressing capability
5529 * @hba: per adapter instance
5530 *
5531 * Returns 0 for success, non-zero for failure
5532 */
5533static int ufshcd_set_dma_mask(struct ufs_hba *hba)
5534{
5535 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
5536 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
5537 return 0;
5538 }
5539 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
5540}
5541
5542/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005543 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305544 * @dev: pointer to device handle
5545 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305546 * Returns 0 on success, non-zero value on failure
5547 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005548int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305549{
5550 struct Scsi_Host *host;
5551 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005552 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305553
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305554 if (!dev) {
5555 dev_err(dev,
5556 "Invalid memory reference for dev is NULL\n");
5557 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305558 goto out_error;
5559 }
5560
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305561 host = scsi_host_alloc(&ufshcd_driver_template,
5562 sizeof(struct ufs_hba));
5563 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305564 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305565 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305566 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305567 }
5568 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305569 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305570 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005571 *hba_handle = hba;
5572
5573out_error:
5574 return err;
5575}
5576EXPORT_SYMBOL(ufshcd_alloc_host);
5577
Sahitya Tummala856b3482014-09-25 15:32:34 +03005578static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
5579{
5580 int ret = 0;
5581 struct ufs_clk_info *clki;
5582 struct list_head *head = &hba->clk_list_head;
5583
5584 if (!head || list_empty(head))
5585 goto out;
5586
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005587 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE);
5588 if (ret)
5589 return ret;
5590
Sahitya Tummala856b3482014-09-25 15:32:34 +03005591 list_for_each_entry(clki, head, list) {
5592 if (!IS_ERR_OR_NULL(clki->clk)) {
5593 if (scale_up && clki->max_freq) {
5594 if (clki->curr_freq == clki->max_freq)
5595 continue;
5596 ret = clk_set_rate(clki->clk, clki->max_freq);
5597 if (ret) {
5598 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5599 __func__, clki->name,
5600 clki->max_freq, ret);
5601 break;
5602 }
5603 clki->curr_freq = clki->max_freq;
5604
5605 } else if (!scale_up && clki->min_freq) {
5606 if (clki->curr_freq == clki->min_freq)
5607 continue;
5608 ret = clk_set_rate(clki->clk, clki->min_freq);
5609 if (ret) {
5610 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5611 __func__, clki->name,
5612 clki->min_freq, ret);
5613 break;
5614 }
5615 clki->curr_freq = clki->min_freq;
5616 }
5617 }
5618 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
5619 clki->name, clk_get_rate(clki->clk));
5620 }
Yaniv Gardif06fcc72015-10-28 13:15:51 +02005621
5622 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE);
5623
Sahitya Tummala856b3482014-09-25 15:32:34 +03005624out:
5625 return ret;
5626}
5627
5628static int ufshcd_devfreq_target(struct device *dev,
5629 unsigned long *freq, u32 flags)
5630{
5631 int err = 0;
5632 struct ufs_hba *hba = dev_get_drvdata(dev);
5633
5634 if (!ufshcd_is_clkscaling_enabled(hba))
5635 return -EINVAL;
5636
5637 if (*freq == UINT_MAX)
5638 err = ufshcd_scale_clks(hba, true);
5639 else if (*freq == 0)
5640 err = ufshcd_scale_clks(hba, false);
5641
5642 return err;
5643}
5644
5645static int ufshcd_devfreq_get_dev_status(struct device *dev,
5646 struct devfreq_dev_status *stat)
5647{
5648 struct ufs_hba *hba = dev_get_drvdata(dev);
5649 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
5650 unsigned long flags;
5651
5652 if (!ufshcd_is_clkscaling_enabled(hba))
5653 return -EINVAL;
5654
5655 memset(stat, 0, sizeof(*stat));
5656
5657 spin_lock_irqsave(hba->host->host_lock, flags);
5658 if (!scaling->window_start_t)
5659 goto start_window;
5660
5661 if (scaling->is_busy_started)
5662 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
5663 scaling->busy_start_t));
5664
5665 stat->total_time = jiffies_to_usecs((long)jiffies -
5666 (long)scaling->window_start_t);
5667 stat->busy_time = scaling->tot_busy_t;
5668start_window:
5669 scaling->window_start_t = jiffies;
5670 scaling->tot_busy_t = 0;
5671
5672 if (hba->outstanding_reqs) {
5673 scaling->busy_start_t = ktime_get();
5674 scaling->is_busy_started = true;
5675 } else {
5676 scaling->busy_start_t = ktime_set(0, 0);
5677 scaling->is_busy_started = false;
5678 }
5679 spin_unlock_irqrestore(hba->host->host_lock, flags);
5680 return 0;
5681}
5682
5683static struct devfreq_dev_profile ufs_devfreq_profile = {
5684 .polling_ms = 100,
5685 .target = ufshcd_devfreq_target,
5686 .get_dev_status = ufshcd_devfreq_get_dev_status,
5687};
5688
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005689/**
5690 * ufshcd_init - Driver initialization routine
5691 * @hba: per-adapter instance
5692 * @mmio_base: base register address
5693 * @irq: Interrupt line of device
5694 * Returns 0 on success, non-zero value on failure
5695 */
5696int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
5697{
5698 int err;
5699 struct Scsi_Host *host = hba->host;
5700 struct device *dev = hba->dev;
5701
5702 if (!mmio_base) {
5703 dev_err(hba->dev,
5704 "Invalid memory reference for mmio_base is NULL\n");
5705 err = -ENODEV;
5706 goto out_error;
5707 }
5708
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305709 hba->mmio_base = mmio_base;
5710 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305711
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005712 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005713 if (err)
5714 goto out_error;
5715
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305716 /* Read capabilities registers */
5717 ufshcd_hba_capabilities(hba);
5718
5719 /* Get UFS version supported by the controller */
5720 hba->ufs_version = ufshcd_get_ufs_version(hba);
5721
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305722 /* Get Interrupt bit mask per version */
5723 hba->intr_mask = ufshcd_get_intr_mask(hba);
5724
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005725 err = ufshcd_set_dma_mask(hba);
5726 if (err) {
5727 dev_err(hba->dev, "set dma mask failed\n");
5728 goto out_disable;
5729 }
5730
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305731 /* Allocate memory for host memory space */
5732 err = ufshcd_memory_alloc(hba);
5733 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305734 dev_err(hba->dev, "Memory allocation failed\n");
5735 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305736 }
5737
5738 /* Configure LRB */
5739 ufshcd_host_memory_configure(hba);
5740
5741 host->can_queue = hba->nutrs;
5742 host->cmd_per_lun = hba->nutrs;
5743 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005744 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305745 host->max_channel = UFSHCD_MAX_CHANNEL;
5746 host->unique_id = host->host_no;
5747 host->max_cmd_len = MAX_CDB_SIZE;
5748
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005749 hba->max_pwr_info.is_valid = false;
5750
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305751 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305752 init_waitqueue_head(&hba->tm_wq);
5753 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305754
5755 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305756 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305757 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305758
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305759 /* Initialize UIC command mutex */
5760 mutex_init(&hba->uic_cmd_mutex);
5761
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305762 /* Initialize mutex for device management commands */
5763 mutex_init(&hba->dev_cmd.lock);
5764
5765 /* Initialize device management tag acquire wait queue */
5766 init_waitqueue_head(&hba->dev_cmd.tag_wq);
5767
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005768 ufshcd_init_clk_gating(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305769 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09005770 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305771 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305772 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005773 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005774 } else {
5775 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305776 }
5777
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305778 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305779 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305780 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005781 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305782 }
5783
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305784 /* Host controller enable */
5785 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305786 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305787 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305788 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305789 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305790
Sahitya Tummala856b3482014-09-25 15:32:34 +03005791 if (ufshcd_is_clkscaling_enabled(hba)) {
5792 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
5793 "simple_ondemand", NULL);
5794 if (IS_ERR(hba->devfreq)) {
5795 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
5796 PTR_ERR(hba->devfreq));
5797 goto out_remove_scsi_host;
5798 }
5799 /* Suspend devfreq until the UFS device is detected */
5800 devfreq_suspend_device(hba->devfreq);
5801 hba->clk_scaling.window_start_t = 0;
5802 }
5803
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305804 /* Hold auto suspend until async scan completes */
5805 pm_runtime_get_sync(dev);
5806
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005807 /*
5808 * The device-initialize-sequence hasn't been invoked yet.
5809 * Set the device to power-off state
5810 */
5811 ufshcd_set_ufs_dev_poweroff(hba);
5812
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305813 async_schedule(ufshcd_async_scan, hba);
5814
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305815 return 0;
5816
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305817out_remove_scsi_host:
5818 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005819exit_gating:
5820 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305821out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005822 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305823 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005824 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305825out_error:
5826 return err;
5827}
5828EXPORT_SYMBOL_GPL(ufshcd_init);
5829
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305830MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
5831MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05305832MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305833MODULE_LICENSE("GPL");
5834MODULE_VERSION(UFSHCD_DRIVER_VERSION);