Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1 | /* |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 2 | * Universal Flash Storage Host controller driver Core |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3 | * |
| 4 | * This code is based on drivers/scsi/ufs/ufshcd.c |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 5 | * Copyright (C) 2011-2013 Samsung India Software Operations |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 6 | * |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 7 | * Authors: |
| 8 | * Santosh Yaraganavi <santosh.sy@samsung.com> |
| 9 | * Vinayak Holikatti <h.vinayak@samsung.com> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 10 | * |
| 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 Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 15 | * See the COPYING file in the top-level directory or visit |
| 16 | * <http://www.gnu.org/licenses/gpl-2.0.html> |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 17 | * |
| 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 Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 23 | * 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 34 | */ |
| 35 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 36 | #include <linux/async.h> |
| 37 | |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 38 | #include "ufshcd.h" |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 39 | #include "unipro.h" |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 40 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 41 | #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\ |
| 42 | UTP_TASK_REQ_COMPL |\ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 43 | UIC_POWER_MODE |\ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 44 | UFSHCD_ERROR_MASK) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 45 | /* UIC command timeout, unit: ms */ |
| 46 | #define UIC_CMD_TIMEOUT 500 |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 47 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 48 | /* 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 53 | /* Query request retries */ |
| 54 | #define QUERY_REQ_RETRIES 10 |
| 55 | /* Query request timeout */ |
| 56 | #define QUERY_REQ_TIMEOUT 30 /* msec */ |
| 57 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 58 | /* Task management command timeout */ |
| 59 | #define TM_CMD_TIMEOUT 100 /* msecs */ |
| 60 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 61 | /* Expose the flag value from utp_upiu_query.value */ |
| 62 | #define MASK_QUERY_UPIU_FLAG_LOC 0xFF |
| 63 | |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 64 | /* Interrupt aggregation default timeout, unit: 40us */ |
| 65 | #define INT_AGGR_DEF_TO 0x02 |
| 66 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 67 | enum { |
| 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 */ |
| 76 | enum { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 77 | UFSHCD_STATE_RESET, |
| 78 | UFSHCD_STATE_ERROR, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 79 | UFSHCD_STATE_OPERATIONAL, |
| 80 | }; |
| 81 | |
| 82 | /* UFSHCD error handling flags */ |
| 83 | enum { |
| 84 | UFSHCD_EH_IN_PROGRESS = (1 << 0), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 85 | }; |
| 86 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 87 | /* UFSHCD UIC layer error flags */ |
| 88 | enum { |
| 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 95 | /* Interrupt configuration options */ |
| 96 | enum { |
| 97 | UFSHCD_INT_DISABLE, |
| 98 | UFSHCD_INT_ENABLE, |
| 99 | UFSHCD_INT_CLEAR, |
| 100 | }; |
| 101 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 102 | #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 | |
| 109 | static void ufshcd_tmc_handler(struct ufs_hba *hba); |
| 110 | static void ufshcd_async_scan(void *data, async_cookie_t cookie); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 111 | static int ufshcd_reset_and_restore(struct ufs_hba *hba); |
| 112 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag); |
Dolev Raviv | 1b3e895 | 2014-06-29 09:40:19 +0300 | [diff] [blame] | 113 | static int ufshcd_read_sdev_qdepth(struct ufs_hba *hba, |
| 114 | struct scsi_device *sdev); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 115 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 116 | /* |
| 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 | */ |
| 127 | static 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 150 | /** |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 151 | * ufshcd_get_intr_mask - Get the interrupt bit mask |
| 152 | * @hba - Pointer to adapter instance |
| 153 | * |
| 154 | * Returns interrupt bit mask per version |
| 155 | */ |
| 156 | static 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 165 | * 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 | */ |
| 170 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 171 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 172 | return ufshcd_readl(hba, REG_UFS_VERSION); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 173 | } |
| 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 S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 180 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 181 | */ |
| 182 | static inline int ufshcd_is_device_present(u32 reg_hcs) |
| 183 | { |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 184 | return (DEVICE_PRESENT & reg_hcs) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 185 | } |
| 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 | */ |
| 194 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 195 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 196 | return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 197 | } |
| 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 | */ |
| 206 | static inline int |
| 207 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 208 | { |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 209 | return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 214 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 215 | * @free_slot: pointer to variable with available slot value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 216 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 217 | * 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 220 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 221 | static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 222 | { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 223 | 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; |
| 237 | out: |
| 238 | return ret; |
| 239 | } |
| 240 | |
| 241 | static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot) |
| 242 | { |
| 243 | clear_bit_unlock(slot, &hba->tm_slots_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 244 | } |
| 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 | */ |
| 251 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 252 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 253 | ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 254 | } |
| 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 | */ |
| 262 | static 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 | */ |
| 285 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 286 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 287 | return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 288 | MASK_UIC_COMMAND_RESULT; |
| 289 | } |
| 290 | |
| 291 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 292 | * 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 | */ |
| 298 | static 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 Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 304 | * ufshcd_get_req_rsp - returns the TR response transaction type |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 305 | * @ucd_rsp_ptr: pointer to response UPIU |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 306 | */ |
| 307 | static inline int |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 308 | ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 309 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 310 | return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 311 | } |
| 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 | */ |
| 320 | static inline int |
| 321 | ufshcd_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 Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 326 | /* |
| 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 | */ |
| 333 | static inline unsigned int |
| 334 | ufshcd_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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 340 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 341 | * 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 | */ |
| 349 | static 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 Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 356 | * ufshcd_reset_intr_aggr - Reset interrupt aggregation values. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 357 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 358 | */ |
| 359 | static inline void |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 360 | ufshcd_reset_intr_aggr(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 361 | { |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 362 | 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 | */ |
| 373 | static inline void |
| 374 | ufshcd_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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 380 | } |
| 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 | */ |
| 388 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 389 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 390 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 397 | * ufshcd_hba_start - Start controller initialization sequence |
| 398 | * @hba: per adapter instance |
| 399 | */ |
| 400 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 401 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 402 | ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 403 | } |
| 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 | */ |
| 411 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 412 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 413 | return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 414 | } |
| 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 | */ |
| 421 | static inline |
| 422 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 423 | { |
| 424 | __set_bit(task_tag, &hba->outstanding_reqs); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 425 | ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | /** |
| 429 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 430 | * @lrb - pointer to local reference block |
| 431 | */ |
| 432 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 433 | { |
| 434 | int len; |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 435 | if (lrbp->sense_buffer && |
| 436 | ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 437 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 438 | memcpy(lrbp->sense_buffer, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 439 | lrbp->ucd_rsp_ptr->sr.sense_data, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 440 | min_t(int, len, SCSI_SENSE_BUFFERSIZE)); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | /** |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 445 | * 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 | */ |
| 450 | static |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 451 | int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 452 | { |
| 453 | struct ufs_query_res *query_res = &hba->dev_cmd.query.response; |
| 454 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 455 | memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 456 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 457 | /* Get the descriptor */ |
| 458 | if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 459 | u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 460 | GENERAL_UPIU_REQUEST_SIZE; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 461 | u16 resp_len; |
| 462 | u16 buf_len; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 463 | |
| 464 | /* data segment length */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 465 | resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 466 | MASK_QUERY_DATA_SEG_LEN; |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 467 | 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 476 | } |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 477 | |
| 478 | return 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 482 | * ufshcd_hba_capabilities - Read controller capabilities |
| 483 | * @hba: per adapter instance |
| 484 | */ |
| 485 | static inline void ufshcd_hba_capabilities(struct ufs_hba *hba) |
| 486 | { |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 487 | hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 488 | |
| 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 Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 496 | * ufshcd_ready_for_uic_cmd - Check if controller is ready |
| 497 | * to accept UIC commands |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 498 | * @hba: per adapter instance |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 499 | * Return true on success, else false |
| 500 | */ |
| 501 | static 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 Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 510 | * 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 | */ |
| 516 | static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) |
| 517 | { |
| 518 | return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; |
| 519 | } |
| 520 | |
| 521 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 522 | * 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 527 | */ |
| 528 | static inline void |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 529 | ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 530 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 531 | WARN_ON(hba->active_uic_cmd); |
| 532 | |
| 533 | hba->active_uic_cmd = uic_cmd; |
| 534 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 535 | /* Write Args */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 536 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 539 | |
| 540 | /* Write UIC Cmd */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 541 | ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 542 | REG_UIC_COMMAND); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 546 | * 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 | */ |
| 553 | static int |
| 554 | ufshcd_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 | */ |
| 581 | static 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 | */ |
| 611 | static int |
| 612 | ufshcd_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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 624 | * 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 | */ |
| 629 | static 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 Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 664 | * ufshcd_enable_intr - enable interrupts |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 665 | * @hba: per adapter instance |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 666 | * @intrs: interrupt bits |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 667 | */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 668 | static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 669 | { |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 670 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 678 | } |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 679 | |
| 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 | */ |
| 688 | static 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 706 | * 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 | */ |
| 712 | static 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 | */ |
| 753 | static |
| 754 | void 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 775 | /** |
| 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 | */ |
| 782 | static 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 Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 787 | u16 len = be16_to_cpu(query->request.upiu_req.length); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 788 | 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 804 | |
| 805 | /* Copy the Descriptor */ |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 806 | if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) |
| 807 | memcpy(descp, query->descriptor, len); |
| 808 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 809 | } |
| 810 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 811 | static 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 824 | * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU) |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 825 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 826 | * @lrb - pointer to local reference block |
| 827 | */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 828 | static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 829 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 830 | u32 upiu_flags; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 831 | int ret = 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 832 | |
| 833 | switch (lrbp->command_type) { |
| 834 | case UTP_CMD_TYPE_SCSI: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 835 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 839 | } else { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 840 | ret = -EINVAL; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 841 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 842 | break; |
| 843 | case UTP_CMD_TYPE_DEV_MANAGE: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 844 | ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 845 | 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 Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 849 | ufshcd_prepare_utp_nop_upiu(lrbp); |
| 850 | else |
| 851 | ret = -EINVAL; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 852 | break; |
| 853 | case UTP_CMD_TYPE_UFS: |
| 854 | /* For UFS native command implementation */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 855 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 863 | break; |
| 864 | } /* end of switch */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 865 | |
| 866 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 867 | } |
| 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 | */ |
| 876 | static 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 Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 888 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 893 | err = SCSI_MLQUEUE_HOST_BUSY; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 894 | 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 905 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 906 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 907 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 908 | /* 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 920 | lrbp = &hba->lrb[tag]; |
| 921 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 922 | WARN_ON(lrbp->cmd); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 923 | 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 Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 928 | lrbp->intr_cmd = false; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 929 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 930 | |
| 931 | /* form UPIU before issuing the command */ |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 932 | ufshcd_compose_upiu(hba, lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 933 | err = ufshcd_map_sg(lrbp); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 934 | if (err) { |
| 935 | lrbp->cmd = NULL; |
| 936 | clear_bit_unlock(tag, &hba->lrb_in_use); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 937 | goto out; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 938 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 939 | |
| 940 | /* issue command to the controller */ |
| 941 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 942 | ufshcd_send_command(hba, tag); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 943 | out_unlock: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 944 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 945 | out: |
| 946 | return err; |
| 947 | } |
| 948 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 949 | static 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 | |
| 964 | static int |
| 965 | ufshcd_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 Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 987 | static int |
| 988 | ufshcd_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 Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 998 | /** |
| 999 | * ufshcd_dev_cmd_completion() - handles device management command responses |
| 1000 | * @hba: per adapter instance |
| 1001 | * @lrbp: pointer to local reference block |
| 1002 | */ |
| 1003 | static int |
| 1004 | ufshcd_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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1019 | case UPIU_TRANSACTION_QUERY_RSP: |
Dolev Raviv | c6d4a83 | 2014-06-29 09:40:18 +0300 | [diff] [blame] | 1020 | err = ufshcd_check_query_response(hba, lrbp); |
| 1021 | if (!err) |
| 1022 | err = ufshcd_copy_query_response(hba, lrbp); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1023 | break; |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1024 | 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 | |
| 1040 | static 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 | */ |
| 1080 | static 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; |
| 1098 | out: |
| 1099 | return ret; |
| 1100 | } |
| 1101 | |
| 1102 | static 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1113 | * 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 Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1115 | */ |
| 1116 | static 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 | |
| 1147 | out_put_tag: |
| 1148 | ufshcd_put_dev_cmd_tag(hba, tag); |
| 1149 | wake_up(&hba->dev_cmd.tag_wq); |
| 1150 | return err; |
| 1151 | } |
| 1152 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1153 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1154 | * 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 | */ |
| 1163 | static 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1178 | * 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 | */ |
| 1186 | static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, |
| 1187 | enum flag_idn idn, bool *flag_res) |
| 1188 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1189 | struct ufs_query_req *request = NULL; |
| 1190 | struct ufs_query_res *response = NULL; |
| 1191 | int err, index = 0, selector = 0; |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1192 | |
| 1193 | BUG_ON(!hba); |
| 1194 | |
| 1195 | mutex_lock(&hba->dev_cmd.lock); |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1196 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 1197 | selector); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1198 | |
| 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1222 | |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1223 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1224 | |
| 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 Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1233 | *flag_res = (be32_to_cpu(response->upiu_res.value) & |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1234 | MASK_QUERY_UPIU_FLAG_LOC) & 0x1; |
| 1235 | |
| 1236 | out_unlock: |
| 1237 | mutex_unlock(&hba->dev_cmd.lock); |
| 1238 | return err; |
| 1239 | } |
| 1240 | |
| 1241 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1242 | * 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 Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 1252 | static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1253 | enum attr_idn idn, u8 index, u8 selector, u32 *attr_val) |
| 1254 | { |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1255 | struct ufs_query_req *request = NULL; |
| 1256 | struct ufs_query_res *response = NULL; |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1257 | 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 Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1269 | ufshcd_init_query(hba, &request, &response, opcode, idn, index, |
| 1270 | selector); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1271 | |
| 1272 | switch (opcode) { |
| 1273 | case UPIU_QUERY_OPCODE_WRITE_ATTR: |
| 1274 | request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; |
Sujit Reddy Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1275 | request->upiu_req.value = cpu_to_be32(*attr_val); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1276 | 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 Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1287 | err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1288 | |
| 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 Thumma | e8c8e82 | 2014-05-26 10:59:10 +0530 | [diff] [blame] | 1295 | *attr_val = be32_to_cpu(response->upiu_res.value); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 1296 | |
| 1297 | out_unlock: |
| 1298 | mutex_unlock(&hba->dev_cmd.lock); |
| 1299 | out: |
| 1300 | return err; |
| 1301 | } |
| 1302 | |
| 1303 | /** |
Dolev Raviv | d44a5f9 | 2014-06-29 09:40:17 +0300 | [diff] [blame] | 1304 | * 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 | */ |
| 1317 | int ufshcd_query_descriptor(struct ufs_hba *hba, |
| 1318 | 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 | |
| 1373 | out_unlock: |
| 1374 | mutex_unlock(&hba->dev_cmd.lock); |
| 1375 | out: |
| 1376 | return err; |
| 1377 | } |
| 1378 | |
| 1379 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1380 | * 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 | */ |
| 1392 | static 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 Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 1398 | hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 1399 | ucdl_size, |
| 1400 | &hba->ucdl_dma_addr, |
| 1401 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1402 | |
| 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 Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1411 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1412 | "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 Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 1421 | hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 1422 | utrdl_size, |
| 1423 | &hba->utrdl_dma_addr, |
| 1424 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1425 | if (!hba->utrdl_base_addr || |
| 1426 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1427 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1428 | "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 Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 1437 | hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, |
| 1438 | utmrdl_size, |
| 1439 | &hba->utmrdl_dma_addr, |
| 1440 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1441 | if (!hba->utmrdl_base_addr || |
| 1442 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1443 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1444 | "Task Management Descriptor Memory allocation failed\n"); |
| 1445 | goto out; |
| 1446 | } |
| 1447 | |
| 1448 | /* Allocate memory for local reference block */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 1449 | hba->lrb = devm_kzalloc(hba->dev, |
| 1450 | hba->nutrs * sizeof(struct ufshcd_lrb), |
| 1451 | GFP_KERNEL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1452 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1453 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1454 | goto out; |
| 1455 | } |
| 1456 | return 0; |
| 1457 | out: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1458 | 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 | */ |
| 1474 | static 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 Thumma | 3ca316c | 2013-06-26 22:39:30 +0530 | [diff] [blame] | 1511 | cpu_to_le16(ALIGNED_UPIU_SIZE >> 2); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1512 | |
| 1513 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1514 | hba->lrb[i].ucd_req_ptr = |
| 1515 | (struct utp_upiu_req *)(cmd_descp + i); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1516 | 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 | */ |
| 1534 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 1535 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1536 | struct uic_command uic_cmd = {0}; |
| 1537 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1538 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1539 | uic_cmd.command = UIC_CMD_DME_LINK_STARTUP; |
| 1540 | |
| 1541 | ret = ufshcd_send_uic_cmd(hba, &uic_cmd); |
| 1542 | if (ret) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1543 | dev_err(hba->dev, |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1544 | "dme-link-startup: error code %d\n", ret); |
| 1545 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1546 | } |
| 1547 | |
| 1548 | /** |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 1549 | * 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 | */ |
| 1558 | int 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 | } |
| 1582 | EXPORT_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 | */ |
| 1593 | int 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; |
| 1617 | out: |
| 1618 | return ret; |
| 1619 | } |
| 1620 | EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr); |
| 1621 | |
| 1622 | /** |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 1623 | * 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 Thumma | bdbe5d2 | 2014-05-26 10:59:11 +0530 | [diff] [blame] | 1630 | static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode) |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 1631 | { |
| 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 | } |
| 1672 | out: |
| 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 Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 1681 | * 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 | */ |
| 1687 | static 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 Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 1745 | * ufshcd_complete_dev_init() - checks device readiness |
| 1746 | * hba: per-adapter instance |
| 1747 | * |
| 1748 | * Set fDeviceInit flag and poll until device toggles it. |
| 1749 | */ |
| 1750 | static 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 | |
| 1791 | out: |
| 1792 | return err; |
| 1793 | } |
| 1794 | |
| 1795 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1796 | * 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 Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1801 | * 2. Enable required interrupts |
| 1802 | * 3. Configure interrupt aggregation |
| 1803 | * 4. Program UTRL and UTMRL base addres |
| 1804 | * 5. Configure run-stop-registers |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1805 | * |
| 1806 | * Returns 0 on success, non-zero value on failure |
| 1807 | */ |
| 1808 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 1809 | { |
| 1810 | int err = 0; |
| 1811 | u32 reg; |
| 1812 | |
| 1813 | /* check if device present */ |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 1814 | reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 1815 | if (!ufshcd_is_device_present(reg)) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1816 | dev_err(hba->dev, "cc: Device not present\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1817 | err = -ENXIO; |
| 1818 | goto out; |
| 1819 | } |
| 1820 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1821 | /* Enable required interrupts */ |
| 1822 | ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); |
| 1823 | |
| 1824 | /* Configure interrupt aggregation */ |
Seungwon Jeon | 7d56865 | 2013-08-31 21:40:20 +0530 | [diff] [blame] | 1825 | ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1826 | |
| 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1837 | /* |
| 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 Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1844 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1845 | "Host controller not ready to process requests"); |
| 1846 | err = -EIO; |
| 1847 | goto out; |
| 1848 | } |
| 1849 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1850 | out: |
| 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 | */ |
| 1864 | static 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 Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 1908 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1909 | "Controller enable failed\n"); |
| 1910 | return -EIO; |
| 1911 | } |
| 1912 | msleep(5); |
| 1913 | } |
| 1914 | return 0; |
| 1915 | } |
| 1916 | |
| 1917 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1918 | * ufshcd_link_startup - Initialize unipro link startup |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1919 | * @hba: per adapter instance |
| 1920 | * |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1921 | * Returns 0 for success, non-zero in case of failure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1922 | */ |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1923 | static int ufshcd_link_startup(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1924 | { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1925 | int ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1926 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1927 | /* enable UIC related interrupts */ |
| 1928 | ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1929 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 1930 | ret = ufshcd_dme_link_startup(hba); |
| 1931 | if (ret) |
| 1932 | goto out; |
| 1933 | |
| 1934 | ret = ufshcd_make_hba_operational(hba); |
| 1935 | |
| 1936 | out: |
| 1937 | if (ret) |
| 1938 | dev_err(hba->dev, "link startup failed %d\n", ret); |
| 1939 | return ret; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /** |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 1943 | * 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 | */ |
| 1952 | static 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 Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1975 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 1976 | * @sdev: pointer to SCSI device |
| 1977 | * |
| 1978 | * Returns success |
| 1979 | */ |
| 1980 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 1981 | { |
| 1982 | struct ufs_hba *hba; |
Dolev Raviv | 1b3e895 | 2014-06-29 09:40:19 +0300 | [diff] [blame] | 1983 | int lun_qdepth; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1984 | |
| 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 Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 1992 | /* allow SCSI layer to restart the device in case of errors */ |
| 1993 | sdev->allow_restart = 1; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 1994 | |
Sujit Reddy Thumma | b2a6c52 | 2014-07-01 12:22:38 +0300 | [diff] [blame^] | 1995 | /* REPORT SUPPORTED OPERATION CODES is not supported */ |
| 1996 | sdev->no_report_opcodes = 1; |
| 1997 | |
Dolev Raviv | 1b3e895 | 2014-06-29 09:40:19 +0300 | [diff] [blame] | 1998 | lun_qdepth = ufshcd_read_sdev_qdepth(hba, sdev); |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 1999 | if (lun_qdepth <= 0) |
| 2000 | /* eventually, we can figure out the real queue depth */ |
Dolev Raviv | 1b3e895 | 2014-06-29 09:40:19 +0300 | [diff] [blame] | 2001 | lun_qdepth = hba->nutrs; |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 2002 | 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 | */ |
| 2021 | int ufshcd_change_queue_depth(struct scsi_device *sdev, int depth, int reason) |
| 2022 | { |
| 2023 | struct ufs_hba *hba = shost_priv(sdev->host); |
| 2024 | |
| 2025 | if (depth > hba->nutrs) |
| 2026 | depth = hba->nutrs; |
| 2027 | |
| 2028 | switch (reason) { |
| 2029 | case SCSI_QDEPTH_DEFAULT: |
| 2030 | case SCSI_QDEPTH_RAMP_UP: |
| 2031 | if (!sdev->tagged_supported) |
| 2032 | depth = 1; |
| 2033 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); |
| 2034 | break; |
| 2035 | case SCSI_QDEPTH_QFULL: |
| 2036 | scsi_track_queue_full(sdev, depth); |
| 2037 | break; |
| 2038 | default: |
| 2039 | return -EOPNOTSUPP; |
Dolev Raviv | 1b3e895 | 2014-06-29 09:40:19 +0300 | [diff] [blame] | 2040 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2041 | |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 2042 | return depth; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | /** |
| 2046 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 2047 | * @sdev: pointer to SCSI device |
| 2048 | */ |
| 2049 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 2050 | { |
| 2051 | struct ufs_hba *hba; |
| 2052 | |
| 2053 | hba = shost_priv(sdev->host); |
| 2054 | scsi_deactivate_tcq(sdev, hba->nutrs); |
| 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | * ufshcd_task_req_compl - handle task management request completion |
| 2059 | * @hba: per adapter instance |
| 2060 | * @index: index of the completed request |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2061 | * @resp: task management service response |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2062 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2063 | * Returns non-zero value on error, zero on success |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2064 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2065 | static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2066 | { |
| 2067 | struct utp_task_req_desc *task_req_descp; |
| 2068 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 2069 | unsigned long flags; |
| 2070 | int ocs_value; |
| 2071 | int task_result; |
| 2072 | |
| 2073 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2074 | |
| 2075 | /* Clear completed tasks from outstanding_tasks */ |
| 2076 | __clear_bit(index, &hba->outstanding_tasks); |
| 2077 | |
| 2078 | task_req_descp = hba->utmrdl_base_addr; |
| 2079 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 2080 | |
| 2081 | if (ocs_value == OCS_SUCCESS) { |
| 2082 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 2083 | task_req_descp[index].task_rsp_upiu; |
| 2084 | task_result = be32_to_cpu(task_rsp_upiup->header.dword_1); |
| 2085 | task_result = ((task_result & MASK_TASK_RESPONSE) >> 8); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2086 | if (resp) |
| 2087 | *resp = (u8)task_result; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2088 | } else { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2089 | dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", |
| 2090 | __func__, ocs_value); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2091 | } |
| 2092 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2093 | |
| 2094 | return ocs_value; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2095 | } |
| 2096 | |
| 2097 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2098 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 2099 | * @lrb: pointer to local reference block of completed command |
| 2100 | * @scsi_status: SCSI command status |
| 2101 | * |
| 2102 | * Returns value base on SCSI command status |
| 2103 | */ |
| 2104 | static inline int |
| 2105 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 2106 | { |
| 2107 | int result = 0; |
| 2108 | |
| 2109 | switch (scsi_status) { |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2110 | case SAM_STAT_CHECK_CONDITION: |
| 2111 | ufshcd_copy_sense_data(lrbp); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2112 | case SAM_STAT_GOOD: |
| 2113 | result |= DID_OK << 16 | |
| 2114 | COMMAND_COMPLETE << 8 | |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2115 | scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2116 | break; |
| 2117 | case SAM_STAT_TASK_SET_FULL: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2118 | case SAM_STAT_BUSY: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2119 | case SAM_STAT_TASK_ABORTED: |
Seungwon Jeon | 1c2623c | 2013-08-31 21:40:19 +0530 | [diff] [blame] | 2120 | ufshcd_copy_sense_data(lrbp); |
| 2121 | result |= scsi_status; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2122 | break; |
| 2123 | default: |
| 2124 | result |= DID_ERROR << 16; |
| 2125 | break; |
| 2126 | } /* end of switch */ |
| 2127 | |
| 2128 | return result; |
| 2129 | } |
| 2130 | |
| 2131 | /** |
| 2132 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 2133 | * @hba: per adapter instance |
| 2134 | * @lrb: pointer to local reference block of completed command |
| 2135 | * |
| 2136 | * Returns result of the command to notify SCSI midlayer |
| 2137 | */ |
| 2138 | static inline int |
| 2139 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 2140 | { |
| 2141 | int result = 0; |
| 2142 | int scsi_status; |
| 2143 | int ocs; |
| 2144 | |
| 2145 | /* overall command status of utrd */ |
| 2146 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 2147 | |
| 2148 | switch (ocs) { |
| 2149 | case OCS_SUCCESS: |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2150 | result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2151 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2152 | switch (result) { |
| 2153 | case UPIU_TRANSACTION_RESPONSE: |
| 2154 | /* |
| 2155 | * get the response UPIU result to extract |
| 2156 | * the SCSI command status |
| 2157 | */ |
| 2158 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 2159 | |
| 2160 | /* |
| 2161 | * get the result based on SCSI status response |
| 2162 | * to notify the SCSI midlayer of the command status |
| 2163 | */ |
| 2164 | scsi_status = result & MASK_SCSI_STATUS; |
| 2165 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2166 | |
| 2167 | if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) |
| 2168 | schedule_work(&hba->eeh_work); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2169 | break; |
| 2170 | case UPIU_TRANSACTION_REJECT_UPIU: |
| 2171 | /* TODO: handle Reject UPIU Response */ |
| 2172 | result = DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2173 | dev_err(hba->dev, |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2174 | "Reject UPIU not fully implemented\n"); |
| 2175 | break; |
| 2176 | default: |
| 2177 | result = DID_ERROR << 16; |
| 2178 | dev_err(hba->dev, |
| 2179 | "Unexpected request response code = %x\n", |
| 2180 | result); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2181 | break; |
| 2182 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2183 | break; |
| 2184 | case OCS_ABORTED: |
| 2185 | result |= DID_ABORT << 16; |
| 2186 | break; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2187 | case OCS_INVALID_COMMAND_STATUS: |
| 2188 | result |= DID_REQUEUE << 16; |
| 2189 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2190 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 2191 | case OCS_INVALID_PRDT_ATTR: |
| 2192 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 2193 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 2194 | case OCS_PEER_COMM_FAILURE: |
| 2195 | case OCS_FATAL_ERROR: |
| 2196 | default: |
| 2197 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 2198 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2199 | "OCS error from controller = %x\n", ocs); |
| 2200 | break; |
| 2201 | } /* end of switch */ |
| 2202 | |
| 2203 | return result; |
| 2204 | } |
| 2205 | |
| 2206 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2207 | * ufshcd_uic_cmd_compl - handle completion of uic command |
| 2208 | * @hba: per adapter instance |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2209 | * @intr_status: interrupt status generated by the controller |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2210 | */ |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2211 | static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2212 | { |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2213 | if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2214 | hba->active_uic_cmd->argument2 |= |
| 2215 | ufshcd_get_uic_cmd_result(hba); |
Seungwon Jeon | 12b4fdb | 2013-08-31 21:40:21 +0530 | [diff] [blame] | 2216 | hba->active_uic_cmd->argument3 = |
| 2217 | ufshcd_get_dme_attr_val(hba); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2218 | complete(&hba->active_uic_cmd->done); |
| 2219 | } |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2220 | |
| 2221 | if ((intr_status & UIC_POWER_MODE) && hba->pwr_done) |
| 2222 | complete(hba->pwr_done); |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2226 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 2227 | * @hba: per adapter instance |
| 2228 | */ |
| 2229 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 2230 | { |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2231 | struct ufshcd_lrb *lrbp; |
| 2232 | struct scsi_cmnd *cmd; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2233 | unsigned long completed_reqs; |
| 2234 | u32 tr_doorbell; |
| 2235 | int result; |
| 2236 | int index; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 2237 | |
| 2238 | /* Resetting interrupt aggregation counters first and reading the |
| 2239 | * DOOR_BELL afterward allows us to handle all the completed requests. |
| 2240 | * In order to prevent other interrupts starvation the DB is read once |
| 2241 | * after reset. The down side of this solution is the possibility of |
| 2242 | * false interrupt if device completes another request after resetting |
| 2243 | * aggregation and before reading the DB. |
| 2244 | */ |
| 2245 | ufshcd_reset_intr_aggr(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2246 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2247 | tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2248 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 2249 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 2250 | for_each_set_bit(index, &completed_reqs, hba->nutrs) { |
| 2251 | lrbp = &hba->lrb[index]; |
| 2252 | cmd = lrbp->cmd; |
| 2253 | if (cmd) { |
| 2254 | result = ufshcd_transfer_rsp_status(hba, lrbp); |
| 2255 | scsi_dma_unmap(cmd); |
| 2256 | cmd->result = result; |
| 2257 | /* Mark completed command as NULL in LRB */ |
| 2258 | lrbp->cmd = NULL; |
| 2259 | clear_bit_unlock(index, &hba->lrb_in_use); |
| 2260 | /* Do not touch lrbp after scsi done */ |
| 2261 | cmd->scsi_done(cmd); |
| 2262 | } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) { |
| 2263 | if (hba->dev_cmd.complete) |
| 2264 | complete(hba->dev_cmd.complete); |
| 2265 | } |
| 2266 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2267 | |
| 2268 | /* clear corresponding bits of completed commands */ |
| 2269 | hba->outstanding_reqs ^= completed_reqs; |
| 2270 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2271 | /* we might have free'd some tags above */ |
| 2272 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2273 | } |
| 2274 | |
| 2275 | /** |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2276 | * ufshcd_disable_ee - disable exception event |
| 2277 | * @hba: per-adapter instance |
| 2278 | * @mask: exception event to disable |
| 2279 | * |
| 2280 | * Disables exception event in the device so that the EVENT_ALERT |
| 2281 | * bit is not set. |
| 2282 | * |
| 2283 | * Returns zero on success, non-zero error value on failure. |
| 2284 | */ |
| 2285 | static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) |
| 2286 | { |
| 2287 | int err = 0; |
| 2288 | u32 val; |
| 2289 | |
| 2290 | if (!(hba->ee_ctrl_mask & mask)) |
| 2291 | goto out; |
| 2292 | |
| 2293 | val = hba->ee_ctrl_mask & ~mask; |
| 2294 | val &= 0xFFFF; /* 2 bytes */ |
| 2295 | err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 2296 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 2297 | if (!err) |
| 2298 | hba->ee_ctrl_mask &= ~mask; |
| 2299 | out: |
| 2300 | return err; |
| 2301 | } |
| 2302 | |
| 2303 | /** |
| 2304 | * ufshcd_enable_ee - enable exception event |
| 2305 | * @hba: per-adapter instance |
| 2306 | * @mask: exception event to enable |
| 2307 | * |
| 2308 | * Enable corresponding exception event in the device to allow |
| 2309 | * device to alert host in critical scenarios. |
| 2310 | * |
| 2311 | * Returns zero on success, non-zero error value on failure. |
| 2312 | */ |
| 2313 | static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) |
| 2314 | { |
| 2315 | int err = 0; |
| 2316 | u32 val; |
| 2317 | |
| 2318 | if (hba->ee_ctrl_mask & mask) |
| 2319 | goto out; |
| 2320 | |
| 2321 | val = hba->ee_ctrl_mask | mask; |
| 2322 | val &= 0xFFFF; /* 2 bytes */ |
| 2323 | err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, |
| 2324 | QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val); |
| 2325 | if (!err) |
| 2326 | hba->ee_ctrl_mask |= mask; |
| 2327 | out: |
| 2328 | return err; |
| 2329 | } |
| 2330 | |
| 2331 | /** |
| 2332 | * ufshcd_enable_auto_bkops - Allow device managed BKOPS |
| 2333 | * @hba: per-adapter instance |
| 2334 | * |
| 2335 | * Allow device to manage background operations on its own. Enabling |
| 2336 | * this might lead to inconsistent latencies during normal data transfers |
| 2337 | * as the device is allowed to manage its own way of handling background |
| 2338 | * operations. |
| 2339 | * |
| 2340 | * Returns zero on success, non-zero on failure. |
| 2341 | */ |
| 2342 | static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) |
| 2343 | { |
| 2344 | int err = 0; |
| 2345 | |
| 2346 | if (hba->auto_bkops_enabled) |
| 2347 | goto out; |
| 2348 | |
| 2349 | err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG, |
| 2350 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 2351 | if (err) { |
| 2352 | dev_err(hba->dev, "%s: failed to enable bkops %d\n", |
| 2353 | __func__, err); |
| 2354 | goto out; |
| 2355 | } |
| 2356 | |
| 2357 | hba->auto_bkops_enabled = true; |
| 2358 | |
| 2359 | /* No need of URGENT_BKOPS exception from the device */ |
| 2360 | err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 2361 | if (err) |
| 2362 | dev_err(hba->dev, "%s: failed to disable exception event %d\n", |
| 2363 | __func__, err); |
| 2364 | out: |
| 2365 | return err; |
| 2366 | } |
| 2367 | |
| 2368 | /** |
| 2369 | * ufshcd_disable_auto_bkops - block device in doing background operations |
| 2370 | * @hba: per-adapter instance |
| 2371 | * |
| 2372 | * Disabling background operations improves command response latency but |
| 2373 | * has drawback of device moving into critical state where the device is |
| 2374 | * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the |
| 2375 | * host is idle so that BKOPS are managed effectively without any negative |
| 2376 | * impacts. |
| 2377 | * |
| 2378 | * Returns zero on success, non-zero on failure. |
| 2379 | */ |
| 2380 | static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) |
| 2381 | { |
| 2382 | int err = 0; |
| 2383 | |
| 2384 | if (!hba->auto_bkops_enabled) |
| 2385 | goto out; |
| 2386 | |
| 2387 | /* |
| 2388 | * If host assisted BKOPs is to be enabled, make sure |
| 2389 | * urgent bkops exception is allowed. |
| 2390 | */ |
| 2391 | err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 2392 | if (err) { |
| 2393 | dev_err(hba->dev, "%s: failed to enable exception event %d\n", |
| 2394 | __func__, err); |
| 2395 | goto out; |
| 2396 | } |
| 2397 | |
| 2398 | err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, |
| 2399 | QUERY_FLAG_IDN_BKOPS_EN, NULL); |
| 2400 | if (err) { |
| 2401 | dev_err(hba->dev, "%s: failed to disable bkops %d\n", |
| 2402 | __func__, err); |
| 2403 | ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); |
| 2404 | goto out; |
| 2405 | } |
| 2406 | |
| 2407 | hba->auto_bkops_enabled = false; |
| 2408 | out: |
| 2409 | return err; |
| 2410 | } |
| 2411 | |
| 2412 | /** |
| 2413 | * ufshcd_force_reset_auto_bkops - force enable of auto bkops |
| 2414 | * @hba: per adapter instance |
| 2415 | * |
| 2416 | * After a device reset the device may toggle the BKOPS_EN flag |
| 2417 | * to default value. The s/w tracking variables should be updated |
| 2418 | * as well. Do this by forcing enable of auto bkops. |
| 2419 | */ |
| 2420 | static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) |
| 2421 | { |
| 2422 | hba->auto_bkops_enabled = false; |
| 2423 | hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; |
| 2424 | ufshcd_enable_auto_bkops(hba); |
| 2425 | } |
| 2426 | |
| 2427 | static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) |
| 2428 | { |
| 2429 | return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 2430 | QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status); |
| 2431 | } |
| 2432 | |
| 2433 | /** |
| 2434 | * ufshcd_urgent_bkops - handle urgent bkops exception event |
| 2435 | * @hba: per-adapter instance |
| 2436 | * |
| 2437 | * Enable fBackgroundOpsEn flag in the device to permit background |
| 2438 | * operations. |
| 2439 | */ |
| 2440 | static int ufshcd_urgent_bkops(struct ufs_hba *hba) |
| 2441 | { |
| 2442 | int err; |
| 2443 | u32 status = 0; |
| 2444 | |
| 2445 | err = ufshcd_get_bkops_status(hba, &status); |
| 2446 | if (err) { |
| 2447 | dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", |
| 2448 | __func__, err); |
| 2449 | goto out; |
| 2450 | } |
| 2451 | |
| 2452 | status = status & 0xF; |
| 2453 | |
| 2454 | /* handle only if status indicates performance impact or critical */ |
| 2455 | if (status >= BKOPS_STATUS_PERF_IMPACT) |
| 2456 | err = ufshcd_enable_auto_bkops(hba); |
| 2457 | out: |
| 2458 | return err; |
| 2459 | } |
| 2460 | |
| 2461 | static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) |
| 2462 | { |
| 2463 | return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR, |
| 2464 | QUERY_ATTR_IDN_EE_STATUS, 0, 0, status); |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * ufshcd_exception_event_handler - handle exceptions raised by device |
| 2469 | * @work: pointer to work data |
| 2470 | * |
| 2471 | * Read bExceptionEventStatus attribute from the device and handle the |
| 2472 | * exception event accordingly. |
| 2473 | */ |
| 2474 | static void ufshcd_exception_event_handler(struct work_struct *work) |
| 2475 | { |
| 2476 | struct ufs_hba *hba; |
| 2477 | int err; |
| 2478 | u32 status = 0; |
| 2479 | hba = container_of(work, struct ufs_hba, eeh_work); |
| 2480 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 2481 | pm_runtime_get_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2482 | err = ufshcd_get_ee_status(hba, &status); |
| 2483 | if (err) { |
| 2484 | dev_err(hba->dev, "%s: failed to get exception status %d\n", |
| 2485 | __func__, err); |
| 2486 | goto out; |
| 2487 | } |
| 2488 | |
| 2489 | status &= hba->ee_ctrl_mask; |
| 2490 | if (status & MASK_EE_URGENT_BKOPS) { |
| 2491 | err = ufshcd_urgent_bkops(hba); |
| 2492 | if (err) |
| 2493 | dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", |
| 2494 | __func__, err); |
| 2495 | } |
| 2496 | out: |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 2497 | pm_runtime_put_sync(hba->dev); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 2498 | return; |
| 2499 | } |
| 2500 | |
| 2501 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2502 | * ufshcd_err_handler - handle UFS errors that require s/w attention |
| 2503 | * @work: pointer to work structure |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2504 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2505 | static void ufshcd_err_handler(struct work_struct *work) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2506 | { |
| 2507 | struct ufs_hba *hba; |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2508 | unsigned long flags; |
| 2509 | u32 err_xfer = 0; |
| 2510 | u32 err_tm = 0; |
| 2511 | int err = 0; |
| 2512 | int tag; |
| 2513 | |
| 2514 | hba = container_of(work, struct ufs_hba, eh_work); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2515 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 2516 | pm_runtime_get_sync(hba->dev); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2517 | |
| 2518 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2519 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) { |
| 2520 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2521 | goto out; |
| 2522 | } |
| 2523 | |
| 2524 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 2525 | ufshcd_set_eh_in_progress(hba); |
| 2526 | |
| 2527 | /* Complete requests that have door-bell cleared by h/w */ |
| 2528 | ufshcd_transfer_req_compl(hba); |
| 2529 | ufshcd_tmc_handler(hba); |
| 2530 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2531 | |
| 2532 | /* Clear pending transfer requests */ |
| 2533 | for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs) |
| 2534 | if (ufshcd_clear_cmd(hba, tag)) |
| 2535 | err_xfer |= 1 << tag; |
| 2536 | |
| 2537 | /* Clear pending task management requests */ |
| 2538 | for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) |
| 2539 | if (ufshcd_clear_tm_cmd(hba, tag)) |
| 2540 | err_tm |= 1 << tag; |
| 2541 | |
| 2542 | /* Complete the requests that are cleared by s/w */ |
| 2543 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2544 | ufshcd_transfer_req_compl(hba); |
| 2545 | ufshcd_tmc_handler(hba); |
| 2546 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2547 | |
| 2548 | /* Fatal errors need reset */ |
| 2549 | if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) || |
| 2550 | ((hba->saved_err & UIC_ERROR) && |
| 2551 | (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) { |
| 2552 | err = ufshcd_reset_and_restore(hba); |
| 2553 | if (err) { |
| 2554 | dev_err(hba->dev, "%s: reset and restore failed\n", |
| 2555 | __func__); |
| 2556 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 2557 | } |
| 2558 | /* |
| 2559 | * Inform scsi mid-layer that we did reset and allow to handle |
| 2560 | * Unit Attention properly. |
| 2561 | */ |
| 2562 | scsi_report_bus_reset(hba->host, 0); |
| 2563 | hba->saved_err = 0; |
| 2564 | hba->saved_uic_err = 0; |
| 2565 | } |
| 2566 | ufshcd_clear_eh_in_progress(hba); |
| 2567 | |
| 2568 | out: |
| 2569 | scsi_unblock_requests(hba->host); |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 2570 | pm_runtime_put_sync(hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2571 | } |
| 2572 | |
| 2573 | /** |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2574 | * ufshcd_update_uic_error - check and set fatal UIC error flags. |
| 2575 | * @hba: per-adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2576 | */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2577 | static void ufshcd_update_uic_error(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2578 | { |
| 2579 | u32 reg; |
| 2580 | |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2581 | /* PA_INIT_ERROR is fatal and needs UIC reset */ |
| 2582 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); |
| 2583 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 2584 | hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; |
| 2585 | |
| 2586 | /* UIC NL/TL/DME errors needs software retry */ |
| 2587 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); |
| 2588 | if (reg) |
| 2589 | hba->uic_error |= UFSHCD_UIC_NL_ERROR; |
| 2590 | |
| 2591 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); |
| 2592 | if (reg) |
| 2593 | hba->uic_error |= UFSHCD_UIC_TL_ERROR; |
| 2594 | |
| 2595 | reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); |
| 2596 | if (reg) |
| 2597 | hba->uic_error |= UFSHCD_UIC_DME_ERROR; |
| 2598 | |
| 2599 | dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", |
| 2600 | __func__, hba->uic_error); |
| 2601 | } |
| 2602 | |
| 2603 | /** |
| 2604 | * ufshcd_check_errors - Check for errors that need s/w attention |
| 2605 | * @hba: per-adapter instance |
| 2606 | */ |
| 2607 | static void ufshcd_check_errors(struct ufs_hba *hba) |
| 2608 | { |
| 2609 | bool queue_eh_work = false; |
| 2610 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2611 | if (hba->errors & INT_FATAL_ERRORS) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2612 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2613 | |
| 2614 | if (hba->errors & UIC_ERROR) { |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2615 | hba->uic_error = 0; |
| 2616 | ufshcd_update_uic_error(hba); |
| 2617 | if (hba->uic_error) |
| 2618 | queue_eh_work = true; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2619 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2620 | |
| 2621 | if (queue_eh_work) { |
| 2622 | /* handle fatal errors only when link is functional */ |
| 2623 | if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) { |
| 2624 | /* block commands from scsi mid-layer */ |
| 2625 | scsi_block_requests(hba->host); |
| 2626 | |
| 2627 | /* transfer error masks to sticky bits */ |
| 2628 | hba->saved_err |= hba->errors; |
| 2629 | hba->saved_uic_err |= hba->uic_error; |
| 2630 | |
| 2631 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 2632 | schedule_work(&hba->eh_work); |
| 2633 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2634 | } |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2635 | /* |
| 2636 | * if (!queue_eh_work) - |
| 2637 | * Other errors are either non-fatal where host recovers |
| 2638 | * itself without s/w intervention or errors that will be |
| 2639 | * handled by the SCSI core layer. |
| 2640 | */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2641 | } |
| 2642 | |
| 2643 | /** |
| 2644 | * ufshcd_tmc_handler - handle task management function completion |
| 2645 | * @hba: per adapter instance |
| 2646 | */ |
| 2647 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 2648 | { |
| 2649 | u32 tm_doorbell; |
| 2650 | |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2651 | tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2652 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2653 | wake_up(&hba->tm_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | /** |
| 2657 | * ufshcd_sl_intr - Interrupt service routine |
| 2658 | * @hba: per adapter instance |
| 2659 | * @intr_status: contains interrupts generated by the controller |
| 2660 | */ |
| 2661 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 2662 | { |
| 2663 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
| 2664 | if (hba->errors) |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 2665 | ufshcd_check_errors(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2666 | |
Seungwon Jeon | 53b3d9c | 2013-08-31 21:40:22 +0530 | [diff] [blame] | 2667 | if (intr_status & UFSHCD_UIC_MASK) |
| 2668 | ufshcd_uic_cmd_compl(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2669 | |
| 2670 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 2671 | ufshcd_tmc_handler(hba); |
| 2672 | |
| 2673 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 2674 | ufshcd_transfer_req_compl(hba); |
| 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * ufshcd_intr - Main interrupt service routine |
| 2679 | * @irq: irq number |
| 2680 | * @__hba: pointer to adapter instance |
| 2681 | * |
| 2682 | * Returns IRQ_HANDLED - If interrupt is valid |
| 2683 | * IRQ_NONE - If invalid interrupt |
| 2684 | */ |
| 2685 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 2686 | { |
| 2687 | u32 intr_status; |
| 2688 | irqreturn_t retval = IRQ_NONE; |
| 2689 | struct ufs_hba *hba = __hba; |
| 2690 | |
| 2691 | spin_lock(hba->host->host_lock); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2692 | intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2693 | |
| 2694 | if (intr_status) { |
Seungwon Jeon | 261ea45 | 2013-06-26 22:39:28 +0530 | [diff] [blame] | 2695 | ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2696 | ufshcd_sl_intr(hba, intr_status); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2697 | retval = IRQ_HANDLED; |
| 2698 | } |
| 2699 | spin_unlock(hba->host->host_lock); |
| 2700 | return retval; |
| 2701 | } |
| 2702 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2703 | static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) |
| 2704 | { |
| 2705 | int err = 0; |
| 2706 | u32 mask = 1 << tag; |
| 2707 | unsigned long flags; |
| 2708 | |
| 2709 | if (!test_bit(tag, &hba->outstanding_tasks)) |
| 2710 | goto out; |
| 2711 | |
| 2712 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2713 | ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR); |
| 2714 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2715 | |
| 2716 | /* poll for max. 1 sec to clear door bell register by h/w */ |
| 2717 | err = ufshcd_wait_for_register(hba, |
| 2718 | REG_UTP_TASK_REQ_DOOR_BELL, |
| 2719 | mask, 0, 1000, 1000); |
| 2720 | out: |
| 2721 | return err; |
| 2722 | } |
| 2723 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2724 | /** |
| 2725 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 2726 | * @hba: per adapter instance |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2727 | * @lun_id: LUN ID to which TM command is sent |
| 2728 | * @task_id: task ID to which the TM command is applicable |
| 2729 | * @tm_function: task management function opcode |
| 2730 | * @tm_response: task management service response return value |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2731 | * |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2732 | * Returns non-zero value on error, zero on success. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2733 | */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2734 | static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, |
| 2735 | u8 tm_function, u8 *tm_response) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2736 | { |
| 2737 | struct utp_task_req_desc *task_req_descp; |
| 2738 | struct utp_upiu_task_req *task_req_upiup; |
| 2739 | struct Scsi_Host *host; |
| 2740 | unsigned long flags; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2741 | int free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2742 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2743 | int task_tag; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2744 | |
| 2745 | host = hba->host; |
| 2746 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2747 | /* |
| 2748 | * Get free slot, sleep if slots are unavailable. |
| 2749 | * Even though we use wait_event() which sleeps indefinitely, |
| 2750 | * the maximum wait time is bounded by %TM_CMD_TIMEOUT. |
| 2751 | */ |
| 2752 | wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot)); |
| 2753 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2754 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2755 | task_req_descp = hba->utmrdl_base_addr; |
| 2756 | task_req_descp += free_slot; |
| 2757 | |
| 2758 | /* Configure task request descriptor */ |
| 2759 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 2760 | task_req_descp->header.dword_2 = |
| 2761 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 2762 | |
| 2763 | /* Configure task request UPIU */ |
| 2764 | task_req_upiup = |
| 2765 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2766 | task_tag = hba->nutrs + free_slot; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2767 | task_req_upiup->header.dword_0 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2768 | UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2769 | lun_id, task_tag); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2770 | task_req_upiup->header.dword_1 = |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2771 | UPIU_HEADER_DWORD(0, tm_function, 0, 0); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2772 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2773 | task_req_upiup->input_param1 = cpu_to_be32(lun_id); |
| 2774 | task_req_upiup->input_param2 = cpu_to_be32(task_id); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2775 | |
| 2776 | /* send command to the controller */ |
| 2777 | __set_bit(free_slot, &hba->outstanding_tasks); |
Seungwon Jeon | b873a275 | 2013-06-26 22:39:26 +0530 | [diff] [blame] | 2778 | ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2779 | |
| 2780 | spin_unlock_irqrestore(host->host_lock, flags); |
| 2781 | |
| 2782 | /* wait until the task management command is completed */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2783 | err = wait_event_timeout(hba->tm_wq, |
| 2784 | test_bit(free_slot, &hba->tm_condition), |
| 2785 | msecs_to_jiffies(TM_CMD_TIMEOUT)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2786 | if (!err) { |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2787 | dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", |
| 2788 | __func__, tm_function); |
| 2789 | if (ufshcd_clear_tm_cmd(hba, free_slot)) |
| 2790 | dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n", |
| 2791 | __func__, free_slot); |
| 2792 | err = -ETIMEDOUT; |
| 2793 | } else { |
| 2794 | err = ufshcd_task_req_compl(hba, free_slot, tm_response); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2795 | } |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2796 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2797 | clear_bit(free_slot, &hba->tm_condition); |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2798 | ufshcd_put_tm_slot(hba, free_slot); |
| 2799 | wake_up(&hba->tm_tag_wq); |
| 2800 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2801 | return err; |
| 2802 | } |
| 2803 | |
| 2804 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2805 | * ufshcd_eh_device_reset_handler - device reset handler registered to |
| 2806 | * scsi layer. |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2807 | * @cmd: SCSI command pointer |
| 2808 | * |
| 2809 | * Returns SUCCESS/FAILED |
| 2810 | */ |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2811 | static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2812 | { |
| 2813 | struct Scsi_Host *host; |
| 2814 | struct ufs_hba *hba; |
| 2815 | unsigned int tag; |
| 2816 | u32 pos; |
| 2817 | int err; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2818 | u8 resp = 0xF; |
| 2819 | struct ufshcd_lrb *lrbp; |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2820 | unsigned long flags; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2821 | |
| 2822 | host = cmd->device->host; |
| 2823 | hba = shost_priv(host); |
| 2824 | tag = cmd->request->tag; |
| 2825 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2826 | lrbp = &hba->lrb[tag]; |
| 2827 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp); |
| 2828 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2829 | if (!err) |
| 2830 | err = resp; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2831 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2832 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2833 | |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2834 | /* clear the commands that were pending for corresponding LUN */ |
| 2835 | for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) { |
| 2836 | if (hba->lrb[pos].lun == lrbp->lun) { |
| 2837 | err = ufshcd_clear_cmd(hba, pos); |
| 2838 | if (err) |
| 2839 | break; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2840 | } |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2841 | } |
| 2842 | spin_lock_irqsave(host->host_lock, flags); |
| 2843 | ufshcd_transfer_req_compl(hba); |
| 2844 | spin_unlock_irqrestore(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2845 | out: |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2846 | if (!err) { |
| 2847 | err = SUCCESS; |
| 2848 | } else { |
| 2849 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 2850 | err = FAILED; |
| 2851 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2852 | return err; |
| 2853 | } |
| 2854 | |
| 2855 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2856 | * ufshcd_abort - abort a specific command |
| 2857 | * @cmd: SCSI command pointer |
| 2858 | * |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2859 | * Abort the pending command in device by sending UFS_ABORT_TASK task management |
| 2860 | * command, and in host controller by clearing the door-bell register. There can |
| 2861 | * be race between controller sending the command to the device while abort is |
| 2862 | * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is |
| 2863 | * really issued and then try to abort it. |
| 2864 | * |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2865 | * Returns SUCCESS/FAILED |
| 2866 | */ |
| 2867 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 2868 | { |
| 2869 | struct Scsi_Host *host; |
| 2870 | struct ufs_hba *hba; |
| 2871 | unsigned long flags; |
| 2872 | unsigned int tag; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2873 | int err = 0; |
| 2874 | int poll_cnt; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2875 | u8 resp = 0xF; |
| 2876 | struct ufshcd_lrb *lrbp; |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 2877 | u32 reg; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2878 | |
| 2879 | host = cmd->device->host; |
| 2880 | hba = shost_priv(host); |
| 2881 | tag = cmd->request->tag; |
| 2882 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2883 | /* If command is already aborted/completed, return SUCCESS */ |
| 2884 | if (!(test_bit(tag, &hba->outstanding_reqs))) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2885 | goto out; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2886 | |
Dolev Raviv | e9d501b | 2014-07-01 12:22:37 +0300 | [diff] [blame] | 2887 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 2888 | if (!(reg & (1 << tag))) { |
| 2889 | dev_err(hba->dev, |
| 2890 | "%s: cmd was completed, but without a notifying intr, tag = %d", |
| 2891 | __func__, tag); |
| 2892 | } |
| 2893 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2894 | lrbp = &hba->lrb[tag]; |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2895 | for (poll_cnt = 100; poll_cnt; poll_cnt--) { |
| 2896 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 2897 | UFS_QUERY_TASK, &resp); |
| 2898 | if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) { |
| 2899 | /* cmd pending in the device */ |
| 2900 | break; |
| 2901 | } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2902 | /* |
| 2903 | * cmd not pending in the device, check if it is |
| 2904 | * in transition. |
| 2905 | */ |
| 2906 | reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 2907 | if (reg & (1 << tag)) { |
| 2908 | /* sleep for max. 200us to stabilize */ |
| 2909 | usleep_range(100, 200); |
| 2910 | continue; |
| 2911 | } |
| 2912 | /* command completed already */ |
| 2913 | goto out; |
| 2914 | } else { |
| 2915 | if (!err) |
| 2916 | err = resp; /* service response error */ |
| 2917 | goto out; |
| 2918 | } |
| 2919 | } |
| 2920 | |
| 2921 | if (!poll_cnt) { |
| 2922 | err = -EBUSY; |
| 2923 | goto out; |
| 2924 | } |
| 2925 | |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2926 | err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, |
| 2927 | UFS_ABORT_TASK, &resp); |
| 2928 | if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) { |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2929 | if (!err) |
| 2930 | err = resp; /* service response error */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2931 | goto out; |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 2932 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2933 | |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2934 | err = ufshcd_clear_cmd(hba, tag); |
| 2935 | if (err) |
| 2936 | goto out; |
| 2937 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2938 | scsi_dma_unmap(cmd); |
| 2939 | |
| 2940 | spin_lock_irqsave(host->host_lock, flags); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2941 | __clear_bit(tag, &hba->outstanding_reqs); |
| 2942 | hba->lrb[tag].cmd = NULL; |
| 2943 | spin_unlock_irqrestore(host->host_lock, flags); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 2944 | |
| 2945 | clear_bit_unlock(tag, &hba->lrb_in_use); |
| 2946 | wake_up(&hba->dev_cmd.tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2947 | out: |
Sujit Reddy Thumma | f20810d | 2014-05-26 10:59:13 +0530 | [diff] [blame] | 2948 | if (!err) { |
| 2949 | err = SUCCESS; |
| 2950 | } else { |
| 2951 | dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); |
| 2952 | err = FAILED; |
| 2953 | } |
| 2954 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2955 | return err; |
| 2956 | } |
| 2957 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 2958 | /** |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 2959 | * ufshcd_host_reset_and_restore - reset and restore host controller |
| 2960 | * @hba: per-adapter instance |
| 2961 | * |
| 2962 | * Note that host controller reset may issue DME_RESET to |
| 2963 | * local and remote (device) Uni-Pro stack and the attributes |
| 2964 | * are reset to default state. |
| 2965 | * |
| 2966 | * Returns zero on success, non-zero on failure |
| 2967 | */ |
| 2968 | static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) |
| 2969 | { |
| 2970 | int err; |
| 2971 | async_cookie_t cookie; |
| 2972 | unsigned long flags; |
| 2973 | |
| 2974 | /* Reset the host controller */ |
| 2975 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 2976 | ufshcd_hba_stop(hba); |
| 2977 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 2978 | |
| 2979 | err = ufshcd_hba_enable(hba); |
| 2980 | if (err) |
| 2981 | goto out; |
| 2982 | |
| 2983 | /* Establish the link again and restore the device */ |
| 2984 | cookie = async_schedule(ufshcd_async_scan, hba); |
| 2985 | /* wait for async scan to be completed */ |
| 2986 | async_synchronize_cookie(++cookie); |
| 2987 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) |
| 2988 | err = -EIO; |
| 2989 | out: |
| 2990 | if (err) |
| 2991 | dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); |
| 2992 | |
| 2993 | return err; |
| 2994 | } |
| 2995 | |
| 2996 | /** |
| 2997 | * ufshcd_reset_and_restore - reset and re-initialize host/device |
| 2998 | * @hba: per-adapter instance |
| 2999 | * |
| 3000 | * Reset and recover device, host and re-establish link. This |
| 3001 | * is helpful to recover the communication in fatal error conditions. |
| 3002 | * |
| 3003 | * Returns zero on success, non-zero on failure |
| 3004 | */ |
| 3005 | static int ufshcd_reset_and_restore(struct ufs_hba *hba) |
| 3006 | { |
| 3007 | int err = 0; |
| 3008 | unsigned long flags; |
| 3009 | |
| 3010 | err = ufshcd_host_reset_and_restore(hba); |
| 3011 | |
| 3012 | /* |
| 3013 | * After reset the door-bell might be cleared, complete |
| 3014 | * outstanding requests in s/w here. |
| 3015 | */ |
| 3016 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3017 | ufshcd_transfer_req_compl(hba); |
| 3018 | ufshcd_tmc_handler(hba); |
| 3019 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3020 | |
| 3021 | return err; |
| 3022 | } |
| 3023 | |
| 3024 | /** |
| 3025 | * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer |
| 3026 | * @cmd - SCSI command pointer |
| 3027 | * |
| 3028 | * Returns SUCCESS/FAILED |
| 3029 | */ |
| 3030 | static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd) |
| 3031 | { |
| 3032 | int err; |
| 3033 | unsigned long flags; |
| 3034 | struct ufs_hba *hba; |
| 3035 | |
| 3036 | hba = shost_priv(cmd->device->host); |
| 3037 | |
| 3038 | /* |
| 3039 | * Check if there is any race with fatal error handling. |
| 3040 | * If so, wait for it to complete. Even though fatal error |
| 3041 | * handling does reset and restore in some cases, don't assume |
| 3042 | * anything out of it. We are just avoiding race here. |
| 3043 | */ |
| 3044 | do { |
| 3045 | spin_lock_irqsave(hba->host->host_lock, flags); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3046 | if (!(work_pending(&hba->eh_work) || |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 3047 | hba->ufshcd_state == UFSHCD_STATE_RESET)) |
| 3048 | break; |
| 3049 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3050 | dev_dbg(hba->dev, "%s: reset in progress\n", __func__); |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3051 | flush_work(&hba->eh_work); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 3052 | } while (1); |
| 3053 | |
| 3054 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 3055 | ufshcd_set_eh_in_progress(hba); |
| 3056 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3057 | |
| 3058 | err = ufshcd_reset_and_restore(hba); |
| 3059 | |
| 3060 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 3061 | if (!err) { |
| 3062 | err = SUCCESS; |
| 3063 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 3064 | } else { |
| 3065 | err = FAILED; |
| 3066 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 3067 | } |
| 3068 | ufshcd_clear_eh_in_progress(hba); |
| 3069 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 3070 | |
| 3071 | return err; |
| 3072 | } |
| 3073 | |
| 3074 | /** |
Dolev Raviv | 1b3e895 | 2014-06-29 09:40:19 +0300 | [diff] [blame] | 3075 | * ufshcd_read_sdev_qdepth - read the lun command queue depth |
| 3076 | * @hba: Pointer to adapter instance |
| 3077 | * @sdev: pointer to SCSI device |
| 3078 | * |
| 3079 | * Return in case of success the lun's queue depth else error. |
| 3080 | */ |
| 3081 | static int ufshcd_read_sdev_qdepth(struct ufs_hba *hba, |
| 3082 | struct scsi_device *sdev) |
| 3083 | { |
| 3084 | int ret; |
| 3085 | int buff_len = UNIT_DESC_MAX_SIZE; |
| 3086 | u8 desc_buf[UNIT_DESC_MAX_SIZE]; |
| 3087 | |
| 3088 | ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC, |
| 3089 | QUERY_DESC_IDN_UNIT, sdev->lun, 0, desc_buf, &buff_len); |
| 3090 | |
| 3091 | if (ret || (buff_len < UNIT_DESC_PARAM_LU_Q_DEPTH)) { |
| 3092 | dev_err(hba->dev, |
| 3093 | "%s:Failed reading unit descriptor. len = %d ret = %d" |
| 3094 | , __func__, buff_len, ret); |
| 3095 | if (!ret) |
| 3096 | ret = -EINVAL; |
| 3097 | |
| 3098 | goto out; |
| 3099 | } |
| 3100 | |
| 3101 | ret = desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH] & 0xFF; |
| 3102 | out: |
| 3103 | return ret; |
| 3104 | } |
| 3105 | |
| 3106 | /** |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3107 | * ufshcd_async_scan - asynchronous execution for link startup |
| 3108 | * @data: data pointer to pass to this function |
| 3109 | * @cookie: cookie data |
| 3110 | */ |
| 3111 | static void ufshcd_async_scan(void *data, async_cookie_t cookie) |
| 3112 | { |
| 3113 | struct ufs_hba *hba = (struct ufs_hba *)data; |
| 3114 | int ret; |
| 3115 | |
| 3116 | ret = ufshcd_link_startup(hba); |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3117 | if (ret) |
| 3118 | goto out; |
| 3119 | |
Seungwon Jeon | d3e89ba | 2013-08-31 21:40:24 +0530 | [diff] [blame] | 3120 | ufshcd_config_max_pwr_mode(hba); |
| 3121 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3122 | ret = ufshcd_verify_dev_init(hba); |
| 3123 | if (ret) |
| 3124 | goto out; |
| 3125 | |
Dolev Raviv | 68078d5 | 2013-07-30 00:35:58 +0530 | [diff] [blame] | 3126 | ret = ufshcd_complete_dev_init(hba); |
| 3127 | if (ret) |
| 3128 | goto out; |
| 3129 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3130 | ufshcd_force_reset_auto_bkops(hba); |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 3131 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 3132 | |
| 3133 | /* If we are in error handling context no need to scan the host */ |
| 3134 | if (!ufshcd_eh_in_progress(hba)) { |
| 3135 | scsi_scan_host(hba->host); |
| 3136 | pm_runtime_put_sync(hba->dev); |
| 3137 | } |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3138 | out: |
| 3139 | return; |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3140 | } |
| 3141 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3142 | static struct scsi_host_template ufshcd_driver_template = { |
| 3143 | .module = THIS_MODULE, |
| 3144 | .name = UFSHCD, |
| 3145 | .proc_name = UFSHCD, |
| 3146 | .queuecommand = ufshcd_queuecommand, |
| 3147 | .slave_alloc = ufshcd_slave_alloc, |
| 3148 | .slave_destroy = ufshcd_slave_destroy, |
Sujit Reddy Thumma | 4264fd6 | 2014-06-29 09:40:20 +0300 | [diff] [blame] | 3149 | .change_queue_depth = ufshcd_change_queue_depth, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3150 | .eh_abort_handler = ufshcd_abort, |
Sujit Reddy Thumma | 3441da7 | 2014-05-26 10:59:14 +0530 | [diff] [blame] | 3151 | .eh_device_reset_handler = ufshcd_eh_device_reset_handler, |
| 3152 | .eh_host_reset_handler = ufshcd_eh_host_reset_handler, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3153 | .this_id = -1, |
| 3154 | .sg_tablesize = SG_ALL, |
| 3155 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 3156 | .can_queue = UFSHCD_CAN_QUEUE, |
| 3157 | }; |
| 3158 | |
| 3159 | /** |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3160 | * ufshcd_suspend - suspend power management function |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3161 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3162 | * @state: power state |
| 3163 | * |
| 3164 | * Returns -ENOSYS |
| 3165 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3166 | int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3167 | { |
| 3168 | /* |
| 3169 | * TODO: |
| 3170 | * 1. Block SCSI requests from SCSI midlayer |
| 3171 | * 2. Change the internal driver state to non operational |
| 3172 | * 3. Set UTRLRSR and UTMRLRSR bits to zero |
| 3173 | * 4. Wait until outstanding commands are completed |
| 3174 | * 5. Set HCE to zero to send the UFS host controller to reset state |
| 3175 | */ |
| 3176 | |
| 3177 | return -ENOSYS; |
| 3178 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3179 | EXPORT_SYMBOL_GPL(ufshcd_suspend); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3180 | |
| 3181 | /** |
| 3182 | * ufshcd_resume - resume power management function |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3183 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3184 | * |
| 3185 | * Returns -ENOSYS |
| 3186 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3187 | int ufshcd_resume(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3188 | { |
| 3189 | /* |
| 3190 | * TODO: |
| 3191 | * 1. Set HCE to 1, to start the UFS host controller |
| 3192 | * initialization process |
| 3193 | * 2. Set UTRLRSR and UTMRLRSR bits to 1 |
| 3194 | * 3. Change the internal driver state to operational |
| 3195 | * 4. Unblock SCSI requests from SCSI midlayer |
| 3196 | */ |
| 3197 | |
| 3198 | return -ENOSYS; |
| 3199 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3200 | EXPORT_SYMBOL_GPL(ufshcd_resume); |
| 3201 | |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3202 | int ufshcd_runtime_suspend(struct ufs_hba *hba) |
| 3203 | { |
| 3204 | if (!hba) |
| 3205 | return 0; |
| 3206 | |
| 3207 | /* |
| 3208 | * The device is idle with no requests in the queue, |
| 3209 | * allow background operations. |
| 3210 | */ |
| 3211 | return ufshcd_enable_auto_bkops(hba); |
| 3212 | } |
| 3213 | EXPORT_SYMBOL(ufshcd_runtime_suspend); |
| 3214 | |
| 3215 | int ufshcd_runtime_resume(struct ufs_hba *hba) |
| 3216 | { |
| 3217 | if (!hba) |
| 3218 | return 0; |
| 3219 | |
| 3220 | return ufshcd_disable_auto_bkops(hba); |
| 3221 | } |
| 3222 | EXPORT_SYMBOL(ufshcd_runtime_resume); |
| 3223 | |
| 3224 | int ufshcd_runtime_idle(struct ufs_hba *hba) |
| 3225 | { |
| 3226 | return 0; |
| 3227 | } |
| 3228 | EXPORT_SYMBOL(ufshcd_runtime_idle); |
| 3229 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3230 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3231 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3232 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3233 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3234 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3235 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3236 | { |
Akinobu Mita | cfdf9c9 | 2013-07-30 00:36:03 +0530 | [diff] [blame] | 3237 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3238 | /* disable interrupts */ |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 3239 | ufshcd_disable_intr(hba, hba->intr_mask); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3240 | ufshcd_hba_stop(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3241 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3242 | scsi_host_put(hba->host); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3243 | } |
| 3244 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 3245 | |
| 3246 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3247 | * ufshcd_init - Driver initialization routine |
| 3248 | * @dev: pointer to device handle |
| 3249 | * @hba_handle: driver private handle |
| 3250 | * @mmio_base: base register address |
| 3251 | * @irq: Interrupt line of device |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3252 | * Returns 0 on success, non-zero value on failure |
| 3253 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3254 | int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle, |
| 3255 | void __iomem *mmio_base, unsigned int irq) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3256 | { |
| 3257 | struct Scsi_Host *host; |
| 3258 | struct ufs_hba *hba; |
| 3259 | int err; |
| 3260 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3261 | if (!dev) { |
| 3262 | dev_err(dev, |
| 3263 | "Invalid memory reference for dev is NULL\n"); |
| 3264 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3265 | goto out_error; |
| 3266 | } |
| 3267 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3268 | if (!mmio_base) { |
| 3269 | dev_err(dev, |
| 3270 | "Invalid memory reference for mmio_base is NULL\n"); |
| 3271 | err = -ENODEV; |
| 3272 | goto out_error; |
| 3273 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3274 | |
| 3275 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 3276 | sizeof(struct ufs_hba)); |
| 3277 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3278 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3279 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3280 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3281 | } |
| 3282 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3283 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3284 | hba->dev = dev; |
| 3285 | hba->mmio_base = mmio_base; |
| 3286 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3287 | |
| 3288 | /* Read capabilities registers */ |
| 3289 | ufshcd_hba_capabilities(hba); |
| 3290 | |
| 3291 | /* Get UFS version supported by the controller */ |
| 3292 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 3293 | |
Seungwon Jeon | 2fbd009 | 2013-06-26 22:39:27 +0530 | [diff] [blame] | 3294 | /* Get Interrupt bit mask per version */ |
| 3295 | hba->intr_mask = ufshcd_get_intr_mask(hba); |
| 3296 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3297 | /* Allocate memory for host memory space */ |
| 3298 | err = ufshcd_memory_alloc(hba); |
| 3299 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3300 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 3301 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | /* Configure LRB */ |
| 3305 | ufshcd_host_memory_configure(hba); |
| 3306 | |
| 3307 | host->can_queue = hba->nutrs; |
| 3308 | host->cmd_per_lun = hba->nutrs; |
| 3309 | host->max_id = UFSHCD_MAX_ID; |
| 3310 | host->max_lun = UFSHCD_MAX_LUNS; |
| 3311 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 3312 | host->unique_id = host->host_no; |
| 3313 | host->max_cmd_len = MAX_CDB_SIZE; |
| 3314 | |
| 3315 | /* Initailize wait queue for task management */ |
Sujit Reddy Thumma | e293313 | 2014-05-26 10:59:12 +0530 | [diff] [blame] | 3316 | init_waitqueue_head(&hba->tm_wq); |
| 3317 | init_waitqueue_head(&hba->tm_tag_wq); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3318 | |
| 3319 | /* Initialize work queues */ |
Sujit Reddy Thumma | e8e7f27 | 2014-05-26 10:59:15 +0530 | [diff] [blame] | 3320 | INIT_WORK(&hba->eh_work, ufshcd_err_handler); |
Sujit Reddy Thumma | 66ec6d5 | 2013-07-30 00:35:59 +0530 | [diff] [blame] | 3321 | INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3322 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3323 | /* Initialize UIC command mutex */ |
| 3324 | mutex_init(&hba->uic_cmd_mutex); |
| 3325 | |
Sujit Reddy Thumma | 5a0b0cb | 2013-07-30 00:35:57 +0530 | [diff] [blame] | 3326 | /* Initialize mutex for device management commands */ |
| 3327 | mutex_init(&hba->dev_cmd.lock); |
| 3328 | |
| 3329 | /* Initialize device management tag acquire wait queue */ |
| 3330 | init_waitqueue_head(&hba->dev_cmd.tag_wq); |
| 3331 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3332 | /* IRQ registration */ |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3333 | err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3334 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3335 | dev_err(hba->dev, "request irq failed\n"); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3336 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | /* Enable SCSI tag mapping */ |
| 3340 | err = scsi_init_shared_tag_map(host, host->can_queue); |
| 3341 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3342 | dev_err(hba->dev, "init shared queue failed\n"); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3343 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3344 | } |
| 3345 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3346 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3347 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3348 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Seungwon Jeon | 2953f85 | 2013-06-27 13:31:54 +0900 | [diff] [blame] | 3349 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3350 | } |
| 3351 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3352 | /* Host controller enable */ |
| 3353 | err = ufshcd_hba_enable(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3354 | if (err) { |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3355 | dev_err(hba->dev, "Host controller enable failed\n"); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3356 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3357 | } |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3358 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3359 | *hba_handle = hba; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3360 | |
Sujit Reddy Thumma | 6269473 | 2013-07-30 00:36:00 +0530 | [diff] [blame] | 3361 | /* Hold auto suspend until async scan completes */ |
| 3362 | pm_runtime_get_sync(dev); |
| 3363 | |
Seungwon Jeon | 6ccf44fe | 2013-06-26 22:39:29 +0530 | [diff] [blame] | 3364 | async_schedule(ufshcd_async_scan, hba); |
| 3365 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3366 | return 0; |
| 3367 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3368 | out_remove_scsi_host: |
| 3369 | scsi_remove_host(hba->host); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3370 | out_disable: |
| 3371 | scsi_host_put(host); |
| 3372 | out_error: |
| 3373 | return err; |
| 3374 | } |
| 3375 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 3376 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame] | 3377 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 3378 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Vinayak Holikatti | e0eca63 | 2013-02-25 21:44:33 +0530 | [diff] [blame] | 3379 | MODULE_DESCRIPTION("Generic UFS host controller driver Core"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 3380 | MODULE_LICENSE("GPL"); |
| 3381 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |