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
36 #include <linux/async.h>
41 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
45 /* UIC command timeout, unit: ms */
46 #define UIC_CMD_TIMEOUT 500
48 /* NOP OUT retries waiting for NOP IN response */
49 #define NOP_OUT_RETRIES 10
50 /* Timeout after 30 msecs if NOP OUT hangs without response */
51 #define NOP_OUT_TIMEOUT 30 /* msecs */
53 /* Query request retries */
54 #define QUERY_REQ_RETRIES 10
55 /* Query request timeout */
56 #define QUERY_REQ_TIMEOUT 30 /* msec */
58 /* Expose the flag value from utp_upiu_query.value */
59 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
61 /* Interrupt aggregation default timeout, unit: 40us */
62 #define INT_AGGR_DEF_TO 0x02
65 UFSHCD_MAX_CHANNEL
= 0,
68 UFSHCD_CMD_PER_LUN
= 32,
69 UFSHCD_CAN_QUEUE
= 32,
74 UFSHCD_STATE_OPERATIONAL
,
79 /* Interrupt configuration options */
87 * ufshcd_wait_for_register - wait for register value to change
88 * @hba - per-adapter interface
89 * @reg - mmio register offset
90 * @mask - mask to apply to read register value
91 * @val - wait condition
92 * @interval_us - polling interval in microsecs
93 * @timeout_ms - timeout in millisecs
95 * Returns -ETIMEDOUT on error, zero on success
97 static int ufshcd_wait_for_register(struct ufs_hba
*hba
, u32 reg
, u32 mask
,
98 u32 val
, unsigned long interval_us
, unsigned long timeout_ms
)
101 unsigned long timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
103 /* ignore bits that we don't intend to wait on */
106 while ((ufshcd_readl(hba
, reg
) & mask
) != val
) {
107 /* wakeup within 50us of expiry */
108 usleep_range(interval_us
, interval_us
+ 50);
110 if (time_after(jiffies
, timeout
)) {
111 if ((ufshcd_readl(hba
, reg
) & mask
) != val
)
121 * ufshcd_get_intr_mask - Get the interrupt bit mask
122 * @hba - Pointer to adapter instance
124 * Returns interrupt bit mask per version
126 static inline u32
ufshcd_get_intr_mask(struct ufs_hba
*hba
)
128 if (hba
->ufs_version
== UFSHCI_VERSION_10
)
129 return INTERRUPT_MASK_ALL_VER_10
;
131 return INTERRUPT_MASK_ALL_VER_11
;
135 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
136 * @hba - Pointer to adapter instance
138 * Returns UFSHCI version supported by the controller
140 static inline u32
ufshcd_get_ufs_version(struct ufs_hba
*hba
)
142 return ufshcd_readl(hba
, REG_UFS_VERSION
);
146 * ufshcd_is_device_present - Check if any device connected to
147 * the host controller
148 * @reg_hcs - host controller status register value
150 * Returns 1 if device present, 0 if no device detected
152 static inline int ufshcd_is_device_present(u32 reg_hcs
)
154 return (DEVICE_PRESENT
& reg_hcs
) ? 1 : 0;
158 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
159 * @lrb: pointer to local command reference block
161 * This function is used to get the OCS field from UTRD
162 * Returns the OCS field in the UTRD
164 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb
*lrbp
)
166 return lrbp
->utr_descriptor_ptr
->header
.dword_2
& MASK_OCS
;
170 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
171 * @task_req_descp: pointer to utp_task_req_desc structure
173 * This function is used to get the OCS field from UTMRD
174 * Returns the OCS field in the UTMRD
177 ufshcd_get_tmr_ocs(struct utp_task_req_desc
*task_req_descp
)
179 return task_req_descp
->header
.dword_2
& MASK_OCS
;
183 * ufshcd_get_tm_free_slot - get a free slot for task management request
184 * @hba: per adapter instance
186 * Returns maximum number of task management request slots in case of
187 * task management queue full or returns the free slot number
189 static inline int ufshcd_get_tm_free_slot(struct ufs_hba
*hba
)
191 return find_first_zero_bit(&hba
->outstanding_tasks
, hba
->nutmrs
);
195 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
196 * @hba: per adapter instance
197 * @pos: position of the bit to be cleared
199 static inline void ufshcd_utrl_clear(struct ufs_hba
*hba
, u32 pos
)
201 ufshcd_writel(hba
, ~(1 << pos
), REG_UTP_TRANSFER_REQ_LIST_CLEAR
);
205 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
206 * @reg: Register value of host controller status
208 * Returns integer, 0 on Success and positive value if failed
210 static inline int ufshcd_get_lists_status(u32 reg
)
213 * The mask 0xFF is for the following HCS register bits
223 return (((reg
) & (0xFF)) >> 1) ^ (0x07);
227 * ufshcd_get_uic_cmd_result - Get the UIC command result
228 * @hba: Pointer to adapter instance
230 * This function gets the result of UIC command completion
231 * Returns 0 on success, non zero value on error
233 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba
*hba
)
235 return ufshcd_readl(hba
, REG_UIC_COMMAND_ARG_2
) &
236 MASK_UIC_COMMAND_RESULT
;
240 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
241 * @hba: Pointer to adapter instance
243 * This function gets UIC command argument3
244 * Returns 0 on success, non zero value on error
246 static inline u32
ufshcd_get_dme_attr_val(struct ufs_hba
*hba
)
248 return ufshcd_readl(hba
, REG_UIC_COMMAND_ARG_3
);
252 * ufshcd_get_req_rsp - returns the TR response transaction type
253 * @ucd_rsp_ptr: pointer to response UPIU
256 ufshcd_get_req_rsp(struct utp_upiu_rsp
*ucd_rsp_ptr
)
258 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_0
) >> 24;
262 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
263 * @ucd_rsp_ptr: pointer to response UPIU
265 * This function gets the response status and scsi_status from response UPIU
266 * Returns the response result code.
269 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp
*ucd_rsp_ptr
)
271 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_1
) & MASK_RSP_UPIU_RESULT
;
275 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
277 * @ucd_rsp_ptr: pointer to response UPIU
279 * Return the data segment length.
281 static inline unsigned int
282 ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp
*ucd_rsp_ptr
)
284 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_2
) &
285 MASK_RSP_UPIU_DATA_SEG_LEN
;
289 * ufshcd_is_exception_event - Check if the device raised an exception event
290 * @ucd_rsp_ptr: pointer to response UPIU
292 * The function checks if the device raised an exception event indicated in
293 * the Device Information field of response UPIU.
295 * Returns true if exception is raised, false otherwise.
297 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp
*ucd_rsp_ptr
)
299 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_2
) &
300 MASK_RSP_EXCEPTION_EVENT
? true : false;
304 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
305 * @hba: per adapter instance
308 ufshcd_reset_intr_aggr(struct ufs_hba
*hba
)
310 ufshcd_writel(hba
, INT_AGGR_ENABLE
|
311 INT_AGGR_COUNTER_AND_TIMER_RESET
,
312 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
316 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
317 * @hba: per adapter instance
318 * @cnt: Interrupt aggregation counter threshold
319 * @tmout: Interrupt aggregation timeout value
322 ufshcd_config_intr_aggr(struct ufs_hba
*hba
, u8 cnt
, u8 tmout
)
324 ufshcd_writel(hba
, INT_AGGR_ENABLE
| INT_AGGR_PARAM_WRITE
|
325 INT_AGGR_COUNTER_THLD_VAL(cnt
) |
326 INT_AGGR_TIMEOUT_VAL(tmout
),
327 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
331 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
332 * When run-stop registers are set to 1, it indicates the
333 * host controller that it can process the requests
334 * @hba: per adapter instance
336 static void ufshcd_enable_run_stop_reg(struct ufs_hba
*hba
)
338 ufshcd_writel(hba
, UTP_TASK_REQ_LIST_RUN_STOP_BIT
,
339 REG_UTP_TASK_REQ_LIST_RUN_STOP
);
340 ufshcd_writel(hba
, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT
,
341 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP
);
345 * ufshcd_hba_start - Start controller initialization sequence
346 * @hba: per adapter instance
348 static inline void ufshcd_hba_start(struct ufs_hba
*hba
)
350 ufshcd_writel(hba
, CONTROLLER_ENABLE
, REG_CONTROLLER_ENABLE
);
354 * ufshcd_is_hba_active - Get controller state
355 * @hba: per adapter instance
357 * Returns zero if controller is active, 1 otherwise
359 static inline int ufshcd_is_hba_active(struct ufs_hba
*hba
)
361 return (ufshcd_readl(hba
, REG_CONTROLLER_ENABLE
) & 0x1) ? 0 : 1;
365 * ufshcd_send_command - Send SCSI or device management commands
366 * @hba: per adapter instance
367 * @task_tag: Task tag of the command
370 void ufshcd_send_command(struct ufs_hba
*hba
, unsigned int task_tag
)
372 __set_bit(task_tag
, &hba
->outstanding_reqs
);
373 ufshcd_writel(hba
, 1 << task_tag
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
377 * ufshcd_copy_sense_data - Copy sense data in case of check condition
378 * @lrb - pointer to local reference block
380 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb
*lrbp
)
383 if (lrbp
->sense_buffer
&&
384 ufshcd_get_rsp_upiu_data_seg_len(lrbp
->ucd_rsp_ptr
)) {
385 len
= be16_to_cpu(lrbp
->ucd_rsp_ptr
->sr
.sense_data_len
);
386 memcpy(lrbp
->sense_buffer
,
387 lrbp
->ucd_rsp_ptr
->sr
.sense_data
,
388 min_t(int, len
, SCSI_SENSE_BUFFERSIZE
));
393 * ufshcd_query_to_cpu() - formats the buffer to native cpu endian
394 * @response: upiu query response to convert
396 static inline void ufshcd_query_to_cpu(struct utp_upiu_query
*response
)
398 response
->length
= be16_to_cpu(response
->length
);
399 response
->value
= be32_to_cpu(response
->value
);
403 * ufshcd_query_to_be() - formats the buffer to big endian
404 * @request: upiu query request to convert
406 static inline void ufshcd_query_to_be(struct utp_upiu_query
*request
)
408 request
->length
= cpu_to_be16(request
->length
);
409 request
->value
= cpu_to_be32(request
->value
);
413 * ufshcd_copy_query_response() - Copy the Query Response and the data
415 * @hba: per adapter instance
416 * @lrb - pointer to local reference block
419 void ufshcd_copy_query_response(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
421 struct ufs_query_res
*query_res
= &hba
->dev_cmd
.query
.response
;
423 /* Get the UPIU response */
424 query_res
->response
= ufshcd_get_rsp_upiu_result(lrbp
->ucd_rsp_ptr
) >>
425 UPIU_RSP_CODE_OFFSET
;
427 memcpy(&query_res
->upiu_res
, &lrbp
->ucd_rsp_ptr
->qr
, QUERY_OSF_SIZE
);
428 ufshcd_query_to_cpu(&query_res
->upiu_res
);
431 /* Get the descriptor */
432 if (lrbp
->ucd_rsp_ptr
->qr
.opcode
== UPIU_QUERY_OPCODE_READ_DESC
) {
433 u8
*descp
= (u8
*)&lrbp
->ucd_rsp_ptr
+
434 GENERAL_UPIU_REQUEST_SIZE
;
437 /* data segment length */
438 len
= be32_to_cpu(lrbp
->ucd_rsp_ptr
->header
.dword_2
) &
439 MASK_QUERY_DATA_SEG_LEN
;
441 memcpy(hba
->dev_cmd
.query
.descriptor
, descp
,
442 min_t(u16
, len
, QUERY_DESC_MAX_SIZE
));
447 * ufshcd_hba_capabilities - Read controller capabilities
448 * @hba: per adapter instance
450 static inline void ufshcd_hba_capabilities(struct ufs_hba
*hba
)
452 hba
->capabilities
= ufshcd_readl(hba
, REG_CONTROLLER_CAPABILITIES
);
454 /* nutrs and nutmrs are 0 based values */
455 hba
->nutrs
= (hba
->capabilities
& MASK_TRANSFER_REQUESTS_SLOTS
) + 1;
457 ((hba
->capabilities
& MASK_TASK_MANAGEMENT_REQUEST_SLOTS
) >> 16) + 1;
461 * ufshcd_ready_for_uic_cmd - Check if controller is ready
462 * to accept UIC commands
463 * @hba: per adapter instance
464 * Return true on success, else false
466 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba
*hba
)
468 if (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) & UIC_COMMAND_READY
)
475 * ufshcd_get_upmcrs - Get the power mode change request status
476 * @hba: Pointer to adapter instance
478 * This function gets the UPMCRS field of HCS register
479 * Returns value of UPMCRS field
481 static inline u8
ufshcd_get_upmcrs(struct ufs_hba
*hba
)
483 return (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) >> 8) & 0x7;
487 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
488 * @hba: per adapter instance
489 * @uic_cmd: UIC command
491 * Mutex must be held.
494 ufshcd_dispatch_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
496 WARN_ON(hba
->active_uic_cmd
);
498 hba
->active_uic_cmd
= uic_cmd
;
501 ufshcd_writel(hba
, uic_cmd
->argument1
, REG_UIC_COMMAND_ARG_1
);
502 ufshcd_writel(hba
, uic_cmd
->argument2
, REG_UIC_COMMAND_ARG_2
);
503 ufshcd_writel(hba
, uic_cmd
->argument3
, REG_UIC_COMMAND_ARG_3
);
506 ufshcd_writel(hba
, uic_cmd
->command
& COMMAND_OPCODE_MASK
,
511 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
512 * @hba: per adapter instance
513 * @uic_command: UIC command
515 * Must be called with mutex held.
516 * Returns 0 only if success.
519 ufshcd_wait_for_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
524 if (wait_for_completion_timeout(&uic_cmd
->done
,
525 msecs_to_jiffies(UIC_CMD_TIMEOUT
)))
526 ret
= uic_cmd
->argument2
& MASK_UIC_COMMAND_RESULT
;
530 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
531 hba
->active_uic_cmd
= NULL
;
532 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
538 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
539 * @hba: per adapter instance
540 * @uic_cmd: UIC command
542 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
544 * Returns 0 only if success.
547 __ufshcd_send_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
552 if (!ufshcd_ready_for_uic_cmd(hba
)) {
554 "Controller not ready to accept UIC commands\n");
558 init_completion(&uic_cmd
->done
);
560 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
561 ufshcd_dispatch_uic_cmd(hba
, uic_cmd
);
562 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
564 ret
= ufshcd_wait_for_uic_cmd(hba
, uic_cmd
);
570 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
571 * @hba: per adapter instance
572 * @uic_cmd: UIC command
574 * Returns 0 only if success.
577 ufshcd_send_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
581 mutex_lock(&hba
->uic_cmd_mutex
);
582 ret
= __ufshcd_send_uic_cmd(hba
, uic_cmd
);
583 mutex_unlock(&hba
->uic_cmd_mutex
);
589 * ufshcd_map_sg - Map scatter-gather list to prdt
590 * @lrbp - pointer to local reference block
592 * Returns 0 in case of success, non-zero value in case of failure
594 static int ufshcd_map_sg(struct ufshcd_lrb
*lrbp
)
596 struct ufshcd_sg_entry
*prd_table
;
597 struct scatterlist
*sg
;
598 struct scsi_cmnd
*cmd
;
603 sg_segments
= scsi_dma_map(cmd
);
608 lrbp
->utr_descriptor_ptr
->prd_table_length
=
609 cpu_to_le16((u16
) (sg_segments
));
611 prd_table
= (struct ufshcd_sg_entry
*)lrbp
->ucd_prdt_ptr
;
613 scsi_for_each_sg(cmd
, sg
, sg_segments
, i
) {
615 cpu_to_le32(((u32
) sg_dma_len(sg
))-1);
616 prd_table
[i
].base_addr
=
617 cpu_to_le32(lower_32_bits(sg
->dma_address
));
618 prd_table
[i
].upper_addr
=
619 cpu_to_le32(upper_32_bits(sg
->dma_address
));
622 lrbp
->utr_descriptor_ptr
->prd_table_length
= 0;
629 * ufshcd_enable_intr - enable interrupts
630 * @hba: per adapter instance
631 * @intrs: interrupt bits
633 static void ufshcd_enable_intr(struct ufs_hba
*hba
, u32 intrs
)
635 u32 set
= ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
637 if (hba
->ufs_version
== UFSHCI_VERSION_10
) {
639 rw
= set
& INTERRUPT_MASK_RW_VER_10
;
640 set
= rw
| ((set
^ intrs
) & intrs
);
645 ufshcd_writel(hba
, set
, REG_INTERRUPT_ENABLE
);
649 * ufshcd_disable_intr - disable interrupts
650 * @hba: per adapter instance
651 * @intrs: interrupt bits
653 static void ufshcd_disable_intr(struct ufs_hba
*hba
, u32 intrs
)
655 u32 set
= ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
657 if (hba
->ufs_version
== UFSHCI_VERSION_10
) {
659 rw
= (set
& INTERRUPT_MASK_RW_VER_10
) &
660 ~(intrs
& INTERRUPT_MASK_RW_VER_10
);
661 set
= rw
| ((set
& intrs
) & ~INTERRUPT_MASK_RW_VER_10
);
667 ufshcd_writel(hba
, set
, REG_INTERRUPT_ENABLE
);
671 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
672 * descriptor according to request
673 * @lrbp: pointer to local reference block
674 * @upiu_flags: flags required in the header
675 * @cmd_dir: requests data direction
677 static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb
*lrbp
,
678 u32
*upiu_flags
, enum dma_data_direction cmd_dir
)
680 struct utp_transfer_req_desc
*req_desc
= lrbp
->utr_descriptor_ptr
;
684 if (cmd_dir
== DMA_FROM_DEVICE
) {
685 data_direction
= UTP_DEVICE_TO_HOST
;
686 *upiu_flags
= UPIU_CMD_FLAGS_READ
;
687 } else if (cmd_dir
== DMA_TO_DEVICE
) {
688 data_direction
= UTP_HOST_TO_DEVICE
;
689 *upiu_flags
= UPIU_CMD_FLAGS_WRITE
;
691 data_direction
= UTP_NO_DATA_TRANSFER
;
692 *upiu_flags
= UPIU_CMD_FLAGS_NONE
;
695 dword_0
= data_direction
| (lrbp
->command_type
696 << UPIU_COMMAND_TYPE_OFFSET
);
698 dword_0
|= UTP_REQ_DESC_INT_CMD
;
700 /* Transfer request descriptor header fields */
701 req_desc
->header
.dword_0
= cpu_to_le32(dword_0
);
704 * assigning invalid value for command status. Controller
705 * updates OCS on command completion, with the command
708 req_desc
->header
.dword_2
=
709 cpu_to_le32(OCS_INVALID_COMMAND_STATUS
);
713 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
715 * @lrbp - local reference block pointer
716 * @upiu_flags - flags
719 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb
*lrbp
, u32 upiu_flags
)
721 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
723 /* command descriptor fields */
724 ucd_req_ptr
->header
.dword_0
= UPIU_HEADER_DWORD(
725 UPIU_TRANSACTION_COMMAND
, upiu_flags
,
726 lrbp
->lun
, lrbp
->task_tag
);
727 ucd_req_ptr
->header
.dword_1
= UPIU_HEADER_DWORD(
728 UPIU_COMMAND_SET_TYPE_SCSI
, 0, 0, 0);
730 /* Total EHS length and Data segment length will be zero */
731 ucd_req_ptr
->header
.dword_2
= 0;
733 ucd_req_ptr
->sc
.exp_data_transfer_len
=
734 cpu_to_be32(lrbp
->cmd
->sdb
.length
);
736 memcpy(ucd_req_ptr
->sc
.cdb
, lrbp
->cmd
->cmnd
,
737 (min_t(unsigned short, lrbp
->cmd
->cmd_len
, MAX_CDB_SIZE
)));
741 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
744 * @lrbp: local reference block pointer
747 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba
*hba
,
748 struct ufshcd_lrb
*lrbp
, u32 upiu_flags
)
750 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
751 struct ufs_query
*query
= &hba
->dev_cmd
.query
;
752 u16 len
= query
->request
.upiu_req
.length
;
753 u8
*descp
= (u8
*)lrbp
->ucd_req_ptr
+ GENERAL_UPIU_REQUEST_SIZE
;
755 /* Query request header */
756 ucd_req_ptr
->header
.dword_0
= UPIU_HEADER_DWORD(
757 UPIU_TRANSACTION_QUERY_REQ
, upiu_flags
,
758 lrbp
->lun
, lrbp
->task_tag
);
759 ucd_req_ptr
->header
.dword_1
= UPIU_HEADER_DWORD(
760 0, query
->request
.query_func
, 0, 0);
762 /* Data segment length */
763 ucd_req_ptr
->header
.dword_2
= UPIU_HEADER_DWORD(
764 0, 0, len
>> 8, (u8
)len
);
766 /* Copy the Query Request buffer as is */
767 memcpy(&ucd_req_ptr
->qr
, &query
->request
.upiu_req
,
769 ufshcd_query_to_be(&ucd_req_ptr
->qr
);
771 /* Copy the Descriptor */
772 if ((len
> 0) && (query
->request
.upiu_req
.opcode
==
773 UPIU_QUERY_OPCODE_WRITE_DESC
)) {
774 memcpy(descp
, query
->descriptor
,
775 min_t(u16
, len
, QUERY_DESC_MAX_SIZE
));
779 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb
*lrbp
)
781 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
783 memset(ucd_req_ptr
, 0, sizeof(struct utp_upiu_req
));
785 /* command descriptor fields */
786 ucd_req_ptr
->header
.dword_0
=
788 UPIU_TRANSACTION_NOP_OUT
, 0, 0, lrbp
->task_tag
);
792 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
793 * @hba - per adapter instance
794 * @lrb - pointer to local reference block
796 static int ufshcd_compose_upiu(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
801 switch (lrbp
->command_type
) {
802 case UTP_CMD_TYPE_SCSI
:
803 if (likely(lrbp
->cmd
)) {
804 ufshcd_prepare_req_desc_hdr(lrbp
, &upiu_flags
,
805 lrbp
->cmd
->sc_data_direction
);
806 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp
, upiu_flags
);
811 case UTP_CMD_TYPE_DEV_MANAGE
:
812 ufshcd_prepare_req_desc_hdr(lrbp
, &upiu_flags
, DMA_NONE
);
813 if (hba
->dev_cmd
.type
== DEV_CMD_TYPE_QUERY
)
814 ufshcd_prepare_utp_query_req_upiu(
815 hba
, lrbp
, upiu_flags
);
816 else if (hba
->dev_cmd
.type
== DEV_CMD_TYPE_NOP
)
817 ufshcd_prepare_utp_nop_upiu(lrbp
);
821 case UTP_CMD_TYPE_UFS
:
822 /* For UFS native command implementation */
824 dev_err(hba
->dev
, "%s: UFS native command are not supported\n",
829 dev_err(hba
->dev
, "%s: unknown command type: 0x%x\n",
830 __func__
, lrbp
->command_type
);
832 } /* end of switch */
838 * ufshcd_queuecommand - main entry point for SCSI requests
839 * @cmd: command from SCSI Midlayer
840 * @done: call back function
842 * Returns 0 for success, non-zero in case of failure
844 static int ufshcd_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*cmd
)
846 struct ufshcd_lrb
*lrbp
;
852 hba
= shost_priv(host
);
854 tag
= cmd
->request
->tag
;
856 if (hba
->ufshcd_state
!= UFSHCD_STATE_OPERATIONAL
) {
857 err
= SCSI_MLQUEUE_HOST_BUSY
;
861 /* acquire the tag to make sure device cmds don't use it */
862 if (test_and_set_bit_lock(tag
, &hba
->lrb_in_use
)) {
864 * Dev manage command in progress, requeue the command.
865 * Requeuing the command helps in cases where the request *may*
866 * find different tag instead of waiting for dev manage command
869 err
= SCSI_MLQUEUE_HOST_BUSY
;
873 lrbp
= &hba
->lrb
[tag
];
877 lrbp
->sense_bufflen
= SCSI_SENSE_BUFFERSIZE
;
878 lrbp
->sense_buffer
= cmd
->sense_buffer
;
879 lrbp
->task_tag
= tag
;
880 lrbp
->lun
= cmd
->device
->lun
;
881 lrbp
->intr_cmd
= false;
882 lrbp
->command_type
= UTP_CMD_TYPE_SCSI
;
884 /* form UPIU before issuing the command */
885 ufshcd_compose_upiu(hba
, lrbp
);
886 err
= ufshcd_map_sg(lrbp
);
889 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
893 /* issue command to the controller */
894 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
895 ufshcd_send_command(hba
, tag
);
896 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
901 static int ufshcd_compose_dev_cmd(struct ufs_hba
*hba
,
902 struct ufshcd_lrb
*lrbp
, enum dev_cmd_type cmd_type
, int tag
)
905 lrbp
->sense_bufflen
= 0;
906 lrbp
->sense_buffer
= NULL
;
907 lrbp
->task_tag
= tag
;
908 lrbp
->lun
= 0; /* device management cmd is not specific to any LUN */
909 lrbp
->command_type
= UTP_CMD_TYPE_DEV_MANAGE
;
910 lrbp
->intr_cmd
= true; /* No interrupt aggregation */
911 hba
->dev_cmd
.type
= cmd_type
;
913 return ufshcd_compose_upiu(hba
, lrbp
);
917 ufshcd_clear_cmd(struct ufs_hba
*hba
, int tag
)
923 /* clear outstanding transaction before retry */
924 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
925 ufshcd_utrl_clear(hba
, tag
);
926 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
929 * wait for for h/w to clear corresponding bit in door-bell.
930 * max. wait is 1 sec.
932 err
= ufshcd_wait_for_register(hba
,
933 REG_UTP_TRANSFER_REQ_DOOR_BELL
,
934 mask
, ~mask
, 1000, 1000);
940 * ufshcd_dev_cmd_completion() - handles device management command responses
941 * @hba: per adapter instance
942 * @lrbp: pointer to local reference block
945 ufshcd_dev_cmd_completion(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
950 resp
= ufshcd_get_req_rsp(lrbp
->ucd_rsp_ptr
);
953 case UPIU_TRANSACTION_NOP_IN
:
954 if (hba
->dev_cmd
.type
!= DEV_CMD_TYPE_NOP
) {
956 dev_err(hba
->dev
, "%s: unexpected response %x\n",
960 case UPIU_TRANSACTION_QUERY_RSP
:
961 ufshcd_copy_query_response(hba
, lrbp
);
963 case UPIU_TRANSACTION_REJECT_UPIU
:
964 /* TODO: handle Reject UPIU Response */
966 dev_err(hba
->dev
, "%s: Reject UPIU not fully implemented\n",
971 dev_err(hba
->dev
, "%s: Invalid device management cmd response: %x\n",
979 static int ufshcd_wait_for_dev_cmd(struct ufs_hba
*hba
,
980 struct ufshcd_lrb
*lrbp
, int max_timeout
)
983 unsigned long time_left
;
986 time_left
= wait_for_completion_timeout(hba
->dev_cmd
.complete
,
987 msecs_to_jiffies(max_timeout
));
989 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
990 hba
->dev_cmd
.complete
= NULL
;
991 if (likely(time_left
)) {
992 err
= ufshcd_get_tr_ocs(lrbp
);
994 err
= ufshcd_dev_cmd_completion(hba
, lrbp
);
996 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1000 if (!ufshcd_clear_cmd(hba
, lrbp
->task_tag
))
1001 /* sucessfully cleared the command, retry if needed */
1009 * ufshcd_get_dev_cmd_tag - Get device management command tag
1010 * @hba: per-adapter instance
1011 * @tag: pointer to variable with available slot value
1013 * Get a free slot and lock it until device management command
1016 * Returns false if free slot is unavailable for locking, else
1017 * return true with tag value in @tag.
1019 static bool ufshcd_get_dev_cmd_tag(struct ufs_hba
*hba
, int *tag_out
)
1029 tmp
= ~hba
->lrb_in_use
;
1030 tag
= find_last_bit(&tmp
, hba
->nutrs
);
1031 if (tag
>= hba
->nutrs
)
1033 } while (test_and_set_bit_lock(tag
, &hba
->lrb_in_use
));
1041 static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba
*hba
, int tag
)
1043 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
1047 * ufshcd_exec_dev_cmd - API for sending device management requests
1049 * @cmd_type - specifies the type (NOP, Query...)
1050 * @timeout - time in seconds
1052 * NOTE: Since there is only one available tag for device management commands,
1053 * it is expected you hold the hba->dev_cmd.lock mutex.
1055 static int ufshcd_exec_dev_cmd(struct ufs_hba
*hba
,
1056 enum dev_cmd_type cmd_type
, int timeout
)
1058 struct ufshcd_lrb
*lrbp
;
1061 struct completion wait
;
1062 unsigned long flags
;
1065 * Get free slot, sleep if slots are unavailable.
1066 * Even though we use wait_event() which sleeps indefinitely,
1067 * the maximum wait time is bounded by SCSI request timeout.
1069 wait_event(hba
->dev_cmd
.tag_wq
, ufshcd_get_dev_cmd_tag(hba
, &tag
));
1071 init_completion(&wait
);
1072 lrbp
= &hba
->lrb
[tag
];
1074 err
= ufshcd_compose_dev_cmd(hba
, lrbp
, cmd_type
, tag
);
1078 hba
->dev_cmd
.complete
= &wait
;
1080 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1081 ufshcd_send_command(hba
, tag
);
1082 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1084 err
= ufshcd_wait_for_dev_cmd(hba
, lrbp
, timeout
);
1087 ufshcd_put_dev_cmd_tag(hba
, tag
);
1088 wake_up(&hba
->dev_cmd
.tag_wq
);
1093 * ufshcd_query_flag() - API function for sending flag query requests
1094 * hba: per-adapter instance
1095 * query_opcode: flag query to perform
1096 * idn: flag idn to access
1097 * flag_res: the flag value after the query request completes
1099 * Returns 0 for success, non-zero in case of failure
1101 static int ufshcd_query_flag(struct ufs_hba
*hba
, enum query_opcode opcode
,
1102 enum flag_idn idn
, bool *flag_res
)
1104 struct ufs_query_req
*request
;
1105 struct ufs_query_res
*response
;
1110 mutex_lock(&hba
->dev_cmd
.lock
);
1111 request
= &hba
->dev_cmd
.query
.request
;
1112 response
= &hba
->dev_cmd
.query
.response
;
1113 memset(request
, 0, sizeof(struct ufs_query_req
));
1114 memset(response
, 0, sizeof(struct ufs_query_res
));
1117 case UPIU_QUERY_OPCODE_SET_FLAG
:
1118 case UPIU_QUERY_OPCODE_CLEAR_FLAG
:
1119 case UPIU_QUERY_OPCODE_TOGGLE_FLAG
:
1120 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
1122 case UPIU_QUERY_OPCODE_READ_FLAG
:
1123 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
1125 /* No dummy reads */
1126 dev_err(hba
->dev
, "%s: Invalid argument for read request\n",
1134 "%s: Expected query flag opcode but got = %d\n",
1139 request
->upiu_req
.opcode
= opcode
;
1140 request
->upiu_req
.idn
= idn
;
1142 /* Send query request */
1143 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
,
1148 "%s: Sending flag query for idn %d failed, err = %d\n",
1149 __func__
, idn
, err
);
1154 *flag_res
= (response
->upiu_res
.value
&
1155 MASK_QUERY_UPIU_FLAG_LOC
) & 0x1;
1158 mutex_unlock(&hba
->dev_cmd
.lock
);
1163 * ufshcd_query_attr - API function for sending attribute requests
1164 * hba: per-adapter instance
1165 * opcode: attribute opcode
1166 * idn: attribute idn to access
1167 * index: index field
1168 * selector: selector field
1169 * attr_val: the attribute value after the query request completes
1171 * Returns 0 for success, non-zero in case of failure
1173 int ufshcd_query_attr(struct ufs_hba
*hba
, enum query_opcode opcode
,
1174 enum attr_idn idn
, u8 index
, u8 selector
, u32
*attr_val
)
1176 struct ufs_query_req
*request
;
1177 struct ufs_query_res
*response
;
1183 dev_err(hba
->dev
, "%s: attribute value required for opcode 0x%x\n",
1189 mutex_lock(&hba
->dev_cmd
.lock
);
1190 request
= &hba
->dev_cmd
.query
.request
;
1191 response
= &hba
->dev_cmd
.query
.response
;
1192 memset(request
, 0, sizeof(struct ufs_query_req
));
1193 memset(response
, 0, sizeof(struct ufs_query_res
));
1196 case UPIU_QUERY_OPCODE_WRITE_ATTR
:
1197 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
1198 request
->upiu_req
.value
= *attr_val
;
1200 case UPIU_QUERY_OPCODE_READ_ATTR
:
1201 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
1204 dev_err(hba
->dev
, "%s: Expected query attr opcode but got = 0x%.2x\n",
1210 request
->upiu_req
.opcode
= opcode
;
1211 request
->upiu_req
.idn
= idn
;
1212 request
->upiu_req
.index
= index
;
1213 request
->upiu_req
.selector
= selector
;
1215 /* Send query request */
1216 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
,
1220 dev_err(hba
->dev
, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1221 __func__
, opcode
, idn
, err
);
1225 *attr_val
= response
->upiu_res
.value
;
1228 mutex_unlock(&hba
->dev_cmd
.lock
);
1234 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1235 * @hba: per adapter instance
1237 * 1. Allocate DMA memory for Command Descriptor array
1238 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1239 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1240 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1242 * 4. Allocate memory for local reference block(lrb).
1244 * Returns 0 for success, non-zero in case of failure
1246 static int ufshcd_memory_alloc(struct ufs_hba
*hba
)
1248 size_t utmrdl_size
, utrdl_size
, ucdl_size
;
1250 /* Allocate memory for UTP command descriptors */
1251 ucdl_size
= (sizeof(struct utp_transfer_cmd_desc
) * hba
->nutrs
);
1252 hba
->ucdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
1254 &hba
->ucdl_dma_addr
,
1258 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1259 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1260 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1261 * be aligned to 128 bytes as well
1263 if (!hba
->ucdl_base_addr
||
1264 WARN_ON(hba
->ucdl_dma_addr
& (PAGE_SIZE
- 1))) {
1266 "Command Descriptor Memory allocation failed\n");
1271 * Allocate memory for UTP Transfer descriptors
1272 * UFSHCI requires 1024 byte alignment of UTRD
1274 utrdl_size
= (sizeof(struct utp_transfer_req_desc
) * hba
->nutrs
);
1275 hba
->utrdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
1277 &hba
->utrdl_dma_addr
,
1279 if (!hba
->utrdl_base_addr
||
1280 WARN_ON(hba
->utrdl_dma_addr
& (PAGE_SIZE
- 1))) {
1282 "Transfer Descriptor Memory allocation failed\n");
1287 * Allocate memory for UTP Task Management descriptors
1288 * UFSHCI requires 1024 byte alignment of UTMRD
1290 utmrdl_size
= sizeof(struct utp_task_req_desc
) * hba
->nutmrs
;
1291 hba
->utmrdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
1293 &hba
->utmrdl_dma_addr
,
1295 if (!hba
->utmrdl_base_addr
||
1296 WARN_ON(hba
->utmrdl_dma_addr
& (PAGE_SIZE
- 1))) {
1298 "Task Management Descriptor Memory allocation failed\n");
1302 /* Allocate memory for local reference block */
1303 hba
->lrb
= devm_kzalloc(hba
->dev
,
1304 hba
->nutrs
* sizeof(struct ufshcd_lrb
),
1307 dev_err(hba
->dev
, "LRB Memory allocation failed\n");
1316 * ufshcd_host_memory_configure - configure local reference block with
1318 * @hba: per adapter instance
1320 * Configure Host memory space
1321 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
1323 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
1325 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
1326 * into local reference block.
1328 static void ufshcd_host_memory_configure(struct ufs_hba
*hba
)
1330 struct utp_transfer_cmd_desc
*cmd_descp
;
1331 struct utp_transfer_req_desc
*utrdlp
;
1332 dma_addr_t cmd_desc_dma_addr
;
1333 dma_addr_t cmd_desc_element_addr
;
1334 u16 response_offset
;
1339 utrdlp
= hba
->utrdl_base_addr
;
1340 cmd_descp
= hba
->ucdl_base_addr
;
1343 offsetof(struct utp_transfer_cmd_desc
, response_upiu
);
1345 offsetof(struct utp_transfer_cmd_desc
, prd_table
);
1347 cmd_desc_size
= sizeof(struct utp_transfer_cmd_desc
);
1348 cmd_desc_dma_addr
= hba
->ucdl_dma_addr
;
1350 for (i
= 0; i
< hba
->nutrs
; i
++) {
1351 /* Configure UTRD with command descriptor base address */
1352 cmd_desc_element_addr
=
1353 (cmd_desc_dma_addr
+ (cmd_desc_size
* i
));
1354 utrdlp
[i
].command_desc_base_addr_lo
=
1355 cpu_to_le32(lower_32_bits(cmd_desc_element_addr
));
1356 utrdlp
[i
].command_desc_base_addr_hi
=
1357 cpu_to_le32(upper_32_bits(cmd_desc_element_addr
));
1359 /* Response upiu and prdt offset should be in double words */
1360 utrdlp
[i
].response_upiu_offset
=
1361 cpu_to_le16((response_offset
>> 2));
1362 utrdlp
[i
].prd_table_offset
=
1363 cpu_to_le16((prdt_offset
>> 2));
1364 utrdlp
[i
].response_upiu_length
=
1365 cpu_to_le16(ALIGNED_UPIU_SIZE
>> 2);
1367 hba
->lrb
[i
].utr_descriptor_ptr
= (utrdlp
+ i
);
1368 hba
->lrb
[i
].ucd_req_ptr
=
1369 (struct utp_upiu_req
*)(cmd_descp
+ i
);
1370 hba
->lrb
[i
].ucd_rsp_ptr
=
1371 (struct utp_upiu_rsp
*)cmd_descp
[i
].response_upiu
;
1372 hba
->lrb
[i
].ucd_prdt_ptr
=
1373 (struct ufshcd_sg_entry
*)cmd_descp
[i
].prd_table
;
1378 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
1379 * @hba: per adapter instance
1381 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
1382 * in order to initialize the Unipro link startup procedure.
1383 * Once the Unipro links are up, the device connected to the controller
1386 * Returns 0 on success, non-zero value on failure
1388 static int ufshcd_dme_link_startup(struct ufs_hba
*hba
)
1390 struct uic_command uic_cmd
= {0};
1393 uic_cmd
.command
= UIC_CMD_DME_LINK_STARTUP
;
1395 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1398 "dme-link-startup: error code %d\n", ret
);
1403 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
1404 * @hba: per adapter instance
1405 * @attr_sel: uic command argument1
1406 * @attr_set: attribute set type as uic command argument2
1407 * @mib_val: setting value as uic command argument3
1408 * @peer: indicate whether peer or local
1410 * Returns 0 on success, non-zero value on failure
1412 int ufshcd_dme_set_attr(struct ufs_hba
*hba
, u32 attr_sel
,
1413 u8 attr_set
, u32 mib_val
, u8 peer
)
1415 struct uic_command uic_cmd
= {0};
1416 static const char *const action
[] = {
1420 const char *set
= action
[!!peer
];
1423 uic_cmd
.command
= peer
?
1424 UIC_CMD_DME_PEER_SET
: UIC_CMD_DME_SET
;
1425 uic_cmd
.argument1
= attr_sel
;
1426 uic_cmd
.argument2
= UIC_ARG_ATTR_TYPE(attr_set
);
1427 uic_cmd
.argument3
= mib_val
;
1429 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1431 dev_err(hba
->dev
, "%s: attr-id 0x%x val 0x%x error code %d\n",
1432 set
, UIC_GET_ATTR_ID(attr_sel
), mib_val
, ret
);
1436 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr
);
1439 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
1440 * @hba: per adapter instance
1441 * @attr_sel: uic command argument1
1442 * @mib_val: the value of the attribute as returned by the UIC command
1443 * @peer: indicate whether peer or local
1445 * Returns 0 on success, non-zero value on failure
1447 int ufshcd_dme_get_attr(struct ufs_hba
*hba
, u32 attr_sel
,
1448 u32
*mib_val
, u8 peer
)
1450 struct uic_command uic_cmd
= {0};
1451 static const char *const action
[] = {
1455 const char *get
= action
[!!peer
];
1458 uic_cmd
.command
= peer
?
1459 UIC_CMD_DME_PEER_GET
: UIC_CMD_DME_GET
;
1460 uic_cmd
.argument1
= attr_sel
;
1462 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1464 dev_err(hba
->dev
, "%s: attr-id 0x%x error code %d\n",
1465 get
, UIC_GET_ATTR_ID(attr_sel
), ret
);
1470 *mib_val
= uic_cmd
.argument3
;
1474 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr
);
1477 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
1478 * using DME_SET primitives.
1479 * @hba: per adapter instance
1480 * @mode: powr mode value
1482 * Returns 0 on success, non-zero value on failure
1484 int ufshcd_uic_change_pwr_mode(struct ufs_hba
*hba
, u8 mode
)
1486 struct uic_command uic_cmd
= {0};
1487 struct completion pwr_done
;
1488 unsigned long flags
;
1492 uic_cmd
.command
= UIC_CMD_DME_SET
;
1493 uic_cmd
.argument1
= UIC_ARG_MIB(PA_PWRMODE
);
1494 uic_cmd
.argument3
= mode
;
1495 init_completion(&pwr_done
);
1497 mutex_lock(&hba
->uic_cmd_mutex
);
1499 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1500 hba
->pwr_done
= &pwr_done
;
1501 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1502 ret
= __ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1505 "pwr mode change with mode 0x%x uic error %d\n",
1510 if (!wait_for_completion_timeout(hba
->pwr_done
,
1511 msecs_to_jiffies(UIC_CMD_TIMEOUT
))) {
1513 "pwr mode change with mode 0x%x completion timeout\n",
1519 status
= ufshcd_get_upmcrs(hba
);
1520 if (status
!= PWR_LOCAL
) {
1522 "pwr mode change failed, host umpcrs:0x%x\n",
1524 ret
= (status
!= PWR_OK
) ? status
: -1;
1527 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1528 hba
->pwr_done
= NULL
;
1529 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1530 mutex_unlock(&hba
->uic_cmd_mutex
);
1535 * ufshcd_config_max_pwr_mode - Set & Change power mode with
1536 * maximum capability attribute information.
1537 * @hba: per adapter instance
1539 * Returns 0 on success, non-zero value on failure
1541 static int ufshcd_config_max_pwr_mode(struct ufs_hba
*hba
)
1543 enum {RX
= 0, TX
= 1};
1544 u32 lanes
[] = {1, 1};
1545 u32 gear
[] = {1, 1};
1546 u8 pwr
[] = {FASTAUTO_MODE
, FASTAUTO_MODE
};
1549 /* Get the connected lane count */
1550 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES
), &lanes
[RX
]);
1551 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES
), &lanes
[TX
]);
1554 * First, get the maximum gears of HS speed.
1555 * If a zero value, it means there is no HSGEAR capability.
1556 * Then, get the maximum gears of PWM speed.
1558 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_MAXRXHSGEAR
), &gear
[RX
]);
1560 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_MAXRXPWMGEAR
), &gear
[RX
]);
1561 pwr
[RX
] = SLOWAUTO_MODE
;
1564 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_MAXRXHSGEAR
), &gear
[TX
]);
1566 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_MAXRXPWMGEAR
),
1568 pwr
[TX
] = SLOWAUTO_MODE
;
1572 * Configure attributes for power mode change with below.
1573 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1574 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1577 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXGEAR
), gear
[RX
]);
1578 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_ACTIVERXDATALANES
), lanes
[RX
]);
1579 if (pwr
[RX
] == FASTAUTO_MODE
)
1580 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXTERMINATION
), TRUE
);
1582 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXGEAR
), gear
[TX
]);
1583 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_ACTIVETXDATALANES
), lanes
[TX
]);
1584 if (pwr
[TX
] == FASTAUTO_MODE
)
1585 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXTERMINATION
), TRUE
);
1587 if (pwr
[RX
] == FASTAUTO_MODE
|| pwr
[TX
] == FASTAUTO_MODE
)
1588 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_HSSERIES
), PA_HS_MODE_B
);
1590 ret
= ufshcd_uic_change_pwr_mode(hba
, pwr
[RX
] << 4 | pwr
[TX
]);
1593 "pwr_mode: power mode change failed %d\n", ret
);
1599 * ufshcd_complete_dev_init() - checks device readiness
1600 * hba: per-adapter instance
1602 * Set fDeviceInit flag and poll until device toggles it.
1604 static int ufshcd_complete_dev_init(struct ufs_hba
*hba
)
1606 int i
, retries
, err
= 0;
1609 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
1610 /* Set the fDeviceInit flag */
1611 err
= ufshcd_query_flag(hba
, UPIU_QUERY_OPCODE_SET_FLAG
,
1612 QUERY_FLAG_IDN_FDEVICEINIT
, NULL
);
1613 if (!err
|| err
== -ETIMEDOUT
)
1615 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, err
);
1619 "%s setting fDeviceInit flag failed with error %d\n",
1624 /* poll for max. 100 iterations for fDeviceInit flag to clear */
1625 for (i
= 0; i
< 100 && !err
&& flag_res
; i
++) {
1626 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
1627 err
= ufshcd_query_flag(hba
,
1628 UPIU_QUERY_OPCODE_READ_FLAG
,
1629 QUERY_FLAG_IDN_FDEVICEINIT
, &flag_res
);
1630 if (!err
|| err
== -ETIMEDOUT
)
1632 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
,
1638 "%s reading fDeviceInit flag failed with error %d\n",
1642 "%s fDeviceInit was not cleared by the device\n",
1650 * ufshcd_make_hba_operational - Make UFS controller operational
1651 * @hba: per adapter instance
1653 * To bring UFS host controller to operational state,
1654 * 1. Check if device is present
1655 * 2. Enable required interrupts
1656 * 3. Configure interrupt aggregation
1657 * 4. Program UTRL and UTMRL base addres
1658 * 5. Configure run-stop-registers
1660 * Returns 0 on success, non-zero value on failure
1662 static int ufshcd_make_hba_operational(struct ufs_hba
*hba
)
1667 /* check if device present */
1668 reg
= ufshcd_readl(hba
, REG_CONTROLLER_STATUS
);
1669 if (!ufshcd_is_device_present(reg
)) {
1670 dev_err(hba
->dev
, "cc: Device not present\n");
1675 /* Enable required interrupts */
1676 ufshcd_enable_intr(hba
, UFSHCD_ENABLE_INTRS
);
1678 /* Configure interrupt aggregation */
1679 ufshcd_config_intr_aggr(hba
, hba
->nutrs
- 1, INT_AGGR_DEF_TO
);
1681 /* Configure UTRL and UTMRL base address registers */
1682 ufshcd_writel(hba
, lower_32_bits(hba
->utrdl_dma_addr
),
1683 REG_UTP_TRANSFER_REQ_LIST_BASE_L
);
1684 ufshcd_writel(hba
, upper_32_bits(hba
->utrdl_dma_addr
),
1685 REG_UTP_TRANSFER_REQ_LIST_BASE_H
);
1686 ufshcd_writel(hba
, lower_32_bits(hba
->utmrdl_dma_addr
),
1687 REG_UTP_TASK_REQ_LIST_BASE_L
);
1688 ufshcd_writel(hba
, upper_32_bits(hba
->utmrdl_dma_addr
),
1689 REG_UTP_TASK_REQ_LIST_BASE_H
);
1692 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
1693 * DEI, HEI bits must be 0
1695 if (!(ufshcd_get_lists_status(reg
))) {
1696 ufshcd_enable_run_stop_reg(hba
);
1699 "Host controller not ready to process requests");
1704 if (hba
->ufshcd_state
== UFSHCD_STATE_RESET
)
1705 scsi_unblock_requests(hba
->host
);
1707 hba
->ufshcd_state
= UFSHCD_STATE_OPERATIONAL
;
1714 * ufshcd_hba_enable - initialize the controller
1715 * @hba: per adapter instance
1717 * The controller resets itself and controller firmware initialization
1718 * sequence kicks off. When controller is ready it will set
1719 * the Host Controller Enable bit to 1.
1721 * Returns 0 on success, non-zero value on failure
1723 static int ufshcd_hba_enable(struct ufs_hba
*hba
)
1728 * msleep of 1 and 5 used in this function might result in msleep(20),
1729 * but it was necessary to send the UFS FPGA to reset mode during
1730 * development and testing of this driver. msleep can be changed to
1731 * mdelay and retry count can be reduced based on the controller.
1733 if (!ufshcd_is_hba_active(hba
)) {
1735 /* change controller state to "reset state" */
1736 ufshcd_hba_stop(hba
);
1739 * This delay is based on the testing done with UFS host
1740 * controller FPGA. The delay can be changed based on the
1741 * host controller used.
1746 /* start controller initialization sequence */
1747 ufshcd_hba_start(hba
);
1750 * To initialize a UFS host controller HCE bit must be set to 1.
1751 * During initialization the HCE bit value changes from 1->0->1.
1752 * When the host controller completes initialization sequence
1753 * it sets the value of HCE bit to 1. The same HCE bit is read back
1754 * to check if the controller has completed initialization sequence.
1755 * So without this delay the value HCE = 1, set in the previous
1756 * instruction might be read back.
1757 * This delay can be changed based on the controller.
1761 /* wait for the host controller to complete initialization */
1763 while (ufshcd_is_hba_active(hba
)) {
1768 "Controller enable failed\n");
1777 * ufshcd_link_startup - Initialize unipro link startup
1778 * @hba: per adapter instance
1780 * Returns 0 for success, non-zero in case of failure
1782 static int ufshcd_link_startup(struct ufs_hba
*hba
)
1786 /* enable UIC related interrupts */
1787 ufshcd_enable_intr(hba
, UIC_COMMAND_COMPL
);
1789 ret
= ufshcd_dme_link_startup(hba
);
1793 ret
= ufshcd_make_hba_operational(hba
);
1797 dev_err(hba
->dev
, "link startup failed %d\n", ret
);
1802 * ufshcd_verify_dev_init() - Verify device initialization
1803 * @hba: per-adapter instance
1805 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
1806 * device Transport Protocol (UTP) layer is ready after a reset.
1807 * If the UTP layer at the device side is not initialized, it may
1808 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
1809 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
1811 static int ufshcd_verify_dev_init(struct ufs_hba
*hba
)
1816 mutex_lock(&hba
->dev_cmd
.lock
);
1817 for (retries
= NOP_OUT_RETRIES
; retries
> 0; retries
--) {
1818 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_NOP
,
1821 if (!err
|| err
== -ETIMEDOUT
)
1824 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, err
);
1826 mutex_unlock(&hba
->dev_cmd
.lock
);
1829 dev_err(hba
->dev
, "%s: NOP OUT failed %d\n", __func__
, err
);
1834 * ufshcd_do_reset - reset the host controller
1835 * @hba: per adapter instance
1837 * Returns SUCCESS/FAILED
1839 static int ufshcd_do_reset(struct ufs_hba
*hba
)
1841 struct ufshcd_lrb
*lrbp
;
1842 unsigned long flags
;
1845 /* block commands from midlayer */
1846 scsi_block_requests(hba
->host
);
1848 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1849 hba
->ufshcd_state
= UFSHCD_STATE_RESET
;
1851 /* send controller to reset state */
1852 ufshcd_hba_stop(hba
);
1853 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1855 /* abort outstanding commands */
1856 for (tag
= 0; tag
< hba
->nutrs
; tag
++) {
1857 if (test_bit(tag
, &hba
->outstanding_reqs
)) {
1858 lrbp
= &hba
->lrb
[tag
];
1860 scsi_dma_unmap(lrbp
->cmd
);
1861 lrbp
->cmd
->result
= DID_RESET
<< 16;
1862 lrbp
->cmd
->scsi_done(lrbp
->cmd
);
1864 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
1869 /* complete device management command */
1870 if (hba
->dev_cmd
.complete
)
1871 complete(hba
->dev_cmd
.complete
);
1873 /* clear outstanding request/task bit maps */
1874 hba
->outstanding_reqs
= 0;
1875 hba
->outstanding_tasks
= 0;
1877 /* Host controller enable */
1878 if (ufshcd_hba_enable(hba
)) {
1880 "Reset: Controller initialization failed\n");
1884 if (ufshcd_link_startup(hba
)) {
1886 "Reset: Link start-up failed\n");
1894 * ufshcd_slave_alloc - handle initial SCSI device configurations
1895 * @sdev: pointer to SCSI device
1899 static int ufshcd_slave_alloc(struct scsi_device
*sdev
)
1901 struct ufs_hba
*hba
;
1903 hba
= shost_priv(sdev
->host
);
1904 sdev
->tagged_supported
= 1;
1906 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
1907 sdev
->use_10_for_ms
= 1;
1908 scsi_set_tag_type(sdev
, MSG_SIMPLE_TAG
);
1911 * Inform SCSI Midlayer that the LUN queue depth is same as the
1912 * controller queue depth. If a LUN queue depth is less than the
1913 * controller queue depth and if the LUN reports
1914 * SAM_STAT_TASK_SET_FULL, the LUN queue depth will be adjusted
1915 * with scsi_adjust_queue_depth.
1917 scsi_activate_tcq(sdev
, hba
->nutrs
);
1922 * ufshcd_slave_destroy - remove SCSI device configurations
1923 * @sdev: pointer to SCSI device
1925 static void ufshcd_slave_destroy(struct scsi_device
*sdev
)
1927 struct ufs_hba
*hba
;
1929 hba
= shost_priv(sdev
->host
);
1930 scsi_deactivate_tcq(sdev
, hba
->nutrs
);
1934 * ufshcd_task_req_compl - handle task management request completion
1935 * @hba: per adapter instance
1936 * @index: index of the completed request
1938 * Returns SUCCESS/FAILED
1940 static int ufshcd_task_req_compl(struct ufs_hba
*hba
, u32 index
)
1942 struct utp_task_req_desc
*task_req_descp
;
1943 struct utp_upiu_task_rsp
*task_rsp_upiup
;
1944 unsigned long flags
;
1948 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1950 /* Clear completed tasks from outstanding_tasks */
1951 __clear_bit(index
, &hba
->outstanding_tasks
);
1953 task_req_descp
= hba
->utmrdl_base_addr
;
1954 ocs_value
= ufshcd_get_tmr_ocs(&task_req_descp
[index
]);
1956 if (ocs_value
== OCS_SUCCESS
) {
1957 task_rsp_upiup
= (struct utp_upiu_task_rsp
*)
1958 task_req_descp
[index
].task_rsp_upiu
;
1959 task_result
= be32_to_cpu(task_rsp_upiup
->header
.dword_1
);
1960 task_result
= ((task_result
& MASK_TASK_RESPONSE
) >> 8);
1962 if (task_result
!= UPIU_TASK_MANAGEMENT_FUNC_COMPL
&&
1963 task_result
!= UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED
)
1964 task_result
= FAILED
;
1966 task_result
= SUCCESS
;
1968 task_result
= FAILED
;
1970 "trc: Invalid ocs = %x\n", ocs_value
);
1972 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1977 * ufshcd_adjust_lun_qdepth - Update LUN queue depth if device responds with
1978 * SAM_STAT_TASK_SET_FULL SCSI command status.
1979 * @cmd: pointer to SCSI command
1981 static void ufshcd_adjust_lun_qdepth(struct scsi_cmnd
*cmd
)
1983 struct ufs_hba
*hba
;
1987 hba
= shost_priv(cmd
->device
->host
);
1990 * LUN queue depth can be obtained by counting outstanding commands
1993 for (i
= 0; i
< hba
->nutrs
; i
++) {
1994 if (test_bit(i
, &hba
->outstanding_reqs
)) {
1997 * Check if the outstanding command belongs
1998 * to the LUN which reported SAM_STAT_TASK_SET_FULL.
2000 if (cmd
->device
->lun
== hba
->lrb
[i
].lun
)
2006 * LUN queue depth will be total outstanding commands, except the
2007 * command for which the LUN reported SAM_STAT_TASK_SET_FULL.
2009 scsi_adjust_queue_depth(cmd
->device
, MSG_SIMPLE_TAG
, lun_qdepth
- 1);
2013 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
2014 * @lrb: pointer to local reference block of completed command
2015 * @scsi_status: SCSI command status
2017 * Returns value base on SCSI command status
2020 ufshcd_scsi_cmd_status(struct ufshcd_lrb
*lrbp
, int scsi_status
)
2024 switch (scsi_status
) {
2025 case SAM_STAT_CHECK_CONDITION
:
2026 ufshcd_copy_sense_data(lrbp
);
2028 result
|= DID_OK
<< 16 |
2029 COMMAND_COMPLETE
<< 8 |
2032 case SAM_STAT_TASK_SET_FULL
:
2034 * If a LUN reports SAM_STAT_TASK_SET_FULL, then the LUN queue
2035 * depth needs to be adjusted to the exact number of
2036 * outstanding commands the LUN can handle at any given time.
2038 ufshcd_adjust_lun_qdepth(lrbp
->cmd
);
2040 case SAM_STAT_TASK_ABORTED
:
2041 ufshcd_copy_sense_data(lrbp
);
2042 result
|= scsi_status
;
2045 result
|= DID_ERROR
<< 16;
2047 } /* end of switch */
2053 * ufshcd_transfer_rsp_status - Get overall status of the response
2054 * @hba: per adapter instance
2055 * @lrb: pointer to local reference block of completed command
2057 * Returns result of the command to notify SCSI midlayer
2060 ufshcd_transfer_rsp_status(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2066 /* overall command status of utrd */
2067 ocs
= ufshcd_get_tr_ocs(lrbp
);
2071 result
= ufshcd_get_req_rsp(lrbp
->ucd_rsp_ptr
);
2074 case UPIU_TRANSACTION_RESPONSE
:
2076 * get the response UPIU result to extract
2077 * the SCSI command status
2079 result
= ufshcd_get_rsp_upiu_result(lrbp
->ucd_rsp_ptr
);
2082 * get the result based on SCSI status response
2083 * to notify the SCSI midlayer of the command status
2085 scsi_status
= result
& MASK_SCSI_STATUS
;
2086 result
= ufshcd_scsi_cmd_status(lrbp
, scsi_status
);
2088 if (ufshcd_is_exception_event(lrbp
->ucd_rsp_ptr
))
2089 schedule_work(&hba
->eeh_work
);
2091 case UPIU_TRANSACTION_REJECT_UPIU
:
2092 /* TODO: handle Reject UPIU Response */
2093 result
= DID_ERROR
<< 16;
2095 "Reject UPIU not fully implemented\n");
2098 result
= DID_ERROR
<< 16;
2100 "Unexpected request response code = %x\n",
2106 result
|= DID_ABORT
<< 16;
2108 case OCS_INVALID_CMD_TABLE_ATTR
:
2109 case OCS_INVALID_PRDT_ATTR
:
2110 case OCS_MISMATCH_DATA_BUF_SIZE
:
2111 case OCS_MISMATCH_RESP_UPIU_SIZE
:
2112 case OCS_PEER_COMM_FAILURE
:
2113 case OCS_FATAL_ERROR
:
2115 result
|= DID_ERROR
<< 16;
2117 "OCS error from controller = %x\n", ocs
);
2119 } /* end of switch */
2125 * ufshcd_uic_cmd_compl - handle completion of uic command
2126 * @hba: per adapter instance
2127 * @intr_status: interrupt status generated by the controller
2129 static void ufshcd_uic_cmd_compl(struct ufs_hba
*hba
, u32 intr_status
)
2131 if ((intr_status
& UIC_COMMAND_COMPL
) && hba
->active_uic_cmd
) {
2132 hba
->active_uic_cmd
->argument2
|=
2133 ufshcd_get_uic_cmd_result(hba
);
2134 hba
->active_uic_cmd
->argument3
=
2135 ufshcd_get_dme_attr_val(hba
);
2136 complete(&hba
->active_uic_cmd
->done
);
2139 if ((intr_status
& UIC_POWER_MODE
) && hba
->pwr_done
)
2140 complete(hba
->pwr_done
);
2144 * ufshcd_transfer_req_compl - handle SCSI and query command completion
2145 * @hba: per adapter instance
2147 static void ufshcd_transfer_req_compl(struct ufs_hba
*hba
)
2149 struct ufshcd_lrb
*lrbp
;
2150 struct scsi_cmnd
*cmd
;
2151 unsigned long completed_reqs
;
2155 bool int_aggr_reset
= false;
2157 tr_doorbell
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
2158 completed_reqs
= tr_doorbell
^ hba
->outstanding_reqs
;
2160 for (index
= 0; index
< hba
->nutrs
; index
++) {
2161 if (test_bit(index
, &completed_reqs
)) {
2162 lrbp
= &hba
->lrb
[index
];
2165 * Don't skip resetting interrupt aggregation counters
2166 * if a regular command is present.
2168 int_aggr_reset
|= !lrbp
->intr_cmd
;
2171 result
= ufshcd_transfer_rsp_status(hba
, lrbp
);
2172 scsi_dma_unmap(cmd
);
2173 cmd
->result
= result
;
2174 /* Mark completed command as NULL in LRB */
2176 clear_bit_unlock(index
, &hba
->lrb_in_use
);
2177 /* Do not touch lrbp after scsi done */
2178 cmd
->scsi_done(cmd
);
2179 } else if (lrbp
->command_type
==
2180 UTP_CMD_TYPE_DEV_MANAGE
) {
2181 if (hba
->dev_cmd
.complete
)
2182 complete(hba
->dev_cmd
.complete
);
2187 /* clear corresponding bits of completed commands */
2188 hba
->outstanding_reqs
^= completed_reqs
;
2190 /* we might have free'd some tags above */
2191 wake_up(&hba
->dev_cmd
.tag_wq
);
2193 /* Reset interrupt aggregation counters */
2195 ufshcd_reset_intr_aggr(hba
);
2199 * ufshcd_disable_ee - disable exception event
2200 * @hba: per-adapter instance
2201 * @mask: exception event to disable
2203 * Disables exception event in the device so that the EVENT_ALERT
2206 * Returns zero on success, non-zero error value on failure.
2208 static int ufshcd_disable_ee(struct ufs_hba
*hba
, u16 mask
)
2213 if (!(hba
->ee_ctrl_mask
& mask
))
2216 val
= hba
->ee_ctrl_mask
& ~mask
;
2217 val
&= 0xFFFF; /* 2 bytes */
2218 err
= ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
2219 QUERY_ATTR_IDN_EE_CONTROL
, 0, 0, &val
);
2221 hba
->ee_ctrl_mask
&= ~mask
;
2227 * ufshcd_enable_ee - enable exception event
2228 * @hba: per-adapter instance
2229 * @mask: exception event to enable
2231 * Enable corresponding exception event in the device to allow
2232 * device to alert host in critical scenarios.
2234 * Returns zero on success, non-zero error value on failure.
2236 static int ufshcd_enable_ee(struct ufs_hba
*hba
, u16 mask
)
2241 if (hba
->ee_ctrl_mask
& mask
)
2244 val
= hba
->ee_ctrl_mask
| mask
;
2245 val
&= 0xFFFF; /* 2 bytes */
2246 err
= ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
2247 QUERY_ATTR_IDN_EE_CONTROL
, 0, 0, &val
);
2249 hba
->ee_ctrl_mask
|= mask
;
2255 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
2256 * @hba: per-adapter instance
2258 * Allow device to manage background operations on its own. Enabling
2259 * this might lead to inconsistent latencies during normal data transfers
2260 * as the device is allowed to manage its own way of handling background
2263 * Returns zero on success, non-zero on failure.
2265 static int ufshcd_enable_auto_bkops(struct ufs_hba
*hba
)
2269 if (hba
->auto_bkops_enabled
)
2272 err
= ufshcd_query_flag(hba
, UPIU_QUERY_OPCODE_SET_FLAG
,
2273 QUERY_FLAG_IDN_BKOPS_EN
, NULL
);
2275 dev_err(hba
->dev
, "%s: failed to enable bkops %d\n",
2280 hba
->auto_bkops_enabled
= true;
2282 /* No need of URGENT_BKOPS exception from the device */
2283 err
= ufshcd_disable_ee(hba
, MASK_EE_URGENT_BKOPS
);
2285 dev_err(hba
->dev
, "%s: failed to disable exception event %d\n",
2292 * ufshcd_disable_auto_bkops - block device in doing background operations
2293 * @hba: per-adapter instance
2295 * Disabling background operations improves command response latency but
2296 * has drawback of device moving into critical state where the device is
2297 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
2298 * host is idle so that BKOPS are managed effectively without any negative
2301 * Returns zero on success, non-zero on failure.
2303 static int ufshcd_disable_auto_bkops(struct ufs_hba
*hba
)
2307 if (!hba
->auto_bkops_enabled
)
2311 * If host assisted BKOPs is to be enabled, make sure
2312 * urgent bkops exception is allowed.
2314 err
= ufshcd_enable_ee(hba
, MASK_EE_URGENT_BKOPS
);
2316 dev_err(hba
->dev
, "%s: failed to enable exception event %d\n",
2321 err
= ufshcd_query_flag(hba
, UPIU_QUERY_OPCODE_CLEAR_FLAG
,
2322 QUERY_FLAG_IDN_BKOPS_EN
, NULL
);
2324 dev_err(hba
->dev
, "%s: failed to disable bkops %d\n",
2326 ufshcd_disable_ee(hba
, MASK_EE_URGENT_BKOPS
);
2330 hba
->auto_bkops_enabled
= false;
2336 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
2337 * @hba: per adapter instance
2339 * After a device reset the device may toggle the BKOPS_EN flag
2340 * to default value. The s/w tracking variables should be updated
2341 * as well. Do this by forcing enable of auto bkops.
2343 static void ufshcd_force_reset_auto_bkops(struct ufs_hba
*hba
)
2345 hba
->auto_bkops_enabled
= false;
2346 hba
->ee_ctrl_mask
|= MASK_EE_URGENT_BKOPS
;
2347 ufshcd_enable_auto_bkops(hba
);
2350 static inline int ufshcd_get_bkops_status(struct ufs_hba
*hba
, u32
*status
)
2352 return ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_READ_ATTR
,
2353 QUERY_ATTR_IDN_BKOPS_STATUS
, 0, 0, status
);
2357 * ufshcd_urgent_bkops - handle urgent bkops exception event
2358 * @hba: per-adapter instance
2360 * Enable fBackgroundOpsEn flag in the device to permit background
2363 static int ufshcd_urgent_bkops(struct ufs_hba
*hba
)
2368 err
= ufshcd_get_bkops_status(hba
, &status
);
2370 dev_err(hba
->dev
, "%s: failed to get BKOPS status %d\n",
2375 status
= status
& 0xF;
2377 /* handle only if status indicates performance impact or critical */
2378 if (status
>= BKOPS_STATUS_PERF_IMPACT
)
2379 err
= ufshcd_enable_auto_bkops(hba
);
2384 static inline int ufshcd_get_ee_status(struct ufs_hba
*hba
, u32
*status
)
2386 return ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_READ_ATTR
,
2387 QUERY_ATTR_IDN_EE_STATUS
, 0, 0, status
);
2391 * ufshcd_exception_event_handler - handle exceptions raised by device
2392 * @work: pointer to work data
2394 * Read bExceptionEventStatus attribute from the device and handle the
2395 * exception event accordingly.
2397 static void ufshcd_exception_event_handler(struct work_struct
*work
)
2399 struct ufs_hba
*hba
;
2402 hba
= container_of(work
, struct ufs_hba
, eeh_work
);
2404 pm_runtime_get_sync(hba
->dev
);
2405 err
= ufshcd_get_ee_status(hba
, &status
);
2407 dev_err(hba
->dev
, "%s: failed to get exception status %d\n",
2412 status
&= hba
->ee_ctrl_mask
;
2413 if (status
& MASK_EE_URGENT_BKOPS
) {
2414 err
= ufshcd_urgent_bkops(hba
);
2416 dev_err(hba
->dev
, "%s: failed to handle urgent bkops %d\n",
2420 pm_runtime_put_sync(hba
->dev
);
2425 * ufshcd_fatal_err_handler - handle fatal errors
2426 * @hba: per adapter instance
2428 static void ufshcd_fatal_err_handler(struct work_struct
*work
)
2430 struct ufs_hba
*hba
;
2431 hba
= container_of(work
, struct ufs_hba
, feh_workq
);
2433 pm_runtime_get_sync(hba
->dev
);
2434 /* check if reset is already in progress */
2435 if (hba
->ufshcd_state
!= UFSHCD_STATE_RESET
)
2436 ufshcd_do_reset(hba
);
2437 pm_runtime_put_sync(hba
->dev
);
2441 * ufshcd_err_handler - Check for fatal errors
2442 * @work: pointer to a work queue structure
2444 static void ufshcd_err_handler(struct ufs_hba
*hba
)
2448 if (hba
->errors
& INT_FATAL_ERRORS
)
2451 if (hba
->errors
& UIC_ERROR
) {
2452 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_DATA_LINK_LAYER
);
2453 if (reg
& UIC_DATA_LINK_LAYER_ERROR_PA_INIT
)
2458 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
2459 schedule_work(&hba
->feh_workq
);
2463 * ufshcd_tmc_handler - handle task management function completion
2464 * @hba: per adapter instance
2466 static void ufshcd_tmc_handler(struct ufs_hba
*hba
)
2470 tm_doorbell
= ufshcd_readl(hba
, REG_UTP_TASK_REQ_DOOR_BELL
);
2471 hba
->tm_condition
= tm_doorbell
^ hba
->outstanding_tasks
;
2472 wake_up_interruptible(&hba
->ufshcd_tm_wait_queue
);
2476 * ufshcd_sl_intr - Interrupt service routine
2477 * @hba: per adapter instance
2478 * @intr_status: contains interrupts generated by the controller
2480 static void ufshcd_sl_intr(struct ufs_hba
*hba
, u32 intr_status
)
2482 hba
->errors
= UFSHCD_ERROR_MASK
& intr_status
;
2484 ufshcd_err_handler(hba
);
2486 if (intr_status
& UFSHCD_UIC_MASK
)
2487 ufshcd_uic_cmd_compl(hba
, intr_status
);
2489 if (intr_status
& UTP_TASK_REQ_COMPL
)
2490 ufshcd_tmc_handler(hba
);
2492 if (intr_status
& UTP_TRANSFER_REQ_COMPL
)
2493 ufshcd_transfer_req_compl(hba
);
2497 * ufshcd_intr - Main interrupt service routine
2499 * @__hba: pointer to adapter instance
2501 * Returns IRQ_HANDLED - If interrupt is valid
2502 * IRQ_NONE - If invalid interrupt
2504 static irqreturn_t
ufshcd_intr(int irq
, void *__hba
)
2507 irqreturn_t retval
= IRQ_NONE
;
2508 struct ufs_hba
*hba
= __hba
;
2510 spin_lock(hba
->host
->host_lock
);
2511 intr_status
= ufshcd_readl(hba
, REG_INTERRUPT_STATUS
);
2514 ufshcd_writel(hba
, intr_status
, REG_INTERRUPT_STATUS
);
2515 ufshcd_sl_intr(hba
, intr_status
);
2516 retval
= IRQ_HANDLED
;
2518 spin_unlock(hba
->host
->host_lock
);
2523 * ufshcd_issue_tm_cmd - issues task management commands to controller
2524 * @hba: per adapter instance
2525 * @lrbp: pointer to local reference block
2527 * Returns SUCCESS/FAILED
2530 ufshcd_issue_tm_cmd(struct ufs_hba
*hba
,
2531 struct ufshcd_lrb
*lrbp
,
2534 struct utp_task_req_desc
*task_req_descp
;
2535 struct utp_upiu_task_req
*task_req_upiup
;
2536 struct Scsi_Host
*host
;
2537 unsigned long flags
;
2543 spin_lock_irqsave(host
->host_lock
, flags
);
2545 /* If task management queue is full */
2546 free_slot
= ufshcd_get_tm_free_slot(hba
);
2547 if (free_slot
>= hba
->nutmrs
) {
2548 spin_unlock_irqrestore(host
->host_lock
, flags
);
2549 dev_err(hba
->dev
, "Task management queue full\n");
2554 task_req_descp
= hba
->utmrdl_base_addr
;
2555 task_req_descp
+= free_slot
;
2557 /* Configure task request descriptor */
2558 task_req_descp
->header
.dword_0
= cpu_to_le32(UTP_REQ_DESC_INT_CMD
);
2559 task_req_descp
->header
.dword_2
=
2560 cpu_to_le32(OCS_INVALID_COMMAND_STATUS
);
2562 /* Configure task request UPIU */
2564 (struct utp_upiu_task_req
*) task_req_descp
->task_req_upiu
;
2565 task_req_upiup
->header
.dword_0
=
2566 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ
, 0,
2567 lrbp
->lun
, lrbp
->task_tag
);
2568 task_req_upiup
->header
.dword_1
=
2569 UPIU_HEADER_DWORD(0, tm_function
, 0, 0);
2571 task_req_upiup
->input_param1
= lrbp
->lun
;
2572 task_req_upiup
->input_param1
=
2573 cpu_to_be32(task_req_upiup
->input_param1
);
2574 task_req_upiup
->input_param2
= lrbp
->task_tag
;
2575 task_req_upiup
->input_param2
=
2576 cpu_to_be32(task_req_upiup
->input_param2
);
2578 /* send command to the controller */
2579 __set_bit(free_slot
, &hba
->outstanding_tasks
);
2580 ufshcd_writel(hba
, 1 << free_slot
, REG_UTP_TASK_REQ_DOOR_BELL
);
2582 spin_unlock_irqrestore(host
->host_lock
, flags
);
2584 /* wait until the task management command is completed */
2586 wait_event_interruptible_timeout(hba
->ufshcd_tm_wait_queue
,
2587 (test_bit(free_slot
,
2588 &hba
->tm_condition
) != 0),
2592 "Task management command timed-out\n");
2596 clear_bit(free_slot
, &hba
->tm_condition
);
2597 err
= ufshcd_task_req_compl(hba
, free_slot
);
2603 * ufshcd_device_reset - reset device and abort all the pending commands
2604 * @cmd: SCSI command pointer
2606 * Returns SUCCESS/FAILED
2608 static int ufshcd_device_reset(struct scsi_cmnd
*cmd
)
2610 struct Scsi_Host
*host
;
2611 struct ufs_hba
*hba
;
2616 host
= cmd
->device
->host
;
2617 hba
= shost_priv(host
);
2618 tag
= cmd
->request
->tag
;
2620 err
= ufshcd_issue_tm_cmd(hba
, &hba
->lrb
[tag
], UFS_LOGICAL_RESET
);
2624 for (pos
= 0; pos
< hba
->nutrs
; pos
++) {
2625 if (test_bit(pos
, &hba
->outstanding_reqs
) &&
2626 (hba
->lrb
[tag
].lun
== hba
->lrb
[pos
].lun
)) {
2628 /* clear the respective UTRLCLR register bit */
2629 ufshcd_utrl_clear(hba
, pos
);
2631 clear_bit(pos
, &hba
->outstanding_reqs
);
2633 if (hba
->lrb
[pos
].cmd
) {
2634 scsi_dma_unmap(hba
->lrb
[pos
].cmd
);
2635 hba
->lrb
[pos
].cmd
->result
=
2637 hba
->lrb
[pos
].cmd
->scsi_done(cmd
);
2638 hba
->lrb
[pos
].cmd
= NULL
;
2639 clear_bit_unlock(pos
, &hba
->lrb_in_use
);
2640 wake_up(&hba
->dev_cmd
.tag_wq
);
2649 * ufshcd_host_reset - Main reset function registered with scsi layer
2650 * @cmd: SCSI command pointer
2652 * Returns SUCCESS/FAILED
2654 static int ufshcd_host_reset(struct scsi_cmnd
*cmd
)
2656 struct ufs_hba
*hba
;
2658 hba
= shost_priv(cmd
->device
->host
);
2660 if (hba
->ufshcd_state
== UFSHCD_STATE_RESET
)
2663 return ufshcd_do_reset(hba
);
2667 * ufshcd_abort - abort a specific command
2668 * @cmd: SCSI command pointer
2670 * Returns SUCCESS/FAILED
2672 static int ufshcd_abort(struct scsi_cmnd
*cmd
)
2674 struct Scsi_Host
*host
;
2675 struct ufs_hba
*hba
;
2676 unsigned long flags
;
2680 host
= cmd
->device
->host
;
2681 hba
= shost_priv(host
);
2682 tag
= cmd
->request
->tag
;
2684 spin_lock_irqsave(host
->host_lock
, flags
);
2686 /* check if command is still pending */
2687 if (!(test_bit(tag
, &hba
->outstanding_reqs
))) {
2689 spin_unlock_irqrestore(host
->host_lock
, flags
);
2692 spin_unlock_irqrestore(host
->host_lock
, flags
);
2694 err
= ufshcd_issue_tm_cmd(hba
, &hba
->lrb
[tag
], UFS_ABORT_TASK
);
2698 scsi_dma_unmap(cmd
);
2700 spin_lock_irqsave(host
->host_lock
, flags
);
2702 /* clear the respective UTRLCLR register bit */
2703 ufshcd_utrl_clear(hba
, tag
);
2705 __clear_bit(tag
, &hba
->outstanding_reqs
);
2706 hba
->lrb
[tag
].cmd
= NULL
;
2707 spin_unlock_irqrestore(host
->host_lock
, flags
);
2709 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
2710 wake_up(&hba
->dev_cmd
.tag_wq
);
2716 * ufshcd_async_scan - asynchronous execution for link startup
2717 * @data: data pointer to pass to this function
2718 * @cookie: cookie data
2720 static void ufshcd_async_scan(void *data
, async_cookie_t cookie
)
2722 struct ufs_hba
*hba
= (struct ufs_hba
*)data
;
2725 ret
= ufshcd_link_startup(hba
);
2729 ufshcd_config_max_pwr_mode(hba
);
2731 ret
= ufshcd_verify_dev_init(hba
);
2735 ret
= ufshcd_complete_dev_init(hba
);
2739 ufshcd_force_reset_auto_bkops(hba
);
2740 scsi_scan_host(hba
->host
);
2741 pm_runtime_put_sync(hba
->dev
);
2746 static struct scsi_host_template ufshcd_driver_template
= {
2747 .module
= THIS_MODULE
,
2749 .proc_name
= UFSHCD
,
2750 .queuecommand
= ufshcd_queuecommand
,
2751 .slave_alloc
= ufshcd_slave_alloc
,
2752 .slave_destroy
= ufshcd_slave_destroy
,
2753 .eh_abort_handler
= ufshcd_abort
,
2754 .eh_device_reset_handler
= ufshcd_device_reset
,
2755 .eh_host_reset_handler
= ufshcd_host_reset
,
2757 .sg_tablesize
= SG_ALL
,
2758 .cmd_per_lun
= UFSHCD_CMD_PER_LUN
,
2759 .can_queue
= UFSHCD_CAN_QUEUE
,
2763 * ufshcd_suspend - suspend power management function
2764 * @hba: per adapter instance
2765 * @state: power state
2769 int ufshcd_suspend(struct ufs_hba
*hba
, pm_message_t state
)
2773 * 1. Block SCSI requests from SCSI midlayer
2774 * 2. Change the internal driver state to non operational
2775 * 3. Set UTRLRSR and UTMRLRSR bits to zero
2776 * 4. Wait until outstanding commands are completed
2777 * 5. Set HCE to zero to send the UFS host controller to reset state
2782 EXPORT_SYMBOL_GPL(ufshcd_suspend
);
2785 * ufshcd_resume - resume power management function
2786 * @hba: per adapter instance
2790 int ufshcd_resume(struct ufs_hba
*hba
)
2794 * 1. Set HCE to 1, to start the UFS host controller
2795 * initialization process
2796 * 2. Set UTRLRSR and UTMRLRSR bits to 1
2797 * 3. Change the internal driver state to operational
2798 * 4. Unblock SCSI requests from SCSI midlayer
2803 EXPORT_SYMBOL_GPL(ufshcd_resume
);
2805 int ufshcd_runtime_suspend(struct ufs_hba
*hba
)
2811 * The device is idle with no requests in the queue,
2812 * allow background operations.
2814 return ufshcd_enable_auto_bkops(hba
);
2816 EXPORT_SYMBOL(ufshcd_runtime_suspend
);
2818 int ufshcd_runtime_resume(struct ufs_hba
*hba
)
2823 return ufshcd_disable_auto_bkops(hba
);
2825 EXPORT_SYMBOL(ufshcd_runtime_resume
);
2827 int ufshcd_runtime_idle(struct ufs_hba
*hba
)
2831 EXPORT_SYMBOL(ufshcd_runtime_idle
);
2834 * ufshcd_remove - de-allocate SCSI host and host memory space
2835 * data structure memory
2836 * @hba - per adapter instance
2838 void ufshcd_remove(struct ufs_hba
*hba
)
2840 scsi_remove_host(hba
->host
);
2841 /* disable interrupts */
2842 ufshcd_disable_intr(hba
, hba
->intr_mask
);
2843 ufshcd_hba_stop(hba
);
2845 scsi_host_put(hba
->host
);
2847 EXPORT_SYMBOL_GPL(ufshcd_remove
);
2850 * ufshcd_init - Driver initialization routine
2851 * @dev: pointer to device handle
2852 * @hba_handle: driver private handle
2853 * @mmio_base: base register address
2854 * @irq: Interrupt line of device
2855 * Returns 0 on success, non-zero value on failure
2857 int ufshcd_init(struct device
*dev
, struct ufs_hba
**hba_handle
,
2858 void __iomem
*mmio_base
, unsigned int irq
)
2860 struct Scsi_Host
*host
;
2861 struct ufs_hba
*hba
;
2866 "Invalid memory reference for dev is NULL\n");
2873 "Invalid memory reference for mmio_base is NULL\n");
2878 host
= scsi_host_alloc(&ufshcd_driver_template
,
2879 sizeof(struct ufs_hba
));
2881 dev_err(dev
, "scsi_host_alloc failed\n");
2885 hba
= shost_priv(host
);
2888 hba
->mmio_base
= mmio_base
;
2891 /* Read capabilities registers */
2892 ufshcd_hba_capabilities(hba
);
2894 /* Get UFS version supported by the controller */
2895 hba
->ufs_version
= ufshcd_get_ufs_version(hba
);
2897 /* Get Interrupt bit mask per version */
2898 hba
->intr_mask
= ufshcd_get_intr_mask(hba
);
2900 /* Allocate memory for host memory space */
2901 err
= ufshcd_memory_alloc(hba
);
2903 dev_err(hba
->dev
, "Memory allocation failed\n");
2908 ufshcd_host_memory_configure(hba
);
2910 host
->can_queue
= hba
->nutrs
;
2911 host
->cmd_per_lun
= hba
->nutrs
;
2912 host
->max_id
= UFSHCD_MAX_ID
;
2913 host
->max_lun
= UFSHCD_MAX_LUNS
;
2914 host
->max_channel
= UFSHCD_MAX_CHANNEL
;
2915 host
->unique_id
= host
->host_no
;
2916 host
->max_cmd_len
= MAX_CDB_SIZE
;
2918 /* Initailize wait queue for task management */
2919 init_waitqueue_head(&hba
->ufshcd_tm_wait_queue
);
2921 /* Initialize work queues */
2922 INIT_WORK(&hba
->feh_workq
, ufshcd_fatal_err_handler
);
2923 INIT_WORK(&hba
->eeh_work
, ufshcd_exception_event_handler
);
2925 /* Initialize UIC command mutex */
2926 mutex_init(&hba
->uic_cmd_mutex
);
2928 /* Initialize mutex for device management commands */
2929 mutex_init(&hba
->dev_cmd
.lock
);
2931 /* Initialize device management tag acquire wait queue */
2932 init_waitqueue_head(&hba
->dev_cmd
.tag_wq
);
2934 /* IRQ registration */
2935 err
= devm_request_irq(dev
, irq
, ufshcd_intr
, IRQF_SHARED
, UFSHCD
, hba
);
2937 dev_err(hba
->dev
, "request irq failed\n");
2941 /* Enable SCSI tag mapping */
2942 err
= scsi_init_shared_tag_map(host
, host
->can_queue
);
2944 dev_err(hba
->dev
, "init shared queue failed\n");
2948 err
= scsi_add_host(host
, hba
->dev
);
2950 dev_err(hba
->dev
, "scsi_add_host failed\n");
2954 /* Host controller enable */
2955 err
= ufshcd_hba_enable(hba
);
2957 dev_err(hba
->dev
, "Host controller enable failed\n");
2958 goto out_remove_scsi_host
;
2963 /* Hold auto suspend until async scan completes */
2964 pm_runtime_get_sync(dev
);
2966 async_schedule(ufshcd_async_scan
, hba
);
2970 out_remove_scsi_host
:
2971 scsi_remove_host(hba
->host
);
2973 scsi_host_put(host
);
2977 EXPORT_SYMBOL_GPL(ufshcd_init
);
2979 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
2980 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
2981 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
2982 MODULE_LICENSE("GPL");
2983 MODULE_VERSION(UFSHCD_DRIVER_VERSION
);