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 /* Task management command timeout */
59 #define TM_CMD_TIMEOUT 100 /* msecs */
61 /* Expose the flag value from utp_upiu_query.value */
62 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
64 /* Interrupt aggregation default timeout, unit: 40us */
65 #define INT_AGGR_DEF_TO 0x02
68 UFSHCD_MAX_CHANNEL
= 0,
71 UFSHCD_CMD_PER_LUN
= 32,
72 UFSHCD_CAN_QUEUE
= 32,
79 UFSHCD_STATE_OPERATIONAL
,
82 /* UFSHCD error handling flags */
84 UFSHCD_EH_IN_PROGRESS
= (1 << 0),
87 /* UFSHCD UIC layer error flags */
89 UFSHCD_UIC_DL_PA_INIT_ERROR
= (1 << 0), /* Data link layer error */
90 UFSHCD_UIC_NL_ERROR
= (1 << 1), /* Network layer error */
91 UFSHCD_UIC_TL_ERROR
= (1 << 2), /* Transport Layer error */
92 UFSHCD_UIC_DME_ERROR
= (1 << 3), /* DME error */
95 /* Interrupt configuration options */
102 #define ufshcd_set_eh_in_progress(h) \
103 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
104 #define ufshcd_eh_in_progress(h) \
105 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
106 #define ufshcd_clear_eh_in_progress(h) \
107 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
109 static void ufshcd_tmc_handler(struct ufs_hba
*hba
);
110 static void ufshcd_async_scan(void *data
, async_cookie_t cookie
);
111 static int ufshcd_reset_and_restore(struct ufs_hba
*hba
);
112 static int ufshcd_clear_tm_cmd(struct ufs_hba
*hba
, int tag
);
113 static int ufshcd_read_sdev_qdepth(struct ufs_hba
*hba
,
114 struct scsi_device
*sdev
);
117 * ufshcd_wait_for_register - wait for register value to change
118 * @hba - per-adapter interface
119 * @reg - mmio register offset
120 * @mask - mask to apply to read register value
121 * @val - wait condition
122 * @interval_us - polling interval in microsecs
123 * @timeout_ms - timeout in millisecs
125 * Returns -ETIMEDOUT on error, zero on success
127 static int ufshcd_wait_for_register(struct ufs_hba
*hba
, u32 reg
, u32 mask
,
128 u32 val
, unsigned long interval_us
, unsigned long timeout_ms
)
131 unsigned long timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
133 /* ignore bits that we don't intend to wait on */
136 while ((ufshcd_readl(hba
, reg
) & mask
) != val
) {
137 /* wakeup within 50us of expiry */
138 usleep_range(interval_us
, interval_us
+ 50);
140 if (time_after(jiffies
, timeout
)) {
141 if ((ufshcd_readl(hba
, reg
) & mask
) != val
)
151 * ufshcd_get_intr_mask - Get the interrupt bit mask
152 * @hba - Pointer to adapter instance
154 * Returns interrupt bit mask per version
156 static inline u32
ufshcd_get_intr_mask(struct ufs_hba
*hba
)
158 if (hba
->ufs_version
== UFSHCI_VERSION_10
)
159 return INTERRUPT_MASK_ALL_VER_10
;
161 return INTERRUPT_MASK_ALL_VER_11
;
165 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
166 * @hba - Pointer to adapter instance
168 * Returns UFSHCI version supported by the controller
170 static inline u32
ufshcd_get_ufs_version(struct ufs_hba
*hba
)
172 return ufshcd_readl(hba
, REG_UFS_VERSION
);
176 * ufshcd_is_device_present - Check if any device connected to
177 * the host controller
178 * @reg_hcs - host controller status register value
180 * Returns 1 if device present, 0 if no device detected
182 static inline int ufshcd_is_device_present(u32 reg_hcs
)
184 return (DEVICE_PRESENT
& reg_hcs
) ? 1 : 0;
188 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
189 * @lrb: pointer to local command reference block
191 * This function is used to get the OCS field from UTRD
192 * Returns the OCS field in the UTRD
194 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb
*lrbp
)
196 return le32_to_cpu(lrbp
->utr_descriptor_ptr
->header
.dword_2
) & MASK_OCS
;
200 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
201 * @task_req_descp: pointer to utp_task_req_desc structure
203 * This function is used to get the OCS field from UTMRD
204 * Returns the OCS field in the UTMRD
207 ufshcd_get_tmr_ocs(struct utp_task_req_desc
*task_req_descp
)
209 return le32_to_cpu(task_req_descp
->header
.dword_2
) & MASK_OCS
;
213 * ufshcd_get_tm_free_slot - get a free slot for task management request
214 * @hba: per adapter instance
215 * @free_slot: pointer to variable with available slot value
217 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
218 * Returns 0 if free slot is not available, else return 1 with tag value
221 static bool ufshcd_get_tm_free_slot(struct ufs_hba
*hba
, int *free_slot
)
230 tag
= find_first_zero_bit(&hba
->tm_slots_in_use
, hba
->nutmrs
);
231 if (tag
>= hba
->nutmrs
)
233 } while (test_and_set_bit_lock(tag
, &hba
->tm_slots_in_use
));
241 static inline void ufshcd_put_tm_slot(struct ufs_hba
*hba
, int slot
)
243 clear_bit_unlock(slot
, &hba
->tm_slots_in_use
);
247 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
248 * @hba: per adapter instance
249 * @pos: position of the bit to be cleared
251 static inline void ufshcd_utrl_clear(struct ufs_hba
*hba
, u32 pos
)
253 ufshcd_writel(hba
, ~(1 << pos
), REG_UTP_TRANSFER_REQ_LIST_CLEAR
);
257 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
258 * @reg: Register value of host controller status
260 * Returns integer, 0 on Success and positive value if failed
262 static inline int ufshcd_get_lists_status(u32 reg
)
265 * The mask 0xFF is for the following HCS register bits
275 return (((reg
) & (0xFF)) >> 1) ^ (0x07);
279 * ufshcd_get_uic_cmd_result - Get the UIC command result
280 * @hba: Pointer to adapter instance
282 * This function gets the result of UIC command completion
283 * Returns 0 on success, non zero value on error
285 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba
*hba
)
287 return ufshcd_readl(hba
, REG_UIC_COMMAND_ARG_2
) &
288 MASK_UIC_COMMAND_RESULT
;
292 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
293 * @hba: Pointer to adapter instance
295 * This function gets UIC command argument3
296 * Returns 0 on success, non zero value on error
298 static inline u32
ufshcd_get_dme_attr_val(struct ufs_hba
*hba
)
300 return ufshcd_readl(hba
, REG_UIC_COMMAND_ARG_3
);
304 * ufshcd_get_req_rsp - returns the TR response transaction type
305 * @ucd_rsp_ptr: pointer to response UPIU
308 ufshcd_get_req_rsp(struct utp_upiu_rsp
*ucd_rsp_ptr
)
310 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_0
) >> 24;
314 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
315 * @ucd_rsp_ptr: pointer to response UPIU
317 * This function gets the response status and scsi_status from response UPIU
318 * Returns the response result code.
321 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp
*ucd_rsp_ptr
)
323 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_1
) & MASK_RSP_UPIU_RESULT
;
327 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
329 * @ucd_rsp_ptr: pointer to response UPIU
331 * Return the data segment length.
333 static inline unsigned int
334 ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp
*ucd_rsp_ptr
)
336 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_2
) &
337 MASK_RSP_UPIU_DATA_SEG_LEN
;
341 * ufshcd_is_exception_event - Check if the device raised an exception event
342 * @ucd_rsp_ptr: pointer to response UPIU
344 * The function checks if the device raised an exception event indicated in
345 * the Device Information field of response UPIU.
347 * Returns true if exception is raised, false otherwise.
349 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp
*ucd_rsp_ptr
)
351 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_2
) &
352 MASK_RSP_EXCEPTION_EVENT
? true : false;
356 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
357 * @hba: per adapter instance
360 ufshcd_reset_intr_aggr(struct ufs_hba
*hba
)
362 ufshcd_writel(hba
, INT_AGGR_ENABLE
|
363 INT_AGGR_COUNTER_AND_TIMER_RESET
,
364 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
368 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
369 * @hba: per adapter instance
370 * @cnt: Interrupt aggregation counter threshold
371 * @tmout: Interrupt aggregation timeout value
374 ufshcd_config_intr_aggr(struct ufs_hba
*hba
, u8 cnt
, u8 tmout
)
376 ufshcd_writel(hba
, INT_AGGR_ENABLE
| INT_AGGR_PARAM_WRITE
|
377 INT_AGGR_COUNTER_THLD_VAL(cnt
) |
378 INT_AGGR_TIMEOUT_VAL(tmout
),
379 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
383 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
384 * When run-stop registers are set to 1, it indicates the
385 * host controller that it can process the requests
386 * @hba: per adapter instance
388 static void ufshcd_enable_run_stop_reg(struct ufs_hba
*hba
)
390 ufshcd_writel(hba
, UTP_TASK_REQ_LIST_RUN_STOP_BIT
,
391 REG_UTP_TASK_REQ_LIST_RUN_STOP
);
392 ufshcd_writel(hba
, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT
,
393 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP
);
397 * ufshcd_hba_start - Start controller initialization sequence
398 * @hba: per adapter instance
400 static inline void ufshcd_hba_start(struct ufs_hba
*hba
)
402 ufshcd_writel(hba
, CONTROLLER_ENABLE
, REG_CONTROLLER_ENABLE
);
406 * ufshcd_is_hba_active - Get controller state
407 * @hba: per adapter instance
409 * Returns zero if controller is active, 1 otherwise
411 static inline int ufshcd_is_hba_active(struct ufs_hba
*hba
)
413 return (ufshcd_readl(hba
, REG_CONTROLLER_ENABLE
) & 0x1) ? 0 : 1;
417 * ufshcd_send_command - Send SCSI or device management commands
418 * @hba: per adapter instance
419 * @task_tag: Task tag of the command
422 void ufshcd_send_command(struct ufs_hba
*hba
, unsigned int task_tag
)
424 __set_bit(task_tag
, &hba
->outstanding_reqs
);
425 ufshcd_writel(hba
, 1 << task_tag
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
429 * ufshcd_copy_sense_data - Copy sense data in case of check condition
430 * @lrb - pointer to local reference block
432 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb
*lrbp
)
435 if (lrbp
->sense_buffer
&&
436 ufshcd_get_rsp_upiu_data_seg_len(lrbp
->ucd_rsp_ptr
)) {
437 len
= be16_to_cpu(lrbp
->ucd_rsp_ptr
->sr
.sense_data_len
);
438 memcpy(lrbp
->sense_buffer
,
439 lrbp
->ucd_rsp_ptr
->sr
.sense_data
,
440 min_t(int, len
, SCSI_SENSE_BUFFERSIZE
));
445 * ufshcd_copy_query_response() - Copy the Query Response and the data
447 * @hba: per adapter instance
448 * @lrb - pointer to local reference block
451 int ufshcd_copy_query_response(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
453 struct ufs_query_res
*query_res
= &hba
->dev_cmd
.query
.response
;
455 memcpy(&query_res
->upiu_res
, &lrbp
->ucd_rsp_ptr
->qr
, QUERY_OSF_SIZE
);
457 /* Get the descriptor */
458 if (lrbp
->ucd_rsp_ptr
->qr
.opcode
== UPIU_QUERY_OPCODE_READ_DESC
) {
459 u8
*descp
= (u8
*)lrbp
->ucd_rsp_ptr
+
460 GENERAL_UPIU_REQUEST_SIZE
;
464 /* data segment length */
465 resp_len
= be32_to_cpu(lrbp
->ucd_rsp_ptr
->header
.dword_2
) &
466 MASK_QUERY_DATA_SEG_LEN
;
467 buf_len
= be16_to_cpu(
468 hba
->dev_cmd
.query
.request
.upiu_req
.length
);
469 if (likely(buf_len
>= resp_len
)) {
470 memcpy(hba
->dev_cmd
.query
.descriptor
, descp
, resp_len
);
473 "%s: Response size is bigger than buffer",
483 * ufshcd_hba_capabilities - Read controller capabilities
484 * @hba: per adapter instance
486 static inline void ufshcd_hba_capabilities(struct ufs_hba
*hba
)
488 hba
->capabilities
= ufshcd_readl(hba
, REG_CONTROLLER_CAPABILITIES
);
490 /* nutrs and nutmrs are 0 based values */
491 hba
->nutrs
= (hba
->capabilities
& MASK_TRANSFER_REQUESTS_SLOTS
) + 1;
493 ((hba
->capabilities
& MASK_TASK_MANAGEMENT_REQUEST_SLOTS
) >> 16) + 1;
497 * ufshcd_ready_for_uic_cmd - Check if controller is ready
498 * to accept UIC commands
499 * @hba: per adapter instance
500 * Return true on success, else false
502 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba
*hba
)
504 if (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) & UIC_COMMAND_READY
)
511 * ufshcd_get_upmcrs - Get the power mode change request status
512 * @hba: Pointer to adapter instance
514 * This function gets the UPMCRS field of HCS register
515 * Returns value of UPMCRS field
517 static inline u8
ufshcd_get_upmcrs(struct ufs_hba
*hba
)
519 return (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) >> 8) & 0x7;
523 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
524 * @hba: per adapter instance
525 * @uic_cmd: UIC command
527 * Mutex must be held.
530 ufshcd_dispatch_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
532 WARN_ON(hba
->active_uic_cmd
);
534 hba
->active_uic_cmd
= uic_cmd
;
537 ufshcd_writel(hba
, uic_cmd
->argument1
, REG_UIC_COMMAND_ARG_1
);
538 ufshcd_writel(hba
, uic_cmd
->argument2
, REG_UIC_COMMAND_ARG_2
);
539 ufshcd_writel(hba
, uic_cmd
->argument3
, REG_UIC_COMMAND_ARG_3
);
542 ufshcd_writel(hba
, uic_cmd
->command
& COMMAND_OPCODE_MASK
,
547 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
548 * @hba: per adapter instance
549 * @uic_command: UIC command
551 * Must be called with mutex held.
552 * Returns 0 only if success.
555 ufshcd_wait_for_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
560 if (wait_for_completion_timeout(&uic_cmd
->done
,
561 msecs_to_jiffies(UIC_CMD_TIMEOUT
)))
562 ret
= uic_cmd
->argument2
& MASK_UIC_COMMAND_RESULT
;
566 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
567 hba
->active_uic_cmd
= NULL
;
568 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
574 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
575 * @hba: per adapter instance
576 * @uic_cmd: UIC command
578 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
580 * Returns 0 only if success.
583 __ufshcd_send_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
588 if (!ufshcd_ready_for_uic_cmd(hba
)) {
590 "Controller not ready to accept UIC commands\n");
594 init_completion(&uic_cmd
->done
);
596 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
597 ufshcd_dispatch_uic_cmd(hba
, uic_cmd
);
598 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
600 ret
= ufshcd_wait_for_uic_cmd(hba
, uic_cmd
);
606 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
607 * @hba: per adapter instance
608 * @uic_cmd: UIC command
610 * Returns 0 only if success.
613 ufshcd_send_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
617 mutex_lock(&hba
->uic_cmd_mutex
);
618 ret
= __ufshcd_send_uic_cmd(hba
, uic_cmd
);
619 mutex_unlock(&hba
->uic_cmd_mutex
);
625 * ufshcd_map_sg - Map scatter-gather list to prdt
626 * @lrbp - pointer to local reference block
628 * Returns 0 in case of success, non-zero value in case of failure
630 static int ufshcd_map_sg(struct ufshcd_lrb
*lrbp
)
632 struct ufshcd_sg_entry
*prd_table
;
633 struct scatterlist
*sg
;
634 struct scsi_cmnd
*cmd
;
639 sg_segments
= scsi_dma_map(cmd
);
644 lrbp
->utr_descriptor_ptr
->prd_table_length
=
645 cpu_to_le16((u16
) (sg_segments
));
647 prd_table
= (struct ufshcd_sg_entry
*)lrbp
->ucd_prdt_ptr
;
649 scsi_for_each_sg(cmd
, sg
, sg_segments
, i
) {
651 cpu_to_le32(((u32
) sg_dma_len(sg
))-1);
652 prd_table
[i
].base_addr
=
653 cpu_to_le32(lower_32_bits(sg
->dma_address
));
654 prd_table
[i
].upper_addr
=
655 cpu_to_le32(upper_32_bits(sg
->dma_address
));
658 lrbp
->utr_descriptor_ptr
->prd_table_length
= 0;
665 * ufshcd_enable_intr - enable interrupts
666 * @hba: per adapter instance
667 * @intrs: interrupt bits
669 static void ufshcd_enable_intr(struct ufs_hba
*hba
, u32 intrs
)
671 u32 set
= ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
673 if (hba
->ufs_version
== UFSHCI_VERSION_10
) {
675 rw
= set
& INTERRUPT_MASK_RW_VER_10
;
676 set
= rw
| ((set
^ intrs
) & intrs
);
681 ufshcd_writel(hba
, set
, REG_INTERRUPT_ENABLE
);
685 * ufshcd_disable_intr - disable interrupts
686 * @hba: per adapter instance
687 * @intrs: interrupt bits
689 static void ufshcd_disable_intr(struct ufs_hba
*hba
, u32 intrs
)
691 u32 set
= ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
693 if (hba
->ufs_version
== UFSHCI_VERSION_10
) {
695 rw
= (set
& INTERRUPT_MASK_RW_VER_10
) &
696 ~(intrs
& INTERRUPT_MASK_RW_VER_10
);
697 set
= rw
| ((set
& intrs
) & ~INTERRUPT_MASK_RW_VER_10
);
703 ufshcd_writel(hba
, set
, REG_INTERRUPT_ENABLE
);
707 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
708 * descriptor according to request
709 * @lrbp: pointer to local reference block
710 * @upiu_flags: flags required in the header
711 * @cmd_dir: requests data direction
713 static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb
*lrbp
,
714 u32
*upiu_flags
, enum dma_data_direction cmd_dir
)
716 struct utp_transfer_req_desc
*req_desc
= lrbp
->utr_descriptor_ptr
;
720 if (cmd_dir
== DMA_FROM_DEVICE
) {
721 data_direction
= UTP_DEVICE_TO_HOST
;
722 *upiu_flags
= UPIU_CMD_FLAGS_READ
;
723 } else if (cmd_dir
== DMA_TO_DEVICE
) {
724 data_direction
= UTP_HOST_TO_DEVICE
;
725 *upiu_flags
= UPIU_CMD_FLAGS_WRITE
;
727 data_direction
= UTP_NO_DATA_TRANSFER
;
728 *upiu_flags
= UPIU_CMD_FLAGS_NONE
;
731 dword_0
= data_direction
| (lrbp
->command_type
732 << UPIU_COMMAND_TYPE_OFFSET
);
734 dword_0
|= UTP_REQ_DESC_INT_CMD
;
736 /* Transfer request descriptor header fields */
737 req_desc
->header
.dword_0
= cpu_to_le32(dword_0
);
740 * assigning invalid value for command status. Controller
741 * updates OCS on command completion, with the command
744 req_desc
->header
.dword_2
=
745 cpu_to_le32(OCS_INVALID_COMMAND_STATUS
);
749 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
751 * @lrbp - local reference block pointer
752 * @upiu_flags - flags
755 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb
*lrbp
, u32 upiu_flags
)
757 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
759 /* command descriptor fields */
760 ucd_req_ptr
->header
.dword_0
= UPIU_HEADER_DWORD(
761 UPIU_TRANSACTION_COMMAND
, upiu_flags
,
762 lrbp
->lun
, lrbp
->task_tag
);
763 ucd_req_ptr
->header
.dword_1
= UPIU_HEADER_DWORD(
764 UPIU_COMMAND_SET_TYPE_SCSI
, 0, 0, 0);
766 /* Total EHS length and Data segment length will be zero */
767 ucd_req_ptr
->header
.dword_2
= 0;
769 ucd_req_ptr
->sc
.exp_data_transfer_len
=
770 cpu_to_be32(lrbp
->cmd
->sdb
.length
);
772 memcpy(ucd_req_ptr
->sc
.cdb
, lrbp
->cmd
->cmnd
,
773 (min_t(unsigned short, lrbp
->cmd
->cmd_len
, MAX_CDB_SIZE
)));
777 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
780 * @lrbp: local reference block pointer
783 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba
*hba
,
784 struct ufshcd_lrb
*lrbp
, u32 upiu_flags
)
786 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
787 struct ufs_query
*query
= &hba
->dev_cmd
.query
;
788 u16 len
= be16_to_cpu(query
->request
.upiu_req
.length
);
789 u8
*descp
= (u8
*)lrbp
->ucd_req_ptr
+ GENERAL_UPIU_REQUEST_SIZE
;
791 /* Query request header */
792 ucd_req_ptr
->header
.dword_0
= UPIU_HEADER_DWORD(
793 UPIU_TRANSACTION_QUERY_REQ
, upiu_flags
,
794 lrbp
->lun
, lrbp
->task_tag
);
795 ucd_req_ptr
->header
.dword_1
= UPIU_HEADER_DWORD(
796 0, query
->request
.query_func
, 0, 0);
798 /* Data segment length */
799 ucd_req_ptr
->header
.dword_2
= UPIU_HEADER_DWORD(
800 0, 0, len
>> 8, (u8
)len
);
802 /* Copy the Query Request buffer as is */
803 memcpy(&ucd_req_ptr
->qr
, &query
->request
.upiu_req
,
806 /* Copy the Descriptor */
807 if (query
->request
.upiu_req
.opcode
== UPIU_QUERY_OPCODE_WRITE_DESC
)
808 memcpy(descp
, query
->descriptor
, len
);
812 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb
*lrbp
)
814 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
816 memset(ucd_req_ptr
, 0, sizeof(struct utp_upiu_req
));
818 /* command descriptor fields */
819 ucd_req_ptr
->header
.dword_0
=
821 UPIU_TRANSACTION_NOP_OUT
, 0, 0, lrbp
->task_tag
);
825 * ufshcd_compose_upiu - form UFS Protocol Information Unit(UPIU)
826 * @hba - per adapter instance
827 * @lrb - pointer to local reference block
829 static int ufshcd_compose_upiu(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
834 switch (lrbp
->command_type
) {
835 case UTP_CMD_TYPE_SCSI
:
836 if (likely(lrbp
->cmd
)) {
837 ufshcd_prepare_req_desc_hdr(lrbp
, &upiu_flags
,
838 lrbp
->cmd
->sc_data_direction
);
839 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp
, upiu_flags
);
844 case UTP_CMD_TYPE_DEV_MANAGE
:
845 ufshcd_prepare_req_desc_hdr(lrbp
, &upiu_flags
, DMA_NONE
);
846 if (hba
->dev_cmd
.type
== DEV_CMD_TYPE_QUERY
)
847 ufshcd_prepare_utp_query_req_upiu(
848 hba
, lrbp
, upiu_flags
);
849 else if (hba
->dev_cmd
.type
== DEV_CMD_TYPE_NOP
)
850 ufshcd_prepare_utp_nop_upiu(lrbp
);
854 case UTP_CMD_TYPE_UFS
:
855 /* For UFS native command implementation */
857 dev_err(hba
->dev
, "%s: UFS native command are not supported\n",
862 dev_err(hba
->dev
, "%s: unknown command type: 0x%x\n",
863 __func__
, lrbp
->command_type
);
865 } /* end of switch */
871 * ufshcd_queuecommand - main entry point for SCSI requests
872 * @cmd: command from SCSI Midlayer
873 * @done: call back function
875 * Returns 0 for success, non-zero in case of failure
877 static int ufshcd_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*cmd
)
879 struct ufshcd_lrb
*lrbp
;
885 hba
= shost_priv(host
);
887 tag
= cmd
->request
->tag
;
889 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
890 switch (hba
->ufshcd_state
) {
891 case UFSHCD_STATE_OPERATIONAL
:
893 case UFSHCD_STATE_RESET
:
894 err
= SCSI_MLQUEUE_HOST_BUSY
;
896 case UFSHCD_STATE_ERROR
:
897 set_host_byte(cmd
, DID_ERROR
);
901 dev_WARN_ONCE(hba
->dev
, 1, "%s: invalid state %d\n",
902 __func__
, hba
->ufshcd_state
);
903 set_host_byte(cmd
, DID_BAD_TARGET
);
907 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
909 /* acquire the tag to make sure device cmds don't use it */
910 if (test_and_set_bit_lock(tag
, &hba
->lrb_in_use
)) {
912 * Dev manage command in progress, requeue the command.
913 * Requeuing the command helps in cases where the request *may*
914 * find different tag instead of waiting for dev manage command
917 err
= SCSI_MLQUEUE_HOST_BUSY
;
921 lrbp
= &hba
->lrb
[tag
];
925 lrbp
->sense_bufflen
= SCSI_SENSE_BUFFERSIZE
;
926 lrbp
->sense_buffer
= cmd
->sense_buffer
;
927 lrbp
->task_tag
= tag
;
928 lrbp
->lun
= cmd
->device
->lun
;
929 lrbp
->intr_cmd
= false;
930 lrbp
->command_type
= UTP_CMD_TYPE_SCSI
;
932 /* form UPIU before issuing the command */
933 ufshcd_compose_upiu(hba
, lrbp
);
934 err
= ufshcd_map_sg(lrbp
);
937 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
941 /* issue command to the controller */
942 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
943 ufshcd_send_command(hba
, tag
);
945 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
950 static int ufshcd_compose_dev_cmd(struct ufs_hba
*hba
,
951 struct ufshcd_lrb
*lrbp
, enum dev_cmd_type cmd_type
, int tag
)
954 lrbp
->sense_bufflen
= 0;
955 lrbp
->sense_buffer
= NULL
;
956 lrbp
->task_tag
= tag
;
957 lrbp
->lun
= 0; /* device management cmd is not specific to any LUN */
958 lrbp
->command_type
= UTP_CMD_TYPE_DEV_MANAGE
;
959 lrbp
->intr_cmd
= true; /* No interrupt aggregation */
960 hba
->dev_cmd
.type
= cmd_type
;
962 return ufshcd_compose_upiu(hba
, lrbp
);
966 ufshcd_clear_cmd(struct ufs_hba
*hba
, int tag
)
972 /* clear outstanding transaction before retry */
973 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
974 ufshcd_utrl_clear(hba
, tag
);
975 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
978 * wait for for h/w to clear corresponding bit in door-bell.
979 * max. wait is 1 sec.
981 err
= ufshcd_wait_for_register(hba
,
982 REG_UTP_TRANSFER_REQ_DOOR_BELL
,
983 mask
, ~mask
, 1000, 1000);
989 ufshcd_check_query_response(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
991 struct ufs_query_res
*query_res
= &hba
->dev_cmd
.query
.response
;
993 /* Get the UPIU response */
994 query_res
->response
= ufshcd_get_rsp_upiu_result(lrbp
->ucd_rsp_ptr
) >>
995 UPIU_RSP_CODE_OFFSET
;
996 return query_res
->response
;
1000 * ufshcd_dev_cmd_completion() - handles device management command responses
1001 * @hba: per adapter instance
1002 * @lrbp: pointer to local reference block
1005 ufshcd_dev_cmd_completion(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
1010 resp
= ufshcd_get_req_rsp(lrbp
->ucd_rsp_ptr
);
1013 case UPIU_TRANSACTION_NOP_IN
:
1014 if (hba
->dev_cmd
.type
!= DEV_CMD_TYPE_NOP
) {
1016 dev_err(hba
->dev
, "%s: unexpected response %x\n",
1020 case UPIU_TRANSACTION_QUERY_RSP
:
1021 err
= ufshcd_check_query_response(hba
, lrbp
);
1023 err
= ufshcd_copy_query_response(hba
, lrbp
);
1025 case UPIU_TRANSACTION_REJECT_UPIU
:
1026 /* TODO: handle Reject UPIU Response */
1028 dev_err(hba
->dev
, "%s: Reject UPIU not fully implemented\n",
1033 dev_err(hba
->dev
, "%s: Invalid device management cmd response: %x\n",
1041 static int ufshcd_wait_for_dev_cmd(struct ufs_hba
*hba
,
1042 struct ufshcd_lrb
*lrbp
, int max_timeout
)
1045 unsigned long time_left
;
1046 unsigned long flags
;
1048 time_left
= wait_for_completion_timeout(hba
->dev_cmd
.complete
,
1049 msecs_to_jiffies(max_timeout
));
1051 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1052 hba
->dev_cmd
.complete
= NULL
;
1053 if (likely(time_left
)) {
1054 err
= ufshcd_get_tr_ocs(lrbp
);
1056 err
= ufshcd_dev_cmd_completion(hba
, lrbp
);
1058 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1062 if (!ufshcd_clear_cmd(hba
, lrbp
->task_tag
))
1063 /* sucessfully cleared the command, retry if needed */
1071 * ufshcd_get_dev_cmd_tag - Get device management command tag
1072 * @hba: per-adapter instance
1073 * @tag: pointer to variable with available slot value
1075 * Get a free slot and lock it until device management command
1078 * Returns false if free slot is unavailable for locking, else
1079 * return true with tag value in @tag.
1081 static bool ufshcd_get_dev_cmd_tag(struct ufs_hba
*hba
, int *tag_out
)
1091 tmp
= ~hba
->lrb_in_use
;
1092 tag
= find_last_bit(&tmp
, hba
->nutrs
);
1093 if (tag
>= hba
->nutrs
)
1095 } while (test_and_set_bit_lock(tag
, &hba
->lrb_in_use
));
1103 static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba
*hba
, int tag
)
1105 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
1109 * ufshcd_exec_dev_cmd - API for sending device management requests
1111 * @cmd_type - specifies the type (NOP, Query...)
1112 * @timeout - time in seconds
1114 * NOTE: Since there is only one available tag for device management commands,
1115 * it is expected you hold the hba->dev_cmd.lock mutex.
1117 static int ufshcd_exec_dev_cmd(struct ufs_hba
*hba
,
1118 enum dev_cmd_type cmd_type
, int timeout
)
1120 struct ufshcd_lrb
*lrbp
;
1123 struct completion wait
;
1124 unsigned long flags
;
1127 * Get free slot, sleep if slots are unavailable.
1128 * Even though we use wait_event() which sleeps indefinitely,
1129 * the maximum wait time is bounded by SCSI request timeout.
1131 wait_event(hba
->dev_cmd
.tag_wq
, ufshcd_get_dev_cmd_tag(hba
, &tag
));
1133 init_completion(&wait
);
1134 lrbp
= &hba
->lrb
[tag
];
1136 err
= ufshcd_compose_dev_cmd(hba
, lrbp
, cmd_type
, tag
);
1140 hba
->dev_cmd
.complete
= &wait
;
1142 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1143 ufshcd_send_command(hba
, tag
);
1144 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1146 err
= ufshcd_wait_for_dev_cmd(hba
, lrbp
, timeout
);
1149 ufshcd_put_dev_cmd_tag(hba
, tag
);
1150 wake_up(&hba
->dev_cmd
.tag_wq
);
1155 * ufshcd_init_query() - init the query response and request parameters
1156 * @hba: per-adapter instance
1157 * @request: address of the request pointer to be initialized
1158 * @response: address of the response pointer to be initialized
1159 * @opcode: operation to perform
1160 * @idn: flag idn to access
1161 * @index: LU number to access
1162 * @selector: query/flag/descriptor further identification
1164 static inline void ufshcd_init_query(struct ufs_hba
*hba
,
1165 struct ufs_query_req
**request
, struct ufs_query_res
**response
,
1166 enum query_opcode opcode
, u8 idn
, u8 index
, u8 selector
)
1168 *request
= &hba
->dev_cmd
.query
.request
;
1169 *response
= &hba
->dev_cmd
.query
.response
;
1170 memset(*request
, 0, sizeof(struct ufs_query_req
));
1171 memset(*response
, 0, sizeof(struct ufs_query_res
));
1172 (*request
)->upiu_req
.opcode
= opcode
;
1173 (*request
)->upiu_req
.idn
= idn
;
1174 (*request
)->upiu_req
.index
= index
;
1175 (*request
)->upiu_req
.selector
= selector
;
1179 * ufshcd_query_flag() - API function for sending flag query requests
1180 * hba: per-adapter instance
1181 * query_opcode: flag query to perform
1182 * idn: flag idn to access
1183 * flag_res: the flag value after the query request completes
1185 * Returns 0 for success, non-zero in case of failure
1187 static int ufshcd_query_flag(struct ufs_hba
*hba
, enum query_opcode opcode
,
1188 enum flag_idn idn
, bool *flag_res
)
1190 struct ufs_query_req
*request
= NULL
;
1191 struct ufs_query_res
*response
= NULL
;
1192 int err
, index
= 0, selector
= 0;
1196 mutex_lock(&hba
->dev_cmd
.lock
);
1197 ufshcd_init_query(hba
, &request
, &response
, opcode
, idn
, index
,
1201 case UPIU_QUERY_OPCODE_SET_FLAG
:
1202 case UPIU_QUERY_OPCODE_CLEAR_FLAG
:
1203 case UPIU_QUERY_OPCODE_TOGGLE_FLAG
:
1204 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
1206 case UPIU_QUERY_OPCODE_READ_FLAG
:
1207 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
1209 /* No dummy reads */
1210 dev_err(hba
->dev
, "%s: Invalid argument for read request\n",
1218 "%s: Expected query flag opcode but got = %d\n",
1224 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
, QUERY_REQ_TIMEOUT
);
1228 "%s: Sending flag query for idn %d failed, err = %d\n",
1229 __func__
, idn
, err
);
1234 *flag_res
= (be32_to_cpu(response
->upiu_res
.value
) &
1235 MASK_QUERY_UPIU_FLAG_LOC
) & 0x1;
1238 mutex_unlock(&hba
->dev_cmd
.lock
);
1243 * ufshcd_query_attr - API function for sending attribute requests
1244 * hba: per-adapter instance
1245 * opcode: attribute opcode
1246 * idn: attribute idn to access
1247 * index: index field
1248 * selector: selector field
1249 * attr_val: the attribute value after the query request completes
1251 * Returns 0 for success, non-zero in case of failure
1253 static int ufshcd_query_attr(struct ufs_hba
*hba
, enum query_opcode opcode
,
1254 enum attr_idn idn
, u8 index
, u8 selector
, u32
*attr_val
)
1256 struct ufs_query_req
*request
= NULL
;
1257 struct ufs_query_res
*response
= NULL
;
1263 dev_err(hba
->dev
, "%s: attribute value required for opcode 0x%x\n",
1269 mutex_lock(&hba
->dev_cmd
.lock
);
1270 ufshcd_init_query(hba
, &request
, &response
, opcode
, idn
, index
,
1274 case UPIU_QUERY_OPCODE_WRITE_ATTR
:
1275 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
1276 request
->upiu_req
.value
= cpu_to_be32(*attr_val
);
1278 case UPIU_QUERY_OPCODE_READ_ATTR
:
1279 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
1282 dev_err(hba
->dev
, "%s: Expected query attr opcode but got = 0x%.2x\n",
1288 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
, QUERY_REQ_TIMEOUT
);
1291 dev_err(hba
->dev
, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1292 __func__
, opcode
, idn
, err
);
1296 *attr_val
= be32_to_cpu(response
->upiu_res
.value
);
1299 mutex_unlock(&hba
->dev_cmd
.lock
);
1305 * ufshcd_query_descriptor - API function for sending descriptor requests
1306 * hba: per-adapter instance
1307 * opcode: attribute opcode
1308 * idn: attribute idn to access
1309 * index: index field
1310 * selector: selector field
1311 * desc_buf: the buffer that contains the descriptor
1312 * buf_len: length parameter passed to the device
1314 * Returns 0 for success, non-zero in case of failure.
1315 * The buf_len parameter will contain, on return, the length parameter
1316 * received on the response.
1318 static int ufshcd_query_descriptor(struct ufs_hba
*hba
,
1319 enum query_opcode opcode
, enum desc_idn idn
, u8 index
,
1320 u8 selector
, u8
*desc_buf
, int *buf_len
)
1322 struct ufs_query_req
*request
= NULL
;
1323 struct ufs_query_res
*response
= NULL
;
1329 dev_err(hba
->dev
, "%s: descriptor buffer required for opcode 0x%x\n",
1335 if (*buf_len
<= QUERY_DESC_MIN_SIZE
|| *buf_len
> QUERY_DESC_MAX_SIZE
) {
1336 dev_err(hba
->dev
, "%s: descriptor buffer size (%d) is out of range\n",
1337 __func__
, *buf_len
);
1342 mutex_lock(&hba
->dev_cmd
.lock
);
1343 ufshcd_init_query(hba
, &request
, &response
, opcode
, idn
, index
,
1345 hba
->dev_cmd
.query
.descriptor
= desc_buf
;
1346 request
->upiu_req
.length
= cpu_to_be16(*buf_len
);
1349 case UPIU_QUERY_OPCODE_WRITE_DESC
:
1350 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
1352 case UPIU_QUERY_OPCODE_READ_DESC
:
1353 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
1357 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
1363 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
, QUERY_REQ_TIMEOUT
);
1366 dev_err(hba
->dev
, "%s: opcode 0x%.2x for idn %d failed, err = %d\n",
1367 __func__
, opcode
, idn
, err
);
1371 hba
->dev_cmd
.query
.descriptor
= NULL
;
1372 *buf_len
= be16_to_cpu(response
->upiu_res
.length
);
1375 mutex_unlock(&hba
->dev_cmd
.lock
);
1381 * ufshcd_memory_alloc - allocate memory for host memory space data structures
1382 * @hba: per adapter instance
1384 * 1. Allocate DMA memory for Command Descriptor array
1385 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
1386 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
1387 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
1389 * 4. Allocate memory for local reference block(lrb).
1391 * Returns 0 for success, non-zero in case of failure
1393 static int ufshcd_memory_alloc(struct ufs_hba
*hba
)
1395 size_t utmrdl_size
, utrdl_size
, ucdl_size
;
1397 /* Allocate memory for UTP command descriptors */
1398 ucdl_size
= (sizeof(struct utp_transfer_cmd_desc
) * hba
->nutrs
);
1399 hba
->ucdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
1401 &hba
->ucdl_dma_addr
,
1405 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
1406 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
1407 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
1408 * be aligned to 128 bytes as well
1410 if (!hba
->ucdl_base_addr
||
1411 WARN_ON(hba
->ucdl_dma_addr
& (PAGE_SIZE
- 1))) {
1413 "Command Descriptor Memory allocation failed\n");
1418 * Allocate memory for UTP Transfer descriptors
1419 * UFSHCI requires 1024 byte alignment of UTRD
1421 utrdl_size
= (sizeof(struct utp_transfer_req_desc
) * hba
->nutrs
);
1422 hba
->utrdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
1424 &hba
->utrdl_dma_addr
,
1426 if (!hba
->utrdl_base_addr
||
1427 WARN_ON(hba
->utrdl_dma_addr
& (PAGE_SIZE
- 1))) {
1429 "Transfer Descriptor Memory allocation failed\n");
1434 * Allocate memory for UTP Task Management descriptors
1435 * UFSHCI requires 1024 byte alignment of UTMRD
1437 utmrdl_size
= sizeof(struct utp_task_req_desc
) * hba
->nutmrs
;
1438 hba
->utmrdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
1440 &hba
->utmrdl_dma_addr
,
1442 if (!hba
->utmrdl_base_addr
||
1443 WARN_ON(hba
->utmrdl_dma_addr
& (PAGE_SIZE
- 1))) {
1445 "Task Management Descriptor Memory allocation failed\n");
1449 /* Allocate memory for local reference block */
1450 hba
->lrb
= devm_kzalloc(hba
->dev
,
1451 hba
->nutrs
* sizeof(struct ufshcd_lrb
),
1454 dev_err(hba
->dev
, "LRB Memory allocation failed\n");
1463 * ufshcd_host_memory_configure - configure local reference block with
1465 * @hba: per adapter instance
1467 * Configure Host memory space
1468 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
1470 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
1472 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
1473 * into local reference block.
1475 static void ufshcd_host_memory_configure(struct ufs_hba
*hba
)
1477 struct utp_transfer_cmd_desc
*cmd_descp
;
1478 struct utp_transfer_req_desc
*utrdlp
;
1479 dma_addr_t cmd_desc_dma_addr
;
1480 dma_addr_t cmd_desc_element_addr
;
1481 u16 response_offset
;
1486 utrdlp
= hba
->utrdl_base_addr
;
1487 cmd_descp
= hba
->ucdl_base_addr
;
1490 offsetof(struct utp_transfer_cmd_desc
, response_upiu
);
1492 offsetof(struct utp_transfer_cmd_desc
, prd_table
);
1494 cmd_desc_size
= sizeof(struct utp_transfer_cmd_desc
);
1495 cmd_desc_dma_addr
= hba
->ucdl_dma_addr
;
1497 for (i
= 0; i
< hba
->nutrs
; i
++) {
1498 /* Configure UTRD with command descriptor base address */
1499 cmd_desc_element_addr
=
1500 (cmd_desc_dma_addr
+ (cmd_desc_size
* i
));
1501 utrdlp
[i
].command_desc_base_addr_lo
=
1502 cpu_to_le32(lower_32_bits(cmd_desc_element_addr
));
1503 utrdlp
[i
].command_desc_base_addr_hi
=
1504 cpu_to_le32(upper_32_bits(cmd_desc_element_addr
));
1506 /* Response upiu and prdt offset should be in double words */
1507 utrdlp
[i
].response_upiu_offset
=
1508 cpu_to_le16((response_offset
>> 2));
1509 utrdlp
[i
].prd_table_offset
=
1510 cpu_to_le16((prdt_offset
>> 2));
1511 utrdlp
[i
].response_upiu_length
=
1512 cpu_to_le16(ALIGNED_UPIU_SIZE
>> 2);
1514 hba
->lrb
[i
].utr_descriptor_ptr
= (utrdlp
+ i
);
1515 hba
->lrb
[i
].ucd_req_ptr
=
1516 (struct utp_upiu_req
*)(cmd_descp
+ i
);
1517 hba
->lrb
[i
].ucd_rsp_ptr
=
1518 (struct utp_upiu_rsp
*)cmd_descp
[i
].response_upiu
;
1519 hba
->lrb
[i
].ucd_prdt_ptr
=
1520 (struct ufshcd_sg_entry
*)cmd_descp
[i
].prd_table
;
1525 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
1526 * @hba: per adapter instance
1528 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
1529 * in order to initialize the Unipro link startup procedure.
1530 * Once the Unipro links are up, the device connected to the controller
1533 * Returns 0 on success, non-zero value on failure
1535 static int ufshcd_dme_link_startup(struct ufs_hba
*hba
)
1537 struct uic_command uic_cmd
= {0};
1540 uic_cmd
.command
= UIC_CMD_DME_LINK_STARTUP
;
1542 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1545 "dme-link-startup: error code %d\n", ret
);
1550 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
1551 * @hba: per adapter instance
1552 * @attr_sel: uic command argument1
1553 * @attr_set: attribute set type as uic command argument2
1554 * @mib_val: setting value as uic command argument3
1555 * @peer: indicate whether peer or local
1557 * Returns 0 on success, non-zero value on failure
1559 int ufshcd_dme_set_attr(struct ufs_hba
*hba
, u32 attr_sel
,
1560 u8 attr_set
, u32 mib_val
, u8 peer
)
1562 struct uic_command uic_cmd
= {0};
1563 static const char *const action
[] = {
1567 const char *set
= action
[!!peer
];
1570 uic_cmd
.command
= peer
?
1571 UIC_CMD_DME_PEER_SET
: UIC_CMD_DME_SET
;
1572 uic_cmd
.argument1
= attr_sel
;
1573 uic_cmd
.argument2
= UIC_ARG_ATTR_TYPE(attr_set
);
1574 uic_cmd
.argument3
= mib_val
;
1576 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1578 dev_err(hba
->dev
, "%s: attr-id 0x%x val 0x%x error code %d\n",
1579 set
, UIC_GET_ATTR_ID(attr_sel
), mib_val
, ret
);
1583 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr
);
1586 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
1587 * @hba: per adapter instance
1588 * @attr_sel: uic command argument1
1589 * @mib_val: the value of the attribute as returned by the UIC command
1590 * @peer: indicate whether peer or local
1592 * Returns 0 on success, non-zero value on failure
1594 int ufshcd_dme_get_attr(struct ufs_hba
*hba
, u32 attr_sel
,
1595 u32
*mib_val
, u8 peer
)
1597 struct uic_command uic_cmd
= {0};
1598 static const char *const action
[] = {
1602 const char *get
= action
[!!peer
];
1605 uic_cmd
.command
= peer
?
1606 UIC_CMD_DME_PEER_GET
: UIC_CMD_DME_GET
;
1607 uic_cmd
.argument1
= attr_sel
;
1609 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1611 dev_err(hba
->dev
, "%s: attr-id 0x%x error code %d\n",
1612 get
, UIC_GET_ATTR_ID(attr_sel
), ret
);
1617 *mib_val
= uic_cmd
.argument3
;
1621 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr
);
1624 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
1625 * using DME_SET primitives.
1626 * @hba: per adapter instance
1627 * @mode: powr mode value
1629 * Returns 0 on success, non-zero value on failure
1631 static int ufshcd_uic_change_pwr_mode(struct ufs_hba
*hba
, u8 mode
)
1633 struct uic_command uic_cmd
= {0};
1634 struct completion pwr_done
;
1635 unsigned long flags
;
1639 uic_cmd
.command
= UIC_CMD_DME_SET
;
1640 uic_cmd
.argument1
= UIC_ARG_MIB(PA_PWRMODE
);
1641 uic_cmd
.argument3
= mode
;
1642 init_completion(&pwr_done
);
1644 mutex_lock(&hba
->uic_cmd_mutex
);
1646 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1647 hba
->pwr_done
= &pwr_done
;
1648 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1649 ret
= __ufshcd_send_uic_cmd(hba
, &uic_cmd
);
1652 "pwr mode change with mode 0x%x uic error %d\n",
1657 if (!wait_for_completion_timeout(hba
->pwr_done
,
1658 msecs_to_jiffies(UIC_CMD_TIMEOUT
))) {
1660 "pwr mode change with mode 0x%x completion timeout\n",
1666 status
= ufshcd_get_upmcrs(hba
);
1667 if (status
!= PWR_LOCAL
) {
1669 "pwr mode change failed, host umpcrs:0x%x\n",
1671 ret
= (status
!= PWR_OK
) ? status
: -1;
1674 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1675 hba
->pwr_done
= NULL
;
1676 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1677 mutex_unlock(&hba
->uic_cmd_mutex
);
1682 * ufshcd_config_max_pwr_mode - Set & Change power mode with
1683 * maximum capability attribute information.
1684 * @hba: per adapter instance
1686 * Returns 0 on success, non-zero value on failure
1688 static int ufshcd_config_max_pwr_mode(struct ufs_hba
*hba
)
1690 enum {RX
= 0, TX
= 1};
1691 u32 lanes
[] = {1, 1};
1692 u32 gear
[] = {1, 1};
1693 u8 pwr
[] = {FASTAUTO_MODE
, FASTAUTO_MODE
};
1696 /* Get the connected lane count */
1697 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES
), &lanes
[RX
]);
1698 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES
), &lanes
[TX
]);
1701 * First, get the maximum gears of HS speed.
1702 * If a zero value, it means there is no HSGEAR capability.
1703 * Then, get the maximum gears of PWM speed.
1705 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_MAXRXHSGEAR
), &gear
[RX
]);
1707 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_MAXRXPWMGEAR
), &gear
[RX
]);
1708 pwr
[RX
] = SLOWAUTO_MODE
;
1711 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_MAXRXHSGEAR
), &gear
[TX
]);
1713 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_MAXRXPWMGEAR
),
1715 pwr
[TX
] = SLOWAUTO_MODE
;
1719 * Configure attributes for power mode change with below.
1720 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
1721 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
1724 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXGEAR
), gear
[RX
]);
1725 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_ACTIVERXDATALANES
), lanes
[RX
]);
1726 if (pwr
[RX
] == FASTAUTO_MODE
)
1727 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXTERMINATION
), TRUE
);
1729 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXGEAR
), gear
[TX
]);
1730 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_ACTIVETXDATALANES
), lanes
[TX
]);
1731 if (pwr
[TX
] == FASTAUTO_MODE
)
1732 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXTERMINATION
), TRUE
);
1734 if (pwr
[RX
] == FASTAUTO_MODE
|| pwr
[TX
] == FASTAUTO_MODE
)
1735 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_HSSERIES
), PA_HS_MODE_B
);
1737 ret
= ufshcd_uic_change_pwr_mode(hba
, pwr
[RX
] << 4 | pwr
[TX
]);
1740 "pwr_mode: power mode change failed %d\n", ret
);
1746 * ufshcd_complete_dev_init() - checks device readiness
1747 * hba: per-adapter instance
1749 * Set fDeviceInit flag and poll until device toggles it.
1751 static int ufshcd_complete_dev_init(struct ufs_hba
*hba
)
1753 int i
, retries
, err
= 0;
1756 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
1757 /* Set the fDeviceInit flag */
1758 err
= ufshcd_query_flag(hba
, UPIU_QUERY_OPCODE_SET_FLAG
,
1759 QUERY_FLAG_IDN_FDEVICEINIT
, NULL
);
1760 if (!err
|| err
== -ETIMEDOUT
)
1762 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, err
);
1766 "%s setting fDeviceInit flag failed with error %d\n",
1771 /* poll for max. 100 iterations for fDeviceInit flag to clear */
1772 for (i
= 0; i
< 100 && !err
&& flag_res
; i
++) {
1773 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
1774 err
= ufshcd_query_flag(hba
,
1775 UPIU_QUERY_OPCODE_READ_FLAG
,
1776 QUERY_FLAG_IDN_FDEVICEINIT
, &flag_res
);
1777 if (!err
|| err
== -ETIMEDOUT
)
1779 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
,
1785 "%s reading fDeviceInit flag failed with error %d\n",
1789 "%s fDeviceInit was not cleared by the device\n",
1797 * ufshcd_make_hba_operational - Make UFS controller operational
1798 * @hba: per adapter instance
1800 * To bring UFS host controller to operational state,
1801 * 1. Check if device is present
1802 * 2. Enable required interrupts
1803 * 3. Configure interrupt aggregation
1804 * 4. Program UTRL and UTMRL base addres
1805 * 5. Configure run-stop-registers
1807 * Returns 0 on success, non-zero value on failure
1809 static int ufshcd_make_hba_operational(struct ufs_hba
*hba
)
1814 /* check if device present */
1815 reg
= ufshcd_readl(hba
, REG_CONTROLLER_STATUS
);
1816 if (!ufshcd_is_device_present(reg
)) {
1817 dev_err(hba
->dev
, "cc: Device not present\n");
1822 /* Enable required interrupts */
1823 ufshcd_enable_intr(hba
, UFSHCD_ENABLE_INTRS
);
1825 /* Configure interrupt aggregation */
1826 ufshcd_config_intr_aggr(hba
, hba
->nutrs
- 1, INT_AGGR_DEF_TO
);
1828 /* Configure UTRL and UTMRL base address registers */
1829 ufshcd_writel(hba
, lower_32_bits(hba
->utrdl_dma_addr
),
1830 REG_UTP_TRANSFER_REQ_LIST_BASE_L
);
1831 ufshcd_writel(hba
, upper_32_bits(hba
->utrdl_dma_addr
),
1832 REG_UTP_TRANSFER_REQ_LIST_BASE_H
);
1833 ufshcd_writel(hba
, lower_32_bits(hba
->utmrdl_dma_addr
),
1834 REG_UTP_TASK_REQ_LIST_BASE_L
);
1835 ufshcd_writel(hba
, upper_32_bits(hba
->utmrdl_dma_addr
),
1836 REG_UTP_TASK_REQ_LIST_BASE_H
);
1839 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
1840 * DEI, HEI bits must be 0
1842 if (!(ufshcd_get_lists_status(reg
))) {
1843 ufshcd_enable_run_stop_reg(hba
);
1846 "Host controller not ready to process requests");
1856 * ufshcd_hba_enable - initialize the controller
1857 * @hba: per adapter instance
1859 * The controller resets itself and controller firmware initialization
1860 * sequence kicks off. When controller is ready it will set
1861 * the Host Controller Enable bit to 1.
1863 * Returns 0 on success, non-zero value on failure
1865 static int ufshcd_hba_enable(struct ufs_hba
*hba
)
1870 * msleep of 1 and 5 used in this function might result in msleep(20),
1871 * but it was necessary to send the UFS FPGA to reset mode during
1872 * development and testing of this driver. msleep can be changed to
1873 * mdelay and retry count can be reduced based on the controller.
1875 if (!ufshcd_is_hba_active(hba
)) {
1877 /* change controller state to "reset state" */
1878 ufshcd_hba_stop(hba
);
1881 * This delay is based on the testing done with UFS host
1882 * controller FPGA. The delay can be changed based on the
1883 * host controller used.
1888 /* start controller initialization sequence */
1889 ufshcd_hba_start(hba
);
1892 * To initialize a UFS host controller HCE bit must be set to 1.
1893 * During initialization the HCE bit value changes from 1->0->1.
1894 * When the host controller completes initialization sequence
1895 * it sets the value of HCE bit to 1. The same HCE bit is read back
1896 * to check if the controller has completed initialization sequence.
1897 * So without this delay the value HCE = 1, set in the previous
1898 * instruction might be read back.
1899 * This delay can be changed based on the controller.
1903 /* wait for the host controller to complete initialization */
1905 while (ufshcd_is_hba_active(hba
)) {
1910 "Controller enable failed\n");
1919 * ufshcd_link_startup - Initialize unipro link startup
1920 * @hba: per adapter instance
1922 * Returns 0 for success, non-zero in case of failure
1924 static int ufshcd_link_startup(struct ufs_hba
*hba
)
1928 /* enable UIC related interrupts */
1929 ufshcd_enable_intr(hba
, UIC_COMMAND_COMPL
);
1931 ret
= ufshcd_dme_link_startup(hba
);
1935 ret
= ufshcd_make_hba_operational(hba
);
1939 dev_err(hba
->dev
, "link startup failed %d\n", ret
);
1944 * ufshcd_verify_dev_init() - Verify device initialization
1945 * @hba: per-adapter instance
1947 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
1948 * device Transport Protocol (UTP) layer is ready after a reset.
1949 * If the UTP layer at the device side is not initialized, it may
1950 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
1951 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
1953 static int ufshcd_verify_dev_init(struct ufs_hba
*hba
)
1958 mutex_lock(&hba
->dev_cmd
.lock
);
1959 for (retries
= NOP_OUT_RETRIES
; retries
> 0; retries
--) {
1960 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_NOP
,
1963 if (!err
|| err
== -ETIMEDOUT
)
1966 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, err
);
1968 mutex_unlock(&hba
->dev_cmd
.lock
);
1971 dev_err(hba
->dev
, "%s: NOP OUT failed %d\n", __func__
, err
);
1976 * ufshcd_slave_alloc - handle initial SCSI device configurations
1977 * @sdev: pointer to SCSI device
1981 static int ufshcd_slave_alloc(struct scsi_device
*sdev
)
1983 struct ufs_hba
*hba
;
1986 hba
= shost_priv(sdev
->host
);
1987 sdev
->tagged_supported
= 1;
1989 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
1990 sdev
->use_10_for_ms
= 1;
1991 scsi_set_tag_type(sdev
, MSG_SIMPLE_TAG
);
1993 /* allow SCSI layer to restart the device in case of errors */
1994 sdev
->allow_restart
= 1;
1996 /* REPORT SUPPORTED OPERATION CODES is not supported */
1997 sdev
->no_report_opcodes
= 1;
1999 lun_qdepth
= ufshcd_read_sdev_qdepth(hba
, sdev
);
2000 if (lun_qdepth
<= 0)
2001 /* eventually, we can figure out the real queue depth */
2002 lun_qdepth
= hba
->nutrs
;
2004 lun_qdepth
= min_t(int, lun_qdepth
, hba
->nutrs
);
2006 dev_dbg(hba
->dev
, "%s: activate tcq with queue depth %d\n",
2007 __func__
, lun_qdepth
);
2008 scsi_activate_tcq(sdev
, lun_qdepth
);
2014 * ufshcd_change_queue_depth - change queue depth
2015 * @sdev: pointer to SCSI device
2016 * @depth: required depth to set
2017 * @reason: reason for changing the depth
2019 * Change queue depth according to the reason and make sure
2020 * the max. limits are not crossed.
2022 static int ufshcd_change_queue_depth(struct scsi_device
*sdev
,
2023 int depth
, int reason
)
2025 struct ufs_hba
*hba
= shost_priv(sdev
->host
);
2027 if (depth
> hba
->nutrs
)
2031 case SCSI_QDEPTH_DEFAULT
:
2032 case SCSI_QDEPTH_RAMP_UP
:
2033 if (!sdev
->tagged_supported
)
2035 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
2037 case SCSI_QDEPTH_QFULL
:
2038 scsi_track_queue_full(sdev
, depth
);
2048 * ufshcd_slave_configure - adjust SCSI device configurations
2049 * @sdev: pointer to SCSI device
2051 static int ufshcd_slave_configure(struct scsi_device
*sdev
)
2053 struct request_queue
*q
= sdev
->request_queue
;
2055 blk_queue_update_dma_pad(q
, PRDT_DATA_BYTE_COUNT_PAD
- 1);
2056 blk_queue_max_segment_size(q
, PRDT_DATA_BYTE_COUNT_MAX
);
2062 * ufshcd_slave_destroy - remove SCSI device configurations
2063 * @sdev: pointer to SCSI device
2065 static void ufshcd_slave_destroy(struct scsi_device
*sdev
)
2067 struct ufs_hba
*hba
;
2069 hba
= shost_priv(sdev
->host
);
2070 scsi_deactivate_tcq(sdev
, hba
->nutrs
);
2074 * ufshcd_task_req_compl - handle task management request completion
2075 * @hba: per adapter instance
2076 * @index: index of the completed request
2077 * @resp: task management service response
2079 * Returns non-zero value on error, zero on success
2081 static int ufshcd_task_req_compl(struct ufs_hba
*hba
, u32 index
, u8
*resp
)
2083 struct utp_task_req_desc
*task_req_descp
;
2084 struct utp_upiu_task_rsp
*task_rsp_upiup
;
2085 unsigned long flags
;
2089 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2091 /* Clear completed tasks from outstanding_tasks */
2092 __clear_bit(index
, &hba
->outstanding_tasks
);
2094 task_req_descp
= hba
->utmrdl_base_addr
;
2095 ocs_value
= ufshcd_get_tmr_ocs(&task_req_descp
[index
]);
2097 if (ocs_value
== OCS_SUCCESS
) {
2098 task_rsp_upiup
= (struct utp_upiu_task_rsp
*)
2099 task_req_descp
[index
].task_rsp_upiu
;
2100 task_result
= be32_to_cpu(task_rsp_upiup
->header
.dword_1
);
2101 task_result
= ((task_result
& MASK_TASK_RESPONSE
) >> 8);
2103 *resp
= (u8
)task_result
;
2105 dev_err(hba
->dev
, "%s: failed, ocs = 0x%x\n",
2106 __func__
, ocs_value
);
2108 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2114 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
2115 * @lrb: pointer to local reference block of completed command
2116 * @scsi_status: SCSI command status
2118 * Returns value base on SCSI command status
2121 ufshcd_scsi_cmd_status(struct ufshcd_lrb
*lrbp
, int scsi_status
)
2125 switch (scsi_status
) {
2126 case SAM_STAT_CHECK_CONDITION
:
2127 ufshcd_copy_sense_data(lrbp
);
2129 result
|= DID_OK
<< 16 |
2130 COMMAND_COMPLETE
<< 8 |
2133 case SAM_STAT_TASK_SET_FULL
:
2135 case SAM_STAT_TASK_ABORTED
:
2136 ufshcd_copy_sense_data(lrbp
);
2137 result
|= scsi_status
;
2140 result
|= DID_ERROR
<< 16;
2142 } /* end of switch */
2148 * ufshcd_transfer_rsp_status - Get overall status of the response
2149 * @hba: per adapter instance
2150 * @lrb: pointer to local reference block of completed command
2152 * Returns result of the command to notify SCSI midlayer
2155 ufshcd_transfer_rsp_status(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2161 /* overall command status of utrd */
2162 ocs
= ufshcd_get_tr_ocs(lrbp
);
2166 result
= ufshcd_get_req_rsp(lrbp
->ucd_rsp_ptr
);
2169 case UPIU_TRANSACTION_RESPONSE
:
2171 * get the response UPIU result to extract
2172 * the SCSI command status
2174 result
= ufshcd_get_rsp_upiu_result(lrbp
->ucd_rsp_ptr
);
2177 * get the result based on SCSI status response
2178 * to notify the SCSI midlayer of the command status
2180 scsi_status
= result
& MASK_SCSI_STATUS
;
2181 result
= ufshcd_scsi_cmd_status(lrbp
, scsi_status
);
2183 if (ufshcd_is_exception_event(lrbp
->ucd_rsp_ptr
))
2184 schedule_work(&hba
->eeh_work
);
2186 case UPIU_TRANSACTION_REJECT_UPIU
:
2187 /* TODO: handle Reject UPIU Response */
2188 result
= DID_ERROR
<< 16;
2190 "Reject UPIU not fully implemented\n");
2193 result
= DID_ERROR
<< 16;
2195 "Unexpected request response code = %x\n",
2201 result
|= DID_ABORT
<< 16;
2203 case OCS_INVALID_COMMAND_STATUS
:
2204 result
|= DID_REQUEUE
<< 16;
2206 case OCS_INVALID_CMD_TABLE_ATTR
:
2207 case OCS_INVALID_PRDT_ATTR
:
2208 case OCS_MISMATCH_DATA_BUF_SIZE
:
2209 case OCS_MISMATCH_RESP_UPIU_SIZE
:
2210 case OCS_PEER_COMM_FAILURE
:
2211 case OCS_FATAL_ERROR
:
2213 result
|= DID_ERROR
<< 16;
2215 "OCS error from controller = %x\n", ocs
);
2217 } /* end of switch */
2223 * ufshcd_uic_cmd_compl - handle completion of uic command
2224 * @hba: per adapter instance
2225 * @intr_status: interrupt status generated by the controller
2227 static void ufshcd_uic_cmd_compl(struct ufs_hba
*hba
, u32 intr_status
)
2229 if ((intr_status
& UIC_COMMAND_COMPL
) && hba
->active_uic_cmd
) {
2230 hba
->active_uic_cmd
->argument2
|=
2231 ufshcd_get_uic_cmd_result(hba
);
2232 hba
->active_uic_cmd
->argument3
=
2233 ufshcd_get_dme_attr_val(hba
);
2234 complete(&hba
->active_uic_cmd
->done
);
2237 if ((intr_status
& UIC_POWER_MODE
) && hba
->pwr_done
)
2238 complete(hba
->pwr_done
);
2242 * ufshcd_transfer_req_compl - handle SCSI and query command completion
2243 * @hba: per adapter instance
2245 static void ufshcd_transfer_req_compl(struct ufs_hba
*hba
)
2247 struct ufshcd_lrb
*lrbp
;
2248 struct scsi_cmnd
*cmd
;
2249 unsigned long completed_reqs
;
2254 /* Resetting interrupt aggregation counters first and reading the
2255 * DOOR_BELL afterward allows us to handle all the completed requests.
2256 * In order to prevent other interrupts starvation the DB is read once
2257 * after reset. The down side of this solution is the possibility of
2258 * false interrupt if device completes another request after resetting
2259 * aggregation and before reading the DB.
2261 ufshcd_reset_intr_aggr(hba
);
2263 tr_doorbell
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
2264 completed_reqs
= tr_doorbell
^ hba
->outstanding_reqs
;
2266 for_each_set_bit(index
, &completed_reqs
, hba
->nutrs
) {
2267 lrbp
= &hba
->lrb
[index
];
2270 result
= ufshcd_transfer_rsp_status(hba
, lrbp
);
2271 scsi_dma_unmap(cmd
);
2272 cmd
->result
= result
;
2273 /* Mark completed command as NULL in LRB */
2275 clear_bit_unlock(index
, &hba
->lrb_in_use
);
2276 /* Do not touch lrbp after scsi done */
2277 cmd
->scsi_done(cmd
);
2278 } else if (lrbp
->command_type
== UTP_CMD_TYPE_DEV_MANAGE
) {
2279 if (hba
->dev_cmd
.complete
)
2280 complete(hba
->dev_cmd
.complete
);
2284 /* clear corresponding bits of completed commands */
2285 hba
->outstanding_reqs
^= completed_reqs
;
2287 /* we might have free'd some tags above */
2288 wake_up(&hba
->dev_cmd
.tag_wq
);
2292 * ufshcd_disable_ee - disable exception event
2293 * @hba: per-adapter instance
2294 * @mask: exception event to disable
2296 * Disables exception event in the device so that the EVENT_ALERT
2299 * Returns zero on success, non-zero error value on failure.
2301 static int ufshcd_disable_ee(struct ufs_hba
*hba
, u16 mask
)
2306 if (!(hba
->ee_ctrl_mask
& mask
))
2309 val
= hba
->ee_ctrl_mask
& ~mask
;
2310 val
&= 0xFFFF; /* 2 bytes */
2311 err
= ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
2312 QUERY_ATTR_IDN_EE_CONTROL
, 0, 0, &val
);
2314 hba
->ee_ctrl_mask
&= ~mask
;
2320 * ufshcd_enable_ee - enable exception event
2321 * @hba: per-adapter instance
2322 * @mask: exception event to enable
2324 * Enable corresponding exception event in the device to allow
2325 * device to alert host in critical scenarios.
2327 * Returns zero on success, non-zero error value on failure.
2329 static int ufshcd_enable_ee(struct ufs_hba
*hba
, u16 mask
)
2334 if (hba
->ee_ctrl_mask
& mask
)
2337 val
= hba
->ee_ctrl_mask
| mask
;
2338 val
&= 0xFFFF; /* 2 bytes */
2339 err
= ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
2340 QUERY_ATTR_IDN_EE_CONTROL
, 0, 0, &val
);
2342 hba
->ee_ctrl_mask
|= mask
;
2348 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
2349 * @hba: per-adapter instance
2351 * Allow device to manage background operations on its own. Enabling
2352 * this might lead to inconsistent latencies during normal data transfers
2353 * as the device is allowed to manage its own way of handling background
2356 * Returns zero on success, non-zero on failure.
2358 static int ufshcd_enable_auto_bkops(struct ufs_hba
*hba
)
2362 if (hba
->auto_bkops_enabled
)
2365 err
= ufshcd_query_flag(hba
, UPIU_QUERY_OPCODE_SET_FLAG
,
2366 QUERY_FLAG_IDN_BKOPS_EN
, NULL
);
2368 dev_err(hba
->dev
, "%s: failed to enable bkops %d\n",
2373 hba
->auto_bkops_enabled
= true;
2375 /* No need of URGENT_BKOPS exception from the device */
2376 err
= ufshcd_disable_ee(hba
, MASK_EE_URGENT_BKOPS
);
2378 dev_err(hba
->dev
, "%s: failed to disable exception event %d\n",
2385 * ufshcd_disable_auto_bkops - block device in doing background operations
2386 * @hba: per-adapter instance
2388 * Disabling background operations improves command response latency but
2389 * has drawback of device moving into critical state where the device is
2390 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
2391 * host is idle so that BKOPS are managed effectively without any negative
2394 * Returns zero on success, non-zero on failure.
2396 static int ufshcd_disable_auto_bkops(struct ufs_hba
*hba
)
2400 if (!hba
->auto_bkops_enabled
)
2404 * If host assisted BKOPs is to be enabled, make sure
2405 * urgent bkops exception is allowed.
2407 err
= ufshcd_enable_ee(hba
, MASK_EE_URGENT_BKOPS
);
2409 dev_err(hba
->dev
, "%s: failed to enable exception event %d\n",
2414 err
= ufshcd_query_flag(hba
, UPIU_QUERY_OPCODE_CLEAR_FLAG
,
2415 QUERY_FLAG_IDN_BKOPS_EN
, NULL
);
2417 dev_err(hba
->dev
, "%s: failed to disable bkops %d\n",
2419 ufshcd_disable_ee(hba
, MASK_EE_URGENT_BKOPS
);
2423 hba
->auto_bkops_enabled
= false;
2429 * ufshcd_force_reset_auto_bkops - force enable of auto bkops
2430 * @hba: per adapter instance
2432 * After a device reset the device may toggle the BKOPS_EN flag
2433 * to default value. The s/w tracking variables should be updated
2434 * as well. Do this by forcing enable of auto bkops.
2436 static void ufshcd_force_reset_auto_bkops(struct ufs_hba
*hba
)
2438 hba
->auto_bkops_enabled
= false;
2439 hba
->ee_ctrl_mask
|= MASK_EE_URGENT_BKOPS
;
2440 ufshcd_enable_auto_bkops(hba
);
2443 static inline int ufshcd_get_bkops_status(struct ufs_hba
*hba
, u32
*status
)
2445 return ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_READ_ATTR
,
2446 QUERY_ATTR_IDN_BKOPS_STATUS
, 0, 0, status
);
2450 * ufshcd_urgent_bkops - handle urgent bkops exception event
2451 * @hba: per-adapter instance
2453 * Enable fBackgroundOpsEn flag in the device to permit background
2456 static int ufshcd_urgent_bkops(struct ufs_hba
*hba
)
2461 err
= ufshcd_get_bkops_status(hba
, &status
);
2463 dev_err(hba
->dev
, "%s: failed to get BKOPS status %d\n",
2468 status
= status
& 0xF;
2470 /* handle only if status indicates performance impact or critical */
2471 if (status
>= BKOPS_STATUS_PERF_IMPACT
)
2472 err
= ufshcd_enable_auto_bkops(hba
);
2477 static inline int ufshcd_get_ee_status(struct ufs_hba
*hba
, u32
*status
)
2479 return ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_READ_ATTR
,
2480 QUERY_ATTR_IDN_EE_STATUS
, 0, 0, status
);
2484 * ufshcd_exception_event_handler - handle exceptions raised by device
2485 * @work: pointer to work data
2487 * Read bExceptionEventStatus attribute from the device and handle the
2488 * exception event accordingly.
2490 static void ufshcd_exception_event_handler(struct work_struct
*work
)
2492 struct ufs_hba
*hba
;
2495 hba
= container_of(work
, struct ufs_hba
, eeh_work
);
2497 pm_runtime_get_sync(hba
->dev
);
2498 err
= ufshcd_get_ee_status(hba
, &status
);
2500 dev_err(hba
->dev
, "%s: failed to get exception status %d\n",
2505 status
&= hba
->ee_ctrl_mask
;
2506 if (status
& MASK_EE_URGENT_BKOPS
) {
2507 err
= ufshcd_urgent_bkops(hba
);
2509 dev_err(hba
->dev
, "%s: failed to handle urgent bkops %d\n",
2513 pm_runtime_put_sync(hba
->dev
);
2518 * ufshcd_err_handler - handle UFS errors that require s/w attention
2519 * @work: pointer to work structure
2521 static void ufshcd_err_handler(struct work_struct
*work
)
2523 struct ufs_hba
*hba
;
2524 unsigned long flags
;
2530 hba
= container_of(work
, struct ufs_hba
, eh_work
);
2532 pm_runtime_get_sync(hba
->dev
);
2534 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2535 if (hba
->ufshcd_state
== UFSHCD_STATE_RESET
) {
2536 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2540 hba
->ufshcd_state
= UFSHCD_STATE_RESET
;
2541 ufshcd_set_eh_in_progress(hba
);
2543 /* Complete requests that have door-bell cleared by h/w */
2544 ufshcd_transfer_req_compl(hba
);
2545 ufshcd_tmc_handler(hba
);
2546 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2548 /* Clear pending transfer requests */
2549 for_each_set_bit(tag
, &hba
->outstanding_reqs
, hba
->nutrs
)
2550 if (ufshcd_clear_cmd(hba
, tag
))
2551 err_xfer
|= 1 << tag
;
2553 /* Clear pending task management requests */
2554 for_each_set_bit(tag
, &hba
->outstanding_tasks
, hba
->nutmrs
)
2555 if (ufshcd_clear_tm_cmd(hba
, tag
))
2558 /* Complete the requests that are cleared by s/w */
2559 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2560 ufshcd_transfer_req_compl(hba
);
2561 ufshcd_tmc_handler(hba
);
2562 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2564 /* Fatal errors need reset */
2565 if (err_xfer
|| err_tm
|| (hba
->saved_err
& INT_FATAL_ERRORS
) ||
2566 ((hba
->saved_err
& UIC_ERROR
) &&
2567 (hba
->saved_uic_err
& UFSHCD_UIC_DL_PA_INIT_ERROR
))) {
2568 err
= ufshcd_reset_and_restore(hba
);
2570 dev_err(hba
->dev
, "%s: reset and restore failed\n",
2572 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
2575 * Inform scsi mid-layer that we did reset and allow to handle
2576 * Unit Attention properly.
2578 scsi_report_bus_reset(hba
->host
, 0);
2580 hba
->saved_uic_err
= 0;
2582 ufshcd_clear_eh_in_progress(hba
);
2585 scsi_unblock_requests(hba
->host
);
2586 pm_runtime_put_sync(hba
->dev
);
2590 * ufshcd_update_uic_error - check and set fatal UIC error flags.
2591 * @hba: per-adapter instance
2593 static void ufshcd_update_uic_error(struct ufs_hba
*hba
)
2597 /* PA_INIT_ERROR is fatal and needs UIC reset */
2598 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_DATA_LINK_LAYER
);
2599 if (reg
& UIC_DATA_LINK_LAYER_ERROR_PA_INIT
)
2600 hba
->uic_error
|= UFSHCD_UIC_DL_PA_INIT_ERROR
;
2602 /* UIC NL/TL/DME errors needs software retry */
2603 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_NETWORK_LAYER
);
2605 hba
->uic_error
|= UFSHCD_UIC_NL_ERROR
;
2607 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_TRANSPORT_LAYER
);
2609 hba
->uic_error
|= UFSHCD_UIC_TL_ERROR
;
2611 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_DME
);
2613 hba
->uic_error
|= UFSHCD_UIC_DME_ERROR
;
2615 dev_dbg(hba
->dev
, "%s: UIC error flags = 0x%08x\n",
2616 __func__
, hba
->uic_error
);
2620 * ufshcd_check_errors - Check for errors that need s/w attention
2621 * @hba: per-adapter instance
2623 static void ufshcd_check_errors(struct ufs_hba
*hba
)
2625 bool queue_eh_work
= false;
2627 if (hba
->errors
& INT_FATAL_ERRORS
)
2628 queue_eh_work
= true;
2630 if (hba
->errors
& UIC_ERROR
) {
2632 ufshcd_update_uic_error(hba
);
2634 queue_eh_work
= true;
2637 if (queue_eh_work
) {
2638 /* handle fatal errors only when link is functional */
2639 if (hba
->ufshcd_state
== UFSHCD_STATE_OPERATIONAL
) {
2640 /* block commands from scsi mid-layer */
2641 scsi_block_requests(hba
->host
);
2643 /* transfer error masks to sticky bits */
2644 hba
->saved_err
|= hba
->errors
;
2645 hba
->saved_uic_err
|= hba
->uic_error
;
2647 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
2648 schedule_work(&hba
->eh_work
);
2652 * if (!queue_eh_work) -
2653 * Other errors are either non-fatal where host recovers
2654 * itself without s/w intervention or errors that will be
2655 * handled by the SCSI core layer.
2660 * ufshcd_tmc_handler - handle task management function completion
2661 * @hba: per adapter instance
2663 static void ufshcd_tmc_handler(struct ufs_hba
*hba
)
2667 tm_doorbell
= ufshcd_readl(hba
, REG_UTP_TASK_REQ_DOOR_BELL
);
2668 hba
->tm_condition
= tm_doorbell
^ hba
->outstanding_tasks
;
2669 wake_up(&hba
->tm_wq
);
2673 * ufshcd_sl_intr - Interrupt service routine
2674 * @hba: per adapter instance
2675 * @intr_status: contains interrupts generated by the controller
2677 static void ufshcd_sl_intr(struct ufs_hba
*hba
, u32 intr_status
)
2679 hba
->errors
= UFSHCD_ERROR_MASK
& intr_status
;
2681 ufshcd_check_errors(hba
);
2683 if (intr_status
& UFSHCD_UIC_MASK
)
2684 ufshcd_uic_cmd_compl(hba
, intr_status
);
2686 if (intr_status
& UTP_TASK_REQ_COMPL
)
2687 ufshcd_tmc_handler(hba
);
2689 if (intr_status
& UTP_TRANSFER_REQ_COMPL
)
2690 ufshcd_transfer_req_compl(hba
);
2694 * ufshcd_intr - Main interrupt service routine
2696 * @__hba: pointer to adapter instance
2698 * Returns IRQ_HANDLED - If interrupt is valid
2699 * IRQ_NONE - If invalid interrupt
2701 static irqreturn_t
ufshcd_intr(int irq
, void *__hba
)
2704 irqreturn_t retval
= IRQ_NONE
;
2705 struct ufs_hba
*hba
= __hba
;
2707 spin_lock(hba
->host
->host_lock
);
2708 intr_status
= ufshcd_readl(hba
, REG_INTERRUPT_STATUS
);
2711 ufshcd_writel(hba
, intr_status
, REG_INTERRUPT_STATUS
);
2712 ufshcd_sl_intr(hba
, intr_status
);
2713 retval
= IRQ_HANDLED
;
2715 spin_unlock(hba
->host
->host_lock
);
2719 static int ufshcd_clear_tm_cmd(struct ufs_hba
*hba
, int tag
)
2722 u32 mask
= 1 << tag
;
2723 unsigned long flags
;
2725 if (!test_bit(tag
, &hba
->outstanding_tasks
))
2728 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2729 ufshcd_writel(hba
, ~(1 << tag
), REG_UTP_TASK_REQ_LIST_CLEAR
);
2730 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2732 /* poll for max. 1 sec to clear door bell register by h/w */
2733 err
= ufshcd_wait_for_register(hba
,
2734 REG_UTP_TASK_REQ_DOOR_BELL
,
2735 mask
, 0, 1000, 1000);
2741 * ufshcd_issue_tm_cmd - issues task management commands to controller
2742 * @hba: per adapter instance
2743 * @lun_id: LUN ID to which TM command is sent
2744 * @task_id: task ID to which the TM command is applicable
2745 * @tm_function: task management function opcode
2746 * @tm_response: task management service response return value
2748 * Returns non-zero value on error, zero on success.
2750 static int ufshcd_issue_tm_cmd(struct ufs_hba
*hba
, int lun_id
, int task_id
,
2751 u8 tm_function
, u8
*tm_response
)
2753 struct utp_task_req_desc
*task_req_descp
;
2754 struct utp_upiu_task_req
*task_req_upiup
;
2755 struct Scsi_Host
*host
;
2756 unsigned long flags
;
2764 * Get free slot, sleep if slots are unavailable.
2765 * Even though we use wait_event() which sleeps indefinitely,
2766 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
2768 wait_event(hba
->tm_tag_wq
, ufshcd_get_tm_free_slot(hba
, &free_slot
));
2770 spin_lock_irqsave(host
->host_lock
, flags
);
2771 task_req_descp
= hba
->utmrdl_base_addr
;
2772 task_req_descp
+= free_slot
;
2774 /* Configure task request descriptor */
2775 task_req_descp
->header
.dword_0
= cpu_to_le32(UTP_REQ_DESC_INT_CMD
);
2776 task_req_descp
->header
.dword_2
=
2777 cpu_to_le32(OCS_INVALID_COMMAND_STATUS
);
2779 /* Configure task request UPIU */
2781 (struct utp_upiu_task_req
*) task_req_descp
->task_req_upiu
;
2782 task_tag
= hba
->nutrs
+ free_slot
;
2783 task_req_upiup
->header
.dword_0
=
2784 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ
, 0,
2786 task_req_upiup
->header
.dword_1
=
2787 UPIU_HEADER_DWORD(0, tm_function
, 0, 0);
2789 task_req_upiup
->input_param1
= cpu_to_be32(lun_id
);
2790 task_req_upiup
->input_param2
= cpu_to_be32(task_id
);
2792 /* send command to the controller */
2793 __set_bit(free_slot
, &hba
->outstanding_tasks
);
2794 ufshcd_writel(hba
, 1 << free_slot
, REG_UTP_TASK_REQ_DOOR_BELL
);
2796 spin_unlock_irqrestore(host
->host_lock
, flags
);
2798 /* wait until the task management command is completed */
2799 err
= wait_event_timeout(hba
->tm_wq
,
2800 test_bit(free_slot
, &hba
->tm_condition
),
2801 msecs_to_jiffies(TM_CMD_TIMEOUT
));
2803 dev_err(hba
->dev
, "%s: task management cmd 0x%.2x timed-out\n",
2804 __func__
, tm_function
);
2805 if (ufshcd_clear_tm_cmd(hba
, free_slot
))
2806 dev_WARN(hba
->dev
, "%s: unable clear tm cmd (slot %d) after timeout\n",
2807 __func__
, free_slot
);
2810 err
= ufshcd_task_req_compl(hba
, free_slot
, tm_response
);
2813 clear_bit(free_slot
, &hba
->tm_condition
);
2814 ufshcd_put_tm_slot(hba
, free_slot
);
2815 wake_up(&hba
->tm_tag_wq
);
2821 * ufshcd_eh_device_reset_handler - device reset handler registered to
2823 * @cmd: SCSI command pointer
2825 * Returns SUCCESS/FAILED
2827 static int ufshcd_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
2829 struct Scsi_Host
*host
;
2830 struct ufs_hba
*hba
;
2835 struct ufshcd_lrb
*lrbp
;
2836 unsigned long flags
;
2838 host
= cmd
->device
->host
;
2839 hba
= shost_priv(host
);
2840 tag
= cmd
->request
->tag
;
2842 lrbp
= &hba
->lrb
[tag
];
2843 err
= ufshcd_issue_tm_cmd(hba
, lrbp
->lun
, 0, UFS_LOGICAL_RESET
, &resp
);
2844 if (err
|| resp
!= UPIU_TASK_MANAGEMENT_FUNC_COMPL
) {
2850 /* clear the commands that were pending for corresponding LUN */
2851 for_each_set_bit(pos
, &hba
->outstanding_reqs
, hba
->nutrs
) {
2852 if (hba
->lrb
[pos
].lun
== lrbp
->lun
) {
2853 err
= ufshcd_clear_cmd(hba
, pos
);
2858 spin_lock_irqsave(host
->host_lock
, flags
);
2859 ufshcd_transfer_req_compl(hba
);
2860 spin_unlock_irqrestore(host
->host_lock
, flags
);
2865 dev_err(hba
->dev
, "%s: failed with err %d\n", __func__
, err
);
2872 * ufshcd_abort - abort a specific command
2873 * @cmd: SCSI command pointer
2875 * Abort the pending command in device by sending UFS_ABORT_TASK task management
2876 * command, and in host controller by clearing the door-bell register. There can
2877 * be race between controller sending the command to the device while abort is
2878 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
2879 * really issued and then try to abort it.
2881 * Returns SUCCESS/FAILED
2883 static int ufshcd_abort(struct scsi_cmnd
*cmd
)
2885 struct Scsi_Host
*host
;
2886 struct ufs_hba
*hba
;
2887 unsigned long flags
;
2892 struct ufshcd_lrb
*lrbp
;
2895 host
= cmd
->device
->host
;
2896 hba
= shost_priv(host
);
2897 tag
= cmd
->request
->tag
;
2899 /* If command is already aborted/completed, return SUCCESS */
2900 if (!(test_bit(tag
, &hba
->outstanding_reqs
)))
2903 reg
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
2904 if (!(reg
& (1 << tag
))) {
2906 "%s: cmd was completed, but without a notifying intr, tag = %d",
2910 lrbp
= &hba
->lrb
[tag
];
2911 for (poll_cnt
= 100; poll_cnt
; poll_cnt
--) {
2912 err
= ufshcd_issue_tm_cmd(hba
, lrbp
->lun
, lrbp
->task_tag
,
2913 UFS_QUERY_TASK
, &resp
);
2914 if (!err
&& resp
== UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED
) {
2915 /* cmd pending in the device */
2917 } else if (!err
&& resp
== UPIU_TASK_MANAGEMENT_FUNC_COMPL
) {
2919 * cmd not pending in the device, check if it is
2922 reg
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
2923 if (reg
& (1 << tag
)) {
2924 /* sleep for max. 200us to stabilize */
2925 usleep_range(100, 200);
2928 /* command completed already */
2932 err
= resp
; /* service response error */
2942 err
= ufshcd_issue_tm_cmd(hba
, lrbp
->lun
, lrbp
->task_tag
,
2943 UFS_ABORT_TASK
, &resp
);
2944 if (err
|| resp
!= UPIU_TASK_MANAGEMENT_FUNC_COMPL
) {
2946 err
= resp
; /* service response error */
2950 err
= ufshcd_clear_cmd(hba
, tag
);
2954 scsi_dma_unmap(cmd
);
2956 spin_lock_irqsave(host
->host_lock
, flags
);
2957 __clear_bit(tag
, &hba
->outstanding_reqs
);
2958 hba
->lrb
[tag
].cmd
= NULL
;
2959 spin_unlock_irqrestore(host
->host_lock
, flags
);
2961 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
2962 wake_up(&hba
->dev_cmd
.tag_wq
);
2967 dev_err(hba
->dev
, "%s: failed with err %d\n", __func__
, err
);
2975 * ufshcd_host_reset_and_restore - reset and restore host controller
2976 * @hba: per-adapter instance
2978 * Note that host controller reset may issue DME_RESET to
2979 * local and remote (device) Uni-Pro stack and the attributes
2980 * are reset to default state.
2982 * Returns zero on success, non-zero on failure
2984 static int ufshcd_host_reset_and_restore(struct ufs_hba
*hba
)
2987 async_cookie_t cookie
;
2988 unsigned long flags
;
2990 /* Reset the host controller */
2991 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2992 ufshcd_hba_stop(hba
);
2993 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2995 err
= ufshcd_hba_enable(hba
);
2999 /* Establish the link again and restore the device */
3000 cookie
= async_schedule(ufshcd_async_scan
, hba
);
3001 /* wait for async scan to be completed */
3002 async_synchronize_cookie(++cookie
);
3003 if (hba
->ufshcd_state
!= UFSHCD_STATE_OPERATIONAL
)
3007 dev_err(hba
->dev
, "%s: Host init failed %d\n", __func__
, err
);
3013 * ufshcd_reset_and_restore - reset and re-initialize host/device
3014 * @hba: per-adapter instance
3016 * Reset and recover device, host and re-establish link. This
3017 * is helpful to recover the communication in fatal error conditions.
3019 * Returns zero on success, non-zero on failure
3021 static int ufshcd_reset_and_restore(struct ufs_hba
*hba
)
3024 unsigned long flags
;
3026 err
= ufshcd_host_reset_and_restore(hba
);
3029 * After reset the door-bell might be cleared, complete
3030 * outstanding requests in s/w here.
3032 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3033 ufshcd_transfer_req_compl(hba
);
3034 ufshcd_tmc_handler(hba
);
3035 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3041 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
3042 * @cmd - SCSI command pointer
3044 * Returns SUCCESS/FAILED
3046 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd
*cmd
)
3049 unsigned long flags
;
3050 struct ufs_hba
*hba
;
3052 hba
= shost_priv(cmd
->device
->host
);
3055 * Check if there is any race with fatal error handling.
3056 * If so, wait for it to complete. Even though fatal error
3057 * handling does reset and restore in some cases, don't assume
3058 * anything out of it. We are just avoiding race here.
3061 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3062 if (!(work_pending(&hba
->eh_work
) ||
3063 hba
->ufshcd_state
== UFSHCD_STATE_RESET
))
3065 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3066 dev_dbg(hba
->dev
, "%s: reset in progress\n", __func__
);
3067 flush_work(&hba
->eh_work
);
3070 hba
->ufshcd_state
= UFSHCD_STATE_RESET
;
3071 ufshcd_set_eh_in_progress(hba
);
3072 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3074 err
= ufshcd_reset_and_restore(hba
);
3076 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3079 hba
->ufshcd_state
= UFSHCD_STATE_OPERATIONAL
;
3082 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
3084 ufshcd_clear_eh_in_progress(hba
);
3085 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3091 * ufshcd_read_sdev_qdepth - read the lun command queue depth
3092 * @hba: Pointer to adapter instance
3093 * @sdev: pointer to SCSI device
3095 * Return in case of success the lun's queue depth else error.
3097 static int ufshcd_read_sdev_qdepth(struct ufs_hba
*hba
,
3098 struct scsi_device
*sdev
)
3101 int buff_len
= UNIT_DESC_MAX_SIZE
;
3102 u8 desc_buf
[UNIT_DESC_MAX_SIZE
];
3104 ret
= ufshcd_query_descriptor(hba
, UPIU_QUERY_OPCODE_READ_DESC
,
3105 QUERY_DESC_IDN_UNIT
, sdev
->lun
, 0, desc_buf
, &buff_len
);
3107 if (ret
|| (buff_len
< UNIT_DESC_PARAM_LU_Q_DEPTH
)) {
3109 "%s:Failed reading unit descriptor. len = %d ret = %d"
3110 , __func__
, buff_len
, ret
);
3117 ret
= desc_buf
[UNIT_DESC_PARAM_LU_Q_DEPTH
] & 0xFF;
3123 * ufshcd_async_scan - asynchronous execution for link startup
3124 * @data: data pointer to pass to this function
3125 * @cookie: cookie data
3127 static void ufshcd_async_scan(void *data
, async_cookie_t cookie
)
3129 struct ufs_hba
*hba
= (struct ufs_hba
*)data
;
3132 ret
= ufshcd_link_startup(hba
);
3136 ufshcd_config_max_pwr_mode(hba
);
3138 ret
= ufshcd_verify_dev_init(hba
);
3142 ret
= ufshcd_complete_dev_init(hba
);
3146 ufshcd_force_reset_auto_bkops(hba
);
3147 hba
->ufshcd_state
= UFSHCD_STATE_OPERATIONAL
;
3149 /* If we are in error handling context no need to scan the host */
3150 if (!ufshcd_eh_in_progress(hba
)) {
3151 scsi_scan_host(hba
->host
);
3152 pm_runtime_put_sync(hba
->dev
);
3158 static struct scsi_host_template ufshcd_driver_template
= {
3159 .module
= THIS_MODULE
,
3161 .proc_name
= UFSHCD
,
3162 .queuecommand
= ufshcd_queuecommand
,
3163 .slave_alloc
= ufshcd_slave_alloc
,
3164 .slave_configure
= ufshcd_slave_configure
,
3165 .slave_destroy
= ufshcd_slave_destroy
,
3166 .change_queue_depth
= ufshcd_change_queue_depth
,
3167 .eh_abort_handler
= ufshcd_abort
,
3168 .eh_device_reset_handler
= ufshcd_eh_device_reset_handler
,
3169 .eh_host_reset_handler
= ufshcd_eh_host_reset_handler
,
3171 .sg_tablesize
= SG_ALL
,
3172 .cmd_per_lun
= UFSHCD_CMD_PER_LUN
,
3173 .can_queue
= UFSHCD_CAN_QUEUE
,
3177 * ufshcd_suspend - suspend power management function
3178 * @hba: per adapter instance
3179 * @state: power state
3183 int ufshcd_suspend(struct ufs_hba
*hba
, pm_message_t state
)
3187 * 1. Block SCSI requests from SCSI midlayer
3188 * 2. Change the internal driver state to non operational
3189 * 3. Set UTRLRSR and UTMRLRSR bits to zero
3190 * 4. Wait until outstanding commands are completed
3191 * 5. Set HCE to zero to send the UFS host controller to reset state
3196 EXPORT_SYMBOL_GPL(ufshcd_suspend
);
3199 * ufshcd_resume - resume power management function
3200 * @hba: per adapter instance
3204 int ufshcd_resume(struct ufs_hba
*hba
)
3208 * 1. Set HCE to 1, to start the UFS host controller
3209 * initialization process
3210 * 2. Set UTRLRSR and UTMRLRSR bits to 1
3211 * 3. Change the internal driver state to operational
3212 * 4. Unblock SCSI requests from SCSI midlayer
3217 EXPORT_SYMBOL_GPL(ufshcd_resume
);
3219 int ufshcd_runtime_suspend(struct ufs_hba
*hba
)
3225 * The device is idle with no requests in the queue,
3226 * allow background operations.
3228 return ufshcd_enable_auto_bkops(hba
);
3230 EXPORT_SYMBOL(ufshcd_runtime_suspend
);
3232 int ufshcd_runtime_resume(struct ufs_hba
*hba
)
3237 return ufshcd_disable_auto_bkops(hba
);
3239 EXPORT_SYMBOL(ufshcd_runtime_resume
);
3241 int ufshcd_runtime_idle(struct ufs_hba
*hba
)
3245 EXPORT_SYMBOL(ufshcd_runtime_idle
);
3248 * ufshcd_remove - de-allocate SCSI host and host memory space
3249 * data structure memory
3250 * @hba - per adapter instance
3252 void ufshcd_remove(struct ufs_hba
*hba
)
3254 scsi_remove_host(hba
->host
);
3255 /* disable interrupts */
3256 ufshcd_disable_intr(hba
, hba
->intr_mask
);
3257 ufshcd_hba_stop(hba
);
3259 scsi_host_put(hba
->host
);
3261 EXPORT_SYMBOL_GPL(ufshcd_remove
);
3264 * ufshcd_set_dma_mask - Set dma mask based on the controller
3265 * addressing capability
3266 * @hba: per adapter instance
3268 * Returns 0 for success, non-zero for failure
3270 static int ufshcd_set_dma_mask(struct ufs_hba
*hba
)
3272 if (hba
->capabilities
& MASK_64_ADDRESSING_SUPPORT
) {
3273 if (!dma_set_mask_and_coherent(hba
->dev
, DMA_BIT_MASK(64)))
3276 return dma_set_mask_and_coherent(hba
->dev
, DMA_BIT_MASK(32));
3280 * ufshcd_init - Driver initialization routine
3281 * @dev: pointer to device handle
3282 * @hba_handle: driver private handle
3283 * @mmio_base: base register address
3284 * @irq: Interrupt line of device
3285 * Returns 0 on success, non-zero value on failure
3287 int ufshcd_init(struct device
*dev
, struct ufs_hba
**hba_handle
,
3288 void __iomem
*mmio_base
, unsigned int irq
)
3290 struct Scsi_Host
*host
;
3291 struct ufs_hba
*hba
;
3296 "Invalid memory reference for dev is NULL\n");
3303 "Invalid memory reference for mmio_base is NULL\n");
3308 host
= scsi_host_alloc(&ufshcd_driver_template
,
3309 sizeof(struct ufs_hba
));
3311 dev_err(dev
, "scsi_host_alloc failed\n");
3315 hba
= shost_priv(host
);
3318 hba
->mmio_base
= mmio_base
;
3321 /* Read capabilities registers */
3322 ufshcd_hba_capabilities(hba
);
3324 /* Get UFS version supported by the controller */
3325 hba
->ufs_version
= ufshcd_get_ufs_version(hba
);
3327 /* Get Interrupt bit mask per version */
3328 hba
->intr_mask
= ufshcd_get_intr_mask(hba
);
3330 err
= ufshcd_set_dma_mask(hba
);
3332 dev_err(hba
->dev
, "set dma mask failed\n");
3336 /* Allocate memory for host memory space */
3337 err
= ufshcd_memory_alloc(hba
);
3339 dev_err(hba
->dev
, "Memory allocation failed\n");
3344 ufshcd_host_memory_configure(hba
);
3346 host
->can_queue
= hba
->nutrs
;
3347 host
->cmd_per_lun
= hba
->nutrs
;
3348 host
->max_id
= UFSHCD_MAX_ID
;
3349 host
->max_lun
= UFSHCD_MAX_LUNS
;
3350 host
->max_channel
= UFSHCD_MAX_CHANNEL
;
3351 host
->unique_id
= host
->host_no
;
3352 host
->max_cmd_len
= MAX_CDB_SIZE
;
3354 /* Initailize wait queue for task management */
3355 init_waitqueue_head(&hba
->tm_wq
);
3356 init_waitqueue_head(&hba
->tm_tag_wq
);
3358 /* Initialize work queues */
3359 INIT_WORK(&hba
->eh_work
, ufshcd_err_handler
);
3360 INIT_WORK(&hba
->eeh_work
, ufshcd_exception_event_handler
);
3362 /* Initialize UIC command mutex */
3363 mutex_init(&hba
->uic_cmd_mutex
);
3365 /* Initialize mutex for device management commands */
3366 mutex_init(&hba
->dev_cmd
.lock
);
3368 /* Initialize device management tag acquire wait queue */
3369 init_waitqueue_head(&hba
->dev_cmd
.tag_wq
);
3371 /* IRQ registration */
3372 err
= devm_request_irq(dev
, irq
, ufshcd_intr
, IRQF_SHARED
, UFSHCD
, hba
);
3374 dev_err(hba
->dev
, "request irq failed\n");
3378 /* Enable SCSI tag mapping */
3379 err
= scsi_init_shared_tag_map(host
, host
->can_queue
);
3381 dev_err(hba
->dev
, "init shared queue failed\n");
3385 err
= scsi_add_host(host
, hba
->dev
);
3387 dev_err(hba
->dev
, "scsi_add_host failed\n");
3391 /* Host controller enable */
3392 err
= ufshcd_hba_enable(hba
);
3394 dev_err(hba
->dev
, "Host controller enable failed\n");
3395 goto out_remove_scsi_host
;
3400 /* Hold auto suspend until async scan completes */
3401 pm_runtime_get_sync(dev
);
3403 async_schedule(ufshcd_async_scan
, hba
);
3407 out_remove_scsi_host
:
3408 scsi_remove_host(hba
->host
);
3410 scsi_host_put(host
);
3414 EXPORT_SYMBOL_GPL(ufshcd_init
);
3416 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
3417 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
3418 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
3419 MODULE_LICENSE("GPL");
3420 MODULE_VERSION(UFSHCD_DRIVER_VERSION
);