2 * Universal Flash Storage Host controller driver Core
4 * This code is based on drivers/scsi/ufs/ufshcd.c
5 * Copyright (C) 2011-2013 Samsung India Software Operations
8 * Santosh Yaraganavi <santosh.sy@samsung.com>
9 * Vinayak Holikatti <h.vinayak@samsung.com>
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.
15 * See the COPYING file in the top-level directory or visit
16 * <http://www.gnu.org/licenses/gpl-2.0.html>
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.
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
38 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
43 UFSHCD_MAX_CHANNEL = 0,
46 UFSHCD_CMD_PER_LUN = 32,
47 UFSHCD_CAN_QUEUE = 32,
52 UFSHCD_STATE_OPERATIONAL,
57 /* Interrupt configuration options */
64 /* Interrupt aggregation options */
71 * ufshcd_get_intr_mask - Get the interrupt bit mask
72 * @hba - Pointer to adapter instance
74 * Returns interrupt bit mask per version
76 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
78 if (hba->ufs_version == UFSHCI_VERSION_10)
79 return INTERRUPT_MASK_ALL_VER_10;
81 return INTERRUPT_MASK_ALL_VER_11;
85 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
86 * @hba - Pointer to adapter instance
88 * Returns UFSHCI version supported by the controller
90 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
92 return ufshcd_readl(hba, REG_UFS_VERSION);
96 * ufshcd_is_device_present - Check if any device connected to
98 * @reg_hcs - host controller status register value
100 * Returns 1 if device present, 0 if no device detected
102 static inline int ufshcd_is_device_present(u32 reg_hcs)
104 return (DEVICE_PRESENT & reg_hcs) ? 1 : 0;
108 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
109 * @lrb: pointer to local command reference block
111 * This function is used to get the OCS field from UTRD
112 * Returns the OCS field in the UTRD
114 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
116 return lrbp->utr_descriptor_ptr->header.dword_2 & MASK_OCS;
120 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
121 * @task_req_descp: pointer to utp_task_req_desc structure
123 * This function is used to get the OCS field from UTMRD
124 * Returns the OCS field in the UTMRD
127 ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
129 return task_req_descp->header.dword_2 & MASK_OCS;
133 * ufshcd_get_tm_free_slot - get a free slot for task management request
134 * @hba: per adapter instance
136 * Returns maximum number of task management request slots in case of
137 * task management queue full or returns the free slot number
139 static inline int ufshcd_get_tm_free_slot(struct ufs_hba *hba)
141 return find_first_zero_bit(&hba->outstanding_tasks, hba->nutmrs);
145 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
146 * @hba: per adapter instance
147 * @pos: position of the bit to be cleared
149 static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
151 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
155 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
156 * @reg: Register value of host controller status
158 * Returns integer, 0 on Success and positive value if failed
160 static inline int ufshcd_get_lists_status(u32 reg)
163 * The mask 0xFF is for the following HCS register bits
173 return (((reg) & (0xFF)) >> 1) ^ (0x07);
177 * ufshcd_get_uic_cmd_result - Get the UIC command result
178 * @hba: Pointer to adapter instance
180 * This function gets the result of UIC command completion
181 * Returns 0 on success, non zero value on error
183 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
185 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
186 MASK_UIC_COMMAND_RESULT;
190 * ufshcd_free_hba_memory - Free allocated memory for LRB, request
192 * @hba: Pointer to adapter instance
194 static inline void ufshcd_free_hba_memory(struct ufs_hba *hba)
196 size_t utmrdl_size, utrdl_size, ucdl_size;
200 if (hba->utmrdl_base_addr) {
201 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
202 dma_free_coherent(hba->dev, utmrdl_size,
203 hba->utmrdl_base_addr, hba->utmrdl_dma_addr);
206 if (hba->utrdl_base_addr) {
208 (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
209 dma_free_coherent(hba->dev, utrdl_size,
210 hba->utrdl_base_addr, hba->utrdl_dma_addr);
213 if (hba->ucdl_base_addr) {
215 (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
216 dma_free_coherent(hba->dev, ucdl_size,
217 hba->ucdl_base_addr, hba->ucdl_dma_addr);
222 * ufshcd_is_valid_req_rsp - checks if controller TR response is valid
223 * @ucd_rsp_ptr: pointer to response UPIU
225 * This function checks the response UPIU for valid transaction type in
227 * Returns 0 on success, non-zero on failure
230 ufshcd_is_valid_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
232 return ((be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24) ==
233 UPIU_TRANSACTION_RESPONSE) ? 0 : DID_ERROR << 16;
237 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
238 * @ucd_rsp_ptr: pointer to response UPIU
240 * This function gets the response status and scsi_status from response UPIU
241 * Returns the response result code.
244 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
246 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
250 * ufshcd_config_int_aggr - Configure interrupt aggregation values.
251 * Currently there is no use case where we want to configure
252 * interrupt aggregation dynamically. So to configure interrupt
253 * aggregation, #define INT_AGGR_COUNTER_THRESHOLD_VALUE and
254 * INT_AGGR_TIMEOUT_VALUE are used.
255 * @hba: per adapter instance
256 * @option: Interrupt aggregation option
259 ufshcd_config_int_aggr(struct ufs_hba *hba, int option)
263 ufshcd_writel(hba, INT_AGGR_ENABLE |
264 INT_AGGR_COUNTER_AND_TIMER_RESET,
265 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
267 case INT_AGGR_CONFIG:
268 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
269 INT_AGGR_COUNTER_THRESHOLD_VALUE |
270 INT_AGGR_TIMEOUT_VALUE,
271 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
277 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
278 * When run-stop registers are set to 1, it indicates the
279 * host controller that it can process the requests
280 * @hba: per adapter instance
282 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
284 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
285 REG_UTP_TASK_REQ_LIST_RUN_STOP);
286 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
287 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
291 * ufshcd_hba_start - Start controller initialization sequence
292 * @hba: per adapter instance
294 static inline void ufshcd_hba_start(struct ufs_hba *hba)
296 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
300 * ufshcd_is_hba_active - Get controller state
301 * @hba: per adapter instance
303 * Returns zero if controller is active, 1 otherwise
305 static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
307 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
311 * ufshcd_send_command - Send SCSI or device management commands
312 * @hba: per adapter instance
313 * @task_tag: Task tag of the command
316 void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
318 __set_bit(task_tag, &hba->outstanding_reqs);
319 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
323 * ufshcd_copy_sense_data - Copy sense data in case of check condition
324 * @lrb - pointer to local reference block
326 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
329 if (lrbp->sense_buffer) {
330 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sense_data_len);
331 memcpy(lrbp->sense_buffer,
332 lrbp->ucd_rsp_ptr->sense_data,
333 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
338 * ufshcd_hba_capabilities - Read controller capabilities
339 * @hba: per adapter instance
341 static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
343 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
345 /* nutrs and nutmrs are 0 based values */
346 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
348 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
352 * ufshcd_send_uic_command - Send UIC commands to unipro layers
353 * @hba: per adapter instance
354 * @uic_command: UIC command
357 ufshcd_send_uic_command(struct ufs_hba *hba, struct uic_command *uic_cmnd)
360 ufshcd_writel(hba, uic_cmnd->argument1, REG_UIC_COMMAND_ARG_1);
361 ufshcd_writel(hba, uic_cmnd->argument2, REG_UIC_COMMAND_ARG_2);
362 ufshcd_writel(hba, uic_cmnd->argument3, REG_UIC_COMMAND_ARG_3);
365 ufshcd_writel(hba, uic_cmnd->command & COMMAND_OPCODE_MASK,
370 * ufshcd_map_sg - Map scatter-gather list to prdt
371 * @lrbp - pointer to local reference block
373 * Returns 0 in case of success, non-zero value in case of failure
375 static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
377 struct ufshcd_sg_entry *prd_table;
378 struct scatterlist *sg;
379 struct scsi_cmnd *cmd;
384 sg_segments = scsi_dma_map(cmd);
389 lrbp->utr_descriptor_ptr->prd_table_length =
390 cpu_to_le16((u16) (sg_segments));
392 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
394 scsi_for_each_sg(cmd, sg, sg_segments, i) {
396 cpu_to_le32(((u32) sg_dma_len(sg))-1);
397 prd_table[i].base_addr =
398 cpu_to_le32(lower_32_bits(sg->dma_address));
399 prd_table[i].upper_addr =
400 cpu_to_le32(upper_32_bits(sg->dma_address));
403 lrbp->utr_descriptor_ptr->prd_table_length = 0;
410 * ufshcd_enable_intr - enable interrupts
411 * @hba: per adapter instance
412 * @intrs: interrupt bits
414 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
416 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
418 if (hba->ufs_version == UFSHCI_VERSION_10) {
420 rw = set & INTERRUPT_MASK_RW_VER_10;
421 set = rw | ((set ^ intrs) & intrs);
426 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
430 * ufshcd_disable_intr - disable interrupts
431 * @hba: per adapter instance
432 * @intrs: interrupt bits
434 static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
436 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
438 if (hba->ufs_version == UFSHCI_VERSION_10) {
440 rw = (set & INTERRUPT_MASK_RW_VER_10) &
441 ~(intrs & INTERRUPT_MASK_RW_VER_10);
442 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
448 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
452 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
453 * @lrb - pointer to local reference block
455 static void ufshcd_compose_upiu(struct ufshcd_lrb *lrbp)
457 struct utp_transfer_req_desc *req_desc;
458 struct utp_upiu_cmd *ucd_cmd_ptr;
462 ucd_cmd_ptr = lrbp->ucd_cmd_ptr;
463 req_desc = lrbp->utr_descriptor_ptr;
465 switch (lrbp->command_type) {
466 case UTP_CMD_TYPE_SCSI:
467 if (lrbp->cmd->sc_data_direction == DMA_FROM_DEVICE) {
468 data_direction = UTP_DEVICE_TO_HOST;
469 upiu_flags = UPIU_CMD_FLAGS_READ;
470 } else if (lrbp->cmd->sc_data_direction == DMA_TO_DEVICE) {
471 data_direction = UTP_HOST_TO_DEVICE;
472 upiu_flags = UPIU_CMD_FLAGS_WRITE;
474 data_direction = UTP_NO_DATA_TRANSFER;
475 upiu_flags = UPIU_CMD_FLAGS_NONE;
478 /* Transfer request descriptor header fields */
479 req_desc->header.dword_0 =
480 cpu_to_le32(data_direction | UTP_SCSI_COMMAND);
483 * assigning invalid value for command status. Controller
484 * updates OCS on command completion, with the command
487 req_desc->header.dword_2 =
488 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
490 /* command descriptor fields */
491 ucd_cmd_ptr->header.dword_0 =
492 cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_COMMAND,
496 ucd_cmd_ptr->header.dword_1 =
498 UPIU_HEADER_DWORD(UPIU_COMMAND_SET_TYPE_SCSI,
503 /* Total EHS length and Data segment length will be zero */
504 ucd_cmd_ptr->header.dword_2 = 0;
506 ucd_cmd_ptr->exp_data_transfer_len =
507 cpu_to_be32(lrbp->cmd->sdb.length);
509 memcpy(ucd_cmd_ptr->cdb,
511 (min_t(unsigned short,
515 case UTP_CMD_TYPE_DEV_MANAGE:
516 /* For query function implementation */
518 case UTP_CMD_TYPE_UFS:
519 /* For UFS native command implementation */
521 } /* end of switch */
525 * ufshcd_queuecommand - main entry point for SCSI requests
526 * @cmd: command from SCSI Midlayer
527 * @done: call back function
529 * Returns 0 for success, non-zero in case of failure
531 static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
533 struct ufshcd_lrb *lrbp;
539 hba = shost_priv(host);
541 tag = cmd->request->tag;
543 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) {
544 err = SCSI_MLQUEUE_HOST_BUSY;
548 lrbp = &hba->lrb[tag];
551 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
552 lrbp->sense_buffer = cmd->sense_buffer;
553 lrbp->task_tag = tag;
554 lrbp->lun = cmd->device->lun;
556 lrbp->command_type = UTP_CMD_TYPE_SCSI;
558 /* form UPIU before issuing the command */
559 ufshcd_compose_upiu(lrbp);
560 err = ufshcd_map_sg(lrbp);
564 /* issue command to the controller */
565 spin_lock_irqsave(hba->host->host_lock, flags);
566 ufshcd_send_command(hba, tag);
567 spin_unlock_irqrestore(hba->host->host_lock, flags);
573 * ufshcd_memory_alloc - allocate memory for host memory space data structures
574 * @hba: per adapter instance
576 * 1. Allocate DMA memory for Command Descriptor array
577 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
578 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
579 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
581 * 4. Allocate memory for local reference block(lrb).
583 * Returns 0 for success, non-zero in case of failure
585 static int ufshcd_memory_alloc(struct ufs_hba *hba)
587 size_t utmrdl_size, utrdl_size, ucdl_size;
589 /* Allocate memory for UTP command descriptors */
590 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
591 hba->ucdl_base_addr = dma_alloc_coherent(hba->dev,
597 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
598 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
599 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
600 * be aligned to 128 bytes as well
602 if (!hba->ucdl_base_addr ||
603 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
605 "Command Descriptor Memory allocation failed\n");
610 * Allocate memory for UTP Transfer descriptors
611 * UFSHCI requires 1024 byte alignment of UTRD
613 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
614 hba->utrdl_base_addr = dma_alloc_coherent(hba->dev,
616 &hba->utrdl_dma_addr,
618 if (!hba->utrdl_base_addr ||
619 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
621 "Transfer Descriptor Memory allocation failed\n");
626 * Allocate memory for UTP Task Management descriptors
627 * UFSHCI requires 1024 byte alignment of UTMRD
629 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
630 hba->utmrdl_base_addr = dma_alloc_coherent(hba->dev,
632 &hba->utmrdl_dma_addr,
634 if (!hba->utmrdl_base_addr ||
635 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
637 "Task Management Descriptor Memory allocation failed\n");
641 /* Allocate memory for local reference block */
642 hba->lrb = kcalloc(hba->nutrs, sizeof(struct ufshcd_lrb), GFP_KERNEL);
644 dev_err(hba->dev, "LRB Memory allocation failed\n");
649 ufshcd_free_hba_memory(hba);
654 * ufshcd_host_memory_configure - configure local reference block with
656 * @hba: per adapter instance
658 * Configure Host memory space
659 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
661 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
663 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
664 * into local reference block.
666 static void ufshcd_host_memory_configure(struct ufs_hba *hba)
668 struct utp_transfer_cmd_desc *cmd_descp;
669 struct utp_transfer_req_desc *utrdlp;
670 dma_addr_t cmd_desc_dma_addr;
671 dma_addr_t cmd_desc_element_addr;
677 utrdlp = hba->utrdl_base_addr;
678 cmd_descp = hba->ucdl_base_addr;
681 offsetof(struct utp_transfer_cmd_desc, response_upiu);
683 offsetof(struct utp_transfer_cmd_desc, prd_table);
685 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
686 cmd_desc_dma_addr = hba->ucdl_dma_addr;
688 for (i = 0; i < hba->nutrs; i++) {
689 /* Configure UTRD with command descriptor base address */
690 cmd_desc_element_addr =
691 (cmd_desc_dma_addr + (cmd_desc_size * i));
692 utrdlp[i].command_desc_base_addr_lo =
693 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
694 utrdlp[i].command_desc_base_addr_hi =
695 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
697 /* Response upiu and prdt offset should be in double words */
698 utrdlp[i].response_upiu_offset =
699 cpu_to_le16((response_offset >> 2));
700 utrdlp[i].prd_table_offset =
701 cpu_to_le16((prdt_offset >> 2));
702 utrdlp[i].response_upiu_length =
703 cpu_to_le16(ALIGNED_UPIU_SIZE);
705 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
706 hba->lrb[i].ucd_cmd_ptr =
707 (struct utp_upiu_cmd *)(cmd_descp + i);
708 hba->lrb[i].ucd_rsp_ptr =
709 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
710 hba->lrb[i].ucd_prdt_ptr =
711 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
716 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
717 * @hba: per adapter instance
719 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
720 * in order to initialize the Unipro link startup procedure.
721 * Once the Unipro links are up, the device connected to the controller
724 * Returns 0 on success, non-zero value on failure
726 static int ufshcd_dme_link_startup(struct ufs_hba *hba)
728 struct uic_command *uic_cmd;
731 /* check if controller is ready to accept UIC commands */
732 if ((ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
733 UIC_COMMAND_READY) == 0x0) {
735 "Controller not ready"
736 " to accept UIC commands\n");
740 spin_lock_irqsave(hba->host->host_lock, flags);
742 /* form UIC command */
743 uic_cmd = &hba->active_uic_cmd;
744 uic_cmd->command = UIC_CMD_DME_LINK_STARTUP;
745 uic_cmd->argument1 = 0;
746 uic_cmd->argument2 = 0;
747 uic_cmd->argument3 = 0;
749 /* enable UIC related interrupts */
750 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
752 /* sending UIC commands to controller */
753 ufshcd_send_uic_command(hba, uic_cmd);
754 spin_unlock_irqrestore(hba->host->host_lock, flags);
759 * ufshcd_make_hba_operational - Make UFS controller operational
760 * @hba: per adapter instance
762 * To bring UFS host controller to operational state,
763 * 1. Check if device is present
764 * 2. Configure run-stop-registers
765 * 3. Enable required interrupts
766 * 4. Configure interrupt aggregation
768 * Returns 0 on success, non-zero value on failure
770 static int ufshcd_make_hba_operational(struct ufs_hba *hba)
775 /* check if device present */
776 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
777 if (!ufshcd_is_device_present(reg)) {
778 dev_err(hba->dev, "cc: Device not present\n");
784 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
785 * DEI, HEI bits must be 0
787 if (!(ufshcd_get_lists_status(reg))) {
788 ufshcd_enable_run_stop_reg(hba);
791 "Host controller not ready to process requests");
796 /* Enable required interrupts */
797 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
799 /* Configure interrupt aggregation */
800 ufshcd_config_int_aggr(hba, INT_AGGR_CONFIG);
802 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
803 scsi_unblock_requests(hba->host);
805 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
806 scsi_scan_host(hba->host);
812 * ufshcd_hba_enable - initialize the controller
813 * @hba: per adapter instance
815 * The controller resets itself and controller firmware initialization
816 * sequence kicks off. When controller is ready it will set
817 * the Host Controller Enable bit to 1.
819 * Returns 0 on success, non-zero value on failure
821 static int ufshcd_hba_enable(struct ufs_hba *hba)
826 * msleep of 1 and 5 used in this function might result in msleep(20),
827 * but it was necessary to send the UFS FPGA to reset mode during
828 * development and testing of this driver. msleep can be changed to
829 * mdelay and retry count can be reduced based on the controller.
831 if (!ufshcd_is_hba_active(hba)) {
833 /* change controller state to "reset state" */
834 ufshcd_hba_stop(hba);
837 * This delay is based on the testing done with UFS host
838 * controller FPGA. The delay can be changed based on the
839 * host controller used.
844 /* start controller initialization sequence */
845 ufshcd_hba_start(hba);
848 * To initialize a UFS host controller HCE bit must be set to 1.
849 * During initialization the HCE bit value changes from 1->0->1.
850 * When the host controller completes initialization sequence
851 * it sets the value of HCE bit to 1. The same HCE bit is read back
852 * to check if the controller has completed initialization sequence.
853 * So without this delay the value HCE = 1, set in the previous
854 * instruction might be read back.
855 * This delay can be changed based on the controller.
859 /* wait for the host controller to complete initialization */
861 while (ufshcd_is_hba_active(hba)) {
866 "Controller enable failed\n");
875 * ufshcd_initialize_hba - start the initialization process
876 * @hba: per adapter instance
878 * 1. Enable the controller via ufshcd_hba_enable.
879 * 2. Program the Transfer Request List Address with the starting address of
881 * 3. Program the Task Management Request List Address with starting address
884 * Returns 0 on success, non-zero value on failure.
886 static int ufshcd_initialize_hba(struct ufs_hba *hba)
888 if (ufshcd_hba_enable(hba))
891 /* Configure UTRL and UTMRL base address registers */
892 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
893 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
894 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
895 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
896 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
897 REG_UTP_TASK_REQ_LIST_BASE_L);
898 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
899 REG_UTP_TASK_REQ_LIST_BASE_H);
901 /* Initialize unipro link startup procedure */
902 return ufshcd_dme_link_startup(hba);
906 * ufshcd_do_reset - reset the host controller
907 * @hba: per adapter instance
909 * Returns SUCCESS/FAILED
911 static int ufshcd_do_reset(struct ufs_hba *hba)
913 struct ufshcd_lrb *lrbp;
917 /* block commands from midlayer */
918 scsi_block_requests(hba->host);
920 spin_lock_irqsave(hba->host->host_lock, flags);
921 hba->ufshcd_state = UFSHCD_STATE_RESET;
923 /* send controller to reset state */
924 ufshcd_hba_stop(hba);
925 spin_unlock_irqrestore(hba->host->host_lock, flags);
927 /* abort outstanding commands */
928 for (tag = 0; tag < hba->nutrs; tag++) {
929 if (test_bit(tag, &hba->outstanding_reqs)) {
930 lrbp = &hba->lrb[tag];
931 scsi_dma_unmap(lrbp->cmd);
932 lrbp->cmd->result = DID_RESET << 16;
933 lrbp->cmd->scsi_done(lrbp->cmd);
938 /* clear outstanding request/task bit maps */
939 hba->outstanding_reqs = 0;
940 hba->outstanding_tasks = 0;
942 /* start the initialization process */
943 if (ufshcd_initialize_hba(hba)) {
945 "Reset: Controller initialization failed\n");
952 * ufshcd_slave_alloc - handle initial SCSI device configurations
953 * @sdev: pointer to SCSI device
957 static int ufshcd_slave_alloc(struct scsi_device *sdev)
961 hba = shost_priv(sdev->host);
962 sdev->tagged_supported = 1;
964 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
965 sdev->use_10_for_ms = 1;
966 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
969 * Inform SCSI Midlayer that the LUN queue depth is same as the
970 * controller queue depth. If a LUN queue depth is less than the
971 * controller queue depth and if the LUN reports
972 * SAM_STAT_TASK_SET_FULL, the LUN queue depth will be adjusted
973 * with scsi_adjust_queue_depth.
975 scsi_activate_tcq(sdev, hba->nutrs);
980 * ufshcd_slave_destroy - remove SCSI device configurations
981 * @sdev: pointer to SCSI device
983 static void ufshcd_slave_destroy(struct scsi_device *sdev)
987 hba = shost_priv(sdev->host);
988 scsi_deactivate_tcq(sdev, hba->nutrs);
992 * ufshcd_task_req_compl - handle task management request completion
993 * @hba: per adapter instance
994 * @index: index of the completed request
996 * Returns SUCCESS/FAILED
998 static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index)
1000 struct utp_task_req_desc *task_req_descp;
1001 struct utp_upiu_task_rsp *task_rsp_upiup;
1002 unsigned long flags;
1006 spin_lock_irqsave(hba->host->host_lock, flags);
1008 /* Clear completed tasks from outstanding_tasks */
1009 __clear_bit(index, &hba->outstanding_tasks);
1011 task_req_descp = hba->utmrdl_base_addr;
1012 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
1014 if (ocs_value == OCS_SUCCESS) {
1015 task_rsp_upiup = (struct utp_upiu_task_rsp *)
1016 task_req_descp[index].task_rsp_upiu;
1017 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
1018 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
1020 if (task_result != UPIU_TASK_MANAGEMENT_FUNC_COMPL &&
1021 task_result != UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED)
1022 task_result = FAILED;
1024 task_result = SUCCESS;
1026 task_result = FAILED;
1028 "trc: Invalid ocs = %x\n", ocs_value);
1030 spin_unlock_irqrestore(hba->host->host_lock, flags);
1035 * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with
1036 * SAM_STAT_TASK_SET_FULL SCSI command status.
1037 * @cmd: pointer to SCSI command
1039 static void ufshcd_adjust_lun_qdepth(struct scsi_cmnd *cmd)
1041 struct ufs_hba *hba;
1045 hba = shost_priv(cmd->device->host);
1048 * LUN queue depth can be obtained by counting outstanding commands
1051 for (i = 0; i < hba->nutrs; i++) {
1052 if (test_bit(i, &hba->outstanding_reqs)) {
1055 * Check if the outstanding command belongs
1056 * to the LUN which reported SAM_STAT_TASK_SET_FULL.
1058 if (cmd->device->lun == hba->lrb[i].lun)
1064 * LUN queue depth will be total outstanding commands, except the
1065 * command for which the LUN reported SAM_STAT_TASK_SET_FULL.
1067 scsi_adjust_queue_depth(cmd->device, MSG_SIMPLE_TAG, lun_qdepth - 1);
1071 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
1072 * @lrb: pointer to local reference block of completed command
1073 * @scsi_status: SCSI command status
1075 * Returns value base on SCSI command status
1078 ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
1082 switch (scsi_status) {
1084 result |= DID_OK << 16 |
1085 COMMAND_COMPLETE << 8 |
1088 case SAM_STAT_CHECK_CONDITION:
1089 result |= DID_OK << 16 |
1090 COMMAND_COMPLETE << 8 |
1091 SAM_STAT_CHECK_CONDITION;
1092 ufshcd_copy_sense_data(lrbp);
1095 result |= SAM_STAT_BUSY;
1097 case SAM_STAT_TASK_SET_FULL:
1100 * If a LUN reports SAM_STAT_TASK_SET_FULL, then the LUN queue
1101 * depth needs to be adjusted to the exact number of
1102 * outstanding commands the LUN can handle at any given time.
1104 ufshcd_adjust_lun_qdepth(lrbp->cmd);
1105 result |= SAM_STAT_TASK_SET_FULL;
1107 case SAM_STAT_TASK_ABORTED:
1108 result |= SAM_STAT_TASK_ABORTED;
1111 result |= DID_ERROR << 16;
1113 } /* end of switch */
1119 * ufshcd_transfer_rsp_status - Get overall status of the response
1120 * @hba: per adapter instance
1121 * @lrb: pointer to local reference block of completed command
1123 * Returns result of the command to notify SCSI midlayer
1126 ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1132 /* overall command status of utrd */
1133 ocs = ufshcd_get_tr_ocs(lrbp);
1138 /* check if the returned transfer response is valid */
1139 result = ufshcd_is_valid_req_rsp(lrbp->ucd_rsp_ptr);
1142 "Invalid response = %x\n", result);
1147 * get the response UPIU result to extract
1148 * the SCSI command status
1150 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
1153 * get the result based on SCSI status response
1154 * to notify the SCSI midlayer of the command status
1156 scsi_status = result & MASK_SCSI_STATUS;
1157 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
1160 result |= DID_ABORT << 16;
1162 case OCS_INVALID_CMD_TABLE_ATTR:
1163 case OCS_INVALID_PRDT_ATTR:
1164 case OCS_MISMATCH_DATA_BUF_SIZE:
1165 case OCS_MISMATCH_RESP_UPIU_SIZE:
1166 case OCS_PEER_COMM_FAILURE:
1167 case OCS_FATAL_ERROR:
1169 result |= DID_ERROR << 16;
1171 "OCS error from controller = %x\n", ocs);
1173 } /* end of switch */
1179 * ufshcd_transfer_req_compl - handle SCSI and query command completion
1180 * @hba: per adapter instance
1182 static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
1184 struct ufshcd_lrb *lrb;
1185 unsigned long completed_reqs;
1191 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
1192 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
1194 for (index = 0; index < hba->nutrs; index++) {
1195 if (test_bit(index, &completed_reqs)) {
1197 result = ufshcd_transfer_rsp_status(hba, &lrb[index]);
1199 if (lrb[index].cmd) {
1200 scsi_dma_unmap(lrb[index].cmd);
1201 lrb[index].cmd->result = result;
1202 lrb[index].cmd->scsi_done(lrb[index].cmd);
1204 /* Mark completed command as NULL in LRB */
1205 lrb[index].cmd = NULL;
1210 /* clear corresponding bits of completed commands */
1211 hba->outstanding_reqs ^= completed_reqs;
1213 /* Reset interrupt aggregation counters */
1214 ufshcd_config_int_aggr(hba, INT_AGGR_RESET);
1218 * ufshcd_uic_cc_handler - handle UIC command completion
1219 * @work: pointer to a work queue structure
1221 * Returns 0 on success, non-zero value on failure
1223 static void ufshcd_uic_cc_handler (struct work_struct *work)
1225 struct ufs_hba *hba;
1227 hba = container_of(work, struct ufs_hba, uic_workq);
1229 if ((hba->active_uic_cmd.command == UIC_CMD_DME_LINK_STARTUP) &&
1230 !(ufshcd_get_uic_cmd_result(hba))) {
1232 if (ufshcd_make_hba_operational(hba))
1234 "cc: hba not operational state\n");
1240 * ufshcd_fatal_err_handler - handle fatal errors
1241 * @hba: per adapter instance
1243 static void ufshcd_fatal_err_handler(struct work_struct *work)
1245 struct ufs_hba *hba;
1246 hba = container_of(work, struct ufs_hba, feh_workq);
1248 /* check if reset is already in progress */
1249 if (hba->ufshcd_state != UFSHCD_STATE_RESET)
1250 ufshcd_do_reset(hba);
1254 * ufshcd_err_handler - Check for fatal errors
1255 * @work: pointer to a work queue structure
1257 static void ufshcd_err_handler(struct ufs_hba *hba)
1261 if (hba->errors & INT_FATAL_ERRORS)
1264 if (hba->errors & UIC_ERROR) {
1265 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER);
1266 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
1271 hba->ufshcd_state = UFSHCD_STATE_ERROR;
1272 schedule_work(&hba->feh_workq);
1276 * ufshcd_tmc_handler - handle task management function completion
1277 * @hba: per adapter instance
1279 static void ufshcd_tmc_handler(struct ufs_hba *hba)
1283 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
1284 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
1285 wake_up_interruptible(&hba->ufshcd_tm_wait_queue);
1289 * ufshcd_sl_intr - Interrupt service routine
1290 * @hba: per adapter instance
1291 * @intr_status: contains interrupts generated by the controller
1293 static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
1295 hba->errors = UFSHCD_ERROR_MASK & intr_status;
1297 ufshcd_err_handler(hba);
1299 if (intr_status & UIC_COMMAND_COMPL)
1300 schedule_work(&hba->uic_workq);
1302 if (intr_status & UTP_TASK_REQ_COMPL)
1303 ufshcd_tmc_handler(hba);
1305 if (intr_status & UTP_TRANSFER_REQ_COMPL)
1306 ufshcd_transfer_req_compl(hba);
1310 * ufshcd_intr - Main interrupt service routine
1312 * @__hba: pointer to adapter instance
1314 * Returns IRQ_HANDLED - If interrupt is valid
1315 * IRQ_NONE - If invalid interrupt
1317 static irqreturn_t ufshcd_intr(int irq, void *__hba)
1320 irqreturn_t retval = IRQ_NONE;
1321 struct ufs_hba *hba = __hba;
1323 spin_lock(hba->host->host_lock);
1324 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
1327 ufshcd_sl_intr(hba, intr_status);
1329 /* If UFSHCI 1.0 then clear interrupt status register */
1330 if (hba->ufs_version == UFSHCI_VERSION_10)
1331 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
1332 retval = IRQ_HANDLED;
1334 spin_unlock(hba->host->host_lock);
1339 * ufshcd_issue_tm_cmd - issues task management commands to controller
1340 * @hba: per adapter instance
1341 * @lrbp: pointer to local reference block
1343 * Returns SUCCESS/FAILED
1346 ufshcd_issue_tm_cmd(struct ufs_hba *hba,
1347 struct ufshcd_lrb *lrbp,
1350 struct utp_task_req_desc *task_req_descp;
1351 struct utp_upiu_task_req *task_req_upiup;
1352 struct Scsi_Host *host;
1353 unsigned long flags;
1359 spin_lock_irqsave(host->host_lock, flags);
1361 /* If task management queue is full */
1362 free_slot = ufshcd_get_tm_free_slot(hba);
1363 if (free_slot >= hba->nutmrs) {
1364 spin_unlock_irqrestore(host->host_lock, flags);
1365 dev_err(hba->dev, "Task management queue full\n");
1370 task_req_descp = hba->utmrdl_base_addr;
1371 task_req_descp += free_slot;
1373 /* Configure task request descriptor */
1374 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
1375 task_req_descp->header.dword_2 =
1376 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
1378 /* Configure task request UPIU */
1380 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
1381 task_req_upiup->header.dword_0 =
1382 cpu_to_be32(UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
1383 lrbp->lun, lrbp->task_tag));
1384 task_req_upiup->header.dword_1 =
1385 cpu_to_be32(UPIU_HEADER_DWORD(0, tm_function, 0, 0));
1387 task_req_upiup->input_param1 = lrbp->lun;
1388 task_req_upiup->input_param1 =
1389 cpu_to_be32(task_req_upiup->input_param1);
1390 task_req_upiup->input_param2 = lrbp->task_tag;
1391 task_req_upiup->input_param2 =
1392 cpu_to_be32(task_req_upiup->input_param2);
1394 /* send command to the controller */
1395 __set_bit(free_slot, &hba->outstanding_tasks);
1396 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
1398 spin_unlock_irqrestore(host->host_lock, flags);
1400 /* wait until the task management command is completed */
1402 wait_event_interruptible_timeout(hba->ufshcd_tm_wait_queue,
1403 (test_bit(free_slot,
1404 &hba->tm_condition) != 0),
1408 "Task management command timed-out\n");
1412 clear_bit(free_slot, &hba->tm_condition);
1413 err = ufshcd_task_req_compl(hba, free_slot);
1419 * ufshcd_device_reset - reset device and abort all the pending commands
1420 * @cmd: SCSI command pointer
1422 * Returns SUCCESS/FAILED
1424 static int ufshcd_device_reset(struct scsi_cmnd *cmd)
1426 struct Scsi_Host *host;
1427 struct ufs_hba *hba;
1432 host = cmd->device->host;
1433 hba = shost_priv(host);
1434 tag = cmd->request->tag;
1436 err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_LOGICAL_RESET);
1440 for (pos = 0; pos < hba->nutrs; pos++) {
1441 if (test_bit(pos, &hba->outstanding_reqs) &&
1442 (hba->lrb[tag].lun == hba->lrb[pos].lun)) {
1444 /* clear the respective UTRLCLR register bit */
1445 ufshcd_utrl_clear(hba, pos);
1447 clear_bit(pos, &hba->outstanding_reqs);
1449 if (hba->lrb[pos].cmd) {
1450 scsi_dma_unmap(hba->lrb[pos].cmd);
1451 hba->lrb[pos].cmd->result =
1453 hba->lrb[pos].cmd->scsi_done(cmd);
1454 hba->lrb[pos].cmd = NULL;
1463 * ufshcd_host_reset - Main reset function registered with scsi layer
1464 * @cmd: SCSI command pointer
1466 * Returns SUCCESS/FAILED
1468 static int ufshcd_host_reset(struct scsi_cmnd *cmd)
1470 struct ufs_hba *hba;
1472 hba = shost_priv(cmd->device->host);
1474 if (hba->ufshcd_state == UFSHCD_STATE_RESET)
1477 return ufshcd_do_reset(hba);
1481 * ufshcd_abort - abort a specific command
1482 * @cmd: SCSI command pointer
1484 * Returns SUCCESS/FAILED
1486 static int ufshcd_abort(struct scsi_cmnd *cmd)
1488 struct Scsi_Host *host;
1489 struct ufs_hba *hba;
1490 unsigned long flags;
1494 host = cmd->device->host;
1495 hba = shost_priv(host);
1496 tag = cmd->request->tag;
1498 spin_lock_irqsave(host->host_lock, flags);
1500 /* check if command is still pending */
1501 if (!(test_bit(tag, &hba->outstanding_reqs))) {
1503 spin_unlock_irqrestore(host->host_lock, flags);
1506 spin_unlock_irqrestore(host->host_lock, flags);
1508 err = ufshcd_issue_tm_cmd(hba, &hba->lrb[tag], UFS_ABORT_TASK);
1512 scsi_dma_unmap(cmd);
1514 spin_lock_irqsave(host->host_lock, flags);
1516 /* clear the respective UTRLCLR register bit */
1517 ufshcd_utrl_clear(hba, tag);
1519 __clear_bit(tag, &hba->outstanding_reqs);
1520 hba->lrb[tag].cmd = NULL;
1521 spin_unlock_irqrestore(host->host_lock, flags);
1526 static struct scsi_host_template ufshcd_driver_template = {
1527 .module = THIS_MODULE,
1529 .proc_name = UFSHCD,
1530 .queuecommand = ufshcd_queuecommand,
1531 .slave_alloc = ufshcd_slave_alloc,
1532 .slave_destroy = ufshcd_slave_destroy,
1533 .eh_abort_handler = ufshcd_abort,
1534 .eh_device_reset_handler = ufshcd_device_reset,
1535 .eh_host_reset_handler = ufshcd_host_reset,
1537 .sg_tablesize = SG_ALL,
1538 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
1539 .can_queue = UFSHCD_CAN_QUEUE,
1543 * ufshcd_suspend - suspend power management function
1544 * @hba: per adapter instance
1545 * @state: power state
1549 int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
1553 * 1. Block SCSI requests from SCSI midlayer
1554 * 2. Change the internal driver state to non operational
1555 * 3. Set UTRLRSR and UTMRLRSR bits to zero
1556 * 4. Wait until outstanding commands are completed
1557 * 5. Set HCE to zero to send the UFS host controller to reset state
1562 EXPORT_SYMBOL_GPL(ufshcd_suspend);
1565 * ufshcd_resume - resume power management function
1566 * @hba: per adapter instance
1570 int ufshcd_resume(struct ufs_hba *hba)
1574 * 1. Set HCE to 1, to start the UFS host controller
1575 * initialization process
1576 * 2. Set UTRLRSR and UTMRLRSR bits to 1
1577 * 3. Change the internal driver state to operational
1578 * 4. Unblock SCSI requests from SCSI midlayer
1583 EXPORT_SYMBOL_GPL(ufshcd_resume);
1586 * ufshcd_hba_free - free allocated memory for
1587 * host memory space data structures
1588 * @hba: per adapter instance
1590 static void ufshcd_hba_free(struct ufs_hba *hba)
1592 iounmap(hba->mmio_base);
1593 ufshcd_free_hba_memory(hba);
1597 * ufshcd_remove - de-allocate SCSI host and host memory space
1598 * data structure memory
1599 * @hba - per adapter instance
1601 void ufshcd_remove(struct ufs_hba *hba)
1603 /* disable interrupts */
1604 ufshcd_disable_intr(hba, hba->intr_mask);
1606 ufshcd_hba_stop(hba);
1607 ufshcd_hba_free(hba);
1609 scsi_remove_host(hba->host);
1610 scsi_host_put(hba->host);
1612 EXPORT_SYMBOL_GPL(ufshcd_remove);
1615 * ufshcd_init - Driver initialization routine
1616 * @dev: pointer to device handle
1617 * @hba_handle: driver private handle
1618 * @mmio_base: base register address
1619 * @irq: Interrupt line of device
1620 * Returns 0 on success, non-zero value on failure
1622 int ufshcd_init(struct device *dev, struct ufs_hba **hba_handle,
1623 void __iomem *mmio_base, unsigned int irq)
1625 struct Scsi_Host *host;
1626 struct ufs_hba *hba;
1631 "Invalid memory reference for dev is NULL\n");
1638 "Invalid memory reference for mmio_base is NULL\n");
1643 host = scsi_host_alloc(&ufshcd_driver_template,
1644 sizeof(struct ufs_hba));
1646 dev_err(dev, "scsi_host_alloc failed\n");
1650 hba = shost_priv(host);
1653 hba->mmio_base = mmio_base;
1656 /* Read capabilities registers */
1657 ufshcd_hba_capabilities(hba);
1659 /* Get UFS version supported by the controller */
1660 hba->ufs_version = ufshcd_get_ufs_version(hba);
1662 /* Get Interrupt bit mask per version */
1663 hba->intr_mask = ufshcd_get_intr_mask(hba);
1665 /* Allocate memory for host memory space */
1666 err = ufshcd_memory_alloc(hba);
1668 dev_err(hba->dev, "Memory allocation failed\n");
1673 ufshcd_host_memory_configure(hba);
1675 host->can_queue = hba->nutrs;
1676 host->cmd_per_lun = hba->nutrs;
1677 host->max_id = UFSHCD_MAX_ID;
1678 host->max_lun = UFSHCD_MAX_LUNS;
1679 host->max_channel = UFSHCD_MAX_CHANNEL;
1680 host->unique_id = host->host_no;
1681 host->max_cmd_len = MAX_CDB_SIZE;
1683 /* Initailize wait queue for task management */
1684 init_waitqueue_head(&hba->ufshcd_tm_wait_queue);
1686 /* Initialize work queues */
1687 INIT_WORK(&hba->uic_workq, ufshcd_uic_cc_handler);
1688 INIT_WORK(&hba->feh_workq, ufshcd_fatal_err_handler);
1690 /* IRQ registration */
1691 err = request_irq(irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
1693 dev_err(hba->dev, "request irq failed\n");
1697 /* Enable SCSI tag mapping */
1698 err = scsi_init_shared_tag_map(host, host->can_queue);
1700 dev_err(hba->dev, "init shared queue failed\n");
1704 err = scsi_add_host(host, hba->dev);
1706 dev_err(hba->dev, "scsi_add_host failed\n");
1710 /* Initialization routine */
1711 err = ufshcd_initialize_hba(hba);
1713 dev_err(hba->dev, "Initialization failed\n");
1714 goto out_remove_scsi_host;
1720 out_remove_scsi_host:
1721 scsi_remove_host(hba->host);
1725 ufshcd_free_hba_memory(hba);
1727 scsi_host_put(host);
1731 EXPORT_SYMBOL_GPL(ufshcd_init);
1733 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
1734 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
1735 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
1736 MODULE_LICENSE("GPL");
1737 MODULE_VERSION(UFSHCD_DRIVER_VERSION);