blob: e96ab253e3e5bd2618954b26b70a719e5603db08 [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
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03006 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05307 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05308 * Authors:
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053016 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053018 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053024 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
34 * this program.
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +030035 *
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053038 */
39
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053040#include <linux/async.h>
Sahitya Tummala856b3482014-09-25 15:32:34 +030041#include <linux/devfreq.h>
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053042
Vinayak Holikattie0eca632013-02-25 21:44:33 +053043#include "ufshcd.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053044#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053045
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053046#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
47 UTP_TASK_REQ_COMPL |\
48 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053049/* UIC command timeout, unit: ms */
50#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053051
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053052/* NOP OUT retries waiting for NOP IN response */
53#define NOP_OUT_RETRIES 10
54/* Timeout after 30 msecs if NOP OUT hangs without response */
55#define NOP_OUT_TIMEOUT 30 /* msecs */
56
Dolev Raviv68078d52013-07-30 00:35:58 +053057/* Query request retries */
58#define QUERY_REQ_RETRIES 10
59/* Query request timeout */
60#define QUERY_REQ_TIMEOUT 30 /* msec */
61
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053062/* Task management command timeout */
63#define TM_CMD_TIMEOUT 100 /* msecs */
64
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +030065/* maximum number of link-startup retries */
66#define DME_LINKSTARTUP_RETRIES 3
67
68/* maximum number of reset retries before giving up */
69#define MAX_HOST_RESET_RETRIES 5
70
Dolev Raviv68078d52013-07-30 00:35:58 +053071/* Expose the flag value from utp_upiu_query.value */
72#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
73
Seungwon Jeon7d568652013-08-31 21:40:20 +053074/* Interrupt aggregation default timeout, unit: 40us */
75#define INT_AGGR_DEF_TO 0x02
76
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +030077#define ufshcd_toggle_vreg(_dev, _vreg, _on) \
78 ({ \
79 int _ret; \
80 if (_on) \
81 _ret = ufshcd_enable_vreg(_dev, _vreg); \
82 else \
83 _ret = ufshcd_disable_vreg(_dev, _vreg); \
84 _ret; \
85 })
86
Subhash Jadavanida461ce2014-09-25 15:32:25 +030087static u32 ufs_query_desc_max_size[] = {
88 QUERY_DESC_DEVICE_MAX_SIZE,
89 QUERY_DESC_CONFIGURAION_MAX_SIZE,
90 QUERY_DESC_UNIT_MAX_SIZE,
91 QUERY_DESC_RFU_MAX_SIZE,
92 QUERY_DESC_INTERCONNECT_MAX_SIZE,
93 QUERY_DESC_STRING_MAX_SIZE,
94 QUERY_DESC_RFU_MAX_SIZE,
95 QUERY_DESC_GEOMETRY_MAZ_SIZE,
96 QUERY_DESC_POWER_MAX_SIZE,
97 QUERY_DESC_RFU_MAX_SIZE,
98};
99
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530100enum {
101 UFSHCD_MAX_CHANNEL = 0,
102 UFSHCD_MAX_ID = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530103 UFSHCD_CMD_PER_LUN = 32,
104 UFSHCD_CAN_QUEUE = 32,
105};
106
107/* UFSHCD states */
108enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530109 UFSHCD_STATE_RESET,
110 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530111 UFSHCD_STATE_OPERATIONAL,
112};
113
114/* UFSHCD error handling flags */
115enum {
116 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530117};
118
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530119/* UFSHCD UIC layer error flags */
120enum {
121 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
122 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
123 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
124 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
125};
126
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530127/* Interrupt configuration options */
128enum {
129 UFSHCD_INT_DISABLE,
130 UFSHCD_INT_ENABLE,
131 UFSHCD_INT_CLEAR,
132};
133
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530134#define ufshcd_set_eh_in_progress(h) \
135 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
136#define ufshcd_eh_in_progress(h) \
137 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
138#define ufshcd_clear_eh_in_progress(h) \
139 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
140
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300141#define ufshcd_set_ufs_dev_active(h) \
142 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
143#define ufshcd_set_ufs_dev_sleep(h) \
144 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
145#define ufshcd_set_ufs_dev_poweroff(h) \
146 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
147#define ufshcd_is_ufs_dev_active(h) \
148 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
149#define ufshcd_is_ufs_dev_sleep(h) \
150 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
151#define ufshcd_is_ufs_dev_poweroff(h) \
152 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
153
154static struct ufs_pm_lvl_states ufs_pm_lvl_states[] = {
155 {UFS_ACTIVE_PWR_MODE, UIC_LINK_ACTIVE_STATE},
156 {UFS_ACTIVE_PWR_MODE, UIC_LINK_HIBERN8_STATE},
157 {UFS_SLEEP_PWR_MODE, UIC_LINK_ACTIVE_STATE},
158 {UFS_SLEEP_PWR_MODE, UIC_LINK_HIBERN8_STATE},
159 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_HIBERN8_STATE},
160 {UFS_POWERDOWN_PWR_MODE, UIC_LINK_OFF_STATE},
161};
162
163static inline enum ufs_dev_pwr_mode
164ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl)
165{
166 return ufs_pm_lvl_states[lvl].dev_state;
167}
168
169static inline enum uic_link_state
170ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl)
171{
172 return ufs_pm_lvl_states[lvl].link_state;
173}
174
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530175static void ufshcd_tmc_handler(struct ufs_hba *hba);
176static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530177static int ufshcd_reset_and_restore(struct ufs_hba *hba);
178static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300179static void ufshcd_hba_exit(struct ufs_hba *hba);
180static int ufshcd_probe_hba(struct ufs_hba *hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300181static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
182 bool skip_ref_clk);
183static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
184static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);
185static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300186static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
187static irqreturn_t ufshcd_intr(int irq, void *__hba);
Dolev Raviv7eb584d2014-09-25 15:32:31 +0300188static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
189 struct ufs_pa_layer_attr *desired_pwr_mode);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300190
191static inline int ufshcd_enable_irq(struct ufs_hba *hba)
192{
193 int ret = 0;
194
195 if (!hba->is_irq_enabled) {
196 ret = request_irq(hba->irq, ufshcd_intr, IRQF_SHARED, UFSHCD,
197 hba);
198 if (ret)
199 dev_err(hba->dev, "%s: request_irq failed, ret=%d\n",
200 __func__, ret);
201 hba->is_irq_enabled = true;
202 }
203
204 return ret;
205}
206
207static inline void ufshcd_disable_irq(struct ufs_hba *hba)
208{
209 if (hba->is_irq_enabled) {
210 free_irq(hba->irq, hba);
211 hba->is_irq_enabled = false;
212 }
213}
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530214
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530215/*
216 * ufshcd_wait_for_register - wait for register value to change
217 * @hba - per-adapter interface
218 * @reg - mmio register offset
219 * @mask - mask to apply to read register value
220 * @val - wait condition
221 * @interval_us - polling interval in microsecs
222 * @timeout_ms - timeout in millisecs
223 *
224 * Returns -ETIMEDOUT on error, zero on success
225 */
226static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
227 u32 val, unsigned long interval_us, unsigned long timeout_ms)
228{
229 int err = 0;
230 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
231
232 /* ignore bits that we don't intend to wait on */
233 val = val & mask;
234
235 while ((ufshcd_readl(hba, reg) & mask) != val) {
236 /* wakeup within 50us of expiry */
237 usleep_range(interval_us, interval_us + 50);
238
239 if (time_after(jiffies, timeout)) {
240 if ((ufshcd_readl(hba, reg) & mask) != val)
241 err = -ETIMEDOUT;
242 break;
243 }
244 }
245
246 return err;
247}
248
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530249/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530250 * ufshcd_get_intr_mask - Get the interrupt bit mask
251 * @hba - Pointer to adapter instance
252 *
253 * Returns interrupt bit mask per version
254 */
255static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
256{
257 if (hba->ufs_version == UFSHCI_VERSION_10)
258 return INTERRUPT_MASK_ALL_VER_10;
259 else
260 return INTERRUPT_MASK_ALL_VER_11;
261}
262
263/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530264 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
265 * @hba - Pointer to adapter instance
266 *
267 * Returns UFSHCI version supported by the controller
268 */
269static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
270{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530271 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530272}
273
274/**
275 * ufshcd_is_device_present - Check if any device connected to
276 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300277 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530278 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530279 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530280 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300281static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530282{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300283 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
284 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530285}
286
287/**
288 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
289 * @lrb: pointer to local command reference block
290 *
291 * This function is used to get the OCS field from UTRD
292 * Returns the OCS field in the UTRD
293 */
294static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
295{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530296 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530297}
298
299/**
300 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
301 * @task_req_descp: pointer to utp_task_req_desc structure
302 *
303 * This function is used to get the OCS field from UTMRD
304 * Returns the OCS field in the UTMRD
305 */
306static inline int
307ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
308{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530309 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530310}
311
312/**
313 * ufshcd_get_tm_free_slot - get a free slot for task management request
314 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530315 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530316 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530317 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
318 * Returns 0 if free slot is not available, else return 1 with tag value
319 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530320 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530321static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530322{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530323 int tag;
324 bool ret = false;
325
326 if (!free_slot)
327 goto out;
328
329 do {
330 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
331 if (tag >= hba->nutmrs)
332 goto out;
333 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
334
335 *free_slot = tag;
336 ret = true;
337out:
338 return ret;
339}
340
341static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
342{
343 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530344}
345
346/**
347 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
348 * @hba: per adapter instance
349 * @pos: position of the bit to be cleared
350 */
351static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
352{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530353 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530354}
355
356/**
357 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
358 * @reg: Register value of host controller status
359 *
360 * Returns integer, 0 on Success and positive value if failed
361 */
362static inline int ufshcd_get_lists_status(u32 reg)
363{
364 /*
365 * The mask 0xFF is for the following HCS register bits
366 * Bit Description
367 * 0 Device Present
368 * 1 UTRLRDY
369 * 2 UTMRLRDY
370 * 3 UCRDY
371 * 4 HEI
372 * 5 DEI
373 * 6-7 reserved
374 */
375 return (((reg) & (0xFF)) >> 1) ^ (0x07);
376}
377
378/**
379 * ufshcd_get_uic_cmd_result - Get the UIC command result
380 * @hba: Pointer to adapter instance
381 *
382 * This function gets the result of UIC command completion
383 * Returns 0 on success, non zero value on error
384 */
385static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
386{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530387 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530388 MASK_UIC_COMMAND_RESULT;
389}
390
391/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530392 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
393 * @hba: Pointer to adapter instance
394 *
395 * This function gets UIC command argument3
396 * Returns 0 on success, non zero value on error
397 */
398static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
399{
400 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
401}
402
403/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530404 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530405 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530406 */
407static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530408ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530409{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530410 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530411}
412
413/**
414 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
415 * @ucd_rsp_ptr: pointer to response UPIU
416 *
417 * This function gets the response status and scsi_status from response UPIU
418 * Returns the response result code.
419 */
420static inline int
421ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
422{
423 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
424}
425
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530426/*
427 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
428 * from response UPIU
429 * @ucd_rsp_ptr: pointer to response UPIU
430 *
431 * Return the data segment length.
432 */
433static inline unsigned int
434ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
435{
436 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
437 MASK_RSP_UPIU_DATA_SEG_LEN;
438}
439
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530440/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530441 * ufshcd_is_exception_event - Check if the device raised an exception event
442 * @ucd_rsp_ptr: pointer to response UPIU
443 *
444 * The function checks if the device raised an exception event indicated in
445 * the Device Information field of response UPIU.
446 *
447 * Returns true if exception is raised, false otherwise.
448 */
449static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
450{
451 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
452 MASK_RSP_EXCEPTION_EVENT ? true : false;
453}
454
455/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530456 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530457 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530458 */
459static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530460ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530461{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530462 ufshcd_writel(hba, INT_AGGR_ENABLE |
463 INT_AGGR_COUNTER_AND_TIMER_RESET,
464 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
465}
466
467/**
468 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
469 * @hba: per adapter instance
470 * @cnt: Interrupt aggregation counter threshold
471 * @tmout: Interrupt aggregation timeout value
472 */
473static inline void
474ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
475{
476 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
477 INT_AGGR_COUNTER_THLD_VAL(cnt) |
478 INT_AGGR_TIMEOUT_VAL(tmout),
479 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530480}
481
482/**
483 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
484 * When run-stop registers are set to 1, it indicates the
485 * host controller that it can process the requests
486 * @hba: per adapter instance
487 */
488static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
489{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530490 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
491 REG_UTP_TASK_REQ_LIST_RUN_STOP);
492 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
493 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530494}
495
496/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530497 * ufshcd_hba_start - Start controller initialization sequence
498 * @hba: per adapter instance
499 */
500static inline void ufshcd_hba_start(struct ufs_hba *hba)
501{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530502 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530503}
504
505/**
506 * ufshcd_is_hba_active - Get controller state
507 * @hba: per adapter instance
508 *
509 * Returns zero if controller is active, 1 otherwise
510 */
511static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
512{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530513 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530514}
515
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300516static void ufshcd_ungate_work(struct work_struct *work)
517{
518 int ret;
519 unsigned long flags;
520 struct ufs_hba *hba = container_of(work, struct ufs_hba,
521 clk_gating.ungate_work);
522
523 cancel_delayed_work_sync(&hba->clk_gating.gate_work);
524
525 spin_lock_irqsave(hba->host->host_lock, flags);
526 if (hba->clk_gating.state == CLKS_ON) {
527 spin_unlock_irqrestore(hba->host->host_lock, flags);
528 goto unblock_reqs;
529 }
530
531 spin_unlock_irqrestore(hba->host->host_lock, flags);
532 ufshcd_setup_clocks(hba, true);
533
534 /* Exit from hibern8 */
535 if (ufshcd_can_hibern8_during_gating(hba)) {
536 /* Prevent gating in this path */
537 hba->clk_gating.is_suspended = true;
538 if (ufshcd_is_link_hibern8(hba)) {
539 ret = ufshcd_uic_hibern8_exit(hba);
540 if (ret)
541 dev_err(hba->dev, "%s: hibern8 exit failed %d\n",
542 __func__, ret);
543 else
544 ufshcd_set_link_active(hba);
545 }
546 hba->clk_gating.is_suspended = false;
547 }
548unblock_reqs:
Sahitya Tummala856b3482014-09-25 15:32:34 +0300549 if (ufshcd_is_clkscaling_enabled(hba))
550 devfreq_resume_device(hba->devfreq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300551 scsi_unblock_requests(hba->host);
552}
553
554/**
555 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
556 * Also, exit from hibern8 mode and set the link as active.
557 * @hba: per adapter instance
558 * @async: This indicates whether caller should ungate clocks asynchronously.
559 */
560int ufshcd_hold(struct ufs_hba *hba, bool async)
561{
562 int rc = 0;
563 unsigned long flags;
564
565 if (!ufshcd_is_clkgating_allowed(hba))
566 goto out;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300567 spin_lock_irqsave(hba->host->host_lock, flags);
568 hba->clk_gating.active_reqs++;
569
Sahitya Tummala856b3482014-09-25 15:32:34 +0300570start:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300571 switch (hba->clk_gating.state) {
572 case CLKS_ON:
573 break;
574 case REQ_CLKS_OFF:
575 if (cancel_delayed_work(&hba->clk_gating.gate_work)) {
576 hba->clk_gating.state = CLKS_ON;
577 break;
578 }
579 /*
580 * If we here, it means gating work is either done or
581 * currently running. Hence, fall through to cancel gating
582 * work and to enable clocks.
583 */
584 case CLKS_OFF:
585 scsi_block_requests(hba->host);
586 hba->clk_gating.state = REQ_CLKS_ON;
587 schedule_work(&hba->clk_gating.ungate_work);
588 /*
589 * fall through to check if we should wait for this
590 * work to be done or not.
591 */
592 case REQ_CLKS_ON:
593 if (async) {
594 rc = -EAGAIN;
595 hba->clk_gating.active_reqs--;
596 break;
597 }
598
599 spin_unlock_irqrestore(hba->host->host_lock, flags);
600 flush_work(&hba->clk_gating.ungate_work);
601 /* Make sure state is CLKS_ON before returning */
Sahitya Tummala856b3482014-09-25 15:32:34 +0300602 spin_lock_irqsave(hba->host->host_lock, flags);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300603 goto start;
604 default:
605 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n",
606 __func__, hba->clk_gating.state);
607 break;
608 }
609 spin_unlock_irqrestore(hba->host->host_lock, flags);
610out:
611 return rc;
612}
613
614static void ufshcd_gate_work(struct work_struct *work)
615{
616 struct ufs_hba *hba = container_of(work, struct ufs_hba,
617 clk_gating.gate_work.work);
618 unsigned long flags;
619
620 spin_lock_irqsave(hba->host->host_lock, flags);
621 if (hba->clk_gating.is_suspended) {
622 hba->clk_gating.state = CLKS_ON;
623 goto rel_lock;
624 }
625
626 if (hba->clk_gating.active_reqs
627 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
628 || hba->lrb_in_use || hba->outstanding_tasks
629 || hba->active_uic_cmd || hba->uic_async_done)
630 goto rel_lock;
631
632 spin_unlock_irqrestore(hba->host->host_lock, flags);
633
634 /* put the link into hibern8 mode before turning off clocks */
635 if (ufshcd_can_hibern8_during_gating(hba)) {
636 if (ufshcd_uic_hibern8_enter(hba)) {
637 hba->clk_gating.state = CLKS_ON;
638 goto out;
639 }
640 ufshcd_set_link_hibern8(hba);
641 }
642
Sahitya Tummala856b3482014-09-25 15:32:34 +0300643 if (ufshcd_is_clkscaling_enabled(hba)) {
644 devfreq_suspend_device(hba->devfreq);
645 hba->clk_scaling.window_start_t = 0;
646 }
647
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300648 if (!ufshcd_is_link_active(hba))
649 ufshcd_setup_clocks(hba, false);
650 else
651 /* If link is active, device ref_clk can't be switched off */
652 __ufshcd_setup_clocks(hba, false, true);
653
654 /*
655 * In case you are here to cancel this work the gating state
656 * would be marked as REQ_CLKS_ON. In this case keep the state
657 * as REQ_CLKS_ON which would anyway imply that clocks are off
658 * and a request to turn them on is pending. By doing this way,
659 * we keep the state machine in tact and this would ultimately
660 * prevent from doing cancel work multiple times when there are
661 * new requests arriving before the current cancel work is done.
662 */
663 spin_lock_irqsave(hba->host->host_lock, flags);
664 if (hba->clk_gating.state == REQ_CLKS_OFF)
665 hba->clk_gating.state = CLKS_OFF;
666
667rel_lock:
668 spin_unlock_irqrestore(hba->host->host_lock, flags);
669out:
670 return;
671}
672
673/* host lock must be held before calling this variant */
674static void __ufshcd_release(struct ufs_hba *hba)
675{
676 if (!ufshcd_is_clkgating_allowed(hba))
677 return;
678
679 hba->clk_gating.active_reqs--;
680
681 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended
682 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL
683 || hba->lrb_in_use || hba->outstanding_tasks
684 || hba->active_uic_cmd || hba->uic_async_done)
685 return;
686
687 hba->clk_gating.state = REQ_CLKS_OFF;
688 schedule_delayed_work(&hba->clk_gating.gate_work,
689 msecs_to_jiffies(hba->clk_gating.delay_ms));
690}
691
692void ufshcd_release(struct ufs_hba *hba)
693{
694 unsigned long flags;
695
696 spin_lock_irqsave(hba->host->host_lock, flags);
697 __ufshcd_release(hba);
698 spin_unlock_irqrestore(hba->host->host_lock, flags);
699}
700
701static ssize_t ufshcd_clkgate_delay_show(struct device *dev,
702 struct device_attribute *attr, char *buf)
703{
704 struct ufs_hba *hba = dev_get_drvdata(dev);
705
706 return snprintf(buf, PAGE_SIZE, "%lu\n", hba->clk_gating.delay_ms);
707}
708
709static ssize_t ufshcd_clkgate_delay_store(struct device *dev,
710 struct device_attribute *attr, const char *buf, size_t count)
711{
712 struct ufs_hba *hba = dev_get_drvdata(dev);
713 unsigned long flags, value;
714
715 if (kstrtoul(buf, 0, &value))
716 return -EINVAL;
717
718 spin_lock_irqsave(hba->host->host_lock, flags);
719 hba->clk_gating.delay_ms = value;
720 spin_unlock_irqrestore(hba->host->host_lock, flags);
721 return count;
722}
723
724static void ufshcd_init_clk_gating(struct ufs_hba *hba)
725{
726 if (!ufshcd_is_clkgating_allowed(hba))
727 return;
728
729 hba->clk_gating.delay_ms = 150;
730 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work);
731 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work);
732
733 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show;
734 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store;
735 sysfs_attr_init(&hba->clk_gating.delay_attr.attr);
736 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms";
737 hba->clk_gating.delay_attr.attr.mode = S_IRUGO | S_IWUSR;
738 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr))
739 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n");
740}
741
742static void ufshcd_exit_clk_gating(struct ufs_hba *hba)
743{
744 if (!ufshcd_is_clkgating_allowed(hba))
745 return;
746 device_remove_file(hba->dev, &hba->clk_gating.delay_attr);
747}
748
Sahitya Tummala856b3482014-09-25 15:32:34 +0300749/* Must be called with host lock acquired */
750static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba)
751{
752 if (!ufshcd_is_clkscaling_enabled(hba))
753 return;
754
755 if (!hba->clk_scaling.is_busy_started) {
756 hba->clk_scaling.busy_start_t = ktime_get();
757 hba->clk_scaling.is_busy_started = true;
758 }
759}
760
761static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba)
762{
763 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
764
765 if (!ufshcd_is_clkscaling_enabled(hba))
766 return;
767
768 if (!hba->outstanding_reqs && scaling->is_busy_started) {
769 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
770 scaling->busy_start_t));
771 scaling->busy_start_t = ktime_set(0, 0);
772 scaling->is_busy_started = false;
773 }
774}
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530775/**
776 * ufshcd_send_command - Send SCSI or device management commands
777 * @hba: per adapter instance
778 * @task_tag: Task tag of the command
779 */
780static inline
781void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
782{
Sahitya Tummala856b3482014-09-25 15:32:34 +0300783 ufshcd_clk_scaling_start_busy(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530784 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530785 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530786}
787
788/**
789 * ufshcd_copy_sense_data - Copy sense data in case of check condition
790 * @lrb - pointer to local reference block
791 */
792static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
793{
794 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530795 if (lrbp->sense_buffer &&
796 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530797 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530798 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530799 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530800 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
801 }
802}
803
804/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530805 * ufshcd_copy_query_response() - Copy the Query Response and the data
806 * descriptor
807 * @hba: per adapter instance
808 * @lrb - pointer to local reference block
809 */
810static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300811int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530812{
813 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
814
Dolev Raviv68078d52013-07-30 00:35:58 +0530815 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530816
Dolev Raviv68078d52013-07-30 00:35:58 +0530817 /* Get the descriptor */
818 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300819 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530820 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300821 u16 resp_len;
822 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530823
824 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300825 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530826 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300827 buf_len = be16_to_cpu(
828 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300829 if (likely(buf_len >= resp_len)) {
830 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
831 } else {
832 dev_warn(hba->dev,
833 "%s: Response size is bigger than buffer",
834 __func__);
835 return -EINVAL;
836 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530837 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300838
839 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530840}
841
842/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530843 * ufshcd_hba_capabilities - Read controller capabilities
844 * @hba: per adapter instance
845 */
846static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
847{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530848 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530849
850 /* nutrs and nutmrs are 0 based values */
851 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
852 hba->nutmrs =
853 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
854}
855
856/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530857 * ufshcd_ready_for_uic_cmd - Check if controller is ready
858 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530859 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530860 * Return true on success, else false
861 */
862static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
863{
864 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
865 return true;
866 else
867 return false;
868}
869
870/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530871 * ufshcd_get_upmcrs - Get the power mode change request status
872 * @hba: Pointer to adapter instance
873 *
874 * This function gets the UPMCRS field of HCS register
875 * Returns value of UPMCRS field
876 */
877static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
878{
879 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
880}
881
882/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530883 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
884 * @hba: per adapter instance
885 * @uic_cmd: UIC command
886 *
887 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530888 */
889static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530890ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530891{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530892 WARN_ON(hba->active_uic_cmd);
893
894 hba->active_uic_cmd = uic_cmd;
895
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530896 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530897 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
898 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
899 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530900
901 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530902 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530903 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530904}
905
906/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530907 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
908 * @hba: per adapter instance
909 * @uic_command: UIC command
910 *
911 * Must be called with mutex held.
912 * Returns 0 only if success.
913 */
914static int
915ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
916{
917 int ret;
918 unsigned long flags;
919
920 if (wait_for_completion_timeout(&uic_cmd->done,
921 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
922 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
923 else
924 ret = -ETIMEDOUT;
925
926 spin_lock_irqsave(hba->host->host_lock, flags);
927 hba->active_uic_cmd = NULL;
928 spin_unlock_irqrestore(hba->host->host_lock, flags);
929
930 return ret;
931}
932
933/**
934 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
935 * @hba: per adapter instance
936 * @uic_cmd: UIC command
937 *
938 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300939 * with mutex held and host_lock locked.
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530940 * Returns 0 only if success.
941 */
942static int
943__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
944{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530945 if (!ufshcd_ready_for_uic_cmd(hba)) {
946 dev_err(hba->dev,
947 "Controller not ready to accept UIC commands\n");
948 return -EIO;
949 }
950
951 init_completion(&uic_cmd->done);
952
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530953 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530954
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300955 return 0;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530956}
957
958/**
959 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
960 * @hba: per adapter instance
961 * @uic_cmd: UIC command
962 *
963 * Returns 0 only if success.
964 */
965static int
966ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
967{
968 int ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300969 unsigned long flags;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530970
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300971 ufshcd_hold(hba, false);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530972 mutex_lock(&hba->uic_cmd_mutex);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300973 spin_lock_irqsave(hba->host->host_lock, flags);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530974 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
Subhash Jadavani57d104c2014-09-25 15:32:30 +0300975 spin_unlock_irqrestore(hba->host->host_lock, flags);
976 if (!ret)
977 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
978
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530979 mutex_unlock(&hba->uic_cmd_mutex);
980
Sahitya Tummala1ab27c92014-09-25 15:32:32 +0300981 ufshcd_release(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530982 return ret;
983}
984
985/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530986 * ufshcd_map_sg - Map scatter-gather list to prdt
987 * @lrbp - pointer to local reference block
988 *
989 * Returns 0 in case of success, non-zero value in case of failure
990 */
991static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
992{
993 struct ufshcd_sg_entry *prd_table;
994 struct scatterlist *sg;
995 struct scsi_cmnd *cmd;
996 int sg_segments;
997 int i;
998
999 cmd = lrbp->cmd;
1000 sg_segments = scsi_dma_map(cmd);
1001 if (sg_segments < 0)
1002 return sg_segments;
1003
1004 if (sg_segments) {
1005 lrbp->utr_descriptor_ptr->prd_table_length =
1006 cpu_to_le16((u16) (sg_segments));
1007
1008 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
1009
1010 scsi_for_each_sg(cmd, sg, sg_segments, i) {
1011 prd_table[i].size =
1012 cpu_to_le32(((u32) sg_dma_len(sg))-1);
1013 prd_table[i].base_addr =
1014 cpu_to_le32(lower_32_bits(sg->dma_address));
1015 prd_table[i].upper_addr =
1016 cpu_to_le32(upper_32_bits(sg->dma_address));
1017 }
1018 } else {
1019 lrbp->utr_descriptor_ptr->prd_table_length = 0;
1020 }
1021
1022 return 0;
1023}
1024
1025/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301026 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301027 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301028 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301029 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301030static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301031{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301032 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1033
1034 if (hba->ufs_version == UFSHCI_VERSION_10) {
1035 u32 rw;
1036 rw = set & INTERRUPT_MASK_RW_VER_10;
1037 set = rw | ((set ^ intrs) & intrs);
1038 } else {
1039 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301040 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05301041
1042 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
1043}
1044
1045/**
1046 * ufshcd_disable_intr - disable interrupts
1047 * @hba: per adapter instance
1048 * @intrs: interrupt bits
1049 */
1050static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
1051{
1052 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
1053
1054 if (hba->ufs_version == UFSHCI_VERSION_10) {
1055 u32 rw;
1056 rw = (set & INTERRUPT_MASK_RW_VER_10) &
1057 ~(intrs & INTERRUPT_MASK_RW_VER_10);
1058 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
1059
1060 } else {
1061 set &= ~intrs;
1062 }
1063
1064 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301065}
1066
1067/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301068 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
1069 * descriptor according to request
1070 * @lrbp: pointer to local reference block
1071 * @upiu_flags: flags required in the header
1072 * @cmd_dir: requests data direction
1073 */
1074static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
1075 u32 *upiu_flags, enum dma_data_direction cmd_dir)
1076{
1077 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
1078 u32 data_direction;
1079 u32 dword_0;
1080
1081 if (cmd_dir == DMA_FROM_DEVICE) {
1082 data_direction = UTP_DEVICE_TO_HOST;
1083 *upiu_flags = UPIU_CMD_FLAGS_READ;
1084 } else if (cmd_dir == DMA_TO_DEVICE) {
1085 data_direction = UTP_HOST_TO_DEVICE;
1086 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
1087 } else {
1088 data_direction = UTP_NO_DATA_TRANSFER;
1089 *upiu_flags = UPIU_CMD_FLAGS_NONE;
1090 }
1091
1092 dword_0 = data_direction | (lrbp->command_type
1093 << UPIU_COMMAND_TYPE_OFFSET);
1094 if (lrbp->intr_cmd)
1095 dword_0 |= UTP_REQ_DESC_INT_CMD;
1096
1097 /* Transfer request descriptor header fields */
1098 req_desc->header.dword_0 = cpu_to_le32(dword_0);
1099
1100 /*
1101 * assigning invalid value for command status. Controller
1102 * updates OCS on command completion, with the command
1103 * status
1104 */
1105 req_desc->header.dword_2 =
1106 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
1107}
1108
1109/**
1110 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
1111 * for scsi commands
1112 * @lrbp - local reference block pointer
1113 * @upiu_flags - flags
1114 */
1115static
1116void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
1117{
1118 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1119
1120 /* command descriptor fields */
1121 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1122 UPIU_TRANSACTION_COMMAND, upiu_flags,
1123 lrbp->lun, lrbp->task_tag);
1124 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1125 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
1126
1127 /* Total EHS length and Data segment length will be zero */
1128 ucd_req_ptr->header.dword_2 = 0;
1129
1130 ucd_req_ptr->sc.exp_data_transfer_len =
1131 cpu_to_be32(lrbp->cmd->sdb.length);
1132
1133 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd,
1134 (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
1135}
1136
Dolev Raviv68078d52013-07-30 00:35:58 +05301137/**
1138 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
1139 * for query requsts
1140 * @hba: UFS hba
1141 * @lrbp: local reference block pointer
1142 * @upiu_flags: flags
1143 */
1144static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
1145 struct ufshcd_lrb *lrbp, u32 upiu_flags)
1146{
1147 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1148 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301149 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +05301150 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
1151
1152 /* Query request header */
1153 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
1154 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
1155 lrbp->lun, lrbp->task_tag);
1156 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
1157 0, query->request.query_func, 0, 0);
1158
1159 /* Data segment length */
1160 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
1161 0, 0, len >> 8, (u8)len);
1162
1163 /* Copy the Query Request buffer as is */
1164 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
1165 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301166
1167 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001168 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
1169 memcpy(descp, query->descriptor, len);
1170
Dolev Raviv68078d52013-07-30 00:35:58 +05301171}
1172
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301173static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
1174{
1175 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
1176
1177 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
1178
1179 /* command descriptor fields */
1180 ucd_req_ptr->header.dword_0 =
1181 UPIU_HEADER_DWORD(
1182 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
1183}
1184
1185/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301186 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301187 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301188 * @lrb - pointer to local reference block
1189 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301190static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301191{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301192 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301193 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301194
1195 switch (lrbp->command_type) {
1196 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301197 if (likely(lrbp->cmd)) {
1198 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
1199 lrbp->cmd->sc_data_direction);
1200 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301201 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301202 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301203 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301204 break;
1205 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301206 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +05301207 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
1208 ufshcd_prepare_utp_query_req_upiu(
1209 hba, lrbp, upiu_flags);
1210 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301211 ufshcd_prepare_utp_nop_upiu(lrbp);
1212 else
1213 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301214 break;
1215 case UTP_CMD_TYPE_UFS:
1216 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301217 ret = -ENOTSUPP;
1218 dev_err(hba->dev, "%s: UFS native command are not supported\n",
1219 __func__);
1220 break;
1221 default:
1222 ret = -ENOTSUPP;
1223 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
1224 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301225 break;
1226 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301227
1228 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301229}
1230
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001231/*
1232 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
1233 * @scsi_lun: scsi LUN id
1234 *
1235 * Returns UPIU LUN id
1236 */
1237static inline u8 ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun)
1238{
1239 if (scsi_is_wlun(scsi_lun))
1240 return (scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID)
1241 | UFS_UPIU_WLUN_ID;
1242 else
1243 return scsi_lun & UFS_UPIU_MAX_UNIT_NUM_ID;
1244}
1245
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301246/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03001247 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
1248 * @scsi_lun: UPIU W-LUN id
1249 *
1250 * Returns SCSI W-LUN id
1251 */
1252static inline u16 ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id)
1253{
1254 return (upiu_wlun_id & ~UFS_UPIU_WLUN_ID) | SCSI_W_LUN_BASE;
1255}
1256
1257/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301258 * ufshcd_queuecommand - main entry point for SCSI requests
1259 * @cmd: command from SCSI Midlayer
1260 * @done: call back function
1261 *
1262 * Returns 0 for success, non-zero in case of failure
1263 */
1264static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
1265{
1266 struct ufshcd_lrb *lrbp;
1267 struct ufs_hba *hba;
1268 unsigned long flags;
1269 int tag;
1270 int err = 0;
1271
1272 hba = shost_priv(host);
1273
1274 tag = cmd->request->tag;
1275
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301276 spin_lock_irqsave(hba->host->host_lock, flags);
1277 switch (hba->ufshcd_state) {
1278 case UFSHCD_STATE_OPERATIONAL:
1279 break;
1280 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301281 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301282 goto out_unlock;
1283 case UFSHCD_STATE_ERROR:
1284 set_host_byte(cmd, DID_ERROR);
1285 cmd->scsi_done(cmd);
1286 goto out_unlock;
1287 default:
1288 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
1289 __func__, hba->ufshcd_state);
1290 set_host_byte(cmd, DID_BAD_TARGET);
1291 cmd->scsi_done(cmd);
1292 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301293 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301294 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301295
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301296 /* acquire the tag to make sure device cmds don't use it */
1297 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
1298 /*
1299 * Dev manage command in progress, requeue the command.
1300 * Requeuing the command helps in cases where the request *may*
1301 * find different tag instead of waiting for dev manage command
1302 * completion.
1303 */
1304 err = SCSI_MLQUEUE_HOST_BUSY;
1305 goto out;
1306 }
1307
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001308 err = ufshcd_hold(hba, true);
1309 if (err) {
1310 err = SCSI_MLQUEUE_HOST_BUSY;
1311 clear_bit_unlock(tag, &hba->lrb_in_use);
1312 goto out;
1313 }
1314 WARN_ON(hba->clk_gating.state != CLKS_ON);
1315
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301316 lrbp = &hba->lrb[tag];
1317
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301318 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301319 lrbp->cmd = cmd;
1320 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
1321 lrbp->sense_buffer = cmd->sense_buffer;
1322 lrbp->task_tag = tag;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001323 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301324 lrbp->intr_cmd = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301325 lrbp->command_type = UTP_CMD_TYPE_SCSI;
1326
1327 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301328 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301329 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301330 if (err) {
1331 lrbp->cmd = NULL;
1332 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301333 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301334 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301335
1336 /* issue command to the controller */
1337 spin_lock_irqsave(hba->host->host_lock, flags);
1338 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05301339out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301340 spin_unlock_irqrestore(hba->host->host_lock, flags);
1341out:
1342 return err;
1343}
1344
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301345static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
1346 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
1347{
1348 lrbp->cmd = NULL;
1349 lrbp->sense_bufflen = 0;
1350 lrbp->sense_buffer = NULL;
1351 lrbp->task_tag = tag;
1352 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
1353 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
1354 lrbp->intr_cmd = true; /* No interrupt aggregation */
1355 hba->dev_cmd.type = cmd_type;
1356
1357 return ufshcd_compose_upiu(hba, lrbp);
1358}
1359
1360static int
1361ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1362{
1363 int err = 0;
1364 unsigned long flags;
1365 u32 mask = 1 << tag;
1366
1367 /* clear outstanding transaction before retry */
1368 spin_lock_irqsave(hba->host->host_lock, flags);
1369 ufshcd_utrl_clear(hba, tag);
1370 spin_unlock_irqrestore(hba->host->host_lock, flags);
1371
1372 /*
1373 * wait for for h/w to clear corresponding bit in door-bell.
1374 * max. wait is 1 sec.
1375 */
1376 err = ufshcd_wait_for_register(hba,
1377 REG_UTP_TRANSFER_REQ_DOOR_BELL,
1378 mask, ~mask, 1000, 1000);
1379
1380 return err;
1381}
1382
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001383static int
1384ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1385{
1386 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1387
1388 /* Get the UPIU response */
1389 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1390 UPIU_RSP_CODE_OFFSET;
1391 return query_res->response;
1392}
1393
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301394/**
1395 * ufshcd_dev_cmd_completion() - handles device management command responses
1396 * @hba: per adapter instance
1397 * @lrbp: pointer to local reference block
1398 */
1399static int
1400ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1401{
1402 int resp;
1403 int err = 0;
1404
1405 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1406
1407 switch (resp) {
1408 case UPIU_TRANSACTION_NOP_IN:
1409 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1410 err = -EINVAL;
1411 dev_err(hba->dev, "%s: unexpected response %x\n",
1412 __func__, resp);
1413 }
1414 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301415 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001416 err = ufshcd_check_query_response(hba, lrbp);
1417 if (!err)
1418 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301419 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301420 case UPIU_TRANSACTION_REJECT_UPIU:
1421 /* TODO: handle Reject UPIU Response */
1422 err = -EPERM;
1423 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1424 __func__);
1425 break;
1426 default:
1427 err = -EINVAL;
1428 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1429 __func__, resp);
1430 break;
1431 }
1432
1433 return err;
1434}
1435
1436static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1437 struct ufshcd_lrb *lrbp, int max_timeout)
1438{
1439 int err = 0;
1440 unsigned long time_left;
1441 unsigned long flags;
1442
1443 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1444 msecs_to_jiffies(max_timeout));
1445
1446 spin_lock_irqsave(hba->host->host_lock, flags);
1447 hba->dev_cmd.complete = NULL;
1448 if (likely(time_left)) {
1449 err = ufshcd_get_tr_ocs(lrbp);
1450 if (!err)
1451 err = ufshcd_dev_cmd_completion(hba, lrbp);
1452 }
1453 spin_unlock_irqrestore(hba->host->host_lock, flags);
1454
1455 if (!time_left) {
1456 err = -ETIMEDOUT;
1457 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
1458 /* sucessfully cleared the command, retry if needed */
1459 err = -EAGAIN;
1460 }
1461
1462 return err;
1463}
1464
1465/**
1466 * ufshcd_get_dev_cmd_tag - Get device management command tag
1467 * @hba: per-adapter instance
1468 * @tag: pointer to variable with available slot value
1469 *
1470 * Get a free slot and lock it until device management command
1471 * completes.
1472 *
1473 * Returns false if free slot is unavailable for locking, else
1474 * return true with tag value in @tag.
1475 */
1476static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1477{
1478 int tag;
1479 bool ret = false;
1480 unsigned long tmp;
1481
1482 if (!tag_out)
1483 goto out;
1484
1485 do {
1486 tmp = ~hba->lrb_in_use;
1487 tag = find_last_bit(&tmp, hba->nutrs);
1488 if (tag >= hba->nutrs)
1489 goto out;
1490 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1491
1492 *tag_out = tag;
1493 ret = true;
1494out:
1495 return ret;
1496}
1497
1498static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1499{
1500 clear_bit_unlock(tag, &hba->lrb_in_use);
1501}
1502
1503/**
1504 * ufshcd_exec_dev_cmd - API for sending device management requests
1505 * @hba - UFS hba
1506 * @cmd_type - specifies the type (NOP, Query...)
1507 * @timeout - time in seconds
1508 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301509 * NOTE: Since there is only one available tag for device management commands,
1510 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301511 */
1512static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1513 enum dev_cmd_type cmd_type, int timeout)
1514{
1515 struct ufshcd_lrb *lrbp;
1516 int err;
1517 int tag;
1518 struct completion wait;
1519 unsigned long flags;
1520
1521 /*
1522 * Get free slot, sleep if slots are unavailable.
1523 * Even though we use wait_event() which sleeps indefinitely,
1524 * the maximum wait time is bounded by SCSI request timeout.
1525 */
1526 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1527
1528 init_completion(&wait);
1529 lrbp = &hba->lrb[tag];
1530 WARN_ON(lrbp->cmd);
1531 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1532 if (unlikely(err))
1533 goto out_put_tag;
1534
1535 hba->dev_cmd.complete = &wait;
1536
1537 spin_lock_irqsave(hba->host->host_lock, flags);
1538 ufshcd_send_command(hba, tag);
1539 spin_unlock_irqrestore(hba->host->host_lock, flags);
1540
1541 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1542
1543out_put_tag:
1544 ufshcd_put_dev_cmd_tag(hba, tag);
1545 wake_up(&hba->dev_cmd.tag_wq);
1546 return err;
1547}
1548
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301549/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001550 * ufshcd_init_query() - init the query response and request parameters
1551 * @hba: per-adapter instance
1552 * @request: address of the request pointer to be initialized
1553 * @response: address of the response pointer to be initialized
1554 * @opcode: operation to perform
1555 * @idn: flag idn to access
1556 * @index: LU number to access
1557 * @selector: query/flag/descriptor further identification
1558 */
1559static inline void ufshcd_init_query(struct ufs_hba *hba,
1560 struct ufs_query_req **request, struct ufs_query_res **response,
1561 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1562{
1563 *request = &hba->dev_cmd.query.request;
1564 *response = &hba->dev_cmd.query.response;
1565 memset(*request, 0, sizeof(struct ufs_query_req));
1566 memset(*response, 0, sizeof(struct ufs_query_res));
1567 (*request)->upiu_req.opcode = opcode;
1568 (*request)->upiu_req.idn = idn;
1569 (*request)->upiu_req.index = index;
1570 (*request)->upiu_req.selector = selector;
1571}
1572
1573/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301574 * ufshcd_query_flag() - API function for sending flag query requests
1575 * hba: per-adapter instance
1576 * query_opcode: flag query to perform
1577 * idn: flag idn to access
1578 * flag_res: the flag value after the query request completes
1579 *
1580 * Returns 0 for success, non-zero in case of failure
1581 */
1582static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1583 enum flag_idn idn, bool *flag_res)
1584{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001585 struct ufs_query_req *request = NULL;
1586 struct ufs_query_res *response = NULL;
1587 int err, index = 0, selector = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301588
1589 BUG_ON(!hba);
1590
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001591 ufshcd_hold(hba, false);
Dolev Raviv68078d52013-07-30 00:35:58 +05301592 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001593 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1594 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301595
1596 switch (opcode) {
1597 case UPIU_QUERY_OPCODE_SET_FLAG:
1598 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1599 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1600 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1601 break;
1602 case UPIU_QUERY_OPCODE_READ_FLAG:
1603 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1604 if (!flag_res) {
1605 /* No dummy reads */
1606 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1607 __func__);
1608 err = -EINVAL;
1609 goto out_unlock;
1610 }
1611 break;
1612 default:
1613 dev_err(hba->dev,
1614 "%s: Expected query flag opcode but got = %d\n",
1615 __func__, opcode);
1616 err = -EINVAL;
1617 goto out_unlock;
1618 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301619
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001620 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Dolev Raviv68078d52013-07-30 00:35:58 +05301621
1622 if (err) {
1623 dev_err(hba->dev,
1624 "%s: Sending flag query for idn %d failed, err = %d\n",
1625 __func__, idn, err);
1626 goto out_unlock;
1627 }
1628
1629 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301630 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301631 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1632
1633out_unlock:
1634 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001635 ufshcd_release(hba);
Dolev Raviv68078d52013-07-30 00:35:58 +05301636 return err;
1637}
1638
1639/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301640 * ufshcd_query_attr - API function for sending attribute requests
1641 * hba: per-adapter instance
1642 * opcode: attribute opcode
1643 * idn: attribute idn to access
1644 * index: index field
1645 * selector: selector field
1646 * attr_val: the attribute value after the query request completes
1647 *
1648 * Returns 0 for success, non-zero in case of failure
1649*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301650static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301651 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1652{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001653 struct ufs_query_req *request = NULL;
1654 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301655 int err;
1656
1657 BUG_ON(!hba);
1658
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001659 ufshcd_hold(hba, false);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301660 if (!attr_val) {
1661 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1662 __func__, opcode);
1663 err = -EINVAL;
1664 goto out;
1665 }
1666
1667 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001668 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1669 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301670
1671 switch (opcode) {
1672 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1673 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301674 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301675 break;
1676 case UPIU_QUERY_OPCODE_READ_ATTR:
1677 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1678 break;
1679 default:
1680 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1681 __func__, opcode);
1682 err = -EINVAL;
1683 goto out_unlock;
1684 }
1685
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001686 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301687
1688 if (err) {
1689 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1690 __func__, opcode, idn, err);
1691 goto out_unlock;
1692 }
1693
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301694 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301695
1696out_unlock:
1697 mutex_unlock(&hba->dev_cmd.lock);
1698out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001699 ufshcd_release(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301700 return err;
1701}
1702
1703/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001704 * ufshcd_query_descriptor - API function for sending descriptor requests
1705 * hba: per-adapter instance
1706 * opcode: attribute opcode
1707 * idn: attribute idn to access
1708 * index: index field
1709 * selector: selector field
1710 * desc_buf: the buffer that contains the descriptor
1711 * buf_len: length parameter passed to the device
1712 *
1713 * Returns 0 for success, non-zero in case of failure.
1714 * The buf_len parameter will contain, on return, the length parameter
1715 * received on the response.
1716 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001717static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001718 enum query_opcode opcode, enum desc_idn idn, u8 index,
1719 u8 selector, u8 *desc_buf, int *buf_len)
1720{
1721 struct ufs_query_req *request = NULL;
1722 struct ufs_query_res *response = NULL;
1723 int err;
1724
1725 BUG_ON(!hba);
1726
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001727 ufshcd_hold(hba, false);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001728 if (!desc_buf) {
1729 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1730 __func__, opcode);
1731 err = -EINVAL;
1732 goto out;
1733 }
1734
1735 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1736 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1737 __func__, *buf_len);
1738 err = -EINVAL;
1739 goto out;
1740 }
1741
1742 mutex_lock(&hba->dev_cmd.lock);
1743 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1744 selector);
1745 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001746 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001747
1748 switch (opcode) {
1749 case UPIU_QUERY_OPCODE_WRITE_DESC:
1750 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1751 break;
1752 case UPIU_QUERY_OPCODE_READ_DESC:
1753 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1754 break;
1755 default:
1756 dev_err(hba->dev,
1757 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1758 __func__, opcode);
1759 err = -EINVAL;
1760 goto out_unlock;
1761 }
1762
1763 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1764
1765 if (err) {
1766 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1767 __func__, opcode, idn, err);
1768 goto out_unlock;
1769 }
1770
1771 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001772 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001773
1774out_unlock:
1775 mutex_unlock(&hba->dev_cmd.lock);
1776out:
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03001777 ufshcd_release(hba);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001778 return err;
1779}
1780
1781/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001782 * ufshcd_read_desc_param - read the specified descriptor parameter
1783 * @hba: Pointer to adapter instance
1784 * @desc_id: descriptor idn value
1785 * @desc_index: descriptor index
1786 * @param_offset: offset of the parameter to read
1787 * @param_read_buf: pointer to buffer where parameter would be read
1788 * @param_size: sizeof(param_read_buf)
1789 *
1790 * Return 0 in case of success, non-zero otherwise
1791 */
1792static int ufshcd_read_desc_param(struct ufs_hba *hba,
1793 enum desc_idn desc_id,
1794 int desc_index,
1795 u32 param_offset,
1796 u8 *param_read_buf,
1797 u32 param_size)
1798{
1799 int ret;
1800 u8 *desc_buf;
1801 u32 buff_len;
1802 bool is_kmalloc = true;
1803
1804 /* safety checks */
1805 if (desc_id >= QUERY_DESC_IDN_MAX)
1806 return -EINVAL;
1807
1808 buff_len = ufs_query_desc_max_size[desc_id];
1809 if ((param_offset + param_size) > buff_len)
1810 return -EINVAL;
1811
1812 if (!param_offset && (param_size == buff_len)) {
1813 /* memory space already available to hold full descriptor */
1814 desc_buf = param_read_buf;
1815 is_kmalloc = false;
1816 } else {
1817 /* allocate memory to hold full descriptor */
1818 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1819 if (!desc_buf)
1820 return -ENOMEM;
1821 }
1822
1823 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1824 desc_id, desc_index, 0, desc_buf,
1825 &buff_len);
1826
1827 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
1828 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
1829 ufs_query_desc_max_size[desc_id])
1830 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
1831 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
1832 __func__, desc_id, param_offset, buff_len, ret);
1833 if (!ret)
1834 ret = -EINVAL;
1835
1836 goto out;
1837 }
1838
1839 if (is_kmalloc)
1840 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1841out:
1842 if (is_kmalloc)
1843 kfree(desc_buf);
1844 return ret;
1845}
1846
1847static inline int ufshcd_read_desc(struct ufs_hba *hba,
1848 enum desc_idn desc_id,
1849 int desc_index,
1850 u8 *buf,
1851 u32 size)
1852{
1853 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1854}
1855
1856static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
1857 u8 *buf,
1858 u32 size)
1859{
1860 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
1861}
1862
1863/**
1864 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
1865 * @hba: Pointer to adapter instance
1866 * @lun: lun id
1867 * @param_offset: offset of the parameter to read
1868 * @param_read_buf: pointer to buffer where parameter would be read
1869 * @param_size: sizeof(param_read_buf)
1870 *
1871 * Return 0 in case of success, non-zero otherwise
1872 */
1873static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
1874 int lun,
1875 enum unit_desc_param param_offset,
1876 u8 *param_read_buf,
1877 u32 param_size)
1878{
1879 /*
1880 * Unit descriptors are only available for general purpose LUs (LUN id
1881 * from 0 to 7) and RPMB Well known LU.
1882 */
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03001883 if (lun != UFS_UPIU_RPMB_WLUN && (lun >= UFS_UPIU_MAX_GENERAL_LUN))
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001884 return -EOPNOTSUPP;
1885
1886 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
1887 param_offset, param_read_buf, param_size);
1888}
1889
1890/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301891 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1892 * @hba: per adapter instance
1893 *
1894 * 1. Allocate DMA memory for Command Descriptor array
1895 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1896 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1897 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1898 * (UTMRDL)
1899 * 4. Allocate memory for local reference block(lrb).
1900 *
1901 * Returns 0 for success, non-zero in case of failure
1902 */
1903static int ufshcd_memory_alloc(struct ufs_hba *hba)
1904{
1905 size_t utmrdl_size, utrdl_size, ucdl_size;
1906
1907 /* Allocate memory for UTP command descriptors */
1908 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001909 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
1910 ucdl_size,
1911 &hba->ucdl_dma_addr,
1912 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301913
1914 /*
1915 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1916 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1917 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1918 * be aligned to 128 bytes as well
1919 */
1920 if (!hba->ucdl_base_addr ||
1921 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301922 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301923 "Command Descriptor Memory allocation failed\n");
1924 goto out;
1925 }
1926
1927 /*
1928 * Allocate memory for UTP Transfer descriptors
1929 * UFSHCI requires 1024 byte alignment of UTRD
1930 */
1931 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001932 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
1933 utrdl_size,
1934 &hba->utrdl_dma_addr,
1935 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301936 if (!hba->utrdl_base_addr ||
1937 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301938 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301939 "Transfer Descriptor Memory allocation failed\n");
1940 goto out;
1941 }
1942
1943 /*
1944 * Allocate memory for UTP Task Management descriptors
1945 * UFSHCI requires 1024 byte alignment of UTMRD
1946 */
1947 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09001948 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
1949 utmrdl_size,
1950 &hba->utmrdl_dma_addr,
1951 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301952 if (!hba->utmrdl_base_addr ||
1953 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301954 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301955 "Task Management Descriptor Memory allocation failed\n");
1956 goto out;
1957 }
1958
1959 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09001960 hba->lrb = devm_kzalloc(hba->dev,
1961 hba->nutrs * sizeof(struct ufshcd_lrb),
1962 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301963 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301964 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301965 goto out;
1966 }
1967 return 0;
1968out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301969 return -ENOMEM;
1970}
1971
1972/**
1973 * ufshcd_host_memory_configure - configure local reference block with
1974 * memory offsets
1975 * @hba: per adapter instance
1976 *
1977 * Configure Host memory space
1978 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
1979 * address.
1980 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
1981 * and PRDT offset.
1982 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
1983 * into local reference block.
1984 */
1985static void ufshcd_host_memory_configure(struct ufs_hba *hba)
1986{
1987 struct utp_transfer_cmd_desc *cmd_descp;
1988 struct utp_transfer_req_desc *utrdlp;
1989 dma_addr_t cmd_desc_dma_addr;
1990 dma_addr_t cmd_desc_element_addr;
1991 u16 response_offset;
1992 u16 prdt_offset;
1993 int cmd_desc_size;
1994 int i;
1995
1996 utrdlp = hba->utrdl_base_addr;
1997 cmd_descp = hba->ucdl_base_addr;
1998
1999 response_offset =
2000 offsetof(struct utp_transfer_cmd_desc, response_upiu);
2001 prdt_offset =
2002 offsetof(struct utp_transfer_cmd_desc, prd_table);
2003
2004 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
2005 cmd_desc_dma_addr = hba->ucdl_dma_addr;
2006
2007 for (i = 0; i < hba->nutrs; i++) {
2008 /* Configure UTRD with command descriptor base address */
2009 cmd_desc_element_addr =
2010 (cmd_desc_dma_addr + (cmd_desc_size * i));
2011 utrdlp[i].command_desc_base_addr_lo =
2012 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
2013 utrdlp[i].command_desc_base_addr_hi =
2014 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
2015
2016 /* Response upiu and prdt offset should be in double words */
2017 utrdlp[i].response_upiu_offset =
2018 cpu_to_le16((response_offset >> 2));
2019 utrdlp[i].prd_table_offset =
2020 cpu_to_le16((prdt_offset >> 2));
2021 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05302022 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302023
2024 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302025 hba->lrb[i].ucd_req_ptr =
2026 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302027 hba->lrb[i].ucd_rsp_ptr =
2028 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
2029 hba->lrb[i].ucd_prdt_ptr =
2030 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
2031 }
2032}
2033
2034/**
2035 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
2036 * @hba: per adapter instance
2037 *
2038 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
2039 * in order to initialize the Unipro link startup procedure.
2040 * Once the Unipro links are up, the device connected to the controller
2041 * is detected.
2042 *
2043 * Returns 0 on success, non-zero value on failure
2044 */
2045static int ufshcd_dme_link_startup(struct ufs_hba *hba)
2046{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302047 struct uic_command uic_cmd = {0};
2048 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302049
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302050 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
2051
2052 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2053 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302054 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302055 "dme-link-startup: error code %d\n", ret);
2056 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302057}
2058
2059/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302060 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
2061 * @hba: per adapter instance
2062 * @attr_sel: uic command argument1
2063 * @attr_set: attribute set type as uic command argument2
2064 * @mib_val: setting value as uic command argument3
2065 * @peer: indicate whether peer or local
2066 *
2067 * Returns 0 on success, non-zero value on failure
2068 */
2069int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
2070 u8 attr_set, u32 mib_val, u8 peer)
2071{
2072 struct uic_command uic_cmd = {0};
2073 static const char *const action[] = {
2074 "dme-set",
2075 "dme-peer-set"
2076 };
2077 const char *set = action[!!peer];
2078 int ret;
2079
2080 uic_cmd.command = peer ?
2081 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
2082 uic_cmd.argument1 = attr_sel;
2083 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
2084 uic_cmd.argument3 = mib_val;
2085
2086 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2087 if (ret)
2088 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
2089 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
2090
2091 return ret;
2092}
2093EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
2094
2095/**
2096 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
2097 * @hba: per adapter instance
2098 * @attr_sel: uic command argument1
2099 * @mib_val: the value of the attribute as returned by the UIC command
2100 * @peer: indicate whether peer or local
2101 *
2102 * Returns 0 on success, non-zero value on failure
2103 */
2104int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
2105 u32 *mib_val, u8 peer)
2106{
2107 struct uic_command uic_cmd = {0};
2108 static const char *const action[] = {
2109 "dme-get",
2110 "dme-peer-get"
2111 };
2112 const char *get = action[!!peer];
2113 int ret;
2114
2115 uic_cmd.command = peer ?
2116 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
2117 uic_cmd.argument1 = attr_sel;
2118
2119 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
2120 if (ret) {
2121 dev_err(hba->dev, "%s: attr-id 0x%x error code %d\n",
2122 get, UIC_GET_ATTR_ID(attr_sel), ret);
2123 goto out;
2124 }
2125
2126 if (mib_val)
2127 *mib_val = uic_cmd.argument3;
2128out:
2129 return ret;
2130}
2131EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
2132
2133/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002134 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
2135 * state) and waits for it to take effect.
2136 *
2137 * @hba: per adapter instance
2138 * @cmd: UIC command to execute
2139 *
2140 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
2141 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
2142 * and device UniPro link and hence it's final completion would be indicated by
2143 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
2144 * addition to normal UIC command completion Status (UCCS). This function only
2145 * returns after the relevant status bits indicate the completion.
2146 *
2147 * Returns 0 on success, non-zero value on failure
2148 */
2149static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd)
2150{
2151 struct completion uic_async_done;
2152 unsigned long flags;
2153 u8 status;
2154 int ret;
2155
2156 mutex_lock(&hba->uic_cmd_mutex);
2157 init_completion(&uic_async_done);
2158
2159 spin_lock_irqsave(hba->host->host_lock, flags);
2160 hba->uic_async_done = &uic_async_done;
2161 ret = __ufshcd_send_uic_cmd(hba, cmd);
2162 spin_unlock_irqrestore(hba->host->host_lock, flags);
2163 if (ret) {
2164 dev_err(hba->dev,
2165 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2166 cmd->command, cmd->argument3, ret);
2167 goto out;
2168 }
2169 ret = ufshcd_wait_for_uic_cmd(hba, cmd);
2170 if (ret) {
2171 dev_err(hba->dev,
2172 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
2173 cmd->command, cmd->argument3, ret);
2174 goto out;
2175 }
2176
2177 if (!wait_for_completion_timeout(hba->uic_async_done,
2178 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
2179 dev_err(hba->dev,
2180 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
2181 cmd->command, cmd->argument3);
2182 ret = -ETIMEDOUT;
2183 goto out;
2184 }
2185
2186 status = ufshcd_get_upmcrs(hba);
2187 if (status != PWR_LOCAL) {
2188 dev_err(hba->dev,
2189 "pwr ctrl cmd 0x%0x failed, host umpcrs:0x%x\n",
2190 cmd->command, status);
2191 ret = (status != PWR_OK) ? status : -1;
2192 }
2193out:
2194 spin_lock_irqsave(hba->host->host_lock, flags);
2195 hba->uic_async_done = NULL;
2196 spin_unlock_irqrestore(hba->host->host_lock, flags);
2197 mutex_unlock(&hba->uic_cmd_mutex);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002198
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002199 return ret;
2200}
2201
2202/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302203 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
2204 * using DME_SET primitives.
2205 * @hba: per adapter instance
2206 * @mode: powr mode value
2207 *
2208 * Returns 0 on success, non-zero value on failure
2209 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05302210static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302211{
2212 struct uic_command uic_cmd = {0};
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002213 int ret;
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302214
2215 uic_cmd.command = UIC_CMD_DME_SET;
2216 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
2217 uic_cmd.argument3 = mode;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002218 ufshcd_hold(hba, false);
2219 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2220 ufshcd_release(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302221
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002222 return ret;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002223}
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302224
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002225static int ufshcd_uic_hibern8_enter(struct ufs_hba *hba)
2226{
2227 struct uic_command uic_cmd = {0};
2228
2229 uic_cmd.command = UIC_CMD_DME_HIBER_ENTER;
2230
2231 return ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
2232}
2233
2234static int ufshcd_uic_hibern8_exit(struct ufs_hba *hba)
2235{
2236 struct uic_command uic_cmd = {0};
2237 int ret;
2238
2239 uic_cmd.command = UIC_CMD_DME_HIBER_EXIT;
2240 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302241 if (ret) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002242 ufshcd_set_link_off(hba);
2243 ret = ufshcd_host_reset_and_restore(hba);
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302244 }
2245
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302246 return ret;
2247}
2248
2249/**
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002250 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
2251 * @hba: per-adapter instance
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302252 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002253static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302254{
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002255 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info;
2256
2257 if (hba->max_pwr_info.is_valid)
2258 return 0;
2259
2260 pwr_info->pwr_tx = FASTAUTO_MODE;
2261 pwr_info->pwr_rx = FASTAUTO_MODE;
2262 pwr_info->hs_rate = PA_HS_MODE_B;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302263
2264 /* Get the connected lane count */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002265 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES),
2266 &pwr_info->lane_rx);
2267 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES),
2268 &pwr_info->lane_tx);
2269
2270 if (!pwr_info->lane_rx || !pwr_info->lane_tx) {
2271 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
2272 __func__,
2273 pwr_info->lane_rx,
2274 pwr_info->lane_tx);
2275 return -EINVAL;
2276 }
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302277
2278 /*
2279 * First, get the maximum gears of HS speed.
2280 * If a zero value, it means there is no HSGEAR capability.
2281 * Then, get the maximum gears of PWM speed.
2282 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002283 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx);
2284 if (!pwr_info->gear_rx) {
2285 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
2286 &pwr_info->gear_rx);
2287 if (!pwr_info->gear_rx) {
2288 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n",
2289 __func__, pwr_info->gear_rx);
2290 return -EINVAL;
2291 }
2292 pwr_info->pwr_rx = SLOWAUTO_MODE;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302293 }
2294
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002295 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR),
2296 &pwr_info->gear_tx);
2297 if (!pwr_info->gear_tx) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302298 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002299 &pwr_info->gear_tx);
2300 if (!pwr_info->gear_tx) {
2301 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n",
2302 __func__, pwr_info->gear_tx);
2303 return -EINVAL;
2304 }
2305 pwr_info->pwr_tx = SLOWAUTO_MODE;
2306 }
2307
2308 hba->max_pwr_info.is_valid = true;
2309 return 0;
2310}
2311
2312static int ufshcd_change_power_mode(struct ufs_hba *hba,
2313 struct ufs_pa_layer_attr *pwr_mode)
2314{
2315 int ret;
2316
2317 /* if already configured to the requested pwr_mode */
2318 if (pwr_mode->gear_rx == hba->pwr_info.gear_rx &&
2319 pwr_mode->gear_tx == hba->pwr_info.gear_tx &&
2320 pwr_mode->lane_rx == hba->pwr_info.lane_rx &&
2321 pwr_mode->lane_tx == hba->pwr_info.lane_tx &&
2322 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx &&
2323 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx &&
2324 pwr_mode->hs_rate == hba->pwr_info.hs_rate) {
2325 dev_dbg(hba->dev, "%s: power already configured\n", __func__);
2326 return 0;
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302327 }
2328
2329 /*
2330 * Configure attributes for power mode change with below.
2331 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
2332 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
2333 * - PA_HSSERIES
2334 */
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002335 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx);
2336 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES),
2337 pwr_mode->lane_rx);
2338 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2339 pwr_mode->pwr_rx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302340 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002341 else
2342 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302343
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002344 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx);
2345 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES),
2346 pwr_mode->lane_tx);
2347 if (pwr_mode->pwr_tx == FASTAUTO_MODE ||
2348 pwr_mode->pwr_tx == FAST_MODE)
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302349 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002350 else
2351 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), FALSE);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302352
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002353 if (pwr_mode->pwr_rx == FASTAUTO_MODE ||
2354 pwr_mode->pwr_tx == FASTAUTO_MODE ||
2355 pwr_mode->pwr_rx == FAST_MODE ||
2356 pwr_mode->pwr_tx == FAST_MODE)
2357 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES),
2358 pwr_mode->hs_rate);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302359
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002360 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4
2361 | pwr_mode->pwr_tx);
2362
2363 if (ret) {
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302364 dev_err(hba->dev,
Dolev Raviv7eb584d2014-09-25 15:32:31 +03002365 "%s: power mode change failed %d\n", __func__, ret);
2366 } else {
2367 if (hba->vops && hba->vops->pwr_change_notify)
2368 hba->vops->pwr_change_notify(hba,
2369 POST_CHANGE, NULL, pwr_mode);
2370
2371 memcpy(&hba->pwr_info, pwr_mode,
2372 sizeof(struct ufs_pa_layer_attr));
2373 }
2374
2375 return ret;
2376}
2377
2378/**
2379 * ufshcd_config_pwr_mode - configure a new power mode
2380 * @hba: per-adapter instance
2381 * @desired_pwr_mode: desired power configuration
2382 */
2383static int ufshcd_config_pwr_mode(struct ufs_hba *hba,
2384 struct ufs_pa_layer_attr *desired_pwr_mode)
2385{
2386 struct ufs_pa_layer_attr final_params = { 0 };
2387 int ret;
2388
2389 if (hba->vops && hba->vops->pwr_change_notify)
2390 hba->vops->pwr_change_notify(hba,
2391 PRE_CHANGE, desired_pwr_mode, &final_params);
2392 else
2393 memcpy(&final_params, desired_pwr_mode, sizeof(final_params));
2394
2395 ret = ufshcd_change_power_mode(hba, &final_params);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05302396
2397 return ret;
2398}
2399
2400/**
Dolev Raviv68078d52013-07-30 00:35:58 +05302401 * ufshcd_complete_dev_init() - checks device readiness
2402 * hba: per-adapter instance
2403 *
2404 * Set fDeviceInit flag and poll until device toggles it.
2405 */
2406static int ufshcd_complete_dev_init(struct ufs_hba *hba)
2407{
2408 int i, retries, err = 0;
2409 bool flag_res = 1;
2410
2411 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2412 /* Set the fDeviceInit flag */
2413 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2414 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
2415 if (!err || err == -ETIMEDOUT)
2416 break;
2417 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2418 }
2419 if (err) {
2420 dev_err(hba->dev,
2421 "%s setting fDeviceInit flag failed with error %d\n",
2422 __func__, err);
2423 goto out;
2424 }
2425
2426 /* poll for max. 100 iterations for fDeviceInit flag to clear */
2427 for (i = 0; i < 100 && !err && flag_res; i++) {
2428 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
2429 err = ufshcd_query_flag(hba,
2430 UPIU_QUERY_OPCODE_READ_FLAG,
2431 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
2432 if (!err || err == -ETIMEDOUT)
2433 break;
2434 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
2435 err);
2436 }
2437 }
2438 if (err)
2439 dev_err(hba->dev,
2440 "%s reading fDeviceInit flag failed with error %d\n",
2441 __func__, err);
2442 else if (flag_res)
2443 dev_err(hba->dev,
2444 "%s fDeviceInit was not cleared by the device\n",
2445 __func__);
2446
2447out:
2448 return err;
2449}
2450
2451/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302452 * ufshcd_make_hba_operational - Make UFS controller operational
2453 * @hba: per adapter instance
2454 *
2455 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002456 * 1. Enable required interrupts
2457 * 2. Configure interrupt aggregation
2458 * 3. Program UTRL and UTMRL base addres
2459 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302460 *
2461 * Returns 0 on success, non-zero value on failure
2462 */
2463static int ufshcd_make_hba_operational(struct ufs_hba *hba)
2464{
2465 int err = 0;
2466 u32 reg;
2467
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302468 /* Enable required interrupts */
2469 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
2470
2471 /* Configure interrupt aggregation */
Seungwon Jeon7d568652013-08-31 21:40:20 +05302472 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302473
2474 /* Configure UTRL and UTMRL base address registers */
2475 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
2476 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
2477 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
2478 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
2479 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
2480 REG_UTP_TASK_REQ_LIST_BASE_L);
2481 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
2482 REG_UTP_TASK_REQ_LIST_BASE_H);
2483
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302484 /*
2485 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
2486 * DEI, HEI bits must be 0
2487 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002488 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302489 if (!(ufshcd_get_lists_status(reg))) {
2490 ufshcd_enable_run_stop_reg(hba);
2491 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302492 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302493 "Host controller not ready to process requests");
2494 err = -EIO;
2495 goto out;
2496 }
2497
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302498out:
2499 return err;
2500}
2501
2502/**
2503 * ufshcd_hba_enable - initialize the controller
2504 * @hba: per adapter instance
2505 *
2506 * The controller resets itself and controller firmware initialization
2507 * sequence kicks off. When controller is ready it will set
2508 * the Host Controller Enable bit to 1.
2509 *
2510 * Returns 0 on success, non-zero value on failure
2511 */
2512static int ufshcd_hba_enable(struct ufs_hba *hba)
2513{
2514 int retry;
2515
2516 /*
2517 * msleep of 1 and 5 used in this function might result in msleep(20),
2518 * but it was necessary to send the UFS FPGA to reset mode during
2519 * development and testing of this driver. msleep can be changed to
2520 * mdelay and retry count can be reduced based on the controller.
2521 */
2522 if (!ufshcd_is_hba_active(hba)) {
2523
2524 /* change controller state to "reset state" */
2525 ufshcd_hba_stop(hba);
2526
2527 /*
2528 * This delay is based on the testing done with UFS host
2529 * controller FPGA. The delay can be changed based on the
2530 * host controller used.
2531 */
2532 msleep(5);
2533 }
2534
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002535 /* UniPro link is disabled at this point */
2536 ufshcd_set_link_off(hba);
2537
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002538 if (hba->vops && hba->vops->hce_enable_notify)
2539 hba->vops->hce_enable_notify(hba, PRE_CHANGE);
2540
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302541 /* start controller initialization sequence */
2542 ufshcd_hba_start(hba);
2543
2544 /*
2545 * To initialize a UFS host controller HCE bit must be set to 1.
2546 * During initialization the HCE bit value changes from 1->0->1.
2547 * When the host controller completes initialization sequence
2548 * it sets the value of HCE bit to 1. The same HCE bit is read back
2549 * to check if the controller has completed initialization sequence.
2550 * So without this delay the value HCE = 1, set in the previous
2551 * instruction might be read back.
2552 * This delay can be changed based on the controller.
2553 */
2554 msleep(1);
2555
2556 /* wait for the host controller to complete initialization */
2557 retry = 10;
2558 while (ufshcd_is_hba_active(hba)) {
2559 if (retry) {
2560 retry--;
2561 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302562 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302563 "Controller enable failed\n");
2564 return -EIO;
2565 }
2566 msleep(5);
2567 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002568
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002569 /* enable UIC related interrupts */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002570 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002571
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002572 if (hba->vops && hba->vops->hce_enable_notify)
2573 hba->vops->hce_enable_notify(hba, POST_CHANGE);
2574
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302575 return 0;
2576}
2577
2578/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302579 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302580 * @hba: per adapter instance
2581 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302582 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302583 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302584static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302585{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302586 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002587 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302588
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002589 do {
2590 if (hba->vops && hba->vops->link_startup_notify)
2591 hba->vops->link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302592
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002593 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002594
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002595 /* check if device is detected by inter-connect layer */
2596 if (!ret && !ufshcd_is_device_present(hba)) {
2597 dev_err(hba->dev, "%s: Device not present\n", __func__);
2598 ret = -ENXIO;
2599 goto out;
2600 }
2601
2602 /*
2603 * DME link lost indication is only received when link is up,
2604 * but we can't be sure if the link is up until link startup
2605 * succeeds. So reset the local Uni-Pro and try again.
2606 */
2607 if (ret && ufshcd_hba_enable(hba))
2608 goto out;
2609 } while (ret && retries--);
2610
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302611 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002612 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302613 goto out;
2614
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002615 /* Include any host controller configuration via UIC commands */
2616 if (hba->vops && hba->vops->link_startup_notify) {
2617 ret = hba->vops->link_startup_notify(hba, POST_CHANGE);
2618 if (ret)
2619 goto out;
2620 }
2621
2622 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302623out:
2624 if (ret)
2625 dev_err(hba->dev, "link startup failed %d\n", ret);
2626 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302627}
2628
2629/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302630 * ufshcd_verify_dev_init() - Verify device initialization
2631 * @hba: per-adapter instance
2632 *
2633 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
2634 * device Transport Protocol (UTP) layer is ready after a reset.
2635 * If the UTP layer at the device side is not initialized, it may
2636 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
2637 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
2638 */
2639static int ufshcd_verify_dev_init(struct ufs_hba *hba)
2640{
2641 int err = 0;
2642 int retries;
2643
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002644 ufshcd_hold(hba, false);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302645 mutex_lock(&hba->dev_cmd.lock);
2646 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
2647 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
2648 NOP_OUT_TIMEOUT);
2649
2650 if (!err || err == -ETIMEDOUT)
2651 break;
2652
2653 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2654 }
2655 mutex_unlock(&hba->dev_cmd.lock);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03002656 ufshcd_release(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302657
2658 if (err)
2659 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2660 return err;
2661}
2662
2663/**
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002664 * ufshcd_set_queue_depth - set lun queue depth
2665 * @sdev: pointer to SCSI device
2666 *
2667 * Read bLUQueueDepth value and activate scsi tagged command
2668 * queueing. For WLUN, queue depth is set to 1. For best-effort
2669 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
2670 * value that host can queue.
2671 */
2672static void ufshcd_set_queue_depth(struct scsi_device *sdev)
2673{
2674 int ret = 0;
2675 u8 lun_qdepth;
2676 struct ufs_hba *hba;
2677
2678 hba = shost_priv(sdev->host);
2679
2680 lun_qdepth = hba->nutrs;
2681 ret = ufshcd_read_unit_desc_param(hba,
2682 ufshcd_scsi_to_upiu_lun(sdev->lun),
2683 UNIT_DESC_PARAM_LU_Q_DEPTH,
2684 &lun_qdepth,
2685 sizeof(lun_qdepth));
2686
2687 /* Some WLUN doesn't support unit descriptor */
2688 if (ret == -EOPNOTSUPP)
2689 lun_qdepth = 1;
2690 else if (!lun_qdepth)
2691 /* eventually, we can figure out the real queue depth */
2692 lun_qdepth = hba->nutrs;
2693 else
2694 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2695
2696 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2697 __func__, lun_qdepth);
Christoph Hellwigee115602014-11-03 20:40:56 +01002698 scsi_adjust_queue_depth(sdev, lun_qdepth);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002699}
2700
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002701/*
2702 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
2703 * @hba: per-adapter instance
2704 * @lun: UFS device lun id
2705 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
2706 *
2707 * Returns 0 in case of success and b_lu_write_protect status would be returned
2708 * @b_lu_write_protect parameter.
2709 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
2710 * Returns -EINVAL in case of invalid parameters passed to this function.
2711 */
2712static int ufshcd_get_lu_wp(struct ufs_hba *hba,
2713 u8 lun,
2714 u8 *b_lu_write_protect)
2715{
2716 int ret;
2717
2718 if (!b_lu_write_protect)
2719 ret = -EINVAL;
2720 /*
2721 * According to UFS device spec, RPMB LU can't be write
2722 * protected so skip reading bLUWriteProtect parameter for
2723 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
2724 */
2725 else if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
2726 ret = -ENOTSUPP;
2727 else
2728 ret = ufshcd_read_unit_desc_param(hba,
2729 lun,
2730 UNIT_DESC_PARAM_LU_WR_PROTECT,
2731 b_lu_write_protect,
2732 sizeof(*b_lu_write_protect));
2733 return ret;
2734}
2735
2736/**
2737 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
2738 * status
2739 * @hba: per-adapter instance
2740 * @sdev: pointer to SCSI device
2741 *
2742 */
2743static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba *hba,
2744 struct scsi_device *sdev)
2745{
2746 if (hba->dev_info.f_power_on_wp_en &&
2747 !hba->dev_info.is_lu_power_on_wp) {
2748 u8 b_lu_write_protect;
2749
2750 if (!ufshcd_get_lu_wp(hba, ufshcd_scsi_to_upiu_lun(sdev->lun),
2751 &b_lu_write_protect) &&
2752 (b_lu_write_protect == UFS_LU_POWER_ON_WP))
2753 hba->dev_info.is_lu_power_on_wp = true;
2754 }
2755}
2756
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002757/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302758 * ufshcd_slave_alloc - handle initial SCSI device configurations
2759 * @sdev: pointer to SCSI device
2760 *
2761 * Returns success
2762 */
2763static int ufshcd_slave_alloc(struct scsi_device *sdev)
2764{
2765 struct ufs_hba *hba;
2766
2767 hba = shost_priv(sdev->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302768
2769 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
2770 sdev->use_10_for_ms = 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302771
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302772 /* allow SCSI layer to restart the device in case of errors */
2773 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002774
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03002775 /* REPORT SUPPORTED OPERATION CODES is not supported */
2776 sdev->no_report_opcodes = 1;
2777
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002778
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002779 ufshcd_set_queue_depth(sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002780
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002781 ufshcd_get_lu_power_on_wp_status(hba, sdev);
2782
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002783 return 0;
2784}
2785
2786/**
2787 * ufshcd_change_queue_depth - change queue depth
2788 * @sdev: pointer to SCSI device
2789 * @depth: required depth to set
2790 * @reason: reason for changing the depth
2791 *
2792 * Change queue depth according to the reason and make sure
2793 * the max. limits are not crossed.
2794 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03002795static int ufshcd_change_queue_depth(struct scsi_device *sdev,
2796 int depth, int reason)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002797{
2798 struct ufs_hba *hba = shost_priv(sdev->host);
2799
2800 if (depth > hba->nutrs)
2801 depth = hba->nutrs;
2802
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002803 scsi_adjust_queue_depth(sdev, depth);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002804 return depth;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302805}
2806
2807/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09002808 * ufshcd_slave_configure - adjust SCSI device configurations
2809 * @sdev: pointer to SCSI device
2810 */
2811static int ufshcd_slave_configure(struct scsi_device *sdev)
2812{
2813 struct request_queue *q = sdev->request_queue;
2814
2815 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
2816 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
2817
2818 return 0;
2819}
2820
2821/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302822 * ufshcd_slave_destroy - remove SCSI device configurations
2823 * @sdev: pointer to SCSI device
2824 */
2825static void ufshcd_slave_destroy(struct scsi_device *sdev)
2826{
2827 struct ufs_hba *hba;
2828
2829 hba = shost_priv(sdev->host);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03002830 /* Drop the reference as it won't be needed anymore */
2831 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN)
2832 hba->sdev_ufs_device = NULL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302833}
2834
2835/**
2836 * ufshcd_task_req_compl - handle task management request completion
2837 * @hba: per adapter instance
2838 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302839 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302840 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302841 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302842 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302843static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302844{
2845 struct utp_task_req_desc *task_req_descp;
2846 struct utp_upiu_task_rsp *task_rsp_upiup;
2847 unsigned long flags;
2848 int ocs_value;
2849 int task_result;
2850
2851 spin_lock_irqsave(hba->host->host_lock, flags);
2852
2853 /* Clear completed tasks from outstanding_tasks */
2854 __clear_bit(index, &hba->outstanding_tasks);
2855
2856 task_req_descp = hba->utmrdl_base_addr;
2857 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
2858
2859 if (ocs_value == OCS_SUCCESS) {
2860 task_rsp_upiup = (struct utp_upiu_task_rsp *)
2861 task_req_descp[index].task_rsp_upiu;
2862 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
2863 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302864 if (resp)
2865 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302866 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302867 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
2868 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302869 }
2870 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302871
2872 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302873}
2874
2875/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302876 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
2877 * @lrb: pointer to local reference block of completed command
2878 * @scsi_status: SCSI command status
2879 *
2880 * Returns value base on SCSI command status
2881 */
2882static inline int
2883ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
2884{
2885 int result = 0;
2886
2887 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302888 case SAM_STAT_CHECK_CONDITION:
2889 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302890 case SAM_STAT_GOOD:
2891 result |= DID_OK << 16 |
2892 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302893 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302894 break;
2895 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302896 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302897 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302898 ufshcd_copy_sense_data(lrbp);
2899 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302900 break;
2901 default:
2902 result |= DID_ERROR << 16;
2903 break;
2904 } /* end of switch */
2905
2906 return result;
2907}
2908
2909/**
2910 * ufshcd_transfer_rsp_status - Get overall status of the response
2911 * @hba: per adapter instance
2912 * @lrb: pointer to local reference block of completed command
2913 *
2914 * Returns result of the command to notify SCSI midlayer
2915 */
2916static inline int
2917ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2918{
2919 int result = 0;
2920 int scsi_status;
2921 int ocs;
2922
2923 /* overall command status of utrd */
2924 ocs = ufshcd_get_tr_ocs(lrbp);
2925
2926 switch (ocs) {
2927 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302928 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302929
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302930 switch (result) {
2931 case UPIU_TRANSACTION_RESPONSE:
2932 /*
2933 * get the response UPIU result to extract
2934 * the SCSI command status
2935 */
2936 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
2937
2938 /*
2939 * get the result based on SCSI status response
2940 * to notify the SCSI midlayer of the command status
2941 */
2942 scsi_status = result & MASK_SCSI_STATUS;
2943 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302944
2945 if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
2946 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302947 break;
2948 case UPIU_TRANSACTION_REJECT_UPIU:
2949 /* TODO: handle Reject UPIU Response */
2950 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302951 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302952 "Reject UPIU not fully implemented\n");
2953 break;
2954 default:
2955 result = DID_ERROR << 16;
2956 dev_err(hba->dev,
2957 "Unexpected request response code = %x\n",
2958 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302959 break;
2960 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302961 break;
2962 case OCS_ABORTED:
2963 result |= DID_ABORT << 16;
2964 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302965 case OCS_INVALID_COMMAND_STATUS:
2966 result |= DID_REQUEUE << 16;
2967 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302968 case OCS_INVALID_CMD_TABLE_ATTR:
2969 case OCS_INVALID_PRDT_ATTR:
2970 case OCS_MISMATCH_DATA_BUF_SIZE:
2971 case OCS_MISMATCH_RESP_UPIU_SIZE:
2972 case OCS_PEER_COMM_FAILURE:
2973 case OCS_FATAL_ERROR:
2974 default:
2975 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302976 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302977 "OCS error from controller = %x\n", ocs);
2978 break;
2979 } /* end of switch */
2980
2981 return result;
2982}
2983
2984/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302985 * ufshcd_uic_cmd_compl - handle completion of uic command
2986 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302987 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302988 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302989static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302990{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302991 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302992 hba->active_uic_cmd->argument2 |=
2993 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302994 hba->active_uic_cmd->argument3 =
2995 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302996 complete(&hba->active_uic_cmd->done);
2997 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302998
Subhash Jadavani57d104c2014-09-25 15:32:30 +03002999 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done)
3000 complete(hba->uic_async_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303001}
3002
3003/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303004 * ufshcd_transfer_req_compl - handle SCSI and query command completion
3005 * @hba: per adapter instance
3006 */
3007static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
3008{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303009 struct ufshcd_lrb *lrbp;
3010 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303011 unsigned long completed_reqs;
3012 u32 tr_doorbell;
3013 int result;
3014 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003015
3016 /* Resetting interrupt aggregation counters first and reading the
3017 * DOOR_BELL afterward allows us to handle all the completed requests.
3018 * In order to prevent other interrupts starvation the DB is read once
3019 * after reset. The down side of this solution is the possibility of
3020 * false interrupt if device completes another request after resetting
3021 * aggregation and before reading the DB.
3022 */
3023 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303024
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303025 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303026 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
3027
Dolev Ravive9d501b2014-07-01 12:22:37 +03003028 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
3029 lrbp = &hba->lrb[index];
3030 cmd = lrbp->cmd;
3031 if (cmd) {
3032 result = ufshcd_transfer_rsp_status(hba, lrbp);
3033 scsi_dma_unmap(cmd);
3034 cmd->result = result;
3035 /* Mark completed command as NULL in LRB */
3036 lrbp->cmd = NULL;
3037 clear_bit_unlock(index, &hba->lrb_in_use);
3038 /* Do not touch lrbp after scsi done */
3039 cmd->scsi_done(cmd);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003040 __ufshcd_release(hba);
Dolev Ravive9d501b2014-07-01 12:22:37 +03003041 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
3042 if (hba->dev_cmd.complete)
3043 complete(hba->dev_cmd.complete);
3044 }
3045 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303046
3047 /* clear corresponding bits of completed commands */
3048 hba->outstanding_reqs ^= completed_reqs;
3049
Sahitya Tummala856b3482014-09-25 15:32:34 +03003050 ufshcd_clk_scaling_update_busy(hba);
3051
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303052 /* we might have free'd some tags above */
3053 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303054}
3055
3056/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303057 * ufshcd_disable_ee - disable exception event
3058 * @hba: per-adapter instance
3059 * @mask: exception event to disable
3060 *
3061 * Disables exception event in the device so that the EVENT_ALERT
3062 * bit is not set.
3063 *
3064 * Returns zero on success, non-zero error value on failure.
3065 */
3066static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
3067{
3068 int err = 0;
3069 u32 val;
3070
3071 if (!(hba->ee_ctrl_mask & mask))
3072 goto out;
3073
3074 val = hba->ee_ctrl_mask & ~mask;
3075 val &= 0xFFFF; /* 2 bytes */
3076 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3077 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3078 if (!err)
3079 hba->ee_ctrl_mask &= ~mask;
3080out:
3081 return err;
3082}
3083
3084/**
3085 * ufshcd_enable_ee - enable exception event
3086 * @hba: per-adapter instance
3087 * @mask: exception event to enable
3088 *
3089 * Enable corresponding exception event in the device to allow
3090 * device to alert host in critical scenarios.
3091 *
3092 * Returns zero on success, non-zero error value on failure.
3093 */
3094static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
3095{
3096 int err = 0;
3097 u32 val;
3098
3099 if (hba->ee_ctrl_mask & mask)
3100 goto out;
3101
3102 val = hba->ee_ctrl_mask | mask;
3103 val &= 0xFFFF; /* 2 bytes */
3104 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
3105 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
3106 if (!err)
3107 hba->ee_ctrl_mask |= mask;
3108out:
3109 return err;
3110}
3111
3112/**
3113 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
3114 * @hba: per-adapter instance
3115 *
3116 * Allow device to manage background operations on its own. Enabling
3117 * this might lead to inconsistent latencies during normal data transfers
3118 * as the device is allowed to manage its own way of handling background
3119 * operations.
3120 *
3121 * Returns zero on success, non-zero on failure.
3122 */
3123static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
3124{
3125 int err = 0;
3126
3127 if (hba->auto_bkops_enabled)
3128 goto out;
3129
3130 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
3131 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3132 if (err) {
3133 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
3134 __func__, err);
3135 goto out;
3136 }
3137
3138 hba->auto_bkops_enabled = true;
3139
3140 /* No need of URGENT_BKOPS exception from the device */
3141 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3142 if (err)
3143 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
3144 __func__, err);
3145out:
3146 return err;
3147}
3148
3149/**
3150 * ufshcd_disable_auto_bkops - block device in doing background operations
3151 * @hba: per-adapter instance
3152 *
3153 * Disabling background operations improves command response latency but
3154 * has drawback of device moving into critical state where the device is
3155 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
3156 * host is idle so that BKOPS are managed effectively without any negative
3157 * impacts.
3158 *
3159 * Returns zero on success, non-zero on failure.
3160 */
3161static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
3162{
3163 int err = 0;
3164
3165 if (!hba->auto_bkops_enabled)
3166 goto out;
3167
3168 /*
3169 * If host assisted BKOPs is to be enabled, make sure
3170 * urgent bkops exception is allowed.
3171 */
3172 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
3173 if (err) {
3174 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
3175 __func__, err);
3176 goto out;
3177 }
3178
3179 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
3180 QUERY_FLAG_IDN_BKOPS_EN, NULL);
3181 if (err) {
3182 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
3183 __func__, err);
3184 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
3185 goto out;
3186 }
3187
3188 hba->auto_bkops_enabled = false;
3189out:
3190 return err;
3191}
3192
3193/**
3194 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
3195 * @hba: per adapter instance
3196 *
3197 * After a device reset the device may toggle the BKOPS_EN flag
3198 * to default value. The s/w tracking variables should be updated
3199 * as well. Do this by forcing enable of auto bkops.
3200 */
3201static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
3202{
3203 hba->auto_bkops_enabled = false;
3204 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
3205 ufshcd_enable_auto_bkops(hba);
3206}
3207
3208static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
3209{
3210 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3211 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
3212}
3213
3214/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003215 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
3216 * @hba: per-adapter instance
3217 * @status: bkops_status value
3218 *
3219 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
3220 * flag in the device to permit background operations if the device
3221 * bkops_status is greater than or equal to "status" argument passed to
3222 * this function, disable otherwise.
3223 *
3224 * Returns 0 for success, non-zero in case of failure.
3225 *
3226 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
3227 * to know whether auto bkops is enabled or disabled after this function
3228 * returns control to it.
3229 */
3230static int ufshcd_bkops_ctrl(struct ufs_hba *hba,
3231 enum bkops_status status)
3232{
3233 int err;
3234 u32 curr_status = 0;
3235
3236 err = ufshcd_get_bkops_status(hba, &curr_status);
3237 if (err) {
3238 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
3239 __func__, err);
3240 goto out;
3241 } else if (curr_status > BKOPS_STATUS_MAX) {
3242 dev_err(hba->dev, "%s: invalid BKOPS status %d\n",
3243 __func__, curr_status);
3244 err = -EINVAL;
3245 goto out;
3246 }
3247
3248 if (curr_status >= status)
3249 err = ufshcd_enable_auto_bkops(hba);
3250 else
3251 err = ufshcd_disable_auto_bkops(hba);
3252out:
3253 return err;
3254}
3255
3256/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303257 * ufshcd_urgent_bkops - handle urgent bkops exception event
3258 * @hba: per-adapter instance
3259 *
3260 * Enable fBackgroundOpsEn flag in the device to permit background
3261 * operations.
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003262 *
3263 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
3264 * and negative error value for any other failure.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303265 */
3266static int ufshcd_urgent_bkops(struct ufs_hba *hba)
3267{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003268 return ufshcd_bkops_ctrl(hba, BKOPS_STATUS_PERF_IMPACT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303269}
3270
3271static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
3272{
3273 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
3274 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
3275}
3276
3277/**
3278 * ufshcd_exception_event_handler - handle exceptions raised by device
3279 * @work: pointer to work data
3280 *
3281 * Read bExceptionEventStatus attribute from the device and handle the
3282 * exception event accordingly.
3283 */
3284static void ufshcd_exception_event_handler(struct work_struct *work)
3285{
3286 struct ufs_hba *hba;
3287 int err;
3288 u32 status = 0;
3289 hba = container_of(work, struct ufs_hba, eeh_work);
3290
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303291 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303292 err = ufshcd_get_ee_status(hba, &status);
3293 if (err) {
3294 dev_err(hba->dev, "%s: failed to get exception status %d\n",
3295 __func__, err);
3296 goto out;
3297 }
3298
3299 status &= hba->ee_ctrl_mask;
3300 if (status & MASK_EE_URGENT_BKOPS) {
3301 err = ufshcd_urgent_bkops(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03003302 if (err < 0)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303303 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
3304 __func__, err);
3305 }
3306out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303307 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303308 return;
3309}
3310
3311/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303312 * ufshcd_err_handler - handle UFS errors that require s/w attention
3313 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303314 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303315static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303316{
3317 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303318 unsigned long flags;
3319 u32 err_xfer = 0;
3320 u32 err_tm = 0;
3321 int err = 0;
3322 int tag;
3323
3324 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303325
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303326 pm_runtime_get_sync(hba->dev);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003327 ufshcd_hold(hba, false);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303328
3329 spin_lock_irqsave(hba->host->host_lock, flags);
3330 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
3331 spin_unlock_irqrestore(hba->host->host_lock, flags);
3332 goto out;
3333 }
3334
3335 hba->ufshcd_state = UFSHCD_STATE_RESET;
3336 ufshcd_set_eh_in_progress(hba);
3337
3338 /* Complete requests that have door-bell cleared by h/w */
3339 ufshcd_transfer_req_compl(hba);
3340 ufshcd_tmc_handler(hba);
3341 spin_unlock_irqrestore(hba->host->host_lock, flags);
3342
3343 /* Clear pending transfer requests */
3344 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
3345 if (ufshcd_clear_cmd(hba, tag))
3346 err_xfer |= 1 << tag;
3347
3348 /* Clear pending task management requests */
3349 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
3350 if (ufshcd_clear_tm_cmd(hba, tag))
3351 err_tm |= 1 << tag;
3352
3353 /* Complete the requests that are cleared by s/w */
3354 spin_lock_irqsave(hba->host->host_lock, flags);
3355 ufshcd_transfer_req_compl(hba);
3356 ufshcd_tmc_handler(hba);
3357 spin_unlock_irqrestore(hba->host->host_lock, flags);
3358
3359 /* Fatal errors need reset */
3360 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
3361 ((hba->saved_err & UIC_ERROR) &&
3362 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
3363 err = ufshcd_reset_and_restore(hba);
3364 if (err) {
3365 dev_err(hba->dev, "%s: reset and restore failed\n",
3366 __func__);
3367 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3368 }
3369 /*
3370 * Inform scsi mid-layer that we did reset and allow to handle
3371 * Unit Attention properly.
3372 */
3373 scsi_report_bus_reset(hba->host, 0);
3374 hba->saved_err = 0;
3375 hba->saved_uic_err = 0;
3376 }
3377 ufshcd_clear_eh_in_progress(hba);
3378
3379out:
3380 scsi_unblock_requests(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003381 ufshcd_release(hba);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303382 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303383}
3384
3385/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303386 * ufshcd_update_uic_error - check and set fatal UIC error flags.
3387 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303388 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303389static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303390{
3391 u32 reg;
3392
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303393 /* PA_INIT_ERROR is fatal and needs UIC reset */
3394 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
3395 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
3396 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
3397
3398 /* UIC NL/TL/DME errors needs software retry */
3399 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
3400 if (reg)
3401 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
3402
3403 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
3404 if (reg)
3405 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
3406
3407 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
3408 if (reg)
3409 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
3410
3411 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
3412 __func__, hba->uic_error);
3413}
3414
3415/**
3416 * ufshcd_check_errors - Check for errors that need s/w attention
3417 * @hba: per-adapter instance
3418 */
3419static void ufshcd_check_errors(struct ufs_hba *hba)
3420{
3421 bool queue_eh_work = false;
3422
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303423 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303424 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303425
3426 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303427 hba->uic_error = 0;
3428 ufshcd_update_uic_error(hba);
3429 if (hba->uic_error)
3430 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303431 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303432
3433 if (queue_eh_work) {
3434 /* handle fatal errors only when link is functional */
3435 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
3436 /* block commands from scsi mid-layer */
3437 scsi_block_requests(hba->host);
3438
3439 /* transfer error masks to sticky bits */
3440 hba->saved_err |= hba->errors;
3441 hba->saved_uic_err |= hba->uic_error;
3442
3443 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3444 schedule_work(&hba->eh_work);
3445 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303446 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303447 /*
3448 * if (!queue_eh_work) -
3449 * Other errors are either non-fatal where host recovers
3450 * itself without s/w intervention or errors that will be
3451 * handled by the SCSI core layer.
3452 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303453}
3454
3455/**
3456 * ufshcd_tmc_handler - handle task management function completion
3457 * @hba: per adapter instance
3458 */
3459static void ufshcd_tmc_handler(struct ufs_hba *hba)
3460{
3461 u32 tm_doorbell;
3462
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303463 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303464 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303465 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303466}
3467
3468/**
3469 * ufshcd_sl_intr - Interrupt service routine
3470 * @hba: per adapter instance
3471 * @intr_status: contains interrupts generated by the controller
3472 */
3473static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
3474{
3475 hba->errors = UFSHCD_ERROR_MASK & intr_status;
3476 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303477 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303478
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05303479 if (intr_status & UFSHCD_UIC_MASK)
3480 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303481
3482 if (intr_status & UTP_TASK_REQ_COMPL)
3483 ufshcd_tmc_handler(hba);
3484
3485 if (intr_status & UTP_TRANSFER_REQ_COMPL)
3486 ufshcd_transfer_req_compl(hba);
3487}
3488
3489/**
3490 * ufshcd_intr - Main interrupt service routine
3491 * @irq: irq number
3492 * @__hba: pointer to adapter instance
3493 *
3494 * Returns IRQ_HANDLED - If interrupt is valid
3495 * IRQ_NONE - If invalid interrupt
3496 */
3497static irqreturn_t ufshcd_intr(int irq, void *__hba)
3498{
3499 u32 intr_status;
3500 irqreturn_t retval = IRQ_NONE;
3501 struct ufs_hba *hba = __hba;
3502
3503 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303504 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303505
3506 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05303507 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303508 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303509 retval = IRQ_HANDLED;
3510 }
3511 spin_unlock(hba->host->host_lock);
3512 return retval;
3513}
3514
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303515static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
3516{
3517 int err = 0;
3518 u32 mask = 1 << tag;
3519 unsigned long flags;
3520
3521 if (!test_bit(tag, &hba->outstanding_tasks))
3522 goto out;
3523
3524 spin_lock_irqsave(hba->host->host_lock, flags);
3525 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
3526 spin_unlock_irqrestore(hba->host->host_lock, flags);
3527
3528 /* poll for max. 1 sec to clear door bell register by h/w */
3529 err = ufshcd_wait_for_register(hba,
3530 REG_UTP_TASK_REQ_DOOR_BELL,
3531 mask, 0, 1000, 1000);
3532out:
3533 return err;
3534}
3535
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303536/**
3537 * ufshcd_issue_tm_cmd - issues task management commands to controller
3538 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303539 * @lun_id: LUN ID to which TM command is sent
3540 * @task_id: task ID to which the TM command is applicable
3541 * @tm_function: task management function opcode
3542 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303543 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303544 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303545 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303546static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
3547 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303548{
3549 struct utp_task_req_desc *task_req_descp;
3550 struct utp_upiu_task_req *task_req_upiup;
3551 struct Scsi_Host *host;
3552 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303553 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303554 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303555 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303556
3557 host = hba->host;
3558
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303559 /*
3560 * Get free slot, sleep if slots are unavailable.
3561 * Even though we use wait_event() which sleeps indefinitely,
3562 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
3563 */
3564 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003565 ufshcd_hold(hba, false);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303566
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303567 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303568 task_req_descp = hba->utmrdl_base_addr;
3569 task_req_descp += free_slot;
3570
3571 /* Configure task request descriptor */
3572 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
3573 task_req_descp->header.dword_2 =
3574 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
3575
3576 /* Configure task request UPIU */
3577 task_req_upiup =
3578 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303579 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303580 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303581 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303582 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303583 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303584 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03003585 /*
3586 * The host shall provide the same value for LUN field in the basic
3587 * header and for Input Parameter.
3588 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303589 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
3590 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303591
3592 /* send command to the controller */
3593 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05303594 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303595
3596 spin_unlock_irqrestore(host->host_lock, flags);
3597
3598 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303599 err = wait_event_timeout(hba->tm_wq,
3600 test_bit(free_slot, &hba->tm_condition),
3601 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303602 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303603 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
3604 __func__, tm_function);
3605 if (ufshcd_clear_tm_cmd(hba, free_slot))
3606 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
3607 __func__, free_slot);
3608 err = -ETIMEDOUT;
3609 } else {
3610 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303611 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303612
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303613 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303614 ufshcd_put_tm_slot(hba, free_slot);
3615 wake_up(&hba->tm_tag_wq);
3616
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003617 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303618 return err;
3619}
3620
3621/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303622 * ufshcd_eh_device_reset_handler - device reset handler registered to
3623 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303624 * @cmd: SCSI command pointer
3625 *
3626 * Returns SUCCESS/FAILED
3627 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303628static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303629{
3630 struct Scsi_Host *host;
3631 struct ufs_hba *hba;
3632 unsigned int tag;
3633 u32 pos;
3634 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303635 u8 resp = 0xF;
3636 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303637 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303638
3639 host = cmd->device->host;
3640 hba = shost_priv(host);
3641 tag = cmd->request->tag;
3642
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303643 lrbp = &hba->lrb[tag];
3644 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
3645 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303646 if (!err)
3647 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303648 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303649 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303650
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303651 /* clear the commands that were pending for corresponding LUN */
3652 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
3653 if (hba->lrb[pos].lun == lrbp->lun) {
3654 err = ufshcd_clear_cmd(hba, pos);
3655 if (err)
3656 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303657 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303658 }
3659 spin_lock_irqsave(host->host_lock, flags);
3660 ufshcd_transfer_req_compl(hba);
3661 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303662out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303663 if (!err) {
3664 err = SUCCESS;
3665 } else {
3666 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3667 err = FAILED;
3668 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303669 return err;
3670}
3671
3672/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303673 * ufshcd_abort - abort a specific command
3674 * @cmd: SCSI command pointer
3675 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303676 * Abort the pending command in device by sending UFS_ABORT_TASK task management
3677 * command, and in host controller by clearing the door-bell register. There can
3678 * be race between controller sending the command to the device while abort is
3679 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
3680 * really issued and then try to abort it.
3681 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303682 * Returns SUCCESS/FAILED
3683 */
3684static int ufshcd_abort(struct scsi_cmnd *cmd)
3685{
3686 struct Scsi_Host *host;
3687 struct ufs_hba *hba;
3688 unsigned long flags;
3689 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303690 int err = 0;
3691 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303692 u8 resp = 0xF;
3693 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003694 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303695
3696 host = cmd->device->host;
3697 hba = shost_priv(host);
3698 tag = cmd->request->tag;
3699
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003700 ufshcd_hold(hba, false);
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303701 /* If command is already aborted/completed, return SUCCESS */
3702 if (!(test_bit(tag, &hba->outstanding_reqs)))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303703 goto out;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303704
Dolev Ravive9d501b2014-07-01 12:22:37 +03003705 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3706 if (!(reg & (1 << tag))) {
3707 dev_err(hba->dev,
3708 "%s: cmd was completed, but without a notifying intr, tag = %d",
3709 __func__, tag);
3710 }
3711
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303712 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303713 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
3714 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3715 UFS_QUERY_TASK, &resp);
3716 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
3717 /* cmd pending in the device */
3718 break;
3719 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303720 /*
3721 * cmd not pending in the device, check if it is
3722 * in transition.
3723 */
3724 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3725 if (reg & (1 << tag)) {
3726 /* sleep for max. 200us to stabilize */
3727 usleep_range(100, 200);
3728 continue;
3729 }
3730 /* command completed already */
3731 goto out;
3732 } else {
3733 if (!err)
3734 err = resp; /* service response error */
3735 goto out;
3736 }
3737 }
3738
3739 if (!poll_cnt) {
3740 err = -EBUSY;
3741 goto out;
3742 }
3743
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303744 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3745 UFS_ABORT_TASK, &resp);
3746 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303747 if (!err)
3748 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303749 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303750 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303751
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303752 err = ufshcd_clear_cmd(hba, tag);
3753 if (err)
3754 goto out;
3755
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303756 scsi_dma_unmap(cmd);
3757
3758 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303759 __clear_bit(tag, &hba->outstanding_reqs);
3760 hba->lrb[tag].cmd = NULL;
3761 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303762
3763 clear_bit_unlock(tag, &hba->lrb_in_use);
3764 wake_up(&hba->dev_cmd.tag_wq);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003765
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303766out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303767 if (!err) {
3768 err = SUCCESS;
3769 } else {
3770 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3771 err = FAILED;
3772 }
3773
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003774 /*
3775 * This ufshcd_release() corresponds to the original scsi cmd that got
3776 * aborted here (as we won't get any IRQ for it).
3777 */
3778 ufshcd_release(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303779 return err;
3780}
3781
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303782/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303783 * ufshcd_host_reset_and_restore - reset and restore host controller
3784 * @hba: per-adapter instance
3785 *
3786 * Note that host controller reset may issue DME_RESET to
3787 * local and remote (device) Uni-Pro stack and the attributes
3788 * are reset to default state.
3789 *
3790 * Returns zero on success, non-zero on failure
3791 */
3792static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
3793{
3794 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303795 unsigned long flags;
3796
3797 /* Reset the host controller */
3798 spin_lock_irqsave(hba->host->host_lock, flags);
3799 ufshcd_hba_stop(hba);
3800 spin_unlock_irqrestore(hba->host->host_lock, flags);
3801
3802 err = ufshcd_hba_enable(hba);
3803 if (err)
3804 goto out;
3805
3806 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003807 err = ufshcd_probe_hba(hba);
3808
3809 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303810 err = -EIO;
3811out:
3812 if (err)
3813 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
3814
3815 return err;
3816}
3817
3818/**
3819 * ufshcd_reset_and_restore - reset and re-initialize host/device
3820 * @hba: per-adapter instance
3821 *
3822 * Reset and recover device, host and re-establish link. This
3823 * is helpful to recover the communication in fatal error conditions.
3824 *
3825 * Returns zero on success, non-zero on failure
3826 */
3827static int ufshcd_reset_and_restore(struct ufs_hba *hba)
3828{
3829 int err = 0;
3830 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003831 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303832
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003833 do {
3834 err = ufshcd_host_reset_and_restore(hba);
3835 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303836
3837 /*
3838 * After reset the door-bell might be cleared, complete
3839 * outstanding requests in s/w here.
3840 */
3841 spin_lock_irqsave(hba->host->host_lock, flags);
3842 ufshcd_transfer_req_compl(hba);
3843 ufshcd_tmc_handler(hba);
3844 spin_unlock_irqrestore(hba->host->host_lock, flags);
3845
3846 return err;
3847}
3848
3849/**
3850 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
3851 * @cmd - SCSI command pointer
3852 *
3853 * Returns SUCCESS/FAILED
3854 */
3855static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
3856{
3857 int err;
3858 unsigned long flags;
3859 struct ufs_hba *hba;
3860
3861 hba = shost_priv(cmd->device->host);
3862
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003863 ufshcd_hold(hba, false);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303864 /*
3865 * Check if there is any race with fatal error handling.
3866 * If so, wait for it to complete. Even though fatal error
3867 * handling does reset and restore in some cases, don't assume
3868 * anything out of it. We are just avoiding race here.
3869 */
3870 do {
3871 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303872 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303873 hba->ufshcd_state == UFSHCD_STATE_RESET))
3874 break;
3875 spin_unlock_irqrestore(hba->host->host_lock, flags);
3876 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303877 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303878 } while (1);
3879
3880 hba->ufshcd_state = UFSHCD_STATE_RESET;
3881 ufshcd_set_eh_in_progress(hba);
3882 spin_unlock_irqrestore(hba->host->host_lock, flags);
3883
3884 err = ufshcd_reset_and_restore(hba);
3885
3886 spin_lock_irqsave(hba->host->host_lock, flags);
3887 if (!err) {
3888 err = SUCCESS;
3889 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3890 } else {
3891 err = FAILED;
3892 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3893 }
3894 ufshcd_clear_eh_in_progress(hba);
3895 spin_unlock_irqrestore(hba->host->host_lock, flags);
3896
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03003897 ufshcd_release(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303898 return err;
3899}
3900
3901/**
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03003902 * ufshcd_get_max_icc_level - calculate the ICC level
3903 * @sup_curr_uA: max. current supported by the regulator
3904 * @start_scan: row at the desc table to start scan from
3905 * @buff: power descriptor buffer
3906 *
3907 * Returns calculated max ICC level for specific regulator
3908 */
3909static u32 ufshcd_get_max_icc_level(int sup_curr_uA, u32 start_scan, char *buff)
3910{
3911 int i;
3912 int curr_uA;
3913 u16 data;
3914 u16 unit;
3915
3916 for (i = start_scan; i >= 0; i--) {
3917 data = be16_to_cpu(*((u16 *)(buff + 2*i)));
3918 unit = (data & ATTR_ICC_LVL_UNIT_MASK) >>
3919 ATTR_ICC_LVL_UNIT_OFFSET;
3920 curr_uA = data & ATTR_ICC_LVL_VALUE_MASK;
3921 switch (unit) {
3922 case UFSHCD_NANO_AMP:
3923 curr_uA = curr_uA / 1000;
3924 break;
3925 case UFSHCD_MILI_AMP:
3926 curr_uA = curr_uA * 1000;
3927 break;
3928 case UFSHCD_AMP:
3929 curr_uA = curr_uA * 1000 * 1000;
3930 break;
3931 case UFSHCD_MICRO_AMP:
3932 default:
3933 break;
3934 }
3935 if (sup_curr_uA >= curr_uA)
3936 break;
3937 }
3938 if (i < 0) {
3939 i = 0;
3940 pr_err("%s: Couldn't find valid icc_level = %d", __func__, i);
3941 }
3942
3943 return (u32)i;
3944}
3945
3946/**
3947 * ufshcd_calc_icc_level - calculate the max ICC level
3948 * In case regulators are not initialized we'll return 0
3949 * @hba: per-adapter instance
3950 * @desc_buf: power descriptor buffer to extract ICC levels from.
3951 * @len: length of desc_buff
3952 *
3953 * Returns calculated ICC level
3954 */
3955static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba,
3956 u8 *desc_buf, int len)
3957{
3958 u32 icc_level = 0;
3959
3960 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq ||
3961 !hba->vreg_info.vccq2) {
3962 dev_err(hba->dev,
3963 "%s: Regulator capability was not set, actvIccLevel=%d",
3964 __func__, icc_level);
3965 goto out;
3966 }
3967
3968 if (hba->vreg_info.vcc)
3969 icc_level = ufshcd_get_max_icc_level(
3970 hba->vreg_info.vcc->max_uA,
3971 POWER_DESC_MAX_ACTV_ICC_LVLS - 1,
3972 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCC_0]);
3973
3974 if (hba->vreg_info.vccq)
3975 icc_level = ufshcd_get_max_icc_level(
3976 hba->vreg_info.vccq->max_uA,
3977 icc_level,
3978 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ_0]);
3979
3980 if (hba->vreg_info.vccq2)
3981 icc_level = ufshcd_get_max_icc_level(
3982 hba->vreg_info.vccq2->max_uA,
3983 icc_level,
3984 &desc_buf[PWR_DESC_ACTIVE_LVLS_VCCQ2_0]);
3985out:
3986 return icc_level;
3987}
3988
3989static void ufshcd_init_icc_levels(struct ufs_hba *hba)
3990{
3991 int ret;
3992 int buff_len = QUERY_DESC_POWER_MAX_SIZE;
3993 u8 desc_buf[QUERY_DESC_POWER_MAX_SIZE];
3994
3995 ret = ufshcd_read_power_desc(hba, desc_buf, buff_len);
3996 if (ret) {
3997 dev_err(hba->dev,
3998 "%s: Failed reading power descriptor.len = %d ret = %d",
3999 __func__, buff_len, ret);
4000 return;
4001 }
4002
4003 hba->init_prefetch_data.icc_level =
4004 ufshcd_find_max_sup_active_icc_level(hba,
4005 desc_buf, buff_len);
4006 dev_dbg(hba->dev, "%s: setting icc_level 0x%x",
4007 __func__, hba->init_prefetch_data.icc_level);
4008
4009 ret = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
4010 QUERY_ATTR_IDN_ACTIVE_ICC_LVL, 0, 0,
4011 &hba->init_prefetch_data.icc_level);
4012
4013 if (ret)
4014 dev_err(hba->dev,
4015 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
4016 __func__, hba->init_prefetch_data.icc_level , ret);
4017
4018}
4019
4020/**
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004021 * ufshcd_scsi_add_wlus - Adds required W-LUs
4022 * @hba: per-adapter instance
4023 *
4024 * UFS device specification requires the UFS devices to support 4 well known
4025 * logical units:
4026 * "REPORT_LUNS" (address: 01h)
4027 * "UFS Device" (address: 50h)
4028 * "RPMB" (address: 44h)
4029 * "BOOT" (address: 30h)
4030 * UFS device's power management needs to be controlled by "POWER CONDITION"
4031 * field of SSU (START STOP UNIT) command. But this "power condition" field
4032 * will take effect only when its sent to "UFS device" well known logical unit
4033 * hence we require the scsi_device instance to represent this logical unit in
4034 * order for the UFS host driver to send the SSU command for power management.
4035
4036 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
4037 * Block) LU so user space process can control this LU. User space may also
4038 * want to have access to BOOT LU.
4039
4040 * This function adds scsi device instances for each of all well known LUs
4041 * (except "REPORT LUNS" LU).
4042 *
4043 * Returns zero on success (all required W-LUs are added successfully),
4044 * non-zero error value on failure (if failed to add any of the required W-LU).
4045 */
4046static int ufshcd_scsi_add_wlus(struct ufs_hba *hba)
4047{
4048 int ret = 0;
4049
4050 hba->sdev_ufs_device = __scsi_add_device(hba->host, 0, 0,
4051 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN), NULL);
4052 if (IS_ERR(hba->sdev_ufs_device)) {
4053 ret = PTR_ERR(hba->sdev_ufs_device);
4054 hba->sdev_ufs_device = NULL;
4055 goto out;
4056 }
4057
4058 hba->sdev_boot = __scsi_add_device(hba->host, 0, 0,
4059 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN), NULL);
4060 if (IS_ERR(hba->sdev_boot)) {
4061 ret = PTR_ERR(hba->sdev_boot);
4062 hba->sdev_boot = NULL;
4063 goto remove_sdev_ufs_device;
4064 }
4065
4066 hba->sdev_rpmb = __scsi_add_device(hba->host, 0, 0,
4067 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN), NULL);
4068 if (IS_ERR(hba->sdev_rpmb)) {
4069 ret = PTR_ERR(hba->sdev_rpmb);
4070 hba->sdev_rpmb = NULL;
4071 goto remove_sdev_boot;
4072 }
4073 goto out;
4074
4075remove_sdev_boot:
4076 scsi_remove_device(hba->sdev_boot);
4077remove_sdev_ufs_device:
4078 scsi_remove_device(hba->sdev_ufs_device);
4079out:
4080 return ret;
4081}
4082
4083/**
4084 * ufshcd_scsi_remove_wlus - Removes the W-LUs which were added by
4085 * ufshcd_scsi_add_wlus()
4086 * @hba: per-adapter instance
4087 *
4088 */
4089static void ufshcd_scsi_remove_wlus(struct ufs_hba *hba)
4090{
4091 if (hba->sdev_ufs_device) {
4092 scsi_remove_device(hba->sdev_ufs_device);
4093 hba->sdev_ufs_device = NULL;
4094 }
4095
4096 if (hba->sdev_boot) {
4097 scsi_remove_device(hba->sdev_boot);
4098 hba->sdev_boot = NULL;
4099 }
4100
4101 if (hba->sdev_rpmb) {
4102 scsi_remove_device(hba->sdev_rpmb);
4103 hba->sdev_rpmb = NULL;
4104 }
4105}
4106
4107/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004108 * ufshcd_probe_hba - probe hba to detect device and initialize
4109 * @hba: per-adapter instance
4110 *
4111 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304112 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004113static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304114{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304115 int ret;
4116
4117 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304118 if (ret)
4119 goto out;
4120
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004121 /* UniPro link is active now */
4122 ufshcd_set_link_active(hba);
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05304123
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304124 ret = ufshcd_verify_dev_init(hba);
4125 if (ret)
4126 goto out;
4127
Dolev Raviv68078d52013-07-30 00:35:58 +05304128 ret = ufshcd_complete_dev_init(hba);
4129 if (ret)
4130 goto out;
4131
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004132 /* UFS device is also active now */
4133 ufshcd_set_ufs_dev_active(hba);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304134 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304135 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004136 hba->wlun_dev_clr_ua = true;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304137
Dolev Raviv7eb584d2014-09-25 15:32:31 +03004138 if (ufshcd_get_max_pwr_mode(hba)) {
4139 dev_err(hba->dev,
4140 "%s: Failed getting max supported power mode\n",
4141 __func__);
4142 } else {
4143 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info);
4144 if (ret)
4145 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n",
4146 __func__, ret);
4147 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004148
4149 /*
4150 * If we are in error handling context or in power management callbacks
4151 * context, no need to scan the host
4152 */
4153 if (!ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4154 bool flag;
4155
4156 /* clear any previous UFS device information */
4157 memset(&hba->dev_info, 0, sizeof(hba->dev_info));
4158 if (!ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG,
4159 QUERY_FLAG_IDN_PWR_ON_WPE, &flag))
4160 hba->dev_info.f_power_on_wp_en = flag;
4161
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004162 if (!hba->is_init_prefetch)
4163 ufshcd_init_icc_levels(hba);
4164
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03004165 /* Add required well known logical units to scsi mid layer */
4166 if (ufshcd_scsi_add_wlus(hba))
4167 goto out;
4168
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304169 scsi_scan_host(hba->host);
4170 pm_runtime_put_sync(hba->dev);
4171 }
Yaniv Gardi3a4bf062014-09-25 15:32:27 +03004172
4173 if (!hba->is_init_prefetch)
4174 hba->is_init_prefetch = true;
4175
Sahitya Tummala856b3482014-09-25 15:32:34 +03004176 /* Resume devfreq after UFS device is detected */
4177 if (ufshcd_is_clkscaling_enabled(hba))
4178 devfreq_resume_device(hba->devfreq);
4179
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05304180out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004181 /*
4182 * If we failed to initialize the device or the device is not
4183 * present, turn off the power/clocks etc.
4184 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004185 if (ret && !ufshcd_eh_in_progress(hba) && !hba->pm_op_in_progress) {
4186 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004187 ufshcd_hba_exit(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004188 }
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004189
4190 return ret;
4191}
4192
4193/**
4194 * ufshcd_async_scan - asynchronous execution for probing hba
4195 * @data: data pointer to pass to this function
4196 * @cookie: cookie data
4197 */
4198static void ufshcd_async_scan(void *data, async_cookie_t cookie)
4199{
4200 struct ufs_hba *hba = (struct ufs_hba *)data;
4201
4202 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05304203}
4204
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304205static struct scsi_host_template ufshcd_driver_template = {
4206 .module = THIS_MODULE,
4207 .name = UFSHCD,
4208 .proc_name = UFSHCD,
4209 .queuecommand = ufshcd_queuecommand,
4210 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09004211 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304212 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03004213 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304214 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05304215 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
4216 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304217 .this_id = -1,
4218 .sg_tablesize = SG_ALL,
4219 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
4220 .can_queue = UFSHCD_CAN_QUEUE,
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004221 .max_host_blocked = 1,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +01004222 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01004223 .track_queue_depth = 1,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304224};
4225
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004226static int ufshcd_config_vreg_load(struct device *dev, struct ufs_vreg *vreg,
4227 int ua)
4228{
4229 int ret = 0;
4230 struct regulator *reg = vreg->reg;
4231 const char *name = vreg->name;
4232
4233 BUG_ON(!vreg);
4234
4235 ret = regulator_set_optimum_mode(reg, ua);
4236 if (ret >= 0) {
4237 /*
4238 * regulator_set_optimum_mode() returns new regulator
4239 * mode upon success.
4240 */
4241 ret = 0;
4242 } else {
4243 dev_err(dev, "%s: %s set optimum mode(ua=%d) failed, err=%d\n",
4244 __func__, name, ua, ret);
4245 }
4246
4247 return ret;
4248}
4249
4250static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba,
4251 struct ufs_vreg *vreg)
4252{
4253 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA);
4254}
4255
4256static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
4257 struct ufs_vreg *vreg)
4258{
4259 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA);
4260}
4261
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004262static int ufshcd_config_vreg(struct device *dev,
4263 struct ufs_vreg *vreg, bool on)
4264{
4265 int ret = 0;
4266 struct regulator *reg = vreg->reg;
4267 const char *name = vreg->name;
4268 int min_uV, uA_load;
4269
4270 BUG_ON(!vreg);
4271
4272 if (regulator_count_voltages(reg) > 0) {
4273 min_uV = on ? vreg->min_uV : 0;
4274 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
4275 if (ret) {
4276 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
4277 __func__, name, ret);
4278 goto out;
4279 }
4280
4281 uA_load = on ? vreg->max_uA : 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004282 ret = ufshcd_config_vreg_load(dev, vreg, uA_load);
4283 if (ret)
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004284 goto out;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004285 }
4286out:
4287 return ret;
4288}
4289
4290static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
4291{
4292 int ret = 0;
4293
4294 if (!vreg || vreg->enabled)
4295 goto out;
4296
4297 ret = ufshcd_config_vreg(dev, vreg, true);
4298 if (!ret)
4299 ret = regulator_enable(vreg->reg);
4300
4301 if (!ret)
4302 vreg->enabled = true;
4303 else
4304 dev_err(dev, "%s: %s enable failed, err=%d\n",
4305 __func__, vreg->name, ret);
4306out:
4307 return ret;
4308}
4309
4310static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
4311{
4312 int ret = 0;
4313
4314 if (!vreg || !vreg->enabled)
4315 goto out;
4316
4317 ret = regulator_disable(vreg->reg);
4318
4319 if (!ret) {
4320 /* ignore errors on applying disable config */
4321 ufshcd_config_vreg(dev, vreg, false);
4322 vreg->enabled = false;
4323 } else {
4324 dev_err(dev, "%s: %s disable failed, err=%d\n",
4325 __func__, vreg->name, ret);
4326 }
4327out:
4328 return ret;
4329}
4330
4331static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
4332{
4333 int ret = 0;
4334 struct device *dev = hba->dev;
4335 struct ufs_vreg_info *info = &hba->vreg_info;
4336
4337 if (!info)
4338 goto out;
4339
4340 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
4341 if (ret)
4342 goto out;
4343
4344 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
4345 if (ret)
4346 goto out;
4347
4348 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
4349 if (ret)
4350 goto out;
4351
4352out:
4353 if (ret) {
4354 ufshcd_toggle_vreg(dev, info->vccq2, false);
4355 ufshcd_toggle_vreg(dev, info->vccq, false);
4356 ufshcd_toggle_vreg(dev, info->vcc, false);
4357 }
4358 return ret;
4359}
4360
Raviv Shvili6a771a62014-09-25 15:32:24 +03004361static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
4362{
4363 struct ufs_vreg_info *info = &hba->vreg_info;
4364
4365 if (info)
4366 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
4367
4368 return 0;
4369}
4370
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004371static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
4372{
4373 int ret = 0;
4374
4375 if (!vreg)
4376 goto out;
4377
4378 vreg->reg = devm_regulator_get(dev, vreg->name);
4379 if (IS_ERR(vreg->reg)) {
4380 ret = PTR_ERR(vreg->reg);
4381 dev_err(dev, "%s: %s get failed, err=%d\n",
4382 __func__, vreg->name, ret);
4383 }
4384out:
4385 return ret;
4386}
4387
4388static int ufshcd_init_vreg(struct ufs_hba *hba)
4389{
4390 int ret = 0;
4391 struct device *dev = hba->dev;
4392 struct ufs_vreg_info *info = &hba->vreg_info;
4393
4394 if (!info)
4395 goto out;
4396
4397 ret = ufshcd_get_vreg(dev, info->vcc);
4398 if (ret)
4399 goto out;
4400
4401 ret = ufshcd_get_vreg(dev, info->vccq);
4402 if (ret)
4403 goto out;
4404
4405 ret = ufshcd_get_vreg(dev, info->vccq2);
4406out:
4407 return ret;
4408}
4409
Raviv Shvili6a771a62014-09-25 15:32:24 +03004410static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
4411{
4412 struct ufs_vreg_info *info = &hba->vreg_info;
4413
4414 if (info)
4415 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
4416
4417 return 0;
4418}
4419
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004420static int __ufshcd_setup_clocks(struct ufs_hba *hba, bool on,
4421 bool skip_ref_clk)
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004422{
4423 int ret = 0;
4424 struct ufs_clk_info *clki;
4425 struct list_head *head = &hba->clk_list_head;
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004426 unsigned long flags;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004427
4428 if (!head || list_empty(head))
4429 goto out;
4430
4431 list_for_each_entry(clki, head, list) {
4432 if (!IS_ERR_OR_NULL(clki->clk)) {
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004433 if (skip_ref_clk && !strcmp(clki->name, "ref_clk"))
4434 continue;
4435
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004436 if (on && !clki->enabled) {
4437 ret = clk_prepare_enable(clki->clk);
4438 if (ret) {
4439 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
4440 __func__, clki->name, ret);
4441 goto out;
4442 }
4443 } else if (!on && clki->enabled) {
4444 clk_disable_unprepare(clki->clk);
4445 }
4446 clki->enabled = on;
4447 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
4448 clki->name, on ? "en" : "dis");
4449 }
4450 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004451
4452 if (hba->vops && hba->vops->setup_clocks)
4453 ret = hba->vops->setup_clocks(hba, on);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004454out:
4455 if (ret) {
4456 list_for_each_entry(clki, head, list) {
4457 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
4458 clk_disable_unprepare(clki->clk);
4459 }
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004460 } else if (!ret && on) {
4461 spin_lock_irqsave(hba->host->host_lock, flags);
4462 hba->clk_gating.state = CLKS_ON;
4463 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004464 }
4465 return ret;
4466}
4467
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004468static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
4469{
4470 return __ufshcd_setup_clocks(hba, on, false);
4471}
4472
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004473static int ufshcd_init_clocks(struct ufs_hba *hba)
4474{
4475 int ret = 0;
4476 struct ufs_clk_info *clki;
4477 struct device *dev = hba->dev;
4478 struct list_head *head = &hba->clk_list_head;
4479
4480 if (!head || list_empty(head))
4481 goto out;
4482
4483 list_for_each_entry(clki, head, list) {
4484 if (!clki->name)
4485 continue;
4486
4487 clki->clk = devm_clk_get(dev, clki->name);
4488 if (IS_ERR(clki->clk)) {
4489 ret = PTR_ERR(clki->clk);
4490 dev_err(dev, "%s: %s clk get failed, %d\n",
4491 __func__, clki->name, ret);
4492 goto out;
4493 }
4494
4495 if (clki->max_freq) {
4496 ret = clk_set_rate(clki->clk, clki->max_freq);
4497 if (ret) {
4498 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
4499 __func__, clki->name,
4500 clki->max_freq, ret);
4501 goto out;
4502 }
Sahitya Tummala856b3482014-09-25 15:32:34 +03004503 clki->curr_freq = clki->max_freq;
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004504 }
4505 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
4506 clki->name, clk_get_rate(clki->clk));
4507 }
4508out:
4509 return ret;
4510}
4511
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004512static int ufshcd_variant_hba_init(struct ufs_hba *hba)
4513{
4514 int err = 0;
4515
4516 if (!hba->vops)
4517 goto out;
4518
4519 if (hba->vops->init) {
4520 err = hba->vops->init(hba);
4521 if (err)
4522 goto out;
4523 }
4524
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004525 if (hba->vops->setup_regulators) {
4526 err = hba->vops->setup_regulators(hba, true);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004527 if (err)
4528 goto out_exit;
4529 }
4530
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004531 goto out;
4532
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03004533out_exit:
4534 if (hba->vops->exit)
4535 hba->vops->exit(hba);
4536out:
4537 if (err)
4538 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
4539 __func__, hba->vops ? hba->vops->name : "", err);
4540 return err;
4541}
4542
4543static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
4544{
4545 if (!hba->vops)
4546 return;
4547
4548 if (hba->vops->setup_clocks)
4549 hba->vops->setup_clocks(hba, false);
4550
4551 if (hba->vops->setup_regulators)
4552 hba->vops->setup_regulators(hba, false);
4553
4554 if (hba->vops->exit)
4555 hba->vops->exit(hba);
4556}
4557
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004558static int ufshcd_hba_init(struct ufs_hba *hba)
4559{
4560 int err;
4561
Raviv Shvili6a771a62014-09-25 15:32:24 +03004562 /*
4563 * Handle host controller power separately from the UFS device power
4564 * rails as it will help controlling the UFS host controller power
4565 * collapse easily which is different than UFS device power collapse.
4566 * Also, enable the host controller power before we go ahead with rest
4567 * of the initialization here.
4568 */
4569 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004570 if (err)
4571 goto out;
4572
Raviv Shvili6a771a62014-09-25 15:32:24 +03004573 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004574 if (err)
4575 goto out;
4576
Raviv Shvili6a771a62014-09-25 15:32:24 +03004577 err = ufshcd_init_clocks(hba);
4578 if (err)
4579 goto out_disable_hba_vreg;
4580
4581 err = ufshcd_setup_clocks(hba, true);
4582 if (err)
4583 goto out_disable_hba_vreg;
4584
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004585 err = ufshcd_init_vreg(hba);
4586 if (err)
4587 goto out_disable_clks;
4588
4589 err = ufshcd_setup_vreg(hba, true);
4590 if (err)
4591 goto out_disable_clks;
4592
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004593 err = ufshcd_variant_hba_init(hba);
4594 if (err)
4595 goto out_disable_vreg;
4596
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004597 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004598 goto out;
4599
4600out_disable_vreg:
4601 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03004602out_disable_clks:
4603 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03004604out_disable_hba_vreg:
4605 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004606out:
4607 return err;
4608}
4609
4610static void ufshcd_hba_exit(struct ufs_hba *hba)
4611{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03004612 if (hba->is_powered) {
4613 ufshcd_variant_hba_exit(hba);
4614 ufshcd_setup_vreg(hba, false);
4615 ufshcd_setup_clocks(hba, false);
4616 ufshcd_setup_hba_vreg(hba, false);
4617 hba->is_powered = false;
4618 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03004619}
4620
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004621static int
4622ufshcd_send_request_sense(struct ufs_hba *hba, struct scsi_device *sdp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304623{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004624 unsigned char cmd[6] = {REQUEST_SENSE,
4625 0,
4626 0,
4627 0,
4628 SCSI_SENSE_BUFFERSIZE,
4629 0};
4630 char *buffer;
4631 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304632
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004633 buffer = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL);
4634 if (!buffer) {
4635 ret = -ENOMEM;
4636 goto out;
4637 }
4638
4639 ret = scsi_execute_req_flags(sdp, cmd, DMA_FROM_DEVICE, buffer,
4640 SCSI_SENSE_BUFFERSIZE, NULL,
4641 msecs_to_jiffies(1000), 3, NULL, REQ_PM);
4642 if (ret)
4643 pr_err("%s: failed with err %d\n", __func__, ret);
4644
4645 kfree(buffer);
4646out:
4647 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304648}
4649
4650/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004651 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
4652 * power mode
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304653 * @hba: per adapter instance
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004654 * @pwr_mode: device power mode to set
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304655 *
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004656 * Returns 0 if requested power mode is set successfully
4657 * Returns non-zero if failed to set the requested power mode
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304658 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004659static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba,
4660 enum ufs_dev_pwr_mode pwr_mode)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304661{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004662 unsigned char cmd[6] = { START_STOP };
4663 struct scsi_sense_hdr sshdr;
4664 struct scsi_device *sdp = hba->sdev_ufs_device;
4665 int ret;
4666
4667 if (!sdp || !scsi_device_online(sdp))
4668 return -ENODEV;
4669
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304670 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004671 * If scsi commands fail, the scsi mid-layer schedules scsi error-
4672 * handling, which would wait for host to be resumed. Since we know
4673 * we are functional while we are here, skip host resume in error
4674 * handling context.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304675 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004676 hba->host->eh_noresume = 1;
4677 if (hba->wlun_dev_clr_ua) {
4678 ret = ufshcd_send_request_sense(hba, sdp);
4679 if (ret)
4680 goto out;
4681 /* Unit attention condition is cleared now */
4682 hba->wlun_dev_clr_ua = false;
4683 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304684
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004685 cmd[4] = pwr_mode << 4;
4686
4687 /*
4688 * Current function would be generally called from the power management
4689 * callbacks hence set the REQ_PM flag so that it doesn't resume the
4690 * already suspended childs.
4691 */
4692 ret = scsi_execute_req_flags(sdp, cmd, DMA_NONE, NULL, 0, &sshdr,
4693 START_STOP_TIMEOUT, 0, NULL, REQ_PM);
4694 if (ret) {
4695 sdev_printk(KERN_WARNING, sdp,
Hannes Reineckeef613292014-10-24 14:27:00 +02004696 "START_STOP failed for power mode: %d, result %x\n",
4697 pwr_mode, ret);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004698 if (driver_byte(ret) & DRIVER_SENSE) {
Hannes Reinecked811b842014-10-24 14:26:45 +02004699 scsi_show_sense_hdr(sdp, NULL, &sshdr);
4700 scsi_show_extd_sense(sdp, NULL, sshdr.asc, sshdr.ascq);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004701 }
4702 }
4703
4704 if (!ret)
4705 hba->curr_dev_pwr_mode = pwr_mode;
4706out:
4707 hba->host->eh_noresume = 0;
4708 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05304709}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05304710
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004711static int ufshcd_link_state_transition(struct ufs_hba *hba,
4712 enum uic_link_state req_link_state,
4713 int check_for_bkops)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304714{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004715 int ret = 0;
4716
4717 if (req_link_state == hba->uic_link_state)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304718 return 0;
4719
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004720 if (req_link_state == UIC_LINK_HIBERN8_STATE) {
4721 ret = ufshcd_uic_hibern8_enter(hba);
4722 if (!ret)
4723 ufshcd_set_link_hibern8(hba);
4724 else
4725 goto out;
4726 }
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304727 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004728 * If autobkops is enabled, link can't be turned off because
4729 * turning off the link would also turn off the device.
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05304730 */
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004731 else if ((req_link_state == UIC_LINK_OFF_STATE) &&
4732 (!check_for_bkops || (check_for_bkops &&
4733 !hba->auto_bkops_enabled))) {
4734 /*
4735 * Change controller state to "reset state" which
4736 * should also put the link in off/reset state
4737 */
4738 ufshcd_hba_stop(hba);
4739 /*
4740 * TODO: Check if we need any delay to make sure that
4741 * controller is reset
4742 */
4743 ufshcd_set_link_off(hba);
4744 }
4745
4746out:
4747 return ret;
4748}
4749
4750static void ufshcd_vreg_set_lpm(struct ufs_hba *hba)
4751{
4752 /*
4753 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
4754 * power.
4755 *
4756 * If UFS device and link is in OFF state, all power supplies (VCC,
4757 * VCCQ, VCCQ2) can be turned off if power on write protect is not
4758 * required. If UFS link is inactive (Hibern8 or OFF state) and device
4759 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
4760 *
4761 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
4762 * in low power state which would save some power.
4763 */
4764 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
4765 !hba->dev_info.is_lu_power_on_wp) {
4766 ufshcd_setup_vreg(hba, false);
4767 } else if (!ufshcd_is_ufs_dev_active(hba)) {
4768 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
4769 if (!ufshcd_is_link_active(hba)) {
4770 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
4771 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2);
4772 }
4773 }
4774}
4775
4776static int ufshcd_vreg_set_hpm(struct ufs_hba *hba)
4777{
4778 int ret = 0;
4779
4780 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) &&
4781 !hba->dev_info.is_lu_power_on_wp) {
4782 ret = ufshcd_setup_vreg(hba, true);
4783 } else if (!ufshcd_is_ufs_dev_active(hba)) {
4784 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true);
4785 if (!ret && !ufshcd_is_link_active(hba)) {
4786 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq);
4787 if (ret)
4788 goto vcc_disable;
4789 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2);
4790 if (ret)
4791 goto vccq_lpm;
4792 }
4793 }
4794 goto out;
4795
4796vccq_lpm:
4797 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq);
4798vcc_disable:
4799 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false);
4800out:
4801 return ret;
4802}
4803
4804static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba)
4805{
4806 if (ufshcd_is_link_off(hba))
4807 ufshcd_setup_hba_vreg(hba, false);
4808}
4809
4810static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba)
4811{
4812 if (ufshcd_is_link_off(hba))
4813 ufshcd_setup_hba_vreg(hba, true);
4814}
4815
4816/**
4817 * ufshcd_suspend - helper function for suspend operations
4818 * @hba: per adapter instance
4819 * @pm_op: desired low power operation type
4820 *
4821 * This function will try to put the UFS device and link into low power
4822 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
4823 * (System PM level).
4824 *
4825 * If this function is called during shutdown, it will make sure that
4826 * both UFS device and UFS link is powered off.
4827 *
4828 * NOTE: UFS device & link must be active before we enter in this function.
4829 *
4830 * Returns 0 for success and non-zero for failure
4831 */
4832static int ufshcd_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op)
4833{
4834 int ret = 0;
4835 enum ufs_pm_level pm_lvl;
4836 enum ufs_dev_pwr_mode req_dev_pwr_mode;
4837 enum uic_link_state req_link_state;
4838
4839 hba->pm_op_in_progress = 1;
4840 if (!ufshcd_is_shutdown_pm(pm_op)) {
4841 pm_lvl = ufshcd_is_runtime_pm(pm_op) ?
4842 hba->rpm_lvl : hba->spm_lvl;
4843 req_dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl);
4844 req_link_state = ufs_get_pm_lvl_to_link_pwr_state(pm_lvl);
4845 } else {
4846 req_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE;
4847 req_link_state = UIC_LINK_OFF_STATE;
4848 }
4849
4850 /*
4851 * If we can't transition into any of the low power modes
4852 * just gate the clocks.
4853 */
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004854 ufshcd_hold(hba, false);
4855 hba->clk_gating.is_suspended = true;
4856
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004857 if (req_dev_pwr_mode == UFS_ACTIVE_PWR_MODE &&
4858 req_link_state == UIC_LINK_ACTIVE_STATE) {
4859 goto disable_clks;
4860 }
4861
4862 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) &&
4863 (req_link_state == hba->uic_link_state))
4864 goto out;
4865
4866 /* UFS device & link must be active before we enter in this function */
4867 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) {
4868 ret = -EINVAL;
4869 goto out;
4870 }
4871
4872 if (ufshcd_is_runtime_pm(pm_op)) {
Subhash Jadavani374a2462014-09-25 15:32:35 +03004873 if (ufshcd_can_autobkops_during_suspend(hba)) {
4874 /*
4875 * The device is idle with no requests in the queue,
4876 * allow background operations if bkops status shows
4877 * that performance might be impacted.
4878 */
4879 ret = ufshcd_urgent_bkops(hba);
4880 if (ret)
4881 goto enable_gating;
4882 } else {
4883 /* make sure that auto bkops is disabled */
4884 ufshcd_disable_auto_bkops(hba);
4885 }
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004886 }
4887
4888 if ((req_dev_pwr_mode != hba->curr_dev_pwr_mode) &&
4889 ((ufshcd_is_runtime_pm(pm_op) && !hba->auto_bkops_enabled) ||
4890 !ufshcd_is_runtime_pm(pm_op))) {
4891 /* ensure that bkops is disabled */
4892 ufshcd_disable_auto_bkops(hba);
4893 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode);
4894 if (ret)
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004895 goto enable_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004896 }
4897
4898 ret = ufshcd_link_state_transition(hba, req_link_state, 1);
4899 if (ret)
4900 goto set_dev_active;
4901
4902 ufshcd_vreg_set_lpm(hba);
4903
4904disable_clks:
4905 /*
Sahitya Tummala856b3482014-09-25 15:32:34 +03004906 * The clock scaling needs access to controller registers. Hence, Wait
4907 * for pending clock scaling work to be done before clocks are
4908 * turned off.
4909 */
4910 if (ufshcd_is_clkscaling_enabled(hba)) {
4911 devfreq_suspend_device(hba->devfreq);
4912 hba->clk_scaling.window_start_t = 0;
4913 }
4914 /*
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004915 * Call vendor specific suspend callback. As these callbacks may access
4916 * vendor specific host controller register space call them before the
4917 * host clocks are ON.
4918 */
4919 if (hba->vops && hba->vops->suspend) {
4920 ret = hba->vops->suspend(hba, pm_op);
4921 if (ret)
4922 goto set_link_active;
4923 }
4924
4925 if (hba->vops && hba->vops->setup_clocks) {
4926 ret = hba->vops->setup_clocks(hba, false);
4927 if (ret)
4928 goto vops_resume;
4929 }
4930
4931 if (!ufshcd_is_link_active(hba))
4932 ufshcd_setup_clocks(hba, false);
4933 else
4934 /* If link is active, device ref_clk can't be switched off */
4935 __ufshcd_setup_clocks(hba, false, true);
4936
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004937 hba->clk_gating.state = CLKS_OFF;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004938 /*
4939 * Disable the host irq as host controller as there won't be any
4940 * host controller trasanction expected till resume.
4941 */
4942 ufshcd_disable_irq(hba);
4943 /* Put the host controller in low power mode if possible */
4944 ufshcd_hba_vreg_set_lpm(hba);
4945 goto out;
4946
4947vops_resume:
4948 if (hba->vops && hba->vops->resume)
4949 hba->vops->resume(hba, pm_op);
4950set_link_active:
4951 ufshcd_vreg_set_hpm(hba);
4952 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba))
4953 ufshcd_set_link_active(hba);
4954 else if (ufshcd_is_link_off(hba))
4955 ufshcd_host_reset_and_restore(hba);
4956set_dev_active:
4957 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE))
4958 ufshcd_disable_auto_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03004959enable_gating:
4960 hba->clk_gating.is_suspended = false;
4961 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004962out:
4963 hba->pm_op_in_progress = 0;
4964 return ret;
4965}
4966
4967/**
4968 * ufshcd_resume - helper function for resume operations
4969 * @hba: per adapter instance
4970 * @pm_op: runtime PM or system PM
4971 *
4972 * This function basically brings the UFS device, UniPro link and controller
4973 * to active state.
4974 *
4975 * Returns 0 for success and non-zero for failure
4976 */
4977static int ufshcd_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op)
4978{
4979 int ret;
4980 enum uic_link_state old_link_state;
4981
4982 hba->pm_op_in_progress = 1;
4983 old_link_state = hba->uic_link_state;
4984
4985 ufshcd_hba_vreg_set_hpm(hba);
4986 /* Make sure clocks are enabled before accessing controller */
4987 ret = ufshcd_setup_clocks(hba, true);
4988 if (ret)
4989 goto out;
4990
Subhash Jadavani57d104c2014-09-25 15:32:30 +03004991 /* enable the host irq as host controller would be active soon */
4992 ret = ufshcd_enable_irq(hba);
4993 if (ret)
4994 goto disable_irq_and_vops_clks;
4995
4996 ret = ufshcd_vreg_set_hpm(hba);
4997 if (ret)
4998 goto disable_irq_and_vops_clks;
4999
5000 /*
5001 * Call vendor specific resume callback. As these callbacks may access
5002 * vendor specific host controller register space call them when the
5003 * host clocks are ON.
5004 */
5005 if (hba->vops && hba->vops->resume) {
5006 ret = hba->vops->resume(hba, pm_op);
5007 if (ret)
5008 goto disable_vreg;
5009 }
5010
5011 if (ufshcd_is_link_hibern8(hba)) {
5012 ret = ufshcd_uic_hibern8_exit(hba);
5013 if (!ret)
5014 ufshcd_set_link_active(hba);
5015 else
5016 goto vendor_suspend;
5017 } else if (ufshcd_is_link_off(hba)) {
5018 ret = ufshcd_host_reset_and_restore(hba);
5019 /*
5020 * ufshcd_host_reset_and_restore() should have already
5021 * set the link state as active
5022 */
5023 if (ret || !ufshcd_is_link_active(hba))
5024 goto vendor_suspend;
5025 }
5026
5027 if (!ufshcd_is_ufs_dev_active(hba)) {
5028 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE);
5029 if (ret)
5030 goto set_old_link_state;
5031 }
5032
Subhash Jadavani374a2462014-09-25 15:32:35 +03005033 /*
5034 * If BKOPs operations are urgently needed at this moment then
5035 * keep auto-bkops enabled or else disable it.
5036 */
5037 ufshcd_urgent_bkops(hba);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005038 hba->clk_gating.is_suspended = false;
5039
Sahitya Tummala856b3482014-09-25 15:32:34 +03005040 if (ufshcd_is_clkscaling_enabled(hba))
5041 devfreq_resume_device(hba->devfreq);
5042
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005043 /* Schedule clock gating in case of no access to UFS device yet */
5044 ufshcd_release(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005045 goto out;
5046
5047set_old_link_state:
5048 ufshcd_link_state_transition(hba, old_link_state, 0);
5049vendor_suspend:
5050 if (hba->vops && hba->vops->suspend)
5051 hba->vops->suspend(hba, pm_op);
5052disable_vreg:
5053 ufshcd_vreg_set_lpm(hba);
5054disable_irq_and_vops_clks:
5055 ufshcd_disable_irq(hba);
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005056 ufshcd_setup_clocks(hba, false);
5057out:
5058 hba->pm_op_in_progress = 0;
5059 return ret;
5060}
5061
5062/**
5063 * ufshcd_system_suspend - system suspend routine
5064 * @hba: per adapter instance
5065 * @pm_op: runtime PM or system PM
5066 *
5067 * Check the description of ufshcd_suspend() function for more details.
5068 *
5069 * Returns 0 for success and non-zero for failure
5070 */
5071int ufshcd_system_suspend(struct ufs_hba *hba)
5072{
5073 int ret = 0;
5074
5075 if (!hba || !hba->is_powered)
5076 goto out;
5077
5078 if (pm_runtime_suspended(hba->dev)) {
5079 if (hba->rpm_lvl == hba->spm_lvl)
5080 /*
5081 * There is possibility that device may still be in
5082 * active state during the runtime suspend.
5083 */
5084 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl) ==
5085 hba->curr_dev_pwr_mode) && !hba->auto_bkops_enabled)
5086 goto out;
5087
5088 /*
5089 * UFS device and/or UFS link low power states during runtime
5090 * suspend seems to be different than what is expected during
5091 * system suspend. Hence runtime resume the devic & link and
5092 * let the system suspend low power states to take effect.
5093 * TODO: If resume takes longer time, we might have optimize
5094 * it in future by not resuming everything if possible.
5095 */
5096 ret = ufshcd_runtime_resume(hba);
5097 if (ret)
5098 goto out;
5099 }
5100
5101 ret = ufshcd_suspend(hba, UFS_SYSTEM_PM);
5102out:
Dolev Ravive7850602014-09-25 15:32:36 +03005103 if (!ret)
5104 hba->is_sys_suspended = true;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005105 return ret;
5106}
5107EXPORT_SYMBOL(ufshcd_system_suspend);
5108
5109/**
5110 * ufshcd_system_resume - system resume routine
5111 * @hba: per adapter instance
5112 *
5113 * Returns 0 for success and non-zero for failure
5114 */
5115
5116int ufshcd_system_resume(struct ufs_hba *hba)
5117{
5118 if (!hba || !hba->is_powered || pm_runtime_suspended(hba->dev))
5119 /*
5120 * Let the runtime resume take care of resuming
5121 * if runtime suspended.
5122 */
5123 return 0;
5124
5125 return ufshcd_resume(hba, UFS_SYSTEM_PM);
5126}
5127EXPORT_SYMBOL(ufshcd_system_resume);
5128
5129/**
5130 * ufshcd_runtime_suspend - runtime suspend routine
5131 * @hba: per adapter instance
5132 *
5133 * Check the description of ufshcd_suspend() function for more details.
5134 *
5135 * Returns 0 for success and non-zero for failure
5136 */
5137int ufshcd_runtime_suspend(struct ufs_hba *hba)
5138{
5139 if (!hba || !hba->is_powered)
5140 return 0;
5141
5142 return ufshcd_suspend(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305143}
5144EXPORT_SYMBOL(ufshcd_runtime_suspend);
5145
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005146/**
5147 * ufshcd_runtime_resume - runtime resume routine
5148 * @hba: per adapter instance
5149 *
5150 * This function basically brings the UFS device, UniPro link and controller
5151 * to active state. Following operations are done in this function:
5152 *
5153 * 1. Turn on all the controller related clocks
5154 * 2. Bring the UniPro link out of Hibernate state
5155 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
5156 * to active state.
5157 * 4. If auto-bkops is enabled on the device, disable it.
5158 *
5159 * So following would be the possible power state after this function return
5160 * successfully:
5161 * S1: UFS device in Active state with VCC rail ON
5162 * UniPro link in Active state
5163 * All the UFS/UniPro controller clocks are ON
5164 *
5165 * Returns 0 for success and non-zero for failure
5166 */
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305167int ufshcd_runtime_resume(struct ufs_hba *hba)
5168{
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005169 if (!hba || !hba->is_powered)
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305170 return 0;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005171 else
5172 return ufshcd_resume(hba, UFS_RUNTIME_PM);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305173}
5174EXPORT_SYMBOL(ufshcd_runtime_resume);
5175
5176int ufshcd_runtime_idle(struct ufs_hba *hba)
5177{
5178 return 0;
5179}
5180EXPORT_SYMBOL(ufshcd_runtime_idle);
5181
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305182/**
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005183 * ufshcd_shutdown - shutdown routine
5184 * @hba: per adapter instance
5185 *
5186 * This function would power off both UFS device and UFS link.
5187 *
5188 * Returns 0 always to allow force shutdown even in case of errors.
5189 */
5190int ufshcd_shutdown(struct ufs_hba *hba)
5191{
5192 int ret = 0;
5193
5194 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba))
5195 goto out;
5196
5197 if (pm_runtime_suspended(hba->dev)) {
5198 ret = ufshcd_runtime_resume(hba);
5199 if (ret)
5200 goto out;
5201 }
5202
5203 ret = ufshcd_suspend(hba, UFS_SHUTDOWN_PM);
5204out:
5205 if (ret)
5206 dev_err(hba->dev, "%s failed, err %d\n", __func__, ret);
5207 /* allow force shutdown even in case of errors */
5208 return 0;
5209}
5210EXPORT_SYMBOL(ufshcd_shutdown);
5211
5212/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305213 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305214 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305215 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305216 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305217void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305218{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05305219 scsi_remove_host(hba->host);
Subhash Jadavani2a8fa602014-09-25 15:32:28 +03005220 ufshcd_scsi_remove_wlus(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305221 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305222 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305223 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305224
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305225 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005226
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005227 ufshcd_exit_clk_gating(hba);
Sahitya Tummala856b3482014-09-25 15:32:34 +03005228 if (ufshcd_is_clkscaling_enabled(hba))
5229 devfreq_remove_device(hba->devfreq);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005230 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305231}
5232EXPORT_SYMBOL_GPL(ufshcd_remove);
5233
5234/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005235 * ufshcd_set_dma_mask - Set dma mask based on the controller
5236 * addressing capability
5237 * @hba: per adapter instance
5238 *
5239 * Returns 0 for success, non-zero for failure
5240 */
5241static int ufshcd_set_dma_mask(struct ufs_hba *hba)
5242{
5243 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
5244 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
5245 return 0;
5246 }
5247 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
5248}
5249
5250/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005251 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305252 * @dev: pointer to device handle
5253 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305254 * Returns 0 on success, non-zero value on failure
5255 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005256int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305257{
5258 struct Scsi_Host *host;
5259 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005260 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305261
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305262 if (!dev) {
5263 dev_err(dev,
5264 "Invalid memory reference for dev is NULL\n");
5265 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305266 goto out_error;
5267 }
5268
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305269 host = scsi_host_alloc(&ufshcd_driver_template,
5270 sizeof(struct ufs_hba));
5271 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305272 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305273 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305274 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305275 }
5276 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305277 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305278 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005279 *hba_handle = hba;
5280
5281out_error:
5282 return err;
5283}
5284EXPORT_SYMBOL(ufshcd_alloc_host);
5285
Sahitya Tummala856b3482014-09-25 15:32:34 +03005286static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up)
5287{
5288 int ret = 0;
5289 struct ufs_clk_info *clki;
5290 struct list_head *head = &hba->clk_list_head;
5291
5292 if (!head || list_empty(head))
5293 goto out;
5294
5295 list_for_each_entry(clki, head, list) {
5296 if (!IS_ERR_OR_NULL(clki->clk)) {
5297 if (scale_up && clki->max_freq) {
5298 if (clki->curr_freq == clki->max_freq)
5299 continue;
5300 ret = clk_set_rate(clki->clk, clki->max_freq);
5301 if (ret) {
5302 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5303 __func__, clki->name,
5304 clki->max_freq, ret);
5305 break;
5306 }
5307 clki->curr_freq = clki->max_freq;
5308
5309 } else if (!scale_up && clki->min_freq) {
5310 if (clki->curr_freq == clki->min_freq)
5311 continue;
5312 ret = clk_set_rate(clki->clk, clki->min_freq);
5313 if (ret) {
5314 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
5315 __func__, clki->name,
5316 clki->min_freq, ret);
5317 break;
5318 }
5319 clki->curr_freq = clki->min_freq;
5320 }
5321 }
5322 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__,
5323 clki->name, clk_get_rate(clki->clk));
5324 }
5325 if (hba->vops->clk_scale_notify)
5326 hba->vops->clk_scale_notify(hba);
5327out:
5328 return ret;
5329}
5330
5331static int ufshcd_devfreq_target(struct device *dev,
5332 unsigned long *freq, u32 flags)
5333{
5334 int err = 0;
5335 struct ufs_hba *hba = dev_get_drvdata(dev);
5336
5337 if (!ufshcd_is_clkscaling_enabled(hba))
5338 return -EINVAL;
5339
5340 if (*freq == UINT_MAX)
5341 err = ufshcd_scale_clks(hba, true);
5342 else if (*freq == 0)
5343 err = ufshcd_scale_clks(hba, false);
5344
5345 return err;
5346}
5347
5348static int ufshcd_devfreq_get_dev_status(struct device *dev,
5349 struct devfreq_dev_status *stat)
5350{
5351 struct ufs_hba *hba = dev_get_drvdata(dev);
5352 struct ufs_clk_scaling *scaling = &hba->clk_scaling;
5353 unsigned long flags;
5354
5355 if (!ufshcd_is_clkscaling_enabled(hba))
5356 return -EINVAL;
5357
5358 memset(stat, 0, sizeof(*stat));
5359
5360 spin_lock_irqsave(hba->host->host_lock, flags);
5361 if (!scaling->window_start_t)
5362 goto start_window;
5363
5364 if (scaling->is_busy_started)
5365 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(),
5366 scaling->busy_start_t));
5367
5368 stat->total_time = jiffies_to_usecs((long)jiffies -
5369 (long)scaling->window_start_t);
5370 stat->busy_time = scaling->tot_busy_t;
5371start_window:
5372 scaling->window_start_t = jiffies;
5373 scaling->tot_busy_t = 0;
5374
5375 if (hba->outstanding_reqs) {
5376 scaling->busy_start_t = ktime_get();
5377 scaling->is_busy_started = true;
5378 } else {
5379 scaling->busy_start_t = ktime_set(0, 0);
5380 scaling->is_busy_started = false;
5381 }
5382 spin_unlock_irqrestore(hba->host->host_lock, flags);
5383 return 0;
5384}
5385
5386static struct devfreq_dev_profile ufs_devfreq_profile = {
5387 .polling_ms = 100,
5388 .target = ufshcd_devfreq_target,
5389 .get_dev_status = ufshcd_devfreq_get_dev_status,
5390};
5391
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005392/**
5393 * ufshcd_init - Driver initialization routine
5394 * @hba: per-adapter instance
5395 * @mmio_base: base register address
5396 * @irq: Interrupt line of device
5397 * Returns 0 on success, non-zero value on failure
5398 */
5399int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
5400{
5401 int err;
5402 struct Scsi_Host *host = hba->host;
5403 struct device *dev = hba->dev;
5404
5405 if (!mmio_base) {
5406 dev_err(hba->dev,
5407 "Invalid memory reference for mmio_base is NULL\n");
5408 err = -ENODEV;
5409 goto out_error;
5410 }
5411
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305412 hba->mmio_base = mmio_base;
5413 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305414
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005415 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03005416 if (err)
5417 goto out_error;
5418
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305419 /* Read capabilities registers */
5420 ufshcd_hba_capabilities(hba);
5421
5422 /* Get UFS version supported by the controller */
5423 hba->ufs_version = ufshcd_get_ufs_version(hba);
5424
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05305425 /* Get Interrupt bit mask per version */
5426 hba->intr_mask = ufshcd_get_intr_mask(hba);
5427
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09005428 err = ufshcd_set_dma_mask(hba);
5429 if (err) {
5430 dev_err(hba->dev, "set dma mask failed\n");
5431 goto out_disable;
5432 }
5433
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305434 /* Allocate memory for host memory space */
5435 err = ufshcd_memory_alloc(hba);
5436 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305437 dev_err(hba->dev, "Memory allocation failed\n");
5438 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305439 }
5440
5441 /* Configure LRB */
5442 ufshcd_host_memory_configure(hba);
5443
5444 host->can_queue = hba->nutrs;
5445 host->cmd_per_lun = hba->nutrs;
5446 host->max_id = UFSHCD_MAX_ID;
Subhash Jadavani0ce147d2014-09-25 15:32:29 +03005447 host->max_lun = UFS_MAX_LUNS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305448 host->max_channel = UFSHCD_MAX_CHANNEL;
5449 host->unique_id = host->host_no;
5450 host->max_cmd_len = MAX_CDB_SIZE;
5451
Dolev Raviv7eb584d2014-09-25 15:32:31 +03005452 hba->max_pwr_info.is_valid = false;
5453
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305454 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05305455 init_waitqueue_head(&hba->tm_wq);
5456 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305457
5458 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05305459 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05305460 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305461
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305462 /* Initialize UIC command mutex */
5463 mutex_init(&hba->uic_cmd_mutex);
5464
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05305465 /* Initialize mutex for device management commands */
5466 mutex_init(&hba->dev_cmd.lock);
5467
5468 /* Initialize device management tag acquire wait queue */
5469 init_waitqueue_head(&hba->dev_cmd.tag_wq);
5470
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005471 ufshcd_init_clk_gating(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305472 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09005473 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305474 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305475 dev_err(hba->dev, "request irq failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005476 goto exit_gating;
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005477 } else {
5478 hba->is_irq_enabled = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305479 }
5480
5481 /* Enable SCSI tag mapping */
5482 err = scsi_init_shared_tag_map(host, host->can_queue);
5483 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305484 dev_err(hba->dev, "init shared queue failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005485 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305486 }
5487
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305488 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305489 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305490 dev_err(hba->dev, "scsi_add_host failed\n");
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005491 goto exit_gating;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305492 }
5493
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305494 /* Host controller enable */
5495 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305496 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305497 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305498 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305499 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305500
Sahitya Tummala856b3482014-09-25 15:32:34 +03005501 if (ufshcd_is_clkscaling_enabled(hba)) {
5502 hba->devfreq = devfreq_add_device(dev, &ufs_devfreq_profile,
5503 "simple_ondemand", NULL);
5504 if (IS_ERR(hba->devfreq)) {
5505 dev_err(hba->dev, "Unable to register with devfreq %ld\n",
5506 PTR_ERR(hba->devfreq));
5507 goto out_remove_scsi_host;
5508 }
5509 /* Suspend devfreq until the UFS device is detected */
5510 devfreq_suspend_device(hba->devfreq);
5511 hba->clk_scaling.window_start_t = 0;
5512 }
5513
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05305514 /* Hold auto suspend until async scan completes */
5515 pm_runtime_get_sync(dev);
5516
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005517 /*
5518 * The device-initialize-sequence hasn't been invoked yet.
5519 * Set the device to power-off state
5520 */
5521 ufshcd_set_ufs_dev_poweroff(hba);
5522
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05305523 async_schedule(ufshcd_async_scan, hba);
5524
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305525 return 0;
5526
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305527out_remove_scsi_host:
5528 scsi_remove_host(hba->host);
Sahitya Tummala1ab27c92014-09-25 15:32:32 +03005529exit_gating:
5530 ufshcd_exit_clk_gating(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305531out_disable:
Subhash Jadavani57d104c2014-09-25 15:32:30 +03005532 hba->is_irq_enabled = false;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305533 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03005534 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305535out_error:
5536 return err;
5537}
5538EXPORT_SYMBOL_GPL(ufshcd_init);
5539
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05305540MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
5541MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05305542MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05305543MODULE_LICENSE("GPL");
5544MODULE_VERSION(UFSHCD_DRIVER_VERSION);