Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Universal Flash Storage Host controller driver |
| 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 | |
| 36 | #include <linux/module.h> |
| 37 | #include <linux/kernel.h> |
| 38 | #include <linux/init.h> |
| 39 | #include <linux/pci.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | #include <linux/io.h> |
| 42 | #include <linux/delay.h> |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/spinlock.h> |
| 45 | #include <linux/workqueue.h> |
| 46 | #include <linux/errno.h> |
| 47 | #include <linux/types.h> |
| 48 | #include <linux/wait.h> |
| 49 | #include <linux/bitops.h> |
| 50 | |
| 51 | #include <asm/irq.h> |
| 52 | #include <asm/byteorder.h> |
| 53 | #include <scsi/scsi.h> |
| 54 | #include <scsi/scsi_cmnd.h> |
| 55 | #include <scsi/scsi_host.h> |
| 56 | #include <scsi/scsi_tcq.h> |
| 57 | #include <scsi/scsi_dbg.h> |
| 58 | #include <scsi/scsi_eh.h> |
| 59 | |
| 60 | #include "ufs.h" |
| 61 | #include "ufshci.h" |
| 62 | |
| 63 | #define UFSHCD "ufshcd" |
| 64 | #define UFSHCD_DRIVER_VERSION "0.1" |
| 65 | |
| 66 | enum { |
| 67 | UFSHCD_MAX_CHANNEL = 0, |
| 68 | UFSHCD_MAX_ID = 1, |
| 69 | UFSHCD_MAX_LUNS = 8, |
| 70 | UFSHCD_CMD_PER_LUN = 32, |
| 71 | UFSHCD_CAN_QUEUE = 32, |
| 72 | }; |
| 73 | |
| 74 | /* UFSHCD states */ |
| 75 | enum { |
| 76 | UFSHCD_STATE_OPERATIONAL, |
| 77 | UFSHCD_STATE_RESET, |
| 78 | UFSHCD_STATE_ERROR, |
| 79 | }; |
| 80 | |
| 81 | /* Interrupt configuration options */ |
| 82 | enum { |
| 83 | UFSHCD_INT_DISABLE, |
| 84 | UFSHCD_INT_ENABLE, |
| 85 | UFSHCD_INT_CLEAR, |
| 86 | }; |
| 87 | |
| 88 | /* Interrupt aggregation options */ |
| 89 | enum { |
| 90 | INT_AGGR_RESET, |
| 91 | INT_AGGR_CONFIG, |
| 92 | }; |
| 93 | |
| 94 | /** |
| 95 | * struct uic_command - UIC command structure |
| 96 | * @command: UIC command |
| 97 | * @argument1: UIC command argument 1 |
| 98 | * @argument2: UIC command argument 2 |
| 99 | * @argument3: UIC command argument 3 |
| 100 | * @cmd_active: Indicate if UIC command is outstanding |
| 101 | * @result: UIC command result |
| 102 | */ |
| 103 | struct uic_command { |
| 104 | u32 command; |
| 105 | u32 argument1; |
| 106 | u32 argument2; |
| 107 | u32 argument3; |
| 108 | int cmd_active; |
| 109 | int result; |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * struct ufs_hba - per adapter private structure |
| 114 | * @mmio_base: UFSHCI base register address |
| 115 | * @ucdl_base_addr: UFS Command Descriptor base address |
| 116 | * @utrdl_base_addr: UTP Transfer Request Descriptor base address |
| 117 | * @utmrdl_base_addr: UTP Task Management Descriptor base address |
| 118 | * @ucdl_dma_addr: UFS Command Descriptor DMA address |
| 119 | * @utrdl_dma_addr: UTRDL DMA address |
| 120 | * @utmrdl_dma_addr: UTMRDL DMA address |
| 121 | * @host: Scsi_Host instance of the driver |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 122 | * @dev: device handle |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 123 | * @lrb: local reference block |
| 124 | * @outstanding_tasks: Bits representing outstanding task requests |
| 125 | * @outstanding_reqs: Bits representing outstanding transfer requests |
| 126 | * @capabilities: UFS Controller Capabilities |
| 127 | * @nutrs: Transfer Request Queue depth supported by controller |
| 128 | * @nutmrs: Task Management Queue depth supported by controller |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 129 | * @ufs_version: UFS Version to which controller complies |
| 130 | * @irq: Irq number of the controller |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 131 | * @active_uic_cmd: handle of active UIC command |
| 132 | * @ufshcd_tm_wait_queue: wait queue for task management |
| 133 | * @tm_condition: condition variable for task management |
| 134 | * @ufshcd_state: UFSHCD states |
| 135 | * @int_enable_mask: Interrupt Mask Bits |
| 136 | * @uic_workq: Work queue for UIC completion handling |
| 137 | * @feh_workq: Work queue for fatal controller error handling |
| 138 | * @errors: HBA errors |
| 139 | */ |
| 140 | struct ufs_hba { |
| 141 | void __iomem *mmio_base; |
| 142 | |
| 143 | /* Virtual memory reference */ |
| 144 | struct utp_transfer_cmd_desc *ucdl_base_addr; |
| 145 | struct utp_transfer_req_desc *utrdl_base_addr; |
| 146 | struct utp_task_req_desc *utmrdl_base_addr; |
| 147 | |
| 148 | /* DMA memory reference */ |
| 149 | dma_addr_t ucdl_dma_addr; |
| 150 | dma_addr_t utrdl_dma_addr; |
| 151 | dma_addr_t utmrdl_dma_addr; |
| 152 | |
| 153 | struct Scsi_Host *host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 154 | struct device *dev; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 155 | |
| 156 | struct ufshcd_lrb *lrb; |
| 157 | |
| 158 | unsigned long outstanding_tasks; |
| 159 | unsigned long outstanding_reqs; |
| 160 | |
| 161 | u32 capabilities; |
| 162 | int nutrs; |
| 163 | int nutmrs; |
| 164 | u32 ufs_version; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 165 | unsigned int irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 166 | |
| 167 | struct uic_command active_uic_cmd; |
| 168 | wait_queue_head_t ufshcd_tm_wait_queue; |
| 169 | unsigned long tm_condition; |
| 170 | |
| 171 | u32 ufshcd_state; |
| 172 | u32 int_enable_mask; |
| 173 | |
| 174 | /* Work Queues */ |
| 175 | struct work_struct uic_workq; |
| 176 | struct work_struct feh_workq; |
| 177 | |
| 178 | /* HBA Errors */ |
| 179 | u32 errors; |
| 180 | }; |
| 181 | |
| 182 | /** |
| 183 | * struct ufshcd_lrb - local reference block |
| 184 | * @utr_descriptor_ptr: UTRD address of the command |
| 185 | * @ucd_cmd_ptr: UCD address of the command |
| 186 | * @ucd_rsp_ptr: Response UPIU address for this command |
| 187 | * @ucd_prdt_ptr: PRDT address of the command |
| 188 | * @cmd: pointer to SCSI command |
| 189 | * @sense_buffer: pointer to sense buffer address of the SCSI command |
| 190 | * @sense_bufflen: Length of the sense buffer |
| 191 | * @scsi_status: SCSI status of the command |
| 192 | * @command_type: SCSI, UFS, Query. |
| 193 | * @task_tag: Task tag of the command |
| 194 | * @lun: LUN of the command |
| 195 | */ |
| 196 | struct ufshcd_lrb { |
| 197 | struct utp_transfer_req_desc *utr_descriptor_ptr; |
| 198 | struct utp_upiu_cmd *ucd_cmd_ptr; |
| 199 | struct utp_upiu_rsp *ucd_rsp_ptr; |
| 200 | struct ufshcd_sg_entry *ucd_prdt_ptr; |
| 201 | |
| 202 | struct scsi_cmnd *cmd; |
| 203 | u8 *sense_buffer; |
| 204 | unsigned int sense_bufflen; |
| 205 | int scsi_status; |
| 206 | |
| 207 | int command_type; |
| 208 | int task_tag; |
| 209 | unsigned int lun; |
| 210 | }; |
| 211 | |
| 212 | /** |
| 213 | * ufshcd_get_ufs_version - Get the UFS version supported by the HBA |
| 214 | * @hba - Pointer to adapter instance |
| 215 | * |
| 216 | * Returns UFSHCI version supported by the controller |
| 217 | */ |
| 218 | static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) |
| 219 | { |
| 220 | return readl(hba->mmio_base + REG_UFS_VERSION); |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * ufshcd_is_device_present - Check if any device connected to |
| 225 | * the host controller |
| 226 | * @reg_hcs - host controller status register value |
| 227 | * |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 228 | * Returns 1 if device present, 0 if no device detected |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 229 | */ |
| 230 | static inline int ufshcd_is_device_present(u32 reg_hcs) |
| 231 | { |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 232 | return (DEVICE_PRESENT & reg_hcs) ? 1 : 0; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | /** |
| 236 | * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status |
| 237 | * @lrb: pointer to local command reference block |
| 238 | * |
| 239 | * This function is used to get the OCS field from UTRD |
| 240 | * Returns the OCS field in the UTRD |
| 241 | */ |
| 242 | static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp) |
| 243 | { |
| 244 | return lrbp->utr_descriptor_ptr->header.dword_2 & MASK_OCS; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status |
| 249 | * @task_req_descp: pointer to utp_task_req_desc structure |
| 250 | * |
| 251 | * This function is used to get the OCS field from UTMRD |
| 252 | * Returns the OCS field in the UTMRD |
| 253 | */ |
| 254 | static inline int |
| 255 | ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp) |
| 256 | { |
| 257 | return task_req_descp->header.dword_2 & MASK_OCS; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * ufshcd_get_tm_free_slot - get a free slot for task management request |
| 262 | * @hba: per adapter instance |
| 263 | * |
| 264 | * Returns maximum number of task management request slots in case of |
| 265 | * task management queue full or returns the free slot number |
| 266 | */ |
| 267 | static inline int ufshcd_get_tm_free_slot(struct ufs_hba *hba) |
| 268 | { |
| 269 | return find_first_zero_bit(&hba->outstanding_tasks, hba->nutmrs); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * ufshcd_utrl_clear - Clear a bit in UTRLCLR register |
| 274 | * @hba: per adapter instance |
| 275 | * @pos: position of the bit to be cleared |
| 276 | */ |
| 277 | static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos) |
| 278 | { |
| 279 | writel(~(1 << pos), |
| 280 | (hba->mmio_base + REG_UTP_TRANSFER_REQ_LIST_CLEAR)); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY |
| 285 | * @reg: Register value of host controller status |
| 286 | * |
| 287 | * Returns integer, 0 on Success and positive value if failed |
| 288 | */ |
| 289 | static inline int ufshcd_get_lists_status(u32 reg) |
| 290 | { |
| 291 | /* |
| 292 | * The mask 0xFF is for the following HCS register bits |
| 293 | * Bit Description |
| 294 | * 0 Device Present |
| 295 | * 1 UTRLRDY |
| 296 | * 2 UTMRLRDY |
| 297 | * 3 UCRDY |
| 298 | * 4 HEI |
| 299 | * 5 DEI |
| 300 | * 6-7 reserved |
| 301 | */ |
| 302 | return (((reg) & (0xFF)) >> 1) ^ (0x07); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * ufshcd_get_uic_cmd_result - Get the UIC command result |
| 307 | * @hba: Pointer to adapter instance |
| 308 | * |
| 309 | * This function gets the result of UIC command completion |
| 310 | * Returns 0 on success, non zero value on error |
| 311 | */ |
| 312 | static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) |
| 313 | { |
| 314 | return readl(hba->mmio_base + REG_UIC_COMMAND_ARG_2) & |
| 315 | MASK_UIC_COMMAND_RESULT; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * ufshcd_free_hba_memory - Free allocated memory for LRB, request |
| 320 | * and task lists |
| 321 | * @hba: Pointer to adapter instance |
| 322 | */ |
| 323 | static inline void ufshcd_free_hba_memory(struct ufs_hba *hba) |
| 324 | { |
| 325 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 326 | |
| 327 | kfree(hba->lrb); |
| 328 | |
| 329 | if (hba->utmrdl_base_addr) { |
| 330 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 331 | dma_free_coherent(hba->dev, utmrdl_size, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 332 | hba->utmrdl_base_addr, hba->utmrdl_dma_addr); |
| 333 | } |
| 334 | |
| 335 | if (hba->utrdl_base_addr) { |
| 336 | utrdl_size = |
| 337 | (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 338 | dma_free_coherent(hba->dev, utrdl_size, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 339 | hba->utrdl_base_addr, hba->utrdl_dma_addr); |
| 340 | } |
| 341 | |
| 342 | if (hba->ucdl_base_addr) { |
| 343 | ucdl_size = |
| 344 | (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 345 | dma_free_coherent(hba->dev, ucdl_size, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 346 | hba->ucdl_base_addr, hba->ucdl_dma_addr); |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
| 351 | * ufshcd_is_valid_req_rsp - checks if controller TR response is valid |
| 352 | * @ucd_rsp_ptr: pointer to response UPIU |
| 353 | * |
| 354 | * This function checks the response UPIU for valid transaction type in |
| 355 | * response field |
| 356 | * Returns 0 on success, non-zero on failure |
| 357 | */ |
| 358 | static inline int |
| 359 | ufshcd_is_valid_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 360 | { |
| 361 | return ((be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24) == |
| 362 | UPIU_TRANSACTION_RESPONSE) ? 0 : DID_ERROR << 16; |
| 363 | } |
| 364 | |
| 365 | /** |
| 366 | * ufshcd_get_rsp_upiu_result - Get the result from response UPIU |
| 367 | * @ucd_rsp_ptr: pointer to response UPIU |
| 368 | * |
| 369 | * This function gets the response status and scsi_status from response UPIU |
| 370 | * Returns the response result code. |
| 371 | */ |
| 372 | static inline int |
| 373 | ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr) |
| 374 | { |
| 375 | return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT; |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * ufshcd_config_int_aggr - Configure interrupt aggregation values. |
| 380 | * Currently there is no use case where we want to configure |
| 381 | * interrupt aggregation dynamically. So to configure interrupt |
| 382 | * aggregation, #define INT_AGGR_COUNTER_THRESHOLD_VALUE and |
| 383 | * INT_AGGR_TIMEOUT_VALUE are used. |
| 384 | * @hba: per adapter instance |
| 385 | * @option: Interrupt aggregation option |
| 386 | */ |
| 387 | static inline void |
| 388 | ufshcd_config_int_aggr(struct ufs_hba *hba, int option) |
| 389 | { |
| 390 | switch (option) { |
| 391 | case INT_AGGR_RESET: |
| 392 | writel((INT_AGGR_ENABLE | |
| 393 | INT_AGGR_COUNTER_AND_TIMER_RESET), |
| 394 | (hba->mmio_base + |
| 395 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL)); |
| 396 | break; |
| 397 | case INT_AGGR_CONFIG: |
| 398 | writel((INT_AGGR_ENABLE | |
| 399 | INT_AGGR_PARAM_WRITE | |
| 400 | INT_AGGR_COUNTER_THRESHOLD_VALUE | |
| 401 | INT_AGGR_TIMEOUT_VALUE), |
| 402 | (hba->mmio_base + |
| 403 | REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL)); |
| 404 | break; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * ufshcd_enable_run_stop_reg - Enable run-stop registers, |
| 410 | * When run-stop registers are set to 1, it indicates the |
| 411 | * host controller that it can process the requests |
| 412 | * @hba: per adapter instance |
| 413 | */ |
| 414 | static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) |
| 415 | { |
| 416 | writel(UTP_TASK_REQ_LIST_RUN_STOP_BIT, |
| 417 | (hba->mmio_base + |
| 418 | REG_UTP_TASK_REQ_LIST_RUN_STOP)); |
| 419 | writel(UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, |
| 420 | (hba->mmio_base + |
| 421 | REG_UTP_TRANSFER_REQ_LIST_RUN_STOP)); |
| 422 | } |
| 423 | |
| 424 | /** |
| 425 | * ufshcd_hba_stop - Send controller to reset state |
| 426 | * @hba: per adapter instance |
| 427 | */ |
| 428 | static inline void ufshcd_hba_stop(struct ufs_hba *hba) |
| 429 | { |
| 430 | writel(CONTROLLER_DISABLE, (hba->mmio_base + REG_CONTROLLER_ENABLE)); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * ufshcd_hba_start - Start controller initialization sequence |
| 435 | * @hba: per adapter instance |
| 436 | */ |
| 437 | static inline void ufshcd_hba_start(struct ufs_hba *hba) |
| 438 | { |
| 439 | writel(CONTROLLER_ENABLE , (hba->mmio_base + REG_CONTROLLER_ENABLE)); |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * ufshcd_is_hba_active - Get controller state |
| 444 | * @hba: per adapter instance |
| 445 | * |
| 446 | * Returns zero if controller is active, 1 otherwise |
| 447 | */ |
| 448 | static inline int ufshcd_is_hba_active(struct ufs_hba *hba) |
| 449 | { |
| 450 | return (readl(hba->mmio_base + REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * ufshcd_send_command - Send SCSI or device management commands |
| 455 | * @hba: per adapter instance |
| 456 | * @task_tag: Task tag of the command |
| 457 | */ |
| 458 | static inline |
| 459 | void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag) |
| 460 | { |
| 461 | __set_bit(task_tag, &hba->outstanding_reqs); |
| 462 | writel((1 << task_tag), |
| 463 | (hba->mmio_base + REG_UTP_TRANSFER_REQ_DOOR_BELL)); |
| 464 | } |
| 465 | |
| 466 | /** |
| 467 | * ufshcd_copy_sense_data - Copy sense data in case of check condition |
| 468 | * @lrb - pointer to local reference block |
| 469 | */ |
| 470 | static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp) |
| 471 | { |
| 472 | int len; |
| 473 | if (lrbp->sense_buffer) { |
| 474 | len = be16_to_cpu(lrbp->ucd_rsp_ptr->sense_data_len); |
| 475 | memcpy(lrbp->sense_buffer, |
| 476 | lrbp->ucd_rsp_ptr->sense_data, |
| 477 | min_t(int, len, SCSI_SENSE_BUFFERSIZE)); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | /** |
| 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 | { |
| 487 | hba->capabilities = |
| 488 | readl(hba->mmio_base + REG_CONTROLLER_CAPABILITIES); |
| 489 | |
| 490 | /* nutrs and nutmrs are 0 based values */ |
| 491 | hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; |
| 492 | hba->nutmrs = |
| 493 | ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; |
| 494 | } |
| 495 | |
| 496 | /** |
| 497 | * ufshcd_send_uic_command - Send UIC commands to unipro layers |
| 498 | * @hba: per adapter instance |
| 499 | * @uic_command: UIC command |
| 500 | */ |
| 501 | static inline void |
| 502 | ufshcd_send_uic_command(struct ufs_hba *hba, struct uic_command *uic_cmnd) |
| 503 | { |
| 504 | /* Write Args */ |
| 505 | writel(uic_cmnd->argument1, |
| 506 | (hba->mmio_base + REG_UIC_COMMAND_ARG_1)); |
| 507 | writel(uic_cmnd->argument2, |
| 508 | (hba->mmio_base + REG_UIC_COMMAND_ARG_2)); |
| 509 | writel(uic_cmnd->argument3, |
| 510 | (hba->mmio_base + REG_UIC_COMMAND_ARG_3)); |
| 511 | |
| 512 | /* Write UIC Cmd */ |
| 513 | writel((uic_cmnd->command & COMMAND_OPCODE_MASK), |
| 514 | (hba->mmio_base + REG_UIC_COMMAND)); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * ufshcd_map_sg - Map scatter-gather list to prdt |
| 519 | * @lrbp - pointer to local reference block |
| 520 | * |
| 521 | * Returns 0 in case of success, non-zero value in case of failure |
| 522 | */ |
| 523 | static int ufshcd_map_sg(struct ufshcd_lrb *lrbp) |
| 524 | { |
| 525 | struct ufshcd_sg_entry *prd_table; |
| 526 | struct scatterlist *sg; |
| 527 | struct scsi_cmnd *cmd; |
| 528 | int sg_segments; |
| 529 | int i; |
| 530 | |
| 531 | cmd = lrbp->cmd; |
| 532 | sg_segments = scsi_dma_map(cmd); |
| 533 | if (sg_segments < 0) |
| 534 | return sg_segments; |
| 535 | |
| 536 | if (sg_segments) { |
| 537 | lrbp->utr_descriptor_ptr->prd_table_length = |
| 538 | cpu_to_le16((u16) (sg_segments)); |
| 539 | |
| 540 | prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr; |
| 541 | |
| 542 | scsi_for_each_sg(cmd, sg, sg_segments, i) { |
| 543 | prd_table[i].size = |
| 544 | cpu_to_le32(((u32) sg_dma_len(sg))-1); |
| 545 | prd_table[i].base_addr = |
| 546 | cpu_to_le32(lower_32_bits(sg->dma_address)); |
| 547 | prd_table[i].upper_addr = |
| 548 | cpu_to_le32(upper_32_bits(sg->dma_address)); |
| 549 | } |
| 550 | } else { |
| 551 | lrbp->utr_descriptor_ptr->prd_table_length = 0; |
| 552 | } |
| 553 | |
| 554 | return 0; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * ufshcd_int_config - enable/disable interrupts |
| 559 | * @hba: per adapter instance |
| 560 | * @option: interrupt option |
| 561 | */ |
| 562 | static void ufshcd_int_config(struct ufs_hba *hba, u32 option) |
| 563 | { |
| 564 | switch (option) { |
| 565 | case UFSHCD_INT_ENABLE: |
| 566 | writel(hba->int_enable_mask, |
| 567 | (hba->mmio_base + REG_INTERRUPT_ENABLE)); |
| 568 | break; |
| 569 | case UFSHCD_INT_DISABLE: |
| 570 | if (hba->ufs_version == UFSHCI_VERSION_10) |
| 571 | writel(INTERRUPT_DISABLE_MASK_10, |
| 572 | (hba->mmio_base + REG_INTERRUPT_ENABLE)); |
| 573 | else |
| 574 | writel(INTERRUPT_DISABLE_MASK_11, |
| 575 | (hba->mmio_base + REG_INTERRUPT_ENABLE)); |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | |
| 580 | /** |
| 581 | * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU) |
| 582 | * @lrb - pointer to local reference block |
| 583 | */ |
| 584 | static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp) |
| 585 | { |
| 586 | struct utp_transfer_req_desc *req_desc; |
| 587 | struct utp_upiu_cmd *ucd_cmd_ptr; |
| 588 | u32 data_direction; |
| 589 | u32 upiu_flags; |
| 590 | |
| 591 | ucd_cmd_ptr = lrbp->ucd_cmd_ptr; |
| 592 | req_desc = lrbp->utr_descriptor_ptr; |
| 593 | |
| 594 | switch (lrbp->command_type) { |
| 595 | case UTP_CMD_TYPE_SCSI: |
| 596 | if (lrbp->cmd->sc_data_direction == DMA_FROM_DEVICE) { |
| 597 | data_direction = UTP_DEVICE_TO_HOST; |
| 598 | upiu_flags = UPIU_CMD_FLAGS_READ; |
| 599 | } else if (lrbp->cmd->sc_data_direction == DMA_TO_DEVICE) { |
| 600 | data_direction = UTP_HOST_TO_DEVICE; |
| 601 | upiu_flags = UPIU_CMD_FLAGS_WRITE; |
| 602 | } else { |
| 603 | data_direction = UTP_NO_DATA_TRANSFER; |
| 604 | upiu_flags = UPIU_CMD_FLAGS_NONE; |
| 605 | } |
| 606 | |
| 607 | /* Transfer request descriptor header fields */ |
| 608 | req_desc->header.dword_0 = |
| 609 | cpu_to_le32(data_direction | UTP_SCSI_COMMAND); |
| 610 | |
| 611 | /* |
| 612 | * assigning invalid value for command status. Controller |
| 613 | * updates OCS on command completion, with the command |
| 614 | * status |
| 615 | */ |
| 616 | req_desc->header.dword_2 = |
| 617 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 618 | |
| 619 | /* command descriptor fields */ |
| 620 | ucd_cmd_ptr->header.dword_0 = |
| 621 | cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND, |
| 622 | upiu_flags, |
| 623 | lrbp->lun, |
| 624 | lrbp->task_tag)); |
| 625 | ucd_cmd_ptr->header.dword_1 = |
| 626 | cpu_to_be32( |
| 627 | UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI, |
| 628 | 0, |
| 629 | 0, |
| 630 | 0)); |
| 631 | |
| 632 | /* Total EHS length and Data segment length will be zero */ |
| 633 | ucd_cmd_ptr->header.dword_2 = 0; |
| 634 | |
| 635 | ucd_cmd_ptr->exp_data_transfer_len = |
| 636 | cpu_to_be32(lrbp->cmd->transfersize); |
| 637 | |
| 638 | memcpy(ucd_cmd_ptr->cdb, |
| 639 | lrbp->cmd->cmnd, |
| 640 | (min_t(unsigned short, |
| 641 | lrbp->cmd->cmd_len, |
| 642 | MAX_CDB_SIZE))); |
| 643 | break; |
| 644 | case UTP_CMD_TYPE_DEV_MANAGE: |
| 645 | /* For query function implementation */ |
| 646 | break; |
| 647 | case UTP_CMD_TYPE_UFS: |
| 648 | /* For UFS native command implementation */ |
| 649 | break; |
| 650 | } /* end of switch */ |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * ufshcd_queuecommand - main entry point for SCSI requests |
| 655 | * @cmd: command from SCSI Midlayer |
| 656 | * @done: call back function |
| 657 | * |
| 658 | * Returns 0 for success, non-zero in case of failure |
| 659 | */ |
| 660 | static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd) |
| 661 | { |
| 662 | struct ufshcd_lrb *lrbp; |
| 663 | struct ufs_hba *hba; |
| 664 | unsigned long flags; |
| 665 | int tag; |
| 666 | int err = 0; |
| 667 | |
| 668 | hba = shost_priv(host); |
| 669 | |
| 670 | tag = cmd->request->tag; |
| 671 | |
| 672 | if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { |
| 673 | err = SCSI_MLQUEUE_HOST_BUSY; |
| 674 | goto out; |
| 675 | } |
| 676 | |
| 677 | lrbp = &hba->lrb[tag]; |
| 678 | |
| 679 | lrbp->cmd = cmd; |
| 680 | lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE; |
| 681 | lrbp->sense_buffer = cmd->sense_buffer; |
| 682 | lrbp->task_tag = tag; |
| 683 | lrbp->lun = cmd->device->lun; |
| 684 | |
| 685 | lrbp->command_type = UTP_CMD_TYPE_SCSI; |
| 686 | |
| 687 | /* form UPIU before issuing the command */ |
| 688 | ufshcd_compose_upiu(lrbp); |
| 689 | err = ufshcd_map_sg(lrbp); |
| 690 | if (err) |
| 691 | goto out; |
| 692 | |
| 693 | /* issue command to the controller */ |
| 694 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 695 | ufshcd_send_command(hba, tag); |
| 696 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 697 | out: |
| 698 | return err; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * ufshcd_memory_alloc - allocate memory for host memory space data structures |
| 703 | * @hba: per adapter instance |
| 704 | * |
| 705 | * 1. Allocate DMA memory for Command Descriptor array |
| 706 | * Each command descriptor consist of Command UPIU, Response UPIU and PRDT |
| 707 | * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL). |
| 708 | * 3. Allocate DMA memory for UTP Task Management Request Descriptor List |
| 709 | * (UTMRDL) |
| 710 | * 4. Allocate memory for local reference block(lrb). |
| 711 | * |
| 712 | * Returns 0 for success, non-zero in case of failure |
| 713 | */ |
| 714 | static int ufshcd_memory_alloc(struct ufs_hba *hba) |
| 715 | { |
| 716 | size_t utmrdl_size, utrdl_size, ucdl_size; |
| 717 | |
| 718 | /* Allocate memory for UTP command descriptors */ |
| 719 | ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 720 | hba->ucdl_base_addr = dma_alloc_coherent(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 721 | ucdl_size, |
| 722 | &hba->ucdl_dma_addr, |
| 723 | GFP_KERNEL); |
| 724 | |
| 725 | /* |
| 726 | * UFSHCI requires UTP command descriptor to be 128 byte aligned. |
| 727 | * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE |
| 728 | * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will |
| 729 | * be aligned to 128 bytes as well |
| 730 | */ |
| 731 | if (!hba->ucdl_base_addr || |
| 732 | WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 733 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 734 | "Command Descriptor Memory allocation failed\n"); |
| 735 | goto out; |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Allocate memory for UTP Transfer descriptors |
| 740 | * UFSHCI requires 1024 byte alignment of UTRD |
| 741 | */ |
| 742 | utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 743 | hba->utrdl_base_addr = dma_alloc_coherent(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 744 | utrdl_size, |
| 745 | &hba->utrdl_dma_addr, |
| 746 | GFP_KERNEL); |
| 747 | if (!hba->utrdl_base_addr || |
| 748 | WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 749 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 750 | "Transfer Descriptor Memory allocation failed\n"); |
| 751 | goto out; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Allocate memory for UTP Task Management descriptors |
| 756 | * UFSHCI requires 1024 byte alignment of UTMRD |
| 757 | */ |
| 758 | utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 759 | hba->utmrdl_base_addr = dma_alloc_coherent(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 760 | utmrdl_size, |
| 761 | &hba->utmrdl_dma_addr, |
| 762 | GFP_KERNEL); |
| 763 | if (!hba->utmrdl_base_addr || |
| 764 | WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 765 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 766 | "Task Management Descriptor Memory allocation failed\n"); |
| 767 | goto out; |
| 768 | } |
| 769 | |
| 770 | /* Allocate memory for local reference block */ |
| 771 | hba->lrb = kcalloc(hba->nutrs, sizeof(struct ufshcd_lrb), GFP_KERNEL); |
| 772 | if (!hba->lrb) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 773 | dev_err(hba->dev, "LRB Memory allocation failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 774 | goto out; |
| 775 | } |
| 776 | return 0; |
| 777 | out: |
| 778 | ufshcd_free_hba_memory(hba); |
| 779 | return -ENOMEM; |
| 780 | } |
| 781 | |
| 782 | /** |
| 783 | * ufshcd_host_memory_configure - configure local reference block with |
| 784 | * memory offsets |
| 785 | * @hba: per adapter instance |
| 786 | * |
| 787 | * Configure Host memory space |
| 788 | * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA |
| 789 | * address. |
| 790 | * 2. Update each UTRD with Response UPIU offset, Response UPIU length |
| 791 | * and PRDT offset. |
| 792 | * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT |
| 793 | * into local reference block. |
| 794 | */ |
| 795 | static void ufshcd_host_memory_configure(struct ufs_hba *hba) |
| 796 | { |
| 797 | struct utp_transfer_cmd_desc *cmd_descp; |
| 798 | struct utp_transfer_req_desc *utrdlp; |
| 799 | dma_addr_t cmd_desc_dma_addr; |
| 800 | dma_addr_t cmd_desc_element_addr; |
| 801 | u16 response_offset; |
| 802 | u16 prdt_offset; |
| 803 | int cmd_desc_size; |
| 804 | int i; |
| 805 | |
| 806 | utrdlp = hba->utrdl_base_addr; |
| 807 | cmd_descp = hba->ucdl_base_addr; |
| 808 | |
| 809 | response_offset = |
| 810 | offsetof(struct utp_transfer_cmd_desc, response_upiu); |
| 811 | prdt_offset = |
| 812 | offsetof(struct utp_transfer_cmd_desc, prd_table); |
| 813 | |
| 814 | cmd_desc_size = sizeof(struct utp_transfer_cmd_desc); |
| 815 | cmd_desc_dma_addr = hba->ucdl_dma_addr; |
| 816 | |
| 817 | for (i = 0; i < hba->nutrs; i++) { |
| 818 | /* Configure UTRD with command descriptor base address */ |
| 819 | cmd_desc_element_addr = |
| 820 | (cmd_desc_dma_addr + (cmd_desc_size * i)); |
| 821 | utrdlp[i].command_desc_base_addr_lo = |
| 822 | cpu_to_le32(lower_32_bits(cmd_desc_element_addr)); |
| 823 | utrdlp[i].command_desc_base_addr_hi = |
| 824 | cpu_to_le32(upper_32_bits(cmd_desc_element_addr)); |
| 825 | |
| 826 | /* Response upiu and prdt offset should be in double words */ |
| 827 | utrdlp[i].response_upiu_offset = |
| 828 | cpu_to_le16((response_offset >> 2)); |
| 829 | utrdlp[i].prd_table_offset = |
| 830 | cpu_to_le16((prdt_offset >> 2)); |
| 831 | utrdlp[i].response_upiu_length = |
| 832 | cpu_to_le16(ALIGNED_UPIU_SIZE); |
| 833 | |
| 834 | hba->lrb[i].utr_descriptor_ptr = (utrdlp + i); |
| 835 | hba->lrb[i].ucd_cmd_ptr = |
| 836 | (struct utp_upiu_cmd *)(cmd_descp + i); |
| 837 | hba->lrb[i].ucd_rsp_ptr = |
| 838 | (struct utp_upiu_rsp *)cmd_descp[i].response_upiu; |
| 839 | hba->lrb[i].ucd_prdt_ptr = |
| 840 | (struct ufshcd_sg_entry *)cmd_descp[i].prd_table; |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | /** |
| 845 | * ufshcd_dme_link_startup - Notify Unipro to perform link startup |
| 846 | * @hba: per adapter instance |
| 847 | * |
| 848 | * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer, |
| 849 | * in order to initialize the Unipro link startup procedure. |
| 850 | * Once the Unipro links are up, the device connected to the controller |
| 851 | * is detected. |
| 852 | * |
| 853 | * Returns 0 on success, non-zero value on failure |
| 854 | */ |
| 855 | static int ufshcd_dme_link_startup(struct ufs_hba *hba) |
| 856 | { |
| 857 | struct uic_command *uic_cmd; |
| 858 | unsigned long flags; |
| 859 | |
| 860 | /* check if controller is ready to accept UIC commands */ |
| 861 | if (((readl(hba->mmio_base + REG_CONTROLLER_STATUS)) & |
| 862 | UIC_COMMAND_READY) == 0x0) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 863 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 864 | "Controller not ready" |
| 865 | " to accept UIC commands\n"); |
| 866 | return -EIO; |
| 867 | } |
| 868 | |
| 869 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 870 | |
| 871 | /* form UIC command */ |
| 872 | uic_cmd = &hba->active_uic_cmd; |
| 873 | uic_cmd->command = UIC_CMD_DME_LINK_STARTUP; |
| 874 | uic_cmd->argument1 = 0; |
| 875 | uic_cmd->argument2 = 0; |
| 876 | uic_cmd->argument3 = 0; |
| 877 | |
| 878 | /* enable UIC related interrupts */ |
| 879 | hba->int_enable_mask |= UIC_COMMAND_COMPL; |
| 880 | ufshcd_int_config(hba, UFSHCD_INT_ENABLE); |
| 881 | |
| 882 | /* sending UIC commands to controller */ |
| 883 | ufshcd_send_uic_command(hba, uic_cmd); |
| 884 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | /** |
| 889 | * ufshcd_make_hba_operational - Make UFS controller operational |
| 890 | * @hba: per adapter instance |
| 891 | * |
| 892 | * To bring UFS host controller to operational state, |
| 893 | * 1. Check if device is present |
| 894 | * 2. Configure run-stop-registers |
| 895 | * 3. Enable required interrupts |
| 896 | * 4. Configure interrupt aggregation |
| 897 | * |
| 898 | * Returns 0 on success, non-zero value on failure |
| 899 | */ |
| 900 | static int ufshcd_make_hba_operational(struct ufs_hba *hba) |
| 901 | { |
| 902 | int err = 0; |
| 903 | u32 reg; |
| 904 | |
| 905 | /* check if device present */ |
| 906 | reg = readl((hba->mmio_base + REG_CONTROLLER_STATUS)); |
Venkatraman S | 73ec513 | 2012-07-10 19:39:23 +0530 | [diff] [blame] | 907 | if (!ufshcd_is_device_present(reg)) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 908 | dev_err(hba->dev, "cc: Device not present\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 909 | err = -ENXIO; |
| 910 | goto out; |
| 911 | } |
| 912 | |
| 913 | /* |
| 914 | * UCRDY, UTMRLDY and UTRLRDY bits must be 1 |
| 915 | * DEI, HEI bits must be 0 |
| 916 | */ |
| 917 | if (!(ufshcd_get_lists_status(reg))) { |
| 918 | ufshcd_enable_run_stop_reg(hba); |
| 919 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 920 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 921 | "Host controller not ready to process requests"); |
| 922 | err = -EIO; |
| 923 | goto out; |
| 924 | } |
| 925 | |
| 926 | /* Enable required interrupts */ |
| 927 | hba->int_enable_mask |= (UTP_TRANSFER_REQ_COMPL | |
| 928 | UIC_ERROR | |
| 929 | UTP_TASK_REQ_COMPL | |
| 930 | DEVICE_FATAL_ERROR | |
| 931 | CONTROLLER_FATAL_ERROR | |
| 932 | SYSTEM_BUS_FATAL_ERROR); |
| 933 | ufshcd_int_config(hba, UFSHCD_INT_ENABLE); |
| 934 | |
| 935 | /* Configure interrupt aggregation */ |
| 936 | ufshcd_config_int_aggr(hba, INT_AGGR_CONFIG); |
| 937 | |
| 938 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
| 939 | scsi_unblock_requests(hba->host); |
| 940 | |
| 941 | hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; |
| 942 | scsi_scan_host(hba->host); |
| 943 | out: |
| 944 | return err; |
| 945 | } |
| 946 | |
| 947 | /** |
| 948 | * ufshcd_hba_enable - initialize the controller |
| 949 | * @hba: per adapter instance |
| 950 | * |
| 951 | * The controller resets itself and controller firmware initialization |
| 952 | * sequence kicks off. When controller is ready it will set |
| 953 | * the Host Controller Enable bit to 1. |
| 954 | * |
| 955 | * Returns 0 on success, non-zero value on failure |
| 956 | */ |
| 957 | static int ufshcd_hba_enable(struct ufs_hba *hba) |
| 958 | { |
| 959 | int retry; |
| 960 | |
| 961 | /* |
| 962 | * msleep of 1 and 5 used in this function might result in msleep(20), |
| 963 | * but it was necessary to send the UFS FPGA to reset mode during |
| 964 | * development and testing of this driver. msleep can be changed to |
| 965 | * mdelay and retry count can be reduced based on the controller. |
| 966 | */ |
| 967 | if (!ufshcd_is_hba_active(hba)) { |
| 968 | |
| 969 | /* change controller state to "reset state" */ |
| 970 | ufshcd_hba_stop(hba); |
| 971 | |
| 972 | /* |
| 973 | * This delay is based on the testing done with UFS host |
| 974 | * controller FPGA. The delay can be changed based on the |
| 975 | * host controller used. |
| 976 | */ |
| 977 | msleep(5); |
| 978 | } |
| 979 | |
| 980 | /* start controller initialization sequence */ |
| 981 | ufshcd_hba_start(hba); |
| 982 | |
| 983 | /* |
| 984 | * To initialize a UFS host controller HCE bit must be set to 1. |
| 985 | * During initialization the HCE bit value changes from 1->0->1. |
| 986 | * When the host controller completes initialization sequence |
| 987 | * it sets the value of HCE bit to 1. The same HCE bit is read back |
| 988 | * to check if the controller has completed initialization sequence. |
| 989 | * So without this delay the value HCE = 1, set in the previous |
| 990 | * instruction might be read back. |
| 991 | * This delay can be changed based on the controller. |
| 992 | */ |
| 993 | msleep(1); |
| 994 | |
| 995 | /* wait for the host controller to complete initialization */ |
| 996 | retry = 10; |
| 997 | while (ufshcd_is_hba_active(hba)) { |
| 998 | if (retry) { |
| 999 | retry--; |
| 1000 | } else { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1001 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1002 | "Controller enable failed\n"); |
| 1003 | return -EIO; |
| 1004 | } |
| 1005 | msleep(5); |
| 1006 | } |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
| 1011 | * ufshcd_initialize_hba - start the initialization process |
| 1012 | * @hba: per adapter instance |
| 1013 | * |
| 1014 | * 1. Enable the controller via ufshcd_hba_enable. |
| 1015 | * 2. Program the Transfer Request List Address with the starting address of |
| 1016 | * UTRDL. |
| 1017 | * 3. Program the Task Management Request List Address with starting address |
| 1018 | * of UTMRDL. |
| 1019 | * |
| 1020 | * Returns 0 on success, non-zero value on failure. |
| 1021 | */ |
| 1022 | static int ufshcd_initialize_hba(struct ufs_hba *hba) |
| 1023 | { |
| 1024 | if (ufshcd_hba_enable(hba)) |
| 1025 | return -EIO; |
| 1026 | |
| 1027 | /* Configure UTRL and UTMRL base address registers */ |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1028 | writel(lower_32_bits(hba->utrdl_dma_addr), |
Santosh Yaraganavi | 85bb445 | 2012-04-23 18:52:11 +0530 | [diff] [blame] | 1029 | (hba->mmio_base + REG_UTP_TRANSFER_REQ_LIST_BASE_L)); |
| 1030 | writel(upper_32_bits(hba->utrdl_dma_addr), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1031 | (hba->mmio_base + REG_UTP_TRANSFER_REQ_LIST_BASE_H)); |
Santosh Yaraganavi | 85bb445 | 2012-04-23 18:52:11 +0530 | [diff] [blame] | 1032 | writel(lower_32_bits(hba->utmrdl_dma_addr), |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1033 | (hba->mmio_base + REG_UTP_TASK_REQ_LIST_BASE_L)); |
| 1034 | writel(upper_32_bits(hba->utmrdl_dma_addr), |
| 1035 | (hba->mmio_base + REG_UTP_TASK_REQ_LIST_BASE_H)); |
| 1036 | |
| 1037 | /* Initialize unipro link startup procedure */ |
| 1038 | return ufshcd_dme_link_startup(hba); |
| 1039 | } |
| 1040 | |
| 1041 | /** |
| 1042 | * ufshcd_do_reset - reset the host controller |
| 1043 | * @hba: per adapter instance |
| 1044 | * |
| 1045 | * Returns SUCCESS/FAILED |
| 1046 | */ |
| 1047 | static int ufshcd_do_reset(struct ufs_hba *hba) |
| 1048 | { |
| 1049 | struct ufshcd_lrb *lrbp; |
| 1050 | unsigned long flags; |
| 1051 | int tag; |
| 1052 | |
| 1053 | /* block commands from midlayer */ |
| 1054 | scsi_block_requests(hba->host); |
| 1055 | |
| 1056 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1057 | hba->ufshcd_state = UFSHCD_STATE_RESET; |
| 1058 | |
| 1059 | /* send controller to reset state */ |
| 1060 | ufshcd_hba_stop(hba); |
| 1061 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1062 | |
| 1063 | /* abort outstanding commands */ |
| 1064 | for (tag = 0; tag < hba->nutrs; tag++) { |
| 1065 | if (test_bit(tag, &hba->outstanding_reqs)) { |
| 1066 | lrbp = &hba->lrb[tag]; |
| 1067 | scsi_dma_unmap(lrbp->cmd); |
| 1068 | lrbp->cmd->result = DID_RESET << 16; |
| 1069 | lrbp->cmd->scsi_done(lrbp->cmd); |
| 1070 | lrbp->cmd = NULL; |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /* clear outstanding request/task bit maps */ |
| 1075 | hba->outstanding_reqs = 0; |
| 1076 | hba->outstanding_tasks = 0; |
| 1077 | |
| 1078 | /* start the initialization process */ |
| 1079 | if (ufshcd_initialize_hba(hba)) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1080 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1081 | "Reset: Controller initialization failed\n"); |
| 1082 | return FAILED; |
| 1083 | } |
| 1084 | return SUCCESS; |
| 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * ufshcd_slave_alloc - handle initial SCSI device configurations |
| 1089 | * @sdev: pointer to SCSI device |
| 1090 | * |
| 1091 | * Returns success |
| 1092 | */ |
| 1093 | static int ufshcd_slave_alloc(struct scsi_device *sdev) |
| 1094 | { |
| 1095 | struct ufs_hba *hba; |
| 1096 | |
| 1097 | hba = shost_priv(sdev->host); |
| 1098 | sdev->tagged_supported = 1; |
| 1099 | |
| 1100 | /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */ |
| 1101 | sdev->use_10_for_ms = 1; |
| 1102 | scsi_set_tag_type(sdev, MSG_SIMPLE_TAG); |
| 1103 | |
| 1104 | /* |
| 1105 | * Inform SCSI Midlayer that the LUN queue depth is same as the |
| 1106 | * controller queue depth. If a LUN queue depth is less than the |
| 1107 | * controller queue depth and if the LUN reports |
| 1108 | * SAM_STAT_TASK_SET_FULL, the LUN queue depth will be adjusted |
| 1109 | * with scsi_adjust_queue_depth. |
| 1110 | */ |
| 1111 | scsi_activate_tcq(sdev, hba->nutrs); |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
| 1116 | * ufshcd_slave_destroy - remove SCSI device configurations |
| 1117 | * @sdev: pointer to SCSI device |
| 1118 | */ |
| 1119 | static void ufshcd_slave_destroy(struct scsi_device *sdev) |
| 1120 | { |
| 1121 | struct ufs_hba *hba; |
| 1122 | |
| 1123 | hba = shost_priv(sdev->host); |
| 1124 | scsi_deactivate_tcq(sdev, hba->nutrs); |
| 1125 | } |
| 1126 | |
| 1127 | /** |
| 1128 | * ufshcd_task_req_compl - handle task management request completion |
| 1129 | * @hba: per adapter instance |
| 1130 | * @index: index of the completed request |
| 1131 | * |
| 1132 | * Returns SUCCESS/FAILED |
| 1133 | */ |
| 1134 | static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index) |
| 1135 | { |
| 1136 | struct utp_task_req_desc *task_req_descp; |
| 1137 | struct utp_upiu_task_rsp *task_rsp_upiup; |
| 1138 | unsigned long flags; |
| 1139 | int ocs_value; |
| 1140 | int task_result; |
| 1141 | |
| 1142 | spin_lock_irqsave(hba->host->host_lock, flags); |
| 1143 | |
| 1144 | /* Clear completed tasks from outstanding_tasks */ |
| 1145 | __clear_bit(index, &hba->outstanding_tasks); |
| 1146 | |
| 1147 | task_req_descp = hba->utmrdl_base_addr; |
| 1148 | ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]); |
| 1149 | |
| 1150 | if (ocs_value == OCS_SUCCESS) { |
| 1151 | task_rsp_upiup = (struct utp_upiu_task_rsp *) |
| 1152 | task_req_descp[index].task_rsp_upiu; |
| 1153 | task_result = be32_to_cpu(task_rsp_upiup->header.dword_1); |
| 1154 | task_result = ((task_result & MASK_TASK_RESPONSE) >> 8); |
| 1155 | |
Venkatraman S | fd0f837 | 2012-04-19 11:46:22 +0530 | [diff] [blame] | 1156 | if (task_result != UPIU_TASK_MANAGEMENT_FUNC_COMPL && |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1157 | task_result != UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) |
| 1158 | task_result = FAILED; |
Namjae Jeon | 94c122a | 2012-07-10 20:41:54 +0530 | [diff] [blame] | 1159 | else |
| 1160 | task_result = SUCCESS; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1161 | } else { |
| 1162 | task_result = FAILED; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1163 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1164 | "trc: Invalid ocs = %x\n", ocs_value); |
| 1165 | } |
| 1166 | spin_unlock_irqrestore(hba->host->host_lock, flags); |
| 1167 | return task_result; |
| 1168 | } |
| 1169 | |
| 1170 | /** |
| 1171 | * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with |
| 1172 | * SAM_STAT_TASK_SET_FULL SCSI command status. |
| 1173 | * @cmd: pointer to SCSI command |
| 1174 | */ |
| 1175 | static void ufshcd_adjust_lun_qdepth(struct scsi_cmnd *cmd) |
| 1176 | { |
| 1177 | struct ufs_hba *hba; |
| 1178 | int i; |
| 1179 | int lun_qdepth = 0; |
| 1180 | |
| 1181 | hba = shost_priv(cmd->device->host); |
| 1182 | |
| 1183 | /* |
| 1184 | * LUN queue depth can be obtained by counting outstanding commands |
| 1185 | * on the LUN. |
| 1186 | */ |
| 1187 | for (i = 0; i < hba->nutrs; i++) { |
| 1188 | if (test_bit(i, &hba->outstanding_reqs)) { |
| 1189 | |
| 1190 | /* |
| 1191 | * Check if the outstanding command belongs |
| 1192 | * to the LUN which reported SAM_STAT_TASK_SET_FULL. |
| 1193 | */ |
| 1194 | if (cmd->device->lun == hba->lrb[i].lun) |
| 1195 | lun_qdepth++; |
| 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /* |
| 1200 | * LUN queue depth will be total outstanding commands, except the |
| 1201 | * command for which the LUN reported SAM_STAT_TASK_SET_FULL. |
| 1202 | */ |
| 1203 | scsi_adjust_queue_depth(cmd->device, MSG_SIMPLE_TAG, lun_qdepth - 1); |
| 1204 | } |
| 1205 | |
| 1206 | /** |
| 1207 | * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status |
| 1208 | * @lrb: pointer to local reference block of completed command |
| 1209 | * @scsi_status: SCSI command status |
| 1210 | * |
| 1211 | * Returns value base on SCSI command status |
| 1212 | */ |
| 1213 | static inline int |
| 1214 | ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status) |
| 1215 | { |
| 1216 | int result = 0; |
| 1217 | |
| 1218 | switch (scsi_status) { |
| 1219 | case SAM_STAT_GOOD: |
| 1220 | result |= DID_OK << 16 | |
| 1221 | COMMAND_COMPLETE << 8 | |
| 1222 | SAM_STAT_GOOD; |
| 1223 | break; |
| 1224 | case SAM_STAT_CHECK_CONDITION: |
| 1225 | result |= DID_OK << 16 | |
| 1226 | COMMAND_COMPLETE << 8 | |
| 1227 | SAM_STAT_CHECK_CONDITION; |
| 1228 | ufshcd_copy_sense_data(lrbp); |
| 1229 | break; |
| 1230 | case SAM_STAT_BUSY: |
| 1231 | result |= SAM_STAT_BUSY; |
| 1232 | break; |
| 1233 | case SAM_STAT_TASK_SET_FULL: |
| 1234 | |
| 1235 | /* |
| 1236 | * If a LUN reports SAM_STAT_TASK_SET_FULL, then the LUN queue |
| 1237 | * depth needs to be adjusted to the exact number of |
| 1238 | * outstanding commands the LUN can handle at any given time. |
| 1239 | */ |
| 1240 | ufshcd_adjust_lun_qdepth(lrbp->cmd); |
| 1241 | result |= SAM_STAT_TASK_SET_FULL; |
| 1242 | break; |
| 1243 | case SAM_STAT_TASK_ABORTED: |
| 1244 | result |= SAM_STAT_TASK_ABORTED; |
| 1245 | break; |
| 1246 | default: |
| 1247 | result |= DID_ERROR << 16; |
| 1248 | break; |
| 1249 | } /* end of switch */ |
| 1250 | |
| 1251 | return result; |
| 1252 | } |
| 1253 | |
| 1254 | /** |
| 1255 | * ufshcd_transfer_rsp_status - Get overall status of the response |
| 1256 | * @hba: per adapter instance |
| 1257 | * @lrb: pointer to local reference block of completed command |
| 1258 | * |
| 1259 | * Returns result of the command to notify SCSI midlayer |
| 1260 | */ |
| 1261 | static inline int |
| 1262 | ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) |
| 1263 | { |
| 1264 | int result = 0; |
| 1265 | int scsi_status; |
| 1266 | int ocs; |
| 1267 | |
| 1268 | /* overall command status of utrd */ |
| 1269 | ocs = ufshcd_get_tr_ocs(lrbp); |
| 1270 | |
| 1271 | switch (ocs) { |
| 1272 | case OCS_SUCCESS: |
| 1273 | |
| 1274 | /* check if the returned transfer response is valid */ |
| 1275 | result = ufshcd_is_valid_req_rsp(lrbp->ucd_rsp_ptr); |
| 1276 | if (result) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1277 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1278 | "Invalid response = %x\n", result); |
| 1279 | break; |
| 1280 | } |
| 1281 | |
| 1282 | /* |
| 1283 | * get the response UPIU result to extract |
| 1284 | * the SCSI command status |
| 1285 | */ |
| 1286 | result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr); |
| 1287 | |
| 1288 | /* |
| 1289 | * get the result based on SCSI status response |
| 1290 | * to notify the SCSI midlayer of the command status |
| 1291 | */ |
| 1292 | scsi_status = result & MASK_SCSI_STATUS; |
| 1293 | result = ufshcd_scsi_cmd_status(lrbp, scsi_status); |
| 1294 | break; |
| 1295 | case OCS_ABORTED: |
| 1296 | result |= DID_ABORT << 16; |
| 1297 | break; |
| 1298 | case OCS_INVALID_CMD_TABLE_ATTR: |
| 1299 | case OCS_INVALID_PRDT_ATTR: |
| 1300 | case OCS_MISMATCH_DATA_BUF_SIZE: |
| 1301 | case OCS_MISMATCH_RESP_UPIU_SIZE: |
| 1302 | case OCS_PEER_COMM_FAILURE: |
| 1303 | case OCS_FATAL_ERROR: |
| 1304 | default: |
| 1305 | result |= DID_ERROR << 16; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1306 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1307 | "OCS error from controller = %x\n", ocs); |
| 1308 | break; |
| 1309 | } /* end of switch */ |
| 1310 | |
| 1311 | return result; |
| 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * ufshcd_transfer_req_compl - handle SCSI and query command completion |
| 1316 | * @hba: per adapter instance |
| 1317 | */ |
| 1318 | static void ufshcd_transfer_req_compl(struct ufs_hba *hba) |
| 1319 | { |
| 1320 | struct ufshcd_lrb *lrb; |
| 1321 | unsigned long completed_reqs; |
| 1322 | u32 tr_doorbell; |
| 1323 | int result; |
| 1324 | int index; |
| 1325 | |
| 1326 | lrb = hba->lrb; |
| 1327 | tr_doorbell = |
| 1328 | readl(hba->mmio_base + REG_UTP_TRANSFER_REQ_DOOR_BELL); |
| 1329 | completed_reqs = tr_doorbell ^ hba->outstanding_reqs; |
| 1330 | |
| 1331 | for (index = 0; index < hba->nutrs; index++) { |
| 1332 | if (test_bit(index, &completed_reqs)) { |
| 1333 | |
| 1334 | result = ufshcd_transfer_rsp_status(hba, &lrb[index]); |
| 1335 | |
| 1336 | if (lrb[index].cmd) { |
| 1337 | scsi_dma_unmap(lrb[index].cmd); |
| 1338 | lrb[index].cmd->result = result; |
| 1339 | lrb[index].cmd->scsi_done(lrb[index].cmd); |
| 1340 | |
| 1341 | /* Mark completed command as NULL in LRB */ |
| 1342 | lrb[index].cmd = NULL; |
| 1343 | } |
| 1344 | } /* end of if */ |
| 1345 | } /* end of for */ |
| 1346 | |
| 1347 | /* clear corresponding bits of completed commands */ |
| 1348 | hba->outstanding_reqs ^= completed_reqs; |
| 1349 | |
| 1350 | /* Reset interrupt aggregation counters */ |
| 1351 | ufshcd_config_int_aggr(hba, INT_AGGR_RESET); |
| 1352 | } |
| 1353 | |
| 1354 | /** |
| 1355 | * ufshcd_uic_cc_handler - handle UIC command completion |
| 1356 | * @work: pointer to a work queue structure |
| 1357 | * |
| 1358 | * Returns 0 on success, non-zero value on failure |
| 1359 | */ |
| 1360 | static void ufshcd_uic_cc_handler (struct work_struct *work) |
| 1361 | { |
| 1362 | struct ufs_hba *hba; |
| 1363 | |
| 1364 | hba = container_of(work, struct ufs_hba, uic_workq); |
| 1365 | |
| 1366 | if ((hba->active_uic_cmd.command == UIC_CMD_DME_LINK_STARTUP) && |
| 1367 | !(ufshcd_get_uic_cmd_result(hba))) { |
| 1368 | |
| 1369 | if (ufshcd_make_hba_operational(hba)) |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1370 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1371 | "cc: hba not operational state\n"); |
| 1372 | return; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * ufshcd_fatal_err_handler - handle fatal errors |
| 1378 | * @hba: per adapter instance |
| 1379 | */ |
| 1380 | static void ufshcd_fatal_err_handler(struct work_struct *work) |
| 1381 | { |
| 1382 | struct ufs_hba *hba; |
| 1383 | hba = container_of(work, struct ufs_hba, feh_workq); |
| 1384 | |
| 1385 | /* check if reset is already in progress */ |
| 1386 | if (hba->ufshcd_state != UFSHCD_STATE_RESET) |
| 1387 | ufshcd_do_reset(hba); |
| 1388 | } |
| 1389 | |
| 1390 | /** |
| 1391 | * ufshcd_err_handler - Check for fatal errors |
| 1392 | * @work: pointer to a work queue structure |
| 1393 | */ |
| 1394 | static void ufshcd_err_handler(struct ufs_hba *hba) |
| 1395 | { |
| 1396 | u32 reg; |
| 1397 | |
| 1398 | if (hba->errors & INT_FATAL_ERRORS) |
| 1399 | goto fatal_eh; |
| 1400 | |
| 1401 | if (hba->errors & UIC_ERROR) { |
| 1402 | |
| 1403 | reg = readl(hba->mmio_base + |
| 1404 | REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); |
| 1405 | if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT) |
| 1406 | goto fatal_eh; |
| 1407 | } |
| 1408 | return; |
| 1409 | fatal_eh: |
| 1410 | hba->ufshcd_state = UFSHCD_STATE_ERROR; |
| 1411 | schedule_work(&hba->feh_workq); |
| 1412 | } |
| 1413 | |
| 1414 | /** |
| 1415 | * ufshcd_tmc_handler - handle task management function completion |
| 1416 | * @hba: per adapter instance |
| 1417 | */ |
| 1418 | static void ufshcd_tmc_handler(struct ufs_hba *hba) |
| 1419 | { |
| 1420 | u32 tm_doorbell; |
| 1421 | |
| 1422 | tm_doorbell = readl(hba->mmio_base + REG_UTP_TASK_REQ_DOOR_BELL); |
| 1423 | hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks; |
| 1424 | wake_up_interruptible(&hba->ufshcd_tm_wait_queue); |
| 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * ufshcd_sl_intr - Interrupt service routine |
| 1429 | * @hba: per adapter instance |
| 1430 | * @intr_status: contains interrupts generated by the controller |
| 1431 | */ |
| 1432 | static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) |
| 1433 | { |
| 1434 | hba->errors = UFSHCD_ERROR_MASK & intr_status; |
| 1435 | if (hba->errors) |
| 1436 | ufshcd_err_handler(hba); |
| 1437 | |
| 1438 | if (intr_status & UIC_COMMAND_COMPL) |
| 1439 | schedule_work(&hba->uic_workq); |
| 1440 | |
| 1441 | if (intr_status & UTP_TASK_REQ_COMPL) |
| 1442 | ufshcd_tmc_handler(hba); |
| 1443 | |
| 1444 | if (intr_status & UTP_TRANSFER_REQ_COMPL) |
| 1445 | ufshcd_transfer_req_compl(hba); |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * ufshcd_intr - Main interrupt service routine |
| 1450 | * @irq: irq number |
| 1451 | * @__hba: pointer to adapter instance |
| 1452 | * |
| 1453 | * Returns IRQ_HANDLED - If interrupt is valid |
| 1454 | * IRQ_NONE - If invalid interrupt |
| 1455 | */ |
| 1456 | static irqreturn_t ufshcd_intr(int irq, void *__hba) |
| 1457 | { |
| 1458 | u32 intr_status; |
| 1459 | irqreturn_t retval = IRQ_NONE; |
| 1460 | struct ufs_hba *hba = __hba; |
| 1461 | |
| 1462 | spin_lock(hba->host->host_lock); |
| 1463 | intr_status = readl(hba->mmio_base + REG_INTERRUPT_STATUS); |
| 1464 | |
| 1465 | if (intr_status) { |
| 1466 | ufshcd_sl_intr(hba, intr_status); |
| 1467 | |
| 1468 | /* If UFSHCI 1.0 then clear interrupt status register */ |
| 1469 | if (hba->ufs_version == UFSHCI_VERSION_10) |
| 1470 | writel(intr_status, |
| 1471 | (hba->mmio_base + REG_INTERRUPT_STATUS)); |
| 1472 | retval = IRQ_HANDLED; |
| 1473 | } |
| 1474 | spin_unlock(hba->host->host_lock); |
| 1475 | return retval; |
| 1476 | } |
| 1477 | |
| 1478 | /** |
| 1479 | * ufshcd_issue_tm_cmd - issues task management commands to controller |
| 1480 | * @hba: per adapter instance |
| 1481 | * @lrbp: pointer to local reference block |
| 1482 | * |
| 1483 | * Returns SUCCESS/FAILED |
| 1484 | */ |
| 1485 | static int |
| 1486 | ufshcd_issue_tm_cmd(struct ufs_hba *hba, |
| 1487 | struct ufshcd_lrb *lrbp, |
| 1488 | u8 tm_function) |
| 1489 | { |
| 1490 | struct utp_task_req_desc *task_req_descp; |
| 1491 | struct utp_upiu_task_req *task_req_upiup; |
| 1492 | struct Scsi_Host *host; |
| 1493 | unsigned long flags; |
| 1494 | int free_slot = 0; |
| 1495 | int err; |
| 1496 | |
| 1497 | host = hba->host; |
| 1498 | |
| 1499 | spin_lock_irqsave(host->host_lock, flags); |
| 1500 | |
| 1501 | /* If task management queue is full */ |
| 1502 | free_slot = ufshcd_get_tm_free_slot(hba); |
| 1503 | if (free_slot >= hba->nutmrs) { |
| 1504 | spin_unlock_irqrestore(host->host_lock, flags); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1505 | dev_err(hba->dev, "Task management queue full\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1506 | err = FAILED; |
| 1507 | goto out; |
| 1508 | } |
| 1509 | |
| 1510 | task_req_descp = hba->utmrdl_base_addr; |
| 1511 | task_req_descp += free_slot; |
| 1512 | |
| 1513 | /* Configure task request descriptor */ |
| 1514 | task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD); |
| 1515 | task_req_descp->header.dword_2 = |
| 1516 | cpu_to_le32(OCS_INVALID_COMMAND_STATUS); |
| 1517 | |
| 1518 | /* Configure task request UPIU */ |
| 1519 | task_req_upiup = |
| 1520 | (struct utp_upiu_task_req *) task_req_descp->task_req_upiu; |
| 1521 | task_req_upiup->header.dword_0 = |
| 1522 | cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0, |
| 1523 | lrbp->lun, lrbp->task_tag)); |
| 1524 | task_req_upiup->header.dword_1 = |
| 1525 | cpu_to_be32(UPIU_HEADER_DWORD(0, tm_function, 0, 0)); |
| 1526 | |
| 1527 | task_req_upiup->input_param1 = lrbp->lun; |
| 1528 | task_req_upiup->input_param1 = |
| 1529 | cpu_to_be32(task_req_upiup->input_param1); |
| 1530 | task_req_upiup->input_param2 = lrbp->task_tag; |
| 1531 | task_req_upiup->input_param2 = |
| 1532 | cpu_to_be32(task_req_upiup->input_param2); |
| 1533 | |
| 1534 | /* send command to the controller */ |
| 1535 | __set_bit(free_slot, &hba->outstanding_tasks); |
| 1536 | writel((1 << free_slot), |
| 1537 | (hba->mmio_base + REG_UTP_TASK_REQ_DOOR_BELL)); |
| 1538 | |
| 1539 | spin_unlock_irqrestore(host->host_lock, flags); |
| 1540 | |
| 1541 | /* wait until the task management command is completed */ |
| 1542 | err = |
| 1543 | wait_event_interruptible_timeout(hba->ufshcd_tm_wait_queue, |
| 1544 | (test_bit(free_slot, |
| 1545 | &hba->tm_condition) != 0), |
| 1546 | 60 * HZ); |
| 1547 | if (!err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1548 | dev_err(hba->dev, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1549 | "Task management command timed-out\n"); |
| 1550 | err = FAILED; |
| 1551 | goto out; |
| 1552 | } |
| 1553 | clear_bit(free_slot, &hba->tm_condition); |
Namjae Jeon | 94c122a | 2012-07-10 20:41:54 +0530 | [diff] [blame] | 1554 | err = ufshcd_task_req_compl(hba, free_slot); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1555 | out: |
| 1556 | return err; |
| 1557 | } |
| 1558 | |
| 1559 | /** |
| 1560 | * ufshcd_device_reset - reset device and abort all the pending commands |
| 1561 | * @cmd: SCSI command pointer |
| 1562 | * |
| 1563 | * Returns SUCCESS/FAILED |
| 1564 | */ |
| 1565 | static int ufshcd_device_reset(struct scsi_cmnd *cmd) |
| 1566 | { |
| 1567 | struct Scsi_Host *host; |
| 1568 | struct ufs_hba *hba; |
| 1569 | unsigned int tag; |
| 1570 | u32 pos; |
| 1571 | int err; |
| 1572 | |
| 1573 | host = cmd->device->host; |
| 1574 | hba = shost_priv(host); |
| 1575 | tag = cmd->request->tag; |
| 1576 | |
| 1577 | err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_LOGICAL_RESET); |
Namjae Jeon | 94c122a | 2012-07-10 20:41:54 +0530 | [diff] [blame] | 1578 | if (err == FAILED) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1579 | goto out; |
| 1580 | |
| 1581 | for (pos = 0; pos < hba->nutrs; pos++) { |
| 1582 | if (test_bit(pos, &hba->outstanding_reqs) && |
| 1583 | (hba->lrb[tag].lun == hba->lrb[pos].lun)) { |
| 1584 | |
| 1585 | /* clear the respective UTRLCLR register bit */ |
| 1586 | ufshcd_utrl_clear(hba, pos); |
| 1587 | |
| 1588 | clear_bit(pos, &hba->outstanding_reqs); |
| 1589 | |
| 1590 | if (hba->lrb[pos].cmd) { |
| 1591 | scsi_dma_unmap(hba->lrb[pos].cmd); |
| 1592 | hba->lrb[pos].cmd->result = |
| 1593 | DID_ABORT << 16; |
| 1594 | hba->lrb[pos].cmd->scsi_done(cmd); |
| 1595 | hba->lrb[pos].cmd = NULL; |
| 1596 | } |
| 1597 | } |
| 1598 | } /* end of for */ |
| 1599 | out: |
| 1600 | return err; |
| 1601 | } |
| 1602 | |
| 1603 | /** |
| 1604 | * ufshcd_host_reset - Main reset function registered with scsi layer |
| 1605 | * @cmd: SCSI command pointer |
| 1606 | * |
| 1607 | * Returns SUCCESS/FAILED |
| 1608 | */ |
| 1609 | static int ufshcd_host_reset(struct scsi_cmnd *cmd) |
| 1610 | { |
| 1611 | struct ufs_hba *hba; |
| 1612 | |
| 1613 | hba = shost_priv(cmd->device->host); |
| 1614 | |
| 1615 | if (hba->ufshcd_state == UFSHCD_STATE_RESET) |
| 1616 | return SUCCESS; |
| 1617 | |
Namjae Jeon | 94c122a | 2012-07-10 20:41:54 +0530 | [diff] [blame] | 1618 | return ufshcd_do_reset(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /** |
| 1622 | * ufshcd_abort - abort a specific command |
| 1623 | * @cmd: SCSI command pointer |
| 1624 | * |
| 1625 | * Returns SUCCESS/FAILED |
| 1626 | */ |
| 1627 | static int ufshcd_abort(struct scsi_cmnd *cmd) |
| 1628 | { |
| 1629 | struct Scsi_Host *host; |
| 1630 | struct ufs_hba *hba; |
| 1631 | unsigned long flags; |
| 1632 | unsigned int tag; |
| 1633 | int err; |
| 1634 | |
| 1635 | host = cmd->device->host; |
| 1636 | hba = shost_priv(host); |
| 1637 | tag = cmd->request->tag; |
| 1638 | |
| 1639 | spin_lock_irqsave(host->host_lock, flags); |
| 1640 | |
| 1641 | /* check if command is still pending */ |
| 1642 | if (!(test_bit(tag, &hba->outstanding_reqs))) { |
| 1643 | err = FAILED; |
| 1644 | spin_unlock_irqrestore(host->host_lock, flags); |
| 1645 | goto out; |
| 1646 | } |
| 1647 | spin_unlock_irqrestore(host->host_lock, flags); |
| 1648 | |
| 1649 | err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_ABORT_TASK); |
Namjae Jeon | 94c122a | 2012-07-10 20:41:54 +0530 | [diff] [blame] | 1650 | if (err == FAILED) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1651 | goto out; |
| 1652 | |
| 1653 | scsi_dma_unmap(cmd); |
| 1654 | |
| 1655 | spin_lock_irqsave(host->host_lock, flags); |
| 1656 | |
| 1657 | /* clear the respective UTRLCLR register bit */ |
| 1658 | ufshcd_utrl_clear(hba, tag); |
| 1659 | |
| 1660 | __clear_bit(tag, &hba->outstanding_reqs); |
| 1661 | hba->lrb[tag].cmd = NULL; |
| 1662 | spin_unlock_irqrestore(host->host_lock, flags); |
| 1663 | out: |
| 1664 | return err; |
| 1665 | } |
| 1666 | |
| 1667 | static struct scsi_host_template ufshcd_driver_template = { |
| 1668 | .module = THIS_MODULE, |
| 1669 | .name = UFSHCD, |
| 1670 | .proc_name = UFSHCD, |
| 1671 | .queuecommand = ufshcd_queuecommand, |
| 1672 | .slave_alloc = ufshcd_slave_alloc, |
| 1673 | .slave_destroy = ufshcd_slave_destroy, |
| 1674 | .eh_abort_handler = ufshcd_abort, |
| 1675 | .eh_device_reset_handler = ufshcd_device_reset, |
| 1676 | .eh_host_reset_handler = ufshcd_host_reset, |
| 1677 | .this_id = -1, |
| 1678 | .sg_tablesize = SG_ALL, |
| 1679 | .cmd_per_lun = UFSHCD_CMD_PER_LUN, |
| 1680 | .can_queue = UFSHCD_CAN_QUEUE, |
| 1681 | }; |
| 1682 | |
| 1683 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1684 | * ufshcd_pci_shutdown - main function to put the controller in reset state |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1685 | * @pdev: pointer to PCI device handle |
| 1686 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1687 | static void ufshcd_pci_shutdown(struct pci_dev *pdev) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1688 | { |
| 1689 | ufshcd_hba_stop((struct ufs_hba *)pci_get_drvdata(pdev)); |
| 1690 | } |
| 1691 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1692 | /** |
| 1693 | * ufshcd_suspend - suspend power management function |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1694 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1695 | * @state: power state |
| 1696 | * |
| 1697 | * Returns -ENOSYS |
| 1698 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1699 | int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1700 | { |
| 1701 | /* |
| 1702 | * TODO: |
| 1703 | * 1. Block SCSI requests from SCSI midlayer |
| 1704 | * 2. Change the internal driver state to non operational |
| 1705 | * 3. Set UTRLRSR and UTMRLRSR bits to zero |
| 1706 | * 4. Wait until outstanding commands are completed |
| 1707 | * 5. Set HCE to zero to send the UFS host controller to reset state |
| 1708 | */ |
| 1709 | |
| 1710 | return -ENOSYS; |
| 1711 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1712 | EXPORT_SYMBOL_GPL(ufshcd_suspend); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1713 | |
| 1714 | /** |
| 1715 | * ufshcd_resume - resume power management function |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1716 | * @hba: per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1717 | * |
| 1718 | * Returns -ENOSYS |
| 1719 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1720 | int ufshcd_resume(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1721 | { |
| 1722 | /* |
| 1723 | * TODO: |
| 1724 | * 1. Set HCE to 1, to start the UFS host controller |
| 1725 | * initialization process |
| 1726 | * 2. Set UTRLRSR and UTMRLRSR bits to 1 |
| 1727 | * 3. Change the internal driver state to operational |
| 1728 | * 4. Unblock SCSI requests from SCSI midlayer |
| 1729 | */ |
| 1730 | |
| 1731 | return -ENOSYS; |
| 1732 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1733 | EXPORT_SYMBOL_GPL(ufshcd_resume); |
| 1734 | |
| 1735 | #ifdef CONFIG_PM |
| 1736 | /** |
| 1737 | * ufshcd_pci_suspend - suspend power management function |
| 1738 | * @pdev: pointer to PCI device handle |
| 1739 | * @state: power state |
| 1740 | * |
| 1741 | * Returns -ENOSYS |
| 1742 | */ |
| 1743 | static int ufshcd_pci_suspend(struct pci_dev *pdev, pm_message_t state) |
| 1744 | { |
| 1745 | /* |
| 1746 | * TODO: |
| 1747 | * 1. Call ufshcd_suspend |
| 1748 | * 2. Do bus specific power management |
| 1749 | */ |
| 1750 | |
| 1751 | return -ENOSYS; |
| 1752 | } |
| 1753 | |
| 1754 | /** |
| 1755 | * ufshcd_pci_resume - resume power management function |
| 1756 | * @pdev: pointer to PCI device handle |
| 1757 | * |
| 1758 | * Returns -ENOSYS |
| 1759 | */ |
| 1760 | static int ufshcd_pci_resume(struct pci_dev *pdev) |
| 1761 | { |
| 1762 | /* |
| 1763 | * TODO: |
| 1764 | * 1. Call ufshcd_resume |
| 1765 | * 2. Do bus specific wake up |
| 1766 | */ |
| 1767 | |
| 1768 | return -ENOSYS; |
| 1769 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1770 | #endif /* CONFIG_PM */ |
| 1771 | |
| 1772 | /** |
| 1773 | * ufshcd_hba_free - free allocated memory for |
| 1774 | * host memory space data structures |
| 1775 | * @hba: per adapter instance |
| 1776 | */ |
| 1777 | static void ufshcd_hba_free(struct ufs_hba *hba) |
| 1778 | { |
| 1779 | iounmap(hba->mmio_base); |
| 1780 | ufshcd_free_hba_memory(hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1784 | * ufshcd_remove - de-allocate SCSI host and host memory space |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1785 | * data structure memory |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1786 | * @hba - per adapter instance |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1787 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1788 | void ufshcd_remove(struct ufs_hba *hba) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1789 | { |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1790 | /* disable interrupts */ |
| 1791 | ufshcd_int_config(hba, UFSHCD_INT_DISABLE); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1792 | |
| 1793 | ufshcd_hba_stop(hba); |
| 1794 | ufshcd_hba_free(hba); |
| 1795 | |
| 1796 | scsi_remove_host(hba->host); |
| 1797 | scsi_host_put(hba->host); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1798 | } |
| 1799 | EXPORT_SYMBOL_GPL(ufshcd_remove); |
| 1800 | |
| 1801 | /** |
| 1802 | * ufshcd_pci_remove - de-allocate PCI/SCSI host and host memory space |
| 1803 | * data structure memory |
| 1804 | * @pdev - pointer to PCI handle |
| 1805 | */ |
| 1806 | static void ufshcd_pci_remove(struct pci_dev *pdev) |
| 1807 | { |
| 1808 | struct ufs_hba *hba = pci_get_drvdata(pdev); |
| 1809 | |
| 1810 | disable_irq(pdev->irq); |
| 1811 | free_irq(pdev->irq, hba); |
| 1812 | ufshcd_remove(hba); |
| 1813 | pci_release_regions(pdev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1814 | pci_set_drvdata(pdev, NULL); |
| 1815 | pci_clear_master(pdev); |
| 1816 | pci_disable_device(pdev); |
| 1817 | } |
| 1818 | |
| 1819 | /** |
| 1820 | * ufshcd_set_dma_mask - Set dma mask based on the controller |
| 1821 | * addressing capability |
| 1822 | * @pdev: PCI device structure |
| 1823 | * |
| 1824 | * Returns 0 for success, non-zero for failure |
| 1825 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1826 | static int ufshcd_set_dma_mask(struct pci_dev *pdev) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1827 | { |
| 1828 | int err; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1829 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1830 | if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) |
| 1831 | && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64))) |
| 1832 | return 0; |
| 1833 | err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32)); |
| 1834 | if (!err) |
| 1835 | err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1836 | return err; |
| 1837 | } |
| 1838 | |
| 1839 | /** |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1840 | * ufshcd_init - Driver initialization routine |
| 1841 | * @dev: pointer to device handle |
| 1842 | * @hba_handle: driver private handle |
| 1843 | * @mmio_base: base register address |
| 1844 | * @irq: Interrupt line of device |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1845 | * Returns 0 on success, non-zero value on failure |
| 1846 | */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1847 | int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle, |
| 1848 | void __iomem *mmio_base, unsigned int irq) |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1849 | { |
| 1850 | struct Scsi_Host *host; |
| 1851 | struct ufs_hba *hba; |
| 1852 | int err; |
| 1853 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1854 | if (!dev) { |
| 1855 | dev_err(dev, |
| 1856 | "Invalid memory reference for dev is NULL\n"); |
| 1857 | err = -ENODEV; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1858 | goto out_error; |
| 1859 | } |
| 1860 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1861 | if (!mmio_base) { |
| 1862 | dev_err(dev, |
| 1863 | "Invalid memory reference for mmio_base is NULL\n"); |
| 1864 | err = -ENODEV; |
| 1865 | goto out_error; |
| 1866 | } |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1867 | |
| 1868 | host = scsi_host_alloc(&ufshcd_driver_template, |
| 1869 | sizeof(struct ufs_hba)); |
| 1870 | if (!host) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1871 | dev_err(dev, "scsi_host_alloc failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1872 | err = -ENOMEM; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1873 | goto out_error; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1874 | } |
| 1875 | hba = shost_priv(host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1876 | hba->host = host; |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1877 | hba->dev = dev; |
| 1878 | hba->mmio_base = mmio_base; |
| 1879 | hba->irq = irq; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1880 | |
| 1881 | /* Read capabilities registers */ |
| 1882 | ufshcd_hba_capabilities(hba); |
| 1883 | |
| 1884 | /* Get UFS version supported by the controller */ |
| 1885 | hba->ufs_version = ufshcd_get_ufs_version(hba); |
| 1886 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1887 | /* Allocate memory for host memory space */ |
| 1888 | err = ufshcd_memory_alloc(hba); |
| 1889 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1890 | dev_err(hba->dev, "Memory allocation failed\n"); |
| 1891 | goto out_disable; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1892 | } |
| 1893 | |
| 1894 | /* Configure LRB */ |
| 1895 | ufshcd_host_memory_configure(hba); |
| 1896 | |
| 1897 | host->can_queue = hba->nutrs; |
| 1898 | host->cmd_per_lun = hba->nutrs; |
| 1899 | host->max_id = UFSHCD_MAX_ID; |
| 1900 | host->max_lun = UFSHCD_MAX_LUNS; |
| 1901 | host->max_channel = UFSHCD_MAX_CHANNEL; |
| 1902 | host->unique_id = host->host_no; |
| 1903 | host->max_cmd_len = MAX_CDB_SIZE; |
| 1904 | |
| 1905 | /* Initailize wait queue for task management */ |
| 1906 | init_waitqueue_head(&hba->ufshcd_tm_wait_queue); |
| 1907 | |
| 1908 | /* Initialize work queues */ |
| 1909 | INIT_WORK(&hba->uic_workq, ufshcd_uic_cc_handler); |
| 1910 | INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler); |
| 1911 | |
| 1912 | /* IRQ registration */ |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1913 | err = request_irq(irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1914 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1915 | dev_err(hba->dev, "request irq failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1916 | goto out_lrb_free; |
| 1917 | } |
| 1918 | |
| 1919 | /* Enable SCSI tag mapping */ |
| 1920 | err = scsi_init_shared_tag_map(host, host->can_queue); |
| 1921 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1922 | dev_err(hba->dev, "init shared queue failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1923 | goto out_free_irq; |
| 1924 | } |
| 1925 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1926 | err = scsi_add_host(host, hba->dev); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1927 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1928 | dev_err(hba->dev, "scsi_add_host failed\n"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1929 | goto out_free_irq; |
| 1930 | } |
| 1931 | |
| 1932 | /* Initialization routine */ |
| 1933 | err = ufshcd_initialize_hba(hba); |
| 1934 | if (err) { |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1935 | dev_err(hba->dev, "Initialization failed\n"); |
| 1936 | goto out_remove_scsi_host; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1937 | } |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1938 | *hba_handle = hba; |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1939 | |
| 1940 | return 0; |
| 1941 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1942 | out_remove_scsi_host: |
| 1943 | scsi_remove_host(hba->host); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1944 | out_free_irq: |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1945 | free_irq(irq, hba); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 1946 | out_lrb_free: |
| 1947 | ufshcd_free_hba_memory(hba); |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 1948 | out_disable: |
| 1949 | scsi_host_put(host); |
| 1950 | out_error: |
| 1951 | return err; |
| 1952 | } |
| 1953 | EXPORT_SYMBOL_GPL(ufshcd_init); |
| 1954 | |
| 1955 | /** |
| 1956 | * ufshcd_pci_probe - probe routine of the driver |
| 1957 | * @pdev: pointer to PCI device handle |
| 1958 | * @id: PCI device id |
| 1959 | * |
| 1960 | * Returns 0 on success, non-zero value on failure |
| 1961 | */ |
| 1962 | static int ufshcd_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) |
| 1963 | { |
| 1964 | struct ufs_hba *hba; |
| 1965 | void __iomem *mmio_base; |
| 1966 | int err; |
| 1967 | |
| 1968 | err = pci_enable_device(pdev); |
| 1969 | if (err) { |
| 1970 | dev_err(&pdev->dev, "pci_enable_device failed\n"); |
| 1971 | goto out_error; |
| 1972 | } |
| 1973 | |
| 1974 | pci_set_master(pdev); |
| 1975 | |
| 1976 | err = pci_request_regions(pdev, UFSHCD); |
| 1977 | if (err < 0) { |
| 1978 | dev_err(&pdev->dev, "request regions failed\n"); |
| 1979 | goto out_disable; |
| 1980 | } |
| 1981 | |
| 1982 | mmio_base = pci_ioremap_bar(pdev, 0); |
| 1983 | if (!mmio_base) { |
| 1984 | dev_err(&pdev->dev, "memory map failed\n"); |
| 1985 | err = -ENOMEM; |
| 1986 | goto out_release_regions; |
| 1987 | } |
| 1988 | |
| 1989 | err = ufshcd_set_dma_mask(pdev); |
| 1990 | if (err) { |
| 1991 | dev_err(&pdev->dev, "set dma mask failed\n"); |
| 1992 | goto out_iounmap; |
| 1993 | } |
| 1994 | |
| 1995 | err = ufshcd_init(&pdev->dev, &hba, mmio_base, pdev->irq); |
| 1996 | if (err) { |
| 1997 | dev_err(&pdev->dev, "Initialization failed\n"); |
| 1998 | goto out_iounmap; |
| 1999 | } |
| 2000 | |
| 2001 | pci_set_drvdata(pdev, hba); |
| 2002 | |
| 2003 | return 0; |
| 2004 | |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2005 | out_iounmap: |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 2006 | iounmap(mmio_base); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2007 | out_release_regions: |
| 2008 | pci_release_regions(pdev); |
Namjae Jeon | 4886b1a | 2012-05-20 03:08:01 -0400 | [diff] [blame] | 2009 | out_disable: |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2010 | pci_clear_master(pdev); |
| 2011 | pci_disable_device(pdev); |
| 2012 | out_error: |
| 2013 | return err; |
| 2014 | } |
| 2015 | |
| 2016 | static DEFINE_PCI_DEVICE_TABLE(ufshcd_pci_tbl) = { |
| 2017 | { PCI_VENDOR_ID_SAMSUNG, 0xC00C, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 }, |
| 2018 | { } /* terminate list */ |
| 2019 | }; |
| 2020 | |
| 2021 | MODULE_DEVICE_TABLE(pci, ufshcd_pci_tbl); |
| 2022 | |
| 2023 | static struct pci_driver ufshcd_pci_driver = { |
| 2024 | .name = UFSHCD, |
| 2025 | .id_table = ufshcd_pci_tbl, |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 2026 | .probe = ufshcd_pci_probe, |
| 2027 | .remove = ufshcd_pci_remove, |
| 2028 | .shutdown = ufshcd_pci_shutdown, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2029 | #ifdef CONFIG_PM |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 2030 | .suspend = ufshcd_pci_suspend, |
| 2031 | .resume = ufshcd_pci_resume, |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2032 | #endif |
| 2033 | }; |
| 2034 | |
Venkatraman S | 17ccafc | 2012-07-10 19:39:22 +0530 | [diff] [blame] | 2035 | module_pci_driver(ufshcd_pci_driver); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2036 | |
Vinayak Holikatti | 3b1d058 | 2013-02-25 21:44:32 +0530 | [diff] [blame^] | 2037 | MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>"); |
| 2038 | MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>"); |
Santosh Yaraganavi | 7a3e97b | 2012-02-29 12:11:50 +0530 | [diff] [blame] | 2039 | MODULE_DESCRIPTION("Generic UFS host controller driver"); |
| 2040 | MODULE_LICENSE("GPL"); |
| 2041 | MODULE_VERSION(UFSHCD_DRIVER_VERSION); |