blob: a450407a56ee0a8f79c3465ef33f9df3b2755a10 [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
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05306 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05307 * Authors:
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053010 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053015 * See the COPYING file in the top-level directory or visit
16 * <http://www.gnu.org/licenses/gpl-2.0.html>
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053017 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +053023 * This program is provided "AS IS" and "WITH ALL FAULTS" and
24 * without warranty of any kind. You are solely responsible for
25 * determining the appropriateness of using and distributing
26 * the program and assume all risks associated with your exercise
27 * of rights with respect to the program, including but not limited
28 * to infringement of third party rights, the risks and costs of
29 * program errors, damage to or loss of data, programs or equipment,
30 * and unavailability or interruption of operations. Under no
31 * circumstances will the contributor of this Program be liable for
32 * any damages of any kind arising from your use or distribution of
33 * this program.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053034 */
35
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053036#include <linux/async.h>
37
Vinayak Holikattie0eca632013-02-25 21:44:33 +053038#include "ufshcd.h"
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053039#include "unipro.h"
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053040
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053041#define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
42 UTP_TASK_REQ_COMPL |\
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +053043 UIC_POWER_MODE |\
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053044 UFSHCD_ERROR_MASK)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +053045/* UIC command timeout, unit: ms */
46#define UIC_CMD_TIMEOUT 500
Seungwon Jeon2fbd0092013-06-26 22:39:27 +053047
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +053048/* NOP OUT retries waiting for NOP IN response */
49#define NOP_OUT_RETRIES 10
50/* Timeout after 30 msecs if NOP OUT hangs without response */
51#define NOP_OUT_TIMEOUT 30 /* msecs */
52
Dolev Raviv68078d52013-07-30 00:35:58 +053053/* Query request retries */
54#define QUERY_REQ_RETRIES 10
55/* Query request timeout */
56#define QUERY_REQ_TIMEOUT 30 /* msec */
57
Sujit Reddy Thummae2933132014-05-26 10:59:12 +053058/* Task management command timeout */
59#define TM_CMD_TIMEOUT 100 /* msecs */
60
Dolev Raviv68078d52013-07-30 00:35:58 +053061/* Expose the flag value from utp_upiu_query.value */
62#define MASK_QUERY_UPIU_FLAG_LOC 0xFF
63
Seungwon Jeon7d568652013-08-31 21:40:20 +053064/* Interrupt aggregation default timeout, unit: 40us */
65#define INT_AGGR_DEF_TO 0x02
66
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053067enum {
68 UFSHCD_MAX_CHANNEL = 0,
69 UFSHCD_MAX_ID = 1,
70 UFSHCD_MAX_LUNS = 8,
71 UFSHCD_CMD_PER_LUN = 32,
72 UFSHCD_CAN_QUEUE = 32,
73};
74
75/* UFSHCD states */
76enum {
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053077 UFSHCD_STATE_RESET,
78 UFSHCD_STATE_ERROR,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +053079 UFSHCD_STATE_OPERATIONAL,
80};
81
82/* UFSHCD error handling flags */
83enum {
84 UFSHCD_EH_IN_PROGRESS = (1 << 0),
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053085};
86
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +053087/* UFSHCD UIC layer error flags */
88enum {
89 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
90 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
91 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
92 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
93};
94
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +053095/* Interrupt configuration options */
96enum {
97 UFSHCD_INT_DISABLE,
98 UFSHCD_INT_ENABLE,
99 UFSHCD_INT_CLEAR,
100};
101
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530102#define ufshcd_set_eh_in_progress(h) \
103 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
104#define ufshcd_eh_in_progress(h) \
105 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
106#define ufshcd_clear_eh_in_progress(h) \
107 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
108
109static void ufshcd_tmc_handler(struct ufs_hba *hba);
110static void ufshcd_async_scan(void *data, async_cookie_t cookie);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +0530111static int ufshcd_reset_and_restore(struct ufs_hba *hba);
112static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
Dolev Raviv1b3e8952014-06-29 09:40:19 +0300113static int ufshcd_read_sdev_qdepth(struct ufs_hba *hba,
114 struct scsi_device *sdev);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530115
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530116/*
117 * ufshcd_wait_for_register - wait for register value to change
118 * @hba - per-adapter interface
119 * @reg - mmio register offset
120 * @mask - mask to apply to read register value
121 * @val - wait condition
122 * @interval_us - polling interval in microsecs
123 * @timeout_ms - timeout in millisecs
124 *
125 * Returns -ETIMEDOUT on error, zero on success
126 */
127static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
128 u32 val, unsigned long interval_us, unsigned long timeout_ms)
129{
130 int err = 0;
131 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
132
133 /* ignore bits that we don't intend to wait on */
134 val = val & mask;
135
136 while ((ufshcd_readl(hba, reg) & mask) != val) {
137 /* wakeup within 50us of expiry */
138 usleep_range(interval_us, interval_us + 50);
139
140 if (time_after(jiffies, timeout)) {
141 if ((ufshcd_readl(hba, reg) & mask) != val)
142 err = -ETIMEDOUT;
143 break;
144 }
145 }
146
147 return err;
148}
149
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530150/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530151 * ufshcd_get_intr_mask - Get the interrupt bit mask
152 * @hba - Pointer to adapter instance
153 *
154 * Returns interrupt bit mask per version
155 */
156static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
157{
158 if (hba->ufs_version == UFSHCI_VERSION_10)
159 return INTERRUPT_MASK_ALL_VER_10;
160 else
161 return INTERRUPT_MASK_ALL_VER_11;
162}
163
164/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530165 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
166 * @hba - Pointer to adapter instance
167 *
168 * Returns UFSHCI version supported by the controller
169 */
170static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
171{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530172 return ufshcd_readl(hba, REG_UFS_VERSION);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530173}
174
175/**
176 * ufshcd_is_device_present - Check if any device connected to
177 * the host controller
178 * @reg_hcs - host controller status register value
179 *
Venkatraman S73ec5132012-07-10 19:39:23 +0530180 * Returns 1 if device present, 0 if no device detected
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530181 */
182static inline int ufshcd_is_device_present(u32 reg_hcs)
183{
Venkatraman S73ec5132012-07-10 19:39:23 +0530184 return (DEVICE_PRESENT & reg_hcs) ? 1 : 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530185}
186
187/**
188 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
189 * @lrb: pointer to local command reference block
190 *
191 * This function is used to get the OCS field from UTRD
192 * Returns the OCS field in the UTRD
193 */
194static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
195{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530196 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530197}
198
199/**
200 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
201 * @task_req_descp: pointer to utp_task_req_desc structure
202 *
203 * This function is used to get the OCS field from UTMRD
204 * Returns the OCS field in the UTMRD
205 */
206static inline int
207ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
208{
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530209 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530210}
211
212/**
213 * ufshcd_get_tm_free_slot - get a free slot for task management request
214 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530215 * @free_slot: pointer to variable with available slot value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530216 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530217 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
218 * Returns 0 if free slot is not available, else return 1 with tag value
219 * in @free_slot.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530220 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530221static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530222{
Sujit Reddy Thummae2933132014-05-26 10:59:12 +0530223 int tag;
224 bool ret = false;
225
226 if (!free_slot)
227 goto out;
228
229 do {
230 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
231 if (tag >= hba->nutmrs)
232 goto out;
233 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
234
235 *free_slot = tag;
236 ret = true;
237out:
238 return ret;
239}
240
241static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
242{
243 clear_bit_unlock(slot, &hba->tm_slots_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530244}
245
246/**
247 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
248 * @hba: per adapter instance
249 * @pos: position of the bit to be cleared
250 */
251static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
252{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530253 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530254}
255
256/**
257 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
258 * @reg: Register value of host controller status
259 *
260 * Returns integer, 0 on Success and positive value if failed
261 */
262static inline int ufshcd_get_lists_status(u32 reg)
263{
264 /*
265 * The mask 0xFF is for the following HCS register bits
266 * Bit Description
267 * 0 Device Present
268 * 1 UTRLRDY
269 * 2 UTMRLRDY
270 * 3 UCRDY
271 * 4 HEI
272 * 5 DEI
273 * 6-7 reserved
274 */
275 return (((reg) & (0xFF)) >> 1) ^ (0x07);
276}
277
278/**
279 * ufshcd_get_uic_cmd_result - Get the UIC command result
280 * @hba: Pointer to adapter instance
281 *
282 * This function gets the result of UIC command completion
283 * Returns 0 on success, non zero value on error
284 */
285static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
286{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530287 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530288 MASK_UIC_COMMAND_RESULT;
289}
290
291/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +0530292 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
293 * @hba: Pointer to adapter instance
294 *
295 * This function gets UIC command argument3
296 * Returns 0 on success, non zero value on error
297 */
298static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
299{
300 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
301}
302
303/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530304 * ufshcd_get_req_rsp - returns the TR response transaction type
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530305 * @ucd_rsp_ptr: pointer to response UPIU
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530306 */
307static inline int
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530308ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530309{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530310 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530311}
312
313/**
314 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
315 * @ucd_rsp_ptr: pointer to response UPIU
316 *
317 * This function gets the response status and scsi_status from response UPIU
318 * Returns the response result code.
319 */
320static inline int
321ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
322{
323 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
324}
325
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530326/*
327 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
328 * from response UPIU
329 * @ucd_rsp_ptr: pointer to response UPIU
330 *
331 * Return the data segment length.
332 */
333static inline unsigned int
334ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
335{
336 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
337 MASK_RSP_UPIU_DATA_SEG_LEN;
338}
339
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530340/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +0530341 * ufshcd_is_exception_event - Check if the device raised an exception event
342 * @ucd_rsp_ptr: pointer to response UPIU
343 *
344 * The function checks if the device raised an exception event indicated in
345 * the Device Information field of response UPIU.
346 *
347 * Returns true if exception is raised, false otherwise.
348 */
349static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
350{
351 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
352 MASK_RSP_EXCEPTION_EVENT ? true : false;
353}
354
355/**
Seungwon Jeon7d568652013-08-31 21:40:20 +0530356 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530357 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530358 */
359static inline void
Seungwon Jeon7d568652013-08-31 21:40:20 +0530360ufshcd_reset_intr_aggr(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530361{
Seungwon Jeon7d568652013-08-31 21:40:20 +0530362 ufshcd_writel(hba, INT_AGGR_ENABLE |
363 INT_AGGR_COUNTER_AND_TIMER_RESET,
364 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
365}
366
367/**
368 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
369 * @hba: per adapter instance
370 * @cnt: Interrupt aggregation counter threshold
371 * @tmout: Interrupt aggregation timeout value
372 */
373static inline void
374ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
375{
376 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
377 INT_AGGR_COUNTER_THLD_VAL(cnt) |
378 INT_AGGR_TIMEOUT_VAL(tmout),
379 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530380}
381
382/**
383 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
384 * When run-stop registers are set to 1, it indicates the
385 * host controller that it can process the requests
386 * @hba: per adapter instance
387 */
388static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
389{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530390 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
391 REG_UTP_TASK_REQ_LIST_RUN_STOP);
392 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
393 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530394}
395
396/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530397 * ufshcd_hba_start - Start controller initialization sequence
398 * @hba: per adapter instance
399 */
400static inline void ufshcd_hba_start(struct ufs_hba *hba)
401{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530402 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530403}
404
405/**
406 * ufshcd_is_hba_active - Get controller state
407 * @hba: per adapter instance
408 *
409 * Returns zero if controller is active, 1 otherwise
410 */
411static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
412{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530413 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530414}
415
416/**
417 * ufshcd_send_command - Send SCSI or device management commands
418 * @hba: per adapter instance
419 * @task_tag: Task tag of the command
420 */
421static inline
422void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
423{
424 __set_bit(task_tag, &hba->outstanding_reqs);
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530425 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530426}
427
428/**
429 * ufshcd_copy_sense_data - Copy sense data in case of check condition
430 * @lrb - pointer to local reference block
431 */
432static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
433{
434 int len;
Seungwon Jeon1c2623c2013-08-31 21:40:19 +0530435 if (lrbp->sense_buffer &&
436 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530437 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530438 memcpy(lrbp->sense_buffer,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530439 lrbp->ucd_rsp_ptr->sr.sense_data,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530440 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
441 }
442}
443
444/**
Dolev Raviv68078d52013-07-30 00:35:58 +0530445 * ufshcd_copy_query_response() - Copy the Query Response and the data
446 * descriptor
447 * @hba: per adapter instance
448 * @lrb - pointer to local reference block
449 */
450static
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300451int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Dolev Raviv68078d52013-07-30 00:35:58 +0530452{
453 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
454
Dolev Raviv68078d52013-07-30 00:35:58 +0530455 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530456
Dolev Raviv68078d52013-07-30 00:35:58 +0530457 /* Get the descriptor */
458 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
Dolev Ravivd44a5f92014-06-29 09:40:17 +0300459 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
Dolev Raviv68078d52013-07-30 00:35:58 +0530460 GENERAL_UPIU_REQUEST_SIZE;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300461 u16 resp_len;
462 u16 buf_len;
Dolev Raviv68078d52013-07-30 00:35:58 +0530463
464 /* data segment length */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300465 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
Dolev Raviv68078d52013-07-30 00:35:58 +0530466 MASK_QUERY_DATA_SEG_LEN;
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300467 buf_len = hba->dev_cmd.query.request.upiu_req.length;
468 if (likely(buf_len >= resp_len)) {
469 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
470 } else {
471 dev_warn(hba->dev,
472 "%s: Response size is bigger than buffer",
473 __func__);
474 return -EINVAL;
475 }
Dolev Raviv68078d52013-07-30 00:35:58 +0530476 }
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300477
478 return 0;
Dolev Raviv68078d52013-07-30 00:35:58 +0530479}
480
481/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530482 * ufshcd_hba_capabilities - Read controller capabilities
483 * @hba: per adapter instance
484 */
485static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
486{
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530487 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530488
489 /* nutrs and nutmrs are 0 based values */
490 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
491 hba->nutmrs =
492 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
493}
494
495/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530496 * ufshcd_ready_for_uic_cmd - Check if controller is ready
497 * to accept UIC commands
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530498 * @hba: per adapter instance
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530499 * Return true on success, else false
500 */
501static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
502{
503 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
504 return true;
505 else
506 return false;
507}
508
509/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +0530510 * ufshcd_get_upmcrs - Get the power mode change request status
511 * @hba: Pointer to adapter instance
512 *
513 * This function gets the UPMCRS field of HCS register
514 * Returns value of UPMCRS field
515 */
516static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
517{
518 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
519}
520
521/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530522 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
523 * @hba: per adapter instance
524 * @uic_cmd: UIC command
525 *
526 * Mutex must be held.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530527 */
528static inline void
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530529ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530530{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530531 WARN_ON(hba->active_uic_cmd);
532
533 hba->active_uic_cmd = uic_cmd;
534
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530535 /* Write Args */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530536 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
537 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
538 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530539
540 /* Write UIC Cmd */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530541 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
Seungwon Jeonb873a2752013-06-26 22:39:26 +0530542 REG_UIC_COMMAND);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530543}
544
545/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +0530546 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
547 * @hba: per adapter instance
548 * @uic_command: UIC command
549 *
550 * Must be called with mutex held.
551 * Returns 0 only if success.
552 */
553static int
554ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
555{
556 int ret;
557 unsigned long flags;
558
559 if (wait_for_completion_timeout(&uic_cmd->done,
560 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
561 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
562 else
563 ret = -ETIMEDOUT;
564
565 spin_lock_irqsave(hba->host->host_lock, flags);
566 hba->active_uic_cmd = NULL;
567 spin_unlock_irqrestore(hba->host->host_lock, flags);
568
569 return ret;
570}
571
572/**
573 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
574 * @hba: per adapter instance
575 * @uic_cmd: UIC command
576 *
577 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
578 * with mutex held.
579 * Returns 0 only if success.
580 */
581static int
582__ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
583{
584 int ret;
585 unsigned long flags;
586
587 if (!ufshcd_ready_for_uic_cmd(hba)) {
588 dev_err(hba->dev,
589 "Controller not ready to accept UIC commands\n");
590 return -EIO;
591 }
592
593 init_completion(&uic_cmd->done);
594
595 spin_lock_irqsave(hba->host->host_lock, flags);
596 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
597 spin_unlock_irqrestore(hba->host->host_lock, flags);
598
599 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
600
601 return ret;
602}
603
604/**
605 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
606 * @hba: per adapter instance
607 * @uic_cmd: UIC command
608 *
609 * Returns 0 only if success.
610 */
611static int
612ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
613{
614 int ret;
615
616 mutex_lock(&hba->uic_cmd_mutex);
617 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
618 mutex_unlock(&hba->uic_cmd_mutex);
619
620 return ret;
621}
622
623/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530624 * ufshcd_map_sg - Map scatter-gather list to prdt
625 * @lrbp - pointer to local reference block
626 *
627 * Returns 0 in case of success, non-zero value in case of failure
628 */
629static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
630{
631 struct ufshcd_sg_entry *prd_table;
632 struct scatterlist *sg;
633 struct scsi_cmnd *cmd;
634 int sg_segments;
635 int i;
636
637 cmd = lrbp->cmd;
638 sg_segments = scsi_dma_map(cmd);
639 if (sg_segments < 0)
640 return sg_segments;
641
642 if (sg_segments) {
643 lrbp->utr_descriptor_ptr->prd_table_length =
644 cpu_to_le16((u16) (sg_segments));
645
646 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
647
648 scsi_for_each_sg(cmd, sg, sg_segments, i) {
649 prd_table[i].size =
650 cpu_to_le32(((u32) sg_dma_len(sg))-1);
651 prd_table[i].base_addr =
652 cpu_to_le32(lower_32_bits(sg->dma_address));
653 prd_table[i].upper_addr =
654 cpu_to_le32(upper_32_bits(sg->dma_address));
655 }
656 } else {
657 lrbp->utr_descriptor_ptr->prd_table_length = 0;
658 }
659
660 return 0;
661}
662
663/**
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530664 * ufshcd_enable_intr - enable interrupts
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530665 * @hba: per adapter instance
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530666 * @intrs: interrupt bits
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530667 */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530668static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530669{
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530670 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
671
672 if (hba->ufs_version == UFSHCI_VERSION_10) {
673 u32 rw;
674 rw = set & INTERRUPT_MASK_RW_VER_10;
675 set = rw | ((set ^ intrs) & intrs);
676 } else {
677 set |= intrs;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530678 }
Seungwon Jeon2fbd0092013-06-26 22:39:27 +0530679
680 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
681}
682
683/**
684 * ufshcd_disable_intr - disable interrupts
685 * @hba: per adapter instance
686 * @intrs: interrupt bits
687 */
688static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
689{
690 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
691
692 if (hba->ufs_version == UFSHCI_VERSION_10) {
693 u32 rw;
694 rw = (set & INTERRUPT_MASK_RW_VER_10) &
695 ~(intrs & INTERRUPT_MASK_RW_VER_10);
696 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
697
698 } else {
699 set &= ~intrs;
700 }
701
702 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530703}
704
705/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530706 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
707 * descriptor according to request
708 * @lrbp: pointer to local reference block
709 * @upiu_flags: flags required in the header
710 * @cmd_dir: requests data direction
711 */
712static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
713 u32 *upiu_flags, enum dma_data_direction cmd_dir)
714{
715 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
716 u32 data_direction;
717 u32 dword_0;
718
719 if (cmd_dir == DMA_FROM_DEVICE) {
720 data_direction = UTP_DEVICE_TO_HOST;
721 *upiu_flags = UPIU_CMD_FLAGS_READ;
722 } else if (cmd_dir == DMA_TO_DEVICE) {
723 data_direction = UTP_HOST_TO_DEVICE;
724 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
725 } else {
726 data_direction = UTP_NO_DATA_TRANSFER;
727 *upiu_flags = UPIU_CMD_FLAGS_NONE;
728 }
729
730 dword_0 = data_direction | (lrbp->command_type
731 << UPIU_COMMAND_TYPE_OFFSET);
732 if (lrbp->intr_cmd)
733 dword_0 |= UTP_REQ_DESC_INT_CMD;
734
735 /* Transfer request descriptor header fields */
736 req_desc->header.dword_0 = cpu_to_le32(dword_0);
737
738 /*
739 * assigning invalid value for command status. Controller
740 * updates OCS on command completion, with the command
741 * status
742 */
743 req_desc->header.dword_2 =
744 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
745}
746
747/**
748 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
749 * for scsi commands
750 * @lrbp - local reference block pointer
751 * @upiu_flags - flags
752 */
753static
754void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
755{
756 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
757
758 /* command descriptor fields */
759 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
760 UPIU_TRANSACTION_COMMAND, upiu_flags,
761 lrbp->lun, lrbp->task_tag);
762 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
763 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
764
765 /* Total EHS length and Data segment length will be zero */
766 ucd_req_ptr->header.dword_2 = 0;
767
768 ucd_req_ptr->sc.exp_data_transfer_len =
769 cpu_to_be32(lrbp->cmd->sdb.length);
770
771 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd,
772 (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
773}
774
Dolev Raviv68078d52013-07-30 00:35:58 +0530775/**
776 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
777 * for query requsts
778 * @hba: UFS hba
779 * @lrbp: local reference block pointer
780 * @upiu_flags: flags
781 */
782static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
783 struct ufshcd_lrb *lrbp, u32 upiu_flags)
784{
785 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
786 struct ufs_query *query = &hba->dev_cmd.query;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +0530787 u16 len = be16_to_cpu(query->request.upiu_req.length);
Dolev Raviv68078d52013-07-30 00:35:58 +0530788 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
789
790 /* Query request header */
791 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
792 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
793 lrbp->lun, lrbp->task_tag);
794 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
795 0, query->request.query_func, 0, 0);
796
797 /* Data segment length */
798 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
799 0, 0, len >> 8, (u8)len);
800
801 /* Copy the Query Request buffer as is */
802 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
803 QUERY_OSF_SIZE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530804
805 /* Copy the Descriptor */
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300806 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
807 memcpy(descp, query->descriptor, len);
808
Dolev Raviv68078d52013-07-30 00:35:58 +0530809}
810
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530811static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
812{
813 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
814
815 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
816
817 /* command descriptor fields */
818 ucd_req_ptr->header.dword_0 =
819 UPIU_HEADER_DWORD(
820 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
821}
822
823/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530824 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530825 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530826 * @lrb - pointer to local reference block
827 */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530828static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530829{
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530830 u32 upiu_flags;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530831 int ret = 0;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530832
833 switch (lrbp->command_type) {
834 case UTP_CMD_TYPE_SCSI:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530835 if (likely(lrbp->cmd)) {
836 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
837 lrbp->cmd->sc_data_direction);
838 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530839 } else {
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530840 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530841 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530842 break;
843 case UTP_CMD_TYPE_DEV_MANAGE:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530844 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
Dolev Raviv68078d52013-07-30 00:35:58 +0530845 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
846 ufshcd_prepare_utp_query_req_upiu(
847 hba, lrbp, upiu_flags);
848 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530849 ufshcd_prepare_utp_nop_upiu(lrbp);
850 else
851 ret = -EINVAL;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530852 break;
853 case UTP_CMD_TYPE_UFS:
854 /* For UFS native command implementation */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530855 ret = -ENOTSUPP;
856 dev_err(hba->dev, "%s: UFS native command are not supported\n",
857 __func__);
858 break;
859 default:
860 ret = -ENOTSUPP;
861 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
862 __func__, lrbp->command_type);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530863 break;
864 } /* end of switch */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530865
866 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530867}
868
869/**
870 * ufshcd_queuecommand - main entry point for SCSI requests
871 * @cmd: command from SCSI Midlayer
872 * @done: call back function
873 *
874 * Returns 0 for success, non-zero in case of failure
875 */
876static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
877{
878 struct ufshcd_lrb *lrbp;
879 struct ufs_hba *hba;
880 unsigned long flags;
881 int tag;
882 int err = 0;
883
884 hba = shost_priv(host);
885
886 tag = cmd->request->tag;
887
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530888 spin_lock_irqsave(hba->host->host_lock, flags);
889 switch (hba->ufshcd_state) {
890 case UFSHCD_STATE_OPERATIONAL:
891 break;
892 case UFSHCD_STATE_RESET:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530893 err = SCSI_MLQUEUE_HOST_BUSY;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530894 goto out_unlock;
895 case UFSHCD_STATE_ERROR:
896 set_host_byte(cmd, DID_ERROR);
897 cmd->scsi_done(cmd);
898 goto out_unlock;
899 default:
900 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
901 __func__, hba->ufshcd_state);
902 set_host_byte(cmd, DID_BAD_TARGET);
903 cmd->scsi_done(cmd);
904 goto out_unlock;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530905 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530906 spin_unlock_irqrestore(hba->host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530907
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530908 /* acquire the tag to make sure device cmds don't use it */
909 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
910 /*
911 * Dev manage command in progress, requeue the command.
912 * Requeuing the command helps in cases where the request *may*
913 * find different tag instead of waiting for dev manage command
914 * completion.
915 */
916 err = SCSI_MLQUEUE_HOST_BUSY;
917 goto out;
918 }
919
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530920 lrbp = &hba->lrb[tag];
921
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530922 WARN_ON(lrbp->cmd);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530923 lrbp->cmd = cmd;
924 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
925 lrbp->sense_buffer = cmd->sense_buffer;
926 lrbp->task_tag = tag;
927 lrbp->lun = cmd->device->lun;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530928 lrbp->intr_cmd = false;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530929 lrbp->command_type = UTP_CMD_TYPE_SCSI;
930
931 /* form UPIU before issuing the command */
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530932 ufshcd_compose_upiu(hba, lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530933 err = ufshcd_map_sg(lrbp);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530934 if (err) {
935 lrbp->cmd = NULL;
936 clear_bit_unlock(tag, &hba->lrb_in_use);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530937 goto out;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530938 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530939
940 /* issue command to the controller */
941 spin_lock_irqsave(hba->host->host_lock, flags);
942 ufshcd_send_command(hba, tag);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +0530943out_unlock:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +0530944 spin_unlock_irqrestore(hba->host->host_lock, flags);
945out:
946 return err;
947}
948
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530949static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
950 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
951{
952 lrbp->cmd = NULL;
953 lrbp->sense_bufflen = 0;
954 lrbp->sense_buffer = NULL;
955 lrbp->task_tag = tag;
956 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
957 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
958 lrbp->intr_cmd = true; /* No interrupt aggregation */
959 hba->dev_cmd.type = cmd_type;
960
961 return ufshcd_compose_upiu(hba, lrbp);
962}
963
964static int
965ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
966{
967 int err = 0;
968 unsigned long flags;
969 u32 mask = 1 << tag;
970
971 /* clear outstanding transaction before retry */
972 spin_lock_irqsave(hba->host->host_lock, flags);
973 ufshcd_utrl_clear(hba, tag);
974 spin_unlock_irqrestore(hba->host->host_lock, flags);
975
976 /*
977 * wait for for h/w to clear corresponding bit in door-bell.
978 * max. wait is 1 sec.
979 */
980 err = ufshcd_wait_for_register(hba,
981 REG_UTP_TRANSFER_REQ_DOOR_BELL,
982 mask, ~mask, 1000, 1000);
983
984 return err;
985}
986
Dolev Ravivc6d4a832014-06-29 09:40:18 +0300987static int
988ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
989{
990 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
991
992 /* Get the UPIU response */
993 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
994 UPIU_RSP_CODE_OFFSET;
995 return query_res->response;
996}
997
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +0530998/**
999 * ufshcd_dev_cmd_completion() - handles device management command responses
1000 * @hba: per adapter instance
1001 * @lrbp: pointer to local reference block
1002 */
1003static int
1004ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1005{
1006 int resp;
1007 int err = 0;
1008
1009 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1010
1011 switch (resp) {
1012 case UPIU_TRANSACTION_NOP_IN:
1013 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1014 err = -EINVAL;
1015 dev_err(hba->dev, "%s: unexpected response %x\n",
1016 __func__, resp);
1017 }
1018 break;
Dolev Raviv68078d52013-07-30 00:35:58 +05301019 case UPIU_TRANSACTION_QUERY_RSP:
Dolev Ravivc6d4a832014-06-29 09:40:18 +03001020 err = ufshcd_check_query_response(hba, lrbp);
1021 if (!err)
1022 err = ufshcd_copy_query_response(hba, lrbp);
Dolev Raviv68078d52013-07-30 00:35:58 +05301023 break;
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301024 case UPIU_TRANSACTION_REJECT_UPIU:
1025 /* TODO: handle Reject UPIU Response */
1026 err = -EPERM;
1027 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1028 __func__);
1029 break;
1030 default:
1031 err = -EINVAL;
1032 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1033 __func__, resp);
1034 break;
1035 }
1036
1037 return err;
1038}
1039
1040static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1041 struct ufshcd_lrb *lrbp, int max_timeout)
1042{
1043 int err = 0;
1044 unsigned long time_left;
1045 unsigned long flags;
1046
1047 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1048 msecs_to_jiffies(max_timeout));
1049
1050 spin_lock_irqsave(hba->host->host_lock, flags);
1051 hba->dev_cmd.complete = NULL;
1052 if (likely(time_left)) {
1053 err = ufshcd_get_tr_ocs(lrbp);
1054 if (!err)
1055 err = ufshcd_dev_cmd_completion(hba, lrbp);
1056 }
1057 spin_unlock_irqrestore(hba->host->host_lock, flags);
1058
1059 if (!time_left) {
1060 err = -ETIMEDOUT;
1061 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
1062 /* sucessfully cleared the command, retry if needed */
1063 err = -EAGAIN;
1064 }
1065
1066 return err;
1067}
1068
1069/**
1070 * ufshcd_get_dev_cmd_tag - Get device management command tag
1071 * @hba: per-adapter instance
1072 * @tag: pointer to variable with available slot value
1073 *
1074 * Get a free slot and lock it until device management command
1075 * completes.
1076 *
1077 * Returns false if free slot is unavailable for locking, else
1078 * return true with tag value in @tag.
1079 */
1080static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1081{
1082 int tag;
1083 bool ret = false;
1084 unsigned long tmp;
1085
1086 if (!tag_out)
1087 goto out;
1088
1089 do {
1090 tmp = ~hba->lrb_in_use;
1091 tag = find_last_bit(&tmp, hba->nutrs);
1092 if (tag >= hba->nutrs)
1093 goto out;
1094 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1095
1096 *tag_out = tag;
1097 ret = true;
1098out:
1099 return ret;
1100}
1101
1102static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1103{
1104 clear_bit_unlock(tag, &hba->lrb_in_use);
1105}
1106
1107/**
1108 * ufshcd_exec_dev_cmd - API for sending device management requests
1109 * @hba - UFS hba
1110 * @cmd_type - specifies the type (NOP, Query...)
1111 * @timeout - time in seconds
1112 *
Dolev Raviv68078d52013-07-30 00:35:58 +05301113 * NOTE: Since there is only one available tag for device management commands,
1114 * it is expected you hold the hba->dev_cmd.lock mutex.
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301115 */
1116static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1117 enum dev_cmd_type cmd_type, int timeout)
1118{
1119 struct ufshcd_lrb *lrbp;
1120 int err;
1121 int tag;
1122 struct completion wait;
1123 unsigned long flags;
1124
1125 /*
1126 * Get free slot, sleep if slots are unavailable.
1127 * Even though we use wait_event() which sleeps indefinitely,
1128 * the maximum wait time is bounded by SCSI request timeout.
1129 */
1130 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1131
1132 init_completion(&wait);
1133 lrbp = &hba->lrb[tag];
1134 WARN_ON(lrbp->cmd);
1135 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1136 if (unlikely(err))
1137 goto out_put_tag;
1138
1139 hba->dev_cmd.complete = &wait;
1140
1141 spin_lock_irqsave(hba->host->host_lock, flags);
1142 ufshcd_send_command(hba, tag);
1143 spin_unlock_irqrestore(hba->host->host_lock, flags);
1144
1145 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1146
1147out_put_tag:
1148 ufshcd_put_dev_cmd_tag(hba, tag);
1149 wake_up(&hba->dev_cmd.tag_wq);
1150 return err;
1151}
1152
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301153/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001154 * ufshcd_init_query() - init the query response and request parameters
1155 * @hba: per-adapter instance
1156 * @request: address of the request pointer to be initialized
1157 * @response: address of the response pointer to be initialized
1158 * @opcode: operation to perform
1159 * @idn: flag idn to access
1160 * @index: LU number to access
1161 * @selector: query/flag/descriptor further identification
1162 */
1163static inline void ufshcd_init_query(struct ufs_hba *hba,
1164 struct ufs_query_req **request, struct ufs_query_res **response,
1165 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1166{
1167 *request = &hba->dev_cmd.query.request;
1168 *response = &hba->dev_cmd.query.response;
1169 memset(*request, 0, sizeof(struct ufs_query_req));
1170 memset(*response, 0, sizeof(struct ufs_query_res));
1171 (*request)->upiu_req.opcode = opcode;
1172 (*request)->upiu_req.idn = idn;
1173 (*request)->upiu_req.index = index;
1174 (*request)->upiu_req.selector = selector;
1175}
1176
1177/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301178 * ufshcd_query_flag() - API function for sending flag query requests
1179 * hba: per-adapter instance
1180 * query_opcode: flag query to perform
1181 * idn: flag idn to access
1182 * flag_res: the flag value after the query request completes
1183 *
1184 * Returns 0 for success, non-zero in case of failure
1185 */
1186static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1187 enum flag_idn idn, bool *flag_res)
1188{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001189 struct ufs_query_req *request = NULL;
1190 struct ufs_query_res *response = NULL;
1191 int err, index = 0, selector = 0;
Dolev Raviv68078d52013-07-30 00:35:58 +05301192
1193 BUG_ON(!hba);
1194
1195 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001196 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1197 selector);
Dolev Raviv68078d52013-07-30 00:35:58 +05301198
1199 switch (opcode) {
1200 case UPIU_QUERY_OPCODE_SET_FLAG:
1201 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1202 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1203 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1204 break;
1205 case UPIU_QUERY_OPCODE_READ_FLAG:
1206 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1207 if (!flag_res) {
1208 /* No dummy reads */
1209 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1210 __func__);
1211 err = -EINVAL;
1212 goto out_unlock;
1213 }
1214 break;
1215 default:
1216 dev_err(hba->dev,
1217 "%s: Expected query flag opcode but got = %d\n",
1218 __func__, opcode);
1219 err = -EINVAL;
1220 goto out_unlock;
1221 }
Dolev Raviv68078d52013-07-30 00:35:58 +05301222
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001223 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Dolev Raviv68078d52013-07-30 00:35:58 +05301224
1225 if (err) {
1226 dev_err(hba->dev,
1227 "%s: Sending flag query for idn %d failed, err = %d\n",
1228 __func__, idn, err);
1229 goto out_unlock;
1230 }
1231
1232 if (flag_res)
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301233 *flag_res = (be32_to_cpu(response->upiu_res.value) &
Dolev Raviv68078d52013-07-30 00:35:58 +05301234 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1235
1236out_unlock:
1237 mutex_unlock(&hba->dev_cmd.lock);
1238 return err;
1239}
1240
1241/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301242 * ufshcd_query_attr - API function for sending attribute requests
1243 * hba: per-adapter instance
1244 * opcode: attribute opcode
1245 * idn: attribute idn to access
1246 * index: index field
1247 * selector: selector field
1248 * attr_val: the attribute value after the query request completes
1249 *
1250 * Returns 0 for success, non-zero in case of failure
1251*/
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301252static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301253 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1254{
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001255 struct ufs_query_req *request = NULL;
1256 struct ufs_query_res *response = NULL;
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301257 int err;
1258
1259 BUG_ON(!hba);
1260
1261 if (!attr_val) {
1262 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1263 __func__, opcode);
1264 err = -EINVAL;
1265 goto out;
1266 }
1267
1268 mutex_lock(&hba->dev_cmd.lock);
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001269 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1270 selector);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301271
1272 switch (opcode) {
1273 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1274 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301275 request->upiu_req.value = cpu_to_be32(*attr_val);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301276 break;
1277 case UPIU_QUERY_OPCODE_READ_ATTR:
1278 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1279 break;
1280 default:
1281 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1282 __func__, opcode);
1283 err = -EINVAL;
1284 goto out_unlock;
1285 }
1286
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001287 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301288
1289 if (err) {
1290 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1291 __func__, opcode, idn, err);
1292 goto out_unlock;
1293 }
1294
Sujit Reddy Thummae8c8e822014-05-26 10:59:10 +05301295 *attr_val = be32_to_cpu(response->upiu_res.value);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05301296
1297out_unlock:
1298 mutex_unlock(&hba->dev_cmd.lock);
1299out:
1300 return err;
1301}
1302
1303/**
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001304 * ufshcd_query_descriptor - API function for sending descriptor requests
1305 * hba: per-adapter instance
1306 * opcode: attribute opcode
1307 * idn: attribute idn to access
1308 * index: index field
1309 * selector: selector field
1310 * desc_buf: the buffer that contains the descriptor
1311 * buf_len: length parameter passed to the device
1312 *
1313 * Returns 0 for success, non-zero in case of failure.
1314 * The buf_len parameter will contain, on return, the length parameter
1315 * received on the response.
1316 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03001317static int ufshcd_query_descriptor(struct ufs_hba *hba,
Dolev Ravivd44a5f92014-06-29 09:40:17 +03001318 enum query_opcode opcode, enum desc_idn idn, u8 index,
1319 u8 selector, u8 *desc_buf, int *buf_len)
1320{
1321 struct ufs_query_req *request = NULL;
1322 struct ufs_query_res *response = NULL;
1323 int err;
1324
1325 BUG_ON(!hba);
1326
1327 if (!desc_buf) {
1328 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1329 __func__, opcode);
1330 err = -EINVAL;
1331 goto out;
1332 }
1333
1334 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1335 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1336 __func__, *buf_len);
1337 err = -EINVAL;
1338 goto out;
1339 }
1340
1341 mutex_lock(&hba->dev_cmd.lock);
1342 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1343 selector);
1344 hba->dev_cmd.query.descriptor = desc_buf;
1345 request->upiu_req.length = *buf_len;
1346
1347 switch (opcode) {
1348 case UPIU_QUERY_OPCODE_WRITE_DESC:
1349 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1350 break;
1351 case UPIU_QUERY_OPCODE_READ_DESC:
1352 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1353 break;
1354 default:
1355 dev_err(hba->dev,
1356 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1357 __func__, opcode);
1358 err = -EINVAL;
1359 goto out_unlock;
1360 }
1361
1362 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1363
1364 if (err) {
1365 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1366 __func__, opcode, idn, err);
1367 goto out_unlock;
1368 }
1369
1370 hba->dev_cmd.query.descriptor = NULL;
1371 *buf_len = response->upiu_res.length;
1372
1373out_unlock:
1374 mutex_unlock(&hba->dev_cmd.lock);
1375out:
1376 return err;
1377}
1378
1379/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301380 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1381 * @hba: per adapter instance
1382 *
1383 * 1. Allocate DMA memory for Command Descriptor array
1384 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1385 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1386 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1387 * (UTMRDL)
1388 * 4. Allocate memory for local reference block(lrb).
1389 *
1390 * Returns 0 for success, non-zero in case of failure
1391 */
1392static int ufshcd_memory_alloc(struct ufs_hba *hba)
1393{
1394 size_t utmrdl_size, utrdl_size, ucdl_size;
1395
1396 /* Allocate memory for UTP command descriptors */
1397 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001398 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
1399 ucdl_size,
1400 &hba->ucdl_dma_addr,
1401 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301402
1403 /*
1404 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1405 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1406 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1407 * be aligned to 128 bytes as well
1408 */
1409 if (!hba->ucdl_base_addr ||
1410 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301411 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301412 "Command Descriptor Memory allocation failed\n");
1413 goto out;
1414 }
1415
1416 /*
1417 * Allocate memory for UTP Transfer descriptors
1418 * UFSHCI requires 1024 byte alignment of UTRD
1419 */
1420 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
Seungwon Jeon2953f852013-06-27 13:31:54 +09001421 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
1422 utrdl_size,
1423 &hba->utrdl_dma_addr,
1424 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301425 if (!hba->utrdl_base_addr ||
1426 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301427 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301428 "Transfer Descriptor Memory allocation failed\n");
1429 goto out;
1430 }
1431
1432 /*
1433 * Allocate memory for UTP Task Management descriptors
1434 * UFSHCI requires 1024 byte alignment of UTMRD
1435 */
1436 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
Seungwon Jeon2953f852013-06-27 13:31:54 +09001437 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
1438 utmrdl_size,
1439 &hba->utmrdl_dma_addr,
1440 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301441 if (!hba->utmrdl_base_addr ||
1442 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301443 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301444 "Task Management Descriptor Memory allocation failed\n");
1445 goto out;
1446 }
1447
1448 /* Allocate memory for local reference block */
Seungwon Jeon2953f852013-06-27 13:31:54 +09001449 hba->lrb = devm_kzalloc(hba->dev,
1450 hba->nutrs * sizeof(struct ufshcd_lrb),
1451 GFP_KERNEL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301452 if (!hba->lrb) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301453 dev_err(hba->dev, "LRB Memory allocation failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301454 goto out;
1455 }
1456 return 0;
1457out:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301458 return -ENOMEM;
1459}
1460
1461/**
1462 * ufshcd_host_memory_configure - configure local reference block with
1463 * memory offsets
1464 * @hba: per adapter instance
1465 *
1466 * Configure Host memory space
1467 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
1468 * address.
1469 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
1470 * and PRDT offset.
1471 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
1472 * into local reference block.
1473 */
1474static void ufshcd_host_memory_configure(struct ufs_hba *hba)
1475{
1476 struct utp_transfer_cmd_desc *cmd_descp;
1477 struct utp_transfer_req_desc *utrdlp;
1478 dma_addr_t cmd_desc_dma_addr;
1479 dma_addr_t cmd_desc_element_addr;
1480 u16 response_offset;
1481 u16 prdt_offset;
1482 int cmd_desc_size;
1483 int i;
1484
1485 utrdlp = hba->utrdl_base_addr;
1486 cmd_descp = hba->ucdl_base_addr;
1487
1488 response_offset =
1489 offsetof(struct utp_transfer_cmd_desc, response_upiu);
1490 prdt_offset =
1491 offsetof(struct utp_transfer_cmd_desc, prd_table);
1492
1493 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
1494 cmd_desc_dma_addr = hba->ucdl_dma_addr;
1495
1496 for (i = 0; i < hba->nutrs; i++) {
1497 /* Configure UTRD with command descriptor base address */
1498 cmd_desc_element_addr =
1499 (cmd_desc_dma_addr + (cmd_desc_size * i));
1500 utrdlp[i].command_desc_base_addr_lo =
1501 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
1502 utrdlp[i].command_desc_base_addr_hi =
1503 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
1504
1505 /* Response upiu and prdt offset should be in double words */
1506 utrdlp[i].response_upiu_offset =
1507 cpu_to_le16((response_offset >> 2));
1508 utrdlp[i].prd_table_offset =
1509 cpu_to_le16((prdt_offset >> 2));
1510 utrdlp[i].response_upiu_length =
Sujit Reddy Thumma3ca316c2013-06-26 22:39:30 +05301511 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301512
1513 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301514 hba->lrb[i].ucd_req_ptr =
1515 (struct utp_upiu_req *)(cmd_descp + i);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301516 hba->lrb[i].ucd_rsp_ptr =
1517 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
1518 hba->lrb[i].ucd_prdt_ptr =
1519 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
1520 }
1521}
1522
1523/**
1524 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
1525 * @hba: per adapter instance
1526 *
1527 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
1528 * in order to initialize the Unipro link startup procedure.
1529 * Once the Unipro links are up, the device connected to the controller
1530 * is detected.
1531 *
1532 * Returns 0 on success, non-zero value on failure
1533 */
1534static int ufshcd_dme_link_startup(struct ufs_hba *hba)
1535{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301536 struct uic_command uic_cmd = {0};
1537 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301538
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301539 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
1540
1541 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1542 if (ret)
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301543 dev_err(hba->dev,
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301544 "dme-link-startup: error code %d\n", ret);
1545 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301546}
1547
1548/**
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05301549 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
1550 * @hba: per adapter instance
1551 * @attr_sel: uic command argument1
1552 * @attr_set: attribute set type as uic command argument2
1553 * @mib_val: setting value as uic command argument3
1554 * @peer: indicate whether peer or local
1555 *
1556 * Returns 0 on success, non-zero value on failure
1557 */
1558int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
1559 u8 attr_set, u32 mib_val, u8 peer)
1560{
1561 struct uic_command uic_cmd = {0};
1562 static const char *const action[] = {
1563 "dme-set",
1564 "dme-peer-set"
1565 };
1566 const char *set = action[!!peer];
1567 int ret;
1568
1569 uic_cmd.command = peer ?
1570 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
1571 uic_cmd.argument1 = attr_sel;
1572 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
1573 uic_cmd.argument3 = mib_val;
1574
1575 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1576 if (ret)
1577 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
1578 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
1579
1580 return ret;
1581}
1582EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
1583
1584/**
1585 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
1586 * @hba: per adapter instance
1587 * @attr_sel: uic command argument1
1588 * @mib_val: the value of the attribute as returned by the UIC command
1589 * @peer: indicate whether peer or local
1590 *
1591 * Returns 0 on success, non-zero value on failure
1592 */
1593int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
1594 u32 *mib_val, u8 peer)
1595{
1596 struct uic_command uic_cmd = {0};
1597 static const char *const action[] = {
1598 "dme-get",
1599 "dme-peer-get"
1600 };
1601 const char *get = action[!!peer];
1602 int ret;
1603
1604 uic_cmd.command = peer ?
1605 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
1606 uic_cmd.argument1 = attr_sel;
1607
1608 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1609 if (ret) {
1610 dev_err(hba->dev, "%s: attr-id 0x%x error code %d\n",
1611 get, UIC_GET_ATTR_ID(attr_sel), ret);
1612 goto out;
1613 }
1614
1615 if (mib_val)
1616 *mib_val = uic_cmd.argument3;
1617out:
1618 return ret;
1619}
1620EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
1621
1622/**
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301623 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
1624 * using DME_SET primitives.
1625 * @hba: per adapter instance
1626 * @mode: powr mode value
1627 *
1628 * Returns 0 on success, non-zero value on failure
1629 */
Sujit Reddy Thummabdbe5d22014-05-26 10:59:11 +05301630static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05301631{
1632 struct uic_command uic_cmd = {0};
1633 struct completion pwr_done;
1634 unsigned long flags;
1635 u8 status;
1636 int ret;
1637
1638 uic_cmd.command = UIC_CMD_DME_SET;
1639 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
1640 uic_cmd.argument3 = mode;
1641 init_completion(&pwr_done);
1642
1643 mutex_lock(&hba->uic_cmd_mutex);
1644
1645 spin_lock_irqsave(hba->host->host_lock, flags);
1646 hba->pwr_done = &pwr_done;
1647 spin_unlock_irqrestore(hba->host->host_lock, flags);
1648 ret = __ufshcd_send_uic_cmd(hba, &uic_cmd);
1649 if (ret) {
1650 dev_err(hba->dev,
1651 "pwr mode change with mode 0x%x uic error %d\n",
1652 mode, ret);
1653 goto out;
1654 }
1655
1656 if (!wait_for_completion_timeout(hba->pwr_done,
1657 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
1658 dev_err(hba->dev,
1659 "pwr mode change with mode 0x%x completion timeout\n",
1660 mode);
1661 ret = -ETIMEDOUT;
1662 goto out;
1663 }
1664
1665 status = ufshcd_get_upmcrs(hba);
1666 if (status != PWR_LOCAL) {
1667 dev_err(hba->dev,
1668 "pwr mode change failed, host umpcrs:0x%x\n",
1669 status);
1670 ret = (status != PWR_OK) ? status : -1;
1671 }
1672out:
1673 spin_lock_irqsave(hba->host->host_lock, flags);
1674 hba->pwr_done = NULL;
1675 spin_unlock_irqrestore(hba->host->host_lock, flags);
1676 mutex_unlock(&hba->uic_cmd_mutex);
1677 return ret;
1678}
1679
1680/**
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05301681 * ufshcd_config_max_pwr_mode - Set & Change power mode with
1682 * maximum capability attribute information.
1683 * @hba: per adapter instance
1684 *
1685 * Returns 0 on success, non-zero value on failure
1686 */
1687static int ufshcd_config_max_pwr_mode(struct ufs_hba *hba)
1688{
1689 enum {RX = 0, TX = 1};
1690 u32 lanes[] = {1, 1};
1691 u32 gear[] = {1, 1};
1692 u8 pwr[] = {FASTAUTO_MODE, FASTAUTO_MODE};
1693 int ret;
1694
1695 /* Get the connected lane count */
1696 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), &lanes[RX]);
1697 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), &lanes[TX]);
1698
1699 /*
1700 * First, get the maximum gears of HS speed.
1701 * If a zero value, it means there is no HSGEAR capability.
1702 * Then, get the maximum gears of PWM speed.
1703 */
1704 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &gear[RX]);
1705 if (!gear[RX]) {
1706 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), &gear[RX]);
1707 pwr[RX] = SLOWAUTO_MODE;
1708 }
1709
1710 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &gear[TX]);
1711 if (!gear[TX]) {
1712 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1713 &gear[TX]);
1714 pwr[TX] = SLOWAUTO_MODE;
1715 }
1716
1717 /*
1718 * Configure attributes for power mode change with below.
1719 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1720 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1721 * - PA_HSSERIES
1722 */
1723 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), gear[RX]);
1724 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), lanes[RX]);
1725 if (pwr[RX] == FASTAUTO_MODE)
1726 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
1727
1728 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), gear[TX]);
1729 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), lanes[TX]);
1730 if (pwr[TX] == FASTAUTO_MODE)
1731 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
1732
1733 if (pwr[RX] == FASTAUTO_MODE || pwr[TX] == FASTAUTO_MODE)
1734 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), PA_HS_MODE_B);
1735
1736 ret = ufshcd_uic_change_pwr_mode(hba, pwr[RX] << 4 | pwr[TX]);
1737 if (ret)
1738 dev_err(hba->dev,
1739 "pwr_mode: power mode change failed %d\n", ret);
1740
1741 return ret;
1742}
1743
1744/**
Dolev Raviv68078d52013-07-30 00:35:58 +05301745 * ufshcd_complete_dev_init() - checks device readiness
1746 * hba: per-adapter instance
1747 *
1748 * Set fDeviceInit flag and poll until device toggles it.
1749 */
1750static int ufshcd_complete_dev_init(struct ufs_hba *hba)
1751{
1752 int i, retries, err = 0;
1753 bool flag_res = 1;
1754
1755 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1756 /* Set the fDeviceInit flag */
1757 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
1758 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
1759 if (!err || err == -ETIMEDOUT)
1760 break;
1761 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1762 }
1763 if (err) {
1764 dev_err(hba->dev,
1765 "%s setting fDeviceInit flag failed with error %d\n",
1766 __func__, err);
1767 goto out;
1768 }
1769
1770 /* poll for max. 100 iterations for fDeviceInit flag to clear */
1771 for (i = 0; i < 100 && !err && flag_res; i++) {
1772 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1773 err = ufshcd_query_flag(hba,
1774 UPIU_QUERY_OPCODE_READ_FLAG,
1775 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
1776 if (!err || err == -ETIMEDOUT)
1777 break;
1778 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
1779 err);
1780 }
1781 }
1782 if (err)
1783 dev_err(hba->dev,
1784 "%s reading fDeviceInit flag failed with error %d\n",
1785 __func__, err);
1786 else if (flag_res)
1787 dev_err(hba->dev,
1788 "%s fDeviceInit was not cleared by the device\n",
1789 __func__);
1790
1791out:
1792 return err;
1793}
1794
1795/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301796 * ufshcd_make_hba_operational - Make UFS controller operational
1797 * @hba: per adapter instance
1798 *
1799 * To bring UFS host controller to operational state,
1800 * 1. Check if device is present
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301801 * 2. Enable required interrupts
1802 * 3. Configure interrupt aggregation
1803 * 4. Program UTRL and UTMRL base addres
1804 * 5. Configure run-stop-registers
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301805 *
1806 * Returns 0 on success, non-zero value on failure
1807 */
1808static int ufshcd_make_hba_operational(struct ufs_hba *hba)
1809{
1810 int err = 0;
1811 u32 reg;
1812
1813 /* check if device present */
Seungwon Jeonb873a2752013-06-26 22:39:26 +05301814 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
Venkatraman S73ec5132012-07-10 19:39:23 +05301815 if (!ufshcd_is_device_present(reg)) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301816 dev_err(hba->dev, "cc: Device not present\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301817 err = -ENXIO;
1818 goto out;
1819 }
1820
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301821 /* Enable required interrupts */
1822 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
1823
1824 /* Configure interrupt aggregation */
Seungwon Jeon7d568652013-08-31 21:40:20 +05301825 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301826
1827 /* Configure UTRL and UTMRL base address registers */
1828 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
1829 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
1830 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
1831 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
1832 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
1833 REG_UTP_TASK_REQ_LIST_BASE_L);
1834 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
1835 REG_UTP_TASK_REQ_LIST_BASE_H);
1836
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301837 /*
1838 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
1839 * DEI, HEI bits must be 0
1840 */
1841 if (!(ufshcd_get_lists_status(reg))) {
1842 ufshcd_enable_run_stop_reg(hba);
1843 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301844 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301845 "Host controller not ready to process requests");
1846 err = -EIO;
1847 goto out;
1848 }
1849
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301850out:
1851 return err;
1852}
1853
1854/**
1855 * ufshcd_hba_enable - initialize the controller
1856 * @hba: per adapter instance
1857 *
1858 * The controller resets itself and controller firmware initialization
1859 * sequence kicks off. When controller is ready it will set
1860 * the Host Controller Enable bit to 1.
1861 *
1862 * Returns 0 on success, non-zero value on failure
1863 */
1864static int ufshcd_hba_enable(struct ufs_hba *hba)
1865{
1866 int retry;
1867
1868 /*
1869 * msleep of 1 and 5 used in this function might result in msleep(20),
1870 * but it was necessary to send the UFS FPGA to reset mode during
1871 * development and testing of this driver. msleep can be changed to
1872 * mdelay and retry count can be reduced based on the controller.
1873 */
1874 if (!ufshcd_is_hba_active(hba)) {
1875
1876 /* change controller state to "reset state" */
1877 ufshcd_hba_stop(hba);
1878
1879 /*
1880 * This delay is based on the testing done with UFS host
1881 * controller FPGA. The delay can be changed based on the
1882 * host controller used.
1883 */
1884 msleep(5);
1885 }
1886
1887 /* start controller initialization sequence */
1888 ufshcd_hba_start(hba);
1889
1890 /*
1891 * To initialize a UFS host controller HCE bit must be set to 1.
1892 * During initialization the HCE bit value changes from 1->0->1.
1893 * When the host controller completes initialization sequence
1894 * it sets the value of HCE bit to 1. The same HCE bit is read back
1895 * to check if the controller has completed initialization sequence.
1896 * So without this delay the value HCE = 1, set in the previous
1897 * instruction might be read back.
1898 * This delay can be changed based on the controller.
1899 */
1900 msleep(1);
1901
1902 /* wait for the host controller to complete initialization */
1903 retry = 10;
1904 while (ufshcd_is_hba_active(hba)) {
1905 if (retry) {
1906 retry--;
1907 } else {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05301908 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301909 "Controller enable failed\n");
1910 return -EIO;
1911 }
1912 msleep(5);
1913 }
1914 return 0;
1915}
1916
1917/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301918 * ufshcd_link_startup - Initialize unipro link startup
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301919 * @hba: per adapter instance
1920 *
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301921 * Returns 0 for success, non-zero in case of failure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301922 */
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301923static int ufshcd_link_startup(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301924{
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301925 int ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301926
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301927 /* enable UIC related interrupts */
1928 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301929
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05301930 ret = ufshcd_dme_link_startup(hba);
1931 if (ret)
1932 goto out;
1933
1934 ret = ufshcd_make_hba_operational(hba);
1935
1936out:
1937 if (ret)
1938 dev_err(hba->dev, "link startup failed %d\n", ret);
1939 return ret;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301940}
1941
1942/**
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05301943 * ufshcd_verify_dev_init() - Verify device initialization
1944 * @hba: per-adapter instance
1945 *
1946 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
1947 * device Transport Protocol (UTP) layer is ready after a reset.
1948 * If the UTP layer at the device side is not initialized, it may
1949 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
1950 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
1951 */
1952static int ufshcd_verify_dev_init(struct ufs_hba *hba)
1953{
1954 int err = 0;
1955 int retries;
1956
1957 mutex_lock(&hba->dev_cmd.lock);
1958 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
1959 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
1960 NOP_OUT_TIMEOUT);
1961
1962 if (!err || err == -ETIMEDOUT)
1963 break;
1964
1965 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1966 }
1967 mutex_unlock(&hba->dev_cmd.lock);
1968
1969 if (err)
1970 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
1971 return err;
1972}
1973
1974/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301975 * ufshcd_slave_alloc - handle initial SCSI device configurations
1976 * @sdev: pointer to SCSI device
1977 *
1978 * Returns success
1979 */
1980static int ufshcd_slave_alloc(struct scsi_device *sdev)
1981{
1982 struct ufs_hba *hba;
Dolev Raviv1b3e8952014-06-29 09:40:19 +03001983 int lun_qdepth;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05301984
1985 hba = shost_priv(sdev->host);
1986 sdev->tagged_supported = 1;
1987
1988 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
1989 sdev->use_10_for_ms = 1;
1990 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
1991
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05301992 /* allow SCSI layer to restart the device in case of errors */
1993 sdev->allow_restart = 1;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03001994
Sujit Reddy Thummab2a6c522014-07-01 12:22:38 +03001995 /* REPORT SUPPORTED OPERATION CODES is not supported */
1996 sdev->no_report_opcodes = 1;
1997
Dolev Raviv1b3e8952014-06-29 09:40:19 +03001998 lun_qdepth = ufshcd_read_sdev_qdepth(hba, sdev);
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03001999 if (lun_qdepth <= 0)
2000 /* eventually, we can figure out the real queue depth */
Dolev Raviv1b3e8952014-06-29 09:40:19 +03002001 lun_qdepth = hba->nutrs;
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002002 else
2003 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2004
2005 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2006 __func__, lun_qdepth);
2007 scsi_activate_tcq(sdev, lun_qdepth);
2008
2009 return 0;
2010}
2011
2012/**
2013 * ufshcd_change_queue_depth - change queue depth
2014 * @sdev: pointer to SCSI device
2015 * @depth: required depth to set
2016 * @reason: reason for changing the depth
2017 *
2018 * Change queue depth according to the reason and make sure
2019 * the max. limits are not crossed.
2020 */
Sujit Reddy Thumma7289f982014-07-23 09:31:11 +03002021static int ufshcd_change_queue_depth(struct scsi_device *sdev,
2022 int depth, int reason)
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002023{
2024 struct ufs_hba *hba = shost_priv(sdev->host);
2025
2026 if (depth > hba->nutrs)
2027 depth = hba->nutrs;
2028
2029 switch (reason) {
2030 case SCSI_QDEPTH_DEFAULT:
2031 case SCSI_QDEPTH_RAMP_UP:
2032 if (!sdev->tagged_supported)
2033 depth = 1;
2034 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2035 break;
2036 case SCSI_QDEPTH_QFULL:
2037 scsi_track_queue_full(sdev, depth);
2038 break;
2039 default:
2040 return -EOPNOTSUPP;
Dolev Raviv1b3e8952014-06-29 09:40:19 +03002041 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302042
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03002043 return depth;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302044}
2045
2046/**
Akinobu Mitaeeda4742014-07-01 23:00:32 +09002047 * ufshcd_slave_configure - adjust SCSI device configurations
2048 * @sdev: pointer to SCSI device
2049 */
2050static int ufshcd_slave_configure(struct scsi_device *sdev)
2051{
2052 struct request_queue *q = sdev->request_queue;
2053
2054 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
2055 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
2056
2057 return 0;
2058}
2059
2060/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302061 * ufshcd_slave_destroy - remove SCSI device configurations
2062 * @sdev: pointer to SCSI device
2063 */
2064static void ufshcd_slave_destroy(struct scsi_device *sdev)
2065{
2066 struct ufs_hba *hba;
2067
2068 hba = shost_priv(sdev->host);
2069 scsi_deactivate_tcq(sdev, hba->nutrs);
2070}
2071
2072/**
2073 * ufshcd_task_req_compl - handle task management request completion
2074 * @hba: per adapter instance
2075 * @index: index of the completed request
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302076 * @resp: task management service response
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302077 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302078 * Returns non-zero value on error, zero on success
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302079 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302080static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302081{
2082 struct utp_task_req_desc *task_req_descp;
2083 struct utp_upiu_task_rsp *task_rsp_upiup;
2084 unsigned long flags;
2085 int ocs_value;
2086 int task_result;
2087
2088 spin_lock_irqsave(hba->host->host_lock, flags);
2089
2090 /* Clear completed tasks from outstanding_tasks */
2091 __clear_bit(index, &hba->outstanding_tasks);
2092
2093 task_req_descp = hba->utmrdl_base_addr;
2094 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
2095
2096 if (ocs_value == OCS_SUCCESS) {
2097 task_rsp_upiup = (struct utp_upiu_task_rsp *)
2098 task_req_descp[index].task_rsp_upiu;
2099 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
2100 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302101 if (resp)
2102 *resp = (u8)task_result;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302103 } else {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302104 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
2105 __func__, ocs_value);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302106 }
2107 spin_unlock_irqrestore(hba->host->host_lock, flags);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302108
2109 return ocs_value;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302110}
2111
2112/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302113 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
2114 * @lrb: pointer to local reference block of completed command
2115 * @scsi_status: SCSI command status
2116 *
2117 * Returns value base on SCSI command status
2118 */
2119static inline int
2120ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
2121{
2122 int result = 0;
2123
2124 switch (scsi_status) {
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302125 case SAM_STAT_CHECK_CONDITION:
2126 ufshcd_copy_sense_data(lrbp);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302127 case SAM_STAT_GOOD:
2128 result |= DID_OK << 16 |
2129 COMMAND_COMPLETE << 8 |
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302130 scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302131 break;
2132 case SAM_STAT_TASK_SET_FULL:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302133 case SAM_STAT_BUSY:
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302134 case SAM_STAT_TASK_ABORTED:
Seungwon Jeon1c2623c2013-08-31 21:40:19 +05302135 ufshcd_copy_sense_data(lrbp);
2136 result |= scsi_status;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302137 break;
2138 default:
2139 result |= DID_ERROR << 16;
2140 break;
2141 } /* end of switch */
2142
2143 return result;
2144}
2145
2146/**
2147 * ufshcd_transfer_rsp_status - Get overall status of the response
2148 * @hba: per adapter instance
2149 * @lrb: pointer to local reference block of completed command
2150 *
2151 * Returns result of the command to notify SCSI midlayer
2152 */
2153static inline int
2154ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2155{
2156 int result = 0;
2157 int scsi_status;
2158 int ocs;
2159
2160 /* overall command status of utrd */
2161 ocs = ufshcd_get_tr_ocs(lrbp);
2162
2163 switch (ocs) {
2164 case OCS_SUCCESS:
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302165 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302166
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302167 switch (result) {
2168 case UPIU_TRANSACTION_RESPONSE:
2169 /*
2170 * get the response UPIU result to extract
2171 * the SCSI command status
2172 */
2173 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
2174
2175 /*
2176 * get the result based on SCSI status response
2177 * to notify the SCSI midlayer of the command status
2178 */
2179 scsi_status = result & MASK_SCSI_STATUS;
2180 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302181
2182 if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
2183 schedule_work(&hba->eeh_work);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302184 break;
2185 case UPIU_TRANSACTION_REJECT_UPIU:
2186 /* TODO: handle Reject UPIU Response */
2187 result = DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302188 dev_err(hba->dev,
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302189 "Reject UPIU not fully implemented\n");
2190 break;
2191 default:
2192 result = DID_ERROR << 16;
2193 dev_err(hba->dev,
2194 "Unexpected request response code = %x\n",
2195 result);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302196 break;
2197 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302198 break;
2199 case OCS_ABORTED:
2200 result |= DID_ABORT << 16;
2201 break;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302202 case OCS_INVALID_COMMAND_STATUS:
2203 result |= DID_REQUEUE << 16;
2204 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302205 case OCS_INVALID_CMD_TABLE_ATTR:
2206 case OCS_INVALID_PRDT_ATTR:
2207 case OCS_MISMATCH_DATA_BUF_SIZE:
2208 case OCS_MISMATCH_RESP_UPIU_SIZE:
2209 case OCS_PEER_COMM_FAILURE:
2210 case OCS_FATAL_ERROR:
2211 default:
2212 result |= DID_ERROR << 16;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05302213 dev_err(hba->dev,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302214 "OCS error from controller = %x\n", ocs);
2215 break;
2216 } /* end of switch */
2217
2218 return result;
2219}
2220
2221/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302222 * ufshcd_uic_cmd_compl - handle completion of uic command
2223 * @hba: per adapter instance
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302224 * @intr_status: interrupt status generated by the controller
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302225 */
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302226static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302227{
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302228 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302229 hba->active_uic_cmd->argument2 |=
2230 ufshcd_get_uic_cmd_result(hba);
Seungwon Jeon12b4fdb2013-08-31 21:40:21 +05302231 hba->active_uic_cmd->argument3 =
2232 ufshcd_get_dme_attr_val(hba);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302233 complete(&hba->active_uic_cmd->done);
2234 }
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302235
2236 if ((intr_status & UIC_POWER_MODE) && hba->pwr_done)
2237 complete(hba->pwr_done);
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302238}
2239
2240/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302241 * ufshcd_transfer_req_compl - handle SCSI and query command completion
2242 * @hba: per adapter instance
2243 */
2244static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
2245{
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302246 struct ufshcd_lrb *lrbp;
2247 struct scsi_cmnd *cmd;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302248 unsigned long completed_reqs;
2249 u32 tr_doorbell;
2250 int result;
2251 int index;
Dolev Ravive9d501b2014-07-01 12:22:37 +03002252
2253 /* Resetting interrupt aggregation counters first and reading the
2254 * DOOR_BELL afterward allows us to handle all the completed requests.
2255 * In order to prevent other interrupts starvation the DB is read once
2256 * after reset. The down side of this solution is the possibility of
2257 * false interrupt if device completes another request after resetting
2258 * aggregation and before reading the DB.
2259 */
2260 ufshcd_reset_intr_aggr(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302261
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302262 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302263 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
2264
Dolev Ravive9d501b2014-07-01 12:22:37 +03002265 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
2266 lrbp = &hba->lrb[index];
2267 cmd = lrbp->cmd;
2268 if (cmd) {
2269 result = ufshcd_transfer_rsp_status(hba, lrbp);
2270 scsi_dma_unmap(cmd);
2271 cmd->result = result;
2272 /* Mark completed command as NULL in LRB */
2273 lrbp->cmd = NULL;
2274 clear_bit_unlock(index, &hba->lrb_in_use);
2275 /* Do not touch lrbp after scsi done */
2276 cmd->scsi_done(cmd);
2277 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
2278 if (hba->dev_cmd.complete)
2279 complete(hba->dev_cmd.complete);
2280 }
2281 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302282
2283 /* clear corresponding bits of completed commands */
2284 hba->outstanding_reqs ^= completed_reqs;
2285
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302286 /* we might have free'd some tags above */
2287 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302288}
2289
2290/**
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302291 * ufshcd_disable_ee - disable exception event
2292 * @hba: per-adapter instance
2293 * @mask: exception event to disable
2294 *
2295 * Disables exception event in the device so that the EVENT_ALERT
2296 * bit is not set.
2297 *
2298 * Returns zero on success, non-zero error value on failure.
2299 */
2300static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
2301{
2302 int err = 0;
2303 u32 val;
2304
2305 if (!(hba->ee_ctrl_mask & mask))
2306 goto out;
2307
2308 val = hba->ee_ctrl_mask & ~mask;
2309 val &= 0xFFFF; /* 2 bytes */
2310 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
2311 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
2312 if (!err)
2313 hba->ee_ctrl_mask &= ~mask;
2314out:
2315 return err;
2316}
2317
2318/**
2319 * ufshcd_enable_ee - enable exception event
2320 * @hba: per-adapter instance
2321 * @mask: exception event to enable
2322 *
2323 * Enable corresponding exception event in the device to allow
2324 * device to alert host in critical scenarios.
2325 *
2326 * Returns zero on success, non-zero error value on failure.
2327 */
2328static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
2329{
2330 int err = 0;
2331 u32 val;
2332
2333 if (hba->ee_ctrl_mask & mask)
2334 goto out;
2335
2336 val = hba->ee_ctrl_mask | mask;
2337 val &= 0xFFFF; /* 2 bytes */
2338 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
2339 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
2340 if (!err)
2341 hba->ee_ctrl_mask |= mask;
2342out:
2343 return err;
2344}
2345
2346/**
2347 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
2348 * @hba: per-adapter instance
2349 *
2350 * Allow device to manage background operations on its own. Enabling
2351 * this might lead to inconsistent latencies during normal data transfers
2352 * as the device is allowed to manage its own way of handling background
2353 * operations.
2354 *
2355 * Returns zero on success, non-zero on failure.
2356 */
2357static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
2358{
2359 int err = 0;
2360
2361 if (hba->auto_bkops_enabled)
2362 goto out;
2363
2364 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2365 QUERY_FLAG_IDN_BKOPS_EN, NULL);
2366 if (err) {
2367 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
2368 __func__, err);
2369 goto out;
2370 }
2371
2372 hba->auto_bkops_enabled = true;
2373
2374 /* No need of URGENT_BKOPS exception from the device */
2375 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
2376 if (err)
2377 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
2378 __func__, err);
2379out:
2380 return err;
2381}
2382
2383/**
2384 * ufshcd_disable_auto_bkops - block device in doing background operations
2385 * @hba: per-adapter instance
2386 *
2387 * Disabling background operations improves command response latency but
2388 * has drawback of device moving into critical state where the device is
2389 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
2390 * host is idle so that BKOPS are managed effectively without any negative
2391 * impacts.
2392 *
2393 * Returns zero on success, non-zero on failure.
2394 */
2395static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
2396{
2397 int err = 0;
2398
2399 if (!hba->auto_bkops_enabled)
2400 goto out;
2401
2402 /*
2403 * If host assisted BKOPs is to be enabled, make sure
2404 * urgent bkops exception is allowed.
2405 */
2406 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
2407 if (err) {
2408 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
2409 __func__, err);
2410 goto out;
2411 }
2412
2413 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
2414 QUERY_FLAG_IDN_BKOPS_EN, NULL);
2415 if (err) {
2416 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
2417 __func__, err);
2418 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
2419 goto out;
2420 }
2421
2422 hba->auto_bkops_enabled = false;
2423out:
2424 return err;
2425}
2426
2427/**
2428 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
2429 * @hba: per adapter instance
2430 *
2431 * After a device reset the device may toggle the BKOPS_EN flag
2432 * to default value. The s/w tracking variables should be updated
2433 * as well. Do this by forcing enable of auto bkops.
2434 */
2435static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
2436{
2437 hba->auto_bkops_enabled = false;
2438 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
2439 ufshcd_enable_auto_bkops(hba);
2440}
2441
2442static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
2443{
2444 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
2445 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
2446}
2447
2448/**
2449 * ufshcd_urgent_bkops - handle urgent bkops exception event
2450 * @hba: per-adapter instance
2451 *
2452 * Enable fBackgroundOpsEn flag in the device to permit background
2453 * operations.
2454 */
2455static int ufshcd_urgent_bkops(struct ufs_hba *hba)
2456{
2457 int err;
2458 u32 status = 0;
2459
2460 err = ufshcd_get_bkops_status(hba, &status);
2461 if (err) {
2462 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
2463 __func__, err);
2464 goto out;
2465 }
2466
2467 status = status & 0xF;
2468
2469 /* handle only if status indicates performance impact or critical */
2470 if (status >= BKOPS_STATUS_PERF_IMPACT)
2471 err = ufshcd_enable_auto_bkops(hba);
2472out:
2473 return err;
2474}
2475
2476static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
2477{
2478 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
2479 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
2480}
2481
2482/**
2483 * ufshcd_exception_event_handler - handle exceptions raised by device
2484 * @work: pointer to work data
2485 *
2486 * Read bExceptionEventStatus attribute from the device and handle the
2487 * exception event accordingly.
2488 */
2489static void ufshcd_exception_event_handler(struct work_struct *work)
2490{
2491 struct ufs_hba *hba;
2492 int err;
2493 u32 status = 0;
2494 hba = container_of(work, struct ufs_hba, eeh_work);
2495
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302496 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302497 err = ufshcd_get_ee_status(hba, &status);
2498 if (err) {
2499 dev_err(hba->dev, "%s: failed to get exception status %d\n",
2500 __func__, err);
2501 goto out;
2502 }
2503
2504 status &= hba->ee_ctrl_mask;
2505 if (status & MASK_EE_URGENT_BKOPS) {
2506 err = ufshcd_urgent_bkops(hba);
2507 if (err)
2508 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
2509 __func__, err);
2510 }
2511out:
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302512 pm_runtime_put_sync(hba->dev);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05302513 return;
2514}
2515
2516/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302517 * ufshcd_err_handler - handle UFS errors that require s/w attention
2518 * @work: pointer to work structure
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302519 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302520static void ufshcd_err_handler(struct work_struct *work)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302521{
2522 struct ufs_hba *hba;
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302523 unsigned long flags;
2524 u32 err_xfer = 0;
2525 u32 err_tm = 0;
2526 int err = 0;
2527 int tag;
2528
2529 hba = container_of(work, struct ufs_hba, eh_work);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302530
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302531 pm_runtime_get_sync(hba->dev);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302532
2533 spin_lock_irqsave(hba->host->host_lock, flags);
2534 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
2535 spin_unlock_irqrestore(hba->host->host_lock, flags);
2536 goto out;
2537 }
2538
2539 hba->ufshcd_state = UFSHCD_STATE_RESET;
2540 ufshcd_set_eh_in_progress(hba);
2541
2542 /* Complete requests that have door-bell cleared by h/w */
2543 ufshcd_transfer_req_compl(hba);
2544 ufshcd_tmc_handler(hba);
2545 spin_unlock_irqrestore(hba->host->host_lock, flags);
2546
2547 /* Clear pending transfer requests */
2548 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
2549 if (ufshcd_clear_cmd(hba, tag))
2550 err_xfer |= 1 << tag;
2551
2552 /* Clear pending task management requests */
2553 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
2554 if (ufshcd_clear_tm_cmd(hba, tag))
2555 err_tm |= 1 << tag;
2556
2557 /* Complete the requests that are cleared by s/w */
2558 spin_lock_irqsave(hba->host->host_lock, flags);
2559 ufshcd_transfer_req_compl(hba);
2560 ufshcd_tmc_handler(hba);
2561 spin_unlock_irqrestore(hba->host->host_lock, flags);
2562
2563 /* Fatal errors need reset */
2564 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
2565 ((hba->saved_err & UIC_ERROR) &&
2566 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
2567 err = ufshcd_reset_and_restore(hba);
2568 if (err) {
2569 dev_err(hba->dev, "%s: reset and restore failed\n",
2570 __func__);
2571 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2572 }
2573 /*
2574 * Inform scsi mid-layer that we did reset and allow to handle
2575 * Unit Attention properly.
2576 */
2577 scsi_report_bus_reset(hba->host, 0);
2578 hba->saved_err = 0;
2579 hba->saved_uic_err = 0;
2580 }
2581 ufshcd_clear_eh_in_progress(hba);
2582
2583out:
2584 scsi_unblock_requests(hba->host);
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05302585 pm_runtime_put_sync(hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302586}
2587
2588/**
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302589 * ufshcd_update_uic_error - check and set fatal UIC error flags.
2590 * @hba: per-adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302591 */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302592static void ufshcd_update_uic_error(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302593{
2594 u32 reg;
2595
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302596 /* PA_INIT_ERROR is fatal and needs UIC reset */
2597 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
2598 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
2599 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
2600
2601 /* UIC NL/TL/DME errors needs software retry */
2602 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
2603 if (reg)
2604 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
2605
2606 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
2607 if (reg)
2608 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
2609
2610 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
2611 if (reg)
2612 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
2613
2614 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
2615 __func__, hba->uic_error);
2616}
2617
2618/**
2619 * ufshcd_check_errors - Check for errors that need s/w attention
2620 * @hba: per-adapter instance
2621 */
2622static void ufshcd_check_errors(struct ufs_hba *hba)
2623{
2624 bool queue_eh_work = false;
2625
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302626 if (hba->errors & INT_FATAL_ERRORS)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302627 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302628
2629 if (hba->errors & UIC_ERROR) {
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302630 hba->uic_error = 0;
2631 ufshcd_update_uic_error(hba);
2632 if (hba->uic_error)
2633 queue_eh_work = true;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302634 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302635
2636 if (queue_eh_work) {
2637 /* handle fatal errors only when link is functional */
2638 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
2639 /* block commands from scsi mid-layer */
2640 scsi_block_requests(hba->host);
2641
2642 /* transfer error masks to sticky bits */
2643 hba->saved_err |= hba->errors;
2644 hba->saved_uic_err |= hba->uic_error;
2645
2646 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2647 schedule_work(&hba->eh_work);
2648 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302649 }
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302650 /*
2651 * if (!queue_eh_work) -
2652 * Other errors are either non-fatal where host recovers
2653 * itself without s/w intervention or errors that will be
2654 * handled by the SCSI core layer.
2655 */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302656}
2657
2658/**
2659 * ufshcd_tmc_handler - handle task management function completion
2660 * @hba: per adapter instance
2661 */
2662static void ufshcd_tmc_handler(struct ufs_hba *hba)
2663{
2664 u32 tm_doorbell;
2665
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302666 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302667 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302668 wake_up(&hba->tm_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302669}
2670
2671/**
2672 * ufshcd_sl_intr - Interrupt service routine
2673 * @hba: per adapter instance
2674 * @intr_status: contains interrupts generated by the controller
2675 */
2676static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
2677{
2678 hba->errors = UFSHCD_ERROR_MASK & intr_status;
2679 if (hba->errors)
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05302680 ufshcd_check_errors(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302681
Seungwon Jeon53b3d9c2013-08-31 21:40:22 +05302682 if (intr_status & UFSHCD_UIC_MASK)
2683 ufshcd_uic_cmd_compl(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302684
2685 if (intr_status & UTP_TASK_REQ_COMPL)
2686 ufshcd_tmc_handler(hba);
2687
2688 if (intr_status & UTP_TRANSFER_REQ_COMPL)
2689 ufshcd_transfer_req_compl(hba);
2690}
2691
2692/**
2693 * ufshcd_intr - Main interrupt service routine
2694 * @irq: irq number
2695 * @__hba: pointer to adapter instance
2696 *
2697 * Returns IRQ_HANDLED - If interrupt is valid
2698 * IRQ_NONE - If invalid interrupt
2699 */
2700static irqreturn_t ufshcd_intr(int irq, void *__hba)
2701{
2702 u32 intr_status;
2703 irqreturn_t retval = IRQ_NONE;
2704 struct ufs_hba *hba = __hba;
2705
2706 spin_lock(hba->host->host_lock);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302707 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302708
2709 if (intr_status) {
Seungwon Jeon261ea452013-06-26 22:39:28 +05302710 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302711 ufshcd_sl_intr(hba, intr_status);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302712 retval = IRQ_HANDLED;
2713 }
2714 spin_unlock(hba->host->host_lock);
2715 return retval;
2716}
2717
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302718static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
2719{
2720 int err = 0;
2721 u32 mask = 1 << tag;
2722 unsigned long flags;
2723
2724 if (!test_bit(tag, &hba->outstanding_tasks))
2725 goto out;
2726
2727 spin_lock_irqsave(hba->host->host_lock, flags);
2728 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
2729 spin_unlock_irqrestore(hba->host->host_lock, flags);
2730
2731 /* poll for max. 1 sec to clear door bell register by h/w */
2732 err = ufshcd_wait_for_register(hba,
2733 REG_UTP_TASK_REQ_DOOR_BELL,
2734 mask, 0, 1000, 1000);
2735out:
2736 return err;
2737}
2738
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302739/**
2740 * ufshcd_issue_tm_cmd - issues task management commands to controller
2741 * @hba: per adapter instance
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302742 * @lun_id: LUN ID to which TM command is sent
2743 * @task_id: task ID to which the TM command is applicable
2744 * @tm_function: task management function opcode
2745 * @tm_response: task management service response return value
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302746 *
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302747 * Returns non-zero value on error, zero on success.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302748 */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302749static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
2750 u8 tm_function, u8 *tm_response)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302751{
2752 struct utp_task_req_desc *task_req_descp;
2753 struct utp_upiu_task_req *task_req_upiup;
2754 struct Scsi_Host *host;
2755 unsigned long flags;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302756 int free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302757 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302758 int task_tag;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302759
2760 host = hba->host;
2761
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302762 /*
2763 * Get free slot, sleep if slots are unavailable.
2764 * Even though we use wait_event() which sleeps indefinitely,
2765 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
2766 */
2767 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
2768
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302769 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302770 task_req_descp = hba->utmrdl_base_addr;
2771 task_req_descp += free_slot;
2772
2773 /* Configure task request descriptor */
2774 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
2775 task_req_descp->header.dword_2 =
2776 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
2777
2778 /* Configure task request UPIU */
2779 task_req_upiup =
2780 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302781 task_tag = hba->nutrs + free_slot;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302782 task_req_upiup->header.dword_0 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302783 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302784 lun_id, task_tag);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302785 task_req_upiup->header.dword_1 =
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302786 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302787
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302788 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
2789 task_req_upiup->input_param2 = cpu_to_be32(task_id);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302790
2791 /* send command to the controller */
2792 __set_bit(free_slot, &hba->outstanding_tasks);
Seungwon Jeonb873a2752013-06-26 22:39:26 +05302793 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302794
2795 spin_unlock_irqrestore(host->host_lock, flags);
2796
2797 /* wait until the task management command is completed */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302798 err = wait_event_timeout(hba->tm_wq,
2799 test_bit(free_slot, &hba->tm_condition),
2800 msecs_to_jiffies(TM_CMD_TIMEOUT));
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302801 if (!err) {
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302802 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
2803 __func__, tm_function);
2804 if (ufshcd_clear_tm_cmd(hba, free_slot))
2805 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
2806 __func__, free_slot);
2807 err = -ETIMEDOUT;
2808 } else {
2809 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302810 }
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302811
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302812 clear_bit(free_slot, &hba->tm_condition);
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302813 ufshcd_put_tm_slot(hba, free_slot);
2814 wake_up(&hba->tm_tag_wq);
2815
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302816 return err;
2817}
2818
2819/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302820 * ufshcd_eh_device_reset_handler - device reset handler registered to
2821 * scsi layer.
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302822 * @cmd: SCSI command pointer
2823 *
2824 * Returns SUCCESS/FAILED
2825 */
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302826static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302827{
2828 struct Scsi_Host *host;
2829 struct ufs_hba *hba;
2830 unsigned int tag;
2831 u32 pos;
2832 int err;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302833 u8 resp = 0xF;
2834 struct ufshcd_lrb *lrbp;
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302835 unsigned long flags;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302836
2837 host = cmd->device->host;
2838 hba = shost_priv(host);
2839 tag = cmd->request->tag;
2840
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302841 lrbp = &hba->lrb[tag];
2842 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
2843 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302844 if (!err)
2845 err = resp;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302846 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302847 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302848
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302849 /* clear the commands that were pending for corresponding LUN */
2850 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
2851 if (hba->lrb[pos].lun == lrbp->lun) {
2852 err = ufshcd_clear_cmd(hba, pos);
2853 if (err)
2854 break;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302855 }
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302856 }
2857 spin_lock_irqsave(host->host_lock, flags);
2858 ufshcd_transfer_req_compl(hba);
2859 spin_unlock_irqrestore(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302860out:
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302861 if (!err) {
2862 err = SUCCESS;
2863 } else {
2864 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
2865 err = FAILED;
2866 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302867 return err;
2868}
2869
2870/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302871 * ufshcd_abort - abort a specific command
2872 * @cmd: SCSI command pointer
2873 *
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302874 * Abort the pending command in device by sending UFS_ABORT_TASK task management
2875 * command, and in host controller by clearing the door-bell register. There can
2876 * be race between controller sending the command to the device while abort is
2877 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
2878 * really issued and then try to abort it.
2879 *
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302880 * Returns SUCCESS/FAILED
2881 */
2882static int ufshcd_abort(struct scsi_cmnd *cmd)
2883{
2884 struct Scsi_Host *host;
2885 struct ufs_hba *hba;
2886 unsigned long flags;
2887 unsigned int tag;
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302888 int err = 0;
2889 int poll_cnt;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302890 u8 resp = 0xF;
2891 struct ufshcd_lrb *lrbp;
Dolev Ravive9d501b2014-07-01 12:22:37 +03002892 u32 reg;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302893
2894 host = cmd->device->host;
2895 hba = shost_priv(host);
2896 tag = cmd->request->tag;
2897
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302898 /* If command is already aborted/completed, return SUCCESS */
2899 if (!(test_bit(tag, &hba->outstanding_reqs)))
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302900 goto out;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302901
Dolev Ravive9d501b2014-07-01 12:22:37 +03002902 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
2903 if (!(reg & (1 << tag))) {
2904 dev_err(hba->dev,
2905 "%s: cmd was completed, but without a notifying intr, tag = %d",
2906 __func__, tag);
2907 }
2908
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302909 lrbp = &hba->lrb[tag];
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302910 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
2911 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
2912 UFS_QUERY_TASK, &resp);
2913 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
2914 /* cmd pending in the device */
2915 break;
2916 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302917 /*
2918 * cmd not pending in the device, check if it is
2919 * in transition.
2920 */
2921 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
2922 if (reg & (1 << tag)) {
2923 /* sleep for max. 200us to stabilize */
2924 usleep_range(100, 200);
2925 continue;
2926 }
2927 /* command completed already */
2928 goto out;
2929 } else {
2930 if (!err)
2931 err = resp; /* service response error */
2932 goto out;
2933 }
2934 }
2935
2936 if (!poll_cnt) {
2937 err = -EBUSY;
2938 goto out;
2939 }
2940
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302941 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
2942 UFS_ABORT_TASK, &resp);
2943 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302944 if (!err)
2945 err = resp; /* service response error */
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302946 goto out;
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05302947 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302948
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302949 err = ufshcd_clear_cmd(hba, tag);
2950 if (err)
2951 goto out;
2952
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302953 scsi_dma_unmap(cmd);
2954
2955 spin_lock_irqsave(host->host_lock, flags);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302956 __clear_bit(tag, &hba->outstanding_reqs);
2957 hba->lrb[tag].cmd = NULL;
2958 spin_unlock_irqrestore(host->host_lock, flags);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05302959
2960 clear_bit_unlock(tag, &hba->lrb_in_use);
2961 wake_up(&hba->dev_cmd.tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302962out:
Sujit Reddy Thummaf20810d2014-05-26 10:59:13 +05302963 if (!err) {
2964 err = SUCCESS;
2965 } else {
2966 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
2967 err = FAILED;
2968 }
2969
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05302970 return err;
2971}
2972
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05302973/**
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05302974 * ufshcd_host_reset_and_restore - reset and restore host controller
2975 * @hba: per-adapter instance
2976 *
2977 * Note that host controller reset may issue DME_RESET to
2978 * local and remote (device) Uni-Pro stack and the attributes
2979 * are reset to default state.
2980 *
2981 * Returns zero on success, non-zero on failure
2982 */
2983static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
2984{
2985 int err;
2986 async_cookie_t cookie;
2987 unsigned long flags;
2988
2989 /* Reset the host controller */
2990 spin_lock_irqsave(hba->host->host_lock, flags);
2991 ufshcd_hba_stop(hba);
2992 spin_unlock_irqrestore(hba->host->host_lock, flags);
2993
2994 err = ufshcd_hba_enable(hba);
2995 if (err)
2996 goto out;
2997
2998 /* Establish the link again and restore the device */
2999 cookie = async_schedule(ufshcd_async_scan, hba);
3000 /* wait for async scan to be completed */
3001 async_synchronize_cookie(++cookie);
3002 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
3003 err = -EIO;
3004out:
3005 if (err)
3006 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
3007
3008 return err;
3009}
3010
3011/**
3012 * ufshcd_reset_and_restore - reset and re-initialize host/device
3013 * @hba: per-adapter instance
3014 *
3015 * Reset and recover device, host and re-establish link. This
3016 * is helpful to recover the communication in fatal error conditions.
3017 *
3018 * Returns zero on success, non-zero on failure
3019 */
3020static int ufshcd_reset_and_restore(struct ufs_hba *hba)
3021{
3022 int err = 0;
3023 unsigned long flags;
3024
3025 err = ufshcd_host_reset_and_restore(hba);
3026
3027 /*
3028 * After reset the door-bell might be cleared, complete
3029 * outstanding requests in s/w here.
3030 */
3031 spin_lock_irqsave(hba->host->host_lock, flags);
3032 ufshcd_transfer_req_compl(hba);
3033 ufshcd_tmc_handler(hba);
3034 spin_unlock_irqrestore(hba->host->host_lock, flags);
3035
3036 return err;
3037}
3038
3039/**
3040 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
3041 * @cmd - SCSI command pointer
3042 *
3043 * Returns SUCCESS/FAILED
3044 */
3045static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
3046{
3047 int err;
3048 unsigned long flags;
3049 struct ufs_hba *hba;
3050
3051 hba = shost_priv(cmd->device->host);
3052
3053 /*
3054 * Check if there is any race with fatal error handling.
3055 * If so, wait for it to complete. Even though fatal error
3056 * handling does reset and restore in some cases, don't assume
3057 * anything out of it. We are just avoiding race here.
3058 */
3059 do {
3060 spin_lock_irqsave(hba->host->host_lock, flags);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303061 if (!(work_pending(&hba->eh_work) ||
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303062 hba->ufshcd_state == UFSHCD_STATE_RESET))
3063 break;
3064 spin_unlock_irqrestore(hba->host->host_lock, flags);
3065 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303066 flush_work(&hba->eh_work);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303067 } while (1);
3068
3069 hba->ufshcd_state = UFSHCD_STATE_RESET;
3070 ufshcd_set_eh_in_progress(hba);
3071 spin_unlock_irqrestore(hba->host->host_lock, flags);
3072
3073 err = ufshcd_reset_and_restore(hba);
3074
3075 spin_lock_irqsave(hba->host->host_lock, flags);
3076 if (!err) {
3077 err = SUCCESS;
3078 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3079 } else {
3080 err = FAILED;
3081 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3082 }
3083 ufshcd_clear_eh_in_progress(hba);
3084 spin_unlock_irqrestore(hba->host->host_lock, flags);
3085
3086 return err;
3087}
3088
3089/**
Dolev Raviv1b3e8952014-06-29 09:40:19 +03003090 * ufshcd_read_sdev_qdepth - read the lun command queue depth
3091 * @hba: Pointer to adapter instance
3092 * @sdev: pointer to SCSI device
3093 *
3094 * Return in case of success the lun's queue depth else error.
3095 */
3096static int ufshcd_read_sdev_qdepth(struct ufs_hba *hba,
3097 struct scsi_device *sdev)
3098{
3099 int ret;
3100 int buff_len = UNIT_DESC_MAX_SIZE;
3101 u8 desc_buf[UNIT_DESC_MAX_SIZE];
3102
3103 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
3104 QUERY_DESC_IDN_UNIT, sdev->lun, 0, desc_buf, &buff_len);
3105
3106 if (ret || (buff_len < UNIT_DESC_PARAM_LU_Q_DEPTH)) {
3107 dev_err(hba->dev,
3108 "%s:Failed reading unit descriptor. len = %d ret = %d"
3109 , __func__, buff_len, ret);
3110 if (!ret)
3111 ret = -EINVAL;
3112
3113 goto out;
3114 }
3115
3116 ret = desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH] & 0xFF;
3117out:
3118 return ret;
3119}
3120
3121/**
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303122 * ufshcd_async_scan - asynchronous execution for link startup
3123 * @data: data pointer to pass to this function
3124 * @cookie: cookie data
3125 */
3126static void ufshcd_async_scan(void *data, async_cookie_t cookie)
3127{
3128 struct ufs_hba *hba = (struct ufs_hba *)data;
3129 int ret;
3130
3131 ret = ufshcd_link_startup(hba);
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303132 if (ret)
3133 goto out;
3134
Seungwon Jeond3e89ba2013-08-31 21:40:24 +05303135 ufshcd_config_max_pwr_mode(hba);
3136
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303137 ret = ufshcd_verify_dev_init(hba);
3138 if (ret)
3139 goto out;
3140
Dolev Raviv68078d52013-07-30 00:35:58 +05303141 ret = ufshcd_complete_dev_init(hba);
3142 if (ret)
3143 goto out;
3144
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303145 ufshcd_force_reset_auto_bkops(hba);
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303146 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3147
3148 /* If we are in error handling context no need to scan the host */
3149 if (!ufshcd_eh_in_progress(hba)) {
3150 scsi_scan_host(hba->host);
3151 pm_runtime_put_sync(hba->dev);
3152 }
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303153out:
3154 return;
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303155}
3156
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303157static struct scsi_host_template ufshcd_driver_template = {
3158 .module = THIS_MODULE,
3159 .name = UFSHCD,
3160 .proc_name = UFSHCD,
3161 .queuecommand = ufshcd_queuecommand,
3162 .slave_alloc = ufshcd_slave_alloc,
Akinobu Mitaeeda4742014-07-01 23:00:32 +09003163 .slave_configure = ufshcd_slave_configure,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303164 .slave_destroy = ufshcd_slave_destroy,
Sujit Reddy Thumma4264fd62014-06-29 09:40:20 +03003165 .change_queue_depth = ufshcd_change_queue_depth,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303166 .eh_abort_handler = ufshcd_abort,
Sujit Reddy Thumma3441da72014-05-26 10:59:14 +05303167 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
3168 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303169 .this_id = -1,
3170 .sg_tablesize = SG_ALL,
3171 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
3172 .can_queue = UFSHCD_CAN_QUEUE,
3173};
3174
3175/**
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303176 * ufshcd_suspend - suspend power management function
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303177 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303178 * @state: power state
3179 *
3180 * Returns -ENOSYS
3181 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303182int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303183{
3184 /*
3185 * TODO:
3186 * 1. Block SCSI requests from SCSI midlayer
3187 * 2. Change the internal driver state to non operational
3188 * 3. Set UTRLRSR and UTMRLRSR bits to zero
3189 * 4. Wait until outstanding commands are completed
3190 * 5. Set HCE to zero to send the UFS host controller to reset state
3191 */
3192
3193 return -ENOSYS;
3194}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303195EXPORT_SYMBOL_GPL(ufshcd_suspend);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303196
3197/**
3198 * ufshcd_resume - resume power management function
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303199 * @hba: per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303200 *
3201 * Returns -ENOSYS
3202 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303203int ufshcd_resume(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303204{
3205 /*
3206 * TODO:
3207 * 1. Set HCE to 1, to start the UFS host controller
3208 * initialization process
3209 * 2. Set UTRLRSR and UTMRLRSR bits to 1
3210 * 3. Change the internal driver state to operational
3211 * 4. Unblock SCSI requests from SCSI midlayer
3212 */
3213
3214 return -ENOSYS;
3215}
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303216EXPORT_SYMBOL_GPL(ufshcd_resume);
3217
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303218int ufshcd_runtime_suspend(struct ufs_hba *hba)
3219{
3220 if (!hba)
3221 return 0;
3222
3223 /*
3224 * The device is idle with no requests in the queue,
3225 * allow background operations.
3226 */
3227 return ufshcd_enable_auto_bkops(hba);
3228}
3229EXPORT_SYMBOL(ufshcd_runtime_suspend);
3230
3231int ufshcd_runtime_resume(struct ufs_hba *hba)
3232{
3233 if (!hba)
3234 return 0;
3235
3236 return ufshcd_disable_auto_bkops(hba);
3237}
3238EXPORT_SYMBOL(ufshcd_runtime_resume);
3239
3240int ufshcd_runtime_idle(struct ufs_hba *hba)
3241{
3242 return 0;
3243}
3244EXPORT_SYMBOL(ufshcd_runtime_idle);
3245
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303246/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303247 * ufshcd_remove - de-allocate SCSI host and host memory space
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303248 * data structure memory
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303249 * @hba - per adapter instance
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303250 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303251void ufshcd_remove(struct ufs_hba *hba)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303252{
Akinobu Mitacfdf9c92013-07-30 00:36:03 +05303253 scsi_remove_host(hba->host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303254 /* disable interrupts */
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05303255 ufshcd_disable_intr(hba, hba->intr_mask);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303256 ufshcd_hba_stop(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303257
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303258 scsi_host_put(hba->host);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303259}
3260EXPORT_SYMBOL_GPL(ufshcd_remove);
3261
3262/**
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09003263 * ufshcd_set_dma_mask - Set dma mask based on the controller
3264 * addressing capability
3265 * @hba: per adapter instance
3266 *
3267 * Returns 0 for success, non-zero for failure
3268 */
3269static int ufshcd_set_dma_mask(struct ufs_hba *hba)
3270{
3271 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
3272 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
3273 return 0;
3274 }
3275 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
3276}
3277
3278/**
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303279 * ufshcd_init - Driver initialization routine
3280 * @dev: pointer to device handle
3281 * @hba_handle: driver private handle
3282 * @mmio_base: base register address
3283 * @irq: Interrupt line of device
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303284 * Returns 0 on success, non-zero value on failure
3285 */
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303286int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle,
3287 void __iomem *mmio_base, unsigned int irq)
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303288{
3289 struct Scsi_Host *host;
3290 struct ufs_hba *hba;
3291 int err;
3292
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303293 if (!dev) {
3294 dev_err(dev,
3295 "Invalid memory reference for dev is NULL\n");
3296 err = -ENODEV;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303297 goto out_error;
3298 }
3299
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303300 if (!mmio_base) {
3301 dev_err(dev,
3302 "Invalid memory reference for mmio_base is NULL\n");
3303 err = -ENODEV;
3304 goto out_error;
3305 }
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303306
3307 host = scsi_host_alloc(&ufshcd_driver_template,
3308 sizeof(struct ufs_hba));
3309 if (!host) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303310 dev_err(dev, "scsi_host_alloc failed\n");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303311 err = -ENOMEM;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303312 goto out_error;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303313 }
3314 hba = shost_priv(host);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303315 hba->host = host;
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303316 hba->dev = dev;
3317 hba->mmio_base = mmio_base;
3318 hba->irq = irq;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303319
3320 /* Read capabilities registers */
3321 ufshcd_hba_capabilities(hba);
3322
3323 /* Get UFS version supported by the controller */
3324 hba->ufs_version = ufshcd_get_ufs_version(hba);
3325
Seungwon Jeon2fbd0092013-06-26 22:39:27 +05303326 /* Get Interrupt bit mask per version */
3327 hba->intr_mask = ufshcd_get_intr_mask(hba);
3328
Akinobu Mitaca3d7bf2014-07-13 21:24:46 +09003329 err = ufshcd_set_dma_mask(hba);
3330 if (err) {
3331 dev_err(hba->dev, "set dma mask failed\n");
3332 goto out_disable;
3333 }
3334
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303335 /* Allocate memory for host memory space */
3336 err = ufshcd_memory_alloc(hba);
3337 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303338 dev_err(hba->dev, "Memory allocation failed\n");
3339 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303340 }
3341
3342 /* Configure LRB */
3343 ufshcd_host_memory_configure(hba);
3344
3345 host->can_queue = hba->nutrs;
3346 host->cmd_per_lun = hba->nutrs;
3347 host->max_id = UFSHCD_MAX_ID;
3348 host->max_lun = UFSHCD_MAX_LUNS;
3349 host->max_channel = UFSHCD_MAX_CHANNEL;
3350 host->unique_id = host->host_no;
3351 host->max_cmd_len = MAX_CDB_SIZE;
3352
3353 /* Initailize wait queue for task management */
Sujit Reddy Thummae2933132014-05-26 10:59:12 +05303354 init_waitqueue_head(&hba->tm_wq);
3355 init_waitqueue_head(&hba->tm_tag_wq);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303356
3357 /* Initialize work queues */
Sujit Reddy Thummae8e7f272014-05-26 10:59:15 +05303358 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
Sujit Reddy Thumma66ec6d52013-07-30 00:35:59 +05303359 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303360
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303361 /* Initialize UIC command mutex */
3362 mutex_init(&hba->uic_cmd_mutex);
3363
Sujit Reddy Thumma5a0b0cb2013-07-30 00:35:57 +05303364 /* Initialize mutex for device management commands */
3365 mutex_init(&hba->dev_cmd.lock);
3366
3367 /* Initialize device management tag acquire wait queue */
3368 init_waitqueue_head(&hba->dev_cmd.tag_wq);
3369
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303370 /* IRQ registration */
Seungwon Jeon2953f852013-06-27 13:31:54 +09003371 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303372 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303373 dev_err(hba->dev, "request irq failed\n");
Seungwon Jeon2953f852013-06-27 13:31:54 +09003374 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303375 }
3376
3377 /* Enable SCSI tag mapping */
3378 err = scsi_init_shared_tag_map(host, host->can_queue);
3379 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303380 dev_err(hba->dev, "init shared queue failed\n");
Seungwon Jeon2953f852013-06-27 13:31:54 +09003381 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303382 }
3383
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303384 err = scsi_add_host(host, hba->dev);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303385 if (err) {
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303386 dev_err(hba->dev, "scsi_add_host failed\n");
Seungwon Jeon2953f852013-06-27 13:31:54 +09003387 goto out_disable;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303388 }
3389
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303390 /* Host controller enable */
3391 err = ufshcd_hba_enable(hba);
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303392 if (err) {
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303393 dev_err(hba->dev, "Host controller enable failed\n");
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303394 goto out_remove_scsi_host;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303395 }
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303396
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303397 *hba_handle = hba;
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303398
Sujit Reddy Thumma62694732013-07-30 00:36:00 +05303399 /* Hold auto suspend until async scan completes */
3400 pm_runtime_get_sync(dev);
3401
Seungwon Jeon6ccf44fe2013-06-26 22:39:29 +05303402 async_schedule(ufshcd_async_scan, hba);
3403
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303404 return 0;
3405
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303406out_remove_scsi_host:
3407 scsi_remove_host(hba->host);
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303408out_disable:
3409 scsi_host_put(host);
3410out_error:
3411 return err;
3412}
3413EXPORT_SYMBOL_GPL(ufshcd_init);
3414
Vinayak Holikatti3b1d0582013-02-25 21:44:32 +05303415MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
3416MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
Vinayak Holikattie0eca632013-02-25 21:44:33 +05303417MODULE_DESCRIPTION("Generic UFS host controller driver Core");
Santosh Yaraganavi7a3e97b2012-02-29 12:11:50 +05303418MODULE_LICENSE("GPL");
3419MODULE_VERSION(UFSHCD_DRIVER_VERSION);