2 * Universal Flash Storage Host controller driver Core
4 * This code is based on drivers/scsi/ufs/ufshcd.c
5 * Copyright (C) 2011-2013 Samsung India Software Operations
6 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
9 * Santosh Yaraganavi <santosh.sy@samsung.com>
10 * Vinayak Holikatti <h.vinayak@samsung.com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
16 * See the COPYING file in the top-level directory or visit
17 * <http://www.gnu.org/licenses/gpl-2.0.html>
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * This program is provided "AS IS" and "WITH ALL FAULTS" and
25 * without warranty of any kind. You are solely responsible for
26 * determining the appropriateness of using and distributing
27 * the program and assume all risks associated with your exercise
28 * of rights with respect to the program, including but not limited
29 * to infringement of third party rights, the risks and costs of
30 * program errors, damage to or loss of data, programs or equipment,
31 * and unavailability or interruption of operations. Under no
32 * circumstances will the contributor of this Program be liable for
33 * any damages of any kind arising from your use or distribution of
36 * The Linux Foundation chooses to take subject only to the GPLv2
37 * license terms, and distributes only under these terms.
40 #include <linux/async.h>
41 #include <linux/devfreq.h>
42 #include <linux/nls.h>
45 #include "ufs_quirks.h"
48 #define CREATE_TRACE_POINTS
49 #include <trace/events/ufs.h>
51 #define UFSHCD_REQ_SENSE_SIZE 18
53 #define UFSHCD_ENABLE_INTRS (UTP_TRANSFER_REQ_COMPL |\
56 /* UIC command timeout, unit: ms */
57 #define UIC_CMD_TIMEOUT 500
59 /* NOP OUT retries waiting for NOP IN response */
60 #define NOP_OUT_RETRIES 10
61 /* Timeout after 30 msecs if NOP OUT hangs without response */
62 #define NOP_OUT_TIMEOUT 30 /* msecs */
64 /* Query request retries */
65 #define QUERY_REQ_RETRIES 3
66 /* Query request timeout */
67 #define QUERY_REQ_TIMEOUT 1500 /* 1.5 seconds */
69 /* Task management command timeout */
70 #define TM_CMD_TIMEOUT 100 /* msecs */
72 /* maximum number of retries for a general UIC command */
73 #define UFS_UIC_COMMAND_RETRIES 3
75 /* maximum number of link-startup retries */
76 #define DME_LINKSTARTUP_RETRIES 3
78 /* Maximum retries for Hibern8 enter */
79 #define UIC_HIBERN8_ENTER_RETRIES 3
81 /* maximum number of reset retries before giving up */
82 #define MAX_HOST_RESET_RETRIES 5
84 /* Expose the flag value from utp_upiu_query.value */
85 #define MASK_QUERY_UPIU_FLAG_LOC 0xFF
87 /* Interrupt aggregation default timeout, unit: 40us */
88 #define INT_AGGR_DEF_TO 0x02
90 #define ufshcd_toggle_vreg(_dev, _vreg, _on) \
94 _ret = ufshcd_enable_vreg(_dev, _vreg); \
96 _ret = ufshcd_disable_vreg(_dev, _vreg); \
100 #define ufshcd_hex_dump(prefix_str, buf, len) \
101 print_hex_dump(KERN_ERR, prefix_str, DUMP_PREFIX_OFFSET, 16, 4, buf, len, false)
103 static u32 ufs_query_desc_max_size
[] = {
104 QUERY_DESC_DEVICE_MAX_SIZE
,
105 QUERY_DESC_CONFIGURAION_MAX_SIZE
,
106 QUERY_DESC_UNIT_MAX_SIZE
,
107 QUERY_DESC_RFU_MAX_SIZE
,
108 QUERY_DESC_INTERCONNECT_MAX_SIZE
,
109 QUERY_DESC_STRING_MAX_SIZE
,
110 QUERY_DESC_RFU_MAX_SIZE
,
111 QUERY_DESC_GEOMETRY_MAX_SIZE
,
112 QUERY_DESC_POWER_MAX_SIZE
,
113 QUERY_DESC_RFU_MAX_SIZE
,
117 UFSHCD_MAX_CHANNEL
= 0,
119 UFSHCD_CMD_PER_LUN
= 32,
120 UFSHCD_CAN_QUEUE
= 32,
127 UFSHCD_STATE_OPERATIONAL
,
128 UFSHCD_STATE_EH_SCHEDULED
,
131 /* UFSHCD error handling flags */
133 UFSHCD_EH_IN_PROGRESS
= (1 << 0),
136 /* UFSHCD UIC layer error flags */
138 UFSHCD_UIC_DL_PA_INIT_ERROR
= (1 << 0), /* Data link layer error */
139 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
= (1 << 1), /* Data link layer error */
140 UFSHCD_UIC_DL_TCx_REPLAY_ERROR
= (1 << 2), /* Data link layer error */
141 UFSHCD_UIC_NL_ERROR
= (1 << 3), /* Network layer error */
142 UFSHCD_UIC_TL_ERROR
= (1 << 4), /* Transport Layer error */
143 UFSHCD_UIC_DME_ERROR
= (1 << 5), /* DME error */
146 /* Interrupt configuration options */
153 #define ufshcd_set_eh_in_progress(h) \
154 (h->eh_flags |= UFSHCD_EH_IN_PROGRESS)
155 #define ufshcd_eh_in_progress(h) \
156 (h->eh_flags & UFSHCD_EH_IN_PROGRESS)
157 #define ufshcd_clear_eh_in_progress(h) \
158 (h->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
160 #define ufshcd_set_ufs_dev_active(h) \
161 ((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)
162 #define ufshcd_set_ufs_dev_sleep(h) \
163 ((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)
164 #define ufshcd_set_ufs_dev_poweroff(h) \
165 ((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)
166 #define ufshcd_is_ufs_dev_active(h) \
167 ((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)
168 #define ufshcd_is_ufs_dev_sleep(h) \
169 ((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)
170 #define ufshcd_is_ufs_dev_poweroff(h) \
171 ((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)
173 static struct ufs_pm_lvl_states ufs_pm_lvl_states
[] = {
174 {UFS_ACTIVE_PWR_MODE
, UIC_LINK_ACTIVE_STATE
},
175 {UFS_ACTIVE_PWR_MODE
, UIC_LINK_HIBERN8_STATE
},
176 {UFS_SLEEP_PWR_MODE
, UIC_LINK_ACTIVE_STATE
},
177 {UFS_SLEEP_PWR_MODE
, UIC_LINK_HIBERN8_STATE
},
178 {UFS_POWERDOWN_PWR_MODE
, UIC_LINK_HIBERN8_STATE
},
179 {UFS_POWERDOWN_PWR_MODE
, UIC_LINK_OFF_STATE
},
182 static inline enum ufs_dev_pwr_mode
183 ufs_get_pm_lvl_to_dev_pwr_mode(enum ufs_pm_level lvl
)
185 return ufs_pm_lvl_states
[lvl
].dev_state
;
188 static inline enum uic_link_state
189 ufs_get_pm_lvl_to_link_pwr_state(enum ufs_pm_level lvl
)
191 return ufs_pm_lvl_states
[lvl
].link_state
;
194 static inline enum ufs_pm_level
195 ufs_get_desired_pm_lvl_for_dev_link_state(enum ufs_dev_pwr_mode dev_state
,
196 enum uic_link_state link_state
)
198 enum ufs_pm_level lvl
;
200 for (lvl
= UFS_PM_LVL_0
; lvl
< UFS_PM_LVL_MAX
; lvl
++) {
201 if ((ufs_pm_lvl_states
[lvl
].dev_state
== dev_state
) &&
202 (ufs_pm_lvl_states
[lvl
].link_state
== link_state
))
206 /* if no match found, return the level 0 */
210 static struct ufs_dev_fix ufs_fixups
[] = {
211 /* UFS cards deviations table */
212 UFS_FIX(UFS_VENDOR_SAMSUNG
, UFS_ANY_MODEL
,
213 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM
),
214 UFS_FIX(UFS_VENDOR_SAMSUNG
, UFS_ANY_MODEL
, UFS_DEVICE_NO_VCCQ
),
215 UFS_FIX(UFS_VENDOR_SAMSUNG
, UFS_ANY_MODEL
,
216 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS
),
217 UFS_FIX(UFS_VENDOR_SAMSUNG
, UFS_ANY_MODEL
,
218 UFS_DEVICE_NO_FASTAUTO
),
219 UFS_FIX(UFS_VENDOR_SAMSUNG
, UFS_ANY_MODEL
,
220 UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE
),
221 UFS_FIX(UFS_VENDOR_TOSHIBA
, UFS_ANY_MODEL
,
222 UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM
),
223 UFS_FIX(UFS_VENDOR_TOSHIBA
, "THGLF2G9C8KBADG",
224 UFS_DEVICE_QUIRK_PA_TACTIVATE
),
225 UFS_FIX(UFS_VENDOR_TOSHIBA
, "THGLF2G9D8KBADG",
226 UFS_DEVICE_QUIRK_PA_TACTIVATE
),
227 UFS_FIX(UFS_VENDOR_SKHYNIX
, UFS_ANY_MODEL
, UFS_DEVICE_NO_VCCQ
),
228 UFS_FIX(UFS_VENDOR_SKHYNIX
, UFS_ANY_MODEL
,
229 UFS_DEVICE_QUIRK_HOST_PA_SAVECONFIGTIME
),
234 static void ufshcd_tmc_handler(struct ufs_hba
*hba
);
235 static void ufshcd_async_scan(void *data
, async_cookie_t cookie
);
236 static int ufshcd_reset_and_restore(struct ufs_hba
*hba
);
237 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd
*cmd
);
238 static int ufshcd_clear_tm_cmd(struct ufs_hba
*hba
, int tag
);
239 static void ufshcd_hba_exit(struct ufs_hba
*hba
);
240 static int ufshcd_probe_hba(struct ufs_hba
*hba
);
241 static int __ufshcd_setup_clocks(struct ufs_hba
*hba
, bool on
,
243 static int ufshcd_setup_clocks(struct ufs_hba
*hba
, bool on
);
244 static int ufshcd_set_vccq_rail_unused(struct ufs_hba
*hba
, bool unused
);
245 static int ufshcd_uic_hibern8_exit(struct ufs_hba
*hba
);
246 static int ufshcd_uic_hibern8_enter(struct ufs_hba
*hba
);
247 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba
*hba
);
248 static int ufshcd_host_reset_and_restore(struct ufs_hba
*hba
);
249 static void ufshcd_resume_clkscaling(struct ufs_hba
*hba
);
250 static void ufshcd_suspend_clkscaling(struct ufs_hba
*hba
);
251 static void __ufshcd_suspend_clkscaling(struct ufs_hba
*hba
);
252 static int ufshcd_scale_clks(struct ufs_hba
*hba
, bool scale_up
);
253 static irqreturn_t
ufshcd_intr(int irq
, void *__hba
);
254 static int ufshcd_config_pwr_mode(struct ufs_hba
*hba
,
255 struct ufs_pa_layer_attr
*desired_pwr_mode
);
256 static int ufshcd_change_power_mode(struct ufs_hba
*hba
,
257 struct ufs_pa_layer_attr
*pwr_mode
);
258 static inline bool ufshcd_valid_tag(struct ufs_hba
*hba
, int tag
)
260 return tag
>= 0 && tag
< hba
->nutrs
;
263 static inline int ufshcd_enable_irq(struct ufs_hba
*hba
)
267 if (!hba
->is_irq_enabled
) {
268 ret
= request_irq(hba
->irq
, ufshcd_intr
, IRQF_SHARED
, UFSHCD
,
271 dev_err(hba
->dev
, "%s: request_irq failed, ret=%d\n",
273 hba
->is_irq_enabled
= true;
279 static inline void ufshcd_disable_irq(struct ufs_hba
*hba
)
281 if (hba
->is_irq_enabled
) {
282 free_irq(hba
->irq
, hba
);
283 hba
->is_irq_enabled
= false;
287 /* replace non-printable or non-ASCII characters with spaces */
288 static inline void ufshcd_remove_non_printable(char *val
)
293 if (*val
< 0x20 || *val
> 0x7e)
297 static void ufshcd_add_command_trace(struct ufs_hba
*hba
,
298 unsigned int tag
, const char *str
)
303 struct ufshcd_lrb
*lrbp
;
304 int transfer_len
= -1;
306 if (!trace_ufshcd_command_enabled())
309 lrbp
= &hba
->lrb
[tag
];
311 if (lrbp
->cmd
) { /* data phase exists */
312 opcode
= (u8
)(*lrbp
->cmd
->cmnd
);
313 if ((opcode
== READ_10
) || (opcode
== WRITE_10
)) {
315 * Currently we only fully trace read(10) and write(10)
318 if (lrbp
->cmd
->request
&& lrbp
->cmd
->request
->bio
)
320 lrbp
->cmd
->request
->bio
->bi_iter
.bi_sector
;
321 transfer_len
= be32_to_cpu(
322 lrbp
->ucd_req_ptr
->sc
.exp_data_transfer_len
);
326 intr
= ufshcd_readl(hba
, REG_INTERRUPT_STATUS
);
327 doorbell
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
328 trace_ufshcd_command(dev_name(hba
->dev
), str
, tag
,
329 doorbell
, transfer_len
, intr
, lba
, opcode
);
332 static void ufshcd_print_clk_freqs(struct ufs_hba
*hba
)
334 struct ufs_clk_info
*clki
;
335 struct list_head
*head
= &hba
->clk_list_head
;
337 if (!head
|| list_empty(head
))
340 list_for_each_entry(clki
, head
, list
) {
341 if (!IS_ERR_OR_NULL(clki
->clk
) && clki
->min_freq
&&
343 dev_err(hba
->dev
, "clk: %s, rate: %u\n",
344 clki
->name
, clki
->curr_freq
);
348 static void ufshcd_print_uic_err_hist(struct ufs_hba
*hba
,
349 struct ufs_uic_err_reg_hist
*err_hist
, char *err_name
)
353 for (i
= 0; i
< UIC_ERR_REG_HIST_LENGTH
; i
++) {
354 int p
= (i
+ err_hist
->pos
- 1) % UIC_ERR_REG_HIST_LENGTH
;
356 if (err_hist
->reg
[p
] == 0)
358 dev_err(hba
->dev
, "%s[%d] = 0x%x at %lld us\n", err_name
, i
,
359 err_hist
->reg
[p
], ktime_to_us(err_hist
->tstamp
[p
]));
363 static void ufshcd_print_host_regs(struct ufs_hba
*hba
)
366 * hex_dump reads its data without the readl macro. This might
367 * cause inconsistency issues on some platform, as the printed
368 * values may be from cache and not the most recent value.
369 * To know whether you are looking at an un-cached version verify
370 * that IORESOURCE_MEM flag is on when xxx_get_resource() is invoked
371 * during platform/pci probe function.
373 ufshcd_hex_dump("host regs: ", hba
->mmio_base
, UFSHCI_REG_SPACE_SIZE
);
374 dev_err(hba
->dev
, "hba->ufs_version = 0x%x, hba->capabilities = 0x%x\n",
375 hba
->ufs_version
, hba
->capabilities
);
377 "hba->outstanding_reqs = 0x%x, hba->outstanding_tasks = 0x%x\n",
378 (u32
)hba
->outstanding_reqs
, (u32
)hba
->outstanding_tasks
);
380 "last_hibern8_exit_tstamp at %lld us, hibern8_exit_cnt = %d\n",
381 ktime_to_us(hba
->ufs_stats
.last_hibern8_exit_tstamp
),
382 hba
->ufs_stats
.hibern8_exit_cnt
);
384 ufshcd_print_uic_err_hist(hba
, &hba
->ufs_stats
.pa_err
, "pa_err");
385 ufshcd_print_uic_err_hist(hba
, &hba
->ufs_stats
.dl_err
, "dl_err");
386 ufshcd_print_uic_err_hist(hba
, &hba
->ufs_stats
.nl_err
, "nl_err");
387 ufshcd_print_uic_err_hist(hba
, &hba
->ufs_stats
.tl_err
, "tl_err");
388 ufshcd_print_uic_err_hist(hba
, &hba
->ufs_stats
.dme_err
, "dme_err");
390 ufshcd_print_clk_freqs(hba
);
392 if (hba
->vops
&& hba
->vops
->dbg_register_dump
)
393 hba
->vops
->dbg_register_dump(hba
);
397 void ufshcd_print_trs(struct ufs_hba
*hba
, unsigned long bitmap
, bool pr_prdt
)
399 struct ufshcd_lrb
*lrbp
;
403 for_each_set_bit(tag
, &bitmap
, hba
->nutrs
) {
404 lrbp
= &hba
->lrb
[tag
];
406 dev_err(hba
->dev
, "UPIU[%d] - issue time %lld us\n",
407 tag
, ktime_to_us(lrbp
->issue_time_stamp
));
409 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n",
410 tag
, (u64
)lrbp
->utrd_dma_addr
);
412 ufshcd_hex_dump("UPIU TRD: ", lrbp
->utr_descriptor_ptr
,
413 sizeof(struct utp_transfer_req_desc
));
414 dev_err(hba
->dev
, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag
,
415 (u64
)lrbp
->ucd_req_dma_addr
);
416 ufshcd_hex_dump("UPIU REQ: ", lrbp
->ucd_req_ptr
,
417 sizeof(struct utp_upiu_req
));
418 dev_err(hba
->dev
, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag
,
419 (u64
)lrbp
->ucd_rsp_dma_addr
);
420 ufshcd_hex_dump("UPIU RSP: ", lrbp
->ucd_rsp_ptr
,
421 sizeof(struct utp_upiu_rsp
));
423 prdt_length
= le16_to_cpu(
424 lrbp
->utr_descriptor_ptr
->prd_table_length
);
426 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n",
428 (u64
)lrbp
->ucd_prdt_dma_addr
);
431 ufshcd_hex_dump("UPIU PRDT: ", lrbp
->ucd_prdt_ptr
,
432 sizeof(struct ufshcd_sg_entry
) * prdt_length
);
436 static void ufshcd_print_tmrs(struct ufs_hba
*hba
, unsigned long bitmap
)
438 struct utp_task_req_desc
*tmrdp
;
441 for_each_set_bit(tag
, &bitmap
, hba
->nutmrs
) {
442 tmrdp
= &hba
->utmrdl_base_addr
[tag
];
443 dev_err(hba
->dev
, "TM[%d] - Task Management Header\n", tag
);
444 ufshcd_hex_dump("TM TRD: ", &tmrdp
->header
,
445 sizeof(struct request_desc_header
));
446 dev_err(hba
->dev
, "TM[%d] - Task Management Request UPIU\n",
448 ufshcd_hex_dump("TM REQ: ", tmrdp
->task_req_upiu
,
449 sizeof(struct utp_upiu_req
));
450 dev_err(hba
->dev
, "TM[%d] - Task Management Response UPIU\n",
452 ufshcd_hex_dump("TM RSP: ", tmrdp
->task_rsp_upiu
,
453 sizeof(struct utp_task_req_desc
));
457 static void ufshcd_print_host_state(struct ufs_hba
*hba
)
459 dev_err(hba
->dev
, "UFS Host state=%d\n", hba
->ufshcd_state
);
460 dev_err(hba
->dev
, "lrb in use=0x%lx, outstanding reqs=0x%lx tasks=0x%lx\n",
461 hba
->lrb_in_use
, hba
->outstanding_tasks
, hba
->outstanding_reqs
);
462 dev_err(hba
->dev
, "saved_err=0x%x, saved_uic_err=0x%x\n",
463 hba
->saved_err
, hba
->saved_uic_err
);
464 dev_err(hba
->dev
, "Device power mode=%d, UIC link state=%d\n",
465 hba
->curr_dev_pwr_mode
, hba
->uic_link_state
);
466 dev_err(hba
->dev
, "PM in progress=%d, sys. suspended=%d\n",
467 hba
->pm_op_in_progress
, hba
->is_sys_suspended
);
468 dev_err(hba
->dev
, "Auto BKOPS=%d, Host self-block=%d\n",
469 hba
->auto_bkops_enabled
, hba
->host
->host_self_blocked
);
470 dev_err(hba
->dev
, "Clk gate=%d\n", hba
->clk_gating
.state
);
471 dev_err(hba
->dev
, "error handling flags=0x%x, req. abort count=%d\n",
472 hba
->eh_flags
, hba
->req_abort_count
);
473 dev_err(hba
->dev
, "Host capabilities=0x%x, caps=0x%x\n",
474 hba
->capabilities
, hba
->caps
);
475 dev_err(hba
->dev
, "quirks=0x%x, dev. quirks=0x%x\n", hba
->quirks
,
480 * ufshcd_print_pwr_info - print power params as saved in hba
482 * @hba: per-adapter instance
484 static void ufshcd_print_pwr_info(struct ufs_hba
*hba
)
486 static const char * const names
[] = {
496 dev_err(hba
->dev
, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n",
498 hba
->pwr_info
.gear_rx
, hba
->pwr_info
.gear_tx
,
499 hba
->pwr_info
.lane_rx
, hba
->pwr_info
.lane_tx
,
500 names
[hba
->pwr_info
.pwr_rx
],
501 names
[hba
->pwr_info
.pwr_tx
],
502 hba
->pwr_info
.hs_rate
);
506 * ufshcd_wait_for_register - wait for register value to change
507 * @hba - per-adapter interface
508 * @reg - mmio register offset
509 * @mask - mask to apply to read register value
510 * @val - wait condition
511 * @interval_us - polling interval in microsecs
512 * @timeout_ms - timeout in millisecs
513 * @can_sleep - perform sleep or just spin
515 * Returns -ETIMEDOUT on error, zero on success
517 int ufshcd_wait_for_register(struct ufs_hba
*hba
, u32 reg
, u32 mask
,
518 u32 val
, unsigned long interval_us
,
519 unsigned long timeout_ms
, bool can_sleep
)
522 unsigned long timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
524 /* ignore bits that we don't intend to wait on */
527 while ((ufshcd_readl(hba
, reg
) & mask
) != val
) {
529 usleep_range(interval_us
, interval_us
+ 50);
532 if (time_after(jiffies
, timeout
)) {
533 if ((ufshcd_readl(hba
, reg
) & mask
) != val
)
543 * ufshcd_get_intr_mask - Get the interrupt bit mask
544 * @hba - Pointer to adapter instance
546 * Returns interrupt bit mask per version
548 static inline u32
ufshcd_get_intr_mask(struct ufs_hba
*hba
)
552 switch (hba
->ufs_version
) {
553 case UFSHCI_VERSION_10
:
554 intr_mask
= INTERRUPT_MASK_ALL_VER_10
;
556 /* allow fall through */
557 case UFSHCI_VERSION_11
:
558 case UFSHCI_VERSION_20
:
559 intr_mask
= INTERRUPT_MASK_ALL_VER_11
;
561 /* allow fall through */
562 case UFSHCI_VERSION_21
:
564 intr_mask
= INTERRUPT_MASK_ALL_VER_21
;
571 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
572 * @hba - Pointer to adapter instance
574 * Returns UFSHCI version supported by the controller
576 static inline u32
ufshcd_get_ufs_version(struct ufs_hba
*hba
)
578 if (hba
->quirks
& UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION
)
579 return ufshcd_vops_get_ufs_hci_version(hba
);
581 return ufshcd_readl(hba
, REG_UFS_VERSION
);
585 * ufshcd_is_device_present - Check if any device connected to
586 * the host controller
587 * @hba: pointer to adapter instance
589 * Returns 1 if device present, 0 if no device detected
591 static inline int ufshcd_is_device_present(struct ufs_hba
*hba
)
593 return (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) &
594 DEVICE_PRESENT
) ? 1 : 0;
598 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
599 * @lrb: pointer to local command reference block
601 * This function is used to get the OCS field from UTRD
602 * Returns the OCS field in the UTRD
604 static inline int ufshcd_get_tr_ocs(struct ufshcd_lrb
*lrbp
)
606 return le32_to_cpu(lrbp
->utr_descriptor_ptr
->header
.dword_2
) & MASK_OCS
;
610 * ufshcd_get_tmr_ocs - Get the UTMRD Overall Command Status
611 * @task_req_descp: pointer to utp_task_req_desc structure
613 * This function is used to get the OCS field from UTMRD
614 * Returns the OCS field in the UTMRD
617 ufshcd_get_tmr_ocs(struct utp_task_req_desc
*task_req_descp
)
619 return le32_to_cpu(task_req_descp
->header
.dword_2
) & MASK_OCS
;
623 * ufshcd_get_tm_free_slot - get a free slot for task management request
624 * @hba: per adapter instance
625 * @free_slot: pointer to variable with available slot value
627 * Get a free tag and lock it until ufshcd_put_tm_slot() is called.
628 * Returns 0 if free slot is not available, else return 1 with tag value
631 static bool ufshcd_get_tm_free_slot(struct ufs_hba
*hba
, int *free_slot
)
640 tag
= find_first_zero_bit(&hba
->tm_slots_in_use
, hba
->nutmrs
);
641 if (tag
>= hba
->nutmrs
)
643 } while (test_and_set_bit_lock(tag
, &hba
->tm_slots_in_use
));
651 static inline void ufshcd_put_tm_slot(struct ufs_hba
*hba
, int slot
)
653 clear_bit_unlock(slot
, &hba
->tm_slots_in_use
);
657 * ufshcd_utrl_clear - Clear a bit in UTRLCLR register
658 * @hba: per adapter instance
659 * @pos: position of the bit to be cleared
661 static inline void ufshcd_utrl_clear(struct ufs_hba
*hba
, u32 pos
)
663 ufshcd_writel(hba
, ~(1 << pos
), REG_UTP_TRANSFER_REQ_LIST_CLEAR
);
667 * ufshcd_outstanding_req_clear - Clear a bit in outstanding request field
668 * @hba: per adapter instance
669 * @tag: position of the bit to be cleared
671 static inline void ufshcd_outstanding_req_clear(struct ufs_hba
*hba
, int tag
)
673 __clear_bit(tag
, &hba
->outstanding_reqs
);
677 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
678 * @reg: Register value of host controller status
680 * Returns integer, 0 on Success and positive value if failed
682 static inline int ufshcd_get_lists_status(u32 reg
)
685 * The mask 0xFF is for the following HCS register bits
693 return ((reg
& 0xFF) >> 1) ^ 0x07;
697 * ufshcd_get_uic_cmd_result - Get the UIC command result
698 * @hba: Pointer to adapter instance
700 * This function gets the result of UIC command completion
701 * Returns 0 on success, non zero value on error
703 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba
*hba
)
705 return ufshcd_readl(hba
, REG_UIC_COMMAND_ARG_2
) &
706 MASK_UIC_COMMAND_RESULT
;
710 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
711 * @hba: Pointer to adapter instance
713 * This function gets UIC command argument3
714 * Returns 0 on success, non zero value on error
716 static inline u32
ufshcd_get_dme_attr_val(struct ufs_hba
*hba
)
718 return ufshcd_readl(hba
, REG_UIC_COMMAND_ARG_3
);
722 * ufshcd_get_req_rsp - returns the TR response transaction type
723 * @ucd_rsp_ptr: pointer to response UPIU
726 ufshcd_get_req_rsp(struct utp_upiu_rsp
*ucd_rsp_ptr
)
728 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_0
) >> 24;
732 * ufshcd_get_rsp_upiu_result - Get the result from response UPIU
733 * @ucd_rsp_ptr: pointer to response UPIU
735 * This function gets the response status and scsi_status from response UPIU
736 * Returns the response result code.
739 ufshcd_get_rsp_upiu_result(struct utp_upiu_rsp
*ucd_rsp_ptr
)
741 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_1
) & MASK_RSP_UPIU_RESULT
;
745 * ufshcd_get_rsp_upiu_data_seg_len - Get the data segment length
747 * @ucd_rsp_ptr: pointer to response UPIU
749 * Return the data segment length.
751 static inline unsigned int
752 ufshcd_get_rsp_upiu_data_seg_len(struct utp_upiu_rsp
*ucd_rsp_ptr
)
754 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_2
) &
755 MASK_RSP_UPIU_DATA_SEG_LEN
;
759 * ufshcd_is_exception_event - Check if the device raised an exception event
760 * @ucd_rsp_ptr: pointer to response UPIU
762 * The function checks if the device raised an exception event indicated in
763 * the Device Information field of response UPIU.
765 * Returns true if exception is raised, false otherwise.
767 static inline bool ufshcd_is_exception_event(struct utp_upiu_rsp
*ucd_rsp_ptr
)
769 return be32_to_cpu(ucd_rsp_ptr
->header
.dword_2
) &
770 MASK_RSP_EXCEPTION_EVENT
? true : false;
774 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
775 * @hba: per adapter instance
778 ufshcd_reset_intr_aggr(struct ufs_hba
*hba
)
780 ufshcd_writel(hba
, INT_AGGR_ENABLE
|
781 INT_AGGR_COUNTER_AND_TIMER_RESET
,
782 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
786 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
787 * @hba: per adapter instance
788 * @cnt: Interrupt aggregation counter threshold
789 * @tmout: Interrupt aggregation timeout value
792 ufshcd_config_intr_aggr(struct ufs_hba
*hba
, u8 cnt
, u8 tmout
)
794 ufshcd_writel(hba
, INT_AGGR_ENABLE
| INT_AGGR_PARAM_WRITE
|
795 INT_AGGR_COUNTER_THLD_VAL(cnt
) |
796 INT_AGGR_TIMEOUT_VAL(tmout
),
797 REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
801 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
802 * @hba: per adapter instance
804 static inline void ufshcd_disable_intr_aggr(struct ufs_hba
*hba
)
806 ufshcd_writel(hba
, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL
);
810 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
811 * When run-stop registers are set to 1, it indicates the
812 * host controller that it can process the requests
813 * @hba: per adapter instance
815 static void ufshcd_enable_run_stop_reg(struct ufs_hba
*hba
)
817 ufshcd_writel(hba
, UTP_TASK_REQ_LIST_RUN_STOP_BIT
,
818 REG_UTP_TASK_REQ_LIST_RUN_STOP
);
819 ufshcd_writel(hba
, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT
,
820 REG_UTP_TRANSFER_REQ_LIST_RUN_STOP
);
824 * ufshcd_hba_start - Start controller initialization sequence
825 * @hba: per adapter instance
827 static inline void ufshcd_hba_start(struct ufs_hba
*hba
)
829 ufshcd_writel(hba
, CONTROLLER_ENABLE
, REG_CONTROLLER_ENABLE
);
833 * ufshcd_is_hba_active - Get controller state
834 * @hba: per adapter instance
836 * Returns zero if controller is active, 1 otherwise
838 static inline int ufshcd_is_hba_active(struct ufs_hba
*hba
)
840 return (ufshcd_readl(hba
, REG_CONTROLLER_ENABLE
) & 0x1) ? 0 : 1;
843 static const char *ufschd_uic_link_state_to_string(
844 enum uic_link_state state
)
847 case UIC_LINK_OFF_STATE
: return "OFF";
848 case UIC_LINK_ACTIVE_STATE
: return "ACTIVE";
849 case UIC_LINK_HIBERN8_STATE
: return "HIBERN8";
850 default: return "UNKNOWN";
854 static const char *ufschd_ufs_dev_pwr_mode_to_string(
855 enum ufs_dev_pwr_mode state
)
858 case UFS_ACTIVE_PWR_MODE
: return "ACTIVE";
859 case UFS_SLEEP_PWR_MODE
: return "SLEEP";
860 case UFS_POWERDOWN_PWR_MODE
: return "POWERDOWN";
861 default: return "UNKNOWN";
865 u32
ufshcd_get_local_unipro_ver(struct ufs_hba
*hba
)
867 /* HCI version 1.0 and 1.1 supports UniPro 1.41 */
868 if ((hba
->ufs_version
== UFSHCI_VERSION_10
) ||
869 (hba
->ufs_version
== UFSHCI_VERSION_11
))
870 return UFS_UNIPRO_VER_1_41
;
872 return UFS_UNIPRO_VER_1_6
;
874 EXPORT_SYMBOL(ufshcd_get_local_unipro_ver
);
876 static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba
*hba
)
879 * If both host and device support UniPro ver1.6 or later, PA layer
880 * parameters tuning happens during link startup itself.
882 * We can manually tune PA layer parameters if either host or device
883 * doesn't support UniPro ver 1.6 or later. But to keep manual tuning
884 * logic simple, we will only do manual tuning if local unipro version
885 * doesn't support ver1.6 or later.
887 if (ufshcd_get_local_unipro_ver(hba
) < UFS_UNIPRO_VER_1_6
)
893 static int ufshcd_scale_clks(struct ufs_hba
*hba
, bool scale_up
)
896 struct ufs_clk_info
*clki
;
897 struct list_head
*head
= &hba
->clk_list_head
;
898 ktime_t start
= ktime_get();
899 bool clk_state_changed
= false;
901 if (!head
|| list_empty(head
))
904 ret
= ufshcd_vops_clk_scale_notify(hba
, scale_up
, PRE_CHANGE
);
908 list_for_each_entry(clki
, head
, list
) {
909 if (!IS_ERR_OR_NULL(clki
->clk
)) {
910 if (scale_up
&& clki
->max_freq
) {
911 if (clki
->curr_freq
== clki
->max_freq
)
914 clk_state_changed
= true;
915 ret
= clk_set_rate(clki
->clk
, clki
->max_freq
);
917 dev_err(hba
->dev
, "%s: %s clk set rate(%dHz) failed, %d\n",
918 __func__
, clki
->name
,
919 clki
->max_freq
, ret
);
922 trace_ufshcd_clk_scaling(dev_name(hba
->dev
),
923 "scaled up", clki
->name
,
927 clki
->curr_freq
= clki
->max_freq
;
929 } else if (!scale_up
&& clki
->min_freq
) {
930 if (clki
->curr_freq
== clki
->min_freq
)
933 clk_state_changed
= true;
934 ret
= clk_set_rate(clki
->clk
, clki
->min_freq
);
936 dev_err(hba
->dev
, "%s: %s clk set rate(%dHz) failed, %d\n",
937 __func__
, clki
->name
,
938 clki
->min_freq
, ret
);
941 trace_ufshcd_clk_scaling(dev_name(hba
->dev
),
942 "scaled down", clki
->name
,
945 clki
->curr_freq
= clki
->min_freq
;
948 dev_dbg(hba
->dev
, "%s: clk: %s, rate: %lu\n", __func__
,
949 clki
->name
, clk_get_rate(clki
->clk
));
952 ret
= ufshcd_vops_clk_scale_notify(hba
, scale_up
, POST_CHANGE
);
955 if (clk_state_changed
)
956 trace_ufshcd_profile_clk_scaling(dev_name(hba
->dev
),
957 (scale_up
? "up" : "down"),
958 ktime_to_us(ktime_sub(ktime_get(), start
)), ret
);
963 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
964 * @hba: per adapter instance
965 * @scale_up: True if scaling up and false if scaling down
967 * Returns true if scaling is required, false otherwise.
969 static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba
*hba
,
972 struct ufs_clk_info
*clki
;
973 struct list_head
*head
= &hba
->clk_list_head
;
975 if (!head
|| list_empty(head
))
978 list_for_each_entry(clki
, head
, list
) {
979 if (!IS_ERR_OR_NULL(clki
->clk
)) {
980 if (scale_up
&& clki
->max_freq
) {
981 if (clki
->curr_freq
== clki
->max_freq
)
984 } else if (!scale_up
&& clki
->min_freq
) {
985 if (clki
->curr_freq
== clki
->min_freq
)
995 static int ufshcd_wait_for_doorbell_clr(struct ufs_hba
*hba
,
1002 bool timeout
= false, do_last_check
= false;
1005 ufshcd_hold(hba
, false);
1006 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1008 * Wait for all the outstanding tasks/transfer requests.
1009 * Verify by checking the doorbell registers are clear.
1011 start
= ktime_get();
1013 if (hba
->ufshcd_state
!= UFSHCD_STATE_OPERATIONAL
) {
1018 tm_doorbell
= ufshcd_readl(hba
, REG_UTP_TASK_REQ_DOOR_BELL
);
1019 tr_doorbell
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
1020 if (!tm_doorbell
&& !tr_doorbell
) {
1023 } else if (do_last_check
) {
1027 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1029 if (ktime_to_us(ktime_sub(ktime_get(), start
)) >
1033 * We might have scheduled out for long time so make
1034 * sure to check if doorbells are cleared by this time
1037 do_last_check
= true;
1039 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1040 } while (tm_doorbell
|| tr_doorbell
);
1044 "%s: timedout waiting for doorbell to clear (tm=0x%x, tr=0x%x)\n",
1045 __func__
, tm_doorbell
, tr_doorbell
);
1049 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1050 ufshcd_release(hba
);
1055 * ufshcd_scale_gear - scale up/down UFS gear
1056 * @hba: per adapter instance
1057 * @scale_up: True for scaling up gear and false for scaling down
1059 * Returns 0 for success,
1060 * Returns -EBUSY if scaling can't happen at this time
1061 * Returns non-zero for any other errors
1063 static int ufshcd_scale_gear(struct ufs_hba
*hba
, bool scale_up
)
1065 #define UFS_MIN_GEAR_TO_SCALE_DOWN UFS_HS_G1
1067 struct ufs_pa_layer_attr new_pwr_info
;
1070 memcpy(&new_pwr_info
, &hba
->clk_scaling
.saved_pwr_info
.info
,
1071 sizeof(struct ufs_pa_layer_attr
));
1073 memcpy(&new_pwr_info
, &hba
->pwr_info
,
1074 sizeof(struct ufs_pa_layer_attr
));
1076 if (hba
->pwr_info
.gear_tx
> UFS_MIN_GEAR_TO_SCALE_DOWN
1077 || hba
->pwr_info
.gear_rx
> UFS_MIN_GEAR_TO_SCALE_DOWN
) {
1078 /* save the current power mode */
1079 memcpy(&hba
->clk_scaling
.saved_pwr_info
.info
,
1081 sizeof(struct ufs_pa_layer_attr
));
1083 /* scale down gear */
1084 new_pwr_info
.gear_tx
= UFS_MIN_GEAR_TO_SCALE_DOWN
;
1085 new_pwr_info
.gear_rx
= UFS_MIN_GEAR_TO_SCALE_DOWN
;
1089 /* check if the power mode needs to be changed or not? */
1090 ret
= ufshcd_change_power_mode(hba
, &new_pwr_info
);
1093 dev_err(hba
->dev
, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)",
1095 hba
->pwr_info
.gear_tx
, hba
->pwr_info
.gear_rx
,
1096 new_pwr_info
.gear_tx
, new_pwr_info
.gear_rx
);
1101 static int ufshcd_clock_scaling_prepare(struct ufs_hba
*hba
)
1103 #define DOORBELL_CLR_TOUT_US (1000 * 1000) /* 1 sec */
1106 * make sure that there are no outstanding requests when
1107 * clock scaling is in progress
1109 scsi_block_requests(hba
->host
);
1110 down_write(&hba
->clk_scaling_lock
);
1111 if (ufshcd_wait_for_doorbell_clr(hba
, DOORBELL_CLR_TOUT_US
)) {
1113 up_write(&hba
->clk_scaling_lock
);
1114 scsi_unblock_requests(hba
->host
);
1120 static void ufshcd_clock_scaling_unprepare(struct ufs_hba
*hba
)
1122 up_write(&hba
->clk_scaling_lock
);
1123 scsi_unblock_requests(hba
->host
);
1127 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1128 * @hba: per adapter instance
1129 * @scale_up: True for scaling up and false for scalin down
1131 * Returns 0 for success,
1132 * Returns -EBUSY if scaling can't happen at this time
1133 * Returns non-zero for any other errors
1135 static int ufshcd_devfreq_scale(struct ufs_hba
*hba
, bool scale_up
)
1139 /* let's not get into low power until clock scaling is completed */
1140 ufshcd_hold(hba
, false);
1142 ret
= ufshcd_clock_scaling_prepare(hba
);
1146 /* scale down the gear before scaling down clocks */
1148 ret
= ufshcd_scale_gear(hba
, false);
1153 ret
= ufshcd_scale_clks(hba
, scale_up
);
1156 ufshcd_scale_gear(hba
, true);
1160 /* scale up the gear after scaling up clocks */
1162 ret
= ufshcd_scale_gear(hba
, true);
1164 ufshcd_scale_clks(hba
, false);
1169 ret
= ufshcd_vops_clk_scale_notify(hba
, scale_up
, POST_CHANGE
);
1172 ufshcd_clock_scaling_unprepare(hba
);
1173 ufshcd_release(hba
);
1177 static void ufshcd_clk_scaling_suspend_work(struct work_struct
*work
)
1179 struct ufs_hba
*hba
= container_of(work
, struct ufs_hba
,
1180 clk_scaling
.suspend_work
);
1181 unsigned long irq_flags
;
1183 spin_lock_irqsave(hba
->host
->host_lock
, irq_flags
);
1184 if (hba
->clk_scaling
.active_reqs
|| hba
->clk_scaling
.is_suspended
) {
1185 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1188 hba
->clk_scaling
.is_suspended
= true;
1189 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1191 __ufshcd_suspend_clkscaling(hba
);
1194 static void ufshcd_clk_scaling_resume_work(struct work_struct
*work
)
1196 struct ufs_hba
*hba
= container_of(work
, struct ufs_hba
,
1197 clk_scaling
.resume_work
);
1198 unsigned long irq_flags
;
1200 spin_lock_irqsave(hba
->host
->host_lock
, irq_flags
);
1201 if (!hba
->clk_scaling
.is_suspended
) {
1202 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1205 hba
->clk_scaling
.is_suspended
= false;
1206 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1208 devfreq_resume_device(hba
->devfreq
);
1211 static int ufshcd_devfreq_target(struct device
*dev
,
1212 unsigned long *freq
, u32 flags
)
1215 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1217 bool scale_up
, sched_clk_scaling_suspend_work
= false;
1218 unsigned long irq_flags
;
1220 if (!ufshcd_is_clkscaling_supported(hba
))
1223 if ((*freq
> 0) && (*freq
< UINT_MAX
)) {
1224 dev_err(hba
->dev
, "%s: invalid freq = %lu\n", __func__
, *freq
);
1228 spin_lock_irqsave(hba
->host
->host_lock
, irq_flags
);
1229 if (ufshcd_eh_in_progress(hba
)) {
1230 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1234 if (!hba
->clk_scaling
.active_reqs
)
1235 sched_clk_scaling_suspend_work
= true;
1237 scale_up
= (*freq
== UINT_MAX
) ? true : false;
1238 if (!ufshcd_is_devfreq_scaling_required(hba
, scale_up
)) {
1239 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1241 goto out
; /* no state change required */
1243 spin_unlock_irqrestore(hba
->host
->host_lock
, irq_flags
);
1245 start
= ktime_get();
1246 ret
= ufshcd_devfreq_scale(hba
, scale_up
);
1248 trace_ufshcd_profile_clk_scaling(dev_name(hba
->dev
),
1249 (scale_up
? "up" : "down"),
1250 ktime_to_us(ktime_sub(ktime_get(), start
)), ret
);
1253 if (sched_clk_scaling_suspend_work
)
1254 queue_work(hba
->clk_scaling
.workq
,
1255 &hba
->clk_scaling
.suspend_work
);
1261 static int ufshcd_devfreq_get_dev_status(struct device
*dev
,
1262 struct devfreq_dev_status
*stat
)
1264 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1265 struct ufs_clk_scaling
*scaling
= &hba
->clk_scaling
;
1266 unsigned long flags
;
1268 if (!ufshcd_is_clkscaling_supported(hba
))
1271 memset(stat
, 0, sizeof(*stat
));
1273 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1274 if (!scaling
->window_start_t
)
1277 if (scaling
->is_busy_started
)
1278 scaling
->tot_busy_t
+= ktime_to_us(ktime_sub(ktime_get(),
1279 scaling
->busy_start_t
));
1281 stat
->total_time
= jiffies_to_usecs((long)jiffies
-
1282 (long)scaling
->window_start_t
);
1283 stat
->busy_time
= scaling
->tot_busy_t
;
1285 scaling
->window_start_t
= jiffies
;
1286 scaling
->tot_busy_t
= 0;
1288 if (hba
->outstanding_reqs
) {
1289 scaling
->busy_start_t
= ktime_get();
1290 scaling
->is_busy_started
= true;
1292 scaling
->busy_start_t
= 0;
1293 scaling
->is_busy_started
= false;
1295 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1299 static struct devfreq_dev_profile ufs_devfreq_profile
= {
1301 .target
= ufshcd_devfreq_target
,
1302 .get_dev_status
= ufshcd_devfreq_get_dev_status
,
1305 static void __ufshcd_suspend_clkscaling(struct ufs_hba
*hba
)
1307 unsigned long flags
;
1309 devfreq_suspend_device(hba
->devfreq
);
1310 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1311 hba
->clk_scaling
.window_start_t
= 0;
1312 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1315 static void ufshcd_suspend_clkscaling(struct ufs_hba
*hba
)
1317 unsigned long flags
;
1318 bool suspend
= false;
1320 if (!ufshcd_is_clkscaling_supported(hba
))
1323 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1324 if (!hba
->clk_scaling
.is_suspended
) {
1326 hba
->clk_scaling
.is_suspended
= true;
1328 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1331 __ufshcd_suspend_clkscaling(hba
);
1334 static void ufshcd_resume_clkscaling(struct ufs_hba
*hba
)
1336 unsigned long flags
;
1337 bool resume
= false;
1339 if (!ufshcd_is_clkscaling_supported(hba
))
1342 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1343 if (hba
->clk_scaling
.is_suspended
) {
1345 hba
->clk_scaling
.is_suspended
= false;
1347 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1350 devfreq_resume_device(hba
->devfreq
);
1353 static ssize_t
ufshcd_clkscale_enable_show(struct device
*dev
,
1354 struct device_attribute
*attr
, char *buf
)
1356 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1358 return snprintf(buf
, PAGE_SIZE
, "%d\n", hba
->clk_scaling
.is_allowed
);
1361 static ssize_t
ufshcd_clkscale_enable_store(struct device
*dev
,
1362 struct device_attribute
*attr
, const char *buf
, size_t count
)
1364 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1368 if (kstrtou32(buf
, 0, &value
))
1372 if (value
== hba
->clk_scaling
.is_allowed
)
1375 pm_runtime_get_sync(hba
->dev
);
1376 ufshcd_hold(hba
, false);
1378 cancel_work_sync(&hba
->clk_scaling
.suspend_work
);
1379 cancel_work_sync(&hba
->clk_scaling
.resume_work
);
1381 hba
->clk_scaling
.is_allowed
= value
;
1384 ufshcd_resume_clkscaling(hba
);
1386 ufshcd_suspend_clkscaling(hba
);
1387 err
= ufshcd_devfreq_scale(hba
, true);
1389 dev_err(hba
->dev
, "%s: failed to scale clocks up %d\n",
1393 ufshcd_release(hba
);
1394 pm_runtime_put_sync(hba
->dev
);
1399 static void ufshcd_clkscaling_init_sysfs(struct ufs_hba
*hba
)
1401 hba
->clk_scaling
.enable_attr
.show
= ufshcd_clkscale_enable_show
;
1402 hba
->clk_scaling
.enable_attr
.store
= ufshcd_clkscale_enable_store
;
1403 sysfs_attr_init(&hba
->clk_scaling
.enable_attr
.attr
);
1404 hba
->clk_scaling
.enable_attr
.attr
.name
= "clkscale_enable";
1405 hba
->clk_scaling
.enable_attr
.attr
.mode
= 0644;
1406 if (device_create_file(hba
->dev
, &hba
->clk_scaling
.enable_attr
))
1407 dev_err(hba
->dev
, "Failed to create sysfs for clkscale_enable\n");
1410 static void ufshcd_ungate_work(struct work_struct
*work
)
1413 unsigned long flags
;
1414 struct ufs_hba
*hba
= container_of(work
, struct ufs_hba
,
1415 clk_gating
.ungate_work
);
1417 cancel_delayed_work_sync(&hba
->clk_gating
.gate_work
);
1419 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1420 if (hba
->clk_gating
.state
== CLKS_ON
) {
1421 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1425 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1426 ufshcd_setup_clocks(hba
, true);
1428 /* Exit from hibern8 */
1429 if (ufshcd_can_hibern8_during_gating(hba
)) {
1430 /* Prevent gating in this path */
1431 hba
->clk_gating
.is_suspended
= true;
1432 if (ufshcd_is_link_hibern8(hba
)) {
1433 ret
= ufshcd_uic_hibern8_exit(hba
);
1435 dev_err(hba
->dev
, "%s: hibern8 exit failed %d\n",
1438 ufshcd_set_link_active(hba
);
1440 hba
->clk_gating
.is_suspended
= false;
1443 scsi_unblock_requests(hba
->host
);
1447 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1448 * Also, exit from hibern8 mode and set the link as active.
1449 * @hba: per adapter instance
1450 * @async: This indicates whether caller should ungate clocks asynchronously.
1452 int ufshcd_hold(struct ufs_hba
*hba
, bool async
)
1455 unsigned long flags
;
1457 if (!ufshcd_is_clkgating_allowed(hba
))
1459 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1460 hba
->clk_gating
.active_reqs
++;
1462 if (ufshcd_eh_in_progress(hba
)) {
1463 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1468 switch (hba
->clk_gating
.state
) {
1471 * Wait for the ungate work to complete if in progress.
1472 * Though the clocks may be in ON state, the link could
1473 * still be in hibner8 state if hibern8 is allowed
1474 * during clock gating.
1475 * Make sure we exit hibern8 state also in addition to
1478 if (ufshcd_can_hibern8_during_gating(hba
) &&
1479 ufshcd_is_link_hibern8(hba
)) {
1480 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1481 flush_work(&hba
->clk_gating
.ungate_work
);
1482 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1487 if (cancel_delayed_work(&hba
->clk_gating
.gate_work
)) {
1488 hba
->clk_gating
.state
= CLKS_ON
;
1489 trace_ufshcd_clk_gating(dev_name(hba
->dev
),
1490 hba
->clk_gating
.state
);
1494 * If we here, it means gating work is either done or
1495 * currently running. Hence, fall through to cancel gating
1496 * work and to enable clocks.
1499 scsi_block_requests(hba
->host
);
1500 hba
->clk_gating
.state
= REQ_CLKS_ON
;
1501 trace_ufshcd_clk_gating(dev_name(hba
->dev
),
1502 hba
->clk_gating
.state
);
1503 schedule_work(&hba
->clk_gating
.ungate_work
);
1505 * fall through to check if we should wait for this
1506 * work to be done or not.
1511 hba
->clk_gating
.active_reqs
--;
1515 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1516 flush_work(&hba
->clk_gating
.ungate_work
);
1517 /* Make sure state is CLKS_ON before returning */
1518 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1521 dev_err(hba
->dev
, "%s: clk gating is in invalid state %d\n",
1522 __func__
, hba
->clk_gating
.state
);
1525 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1529 EXPORT_SYMBOL_GPL(ufshcd_hold
);
1531 static void ufshcd_gate_work(struct work_struct
*work
)
1533 struct ufs_hba
*hba
= container_of(work
, struct ufs_hba
,
1534 clk_gating
.gate_work
.work
);
1535 unsigned long flags
;
1537 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1539 * In case you are here to cancel this work the gating state
1540 * would be marked as REQ_CLKS_ON. In this case save time by
1541 * skipping the gating work and exit after changing the clock
1544 if (hba
->clk_gating
.is_suspended
||
1545 (hba
->clk_gating
.state
== REQ_CLKS_ON
)) {
1546 hba
->clk_gating
.state
= CLKS_ON
;
1547 trace_ufshcd_clk_gating(dev_name(hba
->dev
),
1548 hba
->clk_gating
.state
);
1552 if (hba
->clk_gating
.active_reqs
1553 || hba
->ufshcd_state
!= UFSHCD_STATE_OPERATIONAL
1554 || hba
->lrb_in_use
|| hba
->outstanding_tasks
1555 || hba
->active_uic_cmd
|| hba
->uic_async_done
)
1558 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1560 /* put the link into hibern8 mode before turning off clocks */
1561 if (ufshcd_can_hibern8_during_gating(hba
)) {
1562 if (ufshcd_uic_hibern8_enter(hba
)) {
1563 hba
->clk_gating
.state
= CLKS_ON
;
1564 trace_ufshcd_clk_gating(dev_name(hba
->dev
),
1565 hba
->clk_gating
.state
);
1568 ufshcd_set_link_hibern8(hba
);
1571 if (!ufshcd_is_link_active(hba
))
1572 ufshcd_setup_clocks(hba
, false);
1574 /* If link is active, device ref_clk can't be switched off */
1575 __ufshcd_setup_clocks(hba
, false, true);
1578 * In case you are here to cancel this work the gating state
1579 * would be marked as REQ_CLKS_ON. In this case keep the state
1580 * as REQ_CLKS_ON which would anyway imply that clocks are off
1581 * and a request to turn them on is pending. By doing this way,
1582 * we keep the state machine in tact and this would ultimately
1583 * prevent from doing cancel work multiple times when there are
1584 * new requests arriving before the current cancel work is done.
1586 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1587 if (hba
->clk_gating
.state
== REQ_CLKS_OFF
) {
1588 hba
->clk_gating
.state
= CLKS_OFF
;
1589 trace_ufshcd_clk_gating(dev_name(hba
->dev
),
1590 hba
->clk_gating
.state
);
1593 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1598 /* host lock must be held before calling this variant */
1599 static void __ufshcd_release(struct ufs_hba
*hba
)
1601 if (!ufshcd_is_clkgating_allowed(hba
))
1604 hba
->clk_gating
.active_reqs
--;
1606 if (hba
->clk_gating
.active_reqs
|| hba
->clk_gating
.is_suspended
1607 || hba
->ufshcd_state
!= UFSHCD_STATE_OPERATIONAL
1608 || hba
->lrb_in_use
|| hba
->outstanding_tasks
1609 || hba
->active_uic_cmd
|| hba
->uic_async_done
1610 || ufshcd_eh_in_progress(hba
))
1613 hba
->clk_gating
.state
= REQ_CLKS_OFF
;
1614 trace_ufshcd_clk_gating(dev_name(hba
->dev
), hba
->clk_gating
.state
);
1615 schedule_delayed_work(&hba
->clk_gating
.gate_work
,
1616 msecs_to_jiffies(hba
->clk_gating
.delay_ms
));
1619 void ufshcd_release(struct ufs_hba
*hba
)
1621 unsigned long flags
;
1623 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1624 __ufshcd_release(hba
);
1625 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1627 EXPORT_SYMBOL_GPL(ufshcd_release
);
1629 static ssize_t
ufshcd_clkgate_delay_show(struct device
*dev
,
1630 struct device_attribute
*attr
, char *buf
)
1632 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1634 return snprintf(buf
, PAGE_SIZE
, "%lu\n", hba
->clk_gating
.delay_ms
);
1637 static ssize_t
ufshcd_clkgate_delay_store(struct device
*dev
,
1638 struct device_attribute
*attr
, const char *buf
, size_t count
)
1640 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1641 unsigned long flags
, value
;
1643 if (kstrtoul(buf
, 0, &value
))
1646 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1647 hba
->clk_gating
.delay_ms
= value
;
1648 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1652 static ssize_t
ufshcd_clkgate_enable_show(struct device
*dev
,
1653 struct device_attribute
*attr
, char *buf
)
1655 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1657 return snprintf(buf
, PAGE_SIZE
, "%d\n", hba
->clk_gating
.is_enabled
);
1660 static ssize_t
ufshcd_clkgate_enable_store(struct device
*dev
,
1661 struct device_attribute
*attr
, const char *buf
, size_t count
)
1663 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
1664 unsigned long flags
;
1667 if (kstrtou32(buf
, 0, &value
))
1671 if (value
== hba
->clk_gating
.is_enabled
)
1675 ufshcd_release(hba
);
1677 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1678 hba
->clk_gating
.active_reqs
++;
1679 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1682 hba
->clk_gating
.is_enabled
= value
;
1687 static void ufshcd_init_clk_gating(struct ufs_hba
*hba
)
1689 if (!ufshcd_is_clkgating_allowed(hba
))
1692 hba
->clk_gating
.delay_ms
= 150;
1693 INIT_DELAYED_WORK(&hba
->clk_gating
.gate_work
, ufshcd_gate_work
);
1694 INIT_WORK(&hba
->clk_gating
.ungate_work
, ufshcd_ungate_work
);
1696 hba
->clk_gating
.is_enabled
= true;
1698 hba
->clk_gating
.delay_attr
.show
= ufshcd_clkgate_delay_show
;
1699 hba
->clk_gating
.delay_attr
.store
= ufshcd_clkgate_delay_store
;
1700 sysfs_attr_init(&hba
->clk_gating
.delay_attr
.attr
);
1701 hba
->clk_gating
.delay_attr
.attr
.name
= "clkgate_delay_ms";
1702 hba
->clk_gating
.delay_attr
.attr
.mode
= 0644;
1703 if (device_create_file(hba
->dev
, &hba
->clk_gating
.delay_attr
))
1704 dev_err(hba
->dev
, "Failed to create sysfs for clkgate_delay\n");
1706 hba
->clk_gating
.enable_attr
.show
= ufshcd_clkgate_enable_show
;
1707 hba
->clk_gating
.enable_attr
.store
= ufshcd_clkgate_enable_store
;
1708 sysfs_attr_init(&hba
->clk_gating
.enable_attr
.attr
);
1709 hba
->clk_gating
.enable_attr
.attr
.name
= "clkgate_enable";
1710 hba
->clk_gating
.enable_attr
.attr
.mode
= 0644;
1711 if (device_create_file(hba
->dev
, &hba
->clk_gating
.enable_attr
))
1712 dev_err(hba
->dev
, "Failed to create sysfs for clkgate_enable\n");
1715 static void ufshcd_exit_clk_gating(struct ufs_hba
*hba
)
1717 if (!ufshcd_is_clkgating_allowed(hba
))
1719 device_remove_file(hba
->dev
, &hba
->clk_gating
.delay_attr
);
1720 device_remove_file(hba
->dev
, &hba
->clk_gating
.enable_attr
);
1721 cancel_work_sync(&hba
->clk_gating
.ungate_work
);
1722 cancel_delayed_work_sync(&hba
->clk_gating
.gate_work
);
1725 /* Must be called with host lock acquired */
1726 static void ufshcd_clk_scaling_start_busy(struct ufs_hba
*hba
)
1728 bool queue_resume_work
= false;
1730 if (!ufshcd_is_clkscaling_supported(hba
))
1733 if (!hba
->clk_scaling
.active_reqs
++)
1734 queue_resume_work
= true;
1736 if (!hba
->clk_scaling
.is_allowed
|| hba
->pm_op_in_progress
)
1739 if (queue_resume_work
)
1740 queue_work(hba
->clk_scaling
.workq
,
1741 &hba
->clk_scaling
.resume_work
);
1743 if (!hba
->clk_scaling
.window_start_t
) {
1744 hba
->clk_scaling
.window_start_t
= jiffies
;
1745 hba
->clk_scaling
.tot_busy_t
= 0;
1746 hba
->clk_scaling
.is_busy_started
= false;
1749 if (!hba
->clk_scaling
.is_busy_started
) {
1750 hba
->clk_scaling
.busy_start_t
= ktime_get();
1751 hba
->clk_scaling
.is_busy_started
= true;
1755 static void ufshcd_clk_scaling_update_busy(struct ufs_hba
*hba
)
1757 struct ufs_clk_scaling
*scaling
= &hba
->clk_scaling
;
1759 if (!ufshcd_is_clkscaling_supported(hba
))
1762 if (!hba
->outstanding_reqs
&& scaling
->is_busy_started
) {
1763 scaling
->tot_busy_t
+= ktime_to_us(ktime_sub(ktime_get(),
1764 scaling
->busy_start_t
));
1765 scaling
->busy_start_t
= 0;
1766 scaling
->is_busy_started
= false;
1770 * ufshcd_send_command - Send SCSI or device management commands
1771 * @hba: per adapter instance
1772 * @task_tag: Task tag of the command
1775 void ufshcd_send_command(struct ufs_hba
*hba
, unsigned int task_tag
)
1777 hba
->lrb
[task_tag
].issue_time_stamp
= ktime_get();
1778 ufshcd_clk_scaling_start_busy(hba
);
1779 __set_bit(task_tag
, &hba
->outstanding_reqs
);
1780 ufshcd_writel(hba
, 1 << task_tag
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
1781 /* Make sure that doorbell is committed immediately */
1783 ufshcd_add_command_trace(hba
, task_tag
, "send");
1787 * ufshcd_copy_sense_data - Copy sense data in case of check condition
1788 * @lrb - pointer to local reference block
1790 static inline void ufshcd_copy_sense_data(struct ufshcd_lrb
*lrbp
)
1793 if (lrbp
->sense_buffer
&&
1794 ufshcd_get_rsp_upiu_data_seg_len(lrbp
->ucd_rsp_ptr
)) {
1797 len
= be16_to_cpu(lrbp
->ucd_rsp_ptr
->sr
.sense_data_len
);
1798 len_to_copy
= min_t(int, RESPONSE_UPIU_SENSE_DATA_LENGTH
, len
);
1800 memcpy(lrbp
->sense_buffer
,
1801 lrbp
->ucd_rsp_ptr
->sr
.sense_data
,
1802 min_t(int, len_to_copy
, UFSHCD_REQ_SENSE_SIZE
));
1807 * ufshcd_copy_query_response() - Copy the Query Response and the data
1809 * @hba: per adapter instance
1810 * @lrb - pointer to local reference block
1813 int ufshcd_copy_query_response(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
1815 struct ufs_query_res
*query_res
= &hba
->dev_cmd
.query
.response
;
1817 memcpy(&query_res
->upiu_res
, &lrbp
->ucd_rsp_ptr
->qr
, QUERY_OSF_SIZE
);
1819 /* Get the descriptor */
1820 if (lrbp
->ucd_rsp_ptr
->qr
.opcode
== UPIU_QUERY_OPCODE_READ_DESC
) {
1821 u8
*descp
= (u8
*)lrbp
->ucd_rsp_ptr
+
1822 GENERAL_UPIU_REQUEST_SIZE
;
1826 /* data segment length */
1827 resp_len
= be32_to_cpu(lrbp
->ucd_rsp_ptr
->header
.dword_2
) &
1828 MASK_QUERY_DATA_SEG_LEN
;
1829 buf_len
= be16_to_cpu(
1830 hba
->dev_cmd
.query
.request
.upiu_req
.length
);
1831 if (likely(buf_len
>= resp_len
)) {
1832 memcpy(hba
->dev_cmd
.query
.descriptor
, descp
, resp_len
);
1835 "%s: Response size is bigger than buffer",
1845 * ufshcd_hba_capabilities - Read controller capabilities
1846 * @hba: per adapter instance
1848 static inline void ufshcd_hba_capabilities(struct ufs_hba
*hba
)
1850 hba
->capabilities
= ufshcd_readl(hba
, REG_CONTROLLER_CAPABILITIES
);
1852 /* nutrs and nutmrs are 0 based values */
1853 hba
->nutrs
= (hba
->capabilities
& MASK_TRANSFER_REQUESTS_SLOTS
) + 1;
1855 ((hba
->capabilities
& MASK_TASK_MANAGEMENT_REQUEST_SLOTS
) >> 16) + 1;
1859 * ufshcd_ready_for_uic_cmd - Check if controller is ready
1860 * to accept UIC commands
1861 * @hba: per adapter instance
1862 * Return true on success, else false
1864 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba
*hba
)
1866 if (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) & UIC_COMMAND_READY
)
1873 * ufshcd_get_upmcrs - Get the power mode change request status
1874 * @hba: Pointer to adapter instance
1876 * This function gets the UPMCRS field of HCS register
1877 * Returns value of UPMCRS field
1879 static inline u8
ufshcd_get_upmcrs(struct ufs_hba
*hba
)
1881 return (ufshcd_readl(hba
, REG_CONTROLLER_STATUS
) >> 8) & 0x7;
1885 * ufshcd_dispatch_uic_cmd - Dispatch UIC commands to unipro layers
1886 * @hba: per adapter instance
1887 * @uic_cmd: UIC command
1889 * Mutex must be held.
1892 ufshcd_dispatch_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
1894 WARN_ON(hba
->active_uic_cmd
);
1896 hba
->active_uic_cmd
= uic_cmd
;
1899 ufshcd_writel(hba
, uic_cmd
->argument1
, REG_UIC_COMMAND_ARG_1
);
1900 ufshcd_writel(hba
, uic_cmd
->argument2
, REG_UIC_COMMAND_ARG_2
);
1901 ufshcd_writel(hba
, uic_cmd
->argument3
, REG_UIC_COMMAND_ARG_3
);
1904 ufshcd_writel(hba
, uic_cmd
->command
& COMMAND_OPCODE_MASK
,
1909 * ufshcd_wait_for_uic_cmd - Wait complectioin of UIC command
1910 * @hba: per adapter instance
1911 * @uic_command: UIC command
1913 * Must be called with mutex held.
1914 * Returns 0 only if success.
1917 ufshcd_wait_for_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
1920 unsigned long flags
;
1922 if (wait_for_completion_timeout(&uic_cmd
->done
,
1923 msecs_to_jiffies(UIC_CMD_TIMEOUT
)))
1924 ret
= uic_cmd
->argument2
& MASK_UIC_COMMAND_RESULT
;
1928 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1929 hba
->active_uic_cmd
= NULL
;
1930 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1936 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1937 * @hba: per adapter instance
1938 * @uic_cmd: UIC command
1939 * @completion: initialize the completion only if this is set to true
1941 * Identical to ufshcd_send_uic_cmd() expect mutex. Must be called
1942 * with mutex held and host_lock locked.
1943 * Returns 0 only if success.
1946 __ufshcd_send_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
,
1949 if (!ufshcd_ready_for_uic_cmd(hba
)) {
1951 "Controller not ready to accept UIC commands\n");
1956 init_completion(&uic_cmd
->done
);
1958 ufshcd_dispatch_uic_cmd(hba
, uic_cmd
);
1964 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
1965 * @hba: per adapter instance
1966 * @uic_cmd: UIC command
1968 * Returns 0 only if success.
1971 ufshcd_send_uic_cmd(struct ufs_hba
*hba
, struct uic_command
*uic_cmd
)
1974 unsigned long flags
;
1976 ufshcd_hold(hba
, false);
1977 mutex_lock(&hba
->uic_cmd_mutex
);
1978 ufshcd_add_delay_before_dme_cmd(hba
);
1980 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
1981 ret
= __ufshcd_send_uic_cmd(hba
, uic_cmd
, true);
1982 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
1984 ret
= ufshcd_wait_for_uic_cmd(hba
, uic_cmd
);
1986 mutex_unlock(&hba
->uic_cmd_mutex
);
1988 ufshcd_release(hba
);
1993 * ufshcd_map_sg - Map scatter-gather list to prdt
1994 * @lrbp - pointer to local reference block
1996 * Returns 0 in case of success, non-zero value in case of failure
1998 static int ufshcd_map_sg(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2000 struct ufshcd_sg_entry
*prd_table
;
2001 struct scatterlist
*sg
;
2002 struct scsi_cmnd
*cmd
;
2007 sg_segments
= scsi_dma_map(cmd
);
2008 if (sg_segments
< 0)
2012 if (hba
->quirks
& UFSHCD_QUIRK_PRDT_BYTE_GRAN
)
2013 lrbp
->utr_descriptor_ptr
->prd_table_length
=
2014 cpu_to_le16((u16
)(sg_segments
*
2015 sizeof(struct ufshcd_sg_entry
)));
2017 lrbp
->utr_descriptor_ptr
->prd_table_length
=
2018 cpu_to_le16((u16
) (sg_segments
));
2020 prd_table
= (struct ufshcd_sg_entry
*)lrbp
->ucd_prdt_ptr
;
2022 scsi_for_each_sg(cmd
, sg
, sg_segments
, i
) {
2024 cpu_to_le32(((u32
) sg_dma_len(sg
))-1);
2025 prd_table
[i
].base_addr
=
2026 cpu_to_le32(lower_32_bits(sg
->dma_address
));
2027 prd_table
[i
].upper_addr
=
2028 cpu_to_le32(upper_32_bits(sg
->dma_address
));
2029 prd_table
[i
].reserved
= 0;
2032 lrbp
->utr_descriptor_ptr
->prd_table_length
= 0;
2039 * ufshcd_enable_intr - enable interrupts
2040 * @hba: per adapter instance
2041 * @intrs: interrupt bits
2043 static void ufshcd_enable_intr(struct ufs_hba
*hba
, u32 intrs
)
2045 u32 set
= ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
2047 if (hba
->ufs_version
== UFSHCI_VERSION_10
) {
2049 rw
= set
& INTERRUPT_MASK_RW_VER_10
;
2050 set
= rw
| ((set
^ intrs
) & intrs
);
2055 ufshcd_writel(hba
, set
, REG_INTERRUPT_ENABLE
);
2059 * ufshcd_disable_intr - disable interrupts
2060 * @hba: per adapter instance
2061 * @intrs: interrupt bits
2063 static void ufshcd_disable_intr(struct ufs_hba
*hba
, u32 intrs
)
2065 u32 set
= ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
2067 if (hba
->ufs_version
== UFSHCI_VERSION_10
) {
2069 rw
= (set
& INTERRUPT_MASK_RW_VER_10
) &
2070 ~(intrs
& INTERRUPT_MASK_RW_VER_10
);
2071 set
= rw
| ((set
& intrs
) & ~INTERRUPT_MASK_RW_VER_10
);
2077 ufshcd_writel(hba
, set
, REG_INTERRUPT_ENABLE
);
2081 * ufshcd_prepare_req_desc_hdr() - Fills the requests header
2082 * descriptor according to request
2083 * @lrbp: pointer to local reference block
2084 * @upiu_flags: flags required in the header
2085 * @cmd_dir: requests data direction
2087 static void ufshcd_prepare_req_desc_hdr(struct ufshcd_lrb
*lrbp
,
2088 u32
*upiu_flags
, enum dma_data_direction cmd_dir
)
2090 struct utp_transfer_req_desc
*req_desc
= lrbp
->utr_descriptor_ptr
;
2094 if (cmd_dir
== DMA_FROM_DEVICE
) {
2095 data_direction
= UTP_DEVICE_TO_HOST
;
2096 *upiu_flags
= UPIU_CMD_FLAGS_READ
;
2097 } else if (cmd_dir
== DMA_TO_DEVICE
) {
2098 data_direction
= UTP_HOST_TO_DEVICE
;
2099 *upiu_flags
= UPIU_CMD_FLAGS_WRITE
;
2101 data_direction
= UTP_NO_DATA_TRANSFER
;
2102 *upiu_flags
= UPIU_CMD_FLAGS_NONE
;
2105 dword_0
= data_direction
| (lrbp
->command_type
2106 << UPIU_COMMAND_TYPE_OFFSET
);
2108 dword_0
|= UTP_REQ_DESC_INT_CMD
;
2110 /* Transfer request descriptor header fields */
2111 req_desc
->header
.dword_0
= cpu_to_le32(dword_0
);
2112 /* dword_1 is reserved, hence it is set to 0 */
2113 req_desc
->header
.dword_1
= 0;
2115 * assigning invalid value for command status. Controller
2116 * updates OCS on command completion, with the command
2119 req_desc
->header
.dword_2
=
2120 cpu_to_le32(OCS_INVALID_COMMAND_STATUS
);
2121 /* dword_3 is reserved, hence it is set to 0 */
2122 req_desc
->header
.dword_3
= 0;
2124 req_desc
->prd_table_length
= 0;
2128 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2130 * @lrbp - local reference block pointer
2131 * @upiu_flags - flags
2134 void ufshcd_prepare_utp_scsi_cmd_upiu(struct ufshcd_lrb
*lrbp
, u32 upiu_flags
)
2136 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
2137 unsigned short cdb_len
;
2139 /* command descriptor fields */
2140 ucd_req_ptr
->header
.dword_0
= UPIU_HEADER_DWORD(
2141 UPIU_TRANSACTION_COMMAND
, upiu_flags
,
2142 lrbp
->lun
, lrbp
->task_tag
);
2143 ucd_req_ptr
->header
.dword_1
= UPIU_HEADER_DWORD(
2144 UPIU_COMMAND_SET_TYPE_SCSI
, 0, 0, 0);
2146 /* Total EHS length and Data segment length will be zero */
2147 ucd_req_ptr
->header
.dword_2
= 0;
2149 ucd_req_ptr
->sc
.exp_data_transfer_len
=
2150 cpu_to_be32(lrbp
->cmd
->sdb
.length
);
2152 cdb_len
= min_t(unsigned short, lrbp
->cmd
->cmd_len
, MAX_CDB_SIZE
);
2153 memset(ucd_req_ptr
->sc
.cdb
, 0, MAX_CDB_SIZE
);
2154 memcpy(ucd_req_ptr
->sc
.cdb
, lrbp
->cmd
->cmnd
, cdb_len
);
2156 memset(lrbp
->ucd_rsp_ptr
, 0, sizeof(struct utp_upiu_rsp
));
2160 * ufshcd_prepare_utp_query_req_upiu() - fills the utp_transfer_req_desc,
2163 * @lrbp: local reference block pointer
2164 * @upiu_flags: flags
2166 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba
*hba
,
2167 struct ufshcd_lrb
*lrbp
, u32 upiu_flags
)
2169 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
2170 struct ufs_query
*query
= &hba
->dev_cmd
.query
;
2171 u16 len
= be16_to_cpu(query
->request
.upiu_req
.length
);
2172 u8
*descp
= (u8
*)lrbp
->ucd_req_ptr
+ GENERAL_UPIU_REQUEST_SIZE
;
2174 /* Query request header */
2175 ucd_req_ptr
->header
.dword_0
= UPIU_HEADER_DWORD(
2176 UPIU_TRANSACTION_QUERY_REQ
, upiu_flags
,
2177 lrbp
->lun
, lrbp
->task_tag
);
2178 ucd_req_ptr
->header
.dword_1
= UPIU_HEADER_DWORD(
2179 0, query
->request
.query_func
, 0, 0);
2181 /* Data segment length only need for WRITE_DESC */
2182 if (query
->request
.upiu_req
.opcode
== UPIU_QUERY_OPCODE_WRITE_DESC
)
2183 ucd_req_ptr
->header
.dword_2
=
2184 UPIU_HEADER_DWORD(0, 0, (len
>> 8), (u8
)len
);
2186 ucd_req_ptr
->header
.dword_2
= 0;
2188 /* Copy the Query Request buffer as is */
2189 memcpy(&ucd_req_ptr
->qr
, &query
->request
.upiu_req
,
2192 /* Copy the Descriptor */
2193 if (query
->request
.upiu_req
.opcode
== UPIU_QUERY_OPCODE_WRITE_DESC
)
2194 memcpy(descp
, query
->descriptor
, len
);
2196 memset(lrbp
->ucd_rsp_ptr
, 0, sizeof(struct utp_upiu_rsp
));
2199 static inline void ufshcd_prepare_utp_nop_upiu(struct ufshcd_lrb
*lrbp
)
2201 struct utp_upiu_req
*ucd_req_ptr
= lrbp
->ucd_req_ptr
;
2203 memset(ucd_req_ptr
, 0, sizeof(struct utp_upiu_req
));
2205 /* command descriptor fields */
2206 ucd_req_ptr
->header
.dword_0
=
2208 UPIU_TRANSACTION_NOP_OUT
, 0, 0, lrbp
->task_tag
);
2209 /* clear rest of the fields of basic header */
2210 ucd_req_ptr
->header
.dword_1
= 0;
2211 ucd_req_ptr
->header
.dword_2
= 0;
2213 memset(lrbp
->ucd_rsp_ptr
, 0, sizeof(struct utp_upiu_rsp
));
2217 * ufshcd_comp_devman_upiu - UFS Protocol Information Unit(UPIU)
2218 * for Device Management Purposes
2219 * @hba - per adapter instance
2220 * @lrb - pointer to local reference block
2222 static int ufshcd_comp_devman_upiu(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2227 if (hba
->ufs_version
== UFSHCI_VERSION_20
)
2228 lrbp
->command_type
= UTP_CMD_TYPE_UFS_STORAGE
;
2230 lrbp
->command_type
= UTP_CMD_TYPE_DEV_MANAGE
;
2232 ufshcd_prepare_req_desc_hdr(lrbp
, &upiu_flags
, DMA_NONE
);
2233 if (hba
->dev_cmd
.type
== DEV_CMD_TYPE_QUERY
)
2234 ufshcd_prepare_utp_query_req_upiu(hba
, lrbp
, upiu_flags
);
2235 else if (hba
->dev_cmd
.type
== DEV_CMD_TYPE_NOP
)
2236 ufshcd_prepare_utp_nop_upiu(lrbp
);
2244 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2246 * @hba - per adapter instance
2247 * @lrb - pointer to local reference block
2249 static int ufshcd_comp_scsi_upiu(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2254 if (hba
->ufs_version
== UFSHCI_VERSION_20
)
2255 lrbp
->command_type
= UTP_CMD_TYPE_UFS_STORAGE
;
2257 lrbp
->command_type
= UTP_CMD_TYPE_SCSI
;
2259 if (likely(lrbp
->cmd
)) {
2260 ufshcd_prepare_req_desc_hdr(lrbp
, &upiu_flags
,
2261 lrbp
->cmd
->sc_data_direction
);
2262 ufshcd_prepare_utp_scsi_cmd_upiu(lrbp
, upiu_flags
);
2271 * ufshcd_scsi_to_upiu_lun - maps scsi LUN to UPIU LUN
2272 * @scsi_lun: scsi LUN id
2274 * Returns UPIU LUN id
2276 static inline u8
ufshcd_scsi_to_upiu_lun(unsigned int scsi_lun
)
2278 if (scsi_is_wlun(scsi_lun
))
2279 return (scsi_lun
& UFS_UPIU_MAX_UNIT_NUM_ID
)
2282 return scsi_lun
& UFS_UPIU_MAX_UNIT_NUM_ID
;
2286 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2287 * @scsi_lun: UPIU W-LUN id
2289 * Returns SCSI W-LUN id
2291 static inline u16
ufshcd_upiu_wlun_to_scsi_wlun(u8 upiu_wlun_id
)
2293 return (upiu_wlun_id
& ~UFS_UPIU_WLUN_ID
) | SCSI_W_LUN_BASE
;
2297 * ufshcd_queuecommand - main entry point for SCSI requests
2298 * @cmd: command from SCSI Midlayer
2299 * @done: call back function
2301 * Returns 0 for success, non-zero in case of failure
2303 static int ufshcd_queuecommand(struct Scsi_Host
*host
, struct scsi_cmnd
*cmd
)
2305 struct ufshcd_lrb
*lrbp
;
2306 struct ufs_hba
*hba
;
2307 unsigned long flags
;
2311 hba
= shost_priv(host
);
2313 tag
= cmd
->request
->tag
;
2314 if (!ufshcd_valid_tag(hba
, tag
)) {
2316 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
2317 __func__
, tag
, cmd
, cmd
->request
);
2321 if (!down_read_trylock(&hba
->clk_scaling_lock
))
2322 return SCSI_MLQUEUE_HOST_BUSY
;
2324 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2325 switch (hba
->ufshcd_state
) {
2326 case UFSHCD_STATE_OPERATIONAL
:
2328 case UFSHCD_STATE_EH_SCHEDULED
:
2329 case UFSHCD_STATE_RESET
:
2330 err
= SCSI_MLQUEUE_HOST_BUSY
;
2332 case UFSHCD_STATE_ERROR
:
2333 set_host_byte(cmd
, DID_ERROR
);
2334 cmd
->scsi_done(cmd
);
2337 dev_WARN_ONCE(hba
->dev
, 1, "%s: invalid state %d\n",
2338 __func__
, hba
->ufshcd_state
);
2339 set_host_byte(cmd
, DID_BAD_TARGET
);
2340 cmd
->scsi_done(cmd
);
2344 /* if error handling is in progress, don't issue commands */
2345 if (ufshcd_eh_in_progress(hba
)) {
2346 set_host_byte(cmd
, DID_ERROR
);
2347 cmd
->scsi_done(cmd
);
2350 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2352 hba
->req_abort_count
= 0;
2354 /* acquire the tag to make sure device cmds don't use it */
2355 if (test_and_set_bit_lock(tag
, &hba
->lrb_in_use
)) {
2357 * Dev manage command in progress, requeue the command.
2358 * Requeuing the command helps in cases where the request *may*
2359 * find different tag instead of waiting for dev manage command
2362 err
= SCSI_MLQUEUE_HOST_BUSY
;
2366 err
= ufshcd_hold(hba
, true);
2368 err
= SCSI_MLQUEUE_HOST_BUSY
;
2369 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
2372 WARN_ON(hba
->clk_gating
.state
!= CLKS_ON
);
2374 lrbp
= &hba
->lrb
[tag
];
2378 lrbp
->sense_bufflen
= UFSHCD_REQ_SENSE_SIZE
;
2379 lrbp
->sense_buffer
= cmd
->sense_buffer
;
2380 lrbp
->task_tag
= tag
;
2381 lrbp
->lun
= ufshcd_scsi_to_upiu_lun(cmd
->device
->lun
);
2382 lrbp
->intr_cmd
= !ufshcd_is_intr_aggr_allowed(hba
) ? true : false;
2383 lrbp
->req_abort_skip
= false;
2385 ufshcd_comp_scsi_upiu(hba
, lrbp
);
2387 err
= ufshcd_map_sg(hba
, lrbp
);
2390 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
2393 /* Make sure descriptors are ready before ringing the doorbell */
2396 /* issue command to the controller */
2397 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2398 ufshcd_vops_setup_xfer_req(hba
, tag
, (lrbp
->cmd
? true : false));
2399 ufshcd_send_command(hba
, tag
);
2401 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2403 up_read(&hba
->clk_scaling_lock
);
2407 static int ufshcd_compose_dev_cmd(struct ufs_hba
*hba
,
2408 struct ufshcd_lrb
*lrbp
, enum dev_cmd_type cmd_type
, int tag
)
2411 lrbp
->sense_bufflen
= 0;
2412 lrbp
->sense_buffer
= NULL
;
2413 lrbp
->task_tag
= tag
;
2414 lrbp
->lun
= 0; /* device management cmd is not specific to any LUN */
2415 lrbp
->intr_cmd
= true; /* No interrupt aggregation */
2416 hba
->dev_cmd
.type
= cmd_type
;
2418 return ufshcd_comp_devman_upiu(hba
, lrbp
);
2422 ufshcd_clear_cmd(struct ufs_hba
*hba
, int tag
)
2425 unsigned long flags
;
2426 u32 mask
= 1 << tag
;
2428 /* clear outstanding transaction before retry */
2429 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2430 ufshcd_utrl_clear(hba
, tag
);
2431 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2434 * wait for for h/w to clear corresponding bit in door-bell.
2435 * max. wait is 1 sec.
2437 err
= ufshcd_wait_for_register(hba
,
2438 REG_UTP_TRANSFER_REQ_DOOR_BELL
,
2439 mask
, ~mask
, 1000, 1000, true);
2445 ufshcd_check_query_response(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2447 struct ufs_query_res
*query_res
= &hba
->dev_cmd
.query
.response
;
2449 /* Get the UPIU response */
2450 query_res
->response
= ufshcd_get_rsp_upiu_result(lrbp
->ucd_rsp_ptr
) >>
2451 UPIU_RSP_CODE_OFFSET
;
2452 return query_res
->response
;
2456 * ufshcd_dev_cmd_completion() - handles device management command responses
2457 * @hba: per adapter instance
2458 * @lrbp: pointer to local reference block
2461 ufshcd_dev_cmd_completion(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
2466 hba
->ufs_stats
.last_hibern8_exit_tstamp
= ktime_set(0, 0);
2467 resp
= ufshcd_get_req_rsp(lrbp
->ucd_rsp_ptr
);
2470 case UPIU_TRANSACTION_NOP_IN
:
2471 if (hba
->dev_cmd
.type
!= DEV_CMD_TYPE_NOP
) {
2473 dev_err(hba
->dev
, "%s: unexpected response %x\n",
2477 case UPIU_TRANSACTION_QUERY_RSP
:
2478 err
= ufshcd_check_query_response(hba
, lrbp
);
2480 err
= ufshcd_copy_query_response(hba
, lrbp
);
2482 case UPIU_TRANSACTION_REJECT_UPIU
:
2483 /* TODO: handle Reject UPIU Response */
2485 dev_err(hba
->dev
, "%s: Reject UPIU not fully implemented\n",
2490 dev_err(hba
->dev
, "%s: Invalid device management cmd response: %x\n",
2498 static int ufshcd_wait_for_dev_cmd(struct ufs_hba
*hba
,
2499 struct ufshcd_lrb
*lrbp
, int max_timeout
)
2502 unsigned long time_left
;
2503 unsigned long flags
;
2505 time_left
= wait_for_completion_timeout(hba
->dev_cmd
.complete
,
2506 msecs_to_jiffies(max_timeout
));
2508 /* Make sure descriptors are ready before ringing the doorbell */
2510 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2511 hba
->dev_cmd
.complete
= NULL
;
2512 if (likely(time_left
)) {
2513 err
= ufshcd_get_tr_ocs(lrbp
);
2515 err
= ufshcd_dev_cmd_completion(hba
, lrbp
);
2517 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2521 dev_dbg(hba
->dev
, "%s: dev_cmd request timedout, tag %d\n",
2522 __func__
, lrbp
->task_tag
);
2523 if (!ufshcd_clear_cmd(hba
, lrbp
->task_tag
))
2524 /* successfully cleared the command, retry if needed */
2527 * in case of an error, after clearing the doorbell,
2528 * we also need to clear the outstanding_request
2531 ufshcd_outstanding_req_clear(hba
, lrbp
->task_tag
);
2538 * ufshcd_get_dev_cmd_tag - Get device management command tag
2539 * @hba: per-adapter instance
2540 * @tag: pointer to variable with available slot value
2542 * Get a free slot and lock it until device management command
2545 * Returns false if free slot is unavailable for locking, else
2546 * return true with tag value in @tag.
2548 static bool ufshcd_get_dev_cmd_tag(struct ufs_hba
*hba
, int *tag_out
)
2558 tmp
= ~hba
->lrb_in_use
;
2559 tag
= find_last_bit(&tmp
, hba
->nutrs
);
2560 if (tag
>= hba
->nutrs
)
2562 } while (test_and_set_bit_lock(tag
, &hba
->lrb_in_use
));
2570 static inline void ufshcd_put_dev_cmd_tag(struct ufs_hba
*hba
, int tag
)
2572 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
2576 * ufshcd_exec_dev_cmd - API for sending device management requests
2578 * @cmd_type - specifies the type (NOP, Query...)
2579 * @timeout - time in seconds
2581 * NOTE: Since there is only one available tag for device management commands,
2582 * it is expected you hold the hba->dev_cmd.lock mutex.
2584 static int ufshcd_exec_dev_cmd(struct ufs_hba
*hba
,
2585 enum dev_cmd_type cmd_type
, int timeout
)
2587 struct ufshcd_lrb
*lrbp
;
2590 struct completion wait
;
2591 unsigned long flags
;
2593 down_read(&hba
->clk_scaling_lock
);
2596 * Get free slot, sleep if slots are unavailable.
2597 * Even though we use wait_event() which sleeps indefinitely,
2598 * the maximum wait time is bounded by SCSI request timeout.
2600 wait_event(hba
->dev_cmd
.tag_wq
, ufshcd_get_dev_cmd_tag(hba
, &tag
));
2602 init_completion(&wait
);
2603 lrbp
= &hba
->lrb
[tag
];
2605 err
= ufshcd_compose_dev_cmd(hba
, lrbp
, cmd_type
, tag
);
2609 hba
->dev_cmd
.complete
= &wait
;
2611 /* Make sure descriptors are ready before ringing the doorbell */
2613 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
2614 ufshcd_vops_setup_xfer_req(hba
, tag
, (lrbp
->cmd
? true : false));
2615 ufshcd_send_command(hba
, tag
);
2616 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
2618 err
= ufshcd_wait_for_dev_cmd(hba
, lrbp
, timeout
);
2621 ufshcd_put_dev_cmd_tag(hba
, tag
);
2622 wake_up(&hba
->dev_cmd
.tag_wq
);
2623 up_read(&hba
->clk_scaling_lock
);
2628 * ufshcd_init_query() - init the query response and request parameters
2629 * @hba: per-adapter instance
2630 * @request: address of the request pointer to be initialized
2631 * @response: address of the response pointer to be initialized
2632 * @opcode: operation to perform
2633 * @idn: flag idn to access
2634 * @index: LU number to access
2635 * @selector: query/flag/descriptor further identification
2637 static inline void ufshcd_init_query(struct ufs_hba
*hba
,
2638 struct ufs_query_req
**request
, struct ufs_query_res
**response
,
2639 enum query_opcode opcode
, u8 idn
, u8 index
, u8 selector
)
2641 *request
= &hba
->dev_cmd
.query
.request
;
2642 *response
= &hba
->dev_cmd
.query
.response
;
2643 memset(*request
, 0, sizeof(struct ufs_query_req
));
2644 memset(*response
, 0, sizeof(struct ufs_query_res
));
2645 (*request
)->upiu_req
.opcode
= opcode
;
2646 (*request
)->upiu_req
.idn
= idn
;
2647 (*request
)->upiu_req
.index
= index
;
2648 (*request
)->upiu_req
.selector
= selector
;
2651 static int ufshcd_query_flag_retry(struct ufs_hba
*hba
,
2652 enum query_opcode opcode
, enum flag_idn idn
, bool *flag_res
)
2657 for (retries
= 0; retries
< QUERY_REQ_RETRIES
; retries
++) {
2658 ret
= ufshcd_query_flag(hba
, opcode
, idn
, flag_res
);
2661 "%s: failed with error %d, retries %d\n",
2662 __func__
, ret
, retries
);
2669 "%s: query attribute, opcode %d, idn %d, failed with error %d after %d retires\n",
2670 __func__
, opcode
, idn
, ret
, retries
);
2675 * ufshcd_query_flag() - API function for sending flag query requests
2676 * hba: per-adapter instance
2677 * query_opcode: flag query to perform
2678 * idn: flag idn to access
2679 * flag_res: the flag value after the query request completes
2681 * Returns 0 for success, non-zero in case of failure
2683 int ufshcd_query_flag(struct ufs_hba
*hba
, enum query_opcode opcode
,
2684 enum flag_idn idn
, bool *flag_res
)
2686 struct ufs_query_req
*request
= NULL
;
2687 struct ufs_query_res
*response
= NULL
;
2688 int err
, index
= 0, selector
= 0;
2689 int timeout
= QUERY_REQ_TIMEOUT
;
2693 ufshcd_hold(hba
, false);
2694 mutex_lock(&hba
->dev_cmd
.lock
);
2695 ufshcd_init_query(hba
, &request
, &response
, opcode
, idn
, index
,
2699 case UPIU_QUERY_OPCODE_SET_FLAG
:
2700 case UPIU_QUERY_OPCODE_CLEAR_FLAG
:
2701 case UPIU_QUERY_OPCODE_TOGGLE_FLAG
:
2702 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
2704 case UPIU_QUERY_OPCODE_READ_FLAG
:
2705 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
2707 /* No dummy reads */
2708 dev_err(hba
->dev
, "%s: Invalid argument for read request\n",
2716 "%s: Expected query flag opcode but got = %d\n",
2722 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
, timeout
);
2726 "%s: Sending flag query for idn %d failed, err = %d\n",
2727 __func__
, idn
, err
);
2732 *flag_res
= (be32_to_cpu(response
->upiu_res
.value
) &
2733 MASK_QUERY_UPIU_FLAG_LOC
) & 0x1;
2736 mutex_unlock(&hba
->dev_cmd
.lock
);
2737 ufshcd_release(hba
);
2742 * ufshcd_query_attr - API function for sending attribute requests
2743 * hba: per-adapter instance
2744 * opcode: attribute opcode
2745 * idn: attribute idn to access
2746 * index: index field
2747 * selector: selector field
2748 * attr_val: the attribute value after the query request completes
2750 * Returns 0 for success, non-zero in case of failure
2752 static int ufshcd_query_attr(struct ufs_hba
*hba
, enum query_opcode opcode
,
2753 enum attr_idn idn
, u8 index
, u8 selector
, u32
*attr_val
)
2755 struct ufs_query_req
*request
= NULL
;
2756 struct ufs_query_res
*response
= NULL
;
2761 ufshcd_hold(hba
, false);
2763 dev_err(hba
->dev
, "%s: attribute value required for opcode 0x%x\n",
2769 mutex_lock(&hba
->dev_cmd
.lock
);
2770 ufshcd_init_query(hba
, &request
, &response
, opcode
, idn
, index
,
2774 case UPIU_QUERY_OPCODE_WRITE_ATTR
:
2775 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
2776 request
->upiu_req
.value
= cpu_to_be32(*attr_val
);
2778 case UPIU_QUERY_OPCODE_READ_ATTR
:
2779 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
2782 dev_err(hba
->dev
, "%s: Expected query attr opcode but got = 0x%.2x\n",
2788 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
, QUERY_REQ_TIMEOUT
);
2791 dev_err(hba
->dev
, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2792 __func__
, opcode
, idn
, index
, err
);
2796 *attr_val
= be32_to_cpu(response
->upiu_res
.value
);
2799 mutex_unlock(&hba
->dev_cmd
.lock
);
2801 ufshcd_release(hba
);
2806 * ufshcd_query_attr_retry() - API function for sending query
2807 * attribute with retries
2808 * @hba: per-adapter instance
2809 * @opcode: attribute opcode
2810 * @idn: attribute idn to access
2811 * @index: index field
2812 * @selector: selector field
2813 * @attr_val: the attribute value after the query request
2816 * Returns 0 for success, non-zero in case of failure
2818 static int ufshcd_query_attr_retry(struct ufs_hba
*hba
,
2819 enum query_opcode opcode
, enum attr_idn idn
, u8 index
, u8 selector
,
2825 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
2826 ret
= ufshcd_query_attr(hba
, opcode
, idn
, index
,
2827 selector
, attr_val
);
2829 dev_dbg(hba
->dev
, "%s: failed with error %d, retries %d\n",
2830 __func__
, ret
, retries
);
2837 "%s: query attribute, idn %d, failed with error %d after %d retires\n",
2838 __func__
, idn
, ret
, QUERY_REQ_RETRIES
);
2842 static int __ufshcd_query_descriptor(struct ufs_hba
*hba
,
2843 enum query_opcode opcode
, enum desc_idn idn
, u8 index
,
2844 u8 selector
, u8
*desc_buf
, int *buf_len
)
2846 struct ufs_query_req
*request
= NULL
;
2847 struct ufs_query_res
*response
= NULL
;
2852 ufshcd_hold(hba
, false);
2854 dev_err(hba
->dev
, "%s: descriptor buffer required for opcode 0x%x\n",
2860 if (*buf_len
<= QUERY_DESC_MIN_SIZE
|| *buf_len
> QUERY_DESC_MAX_SIZE
) {
2861 dev_err(hba
->dev
, "%s: descriptor buffer size (%d) is out of range\n",
2862 __func__
, *buf_len
);
2867 mutex_lock(&hba
->dev_cmd
.lock
);
2868 ufshcd_init_query(hba
, &request
, &response
, opcode
, idn
, index
,
2870 hba
->dev_cmd
.query
.descriptor
= desc_buf
;
2871 request
->upiu_req
.length
= cpu_to_be16(*buf_len
);
2874 case UPIU_QUERY_OPCODE_WRITE_DESC
:
2875 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST
;
2877 case UPIU_QUERY_OPCODE_READ_DESC
:
2878 request
->query_func
= UPIU_QUERY_FUNC_STANDARD_READ_REQUEST
;
2882 "%s: Expected query descriptor opcode but got = 0x%.2x\n",
2888 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_QUERY
, QUERY_REQ_TIMEOUT
);
2891 dev_err(hba
->dev
, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n",
2892 __func__
, opcode
, idn
, index
, err
);
2896 hba
->dev_cmd
.query
.descriptor
= NULL
;
2897 *buf_len
= be16_to_cpu(response
->upiu_res
.length
);
2900 mutex_unlock(&hba
->dev_cmd
.lock
);
2902 ufshcd_release(hba
);
2907 * ufshcd_query_descriptor_retry - API function for sending descriptor
2909 * hba: per-adapter instance
2910 * opcode: attribute opcode
2911 * idn: attribute idn to access
2912 * index: index field
2913 * selector: selector field
2914 * desc_buf: the buffer that contains the descriptor
2915 * buf_len: length parameter passed to the device
2917 * Returns 0 for success, non-zero in case of failure.
2918 * The buf_len parameter will contain, on return, the length parameter
2919 * received on the response.
2921 static int ufshcd_query_descriptor_retry(struct ufs_hba
*hba
,
2922 enum query_opcode opcode
,
2923 enum desc_idn idn
, u8 index
,
2925 u8
*desc_buf
, int *buf_len
)
2930 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
2931 err
= __ufshcd_query_descriptor(hba
, opcode
, idn
, index
,
2932 selector
, desc_buf
, buf_len
);
2933 if (!err
|| err
== -EINVAL
)
2941 * ufshcd_read_desc_param - read the specified descriptor parameter
2942 * @hba: Pointer to adapter instance
2943 * @desc_id: descriptor idn value
2944 * @desc_index: descriptor index
2945 * @param_offset: offset of the parameter to read
2946 * @param_read_buf: pointer to buffer where parameter would be read
2947 * @param_size: sizeof(param_read_buf)
2949 * Return 0 in case of success, non-zero otherwise
2951 static int ufshcd_read_desc_param(struct ufs_hba
*hba
,
2952 enum desc_idn desc_id
,
2961 bool is_kmalloc
= true;
2964 if (desc_id
>= QUERY_DESC_IDN_MAX
)
2967 buff_len
= ufs_query_desc_max_size
[desc_id
];
2968 if ((param_offset
+ param_size
) > buff_len
)
2971 if (!param_offset
&& (param_size
== buff_len
)) {
2972 /* memory space already available to hold full descriptor */
2973 desc_buf
= param_read_buf
;
2976 /* allocate memory to hold full descriptor */
2977 desc_buf
= kmalloc(buff_len
, GFP_KERNEL
);
2982 ret
= ufshcd_query_descriptor_retry(hba
, UPIU_QUERY_OPCODE_READ_DESC
,
2983 desc_id
, desc_index
, 0, desc_buf
,
2987 dev_err(hba
->dev
, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret %d",
2988 __func__
, desc_id
, desc_index
, param_offset
, ret
);
2994 if (desc_buf
[QUERY_DESC_DESC_TYPE_OFFSET
] != desc_id
) {
2995 dev_err(hba
->dev
, "%s: invalid desc_id %d in descriptor header",
2996 __func__
, desc_buf
[QUERY_DESC_DESC_TYPE_OFFSET
]);
3002 * While reading variable size descriptors (like string descriptor),
3003 * some UFS devices may report the "LENGTH" (field in "Transaction
3004 * Specific fields" of Query Response UPIU) same as what was requested
3005 * in Query Request UPIU instead of reporting the actual size of the
3006 * variable size descriptor.
3007 * Although it's safe to ignore the "LENGTH" field for variable size
3008 * descriptors as we can always derive the length of the descriptor from
3009 * the descriptor header fields. Hence this change impose the length
3010 * match check only for fixed size descriptors (for which we always
3011 * request the correct size as part of Query Request UPIU).
3013 if ((desc_id
!= QUERY_DESC_IDN_STRING
) &&
3014 (buff_len
!= desc_buf
[QUERY_DESC_LENGTH_OFFSET
])) {
3015 dev_err(hba
->dev
, "%s: desc_buf length mismatch: buff_len %d, buff_len(desc_header) %d",
3016 __func__
, buff_len
, desc_buf
[QUERY_DESC_LENGTH_OFFSET
]);
3022 memcpy(param_read_buf
, &desc_buf
[param_offset
], param_size
);
3029 static inline int ufshcd_read_desc(struct ufs_hba
*hba
,
3030 enum desc_idn desc_id
,
3035 return ufshcd_read_desc_param(hba
, desc_id
, desc_index
, 0, buf
, size
);
3038 static inline int ufshcd_read_power_desc(struct ufs_hba
*hba
,
3045 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
3046 /* Read descriptor*/
3047 err
= ufshcd_read_desc(hba
, QUERY_DESC_IDN_POWER
, 0, buf
, size
);
3050 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, err
);
3056 static int ufshcd_read_device_desc(struct ufs_hba
*hba
, u8
*buf
, u32 size
)
3058 return ufshcd_read_desc(hba
, QUERY_DESC_IDN_DEVICE
, 0, buf
, size
);
3062 * ufshcd_read_string_desc - read string descriptor
3063 * @hba: pointer to adapter instance
3064 * @desc_index: descriptor index
3065 * @buf: pointer to buffer where descriptor would be read
3066 * @size: size of buf
3067 * @ascii: if true convert from unicode to ascii characters
3069 * Return 0 in case of success, non-zero otherwise
3071 #define ASCII_STD true
3072 static int ufshcd_read_string_desc(struct ufs_hba
*hba
, int desc_index
,
3073 u8
*buf
, u32 size
, bool ascii
)
3077 err
= ufshcd_read_desc(hba
,
3078 QUERY_DESC_IDN_STRING
, desc_index
, buf
, size
);
3081 dev_err(hba
->dev
, "%s: reading String Desc failed after %d retries. err = %d\n",
3082 __func__
, QUERY_REQ_RETRIES
, err
);
3093 /* remove header and divide by 2 to move from UTF16 to UTF8 */
3094 ascii_len
= (desc_len
- QUERY_DESC_HDR_SIZE
) / 2 + 1;
3095 if (size
< ascii_len
+ QUERY_DESC_HDR_SIZE
) {
3096 dev_err(hba
->dev
, "%s: buffer allocated size is too small\n",
3102 buff_ascii
= kmalloc(ascii_len
, GFP_KERNEL
);
3109 * the descriptor contains string in UTF16 format
3110 * we need to convert to utf-8 so it can be displayed
3112 utf16s_to_utf8s((wchar_t *)&buf
[QUERY_DESC_HDR_SIZE
],
3113 desc_len
- QUERY_DESC_HDR_SIZE
,
3114 UTF16_BIG_ENDIAN
, buff_ascii
, ascii_len
);
3116 /* replace non-printable or non-ASCII characters with spaces */
3117 for (i
= 0; i
< ascii_len
; i
++)
3118 ufshcd_remove_non_printable(&buff_ascii
[i
]);
3120 memset(buf
+ QUERY_DESC_HDR_SIZE
, 0,
3121 size
- QUERY_DESC_HDR_SIZE
);
3122 memcpy(buf
+ QUERY_DESC_HDR_SIZE
, buff_ascii
, ascii_len
);
3123 buf
[QUERY_DESC_LENGTH_OFFSET
] = ascii_len
+ QUERY_DESC_HDR_SIZE
;
3131 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3132 * @hba: Pointer to adapter instance
3134 * @param_offset: offset of the parameter to read
3135 * @param_read_buf: pointer to buffer where parameter would be read
3136 * @param_size: sizeof(param_read_buf)
3138 * Return 0 in case of success, non-zero otherwise
3140 static inline int ufshcd_read_unit_desc_param(struct ufs_hba
*hba
,
3142 enum unit_desc_param param_offset
,
3147 * Unit descriptors are only available for general purpose LUs (LUN id
3148 * from 0 to 7) and RPMB Well known LU.
3150 if (lun
!= UFS_UPIU_RPMB_WLUN
&& (lun
>= UFS_UPIU_MAX_GENERAL_LUN
))
3153 return ufshcd_read_desc_param(hba
, QUERY_DESC_IDN_UNIT
, lun
,
3154 param_offset
, param_read_buf
, param_size
);
3158 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3159 * @hba: per adapter instance
3161 * 1. Allocate DMA memory for Command Descriptor array
3162 * Each command descriptor consist of Command UPIU, Response UPIU and PRDT
3163 * 2. Allocate DMA memory for UTP Transfer Request Descriptor List (UTRDL).
3164 * 3. Allocate DMA memory for UTP Task Management Request Descriptor List
3166 * 4. Allocate memory for local reference block(lrb).
3168 * Returns 0 for success, non-zero in case of failure
3170 static int ufshcd_memory_alloc(struct ufs_hba
*hba
)
3172 size_t utmrdl_size
, utrdl_size
, ucdl_size
;
3174 /* Allocate memory for UTP command descriptors */
3175 ucdl_size
= (sizeof(struct utp_transfer_cmd_desc
) * hba
->nutrs
);
3176 hba
->ucdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
3178 &hba
->ucdl_dma_addr
,
3182 * UFSHCI requires UTP command descriptor to be 128 byte aligned.
3183 * make sure hba->ucdl_dma_addr is aligned to PAGE_SIZE
3184 * if hba->ucdl_dma_addr is aligned to PAGE_SIZE, then it will
3185 * be aligned to 128 bytes as well
3187 if (!hba
->ucdl_base_addr
||
3188 WARN_ON(hba
->ucdl_dma_addr
& (PAGE_SIZE
- 1))) {
3190 "Command Descriptor Memory allocation failed\n");
3195 * Allocate memory for UTP Transfer descriptors
3196 * UFSHCI requires 1024 byte alignment of UTRD
3198 utrdl_size
= (sizeof(struct utp_transfer_req_desc
) * hba
->nutrs
);
3199 hba
->utrdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
3201 &hba
->utrdl_dma_addr
,
3203 if (!hba
->utrdl_base_addr
||
3204 WARN_ON(hba
->utrdl_dma_addr
& (PAGE_SIZE
- 1))) {
3206 "Transfer Descriptor Memory allocation failed\n");
3211 * Allocate memory for UTP Task Management descriptors
3212 * UFSHCI requires 1024 byte alignment of UTMRD
3214 utmrdl_size
= sizeof(struct utp_task_req_desc
) * hba
->nutmrs
;
3215 hba
->utmrdl_base_addr
= dmam_alloc_coherent(hba
->dev
,
3217 &hba
->utmrdl_dma_addr
,
3219 if (!hba
->utmrdl_base_addr
||
3220 WARN_ON(hba
->utmrdl_dma_addr
& (PAGE_SIZE
- 1))) {
3222 "Task Management Descriptor Memory allocation failed\n");
3226 /* Allocate memory for local reference block */
3227 hba
->lrb
= devm_kzalloc(hba
->dev
,
3228 hba
->nutrs
* sizeof(struct ufshcd_lrb
),
3231 dev_err(hba
->dev
, "LRB Memory allocation failed\n");
3240 * ufshcd_host_memory_configure - configure local reference block with
3242 * @hba: per adapter instance
3244 * Configure Host memory space
3245 * 1. Update Corresponding UTRD.UCDBA and UTRD.UCDBAU with UCD DMA
3247 * 2. Update each UTRD with Response UPIU offset, Response UPIU length
3249 * 3. Save the corresponding addresses of UTRD, UCD.CMD, UCD.RSP and UCD.PRDT
3250 * into local reference block.
3252 static void ufshcd_host_memory_configure(struct ufs_hba
*hba
)
3254 struct utp_transfer_cmd_desc
*cmd_descp
;
3255 struct utp_transfer_req_desc
*utrdlp
;
3256 dma_addr_t cmd_desc_dma_addr
;
3257 dma_addr_t cmd_desc_element_addr
;
3258 u16 response_offset
;
3263 utrdlp
= hba
->utrdl_base_addr
;
3264 cmd_descp
= hba
->ucdl_base_addr
;
3267 offsetof(struct utp_transfer_cmd_desc
, response_upiu
);
3269 offsetof(struct utp_transfer_cmd_desc
, prd_table
);
3271 cmd_desc_size
= sizeof(struct utp_transfer_cmd_desc
);
3272 cmd_desc_dma_addr
= hba
->ucdl_dma_addr
;
3274 for (i
= 0; i
< hba
->nutrs
; i
++) {
3275 /* Configure UTRD with command descriptor base address */
3276 cmd_desc_element_addr
=
3277 (cmd_desc_dma_addr
+ (cmd_desc_size
* i
));
3278 utrdlp
[i
].command_desc_base_addr_lo
=
3279 cpu_to_le32(lower_32_bits(cmd_desc_element_addr
));
3280 utrdlp
[i
].command_desc_base_addr_hi
=
3281 cpu_to_le32(upper_32_bits(cmd_desc_element_addr
));
3283 /* Response upiu and prdt offset should be in double words */
3284 if (hba
->quirks
& UFSHCD_QUIRK_PRDT_BYTE_GRAN
) {
3285 utrdlp
[i
].response_upiu_offset
=
3286 cpu_to_le16(response_offset
);
3287 utrdlp
[i
].prd_table_offset
=
3288 cpu_to_le16(prdt_offset
);
3289 utrdlp
[i
].response_upiu_length
=
3290 cpu_to_le16(ALIGNED_UPIU_SIZE
);
3292 utrdlp
[i
].response_upiu_offset
=
3293 cpu_to_le16((response_offset
>> 2));
3294 utrdlp
[i
].prd_table_offset
=
3295 cpu_to_le16((prdt_offset
>> 2));
3296 utrdlp
[i
].response_upiu_length
=
3297 cpu_to_le16(ALIGNED_UPIU_SIZE
>> 2);
3300 hba
->lrb
[i
].utr_descriptor_ptr
= (utrdlp
+ i
);
3301 hba
->lrb
[i
].utrd_dma_addr
= hba
->utrdl_dma_addr
+
3302 (i
* sizeof(struct utp_transfer_req_desc
));
3303 hba
->lrb
[i
].ucd_req_ptr
=
3304 (struct utp_upiu_req
*)(cmd_descp
+ i
);
3305 hba
->lrb
[i
].ucd_req_dma_addr
= cmd_desc_element_addr
;
3306 hba
->lrb
[i
].ucd_rsp_ptr
=
3307 (struct utp_upiu_rsp
*)cmd_descp
[i
].response_upiu
;
3308 hba
->lrb
[i
].ucd_rsp_dma_addr
= cmd_desc_element_addr
+
3310 hba
->lrb
[i
].ucd_prdt_ptr
=
3311 (struct ufshcd_sg_entry
*)cmd_descp
[i
].prd_table
;
3312 hba
->lrb
[i
].ucd_prdt_dma_addr
= cmd_desc_element_addr
+
3318 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3319 * @hba: per adapter instance
3321 * UIC_CMD_DME_LINK_STARTUP command must be issued to Unipro layer,
3322 * in order to initialize the Unipro link startup procedure.
3323 * Once the Unipro links are up, the device connected to the controller
3326 * Returns 0 on success, non-zero value on failure
3328 static int ufshcd_dme_link_startup(struct ufs_hba
*hba
)
3330 struct uic_command uic_cmd
= {0};
3333 uic_cmd
.command
= UIC_CMD_DME_LINK_STARTUP
;
3335 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
3338 "dme-link-startup: error code %d\n", ret
);
3342 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba
*hba
)
3344 #define MIN_DELAY_BEFORE_DME_CMDS_US 1000
3345 unsigned long min_sleep_time_us
;
3347 if (!(hba
->quirks
& UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS
))
3351 * last_dme_cmd_tstamp will be 0 only for 1st call to
3354 if (unlikely(!ktime_to_us(hba
->last_dme_cmd_tstamp
))) {
3355 min_sleep_time_us
= MIN_DELAY_BEFORE_DME_CMDS_US
;
3357 unsigned long delta
=
3358 (unsigned long) ktime_to_us(
3359 ktime_sub(ktime_get(),
3360 hba
->last_dme_cmd_tstamp
));
3362 if (delta
< MIN_DELAY_BEFORE_DME_CMDS_US
)
3364 MIN_DELAY_BEFORE_DME_CMDS_US
- delta
;
3366 return; /* no more delay required */
3369 /* allow sleep for extra 50us if needed */
3370 usleep_range(min_sleep_time_us
, min_sleep_time_us
+ 50);
3374 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3375 * @hba: per adapter instance
3376 * @attr_sel: uic command argument1
3377 * @attr_set: attribute set type as uic command argument2
3378 * @mib_val: setting value as uic command argument3
3379 * @peer: indicate whether peer or local
3381 * Returns 0 on success, non-zero value on failure
3383 int ufshcd_dme_set_attr(struct ufs_hba
*hba
, u32 attr_sel
,
3384 u8 attr_set
, u32 mib_val
, u8 peer
)
3386 struct uic_command uic_cmd
= {0};
3387 static const char *const action
[] = {
3391 const char *set
= action
[!!peer
];
3393 int retries
= UFS_UIC_COMMAND_RETRIES
;
3395 uic_cmd
.command
= peer
?
3396 UIC_CMD_DME_PEER_SET
: UIC_CMD_DME_SET
;
3397 uic_cmd
.argument1
= attr_sel
;
3398 uic_cmd
.argument2
= UIC_ARG_ATTR_TYPE(attr_set
);
3399 uic_cmd
.argument3
= mib_val
;
3402 /* for peer attributes we retry upon failure */
3403 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
3405 dev_dbg(hba
->dev
, "%s: attr-id 0x%x val 0x%x error code %d\n",
3406 set
, UIC_GET_ATTR_ID(attr_sel
), mib_val
, ret
);
3407 } while (ret
&& peer
&& --retries
);
3410 dev_err(hba
->dev
, "%s: attr-id 0x%x val 0x%x failed %d retries\n",
3411 set
, UIC_GET_ATTR_ID(attr_sel
), mib_val
,
3412 UFS_UIC_COMMAND_RETRIES
- retries
);
3416 EXPORT_SYMBOL_GPL(ufshcd_dme_set_attr
);
3419 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
3420 * @hba: per adapter instance
3421 * @attr_sel: uic command argument1
3422 * @mib_val: the value of the attribute as returned by the UIC command
3423 * @peer: indicate whether peer or local
3425 * Returns 0 on success, non-zero value on failure
3427 int ufshcd_dme_get_attr(struct ufs_hba
*hba
, u32 attr_sel
,
3428 u32
*mib_val
, u8 peer
)
3430 struct uic_command uic_cmd
= {0};
3431 static const char *const action
[] = {
3435 const char *get
= action
[!!peer
];
3437 int retries
= UFS_UIC_COMMAND_RETRIES
;
3438 struct ufs_pa_layer_attr orig_pwr_info
;
3439 struct ufs_pa_layer_attr temp_pwr_info
;
3440 bool pwr_mode_change
= false;
3442 if (peer
&& (hba
->quirks
& UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
)) {
3443 orig_pwr_info
= hba
->pwr_info
;
3444 temp_pwr_info
= orig_pwr_info
;
3446 if (orig_pwr_info
.pwr_tx
== FAST_MODE
||
3447 orig_pwr_info
.pwr_rx
== FAST_MODE
) {
3448 temp_pwr_info
.pwr_tx
= FASTAUTO_MODE
;
3449 temp_pwr_info
.pwr_rx
= FASTAUTO_MODE
;
3450 pwr_mode_change
= true;
3451 } else if (orig_pwr_info
.pwr_tx
== SLOW_MODE
||
3452 orig_pwr_info
.pwr_rx
== SLOW_MODE
) {
3453 temp_pwr_info
.pwr_tx
= SLOWAUTO_MODE
;
3454 temp_pwr_info
.pwr_rx
= SLOWAUTO_MODE
;
3455 pwr_mode_change
= true;
3457 if (pwr_mode_change
) {
3458 ret
= ufshcd_change_power_mode(hba
, &temp_pwr_info
);
3464 uic_cmd
.command
= peer
?
3465 UIC_CMD_DME_PEER_GET
: UIC_CMD_DME_GET
;
3466 uic_cmd
.argument1
= attr_sel
;
3469 /* for peer attributes we retry upon failure */
3470 ret
= ufshcd_send_uic_cmd(hba
, &uic_cmd
);
3472 dev_dbg(hba
->dev
, "%s: attr-id 0x%x error code %d\n",
3473 get
, UIC_GET_ATTR_ID(attr_sel
), ret
);
3474 } while (ret
&& peer
&& --retries
);
3477 dev_err(hba
->dev
, "%s: attr-id 0x%x failed %d retries\n",
3478 get
, UIC_GET_ATTR_ID(attr_sel
),
3479 UFS_UIC_COMMAND_RETRIES
- retries
);
3481 if (mib_val
&& !ret
)
3482 *mib_val
= uic_cmd
.argument3
;
3484 if (peer
&& (hba
->quirks
& UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE
)
3486 ufshcd_change_power_mode(hba
, &orig_pwr_info
);
3490 EXPORT_SYMBOL_GPL(ufshcd_dme_get_attr
);
3493 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
3494 * state) and waits for it to take effect.
3496 * @hba: per adapter instance
3497 * @cmd: UIC command to execute
3499 * DME operations like DME_SET(PA_PWRMODE), DME_HIBERNATE_ENTER &
3500 * DME_HIBERNATE_EXIT commands take some time to take its effect on both host
3501 * and device UniPro link and hence it's final completion would be indicated by
3502 * dedicated status bits in Interrupt Status register (UPMS, UHES, UHXS) in
3503 * addition to normal UIC command completion Status (UCCS). This function only
3504 * returns after the relevant status bits indicate the completion.
3506 * Returns 0 on success, non-zero value on failure
3508 static int ufshcd_uic_pwr_ctrl(struct ufs_hba
*hba
, struct uic_command
*cmd
)
3510 struct completion uic_async_done
;
3511 unsigned long flags
;
3514 bool reenable_intr
= false;
3516 mutex_lock(&hba
->uic_cmd_mutex
);
3517 init_completion(&uic_async_done
);
3518 ufshcd_add_delay_before_dme_cmd(hba
);
3520 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3521 hba
->uic_async_done
= &uic_async_done
;
3522 if (ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
) & UIC_COMMAND_COMPL
) {
3523 ufshcd_disable_intr(hba
, UIC_COMMAND_COMPL
);
3525 * Make sure UIC command completion interrupt is disabled before
3526 * issuing UIC command.
3529 reenable_intr
= true;
3531 ret
= __ufshcd_send_uic_cmd(hba
, cmd
, false);
3532 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3535 "pwr ctrl cmd 0x%x with mode 0x%x uic error %d\n",
3536 cmd
->command
, cmd
->argument3
, ret
);
3540 if (!wait_for_completion_timeout(hba
->uic_async_done
,
3541 msecs_to_jiffies(UIC_CMD_TIMEOUT
))) {
3543 "pwr ctrl cmd 0x%x with mode 0x%x completion timeout\n",
3544 cmd
->command
, cmd
->argument3
);
3549 status
= ufshcd_get_upmcrs(hba
);
3550 if (status
!= PWR_LOCAL
) {
3552 "pwr ctrl cmd 0x%0x failed, host upmcrs:0x%x\n",
3553 cmd
->command
, status
);
3554 ret
= (status
!= PWR_OK
) ? status
: -1;
3558 ufshcd_print_host_state(hba
);
3559 ufshcd_print_pwr_info(hba
);
3560 ufshcd_print_host_regs(hba
);
3563 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3564 hba
->active_uic_cmd
= NULL
;
3565 hba
->uic_async_done
= NULL
;
3567 ufshcd_enable_intr(hba
, UIC_COMMAND_COMPL
);
3568 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3569 mutex_unlock(&hba
->uic_cmd_mutex
);
3575 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
3576 * using DME_SET primitives.
3577 * @hba: per adapter instance
3578 * @mode: powr mode value
3580 * Returns 0 on success, non-zero value on failure
3582 static int ufshcd_uic_change_pwr_mode(struct ufs_hba
*hba
, u8 mode
)
3584 struct uic_command uic_cmd
= {0};
3587 if (hba
->quirks
& UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP
) {
3588 ret
= ufshcd_dme_set(hba
,
3589 UIC_ARG_MIB_SEL(PA_RXHSUNTERMCAP
, 0), 1);
3591 dev_err(hba
->dev
, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n",
3597 uic_cmd
.command
= UIC_CMD_DME_SET
;
3598 uic_cmd
.argument1
= UIC_ARG_MIB(PA_PWRMODE
);
3599 uic_cmd
.argument3
= mode
;
3600 ufshcd_hold(hba
, false);
3601 ret
= ufshcd_uic_pwr_ctrl(hba
, &uic_cmd
);
3602 ufshcd_release(hba
);
3608 static int ufshcd_link_recovery(struct ufs_hba
*hba
)
3611 unsigned long flags
;
3613 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3614 hba
->ufshcd_state
= UFSHCD_STATE_RESET
;
3615 ufshcd_set_eh_in_progress(hba
);
3616 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3618 ret
= ufshcd_host_reset_and_restore(hba
);
3620 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
3622 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
3623 ufshcd_clear_eh_in_progress(hba
);
3624 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
3627 dev_err(hba
->dev
, "%s: link recovery failed, err %d",
3633 static int __ufshcd_uic_hibern8_enter(struct ufs_hba
*hba
)
3636 struct uic_command uic_cmd
= {0};
3637 ktime_t start
= ktime_get();
3639 ufshcd_vops_hibern8_notify(hba
, UIC_CMD_DME_HIBER_ENTER
, PRE_CHANGE
);
3641 uic_cmd
.command
= UIC_CMD_DME_HIBER_ENTER
;
3642 ret
= ufshcd_uic_pwr_ctrl(hba
, &uic_cmd
);
3643 trace_ufshcd_profile_hibern8(dev_name(hba
->dev
), "enter",
3644 ktime_to_us(ktime_sub(ktime_get(), start
)), ret
);
3647 dev_err(hba
->dev
, "%s: hibern8 enter failed. ret = %d\n",
3651 * If link recovery fails then return error so that caller
3652 * don't retry the hibern8 enter again.
3654 if (ufshcd_link_recovery(hba
))
3657 ufshcd_vops_hibern8_notify(hba
, UIC_CMD_DME_HIBER_ENTER
,
3663 static int ufshcd_uic_hibern8_enter(struct ufs_hba
*hba
)
3665 int ret
= 0, retries
;
3667 for (retries
= UIC_HIBERN8_ENTER_RETRIES
; retries
> 0; retries
--) {
3668 ret
= __ufshcd_uic_hibern8_enter(hba
);
3669 if (!ret
|| ret
== -ENOLINK
)
3676 static int ufshcd_uic_hibern8_exit(struct ufs_hba
*hba
)
3678 struct uic_command uic_cmd
= {0};
3680 ktime_t start
= ktime_get();
3682 ufshcd_vops_hibern8_notify(hba
, UIC_CMD_DME_HIBER_EXIT
, PRE_CHANGE
);
3684 uic_cmd
.command
= UIC_CMD_DME_HIBER_EXIT
;
3685 ret
= ufshcd_uic_pwr_ctrl(hba
, &uic_cmd
);
3686 trace_ufshcd_profile_hibern8(dev_name(hba
->dev
), "exit",
3687 ktime_to_us(ktime_sub(ktime_get(), start
)), ret
);
3690 dev_err(hba
->dev
, "%s: hibern8 exit failed. ret = %d\n",
3692 ret
= ufshcd_link_recovery(hba
);
3694 ufshcd_vops_hibern8_notify(hba
, UIC_CMD_DME_HIBER_EXIT
,
3696 hba
->ufs_stats
.last_hibern8_exit_tstamp
= ktime_get();
3697 hba
->ufs_stats
.hibern8_exit_cnt
++;
3704 * ufshcd_init_pwr_info - setting the POR (power on reset)
3705 * values in hba power info
3706 * @hba: per-adapter instance
3708 static void ufshcd_init_pwr_info(struct ufs_hba
*hba
)
3710 hba
->pwr_info
.gear_rx
= UFS_PWM_G1
;
3711 hba
->pwr_info
.gear_tx
= UFS_PWM_G1
;
3712 hba
->pwr_info
.lane_rx
= 1;
3713 hba
->pwr_info
.lane_tx
= 1;
3714 hba
->pwr_info
.pwr_rx
= SLOWAUTO_MODE
;
3715 hba
->pwr_info
.pwr_tx
= SLOWAUTO_MODE
;
3716 hba
->pwr_info
.hs_rate
= 0;
3720 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
3721 * @hba: per-adapter instance
3723 static int ufshcd_get_max_pwr_mode(struct ufs_hba
*hba
)
3725 struct ufs_pa_layer_attr
*pwr_info
= &hba
->max_pwr_info
.info
;
3727 if (hba
->max_pwr_info
.is_valid
)
3730 pwr_info
->pwr_tx
= FAST_MODE
;
3731 pwr_info
->pwr_rx
= FAST_MODE
;
3732 pwr_info
->hs_rate
= PA_HS_MODE_B
;
3734 /* Get the connected lane count */
3735 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES
),
3736 &pwr_info
->lane_rx
);
3737 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES
),
3738 &pwr_info
->lane_tx
);
3740 if (!pwr_info
->lane_rx
|| !pwr_info
->lane_tx
) {
3741 dev_err(hba
->dev
, "%s: invalid connected lanes value. rx=%d, tx=%d\n",
3749 * First, get the maximum gears of HS speed.
3750 * If a zero value, it means there is no HSGEAR capability.
3751 * Then, get the maximum gears of PWM speed.
3753 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_MAXRXHSGEAR
), &pwr_info
->gear_rx
);
3754 if (!pwr_info
->gear_rx
) {
3755 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_MAXRXPWMGEAR
),
3756 &pwr_info
->gear_rx
);
3757 if (!pwr_info
->gear_rx
) {
3758 dev_err(hba
->dev
, "%s: invalid max pwm rx gear read = %d\n",
3759 __func__
, pwr_info
->gear_rx
);
3762 pwr_info
->pwr_rx
= SLOW_MODE
;
3765 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_MAXRXHSGEAR
),
3766 &pwr_info
->gear_tx
);
3767 if (!pwr_info
->gear_tx
) {
3768 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_MAXRXPWMGEAR
),
3769 &pwr_info
->gear_tx
);
3770 if (!pwr_info
->gear_tx
) {
3771 dev_err(hba
->dev
, "%s: invalid max pwm tx gear read = %d\n",
3772 __func__
, pwr_info
->gear_tx
);
3775 pwr_info
->pwr_tx
= SLOW_MODE
;
3778 hba
->max_pwr_info
.is_valid
= true;
3782 static int ufshcd_change_power_mode(struct ufs_hba
*hba
,
3783 struct ufs_pa_layer_attr
*pwr_mode
)
3787 /* if already configured to the requested pwr_mode */
3788 if (pwr_mode
->gear_rx
== hba
->pwr_info
.gear_rx
&&
3789 pwr_mode
->gear_tx
== hba
->pwr_info
.gear_tx
&&
3790 pwr_mode
->lane_rx
== hba
->pwr_info
.lane_rx
&&
3791 pwr_mode
->lane_tx
== hba
->pwr_info
.lane_tx
&&
3792 pwr_mode
->pwr_rx
== hba
->pwr_info
.pwr_rx
&&
3793 pwr_mode
->pwr_tx
== hba
->pwr_info
.pwr_tx
&&
3794 pwr_mode
->hs_rate
== hba
->pwr_info
.hs_rate
) {
3795 dev_dbg(hba
->dev
, "%s: power already configured\n", __func__
);
3800 * Configure attributes for power mode change with below.
3801 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION,
3802 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION,
3805 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXGEAR
), pwr_mode
->gear_rx
);
3806 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_ACTIVERXDATALANES
),
3808 if (pwr_mode
->pwr_rx
== FASTAUTO_MODE
||
3809 pwr_mode
->pwr_rx
== FAST_MODE
)
3810 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXTERMINATION
), TRUE
);
3812 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_RXTERMINATION
), FALSE
);
3814 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXGEAR
), pwr_mode
->gear_tx
);
3815 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_ACTIVETXDATALANES
),
3817 if (pwr_mode
->pwr_tx
== FASTAUTO_MODE
||
3818 pwr_mode
->pwr_tx
== FAST_MODE
)
3819 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXTERMINATION
), TRUE
);
3821 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TXTERMINATION
), FALSE
);
3823 if (pwr_mode
->pwr_rx
== FASTAUTO_MODE
||
3824 pwr_mode
->pwr_tx
== FASTAUTO_MODE
||
3825 pwr_mode
->pwr_rx
== FAST_MODE
||
3826 pwr_mode
->pwr_tx
== FAST_MODE
)
3827 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_HSSERIES
),
3830 ret
= ufshcd_uic_change_pwr_mode(hba
, pwr_mode
->pwr_rx
<< 4
3831 | pwr_mode
->pwr_tx
);
3835 "%s: power mode change failed %d\n", __func__
, ret
);
3837 ufshcd_vops_pwr_change_notify(hba
, POST_CHANGE
, NULL
,
3840 memcpy(&hba
->pwr_info
, pwr_mode
,
3841 sizeof(struct ufs_pa_layer_attr
));
3848 * ufshcd_config_pwr_mode - configure a new power mode
3849 * @hba: per-adapter instance
3850 * @desired_pwr_mode: desired power configuration
3852 static int ufshcd_config_pwr_mode(struct ufs_hba
*hba
,
3853 struct ufs_pa_layer_attr
*desired_pwr_mode
)
3855 struct ufs_pa_layer_attr final_params
= { 0 };
3858 ret
= ufshcd_vops_pwr_change_notify(hba
, PRE_CHANGE
,
3859 desired_pwr_mode
, &final_params
);
3862 memcpy(&final_params
, desired_pwr_mode
, sizeof(final_params
));
3864 ret
= ufshcd_change_power_mode(hba
, &final_params
);
3866 ufshcd_print_pwr_info(hba
);
3872 * ufshcd_complete_dev_init() - checks device readiness
3873 * hba: per-adapter instance
3875 * Set fDeviceInit flag and poll until device toggles it.
3877 static int ufshcd_complete_dev_init(struct ufs_hba
*hba
)
3883 err
= ufshcd_query_flag_retry(hba
, UPIU_QUERY_OPCODE_SET_FLAG
,
3884 QUERY_FLAG_IDN_FDEVICEINIT
, NULL
);
3887 "%s setting fDeviceInit flag failed with error %d\n",
3892 /* poll for max. 1000 iterations for fDeviceInit flag to clear */
3893 for (i
= 0; i
< 1000 && !err
&& flag_res
; i
++)
3894 err
= ufshcd_query_flag_retry(hba
, UPIU_QUERY_OPCODE_READ_FLAG
,
3895 QUERY_FLAG_IDN_FDEVICEINIT
, &flag_res
);
3899 "%s reading fDeviceInit flag failed with error %d\n",
3903 "%s fDeviceInit was not cleared by the device\n",
3911 * ufshcd_make_hba_operational - Make UFS controller operational
3912 * @hba: per adapter instance
3914 * To bring UFS host controller to operational state,
3915 * 1. Enable required interrupts
3916 * 2. Configure interrupt aggregation
3917 * 3. Program UTRL and UTMRL base address
3918 * 4. Configure run-stop-registers
3920 * Returns 0 on success, non-zero value on failure
3922 static int ufshcd_make_hba_operational(struct ufs_hba
*hba
)
3927 /* Enable required interrupts */
3928 ufshcd_enable_intr(hba
, UFSHCD_ENABLE_INTRS
);
3930 /* Configure interrupt aggregation */
3931 if (ufshcd_is_intr_aggr_allowed(hba
))
3932 ufshcd_config_intr_aggr(hba
, hba
->nutrs
- 1, INT_AGGR_DEF_TO
);
3934 ufshcd_disable_intr_aggr(hba
);
3936 /* Configure UTRL and UTMRL base address registers */
3937 ufshcd_writel(hba
, lower_32_bits(hba
->utrdl_dma_addr
),
3938 REG_UTP_TRANSFER_REQ_LIST_BASE_L
);
3939 ufshcd_writel(hba
, upper_32_bits(hba
->utrdl_dma_addr
),
3940 REG_UTP_TRANSFER_REQ_LIST_BASE_H
);
3941 ufshcd_writel(hba
, lower_32_bits(hba
->utmrdl_dma_addr
),
3942 REG_UTP_TASK_REQ_LIST_BASE_L
);
3943 ufshcd_writel(hba
, upper_32_bits(hba
->utmrdl_dma_addr
),
3944 REG_UTP_TASK_REQ_LIST_BASE_H
);
3947 * Make sure base address and interrupt setup are updated before
3948 * enabling the run/stop registers below.
3953 * UCRDY, UTMRLDY and UTRLRDY bits must be 1
3955 reg
= ufshcd_readl(hba
, REG_CONTROLLER_STATUS
);
3956 if (!(ufshcd_get_lists_status(reg
))) {
3957 ufshcd_enable_run_stop_reg(hba
);
3960 "Host controller not ready to process requests");
3970 * ufshcd_hba_stop - Send controller to reset state
3971 * @hba: per adapter instance
3972 * @can_sleep: perform sleep or just spin
3974 static inline void ufshcd_hba_stop(struct ufs_hba
*hba
, bool can_sleep
)
3978 ufshcd_writel(hba
, CONTROLLER_DISABLE
, REG_CONTROLLER_ENABLE
);
3979 err
= ufshcd_wait_for_register(hba
, REG_CONTROLLER_ENABLE
,
3980 CONTROLLER_ENABLE
, CONTROLLER_DISABLE
,
3983 dev_err(hba
->dev
, "%s: Controller disable failed\n", __func__
);
3987 * ufshcd_hba_enable - initialize the controller
3988 * @hba: per adapter instance
3990 * The controller resets itself and controller firmware initialization
3991 * sequence kicks off. When controller is ready it will set
3992 * the Host Controller Enable bit to 1.
3994 * Returns 0 on success, non-zero value on failure
3996 static int ufshcd_hba_enable(struct ufs_hba
*hba
)
4001 * msleep of 1 and 5 used in this function might result in msleep(20),
4002 * but it was necessary to send the UFS FPGA to reset mode during
4003 * development and testing of this driver. msleep can be changed to
4004 * mdelay and retry count can be reduced based on the controller.
4006 if (!ufshcd_is_hba_active(hba
))
4007 /* change controller state to "reset state" */
4008 ufshcd_hba_stop(hba
, true);
4010 /* UniPro link is disabled at this point */
4011 ufshcd_set_link_off(hba
);
4013 ufshcd_vops_hce_enable_notify(hba
, PRE_CHANGE
);
4015 /* start controller initialization sequence */
4016 ufshcd_hba_start(hba
);
4019 * To initialize a UFS host controller HCE bit must be set to 1.
4020 * During initialization the HCE bit value changes from 1->0->1.
4021 * When the host controller completes initialization sequence
4022 * it sets the value of HCE bit to 1. The same HCE bit is read back
4023 * to check if the controller has completed initialization sequence.
4024 * So without this delay the value HCE = 1, set in the previous
4025 * instruction might be read back.
4026 * This delay can be changed based on the controller.
4030 /* wait for the host controller to complete initialization */
4032 while (ufshcd_is_hba_active(hba
)) {
4037 "Controller enable failed\n");
4043 /* enable UIC related interrupts */
4044 ufshcd_enable_intr(hba
, UFSHCD_UIC_MASK
);
4046 ufshcd_vops_hce_enable_notify(hba
, POST_CHANGE
);
4051 static int ufshcd_disable_tx_lcc(struct ufs_hba
*hba
, bool peer
)
4053 int tx_lanes
, i
, err
= 0;
4056 ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES
),
4059 ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES
),
4061 for (i
= 0; i
< tx_lanes
; i
++) {
4063 err
= ufshcd_dme_set(hba
,
4064 UIC_ARG_MIB_SEL(TX_LCC_ENABLE
,
4065 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i
)),
4068 err
= ufshcd_dme_peer_set(hba
,
4069 UIC_ARG_MIB_SEL(TX_LCC_ENABLE
,
4070 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(i
)),
4073 dev_err(hba
->dev
, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d",
4074 __func__
, peer
, i
, err
);
4082 static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba
*hba
)
4084 return ufshcd_disable_tx_lcc(hba
, true);
4088 * ufshcd_link_startup - Initialize unipro link startup
4089 * @hba: per adapter instance
4091 * Returns 0 for success, non-zero in case of failure
4093 static int ufshcd_link_startup(struct ufs_hba
*hba
)
4096 int retries
= DME_LINKSTARTUP_RETRIES
;
4097 bool link_startup_again
= false;
4100 * If UFS device isn't active then we will have to issue link startup
4101 * 2 times to make sure the device state move to active.
4103 if (!ufshcd_is_ufs_dev_active(hba
))
4104 link_startup_again
= true;
4108 ufshcd_vops_link_startup_notify(hba
, PRE_CHANGE
);
4110 ret
= ufshcd_dme_link_startup(hba
);
4112 /* check if device is detected by inter-connect layer */
4113 if (!ret
&& !ufshcd_is_device_present(hba
)) {
4114 dev_err(hba
->dev
, "%s: Device not present\n", __func__
);
4120 * DME link lost indication is only received when link is up,
4121 * but we can't be sure if the link is up until link startup
4122 * succeeds. So reset the local Uni-Pro and try again.
4124 if (ret
&& ufshcd_hba_enable(hba
))
4126 } while (ret
&& retries
--);
4129 /* failed to get the link up... retire */
4132 if (link_startup_again
) {
4133 link_startup_again
= false;
4134 retries
= DME_LINKSTARTUP_RETRIES
;
4138 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */
4139 ufshcd_init_pwr_info(hba
);
4140 ufshcd_print_pwr_info(hba
);
4142 if (hba
->quirks
& UFSHCD_QUIRK_BROKEN_LCC
) {
4143 ret
= ufshcd_disable_device_tx_lcc(hba
);
4148 /* Include any host controller configuration via UIC commands */
4149 ret
= ufshcd_vops_link_startup_notify(hba
, POST_CHANGE
);
4153 ret
= ufshcd_make_hba_operational(hba
);
4156 dev_err(hba
->dev
, "link startup failed %d\n", ret
);
4157 ufshcd_print_host_state(hba
);
4158 ufshcd_print_pwr_info(hba
);
4159 ufshcd_print_host_regs(hba
);
4165 * ufshcd_verify_dev_init() - Verify device initialization
4166 * @hba: per-adapter instance
4168 * Send NOP OUT UPIU and wait for NOP IN response to check whether the
4169 * device Transport Protocol (UTP) layer is ready after a reset.
4170 * If the UTP layer at the device side is not initialized, it may
4171 * not respond with NOP IN UPIU within timeout of %NOP_OUT_TIMEOUT
4172 * and we retry sending NOP OUT for %NOP_OUT_RETRIES iterations.
4174 static int ufshcd_verify_dev_init(struct ufs_hba
*hba
)
4179 ufshcd_hold(hba
, false);
4180 mutex_lock(&hba
->dev_cmd
.lock
);
4181 for (retries
= NOP_OUT_RETRIES
; retries
> 0; retries
--) {
4182 err
= ufshcd_exec_dev_cmd(hba
, DEV_CMD_TYPE_NOP
,
4185 if (!err
|| err
== -ETIMEDOUT
)
4188 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, err
);
4190 mutex_unlock(&hba
->dev_cmd
.lock
);
4191 ufshcd_release(hba
);
4194 dev_err(hba
->dev
, "%s: NOP OUT failed %d\n", __func__
, err
);
4199 * ufshcd_set_queue_depth - set lun queue depth
4200 * @sdev: pointer to SCSI device
4202 * Read bLUQueueDepth value and activate scsi tagged command
4203 * queueing. For WLUN, queue depth is set to 1. For best-effort
4204 * cases (bLUQueueDepth = 0) the queue depth is set to a maximum
4205 * value that host can queue.
4207 static void ufshcd_set_queue_depth(struct scsi_device
*sdev
)
4212 struct ufs_hba
*hba
;
4214 hba
= shost_priv(sdev
->host
);
4216 lun_qdepth
= hba
->nutrs
;
4217 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
4218 /* Read descriptor*/
4219 ret
= ufshcd_read_unit_desc_param(hba
,
4220 ufshcd_scsi_to_upiu_lun(sdev
->lun
),
4221 UNIT_DESC_PARAM_LU_Q_DEPTH
,
4223 sizeof(lun_qdepth
));
4224 if (!ret
|| ret
== -ENOTSUPP
)
4227 dev_dbg(hba
->dev
, "%s: error %d retrying\n", __func__
, ret
);
4230 /* Some WLUN doesn't support unit descriptor */
4231 if (ret
== -EOPNOTSUPP
)
4233 else if (!lun_qdepth
)
4234 /* eventually, we can figure out the real queue depth */
4235 lun_qdepth
= hba
->nutrs
;
4237 lun_qdepth
= min_t(int, lun_qdepth
, hba
->nutrs
);
4239 dev_dbg(hba
->dev
, "%s: activate tcq with queue depth %d\n",
4240 __func__
, lun_qdepth
);
4241 scsi_change_queue_depth(sdev
, lun_qdepth
);
4245 * ufshcd_get_lu_wp - returns the "b_lu_write_protect" from UNIT DESCRIPTOR
4246 * @hba: per-adapter instance
4247 * @lun: UFS device lun id
4248 * @b_lu_write_protect: pointer to buffer to hold the LU's write protect info
4250 * Returns 0 in case of success and b_lu_write_protect status would be returned
4251 * @b_lu_write_protect parameter.
4252 * Returns -ENOTSUPP if reading b_lu_write_protect is not supported.
4253 * Returns -EINVAL in case of invalid parameters passed to this function.
4255 static int ufshcd_get_lu_wp(struct ufs_hba
*hba
,
4257 u8
*b_lu_write_protect
)
4261 if (!b_lu_write_protect
)
4264 * According to UFS device spec, RPMB LU can't be write
4265 * protected so skip reading bLUWriteProtect parameter for
4266 * it. For other W-LUs, UNIT DESCRIPTOR is not available.
4268 else if (lun
>= UFS_UPIU_MAX_GENERAL_LUN
)
4271 ret
= ufshcd_read_unit_desc_param(hba
,
4273 UNIT_DESC_PARAM_LU_WR_PROTECT
,
4275 sizeof(*b_lu_write_protect
));
4280 * ufshcd_get_lu_power_on_wp_status - get LU's power on write protect
4282 * @hba: per-adapter instance
4283 * @sdev: pointer to SCSI device
4286 static inline void ufshcd_get_lu_power_on_wp_status(struct ufs_hba
*hba
,
4287 struct scsi_device
*sdev
)
4289 if (hba
->dev_info
.f_power_on_wp_en
&&
4290 !hba
->dev_info
.is_lu_power_on_wp
) {
4291 u8 b_lu_write_protect
;
4293 if (!ufshcd_get_lu_wp(hba
, ufshcd_scsi_to_upiu_lun(sdev
->lun
),
4294 &b_lu_write_protect
) &&
4295 (b_lu_write_protect
== UFS_LU_POWER_ON_WP
))
4296 hba
->dev_info
.is_lu_power_on_wp
= true;
4301 * ufshcd_slave_alloc - handle initial SCSI device configurations
4302 * @sdev: pointer to SCSI device
4306 static int ufshcd_slave_alloc(struct scsi_device
*sdev
)
4308 struct ufs_hba
*hba
;
4310 hba
= shost_priv(sdev
->host
);
4312 /* Mode sense(6) is not supported by UFS, so use Mode sense(10) */
4313 sdev
->use_10_for_ms
= 1;
4315 /* allow SCSI layer to restart the device in case of errors */
4316 sdev
->allow_restart
= 1;
4318 /* REPORT SUPPORTED OPERATION CODES is not supported */
4319 sdev
->no_report_opcodes
= 1;
4322 ufshcd_set_queue_depth(sdev
);
4324 ufshcd_get_lu_power_on_wp_status(hba
, sdev
);
4330 * ufshcd_change_queue_depth - change queue depth
4331 * @sdev: pointer to SCSI device
4332 * @depth: required depth to set
4334 * Change queue depth and make sure the max. limits are not crossed.
4336 static int ufshcd_change_queue_depth(struct scsi_device
*sdev
, int depth
)
4338 struct ufs_hba
*hba
= shost_priv(sdev
->host
);
4340 if (depth
> hba
->nutrs
)
4342 return scsi_change_queue_depth(sdev
, depth
);
4346 * ufshcd_slave_configure - adjust SCSI device configurations
4347 * @sdev: pointer to SCSI device
4349 static int ufshcd_slave_configure(struct scsi_device
*sdev
)
4351 struct request_queue
*q
= sdev
->request_queue
;
4353 blk_queue_update_dma_pad(q
, PRDT_DATA_BYTE_COUNT_PAD
- 1);
4354 blk_queue_max_segment_size(q
, PRDT_DATA_BYTE_COUNT_MAX
);
4360 * ufshcd_slave_destroy - remove SCSI device configurations
4361 * @sdev: pointer to SCSI device
4363 static void ufshcd_slave_destroy(struct scsi_device
*sdev
)
4365 struct ufs_hba
*hba
;
4367 hba
= shost_priv(sdev
->host
);
4368 /* Drop the reference as it won't be needed anymore */
4369 if (ufshcd_scsi_to_upiu_lun(sdev
->lun
) == UFS_UPIU_UFS_DEVICE_WLUN
) {
4370 unsigned long flags
;
4372 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
4373 hba
->sdev_ufs_device
= NULL
;
4374 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
4379 * ufshcd_task_req_compl - handle task management request completion
4380 * @hba: per adapter instance
4381 * @index: index of the completed request
4382 * @resp: task management service response
4384 * Returns non-zero value on error, zero on success
4386 static int ufshcd_task_req_compl(struct ufs_hba
*hba
, u32 index
, u8
*resp
)
4388 struct utp_task_req_desc
*task_req_descp
;
4389 struct utp_upiu_task_rsp
*task_rsp_upiup
;
4390 unsigned long flags
;
4394 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
4396 /* Clear completed tasks from outstanding_tasks */
4397 __clear_bit(index
, &hba
->outstanding_tasks
);
4399 task_req_descp
= hba
->utmrdl_base_addr
;
4400 ocs_value
= ufshcd_get_tmr_ocs(&task_req_descp
[index
]);
4402 if (ocs_value
== OCS_SUCCESS
) {
4403 task_rsp_upiup
= (struct utp_upiu_task_rsp
*)
4404 task_req_descp
[index
].task_rsp_upiu
;
4405 task_result
= be32_to_cpu(task_rsp_upiup
->output_param1
);
4406 task_result
= task_result
& MASK_TM_SERVICE_RESP
;
4408 *resp
= (u8
)task_result
;
4410 dev_err(hba
->dev
, "%s: failed, ocs = 0x%x\n",
4411 __func__
, ocs_value
);
4413 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
4419 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
4420 * @lrb: pointer to local reference block of completed command
4421 * @scsi_status: SCSI command status
4423 * Returns value base on SCSI command status
4426 ufshcd_scsi_cmd_status(struct ufshcd_lrb
*lrbp
, int scsi_status
)
4430 switch (scsi_status
) {
4431 case SAM_STAT_CHECK_CONDITION
:
4432 ufshcd_copy_sense_data(lrbp
);
4434 result
|= DID_OK
<< 16 |
4435 COMMAND_COMPLETE
<< 8 |
4438 case SAM_STAT_TASK_SET_FULL
:
4440 case SAM_STAT_TASK_ABORTED
:
4441 ufshcd_copy_sense_data(lrbp
);
4442 result
|= scsi_status
;
4445 result
|= DID_ERROR
<< 16;
4447 } /* end of switch */
4453 * ufshcd_transfer_rsp_status - Get overall status of the response
4454 * @hba: per adapter instance
4455 * @lrb: pointer to local reference block of completed command
4457 * Returns result of the command to notify SCSI midlayer
4460 ufshcd_transfer_rsp_status(struct ufs_hba
*hba
, struct ufshcd_lrb
*lrbp
)
4466 /* overall command status of utrd */
4467 ocs
= ufshcd_get_tr_ocs(lrbp
);
4471 result
= ufshcd_get_req_rsp(lrbp
->ucd_rsp_ptr
);
4472 hba
->ufs_stats
.last_hibern8_exit_tstamp
= ktime_set(0, 0);
4474 case UPIU_TRANSACTION_RESPONSE
:
4476 * get the response UPIU result to extract
4477 * the SCSI command status
4479 result
= ufshcd_get_rsp_upiu_result(lrbp
->ucd_rsp_ptr
);
4482 * get the result based on SCSI status response
4483 * to notify the SCSI midlayer of the command status
4485 scsi_status
= result
& MASK_SCSI_STATUS
;
4486 result
= ufshcd_scsi_cmd_status(lrbp
, scsi_status
);
4489 * Currently we are only supporting BKOPs exception
4490 * events hence we can ignore BKOPs exception event
4491 * during power management callbacks. BKOPs exception
4492 * event is not expected to be raised in runtime suspend
4493 * callback as it allows the urgent bkops.
4494 * During system suspend, we are anyway forcefully
4495 * disabling the bkops and if urgent bkops is needed
4496 * it will be enabled on system resume. Long term
4497 * solution could be to abort the system suspend if
4498 * UFS device needs urgent BKOPs.
4500 if (!hba
->pm_op_in_progress
&&
4501 ufshcd_is_exception_event(lrbp
->ucd_rsp_ptr
))
4502 schedule_work(&hba
->eeh_work
);
4504 case UPIU_TRANSACTION_REJECT_UPIU
:
4505 /* TODO: handle Reject UPIU Response */
4506 result
= DID_ERROR
<< 16;
4508 "Reject UPIU not fully implemented\n");
4511 result
= DID_ERROR
<< 16;
4513 "Unexpected request response code = %x\n",
4519 result
|= DID_ABORT
<< 16;
4521 case OCS_INVALID_COMMAND_STATUS
:
4522 result
|= DID_REQUEUE
<< 16;
4524 case OCS_INVALID_CMD_TABLE_ATTR
:
4525 case OCS_INVALID_PRDT_ATTR
:
4526 case OCS_MISMATCH_DATA_BUF_SIZE
:
4527 case OCS_MISMATCH_RESP_UPIU_SIZE
:
4528 case OCS_PEER_COMM_FAILURE
:
4529 case OCS_FATAL_ERROR
:
4531 result
|= DID_ERROR
<< 16;
4533 "OCS error from controller = %x for tag %d\n",
4534 ocs
, lrbp
->task_tag
);
4535 ufshcd_print_host_regs(hba
);
4536 ufshcd_print_host_state(hba
);
4538 } /* end of switch */
4540 if (host_byte(result
) != DID_OK
)
4541 ufshcd_print_trs(hba
, 1 << lrbp
->task_tag
, true);
4546 * ufshcd_uic_cmd_compl - handle completion of uic command
4547 * @hba: per adapter instance
4548 * @intr_status: interrupt status generated by the controller
4550 static void ufshcd_uic_cmd_compl(struct ufs_hba
*hba
, u32 intr_status
)
4552 if ((intr_status
& UIC_COMMAND_COMPL
) && hba
->active_uic_cmd
) {
4553 hba
->active_uic_cmd
->argument2
|=
4554 ufshcd_get_uic_cmd_result(hba
);
4555 hba
->active_uic_cmd
->argument3
=
4556 ufshcd_get_dme_attr_val(hba
);
4557 complete(&hba
->active_uic_cmd
->done
);
4560 if ((intr_status
& UFSHCD_UIC_PWR_MASK
) && hba
->uic_async_done
)
4561 complete(hba
->uic_async_done
);
4565 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
4566 * @hba: per adapter instance
4567 * @completed_reqs: requests to complete
4569 static void __ufshcd_transfer_req_compl(struct ufs_hba
*hba
,
4570 unsigned long completed_reqs
)
4572 struct ufshcd_lrb
*lrbp
;
4573 struct scsi_cmnd
*cmd
;
4577 for_each_set_bit(index
, &completed_reqs
, hba
->nutrs
) {
4578 lrbp
= &hba
->lrb
[index
];
4581 ufshcd_add_command_trace(hba
, index
, "complete");
4582 result
= ufshcd_transfer_rsp_status(hba
, lrbp
);
4583 scsi_dma_unmap(cmd
);
4584 cmd
->result
= result
;
4585 /* Mark completed command as NULL in LRB */
4587 clear_bit_unlock(index
, &hba
->lrb_in_use
);
4588 /* Do not touch lrbp after scsi done */
4589 cmd
->scsi_done(cmd
);
4590 __ufshcd_release(hba
);
4591 } else if (lrbp
->command_type
== UTP_CMD_TYPE_DEV_MANAGE
||
4592 lrbp
->command_type
== UTP_CMD_TYPE_UFS_STORAGE
) {
4593 if (hba
->dev_cmd
.complete
) {
4594 ufshcd_add_command_trace(hba
, index
,
4596 complete(hba
->dev_cmd
.complete
);
4599 if (ufshcd_is_clkscaling_supported(hba
))
4600 hba
->clk_scaling
.active_reqs
--;
4601 if (ufshcd_is_clkscaling_supported(hba
))
4602 hba
->clk_scaling
.active_reqs
--;
4605 /* clear corresponding bits of completed commands */
4606 hba
->outstanding_reqs
^= completed_reqs
;
4608 ufshcd_clk_scaling_update_busy(hba
);
4610 /* we might have free'd some tags above */
4611 wake_up(&hba
->dev_cmd
.tag_wq
);
4615 * ufshcd_transfer_req_compl - handle SCSI and query command completion
4616 * @hba: per adapter instance
4618 static void ufshcd_transfer_req_compl(struct ufs_hba
*hba
)
4620 unsigned long completed_reqs
;
4623 /* Resetting interrupt aggregation counters first and reading the
4624 * DOOR_BELL afterward allows us to handle all the completed requests.
4625 * In order to prevent other interrupts starvation the DB is read once
4626 * after reset. The down side of this solution is the possibility of
4627 * false interrupt if device completes another request after resetting
4628 * aggregation and before reading the DB.
4630 if (ufshcd_is_intr_aggr_allowed(hba
))
4631 ufshcd_reset_intr_aggr(hba
);
4633 tr_doorbell
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
4634 completed_reqs
= tr_doorbell
^ hba
->outstanding_reqs
;
4636 __ufshcd_transfer_req_compl(hba
, completed_reqs
);
4640 * ufshcd_disable_ee - disable exception event
4641 * @hba: per-adapter instance
4642 * @mask: exception event to disable
4644 * Disables exception event in the device so that the EVENT_ALERT
4647 * Returns zero on success, non-zero error value on failure.
4649 static int ufshcd_disable_ee(struct ufs_hba
*hba
, u16 mask
)
4654 if (!(hba
->ee_ctrl_mask
& mask
))
4657 val
= hba
->ee_ctrl_mask
& ~mask
;
4658 val
&= 0xFFFF; /* 2 bytes */
4659 err
= ufshcd_query_attr_retry(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
4660 QUERY_ATTR_IDN_EE_CONTROL
, 0, 0, &val
);
4662 hba
->ee_ctrl_mask
&= ~mask
;
4668 * ufshcd_enable_ee - enable exception event
4669 * @hba: per-adapter instance
4670 * @mask: exception event to enable
4672 * Enable corresponding exception event in the device to allow
4673 * device to alert host in critical scenarios.
4675 * Returns zero on success, non-zero error value on failure.
4677 static int ufshcd_enable_ee(struct ufs_hba
*hba
, u16 mask
)
4682 if (hba
->ee_ctrl_mask
& mask
)
4685 val
= hba
->ee_ctrl_mask
| mask
;
4686 val
&= 0xFFFF; /* 2 bytes */
4687 err
= ufshcd_query_attr_retry(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
4688 QUERY_ATTR_IDN_EE_CONTROL
, 0, 0, &val
);
4690 hba
->ee_ctrl_mask
|= mask
;
4696 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
4697 * @hba: per-adapter instance
4699 * Allow device to manage background operations on its own. Enabling
4700 * this might lead to inconsistent latencies during normal data transfers
4701 * as the device is allowed to manage its own way of handling background
4704 * Returns zero on success, non-zero on failure.
4706 static int ufshcd_enable_auto_bkops(struct ufs_hba
*hba
)
4710 if (hba
->auto_bkops_enabled
)
4713 err
= ufshcd_query_flag_retry(hba
, UPIU_QUERY_OPCODE_SET_FLAG
,
4714 QUERY_FLAG_IDN_BKOPS_EN
, NULL
);
4716 dev_err(hba
->dev
, "%s: failed to enable bkops %d\n",
4721 hba
->auto_bkops_enabled
= true;
4722 trace_ufshcd_auto_bkops_state(dev_name(hba
->dev
), "Enabled");
4724 /* No need of URGENT_BKOPS exception from the device */
4725 err
= ufshcd_disable_ee(hba
, MASK_EE_URGENT_BKOPS
);
4727 dev_err(hba
->dev
, "%s: failed to disable exception event %d\n",
4734 * ufshcd_disable_auto_bkops - block device in doing background operations
4735 * @hba: per-adapter instance
4737 * Disabling background operations improves command response latency but
4738 * has drawback of device moving into critical state where the device is
4739 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
4740 * host is idle so that BKOPS are managed effectively without any negative
4743 * Returns zero on success, non-zero on failure.
4745 static int ufshcd_disable_auto_bkops(struct ufs_hba
*hba
)
4749 if (!hba
->auto_bkops_enabled
)
4753 * If host assisted BKOPs is to be enabled, make sure
4754 * urgent bkops exception is allowed.
4756 err
= ufshcd_enable_ee(hba
, MASK_EE_URGENT_BKOPS
);
4758 dev_err(hba
->dev
, "%s: failed to enable exception event %d\n",
4763 err
= ufshcd_query_flag_retry(hba
, UPIU_QUERY_OPCODE_CLEAR_FLAG
,
4764 QUERY_FLAG_IDN_BKOPS_EN
, NULL
);
4766 dev_err(hba
->dev
, "%s: failed to disable bkops %d\n",
4768 ufshcd_disable_ee(hba
, MASK_EE_URGENT_BKOPS
);
4772 hba
->auto_bkops_enabled
= false;
4773 trace_ufshcd_auto_bkops_state(dev_name(hba
->dev
), "Disabled");
4779 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
4780 * @hba: per adapter instance
4782 * After a device reset the device may toggle the BKOPS_EN flag
4783 * to default value. The s/w tracking variables should be updated
4784 * as well. This function would change the auto-bkops state based on
4785 * UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND.
4787 static void ufshcd_force_reset_auto_bkops(struct ufs_hba
*hba
)
4789 if (ufshcd_keep_autobkops_enabled_except_suspend(hba
)) {
4790 hba
->auto_bkops_enabled
= false;
4791 hba
->ee_ctrl_mask
|= MASK_EE_URGENT_BKOPS
;
4792 ufshcd_enable_auto_bkops(hba
);
4794 hba
->auto_bkops_enabled
= true;
4795 hba
->ee_ctrl_mask
&= ~MASK_EE_URGENT_BKOPS
;
4796 ufshcd_disable_auto_bkops(hba
);
4800 static inline int ufshcd_get_bkops_status(struct ufs_hba
*hba
, u32
*status
)
4802 return ufshcd_query_attr_retry(hba
, UPIU_QUERY_OPCODE_READ_ATTR
,
4803 QUERY_ATTR_IDN_BKOPS_STATUS
, 0, 0, status
);
4807 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
4808 * @hba: per-adapter instance
4809 * @status: bkops_status value
4811 * Read the bkops_status from the UFS device and Enable fBackgroundOpsEn
4812 * flag in the device to permit background operations if the device
4813 * bkops_status is greater than or equal to "status" argument passed to
4814 * this function, disable otherwise.
4816 * Returns 0 for success, non-zero in case of failure.
4818 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
4819 * to know whether auto bkops is enabled or disabled after this function
4820 * returns control to it.
4822 static int ufshcd_bkops_ctrl(struct ufs_hba
*hba
,
4823 enum bkops_status status
)
4826 u32 curr_status
= 0;
4828 err
= ufshcd_get_bkops_status(hba
, &curr_status
);
4830 dev_err(hba
->dev
, "%s: failed to get BKOPS status %d\n",
4833 } else if (curr_status
> BKOPS_STATUS_MAX
) {
4834 dev_err(hba
->dev
, "%s: invalid BKOPS status %d\n",
4835 __func__
, curr_status
);
4840 if (curr_status
>= status
)
4841 err
= ufshcd_enable_auto_bkops(hba
);
4843 err
= ufshcd_disable_auto_bkops(hba
);
4849 * ufshcd_urgent_bkops - handle urgent bkops exception event
4850 * @hba: per-adapter instance
4852 * Enable fBackgroundOpsEn flag in the device to permit background
4855 * If BKOPs is enabled, this function returns 0, 1 if the bkops in not enabled
4856 * and negative error value for any other failure.
4858 static int ufshcd_urgent_bkops(struct ufs_hba
*hba
)
4860 return ufshcd_bkops_ctrl(hba
, hba
->urgent_bkops_lvl
);
4863 static inline int ufshcd_get_ee_status(struct ufs_hba
*hba
, u32
*status
)
4865 return ufshcd_query_attr_retry(hba
, UPIU_QUERY_OPCODE_READ_ATTR
,
4866 QUERY_ATTR_IDN_EE_STATUS
, 0, 0, status
);
4869 static void ufshcd_bkops_exception_event_handler(struct ufs_hba
*hba
)
4872 u32 curr_status
= 0;
4874 if (hba
->is_urgent_bkops_lvl_checked
)
4875 goto enable_auto_bkops
;
4877 err
= ufshcd_get_bkops_status(hba
, &curr_status
);
4879 dev_err(hba
->dev
, "%s: failed to get BKOPS status %d\n",
4885 * We are seeing that some devices are raising the urgent bkops
4886 * exception events even when BKOPS status doesn't indicate performace
4887 * impacted or critical. Handle these device by determining their urgent
4888 * bkops status at runtime.
4890 if (curr_status
< BKOPS_STATUS_PERF_IMPACT
) {
4891 dev_err(hba
->dev
, "%s: device raised urgent BKOPS exception for bkops status %d\n",
4892 __func__
, curr_status
);
4893 /* update the current status as the urgent bkops level */
4894 hba
->urgent_bkops_lvl
= curr_status
;
4895 hba
->is_urgent_bkops_lvl_checked
= true;
4899 err
= ufshcd_enable_auto_bkops(hba
);
4902 dev_err(hba
->dev
, "%s: failed to handle urgent bkops %d\n",
4907 * ufshcd_exception_event_handler - handle exceptions raised by device
4908 * @work: pointer to work data
4910 * Read bExceptionEventStatus attribute from the device and handle the
4911 * exception event accordingly.
4913 static void ufshcd_exception_event_handler(struct work_struct
*work
)
4915 struct ufs_hba
*hba
;
4918 hba
= container_of(work
, struct ufs_hba
, eeh_work
);
4920 pm_runtime_get_sync(hba
->dev
);
4921 err
= ufshcd_get_ee_status(hba
, &status
);
4923 dev_err(hba
->dev
, "%s: failed to get exception status %d\n",
4928 status
&= hba
->ee_ctrl_mask
;
4930 if (status
& MASK_EE_URGENT_BKOPS
)
4931 ufshcd_bkops_exception_event_handler(hba
);
4934 pm_runtime_put_sync(hba
->dev
);
4938 /* Complete requests that have door-bell cleared */
4939 static void ufshcd_complete_requests(struct ufs_hba
*hba
)
4941 ufshcd_transfer_req_compl(hba
);
4942 ufshcd_tmc_handler(hba
);
4946 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
4947 * to recover from the DL NAC errors or not.
4948 * @hba: per-adapter instance
4950 * Returns true if error handling is required, false otherwise
4952 static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba
*hba
)
4954 unsigned long flags
;
4955 bool err_handling
= true;
4957 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
4959 * UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS only workaround the
4960 * device fatal error and/or DL NAC & REPLAY timeout errors.
4962 if (hba
->saved_err
& (CONTROLLER_FATAL_ERROR
| SYSTEM_BUS_FATAL_ERROR
))
4965 if ((hba
->saved_err
& DEVICE_FATAL_ERROR
) ||
4966 ((hba
->saved_err
& UIC_ERROR
) &&
4967 (hba
->saved_uic_err
& UFSHCD_UIC_DL_TCx_REPLAY_ERROR
)))
4970 if ((hba
->saved_err
& UIC_ERROR
) &&
4971 (hba
->saved_uic_err
& UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
)) {
4974 * wait for 50ms to see if we can get any other errors or not.
4976 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
4978 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
4981 * now check if we have got any other severe errors other than
4984 if ((hba
->saved_err
& INT_FATAL_ERRORS
) ||
4985 ((hba
->saved_err
& UIC_ERROR
) &&
4986 (hba
->saved_uic_err
& ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
)))
4990 * As DL NAC is the only error received so far, send out NOP
4991 * command to confirm if link is still active or not.
4992 * - If we don't get any response then do error recovery.
4993 * - If we get response then clear the DL NAC error bit.
4996 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
4997 err
= ufshcd_verify_dev_init(hba
);
4998 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5003 /* Link seems to be alive hence ignore the DL NAC errors */
5004 if (hba
->saved_uic_err
== UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
)
5005 hba
->saved_err
&= ~UIC_ERROR
;
5006 /* clear NAC error */
5007 hba
->saved_uic_err
&= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
;
5008 if (!hba
->saved_uic_err
) {
5009 err_handling
= false;
5014 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5015 return err_handling
;
5019 * ufshcd_err_handler - handle UFS errors that require s/w attention
5020 * @work: pointer to work structure
5022 static void ufshcd_err_handler(struct work_struct
*work
)
5024 struct ufs_hba
*hba
;
5025 unsigned long flags
;
5030 bool needs_reset
= false;
5032 hba
= container_of(work
, struct ufs_hba
, eh_work
);
5034 pm_runtime_get_sync(hba
->dev
);
5035 ufshcd_hold(hba
, false);
5037 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5038 if (hba
->ufshcd_state
== UFSHCD_STATE_RESET
)
5041 hba
->ufshcd_state
= UFSHCD_STATE_RESET
;
5042 ufshcd_set_eh_in_progress(hba
);
5044 /* Complete requests that have door-bell cleared by h/w */
5045 ufshcd_complete_requests(hba
);
5047 if (hba
->dev_quirks
& UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS
) {
5050 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5051 /* release the lock as ufshcd_quirk_dl_nac_errors() may sleep */
5052 ret
= ufshcd_quirk_dl_nac_errors(hba
);
5053 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5055 goto skip_err_handling
;
5057 if ((hba
->saved_err
& INT_FATAL_ERRORS
) ||
5058 ((hba
->saved_err
& UIC_ERROR
) &&
5059 (hba
->saved_uic_err
& (UFSHCD_UIC_DL_PA_INIT_ERROR
|
5060 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
|
5061 UFSHCD_UIC_DL_TCx_REPLAY_ERROR
))))
5065 * if host reset is required then skip clearing the pending
5066 * transfers forcefully because they will automatically get
5067 * cleared after link startup.
5070 goto skip_pending_xfer_clear
;
5072 /* release lock as clear command might sleep */
5073 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5074 /* Clear pending transfer requests */
5075 for_each_set_bit(tag
, &hba
->outstanding_reqs
, hba
->nutrs
) {
5076 if (ufshcd_clear_cmd(hba
, tag
)) {
5078 goto lock_skip_pending_xfer_clear
;
5082 /* Clear pending task management requests */
5083 for_each_set_bit(tag
, &hba
->outstanding_tasks
, hba
->nutmrs
) {
5084 if (ufshcd_clear_tm_cmd(hba
, tag
)) {
5086 goto lock_skip_pending_xfer_clear
;
5090 lock_skip_pending_xfer_clear
:
5091 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5093 /* Complete the requests that are cleared by s/w */
5094 ufshcd_complete_requests(hba
);
5096 if (err_xfer
|| err_tm
)
5099 skip_pending_xfer_clear
:
5100 /* Fatal errors need reset */
5102 unsigned long max_doorbells
= (1UL << hba
->nutrs
) - 1;
5105 * ufshcd_reset_and_restore() does the link reinitialization
5106 * which will need atleast one empty doorbell slot to send the
5107 * device management commands (NOP and query commands).
5108 * If there is no slot empty at this moment then free up last
5111 if (hba
->outstanding_reqs
== max_doorbells
)
5112 __ufshcd_transfer_req_compl(hba
,
5113 (1UL << (hba
->nutrs
- 1)));
5115 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5116 err
= ufshcd_reset_and_restore(hba
);
5117 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5119 dev_err(hba
->dev
, "%s: reset and restore failed\n",
5121 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
5124 * Inform scsi mid-layer that we did reset and allow to handle
5125 * Unit Attention properly.
5127 scsi_report_bus_reset(hba
->host
, 0);
5129 hba
->saved_uic_err
= 0;
5134 hba
->ufshcd_state
= UFSHCD_STATE_OPERATIONAL
;
5135 if (hba
->saved_err
|| hba
->saved_uic_err
)
5136 dev_err_ratelimited(hba
->dev
, "%s: exit: saved_err 0x%x saved_uic_err 0x%x",
5137 __func__
, hba
->saved_err
, hba
->saved_uic_err
);
5140 ufshcd_clear_eh_in_progress(hba
);
5143 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5144 scsi_unblock_requests(hba
->host
);
5145 ufshcd_release(hba
);
5146 pm_runtime_put_sync(hba
->dev
);
5149 static void ufshcd_update_uic_reg_hist(struct ufs_uic_err_reg_hist
*reg_hist
,
5152 reg_hist
->reg
[reg_hist
->pos
] = reg
;
5153 reg_hist
->tstamp
[reg_hist
->pos
] = ktime_get();
5154 reg_hist
->pos
= (reg_hist
->pos
+ 1) % UIC_ERR_REG_HIST_LENGTH
;
5158 * ufshcd_update_uic_error - check and set fatal UIC error flags.
5159 * @hba: per-adapter instance
5161 static void ufshcd_update_uic_error(struct ufs_hba
*hba
)
5165 /* PHY layer lane error */
5166 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER
);
5167 /* Ignore LINERESET indication, as this is not an error */
5168 if ((reg
& UIC_PHY_ADAPTER_LAYER_ERROR
) &&
5169 (reg
& UIC_PHY_ADAPTER_LAYER_LANE_ERR_MASK
)) {
5171 * To know whether this error is fatal or not, DB timeout
5172 * must be checked but this error is handled separately.
5174 dev_dbg(hba
->dev
, "%s: UIC Lane error reported\n", __func__
);
5175 ufshcd_update_uic_reg_hist(&hba
->ufs_stats
.pa_err
, reg
);
5178 /* PA_INIT_ERROR is fatal and needs UIC reset */
5179 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_DATA_LINK_LAYER
);
5181 ufshcd_update_uic_reg_hist(&hba
->ufs_stats
.dl_err
, reg
);
5183 if (reg
& UIC_DATA_LINK_LAYER_ERROR_PA_INIT
)
5184 hba
->uic_error
|= UFSHCD_UIC_DL_PA_INIT_ERROR
;
5185 else if (hba
->dev_quirks
&
5186 UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS
) {
5187 if (reg
& UIC_DATA_LINK_LAYER_ERROR_NAC_RECEIVED
)
5189 UFSHCD_UIC_DL_NAC_RECEIVED_ERROR
;
5190 else if (reg
& UIC_DATA_LINK_LAYER_ERROR_TCx_REPLAY_TIMEOUT
)
5191 hba
->uic_error
|= UFSHCD_UIC_DL_TCx_REPLAY_ERROR
;
5194 /* UIC NL/TL/DME errors needs software retry */
5195 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_NETWORK_LAYER
);
5197 ufshcd_update_uic_reg_hist(&hba
->ufs_stats
.nl_err
, reg
);
5198 hba
->uic_error
|= UFSHCD_UIC_NL_ERROR
;
5201 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_TRANSPORT_LAYER
);
5203 ufshcd_update_uic_reg_hist(&hba
->ufs_stats
.tl_err
, reg
);
5204 hba
->uic_error
|= UFSHCD_UIC_TL_ERROR
;
5207 reg
= ufshcd_readl(hba
, REG_UIC_ERROR_CODE_DME
);
5209 ufshcd_update_uic_reg_hist(&hba
->ufs_stats
.dme_err
, reg
);
5210 hba
->uic_error
|= UFSHCD_UIC_DME_ERROR
;
5213 dev_dbg(hba
->dev
, "%s: UIC error flags = 0x%08x\n",
5214 __func__
, hba
->uic_error
);
5218 * ufshcd_check_errors - Check for errors that need s/w attention
5219 * @hba: per-adapter instance
5221 static void ufshcd_check_errors(struct ufs_hba
*hba
)
5223 bool queue_eh_work
= false;
5225 if (hba
->errors
& INT_FATAL_ERRORS
)
5226 queue_eh_work
= true;
5228 if (hba
->errors
& UIC_ERROR
) {
5230 ufshcd_update_uic_error(hba
);
5232 queue_eh_work
= true;
5235 if (queue_eh_work
) {
5237 * update the transfer error masks to sticky bits, let's do this
5238 * irrespective of current ufshcd_state.
5240 hba
->saved_err
|= hba
->errors
;
5241 hba
->saved_uic_err
|= hba
->uic_error
;
5243 /* handle fatal errors only when link is functional */
5244 if (hba
->ufshcd_state
== UFSHCD_STATE_OPERATIONAL
) {
5245 /* block commands from scsi mid-layer */
5246 scsi_block_requests(hba
->host
);
5248 hba
->ufshcd_state
= UFSHCD_STATE_EH_SCHEDULED
;
5250 /* dump controller state before resetting */
5251 if (hba
->saved_err
& (INT_FATAL_ERRORS
| UIC_ERROR
)) {
5252 bool pr_prdt
= !!(hba
->saved_err
&
5253 SYSTEM_BUS_FATAL_ERROR
);
5255 dev_err(hba
->dev
, "%s: saved_err 0x%x saved_uic_err 0x%x\n",
5256 __func__
, hba
->saved_err
,
5257 hba
->saved_uic_err
);
5259 ufshcd_print_host_regs(hba
);
5260 ufshcd_print_pwr_info(hba
);
5261 ufshcd_print_tmrs(hba
, hba
->outstanding_tasks
);
5262 ufshcd_print_trs(hba
, hba
->outstanding_reqs
,
5265 schedule_work(&hba
->eh_work
);
5269 * if (!queue_eh_work) -
5270 * Other errors are either non-fatal where host recovers
5271 * itself without s/w intervention or errors that will be
5272 * handled by the SCSI core layer.
5277 * ufshcd_tmc_handler - handle task management function completion
5278 * @hba: per adapter instance
5280 static void ufshcd_tmc_handler(struct ufs_hba
*hba
)
5284 tm_doorbell
= ufshcd_readl(hba
, REG_UTP_TASK_REQ_DOOR_BELL
);
5285 hba
->tm_condition
= tm_doorbell
^ hba
->outstanding_tasks
;
5286 wake_up(&hba
->tm_wq
);
5290 * ufshcd_sl_intr - Interrupt service routine
5291 * @hba: per adapter instance
5292 * @intr_status: contains interrupts generated by the controller
5294 static void ufshcd_sl_intr(struct ufs_hba
*hba
, u32 intr_status
)
5296 hba
->errors
= UFSHCD_ERROR_MASK
& intr_status
;
5298 ufshcd_check_errors(hba
);
5300 if (intr_status
& UFSHCD_UIC_MASK
)
5301 ufshcd_uic_cmd_compl(hba
, intr_status
);
5303 if (intr_status
& UTP_TASK_REQ_COMPL
)
5304 ufshcd_tmc_handler(hba
);
5306 if (intr_status
& UTP_TRANSFER_REQ_COMPL
)
5307 ufshcd_transfer_req_compl(hba
);
5311 * ufshcd_intr - Main interrupt service routine
5313 * @__hba: pointer to adapter instance
5315 * Returns IRQ_HANDLED - If interrupt is valid
5316 * IRQ_NONE - If invalid interrupt
5318 static irqreturn_t
ufshcd_intr(int irq
, void *__hba
)
5320 u32 intr_status
, enabled_intr_status
;
5321 irqreturn_t retval
= IRQ_NONE
;
5322 struct ufs_hba
*hba
= __hba
;
5324 spin_lock(hba
->host
->host_lock
);
5325 intr_status
= ufshcd_readl(hba
, REG_INTERRUPT_STATUS
);
5326 enabled_intr_status
=
5327 intr_status
& ufshcd_readl(hba
, REG_INTERRUPT_ENABLE
);
5330 ufshcd_writel(hba
, intr_status
, REG_INTERRUPT_STATUS
);
5332 if (enabled_intr_status
) {
5333 ufshcd_sl_intr(hba
, enabled_intr_status
);
5334 retval
= IRQ_HANDLED
;
5336 spin_unlock(hba
->host
->host_lock
);
5340 static int ufshcd_clear_tm_cmd(struct ufs_hba
*hba
, int tag
)
5343 u32 mask
= 1 << tag
;
5344 unsigned long flags
;
5346 if (!test_bit(tag
, &hba
->outstanding_tasks
))
5349 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5350 ufshcd_writel(hba
, ~(1 << tag
), REG_UTP_TASK_REQ_LIST_CLEAR
);
5351 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5353 /* poll for max. 1 sec to clear door bell register by h/w */
5354 err
= ufshcd_wait_for_register(hba
,
5355 REG_UTP_TASK_REQ_DOOR_BELL
,
5356 mask
, 0, 1000, 1000, true);
5362 * ufshcd_issue_tm_cmd - issues task management commands to controller
5363 * @hba: per adapter instance
5364 * @lun_id: LUN ID to which TM command is sent
5365 * @task_id: task ID to which the TM command is applicable
5366 * @tm_function: task management function opcode
5367 * @tm_response: task management service response return value
5369 * Returns non-zero value on error, zero on success.
5371 static int ufshcd_issue_tm_cmd(struct ufs_hba
*hba
, int lun_id
, int task_id
,
5372 u8 tm_function
, u8
*tm_response
)
5374 struct utp_task_req_desc
*task_req_descp
;
5375 struct utp_upiu_task_req
*task_req_upiup
;
5376 struct Scsi_Host
*host
;
5377 unsigned long flags
;
5385 * Get free slot, sleep if slots are unavailable.
5386 * Even though we use wait_event() which sleeps indefinitely,
5387 * the maximum wait time is bounded by %TM_CMD_TIMEOUT.
5389 wait_event(hba
->tm_tag_wq
, ufshcd_get_tm_free_slot(hba
, &free_slot
));
5390 ufshcd_hold(hba
, false);
5392 spin_lock_irqsave(host
->host_lock
, flags
);
5393 task_req_descp
= hba
->utmrdl_base_addr
;
5394 task_req_descp
+= free_slot
;
5396 /* Configure task request descriptor */
5397 task_req_descp
->header
.dword_0
= cpu_to_le32(UTP_REQ_DESC_INT_CMD
);
5398 task_req_descp
->header
.dword_2
=
5399 cpu_to_le32(OCS_INVALID_COMMAND_STATUS
);
5401 /* Configure task request UPIU */
5403 (struct utp_upiu_task_req
*) task_req_descp
->task_req_upiu
;
5404 task_tag
= hba
->nutrs
+ free_slot
;
5405 task_req_upiup
->header
.dword_0
=
5406 UPIU_HEADER_DWORD(UPIU_TRANSACTION_TASK_REQ
, 0,
5408 task_req_upiup
->header
.dword_1
=
5409 UPIU_HEADER_DWORD(0, tm_function
, 0, 0);
5411 * The host shall provide the same value for LUN field in the basic
5412 * header and for Input Parameter.
5414 task_req_upiup
->input_param1
= cpu_to_be32(lun_id
);
5415 task_req_upiup
->input_param2
= cpu_to_be32(task_id
);
5417 ufshcd_vops_setup_task_mgmt(hba
, free_slot
, tm_function
);
5419 /* send command to the controller */
5420 __set_bit(free_slot
, &hba
->outstanding_tasks
);
5422 /* Make sure descriptors are ready before ringing the task doorbell */
5425 ufshcd_writel(hba
, 1 << free_slot
, REG_UTP_TASK_REQ_DOOR_BELL
);
5426 /* Make sure that doorbell is committed immediately */
5429 spin_unlock_irqrestore(host
->host_lock
, flags
);
5431 /* wait until the task management command is completed */
5432 err
= wait_event_timeout(hba
->tm_wq
,
5433 test_bit(free_slot
, &hba
->tm_condition
),
5434 msecs_to_jiffies(TM_CMD_TIMEOUT
));
5436 dev_err(hba
->dev
, "%s: task management cmd 0x%.2x timed-out\n",
5437 __func__
, tm_function
);
5438 if (ufshcd_clear_tm_cmd(hba
, free_slot
))
5439 dev_WARN(hba
->dev
, "%s: unable clear tm cmd (slot %d) after timeout\n",
5440 __func__
, free_slot
);
5443 err
= ufshcd_task_req_compl(hba
, free_slot
, tm_response
);
5446 clear_bit(free_slot
, &hba
->tm_condition
);
5447 ufshcd_put_tm_slot(hba
, free_slot
);
5448 wake_up(&hba
->tm_tag_wq
);
5450 ufshcd_release(hba
);
5455 * ufshcd_eh_device_reset_handler - device reset handler registered to
5457 * @cmd: SCSI command pointer
5459 * Returns SUCCESS/FAILED
5461 static int ufshcd_eh_device_reset_handler(struct scsi_cmnd
*cmd
)
5463 struct Scsi_Host
*host
;
5464 struct ufs_hba
*hba
;
5469 struct ufshcd_lrb
*lrbp
;
5470 unsigned long flags
;
5472 host
= cmd
->device
->host
;
5473 hba
= shost_priv(host
);
5474 tag
= cmd
->request
->tag
;
5476 lrbp
= &hba
->lrb
[tag
];
5477 err
= ufshcd_issue_tm_cmd(hba
, lrbp
->lun
, 0, UFS_LOGICAL_RESET
, &resp
);
5478 if (err
|| resp
!= UPIU_TASK_MANAGEMENT_FUNC_COMPL
) {
5484 /* clear the commands that were pending for corresponding LUN */
5485 for_each_set_bit(pos
, &hba
->outstanding_reqs
, hba
->nutrs
) {
5486 if (hba
->lrb
[pos
].lun
== lrbp
->lun
) {
5487 err
= ufshcd_clear_cmd(hba
, pos
);
5492 spin_lock_irqsave(host
->host_lock
, flags
);
5493 ufshcd_transfer_req_compl(hba
);
5494 spin_unlock_irqrestore(host
->host_lock
, flags
);
5497 hba
->req_abort_count
= 0;
5501 dev_err(hba
->dev
, "%s: failed with err %d\n", __func__
, err
);
5507 static void ufshcd_set_req_abort_skip(struct ufs_hba
*hba
, unsigned long bitmap
)
5509 struct ufshcd_lrb
*lrbp
;
5512 for_each_set_bit(tag
, &bitmap
, hba
->nutrs
) {
5513 lrbp
= &hba
->lrb
[tag
];
5514 lrbp
->req_abort_skip
= true;
5519 * ufshcd_abort - abort a specific command
5520 * @cmd: SCSI command pointer
5522 * Abort the pending command in device by sending UFS_ABORT_TASK task management
5523 * command, and in host controller by clearing the door-bell register. There can
5524 * be race between controller sending the command to the device while abort is
5525 * issued. To avoid that, first issue UFS_QUERY_TASK to check if the command is
5526 * really issued and then try to abort it.
5528 * Returns SUCCESS/FAILED
5530 static int ufshcd_abort(struct scsi_cmnd
*cmd
)
5532 struct Scsi_Host
*host
;
5533 struct ufs_hba
*hba
;
5534 unsigned long flags
;
5539 struct ufshcd_lrb
*lrbp
;
5542 host
= cmd
->device
->host
;
5543 hba
= shost_priv(host
);
5544 tag
= cmd
->request
->tag
;
5545 lrbp
= &hba
->lrb
[tag
];
5546 if (!ufshcd_valid_tag(hba
, tag
)) {
5548 "%s: invalid command tag %d: cmd=0x%p, cmd->request=0x%p",
5549 __func__
, tag
, cmd
, cmd
->request
);
5554 * Task abort to the device W-LUN is illegal. When this command
5555 * will fail, due to spec violation, scsi err handling next step
5556 * will be to send LU reset which, again, is a spec violation.
5557 * To avoid these unnecessary/illegal step we skip to the last error
5558 * handling stage: reset and restore.
5560 if (lrbp
->lun
== UFS_UPIU_UFS_DEVICE_WLUN
)
5561 return ufshcd_eh_host_reset_handler(cmd
);
5563 ufshcd_hold(hba
, false);
5564 reg
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
5565 /* If command is already aborted/completed, return SUCCESS */
5566 if (!(test_bit(tag
, &hba
->outstanding_reqs
))) {
5568 "%s: cmd at tag %d already completed, outstanding=0x%lx, doorbell=0x%x\n",
5569 __func__
, tag
, hba
->outstanding_reqs
, reg
);
5573 if (!(reg
& (1 << tag
))) {
5575 "%s: cmd was completed, but without a notifying intr, tag = %d",
5579 /* Print Transfer Request of aborted task */
5580 dev_err(hba
->dev
, "%s: Device abort task at tag %d\n", __func__
, tag
);
5583 * Print detailed info about aborted request.
5584 * As more than one request might get aborted at the same time,
5585 * print full information only for the first aborted request in order
5586 * to reduce repeated printouts. For other aborted requests only print
5589 scsi_print_command(hba
->lrb
[tag
].cmd
);
5590 if (!hba
->req_abort_count
) {
5591 ufshcd_print_host_regs(hba
);
5592 ufshcd_print_host_state(hba
);
5593 ufshcd_print_pwr_info(hba
);
5594 ufshcd_print_trs(hba
, 1 << tag
, true);
5596 ufshcd_print_trs(hba
, 1 << tag
, false);
5598 hba
->req_abort_count
++;
5600 /* Skip task abort in case previous aborts failed and report failure */
5601 if (lrbp
->req_abort_skip
) {
5606 for (poll_cnt
= 100; poll_cnt
; poll_cnt
--) {
5607 err
= ufshcd_issue_tm_cmd(hba
, lrbp
->lun
, lrbp
->task_tag
,
5608 UFS_QUERY_TASK
, &resp
);
5609 if (!err
&& resp
== UPIU_TASK_MANAGEMENT_FUNC_SUCCEEDED
) {
5610 /* cmd pending in the device */
5611 dev_err(hba
->dev
, "%s: cmd pending in the device. tag = %d\n",
5614 } else if (!err
&& resp
== UPIU_TASK_MANAGEMENT_FUNC_COMPL
) {
5616 * cmd not pending in the device, check if it is
5619 dev_err(hba
->dev
, "%s: cmd at tag %d not pending in the device.\n",
5621 reg
= ufshcd_readl(hba
, REG_UTP_TRANSFER_REQ_DOOR_BELL
);
5622 if (reg
& (1 << tag
)) {
5623 /* sleep for max. 200us to stabilize */
5624 usleep_range(100, 200);
5627 /* command completed already */
5628 dev_err(hba
->dev
, "%s: cmd at tag %d successfully cleared from DB.\n",
5633 "%s: no response from device. tag = %d, err %d\n",
5634 __func__
, tag
, err
);
5636 err
= resp
; /* service response error */
5646 err
= ufshcd_issue_tm_cmd(hba
, lrbp
->lun
, lrbp
->task_tag
,
5647 UFS_ABORT_TASK
, &resp
);
5648 if (err
|| resp
!= UPIU_TASK_MANAGEMENT_FUNC_COMPL
) {
5650 err
= resp
; /* service response error */
5651 dev_err(hba
->dev
, "%s: issued. tag = %d, err %d\n",
5652 __func__
, tag
, err
);
5657 err
= ufshcd_clear_cmd(hba
, tag
);
5659 dev_err(hba
->dev
, "%s: Failed clearing cmd at tag %d, err %d\n",
5660 __func__
, tag
, err
);
5664 scsi_dma_unmap(cmd
);
5666 spin_lock_irqsave(host
->host_lock
, flags
);
5667 ufshcd_outstanding_req_clear(hba
, tag
);
5668 hba
->lrb
[tag
].cmd
= NULL
;
5669 spin_unlock_irqrestore(host
->host_lock
, flags
);
5671 clear_bit_unlock(tag
, &hba
->lrb_in_use
);
5672 wake_up(&hba
->dev_cmd
.tag_wq
);
5678 dev_err(hba
->dev
, "%s: failed with err %d\n", __func__
, err
);
5679 ufshcd_set_req_abort_skip(hba
, hba
->outstanding_reqs
);
5684 * This ufshcd_release() corresponds to the original scsi cmd that got
5685 * aborted here (as we won't get any IRQ for it).
5687 ufshcd_release(hba
);
5692 * ufshcd_host_reset_and_restore - reset and restore host controller
5693 * @hba: per-adapter instance
5695 * Note that host controller reset may issue DME_RESET to
5696 * local and remote (device) Uni-Pro stack and the attributes
5697 * are reset to default state.
5699 * Returns zero on success, non-zero on failure
5701 static int ufshcd_host_reset_and_restore(struct ufs_hba
*hba
)
5704 unsigned long flags
;
5706 /* Reset the host controller */
5707 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5708 ufshcd_hba_stop(hba
, false);
5709 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5711 /* scale up clocks to max frequency before full reinitialization */
5712 ufshcd_scale_clks(hba
, true);
5714 err
= ufshcd_hba_enable(hba
);
5718 /* Establish the link again and restore the device */
5719 err
= ufshcd_probe_hba(hba
);
5721 if (!err
&& (hba
->ufshcd_state
!= UFSHCD_STATE_OPERATIONAL
))
5725 dev_err(hba
->dev
, "%s: Host init failed %d\n", __func__
, err
);
5731 * ufshcd_reset_and_restore - reset and re-initialize host/device
5732 * @hba: per-adapter instance
5734 * Reset and recover device, host and re-establish link. This
5735 * is helpful to recover the communication in fatal error conditions.
5737 * Returns zero on success, non-zero on failure
5739 static int ufshcd_reset_and_restore(struct ufs_hba
*hba
)
5742 unsigned long flags
;
5743 int retries
= MAX_HOST_RESET_RETRIES
;
5746 err
= ufshcd_host_reset_and_restore(hba
);
5747 } while (err
&& --retries
);
5750 * After reset the door-bell might be cleared, complete
5751 * outstanding requests in s/w here.
5753 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5754 ufshcd_transfer_req_compl(hba
);
5755 ufshcd_tmc_handler(hba
);
5756 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5762 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
5763 * @cmd - SCSI command pointer
5765 * Returns SUCCESS/FAILED
5767 static int ufshcd_eh_host_reset_handler(struct scsi_cmnd
*cmd
)
5770 unsigned long flags
;
5771 struct ufs_hba
*hba
;
5773 hba
= shost_priv(cmd
->device
->host
);
5775 ufshcd_hold(hba
, false);
5777 * Check if there is any race with fatal error handling.
5778 * If so, wait for it to complete. Even though fatal error
5779 * handling does reset and restore in some cases, don't assume
5780 * anything out of it. We are just avoiding race here.
5783 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5784 if (!(work_pending(&hba
->eh_work
) ||
5785 hba
->ufshcd_state
== UFSHCD_STATE_RESET
))
5787 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5788 dev_dbg(hba
->dev
, "%s: reset in progress\n", __func__
);
5789 flush_work(&hba
->eh_work
);
5792 hba
->ufshcd_state
= UFSHCD_STATE_RESET
;
5793 ufshcd_set_eh_in_progress(hba
);
5794 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5796 err
= ufshcd_reset_and_restore(hba
);
5798 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
5801 hba
->ufshcd_state
= UFSHCD_STATE_OPERATIONAL
;
5804 hba
->ufshcd_state
= UFSHCD_STATE_ERROR
;
5806 ufshcd_clear_eh_in_progress(hba
);
5807 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
5809 ufshcd_release(hba
);
5814 * ufshcd_get_max_icc_level - calculate the ICC level
5815 * @sup_curr_uA: max. current supported by the regulator
5816 * @start_scan: row at the desc table to start scan from
5817 * @buff: power descriptor buffer
5819 * Returns calculated max ICC level for specific regulator
5821 static u32
ufshcd_get_max_icc_level(int sup_curr_uA
, u32 start_scan
, char *buff
)
5828 for (i
= start_scan
; i
>= 0; i
--) {
5829 data
= be16_to_cpup((__be16
*)&buff
[2 * i
]);
5830 unit
= (data
& ATTR_ICC_LVL_UNIT_MASK
) >>
5831 ATTR_ICC_LVL_UNIT_OFFSET
;
5832 curr_uA
= data
& ATTR_ICC_LVL_VALUE_MASK
;
5834 case UFSHCD_NANO_AMP
:
5835 curr_uA
= curr_uA
/ 1000;
5837 case UFSHCD_MILI_AMP
:
5838 curr_uA
= curr_uA
* 1000;
5841 curr_uA
= curr_uA
* 1000 * 1000;
5843 case UFSHCD_MICRO_AMP
:
5847 if (sup_curr_uA
>= curr_uA
)
5852 pr_err("%s: Couldn't find valid icc_level = %d", __func__
, i
);
5859 * ufshcd_calc_icc_level - calculate the max ICC level
5860 * In case regulators are not initialized we'll return 0
5861 * @hba: per-adapter instance
5862 * @desc_buf: power descriptor buffer to extract ICC levels from.
5863 * @len: length of desc_buff
5865 * Returns calculated ICC level
5867 static u32
ufshcd_find_max_sup_active_icc_level(struct ufs_hba
*hba
,
5868 u8
*desc_buf
, int len
)
5872 if (!hba
->vreg_info
.vcc
|| !hba
->vreg_info
.vccq
||
5873 !hba
->vreg_info
.vccq2
) {
5875 "%s: Regulator capability was not set, actvIccLevel=%d",
5876 __func__
, icc_level
);
5880 if (hba
->vreg_info
.vcc
)
5881 icc_level
= ufshcd_get_max_icc_level(
5882 hba
->vreg_info
.vcc
->max_uA
,
5883 POWER_DESC_MAX_ACTV_ICC_LVLS
- 1,
5884 &desc_buf
[PWR_DESC_ACTIVE_LVLS_VCC_0
]);
5886 if (hba
->vreg_info
.vccq
)
5887 icc_level
= ufshcd_get_max_icc_level(
5888 hba
->vreg_info
.vccq
->max_uA
,
5890 &desc_buf
[PWR_DESC_ACTIVE_LVLS_VCCQ_0
]);
5892 if (hba
->vreg_info
.vccq2
)
5893 icc_level
= ufshcd_get_max_icc_level(
5894 hba
->vreg_info
.vccq2
->max_uA
,
5896 &desc_buf
[PWR_DESC_ACTIVE_LVLS_VCCQ2_0
]);
5901 static int ufshcd_set_icc_levels_attr(struct ufs_hba
*hba
, u32 icc_level
)
5906 for (retries
= QUERY_REQ_RETRIES
; retries
> 0; retries
--) {
5907 /* write attribute */
5908 ret
= ufshcd_query_attr(hba
, UPIU_QUERY_OPCODE_WRITE_ATTR
,
5909 QUERY_ATTR_IDN_ACTIVE_ICC_LVL
, 0, 0, &icc_level
);
5913 dev_dbg(hba
->dev
, "%s: failed with error %d\n", __func__
, ret
);
5919 static void ufshcd_init_icc_levels(struct ufs_hba
*hba
)
5922 int buff_len
= QUERY_DESC_POWER_MAX_SIZE
;
5923 u8 desc_buf
[QUERY_DESC_POWER_MAX_SIZE
];
5925 ret
= ufshcd_read_power_desc(hba
, desc_buf
, buff_len
);
5928 "%s: Failed reading power descriptor.len = %d ret = %d",
5929 __func__
, buff_len
, ret
);
5933 hba
->init_prefetch_data
.icc_level
=
5934 ufshcd_find_max_sup_active_icc_level(hba
,
5935 desc_buf
, buff_len
);
5936 dev_dbg(hba
->dev
, "%s: setting icc_level 0x%x",
5937 __func__
, hba
->init_prefetch_data
.icc_level
);
5939 ret
= ufshcd_set_icc_levels_attr(hba
,
5940 hba
->init_prefetch_data
.icc_level
);
5944 "%s: Failed configuring bActiveICCLevel = %d ret = %d",
5945 __func__
, hba
->init_prefetch_data
.icc_level
, ret
);
5950 * ufshcd_scsi_add_wlus - Adds required W-LUs
5951 * @hba: per-adapter instance
5953 * UFS device specification requires the UFS devices to support 4 well known
5955 * "REPORT_LUNS" (address: 01h)
5956 * "UFS Device" (address: 50h)
5957 * "RPMB" (address: 44h)
5958 * "BOOT" (address: 30h)
5959 * UFS device's power management needs to be controlled by "POWER CONDITION"
5960 * field of SSU (START STOP UNIT) command. But this "power condition" field
5961 * will take effect only when its sent to "UFS device" well known logical unit
5962 * hence we require the scsi_device instance to represent this logical unit in
5963 * order for the UFS host driver to send the SSU command for power management.
5965 * We also require the scsi_device instance for "RPMB" (Replay Protected Memory
5966 * Block) LU so user space process can control this LU. User space may also
5967 * want to have access to BOOT LU.
5969 * This function adds scsi device instances for each of all well known LUs
5970 * (except "REPORT LUNS" LU).
5972 * Returns zero on success (all required W-LUs are added successfully),
5973 * non-zero error value on failure (if failed to add any of the required W-LU).
5975 static int ufshcd_scsi_add_wlus(struct ufs_hba
*hba
)
5978 struct scsi_device
*sdev_rpmb
;
5979 struct scsi_device
*sdev_boot
;
5981 hba
->sdev_ufs_device
= __scsi_add_device(hba
->host
, 0, 0,
5982 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_UFS_DEVICE_WLUN
), NULL
);
5983 if (IS_ERR(hba
->sdev_ufs_device
)) {
5984 ret
= PTR_ERR(hba
->sdev_ufs_device
);
5985 hba
->sdev_ufs_device
= NULL
;
5988 scsi_device_put(hba
->sdev_ufs_device
);
5990 sdev_boot
= __scsi_add_device(hba
->host
, 0, 0,
5991 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_BOOT_WLUN
), NULL
);
5992 if (IS_ERR(sdev_boot
)) {
5993 ret
= PTR_ERR(sdev_boot
);
5994 goto remove_sdev_ufs_device
;
5996 scsi_device_put(sdev_boot
);
5998 sdev_rpmb
= __scsi_add_device(hba
->host
, 0, 0,
5999 ufshcd_upiu_wlun_to_scsi_wlun(UFS_UPIU_RPMB_WLUN
), NULL
);
6000 if (IS_ERR(sdev_rpmb
)) {
6001 ret
= PTR_ERR(sdev_rpmb
);
6002 goto remove_sdev_boot
;
6004 scsi_device_put(sdev_rpmb
);
6008 scsi_remove_device(sdev_boot
);
6009 remove_sdev_ufs_device
:
6010 scsi_remove_device(hba
->sdev_ufs_device
);
6015 static int ufs_get_device_desc(struct ufs_hba
*hba
,
6016 struct ufs_dev_desc
*dev_desc
)
6020 u8 str_desc_buf
[QUERY_DESC_STRING_MAX_SIZE
+ 1] = {0};
6021 u8 desc_buf
[QUERY_DESC_DEVICE_MAX_SIZE
];
6023 err
= ufshcd_read_device_desc(hba
, desc_buf
,
6024 QUERY_DESC_DEVICE_MAX_SIZE
);
6026 dev_err(hba
->dev
, "%s: Failed reading Device Desc. err = %d\n",
6032 * getting vendor (manufacturerID) and Bank Index in big endian
6035 dev_desc
->wmanufacturerid
= desc_buf
[DEVICE_DESC_PARAM_MANF_ID
] << 8 |
6036 desc_buf
[DEVICE_DESC_PARAM_MANF_ID
+ 1];
6038 model_index
= desc_buf
[DEVICE_DESC_PARAM_PRDCT_NAME
];
6040 err
= ufshcd_read_string_desc(hba
, model_index
, str_desc_buf
,
6041 QUERY_DESC_STRING_MAX_SIZE
, ASCII_STD
);
6043 dev_err(hba
->dev
, "%s: Failed reading Product Name. err = %d\n",
6048 str_desc_buf
[QUERY_DESC_STRING_MAX_SIZE
] = '\0';
6049 strlcpy(dev_desc
->model
, (str_desc_buf
+ QUERY_DESC_HDR_SIZE
),
6050 min_t(u8
, str_desc_buf
[QUERY_DESC_LENGTH_OFFSET
],
6053 /* Null terminate the model string */
6054 dev_desc
->model
[MAX_MODEL_LEN
] = '\0';
6060 static void ufs_fixup_device_setup(struct ufs_hba
*hba
,
6061 struct ufs_dev_desc
*dev_desc
)
6063 struct ufs_dev_fix
*f
;
6065 for (f
= ufs_fixups
; f
->quirk
; f
++) {
6066 if ((f
->card
.wmanufacturerid
== dev_desc
->wmanufacturerid
||
6067 f
->card
.wmanufacturerid
== UFS_ANY_VENDOR
) &&
6068 (STR_PRFX_EQUAL(f
->card
.model
, dev_desc
->model
) ||
6069 !strcmp(f
->card
.model
, UFS_ANY_MODEL
)))
6070 hba
->dev_quirks
|= f
->quirk
;
6075 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
6076 * @hba: per-adapter instance
6078 * PA_TActivate parameter can be tuned manually if UniPro version is less than
6079 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
6080 * RX_MIN_ACTIVATETIME_CAPABILITY attribute. This optimal value can help reduce
6081 * the hibern8 exit latency.
6083 * Returns zero on success, non-zero error value on failure.
6085 static int ufshcd_tune_pa_tactivate(struct ufs_hba
*hba
)
6088 u32 peer_rx_min_activatetime
= 0, tuned_pa_tactivate
;
6090 ret
= ufshcd_dme_peer_get(hba
,
6092 RX_MIN_ACTIVATETIME_CAPABILITY
,
6093 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6094 &peer_rx_min_activatetime
);
6098 /* make sure proper unit conversion is applied */
6099 tuned_pa_tactivate
=
6100 ((peer_rx_min_activatetime
* RX_MIN_ACTIVATETIME_UNIT_US
)
6101 / PA_TACTIVATE_TIME_UNIT_US
);
6102 ret
= ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TACTIVATE
),
6103 tuned_pa_tactivate
);
6110 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
6111 * @hba: per-adapter instance
6113 * PA_Hibern8Time parameter can be tuned manually if UniPro version is less than
6114 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
6115 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
6116 * This optimal value can help reduce the hibern8 exit latency.
6118 * Returns zero on success, non-zero error value on failure.
6120 static int ufshcd_tune_pa_hibern8time(struct ufs_hba
*hba
)
6123 u32 local_tx_hibern8_time_cap
= 0, peer_rx_hibern8_time_cap
= 0;
6124 u32 max_hibern8_time
, tuned_pa_hibern8time
;
6126 ret
= ufshcd_dme_get(hba
,
6127 UIC_ARG_MIB_SEL(TX_HIBERN8TIME_CAPABILITY
,
6128 UIC_ARG_MPHY_TX_GEN_SEL_INDEX(0)),
6129 &local_tx_hibern8_time_cap
);
6133 ret
= ufshcd_dme_peer_get(hba
,
6134 UIC_ARG_MIB_SEL(RX_HIBERN8TIME_CAPABILITY
,
6135 UIC_ARG_MPHY_RX_GEN_SEL_INDEX(0)),
6136 &peer_rx_hibern8_time_cap
);
6140 max_hibern8_time
= max(local_tx_hibern8_time_cap
,
6141 peer_rx_hibern8_time_cap
);
6142 /* make sure proper unit conversion is applied */
6143 tuned_pa_hibern8time
= ((max_hibern8_time
* HIBERN8TIME_UNIT_US
)
6144 / PA_HIBERN8_TIME_UNIT_US
);
6145 ret
= ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_HIBERN8TIME
),
6146 tuned_pa_hibern8time
);
6152 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
6153 * less than device PA_TACTIVATE time.
6154 * @hba: per-adapter instance
6156 * Some UFS devices require host PA_TACTIVATE to be lower than device
6157 * PA_TACTIVATE, we need to enable UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE quirk
6160 * Returns zero on success, non-zero error value on failure.
6162 static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba
*hba
)
6165 u32 granularity
, peer_granularity
;
6166 u32 pa_tactivate
, peer_pa_tactivate
;
6167 u32 pa_tactivate_us
, peer_pa_tactivate_us
;
6168 u8 gran_to_us_table
[] = {1, 4, 8, 16, 32, 100};
6170 ret
= ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_GRANULARITY
),
6175 ret
= ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_GRANULARITY
),
6180 if ((granularity
< PA_GRANULARITY_MIN_VAL
) ||
6181 (granularity
> PA_GRANULARITY_MAX_VAL
)) {
6182 dev_err(hba
->dev
, "%s: invalid host PA_GRANULARITY %d",
6183 __func__
, granularity
);
6187 if ((peer_granularity
< PA_GRANULARITY_MIN_VAL
) ||
6188 (peer_granularity
> PA_GRANULARITY_MAX_VAL
)) {
6189 dev_err(hba
->dev
, "%s: invalid device PA_GRANULARITY %d",
6190 __func__
, peer_granularity
);
6194 ret
= ufshcd_dme_get(hba
, UIC_ARG_MIB(PA_TACTIVATE
), &pa_tactivate
);
6198 ret
= ufshcd_dme_peer_get(hba
, UIC_ARG_MIB(PA_TACTIVATE
),
6199 &peer_pa_tactivate
);
6203 pa_tactivate_us
= pa_tactivate
* gran_to_us_table
[granularity
- 1];
6204 peer_pa_tactivate_us
= peer_pa_tactivate
*
6205 gran_to_us_table
[peer_granularity
- 1];
6207 if (pa_tactivate_us
> peer_pa_tactivate_us
) {
6208 u32 new_peer_pa_tactivate
;
6210 new_peer_pa_tactivate
= pa_tactivate_us
/
6211 gran_to_us_table
[peer_granularity
- 1];
6212 new_peer_pa_tactivate
++;
6213 ret
= ufshcd_dme_peer_set(hba
, UIC_ARG_MIB(PA_TACTIVATE
),
6214 new_peer_pa_tactivate
);
6221 static void ufshcd_tune_unipro_params(struct ufs_hba
*hba
)
6223 if (ufshcd_is_unipro_pa_params_tuning_req(hba
)) {
6224 ufshcd_tune_pa_tactivate(hba
);
6225 ufshcd_tune_pa_hibern8time(hba
);
6228 if (hba
->dev_quirks
& UFS_DEVICE_QUIRK_PA_TACTIVATE
)
6229 /* set 1ms timeout for PA_TACTIVATE */
6230 ufshcd_dme_set(hba
, UIC_ARG_MIB(PA_TACTIVATE
), 10);
6232 if (hba
->dev_quirks
& UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE
)
6233 ufshcd_quirk_tune_host_pa_tactivate(hba
);
6235 ufshcd_vops_apply_dev_quirks(hba
);
6238 static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba
*hba
)
6240 int err_reg_hist_size
= sizeof(struct ufs_uic_err_reg_hist
);
6242 hba
->ufs_stats
.hibern8_exit_cnt
= 0;
6243 hba
->ufs_stats
.last_hibern8_exit_tstamp
= ktime_set(0, 0);
6245 memset(&hba
->ufs_stats
.pa_err
, 0, err_reg_hist_size
);
6246 memset(&hba
->ufs_stats
.dl_err
, 0, err_reg_hist_size
);
6247 memset(&hba
->ufs_stats
.nl_err
, 0, err_reg_hist_size
);
6248 memset(&hba
->ufs_stats
.tl_err
, 0, err_reg_hist_size
);
6249 memset(&hba
->ufs_stats
.dme_err
, 0, err_reg_hist_size
);
6251 hba
->req_abort_count
= 0;
6255 * ufshcd_probe_hba - probe hba to detect device and initialize
6256 * @hba: per-adapter instance
6258 * Execute link-startup and verify device initialization
6260 static int ufshcd_probe_hba(struct ufs_hba
*hba
)
6262 struct ufs_dev_desc card
= {0};
6264 ktime_t start
= ktime_get();
6266 ret
= ufshcd_link_startup(hba
);
6270 /* set the default level for urgent bkops */
6271 hba
->urgent_bkops_lvl
= BKOPS_STATUS_PERF_IMPACT
;
6272 hba
->is_urgent_bkops_lvl_checked
= false;
6274 /* Debug counters initialization */
6275 ufshcd_clear_dbg_ufs_stats(hba
);
6277 /* UniPro link is active now */
6278 ufshcd_set_link_active(hba
);
6280 ret
= ufshcd_verify_dev_init(hba
);
6284 ret
= ufshcd_complete_dev_init(hba
);
6288 ret
= ufs_get_device_desc(hba
, &card
);
6290 dev_err(hba
->dev
, "%s: Failed getting device info. err = %d\n",
6295 ufs_fixup_device_setup(hba
, &card
);
6296 ufshcd_tune_unipro_params(hba
);
6298 ret
= ufshcd_set_vccq_rail_unused(hba
,
6299 (hba
->dev_quirks
& UFS_DEVICE_NO_VCCQ
) ? true : false);
6303 /* UFS device is also active now */
6304 ufshcd_set_ufs_dev_active(hba
);
6305 ufshcd_force_reset_auto_bkops(hba
);
6306 hba
->wlun_dev_clr_ua
= true;
6308 if (ufshcd_get_max_pwr_mode(hba
)) {
6310 "%s: Failed getting max supported power mode\n",
6313 ret
= ufshcd_config_pwr_mode(hba
, &hba
->max_pwr_info
.info
);
6315 dev_err(hba
->dev
, "%s: Failed setting power mode, err = %d\n",
6321 /* set the state as operational after switching to desired gear */
6322 hba
->ufshcd_state
= UFSHCD_STATE_OPERATIONAL
;
6324 * If we are in error handling context or in power management callbacks
6325 * context, no need to scan the host
6327 if (!ufshcd_eh_in_progress(hba
) && !hba
->pm_op_in_progress
) {
6330 /* clear any previous UFS device information */
6331 memset(&hba
->dev_info
, 0, sizeof(hba
->dev_info
));
6332 if (!ufshcd_query_flag_retry(hba
, UPIU_QUERY_OPCODE_READ_FLAG
,
6333 QUERY_FLAG_IDN_PWR_ON_WPE
, &flag
))
6334 hba
->dev_info
.f_power_on_wp_en
= flag
;
6336 if (!hba
->is_init_prefetch
)
6337 ufshcd_init_icc_levels(hba
);
6339 /* Add required well known logical units to scsi mid layer */
6340 if (ufshcd_scsi_add_wlus(hba
))
6343 /* Initialize devfreq after UFS device is detected */
6344 if (ufshcd_is_clkscaling_supported(hba
)) {
6345 memcpy(&hba
->clk_scaling
.saved_pwr_info
.info
,
6347 sizeof(struct ufs_pa_layer_attr
));
6348 hba
->clk_scaling
.saved_pwr_info
.is_valid
= true;
6349 if (!hba
->devfreq
) {
6350 hba
->devfreq
= devm_devfreq_add_device(hba
->dev
,
6351 &ufs_devfreq_profile
,
6354 if (IS_ERR(hba
->devfreq
)) {
6355 ret
= PTR_ERR(hba
->devfreq
);
6356 dev_err(hba
->dev
, "Unable to register with devfreq %d\n",
6361 hba
->clk_scaling
.is_allowed
= true;
6364 scsi_scan_host(hba
->host
);
6365 pm_runtime_put_sync(hba
->dev
);
6368 if (!hba
->is_init_prefetch
)
6369 hba
->is_init_prefetch
= true;
6373 * If we failed to initialize the device or the device is not
6374 * present, turn off the power/clocks etc.
6376 if (ret
&& !ufshcd_eh_in_progress(hba
) && !hba
->pm_op_in_progress
) {
6377 pm_runtime_put_sync(hba
->dev
);
6378 ufshcd_hba_exit(hba
);
6381 trace_ufshcd_init(dev_name(hba
->dev
), ret
,
6382 ktime_to_us(ktime_sub(ktime_get(), start
)),
6383 hba
->curr_dev_pwr_mode
, hba
->uic_link_state
);
6388 * ufshcd_async_scan - asynchronous execution for probing hba
6389 * @data: data pointer to pass to this function
6390 * @cookie: cookie data
6392 static void ufshcd_async_scan(void *data
, async_cookie_t cookie
)
6394 struct ufs_hba
*hba
= (struct ufs_hba
*)data
;
6396 ufshcd_probe_hba(hba
);
6399 static enum blk_eh_timer_return
ufshcd_eh_timed_out(struct scsi_cmnd
*scmd
)
6401 unsigned long flags
;
6402 struct Scsi_Host
*host
;
6403 struct ufs_hba
*hba
;
6407 if (!scmd
|| !scmd
->device
|| !scmd
->device
->host
)
6408 return BLK_EH_NOT_HANDLED
;
6410 host
= scmd
->device
->host
;
6411 hba
= shost_priv(host
);
6413 return BLK_EH_NOT_HANDLED
;
6415 spin_lock_irqsave(host
->host_lock
, flags
);
6417 for_each_set_bit(index
, &hba
->outstanding_reqs
, hba
->nutrs
) {
6418 if (hba
->lrb
[index
].cmd
== scmd
) {
6424 spin_unlock_irqrestore(host
->host_lock
, flags
);
6427 * Bypass SCSI error handling and reset the block layer timer if this
6428 * SCSI command was not actually dispatched to UFS driver, otherwise
6429 * let SCSI layer handle the error as usual.
6431 return found
? BLK_EH_NOT_HANDLED
: BLK_EH_RESET_TIMER
;
6434 static struct scsi_host_template ufshcd_driver_template
= {
6435 .module
= THIS_MODULE
,
6437 .proc_name
= UFSHCD
,
6438 .queuecommand
= ufshcd_queuecommand
,
6439 .slave_alloc
= ufshcd_slave_alloc
,
6440 .slave_configure
= ufshcd_slave_configure
,
6441 .slave_destroy
= ufshcd_slave_destroy
,
6442 .change_queue_depth
= ufshcd_change_queue_depth
,
6443 .eh_abort_handler
= ufshcd_abort
,
6444 .eh_device_reset_handler
= ufshcd_eh_device_reset_handler
,
6445 .eh_host_reset_handler
= ufshcd_eh_host_reset_handler
,
6446 .eh_timed_out
= ufshcd_eh_timed_out
,
6448 .sg_tablesize
= SG_ALL
,
6449 .cmd_per_lun
= UFSHCD_CMD_PER_LUN
,
6450 .can_queue
= UFSHCD_CAN_QUEUE
,
6451 .max_host_blocked
= 1,
6452 .track_queue_depth
= 1,
6455 static int ufshcd_config_vreg_load(struct device
*dev
, struct ufs_vreg
*vreg
,
6463 ret
= regulator_set_load(vreg
->reg
, ua
);
6465 dev_err(dev
, "%s: %s set load (ua=%d) failed, err=%d\n",
6466 __func__
, vreg
->name
, ua
, ret
);
6472 static inline int ufshcd_config_vreg_lpm(struct ufs_hba
*hba
,
6473 struct ufs_vreg
*vreg
)
6477 else if (vreg
->unused
)
6480 return ufshcd_config_vreg_load(hba
->dev
, vreg
,
6481 UFS_VREG_LPM_LOAD_UA
);
6484 static inline int ufshcd_config_vreg_hpm(struct ufs_hba
*hba
,
6485 struct ufs_vreg
*vreg
)
6489 else if (vreg
->unused
)
6492 return ufshcd_config_vreg_load(hba
->dev
, vreg
, vreg
->max_uA
);
6495 static int ufshcd_config_vreg(struct device
*dev
,
6496 struct ufs_vreg
*vreg
, bool on
)
6499 struct regulator
*reg
= vreg
->reg
;
6500 const char *name
= vreg
->name
;
6501 int min_uV
, uA_load
;
6505 if (regulator_count_voltages(reg
) > 0) {
6506 min_uV
= on
? vreg
->min_uV
: 0;
6507 ret
= regulator_set_voltage(reg
, min_uV
, vreg
->max_uV
);
6509 dev_err(dev
, "%s: %s set voltage failed, err=%d\n",
6510 __func__
, name
, ret
);
6514 uA_load
= on
? vreg
->max_uA
: 0;
6515 ret
= ufshcd_config_vreg_load(dev
, vreg
, uA_load
);
6523 static int ufshcd_enable_vreg(struct device
*dev
, struct ufs_vreg
*vreg
)
6529 else if (vreg
->enabled
|| vreg
->unused
)
6532 ret
= ufshcd_config_vreg(dev
, vreg
, true);
6534 ret
= regulator_enable(vreg
->reg
);
6537 vreg
->enabled
= true;
6539 dev_err(dev
, "%s: %s enable failed, err=%d\n",
6540 __func__
, vreg
->name
, ret
);
6545 static int ufshcd_disable_vreg(struct device
*dev
, struct ufs_vreg
*vreg
)
6551 else if (!vreg
->enabled
|| vreg
->unused
)
6554 ret
= regulator_disable(vreg
->reg
);
6557 /* ignore errors on applying disable config */
6558 ufshcd_config_vreg(dev
, vreg
, false);
6559 vreg
->enabled
= false;
6561 dev_err(dev
, "%s: %s disable failed, err=%d\n",
6562 __func__
, vreg
->name
, ret
);
6568 static int ufshcd_setup_vreg(struct ufs_hba
*hba
, bool on
)
6571 struct device
*dev
= hba
->dev
;
6572 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
6577 ret
= ufshcd_toggle_vreg(dev
, info
->vcc
, on
);
6581 ret
= ufshcd_toggle_vreg(dev
, info
->vccq
, on
);
6585 ret
= ufshcd_toggle_vreg(dev
, info
->vccq2
, on
);
6591 ufshcd_toggle_vreg(dev
, info
->vccq2
, false);
6592 ufshcd_toggle_vreg(dev
, info
->vccq
, false);
6593 ufshcd_toggle_vreg(dev
, info
->vcc
, false);
6598 static int ufshcd_setup_hba_vreg(struct ufs_hba
*hba
, bool on
)
6600 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
6603 return ufshcd_toggle_vreg(hba
->dev
, info
->vdd_hba
, on
);
6608 static int ufshcd_get_vreg(struct device
*dev
, struct ufs_vreg
*vreg
)
6615 vreg
->reg
= devm_regulator_get(dev
, vreg
->name
);
6616 if (IS_ERR(vreg
->reg
)) {
6617 ret
= PTR_ERR(vreg
->reg
);
6618 dev_err(dev
, "%s: %s get failed, err=%d\n",
6619 __func__
, vreg
->name
, ret
);
6625 static int ufshcd_init_vreg(struct ufs_hba
*hba
)
6628 struct device
*dev
= hba
->dev
;
6629 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
6634 ret
= ufshcd_get_vreg(dev
, info
->vcc
);
6638 ret
= ufshcd_get_vreg(dev
, info
->vccq
);
6642 ret
= ufshcd_get_vreg(dev
, info
->vccq2
);
6647 static int ufshcd_init_hba_vreg(struct ufs_hba
*hba
)
6649 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
6652 return ufshcd_get_vreg(hba
->dev
, info
->vdd_hba
);
6657 static int ufshcd_set_vccq_rail_unused(struct ufs_hba
*hba
, bool unused
)
6660 struct ufs_vreg_info
*info
= &hba
->vreg_info
;
6664 else if (!info
->vccq
)
6668 /* shut off the rail here */
6669 ret
= ufshcd_toggle_vreg(hba
->dev
, info
->vccq
, false);
6671 * Mark this rail as no longer used, so it doesn't get enabled
6675 info
->vccq
->unused
= true;
6678 * rail should have been already enabled hence just make sure
6679 * that unused flag is cleared.
6681 info
->vccq
->unused
= false;
6687 static int __ufshcd_setup_clocks(struct ufs_hba
*hba
, bool on
,
6691 struct ufs_clk_info
*clki
;
6692 struct list_head
*head
= &hba
->clk_list_head
;
6693 unsigned long flags
;
6694 ktime_t start
= ktime_get();
6695 bool clk_state_changed
= false;
6697 if (!head
|| list_empty(head
))
6700 ret
= ufshcd_vops_setup_clocks(hba
, on
, PRE_CHANGE
);
6704 list_for_each_entry(clki
, head
, list
) {
6705 if (!IS_ERR_OR_NULL(clki
->clk
)) {
6706 if (skip_ref_clk
&& !strcmp(clki
->name
, "ref_clk"))
6709 clk_state_changed
= on
^ clki
->enabled
;
6710 if (on
&& !clki
->enabled
) {
6711 ret
= clk_prepare_enable(clki
->clk
);
6713 dev_err(hba
->dev
, "%s: %s prepare enable failed, %d\n",
6714 __func__
, clki
->name
, ret
);
6717 } else if (!on
&& clki
->enabled
) {
6718 clk_disable_unprepare(clki
->clk
);
6721 dev_dbg(hba
->dev
, "%s: clk: %s %sabled\n", __func__
,
6722 clki
->name
, on
? "en" : "dis");
6726 ret
= ufshcd_vops_setup_clocks(hba
, on
, POST_CHANGE
);
6732 list_for_each_entry(clki
, head
, list
) {
6733 if (!IS_ERR_OR_NULL(clki
->clk
) && clki
->enabled
)
6734 clk_disable_unprepare(clki
->clk
);
6736 } else if (!ret
&& on
) {
6737 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
6738 hba
->clk_gating
.state
= CLKS_ON
;
6739 trace_ufshcd_clk_gating(dev_name(hba
->dev
),
6740 hba
->clk_gating
.state
);
6741 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
6744 if (clk_state_changed
)
6745 trace_ufshcd_profile_clk_gating(dev_name(hba
->dev
),
6746 (on
? "on" : "off"),
6747 ktime_to_us(ktime_sub(ktime_get(), start
)), ret
);
6751 static int ufshcd_setup_clocks(struct ufs_hba
*hba
, bool on
)
6753 return __ufshcd_setup_clocks(hba
, on
, false);
6756 static int ufshcd_init_clocks(struct ufs_hba
*hba
)
6759 struct ufs_clk_info
*clki
;
6760 struct device
*dev
= hba
->dev
;
6761 struct list_head
*head
= &hba
->clk_list_head
;
6763 if (!head
|| list_empty(head
))
6766 list_for_each_entry(clki
, head
, list
) {
6770 clki
->clk
= devm_clk_get(dev
, clki
->name
);
6771 if (IS_ERR(clki
->clk
)) {
6772 ret
= PTR_ERR(clki
->clk
);
6773 dev_err(dev
, "%s: %s clk get failed, %d\n",
6774 __func__
, clki
->name
, ret
);
6778 if (clki
->max_freq
) {
6779 ret
= clk_set_rate(clki
->clk
, clki
->max_freq
);
6781 dev_err(hba
->dev
, "%s: %s clk set rate(%dHz) failed, %d\n",
6782 __func__
, clki
->name
,
6783 clki
->max_freq
, ret
);
6786 clki
->curr_freq
= clki
->max_freq
;
6788 dev_dbg(dev
, "%s: clk: %s, rate: %lu\n", __func__
,
6789 clki
->name
, clk_get_rate(clki
->clk
));
6795 static int ufshcd_variant_hba_init(struct ufs_hba
*hba
)
6802 err
= ufshcd_vops_init(hba
);
6806 err
= ufshcd_vops_setup_regulators(hba
, true);
6813 ufshcd_vops_exit(hba
);
6816 dev_err(hba
->dev
, "%s: variant %s init failed err %d\n",
6817 __func__
, ufshcd_get_var_name(hba
), err
);
6821 static void ufshcd_variant_hba_exit(struct ufs_hba
*hba
)
6826 ufshcd_vops_setup_regulators(hba
, false);
6828 ufshcd_vops_exit(hba
);
6831 static int ufshcd_hba_init(struct ufs_hba
*hba
)
6836 * Handle host controller power separately from the UFS device power
6837 * rails as it will help controlling the UFS host controller power
6838 * collapse easily which is different than UFS device power collapse.
6839 * Also, enable the host controller power before we go ahead with rest
6840 * of the initialization here.
6842 err
= ufshcd_init_hba_vreg(hba
);
6846 err
= ufshcd_setup_hba_vreg(hba
, true);
6850 err
= ufshcd_init_clocks(hba
);
6852 goto out_disable_hba_vreg
;
6854 err
= ufshcd_setup_clocks(hba
, true);
6856 goto out_disable_hba_vreg
;
6858 err
= ufshcd_init_vreg(hba
);
6860 goto out_disable_clks
;
6862 err
= ufshcd_setup_vreg(hba
, true);
6864 goto out_disable_clks
;
6866 err
= ufshcd_variant_hba_init(hba
);
6868 goto out_disable_vreg
;
6870 hba
->is_powered
= true;
6874 ufshcd_setup_vreg(hba
, false);
6876 ufshcd_setup_clocks(hba
, false);
6877 out_disable_hba_vreg
:
6878 ufshcd_setup_hba_vreg(hba
, false);
6883 static void ufshcd_hba_exit(struct ufs_hba
*hba
)
6885 if (hba
->is_powered
) {
6886 ufshcd_variant_hba_exit(hba
);
6887 ufshcd_setup_vreg(hba
, false);
6888 ufshcd_suspend_clkscaling(hba
);
6889 if (ufshcd_is_clkscaling_supported(hba
)) {
6891 ufshcd_suspend_clkscaling(hba
);
6892 destroy_workqueue(hba
->clk_scaling
.workq
);
6894 ufshcd_setup_clocks(hba
, false);
6895 ufshcd_setup_hba_vreg(hba
, false);
6896 hba
->is_powered
= false;
6901 ufshcd_send_request_sense(struct ufs_hba
*hba
, struct scsi_device
*sdp
)
6903 unsigned char cmd
[6] = {REQUEST_SENSE
,
6907 UFSHCD_REQ_SENSE_SIZE
,
6912 buffer
= kzalloc(UFSHCD_REQ_SENSE_SIZE
, GFP_KERNEL
);
6918 ret
= scsi_execute(sdp
, cmd
, DMA_FROM_DEVICE
, buffer
,
6919 UFSHCD_REQ_SENSE_SIZE
, NULL
, NULL
,
6920 msecs_to_jiffies(1000), 3, 0, RQF_PM
, NULL
);
6922 pr_err("%s: failed with err %d\n", __func__
, ret
);
6930 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
6932 * @hba: per adapter instance
6933 * @pwr_mode: device power mode to set
6935 * Returns 0 if requested power mode is set successfully
6936 * Returns non-zero if failed to set the requested power mode
6938 static int ufshcd_set_dev_pwr_mode(struct ufs_hba
*hba
,
6939 enum ufs_dev_pwr_mode pwr_mode
)
6941 unsigned char cmd
[6] = { START_STOP
};
6942 struct scsi_sense_hdr sshdr
;
6943 struct scsi_device
*sdp
;
6944 unsigned long flags
;
6947 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
6948 sdp
= hba
->sdev_ufs_device
;
6950 ret
= scsi_device_get(sdp
);
6951 if (!ret
&& !scsi_device_online(sdp
)) {
6953 scsi_device_put(sdp
);
6958 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
6964 * If scsi commands fail, the scsi mid-layer schedules scsi error-
6965 * handling, which would wait for host to be resumed. Since we know
6966 * we are functional while we are here, skip host resume in error
6969 hba
->host
->eh_noresume
= 1;
6970 if (hba
->wlun_dev_clr_ua
) {
6971 ret
= ufshcd_send_request_sense(hba
, sdp
);
6974 /* Unit attention condition is cleared now */
6975 hba
->wlun_dev_clr_ua
= false;
6978 cmd
[4] = pwr_mode
<< 4;
6981 * Current function would be generally called from the power management
6982 * callbacks hence set the RQF_PM flag so that it doesn't resume the
6983 * already suspended childs.
6985 ret
= scsi_execute(sdp
, cmd
, DMA_NONE
, NULL
, 0, NULL
, &sshdr
,
6986 START_STOP_TIMEOUT
, 0, 0, RQF_PM
, NULL
);
6988 sdev_printk(KERN_WARNING
, sdp
,
6989 "START_STOP failed for power mode: %d, result %x\n",
6991 if (driver_byte(ret
) & DRIVER_SENSE
)
6992 scsi_print_sense_hdr(sdp
, NULL
, &sshdr
);
6996 hba
->curr_dev_pwr_mode
= pwr_mode
;
6998 scsi_device_put(sdp
);
6999 hba
->host
->eh_noresume
= 0;
7003 static int ufshcd_link_state_transition(struct ufs_hba
*hba
,
7004 enum uic_link_state req_link_state
,
7005 int check_for_bkops
)
7009 if (req_link_state
== hba
->uic_link_state
)
7012 if (req_link_state
== UIC_LINK_HIBERN8_STATE
) {
7013 ret
= ufshcd_uic_hibern8_enter(hba
);
7015 ufshcd_set_link_hibern8(hba
);
7020 * If autobkops is enabled, link can't be turned off because
7021 * turning off the link would also turn off the device.
7023 else if ((req_link_state
== UIC_LINK_OFF_STATE
) &&
7024 (!check_for_bkops
|| (check_for_bkops
&&
7025 !hba
->auto_bkops_enabled
))) {
7027 * Let's make sure that link is in low power mode, we are doing
7028 * this currently by putting the link in Hibern8. Otherway to
7029 * put the link in low power mode is to send the DME end point
7030 * to device and then send the DME reset command to local
7031 * unipro. But putting the link in hibern8 is much faster.
7033 ret
= ufshcd_uic_hibern8_enter(hba
);
7037 * Change controller state to "reset state" which
7038 * should also put the link in off/reset state
7040 ufshcd_hba_stop(hba
, true);
7042 * TODO: Check if we need any delay to make sure that
7043 * controller is reset
7045 ufshcd_set_link_off(hba
);
7052 static void ufshcd_vreg_set_lpm(struct ufs_hba
*hba
)
7055 * It seems some UFS devices may keep drawing more than sleep current
7056 * (atleast for 500us) from UFS rails (especially from VCCQ rail).
7057 * To avoid this situation, add 2ms delay before putting these UFS
7058 * rails in LPM mode.
7060 if (!ufshcd_is_link_active(hba
) &&
7061 hba
->dev_quirks
& UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM
)
7062 usleep_range(2000, 2100);
7065 * If UFS device is either in UFS_Sleep turn off VCC rail to save some
7068 * If UFS device and link is in OFF state, all power supplies (VCC,
7069 * VCCQ, VCCQ2) can be turned off if power on write protect is not
7070 * required. If UFS link is inactive (Hibern8 or OFF state) and device
7071 * is in sleep state, put VCCQ & VCCQ2 rails in LPM mode.
7073 * Ignore the error returned by ufshcd_toggle_vreg() as device is anyway
7074 * in low power state which would save some power.
7076 if (ufshcd_is_ufs_dev_poweroff(hba
) && ufshcd_is_link_off(hba
) &&
7077 !hba
->dev_info
.is_lu_power_on_wp
) {
7078 ufshcd_setup_vreg(hba
, false);
7079 } else if (!ufshcd_is_ufs_dev_active(hba
)) {
7080 ufshcd_toggle_vreg(hba
->dev
, hba
->vreg_info
.vcc
, false);
7081 if (!ufshcd_is_link_active(hba
)) {
7082 ufshcd_config_vreg_lpm(hba
, hba
->vreg_info
.vccq
);
7083 ufshcd_config_vreg_lpm(hba
, hba
->vreg_info
.vccq2
);
7088 static int ufshcd_vreg_set_hpm(struct ufs_hba
*hba
)
7092 if (ufshcd_is_ufs_dev_poweroff(hba
) && ufshcd_is_link_off(hba
) &&
7093 !hba
->dev_info
.is_lu_power_on_wp
) {
7094 ret
= ufshcd_setup_vreg(hba
, true);
7095 } else if (!ufshcd_is_ufs_dev_active(hba
)) {
7096 if (!ret
&& !ufshcd_is_link_active(hba
)) {
7097 ret
= ufshcd_config_vreg_hpm(hba
, hba
->vreg_info
.vccq
);
7100 ret
= ufshcd_config_vreg_hpm(hba
, hba
->vreg_info
.vccq2
);
7104 ret
= ufshcd_toggle_vreg(hba
->dev
, hba
->vreg_info
.vcc
, true);
7109 ufshcd_config_vreg_lpm(hba
, hba
->vreg_info
.vccq
);
7111 ufshcd_toggle_vreg(hba
->dev
, hba
->vreg_info
.vcc
, false);
7116 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba
*hba
)
7118 if (ufshcd_is_link_off(hba
))
7119 ufshcd_setup_hba_vreg(hba
, false);
7122 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba
*hba
)
7124 if (ufshcd_is_link_off(hba
))
7125 ufshcd_setup_hba_vreg(hba
, true);
7129 * ufshcd_suspend - helper function for suspend operations
7130 * @hba: per adapter instance
7131 * @pm_op: desired low power operation type
7133 * This function will try to put the UFS device and link into low power
7134 * mode based on the "rpm_lvl" (Runtime PM level) or "spm_lvl"
7135 * (System PM level).
7137 * If this function is called during shutdown, it will make sure that
7138 * both UFS device and UFS link is powered off.
7140 * NOTE: UFS device & link must be active before we enter in this function.
7142 * Returns 0 for success and non-zero for failure
7144 static int ufshcd_suspend(struct ufs_hba
*hba
, enum ufs_pm_op pm_op
)
7147 enum ufs_pm_level pm_lvl
;
7148 enum ufs_dev_pwr_mode req_dev_pwr_mode
;
7149 enum uic_link_state req_link_state
;
7151 hba
->pm_op_in_progress
= 1;
7152 if (!ufshcd_is_shutdown_pm(pm_op
)) {
7153 pm_lvl
= ufshcd_is_runtime_pm(pm_op
) ?
7154 hba
->rpm_lvl
: hba
->spm_lvl
;
7155 req_dev_pwr_mode
= ufs_get_pm_lvl_to_dev_pwr_mode(pm_lvl
);
7156 req_link_state
= ufs_get_pm_lvl_to_link_pwr_state(pm_lvl
);
7158 req_dev_pwr_mode
= UFS_POWERDOWN_PWR_MODE
;
7159 req_link_state
= UIC_LINK_OFF_STATE
;
7163 * If we can't transition into any of the low power modes
7164 * just gate the clocks.
7166 ufshcd_hold(hba
, false);
7167 hba
->clk_gating
.is_suspended
= true;
7169 if (hba
->clk_scaling
.is_allowed
) {
7170 cancel_work_sync(&hba
->clk_scaling
.suspend_work
);
7171 cancel_work_sync(&hba
->clk_scaling
.resume_work
);
7172 ufshcd_suspend_clkscaling(hba
);
7175 if (req_dev_pwr_mode
== UFS_ACTIVE_PWR_MODE
&&
7176 req_link_state
== UIC_LINK_ACTIVE_STATE
) {
7180 if ((req_dev_pwr_mode
== hba
->curr_dev_pwr_mode
) &&
7181 (req_link_state
== hba
->uic_link_state
))
7184 /* UFS device & link must be active before we enter in this function */
7185 if (!ufshcd_is_ufs_dev_active(hba
) || !ufshcd_is_link_active(hba
)) {
7190 if (ufshcd_is_runtime_pm(pm_op
)) {
7191 if (ufshcd_can_autobkops_during_suspend(hba
)) {
7193 * The device is idle with no requests in the queue,
7194 * allow background operations if bkops status shows
7195 * that performance might be impacted.
7197 ret
= ufshcd_urgent_bkops(hba
);
7201 /* make sure that auto bkops is disabled */
7202 ufshcd_disable_auto_bkops(hba
);
7206 if ((req_dev_pwr_mode
!= hba
->curr_dev_pwr_mode
) &&
7207 ((ufshcd_is_runtime_pm(pm_op
) && !hba
->auto_bkops_enabled
) ||
7208 !ufshcd_is_runtime_pm(pm_op
))) {
7209 /* ensure that bkops is disabled */
7210 ufshcd_disable_auto_bkops(hba
);
7211 ret
= ufshcd_set_dev_pwr_mode(hba
, req_dev_pwr_mode
);
7216 ret
= ufshcd_link_state_transition(hba
, req_link_state
, 1);
7218 goto set_dev_active
;
7220 ufshcd_vreg_set_lpm(hba
);
7224 * Call vendor specific suspend callback. As these callbacks may access
7225 * vendor specific host controller register space call them before the
7226 * host clocks are ON.
7228 ret
= ufshcd_vops_suspend(hba
, pm_op
);
7230 goto set_link_active
;
7232 if (!ufshcd_is_link_active(hba
))
7233 ufshcd_setup_clocks(hba
, false);
7235 /* If link is active, device ref_clk can't be switched off */
7236 __ufshcd_setup_clocks(hba
, false, true);
7238 hba
->clk_gating
.state
= CLKS_OFF
;
7239 trace_ufshcd_clk_gating(dev_name(hba
->dev
), hba
->clk_gating
.state
);
7241 * Disable the host irq as host controller as there won't be any
7242 * host controller transaction expected till resume.
7244 ufshcd_disable_irq(hba
);
7245 /* Put the host controller in low power mode if possible */
7246 ufshcd_hba_vreg_set_lpm(hba
);
7250 if (hba
->clk_scaling
.is_allowed
)
7251 ufshcd_resume_clkscaling(hba
);
7252 ufshcd_vreg_set_hpm(hba
);
7253 if (ufshcd_is_link_hibern8(hba
) && !ufshcd_uic_hibern8_exit(hba
))
7254 ufshcd_set_link_active(hba
);
7255 else if (ufshcd_is_link_off(hba
))
7256 ufshcd_host_reset_and_restore(hba
);
7258 if (!ufshcd_set_dev_pwr_mode(hba
, UFS_ACTIVE_PWR_MODE
))
7259 ufshcd_disable_auto_bkops(hba
);
7261 if (hba
->clk_scaling
.is_allowed
)
7262 ufshcd_resume_clkscaling(hba
);
7263 hba
->clk_gating
.is_suspended
= false;
7264 ufshcd_release(hba
);
7266 hba
->pm_op_in_progress
= 0;
7271 * ufshcd_resume - helper function for resume operations
7272 * @hba: per adapter instance
7273 * @pm_op: runtime PM or system PM
7275 * This function basically brings the UFS device, UniPro link and controller
7278 * Returns 0 for success and non-zero for failure
7280 static int ufshcd_resume(struct ufs_hba
*hba
, enum ufs_pm_op pm_op
)
7283 enum uic_link_state old_link_state
;
7285 hba
->pm_op_in_progress
= 1;
7286 old_link_state
= hba
->uic_link_state
;
7288 ufshcd_hba_vreg_set_hpm(hba
);
7289 /* Make sure clocks are enabled before accessing controller */
7290 ret
= ufshcd_setup_clocks(hba
, true);
7294 /* enable the host irq as host controller would be active soon */
7295 ret
= ufshcd_enable_irq(hba
);
7297 goto disable_irq_and_vops_clks
;
7299 ret
= ufshcd_vreg_set_hpm(hba
);
7301 goto disable_irq_and_vops_clks
;
7304 * Call vendor specific resume callback. As these callbacks may access
7305 * vendor specific host controller register space call them when the
7306 * host clocks are ON.
7308 ret
= ufshcd_vops_resume(hba
, pm_op
);
7312 if (ufshcd_is_link_hibern8(hba
)) {
7313 ret
= ufshcd_uic_hibern8_exit(hba
);
7315 ufshcd_set_link_active(hba
);
7317 goto vendor_suspend
;
7318 } else if (ufshcd_is_link_off(hba
)) {
7319 ret
= ufshcd_host_reset_and_restore(hba
);
7321 * ufshcd_host_reset_and_restore() should have already
7322 * set the link state as active
7324 if (ret
|| !ufshcd_is_link_active(hba
))
7325 goto vendor_suspend
;
7328 if (!ufshcd_is_ufs_dev_active(hba
)) {
7329 ret
= ufshcd_set_dev_pwr_mode(hba
, UFS_ACTIVE_PWR_MODE
);
7331 goto set_old_link_state
;
7334 if (ufshcd_keep_autobkops_enabled_except_suspend(hba
))
7335 ufshcd_enable_auto_bkops(hba
);
7338 * If BKOPs operations are urgently needed at this moment then
7339 * keep auto-bkops enabled or else disable it.
7341 ufshcd_urgent_bkops(hba
);
7343 hba
->clk_gating
.is_suspended
= false;
7345 if (hba
->clk_scaling
.is_allowed
)
7346 ufshcd_resume_clkscaling(hba
);
7348 /* Schedule clock gating in case of no access to UFS device yet */
7349 ufshcd_release(hba
);
7353 ufshcd_link_state_transition(hba
, old_link_state
, 0);
7355 ufshcd_vops_suspend(hba
, pm_op
);
7357 ufshcd_vreg_set_lpm(hba
);
7358 disable_irq_and_vops_clks
:
7359 ufshcd_disable_irq(hba
);
7360 if (hba
->clk_scaling
.is_allowed
)
7361 ufshcd_suspend_clkscaling(hba
);
7362 ufshcd_setup_clocks(hba
, false);
7364 hba
->pm_op_in_progress
= 0;
7369 * ufshcd_system_suspend - system suspend routine
7370 * @hba: per adapter instance
7371 * @pm_op: runtime PM or system PM
7373 * Check the description of ufshcd_suspend() function for more details.
7375 * Returns 0 for success and non-zero for failure
7377 int ufshcd_system_suspend(struct ufs_hba
*hba
)
7380 ktime_t start
= ktime_get();
7382 if (!hba
|| !hba
->is_powered
)
7385 if ((ufs_get_pm_lvl_to_dev_pwr_mode(hba
->spm_lvl
) ==
7386 hba
->curr_dev_pwr_mode
) &&
7387 (ufs_get_pm_lvl_to_link_pwr_state(hba
->spm_lvl
) ==
7388 hba
->uic_link_state
))
7391 if (pm_runtime_suspended(hba
->dev
)) {
7393 * UFS device and/or UFS link low power states during runtime
7394 * suspend seems to be different than what is expected during
7395 * system suspend. Hence runtime resume the devic & link and
7396 * let the system suspend low power states to take effect.
7397 * TODO: If resume takes longer time, we might have optimize
7398 * it in future by not resuming everything if possible.
7400 ret
= ufshcd_runtime_resume(hba
);
7405 ret
= ufshcd_suspend(hba
, UFS_SYSTEM_PM
);
7407 trace_ufshcd_system_suspend(dev_name(hba
->dev
), ret
,
7408 ktime_to_us(ktime_sub(ktime_get(), start
)),
7409 hba
->curr_dev_pwr_mode
, hba
->uic_link_state
);
7411 hba
->is_sys_suspended
= true;
7414 EXPORT_SYMBOL(ufshcd_system_suspend
);
7417 * ufshcd_system_resume - system resume routine
7418 * @hba: per adapter instance
7420 * Returns 0 for success and non-zero for failure
7423 int ufshcd_system_resume(struct ufs_hba
*hba
)
7426 ktime_t start
= ktime_get();
7431 if (!hba
->is_powered
|| pm_runtime_suspended(hba
->dev
))
7433 * Let the runtime resume take care of resuming
7434 * if runtime suspended.
7438 ret
= ufshcd_resume(hba
, UFS_SYSTEM_PM
);
7440 trace_ufshcd_system_resume(dev_name(hba
->dev
), ret
,
7441 ktime_to_us(ktime_sub(ktime_get(), start
)),
7442 hba
->curr_dev_pwr_mode
, hba
->uic_link_state
);
7445 EXPORT_SYMBOL(ufshcd_system_resume
);
7448 * ufshcd_runtime_suspend - runtime suspend routine
7449 * @hba: per adapter instance
7451 * Check the description of ufshcd_suspend() function for more details.
7453 * Returns 0 for success and non-zero for failure
7455 int ufshcd_runtime_suspend(struct ufs_hba
*hba
)
7458 ktime_t start
= ktime_get();
7463 if (!hba
->is_powered
)
7466 ret
= ufshcd_suspend(hba
, UFS_RUNTIME_PM
);
7468 trace_ufshcd_runtime_suspend(dev_name(hba
->dev
), ret
,
7469 ktime_to_us(ktime_sub(ktime_get(), start
)),
7470 hba
->curr_dev_pwr_mode
, hba
->uic_link_state
);
7473 EXPORT_SYMBOL(ufshcd_runtime_suspend
);
7476 * ufshcd_runtime_resume - runtime resume routine
7477 * @hba: per adapter instance
7479 * This function basically brings the UFS device, UniPro link and controller
7480 * to active state. Following operations are done in this function:
7482 * 1. Turn on all the controller related clocks
7483 * 2. Bring the UniPro link out of Hibernate state
7484 * 3. If UFS device is in sleep state, turn ON VCC rail and bring the UFS device
7486 * 4. If auto-bkops is enabled on the device, disable it.
7488 * So following would be the possible power state after this function return
7490 * S1: UFS device in Active state with VCC rail ON
7491 * UniPro link in Active state
7492 * All the UFS/UniPro controller clocks are ON
7494 * Returns 0 for success and non-zero for failure
7496 int ufshcd_runtime_resume(struct ufs_hba
*hba
)
7499 ktime_t start
= ktime_get();
7504 if (!hba
->is_powered
)
7507 ret
= ufshcd_resume(hba
, UFS_RUNTIME_PM
);
7509 trace_ufshcd_runtime_resume(dev_name(hba
->dev
), ret
,
7510 ktime_to_us(ktime_sub(ktime_get(), start
)),
7511 hba
->curr_dev_pwr_mode
, hba
->uic_link_state
);
7514 EXPORT_SYMBOL(ufshcd_runtime_resume
);
7516 int ufshcd_runtime_idle(struct ufs_hba
*hba
)
7520 EXPORT_SYMBOL(ufshcd_runtime_idle
);
7522 static inline ssize_t
ufshcd_pm_lvl_store(struct device
*dev
,
7523 struct device_attribute
*attr
,
7524 const char *buf
, size_t count
,
7527 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
7528 unsigned long flags
, value
;
7530 if (kstrtoul(buf
, 0, &value
))
7533 if ((value
< UFS_PM_LVL_0
) || (value
>= UFS_PM_LVL_MAX
))
7536 spin_lock_irqsave(hba
->host
->host_lock
, flags
);
7538 hba
->rpm_lvl
= value
;
7540 hba
->spm_lvl
= value
;
7541 spin_unlock_irqrestore(hba
->host
->host_lock
, flags
);
7545 static ssize_t
ufshcd_rpm_lvl_show(struct device
*dev
,
7546 struct device_attribute
*attr
, char *buf
)
7548 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
7552 curr_len
= snprintf(buf
, PAGE_SIZE
,
7553 "\nCurrent Runtime PM level [%d] => dev_state [%s] link_state [%s]\n",
7555 ufschd_ufs_dev_pwr_mode_to_string(
7556 ufs_pm_lvl_states
[hba
->rpm_lvl
].dev_state
),
7557 ufschd_uic_link_state_to_string(
7558 ufs_pm_lvl_states
[hba
->rpm_lvl
].link_state
));
7560 curr_len
+= snprintf((buf
+ curr_len
), (PAGE_SIZE
- curr_len
),
7561 "\nAll available Runtime PM levels info:\n");
7562 for (lvl
= UFS_PM_LVL_0
; lvl
< UFS_PM_LVL_MAX
; lvl
++)
7563 curr_len
+= snprintf((buf
+ curr_len
), (PAGE_SIZE
- curr_len
),
7564 "\tRuntime PM level [%d] => dev_state [%s] link_state [%s]\n",
7566 ufschd_ufs_dev_pwr_mode_to_string(
7567 ufs_pm_lvl_states
[lvl
].dev_state
),
7568 ufschd_uic_link_state_to_string(
7569 ufs_pm_lvl_states
[lvl
].link_state
));
7574 static ssize_t
ufshcd_rpm_lvl_store(struct device
*dev
,
7575 struct device_attribute
*attr
, const char *buf
, size_t count
)
7577 return ufshcd_pm_lvl_store(dev
, attr
, buf
, count
, true);
7580 static void ufshcd_add_rpm_lvl_sysfs_nodes(struct ufs_hba
*hba
)
7582 hba
->rpm_lvl_attr
.show
= ufshcd_rpm_lvl_show
;
7583 hba
->rpm_lvl_attr
.store
= ufshcd_rpm_lvl_store
;
7584 sysfs_attr_init(&hba
->rpm_lvl_attr
.attr
);
7585 hba
->rpm_lvl_attr
.attr
.name
= "rpm_lvl";
7586 hba
->rpm_lvl_attr
.attr
.mode
= 0644;
7587 if (device_create_file(hba
->dev
, &hba
->rpm_lvl_attr
))
7588 dev_err(hba
->dev
, "Failed to create sysfs for rpm_lvl\n");
7591 static ssize_t
ufshcd_spm_lvl_show(struct device
*dev
,
7592 struct device_attribute
*attr
, char *buf
)
7594 struct ufs_hba
*hba
= dev_get_drvdata(dev
);
7598 curr_len
= snprintf(buf
, PAGE_SIZE
,
7599 "\nCurrent System PM level [%d] => dev_state [%s] link_state [%s]\n",
7601 ufschd_ufs_dev_pwr_mode_to_string(
7602 ufs_pm_lvl_states
[hba
->spm_lvl
].dev_state
),
7603 ufschd_uic_link_state_to_string(
7604 ufs_pm_lvl_states
[hba
->spm_lvl
].link_state
));
7606 curr_len
+= snprintf((buf
+ curr_len
), (PAGE_SIZE
- curr_len
),
7607 "\nAll available System PM levels info:\n");
7608 for (lvl
= UFS_PM_LVL_0
; lvl
< UFS_PM_LVL_MAX
; lvl
++)
7609 curr_len
+= snprintf((buf
+ curr_len
), (PAGE_SIZE
- curr_len
),
7610 "\tSystem PM level [%d] => dev_state [%s] link_state [%s]\n",
7612 ufschd_ufs_dev_pwr_mode_to_string(
7613 ufs_pm_lvl_states
[lvl
].dev_state
),
7614 ufschd_uic_link_state_to_string(
7615 ufs_pm_lvl_states
[lvl
].link_state
));
7620 static ssize_t
ufshcd_spm_lvl_store(struct device
*dev
,
7621 struct device_attribute
*attr
, const char *buf
, size_t count
)
7623 return ufshcd_pm_lvl_store(dev
, attr
, buf
, count
, false);
7626 static void ufshcd_add_spm_lvl_sysfs_nodes(struct ufs_hba
*hba
)
7628 hba
->spm_lvl_attr
.show
= ufshcd_spm_lvl_show
;
7629 hba
->spm_lvl_attr
.store
= ufshcd_spm_lvl_store
;
7630 sysfs_attr_init(&hba
->spm_lvl_attr
.attr
);
7631 hba
->spm_lvl_attr
.attr
.name
= "spm_lvl";
7632 hba
->spm_lvl_attr
.attr
.mode
= 0644;
7633 if (device_create_file(hba
->dev
, &hba
->spm_lvl_attr
))
7634 dev_err(hba
->dev
, "Failed to create sysfs for spm_lvl\n");
7637 static inline void ufshcd_add_sysfs_nodes(struct ufs_hba
*hba
)
7639 ufshcd_add_rpm_lvl_sysfs_nodes(hba
);
7640 ufshcd_add_spm_lvl_sysfs_nodes(hba
);
7644 * ufshcd_shutdown - shutdown routine
7645 * @hba: per adapter instance
7647 * This function would power off both UFS device and UFS link.
7649 * Returns 0 always to allow force shutdown even in case of errors.
7651 int ufshcd_shutdown(struct ufs_hba
*hba
)
7655 if (ufshcd_is_ufs_dev_poweroff(hba
) && ufshcd_is_link_off(hba
))
7658 if (pm_runtime_suspended(hba
->dev
)) {
7659 ret
= ufshcd_runtime_resume(hba
);
7664 ret
= ufshcd_suspend(hba
, UFS_SHUTDOWN_PM
);
7667 dev_err(hba
->dev
, "%s failed, err %d\n", __func__
, ret
);
7668 /* allow force shutdown even in case of errors */
7671 EXPORT_SYMBOL(ufshcd_shutdown
);
7674 * ufshcd_remove - de-allocate SCSI host and host memory space
7675 * data structure memory
7676 * @hba - per adapter instance
7678 void ufshcd_remove(struct ufs_hba
*hba
)
7680 scsi_remove_host(hba
->host
);
7681 /* disable interrupts */
7682 ufshcd_disable_intr(hba
, hba
->intr_mask
);
7683 ufshcd_hba_stop(hba
, true);
7685 ufshcd_exit_clk_gating(hba
);
7686 if (ufshcd_is_clkscaling_supported(hba
))
7687 device_remove_file(hba
->dev
, &hba
->clk_scaling
.enable_attr
);
7688 ufshcd_hba_exit(hba
);
7690 EXPORT_SYMBOL_GPL(ufshcd_remove
);
7693 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
7694 * @hba: pointer to Host Bus Adapter (HBA)
7696 void ufshcd_dealloc_host(struct ufs_hba
*hba
)
7698 scsi_host_put(hba
->host
);
7700 EXPORT_SYMBOL_GPL(ufshcd_dealloc_host
);
7703 * ufshcd_set_dma_mask - Set dma mask based on the controller
7704 * addressing capability
7705 * @hba: per adapter instance
7707 * Returns 0 for success, non-zero for failure
7709 static int ufshcd_set_dma_mask(struct ufs_hba
*hba
)
7711 if (hba
->capabilities
& MASK_64_ADDRESSING_SUPPORT
) {
7712 if (!dma_set_mask_and_coherent(hba
->dev
, DMA_BIT_MASK(64)))
7715 return dma_set_mask_and_coherent(hba
->dev
, DMA_BIT_MASK(32));
7719 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
7720 * @dev: pointer to device handle
7721 * @hba_handle: driver private handle
7722 * Returns 0 on success, non-zero value on failure
7724 int ufshcd_alloc_host(struct device
*dev
, struct ufs_hba
**hba_handle
)
7726 struct Scsi_Host
*host
;
7727 struct ufs_hba
*hba
;
7732 "Invalid memory reference for dev is NULL\n");
7737 host
= scsi_host_alloc(&ufshcd_driver_template
,
7738 sizeof(struct ufs_hba
));
7740 dev_err(dev
, "scsi_host_alloc failed\n");
7744 hba
= shost_priv(host
);
7752 EXPORT_SYMBOL(ufshcd_alloc_host
);
7755 * ufshcd_init - Driver initialization routine
7756 * @hba: per-adapter instance
7757 * @mmio_base: base register address
7758 * @irq: Interrupt line of device
7759 * Returns 0 on success, non-zero value on failure
7761 int ufshcd_init(struct ufs_hba
*hba
, void __iomem
*mmio_base
, unsigned int irq
)
7764 struct Scsi_Host
*host
= hba
->host
;
7765 struct device
*dev
= hba
->dev
;
7769 "Invalid memory reference for mmio_base is NULL\n");
7774 hba
->mmio_base
= mmio_base
;
7777 err
= ufshcd_hba_init(hba
);
7781 /* Read capabilities registers */
7782 ufshcd_hba_capabilities(hba
);
7784 /* Get UFS version supported by the controller */
7785 hba
->ufs_version
= ufshcd_get_ufs_version(hba
);
7787 if ((hba
->ufs_version
!= UFSHCI_VERSION_10
) &&
7788 (hba
->ufs_version
!= UFSHCI_VERSION_11
) &&
7789 (hba
->ufs_version
!= UFSHCI_VERSION_20
) &&
7790 (hba
->ufs_version
!= UFSHCI_VERSION_21
))
7791 dev_err(hba
->dev
, "invalid UFS version 0x%x\n",
7794 /* Get Interrupt bit mask per version */
7795 hba
->intr_mask
= ufshcd_get_intr_mask(hba
);
7797 err
= ufshcd_set_dma_mask(hba
);
7799 dev_err(hba
->dev
, "set dma mask failed\n");
7803 /* Allocate memory for host memory space */
7804 err
= ufshcd_memory_alloc(hba
);
7806 dev_err(hba
->dev
, "Memory allocation failed\n");
7811 ufshcd_host_memory_configure(hba
);
7813 host
->can_queue
= hba
->nutrs
;
7814 host
->cmd_per_lun
= hba
->nutrs
;
7815 host
->max_id
= UFSHCD_MAX_ID
;
7816 host
->max_lun
= UFS_MAX_LUNS
;
7817 host
->max_channel
= UFSHCD_MAX_CHANNEL
;
7818 host
->unique_id
= host
->host_no
;
7819 host
->max_cmd_len
= MAX_CDB_SIZE
;
7821 hba
->max_pwr_info
.is_valid
= false;
7823 /* Initailize wait queue for task management */
7824 init_waitqueue_head(&hba
->tm_wq
);
7825 init_waitqueue_head(&hba
->tm_tag_wq
);
7827 /* Initialize work queues */
7828 INIT_WORK(&hba
->eh_work
, ufshcd_err_handler
);
7829 INIT_WORK(&hba
->eeh_work
, ufshcd_exception_event_handler
);
7831 /* Initialize UIC command mutex */
7832 mutex_init(&hba
->uic_cmd_mutex
);
7834 /* Initialize mutex for device management commands */
7835 mutex_init(&hba
->dev_cmd
.lock
);
7837 init_rwsem(&hba
->clk_scaling_lock
);
7839 /* Initialize device management tag acquire wait queue */
7840 init_waitqueue_head(&hba
->dev_cmd
.tag_wq
);
7842 ufshcd_init_clk_gating(hba
);
7845 * In order to avoid any spurious interrupt immediately after
7846 * registering UFS controller interrupt handler, clear any pending UFS
7847 * interrupt status and disable all the UFS interrupts.
7849 ufshcd_writel(hba
, ufshcd_readl(hba
, REG_INTERRUPT_STATUS
),
7850 REG_INTERRUPT_STATUS
);
7851 ufshcd_writel(hba
, 0, REG_INTERRUPT_ENABLE
);
7853 * Make sure that UFS interrupts are disabled and any pending interrupt
7854 * status is cleared before registering UFS interrupt handler.
7858 /* IRQ registration */
7859 err
= devm_request_irq(dev
, irq
, ufshcd_intr
, IRQF_SHARED
, UFSHCD
, hba
);
7861 dev_err(hba
->dev
, "request irq failed\n");
7864 hba
->is_irq_enabled
= true;
7867 err
= scsi_add_host(host
, hba
->dev
);
7869 dev_err(hba
->dev
, "scsi_add_host failed\n");
7873 /* Host controller enable */
7874 err
= ufshcd_hba_enable(hba
);
7876 dev_err(hba
->dev
, "Host controller enable failed\n");
7877 ufshcd_print_host_regs(hba
);
7878 ufshcd_print_host_state(hba
);
7879 goto out_remove_scsi_host
;
7882 if (ufshcd_is_clkscaling_supported(hba
)) {
7883 char wq_name
[sizeof("ufs_clkscaling_00")];
7885 INIT_WORK(&hba
->clk_scaling
.suspend_work
,
7886 ufshcd_clk_scaling_suspend_work
);
7887 INIT_WORK(&hba
->clk_scaling
.resume_work
,
7888 ufshcd_clk_scaling_resume_work
);
7890 snprintf(wq_name
, ARRAY_SIZE(wq_name
), "ufs_clkscaling_%d",
7892 hba
->clk_scaling
.workq
= create_singlethread_workqueue(wq_name
);
7894 ufshcd_clkscaling_init_sysfs(hba
);
7898 * Set the default power management level for runtime and system PM.
7899 * Default power saving mode is to keep UFS link in Hibern8 state
7900 * and UFS device in sleep state.
7902 hba
->rpm_lvl
= ufs_get_desired_pm_lvl_for_dev_link_state(
7904 UIC_LINK_HIBERN8_STATE
);
7905 hba
->spm_lvl
= ufs_get_desired_pm_lvl_for_dev_link_state(
7907 UIC_LINK_HIBERN8_STATE
);
7909 /* Hold auto suspend until async scan completes */
7910 pm_runtime_get_sync(dev
);
7913 * We are assuming that device wasn't put in sleep/power-down
7914 * state exclusively during the boot stage before kernel.
7915 * This assumption helps avoid doing link startup twice during
7916 * ufshcd_probe_hba().
7918 ufshcd_set_ufs_dev_active(hba
);
7920 async_schedule(ufshcd_async_scan
, hba
);
7921 ufshcd_add_sysfs_nodes(hba
);
7925 out_remove_scsi_host
:
7926 scsi_remove_host(hba
->host
);
7928 ufshcd_exit_clk_gating(hba
);
7930 hba
->is_irq_enabled
= false;
7931 ufshcd_hba_exit(hba
);
7935 EXPORT_SYMBOL_GPL(ufshcd_init
);
7937 MODULE_AUTHOR("Santosh Yaragnavi <santosh.sy@samsung.com>");
7938 MODULE_AUTHOR("Vinayak Holikatti <h.vinayak@samsung.com>");
7939 MODULE_DESCRIPTION("Generic UFS host controller driver Core");
7940 MODULE_LICENSE("GPL");
7941 MODULE_VERSION(UFSHCD_DRIVER_VERSION
);