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
6 * Copyright (c) 2013-2014, The Linux Foundation. All rights reserved.
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
40 #include <linux/async.h>
45 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
49 /* UIC command timeout, unit: ms */
50 #define UIC_CMD_TIMEOUT 500
52 /* NOP OUT retries waiting for NOP IN response */
53 #define NOP_OUT_RETRIES 10
54 /* Timeout after 30 msecs if NOP OUT hangs without response */
55 #define NOP_OUT_TIMEOUT 30 /* msecs */
57 /* Query request retries */
58 #define QUERY_REQ_RETRIES 10
59 /* Query request timeout */
60 #define QUERY_REQ_TIMEOUT 30 /* msec */
62 /* Task management command timeout */
63 #define TM_CMD_TIMEOUT 100 /* msecs */
65 /* Expose the flag value from utp_upiu_query.value */
66 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
68 /* Interrupt aggregation default timeout, unit: 40us */
69 #define INT_AGGR_DEF_TO 0x02
71 #define ufshcd_toggle_vreg(_dev, _vreg, _on) \
75 _ret = ufshcd_enable_vreg(_dev, _vreg); \
77 _ret = ufshcd_disable_vreg(_dev, _vreg); \
82 UFSHCD_MAX_CHANNEL = 0,
85 UFSHCD_CMD_PER_LUN = 32,
86 UFSHCD_CAN_QUEUE = 32,
93 UFSHCD_STATE_OPERATIONAL,
96 /* UFSHCD error handling flags */
98 UFSHCD_EH_IN_PROGRESS = (1 << 0),
101 /* UFSHCD UIC layer error flags */
103 UFSHCD_UIC_DL_PA_INIT_ERROR = (1 << 0), /* Data link layer error */
104 UFSHCD_UIC_NL_ERROR = (1 << 1), /* Network layer error */
105 UFSHCD_UIC_TL_ERROR = (1 << 2), /* Transport Layer error */
106 UFSHCD_UIC_DME_ERROR = (1 << 3), /* DME error */
109 /* Interrupt configuration options */
116 #define ufshcd_set_eh_in_progress(h) \
117 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
118 #define ufshcd_eh_in_progress(h) \
119 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
120 #define ufshcd_clear_eh_in_progress(h) \
121 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
123 static void ufshcd_tmc_handler(struct ufs_hba *hba);
124 static void ufshcd_async_scan(void *data, async_cookie_t cookie);
125 static int ufshcd_reset_and_restore(struct ufs_hba *hba);
126 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
127 static int ufshcd_read_sdev_qdepth(struct ufs_hba *hba,
128 struct scsi_device *sdev);
131 * ufshcd_wait_for_register - wait for register value to change
132 * @hba - per-adapter interface
133 * @reg - mmio register offset
134 * @mask - mask to apply to read register value
135 * @val - wait condition
136 * @interval_us - polling interval in microsecs
137 * @timeout_ms - timeout in millisecs
139 * Returns -ETIMEDOUT on error, zero on success
141 static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask,
142 u32 val, unsigned long interval_us, unsigned long timeout_ms)
145 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
147 /* ignore bits that we don't intend to wait on */
150 while ((ufshcd_readl(hba, reg) & mask) != val) {
151 /* wakeup within 50us of expiry */
152 usleep_range(interval_us, interval_us + 50);
154 if (time_after(jiffies, timeout)) {
155 if ((ufshcd_readl(hba, reg) & mask) != val)
165 * ufshcd_get_intr_mask - Get the interrupt bit mask
166 * @hba - Pointer to adapter instance
168 * Returns interrupt bit mask per version
170 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba)
172 if (hba->ufs_version == UFSHCI_VERSION_10)
173 return INTERRUPT_MASK_ALL_VER_10;
175 return INTERRUPT_MASK_ALL_VER_11;
179 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
180 * @hba - Pointer to adapter instance
182 * Returns UFSHCI version supported by the controller
184 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba)
186 return ufshcd_readl(hba, REG_UFS_VERSION);
190 * ufshcd_is_device_present - Check if any device connected to
191 * the host controller
192 * @hba: pointer to adapter instance
194 * Returns 1 if device present, 0 if no device detected
196 static inline int ufshcd_is_device_present(struct ufs_hba *hba)
198 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) &
199 DEVICE_PRESENT) ? 1 : 0;
203 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
204 * @lrb: pointer to local command reference block
206 * This function is used to get the OCS field from UTRD
207 * Returns the OCS field in the UTRD
209 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb *lrbp)
211 return le32_to_cpu(lrbp->utr_descriptor_ptr->header.dword_2) & MASK_OCS;
215 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
216 * @task_req_descp: pointer to utp_task_req_desc structure
218 * This function is used to get the OCS field from UTMRD
219 * Returns the OCS field in the UTMRD
222 ufshcd_get_tmr_ocs(struct utp_task_req_desc *task_req_descp)
224 return le32_to_cpu(task_req_descp->header.dword_2) & MASK_OCS;
228 * ufshcd_get_tm_free_slot - get a free slot for task management request
229 * @hba: per adapter instance
230 * @free_slot: pointer to variable with available slot value
232 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
233 * Returns 0 if free slot is not available, else return 1 with tag value
236 static bool ufshcd_get_tm_free_slot(struct ufs_hba *hba, int *free_slot)
245 tag = find_first_zero_bit(&hba->tm_slots_in_use, hba->nutmrs);
246 if (tag >= hba->nutmrs)
248 } while (test_and_set_bit_lock(tag, &hba->tm_slots_in_use));
256 static inline void ufshcd_put_tm_slot(struct ufs_hba *hba, int slot)
258 clear_bit_unlock(slot, &hba->tm_slots_in_use);
262 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
263 * @hba: per adapter instance
264 * @pos: position of the bit to be cleared
266 static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 pos)
268 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TRANSFER_REQ_LIST_CLEAR);
272 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
273 * @reg: Register value of host controller status
275 * Returns integer, 0 on Success and positive value if failed
277 static inline int ufshcd_get_lists_status(u32 reg)
280 * The mask 0xFF is for the following HCS register bits
290 return (((reg) & (0xFF)) >> 1) ^ (0x07);
294 * ufshcd_get_uic_cmd_result - Get the UIC command result
295 * @hba: Pointer to adapter instance
297 * This function gets the result of UIC command completion
298 * Returns 0 on success, non zero value on error
300 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba)
302 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) &
303 MASK_UIC_COMMAND_RESULT;
307 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
308 * @hba: Pointer to adapter instance
310 * This function gets UIC command argument3
311 * Returns 0 on success, non zero value on error
313 static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba)
315 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3);
319 * ufshcd_get_req_rsp - returns the TR response transaction type
320 * @ucd_rsp_ptr: pointer to response UPIU
323 ufshcd_get_req_rsp(struct utp_upiu_rsp *ucd_rsp_ptr)
325 return be32_to_cpu(ucd_rsp_ptr->header.dword_0) >> 24;
329 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
330 * @ucd_rsp_ptr: pointer to response UPIU
332 * This function gets the response status and scsi_status from response UPIU
333 * Returns the response result code.
336 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp *ucd_rsp_ptr)
338 return be32_to_cpu(ucd_rsp_ptr->header.dword_1) & MASK_RSP_UPIU_RESULT;
342 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
344 * @ucd_rsp_ptr: pointer to response UPIU
346 * Return the data segment length.
348 static inline unsigned int
349 ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp *ucd_rsp_ptr)
351 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
352 MASK_RSP_UPIU_DATA_SEG_LEN;
356 * ufshcd_is_exception_event - Check if the device raised an exception event
357 * @ucd_rsp_ptr: pointer to response UPIU
359 * The function checks if the device raised an exception event indicated in
360 * the Device Information field of response UPIU.
362 * Returns true if exception is raised, false otherwise.
364 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp *ucd_rsp_ptr)
366 return be32_to_cpu(ucd_rsp_ptr->header.dword_2) &
367 MASK_RSP_EXCEPTION_EVENT ? true : false;
371 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
372 * @hba: per adapter instance
375 ufshcd_reset_intr_aggr(struct ufs_hba *hba)
377 ufshcd_writel(hba, INT_AGGR_ENABLE |
378 INT_AGGR_COUNTER_AND_TIMER_RESET,
379 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
383 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
384 * @hba: per adapter instance
385 * @cnt: Interrupt aggregation counter threshold
386 * @tmout: Interrupt aggregation timeout value
389 ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout)
391 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE |
392 INT_AGGR_COUNTER_THLD_VAL(cnt) |
393 INT_AGGR_TIMEOUT_VAL(tmout),
394 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL);
398 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
399 * When run-stop registers are set to 1, it indicates the
400 * host controller that it can process the requests
401 * @hba: per adapter instance
403 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba)
405 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT,
406 REG_UTP_TASK_REQ_LIST_RUN_STOP);
407 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT,
408 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP);
412 * ufshcd_hba_start - Start controller initialization sequence
413 * @hba: per adapter instance
415 static inline void ufshcd_hba_start(struct ufs_hba *hba)
417 ufshcd_writel(hba, CONTROLLER_ENABLE, REG_CONTROLLER_ENABLE);
421 * ufshcd_is_hba_active - Get controller state
422 * @hba: per adapter instance
424 * Returns zero if controller is active, 1 otherwise
426 static inline int ufshcd_is_hba_active(struct ufs_hba *hba)
428 return (ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & 0x1) ? 0 : 1;
432 * ufshcd_send_command - Send SCSI or device management commands
433 * @hba: per adapter instance
434 * @task_tag: Task tag of the command
437 void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag)
439 __set_bit(task_tag, &hba->outstanding_reqs);
440 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TRANSFER_REQ_DOOR_BELL);
444 * ufshcd_copy_sense_data - Copy sense data in case of check condition
445 * @lrb - pointer to local reference block
447 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb *lrbp)
450 if (lrbp->sense_buffer &&
451 ufshcd_get_rsp_upiu_data_seg_len(lrbp->ucd_rsp_ptr)) {
452 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len);
453 memcpy(lrbp->sense_buffer,
454 lrbp->ucd_rsp_ptr->sr.sense_data,
455 min_t(int, len, SCSI_SENSE_BUFFERSIZE));
460 * ufshcd_copy_query_response() - Copy the Query Response and the data
462 * @hba: per adapter instance
463 * @lrb - pointer to local reference block
466 int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
468 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
470 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE);
472 /* Get the descriptor */
473 if (lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) {
474 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr +
475 GENERAL_UPIU_REQUEST_SIZE;
479 /* data segment length */
480 resp_len = be32_to_cpu(lrbp->ucd_rsp_ptr->header.dword_2) &
481 MASK_QUERY_DATA_SEG_LEN;
482 buf_len = be16_to_cpu(
483 hba->dev_cmd.query.request.upiu_req.length);
484 if (likely(buf_len >= resp_len)) {
485 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len);
488 "%s: Response size is bigger than buffer",
498 * ufshcd_hba_capabilities - Read controller capabilities
499 * @hba: per adapter instance
501 static inline void ufshcd_hba_capabilities(struct ufs_hba *hba)
503 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES);
505 /* nutrs and nutmrs are 0 based values */
506 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1;
508 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1;
512 * ufshcd_ready_for_uic_cmd - Check if controller is ready
513 * to accept UIC commands
514 * @hba: per adapter instance
515 * Return true on success, else false
517 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba)
519 if (ufshcd_readl(hba, REG_CONTROLLER_STATUS) & UIC_COMMAND_READY)
526 * ufshcd_get_upmcrs - Get the power mode change request status
527 * @hba: Pointer to adapter instance
529 * This function gets the UPMCRS field of HCS register
530 * Returns value of UPMCRS field
532 static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba)
534 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7;
538 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
539 * @hba: per adapter instance
540 * @uic_cmd: UIC command
542 * Mutex must be held.
545 ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
547 WARN_ON(hba->active_uic_cmd);
549 hba->active_uic_cmd = uic_cmd;
552 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1);
553 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2);
554 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3);
557 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK,
562 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
563 * @hba: per adapter instance
564 * @uic_command: UIC command
566 * Must be called with mutex held.
567 * Returns 0 only if success.
570 ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
575 if (wait_for_completion_timeout(&uic_cmd->done,
576 msecs_to_jiffies(UIC_CMD_TIMEOUT)))
577 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT;
581 spin_lock_irqsave(hba->host->host_lock, flags);
582 hba->active_uic_cmd = NULL;
583 spin_unlock_irqrestore(hba->host->host_lock, flags);
589 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
590 * @hba: per adapter instance
591 * @uic_cmd: UIC command
593 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
595 * Returns 0 only if success.
598 __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
603 if (!ufshcd_ready_for_uic_cmd(hba)) {
605 "Controller not ready to accept UIC commands\n");
609 init_completion(&uic_cmd->done);
611 spin_lock_irqsave(hba->host->host_lock, flags);
612 ufshcd_dispatch_uic_cmd(hba, uic_cmd);
613 spin_unlock_irqrestore(hba->host->host_lock, flags);
615 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd);
621 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
622 * @hba: per adapter instance
623 * @uic_cmd: UIC command
625 * Returns 0 only if success.
628 ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd)
632 mutex_lock(&hba->uic_cmd_mutex);
633 ret = __ufshcd_send_uic_cmd(hba, uic_cmd);
634 mutex_unlock(&hba->uic_cmd_mutex);
640 * ufshcd_map_sg - Map scatter-gather list to prdt
641 * @lrbp - pointer to local reference block
643 * Returns 0 in case of success, non-zero value in case of failure
645 static int ufshcd_map_sg(struct ufshcd_lrb *lrbp)
647 struct ufshcd_sg_entry *prd_table;
648 struct scatterlist *sg;
649 struct scsi_cmnd *cmd;
654 sg_segments = scsi_dma_map(cmd);
659 lrbp->utr_descriptor_ptr->prd_table_length =
660 cpu_to_le16((u16) (sg_segments));
662 prd_table = (struct ufshcd_sg_entry *)lrbp->ucd_prdt_ptr;
664 scsi_for_each_sg(cmd, sg, sg_segments, i) {
666 cpu_to_le32(((u32) sg_dma_len(sg))-1);
667 prd_table[i].base_addr =
668 cpu_to_le32(lower_32_bits(sg->dma_address));
669 prd_table[i].upper_addr =
670 cpu_to_le32(upper_32_bits(sg->dma_address));
673 lrbp->utr_descriptor_ptr->prd_table_length = 0;
680 * ufshcd_enable_intr - enable interrupts
681 * @hba: per adapter instance
682 * @intrs: interrupt bits
684 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs)
686 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
688 if (hba->ufs_version == UFSHCI_VERSION_10) {
690 rw = set & INTERRUPT_MASK_RW_VER_10;
691 set = rw | ((set ^ intrs) & intrs);
696 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
700 * ufshcd_disable_intr - disable interrupts
701 * @hba: per adapter instance
702 * @intrs: interrupt bits
704 static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs)
706 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE);
708 if (hba->ufs_version == UFSHCI_VERSION_10) {
710 rw = (set & INTERRUPT_MASK_RW_VER_10) &
711 ~(intrs & INTERRUPT_MASK_RW_VER_10);
712 set = rw | ((set & intrs) & ~INTERRUPT_MASK_RW_VER_10);
718 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE);
722 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
723 * descriptor according to request
724 * @lrbp: pointer to local reference block
725 * @upiu_flags: flags required in the header
726 * @cmd_dir: requests data direction
728 static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb *lrbp,
729 u32 *upiu_flags, enum dma_data_direction cmd_dir)
731 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr;
735 if (cmd_dir == DMA_FROM_DEVICE) {
736 data_direction = UTP_DEVICE_TO_HOST;
737 *upiu_flags = UPIU_CMD_FLAGS_READ;
738 } else if (cmd_dir == DMA_TO_DEVICE) {
739 data_direction = UTP_HOST_TO_DEVICE;
740 *upiu_flags = UPIU_CMD_FLAGS_WRITE;
742 data_direction = UTP_NO_DATA_TRANSFER;
743 *upiu_flags = UPIU_CMD_FLAGS_NONE;
746 dword_0 = data_direction | (lrbp->command_type
747 << UPIU_COMMAND_TYPE_OFFSET);
749 dword_0 |= UTP_REQ_DESC_INT_CMD;
751 /* Transfer request descriptor header fields */
752 req_desc->header.dword_0 = cpu_to_le32(dword_0);
755 * assigning invalid value for command status. Controller
756 * updates OCS on command completion, with the command
759 req_desc->header.dword_2 =
760 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
764 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
766 * @lrbp - local reference block pointer
767 * @upiu_flags - flags
770 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb *lrbp, u32 upiu_flags)
772 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
774 /* command descriptor fields */
775 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
776 UPIU_TRANSACTION_COMMAND, upiu_flags,
777 lrbp->lun, lrbp->task_tag);
778 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
779 UPIU_COMMAND_SET_TYPE_SCSI, 0, 0, 0);
781 /* Total EHS length and Data segment length will be zero */
782 ucd_req_ptr->header.dword_2 = 0;
784 ucd_req_ptr->sc.exp_data_transfer_len =
785 cpu_to_be32(lrbp->cmd->sdb.length);
787 memcpy(ucd_req_ptr->sc.cdb, lrbp->cmd->cmnd,
788 (min_t(unsigned short, lrbp->cmd->cmd_len, MAX_CDB_SIZE)));
792 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
795 * @lrbp: local reference block pointer
798 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba,
799 struct ufshcd_lrb *lrbp, u32 upiu_flags)
801 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
802 struct ufs_query *query = &hba->dev_cmd.query;
803 u16 len = be16_to_cpu(query->request.upiu_req.length);
804 u8 *descp = (u8 *)lrbp->ucd_req_ptr + GENERAL_UPIU_REQUEST_SIZE;
806 /* Query request header */
807 ucd_req_ptr->header.dword_0 = UPIU_HEADER_DWORD(
808 UPIU_TRANSACTION_QUERY_REQ, upiu_flags,
809 lrbp->lun, lrbp->task_tag);
810 ucd_req_ptr->header.dword_1 = UPIU_HEADER_DWORD(
811 0, query->request.query_func, 0, 0);
813 /* Data segment length */
814 ucd_req_ptr->header.dword_2 = UPIU_HEADER_DWORD(
815 0, 0, len >> 8, (u8)len);
817 /* Copy the Query Request buffer as is */
818 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req,
821 /* Copy the Descriptor */
822 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC)
823 memcpy(descp, query->descriptor, len);
827 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb *lrbp)
829 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr;
831 memset(ucd_req_ptr, 0, sizeof(struct utp_upiu_req));
833 /* command descriptor fields */
834 ucd_req_ptr->header.dword_0 =
836 UPIU_TRANSACTION_NOP_OUT, 0, 0, lrbp->task_tag);
840 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
841 * @hba - per adapter instance
842 * @lrb - pointer to local reference block
844 static int ufshcd_compose_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
849 switch (lrbp->command_type) {
850 case UTP_CMD_TYPE_SCSI:
851 if (likely(lrbp->cmd)) {
852 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags,
853 lrbp->cmd->sc_data_direction);
854 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp, upiu_flags);
859 case UTP_CMD_TYPE_DEV_MANAGE:
860 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, DMA_NONE);
861 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY)
862 ufshcd_prepare_utp_query_req_upiu(
863 hba, lrbp, upiu_flags);
864 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP)
865 ufshcd_prepare_utp_nop_upiu(lrbp);
869 case UTP_CMD_TYPE_UFS:
870 /* For UFS native command implementation */
872 dev_err(hba->dev, "%s: UFS native command are not supported\n",
877 dev_err(hba->dev, "%s: unknown command type: 0x%x\n",
878 __func__, lrbp->command_type);
880 } /* end of switch */
886 * ufshcd_queuecommand - main entry point for SCSI requests
887 * @cmd: command from SCSI Midlayer
888 * @done: call back function
890 * Returns 0 for success, non-zero in case of failure
892 static int ufshcd_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
894 struct ufshcd_lrb *lrbp;
900 hba = shost_priv(host);
902 tag = cmd->request->tag;
904 spin_lock_irqsave(hba->host->host_lock, flags);
905 switch (hba->ufshcd_state) {
906 case UFSHCD_STATE_OPERATIONAL:
908 case UFSHCD_STATE_RESET:
909 err = SCSI_MLQUEUE_HOST_BUSY;
911 case UFSHCD_STATE_ERROR:
912 set_host_byte(cmd, DID_ERROR);
916 dev_WARN_ONCE(hba->dev, 1, "%s: invalid state %d\n",
917 __func__, hba->ufshcd_state);
918 set_host_byte(cmd, DID_BAD_TARGET);
922 spin_unlock_irqrestore(hba->host->host_lock, flags);
924 /* acquire the tag to make sure device cmds don't use it */
925 if (test_and_set_bit_lock(tag, &hba->lrb_in_use)) {
927 * Dev manage command in progress, requeue the command.
928 * Requeuing the command helps in cases where the request *may*
929 * find different tag instead of waiting for dev manage command
932 err = SCSI_MLQUEUE_HOST_BUSY;
936 lrbp = &hba->lrb[tag];
940 lrbp->sense_bufflen = SCSI_SENSE_BUFFERSIZE;
941 lrbp->sense_buffer = cmd->sense_buffer;
942 lrbp->task_tag = tag;
943 lrbp->lun = cmd->device->lun;
944 lrbp->intr_cmd = false;
945 lrbp->command_type = UTP_CMD_TYPE_SCSI;
947 /* form UPIU before issuing the command */
948 ufshcd_compose_upiu(hba, lrbp);
949 err = ufshcd_map_sg(lrbp);
952 clear_bit_unlock(tag, &hba->lrb_in_use);
956 /* issue command to the controller */
957 spin_lock_irqsave(hba->host->host_lock, flags);
958 ufshcd_send_command(hba, tag);
960 spin_unlock_irqrestore(hba->host->host_lock, flags);
965 static int ufshcd_compose_dev_cmd(struct ufs_hba *hba,
966 struct ufshcd_lrb *lrbp, enum dev_cmd_type cmd_type, int tag)
969 lrbp->sense_bufflen = 0;
970 lrbp->sense_buffer = NULL;
971 lrbp->task_tag = tag;
972 lrbp->lun = 0; /* device management cmd is not specific to any LUN */
973 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE;
974 lrbp->intr_cmd = true; /* No interrupt aggregation */
975 hba->dev_cmd.type = cmd_type;
977 return ufshcd_compose_upiu(hba, lrbp);
981 ufshcd_clear_cmd(struct ufs_hba *hba, int tag)
987 /* clear outstanding transaction before retry */
988 spin_lock_irqsave(hba->host->host_lock, flags);
989 ufshcd_utrl_clear(hba, tag);
990 spin_unlock_irqrestore(hba->host->host_lock, flags);
993 * wait for for h/w to clear corresponding bit in door-bell.
994 * max. wait is 1 sec.
996 err = ufshcd_wait_for_register(hba,
997 REG_UTP_TRANSFER_REQ_DOOR_BELL,
998 mask, ~mask, 1000, 1000);
1004 ufshcd_check_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1006 struct ufs_query_res *query_res = &hba->dev_cmd.query.response;
1008 /* Get the UPIU response */
1009 query_res->response = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr) >>
1010 UPIU_RSP_CODE_OFFSET;
1011 return query_res->response;
1015 * ufshcd_dev_cmd_completion() - handles device management command responses
1016 * @hba: per adapter instance
1017 * @lrbp: pointer to local reference block
1020 ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
1025 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
1028 case UPIU_TRANSACTION_NOP_IN:
1029 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) {
1031 dev_err(hba->dev, "%s: unexpected response %x\n",
1035 case UPIU_TRANSACTION_QUERY_RSP:
1036 err = ufshcd_check_query_response(hba, lrbp);
1038 err = ufshcd_copy_query_response(hba, lrbp);
1040 case UPIU_TRANSACTION_REJECT_UPIU:
1041 /* TODO: handle Reject UPIU Response */
1043 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n",
1048 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n",
1056 static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba,
1057 struct ufshcd_lrb *lrbp, int max_timeout)
1060 unsigned long time_left;
1061 unsigned long flags;
1063 time_left = wait_for_completion_timeout(hba->dev_cmd.complete,
1064 msecs_to_jiffies(max_timeout));
1066 spin_lock_irqsave(hba->host->host_lock, flags);
1067 hba->dev_cmd.complete = NULL;
1068 if (likely(time_left)) {
1069 err = ufshcd_get_tr_ocs(lrbp);
1071 err = ufshcd_dev_cmd_completion(hba, lrbp);
1073 spin_unlock_irqrestore(hba->host->host_lock, flags);
1077 if (!ufshcd_clear_cmd(hba, lrbp->task_tag))
1078 /* sucessfully cleared the command, retry if needed */
1086 * ufshcd_get_dev_cmd_tag - Get device management command tag
1087 * @hba: per-adapter instance
1088 * @tag: pointer to variable with available slot value
1090 * Get a free slot and lock it until device management command
1093 * Returns false if free slot is unavailable for locking, else
1094 * return true with tag value in @tag.
1096 static bool ufshcd_get_dev_cmd_tag(struct ufs_hba *hba, int *tag_out)
1106 tmp = ~hba->lrb_in_use;
1107 tag = find_last_bit(&tmp, hba->nutrs);
1108 if (tag >= hba->nutrs)
1110 } while (test_and_set_bit_lock(tag, &hba->lrb_in_use));
1118 static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba *hba, int tag)
1120 clear_bit_unlock(tag, &hba->lrb_in_use);
1124 * ufshcd_exec_dev_cmd - API for sending device management requests
1126 * @cmd_type - specifies the type (NOP, Query...)
1127 * @timeout - time in seconds
1129 * NOTE: Since there is only one available tag for device management commands,
1130 * it is expected you hold the hba->dev_cmd.lock mutex.
1132 static int ufshcd_exec_dev_cmd(struct ufs_hba *hba,
1133 enum dev_cmd_type cmd_type, int timeout)
1135 struct ufshcd_lrb *lrbp;
1138 struct completion wait;
1139 unsigned long flags;
1142 * Get free slot, sleep if slots are unavailable.
1143 * Even though we use wait_event() which sleeps indefinitely,
1144 * the maximum wait time is bounded by SCSI request timeout.
1146 wait_event(hba->dev_cmd.tag_wq, ufshcd_get_dev_cmd_tag(hba, &tag));
1148 init_completion(&wait);
1149 lrbp = &hba->lrb[tag];
1151 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag);
1155 hba->dev_cmd.complete = &wait;
1157 spin_lock_irqsave(hba->host->host_lock, flags);
1158 ufshcd_send_command(hba, tag);
1159 spin_unlock_irqrestore(hba->host->host_lock, flags);
1161 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout);
1164 ufshcd_put_dev_cmd_tag(hba, tag);
1165 wake_up(&hba->dev_cmd.tag_wq);
1170 * ufshcd_init_query() - init the query response and request parameters
1171 * @hba: per-adapter instance
1172 * @request: address of the request pointer to be initialized
1173 * @response: address of the response pointer to be initialized
1174 * @opcode: operation to perform
1175 * @idn: flag idn to access
1176 * @index: LU number to access
1177 * @selector: query/flag/descriptor further identification
1179 static inline void ufshcd_init_query(struct ufs_hba *hba,
1180 struct ufs_query_req **request, struct ufs_query_res **response,
1181 enum query_opcode opcode, u8 idn, u8 index, u8 selector)
1183 *request = &hba->dev_cmd.query.request;
1184 *response = &hba->dev_cmd.query.response;
1185 memset(*request, 0, sizeof(struct ufs_query_req));
1186 memset(*response, 0, sizeof(struct ufs_query_res));
1187 (*request)->upiu_req.opcode = opcode;
1188 (*request)->upiu_req.idn = idn;
1189 (*request)->upiu_req.index = index;
1190 (*request)->upiu_req.selector = selector;
1194 * ufshcd_query_flag() - API function for sending flag query requests
1195 * hba: per-adapter instance
1196 * query_opcode: flag query to perform
1197 * idn: flag idn to access
1198 * flag_res: the flag value after the query request completes
1200 * Returns 0 for success, non-zero in case of failure
1202 static int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode,
1203 enum flag_idn idn, bool *flag_res)
1205 struct ufs_query_req *request = NULL;
1206 struct ufs_query_res *response = NULL;
1207 int err, index = 0, selector = 0;
1211 mutex_lock(&hba->dev_cmd.lock);
1212 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1216 case UPIU_QUERY_OPCODE_SET_FLAG:
1217 case UPIU_QUERY_OPCODE_CLEAR_FLAG:
1218 case UPIU_QUERY_OPCODE_TOGGLE_FLAG:
1219 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1221 case UPIU_QUERY_OPCODE_READ_FLAG:
1222 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1224 /* No dummy reads */
1225 dev_err(hba->dev, "%s: Invalid argument for read request\n",
1233 "%s: Expected query flag opcode but got = %d\n",
1239 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1243 "%s: Sending flag query for idn %d failed, err = %d\n",
1244 __func__, idn, err);
1249 *flag_res = (be32_to_cpu(response->upiu_res.value) &
1250 MASK_QUERY_UPIU_FLAG_LOC) & 0x1;
1253 mutex_unlock(&hba->dev_cmd.lock);
1258 * ufshcd_query_attr - API function for sending attribute requests
1259 * hba: per-adapter instance
1260 * opcode: attribute opcode
1261 * idn: attribute idn to access
1262 * index: index field
1263 * selector: selector field
1264 * attr_val: the attribute value after the query request completes
1266 * Returns 0 for success, non-zero in case of failure
1268 static int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode,
1269 enum attr_idn idn, u8 index, u8 selector, u32 *attr_val)
1271 struct ufs_query_req *request = NULL;
1272 struct ufs_query_res *response = NULL;
1278 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n",
1284 mutex_lock(&hba->dev_cmd.lock);
1285 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1289 case UPIU_QUERY_OPCODE_WRITE_ATTR:
1290 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1291 request->upiu_req.value = cpu_to_be32(*attr_val);
1293 case UPIU_QUERY_OPCODE_READ_ATTR:
1294 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1297 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n",
1303 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1306 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1307 __func__, opcode, idn, err);
1311 *attr_val = be32_to_cpu(response->upiu_res.value);
1314 mutex_unlock(&hba->dev_cmd.lock);
1320 * ufshcd_query_descriptor - API function for sending descriptor requests
1321 * hba: per-adapter instance
1322 * opcode: attribute opcode
1323 * idn: attribute idn to access
1324 * index: index field
1325 * selector: selector field
1326 * desc_buf: the buffer that contains the descriptor
1327 * buf_len: length parameter passed to the device
1329 * Returns 0 for success, non-zero in case of failure.
1330 * The buf_len parameter will contain, on return, the length parameter
1331 * received on the response.
1333 static int ufshcd_query_descriptor(struct ufs_hba *hba,
1334 enum query_opcode opcode, enum desc_idn idn, u8 index,
1335 u8 selector, u8 *desc_buf, int *buf_len)
1337 struct ufs_query_req *request = NULL;
1338 struct ufs_query_res *response = NULL;
1344 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n",
1350 if (*buf_len <= QUERY_DESC_MIN_SIZE || *buf_len > QUERY_DESC_MAX_SIZE) {
1351 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n",
1352 __func__, *buf_len);
1357 mutex_lock(&hba->dev_cmd.lock);
1358 ufshcd_init_query(hba, &request, &response, opcode, idn, index,
1360 hba->dev_cmd.query.descriptor = desc_buf;
1361 request->upiu_req.length = cpu_to_be16(*buf_len);
1364 case UPIU_QUERY_OPCODE_WRITE_DESC:
1365 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST;
1367 case UPIU_QUERY_OPCODE_READ_DESC:
1368 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST;
1372 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1378 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT);
1381 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1382 __func__, opcode, idn, err);
1386 hba->dev_cmd.query.descriptor = NULL;
1387 *buf_len = be16_to_cpu(response->upiu_res.length);
1390 mutex_unlock(&hba->dev_cmd.lock);
1396 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1397 * @hba: per adapter instance
1399 * 1. Allocate DMA memory for Command Descriptor array
1400 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1401 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1402 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1404 * 4. Allocate memory for local reference block(lrb).
1406 * Returns 0 for success, non-zero in case of failure
1408 static int ufshcd_memory_alloc(struct ufs_hba *hba)
1410 size_t utmrdl_size, utrdl_size, ucdl_size;
1412 /* Allocate memory for UTP command descriptors */
1413 ucdl_size = (sizeof(struct utp_transfer_cmd_desc) * hba->nutrs);
1414 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev,
1416 &hba->ucdl_dma_addr,
1420 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1421 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1422 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1423 * be aligned to 128 bytes as well
1425 if (!hba->ucdl_base_addr ||
1426 WARN_ON(hba->ucdl_dma_addr & (PAGE_SIZE - 1))) {
1428 "Command Descriptor Memory allocation failed\n");
1433 * Allocate memory for UTP Transfer descriptors
1434 * UFSHCI requires 1024 byte alignment of UTRD
1436 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs);
1437 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev,
1439 &hba->utrdl_dma_addr,
1441 if (!hba->utrdl_base_addr ||
1442 WARN_ON(hba->utrdl_dma_addr & (PAGE_SIZE - 1))) {
1444 "Transfer Descriptor Memory allocation failed\n");
1449 * Allocate memory for UTP Task Management descriptors
1450 * UFSHCI requires 1024 byte alignment of UTMRD
1452 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs;
1453 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev,
1455 &hba->utmrdl_dma_addr,
1457 if (!hba->utmrdl_base_addr ||
1458 WARN_ON(hba->utmrdl_dma_addr & (PAGE_SIZE - 1))) {
1460 "Task Management Descriptor Memory allocation failed\n");
1464 /* Allocate memory for local reference block */
1465 hba->lrb = devm_kzalloc(hba->dev,
1466 hba->nutrs * sizeof(struct ufshcd_lrb),
1469 dev_err(hba->dev, "LRB Memory allocation failed\n");
1478 * ufshcd_host_memory_configure - configure local reference block with
1480 * @hba: per adapter instance
1482 * Configure Host memory space
1483 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
1485 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
1487 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
1488 * into local reference block.
1490 static void ufshcd_host_memory_configure(struct ufs_hba *hba)
1492 struct utp_transfer_cmd_desc *cmd_descp;
1493 struct utp_transfer_req_desc *utrdlp;
1494 dma_addr_t cmd_desc_dma_addr;
1495 dma_addr_t cmd_desc_element_addr;
1496 u16 response_offset;
1501 utrdlp = hba->utrdl_base_addr;
1502 cmd_descp = hba->ucdl_base_addr;
1505 offsetof(struct utp_transfer_cmd_desc, response_upiu);
1507 offsetof(struct utp_transfer_cmd_desc, prd_table);
1509 cmd_desc_size = sizeof(struct utp_transfer_cmd_desc);
1510 cmd_desc_dma_addr = hba->ucdl_dma_addr;
1512 for (i = 0; i < hba->nutrs; i++) {
1513 /* Configure UTRD with command descriptor base address */
1514 cmd_desc_element_addr =
1515 (cmd_desc_dma_addr + (cmd_desc_size * i));
1516 utrdlp[i].command_desc_base_addr_lo =
1517 cpu_to_le32(lower_32_bits(cmd_desc_element_addr));
1518 utrdlp[i].command_desc_base_addr_hi =
1519 cpu_to_le32(upper_32_bits(cmd_desc_element_addr));
1521 /* Response upiu and prdt offset should be in double words */
1522 utrdlp[i].response_upiu_offset =
1523 cpu_to_le16((response_offset >> 2));
1524 utrdlp[i].prd_table_offset =
1525 cpu_to_le16((prdt_offset >> 2));
1526 utrdlp[i].response_upiu_length =
1527 cpu_to_le16(ALIGNED_UPIU_SIZE >> 2);
1529 hba->lrb[i].utr_descriptor_ptr = (utrdlp + i);
1530 hba->lrb[i].ucd_req_ptr =
1531 (struct utp_upiu_req *)(cmd_descp + i);
1532 hba->lrb[i].ucd_rsp_ptr =
1533 (struct utp_upiu_rsp *)cmd_descp[i].response_upiu;
1534 hba->lrb[i].ucd_prdt_ptr =
1535 (struct ufshcd_sg_entry *)cmd_descp[i].prd_table;
1540 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
1541 * @hba: per adapter instance
1543 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
1544 * in order to initialize the Unipro link startup procedure.
1545 * Once the Unipro links are up, the device connected to the controller
1548 * Returns 0 on success, non-zero value on failure
1550 static int ufshcd_dme_link_startup(struct ufs_hba *hba)
1552 struct uic_command uic_cmd = {0};
1555 uic_cmd.command = UIC_CMD_DME_LINK_STARTUP;
1557 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1560 "dme-link-startup: error code %d\n", ret);
1565 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
1566 * @hba: per adapter instance
1567 * @attr_sel: uic command argument1
1568 * @attr_set: attribute set type as uic command argument2
1569 * @mib_val: setting value as uic command argument3
1570 * @peer: indicate whether peer or local
1572 * Returns 0 on success, non-zero value on failure
1574 int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,
1575 u8 attr_set, u32 mib_val, u8 peer)
1577 struct uic_command uic_cmd = {0};
1578 static const char *const action[] = {
1582 const char *set = action[!!peer];
1585 uic_cmd.command = peer ?
1586 UIC_CMD_DME_PEER_SET : UIC_CMD_DME_SET;
1587 uic_cmd.argument1 = attr_sel;
1588 uic_cmd.argument2 = UIC_ARG_ATTR_TYPE(attr_set);
1589 uic_cmd.argument3 = mib_val;
1591 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1593 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n",
1594 set, UIC_GET_ATTR_ID(attr_sel), mib_val, ret);
1598 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr);
1601 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
1602 * @hba: per adapter instance
1603 * @attr_sel: uic command argument1
1604 * @mib_val: the value of the attribute as returned by the UIC command
1605 * @peer: indicate whether peer or local
1607 * Returns 0 on success, non-zero value on failure
1609 int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,
1610 u32 *mib_val, u8 peer)
1612 struct uic_command uic_cmd = {0};
1613 static const char *const action[] = {
1617 const char *get = action[!!peer];
1620 uic_cmd.command = peer ?
1621 UIC_CMD_DME_PEER_GET : UIC_CMD_DME_GET;
1622 uic_cmd.argument1 = attr_sel;
1624 ret = ufshcd_send_uic_cmd(hba, &uic_cmd);
1626 dev_err(hba->dev, "%s: attr-id 0x%x error code %d\n",
1627 get, UIC_GET_ATTR_ID(attr_sel), ret);
1632 *mib_val = uic_cmd.argument3;
1636 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr);
1639 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
1640 * using DME_SET primitives.
1641 * @hba: per adapter instance
1642 * @mode: powr mode value
1644 * Returns 0 on success, non-zero value on failure
1646 static int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode)
1648 struct uic_command uic_cmd = {0};
1649 struct completion pwr_done;
1650 unsigned long flags;
1654 uic_cmd.command = UIC_CMD_DME_SET;
1655 uic_cmd.argument1 = UIC_ARG_MIB(PA_PWRMODE);
1656 uic_cmd.argument3 = mode;
1657 init_completion(&pwr_done);
1659 mutex_lock(&hba->uic_cmd_mutex);
1661 spin_lock_irqsave(hba->host->host_lock, flags);
1662 hba->pwr_done = &pwr_done;
1663 spin_unlock_irqrestore(hba->host->host_lock, flags);
1664 ret = __ufshcd_send_uic_cmd(hba, &uic_cmd);
1667 "pwr mode change with mode 0x%x uic error %d\n",
1672 if (!wait_for_completion_timeout(hba->pwr_done,
1673 msecs_to_jiffies(UIC_CMD_TIMEOUT))) {
1675 "pwr mode change with mode 0x%x completion timeout\n",
1681 status = ufshcd_get_upmcrs(hba);
1682 if (status != PWR_LOCAL) {
1684 "pwr mode change failed, host umpcrs:0x%x\n",
1686 ret = (status != PWR_OK) ? status : -1;
1689 spin_lock_irqsave(hba->host->host_lock, flags);
1690 hba->pwr_done = NULL;
1691 spin_unlock_irqrestore(hba->host->host_lock, flags);
1692 mutex_unlock(&hba->uic_cmd_mutex);
1697 * ufshcd_config_max_pwr_mode - Set & Change power mode with
1698 * maximum capability attribute information.
1699 * @hba: per adapter instance
1701 * Returns 0 on success, non-zero value on failure
1703 static int ufshcd_config_max_pwr_mode(struct ufs_hba *hba)
1705 enum {RX = 0, TX = 1};
1706 u32 lanes[] = {1, 1};
1707 u32 gear[] = {1, 1};
1708 u8 pwr[] = {FASTAUTO_MODE, FASTAUTO_MODE};
1711 /* Get the connected lane count */
1712 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), &lanes[RX]);
1713 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), &lanes[TX]);
1716 * First, get the maximum gears of HS speed.
1717 * If a zero value, it means there is no HSGEAR capability.
1718 * Then, get the maximum gears of PWM speed.
1720 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &gear[RX]);
1722 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), &gear[RX]);
1723 pwr[RX] = SLOWAUTO_MODE;
1726 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &gear[TX]);
1728 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR),
1730 pwr[TX] = SLOWAUTO_MODE;
1734 * Configure attributes for power mode change with below.
1735 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1736 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1739 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), gear[RX]);
1740 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), lanes[RX]);
1741 if (pwr[RX] == FASTAUTO_MODE)
1742 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), TRUE);
1744 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), gear[TX]);
1745 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), lanes[TX]);
1746 if (pwr[TX] == FASTAUTO_MODE)
1747 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), TRUE);
1749 if (pwr[RX] == FASTAUTO_MODE || pwr[TX] == FASTAUTO_MODE)
1750 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), PA_HS_MODE_B);
1752 ret = ufshcd_uic_change_pwr_mode(hba, pwr[RX] << 4 | pwr[TX]);
1755 "pwr_mode: power mode change failed %d\n", ret);
1761 * ufshcd_complete_dev_init() - checks device readiness
1762 * hba: per-adapter instance
1764 * Set fDeviceInit flag and poll until device toggles it.
1766 static int ufshcd_complete_dev_init(struct ufs_hba *hba)
1768 int i, retries, err = 0;
1771 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1772 /* Set the fDeviceInit flag */
1773 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
1774 QUERY_FLAG_IDN_FDEVICEINIT, NULL);
1775 if (!err || err == -ETIMEDOUT)
1777 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1781 "%s setting fDeviceInit flag failed with error %d\n",
1786 /* poll for max. 100 iterations for fDeviceInit flag to clear */
1787 for (i = 0; i < 100 && !err && flag_res; i++) {
1788 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) {
1789 err = ufshcd_query_flag(hba,
1790 UPIU_QUERY_OPCODE_READ_FLAG,
1791 QUERY_FLAG_IDN_FDEVICEINIT, &flag_res);
1792 if (!err || err == -ETIMEDOUT)
1794 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__,
1800 "%s reading fDeviceInit flag failed with error %d\n",
1804 "%s fDeviceInit was not cleared by the device\n",
1812 * ufshcd_make_hba_operational - Make UFS controller operational
1813 * @hba: per adapter instance
1815 * To bring UFS host controller to operational state,
1816 * 1. Enable required interrupts
1817 * 2. Configure interrupt aggregation
1818 * 3. Program UTRL and UTMRL base addres
1819 * 4. Configure run-stop-registers
1821 * Returns 0 on success, non-zero value on failure
1823 static int ufshcd_make_hba_operational(struct ufs_hba *hba)
1828 /* Enable required interrupts */
1829 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS);
1831 /* Configure interrupt aggregation */
1832 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO);
1834 /* Configure UTRL and UTMRL base address registers */
1835 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr),
1836 REG_UTP_TRANSFER_REQ_LIST_BASE_L);
1837 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr),
1838 REG_UTP_TRANSFER_REQ_LIST_BASE_H);
1839 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr),
1840 REG_UTP_TASK_REQ_LIST_BASE_L);
1841 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr),
1842 REG_UTP_TASK_REQ_LIST_BASE_H);
1845 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
1846 * DEI, HEI bits must be 0
1848 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS);
1849 if (!(ufshcd_get_lists_status(reg))) {
1850 ufshcd_enable_run_stop_reg(hba);
1853 "Host controller not ready to process requests");
1863 * ufshcd_hba_enable - initialize the controller
1864 * @hba: per adapter instance
1866 * The controller resets itself and controller firmware initialization
1867 * sequence kicks off. When controller is ready it will set
1868 * the Host Controller Enable bit to 1.
1870 * Returns 0 on success, non-zero value on failure
1872 static int ufshcd_hba_enable(struct ufs_hba *hba)
1877 * msleep of 1 and 5 used in this function might result in msleep(20),
1878 * but it was necessary to send the UFS FPGA to reset mode during
1879 * development and testing of this driver. msleep can be changed to
1880 * mdelay and retry count can be reduced based on the controller.
1882 if (!ufshcd_is_hba_active(hba)) {
1884 /* change controller state to "reset state" */
1885 ufshcd_hba_stop(hba);
1888 * This delay is based on the testing done with UFS host
1889 * controller FPGA. The delay can be changed based on the
1890 * host controller used.
1895 if (hba->vops && hba->vops->hce_enable_notify)
1896 hba->vops->hce_enable_notify(hba, PRE_CHANGE);
1898 /* start controller initialization sequence */
1899 ufshcd_hba_start(hba);
1902 * To initialize a UFS host controller HCE bit must be set to 1.
1903 * During initialization the HCE bit value changes from 1->0->1.
1904 * When the host controller completes initialization sequence
1905 * it sets the value of HCE bit to 1. The same HCE bit is read back
1906 * to check if the controller has completed initialization sequence.
1907 * So without this delay the value HCE = 1, set in the previous
1908 * instruction might be read back.
1909 * This delay can be changed based on the controller.
1913 /* wait for the host controller to complete initialization */
1915 while (ufshcd_is_hba_active(hba)) {
1920 "Controller enable failed\n");
1926 if (hba->vops && hba->vops->hce_enable_notify)
1927 hba->vops->hce_enable_notify(hba, POST_CHANGE);
1933 * ufshcd_link_startup - Initialize unipro link startup
1934 * @hba: per adapter instance
1936 * Returns 0 for success, non-zero in case of failure
1938 static int ufshcd_link_startup(struct ufs_hba *hba)
1942 /* enable UIC related interrupts */
1943 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL);
1945 if (hba->vops && hba->vops->link_startup_notify)
1946 hba->vops->link_startup_notify(hba, PRE_CHANGE);
1948 ret = ufshcd_dme_link_startup(hba);
1952 /* check if device is detected by inter-connect layer */
1953 if (!ufshcd_is_device_present(hba)) {
1954 dev_err(hba->dev, "%s: Device not present\n", __func__);
1959 /* Include any host controller configuration via UIC commands */
1960 if (hba->vops && hba->vops->link_startup_notify) {
1961 ret = hba->vops->link_startup_notify(hba, POST_CHANGE);
1966 ret = ufshcd_make_hba_operational(hba);
1969 dev_err(hba->dev, "link startup failed %d\n", ret);
1974 * ufshcd_verify_dev_init() - Verify device initialization
1975 * @hba: per-adapter instance
1977 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
1978 * device Transport Protocol (UTP) layer is ready after a reset.
1979 * If the UTP layer at the device side is not initialized, it may
1980 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
1981 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
1983 static int ufshcd_verify_dev_init(struct ufs_hba *hba)
1988 mutex_lock(&hba->dev_cmd.lock);
1989 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) {
1990 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP,
1993 if (!err || err == -ETIMEDOUT)
1996 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err);
1998 mutex_unlock(&hba->dev_cmd.lock);
2001 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err);
2006 * ufshcd_slave_alloc - handle initial SCSI device configurations
2007 * @sdev: pointer to SCSI device
2011 static int ufshcd_slave_alloc(struct scsi_device *sdev)
2013 struct ufs_hba *hba;
2016 hba = shost_priv(sdev->host);
2017 sdev->tagged_supported = 1;
2019 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
2020 sdev->use_10_for_ms = 1;
2021 scsi_set_tag_type(sdev, MSG_SIMPLE_TAG);
2023 /* allow SCSI layer to restart the device in case of errors */
2024 sdev->allow_restart = 1;
2026 /* REPORT SUPPORTED OPERATION CODES is not supported */
2027 sdev->no_report_opcodes = 1;
2029 lun_qdepth = ufshcd_read_sdev_qdepth(hba, sdev);
2030 if (lun_qdepth <= 0)
2031 /* eventually, we can figure out the real queue depth */
2032 lun_qdepth = hba->nutrs;
2034 lun_qdepth = min_t(int, lun_qdepth, hba->nutrs);
2036 dev_dbg(hba->dev, "%s: activate tcq with queue depth %d\n",
2037 __func__, lun_qdepth);
2038 scsi_activate_tcq(sdev, lun_qdepth);
2044 * ufshcd_change_queue_depth - change queue depth
2045 * @sdev: pointer to SCSI device
2046 * @depth: required depth to set
2047 * @reason: reason for changing the depth
2049 * Change queue depth according to the reason and make sure
2050 * the max. limits are not crossed.
2052 static int ufshcd_change_queue_depth(struct scsi_device *sdev,
2053 int depth, int reason)
2055 struct ufs_hba *hba = shost_priv(sdev->host);
2057 if (depth > hba->nutrs)
2061 case SCSI_QDEPTH_DEFAULT:
2062 case SCSI_QDEPTH_RAMP_UP:
2063 if (!sdev->tagged_supported)
2065 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
2067 case SCSI_QDEPTH_QFULL:
2068 scsi_track_queue_full(sdev, depth);
2078 * ufshcd_slave_configure - adjust SCSI device configurations
2079 * @sdev: pointer to SCSI device
2081 static int ufshcd_slave_configure(struct scsi_device *sdev)
2083 struct request_queue *q = sdev->request_queue;
2085 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1);
2086 blk_queue_max_segment_size(q, PRDT_DATA_BYTE_COUNT_MAX);
2092 * ufshcd_slave_destroy - remove SCSI device configurations
2093 * @sdev: pointer to SCSI device
2095 static void ufshcd_slave_destroy(struct scsi_device *sdev)
2097 struct ufs_hba *hba;
2099 hba = shost_priv(sdev->host);
2100 scsi_deactivate_tcq(sdev, hba->nutrs);
2104 * ufshcd_task_req_compl - handle task management request completion
2105 * @hba: per adapter instance
2106 * @index: index of the completed request
2107 * @resp: task management service response
2109 * Returns non-zero value on error, zero on success
2111 static int ufshcd_task_req_compl(struct ufs_hba *hba, u32 index, u8 *resp)
2113 struct utp_task_req_desc *task_req_descp;
2114 struct utp_upiu_task_rsp *task_rsp_upiup;
2115 unsigned long flags;
2119 spin_lock_irqsave(hba->host->host_lock, flags);
2121 /* Clear completed tasks from outstanding_tasks */
2122 __clear_bit(index, &hba->outstanding_tasks);
2124 task_req_descp = hba->utmrdl_base_addr;
2125 ocs_value = ufshcd_get_tmr_ocs(&task_req_descp[index]);
2127 if (ocs_value == OCS_SUCCESS) {
2128 task_rsp_upiup = (struct utp_upiu_task_rsp *)
2129 task_req_descp[index].task_rsp_upiu;
2130 task_result = be32_to_cpu(task_rsp_upiup->header.dword_1);
2131 task_result = ((task_result & MASK_TASK_RESPONSE) >> 8);
2133 *resp = (u8)task_result;
2135 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n",
2136 __func__, ocs_value);
2138 spin_unlock_irqrestore(hba->host->host_lock, flags);
2144 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
2145 * @lrb: pointer to local reference block of completed command
2146 * @scsi_status: SCSI command status
2148 * Returns value base on SCSI command status
2151 ufshcd_scsi_cmd_status(struct ufshcd_lrb *lrbp, int scsi_status)
2155 switch (scsi_status) {
2156 case SAM_STAT_CHECK_CONDITION:
2157 ufshcd_copy_sense_data(lrbp);
2159 result |= DID_OK << 16 |
2160 COMMAND_COMPLETE << 8 |
2163 case SAM_STAT_TASK_SET_FULL:
2165 case SAM_STAT_TASK_ABORTED:
2166 ufshcd_copy_sense_data(lrbp);
2167 result |= scsi_status;
2170 result |= DID_ERROR << 16;
2172 } /* end of switch */
2178 * ufshcd_transfer_rsp_status - Get overall status of the response
2179 * @hba: per adapter instance
2180 * @lrb: pointer to local reference block of completed command
2182 * Returns result of the command to notify SCSI midlayer
2185 ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp)
2191 /* overall command status of utrd */
2192 ocs = ufshcd_get_tr_ocs(lrbp);
2196 result = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr);
2199 case UPIU_TRANSACTION_RESPONSE:
2201 * get the response UPIU result to extract
2202 * the SCSI command status
2204 result = ufshcd_get_rsp_upiu_result(lrbp->ucd_rsp_ptr);
2207 * get the result based on SCSI status response
2208 * to notify the SCSI midlayer of the command status
2210 scsi_status = result & MASK_SCSI_STATUS;
2211 result = ufshcd_scsi_cmd_status(lrbp, scsi_status);
2213 if (ufshcd_is_exception_event(lrbp->ucd_rsp_ptr))
2214 schedule_work(&hba->eeh_work);
2216 case UPIU_TRANSACTION_REJECT_UPIU:
2217 /* TODO: handle Reject UPIU Response */
2218 result = DID_ERROR << 16;
2220 "Reject UPIU not fully implemented\n");
2223 result = DID_ERROR << 16;
2225 "Unexpected request response code = %x\n",
2231 result |= DID_ABORT << 16;
2233 case OCS_INVALID_COMMAND_STATUS:
2234 result |= DID_REQUEUE << 16;
2236 case OCS_INVALID_CMD_TABLE_ATTR:
2237 case OCS_INVALID_PRDT_ATTR:
2238 case OCS_MISMATCH_DATA_BUF_SIZE:
2239 case OCS_MISMATCH_RESP_UPIU_SIZE:
2240 case OCS_PEER_COMM_FAILURE:
2241 case OCS_FATAL_ERROR:
2243 result |= DID_ERROR << 16;
2245 "OCS error from controller = %x\n", ocs);
2247 } /* end of switch */
2253 * ufshcd_uic_cmd_compl - handle completion of uic command
2254 * @hba: per adapter instance
2255 * @intr_status: interrupt status generated by the controller
2257 static void ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status)
2259 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) {
2260 hba->active_uic_cmd->argument2 |=
2261 ufshcd_get_uic_cmd_result(hba);
2262 hba->active_uic_cmd->argument3 =
2263 ufshcd_get_dme_attr_val(hba);
2264 complete(&hba->active_uic_cmd->done);
2267 if ((intr_status & UIC_POWER_MODE) && hba->pwr_done)
2268 complete(hba->pwr_done);
2272 * ufshcd_transfer_req_compl - handle SCSI and query command completion
2273 * @hba: per adapter instance
2275 static void ufshcd_transfer_req_compl(struct ufs_hba *hba)
2277 struct ufshcd_lrb *lrbp;
2278 struct scsi_cmnd *cmd;
2279 unsigned long completed_reqs;
2284 /* Resetting interrupt aggregation counters first and reading the
2285 * DOOR_BELL afterward allows us to handle all the completed requests.
2286 * In order to prevent other interrupts starvation the DB is read once
2287 * after reset. The down side of this solution is the possibility of
2288 * false interrupt if device completes another request after resetting
2289 * aggregation and before reading the DB.
2291 ufshcd_reset_intr_aggr(hba);
2293 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
2294 completed_reqs = tr_doorbell ^ hba->outstanding_reqs;
2296 for_each_set_bit(index, &completed_reqs, hba->nutrs) {
2297 lrbp = &hba->lrb[index];
2300 result = ufshcd_transfer_rsp_status(hba, lrbp);
2301 scsi_dma_unmap(cmd);
2302 cmd->result = result;
2303 /* Mark completed command as NULL in LRB */
2305 clear_bit_unlock(index, &hba->lrb_in_use);
2306 /* Do not touch lrbp after scsi done */
2307 cmd->scsi_done(cmd);
2308 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE) {
2309 if (hba->dev_cmd.complete)
2310 complete(hba->dev_cmd.complete);
2314 /* clear corresponding bits of completed commands */
2315 hba->outstanding_reqs ^= completed_reqs;
2317 /* we might have free'd some tags above */
2318 wake_up(&hba->dev_cmd.tag_wq);
2322 * ufshcd_disable_ee - disable exception event
2323 * @hba: per-adapter instance
2324 * @mask: exception event to disable
2326 * Disables exception event in the device so that the EVENT_ALERT
2329 * Returns zero on success, non-zero error value on failure.
2331 static int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask)
2336 if (!(hba->ee_ctrl_mask & mask))
2339 val = hba->ee_ctrl_mask & ~mask;
2340 val &= 0xFFFF; /* 2 bytes */
2341 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
2342 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
2344 hba->ee_ctrl_mask &= ~mask;
2350 * ufshcd_enable_ee - enable exception event
2351 * @hba: per-adapter instance
2352 * @mask: exception event to enable
2354 * Enable corresponding exception event in the device to allow
2355 * device to alert host in critical scenarios.
2357 * Returns zero on success, non-zero error value on failure.
2359 static int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask)
2364 if (hba->ee_ctrl_mask & mask)
2367 val = hba->ee_ctrl_mask | mask;
2368 val &= 0xFFFF; /* 2 bytes */
2369 err = ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_WRITE_ATTR,
2370 QUERY_ATTR_IDN_EE_CONTROL, 0, 0, &val);
2372 hba->ee_ctrl_mask |= mask;
2378 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
2379 * @hba: per-adapter instance
2381 * Allow device to manage background operations on its own. Enabling
2382 * this might lead to inconsistent latencies during normal data transfers
2383 * as the device is allowed to manage its own way of handling background
2386 * Returns zero on success, non-zero on failure.
2388 static int ufshcd_enable_auto_bkops(struct ufs_hba *hba)
2392 if (hba->auto_bkops_enabled)
2395 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_SET_FLAG,
2396 QUERY_FLAG_IDN_BKOPS_EN, NULL);
2398 dev_err(hba->dev, "%s: failed to enable bkops %d\n",
2403 hba->auto_bkops_enabled = true;
2405 /* No need of URGENT_BKOPS exception from the device */
2406 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
2408 dev_err(hba->dev, "%s: failed to disable exception event %d\n",
2415 * ufshcd_disable_auto_bkops - block device in doing background operations
2416 * @hba: per-adapter instance
2418 * Disabling background operations improves command response latency but
2419 * has drawback of device moving into critical state where the device is
2420 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
2421 * host is idle so that BKOPS are managed effectively without any negative
2424 * Returns zero on success, non-zero on failure.
2426 static int ufshcd_disable_auto_bkops(struct ufs_hba *hba)
2430 if (!hba->auto_bkops_enabled)
2434 * If host assisted BKOPs is to be enabled, make sure
2435 * urgent bkops exception is allowed.
2437 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS);
2439 dev_err(hba->dev, "%s: failed to enable exception event %d\n",
2444 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG,
2445 QUERY_FLAG_IDN_BKOPS_EN, NULL);
2447 dev_err(hba->dev, "%s: failed to disable bkops %d\n",
2449 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS);
2453 hba->auto_bkops_enabled = false;
2459 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
2460 * @hba: per adapter instance
2462 * After a device reset the device may toggle the BKOPS_EN flag
2463 * to default value. The s/w tracking variables should be updated
2464 * as well. Do this by forcing enable of auto bkops.
2466 static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba)
2468 hba->auto_bkops_enabled = false;
2469 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS;
2470 ufshcd_enable_auto_bkops(hba);
2473 static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status)
2475 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
2476 QUERY_ATTR_IDN_BKOPS_STATUS, 0, 0, status);
2480 * ufshcd_urgent_bkops - handle urgent bkops exception event
2481 * @hba: per-adapter instance
2483 * Enable fBackgroundOpsEn flag in the device to permit background
2486 static int ufshcd_urgent_bkops(struct ufs_hba *hba)
2491 err = ufshcd_get_bkops_status(hba, &status);
2493 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n",
2498 status = status & 0xF;
2500 /* handle only if status indicates performance impact or critical */
2501 if (status >= BKOPS_STATUS_PERF_IMPACT)
2502 err = ufshcd_enable_auto_bkops(hba);
2507 static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status)
2509 return ufshcd_query_attr(hba, UPIU_QUERY_OPCODE_READ_ATTR,
2510 QUERY_ATTR_IDN_EE_STATUS, 0, 0, status);
2514 * ufshcd_exception_event_handler - handle exceptions raised by device
2515 * @work: pointer to work data
2517 * Read bExceptionEventStatus attribute from the device and handle the
2518 * exception event accordingly.
2520 static void ufshcd_exception_event_handler(struct work_struct *work)
2522 struct ufs_hba *hba;
2525 hba = container_of(work, struct ufs_hba, eeh_work);
2527 pm_runtime_get_sync(hba->dev);
2528 err = ufshcd_get_ee_status(hba, &status);
2530 dev_err(hba->dev, "%s: failed to get exception status %d\n",
2535 status &= hba->ee_ctrl_mask;
2536 if (status & MASK_EE_URGENT_BKOPS) {
2537 err = ufshcd_urgent_bkops(hba);
2539 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n",
2543 pm_runtime_put_sync(hba->dev);
2548 * ufshcd_err_handler - handle UFS errors that require s/w attention
2549 * @work: pointer to work structure
2551 static void ufshcd_err_handler(struct work_struct *work)
2553 struct ufs_hba *hba;
2554 unsigned long flags;
2560 hba = container_of(work, struct ufs_hba, eh_work);
2562 pm_runtime_get_sync(hba->dev);
2564 spin_lock_irqsave(hba->host->host_lock, flags);
2565 if (hba->ufshcd_state == UFSHCD_STATE_RESET) {
2566 spin_unlock_irqrestore(hba->host->host_lock, flags);
2570 hba->ufshcd_state = UFSHCD_STATE_RESET;
2571 ufshcd_set_eh_in_progress(hba);
2573 /* Complete requests that have door-bell cleared by h/w */
2574 ufshcd_transfer_req_compl(hba);
2575 ufshcd_tmc_handler(hba);
2576 spin_unlock_irqrestore(hba->host->host_lock, flags);
2578 /* Clear pending transfer requests */
2579 for_each_set_bit(tag, &hba->outstanding_reqs, hba->nutrs)
2580 if (ufshcd_clear_cmd(hba, tag))
2581 err_xfer |= 1 << tag;
2583 /* Clear pending task management requests */
2584 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs)
2585 if (ufshcd_clear_tm_cmd(hba, tag))
2588 /* Complete the requests that are cleared by s/w */
2589 spin_lock_irqsave(hba->host->host_lock, flags);
2590 ufshcd_transfer_req_compl(hba);
2591 ufshcd_tmc_handler(hba);
2592 spin_unlock_irqrestore(hba->host->host_lock, flags);
2594 /* Fatal errors need reset */
2595 if (err_xfer || err_tm || (hba->saved_err & INT_FATAL_ERRORS) ||
2596 ((hba->saved_err & UIC_ERROR) &&
2597 (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR))) {
2598 err = ufshcd_reset_and_restore(hba);
2600 dev_err(hba->dev, "%s: reset and restore failed\n",
2602 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2605 * Inform scsi mid-layer that we did reset and allow to handle
2606 * Unit Attention properly.
2608 scsi_report_bus_reset(hba->host, 0);
2610 hba->saved_uic_err = 0;
2612 ufshcd_clear_eh_in_progress(hba);
2615 scsi_unblock_requests(hba->host);
2616 pm_runtime_put_sync(hba->dev);
2620 * ufshcd_update_uic_error - check and set fatal UIC error flags.
2621 * @hba: per-adapter instance
2623 static void ufshcd_update_uic_error(struct ufs_hba *hba)
2627 /* PA_INIT_ERROR is fatal and needs UIC reset */
2628 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER);
2629 if (reg & UIC_DATA_LINK_LAYER_ERROR_PA_INIT)
2630 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR;
2632 /* UIC NL/TL/DME errors needs software retry */
2633 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER);
2635 hba->uic_error |= UFSHCD_UIC_NL_ERROR;
2637 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER);
2639 hba->uic_error |= UFSHCD_UIC_TL_ERROR;
2641 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME);
2643 hba->uic_error |= UFSHCD_UIC_DME_ERROR;
2645 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n",
2646 __func__, hba->uic_error);
2650 * ufshcd_check_errors - Check for errors that need s/w attention
2651 * @hba: per-adapter instance
2653 static void ufshcd_check_errors(struct ufs_hba *hba)
2655 bool queue_eh_work = false;
2657 if (hba->errors & INT_FATAL_ERRORS)
2658 queue_eh_work = true;
2660 if (hba->errors & UIC_ERROR) {
2662 ufshcd_update_uic_error(hba);
2664 queue_eh_work = true;
2667 if (queue_eh_work) {
2668 /* handle fatal errors only when link is functional */
2669 if (hba->ufshcd_state == UFSHCD_STATE_OPERATIONAL) {
2670 /* block commands from scsi mid-layer */
2671 scsi_block_requests(hba->host);
2673 /* transfer error masks to sticky bits */
2674 hba->saved_err |= hba->errors;
2675 hba->saved_uic_err |= hba->uic_error;
2677 hba->ufshcd_state = UFSHCD_STATE_ERROR;
2678 schedule_work(&hba->eh_work);
2682 * if (!queue_eh_work) -
2683 * Other errors are either non-fatal where host recovers
2684 * itself without s/w intervention or errors that will be
2685 * handled by the SCSI core layer.
2690 * ufshcd_tmc_handler - handle task management function completion
2691 * @hba: per adapter instance
2693 static void ufshcd_tmc_handler(struct ufs_hba *hba)
2697 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL);
2698 hba->tm_condition = tm_doorbell ^ hba->outstanding_tasks;
2699 wake_up(&hba->tm_wq);
2703 * ufshcd_sl_intr - Interrupt service routine
2704 * @hba: per adapter instance
2705 * @intr_status: contains interrupts generated by the controller
2707 static void ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status)
2709 hba->errors = UFSHCD_ERROR_MASK & intr_status;
2711 ufshcd_check_errors(hba);
2713 if (intr_status & UFSHCD_UIC_MASK)
2714 ufshcd_uic_cmd_compl(hba, intr_status);
2716 if (intr_status & UTP_TASK_REQ_COMPL)
2717 ufshcd_tmc_handler(hba);
2719 if (intr_status & UTP_TRANSFER_REQ_COMPL)
2720 ufshcd_transfer_req_compl(hba);
2724 * ufshcd_intr - Main interrupt service routine
2726 * @__hba: pointer to adapter instance
2728 * Returns IRQ_HANDLED - If interrupt is valid
2729 * IRQ_NONE - If invalid interrupt
2731 static irqreturn_t ufshcd_intr(int irq, void *__hba)
2734 irqreturn_t retval = IRQ_NONE;
2735 struct ufs_hba *hba = __hba;
2737 spin_lock(hba->host->host_lock);
2738 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS);
2741 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS);
2742 ufshcd_sl_intr(hba, intr_status);
2743 retval = IRQ_HANDLED;
2745 spin_unlock(hba->host->host_lock);
2749 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag)
2752 u32 mask = 1 << tag;
2753 unsigned long flags;
2755 if (!test_bit(tag, &hba->outstanding_tasks))
2758 spin_lock_irqsave(hba->host->host_lock, flags);
2759 ufshcd_writel(hba, ~(1 << tag), REG_UTP_TASK_REQ_LIST_CLEAR);
2760 spin_unlock_irqrestore(hba->host->host_lock, flags);
2762 /* poll for max. 1 sec to clear door bell register by h/w */
2763 err = ufshcd_wait_for_register(hba,
2764 REG_UTP_TASK_REQ_DOOR_BELL,
2765 mask, 0, 1000, 1000);
2771 * ufshcd_issue_tm_cmd - issues task management commands to controller
2772 * @hba: per adapter instance
2773 * @lun_id: LUN ID to which TM command is sent
2774 * @task_id: task ID to which the TM command is applicable
2775 * @tm_function: task management function opcode
2776 * @tm_response: task management service response return value
2778 * Returns non-zero value on error, zero on success.
2780 static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id,
2781 u8 tm_function, u8 *tm_response)
2783 struct utp_task_req_desc *task_req_descp;
2784 struct utp_upiu_task_req *task_req_upiup;
2785 struct Scsi_Host *host;
2786 unsigned long flags;
2794 * Get free slot, sleep if slots are unavailable.
2795 * Even though we use wait_event() which sleeps indefinitely,
2796 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
2798 wait_event(hba->tm_tag_wq, ufshcd_get_tm_free_slot(hba, &free_slot));
2800 spin_lock_irqsave(host->host_lock, flags);
2801 task_req_descp = hba->utmrdl_base_addr;
2802 task_req_descp += free_slot;
2804 /* Configure task request descriptor */
2805 task_req_descp->header.dword_0 = cpu_to_le32(UTP_REQ_DESC_INT_CMD);
2806 task_req_descp->header.dword_2 =
2807 cpu_to_le32(OCS_INVALID_COMMAND_STATUS);
2809 /* Configure task request UPIU */
2811 (struct utp_upiu_task_req *) task_req_descp->task_req_upiu;
2812 task_tag = hba->nutrs + free_slot;
2813 task_req_upiup->header.dword_0 =
2814 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ, 0,
2816 task_req_upiup->header.dword_1 =
2817 UPIU_HEADER_DWORD(0, tm_function, 0, 0);
2819 task_req_upiup->input_param1 = cpu_to_be32(lun_id);
2820 task_req_upiup->input_param2 = cpu_to_be32(task_id);
2822 /* send command to the controller */
2823 __set_bit(free_slot, &hba->outstanding_tasks);
2824 ufshcd_writel(hba, 1 << free_slot, REG_UTP_TASK_REQ_DOOR_BELL);
2826 spin_unlock_irqrestore(host->host_lock, flags);
2828 /* wait until the task management command is completed */
2829 err = wait_event_timeout(hba->tm_wq,
2830 test_bit(free_slot, &hba->tm_condition),
2831 msecs_to_jiffies(TM_CMD_TIMEOUT));
2833 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n",
2834 __func__, tm_function);
2835 if (ufshcd_clear_tm_cmd(hba, free_slot))
2836 dev_WARN(hba->dev, "%s: unable clear tm cmd (slot %d) after timeout\n",
2837 __func__, free_slot);
2840 err = ufshcd_task_req_compl(hba, free_slot, tm_response);
2843 clear_bit(free_slot, &hba->tm_condition);
2844 ufshcd_put_tm_slot(hba, free_slot);
2845 wake_up(&hba->tm_tag_wq);
2851 * ufshcd_eh_device_reset_handler - device reset handler registered to
2853 * @cmd: SCSI command pointer
2855 * Returns SUCCESS/FAILED
2857 static int ufshcd_eh_device_reset_handler(struct scsi_cmnd *cmd)
2859 struct Scsi_Host *host;
2860 struct ufs_hba *hba;
2865 struct ufshcd_lrb *lrbp;
2866 unsigned long flags;
2868 host = cmd->device->host;
2869 hba = shost_priv(host);
2870 tag = cmd->request->tag;
2872 lrbp = &hba->lrb[tag];
2873 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, 0, UFS_LOGICAL_RESET, &resp);
2874 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
2880 /* clear the commands that were pending for corresponding LUN */
2881 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) {
2882 if (hba->lrb[pos].lun == lrbp->lun) {
2883 err = ufshcd_clear_cmd(hba, pos);
2888 spin_lock_irqsave(host->host_lock, flags);
2889 ufshcd_transfer_req_compl(hba);
2890 spin_unlock_irqrestore(host->host_lock, flags);
2895 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
2902 * ufshcd_abort - abort a specific command
2903 * @cmd: SCSI command pointer
2905 * Abort the pending command in device by sending UFS_ABORT_TASK task management
2906 * command, and in host controller by clearing the door-bell register. There can
2907 * be race between controller sending the command to the device while abort is
2908 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
2909 * really issued and then try to abort it.
2911 * Returns SUCCESS/FAILED
2913 static int ufshcd_abort(struct scsi_cmnd *cmd)
2915 struct Scsi_Host *host;
2916 struct ufs_hba *hba;
2917 unsigned long flags;
2922 struct ufshcd_lrb *lrbp;
2925 host = cmd->device->host;
2926 hba = shost_priv(host);
2927 tag = cmd->request->tag;
2929 /* If command is already aborted/completed, return SUCCESS */
2930 if (!(test_bit(tag, &hba->outstanding_reqs)))
2933 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
2934 if (!(reg & (1 << tag))) {
2936 "%s: cmd was completed, but without a notifying intr, tag = %d",
2940 lrbp = &hba->lrb[tag];
2941 for (poll_cnt = 100; poll_cnt; poll_cnt--) {
2942 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
2943 UFS_QUERY_TASK, &resp);
2944 if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED) {
2945 /* cmd pending in the device */
2947 } else if (!err && resp == UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
2949 * cmd not pending in the device, check if it is
2952 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL);
2953 if (reg & (1 << tag)) {
2954 /* sleep for max. 200us to stabilize */
2955 usleep_range(100, 200);
2958 /* command completed already */
2962 err = resp; /* service response error */
2972 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag,
2973 UFS_ABORT_TASK, &resp);
2974 if (err || resp != UPIU_TASK_MANAGEMENT_FUNC_COMPL) {
2976 err = resp; /* service response error */
2980 err = ufshcd_clear_cmd(hba, tag);
2984 scsi_dma_unmap(cmd);
2986 spin_lock_irqsave(host->host_lock, flags);
2987 __clear_bit(tag, &hba->outstanding_reqs);
2988 hba->lrb[tag].cmd = NULL;
2989 spin_unlock_irqrestore(host->host_lock, flags);
2991 clear_bit_unlock(tag, &hba->lrb_in_use);
2992 wake_up(&hba->dev_cmd.tag_wq);
2997 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err);
3005 * ufshcd_host_reset_and_restore - reset and restore host controller
3006 * @hba: per-adapter instance
3008 * Note that host controller reset may issue DME_RESET to
3009 * local and remote (device) Uni-Pro stack and the attributes
3010 * are reset to default state.
3012 * Returns zero on success, non-zero on failure
3014 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba)
3017 async_cookie_t cookie;
3018 unsigned long flags;
3020 /* Reset the host controller */
3021 spin_lock_irqsave(hba->host->host_lock, flags);
3022 ufshcd_hba_stop(hba);
3023 spin_unlock_irqrestore(hba->host->host_lock, flags);
3025 err = ufshcd_hba_enable(hba);
3029 /* Establish the link again and restore the device */
3030 cookie = async_schedule(ufshcd_async_scan, hba);
3031 /* wait for async scan to be completed */
3032 async_synchronize_cookie(++cookie);
3033 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL)
3037 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err);
3043 * ufshcd_reset_and_restore - reset and re-initialize host/device
3044 * @hba: per-adapter instance
3046 * Reset and recover device, host and re-establish link. This
3047 * is helpful to recover the communication in fatal error conditions.
3049 * Returns zero on success, non-zero on failure
3051 static int ufshcd_reset_and_restore(struct ufs_hba *hba)
3054 unsigned long flags;
3056 err = ufshcd_host_reset_and_restore(hba);
3059 * After reset the door-bell might be cleared, complete
3060 * outstanding requests in s/w here.
3062 spin_lock_irqsave(hba->host->host_lock, flags);
3063 ufshcd_transfer_req_compl(hba);
3064 ufshcd_tmc_handler(hba);
3065 spin_unlock_irqrestore(hba->host->host_lock, flags);
3071 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
3072 * @cmd - SCSI command pointer
3074 * Returns SUCCESS/FAILED
3076 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd *cmd)
3079 unsigned long flags;
3080 struct ufs_hba *hba;
3082 hba = shost_priv(cmd->device->host);
3085 * Check if there is any race with fatal error handling.
3086 * If so, wait for it to complete. Even though fatal error
3087 * handling does reset and restore in some cases, don't assume
3088 * anything out of it. We are just avoiding race here.
3091 spin_lock_irqsave(hba->host->host_lock, flags);
3092 if (!(work_pending(&hba->eh_work) ||
3093 hba->ufshcd_state == UFSHCD_STATE_RESET))
3095 spin_unlock_irqrestore(hba->host->host_lock, flags);
3096 dev_dbg(hba->dev, "%s: reset in progress\n", __func__);
3097 flush_work(&hba->eh_work);
3100 hba->ufshcd_state = UFSHCD_STATE_RESET;
3101 ufshcd_set_eh_in_progress(hba);
3102 spin_unlock_irqrestore(hba->host->host_lock, flags);
3104 err = ufshcd_reset_and_restore(hba);
3106 spin_lock_irqsave(hba->host->host_lock, flags);
3109 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3112 hba->ufshcd_state = UFSHCD_STATE_ERROR;
3114 ufshcd_clear_eh_in_progress(hba);
3115 spin_unlock_irqrestore(hba->host->host_lock, flags);
3121 * ufshcd_read_sdev_qdepth - read the lun command queue depth
3122 * @hba: Pointer to adapter instance
3123 * @sdev: pointer to SCSI device
3125 * Return in case of success the lun's queue depth else error.
3127 static int ufshcd_read_sdev_qdepth(struct ufs_hba *hba,
3128 struct scsi_device *sdev)
3131 int buff_len = UNIT_DESC_MAX_SIZE;
3132 u8 desc_buf[UNIT_DESC_MAX_SIZE];
3134 ret = ufshcd_query_descriptor(hba, UPIU_QUERY_OPCODE_READ_DESC,
3135 QUERY_DESC_IDN_UNIT, sdev->lun, 0, desc_buf, &buff_len);
3137 if (ret || (buff_len < UNIT_DESC_PARAM_LU_Q_DEPTH)) {
3139 "%s:Failed reading unit descriptor. len = %d ret = %d"
3140 , __func__, buff_len, ret);
3147 ret = desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH] & 0xFF;
3153 * ufshcd_async_scan - asynchronous execution for link startup
3154 * @data: data pointer to pass to this function
3155 * @cookie: cookie data
3157 static void ufshcd_async_scan(void *data, async_cookie_t cookie)
3159 struct ufs_hba *hba = (struct ufs_hba *)data;
3162 ret = ufshcd_link_startup(hba);
3166 ufshcd_config_max_pwr_mode(hba);
3168 ret = ufshcd_verify_dev_init(hba);
3172 ret = ufshcd_complete_dev_init(hba);
3176 ufshcd_force_reset_auto_bkops(hba);
3177 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL;
3179 /* If we are in error handling context no need to scan the host */
3180 if (!ufshcd_eh_in_progress(hba)) {
3181 scsi_scan_host(hba->host);
3182 pm_runtime_put_sync(hba->dev);
3188 static struct scsi_host_template ufshcd_driver_template = {
3189 .module = THIS_MODULE,
3191 .proc_name = UFSHCD,
3192 .queuecommand = ufshcd_queuecommand,
3193 .slave_alloc = ufshcd_slave_alloc,
3194 .slave_configure = ufshcd_slave_configure,
3195 .slave_destroy = ufshcd_slave_destroy,
3196 .change_queue_depth = ufshcd_change_queue_depth,
3197 .eh_abort_handler = ufshcd_abort,
3198 .eh_device_reset_handler = ufshcd_eh_device_reset_handler,
3199 .eh_host_reset_handler = ufshcd_eh_host_reset_handler,
3201 .sg_tablesize = SG_ALL,
3202 .cmd_per_lun = UFSHCD_CMD_PER_LUN,
3203 .can_queue = UFSHCD_CAN_QUEUE,
3206 static int ufshcd_config_vreg(struct device *dev,
3207 struct ufs_vreg *vreg, bool on)
3210 struct regulator *reg = vreg->reg;
3211 const char *name = vreg->name;
3212 int min_uV, uA_load;
3216 if (regulator_count_voltages(reg) > 0) {
3217 min_uV = on ? vreg->min_uV : 0;
3218 ret = regulator_set_voltage(reg, min_uV, vreg->max_uV);
3220 dev_err(dev, "%s: %s set voltage failed, err=%d\n",
3221 __func__, name, ret);
3225 uA_load = on ? vreg->max_uA : 0;
3226 ret = regulator_set_optimum_mode(reg, uA_load);
3229 * regulator_set_optimum_mode() returns new regulator
3230 * mode upon success.
3234 dev_err(dev, "%s: %s set optimum mode(uA_load=%d) failed, err=%d\n",
3235 __func__, name, uA_load, ret);
3243 static int ufshcd_enable_vreg(struct device *dev, struct ufs_vreg *vreg)
3247 if (!vreg || vreg->enabled)
3250 ret = ufshcd_config_vreg(dev, vreg, true);
3252 ret = regulator_enable(vreg->reg);
3255 vreg->enabled = true;
3257 dev_err(dev, "%s: %s enable failed, err=%d\n",
3258 __func__, vreg->name, ret);
3263 static int ufshcd_disable_vreg(struct device *dev, struct ufs_vreg *vreg)
3267 if (!vreg || !vreg->enabled)
3270 ret = regulator_disable(vreg->reg);
3273 /* ignore errors on applying disable config */
3274 ufshcd_config_vreg(dev, vreg, false);
3275 vreg->enabled = false;
3277 dev_err(dev, "%s: %s disable failed, err=%d\n",
3278 __func__, vreg->name, ret);
3284 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on)
3287 struct device *dev = hba->dev;
3288 struct ufs_vreg_info *info = &hba->vreg_info;
3293 ret = ufshcd_toggle_vreg(dev, info->vcc, on);
3297 ret = ufshcd_toggle_vreg(dev, info->vccq, on);
3301 ret = ufshcd_toggle_vreg(dev, info->vccq2, on);
3307 ufshcd_toggle_vreg(dev, info->vccq2, false);
3308 ufshcd_toggle_vreg(dev, info->vccq, false);
3309 ufshcd_toggle_vreg(dev, info->vcc, false);
3314 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on)
3316 struct ufs_vreg_info *info = &hba->vreg_info;
3319 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on);
3324 static int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg)
3331 vreg->reg = devm_regulator_get(dev, vreg->name);
3332 if (IS_ERR(vreg->reg)) {
3333 ret = PTR_ERR(vreg->reg);
3334 dev_err(dev, "%s: %s get failed, err=%d\n",
3335 __func__, vreg->name, ret);
3341 static int ufshcd_init_vreg(struct ufs_hba *hba)
3344 struct device *dev = hba->dev;
3345 struct ufs_vreg_info *info = &hba->vreg_info;
3350 ret = ufshcd_get_vreg(dev, info->vcc);
3354 ret = ufshcd_get_vreg(dev, info->vccq);
3358 ret = ufshcd_get_vreg(dev, info->vccq2);
3363 static int ufshcd_init_hba_vreg(struct ufs_hba *hba)
3365 struct ufs_vreg_info *info = &hba->vreg_info;
3368 return ufshcd_get_vreg(hba->dev, info->vdd_hba);
3373 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on)
3376 struct ufs_clk_info *clki;
3377 struct list_head *head = &hba->clk_list_head;
3379 if (!head || list_empty(head))
3382 list_for_each_entry(clki, head, list) {
3383 if (!IS_ERR_OR_NULL(clki->clk)) {
3384 if (on && !clki->enabled) {
3385 ret = clk_prepare_enable(clki->clk);
3387 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n",
3388 __func__, clki->name, ret);
3391 } else if (!on && clki->enabled) {
3392 clk_disable_unprepare(clki->clk);
3395 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__,
3396 clki->name, on ? "en" : "dis");
3401 list_for_each_entry(clki, head, list) {
3402 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled)
3403 clk_disable_unprepare(clki->clk);
3409 static int ufshcd_init_clocks(struct ufs_hba *hba)
3412 struct ufs_clk_info *clki;
3413 struct device *dev = hba->dev;
3414 struct list_head *head = &hba->clk_list_head;
3416 if (!head || list_empty(head))
3419 list_for_each_entry(clki, head, list) {
3423 clki->clk = devm_clk_get(dev, clki->name);
3424 if (IS_ERR(clki->clk)) {
3425 ret = PTR_ERR(clki->clk);
3426 dev_err(dev, "%s: %s clk get failed, %d\n",
3427 __func__, clki->name, ret);
3431 if (clki->max_freq) {
3432 ret = clk_set_rate(clki->clk, clki->max_freq);
3434 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n",
3435 __func__, clki->name,
3436 clki->max_freq, ret);
3440 dev_dbg(dev, "%s: clk: %s, rate: %lu\n", __func__,
3441 clki->name, clk_get_rate(clki->clk));
3447 static int ufshcd_variant_hba_init(struct ufs_hba *hba)
3454 if (hba->vops->init) {
3455 err = hba->vops->init(hba);
3460 if (hba->vops->setup_clocks) {
3461 err = hba->vops->setup_clocks(hba, true);
3466 if (hba->vops->setup_regulators) {
3467 err = hba->vops->setup_regulators(hba, true);
3475 if (hba->vops->setup_clocks)
3476 hba->vops->setup_clocks(hba, false);
3478 if (hba->vops->exit)
3479 hba->vops->exit(hba);
3482 dev_err(hba->dev, "%s: variant %s init failed err %d\n",
3483 __func__, hba->vops ? hba->vops->name : "", err);
3487 static void ufshcd_variant_hba_exit(struct ufs_hba *hba)
3492 if (hba->vops->setup_clocks)
3493 hba->vops->setup_clocks(hba, false);
3495 if (hba->vops->setup_regulators)
3496 hba->vops->setup_regulators(hba, false);
3498 if (hba->vops->exit)
3499 hba->vops->exit(hba);
3502 static int ufshcd_hba_init(struct ufs_hba *hba)
3507 * Handle host controller power separately from the UFS device power
3508 * rails as it will help controlling the UFS host controller power
3509 * collapse easily which is different than UFS device power collapse.
3510 * Also, enable the host controller power before we go ahead with rest
3511 * of the initialization here.
3513 err = ufshcd_init_hba_vreg(hba);
3517 err = ufshcd_setup_hba_vreg(hba, true);
3521 err = ufshcd_init_clocks(hba);
3523 goto out_disable_hba_vreg;
3525 err = ufshcd_setup_clocks(hba, true);
3527 goto out_disable_hba_vreg;
3529 err = ufshcd_init_vreg(hba);
3531 goto out_disable_clks;
3533 err = ufshcd_setup_vreg(hba, true);
3535 goto out_disable_clks;
3537 err = ufshcd_variant_hba_init(hba);
3539 goto out_disable_vreg;
3544 ufshcd_setup_vreg(hba, false);
3546 ufshcd_setup_clocks(hba, false);
3547 out_disable_hba_vreg:
3548 ufshcd_setup_hba_vreg(hba, false);
3553 static void ufshcd_hba_exit(struct ufs_hba *hba)
3555 ufshcd_variant_hba_exit(hba);
3556 ufshcd_setup_vreg(hba, false);
3557 ufshcd_setup_clocks(hba, false);
3558 ufshcd_setup_hba_vreg(hba, false);
3562 * ufshcd_suspend - suspend power management function
3563 * @hba: per adapter instance
3564 * @state: power state
3568 int ufshcd_suspend(struct ufs_hba *hba, pm_message_t state)
3572 * 1. Block SCSI requests from SCSI midlayer
3573 * 2. Change the internal driver state to non operational
3574 * 3. Set UTRLRSR and UTMRLRSR bits to zero
3575 * 4. Wait until outstanding commands are completed
3576 * 5. Set HCE to zero to send the UFS host controller to reset state
3581 EXPORT_SYMBOL_GPL(ufshcd_suspend);
3584 * ufshcd_resume - resume power management function
3585 * @hba: per adapter instance
3589 int ufshcd_resume(struct ufs_hba *hba)
3593 * 1. Set HCE to 1, to start the UFS host controller
3594 * initialization process
3595 * 2. Set UTRLRSR and UTMRLRSR bits to 1
3596 * 3. Change the internal driver state to operational
3597 * 4. Unblock SCSI requests from SCSI midlayer
3602 EXPORT_SYMBOL_GPL(ufshcd_resume);
3604 int ufshcd_runtime_suspend(struct ufs_hba *hba)
3610 * The device is idle with no requests in the queue,
3611 * allow background operations.
3613 return ufshcd_enable_auto_bkops(hba);
3615 EXPORT_SYMBOL(ufshcd_runtime_suspend);
3617 int ufshcd_runtime_resume(struct ufs_hba *hba)
3622 return ufshcd_disable_auto_bkops(hba);
3624 EXPORT_SYMBOL(ufshcd_runtime_resume);
3626 int ufshcd_runtime_idle(struct ufs_hba *hba)
3630 EXPORT_SYMBOL(ufshcd_runtime_idle);
3633 * ufshcd_remove - de-allocate SCSI host and host memory space
3634 * data structure memory
3635 * @hba - per adapter instance
3637 void ufshcd_remove(struct ufs_hba *hba)
3639 scsi_remove_host(hba->host);
3640 /* disable interrupts */
3641 ufshcd_disable_intr(hba, hba->intr_mask);
3642 ufshcd_hba_stop(hba);
3644 scsi_host_put(hba->host);
3646 ufshcd_hba_exit(hba);
3648 EXPORT_SYMBOL_GPL(ufshcd_remove);
3651 * ufshcd_set_dma_mask - Set dma mask based on the controller
3652 * addressing capability
3653 * @hba: per adapter instance
3655 * Returns 0 for success, non-zero for failure
3657 static int ufshcd_set_dma_mask(struct ufs_hba *hba)
3659 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) {
3660 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64)))
3663 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32));
3667 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
3668 * @dev: pointer to device handle
3669 * @hba_handle: driver private handle
3670 * Returns 0 on success, non-zero value on failure
3672 int ufshcd_alloc_host(struct device *dev, struct ufs_hba **hba_handle)
3674 struct Scsi_Host *host;
3675 struct ufs_hba *hba;
3680 "Invalid memory reference for dev is NULL\n");
3685 host = scsi_host_alloc(&ufshcd_driver_template,
3686 sizeof(struct ufs_hba));
3688 dev_err(dev, "scsi_host_alloc failed\n");
3692 hba = shost_priv(host);
3700 EXPORT_SYMBOL(ufshcd_alloc_host);
3703 * ufshcd_init - Driver initialization routine
3704 * @hba: per-adapter instance
3705 * @mmio_base: base register address
3706 * @irq: Interrupt line of device
3707 * Returns 0 on success, non-zero value on failure
3709 int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq)
3712 struct Scsi_Host *host = hba->host;
3713 struct device *dev = hba->dev;
3717 "Invalid memory reference for mmio_base is NULL\n");
3722 hba->mmio_base = mmio_base;
3725 err = ufshcd_hba_init(hba);
3729 /* Read capabilities registers */
3730 ufshcd_hba_capabilities(hba);
3732 /* Get UFS version supported by the controller */
3733 hba->ufs_version = ufshcd_get_ufs_version(hba);
3735 /* Get Interrupt bit mask per version */
3736 hba->intr_mask = ufshcd_get_intr_mask(hba);
3738 err = ufshcd_set_dma_mask(hba);
3740 dev_err(hba->dev, "set dma mask failed\n");
3744 /* Allocate memory for host memory space */
3745 err = ufshcd_memory_alloc(hba);
3747 dev_err(hba->dev, "Memory allocation failed\n");
3752 ufshcd_host_memory_configure(hba);
3754 host->can_queue = hba->nutrs;
3755 host->cmd_per_lun = hba->nutrs;
3756 host->max_id = UFSHCD_MAX_ID;
3757 host->max_lun = UFSHCD_MAX_LUNS;
3758 host->max_channel = UFSHCD_MAX_CHANNEL;
3759 host->unique_id = host->host_no;
3760 host->max_cmd_len = MAX_CDB_SIZE;
3762 /* Initailize wait queue for task management */
3763 init_waitqueue_head(&hba->tm_wq);
3764 init_waitqueue_head(&hba->tm_tag_wq);
3766 /* Initialize work queues */
3767 INIT_WORK(&hba->eh_work, ufshcd_err_handler);
3768 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler);
3770 /* Initialize UIC command mutex */
3771 mutex_init(&hba->uic_cmd_mutex);
3773 /* Initialize mutex for device management commands */
3774 mutex_init(&hba->dev_cmd.lock);
3776 /* Initialize device management tag acquire wait queue */
3777 init_waitqueue_head(&hba->dev_cmd.tag_wq);
3779 /* IRQ registration */
3780 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba);
3782 dev_err(hba->dev, "request irq failed\n");
3786 /* Enable SCSI tag mapping */
3787 err = scsi_init_shared_tag_map(host, host->can_queue);
3789 dev_err(hba->dev, "init shared queue failed\n");
3793 err = scsi_add_host(host, hba->dev);
3795 dev_err(hba->dev, "scsi_add_host failed\n");
3799 /* Host controller enable */
3800 err = ufshcd_hba_enable(hba);
3802 dev_err(hba->dev, "Host controller enable failed\n");
3803 goto out_remove_scsi_host;
3806 /* Hold auto suspend until async scan completes */
3807 pm_runtime_get_sync(dev);
3809 async_schedule(ufshcd_async_scan, hba);
3813 out_remove_scsi_host:
3814 scsi_remove_host(hba->host);
3816 scsi_host_put(host);
3817 ufshcd_hba_exit(hba);
3821 EXPORT_SYMBOL_GPL(ufshcd_init);
3823 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
3824 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
3825 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
3826 MODULE_LICENSE("GPL");
3827 MODULE_VERSION(UFSHCD_DRIVER_VERSION);