/* SPDX-License-Identifier: GPL-2.0-or-later */1/*2* Universal Flash Storage Host controller driver3* Copyright (C) 2011-2013 Samsung India Software Operations4* Copyright (c) 2013-2016, The Linux Foundation. All rights reserved.5*6* Authors:7* Santosh Yaraganavi <[email protected]>8* Vinayak Holikatti <[email protected]>9*/1011#ifndef _UFSHCD_H12#define _UFSHCD_H1314#include <linux/bitfield.h>15#include <linux/blk-crypto-profile.h>16#include <linux/blk-mq.h>17#include <linux/devfreq.h>18#include <linux/fault-inject.h>19#include <linux/debugfs.h>20#include <linux/msi.h>21#include <linux/pm_runtime.h>22#include <linux/dma-direction.h>23#include <scsi/scsi_device.h>24#include <scsi/scsi_host.h>25#include <ufs/unipro.h>26#include <ufs/ufs.h>27#include <ufs/ufs_quirks.h>28#include <ufs/ufshci.h>2930#define UFSHCD "ufshcd"3132struct scsi_device;33struct ufs_hba;3435enum dev_cmd_type {36DEV_CMD_TYPE_NOP = 0x0,37DEV_CMD_TYPE_QUERY = 0x1,38DEV_CMD_TYPE_RPMB = 0x2,39};4041enum ufs_event_type {42/* uic specific errors */43UFS_EVT_PA_ERR = 0,44UFS_EVT_DL_ERR,45UFS_EVT_NL_ERR,46UFS_EVT_TL_ERR,47UFS_EVT_DME_ERR,4849/* fatal errors */50UFS_EVT_AUTO_HIBERN8_ERR,51UFS_EVT_FATAL_ERR,52UFS_EVT_LINK_STARTUP_FAIL,53UFS_EVT_RESUME_ERR,54UFS_EVT_SUSPEND_ERR,55UFS_EVT_WL_SUSP_ERR,56UFS_EVT_WL_RES_ERR,5758/* abnormal events */59UFS_EVT_DEV_RESET,60UFS_EVT_HOST_RESET,61UFS_EVT_ABORT,6263UFS_EVT_CNT,64};6566/**67* struct uic_command - UIC command structure68* @command: UIC command69* @argument1: UIC command argument 170* @argument2: UIC command argument 271* @argument3: UIC command argument 372* @cmd_active: Indicate if UIC command is outstanding73* @done: UIC command completion74*/75struct uic_command {76const u32 command;77const u32 argument1;78u32 argument2;79u32 argument3;80int cmd_active;81struct completion done;82};8384/* Used to differentiate the power management options */85enum ufs_pm_op {86UFS_RUNTIME_PM,87UFS_SYSTEM_PM,88UFS_SHUTDOWN_PM,89};9091/* Host <-> Device UniPro Link state */92enum uic_link_state {93UIC_LINK_OFF_STATE = 0, /* Link powered down or disabled */94UIC_LINK_ACTIVE_STATE = 1, /* Link is in Fast/Slow/Sleep state */95UIC_LINK_HIBERN8_STATE = 2, /* Link is in Hibernate state */96UIC_LINK_BROKEN_STATE = 3, /* Link is in broken state */97};9899#define ufshcd_is_link_off(hba) ((hba)->uic_link_state == UIC_LINK_OFF_STATE)100#define ufshcd_is_link_active(hba) ((hba)->uic_link_state == \101UIC_LINK_ACTIVE_STATE)102#define ufshcd_is_link_hibern8(hba) ((hba)->uic_link_state == \103UIC_LINK_HIBERN8_STATE)104#define ufshcd_is_link_broken(hba) ((hba)->uic_link_state == \105UIC_LINK_BROKEN_STATE)106#define ufshcd_set_link_off(hba) ((hba)->uic_link_state = UIC_LINK_OFF_STATE)107#define ufshcd_set_link_active(hba) ((hba)->uic_link_state = \108UIC_LINK_ACTIVE_STATE)109#define ufshcd_set_link_hibern8(hba) ((hba)->uic_link_state = \110UIC_LINK_HIBERN8_STATE)111#define ufshcd_set_link_broken(hba) ((hba)->uic_link_state = \112UIC_LINK_BROKEN_STATE)113114#define ufshcd_set_ufs_dev_active(h) \115((h)->curr_dev_pwr_mode = UFS_ACTIVE_PWR_MODE)116#define ufshcd_set_ufs_dev_sleep(h) \117((h)->curr_dev_pwr_mode = UFS_SLEEP_PWR_MODE)118#define ufshcd_set_ufs_dev_poweroff(h) \119((h)->curr_dev_pwr_mode = UFS_POWERDOWN_PWR_MODE)120#define ufshcd_set_ufs_dev_deepsleep(h) \121((h)->curr_dev_pwr_mode = UFS_DEEPSLEEP_PWR_MODE)122#define ufshcd_is_ufs_dev_active(h) \123((h)->curr_dev_pwr_mode == UFS_ACTIVE_PWR_MODE)124#define ufshcd_is_ufs_dev_sleep(h) \125((h)->curr_dev_pwr_mode == UFS_SLEEP_PWR_MODE)126#define ufshcd_is_ufs_dev_poweroff(h) \127((h)->curr_dev_pwr_mode == UFS_POWERDOWN_PWR_MODE)128#define ufshcd_is_ufs_dev_deepsleep(h) \129((h)->curr_dev_pwr_mode == UFS_DEEPSLEEP_PWR_MODE)130131/*132* UFS Power management levels.133* Each level is in increasing order of power savings, except DeepSleep134* which is lower than PowerDown with power on but not PowerDown with135* power off.136*/137enum ufs_pm_level {138UFS_PM_LVL_0,139UFS_PM_LVL_1,140UFS_PM_LVL_2,141UFS_PM_LVL_3,142UFS_PM_LVL_4,143UFS_PM_LVL_5,144UFS_PM_LVL_6,145UFS_PM_LVL_MAX146};147148struct ufs_pm_lvl_states {149enum ufs_dev_pwr_mode dev_state;150enum uic_link_state link_state;151};152153/**154* struct ufshcd_lrb - local reference block155* @utr_descriptor_ptr: UTRD address of the command156* @ucd_req_ptr: UCD address of the command157* @ucd_rsp_ptr: Response UPIU address for this command158* @ucd_prdt_ptr: PRDT address of the command159* @utrd_dma_addr: UTRD dma address for debug160* @ucd_prdt_dma_addr: PRDT dma address for debug161* @ucd_rsp_dma_addr: UPIU response dma address for debug162* @ucd_req_dma_addr: UPIU request dma address for debug163* @cmd: pointer to SCSI command164* @scsi_status: SCSI status of the command165* @command_type: SCSI, UFS, Query.166* @task_tag: Task tag of the command167* @lun: LUN of the command168* @intr_cmd: Interrupt command (doesn't participate in interrupt aggregation)169* @issue_time_stamp: time stamp for debug purposes (CLOCK_MONOTONIC)170* @issue_time_stamp_local_clock: time stamp for debug purposes (local_clock)171* @compl_time_stamp: time stamp for statistics (CLOCK_MONOTONIC)172* @compl_time_stamp_local_clock: time stamp for debug purposes (local_clock)173* @crypto_key_slot: the key slot to use for inline crypto (-1 if none)174* @data_unit_num: the data unit number for the first block for inline crypto175* @req_abort_skip: skip request abort task flag176*/177struct ufshcd_lrb {178struct utp_transfer_req_desc *utr_descriptor_ptr;179struct utp_upiu_req *ucd_req_ptr;180struct utp_upiu_rsp *ucd_rsp_ptr;181struct ufshcd_sg_entry *ucd_prdt_ptr;182183dma_addr_t utrd_dma_addr;184dma_addr_t ucd_req_dma_addr;185dma_addr_t ucd_rsp_dma_addr;186dma_addr_t ucd_prdt_dma_addr;187188struct scsi_cmnd *cmd;189int scsi_status;190191int command_type;192int task_tag;193u8 lun; /* UPIU LUN id field is only 8-bit wide */194bool intr_cmd;195ktime_t issue_time_stamp;196u64 issue_time_stamp_local_clock;197ktime_t compl_time_stamp;198u64 compl_time_stamp_local_clock;199#ifdef CONFIG_SCSI_UFS_CRYPTO200int crypto_key_slot;201u64 data_unit_num;202#endif203204bool req_abort_skip;205};206207/**208* struct ufs_query_req - parameters for building a query request209* @query_func: UPIU header query function210* @upiu_req: the query request data211*/212struct ufs_query_req {213u8 query_func;214struct utp_upiu_query upiu_req;215};216217/**218* struct ufs_query_resp - UPIU QUERY219* @response: device response code220* @upiu_res: query response data221*/222struct ufs_query_res {223struct utp_upiu_query upiu_res;224};225226/**227* struct ufs_query - holds relevant data structures for query request228* @request: request upiu and function229* @descriptor: buffer for sending/receiving descriptor230* @response: response upiu and response231*/232struct ufs_query {233struct ufs_query_req request;234u8 *descriptor;235struct ufs_query_res response;236};237238/**239* struct ufs_dev_cmd - all assosiated fields with device management commands240* @type: device management command type - Query, NOP OUT241* @lock: lock to allow one command at a time242* @complete: internal commands completion243* @query: Device management query information244*/245struct ufs_dev_cmd {246enum dev_cmd_type type;247struct mutex lock;248struct completion complete;249struct ufs_query query;250};251252/**253* struct ufs_clk_info - UFS clock related info254* @list: list headed by hba->clk_list_head255* @clk: clock node256* @name: clock name257* @max_freq: maximum frequency supported by the clock258* @min_freq: min frequency that can be used for clock scaling259* @curr_freq: indicates the current frequency that it is set to260* @keep_link_active: indicates that the clk should not be disabled if261* link is active262* @enabled: variable to check against multiple enable/disable263*/264struct ufs_clk_info {265struct list_head list;266struct clk *clk;267const char *name;268u32 max_freq;269u32 min_freq;270u32 curr_freq;271bool keep_link_active;272bool enabled;273};274275enum ufs_notify_change_status {276PRE_CHANGE,277POST_CHANGE,278};279280struct ufs_pa_layer_attr {281u32 gear_rx;282u32 gear_tx;283u32 lane_rx;284u32 lane_tx;285u32 pwr_rx;286u32 pwr_tx;287u32 hs_rate;288};289290struct ufs_pwr_mode_info {291bool is_valid;292struct ufs_pa_layer_attr info;293};294295/**296* struct ufs_hba_variant_ops - variant specific callbacks297* @name: variant name298* @max_num_rtt: maximum RTT supported by the host299* @init: called when the driver is initialized300* @exit: called to cleanup everything done in init301* @set_dma_mask: For setting another DMA mask than indicated by the 64AS302* capability bit.303* @get_ufs_hci_version: called to get UFS HCI version304* @clk_scale_notify: notifies that clks are scaled up/down305* @setup_clocks: called before touching any of the controller registers306* @hce_enable_notify: called before and after HCE enable bit is set to allow307* variant specific Uni-Pro initialization.308* @link_startup_notify: called before and after Link startup is carried out309* to allow variant specific Uni-Pro initialization.310* @pwr_change_notify: called before and after a power mode change311* is carried out to allow vendor spesific capabilities312* to be set. PRE_CHANGE can modify final_params based313* on desired_pwr_mode, but POST_CHANGE must not alter314* the final_params parameter315* @setup_xfer_req: called before any transfer request is issued316* to set some things317* @setup_task_mgmt: called before any task management request is issued318* to set some things319* @hibern8_notify: called around hibern8 enter/exit320* @apply_dev_quirks: called to apply device specific quirks321* @fixup_dev_quirks: called to modify device specific quirks322* @suspend: called during host controller PM callback323* @resume: called during host controller PM callback324* @dbg_register_dump: used to dump controller debug information325* @phy_initialization: used to initialize phys326* @device_reset: called to issue a reset pulse on the UFS device327* @config_scaling_param: called to configure clock scaling parameters328* @fill_crypto_prdt: initialize crypto-related fields in the PRDT329* @event_notify: called to notify important events330* @mcq_config_resource: called to configure MCQ platform resources331* @get_hba_mac: reports maximum number of outstanding commands supported by332* the controller. Should be implemented for UFSHCI 4.0 or later333* controllers that are not compliant with the UFSHCI 4.0 specification.334* @op_runtime_config: called to config Operation and runtime regs Pointers335* @get_outstanding_cqs: called to get outstanding completion queues336* @config_esi: called to config Event Specific Interrupt337* @config_scsi_dev: called to configure SCSI device parameters338* @freq_to_gear_speed: called to map clock frequency to the max supported gear speed339*/340struct ufs_hba_variant_ops {341const char *name;342int max_num_rtt;343int (*init)(struct ufs_hba *);344void (*exit)(struct ufs_hba *);345u32 (*get_ufs_hci_version)(struct ufs_hba *);346int (*set_dma_mask)(struct ufs_hba *);347int (*clk_scale_notify)(struct ufs_hba *, bool, unsigned long,348enum ufs_notify_change_status);349int (*setup_clocks)(struct ufs_hba *, bool,350enum ufs_notify_change_status);351int (*hce_enable_notify)(struct ufs_hba *,352enum ufs_notify_change_status);353int (*link_startup_notify)(struct ufs_hba *,354enum ufs_notify_change_status);355int (*pwr_change_notify)(struct ufs_hba *,356enum ufs_notify_change_status status,357const struct ufs_pa_layer_attr *desired_pwr_mode,358struct ufs_pa_layer_attr *final_params);359void (*setup_xfer_req)(struct ufs_hba *hba, int tag,360bool is_scsi_cmd);361void (*setup_task_mgmt)(struct ufs_hba *, int, u8);362void (*hibern8_notify)(struct ufs_hba *, enum uic_cmd_dme,363enum ufs_notify_change_status);364int (*apply_dev_quirks)(struct ufs_hba *hba);365void (*fixup_dev_quirks)(struct ufs_hba *hba);366int (*suspend)(struct ufs_hba *, enum ufs_pm_op,367enum ufs_notify_change_status);368int (*resume)(struct ufs_hba *, enum ufs_pm_op);369void (*dbg_register_dump)(struct ufs_hba *hba);370int (*phy_initialization)(struct ufs_hba *);371int (*device_reset)(struct ufs_hba *hba);372void (*config_scaling_param)(struct ufs_hba *hba,373struct devfreq_dev_profile *profile,374struct devfreq_simple_ondemand_data *data);375int (*fill_crypto_prdt)(struct ufs_hba *hba,376const struct bio_crypt_ctx *crypt_ctx,377void *prdt, unsigned int num_segments);378void (*event_notify)(struct ufs_hba *hba,379enum ufs_event_type evt, void *data);380int (*mcq_config_resource)(struct ufs_hba *hba);381int (*get_hba_mac)(struct ufs_hba *hba);382int (*op_runtime_config)(struct ufs_hba *hba);383int (*get_outstanding_cqs)(struct ufs_hba *hba,384unsigned long *ocqs);385int (*config_esi)(struct ufs_hba *hba);386void (*config_scsi_dev)(struct scsi_device *sdev);387u32 (*freq_to_gear_speed)(struct ufs_hba *hba, unsigned long freq);388};389390/* clock gating state */391enum clk_gating_state {392CLKS_OFF,393CLKS_ON,394REQ_CLKS_OFF,395REQ_CLKS_ON,396};397398/**399* struct ufs_clk_gating - UFS clock gating related info400* @gate_work: worker to turn off clocks after some delay as specified in401* delay_ms402* @ungate_work: worker to turn on clocks that will be used in case of403* interrupt context404* @clk_gating_workq: workqueue for clock gating work.405* @lock: serialize access to some struct ufs_clk_gating members. An outer lock406* relative to the host lock407* @state: the current clocks state408* @delay_ms: gating delay in ms409* @is_suspended: clk gating is suspended when set to 1 which can be used410* during suspend/resume411* @delay_attr: sysfs attribute to control delay_attr412* @enable_attr: sysfs attribute to enable/disable clock gating413* @is_enabled: Indicates the current status of clock gating414* @is_initialized: Indicates whether clock gating is initialized or not415* @active_reqs: number of requests that are pending and should be waited for416* completion before gating clocks.417*/418struct ufs_clk_gating {419struct delayed_work gate_work;420struct work_struct ungate_work;421struct workqueue_struct *clk_gating_workq;422423spinlock_t lock;424425enum clk_gating_state state;426unsigned long delay_ms;427bool is_suspended;428struct device_attribute delay_attr;429struct device_attribute enable_attr;430bool is_enabled;431bool is_initialized;432int active_reqs;433};434435/**436* struct ufs_clk_scaling - UFS clock scaling related data437* @workq: workqueue to schedule devfreq suspend/resume work438* @suspend_work: worker to suspend devfreq439* @resume_work: worker to resume devfreq440* @lock: serialize access to some struct ufs_clk_scaling members441* @active_reqs: number of requests that are pending. If this is zero when442* devfreq ->target() function is called then schedule "suspend_work" to443* suspend devfreq.444* @tot_busy_t: Total busy time in current polling window445* @window_start_t: Start time (in jiffies) of the current polling window446* @busy_start_t: Start time of current busy period447* @enable_attr: sysfs attribute to enable/disable clock scaling448* @saved_pwr_info: UFS power mode may also be changed during scaling and this449* one keeps track of previous power mode.450* @target_freq: frequency requested by devfreq framework451* @min_gear: lowest HS gear to scale down to452* @wb_gear: enable Write Booster when HS gear scales above or equal to it, else453* disable Write Booster454* @is_enabled: tracks if scaling is currently enabled or not, controlled by455* clkscale_enable sysfs node456* @is_allowed: tracks if scaling is currently allowed or not, used to block457* clock scaling which is not invoked from devfreq governor458* @is_initialized: Indicates whether clock scaling is initialized or not459* @is_busy_started: tracks if busy period has started or not460* @is_suspended: tracks if devfreq is suspended or not461*/462struct ufs_clk_scaling {463struct workqueue_struct *workq;464struct work_struct suspend_work;465struct work_struct resume_work;466467spinlock_t lock;468469int active_reqs;470unsigned long tot_busy_t;471ktime_t window_start_t;472ktime_t busy_start_t;473struct device_attribute enable_attr;474struct ufs_pa_layer_attr saved_pwr_info;475unsigned long target_freq;476u32 min_gear;477u32 wb_gear;478bool is_enabled;479bool is_allowed;480bool is_initialized;481bool is_busy_started;482bool is_suspended;483bool suspend_on_no_request;484};485486#define UFS_EVENT_HIST_LENGTH 8487/**488* struct ufs_event_hist - keeps history of errors489* @pos: index to indicate cyclic buffer position490* @val: cyclic buffer for registers value491* @tstamp: cyclic buffer for time stamp492* @cnt: error counter493*/494struct ufs_event_hist {495int pos;496u32 val[UFS_EVENT_HIST_LENGTH];497u64 tstamp[UFS_EVENT_HIST_LENGTH];498unsigned long long cnt;499};500501/**502* struct ufs_stats - keeps usage/err statistics503* @hibern8_exit_cnt: Counter to keep track of number of exits,504* reset this after link-startup.505* @last_hibern8_exit_tstamp: Set time after the hibern8 exit.506* Clear after the first successful command completion.507* @event: array with event history.508*/509struct ufs_stats {510u32 hibern8_exit_cnt;511u64 last_hibern8_exit_tstamp;512struct ufs_event_hist event[UFS_EVT_CNT];513};514515/**516* enum ufshcd_state - UFS host controller state517* @UFSHCD_STATE_RESET: Link is not operational. Postpone SCSI command518* processing.519* @UFSHCD_STATE_OPERATIONAL: The host controller is operational and can process520* SCSI commands.521* @UFSHCD_STATE_EH_SCHEDULED_NON_FATAL: The error handler has been scheduled.522* SCSI commands may be submitted to the controller.523* @UFSHCD_STATE_EH_SCHEDULED_FATAL: The error handler has been scheduled. Fail524* newly submitted SCSI commands with error code DID_BAD_TARGET.525* @UFSHCD_STATE_ERROR: An unrecoverable error occurred, e.g. link recovery526* failed. Fail all SCSI commands with error code DID_ERROR.527*/528enum ufshcd_state {529UFSHCD_STATE_RESET,530UFSHCD_STATE_OPERATIONAL,531UFSHCD_STATE_EH_SCHEDULED_NON_FATAL,532UFSHCD_STATE_EH_SCHEDULED_FATAL,533UFSHCD_STATE_ERROR,534};535536enum ufshcd_quirks {537/* Interrupt aggregation support is broken */538UFSHCD_QUIRK_BROKEN_INTR_AGGR = 1 << 0,539540/*541* delay before each dme command is required as the unipro542* layer has shown instabilities543*/544UFSHCD_QUIRK_DELAY_BEFORE_DME_CMDS = 1 << 1,545546/*547* If UFS host controller is having issue in processing LCC (Line548* Control Command) coming from device then enable this quirk.549* When this quirk is enabled, host controller driver should disable550* the LCC transmission on UFS device (by clearing TX_LCC_ENABLE551* attribute of device to 0).552*/553UFSHCD_QUIRK_BROKEN_LCC = 1 << 2,554555/*556* The attribute PA_RXHSUNTERMCAP specifies whether or not the557* inbound Link supports unterminated line in HS mode. Setting this558* attribute to 1 fixes moving to HS gear.559*/560UFSHCD_QUIRK_BROKEN_PA_RXHSUNTERMCAP = 1 << 3,561562/*563* This quirk needs to be enabled if the host controller only allows564* accessing the peer dme attributes in AUTO mode (FAST AUTO or565* SLOW AUTO).566*/567UFSHCD_QUIRK_DME_PEER_ACCESS_AUTO_MODE = 1 << 4,568569/*570* This quirk needs to be enabled if the host controller doesn't571* advertise the correct version in UFS_VER register. If this quirk572* is enabled, standard UFS host driver will call the vendor specific573* ops (get_ufs_hci_version) to get the correct version.574*/575UFSHCD_QUIRK_BROKEN_UFS_HCI_VERSION = 1 << 5,576577/*578* Clear handling for transfer/task request list is just opposite.579*/580UFSHCI_QUIRK_BROKEN_REQ_LIST_CLR = 1 << 6,581582/*583* This quirk needs to be enabled if host controller doesn't allow584* that the interrupt aggregation timer and counter are reset by s/w.585*/586UFSHCI_QUIRK_SKIP_RESET_INTR_AGGR = 1 << 7,587588/*589* This quirks needs to be enabled if host controller cannot be590* enabled via HCE register.591*/592UFSHCI_QUIRK_BROKEN_HCE = 1 << 8,593594/*595* This quirk needs to be enabled if the host controller regards596* resolution of the values of PRDTO and PRDTL in UTRD as byte.597*/598UFSHCD_QUIRK_PRDT_BYTE_GRAN = 1 << 9,599600/*601* This quirk needs to be enabled if the host controller reports602* OCS FATAL ERROR with device error through sense data603*/604UFSHCD_QUIRK_BROKEN_OCS_FATAL_ERROR = 1 << 10,605606/*607* This quirk needs to be enabled if the host controller has608* auto-hibernate capability but it doesn't work.609*/610UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8 = 1 << 11,611612/*613* This quirk needs to disable manual flush for write booster614*/615UFSHCI_QUIRK_SKIP_MANUAL_WB_FLUSH_CTRL = 1 << 12,616617/*618* This quirk needs to disable unipro timeout values619* before power mode change620*/621UFSHCD_QUIRK_SKIP_DEF_UNIPRO_TIMEOUT_SETTING = 1 << 13,622623/*624* This quirk needs to be enabled if the host controller does not625* support UIC command626*/627UFSHCD_QUIRK_BROKEN_UIC_CMD = 1 << 15,628629/*630* This quirk needs to be enabled if the host controller cannot631* support physical host configuration.632*/633UFSHCD_QUIRK_SKIP_PH_CONFIGURATION = 1 << 16,634635/*636* This quirk needs to be enabled if the host controller has637* auto-hibernate capability but it's FASTAUTO only.638*/639UFSHCD_QUIRK_HIBERN_FASTAUTO = 1 << 18,640641/*642* This quirk needs to be enabled if the host controller needs643* to reinit the device after switching to maximum gear.644*/645UFSHCD_QUIRK_REINIT_AFTER_MAX_GEAR_SWITCH = 1 << 19,646647/*648* Some host raises interrupt (per queue) in addition to649* CQES (traditional) when ESI is disabled.650* Enable this quirk will disable CQES and use per queue interrupt.651*/652UFSHCD_QUIRK_MCQ_BROKEN_INTR = 1 << 20,653654/*655* Some host does not implement SQ Run Time Command (SQRTC) register656* thus need this quirk to skip related flow.657*/658UFSHCD_QUIRK_MCQ_BROKEN_RTC = 1 << 21,659660/*661* This quirk needs to be enabled if the host controller supports inline662* encryption but it needs to initialize the crypto capabilities in a663* nonstandard way and/or needs to override blk_crypto_ll_ops. If664* enabled, the standard code won't initialize the blk_crypto_profile;665* ufs_hba_variant_ops::init() must do it instead.666*/667UFSHCD_QUIRK_CUSTOM_CRYPTO_PROFILE = 1 << 22,668669/*670* This quirk needs to be enabled if the host controller supports inline671* encryption but does not support the CRYPTO_GENERAL_ENABLE bit, i.e.672* host controller initialization fails if that bit is set.673*/674UFSHCD_QUIRK_BROKEN_CRYPTO_ENABLE = 1 << 23,675676/*677* This quirk needs to be enabled if the host controller driver copies678* cryptographic keys into the PRDT in order to send them to hardware,679* and therefore the PRDT should be zeroized after each request (as per680* the standard best practice for managing keys).681*/682UFSHCD_QUIRK_KEYS_IN_PRDT = 1 << 24,683684/*685* This quirk indicates that the controller reports the value 1 (not686* supported) in the Legacy Single DoorBell Support (LSDBS) bit of the687* Controller Capabilities register although it supports the legacy688* single doorbell mode.689*/690UFSHCD_QUIRK_BROKEN_LSDBS_CAP = 1 << 25,691};692693enum ufshcd_caps {694/* Allow dynamic clk gating */695UFSHCD_CAP_CLK_GATING = 1 << 0,696697/* Allow hiberb8 with clk gating */698UFSHCD_CAP_HIBERN8_WITH_CLK_GATING = 1 << 1,699700/* Allow dynamic clk scaling */701UFSHCD_CAP_CLK_SCALING = 1 << 2,702703/* Allow auto bkops to enabled during runtime suspend */704UFSHCD_CAP_AUTO_BKOPS_SUSPEND = 1 << 3,705706/*707* This capability allows host controller driver to use the UFS HCI's708* interrupt aggregation capability.709* CAUTION: Enabling this might reduce overall UFS throughput.710*/711UFSHCD_CAP_INTR_AGGR = 1 << 4,712713/*714* This capability allows the device auto-bkops to be always enabled715* except during suspend (both runtime and suspend).716* Enabling this capability means that device will always be allowed717* to do background operation when it's active but it might degrade718* the performance of ongoing read/write operations.719*/720UFSHCD_CAP_KEEP_AUTO_BKOPS_ENABLED_EXCEPT_SUSPEND = 1 << 5,721722/*723* This capability allows host controller driver to automatically724* enable runtime power management by itself instead of waiting725* for userspace to control the power management.726*/727UFSHCD_CAP_RPM_AUTOSUSPEND = 1 << 6,728729/*730* This capability allows the host controller driver to turn-on731* WriteBooster, if the underlying device supports it and is732* provisioned to be used. This would increase the write performance.733*/734UFSHCD_CAP_WB_EN = 1 << 7,735736/*737* This capability allows the host controller driver to use the738* inline crypto engine, if it is present739*/740UFSHCD_CAP_CRYPTO = 1 << 8,741742/*743* This capability allows the controller regulators to be put into744* lpm mode aggressively during clock gating.745* This would increase power savings.746*/747UFSHCD_CAP_AGGR_POWER_COLLAPSE = 1 << 9,748749/*750* This capability allows the host controller driver to use DeepSleep,751* if it is supported by the UFS device. The host controller driver must752* support device hardware reset via the hba->device_reset() callback,753* in order to exit DeepSleep state.754*/755UFSHCD_CAP_DEEPSLEEP = 1 << 10,756757/*758* This capability allows the host controller driver to use temperature759* notification if it is supported by the UFS device.760*/761UFSHCD_CAP_TEMP_NOTIF = 1 << 11,762763/*764* Enable WriteBooster when scaling up the clock and disable765* WriteBooster when scaling the clock down.766*/767UFSHCD_CAP_WB_WITH_CLK_SCALING = 1 << 12,768};769770struct ufs_hba_variant_params {771struct devfreq_dev_profile devfreq_profile;772struct devfreq_simple_ondemand_data ondemand_data;773u16 hba_enable_delay_us;774u32 wb_flush_threshold;775};776777struct ufs_hba_monitor {778unsigned long chunk_size;779780unsigned long nr_sec_rw[2];781ktime_t total_busy[2];782783unsigned long nr_req[2];784/* latencies*/785ktime_t lat_sum[2];786ktime_t lat_max[2];787ktime_t lat_min[2];788789u32 nr_queued[2];790ktime_t busy_start_ts[2];791792ktime_t enabled_ts;793bool enabled;794};795796/**797* struct ufshcd_res_info_t - MCQ related resource regions798*799* @name: resource name800* @resource: pointer to resource region801* @base: register base address802*/803struct ufshcd_res_info {804const char *name;805struct resource *resource;806void __iomem *base;807};808809enum ufshcd_res {810RES_UFS,811RES_MCQ,812RES_MCQ_SQD,813RES_MCQ_SQIS,814RES_MCQ_CQD,815RES_MCQ_CQIS,816RES_MCQ_VS,817RES_MAX,818};819820/**821* struct ufshcd_mcq_opr_info_t - Operation and Runtime registers822*823* @offset: Doorbell Address Offset824* @stride: Steps proportional to queue [0...31]825* @base: base address826*/827struct ufshcd_mcq_opr_info_t {828unsigned long offset;829unsigned long stride;830void __iomem *base;831};832833enum ufshcd_mcq_opr {834OPR_SQD,835OPR_SQIS,836OPR_CQD,837OPR_CQIS,838OPR_MAX,839};840841/**842* struct ufs_hba - per adapter private structure843* @mmio_base: UFSHCI base register address844* @ucdl_base_addr: UFS Command Descriptor base address845* @utrdl_base_addr: UTP Transfer Request Descriptor base address846* @utmrdl_base_addr: UTP Task Management Descriptor base address847* @ucdl_dma_addr: UFS Command Descriptor DMA address848* @utrdl_dma_addr: UTRDL DMA address849* @utmrdl_dma_addr: UTMRDL DMA address850* @host: Scsi_Host instance of the driver851* @dev: device handle852* @ufs_device_wlun: WLUN that controls the entire UFS device.853* @hwmon_device: device instance registered with the hwmon core.854* @curr_dev_pwr_mode: active UFS device power mode.855* @uic_link_state: active state of the link to the UFS device.856* @rpm_lvl: desired UFS power management level during runtime PM.857* @spm_lvl: desired UFS power management level during system PM.858* @pm_op_in_progress: whether or not a PM operation is in progress.859* @ahit: value of Auto-Hibernate Idle Timer register.860* @lrb: local reference block861* @outstanding_tasks: Bits representing outstanding task requests862* @outstanding_lock: Protects @outstanding_reqs.863* @outstanding_reqs: Bits representing outstanding transfer requests864* @capabilities: UFS Controller Capabilities865* @mcq_capabilities: UFS Multi Circular Queue capabilities866* @nutrs: Transfer Request Queue depth supported by controller867* @nortt - Max outstanding RTTs supported by controller868* @nutmrs: Task Management Queue depth supported by controller869* @reserved_slot: Used to submit device commands. Protected by @dev_cmd.lock.870* @ufs_version: UFS Version to which controller complies871* @vops: pointer to variant specific operations872* @vps: pointer to variant specific parameters873* @priv: pointer to variant specific private data874* @sg_entry_size: size of struct ufshcd_sg_entry (may include variant fields)875* @irq: Irq number of the controller876* @is_irq_enabled: whether or not the UFS controller interrupt is enabled.877* @dev_ref_clk_freq: reference clock frequency878* @quirks: bitmask with information about deviations from the UFSHCI standard.879* @dev_quirks: bitmask with information about deviations from the UFS standard.880* @tmf_tag_set: TMF tag set.881* @tmf_queue: Used to allocate TMF tags.882* @tmf_rqs: array with pointers to TMF requests while these are in progress.883* @active_uic_cmd: pointer to active UIC command.884* @uic_cmd_mutex: mutex used for serializing UIC command processing.885* @uic_async_done: completion used to wait for power mode or hibernation state886* changes.887* @ufshcd_state: UFSHCD state888* @eh_flags: Error handling flags889* @intr_mask: Interrupt Mask Bits890* @ee_ctrl_mask: Exception event control mask891* @ee_drv_mask: Exception event mask for driver892* @ee_usr_mask: Exception event mask for user (set via debugfs)893* @ee_ctrl_mutex: Used to serialize exception event information.894* @is_powered: flag to check if HBA is powered895* @shutting_down: flag to check if shutdown has been invoked896* @host_sem: semaphore used to serialize concurrent contexts897* @eh_wq: Workqueue that eh_work works on898* @eh_work: Worker to handle UFS errors that require s/w attention899* @eeh_work: Worker to handle exception events900* @errors: HBA errors901* @uic_error: UFS interconnect layer error status902* @saved_err: sticky error mask903* @saved_uic_err: sticky UIC error mask904* @ufs_stats: various error counters905* @force_reset: flag to force eh_work perform a full reset906* @force_pmc: flag to force a power mode change907* @silence_err_logs: flag to silence error logs908* @dev_cmd: ufs device management command information909* @last_dme_cmd_tstamp: time stamp of the last completed DME command910* @nop_out_timeout: NOP OUT timeout value911* @dev_info: information about the UFS device912* @auto_bkops_enabled: to track whether bkops is enabled in device913* @vreg_info: UFS device voltage regulator information914* @clk_list_head: UFS host controller clocks list node head915* @use_pm_opp: Indicates whether OPP based scaling is used or not916* @req_abort_count: number of times ufshcd_abort() has been called917* @lanes_per_direction: number of lanes per data direction between the UFS918* controller and the UFS device.919* @pwr_info: holds current power mode920* @max_pwr_info: keeps the device max valid pwm921* @clk_gating: information related to clock gating922* @caps: bitmask with information about UFS controller capabilities923* @devfreq: frequency scaling information owned by the devfreq core924* @clk_scaling: frequency scaling information owned by the UFS driver925* @system_suspending: system suspend has been started and system resume has926* not yet finished.927* @is_sys_suspended: UFS device has been suspended because of system suspend928* @urgent_bkops_lvl: keeps track of urgent bkops level for device929* @is_urgent_bkops_lvl_checked: keeps track if the urgent bkops level for930* device is known or not.931* @wb_mutex: used to serialize devfreq and sysfs write booster toggling932* @clk_scaling_lock: used to serialize device commands and clock scaling933* @desc_size: descriptor sizes reported by device934* @bsg_dev: struct device associated with the BSG queue935* @bsg_queue: BSG queue associated with the UFS controller936* @rpm_dev_flush_recheck_work: used to suspend from RPM (runtime power937* management) after the UFS device has finished a WriteBooster buffer938* flush or auto BKOP.939* @monitor: statistics about UFS commands940* @crypto_capabilities: Content of crypto capabilities register (0x100)941* @crypto_cap_array: Array of crypto capabilities942* @crypto_cfg_register: Start of the crypto cfg array943* @crypto_profile: the crypto profile of this hba (if applicable)944* @debugfs_root: UFS controller debugfs root directory945* @debugfs_ee_work: used to restore ee_ctrl_mask after a delay946* @debugfs_ee_rate_limit_ms: user configurable delay after which to restore947* ee_ctrl_mask948* @luns_avail: number of regular and well known LUNs supported by the UFS949* device950* @nr_hw_queues: number of hardware queues configured951* @nr_queues: number of Queues of different queue types952* @complete_put: whether or not to call ufshcd_rpm_put() from inside953* ufshcd_resume_complete()954* @mcq_sup: is mcq supported by UFSHC955* @mcq_enabled: is mcq ready to accept requests956* @mcq_esi_enabled: is mcq ESI configured957* @res: array of resource info of MCQ registers958* @mcq_base: Multi circular queue registers base address959* @uhq: array of supported hardware queues960* @dev_cmd_queue: Queue for issuing device management commands961* @mcq_opr: MCQ operation and runtime registers962* @ufs_rtc_update_work: A work for UFS RTC periodic update963* @pm_qos_req: PM QoS request handle964* @pm_qos_enabled: flag to check if pm qos is enabled965* @critical_health_count: count of critical health exceptions966* @dev_lvl_exception_count: count of device level exceptions since last reset967* @dev_lvl_exception_id: vendor specific information about the968* device level exception event.969*/970struct ufs_hba {971void __iomem *mmio_base;972973/* Virtual memory reference */974struct utp_transfer_cmd_desc *ucdl_base_addr;975struct utp_transfer_req_desc *utrdl_base_addr;976struct utp_task_req_desc *utmrdl_base_addr;977978/* DMA memory reference */979dma_addr_t ucdl_dma_addr;980dma_addr_t utrdl_dma_addr;981dma_addr_t utmrdl_dma_addr;982983struct Scsi_Host *host;984struct device *dev;985struct scsi_device *ufs_device_wlun;986987#ifdef CONFIG_SCSI_UFS_HWMON988struct device *hwmon_device;989#endif990991enum ufs_dev_pwr_mode curr_dev_pwr_mode;992enum uic_link_state uic_link_state;993/* Desired UFS power management level during runtime PM */994enum ufs_pm_level rpm_lvl;995/* Desired UFS power management level during system PM */996enum ufs_pm_level spm_lvl;997int pm_op_in_progress;998999/* Auto-Hibernate Idle Timer register value */1000u32 ahit;10011002struct ufshcd_lrb *lrb;10031004unsigned long outstanding_tasks;1005spinlock_t outstanding_lock;1006unsigned long outstanding_reqs;10071008u32 capabilities;1009int nutrs;1010int nortt;1011u32 mcq_capabilities;1012int nutmrs;1013u32 reserved_slot;1014u32 ufs_version;1015const struct ufs_hba_variant_ops *vops;1016struct ufs_hba_variant_params *vps;1017void *priv;1018#ifdef CONFIG_SCSI_UFS_VARIABLE_SG_ENTRY_SIZE1019size_t sg_entry_size;1020#endif1021unsigned int irq;1022bool is_irq_enabled;1023enum ufs_ref_clk_freq dev_ref_clk_freq;10241025unsigned int quirks; /* Deviations from standard UFSHCI spec. */10261027/* Device deviations from standard UFS device spec. */1028unsigned int dev_quirks;10291030struct blk_mq_tag_set tmf_tag_set;1031struct request_queue *tmf_queue;1032struct request **tmf_rqs;10331034struct uic_command *active_uic_cmd;1035struct mutex uic_cmd_mutex;1036struct completion *uic_async_done;10371038enum ufshcd_state ufshcd_state;1039u32 eh_flags;1040u32 intr_mask;1041u16 ee_ctrl_mask;1042u16 ee_drv_mask;1043u16 ee_usr_mask;1044struct mutex ee_ctrl_mutex;1045bool is_powered;1046bool shutting_down;1047struct semaphore host_sem;10481049/* Work Queues */1050struct workqueue_struct *eh_wq;1051struct work_struct eh_work;1052struct work_struct eeh_work;10531054/* HBA Errors */1055u32 errors;1056u32 uic_error;1057u32 saved_err;1058u32 saved_uic_err;1059struct ufs_stats ufs_stats;1060bool force_reset;1061bool force_pmc;1062bool silence_err_logs;10631064/* Device management request data */1065struct ufs_dev_cmd dev_cmd;1066ktime_t last_dme_cmd_tstamp;1067int nop_out_timeout;10681069/* Keeps information of the UFS device connected to this host */1070struct ufs_dev_info dev_info;1071bool auto_bkops_enabled;1072struct ufs_vreg_info vreg_info;1073struct list_head clk_list_head;1074bool use_pm_opp;10751076/* Number of requests aborts */1077int req_abort_count;10781079/* Number of lanes available (1 or 2) for Rx/Tx */1080u32 lanes_per_direction;1081struct ufs_pa_layer_attr pwr_info;1082struct ufs_pwr_mode_info max_pwr_info;10831084struct ufs_clk_gating clk_gating;1085/* Control to enable/disable host capabilities */1086u32 caps;10871088struct devfreq *devfreq;1089struct ufs_clk_scaling clk_scaling;1090bool system_suspending;1091bool is_sys_suspended;10921093enum bkops_status urgent_bkops_lvl;1094bool is_urgent_bkops_lvl_checked;10951096struct mutex wb_mutex;1097struct rw_semaphore clk_scaling_lock;10981099struct device bsg_dev;1100struct request_queue *bsg_queue;1101struct delayed_work rpm_dev_flush_recheck_work;11021103struct ufs_hba_monitor monitor;11041105#ifdef CONFIG_SCSI_UFS_CRYPTO1106union ufs_crypto_capabilities crypto_capabilities;1107union ufs_crypto_cap_entry *crypto_cap_array;1108u32 crypto_cfg_register;1109struct blk_crypto_profile crypto_profile;1110#endif1111#ifdef CONFIG_DEBUG_FS1112struct dentry *debugfs_root;1113struct delayed_work debugfs_ee_work;1114u32 debugfs_ee_rate_limit_ms;1115#endif1116#ifdef CONFIG_SCSI_UFS_FAULT_INJECTION1117struct fault_attr trigger_eh_attr;1118struct fault_attr timeout_attr;1119#endif1120u32 luns_avail;1121unsigned int nr_hw_queues;1122unsigned int nr_queues[HCTX_MAX_TYPES];1123bool complete_put;1124bool scsi_host_added;1125bool mcq_sup;1126bool lsdb_sup;1127bool mcq_enabled;1128bool mcq_esi_enabled;1129struct ufshcd_res_info res[RES_MAX];1130void __iomem *mcq_base;1131struct ufs_hw_queue *uhq;1132struct ufs_hw_queue *dev_cmd_queue;1133struct ufshcd_mcq_opr_info_t mcq_opr[OPR_MAX];11341135struct delayed_work ufs_rtc_update_work;1136struct pm_qos_request pm_qos_req;1137bool pm_qos_enabled;11381139int critical_health_count;1140atomic_t dev_lvl_exception_count;1141u64 dev_lvl_exception_id;1142};11431144/**1145* struct ufs_hw_queue - per hardware queue structure1146* @mcq_sq_head: base address of submission queue head pointer1147* @mcq_sq_tail: base address of submission queue tail pointer1148* @mcq_cq_head: base address of completion queue head pointer1149* @mcq_cq_tail: base address of completion queue tail pointer1150* @sqe_base_addr: submission queue entry base address1151* @sqe_dma_addr: submission queue dma address1152* @cqe_base_addr: completion queue base address1153* @cqe_dma_addr: completion queue dma address1154* @max_entries: max number of slots in this hardware queue1155* @id: hardware queue ID1156* @sq_tp_slot: current slot to which SQ tail pointer is pointing1157* @sq_lock: serialize submission queue access1158* @cq_tail_slot: current slot to which CQ tail pointer is pointing1159* @cq_head_slot: current slot to which CQ head pointer is pointing1160* @cq_lock: Synchronize between multiple polling instances1161* @sq_mutex: prevent submission queue concurrent access1162*/1163struct ufs_hw_queue {1164void __iomem *mcq_sq_head;1165void __iomem *mcq_sq_tail;1166void __iomem *mcq_cq_head;1167void __iomem *mcq_cq_tail;11681169struct utp_transfer_req_desc *sqe_base_addr;1170dma_addr_t sqe_dma_addr;1171struct cq_entry *cqe_base_addr;1172dma_addr_t cqe_dma_addr;1173u32 max_entries;1174u32 id;1175u32 sq_tail_slot;1176spinlock_t sq_lock;1177u32 cq_tail_slot;1178u32 cq_head_slot;1179spinlock_t cq_lock;1180/* prevent concurrent access to submission queue */1181struct mutex sq_mutex;1182};11831184#define MCQ_QCFG_SIZE 0x4011851186static inline unsigned int ufshcd_mcq_opr_offset(struct ufs_hba *hba,1187enum ufshcd_mcq_opr opr, int idx)1188{1189return hba->mcq_opr[opr].offset + hba->mcq_opr[opr].stride * idx;1190}11911192static inline unsigned int ufshcd_mcq_cfg_offset(unsigned int reg, int idx)1193{1194return reg + MCQ_QCFG_SIZE * idx;1195}11961197#ifdef CONFIG_SCSI_UFS_VARIABLE_SG_ENTRY_SIZE1198static inline size_t ufshcd_sg_entry_size(const struct ufs_hba *hba)1199{1200return hba->sg_entry_size;1201}12021203static inline void ufshcd_set_sg_entry_size(struct ufs_hba *hba, size_t sg_entry_size)1204{1205WARN_ON_ONCE(sg_entry_size < sizeof(struct ufshcd_sg_entry));1206hba->sg_entry_size = sg_entry_size;1207}1208#else1209static inline size_t ufshcd_sg_entry_size(const struct ufs_hba *hba)1210{1211return sizeof(struct ufshcd_sg_entry);1212}12131214#define ufshcd_set_sg_entry_size(hba, sg_entry_size) \1215({ (void)(hba); BUILD_BUG_ON(sg_entry_size != sizeof(struct ufshcd_sg_entry)); })1216#endif12171218#ifdef CONFIG_SCSI_UFS_CRYPTO1219static inline struct ufs_hba *1220ufs_hba_from_crypto_profile(struct blk_crypto_profile *profile)1221{1222return container_of(profile, struct ufs_hba, crypto_profile);1223}1224#endif12251226static inline size_t ufshcd_get_ucd_size(const struct ufs_hba *hba)1227{1228return sizeof(struct utp_transfer_cmd_desc) + SG_ALL * ufshcd_sg_entry_size(hba);1229}12301231/* Returns true if clocks can be gated. Otherwise false */1232static inline bool ufshcd_is_clkgating_allowed(struct ufs_hba *hba)1233{1234return hba->caps & UFSHCD_CAP_CLK_GATING;1235}1236static inline bool ufshcd_can_hibern8_during_gating(struct ufs_hba *hba)1237{1238return hba->caps & UFSHCD_CAP_HIBERN8_WITH_CLK_GATING;1239}1240static inline int ufshcd_is_clkscaling_supported(struct ufs_hba *hba)1241{1242return hba->caps & UFSHCD_CAP_CLK_SCALING;1243}1244static inline bool ufshcd_can_autobkops_during_suspend(struct ufs_hba *hba)1245{1246return hba->caps & UFSHCD_CAP_AUTO_BKOPS_SUSPEND;1247}1248static inline bool ufshcd_is_rpm_autosuspend_allowed(struct ufs_hba *hba)1249{1250return hba->caps & UFSHCD_CAP_RPM_AUTOSUSPEND;1251}12521253static inline bool ufshcd_is_intr_aggr_allowed(struct ufs_hba *hba)1254{1255return (hba->caps & UFSHCD_CAP_INTR_AGGR) &&1256!(hba->quirks & UFSHCD_QUIRK_BROKEN_INTR_AGGR);1257}12581259static inline bool ufshcd_can_aggressive_pc(struct ufs_hba *hba)1260{1261return !!(ufshcd_is_link_hibern8(hba) &&1262(hba->caps & UFSHCD_CAP_AGGR_POWER_COLLAPSE));1263}12641265static inline bool ufshcd_is_auto_hibern8_supported(struct ufs_hba *hba)1266{1267return (hba->capabilities & MASK_AUTO_HIBERN8_SUPPORT) &&1268!(hba->quirks & UFSHCD_QUIRK_BROKEN_AUTO_HIBERN8);1269}12701271static inline bool ufshcd_is_auto_hibern8_enabled(struct ufs_hba *hba)1272{1273return FIELD_GET(UFSHCI_AHIBERN8_TIMER_MASK, hba->ahit);1274}12751276static inline bool ufshcd_is_wb_allowed(struct ufs_hba *hba)1277{1278return hba->caps & UFSHCD_CAP_WB_EN;1279}12801281static inline bool ufshcd_enable_wb_if_scaling_up(struct ufs_hba *hba)1282{1283return hba->caps & UFSHCD_CAP_WB_WITH_CLK_SCALING;1284}12851286#define ufsmcq_writel(hba, val, reg) \1287writel((val), (hba)->mcq_base + (reg))1288#define ufsmcq_readl(hba, reg) \1289readl((hba)->mcq_base + (reg))12901291#define ufsmcq_writelx(hba, val, reg) \1292writel_relaxed((val), (hba)->mcq_base + (reg))1293#define ufsmcq_readlx(hba, reg) \1294readl_relaxed((hba)->mcq_base + (reg))12951296#define ufshcd_writel(hba, val, reg) \1297writel((val), (hba)->mmio_base + (reg))1298#define ufshcd_readl(hba, reg) \1299readl((hba)->mmio_base + (reg))13001301/**1302* ufshcd_rmwl - perform read/modify/write for a controller register1303* @hba: per adapter instance1304* @mask: mask to apply on read value1305* @val: actual value to write1306* @reg: register address1307*/1308static inline void ufshcd_rmwl(struct ufs_hba *hba, u32 mask, u32 val, u32 reg)1309{1310u32 tmp;13111312tmp = ufshcd_readl(hba, reg);1313tmp &= ~mask;1314tmp |= (val & mask);1315ufshcd_writel(hba, tmp, reg);1316}13171318void ufshcd_enable_irq(struct ufs_hba *hba);1319void ufshcd_disable_irq(struct ufs_hba *hba);1320int ufshcd_alloc_host(struct device *, struct ufs_hba **);1321int ufshcd_hba_enable(struct ufs_hba *hba);1322int ufshcd_init(struct ufs_hba *, void __iomem *, unsigned int);1323int ufshcd_link_recovery(struct ufs_hba *hba);1324int ufshcd_make_hba_operational(struct ufs_hba *hba);1325void ufshcd_remove(struct ufs_hba *);1326int ufshcd_uic_hibern8_enter(struct ufs_hba *hba);1327int ufshcd_uic_hibern8_exit(struct ufs_hba *hba);1328void ufshcd_delay_us(unsigned long us, unsigned long tolerance);1329void ufshcd_parse_dev_ref_clk_freq(struct ufs_hba *hba, struct clk *refclk);1330void ufshcd_update_evt_hist(struct ufs_hba *hba, u32 id, u32 val);1331void ufshcd_hba_stop(struct ufs_hba *hba);1332void ufshcd_schedule_eh_work(struct ufs_hba *hba);1333void ufshcd_mcq_config_mac(struct ufs_hba *hba, u32 max_active_cmds);1334unsigned int ufshcd_mcq_queue_cfg_addr(struct ufs_hba *hba);1335u32 ufshcd_mcq_read_cqis(struct ufs_hba *hba, int i);1336void ufshcd_mcq_write_cqis(struct ufs_hba *hba, u32 val, int i);1337unsigned long ufshcd_mcq_poll_cqe_lock(struct ufs_hba *hba,1338struct ufs_hw_queue *hwq);1339void ufshcd_mcq_make_queues_operational(struct ufs_hba *hba);1340void ufshcd_mcq_enable(struct ufs_hba *hba);1341void ufshcd_mcq_enable_esi(struct ufs_hba *hba);1342void ufshcd_mcq_config_esi(struct ufs_hba *hba, struct msi_msg *msg);13431344int ufshcd_opp_config_clks(struct device *dev, struct opp_table *opp_table,1345struct dev_pm_opp *opp, void *data,1346bool scaling_down);1347/**1348* ufshcd_set_variant - set variant specific data to the hba1349* @hba: per adapter instance1350* @variant: pointer to variant specific data1351*/1352static inline void ufshcd_set_variant(struct ufs_hba *hba, void *variant)1353{1354BUG_ON(!hba);1355hba->priv = variant;1356}13571358/**1359* ufshcd_get_variant - get variant specific data from the hba1360* @hba: per adapter instance1361*/1362static inline void *ufshcd_get_variant(struct ufs_hba *hba)1363{1364BUG_ON(!hba);1365return hba->priv;1366}13671368#ifdef CONFIG_PM1369extern int ufshcd_runtime_suspend(struct device *dev);1370extern int ufshcd_runtime_resume(struct device *dev);1371#endif1372#ifdef CONFIG_PM_SLEEP1373extern int ufshcd_system_suspend(struct device *dev);1374extern int ufshcd_system_resume(struct device *dev);1375extern int ufshcd_system_freeze(struct device *dev);1376extern int ufshcd_system_thaw(struct device *dev);1377extern int ufshcd_system_restore(struct device *dev);1378#endif13791380extern int ufshcd_dme_reset(struct ufs_hba *hba);1381extern int ufshcd_dme_enable(struct ufs_hba *hba);1382extern int ufshcd_dme_configure_adapt(struct ufs_hba *hba,1383int agreed_gear,1384int adapt_val);1385extern int ufshcd_dme_set_attr(struct ufs_hba *hba, u32 attr_sel,1386u8 attr_set, u32 mib_val, u8 peer);1387extern int ufshcd_dme_get_attr(struct ufs_hba *hba, u32 attr_sel,1388u32 *mib_val, u8 peer);1389extern int ufshcd_config_pwr_mode(struct ufs_hba *hba,1390struct ufs_pa_layer_attr *desired_pwr_mode);1391extern int ufshcd_uic_change_pwr_mode(struct ufs_hba *hba, u8 mode);13921393/* UIC command interfaces for DME primitives */1394#define DME_LOCAL 01395#define DME_PEER 11396#define ATTR_SET_NOR 0 /* NORMAL */1397#define ATTR_SET_ST 1 /* STATIC */13981399static inline int ufshcd_dme_set(struct ufs_hba *hba, u32 attr_sel,1400u32 mib_val)1401{1402return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,1403mib_val, DME_LOCAL);1404}14051406static inline int ufshcd_dme_st_set(struct ufs_hba *hba, u32 attr_sel,1407u32 mib_val)1408{1409return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,1410mib_val, DME_LOCAL);1411}14121413static inline int ufshcd_dme_peer_set(struct ufs_hba *hba, u32 attr_sel,1414u32 mib_val)1415{1416return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_NOR,1417mib_val, DME_PEER);1418}14191420static inline int ufshcd_dme_peer_st_set(struct ufs_hba *hba, u32 attr_sel,1421u32 mib_val)1422{1423return ufshcd_dme_set_attr(hba, attr_sel, ATTR_SET_ST,1424mib_val, DME_PEER);1425}14261427static inline int ufshcd_dme_get(struct ufs_hba *hba,1428u32 attr_sel, u32 *mib_val)1429{1430return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_LOCAL);1431}14321433static inline int ufshcd_dme_peer_get(struct ufs_hba *hba,1434u32 attr_sel, u32 *mib_val)1435{1436return ufshcd_dme_get_attr(hba, attr_sel, mib_val, DME_PEER);1437}14381439static inline bool ufshcd_is_hs_mode(const struct ufs_pa_layer_attr *pwr_info)1440{1441return (pwr_info->pwr_rx == FAST_MODE ||1442pwr_info->pwr_rx == FASTAUTO_MODE) &&1443(pwr_info->pwr_tx == FAST_MODE ||1444pwr_info->pwr_tx == FASTAUTO_MODE);1445}14461447static inline int ufshcd_disable_host_tx_lcc(struct ufs_hba *hba)1448{1449return ufshcd_dme_set(hba, UIC_ARG_MIB(PA_LOCAL_TX_LCC_ENABLE), 0);1450}14511452void ufshcd_auto_hibern8_update(struct ufs_hba *hba, u32 ahit);1453void ufshcd_fixup_dev_quirks(struct ufs_hba *hba,1454const struct ufs_dev_quirk *fixups);1455#define SD_ASCII_STD true1456#define SD_RAW false1457int ufshcd_read_string_desc(struct ufs_hba *hba, u8 desc_index,1458u8 **buf, bool ascii);14591460void ufshcd_hold(struct ufs_hba *hba);1461void ufshcd_release(struct ufs_hba *hba);14621463void ufshcd_clkgate_delay_set(struct device *dev, unsigned long value);14641465int ufshcd_get_vreg(struct device *dev, struct ufs_vreg *vreg);14661467int ufshcd_send_uic_cmd(struct ufs_hba *hba, struct uic_command *uic_cmd);14681469int ufshcd_advanced_rpmb_req_handler(struct ufs_hba *hba, struct utp_upiu_req *req_upiu,1470struct utp_upiu_req *rsp_upiu, struct ufs_ehs *ehs_req,1471struct ufs_ehs *ehs_rsp, int sg_cnt,1472struct scatterlist *sg_list, enum dma_data_direction dir);1473int ufshcd_wb_toggle(struct ufs_hba *hba, bool enable);1474int ufshcd_wb_toggle_buf_flush(struct ufs_hba *hba, bool enable);1475int ufshcd_wb_set_resize_en(struct ufs_hba *hba, enum wb_resize_en en_mode);1476int ufshcd_suspend_prepare(struct device *dev);1477int __ufshcd_suspend_prepare(struct device *dev, bool rpm_ok_for_spm);1478void ufshcd_resume_complete(struct device *dev);1479bool ufshcd_is_hba_active(struct ufs_hba *hba);1480void ufshcd_pm_qos_init(struct ufs_hba *hba);1481void ufshcd_pm_qos_exit(struct ufs_hba *hba);1482int ufshcd_dme_rmw(struct ufs_hba *hba, u32 mask, u32 val, u32 attr);14831484/* Wrapper functions for safely calling variant operations */1485static inline int ufshcd_vops_init(struct ufs_hba *hba)1486{1487if (hba->vops && hba->vops->init)1488return hba->vops->init(hba);14891490return 0;1491}14921493static inline int ufshcd_vops_phy_initialization(struct ufs_hba *hba)1494{1495if (hba->vops && hba->vops->phy_initialization)1496return hba->vops->phy_initialization(hba);14971498return 0;1499}15001501extern const struct ufs_pm_lvl_states ufs_pm_lvl_states[];15021503int ufshcd_dump_regs(struct ufs_hba *hba, size_t offset, size_t len,1504const char *prefix);15051506int __ufshcd_write_ee_control(struct ufs_hba *hba, u32 ee_ctrl_mask);1507int ufshcd_write_ee_control(struct ufs_hba *hba);1508int ufshcd_update_ee_control(struct ufs_hba *hba, u16 *mask,1509const u16 *other_mask, u16 set, u16 clr);15101511#endif /* End of Header */151215131514