blob: af29d4c0a4169dc45535d3be09bd36be12bca828 [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>
41
Vinayak Holikattie0eca632013-02-25 21:44:33 +053042#include "ufshcd.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053043#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053044
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053045#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
46 UTP_TASK_REQ_COMPL |\
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053047 UIC_POWER_MODE |\
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053048 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,
103 UFSHCD_MAX_LUNS = 8,
104 UFSHCD_CMD_PER_LUN = 32,
105 UFSHCD_CAN_QUEUE = 32,
106};
107
108/* UFSHCD states */
109enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530110 UFSHCD_STATE_RESET,
111 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530112 UFSHCD_STATE_OPERATIONAL,
113};
114
115/* UFSHCD error handling flags */
116enum {
117 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530118};
119
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530120/* UFSHCD UIC layer error flags */
121enum {
122 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
123 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
124 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
125 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
126};
127
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530128/* Interrupt configuration options */
129enum {
130 UFSHCD_INT_DISABLE,
131 UFSHCD_INT_ENABLE,
132 UFSHCD_INT_CLEAR,
133};
134
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530135#define ufshcd_set_eh_in_progress(h) \
136 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
137#define ufshcd_eh_in_progress(h) \
138 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
139#define ufshcd_clear_eh_in_progress(h) \
140 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
141
142static void ufshcd_tmc_handler(struct ufs_hba *hba);
143static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530144static int ufshcd_reset_and_restore(struct ufs_hba *hba);
145static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +0300146static void ufshcd_hba_exit(struct ufs_hba *hba);
147static int ufshcd_probe_hba(struct ufs_hba *hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530148
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530149/*
150 * ufshcd_wait_for_register - wait for register value to change
151 * @hba - per-adapter interface
152 * @reg - mmio register offset
153 * @mask - mask to apply to read register value
154 * @val - wait condition
155 * @interval_us - polling interval in microsecs
156 * @timeout_ms - timeout in millisecs
157 *
158 * Returns -ETIMEDOUT on error, zero on success
159 */
160static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
161 u32 val, unsigned long interval_us, unsigned long timeout_ms)
162{
163 int err = 0;
164 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
165
166 /* ignore bits that we don't intend to wait on */
167 val = val & mask;
168
169 while ((ufshcd_readl(hba, reg) & mask) != val) {
170 /* wakeup within 50us of expiry */
171 usleep_range(interval_us, interval_us + 50);
172
173 if (time_after(jiffies, timeout)) {
174 if ((ufshcd_readl(hba, reg) & mask) != val)
175 err = -ETIMEDOUT;
176 break;
177 }
178 }
179
180 return err;
181}
182
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530183/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530184 * ufshcd_get_intr_mask - Get the interrupt bit mask
185 * @hba - Pointer to adapter instance
186 *
187 * Returns interrupt bit mask per version
188 */
189static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
190{
191 if (hba->ufs_version == UFSHCI_VERSION_10)
192 return INTERRUPT_MASK_ALL_VER_10;
193 else
194 return INTERRUPT_MASK_ALL_VER_11;
195}
196
197/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530198 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
199 * @hba - Pointer to adapter instance
200 *
201 * Returns UFSHCI version supported by the controller
202 */
203static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
204{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530205 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530206}
207
208/**
209 * ufshcd_is_device_present - Check if any device connected to
210 * the host controller
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300211 * @hba: pointer to adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530212 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530213 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530214 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300215static inline int ufshcd_is_device_present(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530216{
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +0300217 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
218 DEVICE_PRESENT) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530219}
220
221/**
222 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
223 * @lrb: pointer to local command reference block
224 *
225 * This function is used to get the OCS field from UTRD
226 * Returns the OCS field in the UTRD
227 */
228static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
229{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530230 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530231}
232
233/**
234 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
235 * @task_req_descp: pointer to utp_task_req_desc structure
236 *
237 * This function is used to get the OCS field from UTMRD
238 * Returns the OCS field in the UTMRD
239 */
240static inline int
241ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
242{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530243 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530244}
245
246/**
247 * ufshcd_get_tm_free_slot - get a free slot for task management request
248 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530249 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530250 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530251 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
252 * Returns 0 if free slot is not available, else return 1 with tag value
253 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530254 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530255static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530256{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530257 int tag;
258 bool ret = false;
259
260 if (!free_slot)
261 goto out;
262
263 do {
264 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
265 if (tag >= hba->nutmrs)
266 goto out;
267 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
268
269 *free_slot = tag;
270 ret = true;
271out:
272 return ret;
273}
274
275static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
276{
277 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530278}
279
280/**
281 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
282 * @hba: per adapter instance
283 * @pos: position of the bit to be cleared
284 */
285static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
286{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530287 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530288}
289
290/**
291 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
292 * @reg: Register value of host controller status
293 *
294 * Returns integer, 0 on Success and positive value if failed
295 */
296static inline int ufshcd_get_lists_status(u32 reg)
297{
298 /*
299 * The mask 0xFF is for the following HCS register bits
300 * Bit Description
301 * 0 Device Present
302 * 1 UTRLRDY
303 * 2 UTMRLRDY
304 * 3 UCRDY
305 * 4 HEI
306 * 5 DEI
307 * 6-7 reserved
308 */
309 return (((reg) & (0xFF)) >> 1) ^ (0x07);
310}
311
312/**
313 * ufshcd_get_uic_cmd_result - Get the UIC command result
314 * @hba: Pointer to adapter instance
315 *
316 * This function gets the result of UIC command completion
317 * Returns 0 on success, non zero value on error
318 */
319static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
320{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530321 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530322 MASK_UIC_COMMAND_RESULT;
323}
324
325/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530326 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
327 * @hba: Pointer to adapter instance
328 *
329 * This function gets UIC command argument3
330 * Returns 0 on success, non zero value on error
331 */
332static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
333{
334 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
335}
336
337/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530338 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530339 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530340 */
341static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530342ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530343{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530344 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530345}
346
347/**
348 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
349 * @ucd_rsp_ptr: pointer to response UPIU
350 *
351 * This function gets the response status and scsi_status from response UPIU
352 * Returns the response result code.
353 */
354static inline int
355ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
356{
357 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
358}
359
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530360/*
361 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
362 * from response UPIU
363 * @ucd_rsp_ptr: pointer to response UPIU
364 *
365 * Return the data segment length.
366 */
367static inline unsigned int
368ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
369{
370 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
371 MASK_RSP_UPIU_DATA_SEG_LEN;
372}
373
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530374/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530375 * ufshcd_is_exception_event - Check if the device raised an exception event
376 * @ucd_rsp_ptr: pointer to response UPIU
377 *
378 * The function checks if the device raised an exception event indicated in
379 * the Device Information field of response UPIU.
380 *
381 * Returns true if exception is raised, false otherwise.
382 */
383static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
384{
385 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
386 MASK_RSP_EXCEPTION_EVENT ? true : false;
387}
388
389/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530390 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530391 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530392 */
393static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530394ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530395{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530396 ufshcd_writel(hba, INT_AGGR_ENABLE |
397 INT_AGGR_COUNTER_AND_TIMER_RESET,
398 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
399}
400
401/**
402 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
403 * @hba: per adapter instance
404 * @cnt: Interrupt aggregation counter threshold
405 * @tmout: Interrupt aggregation timeout value
406 */
407static inline void
408ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
409{
410 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
411 INT_AGGR_COUNTER_THLD_VAL(cnt) |
412 INT_AGGR_TIMEOUT_VAL(tmout),
413 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530414}
415
416/**
417 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
418 * When run-stop registers are set to 1, it indicates the
419 * host controller that it can process the requests
420 * @hba: per adapter instance
421 */
422static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
423{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530424 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
425 REG_UTP_TASK_REQ_LIST_RUN_STOP);
426 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
427 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530428}
429
430/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530431 * ufshcd_hba_start - Start controller initialization sequence
432 * @hba: per adapter instance
433 */
434static inline void ufshcd_hba_start(struct ufs_hba *hba)
435{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530436 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530437}
438
439/**
440 * ufshcd_is_hba_active - Get controller state
441 * @hba: per adapter instance
442 *
443 * Returns zero if controller is active, 1 otherwise
444 */
445static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
446{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530447 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530448}
449
450/**
451 * ufshcd_send_command - Send SCSI or device management commands
452 * @hba: per adapter instance
453 * @task_tag: Task tag of the command
454 */
455static inline
456void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
457{
458 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530459 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530460}
461
462/**
463 * ufshcd_copy_sense_data - Copy sense data in case of check condition
464 * @lrb - pointer to local reference block
465 */
466static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
467{
468 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530469 if (lrbp->sense_buffer &&
470 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530471 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530472 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530473 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530474 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
475 }
476}
477
478/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530479 * ufshcd_copy_query_response() - Copy the Query Response and the data
480 * descriptor
481 * @hba: per adapter instance
482 * @lrb - pointer to local reference block
483 */
484static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300485int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530486{
487 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
488
Dolev Raviv68078d52013-07-30 00:35:58 +0530489 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530490
Dolev Raviv68078d52013-07-30 00:35:58 +0530491 /* Get the descriptor */
492 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300493 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530494 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300495 u16 resp_len;
496 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530497
498 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300499 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530500 MASK_QUERY_DATA_SEG_LEN;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +0300501 buf_len = be16_to_cpu(
502 hba->dev_cmd.query.request.upiu_req.length);
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300503 if (likely(buf_len >= resp_len)) {
504 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
505 } else {
506 dev_warn(hba->dev,
507 "%s: Response size is bigger than buffer",
508 __func__);
509 return -EINVAL;
510 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530511 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300512
513 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530514}
515
516/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530517 * ufshcd_hba_capabilities - Read controller capabilities
518 * @hba: per adapter instance
519 */
520static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
521{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530522 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530523
524 /* nutrs and nutmrs are 0 based values */
525 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
526 hba->nutmrs =
527 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
528}
529
530/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530531 * ufshcd_ready_for_uic_cmd - Check if controller is ready
532 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530533 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530534 * Return true on success, else false
535 */
536static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
537{
538 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
539 return true;
540 else
541 return false;
542}
543
544/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530545 * ufshcd_get_upmcrs - Get the power mode change request status
546 * @hba: Pointer to adapter instance
547 *
548 * This function gets the UPMCRS field of HCS register
549 * Returns value of UPMCRS field
550 */
551static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
552{
553 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
554}
555
556/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530557 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
558 * @hba: per adapter instance
559 * @uic_cmd: UIC command
560 *
561 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530562 */
563static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530564ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530565{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530566 WARN_ON(hba->active_uic_cmd);
567
568 hba->active_uic_cmd = uic_cmd;
569
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530570 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530571 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
572 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
573 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530574
575 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530576 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530577 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530578}
579
580/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530581 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
582 * @hba: per adapter instance
583 * @uic_command: UIC command
584 *
585 * Must be called with mutex held.
586 * Returns 0 only if success.
587 */
588static int
589ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
590{
591 int ret;
592 unsigned long flags;
593
594 if (wait_for_completion_timeout(&uic_cmd->done,
595 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
596 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
597 else
598 ret = -ETIMEDOUT;
599
600 spin_lock_irqsave(hba->host->host_lock, flags);
601 hba->active_uic_cmd = NULL;
602 spin_unlock_irqrestore(hba->host->host_lock, flags);
603
604 return ret;
605}
606
607/**
608 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
609 * @hba: per adapter instance
610 * @uic_cmd: UIC command
611 *
612 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
613 * with mutex held.
614 * Returns 0 only if success.
615 */
616static int
617__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
618{
619 int ret;
620 unsigned long flags;
621
622 if (!ufshcd_ready_for_uic_cmd(hba)) {
623 dev_err(hba->dev,
624 "Controller not ready to accept UIC commands\n");
625 return -EIO;
626 }
627
628 init_completion(&uic_cmd->done);
629
630 spin_lock_irqsave(hba->host->host_lock, flags);
631 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
632 spin_unlock_irqrestore(hba->host->host_lock, flags);
633
634 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
635
636 return ret;
637}
638
639/**
640 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
641 * @hba: per adapter instance
642 * @uic_cmd: UIC command
643 *
644 * Returns 0 only if success.
645 */
646static int
647ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
648{
649 int ret;
650
651 mutex_lock(&hba->uic_cmd_mutex);
652 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
653 mutex_unlock(&hba->uic_cmd_mutex);
654
655 return ret;
656}
657
658/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530659 * ufshcd_map_sg - Map scatter-gather list to prdt
660 * @lrbp - pointer to local reference block
661 *
662 * Returns 0 in case of success, non-zero value in case of failure
663 */
664static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
665{
666 struct ufshcd_sg_entry *prd_table;
667 struct scatterlist *sg;
668 struct scsi_cmnd *cmd;
669 int sg_segments;
670 int i;
671
672 cmd = lrbp->cmd;
673 sg_segments = scsi_dma_map(cmd);
674 if (sg_segments < 0)
675 return sg_segments;
676
677 if (sg_segments) {
678 lrbp->utr_descriptor_ptr->prd_table_length =
679 cpu_to_le16((u16) (sg_segments));
680
681 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
682
683 scsi_for_each_sg(cmd, sg, sg_segments, i) {
684 prd_table[i].size =
685 cpu_to_le32(((u32) sg_dma_len(sg))-1);
686 prd_table[i].base_addr =
687 cpu_to_le32(lower_32_bits(sg->dma_address));
688 prd_table[i].upper_addr =
689 cpu_to_le32(upper_32_bits(sg->dma_address));
690 }
691 } else {
692 lrbp->utr_descriptor_ptr->prd_table_length = 0;
693 }
694
695 return 0;
696}
697
698/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530699 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530700 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530701 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530702 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530703static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530704{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530705 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
706
707 if (hba->ufs_version == UFSHCI_VERSION_10) {
708 u32 rw;
709 rw = set & INTERRUPT_MASK_RW_VER_10;
710 set = rw | ((set ^ intrs) & intrs);
711 } else {
712 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530713 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530714
715 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
716}
717
718/**
719 * ufshcd_disable_intr - disable interrupts
720 * @hba: per adapter instance
721 * @intrs: interrupt bits
722 */
723static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
724{
725 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
726
727 if (hba->ufs_version == UFSHCI_VERSION_10) {
728 u32 rw;
729 rw = (set & INTERRUPT_MASK_RW_VER_10) &
730 ~(intrs & INTERRUPT_MASK_RW_VER_10);
731 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
732
733 } else {
734 set &= ~intrs;
735 }
736
737 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530738}
739
740/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530741 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
742 * descriptor according to request
743 * @lrbp: pointer to local reference block
744 * @upiu_flags: flags required in the header
745 * @cmd_dir: requests data direction
746 */
747static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
748 u32 *upiu_flags, enum dma_data_direction cmd_dir)
749{
750 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
751 u32 data_direction;
752 u32 dword_0;
753
754 if (cmd_dir == DMA_FROM_DEVICE) {
755 data_direction = UTP_DEVICE_TO_HOST;
756 *upiu_flags = UPIU_CMD_FLAGS_READ;
757 } else if (cmd_dir == DMA_TO_DEVICE) {
758 data_direction = UTP_HOST_TO_DEVICE;
759 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
760 } else {
761 data_direction = UTP_NO_DATA_TRANSFER;
762 *upiu_flags = UPIU_CMD_FLAGS_NONE;
763 }
764
765 dword_0 = data_direction | (lrbp->command_type
766 << UPIU_COMMAND_TYPE_OFFSET);
767 if (lrbp->intr_cmd)
768 dword_0 |= UTP_REQ_DESC_INT_CMD;
769
770 /* Transfer request descriptor header fields */
771 req_desc->header.dword_0 = cpu_to_le32(dword_0);
772
773 /*
774 * assigning invalid value for command status. Controller
775 * updates OCS on command completion, with the command
776 * status
777 */
778 req_desc->header.dword_2 =
779 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
780}
781
782/**
783 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
784 * for scsi commands
785 * @lrbp - local reference block pointer
786 * @upiu_flags - flags
787 */
788static
789void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
790{
791 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
792
793 /* command descriptor fields */
794 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
795 UPIU_TRANSACTION_COMMAND, upiu_flags,
796 lrbp->lun, lrbp->task_tag);
797 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
798 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
799
800 /* Total EHS length and Data segment length will be zero */
801 ucd_req_ptr->header.dword_2 = 0;
802
803 ucd_req_ptr->sc.exp_data_transfer_len =
804 cpu_to_be32(lrbp->cmd->sdb.length);
805
806 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd,
807 (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
808}
809
Dolev Raviv68078d52013-07-30 00:35:58 +0530810/**
811 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
812 * for query requsts
813 * @hba: UFS hba
814 * @lrbp: local reference block pointer
815 * @upiu_flags: flags
816 */
817static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
818 struct ufshcd_lrb *lrbp, u32 upiu_flags)
819{
820 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
821 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530822 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +0530823 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
824
825 /* Query request header */
826 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
827 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
828 lrbp->lun, lrbp->task_tag);
829 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
830 0, query->request.query_func, 0, 0);
831
832 /* Data segment length */
833 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
834 0, 0, len >> 8, (u8)len);
835
836 /* Copy the Query Request buffer as is */
837 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
838 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530839
840 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300841 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
842 memcpy(descp, query->descriptor, len);
843
Dolev Raviv68078d52013-07-30 00:35:58 +0530844}
845
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530846static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
847{
848 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
849
850 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
851
852 /* command descriptor fields */
853 ucd_req_ptr->header.dword_0 =
854 UPIU_HEADER_DWORD(
855 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
856}
857
858/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530859 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530860 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530861 * @lrb - pointer to local reference block
862 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530863static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530864{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530865 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530866 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530867
868 switch (lrbp->command_type) {
869 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530870 if (likely(lrbp->cmd)) {
871 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
872 lrbp->cmd->sc_data_direction);
873 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530874 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530875 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530876 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530877 break;
878 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530879 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530880 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
881 ufshcd_prepare_utp_query_req_upiu(
882 hba, lrbp, upiu_flags);
883 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530884 ufshcd_prepare_utp_nop_upiu(lrbp);
885 else
886 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530887 break;
888 case UTP_CMD_TYPE_UFS:
889 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530890 ret = -ENOTSUPP;
891 dev_err(hba->dev, "%s: UFS native command are not supported\n",
892 __func__);
893 break;
894 default:
895 ret = -ENOTSUPP;
896 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
897 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530898 break;
899 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530900
901 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530902}
903
904/**
905 * ufshcd_queuecommand - main entry point for SCSI requests
906 * @cmd: command from SCSI Midlayer
907 * @done: call back function
908 *
909 * Returns 0 for success, non-zero in case of failure
910 */
911static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
912{
913 struct ufshcd_lrb *lrbp;
914 struct ufs_hba *hba;
915 unsigned long flags;
916 int tag;
917 int err = 0;
918
919 hba = shost_priv(host);
920
921 tag = cmd->request->tag;
922
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530923 spin_lock_irqsave(hba->host->host_lock, flags);
924 switch (hba->ufshcd_state) {
925 case UFSHCD_STATE_OPERATIONAL:
926 break;
927 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530928 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530929 goto out_unlock;
930 case UFSHCD_STATE_ERROR:
931 set_host_byte(cmd, DID_ERROR);
932 cmd->scsi_done(cmd);
933 goto out_unlock;
934 default:
935 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
936 __func__, hba->ufshcd_state);
937 set_host_byte(cmd, DID_BAD_TARGET);
938 cmd->scsi_done(cmd);
939 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530940 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530941 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530942
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530943 /* acquire the tag to make sure device cmds don't use it */
944 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
945 /*
946 * Dev manage command in progress, requeue the command.
947 * Requeuing the command helps in cases where the request *may*
948 * find different tag instead of waiting for dev manage command
949 * completion.
950 */
951 err = SCSI_MLQUEUE_HOST_BUSY;
952 goto out;
953 }
954
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530955 lrbp = &hba->lrb[tag];
956
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530957 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530958 lrbp->cmd = cmd;
959 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
960 lrbp->sense_buffer = cmd->sense_buffer;
961 lrbp->task_tag = tag;
962 lrbp->lun = cmd->device->lun;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530963 lrbp->intr_cmd = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530964 lrbp->command_type = UTP_CMD_TYPE_SCSI;
965
966 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530967 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530968 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530969 if (err) {
970 lrbp->cmd = NULL;
971 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530972 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530973 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530974
975 /* issue command to the controller */
976 spin_lock_irqsave(hba->host->host_lock, flags);
977 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530978out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530979 spin_unlock_irqrestore(hba->host->host_lock, flags);
980out:
981 return err;
982}
983
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530984static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
985 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
986{
987 lrbp->cmd = NULL;
988 lrbp->sense_bufflen = 0;
989 lrbp->sense_buffer = NULL;
990 lrbp->task_tag = tag;
991 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
992 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
993 lrbp->intr_cmd = true; /* No interrupt aggregation */
994 hba->dev_cmd.type = cmd_type;
995
996 return ufshcd_compose_upiu(hba, lrbp);
997}
998
999static int
1000ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
1001{
1002 int err = 0;
1003 unsigned long flags;
1004 u32 mask = 1 << tag;
1005
1006 /* clear outstanding transaction before retry */
1007 spin_lock_irqsave(hba->host->host_lock, flags);
1008 ufshcd_utrl_clear(hba, tag);
1009 spin_unlock_irqrestore(hba->host->host_lock, flags);
1010
1011 /*
1012 * wait for for h/w to clear corresponding bit in door-bell.
1013 * max. wait is 1 sec.
1014 */
1015 err = ufshcd_wait_for_register(hba,
1016 REG_UTP_TRANSFER_REQ_DOOR_BELL,
1017 mask, ~mask, 1000, 1000);
1018
1019 return err;
1020}
1021
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001022static int
1023ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1024{
1025 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1026
1027 /* Get the UPIU response */
1028 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1029 UPIU_RSP_CODE_OFFSET;
1030 return query_res->response;
1031}
1032
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301033/**
1034 * ufshcd_dev_cmd_completion() - handles device management command responses
1035 * @hba: per adapter instance
1036 * @lrbp: pointer to local reference block
1037 */
1038static int
1039ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1040{
1041 int resp;
1042 int err = 0;
1043
1044 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1045
1046 switch (resp) {
1047 case UPIU_TRANSACTION_NOP_IN:
1048 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1049 err = -EINVAL;
1050 dev_err(hba->dev, "%s: unexpected response %x\n",
1051 __func__, resp);
1052 }
1053 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301054 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001055 err = ufshcd_check_query_response(hba, lrbp);
1056 if (!err)
1057 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301058 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301059 case UPIU_TRANSACTION_REJECT_UPIU:
1060 /* TODO: handle Reject UPIU Response */
1061 err = -EPERM;
1062 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1063 __func__);
1064 break;
1065 default:
1066 err = -EINVAL;
1067 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1068 __func__, resp);
1069 break;
1070 }
1071
1072 return err;
1073}
1074
1075static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1076 struct ufshcd_lrb *lrbp, int max_timeout)
1077{
1078 int err = 0;
1079 unsigned long time_left;
1080 unsigned long flags;
1081
1082 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1083 msecs_to_jiffies(max_timeout));
1084
1085 spin_lock_irqsave(hba->host->host_lock, flags);
1086 hba->dev_cmd.complete = NULL;
1087 if (likely(time_left)) {
1088 err = ufshcd_get_tr_ocs(lrbp);
1089 if (!err)
1090 err = ufshcd_dev_cmd_completion(hba, lrbp);
1091 }
1092 spin_unlock_irqrestore(hba->host->host_lock, flags);
1093
1094 if (!time_left) {
1095 err = -ETIMEDOUT;
1096 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
1097 /* sucessfully cleared the command, retry if needed */
1098 err = -EAGAIN;
1099 }
1100
1101 return err;
1102}
1103
1104/**
1105 * ufshcd_get_dev_cmd_tag - Get device management command tag
1106 * @hba: per-adapter instance
1107 * @tag: pointer to variable with available slot value
1108 *
1109 * Get a free slot and lock it until device management command
1110 * completes.
1111 *
1112 * Returns false if free slot is unavailable for locking, else
1113 * return true with tag value in @tag.
1114 */
1115static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1116{
1117 int tag;
1118 bool ret = false;
1119 unsigned long tmp;
1120
1121 if (!tag_out)
1122 goto out;
1123
1124 do {
1125 tmp = ~hba->lrb_in_use;
1126 tag = find_last_bit(&tmp, hba->nutrs);
1127 if (tag >= hba->nutrs)
1128 goto out;
1129 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1130
1131 *tag_out = tag;
1132 ret = true;
1133out:
1134 return ret;
1135}
1136
1137static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1138{
1139 clear_bit_unlock(tag, &hba->lrb_in_use);
1140}
1141
1142/**
1143 * ufshcd_exec_dev_cmd - API for sending device management requests
1144 * @hba - UFS hba
1145 * @cmd_type - specifies the type (NOP, Query...)
1146 * @timeout - time in seconds
1147 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301148 * NOTE: Since there is only one available tag for device management commands,
1149 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301150 */
1151static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1152 enum dev_cmd_type cmd_type, int timeout)
1153{
1154 struct ufshcd_lrb *lrbp;
1155 int err;
1156 int tag;
1157 struct completion wait;
1158 unsigned long flags;
1159
1160 /*
1161 * Get free slot, sleep if slots are unavailable.
1162 * Even though we use wait_event() which sleeps indefinitely,
1163 * the maximum wait time is bounded by SCSI request timeout.
1164 */
1165 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1166
1167 init_completion(&wait);
1168 lrbp = &hba->lrb[tag];
1169 WARN_ON(lrbp->cmd);
1170 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1171 if (unlikely(err))
1172 goto out_put_tag;
1173
1174 hba->dev_cmd.complete = &wait;
1175
1176 spin_lock_irqsave(hba->host->host_lock, flags);
1177 ufshcd_send_command(hba, tag);
1178 spin_unlock_irqrestore(hba->host->host_lock, flags);
1179
1180 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1181
1182out_put_tag:
1183 ufshcd_put_dev_cmd_tag(hba, tag);
1184 wake_up(&hba->dev_cmd.tag_wq);
1185 return err;
1186}
1187
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301188/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001189 * ufshcd_init_query() - init the query response and request parameters
1190 * @hba: per-adapter instance
1191 * @request: address of the request pointer to be initialized
1192 * @response: address of the response pointer to be initialized
1193 * @opcode: operation to perform
1194 * @idn: flag idn to access
1195 * @index: LU number to access
1196 * @selector: query/flag/descriptor further identification
1197 */
1198static inline void ufshcd_init_query(struct ufs_hba *hba,
1199 struct ufs_query_req **request, struct ufs_query_res **response,
1200 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1201{
1202 *request = &hba->dev_cmd.query.request;
1203 *response = &hba->dev_cmd.query.response;
1204 memset(*request, 0, sizeof(struct ufs_query_req));
1205 memset(*response, 0, sizeof(struct ufs_query_res));
1206 (*request)->upiu_req.opcode = opcode;
1207 (*request)->upiu_req.idn = idn;
1208 (*request)->upiu_req.index = index;
1209 (*request)->upiu_req.selector = selector;
1210}
1211
1212/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301213 * ufshcd_query_flag() - API function for sending flag query requests
1214 * hba: per-adapter instance
1215 * query_opcode: flag query to perform
1216 * idn: flag idn to access
1217 * flag_res: the flag value after the query request completes
1218 *
1219 * Returns 0 for success, non-zero in case of failure
1220 */
1221static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1222 enum flag_idn idn, bool *flag_res)
1223{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001224 struct ufs_query_req *request = NULL;
1225 struct ufs_query_res *response = NULL;
1226 int err, index = 0, selector = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301227
1228 BUG_ON(!hba);
1229
1230 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001231 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1232 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301233
1234 switch (opcode) {
1235 case UPIU_QUERY_OPCODE_SET_FLAG:
1236 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1237 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1238 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1239 break;
1240 case UPIU_QUERY_OPCODE_READ_FLAG:
1241 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1242 if (!flag_res) {
1243 /* No dummy reads */
1244 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1245 __func__);
1246 err = -EINVAL;
1247 goto out_unlock;
1248 }
1249 break;
1250 default:
1251 dev_err(hba->dev,
1252 "%s: Expected query flag opcode but got = %d\n",
1253 __func__, opcode);
1254 err = -EINVAL;
1255 goto out_unlock;
1256 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301257
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001258 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Dolev Raviv68078d52013-07-30 00:35:58 +05301259
1260 if (err) {
1261 dev_err(hba->dev,
1262 "%s: Sending flag query for idn %d failed, err = %d\n",
1263 __func__, idn, err);
1264 goto out_unlock;
1265 }
1266
1267 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301268 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301269 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1270
1271out_unlock:
1272 mutex_unlock(&hba->dev_cmd.lock);
1273 return err;
1274}
1275
1276/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301277 * ufshcd_query_attr - API function for sending attribute requests
1278 * hba: per-adapter instance
1279 * opcode: attribute opcode
1280 * idn: attribute idn to access
1281 * index: index field
1282 * selector: selector field
1283 * attr_val: the attribute value after the query request completes
1284 *
1285 * Returns 0 for success, non-zero in case of failure
1286*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301287static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301288 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1289{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001290 struct ufs_query_req *request = NULL;
1291 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301292 int err;
1293
1294 BUG_ON(!hba);
1295
1296 if (!attr_val) {
1297 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1298 __func__, opcode);
1299 err = -EINVAL;
1300 goto out;
1301 }
1302
1303 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001304 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1305 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301306
1307 switch (opcode) {
1308 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1309 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301310 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301311 break;
1312 case UPIU_QUERY_OPCODE_READ_ATTR:
1313 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1314 break;
1315 default:
1316 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1317 __func__, opcode);
1318 err = -EINVAL;
1319 goto out_unlock;
1320 }
1321
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001322 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301323
1324 if (err) {
1325 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1326 __func__, opcode, idn, err);
1327 goto out_unlock;
1328 }
1329
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301330 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301331
1332out_unlock:
1333 mutex_unlock(&hba->dev_cmd.lock);
1334out:
1335 return err;
1336}
1337
1338/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001339 * ufshcd_query_descriptor - API function for sending descriptor requests
1340 * hba: per-adapter instance
1341 * opcode: attribute opcode
1342 * idn: attribute idn to access
1343 * index: index field
1344 * selector: selector field
1345 * desc_buf: the buffer that contains the descriptor
1346 * buf_len: length parameter passed to the device
1347 *
1348 * Returns 0 for success, non-zero in case of failure.
1349 * The buf_len parameter will contain, on return, the length parameter
1350 * received on the response.
1351 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001352static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001353 enum query_opcode opcode, enum desc_idn idn, u8 index,
1354 u8 selector, u8 *desc_buf, int *buf_len)
1355{
1356 struct ufs_query_req *request = NULL;
1357 struct ufs_query_res *response = NULL;
1358 int err;
1359
1360 BUG_ON(!hba);
1361
1362 if (!desc_buf) {
1363 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1364 __func__, opcode);
1365 err = -EINVAL;
1366 goto out;
1367 }
1368
1369 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1370 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1371 __func__, *buf_len);
1372 err = -EINVAL;
1373 goto out;
1374 }
1375
1376 mutex_lock(&hba->dev_cmd.lock);
1377 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1378 selector);
1379 hba->dev_cmd.query.descriptor = desc_buf;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001380 request->upiu_req.length = cpu_to_be16(*buf_len);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001381
1382 switch (opcode) {
1383 case UPIU_QUERY_OPCODE_WRITE_DESC:
1384 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1385 break;
1386 case UPIU_QUERY_OPCODE_READ_DESC:
1387 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1388 break;
1389 default:
1390 dev_err(hba->dev,
1391 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1392 __func__, opcode);
1393 err = -EINVAL;
1394 goto out_unlock;
1395 }
1396
1397 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1398
1399 if (err) {
1400 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1401 __func__, opcode, idn, err);
1402 goto out_unlock;
1403 }
1404
1405 hba->dev_cmd.query.descriptor = NULL;
Sujit Reddy Thummaea2aab22014-07-23 09:31:12 +03001406 *buf_len = be16_to_cpu(response->upiu_res.length);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001407
1408out_unlock:
1409 mutex_unlock(&hba->dev_cmd.lock);
1410out:
1411 return err;
1412}
1413
1414/**
Subhash Jadavanida461ce2014-09-25 15:32:25 +03001415 * ufshcd_read_desc_param - read the specified descriptor parameter
1416 * @hba: Pointer to adapter instance
1417 * @desc_id: descriptor idn value
1418 * @desc_index: descriptor index
1419 * @param_offset: offset of the parameter to read
1420 * @param_read_buf: pointer to buffer where parameter would be read
1421 * @param_size: sizeof(param_read_buf)
1422 *
1423 * Return 0 in case of success, non-zero otherwise
1424 */
1425static int ufshcd_read_desc_param(struct ufs_hba *hba,
1426 enum desc_idn desc_id,
1427 int desc_index,
1428 u32 param_offset,
1429 u8 *param_read_buf,
1430 u32 param_size)
1431{
1432 int ret;
1433 u8 *desc_buf;
1434 u32 buff_len;
1435 bool is_kmalloc = true;
1436
1437 /* safety checks */
1438 if (desc_id >= QUERY_DESC_IDN_MAX)
1439 return -EINVAL;
1440
1441 buff_len = ufs_query_desc_max_size[desc_id];
1442 if ((param_offset + param_size) > buff_len)
1443 return -EINVAL;
1444
1445 if (!param_offset && (param_size == buff_len)) {
1446 /* memory space already available to hold full descriptor */
1447 desc_buf = param_read_buf;
1448 is_kmalloc = false;
1449 } else {
1450 /* allocate memory to hold full descriptor */
1451 desc_buf = kmalloc(buff_len, GFP_KERNEL);
1452 if (!desc_buf)
1453 return -ENOMEM;
1454 }
1455
1456 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
1457 desc_id, desc_index, 0, desc_buf,
1458 &buff_len);
1459
1460 if (ret || (buff_len < ufs_query_desc_max_size[desc_id]) ||
1461 (desc_buf[QUERY_DESC_LENGTH_OFFSET] !=
1462 ufs_query_desc_max_size[desc_id])
1463 || (desc_buf[QUERY_DESC_DESC_TYPE_OFFSET] != desc_id)) {
1464 dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d param_offset %d buff_len %d ret %d",
1465 __func__, desc_id, param_offset, buff_len, ret);
1466 if (!ret)
1467 ret = -EINVAL;
1468
1469 goto out;
1470 }
1471
1472 if (is_kmalloc)
1473 memcpy(param_read_buf, &desc_buf[param_offset], param_size);
1474out:
1475 if (is_kmalloc)
1476 kfree(desc_buf);
1477 return ret;
1478}
1479
1480static inline int ufshcd_read_desc(struct ufs_hba *hba,
1481 enum desc_idn desc_id,
1482 int desc_index,
1483 u8 *buf,
1484 u32 size)
1485{
1486 return ufshcd_read_desc_param(hba, desc_id, desc_index, 0, buf, size);
1487}
1488
1489static inline int ufshcd_read_power_desc(struct ufs_hba *hba,
1490 u8 *buf,
1491 u32 size)
1492{
1493 return ufshcd_read_desc(hba, QUERY_DESC_IDN_POWER, 0, buf, size);
1494}
1495
1496/**
1497 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
1498 * @hba: Pointer to adapter instance
1499 * @lun: lun id
1500 * @param_offset: offset of the parameter to read
1501 * @param_read_buf: pointer to buffer where parameter would be read
1502 * @param_size: sizeof(param_read_buf)
1503 *
1504 * Return 0 in case of success, non-zero otherwise
1505 */
1506static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba,
1507 int lun,
1508 enum unit_desc_param param_offset,
1509 u8 *param_read_buf,
1510 u32 param_size)
1511{
1512 /*
1513 * Unit descriptors are only available for general purpose LUs (LUN id
1514 * from 0 to 7) and RPMB Well known LU.
1515 */
1516 if (lun >= UFS_UPIU_MAX_GENERAL_LUN)
1517 return -EOPNOTSUPP;
1518
1519 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun,
1520 param_offset, param_read_buf, param_size);
1521}
1522
1523/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301524 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1525 * @hba: per adapter instance
1526 *
1527 * 1. Allocate DMA memory for Command Descriptor array
1528 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1529 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1530 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1531 * (UTMRDL)
1532 * 4. Allocate memory for local reference block(lrb).
1533 *
1534 * Returns 0 for success, non-zero in case of failure
1535 */
1536static int ufshcd_memory_alloc(struct ufs_hba *hba)
1537{
1538 size_t utmrdl_size, utrdl_size, ucdl_size;
1539
1540 /* Allocate memory for UTP command descriptors */
1541 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001542 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
1543 ucdl_size,
1544 &hba->ucdl_dma_addr,
1545 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301546
1547 /*
1548 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1549 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1550 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1551 * be aligned to 128 bytes as well
1552 */
1553 if (!hba->ucdl_base_addr ||
1554 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301555 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301556 "Command Descriptor Memory allocation failed\n");
1557 goto out;
1558 }
1559
1560 /*
1561 * Allocate memory for UTP Transfer descriptors
1562 * UFSHCI requires 1024 byte alignment of UTRD
1563 */
1564 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001565 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
1566 utrdl_size,
1567 &hba->utrdl_dma_addr,
1568 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301569 if (!hba->utrdl_base_addr ||
1570 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301571 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301572 "Transfer Descriptor Memory allocation failed\n");
1573 goto out;
1574 }
1575
1576 /*
1577 * Allocate memory for UTP Task Management descriptors
1578 * UFSHCI requires 1024 byte alignment of UTMRD
1579 */
1580 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09001581 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
1582 utmrdl_size,
1583 &hba->utmrdl_dma_addr,
1584 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301585 if (!hba->utmrdl_base_addr ||
1586 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301587 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301588 "Task Management Descriptor Memory allocation failed\n");
1589 goto out;
1590 }
1591
1592 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09001593 hba->lrb = devm_kzalloc(hba->dev,
1594 hba->nutrs * sizeof(struct ufshcd_lrb),
1595 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301596 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301597 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301598 goto out;
1599 }
1600 return 0;
1601out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301602 return -ENOMEM;
1603}
1604
1605/**
1606 * ufshcd_host_memory_configure - configure local reference block with
1607 * memory offsets
1608 * @hba: per adapter instance
1609 *
1610 * Configure Host memory space
1611 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
1612 * address.
1613 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
1614 * and PRDT offset.
1615 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
1616 * into local reference block.
1617 */
1618static void ufshcd_host_memory_configure(struct ufs_hba *hba)
1619{
1620 struct utp_transfer_cmd_desc *cmd_descp;
1621 struct utp_transfer_req_desc *utrdlp;
1622 dma_addr_t cmd_desc_dma_addr;
1623 dma_addr_t cmd_desc_element_addr;
1624 u16 response_offset;
1625 u16 prdt_offset;
1626 int cmd_desc_size;
1627 int i;
1628
1629 utrdlp = hba->utrdl_base_addr;
1630 cmd_descp = hba->ucdl_base_addr;
1631
1632 response_offset =
1633 offsetof(struct utp_transfer_cmd_desc, response_upiu);
1634 prdt_offset =
1635 offsetof(struct utp_transfer_cmd_desc, prd_table);
1636
1637 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
1638 cmd_desc_dma_addr = hba->ucdl_dma_addr;
1639
1640 for (i = 0; i < hba->nutrs; i++) {
1641 /* Configure UTRD with command descriptor base address */
1642 cmd_desc_element_addr =
1643 (cmd_desc_dma_addr + (cmd_desc_size * i));
1644 utrdlp[i].command_desc_base_addr_lo =
1645 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
1646 utrdlp[i].command_desc_base_addr_hi =
1647 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
1648
1649 /* Response upiu and prdt offset should be in double words */
1650 utrdlp[i].response_upiu_offset =
1651 cpu_to_le16((response_offset >> 2));
1652 utrdlp[i].prd_table_offset =
1653 cpu_to_le16((prdt_offset >> 2));
1654 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05301655 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301656
1657 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301658 hba->lrb[i].ucd_req_ptr =
1659 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301660 hba->lrb[i].ucd_rsp_ptr =
1661 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
1662 hba->lrb[i].ucd_prdt_ptr =
1663 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
1664 }
1665}
1666
1667/**
1668 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
1669 * @hba: per adapter instance
1670 *
1671 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
1672 * in order to initialize the Unipro link startup procedure.
1673 * Once the Unipro links are up, the device connected to the controller
1674 * is detected.
1675 *
1676 * Returns 0 on success, non-zero value on failure
1677 */
1678static int ufshcd_dme_link_startup(struct ufs_hba *hba)
1679{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301680 struct uic_command uic_cmd = {0};
1681 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301682
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301683 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
1684
1685 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1686 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301687 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301688 "dme-link-startup: error code %d\n", ret);
1689 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301690}
1691
1692/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05301693 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
1694 * @hba: per adapter instance
1695 * @attr_sel: uic command argument1
1696 * @attr_set: attribute set type as uic command argument2
1697 * @mib_val: setting value as uic command argument3
1698 * @peer: indicate whether peer or local
1699 *
1700 * Returns 0 on success, non-zero value on failure
1701 */
1702int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
1703 u8 attr_set, u32 mib_val, u8 peer)
1704{
1705 struct uic_command uic_cmd = {0};
1706 static const char *const action[] = {
1707 "dme-set",
1708 "dme-peer-set"
1709 };
1710 const char *set = action[!!peer];
1711 int ret;
1712
1713 uic_cmd.command = peer ?
1714 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
1715 uic_cmd.argument1 = attr_sel;
1716 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
1717 uic_cmd.argument3 = mib_val;
1718
1719 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1720 if (ret)
1721 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
1722 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
1723
1724 return ret;
1725}
1726EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
1727
1728/**
1729 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
1730 * @hba: per adapter instance
1731 * @attr_sel: uic command argument1
1732 * @mib_val: the value of the attribute as returned by the UIC command
1733 * @peer: indicate whether peer or local
1734 *
1735 * Returns 0 on success, non-zero value on failure
1736 */
1737int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
1738 u32 *mib_val, u8 peer)
1739{
1740 struct uic_command uic_cmd = {0};
1741 static const char *const action[] = {
1742 "dme-get",
1743 "dme-peer-get"
1744 };
1745 const char *get = action[!!peer];
1746 int ret;
1747
1748 uic_cmd.command = peer ?
1749 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
1750 uic_cmd.argument1 = attr_sel;
1751
1752 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1753 if (ret) {
1754 dev_err(hba->dev, "%s: attr-id 0x%x error code %d\n",
1755 get, UIC_GET_ATTR_ID(attr_sel), ret);
1756 goto out;
1757 }
1758
1759 if (mib_val)
1760 *mib_val = uic_cmd.argument3;
1761out:
1762 return ret;
1763}
1764EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
1765
1766/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301767 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
1768 * using DME_SET primitives.
1769 * @hba: per adapter instance
1770 * @mode: powr mode value
1771 *
1772 * Returns 0 on success, non-zero value on failure
1773 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301774static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301775{
1776 struct uic_command uic_cmd = {0};
1777 struct completion pwr_done;
1778 unsigned long flags;
1779 u8 status;
1780 int ret;
1781
1782 uic_cmd.command = UIC_CMD_DME_SET;
1783 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
1784 uic_cmd.argument3 = mode;
1785 init_completion(&pwr_done);
1786
1787 mutex_lock(&hba->uic_cmd_mutex);
1788
1789 spin_lock_irqsave(hba->host->host_lock, flags);
1790 hba->pwr_done = &pwr_done;
1791 spin_unlock_irqrestore(hba->host->host_lock, flags);
1792 ret = __ufshcd_send_uic_cmd(hba, &uic_cmd);
1793 if (ret) {
1794 dev_err(hba->dev,
1795 "pwr mode change with mode 0x%x uic error %d\n",
1796 mode, ret);
1797 goto out;
1798 }
1799
1800 if (!wait_for_completion_timeout(hba->pwr_done,
1801 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
1802 dev_err(hba->dev,
1803 "pwr mode change with mode 0x%x completion timeout\n",
1804 mode);
1805 ret = -ETIMEDOUT;
1806 goto out;
1807 }
1808
1809 status = ufshcd_get_upmcrs(hba);
1810 if (status != PWR_LOCAL) {
1811 dev_err(hba->dev,
1812 "pwr mode change failed, host umpcrs:0x%x\n",
1813 status);
1814 ret = (status != PWR_OK) ? status : -1;
1815 }
1816out:
1817 spin_lock_irqsave(hba->host->host_lock, flags);
1818 hba->pwr_done = NULL;
1819 spin_unlock_irqrestore(hba->host->host_lock, flags);
1820 mutex_unlock(&hba->uic_cmd_mutex);
1821 return ret;
1822}
1823
1824/**
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05301825 * ufshcd_config_max_pwr_mode - Set & Change power mode with
1826 * maximum capability attribute information.
1827 * @hba: per adapter instance
1828 *
1829 * Returns 0 on success, non-zero value on failure
1830 */
1831static int ufshcd_config_max_pwr_mode(struct ufs_hba *hba)
1832{
1833 enum {RX = 0, TX = 1};
1834 u32 lanes[] = {1, 1};
1835 u32 gear[] = {1, 1};
1836 u8 pwr[] = {FASTAUTO_MODE, FASTAUTO_MODE};
1837 int ret;
1838
1839 /* Get the connected lane count */
1840 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), &lanes[RX]);
1841 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), &lanes[TX]);
1842
1843 /*
1844 * First, get the maximum gears of HS speed.
1845 * If a zero value, it means there is no HSGEAR capability.
1846 * Then, get the maximum gears of PWM speed.
1847 */
1848 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &gear[RX]);
1849 if (!gear[RX]) {
1850 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), &gear[RX]);
1851 pwr[RX] = SLOWAUTO_MODE;
1852 }
1853
1854 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &gear[TX]);
1855 if (!gear[TX]) {
1856 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1857 &gear[TX]);
1858 pwr[TX] = SLOWAUTO_MODE;
1859 }
1860
1861 /*
1862 * Configure attributes for power mode change with below.
1863 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1864 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1865 * - PA_HSSERIES
1866 */
1867 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), gear[RX]);
1868 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), lanes[RX]);
1869 if (pwr[RX] == FASTAUTO_MODE)
1870 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
1871
1872 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), gear[TX]);
1873 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), lanes[TX]);
1874 if (pwr[TX] == FASTAUTO_MODE)
1875 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
1876
1877 if (pwr[RX] == FASTAUTO_MODE || pwr[TX] == FASTAUTO_MODE)
1878 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), PA_HS_MODE_B);
1879
1880 ret = ufshcd_uic_change_pwr_mode(hba, pwr[RX] << 4 | pwr[TX]);
1881 if (ret)
1882 dev_err(hba->dev,
1883 "pwr_mode: power mode change failed %d\n", ret);
1884
1885 return ret;
1886}
1887
1888/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301889 * ufshcd_complete_dev_init() - checks device readiness
1890 * hba: per-adapter instance
1891 *
1892 * Set fDeviceInit flag and poll until device toggles it.
1893 */
1894static int ufshcd_complete_dev_init(struct ufs_hba *hba)
1895{
1896 int i, retries, err = 0;
1897 bool flag_res = 1;
1898
1899 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1900 /* Set the fDeviceInit flag */
1901 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
1902 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
1903 if (!err || err == -ETIMEDOUT)
1904 break;
1905 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1906 }
1907 if (err) {
1908 dev_err(hba->dev,
1909 "%s setting fDeviceInit flag failed with error %d\n",
1910 __func__, err);
1911 goto out;
1912 }
1913
1914 /* poll for max. 100 iterations for fDeviceInit flag to clear */
1915 for (i = 0; i < 100 && !err && flag_res; i++) {
1916 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1917 err = ufshcd_query_flag(hba,
1918 UPIU_QUERY_OPCODE_READ_FLAG,
1919 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
1920 if (!err || err == -ETIMEDOUT)
1921 break;
1922 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
1923 err);
1924 }
1925 }
1926 if (err)
1927 dev_err(hba->dev,
1928 "%s reading fDeviceInit flag failed with error %d\n",
1929 __func__, err);
1930 else if (flag_res)
1931 dev_err(hba->dev,
1932 "%s fDeviceInit was not cleared by the device\n",
1933 __func__);
1934
1935out:
1936 return err;
1937}
1938
1939/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301940 * ufshcd_make_hba_operational - Make UFS controller operational
1941 * @hba: per adapter instance
1942 *
1943 * To bring UFS host controller to operational state,
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03001944 * 1. Enable required interrupts
1945 * 2. Configure interrupt aggregation
1946 * 3. Program UTRL and UTMRL base addres
1947 * 4. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301948 *
1949 * Returns 0 on success, non-zero value on failure
1950 */
1951static int ufshcd_make_hba_operational(struct ufs_hba *hba)
1952{
1953 int err = 0;
1954 u32 reg;
1955
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301956 /* Enable required interrupts */
1957 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
1958
1959 /* Configure interrupt aggregation */
Seungwon Jeon7d568652013-08-31 21:40:20 +05301960 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301961
1962 /* Configure UTRL and UTMRL base address registers */
1963 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
1964 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
1965 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
1966 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
1967 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
1968 REG_UTP_TASK_REQ_LIST_BASE_L);
1969 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
1970 REG_UTP_TASK_REQ_LIST_BASE_H);
1971
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301972 /*
1973 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
1974 * DEI, HEI bits must be 0
1975 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03001976 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301977 if (!(ufshcd_get_lists_status(reg))) {
1978 ufshcd_enable_run_stop_reg(hba);
1979 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301980 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301981 "Host controller not ready to process requests");
1982 err = -EIO;
1983 goto out;
1984 }
1985
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301986out:
1987 return err;
1988}
1989
1990/**
1991 * ufshcd_hba_enable - initialize the controller
1992 * @hba: per adapter instance
1993 *
1994 * The controller resets itself and controller firmware initialization
1995 * sequence kicks off. When controller is ready it will set
1996 * the Host Controller Enable bit to 1.
1997 *
1998 * Returns 0 on success, non-zero value on failure
1999 */
2000static int ufshcd_hba_enable(struct ufs_hba *hba)
2001{
2002 int retry;
2003
2004 /*
2005 * msleep of 1 and 5 used in this function might result in msleep(20),
2006 * but it was necessary to send the UFS FPGA to reset mode during
2007 * development and testing of this driver. msleep can be changed to
2008 * mdelay and retry count can be reduced based on the controller.
2009 */
2010 if (!ufshcd_is_hba_active(hba)) {
2011
2012 /* change controller state to "reset state" */
2013 ufshcd_hba_stop(hba);
2014
2015 /*
2016 * This delay is based on the testing done with UFS host
2017 * controller FPGA. The delay can be changed based on the
2018 * host controller used.
2019 */
2020 msleep(5);
2021 }
2022
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002023 if (hba->vops && hba->vops->hce_enable_notify)
2024 hba->vops->hce_enable_notify(hba, PRE_CHANGE);
2025
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302026 /* start controller initialization sequence */
2027 ufshcd_hba_start(hba);
2028
2029 /*
2030 * To initialize a UFS host controller HCE bit must be set to 1.
2031 * During initialization the HCE bit value changes from 1->0->1.
2032 * When the host controller completes initialization sequence
2033 * it sets the value of HCE bit to 1. The same HCE bit is read back
2034 * to check if the controller has completed initialization sequence.
2035 * So without this delay the value HCE = 1, set in the previous
2036 * instruction might be read back.
2037 * This delay can be changed based on the controller.
2038 */
2039 msleep(1);
2040
2041 /* wait for the host controller to complete initialization */
2042 retry = 10;
2043 while (ufshcd_is_hba_active(hba)) {
2044 if (retry) {
2045 retry--;
2046 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302047 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302048 "Controller enable failed\n");
2049 return -EIO;
2050 }
2051 msleep(5);
2052 }
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002053
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002054 /* enable UIC related interrupts */
2055 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
2056
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002057 if (hba->vops && hba->vops->hce_enable_notify)
2058 hba->vops->hce_enable_notify(hba, POST_CHANGE);
2059
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302060 return 0;
2061}
2062
2063/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302064 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302065 * @hba: per adapter instance
2066 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302067 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302068 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302069static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302070{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302071 int ret;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002072 int retries = DME_LINKSTARTUP_RETRIES;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302073
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002074 do {
2075 if (hba->vops && hba->vops->link_startup_notify)
2076 hba->vops->link_startup_notify(hba, PRE_CHANGE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302077
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002078 ret = ufshcd_dme_link_startup(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002079
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002080 /* check if device is detected by inter-connect layer */
2081 if (!ret && !ufshcd_is_device_present(hba)) {
2082 dev_err(hba->dev, "%s: Device not present\n", __func__);
2083 ret = -ENXIO;
2084 goto out;
2085 }
2086
2087 /*
2088 * DME link lost indication is only received when link is up,
2089 * but we can't be sure if the link is up until link startup
2090 * succeeds. So reset the local Uni-Pro and try again.
2091 */
2092 if (ret && ufshcd_hba_enable(hba))
2093 goto out;
2094 } while (ret && retries--);
2095
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302096 if (ret)
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03002097 /* failed to get the link up... retire */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302098 goto out;
2099
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03002100 /* Include any host controller configuration via UIC commands */
2101 if (hba->vops && hba->vops->link_startup_notify) {
2102 ret = hba->vops->link_startup_notify(hba, POST_CHANGE);
2103 if (ret)
2104 goto out;
2105 }
2106
2107 ret = ufshcd_make_hba_operational(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302108out:
2109 if (ret)
2110 dev_err(hba->dev, "link startup failed %d\n", ret);
2111 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302112}
2113
2114/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302115 * ufshcd_verify_dev_init() - Verify device initialization
2116 * @hba: per-adapter instance
2117 *
2118 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
2119 * device Transport Protocol (UTP) layer is ready after a reset.
2120 * If the UTP layer at the device side is not initialized, it may
2121 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
2122 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
2123 */
2124static int ufshcd_verify_dev_init(struct ufs_hba *hba)
2125{
2126 int err = 0;
2127 int retries;
2128
2129 mutex_lock(&hba->dev_cmd.lock);
2130 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
2131 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
2132 NOP_OUT_TIMEOUT);
2133
2134 if (!err || err == -ETIMEDOUT)
2135 break;
2136
2137 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
2138 }
2139 mutex_unlock(&hba->dev_cmd.lock);
2140
2141 if (err)
2142 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2143 return err;
2144}
2145
2146/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302147 * ufshcd_slave_alloc - handle initial SCSI device configurations
2148 * @sdev: pointer to SCSI device
2149 *
2150 * Returns success
2151 */
2152static int ufshcd_slave_alloc(struct scsi_device *sdev)
2153{
2154 struct ufs_hba *hba;
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002155 u8 lun_qdepth;
2156 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302157
2158 hba = shost_priv(sdev->host);
2159 sdev->tagged_supported = 1;
2160
2161 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
2162 sdev->use_10_for_ms = 1;
2163 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2164
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302165 /* allow SCSI layer to restart the device in case of errors */
2166 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002167
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03002168 /* REPORT SUPPORTED OPERATION CODES is not supported */
2169 sdev->no_report_opcodes = 1;
2170
Subhash Jadavanida461ce2014-09-25 15:32:25 +03002171 ret = ufshcd_read_unit_desc_param(hba,
2172 sdev->lun,
2173 UNIT_DESC_PARAM_LU_Q_DEPTH,
2174 &lun_qdepth,
2175 sizeof(lun_qdepth));
2176 if (!ret || !lun_qdepth)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002177 /* eventually, we can figure out the real queue depth */
Dolev Raviv1b3e8952014-06-29 09:40:19 +03002178 lun_qdepth = hba->nutrs;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002179 else
2180 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2181
2182 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2183 __func__, lun_qdepth);
2184 scsi_activate_tcq(sdev, lun_qdepth);
2185
2186 return 0;
2187}
2188
2189/**
2190 * ufshcd_change_queue_depth - change queue depth
2191 * @sdev: pointer to SCSI device
2192 * @depth: required depth to set
2193 * @reason: reason for changing the depth
2194 *
2195 * Change queue depth according to the reason and make sure
2196 * the max. limits are not crossed.
2197 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03002198static int ufshcd_change_queue_depth(struct scsi_device *sdev,
2199 int depth, int reason)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002200{
2201 struct ufs_hba *hba = shost_priv(sdev->host);
2202
2203 if (depth > hba->nutrs)
2204 depth = hba->nutrs;
2205
2206 switch (reason) {
2207 case SCSI_QDEPTH_DEFAULT:
2208 case SCSI_QDEPTH_RAMP_UP:
2209 if (!sdev->tagged_supported)
2210 depth = 1;
2211 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2212 break;
2213 case SCSI_QDEPTH_QFULL:
2214 scsi_track_queue_full(sdev, depth);
2215 break;
2216 default:
2217 return -EOPNOTSUPP;
Dolev Raviv1b3e8952014-06-29 09:40:19 +03002218 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302219
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002220 return depth;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302221}
2222
2223/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09002224 * ufshcd_slave_configure - adjust SCSI device configurations
2225 * @sdev: pointer to SCSI device
2226 */
2227static int ufshcd_slave_configure(struct scsi_device *sdev)
2228{
2229 struct request_queue *q = sdev->request_queue;
2230
2231 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
2232 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
2233
2234 return 0;
2235}
2236
2237/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302238 * ufshcd_slave_destroy - remove SCSI device configurations
2239 * @sdev: pointer to SCSI device
2240 */
2241static void ufshcd_slave_destroy(struct scsi_device *sdev)
2242{
2243 struct ufs_hba *hba;
2244
2245 hba = shost_priv(sdev->host);
2246 scsi_deactivate_tcq(sdev, hba->nutrs);
2247}
2248
2249/**
2250 * ufshcd_task_req_compl - handle task management request completion
2251 * @hba: per adapter instance
2252 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302253 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302254 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302255 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302256 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302257static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302258{
2259 struct utp_task_req_desc *task_req_descp;
2260 struct utp_upiu_task_rsp *task_rsp_upiup;
2261 unsigned long flags;
2262 int ocs_value;
2263 int task_result;
2264
2265 spin_lock_irqsave(hba->host->host_lock, flags);
2266
2267 /* Clear completed tasks from outstanding_tasks */
2268 __clear_bit(index, &hba->outstanding_tasks);
2269
2270 task_req_descp = hba->utmrdl_base_addr;
2271 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
2272
2273 if (ocs_value == OCS_SUCCESS) {
2274 task_rsp_upiup = (struct utp_upiu_task_rsp *)
2275 task_req_descp[index].task_rsp_upiu;
2276 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
2277 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302278 if (resp)
2279 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302280 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302281 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
2282 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302283 }
2284 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302285
2286 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302287}
2288
2289/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302290 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
2291 * @lrb: pointer to local reference block of completed command
2292 * @scsi_status: SCSI command status
2293 *
2294 * Returns value base on SCSI command status
2295 */
2296static inline int
2297ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
2298{
2299 int result = 0;
2300
2301 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302302 case SAM_STAT_CHECK_CONDITION:
2303 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302304 case SAM_STAT_GOOD:
2305 result |= DID_OK << 16 |
2306 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302307 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302308 break;
2309 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302310 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302311 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302312 ufshcd_copy_sense_data(lrbp);
2313 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302314 break;
2315 default:
2316 result |= DID_ERROR << 16;
2317 break;
2318 } /* end of switch */
2319
2320 return result;
2321}
2322
2323/**
2324 * ufshcd_transfer_rsp_status - Get overall status of the response
2325 * @hba: per adapter instance
2326 * @lrb: pointer to local reference block of completed command
2327 *
2328 * Returns result of the command to notify SCSI midlayer
2329 */
2330static inline int
2331ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2332{
2333 int result = 0;
2334 int scsi_status;
2335 int ocs;
2336
2337 /* overall command status of utrd */
2338 ocs = ufshcd_get_tr_ocs(lrbp);
2339
2340 switch (ocs) {
2341 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302342 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302343
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302344 switch (result) {
2345 case UPIU_TRANSACTION_RESPONSE:
2346 /*
2347 * get the response UPIU result to extract
2348 * the SCSI command status
2349 */
2350 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
2351
2352 /*
2353 * get the result based on SCSI status response
2354 * to notify the SCSI midlayer of the command status
2355 */
2356 scsi_status = result & MASK_SCSI_STATUS;
2357 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302358
2359 if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
2360 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302361 break;
2362 case UPIU_TRANSACTION_REJECT_UPIU:
2363 /* TODO: handle Reject UPIU Response */
2364 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302365 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302366 "Reject UPIU not fully implemented\n");
2367 break;
2368 default:
2369 result = DID_ERROR << 16;
2370 dev_err(hba->dev,
2371 "Unexpected request response code = %x\n",
2372 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302373 break;
2374 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302375 break;
2376 case OCS_ABORTED:
2377 result |= DID_ABORT << 16;
2378 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302379 case OCS_INVALID_COMMAND_STATUS:
2380 result |= DID_REQUEUE << 16;
2381 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302382 case OCS_INVALID_CMD_TABLE_ATTR:
2383 case OCS_INVALID_PRDT_ATTR:
2384 case OCS_MISMATCH_DATA_BUF_SIZE:
2385 case OCS_MISMATCH_RESP_UPIU_SIZE:
2386 case OCS_PEER_COMM_FAILURE:
2387 case OCS_FATAL_ERROR:
2388 default:
2389 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302390 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302391 "OCS error from controller = %x\n", ocs);
2392 break;
2393 } /* end of switch */
2394
2395 return result;
2396}
2397
2398/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302399 * ufshcd_uic_cmd_compl - handle completion of uic command
2400 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302401 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302402 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302403static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302404{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302405 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302406 hba->active_uic_cmd->argument2 |=
2407 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302408 hba->active_uic_cmd->argument3 =
2409 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302410 complete(&hba->active_uic_cmd->done);
2411 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302412
2413 if ((intr_status & UIC_POWER_MODE) && hba->pwr_done)
2414 complete(hba->pwr_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302415}
2416
2417/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302418 * ufshcd_transfer_req_compl - handle SCSI and query command completion
2419 * @hba: per adapter instance
2420 */
2421static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
2422{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302423 struct ufshcd_lrb *lrbp;
2424 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302425 unsigned long completed_reqs;
2426 u32 tr_doorbell;
2427 int result;
2428 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03002429
2430 /* Resetting interrupt aggregation counters first and reading the
2431 * DOOR_BELL afterward allows us to handle all the completed requests.
2432 * In order to prevent other interrupts starvation the DB is read once
2433 * after reset. The down side of this solution is the possibility of
2434 * false interrupt if device completes another request after resetting
2435 * aggregation and before reading the DB.
2436 */
2437 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302438
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302439 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302440 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
2441
Dolev Ravive9d501b2014-07-01 12:22:37 +03002442 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
2443 lrbp = &hba->lrb[index];
2444 cmd = lrbp->cmd;
2445 if (cmd) {
2446 result = ufshcd_transfer_rsp_status(hba, lrbp);
2447 scsi_dma_unmap(cmd);
2448 cmd->result = result;
2449 /* Mark completed command as NULL in LRB */
2450 lrbp->cmd = NULL;
2451 clear_bit_unlock(index, &hba->lrb_in_use);
2452 /* Do not touch lrbp after scsi done */
2453 cmd->scsi_done(cmd);
2454 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
2455 if (hba->dev_cmd.complete)
2456 complete(hba->dev_cmd.complete);
2457 }
2458 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302459
2460 /* clear corresponding bits of completed commands */
2461 hba->outstanding_reqs ^= completed_reqs;
2462
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302463 /* we might have free'd some tags above */
2464 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302465}
2466
2467/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302468 * ufshcd_disable_ee - disable exception event
2469 * @hba: per-adapter instance
2470 * @mask: exception event to disable
2471 *
2472 * Disables exception event in the device so that the EVENT_ALERT
2473 * bit is not set.
2474 *
2475 * Returns zero on success, non-zero error value on failure.
2476 */
2477static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
2478{
2479 int err = 0;
2480 u32 val;
2481
2482 if (!(hba->ee_ctrl_mask & mask))
2483 goto out;
2484
2485 val = hba->ee_ctrl_mask & ~mask;
2486 val &= 0xFFFF; /* 2 bytes */
2487 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
2488 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
2489 if (!err)
2490 hba->ee_ctrl_mask &= ~mask;
2491out:
2492 return err;
2493}
2494
2495/**
2496 * ufshcd_enable_ee - enable exception event
2497 * @hba: per-adapter instance
2498 * @mask: exception event to enable
2499 *
2500 * Enable corresponding exception event in the device to allow
2501 * device to alert host in critical scenarios.
2502 *
2503 * Returns zero on success, non-zero error value on failure.
2504 */
2505static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
2506{
2507 int err = 0;
2508 u32 val;
2509
2510 if (hba->ee_ctrl_mask & mask)
2511 goto out;
2512
2513 val = hba->ee_ctrl_mask | mask;
2514 val &= 0xFFFF; /* 2 bytes */
2515 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
2516 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
2517 if (!err)
2518 hba->ee_ctrl_mask |= mask;
2519out:
2520 return err;
2521}
2522
2523/**
2524 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
2525 * @hba: per-adapter instance
2526 *
2527 * Allow device to manage background operations on its own. Enabling
2528 * this might lead to inconsistent latencies during normal data transfers
2529 * as the device is allowed to manage its own way of handling background
2530 * operations.
2531 *
2532 * Returns zero on success, non-zero on failure.
2533 */
2534static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
2535{
2536 int err = 0;
2537
2538 if (hba->auto_bkops_enabled)
2539 goto out;
2540
2541 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2542 QUERY_FLAG_IDN_BKOPS_EN, NULL);
2543 if (err) {
2544 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
2545 __func__, err);
2546 goto out;
2547 }
2548
2549 hba->auto_bkops_enabled = true;
2550
2551 /* No need of URGENT_BKOPS exception from the device */
2552 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
2553 if (err)
2554 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
2555 __func__, err);
2556out:
2557 return err;
2558}
2559
2560/**
2561 * ufshcd_disable_auto_bkops - block device in doing background operations
2562 * @hba: per-adapter instance
2563 *
2564 * Disabling background operations improves command response latency but
2565 * has drawback of device moving into critical state where the device is
2566 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
2567 * host is idle so that BKOPS are managed effectively without any negative
2568 * impacts.
2569 *
2570 * Returns zero on success, non-zero on failure.
2571 */
2572static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
2573{
2574 int err = 0;
2575
2576 if (!hba->auto_bkops_enabled)
2577 goto out;
2578
2579 /*
2580 * If host assisted BKOPs is to be enabled, make sure
2581 * urgent bkops exception is allowed.
2582 */
2583 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
2584 if (err) {
2585 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
2586 __func__, err);
2587 goto out;
2588 }
2589
2590 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
2591 QUERY_FLAG_IDN_BKOPS_EN, NULL);
2592 if (err) {
2593 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
2594 __func__, err);
2595 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
2596 goto out;
2597 }
2598
2599 hba->auto_bkops_enabled = false;
2600out:
2601 return err;
2602}
2603
2604/**
2605 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
2606 * @hba: per adapter instance
2607 *
2608 * After a device reset the device may toggle the BKOPS_EN flag
2609 * to default value. The s/w tracking variables should be updated
2610 * as well. Do this by forcing enable of auto bkops.
2611 */
2612static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
2613{
2614 hba->auto_bkops_enabled = false;
2615 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
2616 ufshcd_enable_auto_bkops(hba);
2617}
2618
2619static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
2620{
2621 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
2622 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
2623}
2624
2625/**
2626 * ufshcd_urgent_bkops - handle urgent bkops exception event
2627 * @hba: per-adapter instance
2628 *
2629 * Enable fBackgroundOpsEn flag in the device to permit background
2630 * operations.
2631 */
2632static int ufshcd_urgent_bkops(struct ufs_hba *hba)
2633{
2634 int err;
2635 u32 status = 0;
2636
2637 err = ufshcd_get_bkops_status(hba, &status);
2638 if (err) {
2639 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
2640 __func__, err);
2641 goto out;
2642 }
2643
2644 status = status & 0xF;
2645
2646 /* handle only if status indicates performance impact or critical */
2647 if (status >= BKOPS_STATUS_PERF_IMPACT)
2648 err = ufshcd_enable_auto_bkops(hba);
2649out:
2650 return err;
2651}
2652
2653static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
2654{
2655 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
2656 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
2657}
2658
2659/**
2660 * ufshcd_exception_event_handler - handle exceptions raised by device
2661 * @work: pointer to work data
2662 *
2663 * Read bExceptionEventStatus attribute from the device and handle the
2664 * exception event accordingly.
2665 */
2666static void ufshcd_exception_event_handler(struct work_struct *work)
2667{
2668 struct ufs_hba *hba;
2669 int err;
2670 u32 status = 0;
2671 hba = container_of(work, struct ufs_hba, eeh_work);
2672
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302673 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302674 err = ufshcd_get_ee_status(hba, &status);
2675 if (err) {
2676 dev_err(hba->dev, "%s: failed to get exception status %d\n",
2677 __func__, err);
2678 goto out;
2679 }
2680
2681 status &= hba->ee_ctrl_mask;
2682 if (status & MASK_EE_URGENT_BKOPS) {
2683 err = ufshcd_urgent_bkops(hba);
2684 if (err)
2685 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
2686 __func__, err);
2687 }
2688out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302689 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302690 return;
2691}
2692
2693/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302694 * ufshcd_err_handler - handle UFS errors that require s/w attention
2695 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302696 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302697static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302698{
2699 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302700 unsigned long flags;
2701 u32 err_xfer = 0;
2702 u32 err_tm = 0;
2703 int err = 0;
2704 int tag;
2705
2706 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302707
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302708 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302709
2710 spin_lock_irqsave(hba->host->host_lock, flags);
2711 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
2712 spin_unlock_irqrestore(hba->host->host_lock, flags);
2713 goto out;
2714 }
2715
2716 hba->ufshcd_state = UFSHCD_STATE_RESET;
2717 ufshcd_set_eh_in_progress(hba);
2718
2719 /* Complete requests that have door-bell cleared by h/w */
2720 ufshcd_transfer_req_compl(hba);
2721 ufshcd_tmc_handler(hba);
2722 spin_unlock_irqrestore(hba->host->host_lock, flags);
2723
2724 /* Clear pending transfer requests */
2725 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
2726 if (ufshcd_clear_cmd(hba, tag))
2727 err_xfer |= 1 << tag;
2728
2729 /* Clear pending task management requests */
2730 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
2731 if (ufshcd_clear_tm_cmd(hba, tag))
2732 err_tm |= 1 << tag;
2733
2734 /* Complete the requests that are cleared by s/w */
2735 spin_lock_irqsave(hba->host->host_lock, flags);
2736 ufshcd_transfer_req_compl(hba);
2737 ufshcd_tmc_handler(hba);
2738 spin_unlock_irqrestore(hba->host->host_lock, flags);
2739
2740 /* Fatal errors need reset */
2741 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
2742 ((hba->saved_err & UIC_ERROR) &&
2743 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
2744 err = ufshcd_reset_and_restore(hba);
2745 if (err) {
2746 dev_err(hba->dev, "%s: reset and restore failed\n",
2747 __func__);
2748 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2749 }
2750 /*
2751 * Inform scsi mid-layer that we did reset and allow to handle
2752 * Unit Attention properly.
2753 */
2754 scsi_report_bus_reset(hba->host, 0);
2755 hba->saved_err = 0;
2756 hba->saved_uic_err = 0;
2757 }
2758 ufshcd_clear_eh_in_progress(hba);
2759
2760out:
2761 scsi_unblock_requests(hba->host);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302762 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302763}
2764
2765/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302766 * ufshcd_update_uic_error - check and set fatal UIC error flags.
2767 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302768 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302769static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302770{
2771 u32 reg;
2772
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302773 /* PA_INIT_ERROR is fatal and needs UIC reset */
2774 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
2775 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
2776 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
2777
2778 /* UIC NL/TL/DME errors needs software retry */
2779 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
2780 if (reg)
2781 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
2782
2783 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
2784 if (reg)
2785 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
2786
2787 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
2788 if (reg)
2789 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
2790
2791 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
2792 __func__, hba->uic_error);
2793}
2794
2795/**
2796 * ufshcd_check_errors - Check for errors that need s/w attention
2797 * @hba: per-adapter instance
2798 */
2799static void ufshcd_check_errors(struct ufs_hba *hba)
2800{
2801 bool queue_eh_work = false;
2802
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302803 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302804 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302805
2806 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302807 hba->uic_error = 0;
2808 ufshcd_update_uic_error(hba);
2809 if (hba->uic_error)
2810 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302811 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302812
2813 if (queue_eh_work) {
2814 /* handle fatal errors only when link is functional */
2815 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
2816 /* block commands from scsi mid-layer */
2817 scsi_block_requests(hba->host);
2818
2819 /* transfer error masks to sticky bits */
2820 hba->saved_err |= hba->errors;
2821 hba->saved_uic_err |= hba->uic_error;
2822
2823 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2824 schedule_work(&hba->eh_work);
2825 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302826 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302827 /*
2828 * if (!queue_eh_work) -
2829 * Other errors are either non-fatal where host recovers
2830 * itself without s/w intervention or errors that will be
2831 * handled by the SCSI core layer.
2832 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302833}
2834
2835/**
2836 * ufshcd_tmc_handler - handle task management function completion
2837 * @hba: per adapter instance
2838 */
2839static void ufshcd_tmc_handler(struct ufs_hba *hba)
2840{
2841 u32 tm_doorbell;
2842
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302843 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302844 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302845 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302846}
2847
2848/**
2849 * ufshcd_sl_intr - Interrupt service routine
2850 * @hba: per adapter instance
2851 * @intr_status: contains interrupts generated by the controller
2852 */
2853static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
2854{
2855 hba->errors = UFSHCD_ERROR_MASK & intr_status;
2856 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302857 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302858
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302859 if (intr_status & UFSHCD_UIC_MASK)
2860 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302861
2862 if (intr_status & UTP_TASK_REQ_COMPL)
2863 ufshcd_tmc_handler(hba);
2864
2865 if (intr_status & UTP_TRANSFER_REQ_COMPL)
2866 ufshcd_transfer_req_compl(hba);
2867}
2868
2869/**
2870 * ufshcd_intr - Main interrupt service routine
2871 * @irq: irq number
2872 * @__hba: pointer to adapter instance
2873 *
2874 * Returns IRQ_HANDLED - If interrupt is valid
2875 * IRQ_NONE - If invalid interrupt
2876 */
2877static irqreturn_t ufshcd_intr(int irq, void *__hba)
2878{
2879 u32 intr_status;
2880 irqreturn_t retval = IRQ_NONE;
2881 struct ufs_hba *hba = __hba;
2882
2883 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302884 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302885
2886 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05302887 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302888 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302889 retval = IRQ_HANDLED;
2890 }
2891 spin_unlock(hba->host->host_lock);
2892 return retval;
2893}
2894
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302895static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
2896{
2897 int err = 0;
2898 u32 mask = 1 << tag;
2899 unsigned long flags;
2900
2901 if (!test_bit(tag, &hba->outstanding_tasks))
2902 goto out;
2903
2904 spin_lock_irqsave(hba->host->host_lock, flags);
2905 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
2906 spin_unlock_irqrestore(hba->host->host_lock, flags);
2907
2908 /* poll for max. 1 sec to clear door bell register by h/w */
2909 err = ufshcd_wait_for_register(hba,
2910 REG_UTP_TASK_REQ_DOOR_BELL,
2911 mask, 0, 1000, 1000);
2912out:
2913 return err;
2914}
2915
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302916/**
2917 * ufshcd_issue_tm_cmd - issues task management commands to controller
2918 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302919 * @lun_id: LUN ID to which TM command is sent
2920 * @task_id: task ID to which the TM command is applicable
2921 * @tm_function: task management function opcode
2922 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302923 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302924 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302925 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302926static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
2927 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302928{
2929 struct utp_task_req_desc *task_req_descp;
2930 struct utp_upiu_task_req *task_req_upiup;
2931 struct Scsi_Host *host;
2932 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302933 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302934 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302935 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302936
2937 host = hba->host;
2938
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302939 /*
2940 * Get free slot, sleep if slots are unavailable.
2941 * Even though we use wait_event() which sleeps indefinitely,
2942 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
2943 */
2944 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
2945
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302946 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302947 task_req_descp = hba->utmrdl_base_addr;
2948 task_req_descp += free_slot;
2949
2950 /* Configure task request descriptor */
2951 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
2952 task_req_descp->header.dword_2 =
2953 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
2954
2955 /* Configure task request UPIU */
2956 task_req_upiup =
2957 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302958 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302959 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302960 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302961 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302962 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302963 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302964
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302965 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
2966 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302967
2968 /* send command to the controller */
2969 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302970 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302971
2972 spin_unlock_irqrestore(host->host_lock, flags);
2973
2974 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302975 err = wait_event_timeout(hba->tm_wq,
2976 test_bit(free_slot, &hba->tm_condition),
2977 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302978 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302979 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
2980 __func__, tm_function);
2981 if (ufshcd_clear_tm_cmd(hba, free_slot))
2982 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
2983 __func__, free_slot);
2984 err = -ETIMEDOUT;
2985 } else {
2986 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302987 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302988
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302989 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302990 ufshcd_put_tm_slot(hba, free_slot);
2991 wake_up(&hba->tm_tag_wq);
2992
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302993 return err;
2994}
2995
2996/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302997 * ufshcd_eh_device_reset_handler - device reset handler registered to
2998 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302999 * @cmd: SCSI command pointer
3000 *
3001 * Returns SUCCESS/FAILED
3002 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303003static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303004{
3005 struct Scsi_Host *host;
3006 struct ufs_hba *hba;
3007 unsigned int tag;
3008 u32 pos;
3009 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303010 u8 resp = 0xF;
3011 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303012 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303013
3014 host = cmd->device->host;
3015 hba = shost_priv(host);
3016 tag = cmd->request->tag;
3017
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303018 lrbp = &hba->lrb[tag];
3019 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
3020 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303021 if (!err)
3022 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303023 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303024 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303025
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303026 /* clear the commands that were pending for corresponding LUN */
3027 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
3028 if (hba->lrb[pos].lun == lrbp->lun) {
3029 err = ufshcd_clear_cmd(hba, pos);
3030 if (err)
3031 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303032 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303033 }
3034 spin_lock_irqsave(host->host_lock, flags);
3035 ufshcd_transfer_req_compl(hba);
3036 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303037out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303038 if (!err) {
3039 err = SUCCESS;
3040 } else {
3041 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3042 err = FAILED;
3043 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303044 return err;
3045}
3046
3047/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303048 * ufshcd_abort - abort a specific command
3049 * @cmd: SCSI command pointer
3050 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303051 * Abort the pending command in device by sending UFS_ABORT_TASK task management
3052 * command, and in host controller by clearing the door-bell register. There can
3053 * be race between controller sending the command to the device while abort is
3054 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
3055 * really issued and then try to abort it.
3056 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303057 * Returns SUCCESS/FAILED
3058 */
3059static int ufshcd_abort(struct scsi_cmnd *cmd)
3060{
3061 struct Scsi_Host *host;
3062 struct ufs_hba *hba;
3063 unsigned long flags;
3064 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303065 int err = 0;
3066 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303067 u8 resp = 0xF;
3068 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03003069 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303070
3071 host = cmd->device->host;
3072 hba = shost_priv(host);
3073 tag = cmd->request->tag;
3074
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303075 /* If command is already aborted/completed, return SUCCESS */
3076 if (!(test_bit(tag, &hba->outstanding_reqs)))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303077 goto out;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303078
Dolev Ravive9d501b2014-07-01 12:22:37 +03003079 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3080 if (!(reg & (1 << tag))) {
3081 dev_err(hba->dev,
3082 "%s: cmd was completed, but without a notifying intr, tag = %d",
3083 __func__, tag);
3084 }
3085
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303086 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303087 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
3088 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3089 UFS_QUERY_TASK, &resp);
3090 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
3091 /* cmd pending in the device */
3092 break;
3093 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303094 /*
3095 * cmd not pending in the device, check if it is
3096 * in transition.
3097 */
3098 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
3099 if (reg & (1 << tag)) {
3100 /* sleep for max. 200us to stabilize */
3101 usleep_range(100, 200);
3102 continue;
3103 }
3104 /* command completed already */
3105 goto out;
3106 } else {
3107 if (!err)
3108 err = resp; /* service response error */
3109 goto out;
3110 }
3111 }
3112
3113 if (!poll_cnt) {
3114 err = -EBUSY;
3115 goto out;
3116 }
3117
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303118 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
3119 UFS_ABORT_TASK, &resp);
3120 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303121 if (!err)
3122 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303123 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303124 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303125
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303126 err = ufshcd_clear_cmd(hba, tag);
3127 if (err)
3128 goto out;
3129
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303130 scsi_dma_unmap(cmd);
3131
3132 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303133 __clear_bit(tag, &hba->outstanding_reqs);
3134 hba->lrb[tag].cmd = NULL;
3135 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303136
3137 clear_bit_unlock(tag, &hba->lrb_in_use);
3138 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303139out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05303140 if (!err) {
3141 err = SUCCESS;
3142 } else {
3143 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3144 err = FAILED;
3145 }
3146
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303147 return err;
3148}
3149
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303150/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303151 * ufshcd_host_reset_and_restore - reset and restore host controller
3152 * @hba: per-adapter instance
3153 *
3154 * Note that host controller reset may issue DME_RESET to
3155 * local and remote (device) Uni-Pro stack and the attributes
3156 * are reset to default state.
3157 *
3158 * Returns zero on success, non-zero on failure
3159 */
3160static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
3161{
3162 int err;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303163 unsigned long flags;
3164
3165 /* Reset the host controller */
3166 spin_lock_irqsave(hba->host->host_lock, flags);
3167 ufshcd_hba_stop(hba);
3168 spin_unlock_irqrestore(hba->host->host_lock, flags);
3169
3170 err = ufshcd_hba_enable(hba);
3171 if (err)
3172 goto out;
3173
3174 /* Establish the link again and restore the device */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003175 err = ufshcd_probe_hba(hba);
3176
3177 if (!err && (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL))
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303178 err = -EIO;
3179out:
3180 if (err)
3181 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
3182
3183 return err;
3184}
3185
3186/**
3187 * ufshcd_reset_and_restore - reset and re-initialize host/device
3188 * @hba: per-adapter instance
3189 *
3190 * Reset and recover device, host and re-establish link. This
3191 * is helpful to recover the communication in fatal error conditions.
3192 *
3193 * Returns zero on success, non-zero on failure
3194 */
3195static int ufshcd_reset_and_restore(struct ufs_hba *hba)
3196{
3197 int err = 0;
3198 unsigned long flags;
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003199 int retries = MAX_HOST_RESET_RETRIES;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303200
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003201 do {
3202 err = ufshcd_host_reset_and_restore(hba);
3203 } while (err && --retries);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303204
3205 /*
3206 * After reset the door-bell might be cleared, complete
3207 * outstanding requests in s/w here.
3208 */
3209 spin_lock_irqsave(hba->host->host_lock, flags);
3210 ufshcd_transfer_req_compl(hba);
3211 ufshcd_tmc_handler(hba);
3212 spin_unlock_irqrestore(hba->host->host_lock, flags);
3213
3214 return err;
3215}
3216
3217/**
3218 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
3219 * @cmd - SCSI command pointer
3220 *
3221 * Returns SUCCESS/FAILED
3222 */
3223static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
3224{
3225 int err;
3226 unsigned long flags;
3227 struct ufs_hba *hba;
3228
3229 hba = shost_priv(cmd->device->host);
3230
3231 /*
3232 * Check if there is any race with fatal error handling.
3233 * If so, wait for it to complete. Even though fatal error
3234 * handling does reset and restore in some cases, don't assume
3235 * anything out of it. We are just avoiding race here.
3236 */
3237 do {
3238 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303239 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303240 hba->ufshcd_state == UFSHCD_STATE_RESET))
3241 break;
3242 spin_unlock_irqrestore(hba->host->host_lock, flags);
3243 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303244 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303245 } while (1);
3246
3247 hba->ufshcd_state = UFSHCD_STATE_RESET;
3248 ufshcd_set_eh_in_progress(hba);
3249 spin_unlock_irqrestore(hba->host->host_lock, flags);
3250
3251 err = ufshcd_reset_and_restore(hba);
3252
3253 spin_lock_irqsave(hba->host->host_lock, flags);
3254 if (!err) {
3255 err = SUCCESS;
3256 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3257 } else {
3258 err = FAILED;
3259 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3260 }
3261 ufshcd_clear_eh_in_progress(hba);
3262 spin_unlock_irqrestore(hba->host->host_lock, flags);
3263
3264 return err;
3265}
3266
3267/**
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003268 * ufshcd_probe_hba - probe hba to detect device and initialize
3269 * @hba: per-adapter instance
3270 *
3271 * Execute link-startup and verify device initialization
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303272 */
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003273static int ufshcd_probe_hba(struct ufs_hba *hba)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303274{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303275 int ret;
3276
3277 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303278 if (ret)
3279 goto out;
3280
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303281 ufshcd_config_max_pwr_mode(hba);
3282
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303283 ret = ufshcd_verify_dev_init(hba);
3284 if (ret)
3285 goto out;
3286
Dolev Raviv68078d52013-07-30 00:35:58 +05303287 ret = ufshcd_complete_dev_init(hba);
3288 if (ret)
3289 goto out;
3290
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303291 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303292 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3293
3294 /* If we are in error handling context no need to scan the host */
3295 if (!ufshcd_eh_in_progress(hba)) {
3296 scsi_scan_host(hba->host);
3297 pm_runtime_put_sync(hba->dev);
3298 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303299out:
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003300 /*
3301 * If we failed to initialize the device or the device is not
3302 * present, turn off the power/clocks etc.
3303 */
3304 if (ret && !ufshcd_eh_in_progress(hba))
3305 ufshcd_hba_exit(hba);
3306
3307 return ret;
3308}
3309
3310/**
3311 * ufshcd_async_scan - asynchronous execution for probing hba
3312 * @data: data pointer to pass to this function
3313 * @cookie: cookie data
3314 */
3315static void ufshcd_async_scan(void *data, async_cookie_t cookie)
3316{
3317 struct ufs_hba *hba = (struct ufs_hba *)data;
3318
3319 ufshcd_probe_hba(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303320}
3321
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303322static struct scsi_host_template ufshcd_driver_template = {
3323 .module = THIS_MODULE,
3324 .name = UFSHCD,
3325 .proc_name = UFSHCD,
3326 .queuecommand = ufshcd_queuecommand,
3327 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003328 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303329 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003330 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303331 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303332 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
3333 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303334 .this_id = -1,
3335 .sg_tablesize = SG_ALL,
3336 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
3337 .can_queue = UFSHCD_CAN_QUEUE,
3338};
3339
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003340static int ufshcd_config_vreg(struct device *dev,
3341 struct ufs_vreg *vreg, bool on)
3342{
3343 int ret = 0;
3344 struct regulator *reg = vreg->reg;
3345 const char *name = vreg->name;
3346 int min_uV, uA_load;
3347
3348 BUG_ON(!vreg);
3349
3350 if (regulator_count_voltages(reg) > 0) {
3351 min_uV = on ? vreg->min_uV : 0;
3352 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
3353 if (ret) {
3354 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
3355 __func__, name, ret);
3356 goto out;
3357 }
3358
3359 uA_load = on ? vreg->max_uA : 0;
3360 ret = regulator_set_optimum_mode(reg, uA_load);
3361 if (ret >= 0) {
3362 /*
3363 * regulator_set_optimum_mode() returns new regulator
3364 * mode upon success.
3365 */
3366 ret = 0;
3367 } else {
3368 dev_err(dev, "%s: %s set optimum mode(uA_load=%d) failed, err=%d\n",
3369 __func__, name, uA_load, ret);
3370 goto out;
3371 }
3372 }
3373out:
3374 return ret;
3375}
3376
3377static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
3378{
3379 int ret = 0;
3380
3381 if (!vreg || vreg->enabled)
3382 goto out;
3383
3384 ret = ufshcd_config_vreg(dev, vreg, true);
3385 if (!ret)
3386 ret = regulator_enable(vreg->reg);
3387
3388 if (!ret)
3389 vreg->enabled = true;
3390 else
3391 dev_err(dev, "%s: %s enable failed, err=%d\n",
3392 __func__, vreg->name, ret);
3393out:
3394 return ret;
3395}
3396
3397static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
3398{
3399 int ret = 0;
3400
3401 if (!vreg || !vreg->enabled)
3402 goto out;
3403
3404 ret = regulator_disable(vreg->reg);
3405
3406 if (!ret) {
3407 /* ignore errors on applying disable config */
3408 ufshcd_config_vreg(dev, vreg, false);
3409 vreg->enabled = false;
3410 } else {
3411 dev_err(dev, "%s: %s disable failed, err=%d\n",
3412 __func__, vreg->name, ret);
3413 }
3414out:
3415 return ret;
3416}
3417
3418static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
3419{
3420 int ret = 0;
3421 struct device *dev = hba->dev;
3422 struct ufs_vreg_info *info = &hba->vreg_info;
3423
3424 if (!info)
3425 goto out;
3426
3427 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
3428 if (ret)
3429 goto out;
3430
3431 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
3432 if (ret)
3433 goto out;
3434
3435 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
3436 if (ret)
3437 goto out;
3438
3439out:
3440 if (ret) {
3441 ufshcd_toggle_vreg(dev, info->vccq2, false);
3442 ufshcd_toggle_vreg(dev, info->vccq, false);
3443 ufshcd_toggle_vreg(dev, info->vcc, false);
3444 }
3445 return ret;
3446}
3447
Raviv Shvili6a771a62014-09-25 15:32:24 +03003448static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
3449{
3450 struct ufs_vreg_info *info = &hba->vreg_info;
3451
3452 if (info)
3453 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
3454
3455 return 0;
3456}
3457
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003458static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
3459{
3460 int ret = 0;
3461
3462 if (!vreg)
3463 goto out;
3464
3465 vreg->reg = devm_regulator_get(dev, vreg->name);
3466 if (IS_ERR(vreg->reg)) {
3467 ret = PTR_ERR(vreg->reg);
3468 dev_err(dev, "%s: %s get failed, err=%d\n",
3469 __func__, vreg->name, ret);
3470 }
3471out:
3472 return ret;
3473}
3474
3475static int ufshcd_init_vreg(struct ufs_hba *hba)
3476{
3477 int ret = 0;
3478 struct device *dev = hba->dev;
3479 struct ufs_vreg_info *info = &hba->vreg_info;
3480
3481 if (!info)
3482 goto out;
3483
3484 ret = ufshcd_get_vreg(dev, info->vcc);
3485 if (ret)
3486 goto out;
3487
3488 ret = ufshcd_get_vreg(dev, info->vccq);
3489 if (ret)
3490 goto out;
3491
3492 ret = ufshcd_get_vreg(dev, info->vccq2);
3493out:
3494 return ret;
3495}
3496
Raviv Shvili6a771a62014-09-25 15:32:24 +03003497static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
3498{
3499 struct ufs_vreg_info *info = &hba->vreg_info;
3500
3501 if (info)
3502 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
3503
3504 return 0;
3505}
3506
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03003507static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
3508{
3509 int ret = 0;
3510 struct ufs_clk_info *clki;
3511 struct list_head *head = &hba->clk_list_head;
3512
3513 if (!head || list_empty(head))
3514 goto out;
3515
3516 list_for_each_entry(clki, head, list) {
3517 if (!IS_ERR_OR_NULL(clki->clk)) {
3518 if (on && !clki->enabled) {
3519 ret = clk_prepare_enable(clki->clk);
3520 if (ret) {
3521 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
3522 __func__, clki->name, ret);
3523 goto out;
3524 }
3525 } else if (!on && clki->enabled) {
3526 clk_disable_unprepare(clki->clk);
3527 }
3528 clki->enabled = on;
3529 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
3530 clki->name, on ? "en" : "dis");
3531 }
3532 }
3533out:
3534 if (ret) {
3535 list_for_each_entry(clki, head, list) {
3536 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
3537 clk_disable_unprepare(clki->clk);
3538 }
3539 }
3540 return ret;
3541}
3542
3543static int ufshcd_init_clocks(struct ufs_hba *hba)
3544{
3545 int ret = 0;
3546 struct ufs_clk_info *clki;
3547 struct device *dev = hba->dev;
3548 struct list_head *head = &hba->clk_list_head;
3549
3550 if (!head || list_empty(head))
3551 goto out;
3552
3553 list_for_each_entry(clki, head, list) {
3554 if (!clki->name)
3555 continue;
3556
3557 clki->clk = devm_clk_get(dev, clki->name);
3558 if (IS_ERR(clki->clk)) {
3559 ret = PTR_ERR(clki->clk);
3560 dev_err(dev, "%s: %s clk get failed, %d\n",
3561 __func__, clki->name, ret);
3562 goto out;
3563 }
3564
3565 if (clki->max_freq) {
3566 ret = clk_set_rate(clki->clk, clki->max_freq);
3567 if (ret) {
3568 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
3569 __func__, clki->name,
3570 clki->max_freq, ret);
3571 goto out;
3572 }
3573 }
3574 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
3575 clki->name, clk_get_rate(clki->clk));
3576 }
3577out:
3578 return ret;
3579}
3580
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003581static int ufshcd_variant_hba_init(struct ufs_hba *hba)
3582{
3583 int err = 0;
3584
3585 if (!hba->vops)
3586 goto out;
3587
3588 if (hba->vops->init) {
3589 err = hba->vops->init(hba);
3590 if (err)
3591 goto out;
3592 }
3593
3594 if (hba->vops->setup_clocks) {
3595 err = hba->vops->setup_clocks(hba, true);
3596 if (err)
3597 goto out_exit;
3598 }
3599
3600 if (hba->vops->setup_regulators) {
3601 err = hba->vops->setup_regulators(hba, true);
3602 if (err)
3603 goto out_clks;
3604 }
3605
3606 goto out;
3607
3608out_clks:
3609 if (hba->vops->setup_clocks)
3610 hba->vops->setup_clocks(hba, false);
3611out_exit:
3612 if (hba->vops->exit)
3613 hba->vops->exit(hba);
3614out:
3615 if (err)
3616 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
3617 __func__, hba->vops ? hba->vops->name : "", err);
3618 return err;
3619}
3620
3621static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
3622{
3623 if (!hba->vops)
3624 return;
3625
3626 if (hba->vops->setup_clocks)
3627 hba->vops->setup_clocks(hba, false);
3628
3629 if (hba->vops->setup_regulators)
3630 hba->vops->setup_regulators(hba, false);
3631
3632 if (hba->vops->exit)
3633 hba->vops->exit(hba);
3634}
3635
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003636static int ufshcd_hba_init(struct ufs_hba *hba)
3637{
3638 int err;
3639
Raviv Shvili6a771a62014-09-25 15:32:24 +03003640 /*
3641 * Handle host controller power separately from the UFS device power
3642 * rails as it will help controlling the UFS host controller power
3643 * collapse easily which is different than UFS device power collapse.
3644 * Also, enable the host controller power before we go ahead with rest
3645 * of the initialization here.
3646 */
3647 err = ufshcd_init_hba_vreg(hba);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003648 if (err)
3649 goto out;
3650
Raviv Shvili6a771a62014-09-25 15:32:24 +03003651 err = ufshcd_setup_hba_vreg(hba, true);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003652 if (err)
3653 goto out;
3654
Raviv Shvili6a771a62014-09-25 15:32:24 +03003655 err = ufshcd_init_clocks(hba);
3656 if (err)
3657 goto out_disable_hba_vreg;
3658
3659 err = ufshcd_setup_clocks(hba, true);
3660 if (err)
3661 goto out_disable_hba_vreg;
3662
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03003663 err = ufshcd_init_vreg(hba);
3664 if (err)
3665 goto out_disable_clks;
3666
3667 err = ufshcd_setup_vreg(hba, true);
3668 if (err)
3669 goto out_disable_clks;
3670
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003671 err = ufshcd_variant_hba_init(hba);
3672 if (err)
3673 goto out_disable_vreg;
3674
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003675 hba->is_powered = true;
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003676 goto out;
3677
3678out_disable_vreg:
3679 ufshcd_setup_vreg(hba, false);
Sujit Reddy Thummac6e79da2014-09-25 15:32:23 +03003680out_disable_clks:
3681 ufshcd_setup_clocks(hba, false);
Raviv Shvili6a771a62014-09-25 15:32:24 +03003682out_disable_hba_vreg:
3683 ufshcd_setup_hba_vreg(hba, false);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003684out:
3685 return err;
3686}
3687
3688static void ufshcd_hba_exit(struct ufs_hba *hba)
3689{
Sujit Reddy Thumma1d337ec2014-09-25 15:32:26 +03003690 if (hba->is_powered) {
3691 ufshcd_variant_hba_exit(hba);
3692 ufshcd_setup_vreg(hba, false);
3693 ufshcd_setup_clocks(hba, false);
3694 ufshcd_setup_hba_vreg(hba, false);
3695 hba->is_powered = false;
3696 }
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003697}
3698
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303699/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303700 * ufshcd_suspend - suspend power management function
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303701 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303702 * @state: power state
3703 *
3704 * Returns -ENOSYS
3705 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303706int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303707{
3708 /*
3709 * TODO:
3710 * 1. Block SCSI requests from SCSI midlayer
3711 * 2. Change the internal driver state to non operational
3712 * 3. Set UTRLRSR and UTMRLRSR bits to zero
3713 * 4. Wait until outstanding commands are completed
3714 * 5. Set HCE to zero to send the UFS host controller to reset state
3715 */
3716
3717 return -ENOSYS;
3718}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303719EXPORT_SYMBOL_GPL(ufshcd_suspend);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303720
3721/**
3722 * ufshcd_resume - resume power management function
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303723 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303724 *
3725 * Returns -ENOSYS
3726 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303727int ufshcd_resume(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303728{
3729 /*
3730 * TODO:
3731 * 1. Set HCE to 1, to start the UFS host controller
3732 * initialization process
3733 * 2. Set UTRLRSR and UTMRLRSR bits to 1
3734 * 3. Change the internal driver state to operational
3735 * 4. Unblock SCSI requests from SCSI midlayer
3736 */
3737
3738 return -ENOSYS;
3739}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303740EXPORT_SYMBOL_GPL(ufshcd_resume);
3741
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303742int ufshcd_runtime_suspend(struct ufs_hba *hba)
3743{
3744 if (!hba)
3745 return 0;
3746
3747 /*
3748 * The device is idle with no requests in the queue,
3749 * allow background operations.
3750 */
3751 return ufshcd_enable_auto_bkops(hba);
3752}
3753EXPORT_SYMBOL(ufshcd_runtime_suspend);
3754
3755int ufshcd_runtime_resume(struct ufs_hba *hba)
3756{
3757 if (!hba)
3758 return 0;
3759
3760 return ufshcd_disable_auto_bkops(hba);
3761}
3762EXPORT_SYMBOL(ufshcd_runtime_resume);
3763
3764int ufshcd_runtime_idle(struct ufs_hba *hba)
3765{
3766 return 0;
3767}
3768EXPORT_SYMBOL(ufshcd_runtime_idle);
3769
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303770/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303771 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303772 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303773 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303774 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303775void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303776{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05303777 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303778 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05303779 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303780 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303781
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303782 scsi_host_put(hba->host);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003783
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003784 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303785}
3786EXPORT_SYMBOL_GPL(ufshcd_remove);
3787
3788/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09003789 * ufshcd_set_dma_mask - Set dma mask based on the controller
3790 * addressing capability
3791 * @hba: per adapter instance
3792 *
3793 * Returns 0 for success, non-zero for failure
3794 */
3795static int ufshcd_set_dma_mask(struct ufs_hba *hba)
3796{
3797 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
3798 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
3799 return 0;
3800 }
3801 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
3802}
3803
3804/**
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003805 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303806 * @dev: pointer to device handle
3807 * @hba_handle: driver private handle
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303808 * Returns 0 on success, non-zero value on failure
3809 */
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003810int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303811{
3812 struct Scsi_Host *host;
3813 struct ufs_hba *hba;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003814 int err = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303815
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303816 if (!dev) {
3817 dev_err(dev,
3818 "Invalid memory reference for dev is NULL\n");
3819 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303820 goto out_error;
3821 }
3822
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303823 host = scsi_host_alloc(&ufshcd_driver_template,
3824 sizeof(struct ufs_hba));
3825 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303826 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303827 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303828 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303829 }
3830 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303831 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303832 hba->dev = dev;
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003833 *hba_handle = hba;
3834
3835out_error:
3836 return err;
3837}
3838EXPORT_SYMBOL(ufshcd_alloc_host);
3839
3840/**
3841 * ufshcd_init - Driver initialization routine
3842 * @hba: per-adapter instance
3843 * @mmio_base: base register address
3844 * @irq: Interrupt line of device
3845 * Returns 0 on success, non-zero value on failure
3846 */
3847int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
3848{
3849 int err;
3850 struct Scsi_Host *host = hba->host;
3851 struct device *dev = hba->dev;
3852
3853 if (!mmio_base) {
3854 dev_err(hba->dev,
3855 "Invalid memory reference for mmio_base is NULL\n");
3856 err = -ENODEV;
3857 goto out_error;
3858 }
3859
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303860 hba->mmio_base = mmio_base;
3861 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303862
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003863 err = ufshcd_hba_init(hba);
Sujit Reddy Thumma5c0c28a2014-09-25 15:32:21 +03003864 if (err)
3865 goto out_error;
3866
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303867 /* Read capabilities registers */
3868 ufshcd_hba_capabilities(hba);
3869
3870 /* Get UFS version supported by the controller */
3871 hba->ufs_version = ufshcd_get_ufs_version(hba);
3872
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05303873 /* Get Interrupt bit mask per version */
3874 hba->intr_mask = ufshcd_get_intr_mask(hba);
3875
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09003876 err = ufshcd_set_dma_mask(hba);
3877 if (err) {
3878 dev_err(hba->dev, "set dma mask failed\n");
3879 goto out_disable;
3880 }
3881
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303882 /* Allocate memory for host memory space */
3883 err = ufshcd_memory_alloc(hba);
3884 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303885 dev_err(hba->dev, "Memory allocation failed\n");
3886 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303887 }
3888
3889 /* Configure LRB */
3890 ufshcd_host_memory_configure(hba);
3891
3892 host->can_queue = hba->nutrs;
3893 host->cmd_per_lun = hba->nutrs;
3894 host->max_id = UFSHCD_MAX_ID;
3895 host->max_lun = UFSHCD_MAX_LUNS;
3896 host->max_channel = UFSHCD_MAX_CHANNEL;
3897 host->unique_id = host->host_no;
3898 host->max_cmd_len = MAX_CDB_SIZE;
3899
3900 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303901 init_waitqueue_head(&hba->tm_wq);
3902 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303903
3904 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303905 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303906 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303907
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303908 /* Initialize UIC command mutex */
3909 mutex_init(&hba->uic_cmd_mutex);
3910
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303911 /* Initialize mutex for device management commands */
3912 mutex_init(&hba->dev_cmd.lock);
3913
3914 /* Initialize device management tag acquire wait queue */
3915 init_waitqueue_head(&hba->dev_cmd.tag_wq);
3916
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303917 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003918 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303919 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303920 dev_err(hba->dev, "request irq failed\n");
Seungwon Jeon2953f852013-06-27 13:31:54 +09003921 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303922 }
3923
3924 /* Enable SCSI tag mapping */
3925 err = scsi_init_shared_tag_map(host, host->can_queue);
3926 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303927 dev_err(hba->dev, "init shared queue failed\n");
Seungwon Jeon2953f852013-06-27 13:31:54 +09003928 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303929 }
3930
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303931 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303932 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303933 dev_err(hba->dev, "scsi_add_host failed\n");
Seungwon Jeon2953f852013-06-27 13:31:54 +09003934 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303935 }
3936
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303937 /* Host controller enable */
3938 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303939 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303940 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303941 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303942 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303943
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303944 /* Hold auto suspend until async scan completes */
3945 pm_runtime_get_sync(dev);
3946
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303947 async_schedule(ufshcd_async_scan, hba);
3948
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303949 return 0;
3950
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303951out_remove_scsi_host:
3952 scsi_remove_host(hba->host);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303953out_disable:
3954 scsi_host_put(host);
Sujit Reddy Thummaaa497612014-09-25 15:32:22 +03003955 ufshcd_hba_exit(hba);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303956out_error:
3957 return err;
3958}
3959EXPORT_SYMBOL_GPL(ufshcd_init);
3960
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303961MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
3962MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05303963MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303964MODULE_LICENSE("GPL");
3965MODULE_VERSION(UFSHCD_DRIVER_VERSION);