Lines Matching +full:hba +full:- +full:cap
1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2011-2013 Samsung India Software Operations
5 * Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.
17 #include <linux/blk-pm.h>
30 #include "ufshcd-priv.h"
33 #include "ufs-sysfs.h"
34 #include "ufs-debugfs.h"
35 #include "ufs-fault-injection.h"
37 #include "ufshcd-crypto.h"
74 /* maximum number of link-startup retries */
104 static bool is_mcq_supported(struct ufs_hba *hba) in is_mcq_supported() argument
106 return hba->mcq_sup && use_mcq_mode; in is_mcq_supported()
110 …mode, "Control MCQ mode for controllers starting from UFSHCI 4.0. 1 - enable MCQ, 0 - disable MCQ.…
129 int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len, in ufshcd_dump_regs() argument
136 return -EINVAL; in ufshcd_dump_regs()
140 return -ENOMEM; in ufshcd_dump_regs()
147 regs[pos / 4] = ufshcd_readl(hba, offset + pos); in ufshcd_dump_regs()
160 UFSHCD_CMD_PER_LUN = 32 - UFSHCD_NUM_RESERVED,
161 UFSHCD_CAN_QUEUE = 32 - UFSHCD_NUM_RESERVED,
189 ((h)->eh_flags |= UFSHCD_EH_IN_PROGRESS)
191 ((h)->eh_flags & UFSHCD_EH_IN_PROGRESS)
193 ((h)->eh_flags &= ~UFSHCD_EH_IN_PROGRESS)
265 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba);
267 static int ufshcd_reset_and_restore(struct ufs_hba *hba);
269 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag);
270 static void ufshcd_hba_exit(struct ufs_hba *hba);
271 static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params);
272 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on);
273 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba);
274 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba);
275 static void ufshcd_resume_clkscaling(struct ufs_hba *hba);
276 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba);
277 static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba);
278 static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up);
280 static int ufshcd_change_power_mode(struct ufs_hba *hba,
282 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on);
283 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on);
284 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba,
286 static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba,
288 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba);
289 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba);
291 static inline void ufshcd_enable_irq(struct ufs_hba *hba) in ufshcd_enable_irq() argument
293 if (!hba->is_irq_enabled) { in ufshcd_enable_irq()
294 enable_irq(hba->irq); in ufshcd_enable_irq()
295 hba->is_irq_enabled = true; in ufshcd_enable_irq()
299 static inline void ufshcd_disable_irq(struct ufs_hba *hba) in ufshcd_disable_irq() argument
301 if (hba->is_irq_enabled) { in ufshcd_disable_irq()
302 disable_irq(hba->irq); in ufshcd_disable_irq()
303 hba->is_irq_enabled = false; in ufshcd_disable_irq()
307 static void ufshcd_configure_wb(struct ufs_hba *hba) in ufshcd_configure_wb() argument
309 if (!ufshcd_is_wb_allowed(hba)) in ufshcd_configure_wb()
312 ufshcd_wb_toggle(hba, true); in ufshcd_configure_wb()
314 ufshcd_wb_toggle_buf_flush_during_h8(hba, true); in ufshcd_configure_wb()
316 if (ufshcd_is_wb_buf_flush_allowed(hba)) in ufshcd_configure_wb()
317 ufshcd_wb_toggle_buf_flush(hba, true); in ufshcd_configure_wb()
320 static void ufshcd_scsi_unblock_requests(struct ufs_hba *hba) in ufshcd_scsi_unblock_requests() argument
322 if (atomic_dec_and_test(&hba->scsi_block_reqs_cnt)) in ufshcd_scsi_unblock_requests()
323 scsi_unblock_requests(hba->host); in ufshcd_scsi_unblock_requests()
326 static void ufshcd_scsi_block_requests(struct ufs_hba *hba) in ufshcd_scsi_block_requests() argument
328 if (atomic_inc_return(&hba->scsi_block_reqs_cnt) == 1) in ufshcd_scsi_block_requests()
329 scsi_block_requests(hba->host); in ufshcd_scsi_block_requests()
332 static void ufshcd_add_cmd_upiu_trace(struct ufs_hba *hba, unsigned int tag, in ufshcd_add_cmd_upiu_trace() argument
335 struct utp_upiu_req *rq = hba->lrb[tag].ucd_req_ptr; in ufshcd_add_cmd_upiu_trace()
342 header = &rq->header; in ufshcd_add_cmd_upiu_trace()
344 header = &hba->lrb[tag].ucd_rsp_ptr->header; in ufshcd_add_cmd_upiu_trace()
346 trace_ufshcd_upiu(dev_name(hba->dev), str_t, header, &rq->sc.cdb, in ufshcd_add_cmd_upiu_trace()
350 static void ufshcd_add_query_upiu_trace(struct ufs_hba *hba, in ufshcd_add_query_upiu_trace() argument
357 trace_ufshcd_upiu(dev_name(hba->dev), str_t, &rq_rsp->header, in ufshcd_add_query_upiu_trace()
358 &rq_rsp->qr, UFS_TSF_OSF); in ufshcd_add_query_upiu_trace()
361 static void ufshcd_add_tm_upiu_trace(struct ufs_hba *hba, unsigned int tag, in ufshcd_add_tm_upiu_trace() argument
364 struct utp_task_req_desc *descp = &hba->utmrdl_base_addr[tag]; in ufshcd_add_tm_upiu_trace()
370 trace_ufshcd_upiu(dev_name(hba->dev), str_t, in ufshcd_add_tm_upiu_trace()
371 &descp->upiu_req.req_header, in ufshcd_add_tm_upiu_trace()
372 &descp->upiu_req.input_param1, in ufshcd_add_tm_upiu_trace()
375 trace_ufshcd_upiu(dev_name(hba->dev), str_t, in ufshcd_add_tm_upiu_trace()
376 &descp->upiu_rsp.rsp_header, in ufshcd_add_tm_upiu_trace()
377 &descp->upiu_rsp.output_param1, in ufshcd_add_tm_upiu_trace()
381 static void ufshcd_add_uic_command_trace(struct ufs_hba *hba, in ufshcd_add_uic_command_trace() argument
391 cmd = ucmd->command; in ufshcd_add_uic_command_trace()
393 cmd = ufshcd_readl(hba, REG_UIC_COMMAND); in ufshcd_add_uic_command_trace()
395 trace_ufshcd_uic_command(dev_name(hba->dev), str_t, cmd, in ufshcd_add_uic_command_trace()
396 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_1), in ufshcd_add_uic_command_trace()
397 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2), in ufshcd_add_uic_command_trace()
398 ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3)); in ufshcd_add_uic_command_trace()
401 static void ufshcd_add_command_trace(struct ufs_hba *hba, unsigned int tag, in ufshcd_add_command_trace() argument
408 int hwq_id = -1; in ufshcd_add_command_trace()
409 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_add_command_trace()
410 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_add_command_trace()
412 int transfer_len = -1; in ufshcd_add_command_trace()
418 ufshcd_add_cmd_upiu_trace(hba, tag, str_t); in ufshcd_add_command_trace()
422 opcode = cmd->cmnd[0]; in ufshcd_add_command_trace()
429 be32_to_cpu(lrbp->ucd_req_ptr->sc.exp_data_transfer_len); in ufshcd_add_command_trace()
432 group_id = lrbp->cmd->cmnd[6]; in ufshcd_add_command_trace()
441 intr = ufshcd_readl(hba, REG_INTERRUPT_STATUS); in ufshcd_add_command_trace()
443 if (is_mcq_enabled(hba)) { in ufshcd_add_command_trace()
444 struct ufs_hw_queue *hwq = ufshcd_mcq_req_to_hwq(hba, rq); in ufshcd_add_command_trace()
446 hwq_id = hwq->id; in ufshcd_add_command_trace()
448 doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); in ufshcd_add_command_trace()
450 trace_ufshcd_command(dev_name(hba->dev), str_t, tag, in ufshcd_add_command_trace()
454 static void ufshcd_print_clk_freqs(struct ufs_hba *hba) in ufshcd_print_clk_freqs() argument
457 struct list_head *head = &hba->clk_list_head; in ufshcd_print_clk_freqs()
463 if (!IS_ERR_OR_NULL(clki->clk) && clki->min_freq && in ufshcd_print_clk_freqs()
464 clki->max_freq) in ufshcd_print_clk_freqs()
465 dev_err(hba->dev, "clk: %s, rate: %u\n", in ufshcd_print_clk_freqs()
466 clki->name, clki->curr_freq); in ufshcd_print_clk_freqs()
470 static void ufshcd_print_evt(struct ufs_hba *hba, u32 id, in ufshcd_print_evt() argument
480 e = &hba->ufs_stats.event[id]; in ufshcd_print_evt()
483 int p = (i + e->pos) % UFS_EVENT_HIST_LENGTH; in ufshcd_print_evt()
485 if (e->tstamp[p] == 0) in ufshcd_print_evt()
487 dev_err(hba->dev, "%s[%d] = 0x%x at %lld us\n", err_name, p, in ufshcd_print_evt()
488 e->val[p], div_u64(e->tstamp[p], 1000)); in ufshcd_print_evt()
493 dev_err(hba->dev, "No record of %s\n", err_name); in ufshcd_print_evt()
495 dev_err(hba->dev, "%s: total cnt=%llu\n", err_name, e->cnt); in ufshcd_print_evt()
498 static void ufshcd_print_evt_hist(struct ufs_hba *hba) in ufshcd_print_evt_hist() argument
500 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); in ufshcd_print_evt_hist()
502 ufshcd_print_evt(hba, UFS_EVT_PA_ERR, "pa_err"); in ufshcd_print_evt_hist()
503 ufshcd_print_evt(hba, UFS_EVT_DL_ERR, "dl_err"); in ufshcd_print_evt_hist()
504 ufshcd_print_evt(hba, UFS_EVT_NL_ERR, "nl_err"); in ufshcd_print_evt_hist()
505 ufshcd_print_evt(hba, UFS_EVT_TL_ERR, "tl_err"); in ufshcd_print_evt_hist()
506 ufshcd_print_evt(hba, UFS_EVT_DME_ERR, "dme_err"); in ufshcd_print_evt_hist()
507 ufshcd_print_evt(hba, UFS_EVT_AUTO_HIBERN8_ERR, in ufshcd_print_evt_hist()
509 ufshcd_print_evt(hba, UFS_EVT_FATAL_ERR, "fatal_err"); in ufshcd_print_evt_hist()
510 ufshcd_print_evt(hba, UFS_EVT_LINK_STARTUP_FAIL, in ufshcd_print_evt_hist()
512 ufshcd_print_evt(hba, UFS_EVT_RESUME_ERR, "resume_fail"); in ufshcd_print_evt_hist()
513 ufshcd_print_evt(hba, UFS_EVT_SUSPEND_ERR, in ufshcd_print_evt_hist()
515 ufshcd_print_evt(hba, UFS_EVT_WL_RES_ERR, "wlun resume_fail"); in ufshcd_print_evt_hist()
516 ufshcd_print_evt(hba, UFS_EVT_WL_SUSP_ERR, in ufshcd_print_evt_hist()
518 ufshcd_print_evt(hba, UFS_EVT_DEV_RESET, "dev_reset"); in ufshcd_print_evt_hist()
519 ufshcd_print_evt(hba, UFS_EVT_HOST_RESET, "host_reset"); in ufshcd_print_evt_hist()
520 ufshcd_print_evt(hba, UFS_EVT_ABORT, "task_abort"); in ufshcd_print_evt_hist()
522 ufshcd_vops_dbg_register_dump(hba); in ufshcd_print_evt_hist()
526 void ufshcd_print_tr(struct ufs_hba *hba, int tag, bool pr_prdt) in ufshcd_print_tr() argument
531 lrbp = &hba->lrb[tag]; in ufshcd_print_tr()
533 dev_err(hba->dev, "UPIU[%d] - issue time %lld us\n", in ufshcd_print_tr()
534 tag, div_u64(lrbp->issue_time_stamp_local_clock, 1000)); in ufshcd_print_tr()
535 dev_err(hba->dev, "UPIU[%d] - complete time %lld us\n", in ufshcd_print_tr()
536 tag, div_u64(lrbp->compl_time_stamp_local_clock, 1000)); in ufshcd_print_tr()
537 dev_err(hba->dev, in ufshcd_print_tr()
538 "UPIU[%d] - Transfer Request Descriptor phys@0x%llx\n", in ufshcd_print_tr()
539 tag, (u64)lrbp->utrd_dma_addr); in ufshcd_print_tr()
541 ufshcd_hex_dump("UPIU TRD: ", lrbp->utr_descriptor_ptr, in ufshcd_print_tr()
543 dev_err(hba->dev, "UPIU[%d] - Request UPIU phys@0x%llx\n", tag, in ufshcd_print_tr()
544 (u64)lrbp->ucd_req_dma_addr); in ufshcd_print_tr()
545 ufshcd_hex_dump("UPIU REQ: ", lrbp->ucd_req_ptr, in ufshcd_print_tr()
547 dev_err(hba->dev, "UPIU[%d] - Response UPIU phys@0x%llx\n", tag, in ufshcd_print_tr()
548 (u64)lrbp->ucd_rsp_dma_addr); in ufshcd_print_tr()
549 ufshcd_hex_dump("UPIU RSP: ", lrbp->ucd_rsp_ptr, in ufshcd_print_tr()
553 lrbp->utr_descriptor_ptr->prd_table_length); in ufshcd_print_tr()
554 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) in ufshcd_print_tr()
555 prdt_length /= ufshcd_sg_entry_size(hba); in ufshcd_print_tr()
557 dev_err(hba->dev, in ufshcd_print_tr()
558 "UPIU[%d] - PRDT - %d entries phys@0x%llx\n", in ufshcd_print_tr()
560 (u64)lrbp->ucd_prdt_dma_addr); in ufshcd_print_tr()
563 ufshcd_hex_dump("UPIU PRDT: ", lrbp->ucd_prdt_ptr, in ufshcd_print_tr()
564 ufshcd_sg_entry_size(hba) * prdt_length); in ufshcd_print_tr()
569 struct scsi_device *sdev = req->q->queuedata; in ufshcd_print_tr_iter()
570 struct Scsi_Host *shost = sdev->host; in ufshcd_print_tr_iter()
571 struct ufs_hba *hba = shost_priv(shost); in ufshcd_print_tr_iter() local
573 ufshcd_print_tr(hba, req->tag, *(bool *)priv); in ufshcd_print_tr_iter()
579 * ufshcd_print_trs_all - print trs for all started requests.
580 * @hba: per-adapter instance.
583 static void ufshcd_print_trs_all(struct ufs_hba *hba, bool pr_prdt) in ufshcd_print_trs_all() argument
585 blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_print_tr_iter, &pr_prdt); in ufshcd_print_trs_all()
588 static void ufshcd_print_tmrs(struct ufs_hba *hba, unsigned long bitmap) in ufshcd_print_tmrs() argument
592 for_each_set_bit(tag, &bitmap, hba->nutmrs) { in ufshcd_print_tmrs()
593 struct utp_task_req_desc *tmrdp = &hba->utmrdl_base_addr[tag]; in ufshcd_print_tmrs()
595 dev_err(hba->dev, "TM[%d] - Task Management Header\n", tag); in ufshcd_print_tmrs()
600 static void ufshcd_print_host_state(struct ufs_hba *hba) in ufshcd_print_host_state() argument
602 const struct scsi_device *sdev_ufs = hba->ufs_device_wlun; in ufshcd_print_host_state()
604 dev_err(hba->dev, "UFS Host state=%d\n", hba->ufshcd_state); in ufshcd_print_host_state()
605 dev_err(hba->dev, "outstanding reqs=0x%lx tasks=0x%lx\n", in ufshcd_print_host_state()
606 hba->outstanding_reqs, hba->outstanding_tasks); in ufshcd_print_host_state()
607 dev_err(hba->dev, "saved_err=0x%x, saved_uic_err=0x%x\n", in ufshcd_print_host_state()
608 hba->saved_err, hba->saved_uic_err); in ufshcd_print_host_state()
609 dev_err(hba->dev, "Device power mode=%d, UIC link state=%d\n", in ufshcd_print_host_state()
610 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_print_host_state()
611 dev_err(hba->dev, "PM in progress=%d, sys. suspended=%d\n", in ufshcd_print_host_state()
612 hba->pm_op_in_progress, hba->is_sys_suspended); in ufshcd_print_host_state()
613 dev_err(hba->dev, "Auto BKOPS=%d, Host self-block=%d\n", in ufshcd_print_host_state()
614 hba->auto_bkops_enabled, hba->host->host_self_blocked); in ufshcd_print_host_state()
615 dev_err(hba->dev, "Clk gate=%d\n", hba->clk_gating.state); in ufshcd_print_host_state()
616 dev_err(hba->dev, in ufshcd_print_host_state()
618 div_u64(hba->ufs_stats.last_hibern8_exit_tstamp, 1000), in ufshcd_print_host_state()
619 hba->ufs_stats.hibern8_exit_cnt); in ufshcd_print_host_state()
620 dev_err(hba->dev, "last intr at %lld us, last intr status=0x%x\n", in ufshcd_print_host_state()
621 div_u64(hba->ufs_stats.last_intr_ts, 1000), in ufshcd_print_host_state()
622 hba->ufs_stats.last_intr_status); in ufshcd_print_host_state()
623 dev_err(hba->dev, "error handling flags=0x%x, req. abort count=%d\n", in ufshcd_print_host_state()
624 hba->eh_flags, hba->req_abort_count); in ufshcd_print_host_state()
625 dev_err(hba->dev, "hba->ufs_version=0x%x, Host capabilities=0x%x, caps=0x%x\n", in ufshcd_print_host_state()
626 hba->ufs_version, hba->capabilities, hba->caps); in ufshcd_print_host_state()
627 dev_err(hba->dev, "quirks=0x%x, dev. quirks=0x%x\n", hba->quirks, in ufshcd_print_host_state()
628 hba->dev_quirks); in ufshcd_print_host_state()
630 dev_err(hba->dev, "UFS dev info: %.8s %.16s rev %.4s\n", in ufshcd_print_host_state()
631 sdev_ufs->vendor, sdev_ufs->model, sdev_ufs->rev); in ufshcd_print_host_state()
633 ufshcd_print_clk_freqs(hba); in ufshcd_print_host_state()
637 * ufshcd_print_pwr_info - print power params as saved in hba
639 * @hba: per-adapter instance
641 static void ufshcd_print_pwr_info(struct ufs_hba *hba) in ufshcd_print_pwr_info() argument
655 * never-ending cycles of messages written back to storage by user space in ufshcd_print_pwr_info()
658 dev_dbg(hba->dev, "%s:[RX, TX]: gear=[%d, %d], lane[%d, %d], pwr[%s, %s], rate = %d\n", in ufshcd_print_pwr_info()
660 hba->pwr_info.gear_rx, hba->pwr_info.gear_tx, in ufshcd_print_pwr_info()
661 hba->pwr_info.lane_rx, hba->pwr_info.lane_tx, in ufshcd_print_pwr_info()
662 names[hba->pwr_info.pwr_rx], in ufshcd_print_pwr_info()
663 names[hba->pwr_info.pwr_tx], in ufshcd_print_pwr_info()
664 hba->pwr_info.hs_rate); in ufshcd_print_pwr_info()
667 static void ufshcd_device_reset(struct ufs_hba *hba) in ufshcd_device_reset() argument
671 err = ufshcd_vops_device_reset(hba); in ufshcd_device_reset()
674 ufshcd_set_ufs_dev_active(hba); in ufshcd_device_reset()
675 if (ufshcd_is_wb_allowed(hba)) { in ufshcd_device_reset()
676 hba->dev_info.wb_enabled = false; in ufshcd_device_reset()
677 hba->dev_info.wb_buf_flush_enabled = false; in ufshcd_device_reset()
680 if (err != -EOPNOTSUPP) in ufshcd_device_reset()
681 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, err); in ufshcd_device_reset()
697 * ufshcd_wait_for_register - wait for register value to change
698 * @hba: per-adapter interface
705 * Return: -ETIMEDOUT on error, zero on success.
707 static int ufshcd_wait_for_register(struct ufs_hba *hba, u32 reg, u32 mask, in ufshcd_wait_for_register() argument
717 while ((ufshcd_readl(hba, reg) & mask) != val) { in ufshcd_wait_for_register()
720 if ((ufshcd_readl(hba, reg) & mask) != val) in ufshcd_wait_for_register()
721 err = -ETIMEDOUT; in ufshcd_wait_for_register()
730 * ufshcd_get_intr_mask - Get the interrupt bit mask
731 * @hba: Pointer to adapter instance
735 static inline u32 ufshcd_get_intr_mask(struct ufs_hba *hba) in ufshcd_get_intr_mask() argument
737 if (hba->ufs_version == ufshci_version(1, 0)) in ufshcd_get_intr_mask()
739 if (hba->ufs_version <= ufshci_version(2, 0)) in ufshcd_get_intr_mask()
746 * ufshcd_get_ufs_version - Get the UFS version supported by the HBA
747 * @hba: Pointer to adapter instance
751 static inline u32 ufshcd_get_ufs_version(struct ufs_hba *hba) in ufshcd_get_ufs_version() argument
755 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION) in ufshcd_get_ufs_version()
756 ufshci_ver = ufshcd_vops_get_ufs_hci_version(hba); in ufshcd_get_ufs_version()
758 ufshci_ver = ufshcd_readl(hba, REG_UFS_VERSION); in ufshcd_get_ufs_version()
772 * ufshcd_is_device_present - Check if any device connected to
774 * @hba: pointer to adapter instance
778 static inline bool ufshcd_is_device_present(struct ufs_hba *hba) in ufshcd_is_device_present() argument
780 return ufshcd_readl(hba, REG_CONTROLLER_STATUS) & DEVICE_PRESENT; in ufshcd_is_device_present()
784 * ufshcd_get_tr_ocs - Get the UTRD Overall Command Status
796 return le32_to_cpu(cqe->status) & MASK_OCS; in ufshcd_get_tr_ocs()
798 return lrbp->utr_descriptor_ptr->header.ocs & MASK_OCS; in ufshcd_get_tr_ocs()
802 * ufshcd_utrl_clear() - Clear requests from the controller request list.
803 * @hba: per adapter instance
806 static inline void ufshcd_utrl_clear(struct ufs_hba *hba, u32 mask) in ufshcd_utrl_clear() argument
808 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) in ufshcd_utrl_clear()
823 ufshcd_writel(hba, ~mask, REG_UTP_TRANSFER_REQ_LIST_CLEAR); in ufshcd_utrl_clear()
827 * ufshcd_utmrl_clear - Clear a bit in UTMRLCLR register
828 * @hba: per adapter instance
831 static inline void ufshcd_utmrl_clear(struct ufs_hba *hba, u32 pos) in ufshcd_utmrl_clear() argument
833 if (hba->quirks & UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR) in ufshcd_utmrl_clear()
834 ufshcd_writel(hba, (1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); in ufshcd_utmrl_clear()
836 ufshcd_writel(hba, ~(1 << pos), REG_UTP_TASK_REQ_LIST_CLEAR); in ufshcd_utmrl_clear()
840 * ufshcd_get_lists_status - Check UCRDY, UTRLRDY and UTMRLRDY
851 * ufshcd_get_uic_cmd_result - Get the UIC command result
852 * @hba: Pointer to adapter instance
856 * Return: 0 on success; non-zero value on error.
858 static inline int ufshcd_get_uic_cmd_result(struct ufs_hba *hba) in ufshcd_get_uic_cmd_result() argument
860 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_2) & in ufshcd_get_uic_cmd_result()
865 * ufshcd_get_dme_attr_val - Get the value of attribute returned by UIC command
866 * @hba: Pointer to adapter instance
870 * Return: 0 on success; non-zero value on error.
872 static inline u32 ufshcd_get_dme_attr_val(struct ufs_hba *hba) in ufshcd_get_dme_attr_val() argument
874 return ufshcd_readl(hba, REG_UIC_COMMAND_ARG_3); in ufshcd_get_dme_attr_val()
878 * ufshcd_get_req_rsp - returns the TR response transaction type
886 return ucd_rsp_ptr->header.transaction_code; in ufshcd_get_req_rsp()
890 * ufshcd_is_exception_event - Check if the device raised an exception event
900 return ucd_rsp_ptr->header.device_information & 1; in ufshcd_is_exception_event()
904 * ufshcd_reset_intr_aggr - Reset interrupt aggregation values.
905 * @hba: per adapter instance
908 ufshcd_reset_intr_aggr(struct ufs_hba *hba) in ufshcd_reset_intr_aggr() argument
910 ufshcd_writel(hba, INT_AGGR_ENABLE | in ufshcd_reset_intr_aggr()
916 * ufshcd_config_intr_aggr - Configure interrupt aggregation values.
917 * @hba: per adapter instance
922 ufshcd_config_intr_aggr(struct ufs_hba *hba, u8 cnt, u8 tmout) in ufshcd_config_intr_aggr() argument
924 ufshcd_writel(hba, INT_AGGR_ENABLE | INT_AGGR_PARAM_WRITE | in ufshcd_config_intr_aggr()
931 * ufshcd_disable_intr_aggr - Disables interrupt aggregation.
932 * @hba: per adapter instance
934 static inline void ufshcd_disable_intr_aggr(struct ufs_hba *hba) in ufshcd_disable_intr_aggr() argument
936 ufshcd_writel(hba, 0, REG_UTP_TRANSFER_REQ_INT_AGG_CONTROL); in ufshcd_disable_intr_aggr()
940 * ufshcd_enable_run_stop_reg - Enable run-stop registers,
941 * When run-stop registers are set to 1, it indicates the
943 * @hba: per adapter instance
945 static void ufshcd_enable_run_stop_reg(struct ufs_hba *hba) in ufshcd_enable_run_stop_reg() argument
947 ufshcd_writel(hba, UTP_TASK_REQ_LIST_RUN_STOP_BIT, in ufshcd_enable_run_stop_reg()
949 ufshcd_writel(hba, UTP_TRANSFER_REQ_LIST_RUN_STOP_BIT, in ufshcd_enable_run_stop_reg()
954 * ufshcd_hba_start - Start controller initialization sequence
955 * @hba: per adapter instance
957 static inline void ufshcd_hba_start(struct ufs_hba *hba) in ufshcd_hba_start() argument
961 if (ufshcd_crypto_enable(hba)) in ufshcd_hba_start()
964 ufshcd_writel(hba, val, REG_CONTROLLER_ENABLE); in ufshcd_hba_start()
968 * ufshcd_is_hba_active - Get controller state
969 * @hba: per adapter instance
973 bool ufshcd_is_hba_active(struct ufs_hba *hba) in ufshcd_is_hba_active() argument
975 return ufshcd_readl(hba, REG_CONTROLLER_ENABLE) & CONTROLLER_ENABLE; in ufshcd_is_hba_active()
979 u32 ufshcd_get_local_unipro_ver(struct ufs_hba *hba) in ufshcd_get_local_unipro_ver() argument
982 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_get_local_unipro_ver()
989 static bool ufshcd_is_unipro_pa_params_tuning_req(struct ufs_hba *hba) in ufshcd_is_unipro_pa_params_tuning_req() argument
1000 return ufshcd_get_local_unipro_ver(hba) < UFS_UNIPRO_VER_1_6; in ufshcd_is_unipro_pa_params_tuning_req()
1004 * ufshcd_set_clk_freq - set UFS controller clock frequencies
1005 * @hba: per adapter instance
1010 static int ufshcd_set_clk_freq(struct ufs_hba *hba, bool scale_up) in ufshcd_set_clk_freq() argument
1014 struct list_head *head = &hba->clk_list_head; in ufshcd_set_clk_freq()
1020 if (!IS_ERR_OR_NULL(clki->clk)) { in ufshcd_set_clk_freq()
1021 if (scale_up && clki->max_freq) { in ufshcd_set_clk_freq()
1022 if (clki->curr_freq == clki->max_freq) in ufshcd_set_clk_freq()
1025 ret = clk_set_rate(clki->clk, clki->max_freq); in ufshcd_set_clk_freq()
1027 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", in ufshcd_set_clk_freq()
1028 __func__, clki->name, in ufshcd_set_clk_freq()
1029 clki->max_freq, ret); in ufshcd_set_clk_freq()
1032 trace_ufshcd_clk_scaling(dev_name(hba->dev), in ufshcd_set_clk_freq()
1033 "scaled up", clki->name, in ufshcd_set_clk_freq()
1034 clki->curr_freq, in ufshcd_set_clk_freq()
1035 clki->max_freq); in ufshcd_set_clk_freq()
1037 clki->curr_freq = clki->max_freq; in ufshcd_set_clk_freq()
1039 } else if (!scale_up && clki->min_freq) { in ufshcd_set_clk_freq()
1040 if (clki->curr_freq == clki->min_freq) in ufshcd_set_clk_freq()
1043 ret = clk_set_rate(clki->clk, clki->min_freq); in ufshcd_set_clk_freq()
1045 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", in ufshcd_set_clk_freq()
1046 __func__, clki->name, in ufshcd_set_clk_freq()
1047 clki->min_freq, ret); in ufshcd_set_clk_freq()
1050 trace_ufshcd_clk_scaling(dev_name(hba->dev), in ufshcd_set_clk_freq()
1051 "scaled down", clki->name, in ufshcd_set_clk_freq()
1052 clki->curr_freq, in ufshcd_set_clk_freq()
1053 clki->min_freq); in ufshcd_set_clk_freq()
1054 clki->curr_freq = clki->min_freq; in ufshcd_set_clk_freq()
1057 dev_dbg(hba->dev, "%s: clk: %s, rate: %lu\n", __func__, in ufshcd_set_clk_freq()
1058 clki->name, clk_get_rate(clki->clk)); in ufshcd_set_clk_freq()
1066 * ufshcd_scale_clks - scale up or scale down UFS controller clocks
1067 * @hba: per adapter instance
1072 static int ufshcd_scale_clks(struct ufs_hba *hba, bool scale_up) in ufshcd_scale_clks() argument
1077 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, PRE_CHANGE); in ufshcd_scale_clks()
1081 ret = ufshcd_set_clk_freq(hba, scale_up); in ufshcd_scale_clks()
1085 ret = ufshcd_vops_clk_scale_notify(hba, scale_up, POST_CHANGE); in ufshcd_scale_clks()
1087 ufshcd_set_clk_freq(hba, !scale_up); in ufshcd_scale_clks()
1090 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), in ufshcd_scale_clks()
1097 * ufshcd_is_devfreq_scaling_required - check if scaling is required or not
1098 * @hba: per adapter instance
1103 static bool ufshcd_is_devfreq_scaling_required(struct ufs_hba *hba, in ufshcd_is_devfreq_scaling_required() argument
1107 struct list_head *head = &hba->clk_list_head; in ufshcd_is_devfreq_scaling_required()
1113 if (!IS_ERR_OR_NULL(clki->clk)) { in ufshcd_is_devfreq_scaling_required()
1114 if (scale_up && clki->max_freq) { in ufshcd_is_devfreq_scaling_required()
1115 if (clki->curr_freq == clki->max_freq) in ufshcd_is_devfreq_scaling_required()
1118 } else if (!scale_up && clki->min_freq) { in ufshcd_is_devfreq_scaling_required()
1119 if (clki->curr_freq == clki->min_freq) in ufshcd_is_devfreq_scaling_required()
1136 static u32 ufshcd_pending_cmds(struct ufs_hba *hba) in ufshcd_pending_cmds() argument
1141 lockdep_assert_held(hba->host->host_lock); in ufshcd_pending_cmds()
1142 __shost_for_each_device(sdev, hba->host) in ufshcd_pending_cmds()
1143 pending += sbitmap_weight(&sdev->budget_map); in ufshcd_pending_cmds()
1152 * Return: 0 upon success; -EBUSY upon timeout.
1154 static int ufshcd_wait_for_doorbell_clr(struct ufs_hba *hba, in ufshcd_wait_for_doorbell_clr() argument
1164 ufshcd_hold(hba); in ufshcd_wait_for_doorbell_clr()
1165 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1172 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL) { in ufshcd_wait_for_doorbell_clr()
1173 ret = -EBUSY; in ufshcd_wait_for_doorbell_clr()
1177 tm_doorbell = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); in ufshcd_wait_for_doorbell_clr()
1178 tr_pending = ufshcd_pending_cmds(hba); in ufshcd_wait_for_doorbell_clr()
1186 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1198 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1202 dev_err(hba->dev, in ufshcd_wait_for_doorbell_clr()
1205 ret = -EBUSY; in ufshcd_wait_for_doorbell_clr()
1208 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_wait_for_doorbell_clr()
1209 ufshcd_release(hba); in ufshcd_wait_for_doorbell_clr()
1214 * ufshcd_scale_gear - scale up/down UFS gear
1215 * @hba: per adapter instance
1218 * Return: 0 for success; -EBUSY if scaling can't happen at this time;
1219 * non-zero for any other errors.
1221 static int ufshcd_scale_gear(struct ufs_hba *hba, bool scale_up) in ufshcd_scale_gear() argument
1227 memcpy(&new_pwr_info, &hba->clk_scaling.saved_pwr_info, in ufshcd_scale_gear()
1230 memcpy(&new_pwr_info, &hba->pwr_info, in ufshcd_scale_gear()
1233 if (hba->pwr_info.gear_tx > hba->clk_scaling.min_gear || in ufshcd_scale_gear()
1234 hba->pwr_info.gear_rx > hba->clk_scaling.min_gear) { in ufshcd_scale_gear()
1236 memcpy(&hba->clk_scaling.saved_pwr_info, in ufshcd_scale_gear()
1237 &hba->pwr_info, in ufshcd_scale_gear()
1241 new_pwr_info.gear_tx = hba->clk_scaling.min_gear; in ufshcd_scale_gear()
1242 new_pwr_info.gear_rx = hba->clk_scaling.min_gear; in ufshcd_scale_gear()
1247 ret = ufshcd_config_pwr_mode(hba, &new_pwr_info); in ufshcd_scale_gear()
1249 dev_err(hba->dev, "%s: failed err %d, old gear: (tx %d rx %d), new gear: (tx %d rx %d)", in ufshcd_scale_gear()
1251 hba->pwr_info.gear_tx, hba->pwr_info.gear_rx, in ufshcd_scale_gear()
1261 * Return: 0 upon success; -EBUSY upon timeout.
1263 static int ufshcd_clock_scaling_prepare(struct ufs_hba *hba, u64 timeout_us) in ufshcd_clock_scaling_prepare() argument
1270 blk_mq_quiesce_tagset(&hba->host->tag_set); in ufshcd_clock_scaling_prepare()
1271 mutex_lock(&hba->wb_mutex); in ufshcd_clock_scaling_prepare()
1272 down_write(&hba->clk_scaling_lock); in ufshcd_clock_scaling_prepare()
1274 if (!hba->clk_scaling.is_allowed || in ufshcd_clock_scaling_prepare()
1275 ufshcd_wait_for_doorbell_clr(hba, timeout_us)) { in ufshcd_clock_scaling_prepare()
1276 ret = -EBUSY; in ufshcd_clock_scaling_prepare()
1277 up_write(&hba->clk_scaling_lock); in ufshcd_clock_scaling_prepare()
1278 mutex_unlock(&hba->wb_mutex); in ufshcd_clock_scaling_prepare()
1279 blk_mq_unquiesce_tagset(&hba->host->tag_set); in ufshcd_clock_scaling_prepare()
1284 ufshcd_hold(hba); in ufshcd_clock_scaling_prepare()
1290 static void ufshcd_clock_scaling_unprepare(struct ufs_hba *hba, int err, bool scale_up) in ufshcd_clock_scaling_unprepare() argument
1292 up_write(&hba->clk_scaling_lock); in ufshcd_clock_scaling_unprepare()
1295 if (ufshcd_enable_wb_if_scaling_up(hba) && !err) in ufshcd_clock_scaling_unprepare()
1296 ufshcd_wb_toggle(hba, scale_up); in ufshcd_clock_scaling_unprepare()
1298 mutex_unlock(&hba->wb_mutex); in ufshcd_clock_scaling_unprepare()
1300 blk_mq_unquiesce_tagset(&hba->host->tag_set); in ufshcd_clock_scaling_unprepare()
1301 ufshcd_release(hba); in ufshcd_clock_scaling_unprepare()
1305 * ufshcd_devfreq_scale - scale up/down UFS clocks and gear
1306 * @hba: per adapter instance
1309 * Return: 0 for success; -EBUSY if scaling can't happen at this time; non-zero
1312 static int ufshcd_devfreq_scale(struct ufs_hba *hba, bool scale_up) in ufshcd_devfreq_scale() argument
1316 ret = ufshcd_clock_scaling_prepare(hba, 1 * USEC_PER_SEC); in ufshcd_devfreq_scale()
1322 ret = ufshcd_scale_gear(hba, false); in ufshcd_devfreq_scale()
1327 ret = ufshcd_scale_clks(hba, scale_up); in ufshcd_devfreq_scale()
1330 ufshcd_scale_gear(hba, true); in ufshcd_devfreq_scale()
1336 ret = ufshcd_scale_gear(hba, true); in ufshcd_devfreq_scale()
1338 ufshcd_scale_clks(hba, false); in ufshcd_devfreq_scale()
1344 ufshcd_clock_scaling_unprepare(hba, ret, scale_up); in ufshcd_devfreq_scale()
1350 struct ufs_hba *hba = container_of(work, struct ufs_hba, in ufshcd_clk_scaling_suspend_work() local
1354 spin_lock_irqsave(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_suspend_work()
1355 if (hba->clk_scaling.active_reqs || hba->clk_scaling.is_suspended) { in ufshcd_clk_scaling_suspend_work()
1356 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_suspend_work()
1359 hba->clk_scaling.is_suspended = true; in ufshcd_clk_scaling_suspend_work()
1360 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_suspend_work()
1362 __ufshcd_suspend_clkscaling(hba); in ufshcd_clk_scaling_suspend_work()
1367 struct ufs_hba *hba = container_of(work, struct ufs_hba, in ufshcd_clk_scaling_resume_work() local
1371 spin_lock_irqsave(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_resume_work()
1372 if (!hba->clk_scaling.is_suspended) { in ufshcd_clk_scaling_resume_work()
1373 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_resume_work()
1376 hba->clk_scaling.is_suspended = false; in ufshcd_clk_scaling_resume_work()
1377 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_clk_scaling_resume_work()
1379 devfreq_resume_device(hba->devfreq); in ufshcd_clk_scaling_resume_work()
1386 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_devfreq_target() local
1389 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_target()
1393 if (!ufshcd_is_clkscaling_supported(hba)) in ufshcd_devfreq_target()
1394 return -EINVAL; in ufshcd_devfreq_target()
1396 clki = list_first_entry(&hba->clk_list_head, struct ufs_clk_info, list); in ufshcd_devfreq_target()
1398 *freq = (unsigned long) clk_round_rate(clki->clk, *freq); in ufshcd_devfreq_target()
1399 spin_lock_irqsave(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1400 if (ufshcd_eh_in_progress(hba)) { in ufshcd_devfreq_target()
1401 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1405 if (!hba->clk_scaling.active_reqs) in ufshcd_devfreq_target()
1409 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1413 /* Decide based on the rounded-off frequency and update */ in ufshcd_devfreq_target()
1414 scale_up = *freq == clki->max_freq; in ufshcd_devfreq_target()
1416 *freq = clki->min_freq; in ufshcd_devfreq_target()
1418 if (!ufshcd_is_devfreq_scaling_required(hba, scale_up)) { in ufshcd_devfreq_target()
1419 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1423 spin_unlock_irqrestore(hba->host->host_lock, irq_flags); in ufshcd_devfreq_target()
1426 ret = ufshcd_devfreq_scale(hba, scale_up); in ufshcd_devfreq_target()
1428 trace_ufshcd_profile_clk_scaling(dev_name(hba->dev), in ufshcd_devfreq_target()
1434 queue_work(hba->clk_scaling.workq, in ufshcd_devfreq_target()
1435 &hba->clk_scaling.suspend_work); in ufshcd_devfreq_target()
1443 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_devfreq_get_dev_status() local
1444 struct ufs_clk_scaling *scaling = &hba->clk_scaling; in ufshcd_devfreq_get_dev_status()
1446 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_get_dev_status()
1450 if (!ufshcd_is_clkscaling_supported(hba)) in ufshcd_devfreq_get_dev_status()
1451 return -EINVAL; in ufshcd_devfreq_get_dev_status()
1455 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_devfreq_get_dev_status()
1457 if (!scaling->window_start_t) in ufshcd_devfreq_get_dev_status()
1466 stat->current_frequency = clki->curr_freq; in ufshcd_devfreq_get_dev_status()
1467 if (scaling->is_busy_started) in ufshcd_devfreq_get_dev_status()
1468 scaling->tot_busy_t += ktime_us_delta(curr_t, in ufshcd_devfreq_get_dev_status()
1469 scaling->busy_start_t); in ufshcd_devfreq_get_dev_status()
1471 stat->total_time = ktime_us_delta(curr_t, scaling->window_start_t); in ufshcd_devfreq_get_dev_status()
1472 stat->busy_time = scaling->tot_busy_t; in ufshcd_devfreq_get_dev_status()
1474 scaling->window_start_t = curr_t; in ufshcd_devfreq_get_dev_status()
1475 scaling->tot_busy_t = 0; in ufshcd_devfreq_get_dev_status()
1477 if (scaling->active_reqs) { in ufshcd_devfreq_get_dev_status()
1478 scaling->busy_start_t = curr_t; in ufshcd_devfreq_get_dev_status()
1479 scaling->is_busy_started = true; in ufshcd_devfreq_get_dev_status()
1481 scaling->busy_start_t = 0; in ufshcd_devfreq_get_dev_status()
1482 scaling->is_busy_started = false; in ufshcd_devfreq_get_dev_status()
1484 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_devfreq_get_dev_status()
1488 static int ufshcd_devfreq_init(struct ufs_hba *hba) in ufshcd_devfreq_init() argument
1490 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_init()
1500 dev_pm_opp_add(hba->dev, clki->min_freq, 0); in ufshcd_devfreq_init()
1501 dev_pm_opp_add(hba->dev, clki->max_freq, 0); in ufshcd_devfreq_init()
1503 ufshcd_vops_config_scaling_param(hba, &hba->vps->devfreq_profile, in ufshcd_devfreq_init()
1504 &hba->vps->ondemand_data); in ufshcd_devfreq_init()
1505 devfreq = devfreq_add_device(hba->dev, in ufshcd_devfreq_init()
1506 &hba->vps->devfreq_profile, in ufshcd_devfreq_init()
1508 &hba->vps->ondemand_data); in ufshcd_devfreq_init()
1511 dev_err(hba->dev, "Unable to register with devfreq %d\n", ret); in ufshcd_devfreq_init()
1513 dev_pm_opp_remove(hba->dev, clki->min_freq); in ufshcd_devfreq_init()
1514 dev_pm_opp_remove(hba->dev, clki->max_freq); in ufshcd_devfreq_init()
1518 hba->devfreq = devfreq; in ufshcd_devfreq_init()
1523 static void ufshcd_devfreq_remove(struct ufs_hba *hba) in ufshcd_devfreq_remove() argument
1525 struct list_head *clk_list = &hba->clk_list_head; in ufshcd_devfreq_remove()
1528 if (!hba->devfreq) in ufshcd_devfreq_remove()
1531 devfreq_remove_device(hba->devfreq); in ufshcd_devfreq_remove()
1532 hba->devfreq = NULL; in ufshcd_devfreq_remove()
1535 dev_pm_opp_remove(hba->dev, clki->min_freq); in ufshcd_devfreq_remove()
1536 dev_pm_opp_remove(hba->dev, clki->max_freq); in ufshcd_devfreq_remove()
1539 static void __ufshcd_suspend_clkscaling(struct ufs_hba *hba) in __ufshcd_suspend_clkscaling() argument
1543 devfreq_suspend_device(hba->devfreq); in __ufshcd_suspend_clkscaling()
1544 spin_lock_irqsave(hba->host->host_lock, flags); in __ufshcd_suspend_clkscaling()
1545 hba->clk_scaling.window_start_t = 0; in __ufshcd_suspend_clkscaling()
1546 spin_unlock_irqrestore(hba->host->host_lock, flags); in __ufshcd_suspend_clkscaling()
1549 static void ufshcd_suspend_clkscaling(struct ufs_hba *hba) in ufshcd_suspend_clkscaling() argument
1554 cancel_work_sync(&hba->clk_scaling.suspend_work); in ufshcd_suspend_clkscaling()
1555 cancel_work_sync(&hba->clk_scaling.resume_work); in ufshcd_suspend_clkscaling()
1557 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_suspend_clkscaling()
1558 if (!hba->clk_scaling.is_suspended) { in ufshcd_suspend_clkscaling()
1560 hba->clk_scaling.is_suspended = true; in ufshcd_suspend_clkscaling()
1562 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_suspend_clkscaling()
1565 __ufshcd_suspend_clkscaling(hba); in ufshcd_suspend_clkscaling()
1568 static void ufshcd_resume_clkscaling(struct ufs_hba *hba) in ufshcd_resume_clkscaling() argument
1573 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_resume_clkscaling()
1574 if (hba->clk_scaling.is_suspended) { in ufshcd_resume_clkscaling()
1576 hba->clk_scaling.is_suspended = false; in ufshcd_resume_clkscaling()
1578 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_resume_clkscaling()
1581 devfreq_resume_device(hba->devfreq); in ufshcd_resume_clkscaling()
1587 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_clkscale_enable_show() local
1589 return sysfs_emit(buf, "%d\n", hba->clk_scaling.is_enabled); in ufshcd_clkscale_enable_show()
1595 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_clkscale_enable_store() local
1600 return -EINVAL; in ufshcd_clkscale_enable_store()
1602 down(&hba->host_sem); in ufshcd_clkscale_enable_store()
1603 if (!ufshcd_is_user_access_allowed(hba)) { in ufshcd_clkscale_enable_store()
1604 err = -EBUSY; in ufshcd_clkscale_enable_store()
1609 if (value == hba->clk_scaling.is_enabled) in ufshcd_clkscale_enable_store()
1612 ufshcd_rpm_get_sync(hba); in ufshcd_clkscale_enable_store()
1613 ufshcd_hold(hba); in ufshcd_clkscale_enable_store()
1615 hba->clk_scaling.is_enabled = value; in ufshcd_clkscale_enable_store()
1618 ufshcd_resume_clkscaling(hba); in ufshcd_clkscale_enable_store()
1620 ufshcd_suspend_clkscaling(hba); in ufshcd_clkscale_enable_store()
1621 err = ufshcd_devfreq_scale(hba, true); in ufshcd_clkscale_enable_store()
1623 dev_err(hba->dev, "%s: failed to scale clocks up %d\n", in ufshcd_clkscale_enable_store()
1627 ufshcd_release(hba); in ufshcd_clkscale_enable_store()
1628 ufshcd_rpm_put_sync(hba); in ufshcd_clkscale_enable_store()
1630 up(&hba->host_sem); in ufshcd_clkscale_enable_store()
1634 static void ufshcd_init_clk_scaling_sysfs(struct ufs_hba *hba) in ufshcd_init_clk_scaling_sysfs() argument
1636 hba->clk_scaling.enable_attr.show = ufshcd_clkscale_enable_show; in ufshcd_init_clk_scaling_sysfs()
1637 hba->clk_scaling.enable_attr.store = ufshcd_clkscale_enable_store; in ufshcd_init_clk_scaling_sysfs()
1638 sysfs_attr_init(&hba->clk_scaling.enable_attr.attr); in ufshcd_init_clk_scaling_sysfs()
1639 hba->clk_scaling.enable_attr.attr.name = "clkscale_enable"; in ufshcd_init_clk_scaling_sysfs()
1640 hba->clk_scaling.enable_attr.attr.mode = 0644; in ufshcd_init_clk_scaling_sysfs()
1641 if (device_create_file(hba->dev, &hba->clk_scaling.enable_attr)) in ufshcd_init_clk_scaling_sysfs()
1642 dev_err(hba->dev, "Failed to create sysfs for clkscale_enable\n"); in ufshcd_init_clk_scaling_sysfs()
1645 static void ufshcd_remove_clk_scaling_sysfs(struct ufs_hba *hba) in ufshcd_remove_clk_scaling_sysfs() argument
1647 if (hba->clk_scaling.enable_attr.attr.name) in ufshcd_remove_clk_scaling_sysfs()
1648 device_remove_file(hba->dev, &hba->clk_scaling.enable_attr); in ufshcd_remove_clk_scaling_sysfs()
1651 static void ufshcd_init_clk_scaling(struct ufs_hba *hba) in ufshcd_init_clk_scaling() argument
1655 if (!ufshcd_is_clkscaling_supported(hba)) in ufshcd_init_clk_scaling()
1658 if (!hba->clk_scaling.min_gear) in ufshcd_init_clk_scaling()
1659 hba->clk_scaling.min_gear = UFS_HS_G1; in ufshcd_init_clk_scaling()
1661 INIT_WORK(&hba->clk_scaling.suspend_work, in ufshcd_init_clk_scaling()
1663 INIT_WORK(&hba->clk_scaling.resume_work, in ufshcd_init_clk_scaling()
1667 hba->host->host_no); in ufshcd_init_clk_scaling()
1668 hba->clk_scaling.workq = create_singlethread_workqueue(wq_name); in ufshcd_init_clk_scaling()
1670 hba->clk_scaling.is_initialized = true; in ufshcd_init_clk_scaling()
1673 static void ufshcd_exit_clk_scaling(struct ufs_hba *hba) in ufshcd_exit_clk_scaling() argument
1675 if (!hba->clk_scaling.is_initialized) in ufshcd_exit_clk_scaling()
1678 ufshcd_remove_clk_scaling_sysfs(hba); in ufshcd_exit_clk_scaling()
1679 destroy_workqueue(hba->clk_scaling.workq); in ufshcd_exit_clk_scaling()
1680 ufshcd_devfreq_remove(hba); in ufshcd_exit_clk_scaling()
1681 hba->clk_scaling.is_initialized = false; in ufshcd_exit_clk_scaling()
1688 struct ufs_hba *hba = container_of(work, struct ufs_hba, in ufshcd_ungate_work() local
1691 cancel_delayed_work_sync(&hba->clk_gating.gate_work); in ufshcd_ungate_work()
1693 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_ungate_work()
1694 if (hba->clk_gating.state == CLKS_ON) { in ufshcd_ungate_work()
1695 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_ungate_work()
1699 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_ungate_work()
1700 ufshcd_hba_vreg_set_hpm(hba); in ufshcd_ungate_work()
1701 ufshcd_setup_clocks(hba, true); in ufshcd_ungate_work()
1703 ufshcd_enable_irq(hba); in ufshcd_ungate_work()
1706 if (ufshcd_can_hibern8_during_gating(hba)) { in ufshcd_ungate_work()
1708 hba->clk_gating.is_suspended = true; in ufshcd_ungate_work()
1709 if (ufshcd_is_link_hibern8(hba)) { in ufshcd_ungate_work()
1710 ret = ufshcd_uic_hibern8_exit(hba); in ufshcd_ungate_work()
1712 dev_err(hba->dev, "%s: hibern8 exit failed %d\n", in ufshcd_ungate_work()
1715 ufshcd_set_link_active(hba); in ufshcd_ungate_work()
1717 hba->clk_gating.is_suspended = false; in ufshcd_ungate_work()
1722 * ufshcd_hold - Enable clocks that were gated earlier due to ufshcd_release.
1724 * @hba: per adapter instance
1726 void ufshcd_hold(struct ufs_hba *hba) in ufshcd_hold() argument
1731 if (!ufshcd_is_clkgating_allowed(hba) || in ufshcd_hold()
1732 !hba->clk_gating.is_initialized) in ufshcd_hold()
1734 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hold()
1735 hba->clk_gating.active_reqs++; in ufshcd_hold()
1738 switch (hba->clk_gating.state) { in ufshcd_hold()
1748 if (ufshcd_can_hibern8_during_gating(hba) && in ufshcd_hold()
1749 ufshcd_is_link_hibern8(hba)) { in ufshcd_hold()
1750 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hold()
1751 flush_result = flush_work(&hba->clk_gating.ungate_work); in ufshcd_hold()
1752 if (hba->clk_gating.is_suspended && !flush_result) in ufshcd_hold()
1754 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hold()
1759 if (cancel_delayed_work(&hba->clk_gating.gate_work)) { in ufshcd_hold()
1760 hba->clk_gating.state = CLKS_ON; in ufshcd_hold()
1761 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_hold()
1762 hba->clk_gating.state); in ufshcd_hold()
1772 hba->clk_gating.state = REQ_CLKS_ON; in ufshcd_hold()
1773 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_hold()
1774 hba->clk_gating.state); in ufshcd_hold()
1775 queue_work(hba->clk_gating.clk_gating_workq, in ufshcd_hold()
1776 &hba->clk_gating.ungate_work); in ufshcd_hold()
1783 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hold()
1784 flush_work(&hba->clk_gating.ungate_work); in ufshcd_hold()
1786 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hold()
1789 dev_err(hba->dev, "%s: clk gating is in invalid state %d\n", in ufshcd_hold()
1790 __func__, hba->clk_gating.state); in ufshcd_hold()
1793 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hold()
1799 struct ufs_hba *hba = container_of(work, struct ufs_hba, in ufshcd_gate_work() local
1804 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_gate_work()
1811 if (hba->clk_gating.is_suspended || in ufshcd_gate_work()
1812 (hba->clk_gating.state != REQ_CLKS_OFF)) { in ufshcd_gate_work()
1813 hba->clk_gating.state = CLKS_ON; in ufshcd_gate_work()
1814 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_gate_work()
1815 hba->clk_gating.state); in ufshcd_gate_work()
1819 if (hba->clk_gating.active_reqs in ufshcd_gate_work()
1820 || hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL in ufshcd_gate_work()
1821 || hba->outstanding_reqs || hba->outstanding_tasks in ufshcd_gate_work()
1822 || hba->active_uic_cmd || hba->uic_async_done) in ufshcd_gate_work()
1825 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_gate_work()
1828 if (ufshcd_can_hibern8_during_gating(hba)) { in ufshcd_gate_work()
1829 ret = ufshcd_uic_hibern8_enter(hba); in ufshcd_gate_work()
1831 hba->clk_gating.state = CLKS_ON; in ufshcd_gate_work()
1832 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", in ufshcd_gate_work()
1834 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_gate_work()
1835 hba->clk_gating.state); in ufshcd_gate_work()
1838 ufshcd_set_link_hibern8(hba); in ufshcd_gate_work()
1841 ufshcd_disable_irq(hba); in ufshcd_gate_work()
1843 ufshcd_setup_clocks(hba, false); in ufshcd_gate_work()
1846 ufshcd_hba_vreg_set_lpm(hba); in ufshcd_gate_work()
1856 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_gate_work()
1857 if (hba->clk_gating.state == REQ_CLKS_OFF) { in ufshcd_gate_work()
1858 hba->clk_gating.state = CLKS_OFF; in ufshcd_gate_work()
1859 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_gate_work()
1860 hba->clk_gating.state); in ufshcd_gate_work()
1863 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_gate_work()
1869 static void __ufshcd_release(struct ufs_hba *hba) in __ufshcd_release() argument
1871 if (!ufshcd_is_clkgating_allowed(hba)) in __ufshcd_release()
1874 hba->clk_gating.active_reqs--; in __ufshcd_release()
1876 if (hba->clk_gating.active_reqs || hba->clk_gating.is_suspended || in __ufshcd_release()
1877 hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL || in __ufshcd_release()
1878 hba->outstanding_tasks || !hba->clk_gating.is_initialized || in __ufshcd_release()
1879 hba->active_uic_cmd || hba->uic_async_done || in __ufshcd_release()
1880 hba->clk_gating.state == CLKS_OFF) in __ufshcd_release()
1883 hba->clk_gating.state = REQ_CLKS_OFF; in __ufshcd_release()
1884 trace_ufshcd_clk_gating(dev_name(hba->dev), hba->clk_gating.state); in __ufshcd_release()
1885 queue_delayed_work(hba->clk_gating.clk_gating_workq, in __ufshcd_release()
1886 &hba->clk_gating.gate_work, in __ufshcd_release()
1887 msecs_to_jiffies(hba->clk_gating.delay_ms)); in __ufshcd_release()
1890 void ufshcd_release(struct ufs_hba *hba) in ufshcd_release() argument
1894 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_release()
1895 __ufshcd_release(hba); in ufshcd_release()
1896 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_release()
1903 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_clkgate_delay_show() local
1905 return sysfs_emit(buf, "%lu\n", hba->clk_gating.delay_ms); in ufshcd_clkgate_delay_show()
1910 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_clkgate_delay_set() local
1913 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clkgate_delay_set()
1914 hba->clk_gating.delay_ms = value; in ufshcd_clkgate_delay_set()
1915 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clkgate_delay_set()
1925 return -EINVAL; in ufshcd_clkgate_delay_store()
1934 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_clkgate_enable_show() local
1936 return sysfs_emit(buf, "%d\n", hba->clk_gating.is_enabled); in ufshcd_clkgate_enable_show()
1942 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_clkgate_enable_store() local
1947 return -EINVAL; in ufshcd_clkgate_enable_store()
1951 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clkgate_enable_store()
1952 if (value == hba->clk_gating.is_enabled) in ufshcd_clkgate_enable_store()
1956 __ufshcd_release(hba); in ufshcd_clkgate_enable_store()
1958 hba->clk_gating.active_reqs++; in ufshcd_clkgate_enable_store()
1960 hba->clk_gating.is_enabled = value; in ufshcd_clkgate_enable_store()
1962 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clkgate_enable_store()
1966 static void ufshcd_init_clk_gating_sysfs(struct ufs_hba *hba) in ufshcd_init_clk_gating_sysfs() argument
1968 hba->clk_gating.delay_attr.show = ufshcd_clkgate_delay_show; in ufshcd_init_clk_gating_sysfs()
1969 hba->clk_gating.delay_attr.store = ufshcd_clkgate_delay_store; in ufshcd_init_clk_gating_sysfs()
1970 sysfs_attr_init(&hba->clk_gating.delay_attr.attr); in ufshcd_init_clk_gating_sysfs()
1971 hba->clk_gating.delay_attr.attr.name = "clkgate_delay_ms"; in ufshcd_init_clk_gating_sysfs()
1972 hba->clk_gating.delay_attr.attr.mode = 0644; in ufshcd_init_clk_gating_sysfs()
1973 if (device_create_file(hba->dev, &hba->clk_gating.delay_attr)) in ufshcd_init_clk_gating_sysfs()
1974 dev_err(hba->dev, "Failed to create sysfs for clkgate_delay\n"); in ufshcd_init_clk_gating_sysfs()
1976 hba->clk_gating.enable_attr.show = ufshcd_clkgate_enable_show; in ufshcd_init_clk_gating_sysfs()
1977 hba->clk_gating.enable_attr.store = ufshcd_clkgate_enable_store; in ufshcd_init_clk_gating_sysfs()
1978 sysfs_attr_init(&hba->clk_gating.enable_attr.attr); in ufshcd_init_clk_gating_sysfs()
1979 hba->clk_gating.enable_attr.attr.name = "clkgate_enable"; in ufshcd_init_clk_gating_sysfs()
1980 hba->clk_gating.enable_attr.attr.mode = 0644; in ufshcd_init_clk_gating_sysfs()
1981 if (device_create_file(hba->dev, &hba->clk_gating.enable_attr)) in ufshcd_init_clk_gating_sysfs()
1982 dev_err(hba->dev, "Failed to create sysfs for clkgate_enable\n"); in ufshcd_init_clk_gating_sysfs()
1985 static void ufshcd_remove_clk_gating_sysfs(struct ufs_hba *hba) in ufshcd_remove_clk_gating_sysfs() argument
1987 if (hba->clk_gating.delay_attr.attr.name) in ufshcd_remove_clk_gating_sysfs()
1988 device_remove_file(hba->dev, &hba->clk_gating.delay_attr); in ufshcd_remove_clk_gating_sysfs()
1989 if (hba->clk_gating.enable_attr.attr.name) in ufshcd_remove_clk_gating_sysfs()
1990 device_remove_file(hba->dev, &hba->clk_gating.enable_attr); in ufshcd_remove_clk_gating_sysfs()
1993 static void ufshcd_init_clk_gating(struct ufs_hba *hba) in ufshcd_init_clk_gating() argument
1997 if (!ufshcd_is_clkgating_allowed(hba)) in ufshcd_init_clk_gating()
2000 hba->clk_gating.state = CLKS_ON; in ufshcd_init_clk_gating()
2002 hba->clk_gating.delay_ms = 150; in ufshcd_init_clk_gating()
2003 INIT_DELAYED_WORK(&hba->clk_gating.gate_work, ufshcd_gate_work); in ufshcd_init_clk_gating()
2004 INIT_WORK(&hba->clk_gating.ungate_work, ufshcd_ungate_work); in ufshcd_init_clk_gating()
2007 hba->host->host_no); in ufshcd_init_clk_gating()
2008 hba->clk_gating.clk_gating_workq = alloc_ordered_workqueue(wq_name, in ufshcd_init_clk_gating()
2011 ufshcd_init_clk_gating_sysfs(hba); in ufshcd_init_clk_gating()
2013 hba->clk_gating.is_enabled = true; in ufshcd_init_clk_gating()
2014 hba->clk_gating.is_initialized = true; in ufshcd_init_clk_gating()
2017 static void ufshcd_exit_clk_gating(struct ufs_hba *hba) in ufshcd_exit_clk_gating() argument
2019 if (!hba->clk_gating.is_initialized) in ufshcd_exit_clk_gating()
2022 ufshcd_remove_clk_gating_sysfs(hba); in ufshcd_exit_clk_gating()
2025 ufshcd_hold(hba); in ufshcd_exit_clk_gating()
2026 hba->clk_gating.is_initialized = false; in ufshcd_exit_clk_gating()
2027 ufshcd_release(hba); in ufshcd_exit_clk_gating()
2029 destroy_workqueue(hba->clk_gating.clk_gating_workq); in ufshcd_exit_clk_gating()
2032 static void ufshcd_clk_scaling_start_busy(struct ufs_hba *hba) in ufshcd_clk_scaling_start_busy() argument
2038 if (!ufshcd_is_clkscaling_supported(hba)) in ufshcd_clk_scaling_start_busy()
2041 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clk_scaling_start_busy()
2042 if (!hba->clk_scaling.active_reqs++) in ufshcd_clk_scaling_start_busy()
2045 if (!hba->clk_scaling.is_enabled || hba->pm_op_in_progress) { in ufshcd_clk_scaling_start_busy()
2046 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clk_scaling_start_busy()
2051 queue_work(hba->clk_scaling.workq, in ufshcd_clk_scaling_start_busy()
2052 &hba->clk_scaling.resume_work); in ufshcd_clk_scaling_start_busy()
2054 if (!hba->clk_scaling.window_start_t) { in ufshcd_clk_scaling_start_busy()
2055 hba->clk_scaling.window_start_t = curr_t; in ufshcd_clk_scaling_start_busy()
2056 hba->clk_scaling.tot_busy_t = 0; in ufshcd_clk_scaling_start_busy()
2057 hba->clk_scaling.is_busy_started = false; in ufshcd_clk_scaling_start_busy()
2060 if (!hba->clk_scaling.is_busy_started) { in ufshcd_clk_scaling_start_busy()
2061 hba->clk_scaling.busy_start_t = curr_t; in ufshcd_clk_scaling_start_busy()
2062 hba->clk_scaling.is_busy_started = true; in ufshcd_clk_scaling_start_busy()
2064 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clk_scaling_start_busy()
2067 static void ufshcd_clk_scaling_update_busy(struct ufs_hba *hba) in ufshcd_clk_scaling_update_busy() argument
2069 struct ufs_clk_scaling *scaling = &hba->clk_scaling; in ufshcd_clk_scaling_update_busy()
2072 if (!ufshcd_is_clkscaling_supported(hba)) in ufshcd_clk_scaling_update_busy()
2075 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clk_scaling_update_busy()
2076 hba->clk_scaling.active_reqs--; in ufshcd_clk_scaling_update_busy()
2077 if (!scaling->active_reqs && scaling->is_busy_started) { in ufshcd_clk_scaling_update_busy()
2078 scaling->tot_busy_t += ktime_to_us(ktime_sub(ktime_get(), in ufshcd_clk_scaling_update_busy()
2079 scaling->busy_start_t)); in ufshcd_clk_scaling_update_busy()
2080 scaling->busy_start_t = 0; in ufshcd_clk_scaling_update_busy()
2081 scaling->is_busy_started = false; in ufshcd_clk_scaling_update_busy()
2083 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clk_scaling_update_busy()
2093 return -EINVAL; in ufshcd_monitor_opcode2dir()
2096 static inline bool ufshcd_should_inform_monitor(struct ufs_hba *hba, in ufshcd_should_inform_monitor() argument
2099 const struct ufs_hba_monitor *m = &hba->monitor; in ufshcd_should_inform_monitor()
2101 return (m->enabled && lrbp && lrbp->cmd && in ufshcd_should_inform_monitor()
2102 (!m->chunk_size || m->chunk_size == lrbp->cmd->sdb.length) && in ufshcd_should_inform_monitor()
2103 ktime_before(hba->monitor.enabled_ts, lrbp->issue_time_stamp)); in ufshcd_should_inform_monitor()
2106 static void ufshcd_start_monitor(struct ufs_hba *hba, in ufshcd_start_monitor() argument
2109 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd); in ufshcd_start_monitor()
2112 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_start_monitor()
2113 if (dir >= 0 && hba->monitor.nr_queued[dir]++ == 0) in ufshcd_start_monitor()
2114 hba->monitor.busy_start_ts[dir] = ktime_get(); in ufshcd_start_monitor()
2115 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_start_monitor()
2118 static void ufshcd_update_monitor(struct ufs_hba *hba, const struct ufshcd_lrb *lrbp) in ufshcd_update_monitor() argument
2120 int dir = ufshcd_monitor_opcode2dir(*lrbp->cmd->cmnd); in ufshcd_update_monitor()
2123 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_update_monitor()
2124 if (dir >= 0 && hba->monitor.nr_queued[dir] > 0) { in ufshcd_update_monitor()
2125 const struct request *req = scsi_cmd_to_rq(lrbp->cmd); in ufshcd_update_monitor()
2126 struct ufs_hba_monitor *m = &hba->monitor; in ufshcd_update_monitor()
2129 now = lrbp->compl_time_stamp; in ufshcd_update_monitor()
2130 inc = ktime_sub(now, m->busy_start_ts[dir]); in ufshcd_update_monitor()
2131 m->total_busy[dir] = ktime_add(m->total_busy[dir], inc); in ufshcd_update_monitor()
2132 m->nr_sec_rw[dir] += blk_rq_sectors(req); in ufshcd_update_monitor()
2135 m->nr_req[dir]++; in ufshcd_update_monitor()
2136 lat = ktime_sub(now, lrbp->issue_time_stamp); in ufshcd_update_monitor()
2137 m->lat_sum[dir] += lat; in ufshcd_update_monitor()
2138 if (m->lat_max[dir] < lat || !m->lat_max[dir]) in ufshcd_update_monitor()
2139 m->lat_max[dir] = lat; in ufshcd_update_monitor()
2140 if (m->lat_min[dir] > lat || !m->lat_min[dir]) in ufshcd_update_monitor()
2141 m->lat_min[dir] = lat; in ufshcd_update_monitor()
2143 m->nr_queued[dir]--; in ufshcd_update_monitor()
2145 m->busy_start_ts[dir] = now; in ufshcd_update_monitor()
2147 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_update_monitor()
2151 * ufshcd_send_command - Send SCSI or device management commands
2152 * @hba: per adapter instance
2157 void ufshcd_send_command(struct ufs_hba *hba, unsigned int task_tag, in ufshcd_send_command() argument
2160 struct ufshcd_lrb *lrbp = &hba->lrb[task_tag]; in ufshcd_send_command()
2163 lrbp->issue_time_stamp = ktime_get(); in ufshcd_send_command()
2164 lrbp->issue_time_stamp_local_clock = local_clock(); in ufshcd_send_command()
2165 lrbp->compl_time_stamp = ktime_set(0, 0); in ufshcd_send_command()
2166 lrbp->compl_time_stamp_local_clock = 0; in ufshcd_send_command()
2167 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_SEND); in ufshcd_send_command()
2168 ufshcd_clk_scaling_start_busy(hba); in ufshcd_send_command()
2169 if (unlikely(ufshcd_should_inform_monitor(hba, lrbp))) in ufshcd_send_command()
2170 ufshcd_start_monitor(hba, lrbp); in ufshcd_send_command()
2172 if (is_mcq_enabled(hba)) { in ufshcd_send_command()
2174 struct utp_transfer_req_desc *src = lrbp->utr_descriptor_ptr; in ufshcd_send_command()
2177 spin_lock(&hwq->sq_lock); in ufshcd_send_command()
2178 dest = hwq->sqe_base_addr + hwq->sq_tail_slot; in ufshcd_send_command()
2181 spin_unlock(&hwq->sq_lock); in ufshcd_send_command()
2183 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_send_command()
2184 if (hba->vops && hba->vops->setup_xfer_req) in ufshcd_send_command()
2185 hba->vops->setup_xfer_req(hba, lrbp->task_tag, in ufshcd_send_command()
2186 !!lrbp->cmd); in ufshcd_send_command()
2187 __set_bit(lrbp->task_tag, &hba->outstanding_reqs); in ufshcd_send_command()
2188 ufshcd_writel(hba, 1 << lrbp->task_tag, in ufshcd_send_command()
2190 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_send_command()
2195 * ufshcd_copy_sense_data - Copy sense data in case of check condition
2200 u8 *const sense_buffer = lrbp->cmd->sense_buffer; in ufshcd_copy_sense_data()
2204 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header.data_segment_length); in ufshcd_copy_sense_data()
2208 len = be16_to_cpu(lrbp->ucd_rsp_ptr->sr.sense_data_len); in ufshcd_copy_sense_data()
2211 memcpy(sense_buffer, lrbp->ucd_rsp_ptr->sr.sense_data, in ufshcd_copy_sense_data()
2217 * ufshcd_copy_query_response() - Copy the Query Response and the data
2219 * @hba: per adapter instance
2225 int ufshcd_copy_query_response(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) in ufshcd_copy_query_response() argument
2227 struct ufs_query_res *query_res = &hba->dev_cmd.query.response; in ufshcd_copy_query_response()
2229 memcpy(&query_res->upiu_res, &lrbp->ucd_rsp_ptr->qr, QUERY_OSF_SIZE); in ufshcd_copy_query_response()
2232 if (hba->dev_cmd.query.descriptor && in ufshcd_copy_query_response()
2233 lrbp->ucd_rsp_ptr->qr.opcode == UPIU_QUERY_OPCODE_READ_DESC) { in ufshcd_copy_query_response()
2234 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + in ufshcd_copy_query_response()
2240 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header in ufshcd_copy_query_response()
2243 hba->dev_cmd.query.request.upiu_req.length); in ufshcd_copy_query_response()
2245 memcpy(hba->dev_cmd.query.descriptor, descp, resp_len); in ufshcd_copy_query_response()
2247 dev_warn(hba->dev, in ufshcd_copy_query_response()
2250 return -EINVAL; in ufshcd_copy_query_response()
2258 * ufshcd_hba_capabilities - Read controller capabilities
2259 * @hba: per adapter instance
2263 static inline int ufshcd_hba_capabilities(struct ufs_hba *hba) in ufshcd_hba_capabilities() argument
2267 hba->capabilities = ufshcd_readl(hba, REG_CONTROLLER_CAPABILITIES); in ufshcd_hba_capabilities()
2270 hba->nutrs = (hba->capabilities & MASK_TRANSFER_REQUESTS_SLOTS) + 1; in ufshcd_hba_capabilities()
2271 hba->nutmrs = in ufshcd_hba_capabilities()
2272 ((hba->capabilities & MASK_TASK_MANAGEMENT_REQUEST_SLOTS) >> 16) + 1; in ufshcd_hba_capabilities()
2273 hba->reserved_slot = hba->nutrs - 1; in ufshcd_hba_capabilities()
2276 err = ufshcd_hba_init_crypto_capabilities(hba); in ufshcd_hba_capabilities()
2278 dev_err(hba->dev, "crypto setup failed\n"); in ufshcd_hba_capabilities()
2287 hba->mcq_sup = FIELD_GET(MASK_MCQ_SUPPORT, hba->capabilities); in ufshcd_hba_capabilities()
2292 hba->lsdb_sup = !FIELD_GET(MASK_LSDB_SUPPORT, hba->capabilities); in ufshcd_hba_capabilities()
2293 if (!hba->mcq_sup) in ufshcd_hba_capabilities()
2296 hba->mcq_capabilities = ufshcd_readl(hba, REG_MCQCAP); in ufshcd_hba_capabilities()
2297 hba->ext_iid_sup = FIELD_GET(MASK_EXT_IID_SUPPORT, in ufshcd_hba_capabilities()
2298 hba->mcq_capabilities); in ufshcd_hba_capabilities()
2304 * ufshcd_ready_for_uic_cmd - Check if controller is ready
2306 * @hba: per adapter instance
2310 static inline bool ufshcd_ready_for_uic_cmd(struct ufs_hba *hba) in ufshcd_ready_for_uic_cmd() argument
2314 500, UIC_CMD_TIMEOUT * 1000, false, hba, in ufshcd_ready_for_uic_cmd()
2320 * ufshcd_get_upmcrs - Get the power mode change request status
2321 * @hba: Pointer to adapter instance
2327 static inline u8 ufshcd_get_upmcrs(struct ufs_hba *hba) in ufshcd_get_upmcrs() argument
2329 return (ufshcd_readl(hba, REG_CONTROLLER_STATUS) >> 8) & 0x7; in ufshcd_get_upmcrs()
2333 * ufshcd_dispatch_uic_cmd - Dispatch an UIC command to the Unipro layer
2334 * @hba: per adapter instance
2338 ufshcd_dispatch_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) in ufshcd_dispatch_uic_cmd() argument
2340 lockdep_assert_held(&hba->uic_cmd_mutex); in ufshcd_dispatch_uic_cmd()
2342 WARN_ON(hba->active_uic_cmd); in ufshcd_dispatch_uic_cmd()
2344 hba->active_uic_cmd = uic_cmd; in ufshcd_dispatch_uic_cmd()
2347 ufshcd_writel(hba, uic_cmd->argument1, REG_UIC_COMMAND_ARG_1); in ufshcd_dispatch_uic_cmd()
2348 ufshcd_writel(hba, uic_cmd->argument2, REG_UIC_COMMAND_ARG_2); in ufshcd_dispatch_uic_cmd()
2349 ufshcd_writel(hba, uic_cmd->argument3, REG_UIC_COMMAND_ARG_3); in ufshcd_dispatch_uic_cmd()
2351 ufshcd_add_uic_command_trace(hba, uic_cmd, UFS_CMD_SEND); in ufshcd_dispatch_uic_cmd()
2354 ufshcd_writel(hba, uic_cmd->command & COMMAND_OPCODE_MASK, in ufshcd_dispatch_uic_cmd()
2359 * ufshcd_wait_for_uic_cmd - Wait for completion of an UIC command
2360 * @hba: per adapter instance
2366 ufshcd_wait_for_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) in ufshcd_wait_for_uic_cmd() argument
2371 lockdep_assert_held(&hba->uic_cmd_mutex); in ufshcd_wait_for_uic_cmd()
2373 if (wait_for_completion_timeout(&uic_cmd->done, in ufshcd_wait_for_uic_cmd()
2375 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; in ufshcd_wait_for_uic_cmd()
2377 ret = -ETIMEDOUT; in ufshcd_wait_for_uic_cmd()
2378 dev_err(hba->dev, in ufshcd_wait_for_uic_cmd()
2380 uic_cmd->command, uic_cmd->argument3); in ufshcd_wait_for_uic_cmd()
2382 if (!uic_cmd->cmd_active) { in ufshcd_wait_for_uic_cmd()
2383 dev_err(hba->dev, "%s: UIC cmd has been completed, return the result\n", in ufshcd_wait_for_uic_cmd()
2385 ret = uic_cmd->argument2 & MASK_UIC_COMMAND_RESULT; in ufshcd_wait_for_uic_cmd()
2389 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_wait_for_uic_cmd()
2390 hba->active_uic_cmd = NULL; in ufshcd_wait_for_uic_cmd()
2391 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_wait_for_uic_cmd()
2397 * __ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2398 * @hba: per adapter instance
2404 __ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) in __ufshcd_send_uic_cmd() argument
2406 lockdep_assert_held(&hba->uic_cmd_mutex); in __ufshcd_send_uic_cmd()
2408 if (!ufshcd_ready_for_uic_cmd(hba)) { in __ufshcd_send_uic_cmd()
2409 dev_err(hba->dev, in __ufshcd_send_uic_cmd()
2411 return -EIO; in __ufshcd_send_uic_cmd()
2414 init_completion(&uic_cmd->done); in __ufshcd_send_uic_cmd()
2416 uic_cmd->cmd_active = 1; in __ufshcd_send_uic_cmd()
2417 ufshcd_dispatch_uic_cmd(hba, uic_cmd); in __ufshcd_send_uic_cmd()
2423 * ufshcd_send_uic_cmd - Send UIC commands and retrieve the result
2424 * @hba: per adapter instance
2429 int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) in ufshcd_send_uic_cmd() argument
2433 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD) in ufshcd_send_uic_cmd()
2436 ufshcd_hold(hba); in ufshcd_send_uic_cmd()
2437 mutex_lock(&hba->uic_cmd_mutex); in ufshcd_send_uic_cmd()
2438 ufshcd_add_delay_before_dme_cmd(hba); in ufshcd_send_uic_cmd()
2440 ret = __ufshcd_send_uic_cmd(hba, uic_cmd); in ufshcd_send_uic_cmd()
2442 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); in ufshcd_send_uic_cmd()
2444 mutex_unlock(&hba->uic_cmd_mutex); in ufshcd_send_uic_cmd()
2446 ufshcd_release(hba); in ufshcd_send_uic_cmd()
2451 * ufshcd_sgl_to_prdt - SG list to PRTD (Physical Region Description Table, 4DW format)
2452 * @hba: per-adapter instance
2457 static void ufshcd_sgl_to_prdt(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, int sg_entries, in ufshcd_sgl_to_prdt() argument
2466 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) in ufshcd_sgl_to_prdt()
2467 lrbp->utr_descriptor_ptr->prd_table_length = in ufshcd_sgl_to_prdt()
2468 cpu_to_le16(sg_entries * ufshcd_sg_entry_size(hba)); in ufshcd_sgl_to_prdt()
2470 lrbp->utr_descriptor_ptr->prd_table_length = cpu_to_le16(sg_entries); in ufshcd_sgl_to_prdt()
2472 prd = lrbp->ucd_prdt_ptr; in ufshcd_sgl_to_prdt()
2486 prd->size = cpu_to_le32(len - 1); in ufshcd_sgl_to_prdt()
2487 prd->addr = cpu_to_le64(sg->dma_address); in ufshcd_sgl_to_prdt()
2488 prd->reserved = 0; in ufshcd_sgl_to_prdt()
2489 prd = (void *)prd + ufshcd_sg_entry_size(hba); in ufshcd_sgl_to_prdt()
2492 lrbp->utr_descriptor_ptr->prd_table_length = 0; in ufshcd_sgl_to_prdt()
2497 * ufshcd_map_sg - Map scatter-gather list to prdt
2498 * @hba: per adapter instance
2501 * Return: 0 in case of success, non-zero value in case of failure.
2503 static int ufshcd_map_sg(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) in ufshcd_map_sg() argument
2505 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_map_sg()
2511 ufshcd_sgl_to_prdt(hba, lrbp, sg_segments, scsi_sglist(cmd)); in ufshcd_map_sg()
2517 * ufshcd_enable_intr - enable interrupts
2518 * @hba: per adapter instance
2521 static void ufshcd_enable_intr(struct ufs_hba *hba, u32 intrs) in ufshcd_enable_intr() argument
2523 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); in ufshcd_enable_intr()
2525 if (hba->ufs_version == ufshci_version(1, 0)) { in ufshcd_enable_intr()
2533 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); in ufshcd_enable_intr()
2537 * ufshcd_disable_intr - disable interrupts
2538 * @hba: per adapter instance
2541 static void ufshcd_disable_intr(struct ufs_hba *hba, u32 intrs) in ufshcd_disable_intr() argument
2543 u32 set = ufshcd_readl(hba, REG_INTERRUPT_ENABLE); in ufshcd_disable_intr()
2545 if (hba->ufs_version == ufshci_version(1, 0)) { in ufshcd_disable_intr()
2555 ufshcd_writel(hba, set, REG_INTERRUPT_ENABLE); in ufshcd_disable_intr()
2559 * ufshcd_prepare_req_desc_hdr - Fill UTP Transfer request descriptor header according to request
2569 struct utp_transfer_req_desc *req_desc = lrbp->utr_descriptor_ptr; in ufshcd_prepare_req_desc_hdr()
2570 struct request_desc_header *h = &req_desc->header; in ufshcd_prepare_req_desc_hdr()
2586 h->command_type = lrbp->command_type; in ufshcd_prepare_req_desc_hdr()
2587 h->data_direction = data_direction; in ufshcd_prepare_req_desc_hdr()
2588 h->ehs_length = ehs_length; in ufshcd_prepare_req_desc_hdr()
2590 if (lrbp->intr_cmd) in ufshcd_prepare_req_desc_hdr()
2591 h->interrupt = 1; in ufshcd_prepare_req_desc_hdr()
2601 h->ocs = OCS_INVALID_COMMAND_STATUS; in ufshcd_prepare_req_desc_hdr()
2603 req_desc->prd_table_length = 0; in ufshcd_prepare_req_desc_hdr()
2607 * ufshcd_prepare_utp_scsi_cmd_upiu() - fills the utp_transfer_req_desc,
2615 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_prepare_utp_scsi_cmd_upiu()
2616 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; in ufshcd_prepare_utp_scsi_cmd_upiu()
2619 ucd_req_ptr->header = (struct utp_upiu_header){ in ufshcd_prepare_utp_scsi_cmd_upiu()
2622 .lun = lrbp->lun, in ufshcd_prepare_utp_scsi_cmd_upiu()
2623 .task_tag = lrbp->task_tag, in ufshcd_prepare_utp_scsi_cmd_upiu()
2627 ucd_req_ptr->sc.exp_data_transfer_len = cpu_to_be32(cmd->sdb.length); in ufshcd_prepare_utp_scsi_cmd_upiu()
2629 cdb_len = min_t(unsigned short, cmd->cmd_len, UFS_CDB_SIZE); in ufshcd_prepare_utp_scsi_cmd_upiu()
2630 memset(ucd_req_ptr->sc.cdb, 0, UFS_CDB_SIZE); in ufshcd_prepare_utp_scsi_cmd_upiu()
2631 memcpy(ucd_req_ptr->sc.cdb, cmd->cmnd, cdb_len); in ufshcd_prepare_utp_scsi_cmd_upiu()
2633 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_prepare_utp_scsi_cmd_upiu()
2637 * ufshcd_prepare_utp_query_req_upiu() - fill the utp_transfer_req_desc for query request
2638 * @hba: UFS hba
2642 static void ufshcd_prepare_utp_query_req_upiu(struct ufs_hba *hba, in ufshcd_prepare_utp_query_req_upiu() argument
2645 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; in ufshcd_prepare_utp_query_req_upiu()
2646 struct ufs_query *query = &hba->dev_cmd.query; in ufshcd_prepare_utp_query_req_upiu()
2647 u16 len = be16_to_cpu(query->request.upiu_req.length); in ufshcd_prepare_utp_query_req_upiu()
2650 ucd_req_ptr->header = (struct utp_upiu_header){ in ufshcd_prepare_utp_query_req_upiu()
2653 .lun = lrbp->lun, in ufshcd_prepare_utp_query_req_upiu()
2654 .task_tag = lrbp->task_tag, in ufshcd_prepare_utp_query_req_upiu()
2655 .query_function = query->request.query_func, in ufshcd_prepare_utp_query_req_upiu()
2658 query->request.upiu_req.opcode == in ufshcd_prepare_utp_query_req_upiu()
2665 memcpy(&ucd_req_ptr->qr, &query->request.upiu_req, in ufshcd_prepare_utp_query_req_upiu()
2669 if (query->request.upiu_req.opcode == UPIU_QUERY_OPCODE_WRITE_DESC) in ufshcd_prepare_utp_query_req_upiu()
2670 memcpy(ucd_req_ptr + 1, query->descriptor, len); in ufshcd_prepare_utp_query_req_upiu()
2672 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_prepare_utp_query_req_upiu()
2677 struct utp_upiu_req *ucd_req_ptr = lrbp->ucd_req_ptr; in ufshcd_prepare_utp_nop_upiu()
2681 ucd_req_ptr->header = (struct utp_upiu_header){ in ufshcd_prepare_utp_nop_upiu()
2683 .task_tag = lrbp->task_tag, in ufshcd_prepare_utp_nop_upiu()
2686 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_prepare_utp_nop_upiu()
2690 * ufshcd_compose_devman_upiu - UFS Protocol Information Unit(UPIU)
2692 * @hba: per adapter instance
2697 static int ufshcd_compose_devman_upiu(struct ufs_hba *hba, in ufshcd_compose_devman_upiu() argument
2703 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_compose_devman_upiu()
2704 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; in ufshcd_compose_devman_upiu()
2706 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_compose_devman_upiu()
2709 if (hba->dev_cmd.type == DEV_CMD_TYPE_QUERY) in ufshcd_compose_devman_upiu()
2710 ufshcd_prepare_utp_query_req_upiu(hba, lrbp, upiu_flags); in ufshcd_compose_devman_upiu()
2711 else if (hba->dev_cmd.type == DEV_CMD_TYPE_NOP) in ufshcd_compose_devman_upiu()
2714 ret = -EINVAL; in ufshcd_compose_devman_upiu()
2720 * ufshcd_comp_scsi_upiu - UFS Protocol Information Unit(UPIU)
2722 * @hba: per adapter instance
2727 static int ufshcd_comp_scsi_upiu(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) in ufshcd_comp_scsi_upiu() argument
2732 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_comp_scsi_upiu()
2733 lrbp->command_type = UTP_CMD_TYPE_SCSI; in ufshcd_comp_scsi_upiu()
2735 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_comp_scsi_upiu()
2737 if (likely(lrbp->cmd)) { in ufshcd_comp_scsi_upiu()
2738 ufshcd_prepare_req_desc_hdr(lrbp, &upiu_flags, lrbp->cmd->sc_data_direction, 0); in ufshcd_comp_scsi_upiu()
2741 ret = -EINVAL; in ufshcd_comp_scsi_upiu()
2748 * ufshcd_upiu_wlun_to_scsi_wlun - maps UPIU W-LUN id to SCSI W-LUN ID
2749 * @upiu_wlun_id: UPIU W-LUN id
2751 * Return: SCSI W-LUN id.
2760 return sdev->lun == in is_device_wlun()
2770 struct ufs_hba *hba = shost_priv(shost); in ufshcd_map_queues() local
2773 if (!is_mcq_supported(hba)) { in ufshcd_map_queues()
2774 hba->nr_queues[HCTX_TYPE_DEFAULT] = 1; in ufshcd_map_queues()
2775 hba->nr_queues[HCTX_TYPE_READ] = 0; in ufshcd_map_queues()
2776 hba->nr_queues[HCTX_TYPE_POLL] = 1; in ufshcd_map_queues()
2777 hba->nr_hw_queues = 1; in ufshcd_map_queues()
2780 for (i = 0; i < shost->nr_maps; i++) { in ufshcd_map_queues()
2781 struct blk_mq_queue_map *map = &shost->tag_set.map[i]; in ufshcd_map_queues()
2783 map->nr_queues = hba->nr_queues[i]; in ufshcd_map_queues()
2784 if (!map->nr_queues) in ufshcd_map_queues()
2786 map->queue_offset = queue_offset; in ufshcd_map_queues()
2787 if (i == HCTX_TYPE_POLL && !is_mcq_supported(hba)) in ufshcd_map_queues()
2788 map->queue_offset = 0; in ufshcd_map_queues()
2791 queue_offset += map->nr_queues; in ufshcd_map_queues()
2795 static void ufshcd_init_lrb(struct ufs_hba *hba, struct ufshcd_lrb *lrb, int i) in ufshcd_init_lrb() argument
2797 struct utp_transfer_cmd_desc *cmd_descp = (void *)hba->ucdl_base_addr + in ufshcd_init_lrb()
2798 i * ufshcd_get_ucd_size(hba); in ufshcd_init_lrb()
2799 struct utp_transfer_req_desc *utrdlp = hba->utrdl_base_addr; in ufshcd_init_lrb()
2800 dma_addr_t cmd_desc_element_addr = hba->ucdl_dma_addr + in ufshcd_init_lrb()
2801 i * ufshcd_get_ucd_size(hba); in ufshcd_init_lrb()
2805 lrb->utr_descriptor_ptr = utrdlp + i; in ufshcd_init_lrb()
2806 lrb->utrd_dma_addr = hba->utrdl_dma_addr + in ufshcd_init_lrb()
2808 lrb->ucd_req_ptr = (struct utp_upiu_req *)cmd_descp->command_upiu; in ufshcd_init_lrb()
2809 lrb->ucd_req_dma_addr = cmd_desc_element_addr; in ufshcd_init_lrb()
2810 lrb->ucd_rsp_ptr = (struct utp_upiu_rsp *)cmd_descp->response_upiu; in ufshcd_init_lrb()
2811 lrb->ucd_rsp_dma_addr = cmd_desc_element_addr + response_offset; in ufshcd_init_lrb()
2812 lrb->ucd_prdt_ptr = (struct ufshcd_sg_entry *)cmd_descp->prd_table; in ufshcd_init_lrb()
2813 lrb->ucd_prdt_dma_addr = cmd_desc_element_addr + prdt_offset; in ufshcd_init_lrb()
2817 * ufshcd_queuecommand - main entry point for SCSI requests
2821 * Return: 0 for success, non-zero in case of failure.
2825 struct ufs_hba *hba = shost_priv(host); in ufshcd_queuecommand() local
2826 int tag = scsi_cmd_to_rq(cmd)->tag; in ufshcd_queuecommand()
2831 WARN_ONCE(tag < 0 || tag >= hba->nutrs, "Invalid tag %d\n", tag); in ufshcd_queuecommand()
2833 switch (hba->ufshcd_state) { in ufshcd_queuecommand()
2838 * SCSI error handler can call ->queuecommand() while UFS error in ufshcd_queuecommand()
2844 if (ufshcd_eh_in_progress(hba)) { in ufshcd_queuecommand()
2852 * stage. If a scsi cmd, e.g. the SSU cmd, is sent from hba's in ufshcd_queuecommand()
2860 if (hba->pm_op_in_progress) { in ufshcd_queuecommand()
2861 hba->force_reset = true; in ufshcd_queuecommand()
2876 hba->req_abort_count = 0; in ufshcd_queuecommand()
2878 ufshcd_hold(hba); in ufshcd_queuecommand()
2880 lrbp = &hba->lrb[tag]; in ufshcd_queuecommand()
2881 lrbp->cmd = cmd; in ufshcd_queuecommand()
2882 lrbp->task_tag = tag; in ufshcd_queuecommand()
2883 lrbp->lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); in ufshcd_queuecommand()
2884 lrbp->intr_cmd = !ufshcd_is_intr_aggr_allowed(hba); in ufshcd_queuecommand()
2888 lrbp->req_abort_skip = false; in ufshcd_queuecommand()
2890 ufshcd_comp_scsi_upiu(hba, lrbp); in ufshcd_queuecommand()
2892 err = ufshcd_map_sg(hba, lrbp); in ufshcd_queuecommand()
2894 ufshcd_release(hba); in ufshcd_queuecommand()
2898 if (is_mcq_enabled(hba)) in ufshcd_queuecommand()
2899 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(cmd)); in ufshcd_queuecommand()
2901 ufshcd_send_command(hba, tag, hwq); in ufshcd_queuecommand()
2907 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_queuecommand()
2908 ufshcd_schedule_eh_work(hba); in ufshcd_queuecommand()
2909 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_queuecommand()
2915 static int ufshcd_compose_dev_cmd(struct ufs_hba *hba, in ufshcd_compose_dev_cmd() argument
2918 lrbp->cmd = NULL; in ufshcd_compose_dev_cmd()
2919 lrbp->task_tag = tag; in ufshcd_compose_dev_cmd()
2920 lrbp->lun = 0; /* device management cmd is not specific to any LUN */ in ufshcd_compose_dev_cmd()
2921 lrbp->intr_cmd = true; /* No interrupt aggregation */ in ufshcd_compose_dev_cmd()
2923 hba->dev_cmd.type = cmd_type; in ufshcd_compose_dev_cmd()
2925 return ufshcd_compose_devman_upiu(hba, lrbp); in ufshcd_compose_dev_cmd()
2951 * @hba: per adapter instance
2954 static int ufshcd_clear_cmd(struct ufs_hba *hba, u32 task_tag) in ufshcd_clear_cmd() argument
2960 if (is_mcq_enabled(hba)) { in ufshcd_clear_cmd()
2965 err = ufshcd_mcq_sq_cleanup(hba, task_tag); in ufshcd_clear_cmd()
2967 dev_err(hba->dev, "%s: failed tag=%d. err=%d\n", in ufshcd_clear_cmd()
2977 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clear_cmd()
2978 ufshcd_utrl_clear(hba, mask); in ufshcd_clear_cmd()
2979 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clear_cmd()
2982 * wait for h/w to clear corresponding bit in door-bell. in ufshcd_clear_cmd()
2985 return ufshcd_wait_for_register(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL, in ufshcd_clear_cmd()
2990 * ufshcd_dev_cmd_completion() - handles device management command responses
2991 * @hba: per adapter instance
2997 ufshcd_dev_cmd_completion(struct ufs_hba *hba, struct ufshcd_lrb *lrbp) in ufshcd_dev_cmd_completion() argument
3002 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); in ufshcd_dev_cmd_completion()
3003 resp = ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr); in ufshcd_dev_cmd_completion()
3007 if (hba->dev_cmd.type != DEV_CMD_TYPE_NOP) { in ufshcd_dev_cmd_completion()
3008 err = -EINVAL; in ufshcd_dev_cmd_completion()
3009 dev_err(hba->dev, "%s: unexpected response %x\n", in ufshcd_dev_cmd_completion()
3014 u8 response = lrbp->ucd_rsp_ptr->header.response; in ufshcd_dev_cmd_completion()
3017 err = ufshcd_copy_query_response(hba, lrbp); in ufshcd_dev_cmd_completion()
3022 err = -EPERM; in ufshcd_dev_cmd_completion()
3023 dev_err(hba->dev, "%s: Reject UPIU not fully implemented\n", in ufshcd_dev_cmd_completion()
3027 if (hba->dev_cmd.type != DEV_CMD_TYPE_RPMB) { in ufshcd_dev_cmd_completion()
3028 err = -EINVAL; in ufshcd_dev_cmd_completion()
3029 dev_err(hba->dev, "%s: unexpected response %x\n", __func__, resp); in ufshcd_dev_cmd_completion()
3033 err = -EINVAL; in ufshcd_dev_cmd_completion()
3034 dev_err(hba->dev, "%s: Invalid device management cmd response: %x\n", in ufshcd_dev_cmd_completion()
3042 static int ufshcd_wait_for_dev_cmd(struct ufs_hba *hba, in ufshcd_wait_for_dev_cmd() argument
3051 time_left = wait_for_completion_timeout(hba->dev_cmd.complete, in ufshcd_wait_for_dev_cmd()
3060 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3063 err = ufshcd_dev_cmd_completion(hba, lrbp); in ufshcd_wait_for_dev_cmd()
3065 err = -ETIMEDOUT; in ufshcd_wait_for_dev_cmd()
3066 dev_dbg(hba->dev, "%s: dev_cmd request timedout, tag %d\n", in ufshcd_wait_for_dev_cmd()
3067 __func__, lrbp->task_tag); in ufshcd_wait_for_dev_cmd()
3070 if (is_mcq_enabled(hba)) { in ufshcd_wait_for_dev_cmd()
3072 if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) in ufshcd_wait_for_dev_cmd()
3073 err = -EAGAIN; in ufshcd_wait_for_dev_cmd()
3074 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3079 if (ufshcd_clear_cmd(hba, lrbp->task_tag) == 0) { in ufshcd_wait_for_dev_cmd()
3081 err = -EAGAIN; in ufshcd_wait_for_dev_cmd()
3087 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3088 pending = test_bit(lrbp->task_tag, in ufshcd_wait_for_dev_cmd()
3089 &hba->outstanding_reqs); in ufshcd_wait_for_dev_cmd()
3091 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3092 __clear_bit(lrbp->task_tag, in ufshcd_wait_for_dev_cmd()
3093 &hba->outstanding_reqs); in ufshcd_wait_for_dev_cmd()
3095 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3106 dev_err(hba->dev, "%s: failed to clear tag %d\n", in ufshcd_wait_for_dev_cmd()
3107 __func__, lrbp->task_tag); in ufshcd_wait_for_dev_cmd()
3109 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3110 pending = test_bit(lrbp->task_tag, in ufshcd_wait_for_dev_cmd()
3111 &hba->outstanding_reqs); in ufshcd_wait_for_dev_cmd()
3113 hba->dev_cmd.complete = NULL; in ufshcd_wait_for_dev_cmd()
3114 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_wait_for_dev_cmd()
3131 * ufshcd_exec_dev_cmd - API for sending device management requests
3132 * @hba: UFS hba
3139 * it is expected you hold the hba->dev_cmd.lock mutex.
3141 static int ufshcd_exec_dev_cmd(struct ufs_hba *hba, in ufshcd_exec_dev_cmd() argument
3145 const u32 tag = hba->reserved_slot; in ufshcd_exec_dev_cmd()
3149 /* Protects use of hba->reserved_slot. */ in ufshcd_exec_dev_cmd()
3150 lockdep_assert_held(&hba->dev_cmd.lock); in ufshcd_exec_dev_cmd()
3152 down_read(&hba->clk_scaling_lock); in ufshcd_exec_dev_cmd()
3154 lrbp = &hba->lrb[tag]; in ufshcd_exec_dev_cmd()
3155 lrbp->cmd = NULL; in ufshcd_exec_dev_cmd()
3156 err = ufshcd_compose_dev_cmd(hba, lrbp, cmd_type, tag); in ufshcd_exec_dev_cmd()
3160 hba->dev_cmd.complete = &wait; in ufshcd_exec_dev_cmd()
3162 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); in ufshcd_exec_dev_cmd()
3164 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); in ufshcd_exec_dev_cmd()
3165 err = ufshcd_wait_for_dev_cmd(hba, lrbp, timeout); in ufshcd_exec_dev_cmd()
3166 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP, in ufshcd_exec_dev_cmd()
3167 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); in ufshcd_exec_dev_cmd()
3170 up_read(&hba->clk_scaling_lock); in ufshcd_exec_dev_cmd()
3175 * ufshcd_init_query() - init the query response and request parameters
3176 * @hba: per-adapter instance
3184 static inline void ufshcd_init_query(struct ufs_hba *hba, in ufshcd_init_query() argument
3188 *request = &hba->dev_cmd.query.request; in ufshcd_init_query()
3189 *response = &hba->dev_cmd.query.response; in ufshcd_init_query()
3192 (*request)->upiu_req.opcode = opcode; in ufshcd_init_query()
3193 (*request)->upiu_req.idn = idn; in ufshcd_init_query()
3194 (*request)->upiu_req.index = index; in ufshcd_init_query()
3195 (*request)->upiu_req.selector = selector; in ufshcd_init_query()
3198 static int ufshcd_query_flag_retry(struct ufs_hba *hba, in ufshcd_query_flag_retry() argument
3205 ret = ufshcd_query_flag(hba, opcode, idn, index, flag_res); in ufshcd_query_flag_retry()
3207 dev_dbg(hba->dev, in ufshcd_query_flag_retry()
3215 dev_err(hba->dev, in ufshcd_query_flag_retry()
3222 * ufshcd_query_flag() - API function for sending flag query requests
3223 * @hba: per-adapter instance
3229 * Return: 0 for success, non-zero in case of failure.
3231 int ufshcd_query_flag(struct ufs_hba *hba, enum query_opcode opcode, in ufshcd_query_flag() argument
3239 BUG_ON(!hba); in ufshcd_query_flag()
3241 ufshcd_hold(hba); in ufshcd_query_flag()
3242 mutex_lock(&hba->dev_cmd.lock); in ufshcd_query_flag()
3243 ufshcd_init_query(hba, &request, &response, opcode, idn, index, in ufshcd_query_flag()
3250 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in ufshcd_query_flag()
3253 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; in ufshcd_query_flag()
3256 dev_err(hba->dev, "%s: Invalid argument for read request\n", in ufshcd_query_flag()
3258 err = -EINVAL; in ufshcd_query_flag()
3263 dev_err(hba->dev, in ufshcd_query_flag()
3266 err = -EINVAL; in ufshcd_query_flag()
3270 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, timeout); in ufshcd_query_flag()
3273 dev_err(hba->dev, in ufshcd_query_flag()
3280 *flag_res = (be32_to_cpu(response->upiu_res.value) & in ufshcd_query_flag()
3284 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_query_flag()
3285 ufshcd_release(hba); in ufshcd_query_flag()
3290 * ufshcd_query_attr - API function for sending attribute requests
3291 * @hba: per-adapter instance
3298 * Return: 0 for success, non-zero in case of failure.
3300 int ufshcd_query_attr(struct ufs_hba *hba, enum query_opcode opcode, in ufshcd_query_attr() argument
3307 BUG_ON(!hba); in ufshcd_query_attr()
3310 dev_err(hba->dev, "%s: attribute value required for opcode 0x%x\n", in ufshcd_query_attr()
3312 return -EINVAL; in ufshcd_query_attr()
3315 ufshcd_hold(hba); in ufshcd_query_attr()
3317 mutex_lock(&hba->dev_cmd.lock); in ufshcd_query_attr()
3318 ufshcd_init_query(hba, &request, &response, opcode, idn, index, in ufshcd_query_attr()
3323 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in ufshcd_query_attr()
3324 request->upiu_req.value = cpu_to_be32(*attr_val); in ufshcd_query_attr()
3327 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; in ufshcd_query_attr()
3330 dev_err(hba->dev, "%s: Expected query attr opcode but got = 0x%.2x\n", in ufshcd_query_attr()
3332 err = -EINVAL; in ufshcd_query_attr()
3336 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); in ufshcd_query_attr()
3339 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", in ufshcd_query_attr()
3344 *attr_val = be32_to_cpu(response->upiu_res.value); in ufshcd_query_attr()
3347 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_query_attr()
3348 ufshcd_release(hba); in ufshcd_query_attr()
3353 * ufshcd_query_attr_retry() - API function for sending query
3355 * @hba: per-adapter instance
3363 * Return: 0 for success, non-zero in case of failure.
3365 int ufshcd_query_attr_retry(struct ufs_hba *hba, in ufshcd_query_attr_retry() argument
3372 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { in ufshcd_query_attr_retry()
3373 ret = ufshcd_query_attr(hba, opcode, idn, index, in ufshcd_query_attr_retry()
3376 dev_dbg(hba->dev, "%s: failed with error %d, retries %d\n", in ufshcd_query_attr_retry()
3383 dev_err(hba->dev, in ufshcd_query_attr_retry()
3389 static int __ufshcd_query_descriptor(struct ufs_hba *hba, in __ufshcd_query_descriptor() argument
3397 BUG_ON(!hba); in __ufshcd_query_descriptor()
3400 dev_err(hba->dev, "%s: descriptor buffer required for opcode 0x%x\n", in __ufshcd_query_descriptor()
3402 return -EINVAL; in __ufshcd_query_descriptor()
3406 dev_err(hba->dev, "%s: descriptor buffer size (%d) is out of range\n", in __ufshcd_query_descriptor()
3408 return -EINVAL; in __ufshcd_query_descriptor()
3411 ufshcd_hold(hba); in __ufshcd_query_descriptor()
3413 mutex_lock(&hba->dev_cmd.lock); in __ufshcd_query_descriptor()
3414 ufshcd_init_query(hba, &request, &response, opcode, idn, index, in __ufshcd_query_descriptor()
3416 hba->dev_cmd.query.descriptor = desc_buf; in __ufshcd_query_descriptor()
3417 request->upiu_req.length = cpu_to_be16(*buf_len); in __ufshcd_query_descriptor()
3421 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in __ufshcd_query_descriptor()
3424 request->query_func = UPIU_QUERY_FUNC_STANDARD_READ_REQUEST; in __ufshcd_query_descriptor()
3427 dev_err(hba->dev, in __ufshcd_query_descriptor()
3430 err = -EINVAL; in __ufshcd_query_descriptor()
3434 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); in __ufshcd_query_descriptor()
3437 dev_err(hba->dev, "%s: opcode 0x%.2x for idn %d failed, index %d, err = %d\n", in __ufshcd_query_descriptor()
3442 *buf_len = be16_to_cpu(response->upiu_res.length); in __ufshcd_query_descriptor()
3445 hba->dev_cmd.query.descriptor = NULL; in __ufshcd_query_descriptor()
3446 mutex_unlock(&hba->dev_cmd.lock); in __ufshcd_query_descriptor()
3447 ufshcd_release(hba); in __ufshcd_query_descriptor()
3452 * ufshcd_query_descriptor_retry - API function for sending descriptor requests
3453 * @hba: per-adapter instance
3464 * Return: 0 for success, non-zero in case of failure.
3466 int ufshcd_query_descriptor_retry(struct ufs_hba *hba, in ufshcd_query_descriptor_retry() argument
3475 for (retries = QUERY_REQ_RETRIES; retries > 0; retries--) { in ufshcd_query_descriptor_retry()
3476 err = __ufshcd_query_descriptor(hba, opcode, idn, index, in ufshcd_query_descriptor_retry()
3478 if (!err || err == -EINVAL) in ufshcd_query_descriptor_retry()
3486 * ufshcd_read_desc_param - read the specified descriptor parameter
3487 * @hba: Pointer to adapter instance
3494 * Return: 0 in case of success, non-zero otherwise.
3496 int ufshcd_read_desc_param(struct ufs_hba *hba, in ufshcd_read_desc_param() argument
3510 return -EINVAL; in ufshcd_read_desc_param()
3516 return -ENOMEM; in ufshcd_read_desc_param()
3523 ret = ufshcd_query_descriptor_retry(hba, UPIU_QUERY_OPCODE_READ_DESC, in ufshcd_read_desc_param()
3527 …dev_err(hba->dev, "%s: Failed reading descriptor. desc_id %d, desc_index %d, param_offset %d, ret … in ufshcd_read_desc_param()
3536 dev_err(hba->dev, "%s: Invalid offset 0x%x in descriptor IDN 0x%x, length 0x%x\n", in ufshcd_read_desc_param()
3538 ret = -EINVAL; in ufshcd_read_desc_param()
3544 dev_err(hba->dev, "%s: invalid desc_id %d in descriptor header\n", in ufshcd_read_desc_param()
3546 ret = -EINVAL; in ufshcd_read_desc_param()
3553 ret = -EINVAL; in ufshcd_read_desc_param()
3556 min_t(u32, param_size, buff_len - param_offset)); in ufshcd_read_desc_param()
3565 * struct uc_string_id - unicode string
3577 /* replace non-printable or non-ASCII characters with spaces */
3584 * ufshcd_read_string_desc - read string descriptor
3585 * @hba: pointer to adapter instance
3594 * * -ENOMEM: on allocation failure
3595 * * -EINVAL: on a wrong parameter
3597 int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index, in ufshcd_read_string_desc() argument
3605 return -EINVAL; in ufshcd_read_string_desc()
3609 return -ENOMEM; in ufshcd_read_string_desc()
3611 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_STRING, desc_index, 0, in ufshcd_read_string_desc()
3614 dev_err(hba->dev, "Reading String Desc failed after %d retries. err = %d\n", in ufshcd_read_string_desc()
3620 if (uc_str->len <= QUERY_DESC_HDR_SIZE) { in ufshcd_read_string_desc()
3621 dev_dbg(hba->dev, "String Desc is of zero length\n"); in ufshcd_read_string_desc()
3631 ascii_len = (uc_str->len - QUERY_DESC_HDR_SIZE) / 2 + 1; in ufshcd_read_string_desc()
3634 ret = -ENOMEM; in ufshcd_read_string_desc()
3640 * we need to convert to utf-8 so it can be displayed in ufshcd_read_string_desc()
3642 ret = utf16s_to_utf8s(uc_str->uc, in ufshcd_read_string_desc()
3643 uc_str->len - QUERY_DESC_HDR_SIZE, in ufshcd_read_string_desc()
3644 UTF16_BIG_ENDIAN, str, ascii_len - 1); in ufshcd_read_string_desc()
3646 /* replace non-printable or non-ASCII characters with spaces */ in ufshcd_read_string_desc()
3653 str = kmemdup(uc_str, uc_str->len, GFP_KERNEL); in ufshcd_read_string_desc()
3655 ret = -ENOMEM; in ufshcd_read_string_desc()
3658 ret = uc_str->len; in ufshcd_read_string_desc()
3667 * ufshcd_read_unit_desc_param - read the specified unit descriptor parameter
3668 * @hba: Pointer to adapter instance
3674 * Return: 0 in case of success, non-zero otherwise.
3676 static inline int ufshcd_read_unit_desc_param(struct ufs_hba *hba, in ufshcd_read_unit_desc_param() argument
3686 if (!ufs_is_valid_unit_desc_lun(&hba->dev_info, lun)) in ufshcd_read_unit_desc_param()
3687 return -EOPNOTSUPP; in ufshcd_read_unit_desc_param()
3689 return ufshcd_read_desc_param(hba, QUERY_DESC_IDN_UNIT, lun, in ufshcd_read_unit_desc_param()
3693 static int ufshcd_get_ref_clk_gating_wait(struct ufs_hba *hba) in ufshcd_get_ref_clk_gating_wait() argument
3698 if (hba->dev_info.wspecversion >= 0x300) { in ufshcd_get_ref_clk_gating_wait()
3699 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_get_ref_clk_gating_wait()
3703 dev_err(hba->dev, "Failed reading bRefClkGatingWait. err = %d, use default %uus\n", in ufshcd_get_ref_clk_gating_wait()
3708 dev_err(hba->dev, "Undefined ref clk gating wait time, use default %uus\n", in ufshcd_get_ref_clk_gating_wait()
3712 hba->dev_info.clk_gating_wait_us = gating_wait; in ufshcd_get_ref_clk_gating_wait()
3719 * ufshcd_memory_alloc - allocate memory for host memory space data structures
3720 * @hba: per adapter instance
3729 * Return: 0 for success, non-zero in case of failure.
3731 static int ufshcd_memory_alloc(struct ufs_hba *hba) in ufshcd_memory_alloc() argument
3736 ucdl_size = ufshcd_get_ucd_size(hba) * hba->nutrs; in ufshcd_memory_alloc()
3737 hba->ucdl_base_addr = dmam_alloc_coherent(hba->dev, in ufshcd_memory_alloc()
3739 &hba->ucdl_dma_addr, in ufshcd_memory_alloc()
3745 if (!hba->ucdl_base_addr || in ufshcd_memory_alloc()
3746 WARN_ON(hba->ucdl_dma_addr & (128 - 1))) { in ufshcd_memory_alloc()
3747 dev_err(hba->dev, in ufshcd_memory_alloc()
3756 utrdl_size = (sizeof(struct utp_transfer_req_desc) * hba->nutrs); in ufshcd_memory_alloc()
3757 hba->utrdl_base_addr = dmam_alloc_coherent(hba->dev, in ufshcd_memory_alloc()
3759 &hba->utrdl_dma_addr, in ufshcd_memory_alloc()
3761 if (!hba->utrdl_base_addr || in ufshcd_memory_alloc()
3762 WARN_ON(hba->utrdl_dma_addr & (SZ_1K - 1))) { in ufshcd_memory_alloc()
3763 dev_err(hba->dev, in ufshcd_memory_alloc()
3774 if (hba->utmrdl_base_addr) in ufshcd_memory_alloc()
3780 utmrdl_size = sizeof(struct utp_task_req_desc) * hba->nutmrs; in ufshcd_memory_alloc()
3781 hba->utmrdl_base_addr = dmam_alloc_coherent(hba->dev, in ufshcd_memory_alloc()
3783 &hba->utmrdl_dma_addr, in ufshcd_memory_alloc()
3785 if (!hba->utmrdl_base_addr || in ufshcd_memory_alloc()
3786 WARN_ON(hba->utmrdl_dma_addr & (SZ_1K - 1))) { in ufshcd_memory_alloc()
3787 dev_err(hba->dev, in ufshcd_memory_alloc()
3794 hba->lrb = devm_kcalloc(hba->dev, in ufshcd_memory_alloc()
3795 hba->nutrs, sizeof(struct ufshcd_lrb), in ufshcd_memory_alloc()
3797 if (!hba->lrb) { in ufshcd_memory_alloc()
3798 dev_err(hba->dev, "LRB Memory allocation failed\n"); in ufshcd_memory_alloc()
3803 return -ENOMEM; in ufshcd_memory_alloc()
3807 * ufshcd_host_memory_configure - configure local reference block with
3809 * @hba: per adapter instance
3819 static void ufshcd_host_memory_configure(struct ufs_hba *hba) in ufshcd_host_memory_configure() argument
3829 utrdlp = hba->utrdl_base_addr; in ufshcd_host_memory_configure()
3836 cmd_desc_size = ufshcd_get_ucd_size(hba); in ufshcd_host_memory_configure()
3837 cmd_desc_dma_addr = hba->ucdl_dma_addr; in ufshcd_host_memory_configure()
3839 for (i = 0; i < hba->nutrs; i++) { in ufshcd_host_memory_configure()
3847 if (hba->quirks & UFSHCD_QUIRK_PRDT_BYTE_GRAN) { in ufshcd_host_memory_configure()
3863 ufshcd_init_lrb(hba, &hba->lrb[i], i); in ufshcd_host_memory_configure()
3868 * ufshcd_dme_link_startup - Notify Unipro to perform link startup
3869 * @hba: per adapter instance
3876 * Return: 0 on success, non-zero value on failure.
3878 static int ufshcd_dme_link_startup(struct ufs_hba *hba) in ufshcd_dme_link_startup() argument
3885 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); in ufshcd_dme_link_startup()
3887 dev_dbg(hba->dev, in ufshcd_dme_link_startup()
3888 "dme-link-startup: error code %d\n", ret); in ufshcd_dme_link_startup()
3892 * ufshcd_dme_reset - UIC command for DME_RESET
3893 * @hba: per adapter instance
3898 * Return: 0 on success, non-zero value on failure.
3900 static int ufshcd_dme_reset(struct ufs_hba *hba) in ufshcd_dme_reset() argument
3907 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); in ufshcd_dme_reset()
3909 dev_err(hba->dev, in ufshcd_dme_reset()
3910 "dme-reset: error code %d\n", ret); in ufshcd_dme_reset()
3915 int ufshcd_dme_configure_adapt(struct ufs_hba *hba, in ufshcd_dme_configure_adapt() argument
3924 ret = ufshcd_dme_set(hba, in ufshcd_dme_configure_adapt()
3932 * ufshcd_dme_enable - UIC command for DME_ENABLE
3933 * @hba: per adapter instance
3937 * Return: 0 on success, non-zero value on failure.
3939 static int ufshcd_dme_enable(struct ufs_hba *hba) in ufshcd_dme_enable() argument
3946 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); in ufshcd_dme_enable()
3948 dev_err(hba->dev, in ufshcd_dme_enable()
3949 "dme-enable: error code %d\n", ret); in ufshcd_dme_enable()
3954 static inline void ufshcd_add_delay_before_dme_cmd(struct ufs_hba *hba) in ufshcd_add_delay_before_dme_cmd() argument
3959 if (!(hba->quirks & UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS)) in ufshcd_add_delay_before_dme_cmd()
3966 if (unlikely(!ktime_to_us(hba->last_dme_cmd_tstamp))) { in ufshcd_add_delay_before_dme_cmd()
3972 hba->last_dme_cmd_tstamp)); in ufshcd_add_delay_before_dme_cmd()
3976 MIN_DELAY_BEFORE_DME_CMDS_US - delta; in ufshcd_add_delay_before_dme_cmd()
3987 hba->last_dme_cmd_tstamp = ktime_get(); in ufshcd_add_delay_before_dme_cmd()
3991 * ufshcd_dme_set_attr - UIC command for DME_SET, DME_PEER_SET
3992 * @hba: per adapter instance
3998 * Return: 0 on success, non-zero value on failure.
4000 int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel, in ufshcd_dme_set_attr() argument
4005 "dme-set", in ufshcd_dme_set_attr()
4006 "dme-peer-set" in ufshcd_dme_set_attr()
4020 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); in ufshcd_dme_set_attr()
4022 dev_dbg(hba->dev, "%s: attr-id 0x%x val 0x%x error code %d\n", in ufshcd_dme_set_attr()
4024 } while (ret && peer && --retries); in ufshcd_dme_set_attr()
4027 dev_err(hba->dev, "%s: attr-id 0x%x val 0x%x failed %d retries\n", in ufshcd_dme_set_attr()
4029 UFS_UIC_COMMAND_RETRIES - retries); in ufshcd_dme_set_attr()
4036 * ufshcd_dme_get_attr - UIC command for DME_GET, DME_PEER_GET
4037 * @hba: per adapter instance
4042 * Return: 0 on success, non-zero value on failure.
4044 int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel, in ufshcd_dme_get_attr() argument
4049 "dme-get", in ufshcd_dme_get_attr()
4050 "dme-peer-get" in ufshcd_dme_get_attr()
4059 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE)) { in ufshcd_dme_get_attr()
4060 orig_pwr_info = hba->pwr_info; in ufshcd_dme_get_attr()
4075 ret = ufshcd_change_power_mode(hba, &temp_pwr_info); in ufshcd_dme_get_attr()
4087 ret = ufshcd_send_uic_cmd(hba, &uic_cmd); in ufshcd_dme_get_attr()
4089 dev_dbg(hba->dev, "%s: attr-id 0x%x error code %d\n", in ufshcd_dme_get_attr()
4091 } while (ret && peer && --retries); in ufshcd_dme_get_attr()
4094 dev_err(hba->dev, "%s: attr-id 0x%x failed %d retries\n", in ufshcd_dme_get_attr()
4096 UFS_UIC_COMMAND_RETRIES - retries); in ufshcd_dme_get_attr()
4101 if (peer && (hba->quirks & UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE) in ufshcd_dme_get_attr()
4103 ufshcd_change_power_mode(hba, &orig_pwr_info); in ufshcd_dme_get_attr()
4110 * ufshcd_uic_pwr_ctrl - executes UIC commands (which affects the link power
4113 * @hba: per adapter instance
4123 * Return: 0 on success, non-zero value on failure.
4125 static int ufshcd_uic_pwr_ctrl(struct ufs_hba *hba, struct uic_command *cmd) in ufshcd_uic_pwr_ctrl() argument
4133 mutex_lock(&hba->uic_cmd_mutex); in ufshcd_uic_pwr_ctrl()
4134 ufshcd_add_delay_before_dme_cmd(hba); in ufshcd_uic_pwr_ctrl()
4136 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4137 if (ufshcd_is_link_broken(hba)) { in ufshcd_uic_pwr_ctrl()
4138 ret = -ENOLINK; in ufshcd_uic_pwr_ctrl()
4141 hba->uic_async_done = &uic_async_done; in ufshcd_uic_pwr_ctrl()
4142 if (ufshcd_readl(hba, REG_INTERRUPT_ENABLE) & UIC_COMMAND_COMPL) { in ufshcd_uic_pwr_ctrl()
4143 ufshcd_disable_intr(hba, UIC_COMMAND_COMPL); in ufshcd_uic_pwr_ctrl()
4148 ufshcd_readl(hba, REG_INTERRUPT_ENABLE); in ufshcd_uic_pwr_ctrl()
4151 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4152 ret = __ufshcd_send_uic_cmd(hba, cmd); in ufshcd_uic_pwr_ctrl()
4154 dev_err(hba->dev, in ufshcd_uic_pwr_ctrl()
4156 cmd->command, cmd->argument3, ret); in ufshcd_uic_pwr_ctrl()
4160 if (!wait_for_completion_timeout(hba->uic_async_done, in ufshcd_uic_pwr_ctrl()
4162 dev_err(hba->dev, in ufshcd_uic_pwr_ctrl()
4164 cmd->command, cmd->argument3); in ufshcd_uic_pwr_ctrl()
4166 if (!cmd->cmd_active) { in ufshcd_uic_pwr_ctrl()
4167 dev_err(hba->dev, "%s: Power Mode Change operation has been completed, go check UPMCRS\n", in ufshcd_uic_pwr_ctrl()
4172 ret = -ETIMEDOUT; in ufshcd_uic_pwr_ctrl()
4177 status = ufshcd_get_upmcrs(hba); in ufshcd_uic_pwr_ctrl()
4179 dev_err(hba->dev, in ufshcd_uic_pwr_ctrl()
4181 cmd->command, status); in ufshcd_uic_pwr_ctrl()
4182 ret = (status != PWR_OK) ? status : -1; in ufshcd_uic_pwr_ctrl()
4186 ufshcd_print_host_state(hba); in ufshcd_uic_pwr_ctrl()
4187 ufshcd_print_pwr_info(hba); in ufshcd_uic_pwr_ctrl()
4188 ufshcd_print_evt_hist(hba); in ufshcd_uic_pwr_ctrl()
4191 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4192 hba->active_uic_cmd = NULL; in ufshcd_uic_pwr_ctrl()
4193 hba->uic_async_done = NULL; in ufshcd_uic_pwr_ctrl()
4195 ufshcd_enable_intr(hba, UIC_COMMAND_COMPL); in ufshcd_uic_pwr_ctrl()
4197 ufshcd_set_link_broken(hba); in ufshcd_uic_pwr_ctrl()
4198 ufshcd_schedule_eh_work(hba); in ufshcd_uic_pwr_ctrl()
4201 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_uic_pwr_ctrl()
4202 mutex_unlock(&hba->uic_cmd_mutex); in ufshcd_uic_pwr_ctrl()
4208 * ufshcd_send_bsg_uic_cmd - Send UIC commands requested via BSG layer and retrieve the result
4209 * @hba: per adapter instance
4214 int ufshcd_send_bsg_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd) in ufshcd_send_bsg_uic_cmd() argument
4218 if (hba->quirks & UFSHCD_QUIRK_BROKEN_UIC_CMD) in ufshcd_send_bsg_uic_cmd()
4221 ufshcd_hold(hba); in ufshcd_send_bsg_uic_cmd()
4223 if (uic_cmd->argument1 == UIC_ARG_MIB(PA_PWRMODE) && in ufshcd_send_bsg_uic_cmd()
4224 uic_cmd->command == UIC_CMD_DME_SET) { in ufshcd_send_bsg_uic_cmd()
4225 ret = ufshcd_uic_pwr_ctrl(hba, uic_cmd); in ufshcd_send_bsg_uic_cmd()
4229 mutex_lock(&hba->uic_cmd_mutex); in ufshcd_send_bsg_uic_cmd()
4230 ufshcd_add_delay_before_dme_cmd(hba); in ufshcd_send_bsg_uic_cmd()
4232 ret = __ufshcd_send_uic_cmd(hba, uic_cmd); in ufshcd_send_bsg_uic_cmd()
4234 ret = ufshcd_wait_for_uic_cmd(hba, uic_cmd); in ufshcd_send_bsg_uic_cmd()
4236 mutex_unlock(&hba->uic_cmd_mutex); in ufshcd_send_bsg_uic_cmd()
4239 ufshcd_release(hba); in ufshcd_send_bsg_uic_cmd()
4244 * ufshcd_uic_change_pwr_mode - Perform the UIC power mode chage
4246 * @hba: per adapter instance
4249 * Return: 0 on success, non-zero value on failure.
4251 int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode) in ufshcd_uic_change_pwr_mode() argument
4256 if (hba->quirks & UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP) { in ufshcd_uic_change_pwr_mode()
4257 ret = ufshcd_dme_set(hba, in ufshcd_uic_change_pwr_mode()
4260 dev_err(hba->dev, "%s: failed to enable PA_RXHSUNTERMCAP ret %d\n", in ufshcd_uic_change_pwr_mode()
4269 ufshcd_hold(hba); in ufshcd_uic_change_pwr_mode()
4270 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); in ufshcd_uic_change_pwr_mode()
4271 ufshcd_release(hba); in ufshcd_uic_change_pwr_mode()
4278 int ufshcd_link_recovery(struct ufs_hba *hba) in ufshcd_link_recovery() argument
4283 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_link_recovery()
4284 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_link_recovery()
4285 ufshcd_set_eh_in_progress(hba); in ufshcd_link_recovery()
4286 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_link_recovery()
4289 ufshcd_device_reset(hba); in ufshcd_link_recovery()
4291 ret = ufshcd_host_reset_and_restore(hba); in ufshcd_link_recovery()
4293 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_link_recovery()
4295 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_link_recovery()
4296 ufshcd_clear_eh_in_progress(hba); in ufshcd_link_recovery()
4297 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_link_recovery()
4300 dev_err(hba->dev, "%s: link recovery failed, err %d", in ufshcd_link_recovery()
4307 int ufshcd_uic_hibern8_enter(struct ufs_hba *hba) in ufshcd_uic_hibern8_enter() argument
4313 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, PRE_CHANGE); in ufshcd_uic_hibern8_enter()
4316 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); in ufshcd_uic_hibern8_enter()
4317 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "enter", in ufshcd_uic_hibern8_enter()
4321 dev_err(hba->dev, "%s: hibern8 enter failed. ret = %d\n", in ufshcd_uic_hibern8_enter()
4324 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_ENTER, in ufshcd_uic_hibern8_enter()
4331 int ufshcd_uic_hibern8_exit(struct ufs_hba *hba) in ufshcd_uic_hibern8_exit() argument
4337 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, PRE_CHANGE); in ufshcd_uic_hibern8_exit()
4340 ret = ufshcd_uic_pwr_ctrl(hba, &uic_cmd); in ufshcd_uic_hibern8_exit()
4341 trace_ufshcd_profile_hibern8(dev_name(hba->dev), "exit", in ufshcd_uic_hibern8_exit()
4345 dev_err(hba->dev, "%s: hibern8 exit failed. ret = %d\n", in ufshcd_uic_hibern8_exit()
4348 ufshcd_vops_hibern8_notify(hba, UIC_CMD_DME_HIBER_EXIT, in ufshcd_uic_hibern8_exit()
4350 hba->ufs_stats.last_hibern8_exit_tstamp = local_clock(); in ufshcd_uic_hibern8_exit()
4351 hba->ufs_stats.hibern8_exit_cnt++; in ufshcd_uic_hibern8_exit()
4358 void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit) in ufshcd_auto_hibern8_update() argument
4363 if (!ufshcd_is_auto_hibern8_supported(hba)) in ufshcd_auto_hibern8_update()
4366 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_auto_hibern8_update()
4367 if (hba->ahit != ahit) { in ufshcd_auto_hibern8_update()
4368 hba->ahit = ahit; in ufshcd_auto_hibern8_update()
4371 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_auto_hibern8_update()
4374 !pm_runtime_suspended(&hba->ufs_device_wlun->sdev_gendev)) { in ufshcd_auto_hibern8_update()
4375 ufshcd_rpm_get_sync(hba); in ufshcd_auto_hibern8_update()
4376 ufshcd_hold(hba); in ufshcd_auto_hibern8_update()
4377 ufshcd_auto_hibern8_enable(hba); in ufshcd_auto_hibern8_update()
4378 ufshcd_release(hba); in ufshcd_auto_hibern8_update()
4379 ufshcd_rpm_put_sync(hba); in ufshcd_auto_hibern8_update()
4384 void ufshcd_auto_hibern8_enable(struct ufs_hba *hba) in ufshcd_auto_hibern8_enable() argument
4386 if (!ufshcd_is_auto_hibern8_supported(hba)) in ufshcd_auto_hibern8_enable()
4389 ufshcd_writel(hba, hba->ahit, REG_AUTO_HIBERNATE_IDLE_TIMER); in ufshcd_auto_hibern8_enable()
4393 * ufshcd_init_pwr_info - setting the POR (power on reset)
4394 * values in hba power info
4395 * @hba: per-adapter instance
4397 static void ufshcd_init_pwr_info(struct ufs_hba *hba) in ufshcd_init_pwr_info() argument
4399 hba->pwr_info.gear_rx = UFS_PWM_G1; in ufshcd_init_pwr_info()
4400 hba->pwr_info.gear_tx = UFS_PWM_G1; in ufshcd_init_pwr_info()
4401 hba->pwr_info.lane_rx = UFS_LANE_1; in ufshcd_init_pwr_info()
4402 hba->pwr_info.lane_tx = UFS_LANE_1; in ufshcd_init_pwr_info()
4403 hba->pwr_info.pwr_rx = SLOWAUTO_MODE; in ufshcd_init_pwr_info()
4404 hba->pwr_info.pwr_tx = SLOWAUTO_MODE; in ufshcd_init_pwr_info()
4405 hba->pwr_info.hs_rate = 0; in ufshcd_init_pwr_info()
4409 * ufshcd_get_max_pwr_mode - reads the max power mode negotiated with device
4410 * @hba: per-adapter instance
4414 static int ufshcd_get_max_pwr_mode(struct ufs_hba *hba) in ufshcd_get_max_pwr_mode() argument
4416 struct ufs_pa_layer_attr *pwr_info = &hba->max_pwr_info.info; in ufshcd_get_max_pwr_mode()
4418 if (hba->max_pwr_info.is_valid) in ufshcd_get_max_pwr_mode()
4421 if (hba->quirks & UFSHCD_QUIRK_HIBERN_FASTAUTO) { in ufshcd_get_max_pwr_mode()
4422 pwr_info->pwr_tx = FASTAUTO_MODE; in ufshcd_get_max_pwr_mode()
4423 pwr_info->pwr_rx = FASTAUTO_MODE; in ufshcd_get_max_pwr_mode()
4425 pwr_info->pwr_tx = FAST_MODE; in ufshcd_get_max_pwr_mode()
4426 pwr_info->pwr_rx = FAST_MODE; in ufshcd_get_max_pwr_mode()
4428 pwr_info->hs_rate = PA_HS_MODE_B; in ufshcd_get_max_pwr_mode()
4431 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDRXDATALANES), in ufshcd_get_max_pwr_mode()
4432 &pwr_info->lane_rx); in ufshcd_get_max_pwr_mode()
4433 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), in ufshcd_get_max_pwr_mode()
4434 &pwr_info->lane_tx); in ufshcd_get_max_pwr_mode()
4436 if (!pwr_info->lane_rx || !pwr_info->lane_tx) { in ufshcd_get_max_pwr_mode()
4437 dev_err(hba->dev, "%s: invalid connected lanes value. rx=%d, tx=%d\n", in ufshcd_get_max_pwr_mode()
4439 pwr_info->lane_rx, in ufshcd_get_max_pwr_mode()
4440 pwr_info->lane_tx); in ufshcd_get_max_pwr_mode()
4441 return -EINVAL; in ufshcd_get_max_pwr_mode()
4449 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), &pwr_info->gear_rx); in ufshcd_get_max_pwr_mode()
4450 if (!pwr_info->gear_rx) { in ufshcd_get_max_pwr_mode()
4451 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), in ufshcd_get_max_pwr_mode()
4452 &pwr_info->gear_rx); in ufshcd_get_max_pwr_mode()
4453 if (!pwr_info->gear_rx) { in ufshcd_get_max_pwr_mode()
4454 dev_err(hba->dev, "%s: invalid max pwm rx gear read = %d\n", in ufshcd_get_max_pwr_mode()
4455 __func__, pwr_info->gear_rx); in ufshcd_get_max_pwr_mode()
4456 return -EINVAL; in ufshcd_get_max_pwr_mode()
4458 pwr_info->pwr_rx = SLOW_MODE; in ufshcd_get_max_pwr_mode()
4461 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXHSGEAR), in ufshcd_get_max_pwr_mode()
4462 &pwr_info->gear_tx); in ufshcd_get_max_pwr_mode()
4463 if (!pwr_info->gear_tx) { in ufshcd_get_max_pwr_mode()
4464 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_MAXRXPWMGEAR), in ufshcd_get_max_pwr_mode()
4465 &pwr_info->gear_tx); in ufshcd_get_max_pwr_mode()
4466 if (!pwr_info->gear_tx) { in ufshcd_get_max_pwr_mode()
4467 dev_err(hba->dev, "%s: invalid max pwm tx gear read = %d\n", in ufshcd_get_max_pwr_mode()
4468 __func__, pwr_info->gear_tx); in ufshcd_get_max_pwr_mode()
4469 return -EINVAL; in ufshcd_get_max_pwr_mode()
4471 pwr_info->pwr_tx = SLOW_MODE; in ufshcd_get_max_pwr_mode()
4474 hba->max_pwr_info.is_valid = true; in ufshcd_get_max_pwr_mode()
4478 static int ufshcd_change_power_mode(struct ufs_hba *hba, in ufshcd_change_power_mode() argument
4484 if (!hba->force_pmc && in ufshcd_change_power_mode()
4485 pwr_mode->gear_rx == hba->pwr_info.gear_rx && in ufshcd_change_power_mode()
4486 pwr_mode->gear_tx == hba->pwr_info.gear_tx && in ufshcd_change_power_mode()
4487 pwr_mode->lane_rx == hba->pwr_info.lane_rx && in ufshcd_change_power_mode()
4488 pwr_mode->lane_tx == hba->pwr_info.lane_tx && in ufshcd_change_power_mode()
4489 pwr_mode->pwr_rx == hba->pwr_info.pwr_rx && in ufshcd_change_power_mode()
4490 pwr_mode->pwr_tx == hba->pwr_info.pwr_tx && in ufshcd_change_power_mode()
4491 pwr_mode->hs_rate == hba->pwr_info.hs_rate) { in ufshcd_change_power_mode()
4492 dev_dbg(hba->dev, "%s: power already configured\n", __func__); in ufshcd_change_power_mode()
4498 * - PA_RXGEAR, PA_ACTIVERXDATALANES, PA_RXTERMINATION, in ufshcd_change_power_mode()
4499 * - PA_TXGEAR, PA_ACTIVETXDATALANES, PA_TXTERMINATION, in ufshcd_change_power_mode()
4500 * - PA_HSSERIES in ufshcd_change_power_mode()
4502 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXGEAR), pwr_mode->gear_rx); in ufshcd_change_power_mode()
4503 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVERXDATALANES), in ufshcd_change_power_mode()
4504 pwr_mode->lane_rx); in ufshcd_change_power_mode()
4505 if (pwr_mode->pwr_rx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4506 pwr_mode->pwr_rx == FAST_MODE) in ufshcd_change_power_mode()
4507 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), true); in ufshcd_change_power_mode()
4509 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_RXTERMINATION), false); in ufshcd_change_power_mode()
4511 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXGEAR), pwr_mode->gear_tx); in ufshcd_change_power_mode()
4512 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_ACTIVETXDATALANES), in ufshcd_change_power_mode()
4513 pwr_mode->lane_tx); in ufshcd_change_power_mode()
4514 if (pwr_mode->pwr_tx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4515 pwr_mode->pwr_tx == FAST_MODE) in ufshcd_change_power_mode()
4516 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), true); in ufshcd_change_power_mode()
4518 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TXTERMINATION), false); in ufshcd_change_power_mode()
4520 if (pwr_mode->pwr_rx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4521 pwr_mode->pwr_tx == FASTAUTO_MODE || in ufshcd_change_power_mode()
4522 pwr_mode->pwr_rx == FAST_MODE || in ufshcd_change_power_mode()
4523 pwr_mode->pwr_tx == FAST_MODE) in ufshcd_change_power_mode()
4524 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HSSERIES), in ufshcd_change_power_mode()
4525 pwr_mode->hs_rate); in ufshcd_change_power_mode()
4527 if (!(hba->quirks & UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING)) { in ufshcd_change_power_mode()
4528 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA0), in ufshcd_change_power_mode()
4530 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA1), in ufshcd_change_power_mode()
4532 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA2), in ufshcd_change_power_mode()
4534 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA3), in ufshcd_change_power_mode()
4536 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA4), in ufshcd_change_power_mode()
4538 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_PWRMODEUSERDATA5), in ufshcd_change_power_mode()
4541 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalFC0ProtectionTimeOutVal), in ufshcd_change_power_mode()
4543 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalTC0ReplayTimeOutVal), in ufshcd_change_power_mode()
4545 ufshcd_dme_set(hba, UIC_ARG_MIB(DME_LocalAFC0ReqTimeOutVal), in ufshcd_change_power_mode()
4549 ret = ufshcd_uic_change_pwr_mode(hba, pwr_mode->pwr_rx << 4 in ufshcd_change_power_mode()
4550 | pwr_mode->pwr_tx); in ufshcd_change_power_mode()
4553 dev_err(hba->dev, in ufshcd_change_power_mode()
4556 memcpy(&hba->pwr_info, pwr_mode, in ufshcd_change_power_mode()
4564 * ufshcd_config_pwr_mode - configure a new power mode
4565 * @hba: per-adapter instance
4570 int ufshcd_config_pwr_mode(struct ufs_hba *hba, in ufshcd_config_pwr_mode() argument
4576 ret = ufshcd_vops_pwr_change_notify(hba, PRE_CHANGE, in ufshcd_config_pwr_mode()
4582 ret = ufshcd_change_power_mode(hba, &final_params); in ufshcd_config_pwr_mode()
4585 ufshcd_vops_pwr_change_notify(hba, POST_CHANGE, NULL, in ufshcd_config_pwr_mode()
4593 * ufshcd_complete_dev_init() - checks device readiness
4594 * @hba: per-adapter instance
4600 static int ufshcd_complete_dev_init(struct ufs_hba *hba) in ufshcd_complete_dev_init() argument
4606 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, in ufshcd_complete_dev_init()
4609 dev_err(hba->dev, in ufshcd_complete_dev_init()
4618 err = ufshcd_query_flag(hba, UPIU_QUERY_OPCODE_READ_FLAG, in ufshcd_complete_dev_init()
4626 dev_err(hba->dev, in ufshcd_complete_dev_init()
4630 dev_err(hba->dev, in ufshcd_complete_dev_init()
4633 err = -EBUSY; in ufshcd_complete_dev_init()
4640 * ufshcd_make_hba_operational - Make UFS controller operational
4641 * @hba: per adapter instance
4647 * 4. Configure run-stop-registers
4649 * Return: 0 on success, non-zero value on failure.
4651 int ufshcd_make_hba_operational(struct ufs_hba *hba) in ufshcd_make_hba_operational() argument
4657 ufshcd_enable_intr(hba, UFSHCD_ENABLE_INTRS); in ufshcd_make_hba_operational()
4660 if (ufshcd_is_intr_aggr_allowed(hba)) in ufshcd_make_hba_operational()
4661 ufshcd_config_intr_aggr(hba, hba->nutrs - 1, INT_AGGR_DEF_TO); in ufshcd_make_hba_operational()
4663 ufshcd_disable_intr_aggr(hba); in ufshcd_make_hba_operational()
4666 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), in ufshcd_make_hba_operational()
4668 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), in ufshcd_make_hba_operational()
4670 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), in ufshcd_make_hba_operational()
4672 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), in ufshcd_make_hba_operational()
4684 reg = ufshcd_readl(hba, REG_CONTROLLER_STATUS); in ufshcd_make_hba_operational()
4686 ufshcd_enable_run_stop_reg(hba); in ufshcd_make_hba_operational()
4688 dev_err(hba->dev, in ufshcd_make_hba_operational()
4690 err = -EIO; in ufshcd_make_hba_operational()
4698 * ufshcd_hba_stop - Send controller to reset state
4699 * @hba: per adapter instance
4701 void ufshcd_hba_stop(struct ufs_hba *hba) in ufshcd_hba_stop() argument
4710 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_hba_stop()
4711 ufshcd_writel(hba, CONTROLLER_DISABLE, REG_CONTROLLER_ENABLE); in ufshcd_hba_stop()
4712 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_hba_stop()
4714 err = ufshcd_wait_for_register(hba, REG_CONTROLLER_ENABLE, in ufshcd_hba_stop()
4718 dev_err(hba->dev, "%s: Controller disable failed\n", __func__); in ufshcd_hba_stop()
4723 * ufshcd_hba_execute_hce - initialize the controller
4724 * @hba: per adapter instance
4730 * Return: 0 on success, non-zero value on failure.
4732 static int ufshcd_hba_execute_hce(struct ufs_hba *hba) in ufshcd_hba_execute_hce() argument
4738 if (ufshcd_is_hba_active(hba)) in ufshcd_hba_execute_hce()
4740 ufshcd_hba_stop(hba); in ufshcd_hba_execute_hce()
4743 ufshcd_set_link_off(hba); in ufshcd_hba_execute_hce()
4745 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); in ufshcd_hba_execute_hce()
4748 ufshcd_hba_start(hba); in ufshcd_hba_execute_hce()
4752 * During initialization the HCE bit value changes from 1->0->1. in ufshcd_hba_execute_hce()
4760 ufshcd_delay_us(hba->vps->hba_enable_delay_us, 100); in ufshcd_hba_execute_hce()
4764 while (!ufshcd_is_hba_active(hba)) { in ufshcd_hba_execute_hce()
4766 retry_inner--; in ufshcd_hba_execute_hce()
4768 dev_err(hba->dev, in ufshcd_hba_execute_hce()
4771 retry_outer--; in ufshcd_hba_execute_hce()
4774 return -EIO; in ufshcd_hba_execute_hce()
4780 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); in ufshcd_hba_execute_hce()
4782 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); in ufshcd_hba_execute_hce()
4787 int ufshcd_hba_enable(struct ufs_hba *hba) in ufshcd_hba_enable() argument
4791 if (hba->quirks & UFSHCI_QUIRK_BROKEN_HCE) { in ufshcd_hba_enable()
4792 ufshcd_set_link_off(hba); in ufshcd_hba_enable()
4793 ufshcd_vops_hce_enable_notify(hba, PRE_CHANGE); in ufshcd_hba_enable()
4796 ufshcd_enable_intr(hba, UFSHCD_UIC_MASK); in ufshcd_hba_enable()
4797 ret = ufshcd_dme_reset(hba); in ufshcd_hba_enable()
4799 dev_err(hba->dev, "DME_RESET failed\n"); in ufshcd_hba_enable()
4803 ret = ufshcd_dme_enable(hba); in ufshcd_hba_enable()
4805 dev_err(hba->dev, "Enabling DME failed\n"); in ufshcd_hba_enable()
4809 ufshcd_vops_hce_enable_notify(hba, POST_CHANGE); in ufshcd_hba_enable()
4811 ret = ufshcd_hba_execute_hce(hba); in ufshcd_hba_enable()
4818 static int ufshcd_disable_tx_lcc(struct ufs_hba *hba, bool peer) in ufshcd_disable_tx_lcc() argument
4823 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), in ufshcd_disable_tx_lcc()
4826 ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_CONNECTEDTXDATALANES), in ufshcd_disable_tx_lcc()
4830 err = ufshcd_dme_set(hba, in ufshcd_disable_tx_lcc()
4835 err = ufshcd_dme_peer_set(hba, in ufshcd_disable_tx_lcc()
4840 dev_err(hba->dev, "%s: TX LCC Disable failed, peer = %d, lane = %d, err = %d", in ufshcd_disable_tx_lcc()
4849 static inline int ufshcd_disable_device_tx_lcc(struct ufs_hba *hba) in ufshcd_disable_device_tx_lcc() argument
4851 return ufshcd_disable_tx_lcc(hba, true); in ufshcd_disable_device_tx_lcc()
4854 void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val) in ufshcd_update_evt_hist() argument
4861 e = &hba->ufs_stats.event[id]; in ufshcd_update_evt_hist()
4862 e->val[e->pos] = val; in ufshcd_update_evt_hist()
4863 e->tstamp[e->pos] = local_clock(); in ufshcd_update_evt_hist()
4864 e->cnt += 1; in ufshcd_update_evt_hist()
4865 e->pos = (e->pos + 1) % UFS_EVENT_HIST_LENGTH; in ufshcd_update_evt_hist()
4867 ufshcd_vops_event_notify(hba, id, &val); in ufshcd_update_evt_hist()
4872 * ufshcd_link_startup - Initialize unipro link startup
4873 * @hba: per adapter instance
4875 * Return: 0 for success, non-zero in case of failure.
4877 static int ufshcd_link_startup(struct ufs_hba *hba) in ufshcd_link_startup() argument
4887 if (!ufshcd_is_ufs_dev_active(hba)) in ufshcd_link_startup()
4892 ufshcd_vops_link_startup_notify(hba, PRE_CHANGE); in ufshcd_link_startup()
4894 ret = ufshcd_dme_link_startup(hba); in ufshcd_link_startup()
4896 /* check if device is detected by inter-connect layer */ in ufshcd_link_startup()
4897 if (!ret && !ufshcd_is_device_present(hba)) { in ufshcd_link_startup()
4898 ufshcd_update_evt_hist(hba, in ufshcd_link_startup()
4901 dev_err(hba->dev, "%s: Device not present\n", __func__); in ufshcd_link_startup()
4902 ret = -ENXIO; in ufshcd_link_startup()
4909 * succeeds. So reset the local Uni-Pro and try again. in ufshcd_link_startup()
4911 if (ret && retries && ufshcd_hba_enable(hba)) { in ufshcd_link_startup()
4912 ufshcd_update_evt_hist(hba, in ufshcd_link_startup()
4917 } while (ret && retries--); in ufshcd_link_startup()
4921 ufshcd_update_evt_hist(hba, in ufshcd_link_startup()
4933 /* Mark that link is up in PWM-G1, 1-lane, SLOW-AUTO mode */ in ufshcd_link_startup()
4934 ufshcd_init_pwr_info(hba); in ufshcd_link_startup()
4935 ufshcd_print_pwr_info(hba); in ufshcd_link_startup()
4937 if (hba->quirks & UFSHCD_QUIRK_BROKEN_LCC) { in ufshcd_link_startup()
4938 ret = ufshcd_disable_device_tx_lcc(hba); in ufshcd_link_startup()
4944 ret = ufshcd_vops_link_startup_notify(hba, POST_CHANGE); in ufshcd_link_startup()
4949 ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); in ufshcd_link_startup()
4950 ret = ufshcd_make_hba_operational(hba); in ufshcd_link_startup()
4953 dev_err(hba->dev, "link startup failed %d\n", ret); in ufshcd_link_startup()
4954 ufshcd_print_host_state(hba); in ufshcd_link_startup()
4955 ufshcd_print_pwr_info(hba); in ufshcd_link_startup()
4956 ufshcd_print_evt_hist(hba); in ufshcd_link_startup()
4962 * ufshcd_verify_dev_init() - Verify device initialization
4963 * @hba: per-adapter instance
4973 static int ufshcd_verify_dev_init(struct ufs_hba *hba) in ufshcd_verify_dev_init() argument
4978 ufshcd_hold(hba); in ufshcd_verify_dev_init()
4979 mutex_lock(&hba->dev_cmd.lock); in ufshcd_verify_dev_init()
4980 for (retries = NOP_OUT_RETRIES; retries > 0; retries--) { in ufshcd_verify_dev_init()
4981 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_NOP, in ufshcd_verify_dev_init()
4982 hba->nop_out_timeout); in ufshcd_verify_dev_init()
4984 if (!err || err == -ETIMEDOUT) in ufshcd_verify_dev_init()
4987 dev_dbg(hba->dev, "%s: error %d retrying\n", __func__, err); in ufshcd_verify_dev_init()
4989 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_verify_dev_init()
4990 ufshcd_release(hba); in ufshcd_verify_dev_init()
4993 dev_err(hba->dev, "%s: NOP OUT failed %d\n", __func__, err); in ufshcd_verify_dev_init()
4998 * ufshcd_setup_links - associate link b/w device wlun and other luns
5000 * @hba: pointer to ufs hba
5002 static void ufshcd_setup_links(struct ufs_hba *hba, struct scsi_device *sdev) in ufshcd_setup_links() argument
5010 if (hba->ufs_device_wlun) { in ufshcd_setup_links()
5011 link = device_link_add(&sdev->sdev_gendev, in ufshcd_setup_links()
5012 &hba->ufs_device_wlun->sdev_gendev, in ufshcd_setup_links()
5015 dev_err(&sdev->sdev_gendev, "Failed establishing link - %s\n", in ufshcd_setup_links()
5016 dev_name(&hba->ufs_device_wlun->sdev_gendev)); in ufshcd_setup_links()
5019 hba->luns_avail--; in ufshcd_setup_links()
5021 if (hba->luns_avail == 1) { in ufshcd_setup_links()
5022 ufshcd_rpm_put(hba); in ufshcd_setup_links()
5030 hba->luns_avail--; in ufshcd_setup_links()
5035 * ufshcd_lu_init - Initialize the relevant parameters of the LU
5036 * @hba: per-adapter instance
5039 static void ufshcd_lu_init(struct ufs_hba *hba, struct scsi_device *sdev) in ufshcd_lu_init() argument
5042 u8 lun = ufshcd_scsi_to_upiu_lun(sdev->lun); in ufshcd_lu_init()
5043 u8 lun_qdepth = hba->nutrs; in ufshcd_lu_init()
5051 ret = ufshcd_read_unit_desc_param(hba, lun, 0, desc_buf, len); in ufshcd_lu_init()
5053 if (ret == -EOPNOTSUPP) in ufshcd_lu_init()
5062 * In per-LU queueing architecture, bLUQueueDepth will not be 0, then we will in ufshcd_lu_init()
5063 * use the smaller between UFSHCI CAP.NUTRS and UFS LU bLUQueueDepth in ufshcd_lu_init()
5065 lun_qdepth = min_t(int, desc_buf[UNIT_DESC_PARAM_LU_Q_DEPTH], hba->nutrs); in ufshcd_lu_init()
5071 if (hba->dev_info.f_power_on_wp_en && lun < hba->dev_info.max_lu_supported && in ufshcd_lu_init()
5072 !hba->dev_info.is_lu_power_on_wp && in ufshcd_lu_init()
5074 hba->dev_info.is_lu_power_on_wp = true; in ufshcd_lu_init()
5079 hba->dev_info.b_advanced_rpmb_en = true; in ufshcd_lu_init()
5088 dev_dbg(hba->dev, "Set LU %x queue depth %d\n", lun, lun_qdepth); in ufshcd_lu_init()
5093 * ufshcd_slave_alloc - handle initial SCSI device configurations
5100 struct ufs_hba *hba; in ufshcd_slave_alloc() local
5102 hba = shost_priv(sdev->host); in ufshcd_slave_alloc()
5105 sdev->use_10_for_ms = 1; in ufshcd_slave_alloc()
5108 sdev->set_dbd_for_ms = 1; in ufshcd_slave_alloc()
5111 sdev->allow_restart = 1; in ufshcd_slave_alloc()
5114 sdev->no_report_opcodes = 1; in ufshcd_slave_alloc()
5117 sdev->no_write_same = 1; in ufshcd_slave_alloc()
5119 ufshcd_lu_init(hba, sdev); in ufshcd_slave_alloc()
5121 ufshcd_setup_links(hba, sdev); in ufshcd_slave_alloc()
5127 * ufshcd_change_queue_depth - change queue depth
5137 return scsi_change_queue_depth(sdev, min(depth, sdev->host->can_queue)); in ufshcd_change_queue_depth()
5141 * ufshcd_slave_configure - adjust SCSI device configurations
5148 struct ufs_hba *hba = shost_priv(sdev->host); in ufshcd_slave_configure() local
5149 struct request_queue *q = sdev->request_queue; in ufshcd_slave_configure()
5151 blk_queue_update_dma_pad(q, PRDT_DATA_BYTE_COUNT_PAD - 1); in ufshcd_slave_configure()
5152 if (hba->quirks & UFSHCD_QUIRK_4KB_DMA_ALIGNMENT) in ufshcd_slave_configure()
5153 blk_queue_update_dma_alignment(q, SZ_4K - 1); in ufshcd_slave_configure()
5155 * Block runtime-pm until all consumers are added. in ufshcd_slave_configure()
5159 pm_runtime_get_noresume(&sdev->sdev_gendev); in ufshcd_slave_configure()
5160 else if (ufshcd_is_rpm_autosuspend_allowed(hba)) in ufshcd_slave_configure()
5161 sdev->rpm_autosuspend = 1; in ufshcd_slave_configure()
5163 * Do not print messages during runtime PM to avoid never-ending cycles in ufshcd_slave_configure()
5167 sdev->silence_suspend = 1; in ufshcd_slave_configure()
5169 ufshcd_crypto_register(hba, q); in ufshcd_slave_configure()
5175 * ufshcd_slave_destroy - remove SCSI device configurations
5180 struct ufs_hba *hba; in ufshcd_slave_destroy() local
5183 hba = shost_priv(sdev->host); in ufshcd_slave_destroy()
5186 if (ufshcd_scsi_to_upiu_lun(sdev->lun) == UFS_UPIU_UFS_DEVICE_WLUN) { in ufshcd_slave_destroy()
5187 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5188 hba->ufs_device_wlun = NULL; in ufshcd_slave_destroy()
5189 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5190 } else if (hba->ufs_device_wlun) { in ufshcd_slave_destroy()
5194 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5195 if (hba->ufs_device_wlun) { in ufshcd_slave_destroy()
5196 supplier = &hba->ufs_device_wlun->sdev_gendev; in ufshcd_slave_destroy()
5199 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_slave_destroy()
5207 device_link_remove(&sdev->sdev_gendev, supplier); in ufshcd_slave_destroy()
5214 * ufshcd_scsi_cmd_status - Update SCSI command result based on SCSI status
5247 * ufshcd_transfer_rsp_status - Get overall status of the response
5248 * @hba: per adapter instance
5255 ufshcd_transfer_rsp_status(struct ufs_hba *hba, struct ufshcd_lrb *lrbp, in ufshcd_transfer_rsp_status() argument
5264 upiu_flags = lrbp->ucd_rsp_ptr->header.flags; in ufshcd_transfer_rsp_status()
5265 resid = be32_to_cpu(lrbp->ucd_rsp_ptr->sr.residual_transfer_count); in ufshcd_transfer_rsp_status()
5271 scsi_set_resid(lrbp->cmd, resid); in ufshcd_transfer_rsp_status()
5276 if (hba->quirks & UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR) { in ufshcd_transfer_rsp_status()
5277 if (lrbp->ucd_rsp_ptr->header.response || in ufshcd_transfer_rsp_status()
5278 lrbp->ucd_rsp_ptr->header.status) in ufshcd_transfer_rsp_status()
5284 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); in ufshcd_transfer_rsp_status()
5285 switch (ufshcd_get_req_rsp(lrbp->ucd_rsp_ptr)) { in ufshcd_transfer_rsp_status()
5291 scsi_status = lrbp->ucd_rsp_ptr->header.status; in ufshcd_transfer_rsp_status()
5306 if (!hba->pm_op_in_progress && in ufshcd_transfer_rsp_status()
5307 !ufshcd_eh_in_progress(hba) && in ufshcd_transfer_rsp_status()
5308 ufshcd_is_exception_event(lrbp->ucd_rsp_ptr)) in ufshcd_transfer_rsp_status()
5310 schedule_work(&hba->eeh_work); in ufshcd_transfer_rsp_status()
5315 dev_err(hba->dev, in ufshcd_transfer_rsp_status()
5319 dev_err(hba->dev, in ufshcd_transfer_rsp_status()
5343 dev_err(hba->dev, in ufshcd_transfer_rsp_status()
5345 ocs, lrbp->task_tag); in ufshcd_transfer_rsp_status()
5346 ufshcd_print_evt_hist(hba); in ufshcd_transfer_rsp_status()
5347 ufshcd_print_host_state(hba); in ufshcd_transfer_rsp_status()
5352 (host_byte(result) != DID_REQUEUE) && !hba->silence_err_logs) in ufshcd_transfer_rsp_status()
5353 ufshcd_print_tr(hba, lrbp->task_tag, true); in ufshcd_transfer_rsp_status()
5357 static bool ufshcd_is_auto_hibern8_error(struct ufs_hba *hba, in ufshcd_is_auto_hibern8_error() argument
5360 if (!ufshcd_is_auto_hibern8_supported(hba) || in ufshcd_is_auto_hibern8_error()
5361 !ufshcd_is_auto_hibern8_enabled(hba)) in ufshcd_is_auto_hibern8_error()
5367 if (hba->active_uic_cmd && in ufshcd_is_auto_hibern8_error()
5368 (hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_ENTER || in ufshcd_is_auto_hibern8_error()
5369 hba->active_uic_cmd->command == UIC_CMD_DME_HIBER_EXIT)) in ufshcd_is_auto_hibern8_error()
5376 * ufshcd_uic_cmd_compl - handle completion of uic command
5377 * @hba: per adapter instance
5381 * IRQ_HANDLED - If interrupt is valid
5382 * IRQ_NONE - If invalid interrupt
5384 static irqreturn_t ufshcd_uic_cmd_compl(struct ufs_hba *hba, u32 intr_status) in ufshcd_uic_cmd_compl() argument
5388 spin_lock(hba->host->host_lock); in ufshcd_uic_cmd_compl()
5389 if (ufshcd_is_auto_hibern8_error(hba, intr_status)) in ufshcd_uic_cmd_compl()
5390 hba->errors |= (UFSHCD_UIC_HIBERN8_MASK & intr_status); in ufshcd_uic_cmd_compl()
5392 if ((intr_status & UIC_COMMAND_COMPL) && hba->active_uic_cmd) { in ufshcd_uic_cmd_compl()
5393 hba->active_uic_cmd->argument2 |= in ufshcd_uic_cmd_compl()
5394 ufshcd_get_uic_cmd_result(hba); in ufshcd_uic_cmd_compl()
5395 hba->active_uic_cmd->argument3 = in ufshcd_uic_cmd_compl()
5396 ufshcd_get_dme_attr_val(hba); in ufshcd_uic_cmd_compl()
5397 if (!hba->uic_async_done) in ufshcd_uic_cmd_compl()
5398 hba->active_uic_cmd->cmd_active = 0; in ufshcd_uic_cmd_compl()
5399 complete(&hba->active_uic_cmd->done); in ufshcd_uic_cmd_compl()
5403 if ((intr_status & UFSHCD_UIC_PWR_MASK) && hba->uic_async_done) { in ufshcd_uic_cmd_compl()
5404 hba->active_uic_cmd->cmd_active = 0; in ufshcd_uic_cmd_compl()
5405 complete(hba->uic_async_done); in ufshcd_uic_cmd_compl()
5410 ufshcd_add_uic_command_trace(hba, hba->active_uic_cmd, in ufshcd_uic_cmd_compl()
5412 spin_unlock(hba->host->host_lock); in ufshcd_uic_cmd_compl()
5417 void ufshcd_release_scsi_cmd(struct ufs_hba *hba, in ufshcd_release_scsi_cmd() argument
5420 struct scsi_cmnd *cmd = lrbp->cmd; in ufshcd_release_scsi_cmd()
5423 ufshcd_release(hba); in ufshcd_release_scsi_cmd()
5424 ufshcd_clk_scaling_update_busy(hba); in ufshcd_release_scsi_cmd()
5428 * ufshcd_compl_one_cqe - handle a completion queue entry
5429 * @hba: per adapter instance
5433 void ufshcd_compl_one_cqe(struct ufs_hba *hba, int task_tag, in ufshcd_compl_one_cqe() argument
5440 lrbp = &hba->lrb[task_tag]; in ufshcd_compl_one_cqe()
5441 lrbp->compl_time_stamp = ktime_get(); in ufshcd_compl_one_cqe()
5442 lrbp->compl_time_stamp_local_clock = local_clock(); in ufshcd_compl_one_cqe()
5443 cmd = lrbp->cmd; in ufshcd_compl_one_cqe()
5445 if (unlikely(ufshcd_should_inform_monitor(hba, lrbp))) in ufshcd_compl_one_cqe()
5446 ufshcd_update_monitor(hba, lrbp); in ufshcd_compl_one_cqe()
5447 ufshcd_add_command_trace(hba, task_tag, UFS_CMD_COMP); in ufshcd_compl_one_cqe()
5448 cmd->result = ufshcd_transfer_rsp_status(hba, lrbp, cqe); in ufshcd_compl_one_cqe()
5449 ufshcd_release_scsi_cmd(hba, lrbp); in ufshcd_compl_one_cqe()
5452 } else if (lrbp->command_type == UTP_CMD_TYPE_DEV_MANAGE || in ufshcd_compl_one_cqe()
5453 lrbp->command_type == UTP_CMD_TYPE_UFS_STORAGE) { in ufshcd_compl_one_cqe()
5454 if (hba->dev_cmd.complete) { in ufshcd_compl_one_cqe()
5456 ocs = le32_to_cpu(cqe->status) & MASK_OCS; in ufshcd_compl_one_cqe()
5457 lrbp->utr_descriptor_ptr->header.ocs = ocs; in ufshcd_compl_one_cqe()
5459 complete(hba->dev_cmd.complete); in ufshcd_compl_one_cqe()
5460 ufshcd_clk_scaling_update_busy(hba); in ufshcd_compl_one_cqe()
5466 * __ufshcd_transfer_req_compl - handle SCSI and query command completion
5467 * @hba: per adapter instance
5470 static void __ufshcd_transfer_req_compl(struct ufs_hba *hba, in __ufshcd_transfer_req_compl() argument
5475 for_each_set_bit(tag, &completed_reqs, hba->nutrs) in __ufshcd_transfer_req_compl()
5476 ufshcd_compl_one_cqe(hba, tag, NULL); in __ufshcd_transfer_req_compl()
5481 UFSHCD_POLL_FROM_INTERRUPT_CONTEXT = -1
5484 static void ufshcd_clear_polled(struct ufs_hba *hba, in ufshcd_clear_polled() argument
5489 for_each_set_bit(tag, completed_reqs, hba->nutrs) { in ufshcd_clear_polled()
5490 struct scsi_cmnd *cmd = hba->lrb[tag].cmd; in ufshcd_clear_polled()
5494 if (scsi_cmd_to_rq(cmd)->cmd_flags & REQ_POLLED) in ufshcd_clear_polled()
5505 struct ufs_hba *hba = shost_priv(shost); in ufshcd_poll() local
5510 if (is_mcq_enabled(hba)) { in ufshcd_poll()
5511 hwq = &hba->uhq[queue_num]; in ufshcd_poll()
5513 return ufshcd_mcq_poll_cqe_lock(hba, hwq); in ufshcd_poll()
5516 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_poll()
5517 tr_doorbell = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); in ufshcd_poll()
5518 completed_reqs = ~tr_doorbell & hba->outstanding_reqs; in ufshcd_poll()
5519 WARN_ONCE(completed_reqs & ~hba->outstanding_reqs, in ufshcd_poll()
5521 hba->outstanding_reqs); in ufshcd_poll()
5524 ufshcd_clear_polled(hba, &completed_reqs); in ufshcd_poll()
5526 hba->outstanding_reqs &= ~completed_reqs; in ufshcd_poll()
5527 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_poll()
5530 __ufshcd_transfer_req_compl(hba, completed_reqs); in ufshcd_poll()
5536 * ufshcd_mcq_compl_pending_transfer - MCQ mode function. It is
5541 * @hba: per adapter instance
5546 static void ufshcd_mcq_compl_pending_transfer(struct ufs_hba *hba, in ufshcd_mcq_compl_pending_transfer() argument
5556 for (tag = 0; tag < hba->nutrs; tag++) { in ufshcd_mcq_compl_pending_transfer()
5557 lrbp = &hba->lrb[tag]; in ufshcd_mcq_compl_pending_transfer()
5558 cmd = lrbp->cmd; in ufshcd_mcq_compl_pending_transfer()
5560 test_bit(SCMD_STATE_COMPLETE, &cmd->state)) in ufshcd_mcq_compl_pending_transfer()
5565 hwq = &hba->uhq[hwq_num]; in ufshcd_mcq_compl_pending_transfer()
5568 ufshcd_mcq_compl_all_cqes_lock(hba, hwq); in ufshcd_mcq_compl_pending_transfer()
5573 if (cmd && !test_bit(SCMD_STATE_COMPLETE, &cmd->state)) { in ufshcd_mcq_compl_pending_transfer()
5574 spin_lock_irqsave(&hwq->cq_lock, flags); in ufshcd_mcq_compl_pending_transfer()
5576 ufshcd_release_scsi_cmd(hba, lrbp); in ufshcd_mcq_compl_pending_transfer()
5578 spin_unlock_irqrestore(&hwq->cq_lock, flags); in ufshcd_mcq_compl_pending_transfer()
5581 ufshcd_mcq_poll_cqe_lock(hba, hwq); in ufshcd_mcq_compl_pending_transfer()
5587 * ufshcd_transfer_req_compl - handle SCSI and query command completion
5588 * @hba: per adapter instance
5591 * IRQ_HANDLED - If interrupt is valid
5592 * IRQ_NONE - If invalid interrupt
5594 static irqreturn_t ufshcd_transfer_req_compl(struct ufs_hba *hba) in ufshcd_transfer_req_compl() argument
5603 if (ufshcd_is_intr_aggr_allowed(hba) && in ufshcd_transfer_req_compl()
5604 !(hba->quirks & UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR)) in ufshcd_transfer_req_compl()
5605 ufshcd_reset_intr_aggr(hba); in ufshcd_transfer_req_compl()
5614 ufshcd_poll(hba->host, UFSHCD_POLL_FROM_INTERRUPT_CONTEXT); in ufshcd_transfer_req_compl()
5619 int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask) in __ufshcd_write_ee_control() argument
5621 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, in __ufshcd_write_ee_control()
5626 int ufshcd_write_ee_control(struct ufs_hba *hba) in ufshcd_write_ee_control() argument
5630 mutex_lock(&hba->ee_ctrl_mutex); in ufshcd_write_ee_control()
5631 err = __ufshcd_write_ee_control(hba, hba->ee_ctrl_mask); in ufshcd_write_ee_control()
5632 mutex_unlock(&hba->ee_ctrl_mutex); in ufshcd_write_ee_control()
5634 dev_err(hba->dev, "%s: failed to write ee control %d\n", in ufshcd_write_ee_control()
5639 int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask, in ufshcd_update_ee_control() argument
5645 mutex_lock(&hba->ee_ctrl_mutex); in ufshcd_update_ee_control()
5648 if (ee_ctrl_mask != hba->ee_ctrl_mask) in ufshcd_update_ee_control()
5649 err = __ufshcd_write_ee_control(hba, ee_ctrl_mask); in ufshcd_update_ee_control()
5652 hba->ee_ctrl_mask = ee_ctrl_mask; in ufshcd_update_ee_control()
5655 mutex_unlock(&hba->ee_ctrl_mutex); in ufshcd_update_ee_control()
5660 * ufshcd_disable_ee - disable exception event
5661 * @hba: per-adapter instance
5667 * Return: zero on success, non-zero error value on failure.
5669 static inline int ufshcd_disable_ee(struct ufs_hba *hba, u16 mask) in ufshcd_disable_ee() argument
5671 return ufshcd_update_ee_drv_mask(hba, 0, mask); in ufshcd_disable_ee()
5675 * ufshcd_enable_ee - enable exception event
5676 * @hba: per-adapter instance
5682 * Return: zero on success, non-zero error value on failure.
5684 static inline int ufshcd_enable_ee(struct ufs_hba *hba, u16 mask) in ufshcd_enable_ee() argument
5686 return ufshcd_update_ee_drv_mask(hba, mask, 0); in ufshcd_enable_ee()
5690 * ufshcd_enable_auto_bkops - Allow device managed BKOPS
5691 * @hba: per-adapter instance
5698 * Return: zero on success, non-zero on failure.
5700 static int ufshcd_enable_auto_bkops(struct ufs_hba *hba) in ufshcd_enable_auto_bkops() argument
5704 if (hba->auto_bkops_enabled) in ufshcd_enable_auto_bkops()
5707 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_SET_FLAG, in ufshcd_enable_auto_bkops()
5710 dev_err(hba->dev, "%s: failed to enable bkops %d\n", in ufshcd_enable_auto_bkops()
5715 hba->auto_bkops_enabled = true; in ufshcd_enable_auto_bkops()
5716 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Enabled"); in ufshcd_enable_auto_bkops()
5719 err = ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); in ufshcd_enable_auto_bkops()
5721 dev_err(hba->dev, "%s: failed to disable exception event %d\n", in ufshcd_enable_auto_bkops()
5728 * ufshcd_disable_auto_bkops - block device in doing background operations
5729 * @hba: per-adapter instance
5733 * not-operable. Make sure to call ufshcd_enable_auto_bkops() whenever the
5737 * Return: zero on success, non-zero on failure.
5739 static int ufshcd_disable_auto_bkops(struct ufs_hba *hba) in ufshcd_disable_auto_bkops() argument
5743 if (!hba->auto_bkops_enabled) in ufshcd_disable_auto_bkops()
5750 err = ufshcd_enable_ee(hba, MASK_EE_URGENT_BKOPS); in ufshcd_disable_auto_bkops()
5752 dev_err(hba->dev, "%s: failed to enable exception event %d\n", in ufshcd_disable_auto_bkops()
5757 err = ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_CLEAR_FLAG, in ufshcd_disable_auto_bkops()
5760 dev_err(hba->dev, "%s: failed to disable bkops %d\n", in ufshcd_disable_auto_bkops()
5762 ufshcd_disable_ee(hba, MASK_EE_URGENT_BKOPS); in ufshcd_disable_auto_bkops()
5766 hba->auto_bkops_enabled = false; in ufshcd_disable_auto_bkops()
5767 trace_ufshcd_auto_bkops_state(dev_name(hba->dev), "Disabled"); in ufshcd_disable_auto_bkops()
5768 hba->is_urgent_bkops_lvl_checked = false; in ufshcd_disable_auto_bkops()
5774 * ufshcd_force_reset_auto_bkops - force reset auto bkops state
5775 * @hba: per adapter instance
5779 * as well. This function would change the auto-bkops state based on
5782 static void ufshcd_force_reset_auto_bkops(struct ufs_hba *hba) in ufshcd_force_reset_auto_bkops() argument
5784 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) { in ufshcd_force_reset_auto_bkops()
5785 hba->auto_bkops_enabled = false; in ufshcd_force_reset_auto_bkops()
5786 hba->ee_ctrl_mask |= MASK_EE_URGENT_BKOPS; in ufshcd_force_reset_auto_bkops()
5787 ufshcd_enable_auto_bkops(hba); in ufshcd_force_reset_auto_bkops()
5789 hba->auto_bkops_enabled = true; in ufshcd_force_reset_auto_bkops()
5790 hba->ee_ctrl_mask &= ~MASK_EE_URGENT_BKOPS; in ufshcd_force_reset_auto_bkops()
5791 ufshcd_disable_auto_bkops(hba); in ufshcd_force_reset_auto_bkops()
5793 hba->urgent_bkops_lvl = BKOPS_STATUS_PERF_IMPACT; in ufshcd_force_reset_auto_bkops()
5794 hba->is_urgent_bkops_lvl_checked = false; in ufshcd_force_reset_auto_bkops()
5797 static inline int ufshcd_get_bkops_status(struct ufs_hba *hba, u32 *status) in ufshcd_get_bkops_status() argument
5799 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_get_bkops_status()
5804 * ufshcd_bkops_ctrl - control the auto bkops based on current bkops status
5805 * @hba: per-adapter instance
5813 * Return: 0 for success, non-zero in case of failure.
5815 * NOTE: Caller of this function can check the "hba->auto_bkops_enabled" flag
5819 static int ufshcd_bkops_ctrl(struct ufs_hba *hba, in ufshcd_bkops_ctrl() argument
5825 err = ufshcd_get_bkops_status(hba, &curr_status); in ufshcd_bkops_ctrl()
5827 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", in ufshcd_bkops_ctrl()
5831 dev_err(hba->dev, "%s: invalid BKOPS status %d\n", in ufshcd_bkops_ctrl()
5833 err = -EINVAL; in ufshcd_bkops_ctrl()
5838 err = ufshcd_enable_auto_bkops(hba); in ufshcd_bkops_ctrl()
5840 err = ufshcd_disable_auto_bkops(hba); in ufshcd_bkops_ctrl()
5846 * ufshcd_urgent_bkops - handle urgent bkops exception event
5847 * @hba: per-adapter instance
5857 static int ufshcd_urgent_bkops(struct ufs_hba *hba) in ufshcd_urgent_bkops() argument
5859 return ufshcd_bkops_ctrl(hba, hba->urgent_bkops_lvl); in ufshcd_urgent_bkops()
5862 static inline int ufshcd_get_ee_status(struct ufs_hba *hba, u32 *status) in ufshcd_get_ee_status() argument
5864 return ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_get_ee_status()
5868 static void ufshcd_bkops_exception_event_handler(struct ufs_hba *hba) in ufshcd_bkops_exception_event_handler() argument
5873 if (hba->is_urgent_bkops_lvl_checked) in ufshcd_bkops_exception_event_handler()
5876 err = ufshcd_get_bkops_status(hba, &curr_status); in ufshcd_bkops_exception_event_handler()
5878 dev_err(hba->dev, "%s: failed to get BKOPS status %d\n", in ufshcd_bkops_exception_event_handler()
5890 dev_err(hba->dev, "%s: device raised urgent BKOPS exception for bkops status %d\n", in ufshcd_bkops_exception_event_handler()
5893 hba->urgent_bkops_lvl = curr_status; in ufshcd_bkops_exception_event_handler()
5894 hba->is_urgent_bkops_lvl_checked = true; in ufshcd_bkops_exception_event_handler()
5898 err = ufshcd_enable_auto_bkops(hba); in ufshcd_bkops_exception_event_handler()
5901 dev_err(hba->dev, "%s: failed to handle urgent bkops %d\n", in ufshcd_bkops_exception_event_handler()
5905 static void ufshcd_temp_exception_event_handler(struct ufs_hba *hba, u16 status) in ufshcd_temp_exception_event_handler() argument
5909 if (ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_temp_exception_event_handler()
5913 dev_info(hba->dev, "exception Tcase %d\n", value - 80); in ufshcd_temp_exception_event_handler()
5915 ufs_hwmon_notify_event(hba, status & MASK_EE_URGENT_TEMP); in ufshcd_temp_exception_event_handler()
5923 static int __ufshcd_wb_toggle(struct ufs_hba *hba, bool set, enum flag_idn idn) in __ufshcd_wb_toggle() argument
5929 index = ufshcd_wb_get_query_index(hba); in __ufshcd_wb_toggle()
5930 return ufshcd_query_flag_retry(hba, opcode, idn, index, NULL); in __ufshcd_wb_toggle()
5933 int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable) in ufshcd_wb_toggle() argument
5937 if (!ufshcd_is_wb_allowed(hba) || in ufshcd_wb_toggle()
5938 hba->dev_info.wb_enabled == enable) in ufshcd_wb_toggle()
5941 ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_EN); in ufshcd_wb_toggle()
5943 dev_err(hba->dev, "%s: Write Booster %s failed %d\n", in ufshcd_wb_toggle()
5948 hba->dev_info.wb_enabled = enable; in ufshcd_wb_toggle()
5949 dev_dbg(hba->dev, "%s: Write Booster %s\n", in ufshcd_wb_toggle()
5955 static void ufshcd_wb_toggle_buf_flush_during_h8(struct ufs_hba *hba, in ufshcd_wb_toggle_buf_flush_during_h8() argument
5960 ret = __ufshcd_wb_toggle(hba, enable, in ufshcd_wb_toggle_buf_flush_during_h8()
5963 dev_err(hba->dev, "%s: WB-Buf Flush during H8 %s failed %d\n", in ufshcd_wb_toggle_buf_flush_during_h8()
5967 dev_dbg(hba->dev, "%s: WB-Buf Flush during H8 %s\n", in ufshcd_wb_toggle_buf_flush_during_h8()
5971 int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable) in ufshcd_wb_toggle_buf_flush() argument
5975 if (!ufshcd_is_wb_allowed(hba) || in ufshcd_wb_toggle_buf_flush()
5976 hba->dev_info.wb_buf_flush_enabled == enable) in ufshcd_wb_toggle_buf_flush()
5979 ret = __ufshcd_wb_toggle(hba, enable, QUERY_FLAG_IDN_WB_BUFF_FLUSH_EN); in ufshcd_wb_toggle_buf_flush()
5981 dev_err(hba->dev, "%s: WB-Buf Flush %s failed %d\n", in ufshcd_wb_toggle_buf_flush()
5986 hba->dev_info.wb_buf_flush_enabled = enable; in ufshcd_wb_toggle_buf_flush()
5987 dev_dbg(hba->dev, "%s: WB-Buf Flush %s\n", in ufshcd_wb_toggle_buf_flush()
5993 static bool ufshcd_wb_presrv_usrspc_keep_vcc_on(struct ufs_hba *hba, in ufshcd_wb_presrv_usrspc_keep_vcc_on() argument
6000 index = ufshcd_wb_get_query_index(hba); in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6001 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6005 dev_err(hba->dev, "%s: dCurWriteBoosterBufferSize read failed %d\n", in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6011 dev_info(hba->dev, "dCurWBBuf: %d WB disabled until free-space is available\n", in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6016 return avail_buf < hba->vps->wb_flush_threshold; in ufshcd_wb_presrv_usrspc_keep_vcc_on()
6019 static void ufshcd_wb_force_disable(struct ufs_hba *hba) in ufshcd_wb_force_disable() argument
6021 if (ufshcd_is_wb_buf_flush_allowed(hba)) in ufshcd_wb_force_disable()
6022 ufshcd_wb_toggle_buf_flush(hba, false); in ufshcd_wb_force_disable()
6024 ufshcd_wb_toggle_buf_flush_during_h8(hba, false); in ufshcd_wb_force_disable()
6025 ufshcd_wb_toggle(hba, false); in ufshcd_wb_force_disable()
6026 hba->caps &= ~UFSHCD_CAP_WB_EN; in ufshcd_wb_force_disable()
6028 dev_info(hba->dev, "%s: WB force disabled\n", __func__); in ufshcd_wb_force_disable()
6031 static bool ufshcd_is_wb_buf_lifetime_available(struct ufs_hba *hba) in ufshcd_is_wb_buf_lifetime_available() argument
6037 index = ufshcd_wb_get_query_index(hba); in ufshcd_is_wb_buf_lifetime_available()
6038 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_is_wb_buf_lifetime_available()
6042 dev_err(hba->dev, in ufshcd_is_wb_buf_lifetime_available()
6049 dev_err(hba->dev, "%s: WB buf lifetime is exhausted 0x%02X\n", in ufshcd_is_wb_buf_lifetime_available()
6054 dev_dbg(hba->dev, "%s: WB buf lifetime is 0x%02X\n", in ufshcd_is_wb_buf_lifetime_available()
6060 static bool ufshcd_wb_need_flush(struct ufs_hba *hba) in ufshcd_wb_need_flush() argument
6066 if (!ufshcd_is_wb_allowed(hba)) in ufshcd_wb_need_flush()
6069 if (!ufshcd_is_wb_buf_lifetime_available(hba)) { in ufshcd_wb_need_flush()
6070 ufshcd_wb_force_disable(hba); in ufshcd_wb_need_flush()
6076 * With user-space reduction enabled, it's enough to enable flush in ufshcd_wb_need_flush()
6079 * With user-space preserved enabled, the current-buffer in ufshcd_wb_need_flush()
6085 index = ufshcd_wb_get_query_index(hba); in ufshcd_wb_need_flush()
6086 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_wb_need_flush()
6090 dev_warn(hba->dev, "%s: dAvailableWriteBoosterBufferSize read failed %d\n", in ufshcd_wb_need_flush()
6095 if (!hba->dev_info.b_presrv_uspc_en) in ufshcd_wb_need_flush()
6098 return ufshcd_wb_presrv_usrspc_keep_vcc_on(hba, avail_buf); in ufshcd_wb_need_flush()
6103 struct ufs_hba *hba = container_of(to_delayed_work(work), in ufshcd_rpm_dev_flush_recheck_work() local
6112 ufshcd_rpm_get_sync(hba); in ufshcd_rpm_dev_flush_recheck_work()
6113 ufshcd_rpm_put_sync(hba); in ufshcd_rpm_dev_flush_recheck_work()
6117 * ufshcd_exception_event_handler - handle exceptions raised by device
6125 struct ufs_hba *hba; in ufshcd_exception_event_handler() local
6128 hba = container_of(work, struct ufs_hba, eeh_work); in ufshcd_exception_event_handler()
6130 ufshcd_scsi_block_requests(hba); in ufshcd_exception_event_handler()
6131 err = ufshcd_get_ee_status(hba, &status); in ufshcd_exception_event_handler()
6133 dev_err(hba->dev, "%s: failed to get exception status %d\n", in ufshcd_exception_event_handler()
6138 trace_ufshcd_exception_event(dev_name(hba->dev), status); in ufshcd_exception_event_handler()
6140 if (status & hba->ee_drv_mask & MASK_EE_URGENT_BKOPS) in ufshcd_exception_event_handler()
6141 ufshcd_bkops_exception_event_handler(hba); in ufshcd_exception_event_handler()
6143 if (status & hba->ee_drv_mask & MASK_EE_URGENT_TEMP) in ufshcd_exception_event_handler()
6144 ufshcd_temp_exception_event_handler(hba, status); in ufshcd_exception_event_handler()
6146 ufs_debugfs_exception_event(hba, status); in ufshcd_exception_event_handler()
6148 ufshcd_scsi_unblock_requests(hba); in ufshcd_exception_event_handler()
6151 /* Complete requests that have door-bell cleared */
6152 static void ufshcd_complete_requests(struct ufs_hba *hba, bool force_compl) in ufshcd_complete_requests() argument
6154 if (is_mcq_enabled(hba)) in ufshcd_complete_requests()
6155 ufshcd_mcq_compl_pending_transfer(hba, force_compl); in ufshcd_complete_requests()
6157 ufshcd_transfer_req_compl(hba); in ufshcd_complete_requests()
6159 ufshcd_tmc_handler(hba); in ufshcd_complete_requests()
6163 * ufshcd_quirk_dl_nac_errors - This function checks if error handling is
6165 * @hba: per-adapter instance
6169 static bool ufshcd_quirk_dl_nac_errors(struct ufs_hba *hba) in ufshcd_quirk_dl_nac_errors() argument
6174 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6179 if (hba->saved_err & (CONTROLLER_FATAL_ERROR | SYSTEM_BUS_FATAL_ERROR)) in ufshcd_quirk_dl_nac_errors()
6182 if ((hba->saved_err & DEVICE_FATAL_ERROR) || in ufshcd_quirk_dl_nac_errors()
6183 ((hba->saved_err & UIC_ERROR) && in ufshcd_quirk_dl_nac_errors()
6184 (hba->saved_uic_err & UFSHCD_UIC_DL_TCx_REPLAY_ERROR))) in ufshcd_quirk_dl_nac_errors()
6187 if ((hba->saved_err & UIC_ERROR) && in ufshcd_quirk_dl_nac_errors()
6188 (hba->saved_uic_err & UFSHCD_UIC_DL_NAC_RECEIVED_ERROR)) { in ufshcd_quirk_dl_nac_errors()
6193 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6195 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6201 if ((hba->saved_err & INT_FATAL_ERRORS) || in ufshcd_quirk_dl_nac_errors()
6202 ((hba->saved_err & UIC_ERROR) && in ufshcd_quirk_dl_nac_errors()
6203 (hba->saved_uic_err & ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR))) in ufshcd_quirk_dl_nac_errors()
6209 * - If we don't get any response then do error recovery. in ufshcd_quirk_dl_nac_errors()
6210 * - If we get response then clear the DL NAC error bit. in ufshcd_quirk_dl_nac_errors()
6213 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6214 err = ufshcd_verify_dev_init(hba); in ufshcd_quirk_dl_nac_errors()
6215 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6221 if (hba->saved_uic_err == UFSHCD_UIC_DL_NAC_RECEIVED_ERROR) in ufshcd_quirk_dl_nac_errors()
6222 hba->saved_err &= ~UIC_ERROR; in ufshcd_quirk_dl_nac_errors()
6224 hba->saved_uic_err &= ~UFSHCD_UIC_DL_NAC_RECEIVED_ERROR; in ufshcd_quirk_dl_nac_errors()
6225 if (!hba->saved_uic_err) in ufshcd_quirk_dl_nac_errors()
6229 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_quirk_dl_nac_errors()
6234 static inline bool ufshcd_is_saved_err_fatal(struct ufs_hba *hba) in ufshcd_is_saved_err_fatal() argument
6236 return (hba->saved_uic_err & UFSHCD_UIC_DL_PA_INIT_ERROR) || in ufshcd_is_saved_err_fatal()
6237 (hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)); in ufshcd_is_saved_err_fatal()
6240 void ufshcd_schedule_eh_work(struct ufs_hba *hba) in ufshcd_schedule_eh_work() argument
6242 lockdep_assert_held(hba->host->host_lock); in ufshcd_schedule_eh_work()
6245 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) { in ufshcd_schedule_eh_work()
6246 if (hba->force_reset || ufshcd_is_link_broken(hba) || in ufshcd_schedule_eh_work()
6247 ufshcd_is_saved_err_fatal(hba)) in ufshcd_schedule_eh_work()
6248 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_FATAL; in ufshcd_schedule_eh_work()
6250 hba->ufshcd_state = UFSHCD_STATE_EH_SCHEDULED_NON_FATAL; in ufshcd_schedule_eh_work()
6251 queue_work(hba->eh_wq, &hba->eh_work); in ufshcd_schedule_eh_work()
6255 static void ufshcd_force_error_recovery(struct ufs_hba *hba) in ufshcd_force_error_recovery() argument
6257 spin_lock_irq(hba->host->host_lock); in ufshcd_force_error_recovery()
6258 hba->force_reset = true; in ufshcd_force_error_recovery()
6259 ufshcd_schedule_eh_work(hba); in ufshcd_force_error_recovery()
6260 spin_unlock_irq(hba->host->host_lock); in ufshcd_force_error_recovery()
6263 static void ufshcd_clk_scaling_allow(struct ufs_hba *hba, bool allow) in ufshcd_clk_scaling_allow() argument
6265 mutex_lock(&hba->wb_mutex); in ufshcd_clk_scaling_allow()
6266 down_write(&hba->clk_scaling_lock); in ufshcd_clk_scaling_allow()
6267 hba->clk_scaling.is_allowed = allow; in ufshcd_clk_scaling_allow()
6268 up_write(&hba->clk_scaling_lock); in ufshcd_clk_scaling_allow()
6269 mutex_unlock(&hba->wb_mutex); in ufshcd_clk_scaling_allow()
6272 static void ufshcd_clk_scaling_suspend(struct ufs_hba *hba, bool suspend) in ufshcd_clk_scaling_suspend() argument
6275 if (hba->clk_scaling.is_enabled) in ufshcd_clk_scaling_suspend()
6276 ufshcd_suspend_clkscaling(hba); in ufshcd_clk_scaling_suspend()
6277 ufshcd_clk_scaling_allow(hba, false); in ufshcd_clk_scaling_suspend()
6279 ufshcd_clk_scaling_allow(hba, true); in ufshcd_clk_scaling_suspend()
6280 if (hba->clk_scaling.is_enabled) in ufshcd_clk_scaling_suspend()
6281 ufshcd_resume_clkscaling(hba); in ufshcd_clk_scaling_suspend()
6285 static void ufshcd_err_handling_prepare(struct ufs_hba *hba) in ufshcd_err_handling_prepare() argument
6287 ufshcd_rpm_get_sync(hba); in ufshcd_err_handling_prepare()
6288 if (pm_runtime_status_suspended(&hba->ufs_device_wlun->sdev_gendev) || in ufshcd_err_handling_prepare()
6289 hba->is_sys_suspended) { in ufshcd_err_handling_prepare()
6297 ufshcd_setup_hba_vreg(hba, true); in ufshcd_err_handling_prepare()
6298 ufshcd_enable_irq(hba); in ufshcd_err_handling_prepare()
6299 ufshcd_setup_vreg(hba, true); in ufshcd_err_handling_prepare()
6300 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); in ufshcd_err_handling_prepare()
6301 ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); in ufshcd_err_handling_prepare()
6302 ufshcd_hold(hba); in ufshcd_err_handling_prepare()
6303 if (!ufshcd_is_clkgating_allowed(hba)) in ufshcd_err_handling_prepare()
6304 ufshcd_setup_clocks(hba, true); in ufshcd_err_handling_prepare()
6305 pm_op = hba->is_sys_suspended ? UFS_SYSTEM_PM : UFS_RUNTIME_PM; in ufshcd_err_handling_prepare()
6306 ufshcd_vops_resume(hba, pm_op); in ufshcd_err_handling_prepare()
6308 ufshcd_hold(hba); in ufshcd_err_handling_prepare()
6309 if (ufshcd_is_clkscaling_supported(hba) && in ufshcd_err_handling_prepare()
6310 hba->clk_scaling.is_enabled) in ufshcd_err_handling_prepare()
6311 ufshcd_suspend_clkscaling(hba); in ufshcd_err_handling_prepare()
6312 ufshcd_clk_scaling_allow(hba, false); in ufshcd_err_handling_prepare()
6314 ufshcd_scsi_block_requests(hba); in ufshcd_err_handling_prepare()
6316 blk_mq_wait_quiesce_done(&hba->host->tag_set); in ufshcd_err_handling_prepare()
6317 cancel_work_sync(&hba->eeh_work); in ufshcd_err_handling_prepare()
6320 static void ufshcd_err_handling_unprepare(struct ufs_hba *hba) in ufshcd_err_handling_unprepare() argument
6322 ufshcd_scsi_unblock_requests(hba); in ufshcd_err_handling_unprepare()
6323 ufshcd_release(hba); in ufshcd_err_handling_unprepare()
6324 if (ufshcd_is_clkscaling_supported(hba)) in ufshcd_err_handling_unprepare()
6325 ufshcd_clk_scaling_suspend(hba, false); in ufshcd_err_handling_unprepare()
6326 ufshcd_rpm_put(hba); in ufshcd_err_handling_unprepare()
6329 static inline bool ufshcd_err_handling_should_stop(struct ufs_hba *hba) in ufshcd_err_handling_should_stop() argument
6331 return (!hba->is_powered || hba->shutting_down || in ufshcd_err_handling_should_stop()
6332 !hba->ufs_device_wlun || in ufshcd_err_handling_should_stop()
6333 hba->ufshcd_state == UFSHCD_STATE_ERROR || in ufshcd_err_handling_should_stop()
6334 (!(hba->saved_err || hba->saved_uic_err || hba->force_reset || in ufshcd_err_handling_should_stop()
6335 ufshcd_is_link_broken(hba)))); in ufshcd_err_handling_should_stop()
6339 static void ufshcd_recover_pm_error(struct ufs_hba *hba) in ufshcd_recover_pm_error() argument
6341 struct Scsi_Host *shost = hba->host; in ufshcd_recover_pm_error()
6346 hba->is_sys_suspended = false; in ufshcd_recover_pm_error()
6351 ret = pm_runtime_set_active(&hba->ufs_device_wlun->sdev_gendev); in ufshcd_recover_pm_error()
6353 /* hba device might have a runtime error otherwise */ in ufshcd_recover_pm_error()
6355 ret = pm_runtime_set_active(hba->dev); in ufshcd_recover_pm_error()
6364 q = sdev->request_queue; in ufshcd_recover_pm_error()
6365 if (q->dev && (q->rpm_status == RPM_SUSPENDED || in ufshcd_recover_pm_error()
6366 q->rpm_status == RPM_SUSPENDING)) in ufshcd_recover_pm_error()
6367 pm_request_resume(q->dev); in ufshcd_recover_pm_error()
6372 static inline void ufshcd_recover_pm_error(struct ufs_hba *hba) in ufshcd_recover_pm_error() argument
6377 static bool ufshcd_is_pwr_mode_restore_needed(struct ufs_hba *hba) in ufshcd_is_pwr_mode_restore_needed() argument
6379 struct ufs_pa_layer_attr *pwr_info = &hba->pwr_info; in ufshcd_is_pwr_mode_restore_needed()
6382 ufshcd_dme_get(hba, UIC_ARG_MIB(PA_PWRMODE), &mode); in ufshcd_is_pwr_mode_restore_needed()
6384 if (pwr_info->pwr_rx != ((mode >> PWRMODE_RX_OFFSET) & PWRMODE_MASK)) in ufshcd_is_pwr_mode_restore_needed()
6387 if (pwr_info->pwr_tx != (mode & PWRMODE_MASK)) in ufshcd_is_pwr_mode_restore_needed()
6396 u32 tag = rq->tag; in ufshcd_abort_one()
6398 struct scsi_device *sdev = cmd->device; in ufshcd_abort_one()
6399 struct Scsi_Host *shost = sdev->host; in ufshcd_abort_one()
6400 struct ufs_hba *hba = shost_priv(shost); in ufshcd_abort_one() local
6401 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_abort_one()
6405 *ret = ufshcd_try_to_abort_task(hba, tag); in ufshcd_abort_one()
6406 dev_err(hba->dev, "Aborting tag %d / CDB %#02x %s\n", tag, in ufshcd_abort_one()
6407 hba->lrb[tag].cmd ? hba->lrb[tag].cmd->cmnd[0] : -1, in ufshcd_abort_one()
6411 if (is_mcq_enabled(hba) && (*ret == 0)) { in ufshcd_abort_one()
6412 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd)); in ufshcd_abort_one()
6415 spin_lock_irqsave(&hwq->cq_lock, flags); in ufshcd_abort_one()
6416 if (ufshcd_cmd_inflight(lrbp->cmd)) in ufshcd_abort_one()
6417 ufshcd_release_scsi_cmd(hba, lrbp); in ufshcd_abort_one()
6418 spin_unlock_irqrestore(&hwq->cq_lock, flags); in ufshcd_abort_one()
6425 * ufshcd_abort_all - Abort all pending commands.
6426 * @hba: Host bus adapter pointer.
6430 static bool ufshcd_abort_all(struct ufs_hba *hba) in ufshcd_abort_all() argument
6434 blk_mq_tagset_busy_iter(&hba->host->tag_set, ufshcd_abort_one, &ret); in ufshcd_abort_all()
6439 for_each_set_bit(tag, &hba->outstanding_tasks, hba->nutmrs) { in ufshcd_abort_all()
6440 ret = ufshcd_clear_tm_cmd(hba, tag); in ufshcd_abort_all()
6447 ufshcd_complete_requests(hba, false); in ufshcd_abort_all()
6453 * ufshcd_err_handler - handle UFS errors that require s/w attention
6459 struct ufs_hba *hba; in ufshcd_err_handler() local
6465 hba = container_of(work, struct ufs_hba, eh_work); in ufshcd_err_handler()
6467 dev_info(hba->dev, in ufshcd_err_handler()
6468 …"%s started; HBA state %s; powered %d; shutting down %d; saved_err = %d; saved_uic_err = %d; force… in ufshcd_err_handler()
6469 __func__, ufshcd_state_name[hba->ufshcd_state], in ufshcd_err_handler()
6470 hba->is_powered, hba->shutting_down, hba->saved_err, in ufshcd_err_handler()
6471 hba->saved_uic_err, hba->force_reset, in ufshcd_err_handler()
6472 ufshcd_is_link_broken(hba) ? "; link is broken" : ""); in ufshcd_err_handler()
6474 down(&hba->host_sem); in ufshcd_err_handler()
6475 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6476 if (ufshcd_err_handling_should_stop(hba)) { in ufshcd_err_handler()
6477 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) in ufshcd_err_handler()
6478 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; in ufshcd_err_handler()
6479 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6480 up(&hba->host_sem); in ufshcd_err_handler()
6483 ufshcd_set_eh_in_progress(hba); in ufshcd_err_handler()
6484 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6485 ufshcd_err_handling_prepare(hba); in ufshcd_err_handler()
6486 /* Complete requests that have door-bell cleared by h/w */ in ufshcd_err_handler()
6487 ufshcd_complete_requests(hba, false); in ufshcd_err_handler()
6488 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6493 if (hba->ufshcd_state != UFSHCD_STATE_ERROR) in ufshcd_err_handler()
6494 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_err_handler()
6499 if (ufshcd_err_handling_should_stop(hba)) in ufshcd_err_handler()
6502 if ((hba->dev_quirks & UFS_DEVICE_QUIRK_RECOVERY_FROM_DL_NAC_ERRORS) && in ufshcd_err_handler()
6503 !hba->force_reset) { in ufshcd_err_handler()
6506 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6508 ret = ufshcd_quirk_dl_nac_errors(hba); in ufshcd_err_handler()
6509 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6510 if (!ret && ufshcd_err_handling_should_stop(hba)) in ufshcd_err_handler()
6514 if ((hba->saved_err & (INT_FATAL_ERRORS | UFSHCD_UIC_HIBERN8_MASK)) || in ufshcd_err_handler()
6515 (hba->saved_uic_err && in ufshcd_err_handler()
6516 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { in ufshcd_err_handler()
6517 bool pr_prdt = !!(hba->saved_err & SYSTEM_BUS_FATAL_ERROR); in ufshcd_err_handler()
6519 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6520 ufshcd_print_host_state(hba); in ufshcd_err_handler()
6521 ufshcd_print_pwr_info(hba); in ufshcd_err_handler()
6522 ufshcd_print_evt_hist(hba); in ufshcd_err_handler()
6523 ufshcd_print_tmrs(hba, hba->outstanding_tasks); in ufshcd_err_handler()
6524 ufshcd_print_trs_all(hba, pr_prdt); in ufshcd_err_handler()
6525 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6533 if (hba->force_reset || ufshcd_is_link_broken(hba) || in ufshcd_err_handler()
6534 ufshcd_is_saved_err_fatal(hba) || in ufshcd_err_handler()
6535 ((hba->saved_err & UIC_ERROR) && in ufshcd_err_handler()
6536 (hba->saved_uic_err & (UFSHCD_UIC_DL_NAC_RECEIVED_ERROR | in ufshcd_err_handler()
6546 if (hba->saved_uic_err & UFSHCD_UIC_PA_GENERIC_ERROR) { in ufshcd_err_handler()
6547 hba->saved_uic_err &= ~UFSHCD_UIC_PA_GENERIC_ERROR; in ufshcd_err_handler()
6548 if (!hba->saved_uic_err) in ufshcd_err_handler()
6549 hba->saved_err &= ~UIC_ERROR; in ufshcd_err_handler()
6550 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6551 if (ufshcd_is_pwr_mode_restore_needed(hba)) in ufshcd_err_handler()
6553 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6554 if (!hba->saved_err && !needs_restore) in ufshcd_err_handler()
6558 hba->silence_err_logs = true; in ufshcd_err_handler()
6560 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6562 needs_reset = ufshcd_abort_all(hba); in ufshcd_err_handler()
6564 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6565 hba->silence_err_logs = false; in ufshcd_err_handler()
6574 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6579 down_write(&hba->clk_scaling_lock); in ufshcd_err_handler()
6580 hba->force_pmc = true; in ufshcd_err_handler()
6581 pmc_err = ufshcd_config_pwr_mode(hba, &(hba->pwr_info)); in ufshcd_err_handler()
6584 dev_err(hba->dev, "%s: Failed to restore power mode, err = %d\n", in ufshcd_err_handler()
6587 hba->force_pmc = false; in ufshcd_err_handler()
6588 ufshcd_print_pwr_info(hba); in ufshcd_err_handler()
6589 up_write(&hba->clk_scaling_lock); in ufshcd_err_handler()
6590 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6598 hba->force_reset = false; in ufshcd_err_handler()
6599 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6600 err = ufshcd_reset_and_restore(hba); in ufshcd_err_handler()
6602 dev_err(hba->dev, "%s: reset and restore failed with err %d\n", in ufshcd_err_handler()
6605 ufshcd_recover_pm_error(hba); in ufshcd_err_handler()
6606 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_err_handler()
6611 if (hba->ufshcd_state == UFSHCD_STATE_RESET) in ufshcd_err_handler()
6612 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; in ufshcd_err_handler()
6613 if (hba->saved_err || hba->saved_uic_err) in ufshcd_err_handler()
6614 dev_err_ratelimited(hba->dev, "%s: exit: saved_err 0x%x saved_uic_err 0x%x", in ufshcd_err_handler()
6615 __func__, hba->saved_err, hba->saved_uic_err); in ufshcd_err_handler()
6618 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL && in ufshcd_err_handler()
6619 hba->ufshcd_state != UFSHCD_STATE_ERROR) { in ufshcd_err_handler()
6620 if (--retries) in ufshcd_err_handler()
6622 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_err_handler()
6624 ufshcd_clear_eh_in_progress(hba); in ufshcd_err_handler()
6625 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_err_handler()
6626 ufshcd_err_handling_unprepare(hba); in ufshcd_err_handler()
6627 up(&hba->host_sem); in ufshcd_err_handler()
6629 dev_info(hba->dev, "%s finished; HBA state %s\n", __func__, in ufshcd_err_handler()
6630 ufshcd_state_name[hba->ufshcd_state]); in ufshcd_err_handler()
6634 * ufshcd_update_uic_error - check and set fatal UIC error flags.
6635 * @hba: per-adapter instance
6638 * IRQ_HANDLED - If interrupt is valid
6639 * IRQ_NONE - If invalid interrupt
6641 static irqreturn_t ufshcd_update_uic_error(struct ufs_hba *hba) in ufshcd_update_uic_error() argument
6647 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_PHY_ADAPTER_LAYER); in ufshcd_update_uic_error()
6650 ufshcd_update_evt_hist(hba, UFS_EVT_PA_ERR, reg); in ufshcd_update_uic_error()
6656 dev_dbg(hba->dev, "%s: UIC Lane error reported\n", in ufshcd_update_uic_error()
6663 hba->uic_error |= UFSHCD_UIC_PA_GENERIC_ERROR; in ufshcd_update_uic_error()
6664 if (hba->uic_async_done && hba->active_uic_cmd) in ufshcd_update_uic_error()
6665 cmd = hba->active_uic_cmd; in ufshcd_update_uic_error()
6670 if (cmd && (cmd->command == UIC_CMD_DME_SET)) in ufshcd_update_uic_error()
6671 hba->uic_error &= ~UFSHCD_UIC_PA_GENERIC_ERROR; in ufshcd_update_uic_error()
6677 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DATA_LINK_LAYER); in ufshcd_update_uic_error()
6680 ufshcd_update_evt_hist(hba, UFS_EVT_DL_ERR, reg); in ufshcd_update_uic_error()
6683 hba->uic_error |= UFSHCD_UIC_DL_PA_INIT_ERROR; in ufshcd_update_uic_error()
6684 else if (hba->dev_quirks & in ufshcd_update_uic_error()
6687 hba->uic_error |= in ufshcd_update_uic_error()
6690 hba->uic_error |= UFSHCD_UIC_DL_TCx_REPLAY_ERROR; in ufshcd_update_uic_error()
6696 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_NETWORK_LAYER); in ufshcd_update_uic_error()
6699 ufshcd_update_evt_hist(hba, UFS_EVT_NL_ERR, reg); in ufshcd_update_uic_error()
6700 hba->uic_error |= UFSHCD_UIC_NL_ERROR; in ufshcd_update_uic_error()
6704 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_TRANSPORT_LAYER); in ufshcd_update_uic_error()
6707 ufshcd_update_evt_hist(hba, UFS_EVT_TL_ERR, reg); in ufshcd_update_uic_error()
6708 hba->uic_error |= UFSHCD_UIC_TL_ERROR; in ufshcd_update_uic_error()
6712 reg = ufshcd_readl(hba, REG_UIC_ERROR_CODE_DME); in ufshcd_update_uic_error()
6715 ufshcd_update_evt_hist(hba, UFS_EVT_DME_ERR, reg); in ufshcd_update_uic_error()
6716 hba->uic_error |= UFSHCD_UIC_DME_ERROR; in ufshcd_update_uic_error()
6720 dev_dbg(hba->dev, "%s: UIC error flags = 0x%08x\n", in ufshcd_update_uic_error()
6721 __func__, hba->uic_error); in ufshcd_update_uic_error()
6726 * ufshcd_check_errors - Check for errors that need s/w attention
6727 * @hba: per-adapter instance
6731 * IRQ_HANDLED - If interrupt is valid
6732 * IRQ_NONE - If invalid interrupt
6734 static irqreturn_t ufshcd_check_errors(struct ufs_hba *hba, u32 intr_status) in ufshcd_check_errors() argument
6739 spin_lock(hba->host->host_lock); in ufshcd_check_errors()
6740 hba->errors |= UFSHCD_ERROR_MASK & intr_status; in ufshcd_check_errors()
6742 if (hba->errors & INT_FATAL_ERRORS) { in ufshcd_check_errors()
6743 ufshcd_update_evt_hist(hba, UFS_EVT_FATAL_ERR, in ufshcd_check_errors()
6744 hba->errors); in ufshcd_check_errors()
6748 if (hba->errors & UIC_ERROR) { in ufshcd_check_errors()
6749 hba->uic_error = 0; in ufshcd_check_errors()
6750 retval = ufshcd_update_uic_error(hba); in ufshcd_check_errors()
6751 if (hba->uic_error) in ufshcd_check_errors()
6755 if (hba->errors & UFSHCD_UIC_HIBERN8_MASK) { in ufshcd_check_errors()
6756 dev_err(hba->dev, in ufshcd_check_errors()
6757 "%s: Auto Hibern8 %s failed - status: 0x%08x, upmcrs: 0x%08x\n", in ufshcd_check_errors()
6758 __func__, (hba->errors & UIC_HIBERNATE_ENTER) ? in ufshcd_check_errors()
6760 hba->errors, ufshcd_get_upmcrs(hba)); in ufshcd_check_errors()
6761 ufshcd_update_evt_hist(hba, UFS_EVT_AUTO_HIBERN8_ERR, in ufshcd_check_errors()
6762 hba->errors); in ufshcd_check_errors()
6763 ufshcd_set_link_broken(hba); in ufshcd_check_errors()
6772 hba->saved_err |= hba->errors; in ufshcd_check_errors()
6773 hba->saved_uic_err |= hba->uic_error; in ufshcd_check_errors()
6776 if ((hba->saved_err & in ufshcd_check_errors()
6778 (hba->saved_uic_err && in ufshcd_check_errors()
6779 (hba->saved_uic_err != UFSHCD_UIC_PA_GENERIC_ERROR))) { in ufshcd_check_errors()
6780 dev_err(hba->dev, "%s: saved_err 0x%x saved_uic_err 0x%x\n", in ufshcd_check_errors()
6781 __func__, hba->saved_err, in ufshcd_check_errors()
6782 hba->saved_uic_err); in ufshcd_check_errors()
6783 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, in ufshcd_check_errors()
6785 ufshcd_print_pwr_info(hba); in ufshcd_check_errors()
6787 ufshcd_schedule_eh_work(hba); in ufshcd_check_errors()
6791 * if (!queue_eh_work) - in ufshcd_check_errors()
6792 * Other errors are either non-fatal where host recovers in ufshcd_check_errors()
6796 hba->errors = 0; in ufshcd_check_errors()
6797 hba->uic_error = 0; in ufshcd_check_errors()
6798 spin_unlock(hba->host->host_lock); in ufshcd_check_errors()
6803 * ufshcd_tmc_handler - handle task management function completion
6804 * @hba: per adapter instance
6807 * IRQ_HANDLED - If interrupt is valid
6808 * IRQ_NONE - If invalid interrupt
6810 static irqreturn_t ufshcd_tmc_handler(struct ufs_hba *hba) in ufshcd_tmc_handler() argument
6816 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_tmc_handler()
6817 pending = ufshcd_readl(hba, REG_UTP_TASK_REQ_DOOR_BELL); in ufshcd_tmc_handler()
6818 issued = hba->outstanding_tasks & ~pending; in ufshcd_tmc_handler()
6819 for_each_set_bit(tag, &issued, hba->nutmrs) { in ufshcd_tmc_handler()
6820 struct request *req = hba->tmf_rqs[tag]; in ufshcd_tmc_handler()
6821 struct completion *c = req->end_io_data; in ufshcd_tmc_handler()
6826 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_tmc_handler()
6832 * ufshcd_handle_mcq_cq_events - handle MCQ completion queue events
6833 * @hba: per adapter instance
6837 static irqreturn_t ufshcd_handle_mcq_cq_events(struct ufs_hba *hba) in ufshcd_handle_mcq_cq_events() argument
6845 ret = ufshcd_vops_get_outstanding_cqs(hba, &outstanding_cqs); in ufshcd_handle_mcq_cq_events()
6847 outstanding_cqs = (1U << hba->nr_hw_queues) - 1; in ufshcd_handle_mcq_cq_events()
6850 nr_queues = hba->nr_hw_queues - hba->nr_queues[HCTX_TYPE_POLL]; in ufshcd_handle_mcq_cq_events()
6852 hwq = &hba->uhq[i]; in ufshcd_handle_mcq_cq_events()
6854 events = ufshcd_mcq_read_cqis(hba, i); in ufshcd_handle_mcq_cq_events()
6856 ufshcd_mcq_write_cqis(hba, events, i); in ufshcd_handle_mcq_cq_events()
6859 ufshcd_mcq_poll_cqe_lock(hba, hwq); in ufshcd_handle_mcq_cq_events()
6866 * ufshcd_sl_intr - Interrupt service routine
6867 * @hba: per adapter instance
6871 * IRQ_HANDLED - If interrupt is valid
6872 * IRQ_NONE - If invalid interrupt
6874 static irqreturn_t ufshcd_sl_intr(struct ufs_hba *hba, u32 intr_status) in ufshcd_sl_intr() argument
6879 retval |= ufshcd_uic_cmd_compl(hba, intr_status); in ufshcd_sl_intr()
6881 if (intr_status & UFSHCD_ERROR_MASK || hba->errors) in ufshcd_sl_intr()
6882 retval |= ufshcd_check_errors(hba, intr_status); in ufshcd_sl_intr()
6885 retval |= ufshcd_tmc_handler(hba); in ufshcd_sl_intr()
6888 retval |= ufshcd_transfer_req_compl(hba); in ufshcd_sl_intr()
6891 retval |= ufshcd_handle_mcq_cq_events(hba); in ufshcd_sl_intr()
6897 * ufshcd_intr - Main interrupt service routine
6902 * IRQ_HANDLED - If interrupt is valid
6903 * IRQ_NONE - If invalid interrupt
6909 struct ufs_hba *hba = __hba; in ufshcd_intr() local
6910 int retries = hba->nutrs; in ufshcd_intr()
6912 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); in ufshcd_intr()
6913 hba->ufs_stats.last_intr_status = intr_status; in ufshcd_intr()
6914 hba->ufs_stats.last_intr_ts = local_clock(); in ufshcd_intr()
6917 * There could be max of hba->nutrs reqs in flight and in worst case in ufshcd_intr()
6922 while (intr_status && retries--) { in ufshcd_intr()
6924 intr_status & ufshcd_readl(hba, REG_INTERRUPT_ENABLE); in ufshcd_intr()
6925 ufshcd_writel(hba, intr_status, REG_INTERRUPT_STATUS); in ufshcd_intr()
6927 retval |= ufshcd_sl_intr(hba, enabled_intr_status); in ufshcd_intr()
6929 intr_status = ufshcd_readl(hba, REG_INTERRUPT_STATUS); in ufshcd_intr()
6934 hba->outstanding_reqs) && !ufshcd_eh_in_progress(hba)) { in ufshcd_intr()
6935 dev_err(hba->dev, "%s: Unhandled interrupt 0x%08x (0x%08x, 0x%08x)\n", in ufshcd_intr()
6938 hba->ufs_stats.last_intr_status, in ufshcd_intr()
6940 ufshcd_dump_regs(hba, 0, UFSHCI_REG_SPACE_SIZE, "host_regs: "); in ufshcd_intr()
6946 static int ufshcd_clear_tm_cmd(struct ufs_hba *hba, int tag) in ufshcd_clear_tm_cmd() argument
6952 if (!test_bit(tag, &hba->outstanding_tasks)) in ufshcd_clear_tm_cmd()
6955 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_clear_tm_cmd()
6956 ufshcd_utmrl_clear(hba, tag); in ufshcd_clear_tm_cmd()
6957 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_clear_tm_cmd()
6960 err = ufshcd_wait_for_register(hba, in ufshcd_clear_tm_cmd()
6964 dev_err(hba->dev, "Clearing task management function with tag %d %s\n", in ufshcd_clear_tm_cmd()
6971 static int __ufshcd_issue_tm_cmd(struct ufs_hba *hba, in __ufshcd_issue_tm_cmd() argument
6974 struct request_queue *q = hba->tmf_queue; in __ufshcd_issue_tm_cmd()
6975 struct Scsi_Host *host = hba->host; in __ufshcd_issue_tm_cmd()
6988 req->end_io_data = &wait; in __ufshcd_issue_tm_cmd()
6989 ufshcd_hold(hba); in __ufshcd_issue_tm_cmd()
6991 spin_lock_irqsave(host->host_lock, flags); in __ufshcd_issue_tm_cmd()
6993 task_tag = req->tag; in __ufshcd_issue_tm_cmd()
6994 WARN_ONCE(task_tag < 0 || task_tag >= hba->nutmrs, "Invalid tag %d\n", in __ufshcd_issue_tm_cmd()
6996 hba->tmf_rqs[req->tag] = req; in __ufshcd_issue_tm_cmd()
6997 treq->upiu_req.req_header.task_tag = task_tag; in __ufshcd_issue_tm_cmd()
6999 memcpy(hba->utmrdl_base_addr + task_tag, treq, sizeof(*treq)); in __ufshcd_issue_tm_cmd()
7000 ufshcd_vops_setup_task_mgmt(hba, task_tag, tm_function); in __ufshcd_issue_tm_cmd()
7003 __set_bit(task_tag, &hba->outstanding_tasks); in __ufshcd_issue_tm_cmd()
7005 ufshcd_writel(hba, 1 << task_tag, REG_UTP_TASK_REQ_DOOR_BELL); in __ufshcd_issue_tm_cmd()
7009 spin_unlock_irqrestore(host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7011 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_SEND); in __ufshcd_issue_tm_cmd()
7017 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_ERR); in __ufshcd_issue_tm_cmd()
7018 dev_err(hba->dev, "%s: task management cmd 0x%.2x timed-out\n", in __ufshcd_issue_tm_cmd()
7020 if (ufshcd_clear_tm_cmd(hba, task_tag)) in __ufshcd_issue_tm_cmd()
7021 dev_WARN(hba->dev, "%s: unable to clear tm cmd (slot %d) after timeout\n", in __ufshcd_issue_tm_cmd()
7023 err = -ETIMEDOUT; in __ufshcd_issue_tm_cmd()
7026 memcpy(treq, hba->utmrdl_base_addr + task_tag, sizeof(*treq)); in __ufshcd_issue_tm_cmd()
7028 ufshcd_add_tm_upiu_trace(hba, task_tag, UFS_TM_COMP); in __ufshcd_issue_tm_cmd()
7031 spin_lock_irqsave(hba->host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7032 hba->tmf_rqs[req->tag] = NULL; in __ufshcd_issue_tm_cmd()
7033 __clear_bit(task_tag, &hba->outstanding_tasks); in __ufshcd_issue_tm_cmd()
7034 spin_unlock_irqrestore(hba->host->host_lock, flags); in __ufshcd_issue_tm_cmd()
7036 ufshcd_release(hba); in __ufshcd_issue_tm_cmd()
7043 * ufshcd_issue_tm_cmd - issues task management commands to controller
7044 * @hba: per adapter instance
7050 * Return: non-zero value on error, zero on success.
7052 static int ufshcd_issue_tm_cmd(struct ufs_hba *hba, int lun_id, int task_id, in ufshcd_issue_tm_cmd() argument
7075 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_function); in ufshcd_issue_tm_cmd()
7076 if (err == -ETIMEDOUT) in ufshcd_issue_tm_cmd()
7081 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", in ufshcd_issue_tm_cmd()
7090 * ufshcd_issue_devman_upiu_cmd - API for sending "utrd" type requests
7091 * @hba: per-adapter instance
7099 * Those type of requests uses UTP Transfer Request Descriptor - utrd.
7104 * the caller is expected to hold the hba->dev_cmd.lock mutex.
7108 static int ufshcd_issue_devman_upiu_cmd(struct ufs_hba *hba, in ufshcd_issue_devman_upiu_cmd() argument
7116 const u32 tag = hba->reserved_slot; in ufshcd_issue_devman_upiu_cmd()
7121 /* Protects use of hba->reserved_slot. */ in ufshcd_issue_devman_upiu_cmd()
7122 lockdep_assert_held(&hba->dev_cmd.lock); in ufshcd_issue_devman_upiu_cmd()
7124 down_read(&hba->clk_scaling_lock); in ufshcd_issue_devman_upiu_cmd()
7126 lrbp = &hba->lrb[tag]; in ufshcd_issue_devman_upiu_cmd()
7127 lrbp->cmd = NULL; in ufshcd_issue_devman_upiu_cmd()
7128 lrbp->task_tag = tag; in ufshcd_issue_devman_upiu_cmd()
7129 lrbp->lun = 0; in ufshcd_issue_devman_upiu_cmd()
7130 lrbp->intr_cmd = true; in ufshcd_issue_devman_upiu_cmd()
7132 hba->dev_cmd.type = cmd_type; in ufshcd_issue_devman_upiu_cmd()
7134 if (hba->ufs_version <= ufshci_version(1, 1)) in ufshcd_issue_devman_upiu_cmd()
7135 lrbp->command_type = UTP_CMD_TYPE_DEV_MANAGE; in ufshcd_issue_devman_upiu_cmd()
7137 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_issue_devman_upiu_cmd()
7140 req_upiu->header.task_tag = tag; in ufshcd_issue_devman_upiu_cmd()
7145 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); in ufshcd_issue_devman_upiu_cmd()
7151 memcpy(lrbp->ucd_req_ptr + 1, desc_buff, *buff_len); in ufshcd_issue_devman_upiu_cmd()
7155 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_issue_devman_upiu_cmd()
7157 hba->dev_cmd.complete = &wait; in ufshcd_issue_devman_upiu_cmd()
7159 ufshcd_add_query_upiu_trace(hba, UFS_QUERY_SEND, lrbp->ucd_req_ptr); in ufshcd_issue_devman_upiu_cmd()
7161 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); in ufshcd_issue_devman_upiu_cmd()
7163 * ignore the returning value here - ufshcd_check_query_response is in ufshcd_issue_devman_upiu_cmd()
7167 ufshcd_wait_for_dev_cmd(hba, lrbp, QUERY_REQ_TIMEOUT); in ufshcd_issue_devman_upiu_cmd()
7170 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); in ufshcd_issue_devman_upiu_cmd()
7172 u8 *descp = (u8 *)lrbp->ucd_rsp_ptr + sizeof(*rsp_upiu); in ufshcd_issue_devman_upiu_cmd()
7173 u16 resp_len = be16_to_cpu(lrbp->ucd_rsp_ptr->header in ufshcd_issue_devman_upiu_cmd()
7180 dev_warn(hba->dev, in ufshcd_issue_devman_upiu_cmd()
7184 err = -EINVAL; in ufshcd_issue_devman_upiu_cmd()
7187 ufshcd_add_query_upiu_trace(hba, err ? UFS_QUERY_ERR : UFS_QUERY_COMP, in ufshcd_issue_devman_upiu_cmd()
7188 (struct utp_upiu_req *)lrbp->ucd_rsp_ptr); in ufshcd_issue_devman_upiu_cmd()
7190 up_read(&hba->clk_scaling_lock); in ufshcd_issue_devman_upiu_cmd()
7195 * ufshcd_exec_raw_upiu_cmd - API function for sending raw upiu commands
7196 * @hba: per-adapter instance
7198 * @rsp_upiu: upiu reply - only 8 DW as we do not support scsi commands
7211 int ufshcd_exec_raw_upiu_cmd(struct ufs_hba *hba, in ufshcd_exec_raw_upiu_cmd() argument
7222 u8 tm_f = req_upiu->header.tm_function; in ufshcd_exec_raw_upiu_cmd()
7229 ufshcd_hold(hba); in ufshcd_exec_raw_upiu_cmd()
7230 mutex_lock(&hba->dev_cmd.lock); in ufshcd_exec_raw_upiu_cmd()
7231 err = ufshcd_issue_devman_upiu_cmd(hba, req_upiu, rsp_upiu, in ufshcd_exec_raw_upiu_cmd()
7234 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_exec_raw_upiu_cmd()
7235 ufshcd_release(hba); in ufshcd_exec_raw_upiu_cmd()
7244 err = __ufshcd_issue_tm_cmd(hba, &treq, tm_f); in ufshcd_exec_raw_upiu_cmd()
7245 if (err == -ETIMEDOUT) in ufshcd_exec_raw_upiu_cmd()
7250 dev_err(hba->dev, "%s: failed, ocs = 0x%x\n", __func__, in ufshcd_exec_raw_upiu_cmd()
7259 err = -EINVAL; in ufshcd_exec_raw_upiu_cmd()
7268 * ufshcd_advanced_rpmb_req_handler - handle advanced RPMB request
7269 * @hba: per adapter instance
7278 * Return: zero on success, non-zero on failure.
7280 int ufshcd_advanced_rpmb_req_handler(struct ufs_hba *hba, struct utp_upiu_req *req_upiu, in ufshcd_advanced_rpmb_req_handler() argument
7286 const u32 tag = hba->reserved_slot; in ufshcd_advanced_rpmb_req_handler()
7294 /* Protects use of hba->reserved_slot. */ in ufshcd_advanced_rpmb_req_handler()
7295 ufshcd_hold(hba); in ufshcd_advanced_rpmb_req_handler()
7296 mutex_lock(&hba->dev_cmd.lock); in ufshcd_advanced_rpmb_req_handler()
7297 down_read(&hba->clk_scaling_lock); in ufshcd_advanced_rpmb_req_handler()
7299 lrbp = &hba->lrb[tag]; in ufshcd_advanced_rpmb_req_handler()
7300 lrbp->cmd = NULL; in ufshcd_advanced_rpmb_req_handler()
7301 lrbp->task_tag = tag; in ufshcd_advanced_rpmb_req_handler()
7302 lrbp->lun = UFS_UPIU_RPMB_WLUN; in ufshcd_advanced_rpmb_req_handler()
7304 lrbp->intr_cmd = true; in ufshcd_advanced_rpmb_req_handler()
7306 hba->dev_cmd.type = DEV_CMD_TYPE_RPMB; in ufshcd_advanced_rpmb_req_handler()
7309 lrbp->command_type = UTP_CMD_TYPE_UFS_STORAGE; in ufshcd_advanced_rpmb_req_handler()
7316 if (hba->capabilities & MASK_EHSLUTRD_SUPPORTED) in ufshcd_advanced_rpmb_req_handler()
7322 req_upiu->header.task_tag = tag; in ufshcd_advanced_rpmb_req_handler()
7325 memcpy(lrbp->ucd_req_ptr, req_upiu, sizeof(*lrbp->ucd_req_ptr)); in ufshcd_advanced_rpmb_req_handler()
7327 memcpy(lrbp->ucd_req_ptr + 1, req_ehs, sizeof(*req_ehs)); in ufshcd_advanced_rpmb_req_handler()
7330 ufshcd_sgl_to_prdt(hba, lrbp, sg_cnt, sg_list); in ufshcd_advanced_rpmb_req_handler()
7332 memset(lrbp->ucd_rsp_ptr, 0, sizeof(struct utp_upiu_rsp)); in ufshcd_advanced_rpmb_req_handler()
7334 hba->dev_cmd.complete = &wait; in ufshcd_advanced_rpmb_req_handler()
7336 ufshcd_send_command(hba, tag, hba->dev_cmd_queue); in ufshcd_advanced_rpmb_req_handler()
7338 err = ufshcd_wait_for_dev_cmd(hba, lrbp, ADVANCED_RPMB_REQ_TIMEOUT); in ufshcd_advanced_rpmb_req_handler()
7342 memcpy(rsp_upiu, lrbp->ucd_rsp_ptr, sizeof(*rsp_upiu)); in ufshcd_advanced_rpmb_req_handler()
7344 result = (lrbp->ucd_rsp_ptr->header.response << 8) | in ufshcd_advanced_rpmb_req_handler()
7345 lrbp->ucd_rsp_ptr->header.status; in ufshcd_advanced_rpmb_req_handler()
7347 ehs_len = lrbp->ucd_rsp_ptr->header.ehs_length; in ufshcd_advanced_rpmb_req_handler()
7358 ehs_data = (u8 *)lrbp->ucd_rsp_ptr + EHS_OFFSET_IN_RESPONSE; in ufshcd_advanced_rpmb_req_handler()
7363 up_read(&hba->clk_scaling_lock); in ufshcd_advanced_rpmb_req_handler()
7364 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_advanced_rpmb_req_handler()
7365 ufshcd_release(hba); in ufshcd_advanced_rpmb_req_handler()
7370 * ufshcd_eh_device_reset_handler() - Reset a single logical unit.
7379 struct ufs_hba *hba; in ufshcd_eh_device_reset_handler() local
7386 host = cmd->device->host; in ufshcd_eh_device_reset_handler()
7387 hba = shost_priv(host); in ufshcd_eh_device_reset_handler()
7389 lun = ufshcd_scsi_to_upiu_lun(cmd->device->lun); in ufshcd_eh_device_reset_handler()
7390 err = ufshcd_issue_tm_cmd(hba, lun, 0, UFS_LOGICAL_RESET, &resp); in ufshcd_eh_device_reset_handler()
7397 if (is_mcq_enabled(hba)) { in ufshcd_eh_device_reset_handler()
7398 for (pos = 0; pos < hba->nutrs; pos++) { in ufshcd_eh_device_reset_handler()
7399 lrbp = &hba->lrb[pos]; in ufshcd_eh_device_reset_handler()
7400 if (ufshcd_cmd_inflight(lrbp->cmd) && in ufshcd_eh_device_reset_handler()
7401 lrbp->lun == lun) { in ufshcd_eh_device_reset_handler()
7402 ufshcd_clear_cmd(hba, pos); in ufshcd_eh_device_reset_handler()
7403 hwq = ufshcd_mcq_req_to_hwq(hba, scsi_cmd_to_rq(lrbp->cmd)); in ufshcd_eh_device_reset_handler()
7404 ufshcd_mcq_poll_cqe_lock(hba, hwq); in ufshcd_eh_device_reset_handler()
7412 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7413 for_each_set_bit(pos, &hba->outstanding_reqs, hba->nutrs) in ufshcd_eh_device_reset_handler()
7414 if (hba->lrb[pos].lun == lun) in ufshcd_eh_device_reset_handler()
7416 hba->outstanding_reqs &= ~pending_reqs; in ufshcd_eh_device_reset_handler()
7417 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7419 for_each_set_bit(pos, &pending_reqs, hba->nutrs) { in ufshcd_eh_device_reset_handler()
7420 if (ufshcd_clear_cmd(hba, pos) < 0) { in ufshcd_eh_device_reset_handler()
7421 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7423 ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); in ufshcd_eh_device_reset_handler()
7424 hba->outstanding_reqs |= not_cleared; in ufshcd_eh_device_reset_handler()
7426 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_eh_device_reset_handler()
7428 dev_err(hba->dev, "%s: failed to clear request %d\n", in ufshcd_eh_device_reset_handler()
7432 __ufshcd_transfer_req_compl(hba, pending_reqs & ~not_cleared_mask); in ufshcd_eh_device_reset_handler()
7435 hba->req_abort_count = 0; in ufshcd_eh_device_reset_handler()
7436 ufshcd_update_evt_hist(hba, UFS_EVT_DEV_RESET, (u32)err); in ufshcd_eh_device_reset_handler()
7440 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); in ufshcd_eh_device_reset_handler()
7446 static void ufshcd_set_req_abort_skip(struct ufs_hba *hba, unsigned long bitmap) in ufshcd_set_req_abort_skip() argument
7451 for_each_set_bit(tag, &bitmap, hba->nutrs) { in ufshcd_set_req_abort_skip()
7452 lrbp = &hba->lrb[tag]; in ufshcd_set_req_abort_skip()
7453 lrbp->req_abort_skip = true; in ufshcd_set_req_abort_skip()
7458 * ufshcd_try_to_abort_task - abort a specific task
7459 * @hba: Pointer to adapter instance
7463 * command, and in host controller by clearing the door-bell register. There can
7468 * Return: zero on success, non-zero on failure.
7470 int ufshcd_try_to_abort_task(struct ufs_hba *hba, int tag) in ufshcd_try_to_abort_task() argument
7472 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_try_to_abort_task()
7478 for (poll_cnt = 100; poll_cnt; poll_cnt--) { in ufshcd_try_to_abort_task()
7479 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, in ufshcd_try_to_abort_task()
7483 dev_err(hba->dev, "%s: cmd pending in the device. tag = %d\n", in ufshcd_try_to_abort_task()
7491 dev_err(hba->dev, "%s: cmd at tag %d not pending in the device.\n", in ufshcd_try_to_abort_task()
7493 if (is_mcq_enabled(hba)) { in ufshcd_try_to_abort_task()
7495 if (ufshcd_cmd_inflight(lrbp->cmd)) { in ufshcd_try_to_abort_task()
7501 dev_err(hba->dev, "%s: cmd at tag=%d is cleared.\n", in ufshcd_try_to_abort_task()
7507 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); in ufshcd_try_to_abort_task()
7514 dev_err(hba->dev, "%s: cmd at tag %d successfully cleared from DB.\n", in ufshcd_try_to_abort_task()
7518 dev_err(hba->dev, in ufshcd_try_to_abort_task()
7528 err = -EBUSY; in ufshcd_try_to_abort_task()
7532 err = ufshcd_issue_tm_cmd(hba, lrbp->lun, lrbp->task_tag, in ufshcd_try_to_abort_task()
7537 dev_err(hba->dev, "%s: issued. tag = %d, err %d\n", in ufshcd_try_to_abort_task()
7543 err = ufshcd_clear_cmd(hba, tag); in ufshcd_try_to_abort_task()
7545 dev_err(hba->dev, "%s: Failed clearing cmd at tag %d, err %d\n", in ufshcd_try_to_abort_task()
7553 * ufshcd_abort - scsi host template eh_abort_handler callback
7560 struct Scsi_Host *host = cmd->device->host; in ufshcd_abort()
7561 struct ufs_hba *hba = shost_priv(host); in ufshcd_abort() local
7562 int tag = scsi_cmd_to_rq(cmd)->tag; in ufshcd_abort()
7563 struct ufshcd_lrb *lrbp = &hba->lrb[tag]; in ufshcd_abort()
7571 ufshcd_hold(hba); in ufshcd_abort()
7573 if (!is_mcq_enabled(hba)) { in ufshcd_abort()
7574 reg = ufshcd_readl(hba, REG_UTP_TRANSFER_REQ_DOOR_BELL); in ufshcd_abort()
7575 if (!test_bit(tag, &hba->outstanding_reqs)) { in ufshcd_abort()
7577 dev_err(hba->dev, in ufshcd_abort()
7579 __func__, tag, hba->outstanding_reqs, reg); in ufshcd_abort()
7585 dev_info(hba->dev, "%s: Device abort task at tag %d\n", __func__, tag); in ufshcd_abort()
7595 if (!hba->req_abort_count) { in ufshcd_abort()
7596 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, tag); in ufshcd_abort()
7597 ufshcd_print_evt_hist(hba); in ufshcd_abort()
7598 ufshcd_print_host_state(hba); in ufshcd_abort()
7599 ufshcd_print_pwr_info(hba); in ufshcd_abort()
7600 ufshcd_print_tr(hba, tag, true); in ufshcd_abort()
7602 ufshcd_print_tr(hba, tag, false); in ufshcd_abort()
7604 hba->req_abort_count++; in ufshcd_abort()
7606 if (!is_mcq_enabled(hba) && !(reg & (1 << tag))) { in ufshcd_abort()
7608 dev_err(hba->dev, in ufshcd_abort()
7611 __ufshcd_transfer_req_compl(hba, 1UL << tag); in ufshcd_abort()
7616 * Task abort to the device W-LUN is illegal. When this command in ufshcd_abort()
7620 * the lrb taken by this cmd and re-set it in outstanding_reqs, in ufshcd_abort()
7623 if (lrbp->lun == UFS_UPIU_UFS_DEVICE_WLUN) { in ufshcd_abort()
7624 ufshcd_update_evt_hist(hba, UFS_EVT_ABORT, lrbp->lun); in ufshcd_abort()
7626 spin_lock_irqsave(host->host_lock, flags); in ufshcd_abort()
7627 hba->force_reset = true; in ufshcd_abort()
7628 ufshcd_schedule_eh_work(hba); in ufshcd_abort()
7629 spin_unlock_irqrestore(host->host_lock, flags); in ufshcd_abort()
7633 if (is_mcq_enabled(hba)) { in ufshcd_abort()
7640 if (lrbp->req_abort_skip) { in ufshcd_abort()
7641 dev_err(hba->dev, "%s: skipping abort\n", __func__); in ufshcd_abort()
7642 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); in ufshcd_abort()
7646 err = ufshcd_try_to_abort_task(hba, tag); in ufshcd_abort()
7648 dev_err(hba->dev, "%s: failed with err %d\n", __func__, err); in ufshcd_abort()
7649 ufshcd_set_req_abort_skip(hba, hba->outstanding_reqs); in ufshcd_abort()
7658 spin_lock_irqsave(&hba->outstanding_lock, flags); in ufshcd_abort()
7659 outstanding = __test_and_clear_bit(tag, &hba->outstanding_reqs); in ufshcd_abort()
7660 spin_unlock_irqrestore(&hba->outstanding_lock, flags); in ufshcd_abort()
7663 ufshcd_release_scsi_cmd(hba, lrbp); in ufshcd_abort()
7669 ufshcd_release(hba); in ufshcd_abort()
7674 * ufshcd_host_reset_and_restore - reset and restore host controller
7675 * @hba: per-adapter instance
7678 * local and remote (device) Uni-Pro stack and the attributes
7681 * Return: zero on success, non-zero on failure.
7683 static int ufshcd_host_reset_and_restore(struct ufs_hba *hba) in ufshcd_host_reset_and_restore() argument
7691 ufshcd_hba_stop(hba); in ufshcd_host_reset_and_restore()
7692 hba->silence_err_logs = true; in ufshcd_host_reset_and_restore()
7693 ufshcd_complete_requests(hba, true); in ufshcd_host_reset_and_restore()
7694 hba->silence_err_logs = false; in ufshcd_host_reset_and_restore()
7697 ufshcd_scale_clks(hba, true); in ufshcd_host_reset_and_restore()
7699 err = ufshcd_hba_enable(hba); in ufshcd_host_reset_and_restore()
7703 err = ufshcd_probe_hba(hba, false); in ufshcd_host_reset_and_restore()
7706 dev_err(hba->dev, "%s: Host init failed %d\n", __func__, err); in ufshcd_host_reset_and_restore()
7707 ufshcd_update_evt_hist(hba, UFS_EVT_HOST_RESET, (u32)err); in ufshcd_host_reset_and_restore()
7712 * ufshcd_reset_and_restore - reset and re-initialize host/device
7713 * @hba: per-adapter instance
7715 * Reset and recover device, host and re-establish link. This
7718 * Return: zero on success, non-zero on failure.
7720 static int ufshcd_reset_and_restore(struct ufs_hba *hba) in ufshcd_reset_and_restore() argument
7728 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7734 saved_err |= hba->saved_err; in ufshcd_reset_and_restore()
7735 saved_uic_err |= hba->saved_uic_err; in ufshcd_reset_and_restore()
7736 hba->saved_err = 0; in ufshcd_reset_and_restore()
7737 hba->saved_uic_err = 0; in ufshcd_reset_and_restore()
7738 hba->force_reset = false; in ufshcd_reset_and_restore()
7739 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_reset_and_restore()
7740 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7743 ufshcd_device_reset(hba); in ufshcd_reset_and_restore()
7745 err = ufshcd_host_reset_and_restore(hba); in ufshcd_reset_and_restore()
7747 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7751 if (hba->ufshcd_state != UFSHCD_STATE_OPERATIONAL && in ufshcd_reset_and_restore()
7752 hba->ufshcd_state != UFSHCD_STATE_ERROR && in ufshcd_reset_and_restore()
7753 hba->ufshcd_state != UFSHCD_STATE_EH_SCHEDULED_NON_FATAL) in ufshcd_reset_and_restore()
7754 err = -EAGAIN; in ufshcd_reset_and_restore()
7755 } while (err && --retries); in ufshcd_reset_and_restore()
7758 * Inform scsi mid-layer that we did reset and allow to handle in ufshcd_reset_and_restore()
7761 scsi_report_bus_reset(hba->host, 0); in ufshcd_reset_and_restore()
7763 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_reset_and_restore()
7764 hba->saved_err |= saved_err; in ufshcd_reset_and_restore()
7765 hba->saved_uic_err |= saved_uic_err; in ufshcd_reset_and_restore()
7767 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_reset_and_restore()
7773 * ufshcd_eh_host_reset_handler - host reset handler registered to scsi layer
7782 struct ufs_hba *hba; in ufshcd_eh_host_reset_handler() local
7784 hba = shost_priv(cmd->device->host); in ufshcd_eh_host_reset_handler()
7786 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7787 hba->force_reset = true; in ufshcd_eh_host_reset_handler()
7788 ufshcd_schedule_eh_work(hba); in ufshcd_eh_host_reset_handler()
7789 dev_err(hba->dev, "%s: reset in progress - 1\n", __func__); in ufshcd_eh_host_reset_handler()
7790 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7792 flush_work(&hba->eh_work); in ufshcd_eh_host_reset_handler()
7794 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7795 if (hba->ufshcd_state == UFSHCD_STATE_ERROR) in ufshcd_eh_host_reset_handler()
7797 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_eh_host_reset_handler()
7803 * ufshcd_get_max_icc_level - calculate the ICC level
7818 for (i = start_scan; i >= 0; i--) { in ufshcd_get_max_icc_level()
7849 * ufshcd_find_max_sup_active_icc_level - calculate the max ICC level
7851 * @hba: per-adapter instance
7856 static u32 ufshcd_find_max_sup_active_icc_level(struct ufs_hba *hba, in ufshcd_find_max_sup_active_icc_level() argument
7861 if (!hba->vreg_info.vcc || !hba->vreg_info.vccq || in ufshcd_find_max_sup_active_icc_level()
7862 !hba->vreg_info.vccq2) { in ufshcd_find_max_sup_active_icc_level()
7865 * never-ending cycles of messages written back to storage by in ufshcd_find_max_sup_active_icc_level()
7869 dev_dbg(hba->dev, in ufshcd_find_max_sup_active_icc_level()
7875 if (hba->vreg_info.vcc->max_uA) in ufshcd_find_max_sup_active_icc_level()
7877 hba->vreg_info.vcc->max_uA, in ufshcd_find_max_sup_active_icc_level()
7878 POWER_DESC_MAX_ACTV_ICC_LVLS - 1, in ufshcd_find_max_sup_active_icc_level()
7881 if (hba->vreg_info.vccq->max_uA) in ufshcd_find_max_sup_active_icc_level()
7883 hba->vreg_info.vccq->max_uA, in ufshcd_find_max_sup_active_icc_level()
7887 if (hba->vreg_info.vccq2->max_uA) in ufshcd_find_max_sup_active_icc_level()
7889 hba->vreg_info.vccq2->max_uA, in ufshcd_find_max_sup_active_icc_level()
7896 static void ufshcd_set_active_icc_lvl(struct ufs_hba *hba) in ufshcd_set_active_icc_lvl() argument
7906 ret = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_POWER, 0, 0, in ufshcd_set_active_icc_lvl()
7909 dev_err(hba->dev, in ufshcd_set_active_icc_lvl()
7915 icc_level = ufshcd_find_max_sup_active_icc_level(hba, desc_buf); in ufshcd_set_active_icc_lvl()
7916 dev_dbg(hba->dev, "%s: setting icc_level 0x%x", __func__, icc_level); in ufshcd_set_active_icc_lvl()
7918 ret = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, in ufshcd_set_active_icc_lvl()
7922 dev_err(hba->dev, in ufshcd_set_active_icc_lvl()
7933 blk_pm_runtime_init(sdev->request_queue, &sdev->sdev_gendev); in ufshcd_blk_pm_runtime_init()
7934 if (sdev->rpm_autosuspend) in ufshcd_blk_pm_runtime_init()
7935 pm_runtime_set_autosuspend_delay(&sdev->sdev_gendev, in ufshcd_blk_pm_runtime_init()
7941 * ufshcd_scsi_add_wlus - Adds required W-LUs
7942 * @hba: per-adapter instance
7963 * Return: zero on success (all required W-LUs are added successfully),
7964 * non-zero error value on failure (if failed to add any of the required W-LU).
7966 static int ufshcd_scsi_add_wlus(struct ufs_hba *hba) in ufshcd_scsi_add_wlus() argument
7971 hba->ufs_device_wlun = __scsi_add_device(hba->host, 0, 0, in ufshcd_scsi_add_wlus()
7973 if (IS_ERR(hba->ufs_device_wlun)) { in ufshcd_scsi_add_wlus()
7974 ret = PTR_ERR(hba->ufs_device_wlun); in ufshcd_scsi_add_wlus()
7975 hba->ufs_device_wlun = NULL; in ufshcd_scsi_add_wlus()
7978 scsi_device_put(hba->ufs_device_wlun); in ufshcd_scsi_add_wlus()
7980 sdev_rpmb = __scsi_add_device(hba->host, 0, 0, in ufshcd_scsi_add_wlus()
7989 sdev_boot = __scsi_add_device(hba->host, 0, 0, in ufshcd_scsi_add_wlus()
7992 dev_err(hba->dev, "%s: BOOT WLUN not found\n", __func__); in ufshcd_scsi_add_wlus()
8000 scsi_remove_device(hba->ufs_device_wlun); in ufshcd_scsi_add_wlus()
8005 static void ufshcd_wb_probe(struct ufs_hba *hba, const u8 *desc_buf) in ufshcd_wb_probe() argument
8007 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_wb_probe()
8012 if (!ufshcd_is_wb_allowed(hba)) in ufshcd_wb_probe()
8016 * Probe WB only for UFS-2.2 and UFS-3.1 (and later) devices or in ufshcd_wb_probe()
8020 if (!(dev_info->wspecversion >= 0x310 || in ufshcd_wb_probe()
8021 dev_info->wspecversion == 0x220 || in ufshcd_wb_probe()
8022 (hba->dev_quirks & UFS_DEVICE_QUIRK_SUPPORT_EXTENDED_FEATURES))) in ufshcd_wb_probe()
8036 dev_info->wb_buffer_type = desc_buf[DEVICE_DESC_PARAM_WB_TYPE]; in ufshcd_wb_probe()
8038 dev_info->b_presrv_uspc_en = in ufshcd_wb_probe()
8041 if (dev_info->wb_buffer_type == WB_BUF_MODE_SHARED) { in ufshcd_wb_probe()
8048 ufshcd_read_unit_desc_param(hba, in ufshcd_wb_probe()
8054 dev_info->wb_dedicated_lu = lun; in ufshcd_wb_probe()
8063 if (!ufshcd_is_wb_buf_lifetime_available(hba)) in ufshcd_wb_probe()
8069 hba->caps &= ~UFSHCD_CAP_WB_EN; in ufshcd_wb_probe()
8072 static void ufshcd_temp_notif_probe(struct ufs_hba *hba, const u8 *desc_buf) in ufshcd_temp_notif_probe() argument
8074 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_temp_notif_probe()
8078 if (!(hba->caps & UFSHCD_CAP_TEMP_NOTIF) || dev_info->wspecversion < 0x300) in ufshcd_temp_notif_probe()
8090 ufshcd_enable_ee(hba, mask); in ufshcd_temp_notif_probe()
8091 ufs_hwmon_probe(hba, mask); in ufshcd_temp_notif_probe()
8095 static void ufshcd_ext_iid_probe(struct ufs_hba *hba, u8 *desc_buf) in ufshcd_ext_iid_probe() argument
8097 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_ext_iid_probe()
8102 /* Only UFS-4.0 and above may support EXT_IID */ in ufshcd_ext_iid_probe()
8103 if (dev_info->wspecversion < 0x400) in ufshcd_ext_iid_probe()
8111 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_ext_iid_probe()
8114 dev_err(hba->dev, "failed reading bEXTIIDEn. err = %d\n", err); in ufshcd_ext_iid_probe()
8117 dev_info->b_ext_iid_en = ext_iid_en; in ufshcd_ext_iid_probe()
8120 void ufshcd_fixup_dev_quirks(struct ufs_hba *hba, in ufshcd_fixup_dev_quirks() argument
8124 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_fixup_dev_quirks()
8129 for (f = fixups; f->quirk; f++) { in ufshcd_fixup_dev_quirks()
8130 if ((f->wmanufacturerid == dev_info->wmanufacturerid || in ufshcd_fixup_dev_quirks()
8131 f->wmanufacturerid == UFS_ANY_VENDOR) && in ufshcd_fixup_dev_quirks()
8132 ((dev_info->model && in ufshcd_fixup_dev_quirks()
8133 STR_PRFX_EQUAL(f->model, dev_info->model)) || in ufshcd_fixup_dev_quirks()
8134 !strcmp(f->model, UFS_ANY_MODEL))) in ufshcd_fixup_dev_quirks()
8135 hba->dev_quirks |= f->quirk; in ufshcd_fixup_dev_quirks()
8140 static void ufs_fixup_device_setup(struct ufs_hba *hba) in ufs_fixup_device_setup() argument
8143 ufshcd_fixup_dev_quirks(hba, ufs_fixups); in ufs_fixup_device_setup()
8146 ufshcd_vops_fixup_dev_quirks(hba); in ufs_fixup_device_setup()
8149 static int ufs_get_device_desc(struct ufs_hba *hba) in ufs_get_device_desc() argument
8154 struct ufs_dev_info *dev_info = &hba->dev_info; in ufs_get_device_desc()
8158 err = -ENOMEM; in ufs_get_device_desc()
8162 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_DEVICE, 0, 0, desc_buf, in ufs_get_device_desc()
8165 dev_err(hba->dev, "%s: Failed reading Device Desc. err = %d\n", in ufs_get_device_desc()
8174 dev_info->wmanufacturerid = desc_buf[DEVICE_DESC_PARAM_MANF_ID] << 8 | in ufs_get_device_desc()
8178 dev_info->wspecversion = desc_buf[DEVICE_DESC_PARAM_SPEC_VER] << 8 | in ufs_get_device_desc()
8180 dev_info->bqueuedepth = desc_buf[DEVICE_DESC_PARAM_Q_DPTH]; in ufs_get_device_desc()
8184 err = ufshcd_read_string_desc(hba, model_index, in ufs_get_device_desc()
8185 &dev_info->model, SD_ASCII_STD); in ufs_get_device_desc()
8187 dev_err(hba->dev, "%s: Failed reading Product Name. err = %d\n", in ufs_get_device_desc()
8192 hba->luns_avail = desc_buf[DEVICE_DESC_PARAM_NUM_LU] + in ufs_get_device_desc()
8195 ufs_fixup_device_setup(hba); in ufs_get_device_desc()
8197 ufshcd_wb_probe(hba, desc_buf); in ufs_get_device_desc()
8199 ufshcd_temp_notif_probe(hba, desc_buf); in ufs_get_device_desc()
8201 if (hba->ext_iid_sup) in ufs_get_device_desc()
8202 ufshcd_ext_iid_probe(hba, desc_buf); in ufs_get_device_desc()
8215 static void ufs_put_device_desc(struct ufs_hba *hba) in ufs_put_device_desc() argument
8217 struct ufs_dev_info *dev_info = &hba->dev_info; in ufs_put_device_desc()
8219 kfree(dev_info->model); in ufs_put_device_desc()
8220 dev_info->model = NULL; in ufs_put_device_desc()
8224 * ufshcd_tune_pa_tactivate - Tunes PA_TActivate of local UniPro
8225 * @hba: per-adapter instance
8228 * 1.61. PA_TActivate needs to be greater than or equal to peerM-PHY's
8232 * Return: zero on success, non-zero error value on failure.
8234 static int ufshcd_tune_pa_tactivate(struct ufs_hba *hba) in ufshcd_tune_pa_tactivate() argument
8239 ret = ufshcd_dme_peer_get(hba, in ufshcd_tune_pa_tactivate()
8251 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), in ufshcd_tune_pa_tactivate()
8259 * ufshcd_tune_pa_hibern8time - Tunes PA_Hibern8Time of local UniPro
8260 * @hba: per-adapter instance
8263 * 1.61. PA_Hibern8Time needs to be maximum of local M-PHY's
8264 * TX_HIBERN8TIME_CAPABILITY & peer M-PHY's RX_HIBERN8TIME_CAPABILITY.
8267 * Return: zero on success, non-zero error value on failure.
8269 static int ufshcd_tune_pa_hibern8time(struct ufs_hba *hba) in ufshcd_tune_pa_hibern8time() argument
8275 ret = ufshcd_dme_get(hba, in ufshcd_tune_pa_hibern8time()
8282 ret = ufshcd_dme_peer_get(hba, in ufshcd_tune_pa_hibern8time()
8294 ret = ufshcd_dme_set(hba, UIC_ARG_MIB(PA_HIBERN8TIME), in ufshcd_tune_pa_hibern8time()
8301 * ufshcd_quirk_tune_host_pa_tactivate - Ensures that host PA_TACTIVATE is
8303 * @hba: per-adapter instance
8309 * Return: zero on success, non-zero error value on failure.
8311 static int ufshcd_quirk_tune_host_pa_tactivate(struct ufs_hba *hba) in ufshcd_quirk_tune_host_pa_tactivate() argument
8319 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_GRANULARITY), in ufshcd_quirk_tune_host_pa_tactivate()
8324 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_GRANULARITY), in ufshcd_quirk_tune_host_pa_tactivate()
8331 dev_err(hba->dev, "%s: invalid host PA_GRANULARITY %d", in ufshcd_quirk_tune_host_pa_tactivate()
8333 return -EINVAL; in ufshcd_quirk_tune_host_pa_tactivate()
8338 dev_err(hba->dev, "%s: invalid device PA_GRANULARITY %d", in ufshcd_quirk_tune_host_pa_tactivate()
8340 return -EINVAL; in ufshcd_quirk_tune_host_pa_tactivate()
8343 ret = ufshcd_dme_get(hba, UIC_ARG_MIB(PA_TACTIVATE), &pa_tactivate); in ufshcd_quirk_tune_host_pa_tactivate()
8347 ret = ufshcd_dme_peer_get(hba, UIC_ARG_MIB(PA_TACTIVATE), in ufshcd_quirk_tune_host_pa_tactivate()
8352 pa_tactivate_us = pa_tactivate * gran_to_us_table[granularity - 1]; in ufshcd_quirk_tune_host_pa_tactivate()
8354 gran_to_us_table[peer_granularity - 1]; in ufshcd_quirk_tune_host_pa_tactivate()
8360 gran_to_us_table[peer_granularity - 1]; in ufshcd_quirk_tune_host_pa_tactivate()
8362 ret = ufshcd_dme_peer_set(hba, UIC_ARG_MIB(PA_TACTIVATE), in ufshcd_quirk_tune_host_pa_tactivate()
8370 static void ufshcd_tune_unipro_params(struct ufs_hba *hba) in ufshcd_tune_unipro_params() argument
8372 if (ufshcd_is_unipro_pa_params_tuning_req(hba)) { in ufshcd_tune_unipro_params()
8373 ufshcd_tune_pa_tactivate(hba); in ufshcd_tune_unipro_params()
8374 ufshcd_tune_pa_hibern8time(hba); in ufshcd_tune_unipro_params()
8377 ufshcd_vops_apply_dev_quirks(hba); in ufshcd_tune_unipro_params()
8379 if (hba->dev_quirks & UFS_DEVICE_QUIRK_PA_TACTIVATE) in ufshcd_tune_unipro_params()
8381 ufshcd_dme_set(hba, UIC_ARG_MIB(PA_TACTIVATE), 10); in ufshcd_tune_unipro_params()
8383 if (hba->dev_quirks & UFS_DEVICE_QUIRK_HOST_PA_TACTIVATE) in ufshcd_tune_unipro_params()
8384 ufshcd_quirk_tune_host_pa_tactivate(hba); in ufshcd_tune_unipro_params()
8387 static void ufshcd_clear_dbg_ufs_stats(struct ufs_hba *hba) in ufshcd_clear_dbg_ufs_stats() argument
8389 hba->ufs_stats.hibern8_exit_cnt = 0; in ufshcd_clear_dbg_ufs_stats()
8390 hba->ufs_stats.last_hibern8_exit_tstamp = ktime_set(0, 0); in ufshcd_clear_dbg_ufs_stats()
8391 hba->req_abort_count = 0; in ufshcd_clear_dbg_ufs_stats()
8394 static int ufshcd_device_geo_params_init(struct ufs_hba *hba) in ufshcd_device_geo_params_init() argument
8401 err = -ENOMEM; in ufshcd_device_geo_params_init()
8405 err = ufshcd_read_desc_param(hba, QUERY_DESC_IDN_GEOMETRY, 0, 0, in ufshcd_device_geo_params_init()
8408 dev_err(hba->dev, "%s: Failed reading Geometry Desc. err = %d\n", in ufshcd_device_geo_params_init()
8414 hba->dev_info.max_lu_supported = 32; in ufshcd_device_geo_params_init()
8416 hba->dev_info.max_lu_supported = 8; in ufshcd_device_geo_params_init()
8448 void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk) in ufshcd_parse_dev_ref_clk_freq() argument
8454 hba->dev_ref_clk_freq = in ufshcd_parse_dev_ref_clk_freq()
8457 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) in ufshcd_parse_dev_ref_clk_freq()
8458 dev_err(hba->dev, in ufshcd_parse_dev_ref_clk_freq()
8462 static int ufshcd_set_dev_ref_clk(struct ufs_hba *hba) in ufshcd_set_dev_ref_clk() argument
8466 u32 freq = hba->dev_ref_clk_freq; in ufshcd_set_dev_ref_clk()
8468 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_READ_ATTR, in ufshcd_set_dev_ref_clk()
8472 dev_err(hba->dev, "failed reading bRefClkFreq. err = %d\n", in ufshcd_set_dev_ref_clk()
8480 err = ufshcd_query_attr_retry(hba, UPIU_QUERY_OPCODE_WRITE_ATTR, in ufshcd_set_dev_ref_clk()
8484 dev_err(hba->dev, "bRefClkFreq setting to %lu Hz failed\n", in ufshcd_set_dev_ref_clk()
8489 dev_dbg(hba->dev, "bRefClkFreq setting to %lu Hz succeeded\n", in ufshcd_set_dev_ref_clk()
8496 static int ufshcd_device_params_init(struct ufs_hba *hba) in ufshcd_device_params_init() argument
8502 ret = ufshcd_device_geo_params_init(hba); in ufshcd_device_params_init()
8507 ret = ufs_get_device_desc(hba); in ufshcd_device_params_init()
8509 dev_err(hba->dev, "%s: Failed getting device info. err = %d\n", in ufshcd_device_params_init()
8514 ufshcd_get_ref_clk_gating_wait(hba); in ufshcd_device_params_init()
8516 if (!ufshcd_query_flag_retry(hba, UPIU_QUERY_OPCODE_READ_FLAG, in ufshcd_device_params_init()
8518 hba->dev_info.f_power_on_wp_en = flag; in ufshcd_device_params_init()
8520 /* Probe maximum power mode co-supported by both UFS host and device */ in ufshcd_device_params_init()
8521 if (ufshcd_get_max_pwr_mode(hba)) in ufshcd_device_params_init()
8522 dev_err(hba->dev, in ufshcd_device_params_init()
8529 static void ufshcd_set_timestamp_attr(struct ufs_hba *hba) in ufshcd_set_timestamp_attr() argument
8534 struct ufs_dev_info *dev_info = &hba->dev_info; in ufshcd_set_timestamp_attr()
8537 if (dev_info->wspecversion < 0x400) in ufshcd_set_timestamp_attr()
8540 ufshcd_hold(hba); in ufshcd_set_timestamp_attr()
8542 mutex_lock(&hba->dev_cmd.lock); in ufshcd_set_timestamp_attr()
8544 ufshcd_init_query(hba, &request, &response, in ufshcd_set_timestamp_attr()
8548 request->query_func = UPIU_QUERY_FUNC_STANDARD_WRITE_REQUEST; in ufshcd_set_timestamp_attr()
8550 upiu_data = (struct utp_upiu_query_v4_0 *)&request->upiu_req; in ufshcd_set_timestamp_attr()
8552 put_unaligned_be64(ktime_get_real_ns(), &upiu_data->osf3); in ufshcd_set_timestamp_attr()
8554 err = ufshcd_exec_dev_cmd(hba, DEV_CMD_TYPE_QUERY, QUERY_REQ_TIMEOUT); in ufshcd_set_timestamp_attr()
8557 dev_err(hba->dev, "%s: failed to set timestamp %d\n", in ufshcd_set_timestamp_attr()
8560 mutex_unlock(&hba->dev_cmd.lock); in ufshcd_set_timestamp_attr()
8561 ufshcd_release(hba); in ufshcd_set_timestamp_attr()
8565 * ufshcd_add_lus - probe and add UFS logical units
8566 * @hba: per-adapter instance
8570 static int ufshcd_add_lus(struct ufs_hba *hba) in ufshcd_add_lus() argument
8575 ret = ufshcd_scsi_add_wlus(hba); in ufshcd_add_lus()
8580 if (ufshcd_is_clkscaling_supported(hba)) { in ufshcd_add_lus()
8581 memcpy(&hba->clk_scaling.saved_pwr_info, in ufshcd_add_lus()
8582 &hba->pwr_info, in ufshcd_add_lus()
8584 hba->clk_scaling.is_allowed = true; in ufshcd_add_lus()
8586 ret = ufshcd_devfreq_init(hba); in ufshcd_add_lus()
8590 hba->clk_scaling.is_enabled = true; in ufshcd_add_lus()
8591 ufshcd_init_clk_scaling_sysfs(hba); in ufshcd_add_lus()
8594 ufs_bsg_probe(hba); in ufshcd_add_lus()
8595 scsi_scan_host(hba->host); in ufshcd_add_lus()
8601 /* SDB - Single Doorbell */
8602 static void ufshcd_release_sdb_queue(struct ufs_hba *hba, int nutrs) in ufshcd_release_sdb_queue() argument
8606 ucdl_size = ufshcd_get_ucd_size(hba) * nutrs; in ufshcd_release_sdb_queue()
8607 dmam_free_coherent(hba->dev, ucdl_size, hba->ucdl_base_addr, in ufshcd_release_sdb_queue()
8608 hba->ucdl_dma_addr); in ufshcd_release_sdb_queue()
8611 dmam_free_coherent(hba->dev, utrdl_size, hba->utrdl_base_addr, in ufshcd_release_sdb_queue()
8612 hba->utrdl_dma_addr); in ufshcd_release_sdb_queue()
8614 devm_kfree(hba->dev, hba->lrb); in ufshcd_release_sdb_queue()
8617 static int ufshcd_alloc_mcq(struct ufs_hba *hba) in ufshcd_alloc_mcq() argument
8620 int old_nutrs = hba->nutrs; in ufshcd_alloc_mcq()
8622 ret = ufshcd_mcq_decide_queue_depth(hba); in ufshcd_alloc_mcq()
8626 hba->nutrs = ret; in ufshcd_alloc_mcq()
8627 ret = ufshcd_mcq_init(hba); in ufshcd_alloc_mcq()
8635 if (hba->nutrs != old_nutrs) { in ufshcd_alloc_mcq()
8636 ufshcd_release_sdb_queue(hba, old_nutrs); in ufshcd_alloc_mcq()
8637 ret = ufshcd_memory_alloc(hba); in ufshcd_alloc_mcq()
8640 ufshcd_host_memory_configure(hba); in ufshcd_alloc_mcq()
8643 ret = ufshcd_mcq_memory_alloc(hba); in ufshcd_alloc_mcq()
8649 hba->nutrs = old_nutrs; in ufshcd_alloc_mcq()
8653 static void ufshcd_config_mcq(struct ufs_hba *hba) in ufshcd_config_mcq() argument
8658 ret = ufshcd_mcq_vops_config_esi(hba); in ufshcd_config_mcq()
8659 dev_info(hba->dev, "ESI %sconfigured\n", ret ? "is not " : ""); in ufshcd_config_mcq()
8662 if (hba->quirks & UFSHCD_QUIRK_MCQ_BROKEN_INTR) in ufshcd_config_mcq()
8664 ufshcd_enable_intr(hba, intrs); in ufshcd_config_mcq()
8665 ufshcd_mcq_make_queues_operational(hba); in ufshcd_config_mcq()
8666 ufshcd_mcq_config_mac(hba, hba->nutrs); in ufshcd_config_mcq()
8668 hba->host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_config_mcq()
8669 hba->reserved_slot = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_config_mcq()
8672 ufshcd_writel(hba, ufshcd_readl(hba, REG_UFS_MEM_CFG) | 0x1, in ufshcd_config_mcq()
8674 hba->mcq_enabled = true; in ufshcd_config_mcq()
8676 …dev_info(hba->dev, "MCQ configured, nr_queues=%d, io_queues=%d, read_queue=%d, poll_queues=%d, que… in ufshcd_config_mcq()
8677 hba->nr_hw_queues, hba->nr_queues[HCTX_TYPE_DEFAULT], in ufshcd_config_mcq()
8678 hba->nr_queues[HCTX_TYPE_READ], hba->nr_queues[HCTX_TYPE_POLL], in ufshcd_config_mcq()
8679 hba->nutrs); in ufshcd_config_mcq()
8682 static int ufshcd_device_init(struct ufs_hba *hba, bool init_dev_params) in ufshcd_device_init() argument
8685 struct Scsi_Host *host = hba->host; in ufshcd_device_init()
8687 hba->ufshcd_state = UFSHCD_STATE_RESET; in ufshcd_device_init()
8689 ret = ufshcd_link_startup(hba); in ufshcd_device_init()
8693 if (hba->quirks & UFSHCD_QUIRK_SKIP_PH_CONFIGURATION) in ufshcd_device_init()
8697 ufshcd_clear_dbg_ufs_stats(hba); in ufshcd_device_init()
8700 ufshcd_set_link_active(hba); in ufshcd_device_init()
8703 if (is_mcq_enabled(hba) && !init_dev_params) in ufshcd_device_init()
8704 ufshcd_config_mcq(hba); in ufshcd_device_init()
8707 ret = ufshcd_verify_dev_init(hba); in ufshcd_device_init()
8712 ret = ufshcd_complete_dev_init(hba); in ufshcd_device_init()
8721 ret = ufshcd_device_params_init(hba); in ufshcd_device_init()
8724 if (is_mcq_supported(hba) && !hba->scsi_host_added) { in ufshcd_device_init()
8725 ret = ufshcd_alloc_mcq(hba); in ufshcd_device_init()
8727 ufshcd_config_mcq(hba); in ufshcd_device_init()
8731 dev_err(hba->dev, "MCQ mode is disabled, err=%d\n", in ufshcd_device_init()
8734 ret = scsi_add_host(host, hba->dev); in ufshcd_device_init()
8736 dev_err(hba->dev, "scsi_add_host failed\n"); in ufshcd_device_init()
8739 hba->scsi_host_added = true; in ufshcd_device_init()
8740 } else if (is_mcq_supported(hba)) { in ufshcd_device_init()
8742 ufshcd_config_mcq(hba); in ufshcd_device_init()
8746 ufshcd_tune_unipro_params(hba); in ufshcd_device_init()
8749 ufshcd_set_ufs_dev_active(hba); in ufshcd_device_init()
8750 ufshcd_force_reset_auto_bkops(hba); in ufshcd_device_init()
8752 ufshcd_set_timestamp_attr(hba); in ufshcd_device_init()
8755 if (hba->max_pwr_info.is_valid) { in ufshcd_device_init()
8760 if (hba->dev_ref_clk_freq != REF_CLK_FREQ_INVAL) in ufshcd_device_init()
8761 ufshcd_set_dev_ref_clk(hba); in ufshcd_device_init()
8762 ret = ufshcd_config_pwr_mode(hba, &hba->max_pwr_info.info); in ufshcd_device_init()
8764 dev_err(hba->dev, "%s: Failed setting power mode, err = %d\n", in ufshcd_device_init()
8774 * ufshcd_probe_hba - probe hba to detect device and initialize it
8775 * @hba: per-adapter instance
8778 * Execute link-startup and verify device initialization
8782 static int ufshcd_probe_hba(struct ufs_hba *hba, bool init_dev_params) in ufshcd_probe_hba() argument
8788 ret = ufshcd_device_init(hba, init_dev_params); in ufshcd_probe_hba()
8792 if (!hba->pm_op_in_progress && in ufshcd_probe_hba()
8793 (hba->quirks & UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH)) { in ufshcd_probe_hba()
8795 ufshcd_device_reset(hba); in ufshcd_probe_hba()
8796 ufs_put_device_desc(hba); in ufshcd_probe_hba()
8797 ufshcd_hba_stop(hba); in ufshcd_probe_hba()
8798 ufshcd_vops_reinit_notify(hba); in ufshcd_probe_hba()
8799 ret = ufshcd_hba_enable(hba); in ufshcd_probe_hba()
8801 dev_err(hba->dev, "Host controller enable failed\n"); in ufshcd_probe_hba()
8802 ufshcd_print_evt_hist(hba); in ufshcd_probe_hba()
8803 ufshcd_print_host_state(hba); in ufshcd_probe_hba()
8808 ret = ufshcd_device_init(hba, init_dev_params); in ufshcd_probe_hba()
8813 ufshcd_print_pwr_info(hba); in ufshcd_probe_hba()
8821 ufshcd_set_active_icc_lvl(hba); in ufshcd_probe_hba()
8824 ufshcd_configure_wb(hba); in ufshcd_probe_hba()
8826 if (hba->ee_usr_mask) in ufshcd_probe_hba()
8827 ufshcd_write_ee_control(hba); in ufshcd_probe_hba()
8828 /* Enable Auto-Hibernate if configured */ in ufshcd_probe_hba()
8829 ufshcd_auto_hibern8_enable(hba); in ufshcd_probe_hba()
8832 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_probe_hba()
8834 hba->ufshcd_state = UFSHCD_STATE_ERROR; in ufshcd_probe_hba()
8835 else if (hba->ufshcd_state == UFSHCD_STATE_RESET) in ufshcd_probe_hba()
8836 hba->ufshcd_state = UFSHCD_STATE_OPERATIONAL; in ufshcd_probe_hba()
8837 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_probe_hba()
8839 trace_ufshcd_init(dev_name(hba->dev), ret, in ufshcd_probe_hba()
8841 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_probe_hba()
8846 * ufshcd_async_scan - asynchronous execution for probing hba
8852 struct ufs_hba *hba = (struct ufs_hba *)data; in ufshcd_async_scan() local
8855 down(&hba->host_sem); in ufshcd_async_scan()
8856 /* Initialize hba, detect and initialize UFS device */ in ufshcd_async_scan()
8857 ret = ufshcd_probe_hba(hba, true); in ufshcd_async_scan()
8858 up(&hba->host_sem); in ufshcd_async_scan()
8863 ret = ufshcd_add_lus(hba); in ufshcd_async_scan()
8866 pm_runtime_put_sync(hba->dev); in ufshcd_async_scan()
8869 dev_err(hba->dev, "%s failed: %d\n", __func__, ret); in ufshcd_async_scan()
8874 struct ufs_hba *hba = shost_priv(scmd->device->host); in ufshcd_eh_timed_out() local
8876 if (!hba->system_suspending) { in ufshcd_eh_timed_out()
8887 ufshcd_link_recovery(hba); in ufshcd_eh_timed_out()
8888 dev_info(hba->dev, "%s() finished; outstanding_tasks = %#lx.\n", in ufshcd_eh_timed_out()
8889 __func__, hba->outstanding_tasks); in ufshcd_eh_timed_out()
8891 return hba->outstanding_reqs ? SCSI_EH_RESET_TIMER : SCSI_EH_DONE; in ufshcd_eh_timed_out()
8925 .this_id = -1,
8952 if (!vreg->max_uA) in ufshcd_config_vreg_load()
8955 ret = regulator_set_load(vreg->reg, ua); in ufshcd_config_vreg_load()
8958 __func__, vreg->name, ua, ret); in ufshcd_config_vreg_load()
8964 static inline int ufshcd_config_vreg_lpm(struct ufs_hba *hba, in ufshcd_config_vreg_lpm() argument
8967 return ufshcd_config_vreg_load(hba->dev, vreg, UFS_VREG_LPM_LOAD_UA); in ufshcd_config_vreg_lpm()
8970 static inline int ufshcd_config_vreg_hpm(struct ufs_hba *hba, in ufshcd_config_vreg_hpm() argument
8976 return ufshcd_config_vreg_load(hba->dev, vreg, vreg->max_uA); in ufshcd_config_vreg_hpm()
8982 if (regulator_count_voltages(vreg->reg) <= 0) in ufshcd_config_vreg()
8985 return ufshcd_config_vreg_load(dev, vreg, on ? vreg->max_uA : 0); in ufshcd_config_vreg()
8992 if (!vreg || vreg->enabled) in ufshcd_enable_vreg()
8997 ret = regulator_enable(vreg->reg); in ufshcd_enable_vreg()
9000 vreg->enabled = true; in ufshcd_enable_vreg()
9003 __func__, vreg->name, ret); in ufshcd_enable_vreg()
9012 if (!vreg || !vreg->enabled || vreg->always_on) in ufshcd_disable_vreg()
9015 ret = regulator_disable(vreg->reg); in ufshcd_disable_vreg()
9020 vreg->enabled = false; in ufshcd_disable_vreg()
9023 __func__, vreg->name, ret); in ufshcd_disable_vreg()
9029 static int ufshcd_setup_vreg(struct ufs_hba *hba, bool on) in ufshcd_setup_vreg() argument
9032 struct device *dev = hba->dev; in ufshcd_setup_vreg()
9033 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_setup_vreg()
9035 ret = ufshcd_toggle_vreg(dev, info->vcc, on); in ufshcd_setup_vreg()
9039 ret = ufshcd_toggle_vreg(dev, info->vccq, on); in ufshcd_setup_vreg()
9043 ret = ufshcd_toggle_vreg(dev, info->vccq2, on); in ufshcd_setup_vreg()
9047 ufshcd_toggle_vreg(dev, info->vccq2, false); in ufshcd_setup_vreg()
9048 ufshcd_toggle_vreg(dev, info->vccq, false); in ufshcd_setup_vreg()
9049 ufshcd_toggle_vreg(dev, info->vcc, false); in ufshcd_setup_vreg()
9054 static int ufshcd_setup_hba_vreg(struct ufs_hba *hba, bool on) in ufshcd_setup_hba_vreg() argument
9056 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_setup_hba_vreg()
9058 return ufshcd_toggle_vreg(hba->dev, info->vdd_hba, on); in ufshcd_setup_hba_vreg()
9068 vreg->reg = devm_regulator_get(dev, vreg->name); in ufshcd_get_vreg()
9069 if (IS_ERR(vreg->reg)) { in ufshcd_get_vreg()
9070 ret = PTR_ERR(vreg->reg); in ufshcd_get_vreg()
9072 __func__, vreg->name, ret); in ufshcd_get_vreg()
9079 static int ufshcd_init_vreg(struct ufs_hba *hba) in ufshcd_init_vreg() argument
9082 struct device *dev = hba->dev; in ufshcd_init_vreg()
9083 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_init_vreg()
9085 ret = ufshcd_get_vreg(dev, info->vcc); in ufshcd_init_vreg()
9089 ret = ufshcd_get_vreg(dev, info->vccq); in ufshcd_init_vreg()
9091 ret = ufshcd_get_vreg(dev, info->vccq2); in ufshcd_init_vreg()
9096 static int ufshcd_init_hba_vreg(struct ufs_hba *hba) in ufshcd_init_hba_vreg() argument
9098 struct ufs_vreg_info *info = &hba->vreg_info; in ufshcd_init_hba_vreg()
9100 return ufshcd_get_vreg(hba->dev, info->vdd_hba); in ufshcd_init_hba_vreg()
9103 static int ufshcd_setup_clocks(struct ufs_hba *hba, bool on) in ufshcd_setup_clocks() argument
9107 struct list_head *head = &hba->clk_list_head; in ufshcd_setup_clocks()
9115 ret = ufshcd_vops_setup_clocks(hba, on, PRE_CHANGE); in ufshcd_setup_clocks()
9120 if (!IS_ERR_OR_NULL(clki->clk)) { in ufshcd_setup_clocks()
9125 if (ufshcd_is_link_active(hba) && in ufshcd_setup_clocks()
9126 clki->keep_link_active) in ufshcd_setup_clocks()
9129 clk_state_changed = on ^ clki->enabled; in ufshcd_setup_clocks()
9130 if (on && !clki->enabled) { in ufshcd_setup_clocks()
9131 ret = clk_prepare_enable(clki->clk); in ufshcd_setup_clocks()
9133 dev_err(hba->dev, "%s: %s prepare enable failed, %d\n", in ufshcd_setup_clocks()
9134 __func__, clki->name, ret); in ufshcd_setup_clocks()
9137 } else if (!on && clki->enabled) { in ufshcd_setup_clocks()
9138 clk_disable_unprepare(clki->clk); in ufshcd_setup_clocks()
9140 clki->enabled = on; in ufshcd_setup_clocks()
9141 dev_dbg(hba->dev, "%s: clk: %s %sabled\n", __func__, in ufshcd_setup_clocks()
9142 clki->name, on ? "en" : "dis"); in ufshcd_setup_clocks()
9146 ret = ufshcd_vops_setup_clocks(hba, on, POST_CHANGE); in ufshcd_setup_clocks()
9153 if (!IS_ERR_OR_NULL(clki->clk) && clki->enabled) in ufshcd_setup_clocks()
9154 clk_disable_unprepare(clki->clk); in ufshcd_setup_clocks()
9157 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_setup_clocks()
9158 hba->clk_gating.state = CLKS_ON; in ufshcd_setup_clocks()
9159 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_setup_clocks()
9160 hba->clk_gating.state); in ufshcd_setup_clocks()
9161 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_setup_clocks()
9165 trace_ufshcd_profile_clk_gating(dev_name(hba->dev), in ufshcd_setup_clocks()
9171 static enum ufs_ref_clk_freq ufshcd_parse_ref_clk_property(struct ufs_hba *hba) in ufshcd_parse_ref_clk_property() argument
9174 int ret = device_property_read_u32(hba->dev, "ref-clk-freq", &freq); in ufshcd_parse_ref_clk_property()
9177 dev_dbg(hba->dev, "Cannot query 'ref-clk-freq' property = %d", ret); in ufshcd_parse_ref_clk_property()
9184 static int ufshcd_init_clocks(struct ufs_hba *hba) in ufshcd_init_clocks() argument
9188 struct device *dev = hba->dev; in ufshcd_init_clocks()
9189 struct list_head *head = &hba->clk_list_head; in ufshcd_init_clocks()
9195 if (!clki->name) in ufshcd_init_clocks()
9198 clki->clk = devm_clk_get(dev, clki->name); in ufshcd_init_clocks()
9199 if (IS_ERR(clki->clk)) { in ufshcd_init_clocks()
9200 ret = PTR_ERR(clki->clk); in ufshcd_init_clocks()
9202 __func__, clki->name, ret); in ufshcd_init_clocks()
9211 if (!strcmp(clki->name, "ref_clk")) in ufshcd_init_clocks()
9212 ufshcd_parse_dev_ref_clk_freq(hba, clki->clk); in ufshcd_init_clocks()
9214 if (clki->max_freq) { in ufshcd_init_clocks()
9215 ret = clk_set_rate(clki->clk, clki->max_freq); in ufshcd_init_clocks()
9217 dev_err(hba->dev, "%s: %s clk set rate(%dHz) failed, %d\n", in ufshcd_init_clocks()
9218 __func__, clki->name, in ufshcd_init_clocks()
9219 clki->max_freq, ret); in ufshcd_init_clocks()
9222 clki->curr_freq = clki->max_freq; in ufshcd_init_clocks()
9225 clki->name, clk_get_rate(clki->clk)); in ufshcd_init_clocks()
9231 static int ufshcd_variant_hba_init(struct ufs_hba *hba) in ufshcd_variant_hba_init() argument
9235 if (!hba->vops) in ufshcd_variant_hba_init()
9238 err = ufshcd_vops_init(hba); in ufshcd_variant_hba_init()
9240 dev_err_probe(hba->dev, err, in ufshcd_variant_hba_init()
9242 __func__, ufshcd_get_var_name(hba), err); in ufshcd_variant_hba_init()
9247 static void ufshcd_variant_hba_exit(struct ufs_hba *hba) in ufshcd_variant_hba_exit() argument
9249 if (!hba->vops) in ufshcd_variant_hba_exit()
9252 ufshcd_vops_exit(hba); in ufshcd_variant_hba_exit()
9255 static int ufshcd_hba_init(struct ufs_hba *hba) in ufshcd_hba_init() argument
9266 err = ufshcd_init_hba_vreg(hba); in ufshcd_hba_init()
9270 err = ufshcd_setup_hba_vreg(hba, true); in ufshcd_hba_init()
9274 err = ufshcd_init_clocks(hba); in ufshcd_hba_init()
9278 if (hba->dev_ref_clk_freq == REF_CLK_FREQ_INVAL) in ufshcd_hba_init()
9279 hba->dev_ref_clk_freq = ufshcd_parse_ref_clk_property(hba); in ufshcd_hba_init()
9281 err = ufshcd_setup_clocks(hba, true); in ufshcd_hba_init()
9285 err = ufshcd_init_vreg(hba); in ufshcd_hba_init()
9289 err = ufshcd_setup_vreg(hba, true); in ufshcd_hba_init()
9293 err = ufshcd_variant_hba_init(hba); in ufshcd_hba_init()
9297 ufs_debugfs_hba_init(hba); in ufshcd_hba_init()
9299 hba->is_powered = true; in ufshcd_hba_init()
9303 ufshcd_setup_vreg(hba, false); in ufshcd_hba_init()
9305 ufshcd_setup_clocks(hba, false); in ufshcd_hba_init()
9307 ufshcd_setup_hba_vreg(hba, false); in ufshcd_hba_init()
9312 static void ufshcd_hba_exit(struct ufs_hba *hba) in ufshcd_hba_exit() argument
9314 if (hba->is_powered) { in ufshcd_hba_exit()
9315 ufshcd_exit_clk_scaling(hba); in ufshcd_hba_exit()
9316 ufshcd_exit_clk_gating(hba); in ufshcd_hba_exit()
9317 if (hba->eh_wq) in ufshcd_hba_exit()
9318 destroy_workqueue(hba->eh_wq); in ufshcd_hba_exit()
9319 ufs_debugfs_hba_exit(hba); in ufshcd_hba_exit()
9320 ufshcd_variant_hba_exit(hba); in ufshcd_hba_exit()
9321 ufshcd_setup_vreg(hba, false); in ufshcd_hba_exit()
9322 ufshcd_setup_clocks(hba, false); in ufshcd_hba_exit()
9323 ufshcd_setup_hba_vreg(hba, false); in ufshcd_hba_exit()
9324 hba->is_powered = false; in ufshcd_hba_exit()
9325 ufs_put_device_desc(hba); in ufshcd_hba_exit()
9346 * ufshcd_set_dev_pwr_mode - sends START STOP UNIT command to set device
9348 * @hba: per adapter instance
9354 static int ufshcd_set_dev_pwr_mode(struct ufs_hba *hba, in ufshcd_set_dev_pwr_mode() argument
9362 spin_lock_irqsave(hba->host->host_lock, flags); in ufshcd_set_dev_pwr_mode()
9363 sdp = hba->ufs_device_wlun; in ufshcd_set_dev_pwr_mode()
9367 ret = -ENODEV; in ufshcd_set_dev_pwr_mode()
9368 spin_unlock_irqrestore(hba->host->host_lock, flags); in ufshcd_set_dev_pwr_mode()
9374 * If scsi commands fail, the scsi mid-layer schedules scsi error- in ufshcd_set_dev_pwr_mode()
9379 hba->host->eh_noresume = 1; in ufshcd_set_dev_pwr_mode()
9386 for (retries = 3; retries > 0; --retries) { in ufshcd_set_dev_pwr_mode()
9402 ret = -EIO; in ufshcd_set_dev_pwr_mode()
9405 hba->curr_dev_pwr_mode = pwr_mode; in ufshcd_set_dev_pwr_mode()
9409 hba->host->eh_noresume = 0; in ufshcd_set_dev_pwr_mode()
9413 static int ufshcd_link_state_transition(struct ufs_hba *hba, in ufshcd_link_state_transition() argument
9419 if (req_link_state == hba->uic_link_state) in ufshcd_link_state_transition()
9423 ret = ufshcd_uic_hibern8_enter(hba); in ufshcd_link_state_transition()
9425 ufshcd_set_link_hibern8(hba); in ufshcd_link_state_transition()
9427 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", in ufshcd_link_state_transition()
9438 (!check_for_bkops || !hba->auto_bkops_enabled)) { in ufshcd_link_state_transition()
9449 ret = ufshcd_uic_hibern8_enter(hba); in ufshcd_link_state_transition()
9451 dev_err(hba->dev, "%s: hibern8 enter failed %d\n", in ufshcd_link_state_transition()
9459 ufshcd_hba_stop(hba); in ufshcd_link_state_transition()
9464 ufshcd_set_link_off(hba); in ufshcd_link_state_transition()
9471 static void ufshcd_vreg_set_lpm(struct ufs_hba *hba) in ufshcd_vreg_set_lpm() argument
9481 if (!ufshcd_is_link_active(hba) && in ufshcd_vreg_set_lpm()
9482 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_BEFORE_LPM) in ufshcd_vreg_set_lpm()
9500 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && in ufshcd_vreg_set_lpm()
9501 !hba->dev_info.is_lu_power_on_wp) { in ufshcd_vreg_set_lpm()
9502 ufshcd_setup_vreg(hba, false); in ufshcd_vreg_set_lpm()
9504 } else if (!ufshcd_is_ufs_dev_active(hba)) { in ufshcd_vreg_set_lpm()
9505 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); in ufshcd_vreg_set_lpm()
9507 if (ufshcd_is_link_hibern8(hba) || ufshcd_is_link_off(hba)) { in ufshcd_vreg_set_lpm()
9508 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); in ufshcd_vreg_set_lpm()
9509 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq2); in ufshcd_vreg_set_lpm()
9514 * Some UFS devices require delay after VCC power rail is turned-off. in ufshcd_vreg_set_lpm()
9516 if (vcc_off && hba->vreg_info.vcc && in ufshcd_vreg_set_lpm()
9517 hba->dev_quirks & UFS_DEVICE_QUIRK_DELAY_AFTER_LPM) in ufshcd_vreg_set_lpm()
9522 static int ufshcd_vreg_set_hpm(struct ufs_hba *hba) in ufshcd_vreg_set_hpm() argument
9526 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba) && in ufshcd_vreg_set_hpm()
9527 !hba->dev_info.is_lu_power_on_wp) { in ufshcd_vreg_set_hpm()
9528 ret = ufshcd_setup_vreg(hba, true); in ufshcd_vreg_set_hpm()
9529 } else if (!ufshcd_is_ufs_dev_active(hba)) { in ufshcd_vreg_set_hpm()
9530 if (!ufshcd_is_link_active(hba)) { in ufshcd_vreg_set_hpm()
9531 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq); in ufshcd_vreg_set_hpm()
9534 ret = ufshcd_config_vreg_hpm(hba, hba->vreg_info.vccq2); in ufshcd_vreg_set_hpm()
9538 ret = ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, true); in ufshcd_vreg_set_hpm()
9543 ufshcd_config_vreg_lpm(hba, hba->vreg_info.vccq); in ufshcd_vreg_set_hpm()
9545 ufshcd_toggle_vreg(hba->dev, hba->vreg_info.vcc, false); in ufshcd_vreg_set_hpm()
9551 static void ufshcd_hba_vreg_set_lpm(struct ufs_hba *hba) in ufshcd_hba_vreg_set_lpm() argument
9553 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba)) in ufshcd_hba_vreg_set_lpm()
9554 ufshcd_setup_hba_vreg(hba, false); in ufshcd_hba_vreg_set_lpm()
9557 static void ufshcd_hba_vreg_set_hpm(struct ufs_hba *hba) in ufshcd_hba_vreg_set_hpm() argument
9559 if (ufshcd_is_link_off(hba) || ufshcd_can_aggressive_pc(hba)) in ufshcd_hba_vreg_set_hpm()
9560 ufshcd_setup_hba_vreg(hba, true); in ufshcd_hba_vreg_set_hpm()
9563 static int __ufshcd_wl_suspend(struct ufs_hba *hba, enum ufs_pm_op pm_op) in __ufshcd_wl_suspend() argument
9571 hba->pm_op_in_progress = true; in __ufshcd_wl_suspend()
9574 hba->rpm_lvl : hba->spm_lvl; in __ufshcd_wl_suspend()
9586 ufshcd_hold(hba); in __ufshcd_wl_suspend()
9587 hba->clk_gating.is_suspended = true; in __ufshcd_wl_suspend()
9589 if (ufshcd_is_clkscaling_supported(hba)) in __ufshcd_wl_suspend()
9590 ufshcd_clk_scaling_suspend(hba, true); in __ufshcd_wl_suspend()
9597 if ((req_dev_pwr_mode == hba->curr_dev_pwr_mode) && in __ufshcd_wl_suspend()
9598 (req_link_state == hba->uic_link_state)) in __ufshcd_wl_suspend()
9602 if (!ufshcd_is_ufs_dev_active(hba) || !ufshcd_is_link_active(hba)) { in __ufshcd_wl_suspend()
9604 if (!ufshcd_eh_in_progress(hba)) in __ufshcd_wl_suspend()
9605 ufshcd_force_error_recovery(hba); in __ufshcd_wl_suspend()
9606 ret = -EBUSY; in __ufshcd_wl_suspend()
9611 if (ufshcd_can_autobkops_during_suspend(hba)) { in __ufshcd_wl_suspend()
9617 ret = ufshcd_urgent_bkops(hba); in __ufshcd_wl_suspend()
9624 ufshcd_force_error_recovery(hba); in __ufshcd_wl_suspend()
9625 ret = -EBUSY; in __ufshcd_wl_suspend()
9630 ufshcd_disable_auto_bkops(hba); in __ufshcd_wl_suspend()
9637 hba->dev_info.b_rpm_dev_flush_capable = in __ufshcd_wl_suspend()
9638 hba->auto_bkops_enabled || in __ufshcd_wl_suspend()
9641 ufshcd_is_auto_hibern8_enabled(hba))) && in __ufshcd_wl_suspend()
9642 ufshcd_wb_need_flush(hba)); in __ufshcd_wl_suspend()
9645 flush_work(&hba->eeh_work); in __ufshcd_wl_suspend()
9647 ret = ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE); in __ufshcd_wl_suspend()
9651 if (req_dev_pwr_mode != hba->curr_dev_pwr_mode) { in __ufshcd_wl_suspend()
9654 ufshcd_disable_auto_bkops(hba); in __ufshcd_wl_suspend()
9656 if (!hba->dev_info.b_rpm_dev_flush_capable) { in __ufshcd_wl_suspend()
9657 ret = ufshcd_set_dev_pwr_mode(hba, req_dev_pwr_mode); in __ufshcd_wl_suspend()
9664 ufshcd_force_error_recovery(hba); in __ufshcd_wl_suspend()
9665 ret = -EBUSY; in __ufshcd_wl_suspend()
9676 check_for_bkops = !ufshcd_is_ufs_dev_deepsleep(hba); in __ufshcd_wl_suspend()
9677 ret = ufshcd_link_state_transition(hba, req_link_state, check_for_bkops); in __ufshcd_wl_suspend()
9684 ufshcd_force_error_recovery(hba); in __ufshcd_wl_suspend()
9685 ret = -EBUSY; in __ufshcd_wl_suspend()
9696 ret = ufshcd_vops_suspend(hba, pm_op, POST_CHANGE); in __ufshcd_wl_suspend()
9707 if (ufshcd_is_ufs_dev_deepsleep(hba)) { in __ufshcd_wl_suspend()
9708 ufshcd_device_reset(hba); in __ufshcd_wl_suspend()
9709 WARN_ON(!ufshcd_is_link_off(hba)); in __ufshcd_wl_suspend()
9711 if (ufshcd_is_link_hibern8(hba) && !ufshcd_uic_hibern8_exit(hba)) in __ufshcd_wl_suspend()
9712 ufshcd_set_link_active(hba); in __ufshcd_wl_suspend()
9713 else if (ufshcd_is_link_off(hba)) in __ufshcd_wl_suspend()
9714 ufshcd_host_reset_and_restore(hba); in __ufshcd_wl_suspend()
9717 if (ufshcd_is_ufs_dev_deepsleep(hba)) { in __ufshcd_wl_suspend()
9718 ufshcd_device_reset(hba); in __ufshcd_wl_suspend()
9719 ufshcd_host_reset_and_restore(hba); in __ufshcd_wl_suspend()
9721 if (!ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE)) in __ufshcd_wl_suspend()
9722 ufshcd_disable_auto_bkops(hba); in __ufshcd_wl_suspend()
9724 if (ufshcd_is_clkscaling_supported(hba)) in __ufshcd_wl_suspend()
9725 ufshcd_clk_scaling_suspend(hba, false); in __ufshcd_wl_suspend()
9727 hba->dev_info.b_rpm_dev_flush_capable = false; in __ufshcd_wl_suspend()
9729 if (hba->dev_info.b_rpm_dev_flush_capable) { in __ufshcd_wl_suspend()
9730 schedule_delayed_work(&hba->rpm_dev_flush_recheck_work, in __ufshcd_wl_suspend()
9735 ufshcd_update_evt_hist(hba, UFS_EVT_WL_SUSP_ERR, (u32)ret); in __ufshcd_wl_suspend()
9736 hba->clk_gating.is_suspended = false; in __ufshcd_wl_suspend()
9737 ufshcd_release(hba); in __ufshcd_wl_suspend()
9739 hba->pm_op_in_progress = false; in __ufshcd_wl_suspend()
9744 static int __ufshcd_wl_resume(struct ufs_hba *hba, enum ufs_pm_op pm_op) in __ufshcd_wl_resume() argument
9747 enum uic_link_state old_link_state = hba->uic_link_state; in __ufshcd_wl_resume()
9749 hba->pm_op_in_progress = true; in __ufshcd_wl_resume()
9756 ret = ufshcd_vops_resume(hba, pm_op); in __ufshcd_wl_resume()
9761 WARN_ON(ufshcd_is_ufs_dev_deepsleep(hba) && !ufshcd_is_link_off(hba)); in __ufshcd_wl_resume()
9763 if (ufshcd_is_link_hibern8(hba)) { in __ufshcd_wl_resume()
9764 ret = ufshcd_uic_hibern8_exit(hba); in __ufshcd_wl_resume()
9766 ufshcd_set_link_active(hba); in __ufshcd_wl_resume()
9768 dev_err(hba->dev, "%s: hibern8 exit failed %d\n", in __ufshcd_wl_resume()
9772 } else if (ufshcd_is_link_off(hba)) { in __ufshcd_wl_resume()
9779 ret = ufshcd_reset_and_restore(hba); in __ufshcd_wl_resume()
9784 if (ret || !ufshcd_is_link_active(hba)) in __ufshcd_wl_resume()
9788 if (!ufshcd_is_ufs_dev_active(hba)) { in __ufshcd_wl_resume()
9789 ret = ufshcd_set_dev_pwr_mode(hba, UFS_ACTIVE_PWR_MODE); in __ufshcd_wl_resume()
9792 ufshcd_set_timestamp_attr(hba); in __ufshcd_wl_resume()
9795 if (ufshcd_keep_autobkops_enabled_except_suspend(hba)) in __ufshcd_wl_resume()
9796 ufshcd_enable_auto_bkops(hba); in __ufshcd_wl_resume()
9800 * keep auto-bkops enabled or else disable it. in __ufshcd_wl_resume()
9802 ufshcd_urgent_bkops(hba); in __ufshcd_wl_resume()
9804 if (hba->ee_usr_mask) in __ufshcd_wl_resume()
9805 ufshcd_write_ee_control(hba); in __ufshcd_wl_resume()
9807 if (ufshcd_is_clkscaling_supported(hba)) in __ufshcd_wl_resume()
9808 ufshcd_clk_scaling_suspend(hba, false); in __ufshcd_wl_resume()
9810 if (hba->dev_info.b_rpm_dev_flush_capable) { in __ufshcd_wl_resume()
9811 hba->dev_info.b_rpm_dev_flush_capable = false; in __ufshcd_wl_resume()
9812 cancel_delayed_work(&hba->rpm_dev_flush_recheck_work); in __ufshcd_wl_resume()
9815 /* Enable Auto-Hibernate if configured */ in __ufshcd_wl_resume()
9816 ufshcd_auto_hibern8_enable(hba); in __ufshcd_wl_resume()
9821 ufshcd_link_state_transition(hba, old_link_state, 0); in __ufshcd_wl_resume()
9823 ufshcd_vops_suspend(hba, pm_op, PRE_CHANGE); in __ufshcd_wl_resume()
9824 ufshcd_vops_suspend(hba, pm_op, POST_CHANGE); in __ufshcd_wl_resume()
9827 ufshcd_update_evt_hist(hba, UFS_EVT_WL_RES_ERR, (u32)ret); in __ufshcd_wl_resume()
9828 hba->clk_gating.is_suspended = false; in __ufshcd_wl_resume()
9829 ufshcd_release(hba); in __ufshcd_wl_resume()
9830 hba->pm_op_in_progress = false; in __ufshcd_wl_resume()
9837 struct ufs_hba *hba; in ufshcd_wl_runtime_suspend() local
9841 hba = shost_priv(sdev->host); in ufshcd_wl_runtime_suspend()
9843 ret = __ufshcd_wl_suspend(hba, UFS_RUNTIME_PM); in ufshcd_wl_runtime_suspend()
9845 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_runtime_suspend()
9849 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_runtime_suspend()
9857 struct ufs_hba *hba; in ufshcd_wl_runtime_resume() local
9861 hba = shost_priv(sdev->host); in ufshcd_wl_runtime_resume()
9863 ret = __ufshcd_wl_resume(hba, UFS_RUNTIME_PM); in ufshcd_wl_runtime_resume()
9865 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_runtime_resume()
9869 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_runtime_resume()
9879 struct ufs_hba *hba; in ufshcd_wl_suspend() local
9883 hba = shost_priv(sdev->host); in ufshcd_wl_suspend()
9884 down(&hba->host_sem); in ufshcd_wl_suspend()
9885 hba->system_suspending = true; in ufshcd_wl_suspend()
9890 ret = __ufshcd_wl_suspend(hba, UFS_SYSTEM_PM); in ufshcd_wl_suspend()
9892 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_suspend()
9893 up(&hba->host_sem); in ufshcd_wl_suspend()
9898 hba->is_sys_suspended = true; in ufshcd_wl_suspend()
9901 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_suspend()
9909 struct ufs_hba *hba; in ufshcd_wl_resume() local
9913 hba = shost_priv(sdev->host); in ufshcd_wl_resume()
9918 ret = __ufshcd_wl_resume(hba, UFS_SYSTEM_PM); in ufshcd_wl_resume()
9920 dev_err(&sdev->sdev_gendev, "%s failed: %d\n", __func__, ret); in ufshcd_wl_resume()
9924 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_wl_resume()
9926 hba->is_sys_suspended = false; in ufshcd_wl_resume()
9927 hba->system_suspending = false; in ufshcd_wl_resume()
9928 up(&hba->host_sem); in ufshcd_wl_resume()
9934 * ufshcd_suspend - helper function for suspend operations
9935 * @hba: per adapter instance
9938 * and set vreg and hba-vreg in lpm mode.
9942 static int ufshcd_suspend(struct ufs_hba *hba) in ufshcd_suspend() argument
9946 if (!hba->is_powered) in ufshcd_suspend()
9952 ufshcd_disable_irq(hba); in ufshcd_suspend()
9953 ret = ufshcd_setup_clocks(hba, false); in ufshcd_suspend()
9955 ufshcd_enable_irq(hba); in ufshcd_suspend()
9958 if (ufshcd_is_clkgating_allowed(hba)) { in ufshcd_suspend()
9959 hba->clk_gating.state = CLKS_OFF; in ufshcd_suspend()
9960 trace_ufshcd_clk_gating(dev_name(hba->dev), in ufshcd_suspend()
9961 hba->clk_gating.state); in ufshcd_suspend()
9964 ufshcd_vreg_set_lpm(hba); in ufshcd_suspend()
9966 ufshcd_hba_vreg_set_lpm(hba); in ufshcd_suspend()
9972 * ufshcd_resume - helper function for resume operations
9973 * @hba: per adapter instance
9976 * irqs of the hba.
9978 * Return: 0 for success and non-zero for failure.
9980 static int ufshcd_resume(struct ufs_hba *hba) in ufshcd_resume() argument
9984 if (!hba->is_powered) in ufshcd_resume()
9987 ufshcd_hba_vreg_set_hpm(hba); in ufshcd_resume()
9988 ret = ufshcd_vreg_set_hpm(hba); in ufshcd_resume()
9993 ret = ufshcd_setup_clocks(hba, true); in ufshcd_resume()
9998 ufshcd_enable_irq(hba); in ufshcd_resume()
10003 ufshcd_vreg_set_lpm(hba); in ufshcd_resume()
10006 ufshcd_update_evt_hist(hba, UFS_EVT_RESUME_ERR, (u32)ret); in ufshcd_resume()
10013 * ufshcd_system_suspend - system suspend callback
10019 * Return: 0 for success and non-zero for failure.
10023 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_system_suspend() local
10027 if (pm_runtime_suspended(hba->dev)) in ufshcd_system_suspend()
10030 ret = ufshcd_suspend(hba); in ufshcd_system_suspend()
10032 trace_ufshcd_system_suspend(dev_name(hba->dev), ret, in ufshcd_system_suspend()
10034 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_system_suspend()
10040 * ufshcd_system_resume - system resume callback
10046 * Return: 0 for success and non-zero for failure.
10050 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_system_resume() local
10054 if (pm_runtime_suspended(hba->dev)) in ufshcd_system_resume()
10057 ret = ufshcd_resume(hba); in ufshcd_system_resume()
10060 trace_ufshcd_system_resume(dev_name(hba->dev), ret, in ufshcd_system_resume()
10062 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_system_resume()
10071 * ufshcd_runtime_suspend - runtime suspend callback
10076 * Return: 0 for success and non-zero for failure.
10080 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_runtime_suspend() local
10084 ret = ufshcd_suspend(hba); in ufshcd_runtime_suspend()
10086 trace_ufshcd_runtime_suspend(dev_name(hba->dev), ret, in ufshcd_runtime_suspend()
10088 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_runtime_suspend()
10094 * ufshcd_runtime_resume - runtime resume routine
10107 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_runtime_resume() local
10111 ret = ufshcd_resume(hba); in ufshcd_runtime_resume()
10113 trace_ufshcd_runtime_resume(dev_name(hba->dev), ret, in ufshcd_runtime_resume()
10115 hba->curr_dev_pwr_mode, hba->uic_link_state); in ufshcd_runtime_resume()
10124 struct ufs_hba *hba = shost_priv(sdev->host); in ufshcd_wl_shutdown() local
10126 down(&hba->host_sem); in ufshcd_wl_shutdown()
10127 hba->shutting_down = true; in ufshcd_wl_shutdown()
10128 up(&hba->host_sem); in ufshcd_wl_shutdown()
10131 ufshcd_rpm_get_sync(hba); in ufshcd_wl_shutdown()
10133 shost_for_each_device(sdev, hba->host) { in ufshcd_wl_shutdown()
10134 if (sdev == hba->ufs_device_wlun) in ufshcd_wl_shutdown()
10136 mutex_lock(&sdev->state_mutex); in ufshcd_wl_shutdown()
10138 mutex_unlock(&sdev->state_mutex); in ufshcd_wl_shutdown()
10140 __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM); in ufshcd_wl_shutdown()
10146 if (ufshcd_is_ufs_dev_poweroff(hba) && ufshcd_is_link_off(hba)) in ufshcd_wl_shutdown()
10147 ufshcd_suspend(hba); in ufshcd_wl_shutdown()
10149 hba->is_powered = false; in ufshcd_wl_shutdown()
10153 * ufshcd_remove - de-allocate SCSI host and host memory space
10155 * @hba: per adapter instance
10157 void ufshcd_remove(struct ufs_hba *hba) in ufshcd_remove() argument
10159 if (hba->ufs_device_wlun) in ufshcd_remove()
10160 ufshcd_rpm_get_sync(hba); in ufshcd_remove()
10161 ufs_hwmon_remove(hba); in ufshcd_remove()
10162 ufs_bsg_remove(hba); in ufshcd_remove()
10163 ufs_sysfs_remove_nodes(hba->dev); in ufshcd_remove()
10164 blk_mq_destroy_queue(hba->tmf_queue); in ufshcd_remove()
10165 blk_put_queue(hba->tmf_queue); in ufshcd_remove()
10166 blk_mq_free_tag_set(&hba->tmf_tag_set); in ufshcd_remove()
10167 if (hba->scsi_host_added) in ufshcd_remove()
10168 scsi_remove_host(hba->host); in ufshcd_remove()
10170 ufshcd_disable_intr(hba, hba->intr_mask); in ufshcd_remove()
10171 ufshcd_hba_stop(hba); in ufshcd_remove()
10172 ufshcd_hba_exit(hba); in ufshcd_remove()
10188 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_system_restore() local
10196 ufshcd_writel(hba, lower_32_bits(hba->utrdl_dma_addr), in ufshcd_system_restore()
10198 ufshcd_writel(hba, upper_32_bits(hba->utrdl_dma_addr), in ufshcd_system_restore()
10200 ufshcd_writel(hba, lower_32_bits(hba->utmrdl_dma_addr), in ufshcd_system_restore()
10202 ufshcd_writel(hba, upper_32_bits(hba->utmrdl_dma_addr), in ufshcd_system_restore()
10209 ufshcd_readl(hba, REG_UTP_TASK_REQ_LIST_BASE_H); in ufshcd_system_restore()
10224 * ufshcd_dealloc_host - deallocate Host Bus Adapter (HBA)
10225 * @hba: pointer to Host Bus Adapter (HBA)
10227 void ufshcd_dealloc_host(struct ufs_hba *hba) in ufshcd_dealloc_host() argument
10229 scsi_host_put(hba->host); in ufshcd_dealloc_host()
10234 * ufshcd_set_dma_mask - Set dma mask based on the controller
10236 * @hba: per adapter instance
10238 * Return: 0 for success, non-zero for failure.
10240 static int ufshcd_set_dma_mask(struct ufs_hba *hba) in ufshcd_set_dma_mask() argument
10242 if (hba->vops && hba->vops->set_dma_mask) in ufshcd_set_dma_mask()
10243 return hba->vops->set_dma_mask(hba); in ufshcd_set_dma_mask()
10244 if (hba->capabilities & MASK_64_ADDRESSING_SUPPORT) { in ufshcd_set_dma_mask()
10245 if (!dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(64))) in ufshcd_set_dma_mask()
10248 return dma_set_mask_and_coherent(hba->dev, DMA_BIT_MASK(32)); in ufshcd_set_dma_mask()
10252 * ufshcd_alloc_host - allocate Host Bus Adapter (HBA)
10256 * Return: 0 on success, non-zero value on failure.
10261 struct ufs_hba *hba; in ufshcd_alloc_host() local
10267 err = -ENODEV; in ufshcd_alloc_host()
10275 err = -ENOMEM; in ufshcd_alloc_host()
10278 host->nr_maps = HCTX_TYPE_POLL + 1; in ufshcd_alloc_host()
10279 hba = shost_priv(host); in ufshcd_alloc_host()
10280 hba->host = host; in ufshcd_alloc_host()
10281 hba->dev = dev; in ufshcd_alloc_host()
10282 hba->dev_ref_clk_freq = REF_CLK_FREQ_INVAL; in ufshcd_alloc_host()
10283 hba->nop_out_timeout = NOP_OUT_TIMEOUT; in ufshcd_alloc_host()
10284 ufshcd_set_sg_entry_size(hba, sizeof(struct ufshcd_sg_entry)); in ufshcd_alloc_host()
10285 INIT_LIST_HEAD(&hba->clk_list_head); in ufshcd_alloc_host()
10286 spin_lock_init(&hba->outstanding_lock); in ufshcd_alloc_host()
10288 *hba_handle = hba; in ufshcd_alloc_host()
10308 * ufshcd_init - Driver initialization routine
10309 * @hba: per-adapter instance
10313 * Return: 0 on success, non-zero value on failure.
10315 int ufshcd_init(struct ufs_hba *hba, void __iomem *mmio_base, unsigned int irq) in ufshcd_init() argument
10318 struct Scsi_Host *host = hba->host; in ufshcd_init()
10319 struct device *dev = hba->dev; in ufshcd_init()
10327 dev_set_drvdata(dev, hba); in ufshcd_init()
10330 dev_err(hba->dev, in ufshcd_init()
10332 err = -ENODEV; in ufshcd_init()
10336 hba->mmio_base = mmio_base; in ufshcd_init()
10337 hba->irq = irq; in ufshcd_init()
10338 hba->vps = &ufs_hba_vps; in ufshcd_init()
10340 err = ufshcd_hba_init(hba); in ufshcd_init()
10345 err = ufshcd_hba_capabilities(hba); in ufshcd_init()
10350 hba->ufs_version = ufshcd_get_ufs_version(hba); in ufshcd_init()
10353 hba->intr_mask = ufshcd_get_intr_mask(hba); in ufshcd_init()
10355 err = ufshcd_set_dma_mask(hba); in ufshcd_init()
10357 dev_err(hba->dev, "set dma mask failed\n"); in ufshcd_init()
10362 err = ufshcd_memory_alloc(hba); in ufshcd_init()
10364 dev_err(hba->dev, "Memory allocation failed\n"); in ufshcd_init()
10369 ufshcd_host_memory_configure(hba); in ufshcd_init()
10371 host->can_queue = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_init()
10372 host->cmd_per_lun = hba->nutrs - UFSHCD_NUM_RESERVED; in ufshcd_init()
10373 host->max_id = UFSHCD_MAX_ID; in ufshcd_init()
10374 host->max_lun = UFS_MAX_LUNS; in ufshcd_init()
10375 host->max_channel = UFSHCD_MAX_CHANNEL; in ufshcd_init()
10376 host->unique_id = host->host_no; in ufshcd_init()
10377 host->max_cmd_len = UFS_CDB_SIZE; in ufshcd_init()
10378 host->queuecommand_may_block = !!(hba->caps & UFSHCD_CAP_CLK_GATING); in ufshcd_init()
10380 hba->max_pwr_info.is_valid = false; in ufshcd_init()
10384 hba->host->host_no); in ufshcd_init()
10385 hba->eh_wq = create_singlethread_workqueue(eh_wq_name); in ufshcd_init()
10386 if (!hba->eh_wq) { in ufshcd_init()
10387 dev_err(hba->dev, "%s: failed to create eh workqueue\n", in ufshcd_init()
10389 err = -ENOMEM; in ufshcd_init()
10392 INIT_WORK(&hba->eh_work, ufshcd_err_handler); in ufshcd_init()
10393 INIT_WORK(&hba->eeh_work, ufshcd_exception_event_handler); in ufshcd_init()
10395 sema_init(&hba->host_sem, 1); in ufshcd_init()
10398 mutex_init(&hba->uic_cmd_mutex); in ufshcd_init()
10401 mutex_init(&hba->dev_cmd.lock); in ufshcd_init()
10404 mutex_init(&hba->ee_ctrl_mutex); in ufshcd_init()
10406 mutex_init(&hba->wb_mutex); in ufshcd_init()
10407 init_rwsem(&hba->clk_scaling_lock); in ufshcd_init()
10409 ufshcd_init_clk_gating(hba); in ufshcd_init()
10411 ufshcd_init_clk_scaling(hba); in ufshcd_init()
10418 ufshcd_writel(hba, ufshcd_readl(hba, REG_INTERRUPT_STATUS), in ufshcd_init()
10420 ufshcd_writel(hba, 0, REG_INTERRUPT_ENABLE); in ufshcd_init()
10425 ufshcd_readl(hba, REG_INTERRUPT_ENABLE); in ufshcd_init()
10428 err = devm_request_irq(dev, irq, ufshcd_intr, IRQF_SHARED, UFSHCD, hba); in ufshcd_init()
10430 dev_err(hba->dev, "request irq failed\n"); in ufshcd_init()
10433 hba->is_irq_enabled = true; in ufshcd_init()
10436 if (!is_mcq_supported(hba)) { in ufshcd_init()
10437 if (!hba->lsdb_sup) { in ufshcd_init()
10438 dev_err(hba->dev, "%s: failed to initialize (legacy doorbell mode not supported)\n", in ufshcd_init()
10440 err = -EINVAL; in ufshcd_init()
10443 err = scsi_add_host(host, hba->dev); in ufshcd_init()
10445 dev_err(hba->dev, "scsi_add_host failed\n"); in ufshcd_init()
10448 hba->scsi_host_added = true; in ufshcd_init()
10451 hba->tmf_tag_set = (struct blk_mq_tag_set) { in ufshcd_init()
10453 .queue_depth = hba->nutmrs, in ufshcd_init()
10457 err = blk_mq_alloc_tag_set(&hba->tmf_tag_set); in ufshcd_init()
10460 hba->tmf_queue = blk_mq_init_queue(&hba->tmf_tag_set); in ufshcd_init()
10461 if (IS_ERR(hba->tmf_queue)) { in ufshcd_init()
10462 err = PTR_ERR(hba->tmf_queue); in ufshcd_init()
10465 hba->tmf_rqs = devm_kcalloc(hba->dev, hba->nutmrs, in ufshcd_init()
10466 sizeof(*hba->tmf_rqs), GFP_KERNEL); in ufshcd_init()
10467 if (!hba->tmf_rqs) { in ufshcd_init()
10468 err = -ENOMEM; in ufshcd_init()
10473 ufshcd_device_reset(hba); in ufshcd_init()
10475 ufshcd_init_crypto(hba); in ufshcd_init()
10478 err = ufshcd_hba_enable(hba); in ufshcd_init()
10480 dev_err(hba->dev, "Host controller enable failed\n"); in ufshcd_init()
10481 ufshcd_print_evt_hist(hba); in ufshcd_init()
10482 ufshcd_print_host_state(hba); in ufshcd_init()
10491 hba->rpm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( in ufshcd_init()
10494 hba->spm_lvl = ufs_get_desired_pm_lvl_for_dev_link_state( in ufshcd_init()
10498 INIT_DELAYED_WORK(&hba->rpm_dev_flush_recheck_work, in ufshcd_init()
10501 /* Set the default auto-hiberate idle timer value to 150 ms */ in ufshcd_init()
10502 if (ufshcd_is_auto_hibern8_supported(hba) && !hba->ahit) { in ufshcd_init()
10503 hba->ahit = FIELD_PREP(UFSHCI_AHIBERN8_TIMER_MASK, 150) | in ufshcd_init()
10509 atomic_set(&hba->scsi_block_reqs_cnt, 0); in ufshcd_init()
10511 * We are assuming that device wasn't put in sleep/power-down in ufshcd_init()
10516 ufshcd_set_ufs_dev_active(hba); in ufshcd_init()
10518 async_schedule(ufshcd_async_scan, hba); in ufshcd_init()
10519 ufs_sysfs_add_nodes(hba->dev); in ufshcd_init()
10525 blk_mq_destroy_queue(hba->tmf_queue); in ufshcd_init()
10526 blk_put_queue(hba->tmf_queue); in ufshcd_init()
10528 blk_mq_free_tag_set(&hba->tmf_tag_set); in ufshcd_init()
10530 if (hba->scsi_host_added) in ufshcd_init()
10531 scsi_remove_host(hba->host); in ufshcd_init()
10533 hba->is_irq_enabled = false; in ufshcd_init()
10534 ufshcd_hba_exit(hba); in ufshcd_init()
10542 struct ufs_hba *hba = dev_get_drvdata(dev); in ufshcd_resume_complete() local
10544 if (hba->complete_put) { in ufshcd_resume_complete()
10545 ufshcd_rpm_put(hba); in ufshcd_resume_complete()
10546 hba->complete_put = false; in ufshcd_resume_complete()
10551 static bool ufshcd_rpm_ok_for_spm(struct ufs_hba *hba) in ufshcd_rpm_ok_for_spm() argument
10553 struct device *dev = &hba->ufs_device_wlun->sdev_gendev; in ufshcd_rpm_ok_for_spm()
10559 spin_lock_irqsave(&dev->power.lock, flags); in ufshcd_rpm_ok_for_spm()
10560 dev_pwr_mode = ufs_get_pm_lvl_to_dev_pwr_mode(hba->spm_lvl); in ufshcd_rpm_ok_for_spm()
10561 link_state = ufs_get_pm_lvl_to_link_pwr_state(hba->spm_lvl); in ufshcd_rpm_ok_for_spm()
10563 hba->curr_dev_pwr_mode == dev_pwr_mode && in ufshcd_rpm_ok_for_spm()
10564 hba->uic_link_state == link_state && in ufshcd_rpm_ok_for_spm()
10565 !hba->dev_info.b_rpm_dev_flush_capable; in ufshcd_rpm_ok_for_spm()
10566 spin_unlock_irqrestore(&dev->power.lock, flags); in ufshcd_rpm_ok_for_spm()
10573 struct ufs_hba *hba = dev_get_drvdata(dev); in __ufshcd_suspend_prepare() local
10577 * SCSI assumes that runtime-pm and system-pm for scsi drivers in __ufshcd_suspend_prepare()
10578 * are same. And it doesn't wake up the device for system-suspend in __ufshcd_suspend_prepare()
10582 if (hba->ufs_device_wlun) { in __ufshcd_suspend_prepare()
10584 ufshcd_rpm_get_noresume(hba); in __ufshcd_suspend_prepare()
10589 if (!rpm_ok_for_spm || !ufshcd_rpm_ok_for_spm(hba)) { in __ufshcd_suspend_prepare()
10591 ret = ufshcd_rpm_resume(hba); in __ufshcd_suspend_prepare()
10592 if (ret < 0 && ret != -EACCES) { in __ufshcd_suspend_prepare()
10593 ufshcd_rpm_put(hba); in __ufshcd_suspend_prepare()
10597 hba->complete_put = true; in __ufshcd_suspend_prepare()
10613 struct ufs_hba *hba = shost_priv(sdev->host); in ufshcd_wl_poweroff() local
10615 __ufshcd_wl_suspend(hba, UFS_SHUTDOWN_PM); in ufshcd_wl_poweroff()
10625 return -ENODEV; in ufshcd_wl_probe()
10627 blk_pm_runtime_init(sdev->request_queue, dev); in ufshcd_wl_probe()
10655 * gcc compilers before version 10 cannot do constant-folding for in ufshcd_check_header_layout()
10656 * sub-byte bitfields. Hence skip the layout checks for gcc 9 and in ufshcd_check_header_layout()
10700 * ufs_dev_wlun_template - describes ufs device wlun
10701 * ufs-device wlun - used to send pm commands
10702 * All luns are consumers of ufs-device wlun.
10706 * With ufs design, SSU should be sent to ufs-device wlun.